2 * Performance events core code:
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/idr.h>
17 #include <linux/file.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/hash.h>
21 #include <linux/tick.h>
22 #include <linux/sysfs.h>
23 #include <linux/dcache.h>
24 #include <linux/percpu.h>
25 #include <linux/ptrace.h>
26 #include <linux/reboot.h>
27 #include <linux/vmstat.h>
28 #include <linux/device.h>
29 #include <linux/export.h>
30 #include <linux/vmalloc.h>
31 #include <linux/hardirq.h>
32 #include <linux/rculist.h>
33 #include <linux/uaccess.h>
34 #include <linux/syscalls.h>
35 #include <linux/anon_inodes.h>
36 #include <linux/kernel_stat.h>
37 #include <linux/perf_event.h>
38 #include <linux/ftrace_event.h>
39 #include <linux/hw_breakpoint.h>
40 #include <linux/mm_types.h>
41 #include <linux/cgroup.h>
42 #include <linux/module.h>
46 #include <asm/irq_regs.h>
48 struct remote_function_call {
49 struct task_struct *p;
50 int (*func)(void *info);
55 static void remote_function(void *data)
57 struct remote_function_call *tfc = data;
58 struct task_struct *p = tfc->p;
62 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
66 tfc->ret = tfc->func(tfc->info);
70 * task_function_call - call a function on the cpu on which a task runs
71 * @p: the task to evaluate
72 * @func: the function to be called
73 * @info: the function call argument
75 * Calls the function @func when the task is currently running. This might
76 * be on the current CPU, which just calls the function directly
78 * returns: @func return value, or
79 * -ESRCH - when the process isn't running
80 * -EAGAIN - when the process moved away
83 task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
85 struct remote_function_call data = {
89 .ret = -ESRCH, /* No such (running) process */
93 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
99 * cpu_function_call - call a function on the cpu
100 * @func: the function to be called
101 * @info: the function call argument
103 * Calls the function @func on the remote cpu.
105 * returns: @func return value or -ENXIO when the cpu is offline
107 static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
109 struct remote_function_call data = {
113 .ret = -ENXIO, /* No such CPU */
116 smp_call_function_single(cpu, remote_function, &data, 1);
121 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
122 PERF_FLAG_FD_OUTPUT |\
123 PERF_FLAG_PID_CGROUP |\
124 PERF_FLAG_FD_CLOEXEC)
127 * branch priv levels that need permission checks
129 #define PERF_SAMPLE_BRANCH_PERM_PLM \
130 (PERF_SAMPLE_BRANCH_KERNEL |\
131 PERF_SAMPLE_BRANCH_HV)
134 EVENT_FLEXIBLE = 0x1,
136 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
140 * perf_sched_events : >0 events exist
141 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
143 struct static_key_deferred perf_sched_events __read_mostly;
144 static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
145 static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
147 static atomic_t nr_mmap_events __read_mostly;
148 static atomic_t nr_comm_events __read_mostly;
149 static atomic_t nr_task_events __read_mostly;
150 static atomic_t nr_freq_events __read_mostly;
152 static LIST_HEAD(pmus);
153 static DEFINE_MUTEX(pmus_lock);
154 static struct srcu_struct pmus_srcu;
157 * perf event paranoia level:
158 * -1 - not paranoid at all
159 * 0 - disallow raw tracepoint access for unpriv
160 * 1 - disallow cpu events for unpriv
161 * 2 - disallow kernel profiling for unpriv
163 int sysctl_perf_event_paranoid __read_mostly = 1;
165 /* Minimum for 512 kiB + 1 user control page */
166 int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
169 * max perf event sample rate
171 #define DEFAULT_MAX_SAMPLE_RATE 100000
172 #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
173 #define DEFAULT_CPU_TIME_MAX_PERCENT 25
175 int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
177 static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
178 static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
180 static int perf_sample_allowed_ns __read_mostly =
181 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
183 void update_perf_cpu_limits(void)
185 u64 tmp = perf_sample_period_ns;
187 tmp *= sysctl_perf_cpu_time_max_percent;
189 ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
192 static int perf_rotate_context(struct perf_cpu_context *cpuctx);
194 int perf_proc_update_handler(struct ctl_table *table, int write,
195 void __user *buffer, size_t *lenp,
198 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
203 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
204 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
205 update_perf_cpu_limits();
210 int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
212 int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
213 void __user *buffer, size_t *lenp,
216 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
221 update_perf_cpu_limits();
227 * perf samples are done in some very critical code paths (NMIs).
228 * If they take too much CPU time, the system can lock up and not
229 * get any real work done. This will drop the sample rate when
230 * we detect that events are taking too long.
232 #define NR_ACCUMULATED_SAMPLES 128
233 static DEFINE_PER_CPU(u64, running_sample_length);
235 static void perf_duration_warn(struct irq_work *w)
237 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
238 u64 avg_local_sample_len;
239 u64 local_samples_len;
241 local_samples_len = __get_cpu_var(running_sample_length);
242 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
244 printk_ratelimited(KERN_WARNING
245 "perf interrupt took too long (%lld > %lld), lowering "
246 "kernel.perf_event_max_sample_rate to %d\n",
247 avg_local_sample_len, allowed_ns >> 1,
248 sysctl_perf_event_sample_rate);
251 static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
253 void perf_sample_event_took(u64 sample_len_ns)
255 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
256 u64 avg_local_sample_len;
257 u64 local_samples_len;
262 /* decay the counter by 1 average sample */
263 local_samples_len = __get_cpu_var(running_sample_length);
264 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
265 local_samples_len += sample_len_ns;
266 __get_cpu_var(running_sample_length) = local_samples_len;
269 * note: this will be biased artifically low until we have
270 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
271 * from having to maintain a count.
273 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
275 if (avg_local_sample_len <= allowed_ns)
278 if (max_samples_per_tick <= 1)
281 max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
282 sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
283 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
285 update_perf_cpu_limits();
287 if (!irq_work_queue(&perf_duration_work)) {
288 early_printk("perf interrupt took too long (%lld > %lld), lowering "
289 "kernel.perf_event_max_sample_rate to %d\n",
290 avg_local_sample_len, allowed_ns >> 1,
291 sysctl_perf_event_sample_rate);
295 static atomic64_t perf_event_id;
297 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
298 enum event_type_t event_type);
300 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
301 enum event_type_t event_type,
302 struct task_struct *task);
304 static void update_context_time(struct perf_event_context *ctx);
305 static u64 perf_event_time(struct perf_event *event);
307 void __weak perf_event_print_debug(void) { }
309 extern __weak const char *perf_pmu_name(void)
314 static inline u64 perf_clock(void)
316 return local_clock();
319 static inline struct perf_cpu_context *
320 __get_cpu_context(struct perf_event_context *ctx)
322 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
325 static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
326 struct perf_event_context *ctx)
328 raw_spin_lock(&cpuctx->ctx.lock);
330 raw_spin_lock(&ctx->lock);
333 static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
334 struct perf_event_context *ctx)
337 raw_spin_unlock(&ctx->lock);
338 raw_spin_unlock(&cpuctx->ctx.lock);
341 #ifdef CONFIG_CGROUP_PERF
344 * perf_cgroup_info keeps track of time_enabled for a cgroup.
345 * This is a per-cpu dynamically allocated data structure.
347 struct perf_cgroup_info {
353 struct cgroup_subsys_state css;
354 struct perf_cgroup_info __percpu *info;
358 * Must ensure cgroup is pinned (css_get) before calling
359 * this function. In other words, we cannot call this function
360 * if there is no cgroup event for the current CPU context.
362 static inline struct perf_cgroup *
363 perf_cgroup_from_task(struct task_struct *task)
365 return container_of(task_css(task, perf_event_cgrp_id),
366 struct perf_cgroup, css);
370 perf_cgroup_match(struct perf_event *event)
372 struct perf_event_context *ctx = event->ctx;
373 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
375 /* @event doesn't care about cgroup */
379 /* wants specific cgroup scope but @cpuctx isn't associated with any */
384 * Cgroup scoping is recursive. An event enabled for a cgroup is
385 * also enabled for all its descendant cgroups. If @cpuctx's
386 * cgroup is a descendant of @event's (the test covers identity
387 * case), it's a match.
389 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
390 event->cgrp->css.cgroup);
393 static inline void perf_put_cgroup(struct perf_event *event)
395 css_put(&event->cgrp->css);
398 static inline void perf_detach_cgroup(struct perf_event *event)
400 perf_put_cgroup(event);
404 static inline int is_cgroup_event(struct perf_event *event)
406 return event->cgrp != NULL;
409 static inline u64 perf_cgroup_event_time(struct perf_event *event)
411 struct perf_cgroup_info *t;
413 t = per_cpu_ptr(event->cgrp->info, event->cpu);
417 static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
419 struct perf_cgroup_info *info;
424 info = this_cpu_ptr(cgrp->info);
426 info->time += now - info->timestamp;
427 info->timestamp = now;
430 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
432 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
434 __update_cgrp_time(cgrp_out);
437 static inline void update_cgrp_time_from_event(struct perf_event *event)
439 struct perf_cgroup *cgrp;
442 * ensure we access cgroup data only when needed and
443 * when we know the cgroup is pinned (css_get)
445 if (!is_cgroup_event(event))
448 cgrp = perf_cgroup_from_task(current);
450 * Do not update time when cgroup is not active
452 if (cgrp == event->cgrp)
453 __update_cgrp_time(event->cgrp);
457 perf_cgroup_set_timestamp(struct task_struct *task,
458 struct perf_event_context *ctx)
460 struct perf_cgroup *cgrp;
461 struct perf_cgroup_info *info;
464 * ctx->lock held by caller
465 * ensure we do not access cgroup data
466 * unless we have the cgroup pinned (css_get)
468 if (!task || !ctx->nr_cgroups)
471 cgrp = perf_cgroup_from_task(task);
472 info = this_cpu_ptr(cgrp->info);
473 info->timestamp = ctx->timestamp;
476 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
477 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
480 * reschedule events based on the cgroup constraint of task.
482 * mode SWOUT : schedule out everything
483 * mode SWIN : schedule in based on cgroup for next
485 void perf_cgroup_switch(struct task_struct *task, int mode)
487 struct perf_cpu_context *cpuctx;
492 * disable interrupts to avoid geting nr_cgroup
493 * changes via __perf_event_disable(). Also
496 local_irq_save(flags);
499 * we reschedule only in the presence of cgroup
500 * constrained events.
504 list_for_each_entry_rcu(pmu, &pmus, entry) {
505 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
506 if (cpuctx->unique_pmu != pmu)
507 continue; /* ensure we process each cpuctx once */
510 * perf_cgroup_events says at least one
511 * context on this CPU has cgroup events.
513 * ctx->nr_cgroups reports the number of cgroup
514 * events for a context.
516 if (cpuctx->ctx.nr_cgroups > 0) {
517 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
518 perf_pmu_disable(cpuctx->ctx.pmu);
520 if (mode & PERF_CGROUP_SWOUT) {
521 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
523 * must not be done before ctxswout due
524 * to event_filter_match() in event_sched_out()
529 if (mode & PERF_CGROUP_SWIN) {
530 WARN_ON_ONCE(cpuctx->cgrp);
532 * set cgrp before ctxsw in to allow
533 * event_filter_match() to not have to pass
536 cpuctx->cgrp = perf_cgroup_from_task(task);
537 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
539 perf_pmu_enable(cpuctx->ctx.pmu);
540 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
546 local_irq_restore(flags);
549 static inline void perf_cgroup_sched_out(struct task_struct *task,
550 struct task_struct *next)
552 struct perf_cgroup *cgrp1;
553 struct perf_cgroup *cgrp2 = NULL;
556 * we come here when we know perf_cgroup_events > 0
558 cgrp1 = perf_cgroup_from_task(task);
561 * next is NULL when called from perf_event_enable_on_exec()
562 * that will systematically cause a cgroup_switch()
565 cgrp2 = perf_cgroup_from_task(next);
568 * only schedule out current cgroup events if we know
569 * that we are switching to a different cgroup. Otherwise,
570 * do no touch the cgroup events.
573 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
576 static inline void perf_cgroup_sched_in(struct task_struct *prev,
577 struct task_struct *task)
579 struct perf_cgroup *cgrp1;
580 struct perf_cgroup *cgrp2 = NULL;
583 * we come here when we know perf_cgroup_events > 0
585 cgrp1 = perf_cgroup_from_task(task);
587 /* prev can never be NULL */
588 cgrp2 = perf_cgroup_from_task(prev);
591 * only need to schedule in cgroup events if we are changing
592 * cgroup during ctxsw. Cgroup events were not scheduled
593 * out of ctxsw out if that was not the case.
596 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
599 static inline int perf_cgroup_connect(int fd, struct perf_event *event,
600 struct perf_event_attr *attr,
601 struct perf_event *group_leader)
603 struct perf_cgroup *cgrp;
604 struct cgroup_subsys_state *css;
605 struct fd f = fdget(fd);
611 css = css_tryget_from_dir(f.file->f_dentry, &perf_event_cgrp_subsys);
617 cgrp = container_of(css, struct perf_cgroup, css);
621 * all events in a group must monitor
622 * the same cgroup because a task belongs
623 * to only one perf cgroup at a time
625 if (group_leader && group_leader->cgrp != cgrp) {
626 perf_detach_cgroup(event);
635 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
637 struct perf_cgroup_info *t;
638 t = per_cpu_ptr(event->cgrp->info, event->cpu);
639 event->shadow_ctx_time = now - t->timestamp;
643 perf_cgroup_defer_enabled(struct perf_event *event)
646 * when the current task's perf cgroup does not match
647 * the event's, we need to remember to call the
648 * perf_mark_enable() function the first time a task with
649 * a matching perf cgroup is scheduled in.
651 if (is_cgroup_event(event) && !perf_cgroup_match(event))
652 event->cgrp_defer_enabled = 1;
656 perf_cgroup_mark_enabled(struct perf_event *event,
657 struct perf_event_context *ctx)
659 struct perf_event *sub;
660 u64 tstamp = perf_event_time(event);
662 if (!event->cgrp_defer_enabled)
665 event->cgrp_defer_enabled = 0;
667 event->tstamp_enabled = tstamp - event->total_time_enabled;
668 list_for_each_entry(sub, &event->sibling_list, group_entry) {
669 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
670 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
671 sub->cgrp_defer_enabled = 0;
675 #else /* !CONFIG_CGROUP_PERF */
678 perf_cgroup_match(struct perf_event *event)
683 static inline void perf_detach_cgroup(struct perf_event *event)
686 static inline int is_cgroup_event(struct perf_event *event)
691 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
696 static inline void update_cgrp_time_from_event(struct perf_event *event)
700 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
704 static inline void perf_cgroup_sched_out(struct task_struct *task,
705 struct task_struct *next)
709 static inline void perf_cgroup_sched_in(struct task_struct *prev,
710 struct task_struct *task)
714 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
715 struct perf_event_attr *attr,
716 struct perf_event *group_leader)
722 perf_cgroup_set_timestamp(struct task_struct *task,
723 struct perf_event_context *ctx)
728 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
733 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
737 static inline u64 perf_cgroup_event_time(struct perf_event *event)
743 perf_cgroup_defer_enabled(struct perf_event *event)
748 perf_cgroup_mark_enabled(struct perf_event *event,
749 struct perf_event_context *ctx)
755 * set default to be dependent on timer tick just
758 #define PERF_CPU_HRTIMER (1000 / HZ)
760 * function must be called with interrupts disbled
762 static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
764 struct perf_cpu_context *cpuctx;
765 enum hrtimer_restart ret = HRTIMER_NORESTART;
768 WARN_ON(!irqs_disabled());
770 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
772 rotations = perf_rotate_context(cpuctx);
775 * arm timer if needed
778 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
779 ret = HRTIMER_RESTART;
785 /* CPU is going down */
786 void perf_cpu_hrtimer_cancel(int cpu)
788 struct perf_cpu_context *cpuctx;
792 if (WARN_ON(cpu != smp_processor_id()))
795 local_irq_save(flags);
799 list_for_each_entry_rcu(pmu, &pmus, entry) {
800 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
802 if (pmu->task_ctx_nr == perf_sw_context)
805 hrtimer_cancel(&cpuctx->hrtimer);
810 local_irq_restore(flags);
813 static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
815 struct hrtimer *hr = &cpuctx->hrtimer;
816 struct pmu *pmu = cpuctx->ctx.pmu;
819 /* no multiplexing needed for SW PMU */
820 if (pmu->task_ctx_nr == perf_sw_context)
824 * check default is sane, if not set then force to
825 * default interval (1/tick)
827 timer = pmu->hrtimer_interval_ms;
829 timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
831 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
833 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
834 hr->function = perf_cpu_hrtimer_handler;
837 static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
839 struct hrtimer *hr = &cpuctx->hrtimer;
840 struct pmu *pmu = cpuctx->ctx.pmu;
843 if (pmu->task_ctx_nr == perf_sw_context)
846 if (hrtimer_active(hr))
849 if (!hrtimer_callback_running(hr))
850 __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
851 0, HRTIMER_MODE_REL_PINNED, 0);
854 void perf_pmu_disable(struct pmu *pmu)
856 int *count = this_cpu_ptr(pmu->pmu_disable_count);
858 pmu->pmu_disable(pmu);
861 void perf_pmu_enable(struct pmu *pmu)
863 int *count = this_cpu_ptr(pmu->pmu_disable_count);
865 pmu->pmu_enable(pmu);
868 static DEFINE_PER_CPU(struct list_head, rotation_list);
871 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
872 * because they're strictly cpu affine and rotate_start is called with IRQs
873 * disabled, while rotate_context is called from IRQ context.
875 static void perf_pmu_rotate_start(struct pmu *pmu)
877 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
878 struct list_head *head = &__get_cpu_var(rotation_list);
880 WARN_ON(!irqs_disabled());
882 if (list_empty(&cpuctx->rotation_list))
883 list_add(&cpuctx->rotation_list, head);
886 static void get_ctx(struct perf_event_context *ctx)
888 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
891 static void put_ctx(struct perf_event_context *ctx)
893 if (atomic_dec_and_test(&ctx->refcount)) {
895 put_ctx(ctx->parent_ctx);
897 put_task_struct(ctx->task);
898 kfree_rcu(ctx, rcu_head);
902 static void unclone_ctx(struct perf_event_context *ctx)
904 if (ctx->parent_ctx) {
905 put_ctx(ctx->parent_ctx);
906 ctx->parent_ctx = NULL;
911 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
914 * only top level events have the pid namespace they were created in
917 event = event->parent;
919 return task_tgid_nr_ns(p, event->ns);
922 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
925 * only top level events have the pid namespace they were created in
928 event = event->parent;
930 return task_pid_nr_ns(p, event->ns);
934 * If we inherit events we want to return the parent event id
937 static u64 primary_event_id(struct perf_event *event)
942 id = event->parent->id;
948 * Get the perf_event_context for a task and lock it.
949 * This has to cope with with the fact that until it is locked,
950 * the context could get moved to another task.
952 static struct perf_event_context *
953 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
955 struct perf_event_context *ctx;
959 * One of the few rules of preemptible RCU is that one cannot do
960 * rcu_read_unlock() while holding a scheduler (or nested) lock when
961 * part of the read side critical section was preemptible -- see
962 * rcu_read_unlock_special().
964 * Since ctx->lock nests under rq->lock we must ensure the entire read
965 * side critical section is non-preemptible.
969 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
972 * If this context is a clone of another, it might
973 * get swapped for another underneath us by
974 * perf_event_task_sched_out, though the
975 * rcu_read_lock() protects us from any context
976 * getting freed. Lock the context and check if it
977 * got swapped before we could get the lock, and retry
978 * if so. If we locked the right context, then it
979 * can't get swapped on us any more.
981 raw_spin_lock_irqsave(&ctx->lock, *flags);
982 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
983 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
989 if (!atomic_inc_not_zero(&ctx->refcount)) {
990 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
1000 * Get the context for a task and increment its pin_count so it
1001 * can't get swapped to another task. This also increments its
1002 * reference count so that the context can't get freed.
1004 static struct perf_event_context *
1005 perf_pin_task_context(struct task_struct *task, int ctxn)
1007 struct perf_event_context *ctx;
1008 unsigned long flags;
1010 ctx = perf_lock_task_context(task, ctxn, &flags);
1013 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1018 static void perf_unpin_context(struct perf_event_context *ctx)
1020 unsigned long flags;
1022 raw_spin_lock_irqsave(&ctx->lock, flags);
1024 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1028 * Update the record of the current time in a context.
1030 static void update_context_time(struct perf_event_context *ctx)
1032 u64 now = perf_clock();
1034 ctx->time += now - ctx->timestamp;
1035 ctx->timestamp = now;
1038 static u64 perf_event_time(struct perf_event *event)
1040 struct perf_event_context *ctx = event->ctx;
1042 if (is_cgroup_event(event))
1043 return perf_cgroup_event_time(event);
1045 return ctx ? ctx->time : 0;
1049 * Update the total_time_enabled and total_time_running fields for a event.
1050 * The caller of this function needs to hold the ctx->lock.
1052 static void update_event_times(struct perf_event *event)
1054 struct perf_event_context *ctx = event->ctx;
1057 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1058 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1061 * in cgroup mode, time_enabled represents
1062 * the time the event was enabled AND active
1063 * tasks were in the monitored cgroup. This is
1064 * independent of the activity of the context as
1065 * there may be a mix of cgroup and non-cgroup events.
1067 * That is why we treat cgroup events differently
1070 if (is_cgroup_event(event))
1071 run_end = perf_cgroup_event_time(event);
1072 else if (ctx->is_active)
1073 run_end = ctx->time;
1075 run_end = event->tstamp_stopped;
1077 event->total_time_enabled = run_end - event->tstamp_enabled;
1079 if (event->state == PERF_EVENT_STATE_INACTIVE)
1080 run_end = event->tstamp_stopped;
1082 run_end = perf_event_time(event);
1084 event->total_time_running = run_end - event->tstamp_running;
1089 * Update total_time_enabled and total_time_running for all events in a group.
1091 static void update_group_times(struct perf_event *leader)
1093 struct perf_event *event;
1095 update_event_times(leader);
1096 list_for_each_entry(event, &leader->sibling_list, group_entry)
1097 update_event_times(event);
1100 static struct list_head *
1101 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1103 if (event->attr.pinned)
1104 return &ctx->pinned_groups;
1106 return &ctx->flexible_groups;
1110 * Add a event from the lists for its context.
1111 * Must be called with ctx->mutex and ctx->lock held.
1114 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1116 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1117 event->attach_state |= PERF_ATTACH_CONTEXT;
1120 * If we're a stand alone event or group leader, we go to the context
1121 * list, group events are kept attached to the group so that
1122 * perf_group_detach can, at all times, locate all siblings.
1124 if (event->group_leader == event) {
1125 struct list_head *list;
1127 if (is_software_event(event))
1128 event->group_flags |= PERF_GROUP_SOFTWARE;
1130 list = ctx_group_list(event, ctx);
1131 list_add_tail(&event->group_entry, list);
1134 if (is_cgroup_event(event))
1137 if (has_branch_stack(event))
1138 ctx->nr_branch_stack++;
1140 list_add_rcu(&event->event_entry, &ctx->event_list);
1141 if (!ctx->nr_events)
1142 perf_pmu_rotate_start(ctx->pmu);
1144 if (event->attr.inherit_stat)
1151 * Initialize event state based on the perf_event_attr::disabled.
1153 static inline void perf_event__state_init(struct perf_event *event)
1155 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1156 PERF_EVENT_STATE_INACTIVE;
1160 * Called at perf_event creation and when events are attached/detached from a
1163 static void perf_event__read_size(struct perf_event *event)
1165 int entry = sizeof(u64); /* value */
1169 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1170 size += sizeof(u64);
1172 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1173 size += sizeof(u64);
1175 if (event->attr.read_format & PERF_FORMAT_ID)
1176 entry += sizeof(u64);
1178 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1179 nr += event->group_leader->nr_siblings;
1180 size += sizeof(u64);
1184 event->read_size = size;
1187 static void perf_event__header_size(struct perf_event *event)
1189 struct perf_sample_data *data;
1190 u64 sample_type = event->attr.sample_type;
1193 perf_event__read_size(event);
1195 if (sample_type & PERF_SAMPLE_IP)
1196 size += sizeof(data->ip);
1198 if (sample_type & PERF_SAMPLE_ADDR)
1199 size += sizeof(data->addr);
1201 if (sample_type & PERF_SAMPLE_PERIOD)
1202 size += sizeof(data->period);
1204 if (sample_type & PERF_SAMPLE_WEIGHT)
1205 size += sizeof(data->weight);
1207 if (sample_type & PERF_SAMPLE_READ)
1208 size += event->read_size;
1210 if (sample_type & PERF_SAMPLE_DATA_SRC)
1211 size += sizeof(data->data_src.val);
1213 if (sample_type & PERF_SAMPLE_TRANSACTION)
1214 size += sizeof(data->txn);
1216 event->header_size = size;
1219 static void perf_event__id_header_size(struct perf_event *event)
1221 struct perf_sample_data *data;
1222 u64 sample_type = event->attr.sample_type;
1225 if (sample_type & PERF_SAMPLE_TID)
1226 size += sizeof(data->tid_entry);
1228 if (sample_type & PERF_SAMPLE_TIME)
1229 size += sizeof(data->time);
1231 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1232 size += sizeof(data->id);
1234 if (sample_type & PERF_SAMPLE_ID)
1235 size += sizeof(data->id);
1237 if (sample_type & PERF_SAMPLE_STREAM_ID)
1238 size += sizeof(data->stream_id);
1240 if (sample_type & PERF_SAMPLE_CPU)
1241 size += sizeof(data->cpu_entry);
1243 event->id_header_size = size;
1246 static void perf_group_attach(struct perf_event *event)
1248 struct perf_event *group_leader = event->group_leader, *pos;
1251 * We can have double attach due to group movement in perf_event_open.
1253 if (event->attach_state & PERF_ATTACH_GROUP)
1256 event->attach_state |= PERF_ATTACH_GROUP;
1258 if (group_leader == event)
1261 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1262 !is_software_event(event))
1263 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1265 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1266 group_leader->nr_siblings++;
1268 perf_event__header_size(group_leader);
1270 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1271 perf_event__header_size(pos);
1275 * Remove a event from the lists for its context.
1276 * Must be called with ctx->mutex and ctx->lock held.
1279 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1281 struct perf_cpu_context *cpuctx;
1283 * We can have double detach due to exit/hot-unplug + close.
1285 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
1288 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1290 if (is_cgroup_event(event)) {
1292 cpuctx = __get_cpu_context(ctx);
1294 * if there are no more cgroup events
1295 * then cler cgrp to avoid stale pointer
1296 * in update_cgrp_time_from_cpuctx()
1298 if (!ctx->nr_cgroups)
1299 cpuctx->cgrp = NULL;
1302 if (has_branch_stack(event))
1303 ctx->nr_branch_stack--;
1306 if (event->attr.inherit_stat)
1309 list_del_rcu(&event->event_entry);
1311 if (event->group_leader == event)
1312 list_del_init(&event->group_entry);
1314 update_group_times(event);
1317 * If event was in error state, then keep it
1318 * that way, otherwise bogus counts will be
1319 * returned on read(). The only way to get out
1320 * of error state is by explicit re-enabling
1323 if (event->state > PERF_EVENT_STATE_OFF)
1324 event->state = PERF_EVENT_STATE_OFF;
1329 static void perf_group_detach(struct perf_event *event)
1331 struct perf_event *sibling, *tmp;
1332 struct list_head *list = NULL;
1335 * We can have double detach due to exit/hot-unplug + close.
1337 if (!(event->attach_state & PERF_ATTACH_GROUP))
1340 event->attach_state &= ~PERF_ATTACH_GROUP;
1343 * If this is a sibling, remove it from its group.
1345 if (event->group_leader != event) {
1346 list_del_init(&event->group_entry);
1347 event->group_leader->nr_siblings--;
1351 if (!list_empty(&event->group_entry))
1352 list = &event->group_entry;
1355 * If this was a group event with sibling events then
1356 * upgrade the siblings to singleton events by adding them
1357 * to whatever list we are on.
1359 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
1361 list_move_tail(&sibling->group_entry, list);
1362 sibling->group_leader = sibling;
1364 /* Inherit group flags from the previous leader */
1365 sibling->group_flags = event->group_flags;
1369 perf_event__header_size(event->group_leader);
1371 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1372 perf_event__header_size(tmp);
1376 event_filter_match(struct perf_event *event)
1378 return (event->cpu == -1 || event->cpu == smp_processor_id())
1379 && perf_cgroup_match(event);
1383 event_sched_out(struct perf_event *event,
1384 struct perf_cpu_context *cpuctx,
1385 struct perf_event_context *ctx)
1387 u64 tstamp = perf_event_time(event);
1390 * An event which could not be activated because of
1391 * filter mismatch still needs to have its timings
1392 * maintained, otherwise bogus information is return
1393 * via read() for time_enabled, time_running:
1395 if (event->state == PERF_EVENT_STATE_INACTIVE
1396 && !event_filter_match(event)) {
1397 delta = tstamp - event->tstamp_stopped;
1398 event->tstamp_running += delta;
1399 event->tstamp_stopped = tstamp;
1402 if (event->state != PERF_EVENT_STATE_ACTIVE)
1405 perf_pmu_disable(event->pmu);
1407 event->state = PERF_EVENT_STATE_INACTIVE;
1408 if (event->pending_disable) {
1409 event->pending_disable = 0;
1410 event->state = PERF_EVENT_STATE_OFF;
1412 event->tstamp_stopped = tstamp;
1413 event->pmu->del(event, 0);
1416 if (!is_software_event(event))
1417 cpuctx->active_oncpu--;
1419 if (event->attr.freq && event->attr.sample_freq)
1421 if (event->attr.exclusive || !cpuctx->active_oncpu)
1422 cpuctx->exclusive = 0;
1424 perf_pmu_enable(event->pmu);
1428 group_sched_out(struct perf_event *group_event,
1429 struct perf_cpu_context *cpuctx,
1430 struct perf_event_context *ctx)
1432 struct perf_event *event;
1433 int state = group_event->state;
1435 event_sched_out(group_event, cpuctx, ctx);
1438 * Schedule out siblings (if any):
1440 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1441 event_sched_out(event, cpuctx, ctx);
1443 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
1444 cpuctx->exclusive = 0;
1448 * Cross CPU call to remove a performance event
1450 * We disable the event on the hardware level first. After that we
1451 * remove it from the context list.
1453 static int __perf_remove_from_context(void *info)
1455 struct perf_event *event = info;
1456 struct perf_event_context *ctx = event->ctx;
1457 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1459 raw_spin_lock(&ctx->lock);
1460 event_sched_out(event, cpuctx, ctx);
1461 list_del_event(event, ctx);
1462 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1464 cpuctx->task_ctx = NULL;
1466 raw_spin_unlock(&ctx->lock);
1473 * Remove the event from a task's (or a CPU's) list of events.
1475 * CPU events are removed with a smp call. For task events we only
1476 * call when the task is on a CPU.
1478 * If event->ctx is a cloned context, callers must make sure that
1479 * every task struct that event->ctx->task could possibly point to
1480 * remains valid. This is OK when called from perf_release since
1481 * that only calls us on the top-level context, which can't be a clone.
1482 * When called from perf_event_exit_task, it's OK because the
1483 * context has been detached from its task.
1485 static void perf_remove_from_context(struct perf_event *event)
1487 struct perf_event_context *ctx = event->ctx;
1488 struct task_struct *task = ctx->task;
1490 lockdep_assert_held(&ctx->mutex);
1494 * Per cpu events are removed via an smp call and
1495 * the removal is always successful.
1497 cpu_function_call(event->cpu, __perf_remove_from_context, event);
1502 if (!task_function_call(task, __perf_remove_from_context, event))
1505 raw_spin_lock_irq(&ctx->lock);
1507 * If we failed to find a running task, but find the context active now
1508 * that we've acquired the ctx->lock, retry.
1510 if (ctx->is_active) {
1511 raw_spin_unlock_irq(&ctx->lock);
1516 * Since the task isn't running, its safe to remove the event, us
1517 * holding the ctx->lock ensures the task won't get scheduled in.
1519 list_del_event(event, ctx);
1520 raw_spin_unlock_irq(&ctx->lock);
1524 * Cross CPU call to disable a performance event
1526 int __perf_event_disable(void *info)
1528 struct perf_event *event = info;
1529 struct perf_event_context *ctx = event->ctx;
1530 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1533 * If this is a per-task event, need to check whether this
1534 * event's task is the current task on this cpu.
1536 * Can trigger due to concurrent perf_event_context_sched_out()
1537 * flipping contexts around.
1539 if (ctx->task && cpuctx->task_ctx != ctx)
1542 raw_spin_lock(&ctx->lock);
1545 * If the event is on, turn it off.
1546 * If it is in error state, leave it in error state.
1548 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1549 update_context_time(ctx);
1550 update_cgrp_time_from_event(event);
1551 update_group_times(event);
1552 if (event == event->group_leader)
1553 group_sched_out(event, cpuctx, ctx);
1555 event_sched_out(event, cpuctx, ctx);
1556 event->state = PERF_EVENT_STATE_OFF;
1559 raw_spin_unlock(&ctx->lock);
1567 * If event->ctx is a cloned context, callers must make sure that
1568 * every task struct that event->ctx->task could possibly point to
1569 * remains valid. This condition is satisifed when called through
1570 * perf_event_for_each_child or perf_event_for_each because they
1571 * hold the top-level event's child_mutex, so any descendant that
1572 * goes to exit will block in sync_child_event.
1573 * When called from perf_pending_event it's OK because event->ctx
1574 * is the current context on this CPU and preemption is disabled,
1575 * hence we can't get into perf_event_task_sched_out for this context.
1577 void perf_event_disable(struct perf_event *event)
1579 struct perf_event_context *ctx = event->ctx;
1580 struct task_struct *task = ctx->task;
1584 * Disable the event on the cpu that it's on
1586 cpu_function_call(event->cpu, __perf_event_disable, event);
1591 if (!task_function_call(task, __perf_event_disable, event))
1594 raw_spin_lock_irq(&ctx->lock);
1596 * If the event is still active, we need to retry the cross-call.
1598 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1599 raw_spin_unlock_irq(&ctx->lock);
1601 * Reload the task pointer, it might have been changed by
1602 * a concurrent perf_event_context_sched_out().
1609 * Since we have the lock this context can't be scheduled
1610 * in, so we can change the state safely.
1612 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1613 update_group_times(event);
1614 event->state = PERF_EVENT_STATE_OFF;
1616 raw_spin_unlock_irq(&ctx->lock);
1618 EXPORT_SYMBOL_GPL(perf_event_disable);
1620 static void perf_set_shadow_time(struct perf_event *event,
1621 struct perf_event_context *ctx,
1625 * use the correct time source for the time snapshot
1627 * We could get by without this by leveraging the
1628 * fact that to get to this function, the caller
1629 * has most likely already called update_context_time()
1630 * and update_cgrp_time_xx() and thus both timestamp
1631 * are identical (or very close). Given that tstamp is,
1632 * already adjusted for cgroup, we could say that:
1633 * tstamp - ctx->timestamp
1635 * tstamp - cgrp->timestamp.
1637 * Then, in perf_output_read(), the calculation would
1638 * work with no changes because:
1639 * - event is guaranteed scheduled in
1640 * - no scheduled out in between
1641 * - thus the timestamp would be the same
1643 * But this is a bit hairy.
1645 * So instead, we have an explicit cgroup call to remain
1646 * within the time time source all along. We believe it
1647 * is cleaner and simpler to understand.
1649 if (is_cgroup_event(event))
1650 perf_cgroup_set_shadow_time(event, tstamp);
1652 event->shadow_ctx_time = tstamp - ctx->timestamp;
1655 #define MAX_INTERRUPTS (~0ULL)
1657 static void perf_log_throttle(struct perf_event *event, int enable);
1660 event_sched_in(struct perf_event *event,
1661 struct perf_cpu_context *cpuctx,
1662 struct perf_event_context *ctx)
1664 u64 tstamp = perf_event_time(event);
1667 if (event->state <= PERF_EVENT_STATE_OFF)
1670 event->state = PERF_EVENT_STATE_ACTIVE;
1671 event->oncpu = smp_processor_id();
1674 * Unthrottle events, since we scheduled we might have missed several
1675 * ticks already, also for a heavily scheduling task there is little
1676 * guarantee it'll get a tick in a timely manner.
1678 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1679 perf_log_throttle(event, 1);
1680 event->hw.interrupts = 0;
1684 * The new state must be visible before we turn it on in the hardware:
1688 perf_pmu_disable(event->pmu);
1690 if (event->pmu->add(event, PERF_EF_START)) {
1691 event->state = PERF_EVENT_STATE_INACTIVE;
1697 event->tstamp_running += tstamp - event->tstamp_stopped;
1699 perf_set_shadow_time(event, ctx, tstamp);
1701 if (!is_software_event(event))
1702 cpuctx->active_oncpu++;
1704 if (event->attr.freq && event->attr.sample_freq)
1707 if (event->attr.exclusive)
1708 cpuctx->exclusive = 1;
1711 perf_pmu_enable(event->pmu);
1717 group_sched_in(struct perf_event *group_event,
1718 struct perf_cpu_context *cpuctx,
1719 struct perf_event_context *ctx)
1721 struct perf_event *event, *partial_group = NULL;
1722 struct pmu *pmu = ctx->pmu;
1723 u64 now = ctx->time;
1724 bool simulate = false;
1726 if (group_event->state == PERF_EVENT_STATE_OFF)
1729 pmu->start_txn(pmu);
1731 if (event_sched_in(group_event, cpuctx, ctx)) {
1732 pmu->cancel_txn(pmu);
1733 perf_cpu_hrtimer_restart(cpuctx);
1738 * Schedule in siblings as one group (if any):
1740 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1741 if (event_sched_in(event, cpuctx, ctx)) {
1742 partial_group = event;
1747 if (!pmu->commit_txn(pmu))
1752 * Groups can be scheduled in as one unit only, so undo any
1753 * partial group before returning:
1754 * The events up to the failed event are scheduled out normally,
1755 * tstamp_stopped will be updated.
1757 * The failed events and the remaining siblings need to have
1758 * their timings updated as if they had gone thru event_sched_in()
1759 * and event_sched_out(). This is required to get consistent timings
1760 * across the group. This also takes care of the case where the group
1761 * could never be scheduled by ensuring tstamp_stopped is set to mark
1762 * the time the event was actually stopped, such that time delta
1763 * calculation in update_event_times() is correct.
1765 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1766 if (event == partial_group)
1770 event->tstamp_running += now - event->tstamp_stopped;
1771 event->tstamp_stopped = now;
1773 event_sched_out(event, cpuctx, ctx);
1776 event_sched_out(group_event, cpuctx, ctx);
1778 pmu->cancel_txn(pmu);
1780 perf_cpu_hrtimer_restart(cpuctx);
1786 * Work out whether we can put this event group on the CPU now.
1788 static int group_can_go_on(struct perf_event *event,
1789 struct perf_cpu_context *cpuctx,
1793 * Groups consisting entirely of software events can always go on.
1795 if (event->group_flags & PERF_GROUP_SOFTWARE)
1798 * If an exclusive group is already on, no other hardware
1801 if (cpuctx->exclusive)
1804 * If this group is exclusive and there are already
1805 * events on the CPU, it can't go on.
1807 if (event->attr.exclusive && cpuctx->active_oncpu)
1810 * Otherwise, try to add it if all previous groups were able
1816 static void add_event_to_ctx(struct perf_event *event,
1817 struct perf_event_context *ctx)
1819 u64 tstamp = perf_event_time(event);
1821 list_add_event(event, ctx);
1822 perf_group_attach(event);
1823 event->tstamp_enabled = tstamp;
1824 event->tstamp_running = tstamp;
1825 event->tstamp_stopped = tstamp;
1828 static void task_ctx_sched_out(struct perf_event_context *ctx);
1830 ctx_sched_in(struct perf_event_context *ctx,
1831 struct perf_cpu_context *cpuctx,
1832 enum event_type_t event_type,
1833 struct task_struct *task);
1835 static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1836 struct perf_event_context *ctx,
1837 struct task_struct *task)
1839 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1841 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1842 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1844 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1848 * Cross CPU call to install and enable a performance event
1850 * Must be called with ctx->mutex held
1852 static int __perf_install_in_context(void *info)
1854 struct perf_event *event = info;
1855 struct perf_event_context *ctx = event->ctx;
1856 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1857 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1858 struct task_struct *task = current;
1860 perf_ctx_lock(cpuctx, task_ctx);
1861 perf_pmu_disable(cpuctx->ctx.pmu);
1864 * If there was an active task_ctx schedule it out.
1867 task_ctx_sched_out(task_ctx);
1870 * If the context we're installing events in is not the
1871 * active task_ctx, flip them.
1873 if (ctx->task && task_ctx != ctx) {
1875 raw_spin_unlock(&task_ctx->lock);
1876 raw_spin_lock(&ctx->lock);
1881 cpuctx->task_ctx = task_ctx;
1882 task = task_ctx->task;
1885 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
1887 update_context_time(ctx);
1889 * update cgrp time only if current cgrp
1890 * matches event->cgrp. Must be done before
1891 * calling add_event_to_ctx()
1893 update_cgrp_time_from_event(event);
1895 add_event_to_ctx(event, ctx);
1898 * Schedule everything back in
1900 perf_event_sched_in(cpuctx, task_ctx, task);
1902 perf_pmu_enable(cpuctx->ctx.pmu);
1903 perf_ctx_unlock(cpuctx, task_ctx);
1909 * Attach a performance event to a context
1911 * First we add the event to the list with the hardware enable bit
1912 * in event->hw_config cleared.
1914 * If the event is attached to a task which is on a CPU we use a smp
1915 * call to enable it in the task context. The task might have been
1916 * scheduled away, but we check this in the smp call again.
1919 perf_install_in_context(struct perf_event_context *ctx,
1920 struct perf_event *event,
1923 struct task_struct *task = ctx->task;
1925 lockdep_assert_held(&ctx->mutex);
1928 if (event->cpu != -1)
1933 * Per cpu events are installed via an smp call and
1934 * the install is always successful.
1936 cpu_function_call(cpu, __perf_install_in_context, event);
1941 if (!task_function_call(task, __perf_install_in_context, event))
1944 raw_spin_lock_irq(&ctx->lock);
1946 * If we failed to find a running task, but find the context active now
1947 * that we've acquired the ctx->lock, retry.
1949 if (ctx->is_active) {
1950 raw_spin_unlock_irq(&ctx->lock);
1955 * Since the task isn't running, its safe to add the event, us holding
1956 * the ctx->lock ensures the task won't get scheduled in.
1958 add_event_to_ctx(event, ctx);
1959 raw_spin_unlock_irq(&ctx->lock);
1963 * Put a event into inactive state and update time fields.
1964 * Enabling the leader of a group effectively enables all
1965 * the group members that aren't explicitly disabled, so we
1966 * have to update their ->tstamp_enabled also.
1967 * Note: this works for group members as well as group leaders
1968 * since the non-leader members' sibling_lists will be empty.
1970 static void __perf_event_mark_enabled(struct perf_event *event)
1972 struct perf_event *sub;
1973 u64 tstamp = perf_event_time(event);
1975 event->state = PERF_EVENT_STATE_INACTIVE;
1976 event->tstamp_enabled = tstamp - event->total_time_enabled;
1977 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1978 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1979 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1984 * Cross CPU call to enable a performance event
1986 static int __perf_event_enable(void *info)
1988 struct perf_event *event = info;
1989 struct perf_event_context *ctx = event->ctx;
1990 struct perf_event *leader = event->group_leader;
1991 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1995 * There's a time window between 'ctx->is_active' check
1996 * in perf_event_enable function and this place having:
1998 * - ctx->lock unlocked
2000 * where the task could be killed and 'ctx' deactivated
2001 * by perf_event_exit_task.
2003 if (!ctx->is_active)
2006 raw_spin_lock(&ctx->lock);
2007 update_context_time(ctx);
2009 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2013 * set current task's cgroup time reference point
2015 perf_cgroup_set_timestamp(current, ctx);
2017 __perf_event_mark_enabled(event);
2019 if (!event_filter_match(event)) {
2020 if (is_cgroup_event(event))
2021 perf_cgroup_defer_enabled(event);
2026 * If the event is in a group and isn't the group leader,
2027 * then don't put it on unless the group is on.
2029 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
2032 if (!group_can_go_on(event, cpuctx, 1)) {
2035 if (event == leader)
2036 err = group_sched_in(event, cpuctx, ctx);
2038 err = event_sched_in(event, cpuctx, ctx);
2043 * If this event can't go on and it's part of a
2044 * group, then the whole group has to come off.
2046 if (leader != event) {
2047 group_sched_out(leader, cpuctx, ctx);
2048 perf_cpu_hrtimer_restart(cpuctx);
2050 if (leader->attr.pinned) {
2051 update_group_times(leader);
2052 leader->state = PERF_EVENT_STATE_ERROR;
2057 raw_spin_unlock(&ctx->lock);
2065 * If event->ctx is a cloned context, callers must make sure that
2066 * every task struct that event->ctx->task could possibly point to
2067 * remains valid. This condition is satisfied when called through
2068 * perf_event_for_each_child or perf_event_for_each as described
2069 * for perf_event_disable.
2071 void perf_event_enable(struct perf_event *event)
2073 struct perf_event_context *ctx = event->ctx;
2074 struct task_struct *task = ctx->task;
2078 * Enable the event on the cpu that it's on
2080 cpu_function_call(event->cpu, __perf_event_enable, event);
2084 raw_spin_lock_irq(&ctx->lock);
2085 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2089 * If the event is in error state, clear that first.
2090 * That way, if we see the event in error state below, we
2091 * know that it has gone back into error state, as distinct
2092 * from the task having been scheduled away before the
2093 * cross-call arrived.
2095 if (event->state == PERF_EVENT_STATE_ERROR)
2096 event->state = PERF_EVENT_STATE_OFF;
2099 if (!ctx->is_active) {
2100 __perf_event_mark_enabled(event);
2104 raw_spin_unlock_irq(&ctx->lock);
2106 if (!task_function_call(task, __perf_event_enable, event))
2109 raw_spin_lock_irq(&ctx->lock);
2112 * If the context is active and the event is still off,
2113 * we need to retry the cross-call.
2115 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
2117 * task could have been flipped by a concurrent
2118 * perf_event_context_sched_out()
2125 raw_spin_unlock_irq(&ctx->lock);
2127 EXPORT_SYMBOL_GPL(perf_event_enable);
2129 int perf_event_refresh(struct perf_event *event, int refresh)
2132 * not supported on inherited events
2134 if (event->attr.inherit || !is_sampling_event(event))
2137 atomic_add(refresh, &event->event_limit);
2138 perf_event_enable(event);
2142 EXPORT_SYMBOL_GPL(perf_event_refresh);
2144 static void ctx_sched_out(struct perf_event_context *ctx,
2145 struct perf_cpu_context *cpuctx,
2146 enum event_type_t event_type)
2148 struct perf_event *event;
2149 int is_active = ctx->is_active;
2151 ctx->is_active &= ~event_type;
2152 if (likely(!ctx->nr_events))
2155 update_context_time(ctx);
2156 update_cgrp_time_from_cpuctx(cpuctx);
2157 if (!ctx->nr_active)
2160 perf_pmu_disable(ctx->pmu);
2161 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
2162 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2163 group_sched_out(event, cpuctx, ctx);
2166 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
2167 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
2168 group_sched_out(event, cpuctx, ctx);
2170 perf_pmu_enable(ctx->pmu);
2174 * Test whether two contexts are equivalent, i.e. whether they have both been
2175 * cloned from the same version of the same context.
2177 * Equivalence is measured using a generation number in the context that is
2178 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2179 * and list_del_event().
2181 static int context_equiv(struct perf_event_context *ctx1,
2182 struct perf_event_context *ctx2)
2184 /* Pinning disables the swap optimization */
2185 if (ctx1->pin_count || ctx2->pin_count)
2188 /* If ctx1 is the parent of ctx2 */
2189 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2192 /* If ctx2 is the parent of ctx1 */
2193 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2197 * If ctx1 and ctx2 have the same parent; we flatten the parent
2198 * hierarchy, see perf_event_init_context().
2200 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2201 ctx1->parent_gen == ctx2->parent_gen)
2208 static void __perf_event_sync_stat(struct perf_event *event,
2209 struct perf_event *next_event)
2213 if (!event->attr.inherit_stat)
2217 * Update the event value, we cannot use perf_event_read()
2218 * because we're in the middle of a context switch and have IRQs
2219 * disabled, which upsets smp_call_function_single(), however
2220 * we know the event must be on the current CPU, therefore we
2221 * don't need to use it.
2223 switch (event->state) {
2224 case PERF_EVENT_STATE_ACTIVE:
2225 event->pmu->read(event);
2228 case PERF_EVENT_STATE_INACTIVE:
2229 update_event_times(event);
2237 * In order to keep per-task stats reliable we need to flip the event
2238 * values when we flip the contexts.
2240 value = local64_read(&next_event->count);
2241 value = local64_xchg(&event->count, value);
2242 local64_set(&next_event->count, value);
2244 swap(event->total_time_enabled, next_event->total_time_enabled);
2245 swap(event->total_time_running, next_event->total_time_running);
2248 * Since we swizzled the values, update the user visible data too.
2250 perf_event_update_userpage(event);
2251 perf_event_update_userpage(next_event);
2254 static void perf_event_sync_stat(struct perf_event_context *ctx,
2255 struct perf_event_context *next_ctx)
2257 struct perf_event *event, *next_event;
2262 update_context_time(ctx);
2264 event = list_first_entry(&ctx->event_list,
2265 struct perf_event, event_entry);
2267 next_event = list_first_entry(&next_ctx->event_list,
2268 struct perf_event, event_entry);
2270 while (&event->event_entry != &ctx->event_list &&
2271 &next_event->event_entry != &next_ctx->event_list) {
2273 __perf_event_sync_stat(event, next_event);
2275 event = list_next_entry(event, event_entry);
2276 next_event = list_next_entry(next_event, event_entry);
2280 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2281 struct task_struct *next)
2283 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
2284 struct perf_event_context *next_ctx;
2285 struct perf_event_context *parent, *next_parent;
2286 struct perf_cpu_context *cpuctx;
2292 cpuctx = __get_cpu_context(ctx);
2293 if (!cpuctx->task_ctx)
2297 next_ctx = next->perf_event_ctxp[ctxn];
2301 parent = rcu_dereference(ctx->parent_ctx);
2302 next_parent = rcu_dereference(next_ctx->parent_ctx);
2304 /* If neither context have a parent context; they cannot be clones. */
2305 if (!parent && !next_parent)
2308 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
2310 * Looks like the two contexts are clones, so we might be
2311 * able to optimize the context switch. We lock both
2312 * contexts and check that they are clones under the
2313 * lock (including re-checking that neither has been
2314 * uncloned in the meantime). It doesn't matter which
2315 * order we take the locks because no other cpu could
2316 * be trying to lock both of these tasks.
2318 raw_spin_lock(&ctx->lock);
2319 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
2320 if (context_equiv(ctx, next_ctx)) {
2322 * XXX do we need a memory barrier of sorts
2323 * wrt to rcu_dereference() of perf_event_ctxp
2325 task->perf_event_ctxp[ctxn] = next_ctx;
2326 next->perf_event_ctxp[ctxn] = ctx;
2328 next_ctx->task = task;
2331 perf_event_sync_stat(ctx, next_ctx);
2333 raw_spin_unlock(&next_ctx->lock);
2334 raw_spin_unlock(&ctx->lock);
2340 raw_spin_lock(&ctx->lock);
2341 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2342 cpuctx->task_ctx = NULL;
2343 raw_spin_unlock(&ctx->lock);
2347 #define for_each_task_context_nr(ctxn) \
2348 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2351 * Called from scheduler to remove the events of the current task,
2352 * with interrupts disabled.
2354 * We stop each event and update the event value in event->count.
2356 * This does not protect us against NMI, but disable()
2357 * sets the disabled bit in the control field of event _before_
2358 * accessing the event control register. If a NMI hits, then it will
2359 * not restart the event.
2361 void __perf_event_task_sched_out(struct task_struct *task,
2362 struct task_struct *next)
2366 for_each_task_context_nr(ctxn)
2367 perf_event_context_sched_out(task, ctxn, next);
2370 * if cgroup events exist on this CPU, then we need
2371 * to check if we have to switch out PMU state.
2372 * cgroup event are system-wide mode only
2374 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2375 perf_cgroup_sched_out(task, next);
2378 static void task_ctx_sched_out(struct perf_event_context *ctx)
2380 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2382 if (!cpuctx->task_ctx)
2385 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2388 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2389 cpuctx->task_ctx = NULL;
2393 * Called with IRQs disabled
2395 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2396 enum event_type_t event_type)
2398 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
2402 ctx_pinned_sched_in(struct perf_event_context *ctx,
2403 struct perf_cpu_context *cpuctx)
2405 struct perf_event *event;
2407 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2408 if (event->state <= PERF_EVENT_STATE_OFF)
2410 if (!event_filter_match(event))
2413 /* may need to reset tstamp_enabled */
2414 if (is_cgroup_event(event))
2415 perf_cgroup_mark_enabled(event, ctx);
2417 if (group_can_go_on(event, cpuctx, 1))
2418 group_sched_in(event, cpuctx, ctx);
2421 * If this pinned group hasn't been scheduled,
2422 * put it in error state.
2424 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2425 update_group_times(event);
2426 event->state = PERF_EVENT_STATE_ERROR;
2432 ctx_flexible_sched_in(struct perf_event_context *ctx,
2433 struct perf_cpu_context *cpuctx)
2435 struct perf_event *event;
2438 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2439 /* Ignore events in OFF or ERROR state */
2440 if (event->state <= PERF_EVENT_STATE_OFF)
2443 * Listen to the 'cpu' scheduling filter constraint
2446 if (!event_filter_match(event))
2449 /* may need to reset tstamp_enabled */
2450 if (is_cgroup_event(event))
2451 perf_cgroup_mark_enabled(event, ctx);
2453 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2454 if (group_sched_in(event, cpuctx, ctx))
2461 ctx_sched_in(struct perf_event_context *ctx,
2462 struct perf_cpu_context *cpuctx,
2463 enum event_type_t event_type,
2464 struct task_struct *task)
2467 int is_active = ctx->is_active;
2469 ctx->is_active |= event_type;
2470 if (likely(!ctx->nr_events))
2474 ctx->timestamp = now;
2475 perf_cgroup_set_timestamp(task, ctx);
2477 * First go through the list and put on any pinned groups
2478 * in order to give them the best chance of going on.
2480 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
2481 ctx_pinned_sched_in(ctx, cpuctx);
2483 /* Then walk through the lower prio flexible groups */
2484 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
2485 ctx_flexible_sched_in(ctx, cpuctx);
2488 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2489 enum event_type_t event_type,
2490 struct task_struct *task)
2492 struct perf_event_context *ctx = &cpuctx->ctx;
2494 ctx_sched_in(ctx, cpuctx, event_type, task);
2497 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2498 struct task_struct *task)
2500 struct perf_cpu_context *cpuctx;
2502 cpuctx = __get_cpu_context(ctx);
2503 if (cpuctx->task_ctx == ctx)
2506 perf_ctx_lock(cpuctx, ctx);
2507 perf_pmu_disable(ctx->pmu);
2509 * We want to keep the following priority order:
2510 * cpu pinned (that don't need to move), task pinned,
2511 * cpu flexible, task flexible.
2513 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2516 cpuctx->task_ctx = ctx;
2518 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2520 perf_pmu_enable(ctx->pmu);
2521 perf_ctx_unlock(cpuctx, ctx);
2524 * Since these rotations are per-cpu, we need to ensure the
2525 * cpu-context we got scheduled on is actually rotating.
2527 perf_pmu_rotate_start(ctx->pmu);
2531 * When sampling the branck stack in system-wide, it may be necessary
2532 * to flush the stack on context switch. This happens when the branch
2533 * stack does not tag its entries with the pid of the current task.
2534 * Otherwise it becomes impossible to associate a branch entry with a
2535 * task. This ambiguity is more likely to appear when the branch stack
2536 * supports priv level filtering and the user sets it to monitor only
2537 * at the user level (which could be a useful measurement in system-wide
2538 * mode). In that case, the risk is high of having a branch stack with
2539 * branch from multiple tasks. Flushing may mean dropping the existing
2540 * entries or stashing them somewhere in the PMU specific code layer.
2542 * This function provides the context switch callback to the lower code
2543 * layer. It is invoked ONLY when there is at least one system-wide context
2544 * with at least one active event using taken branch sampling.
2546 static void perf_branch_stack_sched_in(struct task_struct *prev,
2547 struct task_struct *task)
2549 struct perf_cpu_context *cpuctx;
2551 unsigned long flags;
2553 /* no need to flush branch stack if not changing task */
2557 local_irq_save(flags);
2561 list_for_each_entry_rcu(pmu, &pmus, entry) {
2562 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2565 * check if the context has at least one
2566 * event using PERF_SAMPLE_BRANCH_STACK
2568 if (cpuctx->ctx.nr_branch_stack > 0
2569 && pmu->flush_branch_stack) {
2571 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2573 perf_pmu_disable(pmu);
2575 pmu->flush_branch_stack();
2577 perf_pmu_enable(pmu);
2579 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2585 local_irq_restore(flags);
2589 * Called from scheduler to add the events of the current task
2590 * with interrupts disabled.
2592 * We restore the event value and then enable it.
2594 * This does not protect us against NMI, but enable()
2595 * sets the enabled bit in the control field of event _before_
2596 * accessing the event control register. If a NMI hits, then it will
2597 * keep the event running.
2599 void __perf_event_task_sched_in(struct task_struct *prev,
2600 struct task_struct *task)
2602 struct perf_event_context *ctx;
2605 for_each_task_context_nr(ctxn) {
2606 ctx = task->perf_event_ctxp[ctxn];
2610 perf_event_context_sched_in(ctx, task);
2613 * if cgroup events exist on this CPU, then we need
2614 * to check if we have to switch in PMU state.
2615 * cgroup event are system-wide mode only
2617 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2618 perf_cgroup_sched_in(prev, task);
2620 /* check for system-wide branch_stack events */
2621 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2622 perf_branch_stack_sched_in(prev, task);
2625 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2627 u64 frequency = event->attr.sample_freq;
2628 u64 sec = NSEC_PER_SEC;
2629 u64 divisor, dividend;
2631 int count_fls, nsec_fls, frequency_fls, sec_fls;
2633 count_fls = fls64(count);
2634 nsec_fls = fls64(nsec);
2635 frequency_fls = fls64(frequency);
2639 * We got @count in @nsec, with a target of sample_freq HZ
2640 * the target period becomes:
2643 * period = -------------------
2644 * @nsec * sample_freq
2649 * Reduce accuracy by one bit such that @a and @b converge
2650 * to a similar magnitude.
2652 #define REDUCE_FLS(a, b) \
2654 if (a##_fls > b##_fls) { \
2664 * Reduce accuracy until either term fits in a u64, then proceed with
2665 * the other, so that finally we can do a u64/u64 division.
2667 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2668 REDUCE_FLS(nsec, frequency);
2669 REDUCE_FLS(sec, count);
2672 if (count_fls + sec_fls > 64) {
2673 divisor = nsec * frequency;
2675 while (count_fls + sec_fls > 64) {
2676 REDUCE_FLS(count, sec);
2680 dividend = count * sec;
2682 dividend = count * sec;
2684 while (nsec_fls + frequency_fls > 64) {
2685 REDUCE_FLS(nsec, frequency);
2689 divisor = nsec * frequency;
2695 return div64_u64(dividend, divisor);
2698 static DEFINE_PER_CPU(int, perf_throttled_count);
2699 static DEFINE_PER_CPU(u64, perf_throttled_seq);
2701 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
2703 struct hw_perf_event *hwc = &event->hw;
2704 s64 period, sample_period;
2707 period = perf_calculate_period(event, nsec, count);
2709 delta = (s64)(period - hwc->sample_period);
2710 delta = (delta + 7) / 8; /* low pass filter */
2712 sample_period = hwc->sample_period + delta;
2717 hwc->sample_period = sample_period;
2719 if (local64_read(&hwc->period_left) > 8*sample_period) {
2721 event->pmu->stop(event, PERF_EF_UPDATE);
2723 local64_set(&hwc->period_left, 0);
2726 event->pmu->start(event, PERF_EF_RELOAD);
2731 * combine freq adjustment with unthrottling to avoid two passes over the
2732 * events. At the same time, make sure, having freq events does not change
2733 * the rate of unthrottling as that would introduce bias.
2735 static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2738 struct perf_event *event;
2739 struct hw_perf_event *hwc;
2740 u64 now, period = TICK_NSEC;
2744 * only need to iterate over all events iff:
2745 * - context have events in frequency mode (needs freq adjust)
2746 * - there are events to unthrottle on this cpu
2748 if (!(ctx->nr_freq || needs_unthr))
2751 raw_spin_lock(&ctx->lock);
2752 perf_pmu_disable(ctx->pmu);
2754 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2755 if (event->state != PERF_EVENT_STATE_ACTIVE)
2758 if (!event_filter_match(event))
2761 perf_pmu_disable(event->pmu);
2765 if (hwc->interrupts == MAX_INTERRUPTS) {
2766 hwc->interrupts = 0;
2767 perf_log_throttle(event, 1);
2768 event->pmu->start(event, 0);
2771 if (!event->attr.freq || !event->attr.sample_freq)
2775 * stop the event and update event->count
2777 event->pmu->stop(event, PERF_EF_UPDATE);
2779 now = local64_read(&event->count);
2780 delta = now - hwc->freq_count_stamp;
2781 hwc->freq_count_stamp = now;
2785 * reload only if value has changed
2786 * we have stopped the event so tell that
2787 * to perf_adjust_period() to avoid stopping it
2791 perf_adjust_period(event, period, delta, false);
2793 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
2795 perf_pmu_enable(event->pmu);
2798 perf_pmu_enable(ctx->pmu);
2799 raw_spin_unlock(&ctx->lock);
2803 * Round-robin a context's events:
2805 static void rotate_ctx(struct perf_event_context *ctx)
2808 * Rotate the first entry last of non-pinned groups. Rotation might be
2809 * disabled by the inheritance code.
2811 if (!ctx->rotate_disable)
2812 list_rotate_left(&ctx->flexible_groups);
2816 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2817 * because they're strictly cpu affine and rotate_start is called with IRQs
2818 * disabled, while rotate_context is called from IRQ context.
2820 static int perf_rotate_context(struct perf_cpu_context *cpuctx)
2822 struct perf_event_context *ctx = NULL;
2823 int rotate = 0, remove = 1;
2825 if (cpuctx->ctx.nr_events) {
2827 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2831 ctx = cpuctx->task_ctx;
2832 if (ctx && ctx->nr_events) {
2834 if (ctx->nr_events != ctx->nr_active)
2841 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2842 perf_pmu_disable(cpuctx->ctx.pmu);
2844 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2846 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
2848 rotate_ctx(&cpuctx->ctx);
2852 perf_event_sched_in(cpuctx, ctx, current);
2854 perf_pmu_enable(cpuctx->ctx.pmu);
2855 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2858 list_del_init(&cpuctx->rotation_list);
2863 #ifdef CONFIG_NO_HZ_FULL
2864 bool perf_event_can_stop_tick(void)
2866 if (atomic_read(&nr_freq_events) ||
2867 __this_cpu_read(perf_throttled_count))
2874 void perf_event_task_tick(void)
2876 struct list_head *head = &__get_cpu_var(rotation_list);
2877 struct perf_cpu_context *cpuctx, *tmp;
2878 struct perf_event_context *ctx;
2881 WARN_ON(!irqs_disabled());
2883 __this_cpu_inc(perf_throttled_seq);
2884 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2886 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2888 perf_adjust_freq_unthr_context(ctx, throttled);
2890 ctx = cpuctx->task_ctx;
2892 perf_adjust_freq_unthr_context(ctx, throttled);
2896 static int event_enable_on_exec(struct perf_event *event,
2897 struct perf_event_context *ctx)
2899 if (!event->attr.enable_on_exec)
2902 event->attr.enable_on_exec = 0;
2903 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2906 __perf_event_mark_enabled(event);
2912 * Enable all of a task's events that have been marked enable-on-exec.
2913 * This expects task == current.
2915 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2917 struct perf_event *event;
2918 unsigned long flags;
2922 local_irq_save(flags);
2923 if (!ctx || !ctx->nr_events)
2927 * We must ctxsw out cgroup events to avoid conflict
2928 * when invoking perf_task_event_sched_in() later on
2929 * in this function. Otherwise we end up trying to
2930 * ctxswin cgroup events which are already scheduled
2933 perf_cgroup_sched_out(current, NULL);
2935 raw_spin_lock(&ctx->lock);
2936 task_ctx_sched_out(ctx);
2938 list_for_each_entry(event, &ctx->event_list, event_entry) {
2939 ret = event_enable_on_exec(event, ctx);
2945 * Unclone this context if we enabled any event.
2950 raw_spin_unlock(&ctx->lock);
2953 * Also calls ctxswin for cgroup events, if any:
2955 perf_event_context_sched_in(ctx, ctx->task);
2957 local_irq_restore(flags);
2961 * Cross CPU call to read the hardware event
2963 static void __perf_event_read(void *info)
2965 struct perf_event *event = info;
2966 struct perf_event_context *ctx = event->ctx;
2967 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2970 * If this is a task context, we need to check whether it is
2971 * the current task context of this cpu. If not it has been
2972 * scheduled out before the smp call arrived. In that case
2973 * event->count would have been updated to a recent sample
2974 * when the event was scheduled out.
2976 if (ctx->task && cpuctx->task_ctx != ctx)
2979 raw_spin_lock(&ctx->lock);
2980 if (ctx->is_active) {
2981 update_context_time(ctx);
2982 update_cgrp_time_from_event(event);
2984 update_event_times(event);
2985 if (event->state == PERF_EVENT_STATE_ACTIVE)
2986 event->pmu->read(event);
2987 raw_spin_unlock(&ctx->lock);
2990 static inline u64 perf_event_count(struct perf_event *event)
2992 return local64_read(&event->count) + atomic64_read(&event->child_count);
2995 static u64 perf_event_read(struct perf_event *event)
2998 * If event is enabled and currently active on a CPU, update the
2999 * value in the event structure:
3001 if (event->state == PERF_EVENT_STATE_ACTIVE) {
3002 smp_call_function_single(event->oncpu,
3003 __perf_event_read, event, 1);
3004 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
3005 struct perf_event_context *ctx = event->ctx;
3006 unsigned long flags;
3008 raw_spin_lock_irqsave(&ctx->lock, flags);
3010 * may read while context is not active
3011 * (e.g., thread is blocked), in that case
3012 * we cannot update context time
3014 if (ctx->is_active) {
3015 update_context_time(ctx);
3016 update_cgrp_time_from_event(event);
3018 update_event_times(event);
3019 raw_spin_unlock_irqrestore(&ctx->lock, flags);
3022 return perf_event_count(event);
3026 * Initialize the perf_event context in a task_struct:
3028 static void __perf_event_init_context(struct perf_event_context *ctx)
3030 raw_spin_lock_init(&ctx->lock);
3031 mutex_init(&ctx->mutex);
3032 INIT_LIST_HEAD(&ctx->pinned_groups);
3033 INIT_LIST_HEAD(&ctx->flexible_groups);
3034 INIT_LIST_HEAD(&ctx->event_list);
3035 atomic_set(&ctx->refcount, 1);
3038 static struct perf_event_context *
3039 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
3041 struct perf_event_context *ctx;
3043 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3047 __perf_event_init_context(ctx);
3050 get_task_struct(task);
3057 static struct task_struct *
3058 find_lively_task_by_vpid(pid_t vpid)
3060 struct task_struct *task;
3067 task = find_task_by_vpid(vpid);
3069 get_task_struct(task);
3073 return ERR_PTR(-ESRCH);
3075 /* Reuse ptrace permission checks for now. */
3077 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3082 put_task_struct(task);
3083 return ERR_PTR(err);
3088 * Returns a matching context with refcount and pincount.
3090 static struct perf_event_context *
3091 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
3093 struct perf_event_context *ctx;
3094 struct perf_cpu_context *cpuctx;
3095 unsigned long flags;
3099 /* Must be root to operate on a CPU event: */
3100 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3101 return ERR_PTR(-EACCES);
3104 * We could be clever and allow to attach a event to an
3105 * offline CPU and activate it when the CPU comes up, but
3108 if (!cpu_online(cpu))
3109 return ERR_PTR(-ENODEV);
3111 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
3120 ctxn = pmu->task_ctx_nr;
3125 ctx = perf_lock_task_context(task, ctxn, &flags);
3129 raw_spin_unlock_irqrestore(&ctx->lock, flags);
3131 ctx = alloc_perf_context(pmu, task);
3137 mutex_lock(&task->perf_event_mutex);
3139 * If it has already passed perf_event_exit_task().
3140 * we must see PF_EXITING, it takes this mutex too.
3142 if (task->flags & PF_EXITING)
3144 else if (task->perf_event_ctxp[ctxn])
3149 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
3151 mutex_unlock(&task->perf_event_mutex);
3153 if (unlikely(err)) {
3165 return ERR_PTR(err);
3168 static void perf_event_free_filter(struct perf_event *event);
3170 static void free_event_rcu(struct rcu_head *head)
3172 struct perf_event *event;
3174 event = container_of(head, struct perf_event, rcu_head);
3176 put_pid_ns(event->ns);
3177 perf_event_free_filter(event);
3181 static void ring_buffer_put(struct ring_buffer *rb);
3182 static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb);
3184 static void unaccount_event_cpu(struct perf_event *event, int cpu)
3189 if (has_branch_stack(event)) {
3190 if (!(event->attach_state & PERF_ATTACH_TASK))
3191 atomic_dec(&per_cpu(perf_branch_stack_events, cpu));
3193 if (is_cgroup_event(event))
3194 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3197 static void unaccount_event(struct perf_event *event)
3202 if (event->attach_state & PERF_ATTACH_TASK)
3203 static_key_slow_dec_deferred(&perf_sched_events);
3204 if (event->attr.mmap || event->attr.mmap_data)
3205 atomic_dec(&nr_mmap_events);
3206 if (event->attr.comm)
3207 atomic_dec(&nr_comm_events);
3208 if (event->attr.task)
3209 atomic_dec(&nr_task_events);
3210 if (event->attr.freq)
3211 atomic_dec(&nr_freq_events);
3212 if (is_cgroup_event(event))
3213 static_key_slow_dec_deferred(&perf_sched_events);
3214 if (has_branch_stack(event))
3215 static_key_slow_dec_deferred(&perf_sched_events);
3217 unaccount_event_cpu(event, event->cpu);
3220 static void __free_event(struct perf_event *event)
3222 if (!event->parent) {
3223 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3224 put_callchain_buffers();
3228 event->destroy(event);
3231 put_ctx(event->ctx);
3234 module_put(event->pmu->module);
3236 call_rcu(&event->rcu_head, free_event_rcu);
3238 static void free_event(struct perf_event *event)
3240 irq_work_sync(&event->pending);
3242 unaccount_event(event);
3245 struct ring_buffer *rb;
3248 * Can happen when we close an event with re-directed output.
3250 * Since we have a 0 refcount, perf_mmap_close() will skip
3251 * over us; possibly making our ring_buffer_put() the last.
3253 mutex_lock(&event->mmap_mutex);
3256 rcu_assign_pointer(event->rb, NULL);
3257 ring_buffer_detach(event, rb);
3258 ring_buffer_put(rb); /* could be last */
3260 mutex_unlock(&event->mmap_mutex);
3263 if (is_cgroup_event(event))
3264 perf_detach_cgroup(event);
3267 __free_event(event);
3270 int perf_event_release_kernel(struct perf_event *event)
3272 struct perf_event_context *ctx = event->ctx;
3274 WARN_ON_ONCE(ctx->parent_ctx);
3276 * There are two ways this annotation is useful:
3278 * 1) there is a lock recursion from perf_event_exit_task
3279 * see the comment there.
3281 * 2) there is a lock-inversion with mmap_sem through
3282 * perf_event_read_group(), which takes faults while
3283 * holding ctx->mutex, however this is called after
3284 * the last filedesc died, so there is no possibility
3285 * to trigger the AB-BA case.
3287 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
3288 raw_spin_lock_irq(&ctx->lock);
3289 perf_group_detach(event);
3290 raw_spin_unlock_irq(&ctx->lock);
3291 perf_remove_from_context(event);
3292 mutex_unlock(&ctx->mutex);
3298 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3301 * Called when the last reference to the file is gone.
3303 static void put_event(struct perf_event *event)
3305 struct task_struct *owner;
3307 if (!atomic_long_dec_and_test(&event->refcount))
3311 owner = ACCESS_ONCE(event->owner);
3313 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3314 * !owner it means the list deletion is complete and we can indeed
3315 * free this event, otherwise we need to serialize on
3316 * owner->perf_event_mutex.
3318 smp_read_barrier_depends();
3321 * Since delayed_put_task_struct() also drops the last
3322 * task reference we can safely take a new reference
3323 * while holding the rcu_read_lock().
3325 get_task_struct(owner);
3330 mutex_lock(&owner->perf_event_mutex);
3332 * We have to re-check the event->owner field, if it is cleared
3333 * we raced with perf_event_exit_task(), acquiring the mutex
3334 * ensured they're done, and we can proceed with freeing the
3338 list_del_init(&event->owner_entry);
3339 mutex_unlock(&owner->perf_event_mutex);
3340 put_task_struct(owner);
3343 perf_event_release_kernel(event);
3346 static int perf_release(struct inode *inode, struct file *file)
3348 put_event(file->private_data);
3352 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
3354 struct perf_event *child;
3360 mutex_lock(&event->child_mutex);
3361 total += perf_event_read(event);
3362 *enabled += event->total_time_enabled +
3363 atomic64_read(&event->child_total_time_enabled);
3364 *running += event->total_time_running +
3365 atomic64_read(&event->child_total_time_running);
3367 list_for_each_entry(child, &event->child_list, child_list) {
3368 total += perf_event_read(child);
3369 *enabled += child->total_time_enabled;
3370 *running += child->total_time_running;
3372 mutex_unlock(&event->child_mutex);
3376 EXPORT_SYMBOL_GPL(perf_event_read_value);
3378 static int perf_event_read_group(struct perf_event *event,
3379 u64 read_format, char __user *buf)
3381 struct perf_event *leader = event->group_leader, *sub;
3382 int n = 0, size = 0, ret = -EFAULT;
3383 struct perf_event_context *ctx = leader->ctx;
3385 u64 count, enabled, running;
3387 mutex_lock(&ctx->mutex);
3388 count = perf_event_read_value(leader, &enabled, &running);
3390 values[n++] = 1 + leader->nr_siblings;
3391 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3392 values[n++] = enabled;
3393 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3394 values[n++] = running;
3395 values[n++] = count;
3396 if (read_format & PERF_FORMAT_ID)
3397 values[n++] = primary_event_id(leader);
3399 size = n * sizeof(u64);
3401 if (copy_to_user(buf, values, size))
3406 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3409 values[n++] = perf_event_read_value(sub, &enabled, &running);
3410 if (read_format & PERF_FORMAT_ID)
3411 values[n++] = primary_event_id(sub);
3413 size = n * sizeof(u64);
3415 if (copy_to_user(buf + ret, values, size)) {
3423 mutex_unlock(&ctx->mutex);
3428 static int perf_event_read_one(struct perf_event *event,
3429 u64 read_format, char __user *buf)
3431 u64 enabled, running;
3435 values[n++] = perf_event_read_value(event, &enabled, &running);
3436 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3437 values[n++] = enabled;
3438 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3439 values[n++] = running;
3440 if (read_format & PERF_FORMAT_ID)
3441 values[n++] = primary_event_id(event);
3443 if (copy_to_user(buf, values, n * sizeof(u64)))
3446 return n * sizeof(u64);
3450 * Read the performance event - simple non blocking version for now
3453 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3455 u64 read_format = event->attr.read_format;
3459 * Return end-of-file for a read on a event that is in
3460 * error state (i.e. because it was pinned but it couldn't be
3461 * scheduled on to the CPU at some point).
3463 if (event->state == PERF_EVENT_STATE_ERROR)
3466 if (count < event->read_size)
3469 WARN_ON_ONCE(event->ctx->parent_ctx);
3470 if (read_format & PERF_FORMAT_GROUP)
3471 ret = perf_event_read_group(event, read_format, buf);
3473 ret = perf_event_read_one(event, read_format, buf);
3479 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3481 struct perf_event *event = file->private_data;
3483 return perf_read_hw(event, buf, count);
3486 static unsigned int perf_poll(struct file *file, poll_table *wait)
3488 struct perf_event *event = file->private_data;
3489 struct ring_buffer *rb;
3490 unsigned int events = POLL_HUP;
3493 * Pin the event->rb by taking event->mmap_mutex; otherwise
3494 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
3496 mutex_lock(&event->mmap_mutex);
3499 events = atomic_xchg(&rb->poll, 0);
3500 mutex_unlock(&event->mmap_mutex);
3502 poll_wait(file, &event->waitq, wait);
3507 static void perf_event_reset(struct perf_event *event)
3509 (void)perf_event_read(event);
3510 local64_set(&event->count, 0);
3511 perf_event_update_userpage(event);
3515 * Holding the top-level event's child_mutex means that any
3516 * descendant process that has inherited this event will block
3517 * in sync_child_event if it goes to exit, thus satisfying the
3518 * task existence requirements of perf_event_enable/disable.
3520 static void perf_event_for_each_child(struct perf_event *event,
3521 void (*func)(struct perf_event *))
3523 struct perf_event *child;
3525 WARN_ON_ONCE(event->ctx->parent_ctx);
3526 mutex_lock(&event->child_mutex);
3528 list_for_each_entry(child, &event->child_list, child_list)
3530 mutex_unlock(&event->child_mutex);
3533 static void perf_event_for_each(struct perf_event *event,
3534 void (*func)(struct perf_event *))
3536 struct perf_event_context *ctx = event->ctx;
3537 struct perf_event *sibling;
3539 WARN_ON_ONCE(ctx->parent_ctx);
3540 mutex_lock(&ctx->mutex);
3541 event = event->group_leader;
3543 perf_event_for_each_child(event, func);
3544 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3545 perf_event_for_each_child(sibling, func);
3546 mutex_unlock(&ctx->mutex);
3549 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3551 struct perf_event_context *ctx = event->ctx;
3552 int ret = 0, active;
3555 if (!is_sampling_event(event))
3558 if (copy_from_user(&value, arg, sizeof(value)))
3564 raw_spin_lock_irq(&ctx->lock);
3565 if (event->attr.freq) {
3566 if (value > sysctl_perf_event_sample_rate) {
3571 event->attr.sample_freq = value;
3573 event->attr.sample_period = value;
3574 event->hw.sample_period = value;
3577 active = (event->state == PERF_EVENT_STATE_ACTIVE);
3579 perf_pmu_disable(ctx->pmu);
3580 event->pmu->stop(event, PERF_EF_UPDATE);
3583 local64_set(&event->hw.period_left, 0);
3586 event->pmu->start(event, PERF_EF_RELOAD);
3587 perf_pmu_enable(ctx->pmu);
3591 raw_spin_unlock_irq(&ctx->lock);
3596 static const struct file_operations perf_fops;
3598 static inline int perf_fget_light(int fd, struct fd *p)
3600 struct fd f = fdget(fd);
3604 if (f.file->f_op != &perf_fops) {
3612 static int perf_event_set_output(struct perf_event *event,
3613 struct perf_event *output_event);
3614 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3616 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3618 struct perf_event *event = file->private_data;
3619 void (*func)(struct perf_event *);
3623 case PERF_EVENT_IOC_ENABLE:
3624 func = perf_event_enable;
3626 case PERF_EVENT_IOC_DISABLE:
3627 func = perf_event_disable;
3629 case PERF_EVENT_IOC_RESET:
3630 func = perf_event_reset;
3633 case PERF_EVENT_IOC_REFRESH:
3634 return perf_event_refresh(event, arg);
3636 case PERF_EVENT_IOC_PERIOD:
3637 return perf_event_period(event, (u64 __user *)arg);
3639 case PERF_EVENT_IOC_ID:
3641 u64 id = primary_event_id(event);
3643 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
3648 case PERF_EVENT_IOC_SET_OUTPUT:
3652 struct perf_event *output_event;
3654 ret = perf_fget_light(arg, &output);
3657 output_event = output.file->private_data;
3658 ret = perf_event_set_output(event, output_event);
3661 ret = perf_event_set_output(event, NULL);
3666 case PERF_EVENT_IOC_SET_FILTER:
3667 return perf_event_set_filter(event, (void __user *)arg);
3673 if (flags & PERF_IOC_FLAG_GROUP)
3674 perf_event_for_each(event, func);
3676 perf_event_for_each_child(event, func);
3681 int perf_event_task_enable(void)
3683 struct perf_event *event;
3685 mutex_lock(¤t->perf_event_mutex);
3686 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3687 perf_event_for_each_child(event, perf_event_enable);
3688 mutex_unlock(¤t->perf_event_mutex);
3693 int perf_event_task_disable(void)
3695 struct perf_event *event;
3697 mutex_lock(¤t->perf_event_mutex);
3698 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3699 perf_event_for_each_child(event, perf_event_disable);
3700 mutex_unlock(¤t->perf_event_mutex);
3705 static int perf_event_index(struct perf_event *event)
3707 if (event->hw.state & PERF_HES_STOPPED)
3710 if (event->state != PERF_EVENT_STATE_ACTIVE)
3713 return event->pmu->event_idx(event);
3716 static void calc_timer_values(struct perf_event *event,
3723 *now = perf_clock();
3724 ctx_time = event->shadow_ctx_time + *now;
3725 *enabled = ctx_time - event->tstamp_enabled;
3726 *running = ctx_time - event->tstamp_running;
3729 static void perf_event_init_userpage(struct perf_event *event)
3731 struct perf_event_mmap_page *userpg;
3732 struct ring_buffer *rb;
3735 rb = rcu_dereference(event->rb);
3739 userpg = rb->user_page;
3741 /* Allow new userspace to detect that bit 0 is deprecated */
3742 userpg->cap_bit0_is_deprecated = 1;
3743 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
3749 void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
3754 * Callers need to ensure there can be no nesting of this function, otherwise
3755 * the seqlock logic goes bad. We can not serialize this because the arch
3756 * code calls this from NMI context.
3758 void perf_event_update_userpage(struct perf_event *event)
3760 struct perf_event_mmap_page *userpg;
3761 struct ring_buffer *rb;
3762 u64 enabled, running, now;
3765 rb = rcu_dereference(event->rb);
3770 * compute total_time_enabled, total_time_running
3771 * based on snapshot values taken when the event
3772 * was last scheduled in.
3774 * we cannot simply called update_context_time()
3775 * because of locking issue as we can be called in
3778 calc_timer_values(event, &now, &enabled, &running);
3780 userpg = rb->user_page;
3782 * Disable preemption so as to not let the corresponding user-space
3783 * spin too long if we get preempted.
3788 userpg->index = perf_event_index(event);
3789 userpg->offset = perf_event_count(event);
3791 userpg->offset -= local64_read(&event->hw.prev_count);
3793 userpg->time_enabled = enabled +
3794 atomic64_read(&event->child_total_time_enabled);
3796 userpg->time_running = running +
3797 atomic64_read(&event->child_total_time_running);
3799 arch_perf_update_userpage(userpg, now);
3808 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3810 struct perf_event *event = vma->vm_file->private_data;
3811 struct ring_buffer *rb;
3812 int ret = VM_FAULT_SIGBUS;
3814 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3815 if (vmf->pgoff == 0)
3821 rb = rcu_dereference(event->rb);
3825 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3828 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
3832 get_page(vmf->page);
3833 vmf->page->mapping = vma->vm_file->f_mapping;
3834 vmf->page->index = vmf->pgoff;
3843 static void ring_buffer_attach(struct perf_event *event,
3844 struct ring_buffer *rb)
3846 unsigned long flags;
3848 if (!list_empty(&event->rb_entry))
3851 spin_lock_irqsave(&rb->event_lock, flags);
3852 if (list_empty(&event->rb_entry))
3853 list_add(&event->rb_entry, &rb->event_list);
3854 spin_unlock_irqrestore(&rb->event_lock, flags);
3857 static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb)
3859 unsigned long flags;
3861 if (list_empty(&event->rb_entry))
3864 spin_lock_irqsave(&rb->event_lock, flags);
3865 list_del_init(&event->rb_entry);
3866 wake_up_all(&event->waitq);
3867 spin_unlock_irqrestore(&rb->event_lock, flags);
3870 static void ring_buffer_wakeup(struct perf_event *event)
3872 struct ring_buffer *rb;
3875 rb = rcu_dereference(event->rb);
3877 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3878 wake_up_all(&event->waitq);
3883 static void rb_free_rcu(struct rcu_head *rcu_head)
3885 struct ring_buffer *rb;
3887 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3891 static struct ring_buffer *ring_buffer_get(struct perf_event *event)
3893 struct ring_buffer *rb;
3896 rb = rcu_dereference(event->rb);
3898 if (!atomic_inc_not_zero(&rb->refcount))
3906 static void ring_buffer_put(struct ring_buffer *rb)
3908 if (!atomic_dec_and_test(&rb->refcount))
3911 WARN_ON_ONCE(!list_empty(&rb->event_list));
3913 call_rcu(&rb->rcu_head, rb_free_rcu);
3916 static void perf_mmap_open(struct vm_area_struct *vma)
3918 struct perf_event *event = vma->vm_file->private_data;
3920 atomic_inc(&event->mmap_count);
3921 atomic_inc(&event->rb->mmap_count);
3925 * A buffer can be mmap()ed multiple times; either directly through the same
3926 * event, or through other events by use of perf_event_set_output().
3928 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3929 * the buffer here, where we still have a VM context. This means we need
3930 * to detach all events redirecting to us.
3932 static void perf_mmap_close(struct vm_area_struct *vma)
3934 struct perf_event *event = vma->vm_file->private_data;
3936 struct ring_buffer *rb = event->rb;
3937 struct user_struct *mmap_user = rb->mmap_user;
3938 int mmap_locked = rb->mmap_locked;
3939 unsigned long size = perf_data_size(rb);
3941 atomic_dec(&rb->mmap_count);
3943 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
3946 /* Detach current event from the buffer. */
3947 rcu_assign_pointer(event->rb, NULL);
3948 ring_buffer_detach(event, rb);
3949 mutex_unlock(&event->mmap_mutex);
3951 /* If there's still other mmap()s of this buffer, we're done. */
3952 if (atomic_read(&rb->mmap_count)) {
3953 ring_buffer_put(rb); /* can't be last */
3958 * No other mmap()s, detach from all other events that might redirect
3959 * into the now unreachable buffer. Somewhat complicated by the
3960 * fact that rb::event_lock otherwise nests inside mmap_mutex.
3964 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
3965 if (!atomic_long_inc_not_zero(&event->refcount)) {
3967 * This event is en-route to free_event() which will
3968 * detach it and remove it from the list.
3974 mutex_lock(&event->mmap_mutex);
3976 * Check we didn't race with perf_event_set_output() which can
3977 * swizzle the rb from under us while we were waiting to
3978 * acquire mmap_mutex.
3980 * If we find a different rb; ignore this event, a next
3981 * iteration will no longer find it on the list. We have to
3982 * still restart the iteration to make sure we're not now
3983 * iterating the wrong list.
3985 if (event->rb == rb) {
3986 rcu_assign_pointer(event->rb, NULL);
3987 ring_buffer_detach(event, rb);
3988 ring_buffer_put(rb); /* can't be last, we still have one */
3990 mutex_unlock(&event->mmap_mutex);
3994 * Restart the iteration; either we're on the wrong list or
3995 * destroyed its integrity by doing a deletion.
4002 * It could be there's still a few 0-ref events on the list; they'll
4003 * get cleaned up by free_event() -- they'll also still have their
4004 * ref on the rb and will free it whenever they are done with it.
4006 * Aside from that, this buffer is 'fully' detached and unmapped,
4007 * undo the VM accounting.
4010 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4011 vma->vm_mm->pinned_vm -= mmap_locked;
4012 free_uid(mmap_user);
4014 ring_buffer_put(rb); /* could be last */
4017 static const struct vm_operations_struct perf_mmap_vmops = {
4018 .open = perf_mmap_open,
4019 .close = perf_mmap_close,
4020 .fault = perf_mmap_fault,
4021 .page_mkwrite = perf_mmap_fault,
4024 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4026 struct perf_event *event = file->private_data;
4027 unsigned long user_locked, user_lock_limit;
4028 struct user_struct *user = current_user();
4029 unsigned long locked, lock_limit;
4030 struct ring_buffer *rb;
4031 unsigned long vma_size;
4032 unsigned long nr_pages;
4033 long user_extra, extra;
4034 int ret = 0, flags = 0;
4037 * Don't allow mmap() of inherited per-task counters. This would
4038 * create a performance issue due to all children writing to the
4041 if (event->cpu == -1 && event->attr.inherit)
4044 if (!(vma->vm_flags & VM_SHARED))
4047 vma_size = vma->vm_end - vma->vm_start;
4048 nr_pages = (vma_size / PAGE_SIZE) - 1;
4051 * If we have rb pages ensure they're a power-of-two number, so we
4052 * can do bitmasks instead of modulo.
4054 if (nr_pages != 0 && !is_power_of_2(nr_pages))
4057 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4060 if (vma->vm_pgoff != 0)
4063 WARN_ON_ONCE(event->ctx->parent_ctx);
4065 mutex_lock(&event->mmap_mutex);
4067 if (event->rb->nr_pages != nr_pages) {
4072 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4074 * Raced against perf_mmap_close() through
4075 * perf_event_set_output(). Try again, hope for better
4078 mutex_unlock(&event->mmap_mutex);
4085 user_extra = nr_pages + 1;
4086 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4089 * Increase the limit linearly with more CPUs:
4091 user_lock_limit *= num_online_cpus();
4093 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4096 if (user_locked > user_lock_limit)
4097 extra = user_locked - user_lock_limit;
4099 lock_limit = rlimit(RLIMIT_MEMLOCK);
4100 lock_limit >>= PAGE_SHIFT;
4101 locked = vma->vm_mm->pinned_vm + extra;
4103 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4104 !capable(CAP_IPC_LOCK)) {
4111 if (vma->vm_flags & VM_WRITE)
4112 flags |= RING_BUFFER_WRITABLE;
4114 rb = rb_alloc(nr_pages,
4115 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4123 atomic_set(&rb->mmap_count, 1);
4124 rb->mmap_locked = extra;
4125 rb->mmap_user = get_current_user();
4127 atomic_long_add(user_extra, &user->locked_vm);
4128 vma->vm_mm->pinned_vm += extra;
4130 ring_buffer_attach(event, rb);
4131 rcu_assign_pointer(event->rb, rb);
4133 perf_event_init_userpage(event);
4134 perf_event_update_userpage(event);
4138 atomic_inc(&event->mmap_count);
4139 mutex_unlock(&event->mmap_mutex);
4142 * Since pinned accounting is per vm we cannot allow fork() to copy our
4145 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
4146 vma->vm_ops = &perf_mmap_vmops;
4151 static int perf_fasync(int fd, struct file *filp, int on)
4153 struct inode *inode = file_inode(filp);
4154 struct perf_event *event = filp->private_data;
4157 mutex_lock(&inode->i_mutex);
4158 retval = fasync_helper(fd, filp, on, &event->fasync);
4159 mutex_unlock(&inode->i_mutex);
4167 static const struct file_operations perf_fops = {
4168 .llseek = no_llseek,
4169 .release = perf_release,
4172 .unlocked_ioctl = perf_ioctl,
4173 .compat_ioctl = perf_ioctl,
4175 .fasync = perf_fasync,
4181 * If there's data, ensure we set the poll() state and publish everything
4182 * to user-space before waking everybody up.
4185 void perf_event_wakeup(struct perf_event *event)
4187 ring_buffer_wakeup(event);
4189 if (event->pending_kill) {
4190 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
4191 event->pending_kill = 0;
4195 static void perf_pending_event(struct irq_work *entry)
4197 struct perf_event *event = container_of(entry,
4198 struct perf_event, pending);
4200 if (event->pending_disable) {
4201 event->pending_disable = 0;
4202 __perf_event_disable(event);
4205 if (event->pending_wakeup) {
4206 event->pending_wakeup = 0;
4207 perf_event_wakeup(event);
4212 * We assume there is only KVM supporting the callbacks.
4213 * Later on, we might change it to a list if there is
4214 * another virtualization implementation supporting the callbacks.
4216 struct perf_guest_info_callbacks *perf_guest_cbs;
4218 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4220 perf_guest_cbs = cbs;
4223 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4225 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4227 perf_guest_cbs = NULL;
4230 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4233 perf_output_sample_regs(struct perf_output_handle *handle,
4234 struct pt_regs *regs, u64 mask)
4238 for_each_set_bit(bit, (const unsigned long *) &mask,
4239 sizeof(mask) * BITS_PER_BYTE) {
4242 val = perf_reg_value(regs, bit);
4243 perf_output_put(handle, val);
4247 static void perf_sample_regs_user(struct perf_regs_user *regs_user,
4248 struct pt_regs *regs)
4250 if (!user_mode(regs)) {
4252 regs = task_pt_regs(current);
4258 regs_user->regs = regs;
4259 regs_user->abi = perf_reg_abi(current);
4264 * Get remaining task size from user stack pointer.
4266 * It'd be better to take stack vma map and limit this more
4267 * precisly, but there's no way to get it safely under interrupt,
4268 * so using TASK_SIZE as limit.
4270 static u64 perf_ustack_task_size(struct pt_regs *regs)
4272 unsigned long addr = perf_user_stack_pointer(regs);
4274 if (!addr || addr >= TASK_SIZE)
4277 return TASK_SIZE - addr;
4281 perf_sample_ustack_size(u16 stack_size, u16 header_size,
4282 struct pt_regs *regs)
4286 /* No regs, no stack pointer, no dump. */
4291 * Check if we fit in with the requested stack size into the:
4293 * If we don't, we limit the size to the TASK_SIZE.
4295 * - remaining sample size
4296 * If we don't, we customize the stack size to
4297 * fit in to the remaining sample size.
4300 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
4301 stack_size = min(stack_size, (u16) task_size);
4303 /* Current header size plus static size and dynamic size. */
4304 header_size += 2 * sizeof(u64);
4306 /* Do we fit in with the current stack dump size? */
4307 if ((u16) (header_size + stack_size) < header_size) {
4309 * If we overflow the maximum size for the sample,
4310 * we customize the stack dump size to fit in.
4312 stack_size = USHRT_MAX - header_size - sizeof(u64);
4313 stack_size = round_up(stack_size, sizeof(u64));
4320 perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4321 struct pt_regs *regs)
4323 /* Case of a kernel thread, nothing to dump */
4326 perf_output_put(handle, size);
4335 * - the size requested by user or the best one we can fit
4336 * in to the sample max size
4338 * - user stack dump data
4340 * - the actual dumped size
4344 perf_output_put(handle, dump_size);
4347 sp = perf_user_stack_pointer(regs);
4348 rem = __output_copy_user(handle, (void *) sp, dump_size);
4349 dyn_size = dump_size - rem;
4351 perf_output_skip(handle, rem);
4354 perf_output_put(handle, dyn_size);
4358 static void __perf_event_header__init_id(struct perf_event_header *header,
4359 struct perf_sample_data *data,
4360 struct perf_event *event)
4362 u64 sample_type = event->attr.sample_type;
4364 data->type = sample_type;
4365 header->size += event->id_header_size;
4367 if (sample_type & PERF_SAMPLE_TID) {
4368 /* namespace issues */
4369 data->tid_entry.pid = perf_event_pid(event, current);
4370 data->tid_entry.tid = perf_event_tid(event, current);
4373 if (sample_type & PERF_SAMPLE_TIME)
4374 data->time = perf_clock();
4376 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
4377 data->id = primary_event_id(event);
4379 if (sample_type & PERF_SAMPLE_STREAM_ID)
4380 data->stream_id = event->id;
4382 if (sample_type & PERF_SAMPLE_CPU) {
4383 data->cpu_entry.cpu = raw_smp_processor_id();
4384 data->cpu_entry.reserved = 0;
4388 void perf_event_header__init_id(struct perf_event_header *header,
4389 struct perf_sample_data *data,
4390 struct perf_event *event)
4392 if (event->attr.sample_id_all)
4393 __perf_event_header__init_id(header, data, event);
4396 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4397 struct perf_sample_data *data)
4399 u64 sample_type = data->type;
4401 if (sample_type & PERF_SAMPLE_TID)
4402 perf_output_put(handle, data->tid_entry);
4404 if (sample_type & PERF_SAMPLE_TIME)
4405 perf_output_put(handle, data->time);
4407 if (sample_type & PERF_SAMPLE_ID)
4408 perf_output_put(handle, data->id);
4410 if (sample_type & PERF_SAMPLE_STREAM_ID)
4411 perf_output_put(handle, data->stream_id);
4413 if (sample_type & PERF_SAMPLE_CPU)
4414 perf_output_put(handle, data->cpu_entry);
4416 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4417 perf_output_put(handle, data->id);
4420 void perf_event__output_id_sample(struct perf_event *event,
4421 struct perf_output_handle *handle,
4422 struct perf_sample_data *sample)
4424 if (event->attr.sample_id_all)
4425 __perf_event__output_id_sample(handle, sample);
4428 static void perf_output_read_one(struct perf_output_handle *handle,
4429 struct perf_event *event,
4430 u64 enabled, u64 running)
4432 u64 read_format = event->attr.read_format;
4436 values[n++] = perf_event_count(event);
4437 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4438 values[n++] = enabled +
4439 atomic64_read(&event->child_total_time_enabled);
4441 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4442 values[n++] = running +
4443 atomic64_read(&event->child_total_time_running);
4445 if (read_format & PERF_FORMAT_ID)
4446 values[n++] = primary_event_id(event);
4448 __output_copy(handle, values, n * sizeof(u64));
4452 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4454 static void perf_output_read_group(struct perf_output_handle *handle,
4455 struct perf_event *event,
4456 u64 enabled, u64 running)
4458 struct perf_event *leader = event->group_leader, *sub;
4459 u64 read_format = event->attr.read_format;
4463 values[n++] = 1 + leader->nr_siblings;
4465 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4466 values[n++] = enabled;
4468 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4469 values[n++] = running;
4471 if (leader != event)
4472 leader->pmu->read(leader);
4474 values[n++] = perf_event_count(leader);
4475 if (read_format & PERF_FORMAT_ID)
4476 values[n++] = primary_event_id(leader);
4478 __output_copy(handle, values, n * sizeof(u64));
4480 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4483 if ((sub != event) &&
4484 (sub->state == PERF_EVENT_STATE_ACTIVE))
4485 sub->pmu->read(sub);
4487 values[n++] = perf_event_count(sub);
4488 if (read_format & PERF_FORMAT_ID)
4489 values[n++] = primary_event_id(sub);
4491 __output_copy(handle, values, n * sizeof(u64));
4495 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4496 PERF_FORMAT_TOTAL_TIME_RUNNING)
4498 static void perf_output_read(struct perf_output_handle *handle,
4499 struct perf_event *event)
4501 u64 enabled = 0, running = 0, now;
4502 u64 read_format = event->attr.read_format;
4505 * compute total_time_enabled, total_time_running
4506 * based on snapshot values taken when the event
4507 * was last scheduled in.
4509 * we cannot simply called update_context_time()
4510 * because of locking issue as we are called in
4513 if (read_format & PERF_FORMAT_TOTAL_TIMES)
4514 calc_timer_values(event, &now, &enabled, &running);
4516 if (event->attr.read_format & PERF_FORMAT_GROUP)
4517 perf_output_read_group(handle, event, enabled, running);
4519 perf_output_read_one(handle, event, enabled, running);
4522 void perf_output_sample(struct perf_output_handle *handle,
4523 struct perf_event_header *header,
4524 struct perf_sample_data *data,
4525 struct perf_event *event)
4527 u64 sample_type = data->type;
4529 perf_output_put(handle, *header);
4531 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4532 perf_output_put(handle, data->id);
4534 if (sample_type & PERF_SAMPLE_IP)
4535 perf_output_put(handle, data->ip);
4537 if (sample_type & PERF_SAMPLE_TID)
4538 perf_output_put(handle, data->tid_entry);
4540 if (sample_type & PERF_SAMPLE_TIME)
4541 perf_output_put(handle, data->time);
4543 if (sample_type & PERF_SAMPLE_ADDR)
4544 perf_output_put(handle, data->addr);
4546 if (sample_type & PERF_SAMPLE_ID)
4547 perf_output_put(handle, data->id);
4549 if (sample_type & PERF_SAMPLE_STREAM_ID)
4550 perf_output_put(handle, data->stream_id);
4552 if (sample_type & PERF_SAMPLE_CPU)
4553 perf_output_put(handle, data->cpu_entry);
4555 if (sample_type & PERF_SAMPLE_PERIOD)
4556 perf_output_put(handle, data->period);
4558 if (sample_type & PERF_SAMPLE_READ)
4559 perf_output_read(handle, event);
4561 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4562 if (data->callchain) {
4565 if (data->callchain)
4566 size += data->callchain->nr;
4568 size *= sizeof(u64);
4570 __output_copy(handle, data->callchain, size);
4573 perf_output_put(handle, nr);
4577 if (sample_type & PERF_SAMPLE_RAW) {
4579 perf_output_put(handle, data->raw->size);
4580 __output_copy(handle, data->raw->data,
4587 .size = sizeof(u32),
4590 perf_output_put(handle, raw);
4594 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4595 if (data->br_stack) {
4598 size = data->br_stack->nr
4599 * sizeof(struct perf_branch_entry);
4601 perf_output_put(handle, data->br_stack->nr);
4602 perf_output_copy(handle, data->br_stack->entries, size);
4605 * we always store at least the value of nr
4608 perf_output_put(handle, nr);
4612 if (sample_type & PERF_SAMPLE_REGS_USER) {
4613 u64 abi = data->regs_user.abi;
4616 * If there are no regs to dump, notice it through
4617 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4619 perf_output_put(handle, abi);
4622 u64 mask = event->attr.sample_regs_user;
4623 perf_output_sample_regs(handle,
4624 data->regs_user.regs,
4629 if (sample_type & PERF_SAMPLE_STACK_USER) {
4630 perf_output_sample_ustack(handle,
4631 data->stack_user_size,
4632 data->regs_user.regs);
4635 if (sample_type & PERF_SAMPLE_WEIGHT)
4636 perf_output_put(handle, data->weight);
4638 if (sample_type & PERF_SAMPLE_DATA_SRC)
4639 perf_output_put(handle, data->data_src.val);
4641 if (sample_type & PERF_SAMPLE_TRANSACTION)
4642 perf_output_put(handle, data->txn);
4644 if (!event->attr.watermark) {
4645 int wakeup_events = event->attr.wakeup_events;
4647 if (wakeup_events) {
4648 struct ring_buffer *rb = handle->rb;
4649 int events = local_inc_return(&rb->events);
4651 if (events >= wakeup_events) {
4652 local_sub(wakeup_events, &rb->events);
4653 local_inc(&rb->wakeup);
4659 void perf_prepare_sample(struct perf_event_header *header,
4660 struct perf_sample_data *data,
4661 struct perf_event *event,
4662 struct pt_regs *regs)
4664 u64 sample_type = event->attr.sample_type;
4666 header->type = PERF_RECORD_SAMPLE;
4667 header->size = sizeof(*header) + event->header_size;
4670 header->misc |= perf_misc_flags(regs);
4672 __perf_event_header__init_id(header, data, event);
4674 if (sample_type & PERF_SAMPLE_IP)
4675 data->ip = perf_instruction_pointer(regs);
4677 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4680 data->callchain = perf_callchain(event, regs);
4682 if (data->callchain)
4683 size += data->callchain->nr;
4685 header->size += size * sizeof(u64);
4688 if (sample_type & PERF_SAMPLE_RAW) {
4689 int size = sizeof(u32);
4692 size += data->raw->size;
4694 size += sizeof(u32);
4696 WARN_ON_ONCE(size & (sizeof(u64)-1));
4697 header->size += size;
4700 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4701 int size = sizeof(u64); /* nr */
4702 if (data->br_stack) {
4703 size += data->br_stack->nr
4704 * sizeof(struct perf_branch_entry);
4706 header->size += size;
4709 if (sample_type & PERF_SAMPLE_REGS_USER) {
4710 /* regs dump ABI info */
4711 int size = sizeof(u64);
4713 perf_sample_regs_user(&data->regs_user, regs);
4715 if (data->regs_user.regs) {
4716 u64 mask = event->attr.sample_regs_user;
4717 size += hweight64(mask) * sizeof(u64);
4720 header->size += size;
4723 if (sample_type & PERF_SAMPLE_STACK_USER) {
4725 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4726 * processed as the last one or have additional check added
4727 * in case new sample type is added, because we could eat
4728 * up the rest of the sample size.
4730 struct perf_regs_user *uregs = &data->regs_user;
4731 u16 stack_size = event->attr.sample_stack_user;
4732 u16 size = sizeof(u64);
4735 perf_sample_regs_user(uregs, regs);
4737 stack_size = perf_sample_ustack_size(stack_size, header->size,
4741 * If there is something to dump, add space for the dump
4742 * itself and for the field that tells the dynamic size,
4743 * which is how many have been actually dumped.
4746 size += sizeof(u64) + stack_size;
4748 data->stack_user_size = stack_size;
4749 header->size += size;
4753 static void perf_event_output(struct perf_event *event,
4754 struct perf_sample_data *data,
4755 struct pt_regs *regs)
4757 struct perf_output_handle handle;
4758 struct perf_event_header header;
4760 /* protect the callchain buffers */
4763 perf_prepare_sample(&header, data, event, regs);
4765 if (perf_output_begin(&handle, event, header.size))
4768 perf_output_sample(&handle, &header, data, event);
4770 perf_output_end(&handle);
4780 struct perf_read_event {
4781 struct perf_event_header header;
4788 perf_event_read_event(struct perf_event *event,
4789 struct task_struct *task)
4791 struct perf_output_handle handle;
4792 struct perf_sample_data sample;
4793 struct perf_read_event read_event = {
4795 .type = PERF_RECORD_READ,
4797 .size = sizeof(read_event) + event->read_size,
4799 .pid = perf_event_pid(event, task),
4800 .tid = perf_event_tid(event, task),
4804 perf_event_header__init_id(&read_event.header, &sample, event);
4805 ret = perf_output_begin(&handle, event, read_event.header.size);
4809 perf_output_put(&handle, read_event);
4810 perf_output_read(&handle, event);
4811 perf_event__output_id_sample(event, &handle, &sample);
4813 perf_output_end(&handle);
4816 typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
4819 perf_event_aux_ctx(struct perf_event_context *ctx,
4820 perf_event_aux_output_cb output,
4823 struct perf_event *event;
4825 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4826 if (event->state < PERF_EVENT_STATE_INACTIVE)
4828 if (!event_filter_match(event))
4830 output(event, data);
4835 perf_event_aux(perf_event_aux_output_cb output, void *data,
4836 struct perf_event_context *task_ctx)
4838 struct perf_cpu_context *cpuctx;
4839 struct perf_event_context *ctx;
4844 list_for_each_entry_rcu(pmu, &pmus, entry) {
4845 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4846 if (cpuctx->unique_pmu != pmu)
4848 perf_event_aux_ctx(&cpuctx->ctx, output, data);
4851 ctxn = pmu->task_ctx_nr;
4854 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4856 perf_event_aux_ctx(ctx, output, data);
4858 put_cpu_ptr(pmu->pmu_cpu_context);
4863 perf_event_aux_ctx(task_ctx, output, data);
4870 * task tracking -- fork/exit
4872 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
4875 struct perf_task_event {
4876 struct task_struct *task;
4877 struct perf_event_context *task_ctx;
4880 struct perf_event_header header;
4890 static int perf_event_task_match(struct perf_event *event)
4892 return event->attr.comm || event->attr.mmap ||
4893 event->attr.mmap2 || event->attr.mmap_data ||
4897 static void perf_event_task_output(struct perf_event *event,
4900 struct perf_task_event *task_event = data;
4901 struct perf_output_handle handle;
4902 struct perf_sample_data sample;
4903 struct task_struct *task = task_event->task;
4904 int ret, size = task_event->event_id.header.size;
4906 if (!perf_event_task_match(event))
4909 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4911 ret = perf_output_begin(&handle, event,
4912 task_event->event_id.header.size);
4916 task_event->event_id.pid = perf_event_pid(event, task);
4917 task_event->event_id.ppid = perf_event_pid(event, current);
4919 task_event->event_id.tid = perf_event_tid(event, task);
4920 task_event->event_id.ptid = perf_event_tid(event, current);
4922 perf_output_put(&handle, task_event->event_id);
4924 perf_event__output_id_sample(event, &handle, &sample);
4926 perf_output_end(&handle);
4928 task_event->event_id.header.size = size;
4931 static void perf_event_task(struct task_struct *task,
4932 struct perf_event_context *task_ctx,
4935 struct perf_task_event task_event;
4937 if (!atomic_read(&nr_comm_events) &&
4938 !atomic_read(&nr_mmap_events) &&
4939 !atomic_read(&nr_task_events))
4942 task_event = (struct perf_task_event){
4944 .task_ctx = task_ctx,
4947 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4949 .size = sizeof(task_event.event_id),
4955 .time = perf_clock(),
4959 perf_event_aux(perf_event_task_output,
4964 void perf_event_fork(struct task_struct *task)
4966 perf_event_task(task, NULL, 1);
4973 struct perf_comm_event {
4974 struct task_struct *task;
4979 struct perf_event_header header;
4986 static int perf_event_comm_match(struct perf_event *event)
4988 return event->attr.comm;
4991 static void perf_event_comm_output(struct perf_event *event,
4994 struct perf_comm_event *comm_event = data;
4995 struct perf_output_handle handle;
4996 struct perf_sample_data sample;
4997 int size = comm_event->event_id.header.size;
5000 if (!perf_event_comm_match(event))
5003 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
5004 ret = perf_output_begin(&handle, event,
5005 comm_event->event_id.header.size);
5010 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5011 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5013 perf_output_put(&handle, comm_event->event_id);
5014 __output_copy(&handle, comm_event->comm,
5015 comm_event->comm_size);
5017 perf_event__output_id_sample(event, &handle, &sample);
5019 perf_output_end(&handle);
5021 comm_event->event_id.header.size = size;
5024 static void perf_event_comm_event(struct perf_comm_event *comm_event)
5026 char comm[TASK_COMM_LEN];
5029 memset(comm, 0, sizeof(comm));
5030 strlcpy(comm, comm_event->task->comm, sizeof(comm));
5031 size = ALIGN(strlen(comm)+1, sizeof(u64));
5033 comm_event->comm = comm;
5034 comm_event->comm_size = size;
5036 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
5038 perf_event_aux(perf_event_comm_output,
5043 void perf_event_comm(struct task_struct *task)
5045 struct perf_comm_event comm_event;
5046 struct perf_event_context *ctx;
5050 for_each_task_context_nr(ctxn) {
5051 ctx = task->perf_event_ctxp[ctxn];
5055 perf_event_enable_on_exec(ctx);
5059 if (!atomic_read(&nr_comm_events))
5062 comm_event = (struct perf_comm_event){
5068 .type = PERF_RECORD_COMM,
5077 perf_event_comm_event(&comm_event);
5084 struct perf_mmap_event {
5085 struct vm_area_struct *vma;
5087 const char *file_name;
5094 struct perf_event_header header;
5104 static int perf_event_mmap_match(struct perf_event *event,
5107 struct perf_mmap_event *mmap_event = data;
5108 struct vm_area_struct *vma = mmap_event->vma;
5109 int executable = vma->vm_flags & VM_EXEC;
5111 return (!executable && event->attr.mmap_data) ||
5112 (executable && (event->attr.mmap || event->attr.mmap2));
5115 static void perf_event_mmap_output(struct perf_event *event,
5118 struct perf_mmap_event *mmap_event = data;
5119 struct perf_output_handle handle;
5120 struct perf_sample_data sample;
5121 int size = mmap_event->event_id.header.size;
5124 if (!perf_event_mmap_match(event, data))
5127 if (event->attr.mmap2) {
5128 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5129 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5130 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5131 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
5132 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
5135 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5136 ret = perf_output_begin(&handle, event,
5137 mmap_event->event_id.header.size);
5141 mmap_event->event_id.pid = perf_event_pid(event, current);
5142 mmap_event->event_id.tid = perf_event_tid(event, current);
5144 perf_output_put(&handle, mmap_event->event_id);
5146 if (event->attr.mmap2) {
5147 perf_output_put(&handle, mmap_event->maj);
5148 perf_output_put(&handle, mmap_event->min);
5149 perf_output_put(&handle, mmap_event->ino);
5150 perf_output_put(&handle, mmap_event->ino_generation);
5153 __output_copy(&handle, mmap_event->file_name,
5154 mmap_event->file_size);
5156 perf_event__output_id_sample(event, &handle, &sample);
5158 perf_output_end(&handle);
5160 mmap_event->event_id.header.size = size;
5163 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
5165 struct vm_area_struct *vma = mmap_event->vma;
5166 struct file *file = vma->vm_file;
5167 int maj = 0, min = 0;
5168 u64 ino = 0, gen = 0;
5175 struct inode *inode;
5178 buf = kmalloc(PATH_MAX, GFP_KERNEL);
5184 * d_path() works from the end of the rb backwards, so we
5185 * need to add enough zero bytes after the string to handle
5186 * the 64bit alignment we do later.
5188 name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64));
5193 inode = file_inode(vma->vm_file);
5194 dev = inode->i_sb->s_dev;
5196 gen = inode->i_generation;
5201 name = (char *)arch_vma_name(vma);
5205 if (vma->vm_start <= vma->vm_mm->start_brk &&
5206 vma->vm_end >= vma->vm_mm->brk) {
5210 if (vma->vm_start <= vma->vm_mm->start_stack &&
5211 vma->vm_end >= vma->vm_mm->start_stack) {
5221 strlcpy(tmp, name, sizeof(tmp));
5225 * Since our buffer works in 8 byte units we need to align our string
5226 * size to a multiple of 8. However, we must guarantee the tail end is
5227 * zero'd out to avoid leaking random bits to userspace.
5229 size = strlen(name)+1;
5230 while (!IS_ALIGNED(size, sizeof(u64)))
5231 name[size++] = '\0';
5233 mmap_event->file_name = name;
5234 mmap_event->file_size = size;
5235 mmap_event->maj = maj;
5236 mmap_event->min = min;
5237 mmap_event->ino = ino;
5238 mmap_event->ino_generation = gen;
5240 if (!(vma->vm_flags & VM_EXEC))
5241 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
5243 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
5245 perf_event_aux(perf_event_mmap_output,
5252 void perf_event_mmap(struct vm_area_struct *vma)
5254 struct perf_mmap_event mmap_event;
5256 if (!atomic_read(&nr_mmap_events))
5259 mmap_event = (struct perf_mmap_event){
5265 .type = PERF_RECORD_MMAP,
5266 .misc = PERF_RECORD_MISC_USER,
5271 .start = vma->vm_start,
5272 .len = vma->vm_end - vma->vm_start,
5273 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
5275 /* .maj (attr_mmap2 only) */
5276 /* .min (attr_mmap2 only) */
5277 /* .ino (attr_mmap2 only) */
5278 /* .ino_generation (attr_mmap2 only) */
5281 perf_event_mmap_event(&mmap_event);
5285 * IRQ throttle logging
5288 static void perf_log_throttle(struct perf_event *event, int enable)
5290 struct perf_output_handle handle;
5291 struct perf_sample_data sample;
5295 struct perf_event_header header;
5299 } throttle_event = {
5301 .type = PERF_RECORD_THROTTLE,
5303 .size = sizeof(throttle_event),
5305 .time = perf_clock(),
5306 .id = primary_event_id(event),
5307 .stream_id = event->id,
5311 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
5313 perf_event_header__init_id(&throttle_event.header, &sample, event);
5315 ret = perf_output_begin(&handle, event,
5316 throttle_event.header.size);
5320 perf_output_put(&handle, throttle_event);
5321 perf_event__output_id_sample(event, &handle, &sample);
5322 perf_output_end(&handle);
5326 * Generic event overflow handling, sampling.
5329 static int __perf_event_overflow(struct perf_event *event,
5330 int throttle, struct perf_sample_data *data,
5331 struct pt_regs *regs)
5333 int events = atomic_read(&event->event_limit);
5334 struct hw_perf_event *hwc = &event->hw;
5339 * Non-sampling counters might still use the PMI to fold short
5340 * hardware counters, ignore those.
5342 if (unlikely(!is_sampling_event(event)))
5345 seq = __this_cpu_read(perf_throttled_seq);
5346 if (seq != hwc->interrupts_seq) {
5347 hwc->interrupts_seq = seq;
5348 hwc->interrupts = 1;
5351 if (unlikely(throttle
5352 && hwc->interrupts >= max_samples_per_tick)) {
5353 __this_cpu_inc(perf_throttled_count);
5354 hwc->interrupts = MAX_INTERRUPTS;
5355 perf_log_throttle(event, 0);
5356 tick_nohz_full_kick();
5361 if (event->attr.freq) {
5362 u64 now = perf_clock();
5363 s64 delta = now - hwc->freq_time_stamp;
5365 hwc->freq_time_stamp = now;
5367 if (delta > 0 && delta < 2*TICK_NSEC)
5368 perf_adjust_period(event, delta, hwc->last_period, true);
5372 * XXX event_limit might not quite work as expected on inherited
5376 event->pending_kill = POLL_IN;
5377 if (events && atomic_dec_and_test(&event->event_limit)) {
5379 event->pending_kill = POLL_HUP;
5380 event->pending_disable = 1;
5381 irq_work_queue(&event->pending);
5384 if (event->overflow_handler)
5385 event->overflow_handler(event, data, regs);
5387 perf_event_output(event, data, regs);
5389 if (event->fasync && event->pending_kill) {
5390 event->pending_wakeup = 1;
5391 irq_work_queue(&event->pending);
5397 int perf_event_overflow(struct perf_event *event,
5398 struct perf_sample_data *data,
5399 struct pt_regs *regs)
5401 return __perf_event_overflow(event, 1, data, regs);
5405 * Generic software event infrastructure
5408 struct swevent_htable {
5409 struct swevent_hlist *swevent_hlist;
5410 struct mutex hlist_mutex;
5413 /* Recursion avoidance in each contexts */
5414 int recursion[PERF_NR_CONTEXTS];
5417 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5420 * We directly increment event->count and keep a second value in
5421 * event->hw.period_left to count intervals. This period event
5422 * is kept in the range [-sample_period, 0] so that we can use the
5426 u64 perf_swevent_set_period(struct perf_event *event)
5428 struct hw_perf_event *hwc = &event->hw;
5429 u64 period = hwc->last_period;
5433 hwc->last_period = hwc->sample_period;
5436 old = val = local64_read(&hwc->period_left);
5440 nr = div64_u64(period + val, period);
5441 offset = nr * period;
5443 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
5449 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
5450 struct perf_sample_data *data,
5451 struct pt_regs *regs)
5453 struct hw_perf_event *hwc = &event->hw;
5457 overflow = perf_swevent_set_period(event);
5459 if (hwc->interrupts == MAX_INTERRUPTS)
5462 for (; overflow; overflow--) {
5463 if (__perf_event_overflow(event, throttle,
5466 * We inhibit the overflow from happening when
5467 * hwc->interrupts == MAX_INTERRUPTS.
5475 static void perf_swevent_event(struct perf_event *event, u64 nr,
5476 struct perf_sample_data *data,
5477 struct pt_regs *regs)
5479 struct hw_perf_event *hwc = &event->hw;
5481 local64_add(nr, &event->count);
5486 if (!is_sampling_event(event))
5489 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5491 return perf_swevent_overflow(event, 1, data, regs);
5493 data->period = event->hw.last_period;
5495 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
5496 return perf_swevent_overflow(event, 1, data, regs);
5498 if (local64_add_negative(nr, &hwc->period_left))
5501 perf_swevent_overflow(event, 0, data, regs);
5504 static int perf_exclude_event(struct perf_event *event,
5505 struct pt_regs *regs)
5507 if (event->hw.state & PERF_HES_STOPPED)
5511 if (event->attr.exclude_user && user_mode(regs))
5514 if (event->attr.exclude_kernel && !user_mode(regs))
5521 static int perf_swevent_match(struct perf_event *event,
5522 enum perf_type_id type,
5524 struct perf_sample_data *data,
5525 struct pt_regs *regs)
5527 if (event->attr.type != type)
5530 if (event->attr.config != event_id)
5533 if (perf_exclude_event(event, regs))
5539 static inline u64 swevent_hash(u64 type, u32 event_id)
5541 u64 val = event_id | (type << 32);
5543 return hash_64(val, SWEVENT_HLIST_BITS);
5546 static inline struct hlist_head *
5547 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
5549 u64 hash = swevent_hash(type, event_id);
5551 return &hlist->heads[hash];
5554 /* For the read side: events when they trigger */
5555 static inline struct hlist_head *
5556 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
5558 struct swevent_hlist *hlist;
5560 hlist = rcu_dereference(swhash->swevent_hlist);
5564 return __find_swevent_head(hlist, type, event_id);
5567 /* For the event head insertion and removal in the hlist */
5568 static inline struct hlist_head *
5569 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
5571 struct swevent_hlist *hlist;
5572 u32 event_id = event->attr.config;
5573 u64 type = event->attr.type;
5576 * Event scheduling is always serialized against hlist allocation
5577 * and release. Which makes the protected version suitable here.
5578 * The context lock guarantees that.
5580 hlist = rcu_dereference_protected(swhash->swevent_hlist,
5581 lockdep_is_held(&event->ctx->lock));
5585 return __find_swevent_head(hlist, type, event_id);
5588 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5590 struct perf_sample_data *data,
5591 struct pt_regs *regs)
5593 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5594 struct perf_event *event;
5595 struct hlist_head *head;
5598 head = find_swevent_head_rcu(swhash, type, event_id);
5602 hlist_for_each_entry_rcu(event, head, hlist_entry) {
5603 if (perf_swevent_match(event, type, event_id, data, regs))
5604 perf_swevent_event(event, nr, data, regs);
5610 int perf_swevent_get_recursion_context(void)
5612 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5614 return get_recursion_context(swhash->recursion);
5616 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
5618 inline void perf_swevent_put_recursion_context(int rctx)
5620 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5622 put_recursion_context(swhash->recursion, rctx);
5625 void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
5627 struct perf_sample_data data;
5630 preempt_disable_notrace();
5631 rctx = perf_swevent_get_recursion_context();
5635 perf_sample_data_init(&data, addr, 0);
5637 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
5639 perf_swevent_put_recursion_context(rctx);
5640 preempt_enable_notrace();
5643 static void perf_swevent_read(struct perf_event *event)
5647 static int perf_swevent_add(struct perf_event *event, int flags)
5649 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5650 struct hw_perf_event *hwc = &event->hw;
5651 struct hlist_head *head;
5653 if (is_sampling_event(event)) {
5654 hwc->last_period = hwc->sample_period;
5655 perf_swevent_set_period(event);
5658 hwc->state = !(flags & PERF_EF_START);
5660 head = find_swevent_head(swhash, event);
5661 if (WARN_ON_ONCE(!head))
5664 hlist_add_head_rcu(&event->hlist_entry, head);
5669 static void perf_swevent_del(struct perf_event *event, int flags)
5671 hlist_del_rcu(&event->hlist_entry);
5674 static void perf_swevent_start(struct perf_event *event, int flags)
5676 event->hw.state = 0;
5679 static void perf_swevent_stop(struct perf_event *event, int flags)
5681 event->hw.state = PERF_HES_STOPPED;
5684 /* Deref the hlist from the update side */
5685 static inline struct swevent_hlist *
5686 swevent_hlist_deref(struct swevent_htable *swhash)
5688 return rcu_dereference_protected(swhash->swevent_hlist,
5689 lockdep_is_held(&swhash->hlist_mutex));
5692 static void swevent_hlist_release(struct swevent_htable *swhash)
5694 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
5699 rcu_assign_pointer(swhash->swevent_hlist, NULL);
5700 kfree_rcu(hlist, rcu_head);
5703 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5705 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5707 mutex_lock(&swhash->hlist_mutex);
5709 if (!--swhash->hlist_refcount)
5710 swevent_hlist_release(swhash);
5712 mutex_unlock(&swhash->hlist_mutex);
5715 static void swevent_hlist_put(struct perf_event *event)
5719 for_each_possible_cpu(cpu)
5720 swevent_hlist_put_cpu(event, cpu);
5723 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5725 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5728 mutex_lock(&swhash->hlist_mutex);
5730 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
5731 struct swevent_hlist *hlist;
5733 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5738 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5740 swhash->hlist_refcount++;
5742 mutex_unlock(&swhash->hlist_mutex);
5747 static int swevent_hlist_get(struct perf_event *event)
5750 int cpu, failed_cpu;
5753 for_each_possible_cpu(cpu) {
5754 err = swevent_hlist_get_cpu(event, cpu);
5764 for_each_possible_cpu(cpu) {
5765 if (cpu == failed_cpu)
5767 swevent_hlist_put_cpu(event, cpu);
5774 struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
5776 static void sw_perf_event_destroy(struct perf_event *event)
5778 u64 event_id = event->attr.config;
5780 WARN_ON(event->parent);
5782 static_key_slow_dec(&perf_swevent_enabled[event_id]);
5783 swevent_hlist_put(event);
5786 static int perf_swevent_init(struct perf_event *event)
5788 u64 event_id = event->attr.config;
5790 if (event->attr.type != PERF_TYPE_SOFTWARE)
5794 * no branch sampling for software events
5796 if (has_branch_stack(event))
5800 case PERF_COUNT_SW_CPU_CLOCK:
5801 case PERF_COUNT_SW_TASK_CLOCK:
5808 if (event_id >= PERF_COUNT_SW_MAX)
5811 if (!event->parent) {
5814 err = swevent_hlist_get(event);
5818 static_key_slow_inc(&perf_swevent_enabled[event_id]);
5819 event->destroy = sw_perf_event_destroy;
5825 static int perf_swevent_event_idx(struct perf_event *event)
5830 static struct pmu perf_swevent = {
5831 .task_ctx_nr = perf_sw_context,
5833 .event_init = perf_swevent_init,
5834 .add = perf_swevent_add,
5835 .del = perf_swevent_del,
5836 .start = perf_swevent_start,
5837 .stop = perf_swevent_stop,
5838 .read = perf_swevent_read,
5840 .event_idx = perf_swevent_event_idx,
5843 #ifdef CONFIG_EVENT_TRACING
5845 static int perf_tp_filter_match(struct perf_event *event,
5846 struct perf_sample_data *data)
5848 void *record = data->raw->data;
5850 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5855 static int perf_tp_event_match(struct perf_event *event,
5856 struct perf_sample_data *data,
5857 struct pt_regs *regs)
5859 if (event->hw.state & PERF_HES_STOPPED)
5862 * All tracepoints are from kernel-space.
5864 if (event->attr.exclude_kernel)
5867 if (!perf_tp_filter_match(event, data))
5873 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5874 struct pt_regs *regs, struct hlist_head *head, int rctx,
5875 struct task_struct *task)
5877 struct perf_sample_data data;
5878 struct perf_event *event;
5880 struct perf_raw_record raw = {
5885 perf_sample_data_init(&data, addr, 0);
5888 hlist_for_each_entry_rcu(event, head, hlist_entry) {
5889 if (perf_tp_event_match(event, &data, regs))
5890 perf_swevent_event(event, count, &data, regs);
5894 * If we got specified a target task, also iterate its context and
5895 * deliver this event there too.
5897 if (task && task != current) {
5898 struct perf_event_context *ctx;
5899 struct trace_entry *entry = record;
5902 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
5906 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5907 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5909 if (event->attr.config != entry->type)
5911 if (perf_tp_event_match(event, &data, regs))
5912 perf_swevent_event(event, count, &data, regs);
5918 perf_swevent_put_recursion_context(rctx);
5920 EXPORT_SYMBOL_GPL(perf_tp_event);
5922 static void tp_perf_event_destroy(struct perf_event *event)
5924 perf_trace_destroy(event);
5927 static int perf_tp_event_init(struct perf_event *event)
5931 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5935 * no branch sampling for tracepoint events
5937 if (has_branch_stack(event))
5940 err = perf_trace_init(event);
5944 event->destroy = tp_perf_event_destroy;
5949 static struct pmu perf_tracepoint = {
5950 .task_ctx_nr = perf_sw_context,
5952 .event_init = perf_tp_event_init,
5953 .add = perf_trace_add,
5954 .del = perf_trace_del,
5955 .start = perf_swevent_start,
5956 .stop = perf_swevent_stop,
5957 .read = perf_swevent_read,
5959 .event_idx = perf_swevent_event_idx,
5962 static inline void perf_tp_register(void)
5964 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5967 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5972 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5975 filter_str = strndup_user(arg, PAGE_SIZE);
5976 if (IS_ERR(filter_str))
5977 return PTR_ERR(filter_str);
5979 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5985 static void perf_event_free_filter(struct perf_event *event)
5987 ftrace_profile_free_filter(event);
5992 static inline void perf_tp_register(void)
5996 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6001 static void perf_event_free_filter(struct perf_event *event)
6005 #endif /* CONFIG_EVENT_TRACING */
6007 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6008 void perf_bp_event(struct perf_event *bp, void *data)
6010 struct perf_sample_data sample;
6011 struct pt_regs *regs = data;
6013 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
6015 if (!bp->hw.state && !perf_exclude_event(bp, regs))
6016 perf_swevent_event(bp, 1, &sample, regs);
6021 * hrtimer based swevent callback
6024 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
6026 enum hrtimer_restart ret = HRTIMER_RESTART;
6027 struct perf_sample_data data;
6028 struct pt_regs *regs;
6029 struct perf_event *event;
6032 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
6034 if (event->state != PERF_EVENT_STATE_ACTIVE)
6035 return HRTIMER_NORESTART;
6037 event->pmu->read(event);
6039 perf_sample_data_init(&data, 0, event->hw.last_period);
6040 regs = get_irq_regs();
6042 if (regs && !perf_exclude_event(event, regs)) {
6043 if (!(event->attr.exclude_idle && is_idle_task(current)))
6044 if (__perf_event_overflow(event, 1, &data, regs))
6045 ret = HRTIMER_NORESTART;
6048 period = max_t(u64, 10000, event->hw.sample_period);
6049 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
6054 static void perf_swevent_start_hrtimer(struct perf_event *event)
6056 struct hw_perf_event *hwc = &event->hw;
6059 if (!is_sampling_event(event))
6062 period = local64_read(&hwc->period_left);
6067 local64_set(&hwc->period_left, 0);
6069 period = max_t(u64, 10000, hwc->sample_period);
6071 __hrtimer_start_range_ns(&hwc->hrtimer,
6072 ns_to_ktime(period), 0,
6073 HRTIMER_MODE_REL_PINNED, 0);
6076 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
6078 struct hw_perf_event *hwc = &event->hw;
6080 if (is_sampling_event(event)) {
6081 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
6082 local64_set(&hwc->period_left, ktime_to_ns(remaining));
6084 hrtimer_cancel(&hwc->hrtimer);
6088 static void perf_swevent_init_hrtimer(struct perf_event *event)
6090 struct hw_perf_event *hwc = &event->hw;
6092 if (!is_sampling_event(event))
6095 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6096 hwc->hrtimer.function = perf_swevent_hrtimer;
6099 * Since hrtimers have a fixed rate, we can do a static freq->period
6100 * mapping and avoid the whole period adjust feedback stuff.
6102 if (event->attr.freq) {
6103 long freq = event->attr.sample_freq;
6105 event->attr.sample_period = NSEC_PER_SEC / freq;
6106 hwc->sample_period = event->attr.sample_period;
6107 local64_set(&hwc->period_left, hwc->sample_period);
6108 hwc->last_period = hwc->sample_period;
6109 event->attr.freq = 0;
6114 * Software event: cpu wall time clock
6117 static void cpu_clock_event_update(struct perf_event *event)
6122 now = local_clock();
6123 prev = local64_xchg(&event->hw.prev_count, now);
6124 local64_add(now - prev, &event->count);
6127 static void cpu_clock_event_start(struct perf_event *event, int flags)
6129 local64_set(&event->hw.prev_count, local_clock());
6130 perf_swevent_start_hrtimer(event);
6133 static void cpu_clock_event_stop(struct perf_event *event, int flags)
6135 perf_swevent_cancel_hrtimer(event);
6136 cpu_clock_event_update(event);
6139 static int cpu_clock_event_add(struct perf_event *event, int flags)
6141 if (flags & PERF_EF_START)
6142 cpu_clock_event_start(event, flags);
6147 static void cpu_clock_event_del(struct perf_event *event, int flags)
6149 cpu_clock_event_stop(event, flags);
6152 static void cpu_clock_event_read(struct perf_event *event)
6154 cpu_clock_event_update(event);
6157 static int cpu_clock_event_init(struct perf_event *event)
6159 if (event->attr.type != PERF_TYPE_SOFTWARE)
6162 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
6166 * no branch sampling for software events
6168 if (has_branch_stack(event))
6171 perf_swevent_init_hrtimer(event);
6176 static struct pmu perf_cpu_clock = {
6177 .task_ctx_nr = perf_sw_context,
6179 .event_init = cpu_clock_event_init,
6180 .add = cpu_clock_event_add,
6181 .del = cpu_clock_event_del,
6182 .start = cpu_clock_event_start,
6183 .stop = cpu_clock_event_stop,
6184 .read = cpu_clock_event_read,
6186 .event_idx = perf_swevent_event_idx,
6190 * Software event: task time clock
6193 static void task_clock_event_update(struct perf_event *event, u64 now)
6198 prev = local64_xchg(&event->hw.prev_count, now);
6200 local64_add(delta, &event->count);
6203 static void task_clock_event_start(struct perf_event *event, int flags)
6205 local64_set(&event->hw.prev_count, event->ctx->time);
6206 perf_swevent_start_hrtimer(event);
6209 static void task_clock_event_stop(struct perf_event *event, int flags)
6211 perf_swevent_cancel_hrtimer(event);
6212 task_clock_event_update(event, event->ctx->time);
6215 static int task_clock_event_add(struct perf_event *event, int flags)
6217 if (flags & PERF_EF_START)
6218 task_clock_event_start(event, flags);
6223 static void task_clock_event_del(struct perf_event *event, int flags)
6225 task_clock_event_stop(event, PERF_EF_UPDATE);
6228 static void task_clock_event_read(struct perf_event *event)
6230 u64 now = perf_clock();
6231 u64 delta = now - event->ctx->timestamp;
6232 u64 time = event->ctx->time + delta;
6234 task_clock_event_update(event, time);
6237 static int task_clock_event_init(struct perf_event *event)
6239 if (event->attr.type != PERF_TYPE_SOFTWARE)
6242 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
6246 * no branch sampling for software events
6248 if (has_branch_stack(event))
6251 perf_swevent_init_hrtimer(event);
6256 static struct pmu perf_task_clock = {
6257 .task_ctx_nr = perf_sw_context,
6259 .event_init = task_clock_event_init,
6260 .add = task_clock_event_add,
6261 .del = task_clock_event_del,
6262 .start = task_clock_event_start,
6263 .stop = task_clock_event_stop,
6264 .read = task_clock_event_read,
6266 .event_idx = perf_swevent_event_idx,
6269 static void perf_pmu_nop_void(struct pmu *pmu)
6273 static int perf_pmu_nop_int(struct pmu *pmu)
6278 static void perf_pmu_start_txn(struct pmu *pmu)
6280 perf_pmu_disable(pmu);
6283 static int perf_pmu_commit_txn(struct pmu *pmu)
6285 perf_pmu_enable(pmu);
6289 static void perf_pmu_cancel_txn(struct pmu *pmu)
6291 perf_pmu_enable(pmu);
6294 static int perf_event_idx_default(struct perf_event *event)
6296 return event->hw.idx + 1;
6300 * Ensures all contexts with the same task_ctx_nr have the same
6301 * pmu_cpu_context too.
6303 static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
6310 list_for_each_entry(pmu, &pmus, entry) {
6311 if (pmu->task_ctx_nr == ctxn)
6312 return pmu->pmu_cpu_context;
6318 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
6322 for_each_possible_cpu(cpu) {
6323 struct perf_cpu_context *cpuctx;
6325 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6327 if (cpuctx->unique_pmu == old_pmu)
6328 cpuctx->unique_pmu = pmu;
6332 static void free_pmu_context(struct pmu *pmu)
6336 mutex_lock(&pmus_lock);
6338 * Like a real lame refcount.
6340 list_for_each_entry(i, &pmus, entry) {
6341 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
6342 update_pmu_context(i, pmu);
6347 free_percpu(pmu->pmu_cpu_context);
6349 mutex_unlock(&pmus_lock);
6351 static struct idr pmu_idr;
6354 type_show(struct device *dev, struct device_attribute *attr, char *page)
6356 struct pmu *pmu = dev_get_drvdata(dev);
6358 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6360 static DEVICE_ATTR_RO(type);
6363 perf_event_mux_interval_ms_show(struct device *dev,
6364 struct device_attribute *attr,
6367 struct pmu *pmu = dev_get_drvdata(dev);
6369 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6373 perf_event_mux_interval_ms_store(struct device *dev,
6374 struct device_attribute *attr,
6375 const char *buf, size_t count)
6377 struct pmu *pmu = dev_get_drvdata(dev);
6378 int timer, cpu, ret;
6380 ret = kstrtoint(buf, 0, &timer);
6387 /* same value, noting to do */
6388 if (timer == pmu->hrtimer_interval_ms)
6391 pmu->hrtimer_interval_ms = timer;
6393 /* update all cpuctx for this PMU */
6394 for_each_possible_cpu(cpu) {
6395 struct perf_cpu_context *cpuctx;
6396 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6397 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6399 if (hrtimer_active(&cpuctx->hrtimer))
6400 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6405 static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
6407 static struct attribute *pmu_dev_attrs[] = {
6408 &dev_attr_type.attr,
6409 &dev_attr_perf_event_mux_interval_ms.attr,
6412 ATTRIBUTE_GROUPS(pmu_dev);
6414 static int pmu_bus_running;
6415 static struct bus_type pmu_bus = {
6416 .name = "event_source",
6417 .dev_groups = pmu_dev_groups,
6420 static void pmu_dev_release(struct device *dev)
6425 static int pmu_dev_alloc(struct pmu *pmu)
6429 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6433 pmu->dev->groups = pmu->attr_groups;
6434 device_initialize(pmu->dev);
6435 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6439 dev_set_drvdata(pmu->dev, pmu);
6440 pmu->dev->bus = &pmu_bus;
6441 pmu->dev->release = pmu_dev_release;
6442 ret = device_add(pmu->dev);
6450 put_device(pmu->dev);
6454 static struct lock_class_key cpuctx_mutex;
6455 static struct lock_class_key cpuctx_lock;
6457 int perf_pmu_register(struct pmu *pmu, const char *name, int type)
6461 mutex_lock(&pmus_lock);
6463 pmu->pmu_disable_count = alloc_percpu(int);
6464 if (!pmu->pmu_disable_count)
6473 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6481 if (pmu_bus_running) {
6482 ret = pmu_dev_alloc(pmu);
6488 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6489 if (pmu->pmu_cpu_context)
6490 goto got_cpu_context;
6493 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6494 if (!pmu->pmu_cpu_context)
6497 for_each_possible_cpu(cpu) {
6498 struct perf_cpu_context *cpuctx;
6500 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6501 __perf_event_init_context(&cpuctx->ctx);
6502 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
6503 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
6504 cpuctx->ctx.type = cpu_context;
6505 cpuctx->ctx.pmu = pmu;
6507 __perf_cpu_hrtimer_init(cpuctx, cpu);
6509 INIT_LIST_HEAD(&cpuctx->rotation_list);
6510 cpuctx->unique_pmu = pmu;
6514 if (!pmu->start_txn) {
6515 if (pmu->pmu_enable) {
6517 * If we have pmu_enable/pmu_disable calls, install
6518 * transaction stubs that use that to try and batch
6519 * hardware accesses.
6521 pmu->start_txn = perf_pmu_start_txn;
6522 pmu->commit_txn = perf_pmu_commit_txn;
6523 pmu->cancel_txn = perf_pmu_cancel_txn;
6525 pmu->start_txn = perf_pmu_nop_void;
6526 pmu->commit_txn = perf_pmu_nop_int;
6527 pmu->cancel_txn = perf_pmu_nop_void;
6531 if (!pmu->pmu_enable) {
6532 pmu->pmu_enable = perf_pmu_nop_void;
6533 pmu->pmu_disable = perf_pmu_nop_void;
6536 if (!pmu->event_idx)
6537 pmu->event_idx = perf_event_idx_default;
6539 list_add_rcu(&pmu->entry, &pmus);
6542 mutex_unlock(&pmus_lock);
6547 device_del(pmu->dev);
6548 put_device(pmu->dev);
6551 if (pmu->type >= PERF_TYPE_MAX)
6552 idr_remove(&pmu_idr, pmu->type);
6555 free_percpu(pmu->pmu_disable_count);
6558 EXPORT_SYMBOL_GPL(perf_pmu_register);
6560 void perf_pmu_unregister(struct pmu *pmu)
6562 mutex_lock(&pmus_lock);
6563 list_del_rcu(&pmu->entry);
6564 mutex_unlock(&pmus_lock);
6567 * We dereference the pmu list under both SRCU and regular RCU, so
6568 * synchronize against both of those.
6570 synchronize_srcu(&pmus_srcu);
6573 free_percpu(pmu->pmu_disable_count);
6574 if (pmu->type >= PERF_TYPE_MAX)
6575 idr_remove(&pmu_idr, pmu->type);
6576 device_del(pmu->dev);
6577 put_device(pmu->dev);
6578 free_pmu_context(pmu);
6580 EXPORT_SYMBOL_GPL(perf_pmu_unregister);
6582 struct pmu *perf_init_event(struct perf_event *event)
6584 struct pmu *pmu = NULL;
6588 idx = srcu_read_lock(&pmus_srcu);
6591 pmu = idr_find(&pmu_idr, event->attr.type);
6594 if (!try_module_get(pmu->module)) {
6595 pmu = ERR_PTR(-ENODEV);
6599 ret = pmu->event_init(event);
6605 list_for_each_entry_rcu(pmu, &pmus, entry) {
6606 if (!try_module_get(pmu->module)) {
6607 pmu = ERR_PTR(-ENODEV);
6611 ret = pmu->event_init(event);
6615 if (ret != -ENOENT) {
6620 pmu = ERR_PTR(-ENOENT);
6622 srcu_read_unlock(&pmus_srcu, idx);
6627 static void account_event_cpu(struct perf_event *event, int cpu)
6632 if (has_branch_stack(event)) {
6633 if (!(event->attach_state & PERF_ATTACH_TASK))
6634 atomic_inc(&per_cpu(perf_branch_stack_events, cpu));
6636 if (is_cgroup_event(event))
6637 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
6640 static void account_event(struct perf_event *event)
6645 if (event->attach_state & PERF_ATTACH_TASK)
6646 static_key_slow_inc(&perf_sched_events.key);
6647 if (event->attr.mmap || event->attr.mmap_data)
6648 atomic_inc(&nr_mmap_events);
6649 if (event->attr.comm)
6650 atomic_inc(&nr_comm_events);
6651 if (event->attr.task)
6652 atomic_inc(&nr_task_events);
6653 if (event->attr.freq) {
6654 if (atomic_inc_return(&nr_freq_events) == 1)
6655 tick_nohz_full_kick_all();
6657 if (has_branch_stack(event))
6658 static_key_slow_inc(&perf_sched_events.key);
6659 if (is_cgroup_event(event))
6660 static_key_slow_inc(&perf_sched_events.key);
6662 account_event_cpu(event, event->cpu);
6666 * Allocate and initialize a event structure
6668 static struct perf_event *
6669 perf_event_alloc(struct perf_event_attr *attr, int cpu,
6670 struct task_struct *task,
6671 struct perf_event *group_leader,
6672 struct perf_event *parent_event,
6673 perf_overflow_handler_t overflow_handler,
6677 struct perf_event *event;
6678 struct hw_perf_event *hwc;
6681 if ((unsigned)cpu >= nr_cpu_ids) {
6682 if (!task || cpu != -1)
6683 return ERR_PTR(-EINVAL);
6686 event = kzalloc(sizeof(*event), GFP_KERNEL);
6688 return ERR_PTR(-ENOMEM);
6691 * Single events are their own group leaders, with an
6692 * empty sibling list:
6695 group_leader = event;
6697 mutex_init(&event->child_mutex);
6698 INIT_LIST_HEAD(&event->child_list);
6700 INIT_LIST_HEAD(&event->group_entry);
6701 INIT_LIST_HEAD(&event->event_entry);
6702 INIT_LIST_HEAD(&event->sibling_list);
6703 INIT_LIST_HEAD(&event->rb_entry);
6704 INIT_LIST_HEAD(&event->active_entry);
6705 INIT_HLIST_NODE(&event->hlist_entry);
6708 init_waitqueue_head(&event->waitq);
6709 init_irq_work(&event->pending, perf_pending_event);
6711 mutex_init(&event->mmap_mutex);
6713 atomic_long_set(&event->refcount, 1);
6715 event->attr = *attr;
6716 event->group_leader = group_leader;
6720 event->parent = parent_event;
6722 event->ns = get_pid_ns(task_active_pid_ns(current));
6723 event->id = atomic64_inc_return(&perf_event_id);
6725 event->state = PERF_EVENT_STATE_INACTIVE;
6728 event->attach_state = PERF_ATTACH_TASK;
6730 if (attr->type == PERF_TYPE_TRACEPOINT)
6731 event->hw.tp_target = task;
6732 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6734 * hw_breakpoint is a bit difficult here..
6736 else if (attr->type == PERF_TYPE_BREAKPOINT)
6737 event->hw.bp_target = task;
6741 if (!overflow_handler && parent_event) {
6742 overflow_handler = parent_event->overflow_handler;
6743 context = parent_event->overflow_handler_context;
6746 event->overflow_handler = overflow_handler;
6747 event->overflow_handler_context = context;
6749 perf_event__state_init(event);
6754 hwc->sample_period = attr->sample_period;
6755 if (attr->freq && attr->sample_freq)
6756 hwc->sample_period = 1;
6757 hwc->last_period = hwc->sample_period;
6759 local64_set(&hwc->period_left, hwc->sample_period);
6762 * we currently do not support PERF_FORMAT_GROUP on inherited events
6764 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6767 pmu = perf_init_event(event);
6770 else if (IS_ERR(pmu)) {
6775 if (!event->parent) {
6776 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6777 err = get_callchain_buffers();
6787 event->destroy(event);
6788 module_put(pmu->module);
6791 put_pid_ns(event->ns);
6794 return ERR_PTR(err);
6797 static int perf_copy_attr(struct perf_event_attr __user *uattr,
6798 struct perf_event_attr *attr)
6803 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6807 * zero the full structure, so that a short copy will be nice.
6809 memset(attr, 0, sizeof(*attr));
6811 ret = get_user(size, &uattr->size);
6815 if (size > PAGE_SIZE) /* silly large */
6818 if (!size) /* abi compat */
6819 size = PERF_ATTR_SIZE_VER0;
6821 if (size < PERF_ATTR_SIZE_VER0)
6825 * If we're handed a bigger struct than we know of,
6826 * ensure all the unknown bits are 0 - i.e. new
6827 * user-space does not rely on any kernel feature
6828 * extensions we dont know about yet.
6830 if (size > sizeof(*attr)) {
6831 unsigned char __user *addr;
6832 unsigned char __user *end;
6835 addr = (void __user *)uattr + sizeof(*attr);
6836 end = (void __user *)uattr + size;
6838 for (; addr < end; addr++) {
6839 ret = get_user(val, addr);
6845 size = sizeof(*attr);
6848 ret = copy_from_user(attr, uattr, size);
6852 /* disabled for now */
6856 if (attr->__reserved_1)
6859 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6862 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6865 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
6866 u64 mask = attr->branch_sample_type;
6868 /* only using defined bits */
6869 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6872 /* at least one branch bit must be set */
6873 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6876 /* propagate priv level, when not set for branch */
6877 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6879 /* exclude_kernel checked on syscall entry */
6880 if (!attr->exclude_kernel)
6881 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6883 if (!attr->exclude_user)
6884 mask |= PERF_SAMPLE_BRANCH_USER;
6886 if (!attr->exclude_hv)
6887 mask |= PERF_SAMPLE_BRANCH_HV;
6889 * adjust user setting (for HW filter setup)
6891 attr->branch_sample_type = mask;
6893 /* privileged levels capture (kernel, hv): check permissions */
6894 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
6895 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6899 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
6900 ret = perf_reg_validate(attr->sample_regs_user);
6905 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
6906 if (!arch_perf_have_user_stack_dump())
6910 * We have __u32 type for the size, but so far
6911 * we can only use __u16 as maximum due to the
6912 * __u16 sample size limit.
6914 if (attr->sample_stack_user >= USHRT_MAX)
6916 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
6924 put_user(sizeof(*attr), &uattr->size);
6930 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6932 struct ring_buffer *rb = NULL, *old_rb = NULL;
6938 /* don't allow circular references */
6939 if (event == output_event)
6943 * Don't allow cross-cpu buffers
6945 if (output_event->cpu != event->cpu)
6949 * If its not a per-cpu rb, it must be the same task.
6951 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6955 mutex_lock(&event->mmap_mutex);
6956 /* Can't redirect output if we've got an active mmap() */
6957 if (atomic_read(&event->mmap_count))
6963 /* get the rb we want to redirect to */
6964 rb = ring_buffer_get(output_event);
6970 ring_buffer_detach(event, old_rb);
6973 ring_buffer_attach(event, rb);
6975 rcu_assign_pointer(event->rb, rb);
6978 ring_buffer_put(old_rb);
6980 * Since we detached before setting the new rb, so that we
6981 * could attach the new rb, we could have missed a wakeup.
6984 wake_up_all(&event->waitq);
6989 mutex_unlock(&event->mmap_mutex);
6996 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6998 * @attr_uptr: event_id type attributes for monitoring/sampling
7001 * @group_fd: group leader event fd
7003 SYSCALL_DEFINE5(perf_event_open,
7004 struct perf_event_attr __user *, attr_uptr,
7005 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
7007 struct perf_event *group_leader = NULL, *output_event = NULL;
7008 struct perf_event *event, *sibling;
7009 struct perf_event_attr attr;
7010 struct perf_event_context *ctx;
7011 struct file *event_file = NULL;
7012 struct fd group = {NULL, 0};
7013 struct task_struct *task = NULL;
7018 int f_flags = O_RDWR;
7020 /* for future expandability... */
7021 if (flags & ~PERF_FLAG_ALL)
7024 err = perf_copy_attr(attr_uptr, &attr);
7028 if (!attr.exclude_kernel) {
7029 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7034 if (attr.sample_freq > sysctl_perf_event_sample_rate)
7039 * In cgroup mode, the pid argument is used to pass the fd
7040 * opened to the cgroup directory in cgroupfs. The cpu argument
7041 * designates the cpu on which to monitor threads from that
7044 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
7047 if (flags & PERF_FLAG_FD_CLOEXEC)
7048 f_flags |= O_CLOEXEC;
7050 event_fd = get_unused_fd_flags(f_flags);
7054 if (group_fd != -1) {
7055 err = perf_fget_light(group_fd, &group);
7058 group_leader = group.file->private_data;
7059 if (flags & PERF_FLAG_FD_OUTPUT)
7060 output_event = group_leader;
7061 if (flags & PERF_FLAG_FD_NO_GROUP)
7062 group_leader = NULL;
7065 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
7066 task = find_lively_task_by_vpid(pid);
7068 err = PTR_ERR(task);
7075 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
7077 if (IS_ERR(event)) {
7078 err = PTR_ERR(event);
7082 if (flags & PERF_FLAG_PID_CGROUP) {
7083 err = perf_cgroup_connect(pid, event, &attr, group_leader);
7085 __free_event(event);
7090 account_event(event);
7093 * Special case software events and allow them to be part of
7094 * any hardware group.
7099 (is_software_event(event) != is_software_event(group_leader))) {
7100 if (is_software_event(event)) {
7102 * If event and group_leader are not both a software
7103 * event, and event is, then group leader is not.
7105 * Allow the addition of software events to !software
7106 * groups, this is safe because software events never
7109 pmu = group_leader->pmu;
7110 } else if (is_software_event(group_leader) &&
7111 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
7113 * In case the group is a pure software group, and we
7114 * try to add a hardware event, move the whole group to
7115 * the hardware context.
7122 * Get the target context (task or percpu):
7124 ctx = find_get_context(pmu, task, event->cpu);
7131 put_task_struct(task);
7136 * Look up the group leader (we will attach this event to it):
7142 * Do not allow a recursive hierarchy (this new sibling
7143 * becoming part of another group-sibling):
7145 if (group_leader->group_leader != group_leader)
7148 * Do not allow to attach to a group in a different
7149 * task or CPU context:
7152 if (group_leader->ctx->type != ctx->type)
7155 if (group_leader->ctx != ctx)
7160 * Only a group leader can be exclusive or pinned
7162 if (attr.exclusive || attr.pinned)
7167 err = perf_event_set_output(event, output_event);
7172 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
7174 if (IS_ERR(event_file)) {
7175 err = PTR_ERR(event_file);
7180 struct perf_event_context *gctx = group_leader->ctx;
7182 mutex_lock(&gctx->mutex);
7183 perf_remove_from_context(group_leader);
7186 * Removing from the context ends up with disabled
7187 * event. What we want here is event in the initial
7188 * startup state, ready to be add into new context.
7190 perf_event__state_init(group_leader);
7191 list_for_each_entry(sibling, &group_leader->sibling_list,
7193 perf_remove_from_context(sibling);
7194 perf_event__state_init(sibling);
7197 mutex_unlock(&gctx->mutex);
7201 WARN_ON_ONCE(ctx->parent_ctx);
7202 mutex_lock(&ctx->mutex);
7206 perf_install_in_context(ctx, group_leader, event->cpu);
7208 list_for_each_entry(sibling, &group_leader->sibling_list,
7210 perf_install_in_context(ctx, sibling, event->cpu);
7215 perf_install_in_context(ctx, event, event->cpu);
7216 perf_unpin_context(ctx);
7217 mutex_unlock(&ctx->mutex);
7221 event->owner = current;
7223 mutex_lock(¤t->perf_event_mutex);
7224 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
7225 mutex_unlock(¤t->perf_event_mutex);
7228 * Precalculate sample_data sizes
7230 perf_event__header_size(event);
7231 perf_event__id_header_size(event);
7234 * Drop the reference on the group_event after placing the
7235 * new event on the sibling_list. This ensures destruction
7236 * of the group leader will find the pointer to itself in
7237 * perf_group_detach().
7240 fd_install(event_fd, event_file);
7244 perf_unpin_context(ctx);
7251 put_task_struct(task);
7255 put_unused_fd(event_fd);
7260 * perf_event_create_kernel_counter
7262 * @attr: attributes of the counter to create
7263 * @cpu: cpu in which the counter is bound
7264 * @task: task to profile (NULL for percpu)
7267 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
7268 struct task_struct *task,
7269 perf_overflow_handler_t overflow_handler,
7272 struct perf_event_context *ctx;
7273 struct perf_event *event;
7277 * Get the target context (task or percpu):
7280 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
7281 overflow_handler, context);
7282 if (IS_ERR(event)) {
7283 err = PTR_ERR(event);
7287 account_event(event);
7289 ctx = find_get_context(event->pmu, task, cpu);
7295 WARN_ON_ONCE(ctx->parent_ctx);
7296 mutex_lock(&ctx->mutex);
7297 perf_install_in_context(ctx, event, cpu);
7298 perf_unpin_context(ctx);
7299 mutex_unlock(&ctx->mutex);
7306 return ERR_PTR(err);
7308 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
7310 void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
7312 struct perf_event_context *src_ctx;
7313 struct perf_event_context *dst_ctx;
7314 struct perf_event *event, *tmp;
7317 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
7318 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
7320 mutex_lock(&src_ctx->mutex);
7321 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
7323 perf_remove_from_context(event);
7324 unaccount_event_cpu(event, src_cpu);
7326 list_add(&event->migrate_entry, &events);
7328 mutex_unlock(&src_ctx->mutex);
7332 mutex_lock(&dst_ctx->mutex);
7333 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7334 list_del(&event->migrate_entry);
7335 if (event->state >= PERF_EVENT_STATE_OFF)
7336 event->state = PERF_EVENT_STATE_INACTIVE;
7337 account_event_cpu(event, dst_cpu);
7338 perf_install_in_context(dst_ctx, event, dst_cpu);
7341 mutex_unlock(&dst_ctx->mutex);
7343 EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
7345 static void sync_child_event(struct perf_event *child_event,
7346 struct task_struct *child)
7348 struct perf_event *parent_event = child_event->parent;
7351 if (child_event->attr.inherit_stat)
7352 perf_event_read_event(child_event, child);
7354 child_val = perf_event_count(child_event);
7357 * Add back the child's count to the parent's count:
7359 atomic64_add(child_val, &parent_event->child_count);
7360 atomic64_add(child_event->total_time_enabled,
7361 &parent_event->child_total_time_enabled);
7362 atomic64_add(child_event->total_time_running,
7363 &parent_event->child_total_time_running);
7366 * Remove this event from the parent's list
7368 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7369 mutex_lock(&parent_event->child_mutex);
7370 list_del_init(&child_event->child_list);
7371 mutex_unlock(&parent_event->child_mutex);
7374 * Release the parent event, if this was the last
7377 put_event(parent_event);
7381 __perf_event_exit_task(struct perf_event *child_event,
7382 struct perf_event_context *child_ctx,
7383 struct task_struct *child)
7385 if (child_event->parent) {
7386 raw_spin_lock_irq(&child_ctx->lock);
7387 perf_group_detach(child_event);
7388 raw_spin_unlock_irq(&child_ctx->lock);
7391 perf_remove_from_context(child_event);
7394 * It can happen that the parent exits first, and has events
7395 * that are still around due to the child reference. These
7396 * events need to be zapped.
7398 if (child_event->parent) {
7399 sync_child_event(child_event, child);
7400 free_event(child_event);
7404 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
7406 struct perf_event *child_event, *tmp;
7407 struct perf_event_context *child_ctx;
7408 unsigned long flags;
7410 if (likely(!child->perf_event_ctxp[ctxn])) {
7411 perf_event_task(child, NULL, 0);
7415 local_irq_save(flags);
7417 * We can't reschedule here because interrupts are disabled,
7418 * and either child is current or it is a task that can't be
7419 * scheduled, so we are now safe from rescheduling changing
7422 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
7425 * Take the context lock here so that if find_get_context is
7426 * reading child->perf_event_ctxp, we wait until it has
7427 * incremented the context's refcount before we do put_ctx below.
7429 raw_spin_lock(&child_ctx->lock);
7430 task_ctx_sched_out(child_ctx);
7431 child->perf_event_ctxp[ctxn] = NULL;
7433 * If this context is a clone; unclone it so it can't get
7434 * swapped to another process while we're removing all
7435 * the events from it.
7437 unclone_ctx(child_ctx);
7438 update_context_time(child_ctx);
7439 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
7442 * Report the task dead after unscheduling the events so that we
7443 * won't get any samples after PERF_RECORD_EXIT. We can however still
7444 * get a few PERF_RECORD_READ events.
7446 perf_event_task(child, child_ctx, 0);
7449 * We can recurse on the same lock type through:
7451 * __perf_event_exit_task()
7452 * sync_child_event()
7454 * mutex_lock(&ctx->mutex)
7456 * But since its the parent context it won't be the same instance.
7458 mutex_lock(&child_ctx->mutex);
7461 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
7463 __perf_event_exit_task(child_event, child_ctx, child);
7465 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
7467 __perf_event_exit_task(child_event, child_ctx, child);
7470 * If the last event was a group event, it will have appended all
7471 * its siblings to the list, but we obtained 'tmp' before that which
7472 * will still point to the list head terminating the iteration.
7474 if (!list_empty(&child_ctx->pinned_groups) ||
7475 !list_empty(&child_ctx->flexible_groups))
7478 mutex_unlock(&child_ctx->mutex);
7484 * When a child task exits, feed back event values to parent events.
7486 void perf_event_exit_task(struct task_struct *child)
7488 struct perf_event *event, *tmp;
7491 mutex_lock(&child->perf_event_mutex);
7492 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
7494 list_del_init(&event->owner_entry);
7497 * Ensure the list deletion is visible before we clear
7498 * the owner, closes a race against perf_release() where
7499 * we need to serialize on the owner->perf_event_mutex.
7502 event->owner = NULL;
7504 mutex_unlock(&child->perf_event_mutex);
7506 for_each_task_context_nr(ctxn)
7507 perf_event_exit_task_context(child, ctxn);
7510 static void perf_free_event(struct perf_event *event,
7511 struct perf_event_context *ctx)
7513 struct perf_event *parent = event->parent;
7515 if (WARN_ON_ONCE(!parent))
7518 mutex_lock(&parent->child_mutex);
7519 list_del_init(&event->child_list);
7520 mutex_unlock(&parent->child_mutex);
7524 perf_group_detach(event);
7525 list_del_event(event, ctx);
7530 * free an unexposed, unused context as created by inheritance by
7531 * perf_event_init_task below, used by fork() in case of fail.
7533 void perf_event_free_task(struct task_struct *task)
7535 struct perf_event_context *ctx;
7536 struct perf_event *event, *tmp;
7539 for_each_task_context_nr(ctxn) {
7540 ctx = task->perf_event_ctxp[ctxn];
7544 mutex_lock(&ctx->mutex);
7546 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
7548 perf_free_event(event, ctx);
7550 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
7552 perf_free_event(event, ctx);
7554 if (!list_empty(&ctx->pinned_groups) ||
7555 !list_empty(&ctx->flexible_groups))
7558 mutex_unlock(&ctx->mutex);
7564 void perf_event_delayed_put(struct task_struct *task)
7568 for_each_task_context_nr(ctxn)
7569 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
7573 * inherit a event from parent task to child task:
7575 static struct perf_event *
7576 inherit_event(struct perf_event *parent_event,
7577 struct task_struct *parent,
7578 struct perf_event_context *parent_ctx,
7579 struct task_struct *child,
7580 struct perf_event *group_leader,
7581 struct perf_event_context *child_ctx)
7583 struct perf_event *child_event;
7584 unsigned long flags;
7587 * Instead of creating recursive hierarchies of events,
7588 * we link inherited events back to the original parent,
7589 * which has a filp for sure, which we use as the reference
7592 if (parent_event->parent)
7593 parent_event = parent_event->parent;
7595 child_event = perf_event_alloc(&parent_event->attr,
7598 group_leader, parent_event,
7600 if (IS_ERR(child_event))
7603 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
7604 free_event(child_event);
7611 * Make the child state follow the state of the parent event,
7612 * not its attr.disabled bit. We hold the parent's mutex,
7613 * so we won't race with perf_event_{en, dis}able_family.
7615 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
7616 child_event->state = PERF_EVENT_STATE_INACTIVE;
7618 child_event->state = PERF_EVENT_STATE_OFF;
7620 if (parent_event->attr.freq) {
7621 u64 sample_period = parent_event->hw.sample_period;
7622 struct hw_perf_event *hwc = &child_event->hw;
7624 hwc->sample_period = sample_period;
7625 hwc->last_period = sample_period;
7627 local64_set(&hwc->period_left, sample_period);
7630 child_event->ctx = child_ctx;
7631 child_event->overflow_handler = parent_event->overflow_handler;
7632 child_event->overflow_handler_context
7633 = parent_event->overflow_handler_context;
7636 * Precalculate sample_data sizes
7638 perf_event__header_size(child_event);
7639 perf_event__id_header_size(child_event);
7642 * Link it up in the child's context:
7644 raw_spin_lock_irqsave(&child_ctx->lock, flags);
7645 add_event_to_ctx(child_event, child_ctx);
7646 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
7649 * Link this into the parent event's child list
7651 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7652 mutex_lock(&parent_event->child_mutex);
7653 list_add_tail(&child_event->child_list, &parent_event->child_list);
7654 mutex_unlock(&parent_event->child_mutex);
7659 static int inherit_group(struct perf_event *parent_event,
7660 struct task_struct *parent,
7661 struct perf_event_context *parent_ctx,
7662 struct task_struct *child,
7663 struct perf_event_context *child_ctx)
7665 struct perf_event *leader;
7666 struct perf_event *sub;
7667 struct perf_event *child_ctr;
7669 leader = inherit_event(parent_event, parent, parent_ctx,
7670 child, NULL, child_ctx);
7672 return PTR_ERR(leader);
7673 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7674 child_ctr = inherit_event(sub, parent, parent_ctx,
7675 child, leader, child_ctx);
7676 if (IS_ERR(child_ctr))
7677 return PTR_ERR(child_ctr);
7683 inherit_task_group(struct perf_event *event, struct task_struct *parent,
7684 struct perf_event_context *parent_ctx,
7685 struct task_struct *child, int ctxn,
7689 struct perf_event_context *child_ctx;
7691 if (!event->attr.inherit) {
7696 child_ctx = child->perf_event_ctxp[ctxn];
7699 * This is executed from the parent task context, so
7700 * inherit events that have been marked for cloning.
7701 * First allocate and initialize a context for the
7705 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
7709 child->perf_event_ctxp[ctxn] = child_ctx;
7712 ret = inherit_group(event, parent, parent_ctx,
7722 * Initialize the perf_event context in task_struct
7724 int perf_event_init_context(struct task_struct *child, int ctxn)
7726 struct perf_event_context *child_ctx, *parent_ctx;
7727 struct perf_event_context *cloned_ctx;
7728 struct perf_event *event;
7729 struct task_struct *parent = current;
7730 int inherited_all = 1;
7731 unsigned long flags;
7734 if (likely(!parent->perf_event_ctxp[ctxn]))
7738 * If the parent's context is a clone, pin it so it won't get
7741 parent_ctx = perf_pin_task_context(parent, ctxn);
7744 * No need to check if parent_ctx != NULL here; since we saw
7745 * it non-NULL earlier, the only reason for it to become NULL
7746 * is if we exit, and since we're currently in the middle of
7747 * a fork we can't be exiting at the same time.
7751 * Lock the parent list. No need to lock the child - not PID
7752 * hashed yet and not running, so nobody can access it.
7754 mutex_lock(&parent_ctx->mutex);
7757 * We dont have to disable NMIs - we are only looking at
7758 * the list, not manipulating it:
7760 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
7761 ret = inherit_task_group(event, parent, parent_ctx,
7762 child, ctxn, &inherited_all);
7768 * We can't hold ctx->lock when iterating the ->flexible_group list due
7769 * to allocations, but we need to prevent rotation because
7770 * rotate_ctx() will change the list from interrupt context.
7772 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7773 parent_ctx->rotate_disable = 1;
7774 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7776 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
7777 ret = inherit_task_group(event, parent, parent_ctx,
7778 child, ctxn, &inherited_all);
7783 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7784 parent_ctx->rotate_disable = 0;
7786 child_ctx = child->perf_event_ctxp[ctxn];
7788 if (child_ctx && inherited_all) {
7790 * Mark the child context as a clone of the parent
7791 * context, or of whatever the parent is a clone of.
7793 * Note that if the parent is a clone, the holding of
7794 * parent_ctx->lock avoids it from being uncloned.
7796 cloned_ctx = parent_ctx->parent_ctx;
7798 child_ctx->parent_ctx = cloned_ctx;
7799 child_ctx->parent_gen = parent_ctx->parent_gen;
7801 child_ctx->parent_ctx = parent_ctx;
7802 child_ctx->parent_gen = parent_ctx->generation;
7804 get_ctx(child_ctx->parent_ctx);
7807 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7808 mutex_unlock(&parent_ctx->mutex);
7810 perf_unpin_context(parent_ctx);
7811 put_ctx(parent_ctx);
7817 * Initialize the perf_event context in task_struct
7819 int perf_event_init_task(struct task_struct *child)
7823 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7824 mutex_init(&child->perf_event_mutex);
7825 INIT_LIST_HEAD(&child->perf_event_list);
7827 for_each_task_context_nr(ctxn) {
7828 ret = perf_event_init_context(child, ctxn);
7836 static void __init perf_event_init_all_cpus(void)
7838 struct swevent_htable *swhash;
7841 for_each_possible_cpu(cpu) {
7842 swhash = &per_cpu(swevent_htable, cpu);
7843 mutex_init(&swhash->hlist_mutex);
7844 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
7848 static void perf_event_init_cpu(int cpu)
7850 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7852 mutex_lock(&swhash->hlist_mutex);
7853 if (swhash->hlist_refcount > 0) {
7854 struct swevent_hlist *hlist;
7856 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7858 rcu_assign_pointer(swhash->swevent_hlist, hlist);
7860 mutex_unlock(&swhash->hlist_mutex);
7863 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7864 static void perf_pmu_rotate_stop(struct pmu *pmu)
7866 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7868 WARN_ON(!irqs_disabled());
7870 list_del_init(&cpuctx->rotation_list);
7873 static void __perf_event_exit_context(void *__info)
7875 struct perf_event_context *ctx = __info;
7876 struct perf_event *event;
7878 perf_pmu_rotate_stop(ctx->pmu);
7881 list_for_each_entry_rcu(event, &ctx->event_list, event_entry)
7882 __perf_remove_from_context(event);
7886 static void perf_event_exit_cpu_context(int cpu)
7888 struct perf_event_context *ctx;
7892 idx = srcu_read_lock(&pmus_srcu);
7893 list_for_each_entry_rcu(pmu, &pmus, entry) {
7894 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
7896 mutex_lock(&ctx->mutex);
7897 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7898 mutex_unlock(&ctx->mutex);
7900 srcu_read_unlock(&pmus_srcu, idx);
7903 static void perf_event_exit_cpu(int cpu)
7905 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7907 perf_event_exit_cpu_context(cpu);
7909 mutex_lock(&swhash->hlist_mutex);
7910 swevent_hlist_release(swhash);
7911 mutex_unlock(&swhash->hlist_mutex);
7914 static inline void perf_event_exit_cpu(int cpu) { }
7918 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7922 for_each_online_cpu(cpu)
7923 perf_event_exit_cpu(cpu);
7929 * Run the perf reboot notifier at the very last possible moment so that
7930 * the generic watchdog code runs as long as possible.
7932 static struct notifier_block perf_reboot_notifier = {
7933 .notifier_call = perf_reboot,
7934 .priority = INT_MIN,
7938 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7940 unsigned int cpu = (long)hcpu;
7942 switch (action & ~CPU_TASKS_FROZEN) {
7944 case CPU_UP_PREPARE:
7945 case CPU_DOWN_FAILED:
7946 perf_event_init_cpu(cpu);
7949 case CPU_UP_CANCELED:
7950 case CPU_DOWN_PREPARE:
7951 perf_event_exit_cpu(cpu);
7960 void __init perf_event_init(void)
7966 perf_event_init_all_cpus();
7967 init_srcu_struct(&pmus_srcu);
7968 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7969 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7970 perf_pmu_register(&perf_task_clock, NULL, -1);
7972 perf_cpu_notifier(perf_cpu_notify);
7973 register_reboot_notifier(&perf_reboot_notifier);
7975 ret = init_hw_breakpoint();
7976 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7978 /* do not patch jump label more than once per second */
7979 jump_label_rate_limit(&perf_sched_events, HZ);
7982 * Build time assertion that we keep the data_head at the intended
7983 * location. IOW, validation we got the __reserved[] size right.
7985 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
7989 static int __init perf_event_sysfs_init(void)
7994 mutex_lock(&pmus_lock);
7996 ret = bus_register(&pmu_bus);
8000 list_for_each_entry(pmu, &pmus, entry) {
8001 if (!pmu->name || pmu->type < 0)
8004 ret = pmu_dev_alloc(pmu);
8005 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
8007 pmu_bus_running = 1;
8011 mutex_unlock(&pmus_lock);
8015 device_initcall(perf_event_sysfs_init);
8017 #ifdef CONFIG_CGROUP_PERF
8018 static struct cgroup_subsys_state *
8019 perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
8021 struct perf_cgroup *jc;
8023 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
8025 return ERR_PTR(-ENOMEM);
8027 jc->info = alloc_percpu(struct perf_cgroup_info);
8030 return ERR_PTR(-ENOMEM);
8036 static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
8038 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
8040 free_percpu(jc->info);
8044 static int __perf_cgroup_move(void *info)
8046 struct task_struct *task = info;
8047 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
8051 static void perf_cgroup_attach(struct cgroup_subsys_state *css,
8052 struct cgroup_taskset *tset)
8054 struct task_struct *task;
8056 cgroup_taskset_for_each(task, tset)
8057 task_function_call(task, __perf_cgroup_move, task);
8060 static void perf_cgroup_exit(struct cgroup_subsys_state *css,
8061 struct cgroup_subsys_state *old_css,
8062 struct task_struct *task)
8065 * cgroup_exit() is called in the copy_process() failure path.
8066 * Ignore this case since the task hasn't ran yet, this avoids
8067 * trying to poke a half freed task state from generic code.
8069 if (!(task->flags & PF_EXITING))
8072 task_function_call(task, __perf_cgroup_move, task);
8075 struct cgroup_subsys perf_event_cgrp_subsys = {
8076 .css_alloc = perf_cgroup_css_alloc,
8077 .css_free = perf_cgroup_css_free,
8078 .exit = perf_cgroup_exit,
8079 .attach = perf_cgroup_attach,
8081 #endif /* CONFIG_CGROUP_PERF */