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/cgroup.h>
38 #include <linux/perf_event.h>
39 #include <linux/ftrace_event.h>
40 #include <linux/hw_breakpoint.h>
41 #include <linux/mm_types.h>
42 #include <linux/module.h>
43 #include <linux/mman.h>
44 #include <linux/compat.h>
48 #include <asm/irq_regs.h>
50 static struct workqueue_struct
*perf_wq
;
52 struct remote_function_call
{
53 struct task_struct
*p
;
54 int (*func
)(void *info
);
59 static void remote_function(void *data
)
61 struct remote_function_call
*tfc
= data
;
62 struct task_struct
*p
= tfc
->p
;
66 if (task_cpu(p
) != smp_processor_id() || !task_curr(p
))
70 tfc
->ret
= tfc
->func(tfc
->info
);
74 * task_function_call - call a function on the cpu on which a task runs
75 * @p: the task to evaluate
76 * @func: the function to be called
77 * @info: the function call argument
79 * Calls the function @func when the task is currently running. This might
80 * be on the current CPU, which just calls the function directly
82 * returns: @func return value, or
83 * -ESRCH - when the process isn't running
84 * -EAGAIN - when the process moved away
87 task_function_call(struct task_struct
*p
, int (*func
) (void *info
), void *info
)
89 struct remote_function_call data
= {
93 .ret
= -ESRCH
, /* No such (running) process */
97 smp_call_function_single(task_cpu(p
), remote_function
, &data
, 1);
103 * cpu_function_call - call a function on the cpu
104 * @func: the function to be called
105 * @info: the function call argument
107 * Calls the function @func on the remote cpu.
109 * returns: @func return value or -ENXIO when the cpu is offline
111 static int cpu_function_call(int cpu
, int (*func
) (void *info
), void *info
)
113 struct remote_function_call data
= {
117 .ret
= -ENXIO
, /* No such CPU */
120 smp_call_function_single(cpu
, remote_function
, &data
, 1);
125 #define EVENT_OWNER_KERNEL ((void *) -1)
127 static bool is_kernel_event(struct perf_event
*event
)
129 return event
->owner
== EVENT_OWNER_KERNEL
;
132 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
133 PERF_FLAG_FD_OUTPUT |\
134 PERF_FLAG_PID_CGROUP |\
135 PERF_FLAG_FD_CLOEXEC)
138 * branch priv levels that need permission checks
140 #define PERF_SAMPLE_BRANCH_PERM_PLM \
141 (PERF_SAMPLE_BRANCH_KERNEL |\
142 PERF_SAMPLE_BRANCH_HV)
145 EVENT_FLEXIBLE
= 0x1,
147 EVENT_ALL
= EVENT_FLEXIBLE
| EVENT_PINNED
,
151 * perf_sched_events : >0 events exist
152 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
154 struct static_key_deferred perf_sched_events __read_mostly
;
155 static DEFINE_PER_CPU(atomic_t
, perf_cgroup_events
);
156 static DEFINE_PER_CPU(int, perf_sched_cb_usages
);
158 static atomic_t nr_mmap_events __read_mostly
;
159 static atomic_t nr_comm_events __read_mostly
;
160 static atomic_t nr_task_events __read_mostly
;
161 static atomic_t nr_freq_events __read_mostly
;
163 static LIST_HEAD(pmus
);
164 static DEFINE_MUTEX(pmus_lock
);
165 static struct srcu_struct pmus_srcu
;
168 * perf event paranoia level:
169 * -1 - not paranoid at all
170 * 0 - disallow raw tracepoint access for unpriv
171 * 1 - disallow cpu events for unpriv
172 * 2 - disallow kernel profiling for unpriv
174 int sysctl_perf_event_paranoid __read_mostly
= 1;
176 /* Minimum for 512 kiB + 1 user control page */
177 int sysctl_perf_event_mlock __read_mostly
= 512 + (PAGE_SIZE
/ 1024); /* 'free' kiB per user */
180 * max perf event sample rate
182 #define DEFAULT_MAX_SAMPLE_RATE 100000
183 #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
184 #define DEFAULT_CPU_TIME_MAX_PERCENT 25
186 int sysctl_perf_event_sample_rate __read_mostly
= DEFAULT_MAX_SAMPLE_RATE
;
188 static int max_samples_per_tick __read_mostly
= DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE
, HZ
);
189 static int perf_sample_period_ns __read_mostly
= DEFAULT_SAMPLE_PERIOD_NS
;
191 static int perf_sample_allowed_ns __read_mostly
=
192 DEFAULT_SAMPLE_PERIOD_NS
* DEFAULT_CPU_TIME_MAX_PERCENT
/ 100;
194 void update_perf_cpu_limits(void)
196 u64 tmp
= perf_sample_period_ns
;
198 tmp
*= sysctl_perf_cpu_time_max_percent
;
200 ACCESS_ONCE(perf_sample_allowed_ns
) = tmp
;
203 static int perf_rotate_context(struct perf_cpu_context
*cpuctx
);
205 int perf_proc_update_handler(struct ctl_table
*table
, int write
,
206 void __user
*buffer
, size_t *lenp
,
209 int ret
= proc_dointvec_minmax(table
, write
, buffer
, lenp
, ppos
);
214 max_samples_per_tick
= DIV_ROUND_UP(sysctl_perf_event_sample_rate
, HZ
);
215 perf_sample_period_ns
= NSEC_PER_SEC
/ sysctl_perf_event_sample_rate
;
216 update_perf_cpu_limits();
221 int sysctl_perf_cpu_time_max_percent __read_mostly
= DEFAULT_CPU_TIME_MAX_PERCENT
;
223 int perf_cpu_time_max_percent_handler(struct ctl_table
*table
, int write
,
224 void __user
*buffer
, size_t *lenp
,
227 int ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
232 update_perf_cpu_limits();
238 * perf samples are done in some very critical code paths (NMIs).
239 * If they take too much CPU time, the system can lock up and not
240 * get any real work done. This will drop the sample rate when
241 * we detect that events are taking too long.
243 #define NR_ACCUMULATED_SAMPLES 128
244 static DEFINE_PER_CPU(u64
, running_sample_length
);
246 static void perf_duration_warn(struct irq_work
*w
)
248 u64 allowed_ns
= ACCESS_ONCE(perf_sample_allowed_ns
);
249 u64 avg_local_sample_len
;
250 u64 local_samples_len
;
252 local_samples_len
= __this_cpu_read(running_sample_length
);
253 avg_local_sample_len
= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
255 printk_ratelimited(KERN_WARNING
256 "perf interrupt took too long (%lld > %lld), lowering "
257 "kernel.perf_event_max_sample_rate to %d\n",
258 avg_local_sample_len
, allowed_ns
>> 1,
259 sysctl_perf_event_sample_rate
);
262 static DEFINE_IRQ_WORK(perf_duration_work
, perf_duration_warn
);
264 void perf_sample_event_took(u64 sample_len_ns
)
266 u64 allowed_ns
= ACCESS_ONCE(perf_sample_allowed_ns
);
267 u64 avg_local_sample_len
;
268 u64 local_samples_len
;
273 /* decay the counter by 1 average sample */
274 local_samples_len
= __this_cpu_read(running_sample_length
);
275 local_samples_len
-= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
276 local_samples_len
+= sample_len_ns
;
277 __this_cpu_write(running_sample_length
, local_samples_len
);
280 * note: this will be biased artifically low until we have
281 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
282 * from having to maintain a count.
284 avg_local_sample_len
= local_samples_len
/NR_ACCUMULATED_SAMPLES
;
286 if (avg_local_sample_len
<= allowed_ns
)
289 if (max_samples_per_tick
<= 1)
292 max_samples_per_tick
= DIV_ROUND_UP(max_samples_per_tick
, 2);
293 sysctl_perf_event_sample_rate
= max_samples_per_tick
* HZ
;
294 perf_sample_period_ns
= NSEC_PER_SEC
/ sysctl_perf_event_sample_rate
;
296 update_perf_cpu_limits();
298 if (!irq_work_queue(&perf_duration_work
)) {
299 early_printk("perf interrupt took too long (%lld > %lld), lowering "
300 "kernel.perf_event_max_sample_rate to %d\n",
301 avg_local_sample_len
, allowed_ns
>> 1,
302 sysctl_perf_event_sample_rate
);
306 static atomic64_t perf_event_id
;
308 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
309 enum event_type_t event_type
);
311 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
312 enum event_type_t event_type
,
313 struct task_struct
*task
);
315 static void update_context_time(struct perf_event_context
*ctx
);
316 static u64
perf_event_time(struct perf_event
*event
);
318 void __weak
perf_event_print_debug(void) { }
320 extern __weak
const char *perf_pmu_name(void)
325 static inline u64
perf_clock(void)
327 return local_clock();
330 static inline struct perf_cpu_context
*
331 __get_cpu_context(struct perf_event_context
*ctx
)
333 return this_cpu_ptr(ctx
->pmu
->pmu_cpu_context
);
336 static void perf_ctx_lock(struct perf_cpu_context
*cpuctx
,
337 struct perf_event_context
*ctx
)
339 raw_spin_lock(&cpuctx
->ctx
.lock
);
341 raw_spin_lock(&ctx
->lock
);
344 static void perf_ctx_unlock(struct perf_cpu_context
*cpuctx
,
345 struct perf_event_context
*ctx
)
348 raw_spin_unlock(&ctx
->lock
);
349 raw_spin_unlock(&cpuctx
->ctx
.lock
);
352 #ifdef CONFIG_CGROUP_PERF
355 perf_cgroup_match(struct perf_event
*event
)
357 struct perf_event_context
*ctx
= event
->ctx
;
358 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
360 /* @event doesn't care about cgroup */
364 /* wants specific cgroup scope but @cpuctx isn't associated with any */
369 * Cgroup scoping is recursive. An event enabled for a cgroup is
370 * also enabled for all its descendant cgroups. If @cpuctx's
371 * cgroup is a descendant of @event's (the test covers identity
372 * case), it's a match.
374 return cgroup_is_descendant(cpuctx
->cgrp
->css
.cgroup
,
375 event
->cgrp
->css
.cgroup
);
378 static inline void perf_detach_cgroup(struct perf_event
*event
)
380 css_put(&event
->cgrp
->css
);
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_online_from_dir(f
.file
->f_path
.dentry
,
592 &perf_event_cgrp_subsys
);
598 cgrp
= container_of(css
, struct perf_cgroup
, css
);
602 * all events in a group must monitor
603 * the same cgroup because a task belongs
604 * to only one perf cgroup at a time
606 if (group_leader
&& group_leader
->cgrp
!= cgrp
) {
607 perf_detach_cgroup(event
);
616 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
618 struct perf_cgroup_info
*t
;
619 t
= per_cpu_ptr(event
->cgrp
->info
, event
->cpu
);
620 event
->shadow_ctx_time
= now
- t
->timestamp
;
624 perf_cgroup_defer_enabled(struct perf_event
*event
)
627 * when the current task's perf cgroup does not match
628 * the event's, we need to remember to call the
629 * perf_mark_enable() function the first time a task with
630 * a matching perf cgroup is scheduled in.
632 if (is_cgroup_event(event
) && !perf_cgroup_match(event
))
633 event
->cgrp_defer_enabled
= 1;
637 perf_cgroup_mark_enabled(struct perf_event
*event
,
638 struct perf_event_context
*ctx
)
640 struct perf_event
*sub
;
641 u64 tstamp
= perf_event_time(event
);
643 if (!event
->cgrp_defer_enabled
)
646 event
->cgrp_defer_enabled
= 0;
648 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
649 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
650 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
) {
651 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
652 sub
->cgrp_defer_enabled
= 0;
656 #else /* !CONFIG_CGROUP_PERF */
659 perf_cgroup_match(struct perf_event
*event
)
664 static inline void perf_detach_cgroup(struct perf_event
*event
)
667 static inline int is_cgroup_event(struct perf_event
*event
)
672 static inline u64
perf_cgroup_event_cgrp_time(struct perf_event
*event
)
677 static inline void update_cgrp_time_from_event(struct perf_event
*event
)
681 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context
*cpuctx
)
685 static inline void perf_cgroup_sched_out(struct task_struct
*task
,
686 struct task_struct
*next
)
690 static inline void perf_cgroup_sched_in(struct task_struct
*prev
,
691 struct task_struct
*task
)
695 static inline int perf_cgroup_connect(pid_t pid
, struct perf_event
*event
,
696 struct perf_event_attr
*attr
,
697 struct perf_event
*group_leader
)
703 perf_cgroup_set_timestamp(struct task_struct
*task
,
704 struct perf_event_context
*ctx
)
709 perf_cgroup_switch(struct task_struct
*task
, struct task_struct
*next
)
714 perf_cgroup_set_shadow_time(struct perf_event
*event
, u64 now
)
718 static inline u64
perf_cgroup_event_time(struct perf_event
*event
)
724 perf_cgroup_defer_enabled(struct perf_event
*event
)
729 perf_cgroup_mark_enabled(struct perf_event
*event
,
730 struct perf_event_context
*ctx
)
736 * set default to be dependent on timer tick just
739 #define PERF_CPU_HRTIMER (1000 / HZ)
741 * function must be called with interrupts disbled
743 static enum hrtimer_restart
perf_cpu_hrtimer_handler(struct hrtimer
*hr
)
745 struct perf_cpu_context
*cpuctx
;
746 enum hrtimer_restart ret
= HRTIMER_NORESTART
;
749 WARN_ON(!irqs_disabled());
751 cpuctx
= container_of(hr
, struct perf_cpu_context
, hrtimer
);
753 rotations
= perf_rotate_context(cpuctx
);
756 * arm timer if needed
759 hrtimer_forward_now(hr
, cpuctx
->hrtimer_interval
);
760 ret
= HRTIMER_RESTART
;
766 /* CPU is going down */
767 void perf_cpu_hrtimer_cancel(int cpu
)
769 struct perf_cpu_context
*cpuctx
;
773 if (WARN_ON(cpu
!= smp_processor_id()))
776 local_irq_save(flags
);
780 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
781 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
783 if (pmu
->task_ctx_nr
== perf_sw_context
)
786 hrtimer_cancel(&cpuctx
->hrtimer
);
791 local_irq_restore(flags
);
794 static void __perf_cpu_hrtimer_init(struct perf_cpu_context
*cpuctx
, int cpu
)
796 struct hrtimer
*hr
= &cpuctx
->hrtimer
;
797 struct pmu
*pmu
= cpuctx
->ctx
.pmu
;
800 /* no multiplexing needed for SW PMU */
801 if (pmu
->task_ctx_nr
== perf_sw_context
)
805 * check default is sane, if not set then force to
806 * default interval (1/tick)
808 timer
= pmu
->hrtimer_interval_ms
;
810 timer
= pmu
->hrtimer_interval_ms
= PERF_CPU_HRTIMER
;
812 cpuctx
->hrtimer_interval
= ns_to_ktime(NSEC_PER_MSEC
* timer
);
814 hrtimer_init(hr
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL_PINNED
);
815 hr
->function
= perf_cpu_hrtimer_handler
;
818 static void perf_cpu_hrtimer_restart(struct perf_cpu_context
*cpuctx
)
820 struct hrtimer
*hr
= &cpuctx
->hrtimer
;
821 struct pmu
*pmu
= cpuctx
->ctx
.pmu
;
824 if (pmu
->task_ctx_nr
== perf_sw_context
)
827 if (hrtimer_active(hr
))
830 if (!hrtimer_callback_running(hr
))
831 __hrtimer_start_range_ns(hr
, cpuctx
->hrtimer_interval
,
832 0, HRTIMER_MODE_REL_PINNED
, 0);
835 void perf_pmu_disable(struct pmu
*pmu
)
837 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
839 pmu
->pmu_disable(pmu
);
842 void perf_pmu_enable(struct pmu
*pmu
)
844 int *count
= this_cpu_ptr(pmu
->pmu_disable_count
);
846 pmu
->pmu_enable(pmu
);
849 static DEFINE_PER_CPU(struct list_head
, active_ctx_list
);
852 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
853 * perf_event_task_tick() are fully serialized because they're strictly cpu
854 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
855 * disabled, while perf_event_task_tick is called from IRQ context.
857 static void perf_event_ctx_activate(struct perf_event_context
*ctx
)
859 struct list_head
*head
= this_cpu_ptr(&active_ctx_list
);
861 WARN_ON(!irqs_disabled());
863 WARN_ON(!list_empty(&ctx
->active_ctx_list
));
865 list_add(&ctx
->active_ctx_list
, head
);
868 static void perf_event_ctx_deactivate(struct perf_event_context
*ctx
)
870 WARN_ON(!irqs_disabled());
872 WARN_ON(list_empty(&ctx
->active_ctx_list
));
874 list_del_init(&ctx
->active_ctx_list
);
877 static void get_ctx(struct perf_event_context
*ctx
)
879 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
882 static void free_ctx(struct rcu_head
*head
)
884 struct perf_event_context
*ctx
;
886 ctx
= container_of(head
, struct perf_event_context
, rcu_head
);
887 kfree(ctx
->task_ctx_data
);
891 static void put_ctx(struct perf_event_context
*ctx
)
893 if (atomic_dec_and_test(&ctx
->refcount
)) {
895 put_ctx(ctx
->parent_ctx
);
897 put_task_struct(ctx
->task
);
898 call_rcu(&ctx
->rcu_head
, free_ctx
);
903 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
904 * perf_pmu_migrate_context() we need some magic.
906 * Those places that change perf_event::ctx will hold both
907 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
909 * Lock ordering is by mutex address. There is one other site where
910 * perf_event_context::mutex nests and that is put_event(). But remember that
911 * that is a parent<->child context relation, and migration does not affect
912 * children, therefore these two orderings should not interact.
914 * The change in perf_event::ctx does not affect children (as claimed above)
915 * because the sys_perf_event_open() case will install a new event and break
916 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
917 * concerned with cpuctx and that doesn't have children.
919 * The places that change perf_event::ctx will issue:
921 * perf_remove_from_context();
923 * perf_install_in_context();
925 * to affect the change. The remove_from_context() + synchronize_rcu() should
926 * quiesce the event, after which we can install it in the new location. This
927 * means that only external vectors (perf_fops, prctl) can perturb the event
928 * while in transit. Therefore all such accessors should also acquire
929 * perf_event_context::mutex to serialize against this.
931 * However; because event->ctx can change while we're waiting to acquire
932 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
936 * task_struct::perf_event_mutex
937 * perf_event_context::mutex
938 * perf_event_context::lock
939 * perf_event::child_mutex;
940 * perf_event::mmap_mutex
943 static struct perf_event_context
*
944 perf_event_ctx_lock_nested(struct perf_event
*event
, int nesting
)
946 struct perf_event_context
*ctx
;
950 ctx
= ACCESS_ONCE(event
->ctx
);
951 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
957 mutex_lock_nested(&ctx
->mutex
, nesting
);
958 if (event
->ctx
!= ctx
) {
959 mutex_unlock(&ctx
->mutex
);
967 static inline struct perf_event_context
*
968 perf_event_ctx_lock(struct perf_event
*event
)
970 return perf_event_ctx_lock_nested(event
, 0);
973 static void perf_event_ctx_unlock(struct perf_event
*event
,
974 struct perf_event_context
*ctx
)
976 mutex_unlock(&ctx
->mutex
);
981 * This must be done under the ctx->lock, such as to serialize against
982 * context_equiv(), therefore we cannot call put_ctx() since that might end up
983 * calling scheduler related locks and ctx->lock nests inside those.
985 static __must_check
struct perf_event_context
*
986 unclone_ctx(struct perf_event_context
*ctx
)
988 struct perf_event_context
*parent_ctx
= ctx
->parent_ctx
;
990 lockdep_assert_held(&ctx
->lock
);
993 ctx
->parent_ctx
= NULL
;
999 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
1002 * only top level events have the pid namespace they were created in
1005 event
= event
->parent
;
1007 return task_tgid_nr_ns(p
, event
->ns
);
1010 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
1013 * only top level events have the pid namespace they were created in
1016 event
= event
->parent
;
1018 return task_pid_nr_ns(p
, event
->ns
);
1022 * If we inherit events we want to return the parent event id
1025 static u64
primary_event_id(struct perf_event
*event
)
1030 id
= event
->parent
->id
;
1036 * Get the perf_event_context for a task and lock it.
1037 * This has to cope with with the fact that until it is locked,
1038 * the context could get moved to another task.
1040 static struct perf_event_context
*
1041 perf_lock_task_context(struct task_struct
*task
, int ctxn
, unsigned long *flags
)
1043 struct perf_event_context
*ctx
;
1047 * One of the few rules of preemptible RCU is that one cannot do
1048 * rcu_read_unlock() while holding a scheduler (or nested) lock when
1049 * part of the read side critical section was preemptible -- see
1050 * rcu_read_unlock_special().
1052 * Since ctx->lock nests under rq->lock we must ensure the entire read
1053 * side critical section is non-preemptible.
1057 ctx
= rcu_dereference(task
->perf_event_ctxp
[ctxn
]);
1060 * If this context is a clone of another, it might
1061 * get swapped for another underneath us by
1062 * perf_event_task_sched_out, though the
1063 * rcu_read_lock() protects us from any context
1064 * getting freed. Lock the context and check if it
1065 * got swapped before we could get the lock, and retry
1066 * if so. If we locked the right context, then it
1067 * can't get swapped on us any more.
1069 raw_spin_lock_irqsave(&ctx
->lock
, *flags
);
1070 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
[ctxn
])) {
1071 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
1077 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
1078 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
1088 * Get the context for a task and increment its pin_count so it
1089 * can't get swapped to another task. This also increments its
1090 * reference count so that the context can't get freed.
1092 static struct perf_event_context
*
1093 perf_pin_task_context(struct task_struct
*task
, int ctxn
)
1095 struct perf_event_context
*ctx
;
1096 unsigned long flags
;
1098 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
1101 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1106 static void perf_unpin_context(struct perf_event_context
*ctx
)
1108 unsigned long flags
;
1110 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
1112 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1116 * Update the record of the current time in a context.
1118 static void update_context_time(struct perf_event_context
*ctx
)
1120 u64 now
= perf_clock();
1122 ctx
->time
+= now
- ctx
->timestamp
;
1123 ctx
->timestamp
= now
;
1126 static u64
perf_event_time(struct perf_event
*event
)
1128 struct perf_event_context
*ctx
= event
->ctx
;
1130 if (is_cgroup_event(event
))
1131 return perf_cgroup_event_time(event
);
1133 return ctx
? ctx
->time
: 0;
1137 * Update the total_time_enabled and total_time_running fields for a event.
1138 * The caller of this function needs to hold the ctx->lock.
1140 static void update_event_times(struct perf_event
*event
)
1142 struct perf_event_context
*ctx
= event
->ctx
;
1145 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
1146 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
1149 * in cgroup mode, time_enabled represents
1150 * the time the event was enabled AND active
1151 * tasks were in the monitored cgroup. This is
1152 * independent of the activity of the context as
1153 * there may be a mix of cgroup and non-cgroup events.
1155 * That is why we treat cgroup events differently
1158 if (is_cgroup_event(event
))
1159 run_end
= perf_cgroup_event_time(event
);
1160 else if (ctx
->is_active
)
1161 run_end
= ctx
->time
;
1163 run_end
= event
->tstamp_stopped
;
1165 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
1167 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
1168 run_end
= event
->tstamp_stopped
;
1170 run_end
= perf_event_time(event
);
1172 event
->total_time_running
= run_end
- event
->tstamp_running
;
1177 * Update total_time_enabled and total_time_running for all events in a group.
1179 static void update_group_times(struct perf_event
*leader
)
1181 struct perf_event
*event
;
1183 update_event_times(leader
);
1184 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
1185 update_event_times(event
);
1188 static struct list_head
*
1189 ctx_group_list(struct perf_event
*event
, struct perf_event_context
*ctx
)
1191 if (event
->attr
.pinned
)
1192 return &ctx
->pinned_groups
;
1194 return &ctx
->flexible_groups
;
1198 * Add a event from the lists for its context.
1199 * Must be called with ctx->mutex and ctx->lock held.
1202 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
1204 WARN_ON_ONCE(event
->attach_state
& PERF_ATTACH_CONTEXT
);
1205 event
->attach_state
|= PERF_ATTACH_CONTEXT
;
1208 * If we're a stand alone event or group leader, we go to the context
1209 * list, group events are kept attached to the group so that
1210 * perf_group_detach can, at all times, locate all siblings.
1212 if (event
->group_leader
== event
) {
1213 struct list_head
*list
;
1215 if (is_software_event(event
))
1216 event
->group_flags
|= PERF_GROUP_SOFTWARE
;
1218 list
= ctx_group_list(event
, ctx
);
1219 list_add_tail(&event
->group_entry
, list
);
1222 if (is_cgroup_event(event
))
1225 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
1227 if (event
->attr
.inherit_stat
)
1234 * Initialize event state based on the perf_event_attr::disabled.
1236 static inline void perf_event__state_init(struct perf_event
*event
)
1238 event
->state
= event
->attr
.disabled
? PERF_EVENT_STATE_OFF
:
1239 PERF_EVENT_STATE_INACTIVE
;
1243 * Called at perf_event creation and when events are attached/detached from a
1246 static void perf_event__read_size(struct perf_event
*event
)
1248 int entry
= sizeof(u64
); /* value */
1252 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1253 size
+= sizeof(u64
);
1255 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1256 size
+= sizeof(u64
);
1258 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
1259 entry
+= sizeof(u64
);
1261 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
1262 nr
+= event
->group_leader
->nr_siblings
;
1263 size
+= sizeof(u64
);
1267 event
->read_size
= size
;
1270 static void perf_event__header_size(struct perf_event
*event
)
1272 struct perf_sample_data
*data
;
1273 u64 sample_type
= event
->attr
.sample_type
;
1276 perf_event__read_size(event
);
1278 if (sample_type
& PERF_SAMPLE_IP
)
1279 size
+= sizeof(data
->ip
);
1281 if (sample_type
& PERF_SAMPLE_ADDR
)
1282 size
+= sizeof(data
->addr
);
1284 if (sample_type
& PERF_SAMPLE_PERIOD
)
1285 size
+= sizeof(data
->period
);
1287 if (sample_type
& PERF_SAMPLE_WEIGHT
)
1288 size
+= sizeof(data
->weight
);
1290 if (sample_type
& PERF_SAMPLE_READ
)
1291 size
+= event
->read_size
;
1293 if (sample_type
& PERF_SAMPLE_DATA_SRC
)
1294 size
+= sizeof(data
->data_src
.val
);
1296 if (sample_type
& PERF_SAMPLE_TRANSACTION
)
1297 size
+= sizeof(data
->txn
);
1299 event
->header_size
= size
;
1302 static void perf_event__id_header_size(struct perf_event
*event
)
1304 struct perf_sample_data
*data
;
1305 u64 sample_type
= event
->attr
.sample_type
;
1308 if (sample_type
& PERF_SAMPLE_TID
)
1309 size
+= sizeof(data
->tid_entry
);
1311 if (sample_type
& PERF_SAMPLE_TIME
)
1312 size
+= sizeof(data
->time
);
1314 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
1315 size
+= sizeof(data
->id
);
1317 if (sample_type
& PERF_SAMPLE_ID
)
1318 size
+= sizeof(data
->id
);
1320 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
1321 size
+= sizeof(data
->stream_id
);
1323 if (sample_type
& PERF_SAMPLE_CPU
)
1324 size
+= sizeof(data
->cpu_entry
);
1326 event
->id_header_size
= size
;
1329 static void perf_group_attach(struct perf_event
*event
)
1331 struct perf_event
*group_leader
= event
->group_leader
, *pos
;
1334 * We can have double attach due to group movement in perf_event_open.
1336 if (event
->attach_state
& PERF_ATTACH_GROUP
)
1339 event
->attach_state
|= PERF_ATTACH_GROUP
;
1341 if (group_leader
== event
)
1344 WARN_ON_ONCE(group_leader
->ctx
!= event
->ctx
);
1346 if (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
&&
1347 !is_software_event(event
))
1348 group_leader
->group_flags
&= ~PERF_GROUP_SOFTWARE
;
1350 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
1351 group_leader
->nr_siblings
++;
1353 perf_event__header_size(group_leader
);
1355 list_for_each_entry(pos
, &group_leader
->sibling_list
, group_entry
)
1356 perf_event__header_size(pos
);
1360 * Remove a event from the lists for its context.
1361 * Must be called with ctx->mutex and ctx->lock held.
1364 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
1366 struct perf_cpu_context
*cpuctx
;
1368 WARN_ON_ONCE(event
->ctx
!= ctx
);
1369 lockdep_assert_held(&ctx
->lock
);
1372 * We can have double detach due to exit/hot-unplug + close.
1374 if (!(event
->attach_state
& PERF_ATTACH_CONTEXT
))
1377 event
->attach_state
&= ~PERF_ATTACH_CONTEXT
;
1379 if (is_cgroup_event(event
)) {
1381 cpuctx
= __get_cpu_context(ctx
);
1383 * if there are no more cgroup events
1384 * then cler cgrp to avoid stale pointer
1385 * in update_cgrp_time_from_cpuctx()
1387 if (!ctx
->nr_cgroups
)
1388 cpuctx
->cgrp
= NULL
;
1392 if (event
->attr
.inherit_stat
)
1395 list_del_rcu(&event
->event_entry
);
1397 if (event
->group_leader
== event
)
1398 list_del_init(&event
->group_entry
);
1400 update_group_times(event
);
1403 * If event was in error state, then keep it
1404 * that way, otherwise bogus counts will be
1405 * returned on read(). The only way to get out
1406 * of error state is by explicit re-enabling
1409 if (event
->state
> PERF_EVENT_STATE_OFF
)
1410 event
->state
= PERF_EVENT_STATE_OFF
;
1415 static void perf_group_detach(struct perf_event
*event
)
1417 struct perf_event
*sibling
, *tmp
;
1418 struct list_head
*list
= NULL
;
1421 * We can have double detach due to exit/hot-unplug + close.
1423 if (!(event
->attach_state
& PERF_ATTACH_GROUP
))
1426 event
->attach_state
&= ~PERF_ATTACH_GROUP
;
1429 * If this is a sibling, remove it from its group.
1431 if (event
->group_leader
!= event
) {
1432 list_del_init(&event
->group_entry
);
1433 event
->group_leader
->nr_siblings
--;
1437 if (!list_empty(&event
->group_entry
))
1438 list
= &event
->group_entry
;
1441 * If this was a group event with sibling events then
1442 * upgrade the siblings to singleton events by adding them
1443 * to whatever list we are on.
1445 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
1447 list_move_tail(&sibling
->group_entry
, list
);
1448 sibling
->group_leader
= sibling
;
1450 /* Inherit group flags from the previous leader */
1451 sibling
->group_flags
= event
->group_flags
;
1453 WARN_ON_ONCE(sibling
->ctx
!= event
->ctx
);
1457 perf_event__header_size(event
->group_leader
);
1459 list_for_each_entry(tmp
, &event
->group_leader
->sibling_list
, group_entry
)
1460 perf_event__header_size(tmp
);
1464 * User event without the task.
1466 static bool is_orphaned_event(struct perf_event
*event
)
1468 return event
&& !is_kernel_event(event
) && !event
->owner
;
1472 * Event has a parent but parent's task finished and it's
1473 * alive only because of children holding refference.
1475 static bool is_orphaned_child(struct perf_event
*event
)
1477 return is_orphaned_event(event
->parent
);
1480 static void orphans_remove_work(struct work_struct
*work
);
1482 static void schedule_orphans_remove(struct perf_event_context
*ctx
)
1484 if (!ctx
->task
|| ctx
->orphans_remove_sched
|| !perf_wq
)
1487 if (queue_delayed_work(perf_wq
, &ctx
->orphans_remove
, 1)) {
1489 ctx
->orphans_remove_sched
= true;
1493 static int __init
perf_workqueue_init(void)
1495 perf_wq
= create_singlethread_workqueue("perf");
1496 WARN(!perf_wq
, "failed to create perf workqueue\n");
1497 return perf_wq
? 0 : -1;
1500 core_initcall(perf_workqueue_init
);
1503 event_filter_match(struct perf_event
*event
)
1505 return (event
->cpu
== -1 || event
->cpu
== smp_processor_id())
1506 && perf_cgroup_match(event
);
1510 event_sched_out(struct perf_event
*event
,
1511 struct perf_cpu_context
*cpuctx
,
1512 struct perf_event_context
*ctx
)
1514 u64 tstamp
= perf_event_time(event
);
1517 WARN_ON_ONCE(event
->ctx
!= ctx
);
1518 lockdep_assert_held(&ctx
->lock
);
1521 * An event which could not be activated because of
1522 * filter mismatch still needs to have its timings
1523 * maintained, otherwise bogus information is return
1524 * via read() for time_enabled, time_running:
1526 if (event
->state
== PERF_EVENT_STATE_INACTIVE
1527 && !event_filter_match(event
)) {
1528 delta
= tstamp
- event
->tstamp_stopped
;
1529 event
->tstamp_running
+= delta
;
1530 event
->tstamp_stopped
= tstamp
;
1533 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1536 perf_pmu_disable(event
->pmu
);
1538 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1539 if (event
->pending_disable
) {
1540 event
->pending_disable
= 0;
1541 event
->state
= PERF_EVENT_STATE_OFF
;
1543 event
->tstamp_stopped
= tstamp
;
1544 event
->pmu
->del(event
, 0);
1547 if (!is_software_event(event
))
1548 cpuctx
->active_oncpu
--;
1549 if (!--ctx
->nr_active
)
1550 perf_event_ctx_deactivate(ctx
);
1551 if (event
->attr
.freq
&& event
->attr
.sample_freq
)
1553 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
1554 cpuctx
->exclusive
= 0;
1556 if (is_orphaned_child(event
))
1557 schedule_orphans_remove(ctx
);
1559 perf_pmu_enable(event
->pmu
);
1563 group_sched_out(struct perf_event
*group_event
,
1564 struct perf_cpu_context
*cpuctx
,
1565 struct perf_event_context
*ctx
)
1567 struct perf_event
*event
;
1568 int state
= group_event
->state
;
1570 event_sched_out(group_event
, cpuctx
, ctx
);
1573 * Schedule out siblings (if any):
1575 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
1576 event_sched_out(event
, cpuctx
, ctx
);
1578 if (state
== PERF_EVENT_STATE_ACTIVE
&& group_event
->attr
.exclusive
)
1579 cpuctx
->exclusive
= 0;
1582 struct remove_event
{
1583 struct perf_event
*event
;
1588 * Cross CPU call to remove a performance event
1590 * We disable the event on the hardware level first. After that we
1591 * remove it from the context list.
1593 static int __perf_remove_from_context(void *info
)
1595 struct remove_event
*re
= info
;
1596 struct perf_event
*event
= re
->event
;
1597 struct perf_event_context
*ctx
= event
->ctx
;
1598 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1600 raw_spin_lock(&ctx
->lock
);
1601 event_sched_out(event
, cpuctx
, ctx
);
1602 if (re
->detach_group
)
1603 perf_group_detach(event
);
1604 list_del_event(event
, ctx
);
1605 if (!ctx
->nr_events
&& cpuctx
->task_ctx
== ctx
) {
1607 cpuctx
->task_ctx
= NULL
;
1609 raw_spin_unlock(&ctx
->lock
);
1616 * Remove the event from a task's (or a CPU's) list of events.
1618 * CPU events are removed with a smp call. For task events we only
1619 * call when the task is on a CPU.
1621 * If event->ctx is a cloned context, callers must make sure that
1622 * every task struct that event->ctx->task could possibly point to
1623 * remains valid. This is OK when called from perf_release since
1624 * that only calls us on the top-level context, which can't be a clone.
1625 * When called from perf_event_exit_task, it's OK because the
1626 * context has been detached from its task.
1628 static void perf_remove_from_context(struct perf_event
*event
, bool detach_group
)
1630 struct perf_event_context
*ctx
= event
->ctx
;
1631 struct task_struct
*task
= ctx
->task
;
1632 struct remove_event re
= {
1634 .detach_group
= detach_group
,
1637 lockdep_assert_held(&ctx
->mutex
);
1641 * Per cpu events are removed via an smp call. The removal can
1642 * fail if the CPU is currently offline, but in that case we
1643 * already called __perf_remove_from_context from
1644 * perf_event_exit_cpu.
1646 cpu_function_call(event
->cpu
, __perf_remove_from_context
, &re
);
1651 if (!task_function_call(task
, __perf_remove_from_context
, &re
))
1654 raw_spin_lock_irq(&ctx
->lock
);
1656 * If we failed to find a running task, but find the context active now
1657 * that we've acquired the ctx->lock, retry.
1659 if (ctx
->is_active
) {
1660 raw_spin_unlock_irq(&ctx
->lock
);
1662 * Reload the task pointer, it might have been changed by
1663 * a concurrent perf_event_context_sched_out().
1670 * Since the task isn't running, its safe to remove the event, us
1671 * holding the ctx->lock ensures the task won't get scheduled in.
1674 perf_group_detach(event
);
1675 list_del_event(event
, ctx
);
1676 raw_spin_unlock_irq(&ctx
->lock
);
1680 * Cross CPU call to disable a performance event
1682 int __perf_event_disable(void *info
)
1684 struct perf_event
*event
= info
;
1685 struct perf_event_context
*ctx
= event
->ctx
;
1686 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
1689 * If this is a per-task event, need to check whether this
1690 * event's task is the current task on this cpu.
1692 * Can trigger due to concurrent perf_event_context_sched_out()
1693 * flipping contexts around.
1695 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
1698 raw_spin_lock(&ctx
->lock
);
1701 * If the event is on, turn it off.
1702 * If it is in error state, leave it in error state.
1704 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
) {
1705 update_context_time(ctx
);
1706 update_cgrp_time_from_event(event
);
1707 update_group_times(event
);
1708 if (event
== event
->group_leader
)
1709 group_sched_out(event
, cpuctx
, ctx
);
1711 event_sched_out(event
, cpuctx
, ctx
);
1712 event
->state
= PERF_EVENT_STATE_OFF
;
1715 raw_spin_unlock(&ctx
->lock
);
1723 * If event->ctx is a cloned context, callers must make sure that
1724 * every task struct that event->ctx->task could possibly point to
1725 * remains valid. This condition is satisifed when called through
1726 * perf_event_for_each_child or perf_event_for_each because they
1727 * hold the top-level event's child_mutex, so any descendant that
1728 * goes to exit will block in sync_child_event.
1729 * When called from perf_pending_event it's OK because event->ctx
1730 * is the current context on this CPU and preemption is disabled,
1731 * hence we can't get into perf_event_task_sched_out for this context.
1733 static void _perf_event_disable(struct perf_event
*event
)
1735 struct perf_event_context
*ctx
= event
->ctx
;
1736 struct task_struct
*task
= ctx
->task
;
1740 * Disable the event on the cpu that it's on
1742 cpu_function_call(event
->cpu
, __perf_event_disable
, event
);
1747 if (!task_function_call(task
, __perf_event_disable
, event
))
1750 raw_spin_lock_irq(&ctx
->lock
);
1752 * If the event is still active, we need to retry the cross-call.
1754 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
1755 raw_spin_unlock_irq(&ctx
->lock
);
1757 * Reload the task pointer, it might have been changed by
1758 * a concurrent perf_event_context_sched_out().
1765 * Since we have the lock this context can't be scheduled
1766 * in, so we can change the state safely.
1768 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1769 update_group_times(event
);
1770 event
->state
= PERF_EVENT_STATE_OFF
;
1772 raw_spin_unlock_irq(&ctx
->lock
);
1776 * Strictly speaking kernel users cannot create groups and therefore this
1777 * interface does not need the perf_event_ctx_lock() magic.
1779 void perf_event_disable(struct perf_event
*event
)
1781 struct perf_event_context
*ctx
;
1783 ctx
= perf_event_ctx_lock(event
);
1784 _perf_event_disable(event
);
1785 perf_event_ctx_unlock(event
, ctx
);
1787 EXPORT_SYMBOL_GPL(perf_event_disable
);
1789 static void perf_set_shadow_time(struct perf_event
*event
,
1790 struct perf_event_context
*ctx
,
1794 * use the correct time source for the time snapshot
1796 * We could get by without this by leveraging the
1797 * fact that to get to this function, the caller
1798 * has most likely already called update_context_time()
1799 * and update_cgrp_time_xx() and thus both timestamp
1800 * are identical (or very close). Given that tstamp is,
1801 * already adjusted for cgroup, we could say that:
1802 * tstamp - ctx->timestamp
1804 * tstamp - cgrp->timestamp.
1806 * Then, in perf_output_read(), the calculation would
1807 * work with no changes because:
1808 * - event is guaranteed scheduled in
1809 * - no scheduled out in between
1810 * - thus the timestamp would be the same
1812 * But this is a bit hairy.
1814 * So instead, we have an explicit cgroup call to remain
1815 * within the time time source all along. We believe it
1816 * is cleaner and simpler to understand.
1818 if (is_cgroup_event(event
))
1819 perf_cgroup_set_shadow_time(event
, tstamp
);
1821 event
->shadow_ctx_time
= tstamp
- ctx
->timestamp
;
1824 #define MAX_INTERRUPTS (~0ULL)
1826 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1829 event_sched_in(struct perf_event
*event
,
1830 struct perf_cpu_context
*cpuctx
,
1831 struct perf_event_context
*ctx
)
1833 u64 tstamp
= perf_event_time(event
);
1836 lockdep_assert_held(&ctx
->lock
);
1838 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1841 event
->state
= PERF_EVENT_STATE_ACTIVE
;
1842 event
->oncpu
= smp_processor_id();
1845 * Unthrottle events, since we scheduled we might have missed several
1846 * ticks already, also for a heavily scheduling task there is little
1847 * guarantee it'll get a tick in a timely manner.
1849 if (unlikely(event
->hw
.interrupts
== MAX_INTERRUPTS
)) {
1850 perf_log_throttle(event
, 1);
1851 event
->hw
.interrupts
= 0;
1855 * The new state must be visible before we turn it on in the hardware:
1859 perf_pmu_disable(event
->pmu
);
1861 event
->tstamp_running
+= tstamp
- event
->tstamp_stopped
;
1863 perf_set_shadow_time(event
, ctx
, tstamp
);
1865 if (event
->pmu
->add(event
, PERF_EF_START
)) {
1866 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1872 if (!is_software_event(event
))
1873 cpuctx
->active_oncpu
++;
1874 if (!ctx
->nr_active
++)
1875 perf_event_ctx_activate(ctx
);
1876 if (event
->attr
.freq
&& event
->attr
.sample_freq
)
1879 if (event
->attr
.exclusive
)
1880 cpuctx
->exclusive
= 1;
1882 if (is_orphaned_child(event
))
1883 schedule_orphans_remove(ctx
);
1886 perf_pmu_enable(event
->pmu
);
1892 group_sched_in(struct perf_event
*group_event
,
1893 struct perf_cpu_context
*cpuctx
,
1894 struct perf_event_context
*ctx
)
1896 struct perf_event
*event
, *partial_group
= NULL
;
1897 struct pmu
*pmu
= ctx
->pmu
;
1898 u64 now
= ctx
->time
;
1899 bool simulate
= false;
1901 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
1904 pmu
->start_txn(pmu
);
1906 if (event_sched_in(group_event
, cpuctx
, ctx
)) {
1907 pmu
->cancel_txn(pmu
);
1908 perf_cpu_hrtimer_restart(cpuctx
);
1913 * Schedule in siblings as one group (if any):
1915 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
1916 if (event_sched_in(event
, cpuctx
, ctx
)) {
1917 partial_group
= event
;
1922 if (!pmu
->commit_txn(pmu
))
1927 * Groups can be scheduled in as one unit only, so undo any
1928 * partial group before returning:
1929 * The events up to the failed event are scheduled out normally,
1930 * tstamp_stopped will be updated.
1932 * The failed events and the remaining siblings need to have
1933 * their timings updated as if they had gone thru event_sched_in()
1934 * and event_sched_out(). This is required to get consistent timings
1935 * across the group. This also takes care of the case where the group
1936 * could never be scheduled by ensuring tstamp_stopped is set to mark
1937 * the time the event was actually stopped, such that time delta
1938 * calculation in update_event_times() is correct.
1940 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
1941 if (event
== partial_group
)
1945 event
->tstamp_running
+= now
- event
->tstamp_stopped
;
1946 event
->tstamp_stopped
= now
;
1948 event_sched_out(event
, cpuctx
, ctx
);
1951 event_sched_out(group_event
, cpuctx
, ctx
);
1953 pmu
->cancel_txn(pmu
);
1955 perf_cpu_hrtimer_restart(cpuctx
);
1961 * Work out whether we can put this event group on the CPU now.
1963 static int group_can_go_on(struct perf_event
*event
,
1964 struct perf_cpu_context
*cpuctx
,
1968 * Groups consisting entirely of software events can always go on.
1970 if (event
->group_flags
& PERF_GROUP_SOFTWARE
)
1973 * If an exclusive group is already on, no other hardware
1976 if (cpuctx
->exclusive
)
1979 * If this group is exclusive and there are already
1980 * events on the CPU, it can't go on.
1982 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
1985 * Otherwise, try to add it if all previous groups were able
1991 static void add_event_to_ctx(struct perf_event
*event
,
1992 struct perf_event_context
*ctx
)
1994 u64 tstamp
= perf_event_time(event
);
1996 list_add_event(event
, ctx
);
1997 perf_group_attach(event
);
1998 event
->tstamp_enabled
= tstamp
;
1999 event
->tstamp_running
= tstamp
;
2000 event
->tstamp_stopped
= tstamp
;
2003 static void task_ctx_sched_out(struct perf_event_context
*ctx
);
2005 ctx_sched_in(struct perf_event_context
*ctx
,
2006 struct perf_cpu_context
*cpuctx
,
2007 enum event_type_t event_type
,
2008 struct task_struct
*task
);
2010 static void perf_event_sched_in(struct perf_cpu_context
*cpuctx
,
2011 struct perf_event_context
*ctx
,
2012 struct task_struct
*task
)
2014 cpu_ctx_sched_in(cpuctx
, EVENT_PINNED
, task
);
2016 ctx_sched_in(ctx
, cpuctx
, EVENT_PINNED
, task
);
2017 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
, task
);
2019 ctx_sched_in(ctx
, cpuctx
, EVENT_FLEXIBLE
, task
);
2023 * Cross CPU call to install and enable a performance event
2025 * Must be called with ctx->mutex held
2027 static int __perf_install_in_context(void *info
)
2029 struct perf_event
*event
= info
;
2030 struct perf_event_context
*ctx
= event
->ctx
;
2031 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
2032 struct perf_event_context
*task_ctx
= cpuctx
->task_ctx
;
2033 struct task_struct
*task
= current
;
2035 perf_ctx_lock(cpuctx
, task_ctx
);
2036 perf_pmu_disable(cpuctx
->ctx
.pmu
);
2039 * If there was an active task_ctx schedule it out.
2042 task_ctx_sched_out(task_ctx
);
2045 * If the context we're installing events in is not the
2046 * active task_ctx, flip them.
2048 if (ctx
->task
&& task_ctx
!= ctx
) {
2050 raw_spin_unlock(&task_ctx
->lock
);
2051 raw_spin_lock(&ctx
->lock
);
2056 cpuctx
->task_ctx
= task_ctx
;
2057 task
= task_ctx
->task
;
2060 cpu_ctx_sched_out(cpuctx
, EVENT_ALL
);
2062 update_context_time(ctx
);
2064 * update cgrp time only if current cgrp
2065 * matches event->cgrp. Must be done before
2066 * calling add_event_to_ctx()
2068 update_cgrp_time_from_event(event
);
2070 add_event_to_ctx(event
, ctx
);
2073 * Schedule everything back in
2075 perf_event_sched_in(cpuctx
, task_ctx
, task
);
2077 perf_pmu_enable(cpuctx
->ctx
.pmu
);
2078 perf_ctx_unlock(cpuctx
, task_ctx
);
2084 * Attach a performance event to a context
2086 * First we add the event to the list with the hardware enable bit
2087 * in event->hw_config cleared.
2089 * If the event is attached to a task which is on a CPU we use a smp
2090 * call to enable it in the task context. The task might have been
2091 * scheduled away, but we check this in the smp call again.
2094 perf_install_in_context(struct perf_event_context
*ctx
,
2095 struct perf_event
*event
,
2098 struct task_struct
*task
= ctx
->task
;
2100 lockdep_assert_held(&ctx
->mutex
);
2103 if (event
->cpu
!= -1)
2108 * Per cpu events are installed via an smp call and
2109 * the install is always successful.
2111 cpu_function_call(cpu
, __perf_install_in_context
, event
);
2116 if (!task_function_call(task
, __perf_install_in_context
, event
))
2119 raw_spin_lock_irq(&ctx
->lock
);
2121 * If we failed to find a running task, but find the context active now
2122 * that we've acquired the ctx->lock, retry.
2124 if (ctx
->is_active
) {
2125 raw_spin_unlock_irq(&ctx
->lock
);
2127 * Reload the task pointer, it might have been changed by
2128 * a concurrent perf_event_context_sched_out().
2135 * Since the task isn't running, its safe to add the event, us holding
2136 * the ctx->lock ensures the task won't get scheduled in.
2138 add_event_to_ctx(event
, ctx
);
2139 raw_spin_unlock_irq(&ctx
->lock
);
2143 * Put a event into inactive state and update time fields.
2144 * Enabling the leader of a group effectively enables all
2145 * the group members that aren't explicitly disabled, so we
2146 * have to update their ->tstamp_enabled also.
2147 * Note: this works for group members as well as group leaders
2148 * since the non-leader members' sibling_lists will be empty.
2150 static void __perf_event_mark_enabled(struct perf_event
*event
)
2152 struct perf_event
*sub
;
2153 u64 tstamp
= perf_event_time(event
);
2155 event
->state
= PERF_EVENT_STATE_INACTIVE
;
2156 event
->tstamp_enabled
= tstamp
- event
->total_time_enabled
;
2157 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
) {
2158 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
)
2159 sub
->tstamp_enabled
= tstamp
- sub
->total_time_enabled
;
2164 * Cross CPU call to enable a performance event
2166 static int __perf_event_enable(void *info
)
2168 struct perf_event
*event
= info
;
2169 struct perf_event_context
*ctx
= event
->ctx
;
2170 struct perf_event
*leader
= event
->group_leader
;
2171 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
2175 * There's a time window between 'ctx->is_active' check
2176 * in perf_event_enable function and this place having:
2178 * - ctx->lock unlocked
2180 * where the task could be killed and 'ctx' deactivated
2181 * by perf_event_exit_task.
2183 if (!ctx
->is_active
)
2186 raw_spin_lock(&ctx
->lock
);
2187 update_context_time(ctx
);
2189 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
2193 * set current task's cgroup time reference point
2195 perf_cgroup_set_timestamp(current
, ctx
);
2197 __perf_event_mark_enabled(event
);
2199 if (!event_filter_match(event
)) {
2200 if (is_cgroup_event(event
))
2201 perf_cgroup_defer_enabled(event
);
2206 * If the event is in a group and isn't the group leader,
2207 * then don't put it on unless the group is on.
2209 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
)
2212 if (!group_can_go_on(event
, cpuctx
, 1)) {
2215 if (event
== leader
)
2216 err
= group_sched_in(event
, cpuctx
, ctx
);
2218 err
= event_sched_in(event
, cpuctx
, ctx
);
2223 * If this event can't go on and it's part of a
2224 * group, then the whole group has to come off.
2226 if (leader
!= event
) {
2227 group_sched_out(leader
, cpuctx
, ctx
);
2228 perf_cpu_hrtimer_restart(cpuctx
);
2230 if (leader
->attr
.pinned
) {
2231 update_group_times(leader
);
2232 leader
->state
= PERF_EVENT_STATE_ERROR
;
2237 raw_spin_unlock(&ctx
->lock
);
2245 * If event->ctx is a cloned context, callers must make sure that
2246 * every task struct that event->ctx->task could possibly point to
2247 * remains valid. This condition is satisfied when called through
2248 * perf_event_for_each_child or perf_event_for_each as described
2249 * for perf_event_disable.
2251 static void _perf_event_enable(struct perf_event
*event
)
2253 struct perf_event_context
*ctx
= event
->ctx
;
2254 struct task_struct
*task
= ctx
->task
;
2258 * Enable the event on the cpu that it's on
2260 cpu_function_call(event
->cpu
, __perf_event_enable
, event
);
2264 raw_spin_lock_irq(&ctx
->lock
);
2265 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
2269 * If the event is in error state, clear that first.
2270 * That way, if we see the event in error state below, we
2271 * know that it has gone back into error state, as distinct
2272 * from the task having been scheduled away before the
2273 * cross-call arrived.
2275 if (event
->state
== PERF_EVENT_STATE_ERROR
)
2276 event
->state
= PERF_EVENT_STATE_OFF
;
2279 if (!ctx
->is_active
) {
2280 __perf_event_mark_enabled(event
);
2284 raw_spin_unlock_irq(&ctx
->lock
);
2286 if (!task_function_call(task
, __perf_event_enable
, event
))
2289 raw_spin_lock_irq(&ctx
->lock
);
2292 * If the context is active and the event is still off,
2293 * we need to retry the cross-call.
2295 if (ctx
->is_active
&& event
->state
== PERF_EVENT_STATE_OFF
) {
2297 * task could have been flipped by a concurrent
2298 * perf_event_context_sched_out()
2305 raw_spin_unlock_irq(&ctx
->lock
);
2309 * See perf_event_disable();
2311 void perf_event_enable(struct perf_event
*event
)
2313 struct perf_event_context
*ctx
;
2315 ctx
= perf_event_ctx_lock(event
);
2316 _perf_event_enable(event
);
2317 perf_event_ctx_unlock(event
, ctx
);
2319 EXPORT_SYMBOL_GPL(perf_event_enable
);
2321 static int _perf_event_refresh(struct perf_event
*event
, int refresh
)
2324 * not supported on inherited events
2326 if (event
->attr
.inherit
|| !is_sampling_event(event
))
2329 atomic_add(refresh
, &event
->event_limit
);
2330 _perf_event_enable(event
);
2336 * See perf_event_disable()
2338 int perf_event_refresh(struct perf_event
*event
, int refresh
)
2340 struct perf_event_context
*ctx
;
2343 ctx
= perf_event_ctx_lock(event
);
2344 ret
= _perf_event_refresh(event
, refresh
);
2345 perf_event_ctx_unlock(event
, ctx
);
2349 EXPORT_SYMBOL_GPL(perf_event_refresh
);
2351 static void ctx_sched_out(struct perf_event_context
*ctx
,
2352 struct perf_cpu_context
*cpuctx
,
2353 enum event_type_t event_type
)
2355 struct perf_event
*event
;
2356 int is_active
= ctx
->is_active
;
2358 ctx
->is_active
&= ~event_type
;
2359 if (likely(!ctx
->nr_events
))
2362 update_context_time(ctx
);
2363 update_cgrp_time_from_cpuctx(cpuctx
);
2364 if (!ctx
->nr_active
)
2367 perf_pmu_disable(ctx
->pmu
);
2368 if ((is_active
& EVENT_PINNED
) && (event_type
& EVENT_PINNED
)) {
2369 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
)
2370 group_sched_out(event
, cpuctx
, ctx
);
2373 if ((is_active
& EVENT_FLEXIBLE
) && (event_type
& EVENT_FLEXIBLE
)) {
2374 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
)
2375 group_sched_out(event
, cpuctx
, ctx
);
2377 perf_pmu_enable(ctx
->pmu
);
2381 * Test whether two contexts are equivalent, i.e. whether they have both been
2382 * cloned from the same version of the same context.
2384 * Equivalence is measured using a generation number in the context that is
2385 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2386 * and list_del_event().
2388 static int context_equiv(struct perf_event_context
*ctx1
,
2389 struct perf_event_context
*ctx2
)
2391 lockdep_assert_held(&ctx1
->lock
);
2392 lockdep_assert_held(&ctx2
->lock
);
2394 /* Pinning disables the swap optimization */
2395 if (ctx1
->pin_count
|| ctx2
->pin_count
)
2398 /* If ctx1 is the parent of ctx2 */
2399 if (ctx1
== ctx2
->parent_ctx
&& ctx1
->generation
== ctx2
->parent_gen
)
2402 /* If ctx2 is the parent of ctx1 */
2403 if (ctx1
->parent_ctx
== ctx2
&& ctx1
->parent_gen
== ctx2
->generation
)
2407 * If ctx1 and ctx2 have the same parent; we flatten the parent
2408 * hierarchy, see perf_event_init_context().
2410 if (ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
&&
2411 ctx1
->parent_gen
== ctx2
->parent_gen
)
2418 static void __perf_event_sync_stat(struct perf_event
*event
,
2419 struct perf_event
*next_event
)
2423 if (!event
->attr
.inherit_stat
)
2427 * Update the event value, we cannot use perf_event_read()
2428 * because we're in the middle of a context switch and have IRQs
2429 * disabled, which upsets smp_call_function_single(), however
2430 * we know the event must be on the current CPU, therefore we
2431 * don't need to use it.
2433 switch (event
->state
) {
2434 case PERF_EVENT_STATE_ACTIVE
:
2435 event
->pmu
->read(event
);
2438 case PERF_EVENT_STATE_INACTIVE
:
2439 update_event_times(event
);
2447 * In order to keep per-task stats reliable we need to flip the event
2448 * values when we flip the contexts.
2450 value
= local64_read(&next_event
->count
);
2451 value
= local64_xchg(&event
->count
, value
);
2452 local64_set(&next_event
->count
, value
);
2454 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
2455 swap(event
->total_time_running
, next_event
->total_time_running
);
2458 * Since we swizzled the values, update the user visible data too.
2460 perf_event_update_userpage(event
);
2461 perf_event_update_userpage(next_event
);
2464 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
2465 struct perf_event_context
*next_ctx
)
2467 struct perf_event
*event
, *next_event
;
2472 update_context_time(ctx
);
2474 event
= list_first_entry(&ctx
->event_list
,
2475 struct perf_event
, event_entry
);
2477 next_event
= list_first_entry(&next_ctx
->event_list
,
2478 struct perf_event
, event_entry
);
2480 while (&event
->event_entry
!= &ctx
->event_list
&&
2481 &next_event
->event_entry
!= &next_ctx
->event_list
) {
2483 __perf_event_sync_stat(event
, next_event
);
2485 event
= list_next_entry(event
, event_entry
);
2486 next_event
= list_next_entry(next_event
, event_entry
);
2490 static void perf_event_context_sched_out(struct task_struct
*task
, int ctxn
,
2491 struct task_struct
*next
)
2493 struct perf_event_context
*ctx
= task
->perf_event_ctxp
[ctxn
];
2494 struct perf_event_context
*next_ctx
;
2495 struct perf_event_context
*parent
, *next_parent
;
2496 struct perf_cpu_context
*cpuctx
;
2502 cpuctx
= __get_cpu_context(ctx
);
2503 if (!cpuctx
->task_ctx
)
2507 next_ctx
= next
->perf_event_ctxp
[ctxn
];
2511 parent
= rcu_dereference(ctx
->parent_ctx
);
2512 next_parent
= rcu_dereference(next_ctx
->parent_ctx
);
2514 /* If neither context have a parent context; they cannot be clones. */
2515 if (!parent
&& !next_parent
)
2518 if (next_parent
== ctx
|| next_ctx
== parent
|| next_parent
== parent
) {
2520 * Looks like the two contexts are clones, so we might be
2521 * able to optimize the context switch. We lock both
2522 * contexts and check that they are clones under the
2523 * lock (including re-checking that neither has been
2524 * uncloned in the meantime). It doesn't matter which
2525 * order we take the locks because no other cpu could
2526 * be trying to lock both of these tasks.
2528 raw_spin_lock(&ctx
->lock
);
2529 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
2530 if (context_equiv(ctx
, next_ctx
)) {
2532 * XXX do we need a memory barrier of sorts
2533 * wrt to rcu_dereference() of perf_event_ctxp
2535 task
->perf_event_ctxp
[ctxn
] = next_ctx
;
2536 next
->perf_event_ctxp
[ctxn
] = ctx
;
2538 next_ctx
->task
= task
;
2540 swap(ctx
->task_ctx_data
, next_ctx
->task_ctx_data
);
2544 perf_event_sync_stat(ctx
, next_ctx
);
2546 raw_spin_unlock(&next_ctx
->lock
);
2547 raw_spin_unlock(&ctx
->lock
);
2553 raw_spin_lock(&ctx
->lock
);
2554 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
2555 cpuctx
->task_ctx
= NULL
;
2556 raw_spin_unlock(&ctx
->lock
);
2560 void perf_sched_cb_dec(struct pmu
*pmu
)
2562 this_cpu_dec(perf_sched_cb_usages
);
2565 void perf_sched_cb_inc(struct pmu
*pmu
)
2567 this_cpu_inc(perf_sched_cb_usages
);
2571 * This function provides the context switch callback to the lower code
2572 * layer. It is invoked ONLY when the context switch callback is enabled.
2574 static void perf_pmu_sched_task(struct task_struct
*prev
,
2575 struct task_struct
*next
,
2578 struct perf_cpu_context
*cpuctx
;
2580 unsigned long flags
;
2585 local_irq_save(flags
);
2589 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
2590 if (pmu
->sched_task
) {
2591 cpuctx
= this_cpu_ptr(pmu
->pmu_cpu_context
);
2593 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
2595 perf_pmu_disable(pmu
);
2597 pmu
->sched_task(cpuctx
->task_ctx
, sched_in
);
2599 perf_pmu_enable(pmu
);
2601 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
2607 local_irq_restore(flags
);
2610 #define for_each_task_context_nr(ctxn) \
2611 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2614 * Called from scheduler to remove the events of the current task,
2615 * with interrupts disabled.
2617 * We stop each event and update the event value in event->count.
2619 * This does not protect us against NMI, but disable()
2620 * sets the disabled bit in the control field of event _before_
2621 * accessing the event control register. If a NMI hits, then it will
2622 * not restart the event.
2624 void __perf_event_task_sched_out(struct task_struct
*task
,
2625 struct task_struct
*next
)
2629 if (__this_cpu_read(perf_sched_cb_usages
))
2630 perf_pmu_sched_task(task
, next
, false);
2632 for_each_task_context_nr(ctxn
)
2633 perf_event_context_sched_out(task
, ctxn
, next
);
2636 * if cgroup events exist on this CPU, then we need
2637 * to check if we have to switch out PMU state.
2638 * cgroup event are system-wide mode only
2640 if (atomic_read(this_cpu_ptr(&perf_cgroup_events
)))
2641 perf_cgroup_sched_out(task
, next
);
2644 static void task_ctx_sched_out(struct perf_event_context
*ctx
)
2646 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
2648 if (!cpuctx
->task_ctx
)
2651 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
2654 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
2655 cpuctx
->task_ctx
= NULL
;
2659 * Called with IRQs disabled
2661 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
2662 enum event_type_t event_type
)
2664 ctx_sched_out(&cpuctx
->ctx
, cpuctx
, event_type
);
2668 ctx_pinned_sched_in(struct perf_event_context
*ctx
,
2669 struct perf_cpu_context
*cpuctx
)
2671 struct perf_event
*event
;
2673 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
2674 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2676 if (!event_filter_match(event
))
2679 /* may need to reset tstamp_enabled */
2680 if (is_cgroup_event(event
))
2681 perf_cgroup_mark_enabled(event
, ctx
);
2683 if (group_can_go_on(event
, cpuctx
, 1))
2684 group_sched_in(event
, cpuctx
, ctx
);
2687 * If this pinned group hasn't been scheduled,
2688 * put it in error state.
2690 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
2691 update_group_times(event
);
2692 event
->state
= PERF_EVENT_STATE_ERROR
;
2698 ctx_flexible_sched_in(struct perf_event_context
*ctx
,
2699 struct perf_cpu_context
*cpuctx
)
2701 struct perf_event
*event
;
2704 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
2705 /* Ignore events in OFF or ERROR state */
2706 if (event
->state
<= PERF_EVENT_STATE_OFF
)
2709 * Listen to the 'cpu' scheduling filter constraint
2712 if (!event_filter_match(event
))
2715 /* may need to reset tstamp_enabled */
2716 if (is_cgroup_event(event
))
2717 perf_cgroup_mark_enabled(event
, ctx
);
2719 if (group_can_go_on(event
, cpuctx
, can_add_hw
)) {
2720 if (group_sched_in(event
, cpuctx
, ctx
))
2727 ctx_sched_in(struct perf_event_context
*ctx
,
2728 struct perf_cpu_context
*cpuctx
,
2729 enum event_type_t event_type
,
2730 struct task_struct
*task
)
2733 int is_active
= ctx
->is_active
;
2735 ctx
->is_active
|= event_type
;
2736 if (likely(!ctx
->nr_events
))
2740 ctx
->timestamp
= now
;
2741 perf_cgroup_set_timestamp(task
, ctx
);
2743 * First go through the list and put on any pinned groups
2744 * in order to give them the best chance of going on.
2746 if (!(is_active
& EVENT_PINNED
) && (event_type
& EVENT_PINNED
))
2747 ctx_pinned_sched_in(ctx
, cpuctx
);
2749 /* Then walk through the lower prio flexible groups */
2750 if (!(is_active
& EVENT_FLEXIBLE
) && (event_type
& EVENT_FLEXIBLE
))
2751 ctx_flexible_sched_in(ctx
, cpuctx
);
2754 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
2755 enum event_type_t event_type
,
2756 struct task_struct
*task
)
2758 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
2760 ctx_sched_in(ctx
, cpuctx
, event_type
, task
);
2763 static void perf_event_context_sched_in(struct perf_event_context
*ctx
,
2764 struct task_struct
*task
)
2766 struct perf_cpu_context
*cpuctx
;
2768 cpuctx
= __get_cpu_context(ctx
);
2769 if (cpuctx
->task_ctx
== ctx
)
2772 perf_ctx_lock(cpuctx
, ctx
);
2773 perf_pmu_disable(ctx
->pmu
);
2775 * We want to keep the following priority order:
2776 * cpu pinned (that don't need to move), task pinned,
2777 * cpu flexible, task flexible.
2779 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
2782 cpuctx
->task_ctx
= ctx
;
2784 perf_event_sched_in(cpuctx
, cpuctx
->task_ctx
, task
);
2786 perf_pmu_enable(ctx
->pmu
);
2787 perf_ctx_unlock(cpuctx
, ctx
);
2791 * Called from scheduler to add the events of the current task
2792 * with interrupts disabled.
2794 * We restore the event value and then enable it.
2796 * This does not protect us against NMI, but enable()
2797 * sets the enabled bit in the control field of event _before_
2798 * accessing the event control register. If a NMI hits, then it will
2799 * keep the event running.
2801 void __perf_event_task_sched_in(struct task_struct
*prev
,
2802 struct task_struct
*task
)
2804 struct perf_event_context
*ctx
;
2807 for_each_task_context_nr(ctxn
) {
2808 ctx
= task
->perf_event_ctxp
[ctxn
];
2812 perf_event_context_sched_in(ctx
, task
);
2815 * if cgroup events exist on this CPU, then we need
2816 * to check if we have to switch in PMU state.
2817 * cgroup event are system-wide mode only
2819 if (atomic_read(this_cpu_ptr(&perf_cgroup_events
)))
2820 perf_cgroup_sched_in(prev
, task
);
2822 if (__this_cpu_read(perf_sched_cb_usages
))
2823 perf_pmu_sched_task(prev
, task
, true);
2826 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
2828 u64 frequency
= event
->attr
.sample_freq
;
2829 u64 sec
= NSEC_PER_SEC
;
2830 u64 divisor
, dividend
;
2832 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
2834 count_fls
= fls64(count
);
2835 nsec_fls
= fls64(nsec
);
2836 frequency_fls
= fls64(frequency
);
2840 * We got @count in @nsec, with a target of sample_freq HZ
2841 * the target period becomes:
2844 * period = -------------------
2845 * @nsec * sample_freq
2850 * Reduce accuracy by one bit such that @a and @b converge
2851 * to a similar magnitude.
2853 #define REDUCE_FLS(a, b) \
2855 if (a##_fls > b##_fls) { \
2865 * Reduce accuracy until either term fits in a u64, then proceed with
2866 * the other, so that finally we can do a u64/u64 division.
2868 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
2869 REDUCE_FLS(nsec
, frequency
);
2870 REDUCE_FLS(sec
, count
);
2873 if (count_fls
+ sec_fls
> 64) {
2874 divisor
= nsec
* frequency
;
2876 while (count_fls
+ sec_fls
> 64) {
2877 REDUCE_FLS(count
, sec
);
2881 dividend
= count
* sec
;
2883 dividend
= count
* sec
;
2885 while (nsec_fls
+ frequency_fls
> 64) {
2886 REDUCE_FLS(nsec
, frequency
);
2890 divisor
= nsec
* frequency
;
2896 return div64_u64(dividend
, divisor
);
2899 static DEFINE_PER_CPU(int, perf_throttled_count
);
2900 static DEFINE_PER_CPU(u64
, perf_throttled_seq
);
2902 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
, bool disable
)
2904 struct hw_perf_event
*hwc
= &event
->hw
;
2905 s64 period
, sample_period
;
2908 period
= perf_calculate_period(event
, nsec
, count
);
2910 delta
= (s64
)(period
- hwc
->sample_period
);
2911 delta
= (delta
+ 7) / 8; /* low pass filter */
2913 sample_period
= hwc
->sample_period
+ delta
;
2918 hwc
->sample_period
= sample_period
;
2920 if (local64_read(&hwc
->period_left
) > 8*sample_period
) {
2922 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
2924 local64_set(&hwc
->period_left
, 0);
2927 event
->pmu
->start(event
, PERF_EF_RELOAD
);
2932 * combine freq adjustment with unthrottling to avoid two passes over the
2933 * events. At the same time, make sure, having freq events does not change
2934 * the rate of unthrottling as that would introduce bias.
2936 static void perf_adjust_freq_unthr_context(struct perf_event_context
*ctx
,
2939 struct perf_event
*event
;
2940 struct hw_perf_event
*hwc
;
2941 u64 now
, period
= TICK_NSEC
;
2945 * only need to iterate over all events iff:
2946 * - context have events in frequency mode (needs freq adjust)
2947 * - there are events to unthrottle on this cpu
2949 if (!(ctx
->nr_freq
|| needs_unthr
))
2952 raw_spin_lock(&ctx
->lock
);
2953 perf_pmu_disable(ctx
->pmu
);
2955 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
2956 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
2959 if (!event_filter_match(event
))
2962 perf_pmu_disable(event
->pmu
);
2966 if (hwc
->interrupts
== MAX_INTERRUPTS
) {
2967 hwc
->interrupts
= 0;
2968 perf_log_throttle(event
, 1);
2969 event
->pmu
->start(event
, 0);
2972 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
2976 * stop the event and update event->count
2978 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
2980 now
= local64_read(&event
->count
);
2981 delta
= now
- hwc
->freq_count_stamp
;
2982 hwc
->freq_count_stamp
= now
;
2986 * reload only if value has changed
2987 * we have stopped the event so tell that
2988 * to perf_adjust_period() to avoid stopping it
2992 perf_adjust_period(event
, period
, delta
, false);
2994 event
->pmu
->start(event
, delta
> 0 ? PERF_EF_RELOAD
: 0);
2996 perf_pmu_enable(event
->pmu
);
2999 perf_pmu_enable(ctx
->pmu
);
3000 raw_spin_unlock(&ctx
->lock
);
3004 * Round-robin a context's events:
3006 static void rotate_ctx(struct perf_event_context
*ctx
)
3009 * Rotate the first entry last of non-pinned groups. Rotation might be
3010 * disabled by the inheritance code.
3012 if (!ctx
->rotate_disable
)
3013 list_rotate_left(&ctx
->flexible_groups
);
3016 static int perf_rotate_context(struct perf_cpu_context
*cpuctx
)
3018 struct perf_event_context
*ctx
= NULL
;
3021 if (cpuctx
->ctx
.nr_events
) {
3022 if (cpuctx
->ctx
.nr_events
!= cpuctx
->ctx
.nr_active
)
3026 ctx
= cpuctx
->task_ctx
;
3027 if (ctx
&& ctx
->nr_events
) {
3028 if (ctx
->nr_events
!= ctx
->nr_active
)
3035 perf_ctx_lock(cpuctx
, cpuctx
->task_ctx
);
3036 perf_pmu_disable(cpuctx
->ctx
.pmu
);
3038 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
3040 ctx_sched_out(ctx
, cpuctx
, EVENT_FLEXIBLE
);
3042 rotate_ctx(&cpuctx
->ctx
);
3046 perf_event_sched_in(cpuctx
, ctx
, current
);
3048 perf_pmu_enable(cpuctx
->ctx
.pmu
);
3049 perf_ctx_unlock(cpuctx
, cpuctx
->task_ctx
);
3055 #ifdef CONFIG_NO_HZ_FULL
3056 bool perf_event_can_stop_tick(void)
3058 if (atomic_read(&nr_freq_events
) ||
3059 __this_cpu_read(perf_throttled_count
))
3066 void perf_event_task_tick(void)
3068 struct list_head
*head
= this_cpu_ptr(&active_ctx_list
);
3069 struct perf_event_context
*ctx
, *tmp
;
3072 WARN_ON(!irqs_disabled());
3074 __this_cpu_inc(perf_throttled_seq
);
3075 throttled
= __this_cpu_xchg(perf_throttled_count
, 0);
3077 list_for_each_entry_safe(ctx
, tmp
, head
, active_ctx_list
)
3078 perf_adjust_freq_unthr_context(ctx
, throttled
);
3081 static int event_enable_on_exec(struct perf_event
*event
,
3082 struct perf_event_context
*ctx
)
3084 if (!event
->attr
.enable_on_exec
)
3087 event
->attr
.enable_on_exec
= 0;
3088 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
3091 __perf_event_mark_enabled(event
);
3097 * Enable all of a task's events that have been marked enable-on-exec.
3098 * This expects task == current.
3100 static void perf_event_enable_on_exec(struct perf_event_context
*ctx
)
3102 struct perf_event_context
*clone_ctx
= NULL
;
3103 struct perf_event
*event
;
3104 unsigned long flags
;
3108 local_irq_save(flags
);
3109 if (!ctx
|| !ctx
->nr_events
)
3113 * We must ctxsw out cgroup events to avoid conflict
3114 * when invoking perf_task_event_sched_in() later on
3115 * in this function. Otherwise we end up trying to
3116 * ctxswin cgroup events which are already scheduled
3119 perf_cgroup_sched_out(current
, NULL
);
3121 raw_spin_lock(&ctx
->lock
);
3122 task_ctx_sched_out(ctx
);
3124 list_for_each_entry(event
, &ctx
->event_list
, event_entry
) {
3125 ret
= event_enable_on_exec(event
, ctx
);
3131 * Unclone this context if we enabled any event.
3134 clone_ctx
= unclone_ctx(ctx
);
3136 raw_spin_unlock(&ctx
->lock
);
3139 * Also calls ctxswin for cgroup events, if any:
3141 perf_event_context_sched_in(ctx
, ctx
->task
);
3143 local_irq_restore(flags
);
3149 void perf_event_exec(void)
3151 struct perf_event_context
*ctx
;
3155 for_each_task_context_nr(ctxn
) {
3156 ctx
= current
->perf_event_ctxp
[ctxn
];
3160 perf_event_enable_on_exec(ctx
);
3166 * Cross CPU call to read the hardware event
3168 static void __perf_event_read(void *info
)
3170 struct perf_event
*event
= info
;
3171 struct perf_event_context
*ctx
= event
->ctx
;
3172 struct perf_cpu_context
*cpuctx
= __get_cpu_context(ctx
);
3175 * If this is a task context, we need to check whether it is
3176 * the current task context of this cpu. If not it has been
3177 * scheduled out before the smp call arrived. In that case
3178 * event->count would have been updated to a recent sample
3179 * when the event was scheduled out.
3181 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
3184 raw_spin_lock(&ctx
->lock
);
3185 if (ctx
->is_active
) {
3186 update_context_time(ctx
);
3187 update_cgrp_time_from_event(event
);
3189 update_event_times(event
);
3190 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
3191 event
->pmu
->read(event
);
3192 raw_spin_unlock(&ctx
->lock
);
3195 static inline u64
perf_event_count(struct perf_event
*event
)
3197 if (event
->pmu
->count
)
3198 return event
->pmu
->count(event
);
3200 return __perf_event_count(event
);
3203 static u64
perf_event_read(struct perf_event
*event
)
3206 * If event is enabled and currently active on a CPU, update the
3207 * value in the event structure:
3209 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
3210 smp_call_function_single(event
->oncpu
,
3211 __perf_event_read
, event
, 1);
3212 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
3213 struct perf_event_context
*ctx
= event
->ctx
;
3214 unsigned long flags
;
3216 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
3218 * may read while context is not active
3219 * (e.g., thread is blocked), in that case
3220 * we cannot update context time
3222 if (ctx
->is_active
) {
3223 update_context_time(ctx
);
3224 update_cgrp_time_from_event(event
);
3226 update_event_times(event
);
3227 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
3230 return perf_event_count(event
);
3234 * Initialize the perf_event context in a task_struct:
3236 static void __perf_event_init_context(struct perf_event_context
*ctx
)
3238 raw_spin_lock_init(&ctx
->lock
);
3239 mutex_init(&ctx
->mutex
);
3240 INIT_LIST_HEAD(&ctx
->active_ctx_list
);
3241 INIT_LIST_HEAD(&ctx
->pinned_groups
);
3242 INIT_LIST_HEAD(&ctx
->flexible_groups
);
3243 INIT_LIST_HEAD(&ctx
->event_list
);
3244 atomic_set(&ctx
->refcount
, 1);
3245 INIT_DELAYED_WORK(&ctx
->orphans_remove
, orphans_remove_work
);
3248 static struct perf_event_context
*
3249 alloc_perf_context(struct pmu
*pmu
, struct task_struct
*task
)
3251 struct perf_event_context
*ctx
;
3253 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
3257 __perf_event_init_context(ctx
);
3260 get_task_struct(task
);
3267 static struct task_struct
*
3268 find_lively_task_by_vpid(pid_t vpid
)
3270 struct task_struct
*task
;
3277 task
= find_task_by_vpid(vpid
);
3279 get_task_struct(task
);
3283 return ERR_PTR(-ESRCH
);
3285 /* Reuse ptrace permission checks for now. */
3287 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
3292 put_task_struct(task
);
3293 return ERR_PTR(err
);
3298 * Returns a matching context with refcount and pincount.
3300 static struct perf_event_context
*
3301 find_get_context(struct pmu
*pmu
, struct task_struct
*task
,
3302 struct perf_event
*event
)
3304 struct perf_event_context
*ctx
, *clone_ctx
= NULL
;
3305 struct perf_cpu_context
*cpuctx
;
3306 void *task_ctx_data
= NULL
;
3307 unsigned long flags
;
3309 int cpu
= event
->cpu
;
3312 /* Must be root to operate on a CPU event: */
3313 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
3314 return ERR_PTR(-EACCES
);
3317 * We could be clever and allow to attach a event to an
3318 * offline CPU and activate it when the CPU comes up, but
3321 if (!cpu_online(cpu
))
3322 return ERR_PTR(-ENODEV
);
3324 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
3333 ctxn
= pmu
->task_ctx_nr
;
3337 if (event
->attach_state
& PERF_ATTACH_TASK_DATA
) {
3338 task_ctx_data
= kzalloc(pmu
->task_ctx_size
, GFP_KERNEL
);
3339 if (!task_ctx_data
) {
3346 ctx
= perf_lock_task_context(task
, ctxn
, &flags
);
3348 clone_ctx
= unclone_ctx(ctx
);
3351 if (task_ctx_data
&& !ctx
->task_ctx_data
) {
3352 ctx
->task_ctx_data
= task_ctx_data
;
3353 task_ctx_data
= NULL
;
3355 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
3360 ctx
= alloc_perf_context(pmu
, task
);
3365 if (task_ctx_data
) {
3366 ctx
->task_ctx_data
= task_ctx_data
;
3367 task_ctx_data
= NULL
;
3371 mutex_lock(&task
->perf_event_mutex
);
3373 * If it has already passed perf_event_exit_task().
3374 * we must see PF_EXITING, it takes this mutex too.
3376 if (task
->flags
& PF_EXITING
)
3378 else if (task
->perf_event_ctxp
[ctxn
])
3383 rcu_assign_pointer(task
->perf_event_ctxp
[ctxn
], ctx
);
3385 mutex_unlock(&task
->perf_event_mutex
);
3387 if (unlikely(err
)) {
3396 kfree(task_ctx_data
);
3400 kfree(task_ctx_data
);
3401 return ERR_PTR(err
);
3404 static void perf_event_free_filter(struct perf_event
*event
);
3406 static void free_event_rcu(struct rcu_head
*head
)
3408 struct perf_event
*event
;
3410 event
= container_of(head
, struct perf_event
, rcu_head
);
3412 put_pid_ns(event
->ns
);
3413 perf_event_free_filter(event
);
3417 static void ring_buffer_put(struct ring_buffer
*rb
);
3418 static void ring_buffer_attach(struct perf_event
*event
,
3419 struct ring_buffer
*rb
);
3421 static void unaccount_event_cpu(struct perf_event
*event
, int cpu
)
3426 if (is_cgroup_event(event
))
3427 atomic_dec(&per_cpu(perf_cgroup_events
, cpu
));
3430 static void unaccount_event(struct perf_event
*event
)
3435 if (event
->attach_state
& PERF_ATTACH_TASK
)
3436 static_key_slow_dec_deferred(&perf_sched_events
);
3437 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
3438 atomic_dec(&nr_mmap_events
);
3439 if (event
->attr
.comm
)
3440 atomic_dec(&nr_comm_events
);
3441 if (event
->attr
.task
)
3442 atomic_dec(&nr_task_events
);
3443 if (event
->attr
.freq
)
3444 atomic_dec(&nr_freq_events
);
3445 if (is_cgroup_event(event
))
3446 static_key_slow_dec_deferred(&perf_sched_events
);
3447 if (has_branch_stack(event
))
3448 static_key_slow_dec_deferred(&perf_sched_events
);
3450 unaccount_event_cpu(event
, event
->cpu
);
3453 static void __free_event(struct perf_event
*event
)
3455 if (!event
->parent
) {
3456 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
)
3457 put_callchain_buffers();
3461 event
->destroy(event
);
3464 put_ctx(event
->ctx
);
3467 module_put(event
->pmu
->module
);
3469 call_rcu(&event
->rcu_head
, free_event_rcu
);
3472 static void _free_event(struct perf_event
*event
)
3474 irq_work_sync(&event
->pending
);
3476 unaccount_event(event
);
3480 * Can happen when we close an event with re-directed output.
3482 * Since we have a 0 refcount, perf_mmap_close() will skip
3483 * over us; possibly making our ring_buffer_put() the last.
3485 mutex_lock(&event
->mmap_mutex
);
3486 ring_buffer_attach(event
, NULL
);
3487 mutex_unlock(&event
->mmap_mutex
);
3490 if (is_cgroup_event(event
))
3491 perf_detach_cgroup(event
);
3493 __free_event(event
);
3497 * Used to free events which have a known refcount of 1, such as in error paths
3498 * where the event isn't exposed yet and inherited events.
3500 static void free_event(struct perf_event
*event
)
3502 if (WARN(atomic_long_cmpxchg(&event
->refcount
, 1, 0) != 1,
3503 "unexpected event refcount: %ld; ptr=%p\n",
3504 atomic_long_read(&event
->refcount
), event
)) {
3505 /* leak to avoid use-after-free */
3513 * Remove user event from the owner task.
3515 static void perf_remove_from_owner(struct perf_event
*event
)
3517 struct task_struct
*owner
;
3520 owner
= ACCESS_ONCE(event
->owner
);
3522 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3523 * !owner it means the list deletion is complete and we can indeed
3524 * free this event, otherwise we need to serialize on
3525 * owner->perf_event_mutex.
3527 smp_read_barrier_depends();
3530 * Since delayed_put_task_struct() also drops the last
3531 * task reference we can safely take a new reference
3532 * while holding the rcu_read_lock().
3534 get_task_struct(owner
);
3540 * If we're here through perf_event_exit_task() we're already
3541 * holding ctx->mutex which would be an inversion wrt. the
3542 * normal lock order.
3544 * However we can safely take this lock because its the child
3547 mutex_lock_nested(&owner
->perf_event_mutex
, SINGLE_DEPTH_NESTING
);
3550 * We have to re-check the event->owner field, if it is cleared
3551 * we raced with perf_event_exit_task(), acquiring the mutex
3552 * ensured they're done, and we can proceed with freeing the
3556 list_del_init(&event
->owner_entry
);
3557 mutex_unlock(&owner
->perf_event_mutex
);
3558 put_task_struct(owner
);
3563 * Called when the last reference to the file is gone.
3565 static void put_event(struct perf_event
*event
)
3567 struct perf_event_context
*ctx
;
3569 if (!atomic_long_dec_and_test(&event
->refcount
))
3572 if (!is_kernel_event(event
))
3573 perf_remove_from_owner(event
);
3576 * There are two ways this annotation is useful:
3578 * 1) there is a lock recursion from perf_event_exit_task
3579 * see the comment there.
3581 * 2) there is a lock-inversion with mmap_sem through
3582 * perf_event_read_group(), which takes faults while
3583 * holding ctx->mutex, however this is called after
3584 * the last filedesc died, so there is no possibility
3585 * to trigger the AB-BA case.
3587 ctx
= perf_event_ctx_lock_nested(event
, SINGLE_DEPTH_NESTING
);
3588 WARN_ON_ONCE(ctx
->parent_ctx
);
3589 perf_remove_from_context(event
, true);
3590 mutex_unlock(&ctx
->mutex
);
3595 int perf_event_release_kernel(struct perf_event
*event
)
3600 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
3602 static int perf_release(struct inode
*inode
, struct file
*file
)
3604 put_event(file
->private_data
);
3609 * Remove all orphanes events from the context.
3611 static void orphans_remove_work(struct work_struct
*work
)
3613 struct perf_event_context
*ctx
;
3614 struct perf_event
*event
, *tmp
;
3616 ctx
= container_of(work
, struct perf_event_context
,
3617 orphans_remove
.work
);
3619 mutex_lock(&ctx
->mutex
);
3620 list_for_each_entry_safe(event
, tmp
, &ctx
->event_list
, event_entry
) {
3621 struct perf_event
*parent_event
= event
->parent
;
3623 if (!is_orphaned_child(event
))
3626 perf_remove_from_context(event
, true);
3628 mutex_lock(&parent_event
->child_mutex
);
3629 list_del_init(&event
->child_list
);
3630 mutex_unlock(&parent_event
->child_mutex
);
3633 put_event(parent_event
);
3636 raw_spin_lock_irq(&ctx
->lock
);
3637 ctx
->orphans_remove_sched
= false;
3638 raw_spin_unlock_irq(&ctx
->lock
);
3639 mutex_unlock(&ctx
->mutex
);
3644 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
3646 struct perf_event
*child
;
3652 mutex_lock(&event
->child_mutex
);
3653 total
+= perf_event_read(event
);
3654 *enabled
+= event
->total_time_enabled
+
3655 atomic64_read(&event
->child_total_time_enabled
);
3656 *running
+= event
->total_time_running
+
3657 atomic64_read(&event
->child_total_time_running
);
3659 list_for_each_entry(child
, &event
->child_list
, child_list
) {
3660 total
+= perf_event_read(child
);
3661 *enabled
+= child
->total_time_enabled
;
3662 *running
+= child
->total_time_running
;
3664 mutex_unlock(&event
->child_mutex
);
3668 EXPORT_SYMBOL_GPL(perf_event_read_value
);
3670 static int perf_event_read_group(struct perf_event
*event
,
3671 u64 read_format
, char __user
*buf
)
3673 struct perf_event
*leader
= event
->group_leader
, *sub
;
3674 struct perf_event_context
*ctx
= leader
->ctx
;
3675 int n
= 0, size
= 0, ret
;
3676 u64 count
, enabled
, running
;
3679 lockdep_assert_held(&ctx
->mutex
);
3681 count
= perf_event_read_value(leader
, &enabled
, &running
);
3683 values
[n
++] = 1 + leader
->nr_siblings
;
3684 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3685 values
[n
++] = enabled
;
3686 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3687 values
[n
++] = running
;
3688 values
[n
++] = count
;
3689 if (read_format
& PERF_FORMAT_ID
)
3690 values
[n
++] = primary_event_id(leader
);
3692 size
= n
* sizeof(u64
);
3694 if (copy_to_user(buf
, values
, size
))
3699 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3702 values
[n
++] = perf_event_read_value(sub
, &enabled
, &running
);
3703 if (read_format
& PERF_FORMAT_ID
)
3704 values
[n
++] = primary_event_id(sub
);
3706 size
= n
* sizeof(u64
);
3708 if (copy_to_user(buf
+ ret
, values
, size
)) {
3718 static int perf_event_read_one(struct perf_event
*event
,
3719 u64 read_format
, char __user
*buf
)
3721 u64 enabled
, running
;
3725 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
3726 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3727 values
[n
++] = enabled
;
3728 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3729 values
[n
++] = running
;
3730 if (read_format
& PERF_FORMAT_ID
)
3731 values
[n
++] = primary_event_id(event
);
3733 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
3736 return n
* sizeof(u64
);
3739 static bool is_event_hup(struct perf_event
*event
)
3743 if (event
->state
!= PERF_EVENT_STATE_EXIT
)
3746 mutex_lock(&event
->child_mutex
);
3747 no_children
= list_empty(&event
->child_list
);
3748 mutex_unlock(&event
->child_mutex
);
3753 * Read the performance event - simple non blocking version for now
3756 perf_read_hw(struct perf_event
*event
, char __user
*buf
, size_t count
)
3758 u64 read_format
= event
->attr
.read_format
;
3762 * Return end-of-file for a read on a event that is in
3763 * error state (i.e. because it was pinned but it couldn't be
3764 * scheduled on to the CPU at some point).
3766 if (event
->state
== PERF_EVENT_STATE_ERROR
)
3769 if (count
< event
->read_size
)
3772 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3773 if (read_format
& PERF_FORMAT_GROUP
)
3774 ret
= perf_event_read_group(event
, read_format
, buf
);
3776 ret
= perf_event_read_one(event
, read_format
, buf
);
3782 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
3784 struct perf_event
*event
= file
->private_data
;
3785 struct perf_event_context
*ctx
;
3788 ctx
= perf_event_ctx_lock(event
);
3789 ret
= perf_read_hw(event
, buf
, count
);
3790 perf_event_ctx_unlock(event
, ctx
);
3795 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
3797 struct perf_event
*event
= file
->private_data
;
3798 struct ring_buffer
*rb
;
3799 unsigned int events
= POLLHUP
;
3801 poll_wait(file
, &event
->waitq
, wait
);
3803 if (is_event_hup(event
))
3807 * Pin the event->rb by taking event->mmap_mutex; otherwise
3808 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
3810 mutex_lock(&event
->mmap_mutex
);
3813 events
= atomic_xchg(&rb
->poll
, 0);
3814 mutex_unlock(&event
->mmap_mutex
);
3818 static void _perf_event_reset(struct perf_event
*event
)
3820 (void)perf_event_read(event
);
3821 local64_set(&event
->count
, 0);
3822 perf_event_update_userpage(event
);
3826 * Holding the top-level event's child_mutex means that any
3827 * descendant process that has inherited this event will block
3828 * in sync_child_event if it goes to exit, thus satisfying the
3829 * task existence requirements of perf_event_enable/disable.
3831 static void perf_event_for_each_child(struct perf_event
*event
,
3832 void (*func
)(struct perf_event
*))
3834 struct perf_event
*child
;
3836 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
3838 mutex_lock(&event
->child_mutex
);
3840 list_for_each_entry(child
, &event
->child_list
, child_list
)
3842 mutex_unlock(&event
->child_mutex
);
3845 static void perf_event_for_each(struct perf_event
*event
,
3846 void (*func
)(struct perf_event
*))
3848 struct perf_event_context
*ctx
= event
->ctx
;
3849 struct perf_event
*sibling
;
3851 lockdep_assert_held(&ctx
->mutex
);
3853 event
= event
->group_leader
;
3855 perf_event_for_each_child(event
, func
);
3856 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
3857 perf_event_for_each_child(sibling
, func
);
3860 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
3862 struct perf_event_context
*ctx
= event
->ctx
;
3863 int ret
= 0, active
;
3866 if (!is_sampling_event(event
))
3869 if (copy_from_user(&value
, arg
, sizeof(value
)))
3875 raw_spin_lock_irq(&ctx
->lock
);
3876 if (event
->attr
.freq
) {
3877 if (value
> sysctl_perf_event_sample_rate
) {
3882 event
->attr
.sample_freq
= value
;
3884 event
->attr
.sample_period
= value
;
3885 event
->hw
.sample_period
= value
;
3888 active
= (event
->state
== PERF_EVENT_STATE_ACTIVE
);
3890 perf_pmu_disable(ctx
->pmu
);
3891 event
->pmu
->stop(event
, PERF_EF_UPDATE
);
3894 local64_set(&event
->hw
.period_left
, 0);
3897 event
->pmu
->start(event
, PERF_EF_RELOAD
);
3898 perf_pmu_enable(ctx
->pmu
);
3902 raw_spin_unlock_irq(&ctx
->lock
);
3907 static const struct file_operations perf_fops
;
3909 static inline int perf_fget_light(int fd
, struct fd
*p
)
3911 struct fd f
= fdget(fd
);
3915 if (f
.file
->f_op
!= &perf_fops
) {
3923 static int perf_event_set_output(struct perf_event
*event
,
3924 struct perf_event
*output_event
);
3925 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
3927 static long _perf_ioctl(struct perf_event
*event
, unsigned int cmd
, unsigned long arg
)
3929 void (*func
)(struct perf_event
*);
3933 case PERF_EVENT_IOC_ENABLE
:
3934 func
= _perf_event_enable
;
3936 case PERF_EVENT_IOC_DISABLE
:
3937 func
= _perf_event_disable
;
3939 case PERF_EVENT_IOC_RESET
:
3940 func
= _perf_event_reset
;
3943 case PERF_EVENT_IOC_REFRESH
:
3944 return _perf_event_refresh(event
, arg
);
3946 case PERF_EVENT_IOC_PERIOD
:
3947 return perf_event_period(event
, (u64 __user
*)arg
);
3949 case PERF_EVENT_IOC_ID
:
3951 u64 id
= primary_event_id(event
);
3953 if (copy_to_user((void __user
*)arg
, &id
, sizeof(id
)))
3958 case PERF_EVENT_IOC_SET_OUTPUT
:
3962 struct perf_event
*output_event
;
3964 ret
= perf_fget_light(arg
, &output
);
3967 output_event
= output
.file
->private_data
;
3968 ret
= perf_event_set_output(event
, output_event
);
3971 ret
= perf_event_set_output(event
, NULL
);
3976 case PERF_EVENT_IOC_SET_FILTER
:
3977 return perf_event_set_filter(event
, (void __user
*)arg
);
3983 if (flags
& PERF_IOC_FLAG_GROUP
)
3984 perf_event_for_each(event
, func
);
3986 perf_event_for_each_child(event
, func
);
3991 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
3993 struct perf_event
*event
= file
->private_data
;
3994 struct perf_event_context
*ctx
;
3997 ctx
= perf_event_ctx_lock(event
);
3998 ret
= _perf_ioctl(event
, cmd
, arg
);
3999 perf_event_ctx_unlock(event
, ctx
);
4004 #ifdef CONFIG_COMPAT
4005 static long perf_compat_ioctl(struct file
*file
, unsigned int cmd
,
4008 switch (_IOC_NR(cmd
)) {
4009 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER
):
4010 case _IOC_NR(PERF_EVENT_IOC_ID
):
4011 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4012 if (_IOC_SIZE(cmd
) == sizeof(compat_uptr_t
)) {
4013 cmd
&= ~IOCSIZE_MASK
;
4014 cmd
|= sizeof(void *) << IOCSIZE_SHIFT
;
4018 return perf_ioctl(file
, cmd
, arg
);
4021 # define perf_compat_ioctl NULL
4024 int perf_event_task_enable(void)
4026 struct perf_event_context
*ctx
;
4027 struct perf_event
*event
;
4029 mutex_lock(¤t
->perf_event_mutex
);
4030 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
) {
4031 ctx
= perf_event_ctx_lock(event
);
4032 perf_event_for_each_child(event
, _perf_event_enable
);
4033 perf_event_ctx_unlock(event
, ctx
);
4035 mutex_unlock(¤t
->perf_event_mutex
);
4040 int perf_event_task_disable(void)
4042 struct perf_event_context
*ctx
;
4043 struct perf_event
*event
;
4045 mutex_lock(¤t
->perf_event_mutex
);
4046 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
) {
4047 ctx
= perf_event_ctx_lock(event
);
4048 perf_event_for_each_child(event
, _perf_event_disable
);
4049 perf_event_ctx_unlock(event
, ctx
);
4051 mutex_unlock(¤t
->perf_event_mutex
);
4056 static int perf_event_index(struct perf_event
*event
)
4058 if (event
->hw
.state
& PERF_HES_STOPPED
)
4061 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
4064 return event
->pmu
->event_idx(event
);
4067 static void calc_timer_values(struct perf_event
*event
,
4074 *now
= perf_clock();
4075 ctx_time
= event
->shadow_ctx_time
+ *now
;
4076 *enabled
= ctx_time
- event
->tstamp_enabled
;
4077 *running
= ctx_time
- event
->tstamp_running
;
4080 static void perf_event_init_userpage(struct perf_event
*event
)
4082 struct perf_event_mmap_page
*userpg
;
4083 struct ring_buffer
*rb
;
4086 rb
= rcu_dereference(event
->rb
);
4090 userpg
= rb
->user_page
;
4092 /* Allow new userspace to detect that bit 0 is deprecated */
4093 userpg
->cap_bit0_is_deprecated
= 1;
4094 userpg
->size
= offsetof(struct perf_event_mmap_page
, __reserved
);
4100 void __weak
arch_perf_update_userpage(
4101 struct perf_event
*event
, struct perf_event_mmap_page
*userpg
, u64 now
)
4106 * Callers need to ensure there can be no nesting of this function, otherwise
4107 * the seqlock logic goes bad. We can not serialize this because the arch
4108 * code calls this from NMI context.
4110 void perf_event_update_userpage(struct perf_event
*event
)
4112 struct perf_event_mmap_page
*userpg
;
4113 struct ring_buffer
*rb
;
4114 u64 enabled
, running
, now
;
4117 rb
= rcu_dereference(event
->rb
);
4122 * compute total_time_enabled, total_time_running
4123 * based on snapshot values taken when the event
4124 * was last scheduled in.
4126 * we cannot simply called update_context_time()
4127 * because of locking issue as we can be called in
4130 calc_timer_values(event
, &now
, &enabled
, &running
);
4132 userpg
= rb
->user_page
;
4134 * Disable preemption so as to not let the corresponding user-space
4135 * spin too long if we get preempted.
4140 userpg
->index
= perf_event_index(event
);
4141 userpg
->offset
= perf_event_count(event
);
4143 userpg
->offset
-= local64_read(&event
->hw
.prev_count
);
4145 userpg
->time_enabled
= enabled
+
4146 atomic64_read(&event
->child_total_time_enabled
);
4148 userpg
->time_running
= running
+
4149 atomic64_read(&event
->child_total_time_running
);
4151 arch_perf_update_userpage(event
, userpg
, now
);
4160 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
4162 struct perf_event
*event
= vma
->vm_file
->private_data
;
4163 struct ring_buffer
*rb
;
4164 int ret
= VM_FAULT_SIGBUS
;
4166 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
4167 if (vmf
->pgoff
== 0)
4173 rb
= rcu_dereference(event
->rb
);
4177 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
4180 vmf
->page
= perf_mmap_to_page(rb
, vmf
->pgoff
);
4184 get_page(vmf
->page
);
4185 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
4186 vmf
->page
->index
= vmf
->pgoff
;
4195 static void ring_buffer_attach(struct perf_event
*event
,
4196 struct ring_buffer
*rb
)
4198 struct ring_buffer
*old_rb
= NULL
;
4199 unsigned long flags
;
4203 * Should be impossible, we set this when removing
4204 * event->rb_entry and wait/clear when adding event->rb_entry.
4206 WARN_ON_ONCE(event
->rcu_pending
);
4209 event
->rcu_batches
= get_state_synchronize_rcu();
4210 event
->rcu_pending
= 1;
4212 spin_lock_irqsave(&old_rb
->event_lock
, flags
);
4213 list_del_rcu(&event
->rb_entry
);
4214 spin_unlock_irqrestore(&old_rb
->event_lock
, flags
);
4217 if (event
->rcu_pending
&& rb
) {
4218 cond_synchronize_rcu(event
->rcu_batches
);
4219 event
->rcu_pending
= 0;
4223 spin_lock_irqsave(&rb
->event_lock
, flags
);
4224 list_add_rcu(&event
->rb_entry
, &rb
->event_list
);
4225 spin_unlock_irqrestore(&rb
->event_lock
, flags
);
4228 rcu_assign_pointer(event
->rb
, rb
);
4231 ring_buffer_put(old_rb
);
4233 * Since we detached before setting the new rb, so that we
4234 * could attach the new rb, we could have missed a wakeup.
4237 wake_up_all(&event
->waitq
);
4241 static void ring_buffer_wakeup(struct perf_event
*event
)
4243 struct ring_buffer
*rb
;
4246 rb
= rcu_dereference(event
->rb
);
4248 list_for_each_entry_rcu(event
, &rb
->event_list
, rb_entry
)
4249 wake_up_all(&event
->waitq
);
4254 static void rb_free_rcu(struct rcu_head
*rcu_head
)
4256 struct ring_buffer
*rb
;
4258 rb
= container_of(rcu_head
, struct ring_buffer
, rcu_head
);
4262 static struct ring_buffer
*ring_buffer_get(struct perf_event
*event
)
4264 struct ring_buffer
*rb
;
4267 rb
= rcu_dereference(event
->rb
);
4269 if (!atomic_inc_not_zero(&rb
->refcount
))
4277 static void ring_buffer_put(struct ring_buffer
*rb
)
4279 if (!atomic_dec_and_test(&rb
->refcount
))
4282 WARN_ON_ONCE(!list_empty(&rb
->event_list
));
4284 call_rcu(&rb
->rcu_head
, rb_free_rcu
);
4287 static void perf_mmap_open(struct vm_area_struct
*vma
)
4289 struct perf_event
*event
= vma
->vm_file
->private_data
;
4291 atomic_inc(&event
->mmap_count
);
4292 atomic_inc(&event
->rb
->mmap_count
);
4294 if (event
->pmu
->event_mapped
)
4295 event
->pmu
->event_mapped(event
);
4299 * A buffer can be mmap()ed multiple times; either directly through the same
4300 * event, or through other events by use of perf_event_set_output().
4302 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4303 * the buffer here, where we still have a VM context. This means we need
4304 * to detach all events redirecting to us.
4306 static void perf_mmap_close(struct vm_area_struct
*vma
)
4308 struct perf_event
*event
= vma
->vm_file
->private_data
;
4310 struct ring_buffer
*rb
= ring_buffer_get(event
);
4311 struct user_struct
*mmap_user
= rb
->mmap_user
;
4312 int mmap_locked
= rb
->mmap_locked
;
4313 unsigned long size
= perf_data_size(rb
);
4315 if (event
->pmu
->event_unmapped
)
4316 event
->pmu
->event_unmapped(event
);
4318 atomic_dec(&rb
->mmap_count
);
4320 if (!atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
))
4323 ring_buffer_attach(event
, NULL
);
4324 mutex_unlock(&event
->mmap_mutex
);
4326 /* If there's still other mmap()s of this buffer, we're done. */
4327 if (atomic_read(&rb
->mmap_count
))
4331 * No other mmap()s, detach from all other events that might redirect
4332 * into the now unreachable buffer. Somewhat complicated by the
4333 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4337 list_for_each_entry_rcu(event
, &rb
->event_list
, rb_entry
) {
4338 if (!atomic_long_inc_not_zero(&event
->refcount
)) {
4340 * This event is en-route to free_event() which will
4341 * detach it and remove it from the list.
4347 mutex_lock(&event
->mmap_mutex
);
4349 * Check we didn't race with perf_event_set_output() which can
4350 * swizzle the rb from under us while we were waiting to
4351 * acquire mmap_mutex.
4353 * If we find a different rb; ignore this event, a next
4354 * iteration will no longer find it on the list. We have to
4355 * still restart the iteration to make sure we're not now
4356 * iterating the wrong list.
4358 if (event
->rb
== rb
)
4359 ring_buffer_attach(event
, NULL
);
4361 mutex_unlock(&event
->mmap_mutex
);
4365 * Restart the iteration; either we're on the wrong list or
4366 * destroyed its integrity by doing a deletion.
4373 * It could be there's still a few 0-ref events on the list; they'll
4374 * get cleaned up by free_event() -- they'll also still have their
4375 * ref on the rb and will free it whenever they are done with it.
4377 * Aside from that, this buffer is 'fully' detached and unmapped,
4378 * undo the VM accounting.
4381 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &mmap_user
->locked_vm
);
4382 vma
->vm_mm
->pinned_vm
-= mmap_locked
;
4383 free_uid(mmap_user
);
4386 ring_buffer_put(rb
); /* could be last */
4389 static const struct vm_operations_struct perf_mmap_vmops
= {
4390 .open
= perf_mmap_open
,
4391 .close
= perf_mmap_close
,
4392 .fault
= perf_mmap_fault
,
4393 .page_mkwrite
= perf_mmap_fault
,
4396 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
4398 struct perf_event
*event
= file
->private_data
;
4399 unsigned long user_locked
, user_lock_limit
;
4400 struct user_struct
*user
= current_user();
4401 unsigned long locked
, lock_limit
;
4402 struct ring_buffer
*rb
;
4403 unsigned long vma_size
;
4404 unsigned long nr_pages
;
4405 long user_extra
, extra
;
4406 int ret
= 0, flags
= 0;
4409 * Don't allow mmap() of inherited per-task counters. This would
4410 * create a performance issue due to all children writing to the
4413 if (event
->cpu
== -1 && event
->attr
.inherit
)
4416 if (!(vma
->vm_flags
& VM_SHARED
))
4419 vma_size
= vma
->vm_end
- vma
->vm_start
;
4420 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
4423 * If we have rb pages ensure they're a power-of-two number, so we
4424 * can do bitmasks instead of modulo.
4426 if (!is_power_of_2(nr_pages
))
4429 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
4432 if (vma
->vm_pgoff
!= 0)
4435 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
4437 mutex_lock(&event
->mmap_mutex
);
4439 if (event
->rb
->nr_pages
!= nr_pages
) {
4444 if (!atomic_inc_not_zero(&event
->rb
->mmap_count
)) {
4446 * Raced against perf_mmap_close() through
4447 * perf_event_set_output(). Try again, hope for better
4450 mutex_unlock(&event
->mmap_mutex
);
4457 user_extra
= nr_pages
+ 1;
4458 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
4461 * Increase the limit linearly with more CPUs:
4463 user_lock_limit
*= num_online_cpus();
4465 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
4468 if (user_locked
> user_lock_limit
)
4469 extra
= user_locked
- user_lock_limit
;
4471 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
4472 lock_limit
>>= PAGE_SHIFT
;
4473 locked
= vma
->vm_mm
->pinned_vm
+ extra
;
4475 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
4476 !capable(CAP_IPC_LOCK
)) {
4483 if (vma
->vm_flags
& VM_WRITE
)
4484 flags
|= RING_BUFFER_WRITABLE
;
4486 rb
= rb_alloc(nr_pages
,
4487 event
->attr
.watermark
? event
->attr
.wakeup_watermark
: 0,
4495 atomic_set(&rb
->mmap_count
, 1);
4496 rb
->mmap_locked
= extra
;
4497 rb
->mmap_user
= get_current_user();
4499 atomic_long_add(user_extra
, &user
->locked_vm
);
4500 vma
->vm_mm
->pinned_vm
+= extra
;
4502 ring_buffer_attach(event
, rb
);
4504 perf_event_init_userpage(event
);
4505 perf_event_update_userpage(event
);
4509 atomic_inc(&event
->mmap_count
);
4510 mutex_unlock(&event
->mmap_mutex
);
4513 * Since pinned accounting is per vm we cannot allow fork() to copy our
4516 vma
->vm_flags
|= VM_DONTCOPY
| VM_DONTEXPAND
| VM_DONTDUMP
;
4517 vma
->vm_ops
= &perf_mmap_vmops
;
4519 if (event
->pmu
->event_mapped
)
4520 event
->pmu
->event_mapped(event
);
4525 static int perf_fasync(int fd
, struct file
*filp
, int on
)
4527 struct inode
*inode
= file_inode(filp
);
4528 struct perf_event
*event
= filp
->private_data
;
4531 mutex_lock(&inode
->i_mutex
);
4532 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
4533 mutex_unlock(&inode
->i_mutex
);
4541 static const struct file_operations perf_fops
= {
4542 .llseek
= no_llseek
,
4543 .release
= perf_release
,
4546 .unlocked_ioctl
= perf_ioctl
,
4547 .compat_ioctl
= perf_compat_ioctl
,
4549 .fasync
= perf_fasync
,
4555 * If there's data, ensure we set the poll() state and publish everything
4556 * to user-space before waking everybody up.
4559 void perf_event_wakeup(struct perf_event
*event
)
4561 ring_buffer_wakeup(event
);
4563 if (event
->pending_kill
) {
4564 kill_fasync(&event
->fasync
, SIGIO
, event
->pending_kill
);
4565 event
->pending_kill
= 0;
4569 static void perf_pending_event(struct irq_work
*entry
)
4571 struct perf_event
*event
= container_of(entry
,
4572 struct perf_event
, pending
);
4574 if (event
->pending_disable
) {
4575 event
->pending_disable
= 0;
4576 __perf_event_disable(event
);
4579 if (event
->pending_wakeup
) {
4580 event
->pending_wakeup
= 0;
4581 perf_event_wakeup(event
);
4586 * We assume there is only KVM supporting the callbacks.
4587 * Later on, we might change it to a list if there is
4588 * another virtualization implementation supporting the callbacks.
4590 struct perf_guest_info_callbacks
*perf_guest_cbs
;
4592 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
4594 perf_guest_cbs
= cbs
;
4597 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks
);
4599 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks
*cbs
)
4601 perf_guest_cbs
= NULL
;
4604 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks
);
4607 perf_output_sample_regs(struct perf_output_handle
*handle
,
4608 struct pt_regs
*regs
, u64 mask
)
4612 for_each_set_bit(bit
, (const unsigned long *) &mask
,
4613 sizeof(mask
) * BITS_PER_BYTE
) {
4616 val
= perf_reg_value(regs
, bit
);
4617 perf_output_put(handle
, val
);
4621 static void perf_sample_regs_user(struct perf_regs
*regs_user
,
4622 struct pt_regs
*regs
,
4623 struct pt_regs
*regs_user_copy
)
4625 if (user_mode(regs
)) {
4626 regs_user
->abi
= perf_reg_abi(current
);
4627 regs_user
->regs
= regs
;
4628 } else if (current
->mm
) {
4629 perf_get_regs_user(regs_user
, regs
, regs_user_copy
);
4631 regs_user
->abi
= PERF_SAMPLE_REGS_ABI_NONE
;
4632 regs_user
->regs
= NULL
;
4636 static void perf_sample_regs_intr(struct perf_regs
*regs_intr
,
4637 struct pt_regs
*regs
)
4639 regs_intr
->regs
= regs
;
4640 regs_intr
->abi
= perf_reg_abi(current
);
4645 * Get remaining task size from user stack pointer.
4647 * It'd be better to take stack vma map and limit this more
4648 * precisly, but there's no way to get it safely under interrupt,
4649 * so using TASK_SIZE as limit.
4651 static u64
perf_ustack_task_size(struct pt_regs
*regs
)
4653 unsigned long addr
= perf_user_stack_pointer(regs
);
4655 if (!addr
|| addr
>= TASK_SIZE
)
4658 return TASK_SIZE
- addr
;
4662 perf_sample_ustack_size(u16 stack_size
, u16 header_size
,
4663 struct pt_regs
*regs
)
4667 /* No regs, no stack pointer, no dump. */
4672 * Check if we fit in with the requested stack size into the:
4674 * If we don't, we limit the size to the TASK_SIZE.
4676 * - remaining sample size
4677 * If we don't, we customize the stack size to
4678 * fit in to the remaining sample size.
4681 task_size
= min((u64
) USHRT_MAX
, perf_ustack_task_size(regs
));
4682 stack_size
= min(stack_size
, (u16
) task_size
);
4684 /* Current header size plus static size and dynamic size. */
4685 header_size
+= 2 * sizeof(u64
);
4687 /* Do we fit in with the current stack dump size? */
4688 if ((u16
) (header_size
+ stack_size
) < header_size
) {
4690 * If we overflow the maximum size for the sample,
4691 * we customize the stack dump size to fit in.
4693 stack_size
= USHRT_MAX
- header_size
- sizeof(u64
);
4694 stack_size
= round_up(stack_size
, sizeof(u64
));
4701 perf_output_sample_ustack(struct perf_output_handle
*handle
, u64 dump_size
,
4702 struct pt_regs
*regs
)
4704 /* Case of a kernel thread, nothing to dump */
4707 perf_output_put(handle
, size
);
4716 * - the size requested by user or the best one we can fit
4717 * in to the sample max size
4719 * - user stack dump data
4721 * - the actual dumped size
4725 perf_output_put(handle
, dump_size
);
4728 sp
= perf_user_stack_pointer(regs
);
4729 rem
= __output_copy_user(handle
, (void *) sp
, dump_size
);
4730 dyn_size
= dump_size
- rem
;
4732 perf_output_skip(handle
, rem
);
4735 perf_output_put(handle
, dyn_size
);
4739 static void __perf_event_header__init_id(struct perf_event_header
*header
,
4740 struct perf_sample_data
*data
,
4741 struct perf_event
*event
)
4743 u64 sample_type
= event
->attr
.sample_type
;
4745 data
->type
= sample_type
;
4746 header
->size
+= event
->id_header_size
;
4748 if (sample_type
& PERF_SAMPLE_TID
) {
4749 /* namespace issues */
4750 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
4751 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
4754 if (sample_type
& PERF_SAMPLE_TIME
)
4755 data
->time
= perf_clock();
4757 if (sample_type
& (PERF_SAMPLE_ID
| PERF_SAMPLE_IDENTIFIER
))
4758 data
->id
= primary_event_id(event
);
4760 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4761 data
->stream_id
= event
->id
;
4763 if (sample_type
& PERF_SAMPLE_CPU
) {
4764 data
->cpu_entry
.cpu
= raw_smp_processor_id();
4765 data
->cpu_entry
.reserved
= 0;
4769 void perf_event_header__init_id(struct perf_event_header
*header
,
4770 struct perf_sample_data
*data
,
4771 struct perf_event
*event
)
4773 if (event
->attr
.sample_id_all
)
4774 __perf_event_header__init_id(header
, data
, event
);
4777 static void __perf_event__output_id_sample(struct perf_output_handle
*handle
,
4778 struct perf_sample_data
*data
)
4780 u64 sample_type
= data
->type
;
4782 if (sample_type
& PERF_SAMPLE_TID
)
4783 perf_output_put(handle
, data
->tid_entry
);
4785 if (sample_type
& PERF_SAMPLE_TIME
)
4786 perf_output_put(handle
, data
->time
);
4788 if (sample_type
& PERF_SAMPLE_ID
)
4789 perf_output_put(handle
, data
->id
);
4791 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4792 perf_output_put(handle
, data
->stream_id
);
4794 if (sample_type
& PERF_SAMPLE_CPU
)
4795 perf_output_put(handle
, data
->cpu_entry
);
4797 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
4798 perf_output_put(handle
, data
->id
);
4801 void perf_event__output_id_sample(struct perf_event
*event
,
4802 struct perf_output_handle
*handle
,
4803 struct perf_sample_data
*sample
)
4805 if (event
->attr
.sample_id_all
)
4806 __perf_event__output_id_sample(handle
, sample
);
4809 static void perf_output_read_one(struct perf_output_handle
*handle
,
4810 struct perf_event
*event
,
4811 u64 enabled
, u64 running
)
4813 u64 read_format
= event
->attr
.read_format
;
4817 values
[n
++] = perf_event_count(event
);
4818 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
4819 values
[n
++] = enabled
+
4820 atomic64_read(&event
->child_total_time_enabled
);
4822 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
4823 values
[n
++] = running
+
4824 atomic64_read(&event
->child_total_time_running
);
4826 if (read_format
& PERF_FORMAT_ID
)
4827 values
[n
++] = primary_event_id(event
);
4829 __output_copy(handle
, values
, n
* sizeof(u64
));
4833 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4835 static void perf_output_read_group(struct perf_output_handle
*handle
,
4836 struct perf_event
*event
,
4837 u64 enabled
, u64 running
)
4839 struct perf_event
*leader
= event
->group_leader
, *sub
;
4840 u64 read_format
= event
->attr
.read_format
;
4844 values
[n
++] = 1 + leader
->nr_siblings
;
4846 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
4847 values
[n
++] = enabled
;
4849 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
4850 values
[n
++] = running
;
4852 if (leader
!= event
)
4853 leader
->pmu
->read(leader
);
4855 values
[n
++] = perf_event_count(leader
);
4856 if (read_format
& PERF_FORMAT_ID
)
4857 values
[n
++] = primary_event_id(leader
);
4859 __output_copy(handle
, values
, n
* sizeof(u64
));
4861 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
4864 if ((sub
!= event
) &&
4865 (sub
->state
== PERF_EVENT_STATE_ACTIVE
))
4866 sub
->pmu
->read(sub
);
4868 values
[n
++] = perf_event_count(sub
);
4869 if (read_format
& PERF_FORMAT_ID
)
4870 values
[n
++] = primary_event_id(sub
);
4872 __output_copy(handle
, values
, n
* sizeof(u64
));
4876 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4877 PERF_FORMAT_TOTAL_TIME_RUNNING)
4879 static void perf_output_read(struct perf_output_handle
*handle
,
4880 struct perf_event
*event
)
4882 u64 enabled
= 0, running
= 0, now
;
4883 u64 read_format
= event
->attr
.read_format
;
4886 * compute total_time_enabled, total_time_running
4887 * based on snapshot values taken when the event
4888 * was last scheduled in.
4890 * we cannot simply called update_context_time()
4891 * because of locking issue as we are called in
4894 if (read_format
& PERF_FORMAT_TOTAL_TIMES
)
4895 calc_timer_values(event
, &now
, &enabled
, &running
);
4897 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
4898 perf_output_read_group(handle
, event
, enabled
, running
);
4900 perf_output_read_one(handle
, event
, enabled
, running
);
4903 void perf_output_sample(struct perf_output_handle
*handle
,
4904 struct perf_event_header
*header
,
4905 struct perf_sample_data
*data
,
4906 struct perf_event
*event
)
4908 u64 sample_type
= data
->type
;
4910 perf_output_put(handle
, *header
);
4912 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
4913 perf_output_put(handle
, data
->id
);
4915 if (sample_type
& PERF_SAMPLE_IP
)
4916 perf_output_put(handle
, data
->ip
);
4918 if (sample_type
& PERF_SAMPLE_TID
)
4919 perf_output_put(handle
, data
->tid_entry
);
4921 if (sample_type
& PERF_SAMPLE_TIME
)
4922 perf_output_put(handle
, data
->time
);
4924 if (sample_type
& PERF_SAMPLE_ADDR
)
4925 perf_output_put(handle
, data
->addr
);
4927 if (sample_type
& PERF_SAMPLE_ID
)
4928 perf_output_put(handle
, data
->id
);
4930 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
4931 perf_output_put(handle
, data
->stream_id
);
4933 if (sample_type
& PERF_SAMPLE_CPU
)
4934 perf_output_put(handle
, data
->cpu_entry
);
4936 if (sample_type
& PERF_SAMPLE_PERIOD
)
4937 perf_output_put(handle
, data
->period
);
4939 if (sample_type
& PERF_SAMPLE_READ
)
4940 perf_output_read(handle
, event
);
4942 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
4943 if (data
->callchain
) {
4946 if (data
->callchain
)
4947 size
+= data
->callchain
->nr
;
4949 size
*= sizeof(u64
);
4951 __output_copy(handle
, data
->callchain
, size
);
4954 perf_output_put(handle
, nr
);
4958 if (sample_type
& PERF_SAMPLE_RAW
) {
4960 perf_output_put(handle
, data
->raw
->size
);
4961 __output_copy(handle
, data
->raw
->data
,
4968 .size
= sizeof(u32
),
4971 perf_output_put(handle
, raw
);
4975 if (sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
4976 if (data
->br_stack
) {
4979 size
= data
->br_stack
->nr
4980 * sizeof(struct perf_branch_entry
);
4982 perf_output_put(handle
, data
->br_stack
->nr
);
4983 perf_output_copy(handle
, data
->br_stack
->entries
, size
);
4986 * we always store at least the value of nr
4989 perf_output_put(handle
, nr
);
4993 if (sample_type
& PERF_SAMPLE_REGS_USER
) {
4994 u64 abi
= data
->regs_user
.abi
;
4997 * If there are no regs to dump, notice it through
4998 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5000 perf_output_put(handle
, abi
);
5003 u64 mask
= event
->attr
.sample_regs_user
;
5004 perf_output_sample_regs(handle
,
5005 data
->regs_user
.regs
,
5010 if (sample_type
& PERF_SAMPLE_STACK_USER
) {
5011 perf_output_sample_ustack(handle
,
5012 data
->stack_user_size
,
5013 data
->regs_user
.regs
);
5016 if (sample_type
& PERF_SAMPLE_WEIGHT
)
5017 perf_output_put(handle
, data
->weight
);
5019 if (sample_type
& PERF_SAMPLE_DATA_SRC
)
5020 perf_output_put(handle
, data
->data_src
.val
);
5022 if (sample_type
& PERF_SAMPLE_TRANSACTION
)
5023 perf_output_put(handle
, data
->txn
);
5025 if (sample_type
& PERF_SAMPLE_REGS_INTR
) {
5026 u64 abi
= data
->regs_intr
.abi
;
5028 * If there are no regs to dump, notice it through
5029 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5031 perf_output_put(handle
, abi
);
5034 u64 mask
= event
->attr
.sample_regs_intr
;
5036 perf_output_sample_regs(handle
,
5037 data
->regs_intr
.regs
,
5042 if (!event
->attr
.watermark
) {
5043 int wakeup_events
= event
->attr
.wakeup_events
;
5045 if (wakeup_events
) {
5046 struct ring_buffer
*rb
= handle
->rb
;
5047 int events
= local_inc_return(&rb
->events
);
5049 if (events
>= wakeup_events
) {
5050 local_sub(wakeup_events
, &rb
->events
);
5051 local_inc(&rb
->wakeup
);
5057 void perf_prepare_sample(struct perf_event_header
*header
,
5058 struct perf_sample_data
*data
,
5059 struct perf_event
*event
,
5060 struct pt_regs
*regs
)
5062 u64 sample_type
= event
->attr
.sample_type
;
5064 header
->type
= PERF_RECORD_SAMPLE
;
5065 header
->size
= sizeof(*header
) + event
->header_size
;
5068 header
->misc
|= perf_misc_flags(regs
);
5070 __perf_event_header__init_id(header
, data
, event
);
5072 if (sample_type
& PERF_SAMPLE_IP
)
5073 data
->ip
= perf_instruction_pointer(regs
);
5075 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
5078 data
->callchain
= perf_callchain(event
, regs
);
5080 if (data
->callchain
)
5081 size
+= data
->callchain
->nr
;
5083 header
->size
+= size
* sizeof(u64
);
5086 if (sample_type
& PERF_SAMPLE_RAW
) {
5087 int size
= sizeof(u32
);
5090 size
+= data
->raw
->size
;
5092 size
+= sizeof(u32
);
5094 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
5095 header
->size
+= size
;
5098 if (sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
5099 int size
= sizeof(u64
); /* nr */
5100 if (data
->br_stack
) {
5101 size
+= data
->br_stack
->nr
5102 * sizeof(struct perf_branch_entry
);
5104 header
->size
+= size
;
5107 if (sample_type
& (PERF_SAMPLE_REGS_USER
| PERF_SAMPLE_STACK_USER
))
5108 perf_sample_regs_user(&data
->regs_user
, regs
,
5109 &data
->regs_user_copy
);
5111 if (sample_type
& PERF_SAMPLE_REGS_USER
) {
5112 /* regs dump ABI info */
5113 int size
= sizeof(u64
);
5115 if (data
->regs_user
.regs
) {
5116 u64 mask
= event
->attr
.sample_regs_user
;
5117 size
+= hweight64(mask
) * sizeof(u64
);
5120 header
->size
+= size
;
5123 if (sample_type
& PERF_SAMPLE_STACK_USER
) {
5125 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5126 * processed as the last one or have additional check added
5127 * in case new sample type is added, because we could eat
5128 * up the rest of the sample size.
5130 u16 stack_size
= event
->attr
.sample_stack_user
;
5131 u16 size
= sizeof(u64
);
5133 stack_size
= perf_sample_ustack_size(stack_size
, header
->size
,
5134 data
->regs_user
.regs
);
5137 * If there is something to dump, add space for the dump
5138 * itself and for the field that tells the dynamic size,
5139 * which is how many have been actually dumped.
5142 size
+= sizeof(u64
) + stack_size
;
5144 data
->stack_user_size
= stack_size
;
5145 header
->size
+= size
;
5148 if (sample_type
& PERF_SAMPLE_REGS_INTR
) {
5149 /* regs dump ABI info */
5150 int size
= sizeof(u64
);
5152 perf_sample_regs_intr(&data
->regs_intr
, regs
);
5154 if (data
->regs_intr
.regs
) {
5155 u64 mask
= event
->attr
.sample_regs_intr
;
5157 size
+= hweight64(mask
) * sizeof(u64
);
5160 header
->size
+= size
;
5164 static void perf_event_output(struct perf_event
*event
,
5165 struct perf_sample_data
*data
,
5166 struct pt_regs
*regs
)
5168 struct perf_output_handle handle
;
5169 struct perf_event_header header
;
5171 /* protect the callchain buffers */
5174 perf_prepare_sample(&header
, data
, event
, regs
);
5176 if (perf_output_begin(&handle
, event
, header
.size
))
5179 perf_output_sample(&handle
, &header
, data
, event
);
5181 perf_output_end(&handle
);
5191 struct perf_read_event
{
5192 struct perf_event_header header
;
5199 perf_event_read_event(struct perf_event
*event
,
5200 struct task_struct
*task
)
5202 struct perf_output_handle handle
;
5203 struct perf_sample_data sample
;
5204 struct perf_read_event read_event
= {
5206 .type
= PERF_RECORD_READ
,
5208 .size
= sizeof(read_event
) + event
->read_size
,
5210 .pid
= perf_event_pid(event
, task
),
5211 .tid
= perf_event_tid(event
, task
),
5215 perf_event_header__init_id(&read_event
.header
, &sample
, event
);
5216 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
);
5220 perf_output_put(&handle
, read_event
);
5221 perf_output_read(&handle
, event
);
5222 perf_event__output_id_sample(event
, &handle
, &sample
);
5224 perf_output_end(&handle
);
5227 typedef void (perf_event_aux_output_cb
)(struct perf_event
*event
, void *data
);
5230 perf_event_aux_ctx(struct perf_event_context
*ctx
,
5231 perf_event_aux_output_cb output
,
5234 struct perf_event
*event
;
5236 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
5237 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
5239 if (!event_filter_match(event
))
5241 output(event
, data
);
5246 perf_event_aux(perf_event_aux_output_cb output
, void *data
,
5247 struct perf_event_context
*task_ctx
)
5249 struct perf_cpu_context
*cpuctx
;
5250 struct perf_event_context
*ctx
;
5255 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
5256 cpuctx
= get_cpu_ptr(pmu
->pmu_cpu_context
);
5257 if (cpuctx
->unique_pmu
!= pmu
)
5259 perf_event_aux_ctx(&cpuctx
->ctx
, output
, data
);
5262 ctxn
= pmu
->task_ctx_nr
;
5265 ctx
= rcu_dereference(current
->perf_event_ctxp
[ctxn
]);
5267 perf_event_aux_ctx(ctx
, output
, data
);
5269 put_cpu_ptr(pmu
->pmu_cpu_context
);
5274 perf_event_aux_ctx(task_ctx
, output
, data
);
5281 * task tracking -- fork/exit
5283 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
5286 struct perf_task_event
{
5287 struct task_struct
*task
;
5288 struct perf_event_context
*task_ctx
;
5291 struct perf_event_header header
;
5301 static int perf_event_task_match(struct perf_event
*event
)
5303 return event
->attr
.comm
|| event
->attr
.mmap
||
5304 event
->attr
.mmap2
|| event
->attr
.mmap_data
||
5308 static void perf_event_task_output(struct perf_event
*event
,
5311 struct perf_task_event
*task_event
= data
;
5312 struct perf_output_handle handle
;
5313 struct perf_sample_data sample
;
5314 struct task_struct
*task
= task_event
->task
;
5315 int ret
, size
= task_event
->event_id
.header
.size
;
5317 if (!perf_event_task_match(event
))
5320 perf_event_header__init_id(&task_event
->event_id
.header
, &sample
, event
);
5322 ret
= perf_output_begin(&handle
, event
,
5323 task_event
->event_id
.header
.size
);
5327 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
5328 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
5330 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
5331 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
5333 perf_output_put(&handle
, task_event
->event_id
);
5335 perf_event__output_id_sample(event
, &handle
, &sample
);
5337 perf_output_end(&handle
);
5339 task_event
->event_id
.header
.size
= size
;
5342 static void perf_event_task(struct task_struct
*task
,
5343 struct perf_event_context
*task_ctx
,
5346 struct perf_task_event task_event
;
5348 if (!atomic_read(&nr_comm_events
) &&
5349 !atomic_read(&nr_mmap_events
) &&
5350 !atomic_read(&nr_task_events
))
5353 task_event
= (struct perf_task_event
){
5355 .task_ctx
= task_ctx
,
5358 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
5360 .size
= sizeof(task_event
.event_id
),
5366 .time
= perf_clock(),
5370 perf_event_aux(perf_event_task_output
,
5375 void perf_event_fork(struct task_struct
*task
)
5377 perf_event_task(task
, NULL
, 1);
5384 struct perf_comm_event
{
5385 struct task_struct
*task
;
5390 struct perf_event_header header
;
5397 static int perf_event_comm_match(struct perf_event
*event
)
5399 return event
->attr
.comm
;
5402 static void perf_event_comm_output(struct perf_event
*event
,
5405 struct perf_comm_event
*comm_event
= data
;
5406 struct perf_output_handle handle
;
5407 struct perf_sample_data sample
;
5408 int size
= comm_event
->event_id
.header
.size
;
5411 if (!perf_event_comm_match(event
))
5414 perf_event_header__init_id(&comm_event
->event_id
.header
, &sample
, event
);
5415 ret
= perf_output_begin(&handle
, event
,
5416 comm_event
->event_id
.header
.size
);
5421 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
5422 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
5424 perf_output_put(&handle
, comm_event
->event_id
);
5425 __output_copy(&handle
, comm_event
->comm
,
5426 comm_event
->comm_size
);
5428 perf_event__output_id_sample(event
, &handle
, &sample
);
5430 perf_output_end(&handle
);
5432 comm_event
->event_id
.header
.size
= size
;
5435 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
5437 char comm
[TASK_COMM_LEN
];
5440 memset(comm
, 0, sizeof(comm
));
5441 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
5442 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
5444 comm_event
->comm
= comm
;
5445 comm_event
->comm_size
= size
;
5447 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
5449 perf_event_aux(perf_event_comm_output
,
5454 void perf_event_comm(struct task_struct
*task
, bool exec
)
5456 struct perf_comm_event comm_event
;
5458 if (!atomic_read(&nr_comm_events
))
5461 comm_event
= (struct perf_comm_event
){
5467 .type
= PERF_RECORD_COMM
,
5468 .misc
= exec
? PERF_RECORD_MISC_COMM_EXEC
: 0,
5476 perf_event_comm_event(&comm_event
);
5483 struct perf_mmap_event
{
5484 struct vm_area_struct
*vma
;
5486 const char *file_name
;
5494 struct perf_event_header header
;
5504 static int perf_event_mmap_match(struct perf_event
*event
,
5507 struct perf_mmap_event
*mmap_event
= data
;
5508 struct vm_area_struct
*vma
= mmap_event
->vma
;
5509 int executable
= vma
->vm_flags
& VM_EXEC
;
5511 return (!executable
&& event
->attr
.mmap_data
) ||
5512 (executable
&& (event
->attr
.mmap
|| event
->attr
.mmap2
));
5515 static void perf_event_mmap_output(struct perf_event
*event
,
5518 struct perf_mmap_event
*mmap_event
= data
;
5519 struct perf_output_handle handle
;
5520 struct perf_sample_data sample
;
5521 int size
= mmap_event
->event_id
.header
.size
;
5524 if (!perf_event_mmap_match(event
, data
))
5527 if (event
->attr
.mmap2
) {
5528 mmap_event
->event_id
.header
.type
= PERF_RECORD_MMAP2
;
5529 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->maj
);
5530 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->min
);
5531 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->ino
);
5532 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->ino_generation
);
5533 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->prot
);
5534 mmap_event
->event_id
.header
.size
+= sizeof(mmap_event
->flags
);
5537 perf_event_header__init_id(&mmap_event
->event_id
.header
, &sample
, event
);
5538 ret
= perf_output_begin(&handle
, event
,
5539 mmap_event
->event_id
.header
.size
);
5543 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
5544 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
5546 perf_output_put(&handle
, mmap_event
->event_id
);
5548 if (event
->attr
.mmap2
) {
5549 perf_output_put(&handle
, mmap_event
->maj
);
5550 perf_output_put(&handle
, mmap_event
->min
);
5551 perf_output_put(&handle
, mmap_event
->ino
);
5552 perf_output_put(&handle
, mmap_event
->ino_generation
);
5553 perf_output_put(&handle
, mmap_event
->prot
);
5554 perf_output_put(&handle
, mmap_event
->flags
);
5557 __output_copy(&handle
, mmap_event
->file_name
,
5558 mmap_event
->file_size
);
5560 perf_event__output_id_sample(event
, &handle
, &sample
);
5562 perf_output_end(&handle
);
5564 mmap_event
->event_id
.header
.size
= size
;
5567 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
5569 struct vm_area_struct
*vma
= mmap_event
->vma
;
5570 struct file
*file
= vma
->vm_file
;
5571 int maj
= 0, min
= 0;
5572 u64 ino
= 0, gen
= 0;
5573 u32 prot
= 0, flags
= 0;
5580 struct inode
*inode
;
5583 buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
5589 * d_path() works from the end of the rb backwards, so we
5590 * need to add enough zero bytes after the string to handle
5591 * the 64bit alignment we do later.
5593 name
= d_path(&file
->f_path
, buf
, PATH_MAX
- sizeof(u64
));
5598 inode
= file_inode(vma
->vm_file
);
5599 dev
= inode
->i_sb
->s_dev
;
5601 gen
= inode
->i_generation
;
5605 if (vma
->vm_flags
& VM_READ
)
5607 if (vma
->vm_flags
& VM_WRITE
)
5609 if (vma
->vm_flags
& VM_EXEC
)
5612 if (vma
->vm_flags
& VM_MAYSHARE
)
5615 flags
= MAP_PRIVATE
;
5617 if (vma
->vm_flags
& VM_DENYWRITE
)
5618 flags
|= MAP_DENYWRITE
;
5619 if (vma
->vm_flags
& VM_MAYEXEC
)
5620 flags
|= MAP_EXECUTABLE
;
5621 if (vma
->vm_flags
& VM_LOCKED
)
5622 flags
|= MAP_LOCKED
;
5623 if (vma
->vm_flags
& VM_HUGETLB
)
5624 flags
|= MAP_HUGETLB
;
5628 if (vma
->vm_ops
&& vma
->vm_ops
->name
) {
5629 name
= (char *) vma
->vm_ops
->name(vma
);
5634 name
= (char *)arch_vma_name(vma
);
5638 if (vma
->vm_start
<= vma
->vm_mm
->start_brk
&&
5639 vma
->vm_end
>= vma
->vm_mm
->brk
) {
5643 if (vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
5644 vma
->vm_end
>= vma
->vm_mm
->start_stack
) {
5654 strlcpy(tmp
, name
, sizeof(tmp
));
5658 * Since our buffer works in 8 byte units we need to align our string
5659 * size to a multiple of 8. However, we must guarantee the tail end is
5660 * zero'd out to avoid leaking random bits to userspace.
5662 size
= strlen(name
)+1;
5663 while (!IS_ALIGNED(size
, sizeof(u64
)))
5664 name
[size
++] = '\0';
5666 mmap_event
->file_name
= name
;
5667 mmap_event
->file_size
= size
;
5668 mmap_event
->maj
= maj
;
5669 mmap_event
->min
= min
;
5670 mmap_event
->ino
= ino
;
5671 mmap_event
->ino_generation
= gen
;
5672 mmap_event
->prot
= prot
;
5673 mmap_event
->flags
= flags
;
5675 if (!(vma
->vm_flags
& VM_EXEC
))
5676 mmap_event
->event_id
.header
.misc
|= PERF_RECORD_MISC_MMAP_DATA
;
5678 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
5680 perf_event_aux(perf_event_mmap_output
,
5687 void perf_event_mmap(struct vm_area_struct
*vma
)
5689 struct perf_mmap_event mmap_event
;
5691 if (!atomic_read(&nr_mmap_events
))
5694 mmap_event
= (struct perf_mmap_event
){
5700 .type
= PERF_RECORD_MMAP
,
5701 .misc
= PERF_RECORD_MISC_USER
,
5706 .start
= vma
->vm_start
,
5707 .len
= vma
->vm_end
- vma
->vm_start
,
5708 .pgoff
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
,
5710 /* .maj (attr_mmap2 only) */
5711 /* .min (attr_mmap2 only) */
5712 /* .ino (attr_mmap2 only) */
5713 /* .ino_generation (attr_mmap2 only) */
5714 /* .prot (attr_mmap2 only) */
5715 /* .flags (attr_mmap2 only) */
5718 perf_event_mmap_event(&mmap_event
);
5722 * IRQ throttle logging
5725 static void perf_log_throttle(struct perf_event
*event
, int enable
)
5727 struct perf_output_handle handle
;
5728 struct perf_sample_data sample
;
5732 struct perf_event_header header
;
5736 } throttle_event
= {
5738 .type
= PERF_RECORD_THROTTLE
,
5740 .size
= sizeof(throttle_event
),
5742 .time
= perf_clock(),
5743 .id
= primary_event_id(event
),
5744 .stream_id
= event
->id
,
5748 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
5750 perf_event_header__init_id(&throttle_event
.header
, &sample
, event
);
5752 ret
= perf_output_begin(&handle
, event
,
5753 throttle_event
.header
.size
);
5757 perf_output_put(&handle
, throttle_event
);
5758 perf_event__output_id_sample(event
, &handle
, &sample
);
5759 perf_output_end(&handle
);
5763 * Generic event overflow handling, sampling.
5766 static int __perf_event_overflow(struct perf_event
*event
,
5767 int throttle
, struct perf_sample_data
*data
,
5768 struct pt_regs
*regs
)
5770 int events
= atomic_read(&event
->event_limit
);
5771 struct hw_perf_event
*hwc
= &event
->hw
;
5776 * Non-sampling counters might still use the PMI to fold short
5777 * hardware counters, ignore those.
5779 if (unlikely(!is_sampling_event(event
)))
5782 seq
= __this_cpu_read(perf_throttled_seq
);
5783 if (seq
!= hwc
->interrupts_seq
) {
5784 hwc
->interrupts_seq
= seq
;
5785 hwc
->interrupts
= 1;
5788 if (unlikely(throttle
5789 && hwc
->interrupts
>= max_samples_per_tick
)) {
5790 __this_cpu_inc(perf_throttled_count
);
5791 hwc
->interrupts
= MAX_INTERRUPTS
;
5792 perf_log_throttle(event
, 0);
5793 tick_nohz_full_kick();
5798 if (event
->attr
.freq
) {
5799 u64 now
= perf_clock();
5800 s64 delta
= now
- hwc
->freq_time_stamp
;
5802 hwc
->freq_time_stamp
= now
;
5804 if (delta
> 0 && delta
< 2*TICK_NSEC
)
5805 perf_adjust_period(event
, delta
, hwc
->last_period
, true);
5809 * XXX event_limit might not quite work as expected on inherited
5813 event
->pending_kill
= POLL_IN
;
5814 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
5816 event
->pending_kill
= POLL_HUP
;
5817 event
->pending_disable
= 1;
5818 irq_work_queue(&event
->pending
);
5821 if (event
->overflow_handler
)
5822 event
->overflow_handler(event
, data
, regs
);
5824 perf_event_output(event
, data
, regs
);
5826 if (event
->fasync
&& event
->pending_kill
) {
5827 event
->pending_wakeup
= 1;
5828 irq_work_queue(&event
->pending
);
5834 int perf_event_overflow(struct perf_event
*event
,
5835 struct perf_sample_data
*data
,
5836 struct pt_regs
*regs
)
5838 return __perf_event_overflow(event
, 1, data
, regs
);
5842 * Generic software event infrastructure
5845 struct swevent_htable
{
5846 struct swevent_hlist
*swevent_hlist
;
5847 struct mutex hlist_mutex
;
5850 /* Recursion avoidance in each contexts */
5851 int recursion
[PERF_NR_CONTEXTS
];
5853 /* Keeps track of cpu being initialized/exited */
5857 static DEFINE_PER_CPU(struct swevent_htable
, swevent_htable
);
5860 * We directly increment event->count and keep a second value in
5861 * event->hw.period_left to count intervals. This period event
5862 * is kept in the range [-sample_period, 0] so that we can use the
5866 u64
perf_swevent_set_period(struct perf_event
*event
)
5868 struct hw_perf_event
*hwc
= &event
->hw
;
5869 u64 period
= hwc
->last_period
;
5873 hwc
->last_period
= hwc
->sample_period
;
5876 old
= val
= local64_read(&hwc
->period_left
);
5880 nr
= div64_u64(period
+ val
, period
);
5881 offset
= nr
* period
;
5883 if (local64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
5889 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
5890 struct perf_sample_data
*data
,
5891 struct pt_regs
*regs
)
5893 struct hw_perf_event
*hwc
= &event
->hw
;
5897 overflow
= perf_swevent_set_period(event
);
5899 if (hwc
->interrupts
== MAX_INTERRUPTS
)
5902 for (; overflow
; overflow
--) {
5903 if (__perf_event_overflow(event
, throttle
,
5906 * We inhibit the overflow from happening when
5907 * hwc->interrupts == MAX_INTERRUPTS.
5915 static void perf_swevent_event(struct perf_event
*event
, u64 nr
,
5916 struct perf_sample_data
*data
,
5917 struct pt_regs
*regs
)
5919 struct hw_perf_event
*hwc
= &event
->hw
;
5921 local64_add(nr
, &event
->count
);
5926 if (!is_sampling_event(event
))
5929 if ((event
->attr
.sample_type
& PERF_SAMPLE_PERIOD
) && !event
->attr
.freq
) {
5931 return perf_swevent_overflow(event
, 1, data
, regs
);
5933 data
->period
= event
->hw
.last_period
;
5935 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
5936 return perf_swevent_overflow(event
, 1, data
, regs
);
5938 if (local64_add_negative(nr
, &hwc
->period_left
))
5941 perf_swevent_overflow(event
, 0, data
, regs
);
5944 static int perf_exclude_event(struct perf_event
*event
,
5945 struct pt_regs
*regs
)
5947 if (event
->hw
.state
& PERF_HES_STOPPED
)
5951 if (event
->attr
.exclude_user
&& user_mode(regs
))
5954 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
5961 static int perf_swevent_match(struct perf_event
*event
,
5962 enum perf_type_id type
,
5964 struct perf_sample_data
*data
,
5965 struct pt_regs
*regs
)
5967 if (event
->attr
.type
!= type
)
5970 if (event
->attr
.config
!= event_id
)
5973 if (perf_exclude_event(event
, regs
))
5979 static inline u64
swevent_hash(u64 type
, u32 event_id
)
5981 u64 val
= event_id
| (type
<< 32);
5983 return hash_64(val
, SWEVENT_HLIST_BITS
);
5986 static inline struct hlist_head
*
5987 __find_swevent_head(struct swevent_hlist
*hlist
, u64 type
, u32 event_id
)
5989 u64 hash
= swevent_hash(type
, event_id
);
5991 return &hlist
->heads
[hash
];
5994 /* For the read side: events when they trigger */
5995 static inline struct hlist_head
*
5996 find_swevent_head_rcu(struct swevent_htable
*swhash
, u64 type
, u32 event_id
)
5998 struct swevent_hlist
*hlist
;
6000 hlist
= rcu_dereference(swhash
->swevent_hlist
);
6004 return __find_swevent_head(hlist
, type
, event_id
);
6007 /* For the event head insertion and removal in the hlist */
6008 static inline struct hlist_head
*
6009 find_swevent_head(struct swevent_htable
*swhash
, struct perf_event
*event
)
6011 struct swevent_hlist
*hlist
;
6012 u32 event_id
= event
->attr
.config
;
6013 u64 type
= event
->attr
.type
;
6016 * Event scheduling is always serialized against hlist allocation
6017 * and release. Which makes the protected version suitable here.
6018 * The context lock guarantees that.
6020 hlist
= rcu_dereference_protected(swhash
->swevent_hlist
,
6021 lockdep_is_held(&event
->ctx
->lock
));
6025 return __find_swevent_head(hlist
, type
, event_id
);
6028 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
6030 struct perf_sample_data
*data
,
6031 struct pt_regs
*regs
)
6033 struct swevent_htable
*swhash
= this_cpu_ptr(&swevent_htable
);
6034 struct perf_event
*event
;
6035 struct hlist_head
*head
;
6038 head
= find_swevent_head_rcu(swhash
, type
, event_id
);
6042 hlist_for_each_entry_rcu(event
, head
, hlist_entry
) {
6043 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
6044 perf_swevent_event(event
, nr
, data
, regs
);
6050 DEFINE_PER_CPU(struct pt_regs
, __perf_regs
[4]);
6052 int perf_swevent_get_recursion_context(void)
6054 struct swevent_htable
*swhash
= this_cpu_ptr(&swevent_htable
);
6056 return get_recursion_context(swhash
->recursion
);
6058 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
6060 inline void perf_swevent_put_recursion_context(int rctx
)
6062 struct swevent_htable
*swhash
= this_cpu_ptr(&swevent_htable
);
6064 put_recursion_context(swhash
->recursion
, rctx
);
6067 void ___perf_sw_event(u32 event_id
, u64 nr
, struct pt_regs
*regs
, u64 addr
)
6069 struct perf_sample_data data
;
6071 if (WARN_ON_ONCE(!regs
))
6074 perf_sample_data_init(&data
, addr
, 0);
6075 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, &data
, regs
);
6078 void __perf_sw_event(u32 event_id
, u64 nr
, struct pt_regs
*regs
, u64 addr
)
6082 preempt_disable_notrace();
6083 rctx
= perf_swevent_get_recursion_context();
6084 if (unlikely(rctx
< 0))
6087 ___perf_sw_event(event_id
, nr
, regs
, addr
);
6089 perf_swevent_put_recursion_context(rctx
);
6091 preempt_enable_notrace();
6094 static void perf_swevent_read(struct perf_event
*event
)
6098 static int perf_swevent_add(struct perf_event
*event
, int flags
)
6100 struct swevent_htable
*swhash
= this_cpu_ptr(&swevent_htable
);
6101 struct hw_perf_event
*hwc
= &event
->hw
;
6102 struct hlist_head
*head
;
6104 if (is_sampling_event(event
)) {
6105 hwc
->last_period
= hwc
->sample_period
;
6106 perf_swevent_set_period(event
);
6109 hwc
->state
= !(flags
& PERF_EF_START
);
6111 head
= find_swevent_head(swhash
, event
);
6114 * We can race with cpu hotplug code. Do not
6115 * WARN if the cpu just got unplugged.
6117 WARN_ON_ONCE(swhash
->online
);
6121 hlist_add_head_rcu(&event
->hlist_entry
, head
);
6122 perf_event_update_userpage(event
);
6127 static void perf_swevent_del(struct perf_event
*event
, int flags
)
6129 hlist_del_rcu(&event
->hlist_entry
);
6132 static void perf_swevent_start(struct perf_event
*event
, int flags
)
6134 event
->hw
.state
= 0;
6137 static void perf_swevent_stop(struct perf_event
*event
, int flags
)
6139 event
->hw
.state
= PERF_HES_STOPPED
;
6142 /* Deref the hlist from the update side */
6143 static inline struct swevent_hlist
*
6144 swevent_hlist_deref(struct swevent_htable
*swhash
)
6146 return rcu_dereference_protected(swhash
->swevent_hlist
,
6147 lockdep_is_held(&swhash
->hlist_mutex
));
6150 static void swevent_hlist_release(struct swevent_htable
*swhash
)
6152 struct swevent_hlist
*hlist
= swevent_hlist_deref(swhash
);
6157 RCU_INIT_POINTER(swhash
->swevent_hlist
, NULL
);
6158 kfree_rcu(hlist
, rcu_head
);
6161 static void swevent_hlist_put_cpu(struct perf_event
*event
, int cpu
)
6163 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
6165 mutex_lock(&swhash
->hlist_mutex
);
6167 if (!--swhash
->hlist_refcount
)
6168 swevent_hlist_release(swhash
);
6170 mutex_unlock(&swhash
->hlist_mutex
);
6173 static void swevent_hlist_put(struct perf_event
*event
)
6177 for_each_possible_cpu(cpu
)
6178 swevent_hlist_put_cpu(event
, cpu
);
6181 static int swevent_hlist_get_cpu(struct perf_event
*event
, int cpu
)
6183 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
6186 mutex_lock(&swhash
->hlist_mutex
);
6188 if (!swevent_hlist_deref(swhash
) && cpu_online(cpu
)) {
6189 struct swevent_hlist
*hlist
;
6191 hlist
= kzalloc(sizeof(*hlist
), GFP_KERNEL
);
6196 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
6198 swhash
->hlist_refcount
++;
6200 mutex_unlock(&swhash
->hlist_mutex
);
6205 static int swevent_hlist_get(struct perf_event
*event
)
6208 int cpu
, failed_cpu
;
6211 for_each_possible_cpu(cpu
) {
6212 err
= swevent_hlist_get_cpu(event
, cpu
);
6222 for_each_possible_cpu(cpu
) {
6223 if (cpu
== failed_cpu
)
6225 swevent_hlist_put_cpu(event
, cpu
);
6232 struct static_key perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
6234 static void sw_perf_event_destroy(struct perf_event
*event
)
6236 u64 event_id
= event
->attr
.config
;
6238 WARN_ON(event
->parent
);
6240 static_key_slow_dec(&perf_swevent_enabled
[event_id
]);
6241 swevent_hlist_put(event
);
6244 static int perf_swevent_init(struct perf_event
*event
)
6246 u64 event_id
= event
->attr
.config
;
6248 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
6252 * no branch sampling for software events
6254 if (has_branch_stack(event
))
6258 case PERF_COUNT_SW_CPU_CLOCK
:
6259 case PERF_COUNT_SW_TASK_CLOCK
:
6266 if (event_id
>= PERF_COUNT_SW_MAX
)
6269 if (!event
->parent
) {
6272 err
= swevent_hlist_get(event
);
6276 static_key_slow_inc(&perf_swevent_enabled
[event_id
]);
6277 event
->destroy
= sw_perf_event_destroy
;
6283 static struct pmu perf_swevent
= {
6284 .task_ctx_nr
= perf_sw_context
,
6286 .event_init
= perf_swevent_init
,
6287 .add
= perf_swevent_add
,
6288 .del
= perf_swevent_del
,
6289 .start
= perf_swevent_start
,
6290 .stop
= perf_swevent_stop
,
6291 .read
= perf_swevent_read
,
6294 #ifdef CONFIG_EVENT_TRACING
6296 static int perf_tp_filter_match(struct perf_event
*event
,
6297 struct perf_sample_data
*data
)
6299 void *record
= data
->raw
->data
;
6301 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
6306 static int perf_tp_event_match(struct perf_event
*event
,
6307 struct perf_sample_data
*data
,
6308 struct pt_regs
*regs
)
6310 if (event
->hw
.state
& PERF_HES_STOPPED
)
6313 * All tracepoints are from kernel-space.
6315 if (event
->attr
.exclude_kernel
)
6318 if (!perf_tp_filter_match(event
, data
))
6324 void perf_tp_event(u64 addr
, u64 count
, void *record
, int entry_size
,
6325 struct pt_regs
*regs
, struct hlist_head
*head
, int rctx
,
6326 struct task_struct
*task
)
6328 struct perf_sample_data data
;
6329 struct perf_event
*event
;
6331 struct perf_raw_record raw
= {
6336 perf_sample_data_init(&data
, addr
, 0);
6339 hlist_for_each_entry_rcu(event
, head
, hlist_entry
) {
6340 if (perf_tp_event_match(event
, &data
, regs
))
6341 perf_swevent_event(event
, count
, &data
, regs
);
6345 * If we got specified a target task, also iterate its context and
6346 * deliver this event there too.
6348 if (task
&& task
!= current
) {
6349 struct perf_event_context
*ctx
;
6350 struct trace_entry
*entry
= record
;
6353 ctx
= rcu_dereference(task
->perf_event_ctxp
[perf_sw_context
]);
6357 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
6358 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
6360 if (event
->attr
.config
!= entry
->type
)
6362 if (perf_tp_event_match(event
, &data
, regs
))
6363 perf_swevent_event(event
, count
, &data
, regs
);
6369 perf_swevent_put_recursion_context(rctx
);
6371 EXPORT_SYMBOL_GPL(perf_tp_event
);
6373 static void tp_perf_event_destroy(struct perf_event
*event
)
6375 perf_trace_destroy(event
);
6378 static int perf_tp_event_init(struct perf_event
*event
)
6382 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
6386 * no branch sampling for tracepoint events
6388 if (has_branch_stack(event
))
6391 err
= perf_trace_init(event
);
6395 event
->destroy
= tp_perf_event_destroy
;
6400 static struct pmu perf_tracepoint
= {
6401 .task_ctx_nr
= perf_sw_context
,
6403 .event_init
= perf_tp_event_init
,
6404 .add
= perf_trace_add
,
6405 .del
= perf_trace_del
,
6406 .start
= perf_swevent_start
,
6407 .stop
= perf_swevent_stop
,
6408 .read
= perf_swevent_read
,
6411 static inline void perf_tp_register(void)
6413 perf_pmu_register(&perf_tracepoint
, "tracepoint", PERF_TYPE_TRACEPOINT
);
6416 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
6421 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
6424 filter_str
= strndup_user(arg
, PAGE_SIZE
);
6425 if (IS_ERR(filter_str
))
6426 return PTR_ERR(filter_str
);
6428 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
, filter_str
);
6434 static void perf_event_free_filter(struct perf_event
*event
)
6436 ftrace_profile_free_filter(event
);
6441 static inline void perf_tp_register(void)
6445 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
6450 static void perf_event_free_filter(struct perf_event
*event
)
6454 #endif /* CONFIG_EVENT_TRACING */
6456 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6457 void perf_bp_event(struct perf_event
*bp
, void *data
)
6459 struct perf_sample_data sample
;
6460 struct pt_regs
*regs
= data
;
6462 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
, 0);
6464 if (!bp
->hw
.state
&& !perf_exclude_event(bp
, regs
))
6465 perf_swevent_event(bp
, 1, &sample
, regs
);
6470 * hrtimer based swevent callback
6473 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
6475 enum hrtimer_restart ret
= HRTIMER_RESTART
;
6476 struct perf_sample_data data
;
6477 struct pt_regs
*regs
;
6478 struct perf_event
*event
;
6481 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
6483 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
6484 return HRTIMER_NORESTART
;
6486 event
->pmu
->read(event
);
6488 perf_sample_data_init(&data
, 0, event
->hw
.last_period
);
6489 regs
= get_irq_regs();
6491 if (regs
&& !perf_exclude_event(event
, regs
)) {
6492 if (!(event
->attr
.exclude_idle
&& is_idle_task(current
)))
6493 if (__perf_event_overflow(event
, 1, &data
, regs
))
6494 ret
= HRTIMER_NORESTART
;
6497 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
6498 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
6503 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
6505 struct hw_perf_event
*hwc
= &event
->hw
;
6508 if (!is_sampling_event(event
))
6511 period
= local64_read(&hwc
->period_left
);
6516 local64_set(&hwc
->period_left
, 0);
6518 period
= max_t(u64
, 10000, hwc
->sample_period
);
6520 __hrtimer_start_range_ns(&hwc
->hrtimer
,
6521 ns_to_ktime(period
), 0,
6522 HRTIMER_MODE_REL_PINNED
, 0);
6525 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
6527 struct hw_perf_event
*hwc
= &event
->hw
;
6529 if (is_sampling_event(event
)) {
6530 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
6531 local64_set(&hwc
->period_left
, ktime_to_ns(remaining
));
6533 hrtimer_cancel(&hwc
->hrtimer
);
6537 static void perf_swevent_init_hrtimer(struct perf_event
*event
)
6539 struct hw_perf_event
*hwc
= &event
->hw
;
6541 if (!is_sampling_event(event
))
6544 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
6545 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
6548 * Since hrtimers have a fixed rate, we can do a static freq->period
6549 * mapping and avoid the whole period adjust feedback stuff.
6551 if (event
->attr
.freq
) {
6552 long freq
= event
->attr
.sample_freq
;
6554 event
->attr
.sample_period
= NSEC_PER_SEC
/ freq
;
6555 hwc
->sample_period
= event
->attr
.sample_period
;
6556 local64_set(&hwc
->period_left
, hwc
->sample_period
);
6557 hwc
->last_period
= hwc
->sample_period
;
6558 event
->attr
.freq
= 0;
6563 * Software event: cpu wall time clock
6566 static void cpu_clock_event_update(struct perf_event
*event
)
6571 now
= local_clock();
6572 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
6573 local64_add(now
- prev
, &event
->count
);
6576 static void cpu_clock_event_start(struct perf_event
*event
, int flags
)
6578 local64_set(&event
->hw
.prev_count
, local_clock());
6579 perf_swevent_start_hrtimer(event
);
6582 static void cpu_clock_event_stop(struct perf_event
*event
, int flags
)
6584 perf_swevent_cancel_hrtimer(event
);
6585 cpu_clock_event_update(event
);
6588 static int cpu_clock_event_add(struct perf_event
*event
, int flags
)
6590 if (flags
& PERF_EF_START
)
6591 cpu_clock_event_start(event
, flags
);
6592 perf_event_update_userpage(event
);
6597 static void cpu_clock_event_del(struct perf_event
*event
, int flags
)
6599 cpu_clock_event_stop(event
, flags
);
6602 static void cpu_clock_event_read(struct perf_event
*event
)
6604 cpu_clock_event_update(event
);
6607 static int cpu_clock_event_init(struct perf_event
*event
)
6609 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
6612 if (event
->attr
.config
!= PERF_COUNT_SW_CPU_CLOCK
)
6616 * no branch sampling for software events
6618 if (has_branch_stack(event
))
6621 perf_swevent_init_hrtimer(event
);
6626 static struct pmu perf_cpu_clock
= {
6627 .task_ctx_nr
= perf_sw_context
,
6629 .event_init
= cpu_clock_event_init
,
6630 .add
= cpu_clock_event_add
,
6631 .del
= cpu_clock_event_del
,
6632 .start
= cpu_clock_event_start
,
6633 .stop
= cpu_clock_event_stop
,
6634 .read
= cpu_clock_event_read
,
6638 * Software event: task time clock
6641 static void task_clock_event_update(struct perf_event
*event
, u64 now
)
6646 prev
= local64_xchg(&event
->hw
.prev_count
, now
);
6648 local64_add(delta
, &event
->count
);
6651 static void task_clock_event_start(struct perf_event
*event
, int flags
)
6653 local64_set(&event
->hw
.prev_count
, event
->ctx
->time
);
6654 perf_swevent_start_hrtimer(event
);
6657 static void task_clock_event_stop(struct perf_event
*event
, int flags
)
6659 perf_swevent_cancel_hrtimer(event
);
6660 task_clock_event_update(event
, event
->ctx
->time
);
6663 static int task_clock_event_add(struct perf_event
*event
, int flags
)
6665 if (flags
& PERF_EF_START
)
6666 task_clock_event_start(event
, flags
);
6667 perf_event_update_userpage(event
);
6672 static void task_clock_event_del(struct perf_event
*event
, int flags
)
6674 task_clock_event_stop(event
, PERF_EF_UPDATE
);
6677 static void task_clock_event_read(struct perf_event
*event
)
6679 u64 now
= perf_clock();
6680 u64 delta
= now
- event
->ctx
->timestamp
;
6681 u64 time
= event
->ctx
->time
+ delta
;
6683 task_clock_event_update(event
, time
);
6686 static int task_clock_event_init(struct perf_event
*event
)
6688 if (event
->attr
.type
!= PERF_TYPE_SOFTWARE
)
6691 if (event
->attr
.config
!= PERF_COUNT_SW_TASK_CLOCK
)
6695 * no branch sampling for software events
6697 if (has_branch_stack(event
))
6700 perf_swevent_init_hrtimer(event
);
6705 static struct pmu perf_task_clock
= {
6706 .task_ctx_nr
= perf_sw_context
,
6708 .event_init
= task_clock_event_init
,
6709 .add
= task_clock_event_add
,
6710 .del
= task_clock_event_del
,
6711 .start
= task_clock_event_start
,
6712 .stop
= task_clock_event_stop
,
6713 .read
= task_clock_event_read
,
6716 static void perf_pmu_nop_void(struct pmu
*pmu
)
6720 static int perf_pmu_nop_int(struct pmu
*pmu
)
6725 static void perf_pmu_start_txn(struct pmu
*pmu
)
6727 perf_pmu_disable(pmu
);
6730 static int perf_pmu_commit_txn(struct pmu
*pmu
)
6732 perf_pmu_enable(pmu
);
6736 static void perf_pmu_cancel_txn(struct pmu
*pmu
)
6738 perf_pmu_enable(pmu
);
6741 static int perf_event_idx_default(struct perf_event
*event
)
6747 * Ensures all contexts with the same task_ctx_nr have the same
6748 * pmu_cpu_context too.
6750 static struct perf_cpu_context __percpu
*find_pmu_context(int ctxn
)
6757 list_for_each_entry(pmu
, &pmus
, entry
) {
6758 if (pmu
->task_ctx_nr
== ctxn
)
6759 return pmu
->pmu_cpu_context
;
6765 static void update_pmu_context(struct pmu
*pmu
, struct pmu
*old_pmu
)
6769 for_each_possible_cpu(cpu
) {
6770 struct perf_cpu_context
*cpuctx
;
6772 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
6774 if (cpuctx
->unique_pmu
== old_pmu
)
6775 cpuctx
->unique_pmu
= pmu
;
6779 static void free_pmu_context(struct pmu
*pmu
)
6783 mutex_lock(&pmus_lock
);
6785 * Like a real lame refcount.
6787 list_for_each_entry(i
, &pmus
, entry
) {
6788 if (i
->pmu_cpu_context
== pmu
->pmu_cpu_context
) {
6789 update_pmu_context(i
, pmu
);
6794 free_percpu(pmu
->pmu_cpu_context
);
6796 mutex_unlock(&pmus_lock
);
6798 static struct idr pmu_idr
;
6801 type_show(struct device
*dev
, struct device_attribute
*attr
, char *page
)
6803 struct pmu
*pmu
= dev_get_drvdata(dev
);
6805 return snprintf(page
, PAGE_SIZE
-1, "%d\n", pmu
->type
);
6807 static DEVICE_ATTR_RO(type
);
6810 perf_event_mux_interval_ms_show(struct device
*dev
,
6811 struct device_attribute
*attr
,
6814 struct pmu
*pmu
= dev_get_drvdata(dev
);
6816 return snprintf(page
, PAGE_SIZE
-1, "%d\n", pmu
->hrtimer_interval_ms
);
6820 perf_event_mux_interval_ms_store(struct device
*dev
,
6821 struct device_attribute
*attr
,
6822 const char *buf
, size_t count
)
6824 struct pmu
*pmu
= dev_get_drvdata(dev
);
6825 int timer
, cpu
, ret
;
6827 ret
= kstrtoint(buf
, 0, &timer
);
6834 /* same value, noting to do */
6835 if (timer
== pmu
->hrtimer_interval_ms
)
6838 pmu
->hrtimer_interval_ms
= timer
;
6840 /* update all cpuctx for this PMU */
6841 for_each_possible_cpu(cpu
) {
6842 struct perf_cpu_context
*cpuctx
;
6843 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
6844 cpuctx
->hrtimer_interval
= ns_to_ktime(NSEC_PER_MSEC
* timer
);
6846 if (hrtimer_active(&cpuctx
->hrtimer
))
6847 hrtimer_forward_now(&cpuctx
->hrtimer
, cpuctx
->hrtimer_interval
);
6852 static DEVICE_ATTR_RW(perf_event_mux_interval_ms
);
6854 static struct attribute
*pmu_dev_attrs
[] = {
6855 &dev_attr_type
.attr
,
6856 &dev_attr_perf_event_mux_interval_ms
.attr
,
6859 ATTRIBUTE_GROUPS(pmu_dev
);
6861 static int pmu_bus_running
;
6862 static struct bus_type pmu_bus
= {
6863 .name
= "event_source",
6864 .dev_groups
= pmu_dev_groups
,
6867 static void pmu_dev_release(struct device
*dev
)
6872 static int pmu_dev_alloc(struct pmu
*pmu
)
6876 pmu
->dev
= kzalloc(sizeof(struct device
), GFP_KERNEL
);
6880 pmu
->dev
->groups
= pmu
->attr_groups
;
6881 device_initialize(pmu
->dev
);
6882 ret
= dev_set_name(pmu
->dev
, "%s", pmu
->name
);
6886 dev_set_drvdata(pmu
->dev
, pmu
);
6887 pmu
->dev
->bus
= &pmu_bus
;
6888 pmu
->dev
->release
= pmu_dev_release
;
6889 ret
= device_add(pmu
->dev
);
6897 put_device(pmu
->dev
);
6901 static struct lock_class_key cpuctx_mutex
;
6902 static struct lock_class_key cpuctx_lock
;
6904 int perf_pmu_register(struct pmu
*pmu
, const char *name
, int type
)
6908 mutex_lock(&pmus_lock
);
6910 pmu
->pmu_disable_count
= alloc_percpu(int);
6911 if (!pmu
->pmu_disable_count
)
6920 type
= idr_alloc(&pmu_idr
, pmu
, PERF_TYPE_MAX
, 0, GFP_KERNEL
);
6928 if (pmu_bus_running
) {
6929 ret
= pmu_dev_alloc(pmu
);
6935 pmu
->pmu_cpu_context
= find_pmu_context(pmu
->task_ctx_nr
);
6936 if (pmu
->pmu_cpu_context
)
6937 goto got_cpu_context
;
6940 pmu
->pmu_cpu_context
= alloc_percpu(struct perf_cpu_context
);
6941 if (!pmu
->pmu_cpu_context
)
6944 for_each_possible_cpu(cpu
) {
6945 struct perf_cpu_context
*cpuctx
;
6947 cpuctx
= per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
);
6948 __perf_event_init_context(&cpuctx
->ctx
);
6949 lockdep_set_class(&cpuctx
->ctx
.mutex
, &cpuctx_mutex
);
6950 lockdep_set_class(&cpuctx
->ctx
.lock
, &cpuctx_lock
);
6951 cpuctx
->ctx
.pmu
= pmu
;
6953 __perf_cpu_hrtimer_init(cpuctx
, cpu
);
6955 cpuctx
->unique_pmu
= pmu
;
6959 if (!pmu
->start_txn
) {
6960 if (pmu
->pmu_enable
) {
6962 * If we have pmu_enable/pmu_disable calls, install
6963 * transaction stubs that use that to try and batch
6964 * hardware accesses.
6966 pmu
->start_txn
= perf_pmu_start_txn
;
6967 pmu
->commit_txn
= perf_pmu_commit_txn
;
6968 pmu
->cancel_txn
= perf_pmu_cancel_txn
;
6970 pmu
->start_txn
= perf_pmu_nop_void
;
6971 pmu
->commit_txn
= perf_pmu_nop_int
;
6972 pmu
->cancel_txn
= perf_pmu_nop_void
;
6976 if (!pmu
->pmu_enable
) {
6977 pmu
->pmu_enable
= perf_pmu_nop_void
;
6978 pmu
->pmu_disable
= perf_pmu_nop_void
;
6981 if (!pmu
->event_idx
)
6982 pmu
->event_idx
= perf_event_idx_default
;
6984 list_add_rcu(&pmu
->entry
, &pmus
);
6987 mutex_unlock(&pmus_lock
);
6992 device_del(pmu
->dev
);
6993 put_device(pmu
->dev
);
6996 if (pmu
->type
>= PERF_TYPE_MAX
)
6997 idr_remove(&pmu_idr
, pmu
->type
);
7000 free_percpu(pmu
->pmu_disable_count
);
7003 EXPORT_SYMBOL_GPL(perf_pmu_register
);
7005 void perf_pmu_unregister(struct pmu
*pmu
)
7007 mutex_lock(&pmus_lock
);
7008 list_del_rcu(&pmu
->entry
);
7009 mutex_unlock(&pmus_lock
);
7012 * We dereference the pmu list under both SRCU and regular RCU, so
7013 * synchronize against both of those.
7015 synchronize_srcu(&pmus_srcu
);
7018 free_percpu(pmu
->pmu_disable_count
);
7019 if (pmu
->type
>= PERF_TYPE_MAX
)
7020 idr_remove(&pmu_idr
, pmu
->type
);
7021 device_del(pmu
->dev
);
7022 put_device(pmu
->dev
);
7023 free_pmu_context(pmu
);
7025 EXPORT_SYMBOL_GPL(perf_pmu_unregister
);
7027 static int perf_try_init_event(struct pmu
*pmu
, struct perf_event
*event
)
7031 if (!try_module_get(pmu
->module
))
7034 ret
= pmu
->event_init(event
);
7036 module_put(pmu
->module
);
7041 struct pmu
*perf_init_event(struct perf_event
*event
)
7043 struct pmu
*pmu
= NULL
;
7047 idx
= srcu_read_lock(&pmus_srcu
);
7050 pmu
= idr_find(&pmu_idr
, event
->attr
.type
);
7053 ret
= perf_try_init_event(pmu
, event
);
7059 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
7060 ret
= perf_try_init_event(pmu
, event
);
7064 if (ret
!= -ENOENT
) {
7069 pmu
= ERR_PTR(-ENOENT
);
7071 srcu_read_unlock(&pmus_srcu
, idx
);
7076 static void account_event_cpu(struct perf_event
*event
, int cpu
)
7081 if (is_cgroup_event(event
))
7082 atomic_inc(&per_cpu(perf_cgroup_events
, cpu
));
7085 static void account_event(struct perf_event
*event
)
7090 if (event
->attach_state
& PERF_ATTACH_TASK
)
7091 static_key_slow_inc(&perf_sched_events
.key
);
7092 if (event
->attr
.mmap
|| event
->attr
.mmap_data
)
7093 atomic_inc(&nr_mmap_events
);
7094 if (event
->attr
.comm
)
7095 atomic_inc(&nr_comm_events
);
7096 if (event
->attr
.task
)
7097 atomic_inc(&nr_task_events
);
7098 if (event
->attr
.freq
) {
7099 if (atomic_inc_return(&nr_freq_events
) == 1)
7100 tick_nohz_full_kick_all();
7102 if (has_branch_stack(event
))
7103 static_key_slow_inc(&perf_sched_events
.key
);
7104 if (is_cgroup_event(event
))
7105 static_key_slow_inc(&perf_sched_events
.key
);
7107 account_event_cpu(event
, event
->cpu
);
7111 * Allocate and initialize a event structure
7113 static struct perf_event
*
7114 perf_event_alloc(struct perf_event_attr
*attr
, int cpu
,
7115 struct task_struct
*task
,
7116 struct perf_event
*group_leader
,
7117 struct perf_event
*parent_event
,
7118 perf_overflow_handler_t overflow_handler
,
7122 struct perf_event
*event
;
7123 struct hw_perf_event
*hwc
;
7126 if ((unsigned)cpu
>= nr_cpu_ids
) {
7127 if (!task
|| cpu
!= -1)
7128 return ERR_PTR(-EINVAL
);
7131 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
7133 return ERR_PTR(-ENOMEM
);
7136 * Single events are their own group leaders, with an
7137 * empty sibling list:
7140 group_leader
= event
;
7142 mutex_init(&event
->child_mutex
);
7143 INIT_LIST_HEAD(&event
->child_list
);
7145 INIT_LIST_HEAD(&event
->group_entry
);
7146 INIT_LIST_HEAD(&event
->event_entry
);
7147 INIT_LIST_HEAD(&event
->sibling_list
);
7148 INIT_LIST_HEAD(&event
->rb_entry
);
7149 INIT_LIST_HEAD(&event
->active_entry
);
7150 INIT_HLIST_NODE(&event
->hlist_entry
);
7153 init_waitqueue_head(&event
->waitq
);
7154 init_irq_work(&event
->pending
, perf_pending_event
);
7156 mutex_init(&event
->mmap_mutex
);
7158 atomic_long_set(&event
->refcount
, 1);
7160 event
->attr
= *attr
;
7161 event
->group_leader
= group_leader
;
7165 event
->parent
= parent_event
;
7167 event
->ns
= get_pid_ns(task_active_pid_ns(current
));
7168 event
->id
= atomic64_inc_return(&perf_event_id
);
7170 event
->state
= PERF_EVENT_STATE_INACTIVE
;
7173 event
->attach_state
= PERF_ATTACH_TASK
;
7175 if (attr
->type
== PERF_TYPE_TRACEPOINT
)
7176 event
->hw
.tp_target
= task
;
7177 #ifdef CONFIG_HAVE_HW_BREAKPOINT
7179 * hw_breakpoint is a bit difficult here..
7181 else if (attr
->type
== PERF_TYPE_BREAKPOINT
)
7182 event
->hw
.bp_target
= task
;
7186 if (!overflow_handler
&& parent_event
) {
7187 overflow_handler
= parent_event
->overflow_handler
;
7188 context
= parent_event
->overflow_handler_context
;
7191 event
->overflow_handler
= overflow_handler
;
7192 event
->overflow_handler_context
= context
;
7194 perf_event__state_init(event
);
7199 hwc
->sample_period
= attr
->sample_period
;
7200 if (attr
->freq
&& attr
->sample_freq
)
7201 hwc
->sample_period
= 1;
7202 hwc
->last_period
= hwc
->sample_period
;
7204 local64_set(&hwc
->period_left
, hwc
->sample_period
);
7207 * we currently do not support PERF_FORMAT_GROUP on inherited events
7209 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
7212 if (!has_branch_stack(event
))
7213 event
->attr
.branch_sample_type
= 0;
7215 pmu
= perf_init_event(event
);
7218 else if (IS_ERR(pmu
)) {
7223 if (!event
->parent
) {
7224 if (event
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) {
7225 err
= get_callchain_buffers();
7235 event
->destroy(event
);
7236 module_put(pmu
->module
);
7239 put_pid_ns(event
->ns
);
7242 return ERR_PTR(err
);
7245 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
7246 struct perf_event_attr
*attr
)
7251 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
7255 * zero the full structure, so that a short copy will be nice.
7257 memset(attr
, 0, sizeof(*attr
));
7259 ret
= get_user(size
, &uattr
->size
);
7263 if (size
> PAGE_SIZE
) /* silly large */
7266 if (!size
) /* abi compat */
7267 size
= PERF_ATTR_SIZE_VER0
;
7269 if (size
< PERF_ATTR_SIZE_VER0
)
7273 * If we're handed a bigger struct than we know of,
7274 * ensure all the unknown bits are 0 - i.e. new
7275 * user-space does not rely on any kernel feature
7276 * extensions we dont know about yet.
7278 if (size
> sizeof(*attr
)) {
7279 unsigned char __user
*addr
;
7280 unsigned char __user
*end
;
7283 addr
= (void __user
*)uattr
+ sizeof(*attr
);
7284 end
= (void __user
*)uattr
+ size
;
7286 for (; addr
< end
; addr
++) {
7287 ret
= get_user(val
, addr
);
7293 size
= sizeof(*attr
);
7296 ret
= copy_from_user(attr
, uattr
, size
);
7300 if (attr
->__reserved_1
)
7303 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
7306 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
7309 if (attr
->sample_type
& PERF_SAMPLE_BRANCH_STACK
) {
7310 u64 mask
= attr
->branch_sample_type
;
7312 /* only using defined bits */
7313 if (mask
& ~(PERF_SAMPLE_BRANCH_MAX
-1))
7316 /* at least one branch bit must be set */
7317 if (!(mask
& ~PERF_SAMPLE_BRANCH_PLM_ALL
))
7320 /* propagate priv level, when not set for branch */
7321 if (!(mask
& PERF_SAMPLE_BRANCH_PLM_ALL
)) {
7323 /* exclude_kernel checked on syscall entry */
7324 if (!attr
->exclude_kernel
)
7325 mask
|= PERF_SAMPLE_BRANCH_KERNEL
;
7327 if (!attr
->exclude_user
)
7328 mask
|= PERF_SAMPLE_BRANCH_USER
;
7330 if (!attr
->exclude_hv
)
7331 mask
|= PERF_SAMPLE_BRANCH_HV
;
7333 * adjust user setting (for HW filter setup)
7335 attr
->branch_sample_type
= mask
;
7337 /* privileged levels capture (kernel, hv): check permissions */
7338 if ((mask
& PERF_SAMPLE_BRANCH_PERM_PLM
)
7339 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
7343 if (attr
->sample_type
& PERF_SAMPLE_REGS_USER
) {
7344 ret
= perf_reg_validate(attr
->sample_regs_user
);
7349 if (attr
->sample_type
& PERF_SAMPLE_STACK_USER
) {
7350 if (!arch_perf_have_user_stack_dump())
7354 * We have __u32 type for the size, but so far
7355 * we can only use __u16 as maximum due to the
7356 * __u16 sample size limit.
7358 if (attr
->sample_stack_user
>= USHRT_MAX
)
7360 else if (!IS_ALIGNED(attr
->sample_stack_user
, sizeof(u64
)))
7364 if (attr
->sample_type
& PERF_SAMPLE_REGS_INTR
)
7365 ret
= perf_reg_validate(attr
->sample_regs_intr
);
7370 put_user(sizeof(*attr
), &uattr
->size
);
7376 perf_event_set_output(struct perf_event
*event
, struct perf_event
*output_event
)
7378 struct ring_buffer
*rb
= NULL
;
7384 /* don't allow circular references */
7385 if (event
== output_event
)
7389 * Don't allow cross-cpu buffers
7391 if (output_event
->cpu
!= event
->cpu
)
7395 * If its not a per-cpu rb, it must be the same task.
7397 if (output_event
->cpu
== -1 && output_event
->ctx
!= event
->ctx
)
7401 mutex_lock(&event
->mmap_mutex
);
7402 /* Can't redirect output if we've got an active mmap() */
7403 if (atomic_read(&event
->mmap_count
))
7407 /* get the rb we want to redirect to */
7408 rb
= ring_buffer_get(output_event
);
7413 ring_buffer_attach(event
, rb
);
7417 mutex_unlock(&event
->mmap_mutex
);
7423 static void mutex_lock_double(struct mutex
*a
, struct mutex
*b
)
7429 mutex_lock_nested(b
, SINGLE_DEPTH_NESTING
);
7433 * sys_perf_event_open - open a performance event, associate it to a task/cpu
7435 * @attr_uptr: event_id type attributes for monitoring/sampling
7438 * @group_fd: group leader event fd
7440 SYSCALL_DEFINE5(perf_event_open
,
7441 struct perf_event_attr __user
*, attr_uptr
,
7442 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
7444 struct perf_event
*group_leader
= NULL
, *output_event
= NULL
;
7445 struct perf_event
*event
, *sibling
;
7446 struct perf_event_attr attr
;
7447 struct perf_event_context
*ctx
, *uninitialized_var(gctx
);
7448 struct file
*event_file
= NULL
;
7449 struct fd group
= {NULL
, 0};
7450 struct task_struct
*task
= NULL
;
7455 int f_flags
= O_RDWR
;
7457 /* for future expandability... */
7458 if (flags
& ~PERF_FLAG_ALL
)
7461 err
= perf_copy_attr(attr_uptr
, &attr
);
7465 if (!attr
.exclude_kernel
) {
7466 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
7471 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
7474 if (attr
.sample_period
& (1ULL << 63))
7479 * In cgroup mode, the pid argument is used to pass the fd
7480 * opened to the cgroup directory in cgroupfs. The cpu argument
7481 * designates the cpu on which to monitor threads from that
7484 if ((flags
& PERF_FLAG_PID_CGROUP
) && (pid
== -1 || cpu
== -1))
7487 if (flags
& PERF_FLAG_FD_CLOEXEC
)
7488 f_flags
|= O_CLOEXEC
;
7490 event_fd
= get_unused_fd_flags(f_flags
);
7494 if (group_fd
!= -1) {
7495 err
= perf_fget_light(group_fd
, &group
);
7498 group_leader
= group
.file
->private_data
;
7499 if (flags
& PERF_FLAG_FD_OUTPUT
)
7500 output_event
= group_leader
;
7501 if (flags
& PERF_FLAG_FD_NO_GROUP
)
7502 group_leader
= NULL
;
7505 if (pid
!= -1 && !(flags
& PERF_FLAG_PID_CGROUP
)) {
7506 task
= find_lively_task_by_vpid(pid
);
7508 err
= PTR_ERR(task
);
7513 if (task
&& group_leader
&&
7514 group_leader
->attr
.inherit
!= attr
.inherit
) {
7521 event
= perf_event_alloc(&attr
, cpu
, task
, group_leader
, NULL
,
7523 if (IS_ERR(event
)) {
7524 err
= PTR_ERR(event
);
7528 if (flags
& PERF_FLAG_PID_CGROUP
) {
7529 err
= perf_cgroup_connect(pid
, event
, &attr
, group_leader
);
7531 __free_event(event
);
7536 if (is_sampling_event(event
)) {
7537 if (event
->pmu
->capabilities
& PERF_PMU_CAP_NO_INTERRUPT
) {
7543 account_event(event
);
7546 * Special case software events and allow them to be part of
7547 * any hardware group.
7552 (is_software_event(event
) != is_software_event(group_leader
))) {
7553 if (is_software_event(event
)) {
7555 * If event and group_leader are not both a software
7556 * event, and event is, then group leader is not.
7558 * Allow the addition of software events to !software
7559 * groups, this is safe because software events never
7562 pmu
= group_leader
->pmu
;
7563 } else if (is_software_event(group_leader
) &&
7564 (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
)) {
7566 * In case the group is a pure software group, and we
7567 * try to add a hardware event, move the whole group to
7568 * the hardware context.
7575 * Get the target context (task or percpu):
7577 ctx
= find_get_context(pmu
, task
, event
);
7584 put_task_struct(task
);
7589 * Look up the group leader (we will attach this event to it):
7595 * Do not allow a recursive hierarchy (this new sibling
7596 * becoming part of another group-sibling):
7598 if (group_leader
->group_leader
!= group_leader
)
7601 * Do not allow to attach to a group in a different
7602 * task or CPU context:
7606 * Make sure we're both on the same task, or both
7609 if (group_leader
->ctx
->task
!= ctx
->task
)
7613 * Make sure we're both events for the same CPU;
7614 * grouping events for different CPUs is broken; since
7615 * you can never concurrently schedule them anyhow.
7617 if (group_leader
->cpu
!= event
->cpu
)
7620 if (group_leader
->ctx
!= ctx
)
7625 * Only a group leader can be exclusive or pinned
7627 if (attr
.exclusive
|| attr
.pinned
)
7632 err
= perf_event_set_output(event
, output_event
);
7637 event_file
= anon_inode_getfile("[perf_event]", &perf_fops
, event
,
7639 if (IS_ERR(event_file
)) {
7640 err
= PTR_ERR(event_file
);
7645 gctx
= group_leader
->ctx
;
7648 * See perf_event_ctx_lock() for comments on the details
7649 * of swizzling perf_event::ctx.
7651 mutex_lock_double(&gctx
->mutex
, &ctx
->mutex
);
7653 perf_remove_from_context(group_leader
, false);
7655 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
7657 perf_remove_from_context(sibling
, false);
7661 mutex_lock(&ctx
->mutex
);
7664 WARN_ON_ONCE(ctx
->parent_ctx
);
7668 * Wait for everybody to stop referencing the events through
7669 * the old lists, before installing it on new lists.
7674 * Install the group siblings before the group leader.
7676 * Because a group leader will try and install the entire group
7677 * (through the sibling list, which is still in-tact), we can
7678 * end up with siblings installed in the wrong context.
7680 * By installing siblings first we NO-OP because they're not
7681 * reachable through the group lists.
7683 list_for_each_entry(sibling
, &group_leader
->sibling_list
,
7685 perf_event__state_init(sibling
);
7686 perf_install_in_context(ctx
, sibling
, sibling
->cpu
);
7691 * Removing from the context ends up with disabled
7692 * event. What we want here is event in the initial
7693 * startup state, ready to be add into new context.
7695 perf_event__state_init(group_leader
);
7696 perf_install_in_context(ctx
, group_leader
, group_leader
->cpu
);
7700 perf_install_in_context(ctx
, event
, event
->cpu
);
7701 perf_unpin_context(ctx
);
7704 mutex_unlock(&gctx
->mutex
);
7707 mutex_unlock(&ctx
->mutex
);
7711 event
->owner
= current
;
7713 mutex_lock(¤t
->perf_event_mutex
);
7714 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
7715 mutex_unlock(¤t
->perf_event_mutex
);
7718 * Precalculate sample_data sizes
7720 perf_event__header_size(event
);
7721 perf_event__id_header_size(event
);
7724 * Drop the reference on the group_event after placing the
7725 * new event on the sibling_list. This ensures destruction
7726 * of the group leader will find the pointer to itself in
7727 * perf_group_detach().
7730 fd_install(event_fd
, event_file
);
7734 perf_unpin_context(ctx
);
7742 put_task_struct(task
);
7746 put_unused_fd(event_fd
);
7751 * perf_event_create_kernel_counter
7753 * @attr: attributes of the counter to create
7754 * @cpu: cpu in which the counter is bound
7755 * @task: task to profile (NULL for percpu)
7758 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
7759 struct task_struct
*task
,
7760 perf_overflow_handler_t overflow_handler
,
7763 struct perf_event_context
*ctx
;
7764 struct perf_event
*event
;
7768 * Get the target context (task or percpu):
7771 event
= perf_event_alloc(attr
, cpu
, task
, NULL
, NULL
,
7772 overflow_handler
, context
);
7773 if (IS_ERR(event
)) {
7774 err
= PTR_ERR(event
);
7778 /* Mark owner so we could distinguish it from user events. */
7779 event
->owner
= EVENT_OWNER_KERNEL
;
7781 account_event(event
);
7783 ctx
= find_get_context(event
->pmu
, task
, event
);
7789 WARN_ON_ONCE(ctx
->parent_ctx
);
7790 mutex_lock(&ctx
->mutex
);
7791 perf_install_in_context(ctx
, event
, cpu
);
7792 perf_unpin_context(ctx
);
7793 mutex_unlock(&ctx
->mutex
);
7800 return ERR_PTR(err
);
7802 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
7804 void perf_pmu_migrate_context(struct pmu
*pmu
, int src_cpu
, int dst_cpu
)
7806 struct perf_event_context
*src_ctx
;
7807 struct perf_event_context
*dst_ctx
;
7808 struct perf_event
*event
, *tmp
;
7811 src_ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, src_cpu
)->ctx
;
7812 dst_ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, dst_cpu
)->ctx
;
7815 * See perf_event_ctx_lock() for comments on the details
7816 * of swizzling perf_event::ctx.
7818 mutex_lock_double(&src_ctx
->mutex
, &dst_ctx
->mutex
);
7819 list_for_each_entry_safe(event
, tmp
, &src_ctx
->event_list
,
7821 perf_remove_from_context(event
, false);
7822 unaccount_event_cpu(event
, src_cpu
);
7824 list_add(&event
->migrate_entry
, &events
);
7828 * Wait for the events to quiesce before re-instating them.
7833 * Re-instate events in 2 passes.
7835 * Skip over group leaders and only install siblings on this first
7836 * pass, siblings will not get enabled without a leader, however a
7837 * leader will enable its siblings, even if those are still on the old
7840 list_for_each_entry_safe(event
, tmp
, &events
, migrate_entry
) {
7841 if (event
->group_leader
== event
)
7844 list_del(&event
->migrate_entry
);
7845 if (event
->state
>= PERF_EVENT_STATE_OFF
)
7846 event
->state
= PERF_EVENT_STATE_INACTIVE
;
7847 account_event_cpu(event
, dst_cpu
);
7848 perf_install_in_context(dst_ctx
, event
, dst_cpu
);
7853 * Once all the siblings are setup properly, install the group leaders
7856 list_for_each_entry_safe(event
, tmp
, &events
, migrate_entry
) {
7857 list_del(&event
->migrate_entry
);
7858 if (event
->state
>= PERF_EVENT_STATE_OFF
)
7859 event
->state
= PERF_EVENT_STATE_INACTIVE
;
7860 account_event_cpu(event
, dst_cpu
);
7861 perf_install_in_context(dst_ctx
, event
, dst_cpu
);
7864 mutex_unlock(&dst_ctx
->mutex
);
7865 mutex_unlock(&src_ctx
->mutex
);
7867 EXPORT_SYMBOL_GPL(perf_pmu_migrate_context
);
7869 static void sync_child_event(struct perf_event
*child_event
,
7870 struct task_struct
*child
)
7872 struct perf_event
*parent_event
= child_event
->parent
;
7875 if (child_event
->attr
.inherit_stat
)
7876 perf_event_read_event(child_event
, child
);
7878 child_val
= perf_event_count(child_event
);
7881 * Add back the child's count to the parent's count:
7883 atomic64_add(child_val
, &parent_event
->child_count
);
7884 atomic64_add(child_event
->total_time_enabled
,
7885 &parent_event
->child_total_time_enabled
);
7886 atomic64_add(child_event
->total_time_running
,
7887 &parent_event
->child_total_time_running
);
7890 * Remove this event from the parent's list
7892 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
7893 mutex_lock(&parent_event
->child_mutex
);
7894 list_del_init(&child_event
->child_list
);
7895 mutex_unlock(&parent_event
->child_mutex
);
7898 * Make sure user/parent get notified, that we just
7901 perf_event_wakeup(parent_event
);
7904 * Release the parent event, if this was the last
7907 put_event(parent_event
);
7911 __perf_event_exit_task(struct perf_event
*child_event
,
7912 struct perf_event_context
*child_ctx
,
7913 struct task_struct
*child
)
7916 * Do not destroy the 'original' grouping; because of the context
7917 * switch optimization the original events could've ended up in a
7918 * random child task.
7920 * If we were to destroy the original group, all group related
7921 * operations would cease to function properly after this random
7924 * Do destroy all inherited groups, we don't care about those
7925 * and being thorough is better.
7927 perf_remove_from_context(child_event
, !!child_event
->parent
);
7930 * It can happen that the parent exits first, and has events
7931 * that are still around due to the child reference. These
7932 * events need to be zapped.
7934 if (child_event
->parent
) {
7935 sync_child_event(child_event
, child
);
7936 free_event(child_event
);
7938 child_event
->state
= PERF_EVENT_STATE_EXIT
;
7939 perf_event_wakeup(child_event
);
7943 static void perf_event_exit_task_context(struct task_struct
*child
, int ctxn
)
7945 struct perf_event
*child_event
, *next
;
7946 struct perf_event_context
*child_ctx
, *clone_ctx
= NULL
;
7947 unsigned long flags
;
7949 if (likely(!child
->perf_event_ctxp
[ctxn
])) {
7950 perf_event_task(child
, NULL
, 0);
7954 local_irq_save(flags
);
7956 * We can't reschedule here because interrupts are disabled,
7957 * and either child is current or it is a task that can't be
7958 * scheduled, so we are now safe from rescheduling changing
7961 child_ctx
= rcu_dereference_raw(child
->perf_event_ctxp
[ctxn
]);
7964 * Take the context lock here so that if find_get_context is
7965 * reading child->perf_event_ctxp, we wait until it has
7966 * incremented the context's refcount before we do put_ctx below.
7968 raw_spin_lock(&child_ctx
->lock
);
7969 task_ctx_sched_out(child_ctx
);
7970 child
->perf_event_ctxp
[ctxn
] = NULL
;
7973 * If this context is a clone; unclone it so it can't get
7974 * swapped to another process while we're removing all
7975 * the events from it.
7977 clone_ctx
= unclone_ctx(child_ctx
);
7978 update_context_time(child_ctx
);
7979 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
7985 * Report the task dead after unscheduling the events so that we
7986 * won't get any samples after PERF_RECORD_EXIT. We can however still
7987 * get a few PERF_RECORD_READ events.
7989 perf_event_task(child
, child_ctx
, 0);
7992 * We can recurse on the same lock type through:
7994 * __perf_event_exit_task()
7995 * sync_child_event()
7997 * mutex_lock(&ctx->mutex)
7999 * But since its the parent context it won't be the same instance.
8001 mutex_lock(&child_ctx
->mutex
);
8003 list_for_each_entry_safe(child_event
, next
, &child_ctx
->event_list
, event_entry
)
8004 __perf_event_exit_task(child_event
, child_ctx
, child
);
8006 mutex_unlock(&child_ctx
->mutex
);
8012 * When a child task exits, feed back event values to parent events.
8014 void perf_event_exit_task(struct task_struct
*child
)
8016 struct perf_event
*event
, *tmp
;
8019 mutex_lock(&child
->perf_event_mutex
);
8020 list_for_each_entry_safe(event
, tmp
, &child
->perf_event_list
,
8022 list_del_init(&event
->owner_entry
);
8025 * Ensure the list deletion is visible before we clear
8026 * the owner, closes a race against perf_release() where
8027 * we need to serialize on the owner->perf_event_mutex.
8030 event
->owner
= NULL
;
8032 mutex_unlock(&child
->perf_event_mutex
);
8034 for_each_task_context_nr(ctxn
)
8035 perf_event_exit_task_context(child
, ctxn
);
8038 static void perf_free_event(struct perf_event
*event
,
8039 struct perf_event_context
*ctx
)
8041 struct perf_event
*parent
= event
->parent
;
8043 if (WARN_ON_ONCE(!parent
))
8046 mutex_lock(&parent
->child_mutex
);
8047 list_del_init(&event
->child_list
);
8048 mutex_unlock(&parent
->child_mutex
);
8052 raw_spin_lock_irq(&ctx
->lock
);
8053 perf_group_detach(event
);
8054 list_del_event(event
, ctx
);
8055 raw_spin_unlock_irq(&ctx
->lock
);
8060 * Free an unexposed, unused context as created by inheritance by
8061 * perf_event_init_task below, used by fork() in case of fail.
8063 * Not all locks are strictly required, but take them anyway to be nice and
8064 * help out with the lockdep assertions.
8066 void perf_event_free_task(struct task_struct
*task
)
8068 struct perf_event_context
*ctx
;
8069 struct perf_event
*event
, *tmp
;
8072 for_each_task_context_nr(ctxn
) {
8073 ctx
= task
->perf_event_ctxp
[ctxn
];
8077 mutex_lock(&ctx
->mutex
);
8079 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
,
8081 perf_free_event(event
, ctx
);
8083 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
,
8085 perf_free_event(event
, ctx
);
8087 if (!list_empty(&ctx
->pinned_groups
) ||
8088 !list_empty(&ctx
->flexible_groups
))
8091 mutex_unlock(&ctx
->mutex
);
8097 void perf_event_delayed_put(struct task_struct
*task
)
8101 for_each_task_context_nr(ctxn
)
8102 WARN_ON_ONCE(task
->perf_event_ctxp
[ctxn
]);
8106 * inherit a event from parent task to child task:
8108 static struct perf_event
*
8109 inherit_event(struct perf_event
*parent_event
,
8110 struct task_struct
*parent
,
8111 struct perf_event_context
*parent_ctx
,
8112 struct task_struct
*child
,
8113 struct perf_event
*group_leader
,
8114 struct perf_event_context
*child_ctx
)
8116 enum perf_event_active_state parent_state
= parent_event
->state
;
8117 struct perf_event
*child_event
;
8118 unsigned long flags
;
8121 * Instead of creating recursive hierarchies of events,
8122 * we link inherited events back to the original parent,
8123 * which has a filp for sure, which we use as the reference
8126 if (parent_event
->parent
)
8127 parent_event
= parent_event
->parent
;
8129 child_event
= perf_event_alloc(&parent_event
->attr
,
8132 group_leader
, parent_event
,
8134 if (IS_ERR(child_event
))
8137 if (is_orphaned_event(parent_event
) ||
8138 !atomic_long_inc_not_zero(&parent_event
->refcount
)) {
8139 free_event(child_event
);
8146 * Make the child state follow the state of the parent event,
8147 * not its attr.disabled bit. We hold the parent's mutex,
8148 * so we won't race with perf_event_{en, dis}able_family.
8150 if (parent_state
>= PERF_EVENT_STATE_INACTIVE
)
8151 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
8153 child_event
->state
= PERF_EVENT_STATE_OFF
;
8155 if (parent_event
->attr
.freq
) {
8156 u64 sample_period
= parent_event
->hw
.sample_period
;
8157 struct hw_perf_event
*hwc
= &child_event
->hw
;
8159 hwc
->sample_period
= sample_period
;
8160 hwc
->last_period
= sample_period
;
8162 local64_set(&hwc
->period_left
, sample_period
);
8165 child_event
->ctx
= child_ctx
;
8166 child_event
->overflow_handler
= parent_event
->overflow_handler
;
8167 child_event
->overflow_handler_context
8168 = parent_event
->overflow_handler_context
;
8171 * Precalculate sample_data sizes
8173 perf_event__header_size(child_event
);
8174 perf_event__id_header_size(child_event
);
8177 * Link it up in the child's context:
8179 raw_spin_lock_irqsave(&child_ctx
->lock
, flags
);
8180 add_event_to_ctx(child_event
, child_ctx
);
8181 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
8184 * Link this into the parent event's child list
8186 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
8187 mutex_lock(&parent_event
->child_mutex
);
8188 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
8189 mutex_unlock(&parent_event
->child_mutex
);
8194 static int inherit_group(struct perf_event
*parent_event
,
8195 struct task_struct
*parent
,
8196 struct perf_event_context
*parent_ctx
,
8197 struct task_struct
*child
,
8198 struct perf_event_context
*child_ctx
)
8200 struct perf_event
*leader
;
8201 struct perf_event
*sub
;
8202 struct perf_event
*child_ctr
;
8204 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
8205 child
, NULL
, child_ctx
);
8207 return PTR_ERR(leader
);
8208 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
8209 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
8210 child
, leader
, child_ctx
);
8211 if (IS_ERR(child_ctr
))
8212 return PTR_ERR(child_ctr
);
8218 inherit_task_group(struct perf_event
*event
, struct task_struct
*parent
,
8219 struct perf_event_context
*parent_ctx
,
8220 struct task_struct
*child
, int ctxn
,
8224 struct perf_event_context
*child_ctx
;
8226 if (!event
->attr
.inherit
) {
8231 child_ctx
= child
->perf_event_ctxp
[ctxn
];
8234 * This is executed from the parent task context, so
8235 * inherit events that have been marked for cloning.
8236 * First allocate and initialize a context for the
8240 child_ctx
= alloc_perf_context(parent_ctx
->pmu
, child
);
8244 child
->perf_event_ctxp
[ctxn
] = child_ctx
;
8247 ret
= inherit_group(event
, parent
, parent_ctx
,
8257 * Initialize the perf_event context in task_struct
8259 static int perf_event_init_context(struct task_struct
*child
, int ctxn
)
8261 struct perf_event_context
*child_ctx
, *parent_ctx
;
8262 struct perf_event_context
*cloned_ctx
;
8263 struct perf_event
*event
;
8264 struct task_struct
*parent
= current
;
8265 int inherited_all
= 1;
8266 unsigned long flags
;
8269 if (likely(!parent
->perf_event_ctxp
[ctxn
]))
8273 * If the parent's context is a clone, pin it so it won't get
8276 parent_ctx
= perf_pin_task_context(parent
, ctxn
);
8281 * No need to check if parent_ctx != NULL here; since we saw
8282 * it non-NULL earlier, the only reason for it to become NULL
8283 * is if we exit, and since we're currently in the middle of
8284 * a fork we can't be exiting at the same time.
8288 * Lock the parent list. No need to lock the child - not PID
8289 * hashed yet and not running, so nobody can access it.
8291 mutex_lock(&parent_ctx
->mutex
);
8294 * We dont have to disable NMIs - we are only looking at
8295 * the list, not manipulating it:
8297 list_for_each_entry(event
, &parent_ctx
->pinned_groups
, group_entry
) {
8298 ret
= inherit_task_group(event
, parent
, parent_ctx
,
8299 child
, ctxn
, &inherited_all
);
8305 * We can't hold ctx->lock when iterating the ->flexible_group list due
8306 * to allocations, but we need to prevent rotation because
8307 * rotate_ctx() will change the list from interrupt context.
8309 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
8310 parent_ctx
->rotate_disable
= 1;
8311 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
8313 list_for_each_entry(event
, &parent_ctx
->flexible_groups
, group_entry
) {
8314 ret
= inherit_task_group(event
, parent
, parent_ctx
,
8315 child
, ctxn
, &inherited_all
);
8320 raw_spin_lock_irqsave(&parent_ctx
->lock
, flags
);
8321 parent_ctx
->rotate_disable
= 0;
8323 child_ctx
= child
->perf_event_ctxp
[ctxn
];
8325 if (child_ctx
&& inherited_all
) {
8327 * Mark the child context as a clone of the parent
8328 * context, or of whatever the parent is a clone of.
8330 * Note that if the parent is a clone, the holding of
8331 * parent_ctx->lock avoids it from being uncloned.
8333 cloned_ctx
= parent_ctx
->parent_ctx
;
8335 child_ctx
->parent_ctx
= cloned_ctx
;
8336 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
8338 child_ctx
->parent_ctx
= parent_ctx
;
8339 child_ctx
->parent_gen
= parent_ctx
->generation
;
8341 get_ctx(child_ctx
->parent_ctx
);
8344 raw_spin_unlock_irqrestore(&parent_ctx
->lock
, flags
);
8345 mutex_unlock(&parent_ctx
->mutex
);
8347 perf_unpin_context(parent_ctx
);
8348 put_ctx(parent_ctx
);
8354 * Initialize the perf_event context in task_struct
8356 int perf_event_init_task(struct task_struct
*child
)
8360 memset(child
->perf_event_ctxp
, 0, sizeof(child
->perf_event_ctxp
));
8361 mutex_init(&child
->perf_event_mutex
);
8362 INIT_LIST_HEAD(&child
->perf_event_list
);
8364 for_each_task_context_nr(ctxn
) {
8365 ret
= perf_event_init_context(child
, ctxn
);
8367 perf_event_free_task(child
);
8375 static void __init
perf_event_init_all_cpus(void)
8377 struct swevent_htable
*swhash
;
8380 for_each_possible_cpu(cpu
) {
8381 swhash
= &per_cpu(swevent_htable
, cpu
);
8382 mutex_init(&swhash
->hlist_mutex
);
8383 INIT_LIST_HEAD(&per_cpu(active_ctx_list
, cpu
));
8387 static void perf_event_init_cpu(int cpu
)
8389 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
8391 mutex_lock(&swhash
->hlist_mutex
);
8392 swhash
->online
= true;
8393 if (swhash
->hlist_refcount
> 0) {
8394 struct swevent_hlist
*hlist
;
8396 hlist
= kzalloc_node(sizeof(*hlist
), GFP_KERNEL
, cpu_to_node(cpu
));
8398 rcu_assign_pointer(swhash
->swevent_hlist
, hlist
);
8400 mutex_unlock(&swhash
->hlist_mutex
);
8403 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
8404 static void __perf_event_exit_context(void *__info
)
8406 struct remove_event re
= { .detach_group
= true };
8407 struct perf_event_context
*ctx
= __info
;
8410 list_for_each_entry_rcu(re
.event
, &ctx
->event_list
, event_entry
)
8411 __perf_remove_from_context(&re
);
8415 static void perf_event_exit_cpu_context(int cpu
)
8417 struct perf_event_context
*ctx
;
8421 idx
= srcu_read_lock(&pmus_srcu
);
8422 list_for_each_entry_rcu(pmu
, &pmus
, entry
) {
8423 ctx
= &per_cpu_ptr(pmu
->pmu_cpu_context
, cpu
)->ctx
;
8425 mutex_lock(&ctx
->mutex
);
8426 smp_call_function_single(cpu
, __perf_event_exit_context
, ctx
, 1);
8427 mutex_unlock(&ctx
->mutex
);
8429 srcu_read_unlock(&pmus_srcu
, idx
);
8432 static void perf_event_exit_cpu(int cpu
)
8434 struct swevent_htable
*swhash
= &per_cpu(swevent_htable
, cpu
);
8436 perf_event_exit_cpu_context(cpu
);
8438 mutex_lock(&swhash
->hlist_mutex
);
8439 swhash
->online
= false;
8440 swevent_hlist_release(swhash
);
8441 mutex_unlock(&swhash
->hlist_mutex
);
8444 static inline void perf_event_exit_cpu(int cpu
) { }
8448 perf_reboot(struct notifier_block
*notifier
, unsigned long val
, void *v
)
8452 for_each_online_cpu(cpu
)
8453 perf_event_exit_cpu(cpu
);
8459 * Run the perf reboot notifier at the very last possible moment so that
8460 * the generic watchdog code runs as long as possible.
8462 static struct notifier_block perf_reboot_notifier
= {
8463 .notifier_call
= perf_reboot
,
8464 .priority
= INT_MIN
,
8468 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
8470 unsigned int cpu
= (long)hcpu
;
8472 switch (action
& ~CPU_TASKS_FROZEN
) {
8474 case CPU_UP_PREPARE
:
8475 case CPU_DOWN_FAILED
:
8476 perf_event_init_cpu(cpu
);
8479 case CPU_UP_CANCELED
:
8480 case CPU_DOWN_PREPARE
:
8481 perf_event_exit_cpu(cpu
);
8490 void __init
perf_event_init(void)
8496 perf_event_init_all_cpus();
8497 init_srcu_struct(&pmus_srcu
);
8498 perf_pmu_register(&perf_swevent
, "software", PERF_TYPE_SOFTWARE
);
8499 perf_pmu_register(&perf_cpu_clock
, NULL
, -1);
8500 perf_pmu_register(&perf_task_clock
, NULL
, -1);
8502 perf_cpu_notifier(perf_cpu_notify
);
8503 register_reboot_notifier(&perf_reboot_notifier
);
8505 ret
= init_hw_breakpoint();
8506 WARN(ret
, "hw_breakpoint initialization failed with: %d", ret
);
8508 /* do not patch jump label more than once per second */
8509 jump_label_rate_limit(&perf_sched_events
, HZ
);
8512 * Build time assertion that we keep the data_head at the intended
8513 * location. IOW, validation we got the __reserved[] size right.
8515 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page
, data_head
))
8519 static int __init
perf_event_sysfs_init(void)
8524 mutex_lock(&pmus_lock
);
8526 ret
= bus_register(&pmu_bus
);
8530 list_for_each_entry(pmu
, &pmus
, entry
) {
8531 if (!pmu
->name
|| pmu
->type
< 0)
8534 ret
= pmu_dev_alloc(pmu
);
8535 WARN(ret
, "Failed to register pmu: %s, reason %d\n", pmu
->name
, ret
);
8537 pmu_bus_running
= 1;
8541 mutex_unlock(&pmus_lock
);
8545 device_initcall(perf_event_sysfs_init
);
8547 #ifdef CONFIG_CGROUP_PERF
8548 static struct cgroup_subsys_state
*
8549 perf_cgroup_css_alloc(struct cgroup_subsys_state
*parent_css
)
8551 struct perf_cgroup
*jc
;
8553 jc
= kzalloc(sizeof(*jc
), GFP_KERNEL
);
8555 return ERR_PTR(-ENOMEM
);
8557 jc
->info
= alloc_percpu(struct perf_cgroup_info
);
8560 return ERR_PTR(-ENOMEM
);
8566 static void perf_cgroup_css_free(struct cgroup_subsys_state
*css
)
8568 struct perf_cgroup
*jc
= container_of(css
, struct perf_cgroup
, css
);
8570 free_percpu(jc
->info
);
8574 static int __perf_cgroup_move(void *info
)
8576 struct task_struct
*task
= info
;
8577 perf_cgroup_switch(task
, PERF_CGROUP_SWOUT
| PERF_CGROUP_SWIN
);
8581 static void perf_cgroup_attach(struct cgroup_subsys_state
*css
,
8582 struct cgroup_taskset
*tset
)
8584 struct task_struct
*task
;
8586 cgroup_taskset_for_each(task
, tset
)
8587 task_function_call(task
, __perf_cgroup_move
, task
);
8590 static void perf_cgroup_exit(struct cgroup_subsys_state
*css
,
8591 struct cgroup_subsys_state
*old_css
,
8592 struct task_struct
*task
)
8595 * cgroup_exit() is called in the copy_process() failure path.
8596 * Ignore this case since the task hasn't ran yet, this avoids
8597 * trying to poke a half freed task state from generic code.
8599 if (!(task
->flags
& PF_EXITING
))
8602 task_function_call(task
, __perf_cgroup_move
, task
);
8605 struct cgroup_subsys perf_event_cgrp_subsys
= {
8606 .css_alloc
= perf_cgroup_css_alloc
,
8607 .css_free
= perf_cgroup_css_free
,
8608 .exit
= perf_cgroup_exit
,
8609 .attach
= perf_cgroup_attach
,
8611 #endif /* CONFIG_CGROUP_PERF */