2 * Performance events core code:
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/idr.h>
17 #include <linux/file.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/hash.h>
21 #include <linux/tick.h>
22 #include <linux/sysfs.h>
23 #include <linux/dcache.h>
24 #include <linux/percpu.h>
25 #include <linux/ptrace.h>
26 #include <linux/reboot.h>
27 #include <linux/vmstat.h>
28 #include <linux/device.h>
29 #include <linux/export.h>
30 #include <linux/vmalloc.h>
31 #include <linux/hardirq.h>
32 #include <linux/rculist.h>
33 #include <linux/uaccess.h>
34 #include <linux/syscalls.h>
35 #include <linux/anon_inodes.h>
36 #include <linux/kernel_stat.h>
37 #include <linux/perf_event.h>
38 #include <linux/ftrace_event.h>
39 #include <linux/hw_breakpoint.h>
40 #include <linux/mm_types.h>
41 #include <linux/cgroup.h>
45 #include <asm/irq_regs.h>
47 struct remote_function_call {
48 struct task_struct *p;
49 int (*func)(void *info);
54 static void remote_function(void *data)
56 struct remote_function_call *tfc = data;
57 struct task_struct *p = tfc->p;
61 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
65 tfc->ret = tfc->func(tfc->info);
69 * task_function_call - call a function on the cpu on which a task runs
70 * @p: the task to evaluate
71 * @func: the function to be called
72 * @info: the function call argument
74 * Calls the function @func when the task is currently running. This might
75 * be on the current CPU, which just calls the function directly
77 * returns: @func return value, or
78 * -ESRCH - when the process isn't running
79 * -EAGAIN - when the process moved away
82 task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
84 struct remote_function_call data = {
88 .ret = -ESRCH, /* No such (running) process */
92 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
98 * cpu_function_call - call a function on the cpu
99 * @func: the function to be called
100 * @info: the function call argument
102 * Calls the function @func on the remote cpu.
104 * returns: @func return value or -ENXIO when the cpu is offline
106 static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
108 struct remote_function_call data = {
112 .ret = -ENXIO, /* No such CPU */
115 smp_call_function_single(cpu, remote_function, &data, 1);
120 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
121 PERF_FLAG_FD_OUTPUT |\
122 PERF_FLAG_PID_CGROUP)
125 * branch priv levels that need permission checks
127 #define PERF_SAMPLE_BRANCH_PERM_PLM \
128 (PERF_SAMPLE_BRANCH_KERNEL |\
129 PERF_SAMPLE_BRANCH_HV)
132 EVENT_FLEXIBLE = 0x1,
134 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
138 * perf_sched_events : >0 events exist
139 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
141 struct static_key_deferred perf_sched_events __read_mostly;
142 static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
143 static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
145 static atomic_t nr_mmap_events __read_mostly;
146 static atomic_t nr_comm_events __read_mostly;
147 static atomic_t nr_task_events __read_mostly;
149 static LIST_HEAD(pmus);
150 static DEFINE_MUTEX(pmus_lock);
151 static struct srcu_struct pmus_srcu;
154 * perf event paranoia level:
155 * -1 - not paranoid at all
156 * 0 - disallow raw tracepoint access for unpriv
157 * 1 - disallow cpu events for unpriv
158 * 2 - disallow kernel profiling for unpriv
160 int sysctl_perf_event_paranoid __read_mostly = 1;
162 /* Minimum for 512 kiB + 1 user control page */
163 int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
166 * max perf event sample rate
168 #define DEFAULT_MAX_SAMPLE_RATE 100000
169 #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
170 #define DEFAULT_CPU_TIME_MAX_PERCENT 25
172 int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
174 static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
175 static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
177 static atomic_t perf_sample_allowed_ns __read_mostly =
178 ATOMIC_INIT( DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100);
180 void update_perf_cpu_limits(void)
182 u64 tmp = perf_sample_period_ns;
184 tmp *= sysctl_perf_cpu_time_max_percent;
185 tmp = do_div(tmp, 100);
186 atomic_set(&perf_sample_allowed_ns, tmp);
189 static int perf_rotate_context(struct perf_cpu_context *cpuctx);
191 int perf_proc_update_handler(struct ctl_table *table, int write,
192 void __user *buffer, size_t *lenp,
195 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
200 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
201 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
202 update_perf_cpu_limits();
207 int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
209 int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
210 void __user *buffer, size_t *lenp,
213 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
218 update_perf_cpu_limits();
224 * perf samples are done in some very critical code paths (NMIs).
225 * If they take too much CPU time, the system can lock up and not
226 * get any real work done. This will drop the sample rate when
227 * we detect that events are taking too long.
229 #define NR_ACCUMULATED_SAMPLES 128
230 DEFINE_PER_CPU(u64, running_sample_length);
232 void perf_sample_event_took(u64 sample_len_ns)
234 u64 avg_local_sample_len;
235 u64 local_samples_len = __get_cpu_var(running_sample_length);
237 if (atomic_read(&perf_sample_allowed_ns) == 0)
240 /* decay the counter by 1 average sample */
241 local_samples_len = __get_cpu_var(running_sample_length);
242 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
243 local_samples_len += sample_len_ns;
244 __get_cpu_var(running_sample_length) = local_samples_len;
247 * note: this will be biased artifically low until we have
248 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
249 * from having to maintain a count.
251 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
253 if (avg_local_sample_len <= atomic_read(&perf_sample_allowed_ns))
256 if (max_samples_per_tick <= 1)
259 max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
260 sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
261 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
263 printk_ratelimited(KERN_WARNING
264 "perf samples too long (%lld > %d), lowering "
265 "kernel.perf_event_max_sample_rate to %d\n",
266 avg_local_sample_len,
267 atomic_read(&perf_sample_allowed_ns),
268 sysctl_perf_event_sample_rate);
270 update_perf_cpu_limits();
273 static atomic64_t perf_event_id;
275 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
276 enum event_type_t event_type);
278 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
279 enum event_type_t event_type,
280 struct task_struct *task);
282 static void update_context_time(struct perf_event_context *ctx);
283 static u64 perf_event_time(struct perf_event *event);
285 void __weak perf_event_print_debug(void) { }
287 extern __weak const char *perf_pmu_name(void)
292 static inline u64 perf_clock(void)
294 return local_clock();
297 static inline struct perf_cpu_context *
298 __get_cpu_context(struct perf_event_context *ctx)
300 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
303 static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
304 struct perf_event_context *ctx)
306 raw_spin_lock(&cpuctx->ctx.lock);
308 raw_spin_lock(&ctx->lock);
311 static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
312 struct perf_event_context *ctx)
315 raw_spin_unlock(&ctx->lock);
316 raw_spin_unlock(&cpuctx->ctx.lock);
319 #ifdef CONFIG_CGROUP_PERF
322 * perf_cgroup_info keeps track of time_enabled for a cgroup.
323 * This is a per-cpu dynamically allocated data structure.
325 struct perf_cgroup_info {
331 struct cgroup_subsys_state css;
332 struct perf_cgroup_info __percpu *info;
336 * Must ensure cgroup is pinned (css_get) before calling
337 * this function. In other words, we cannot call this function
338 * if there is no cgroup event for the current CPU context.
340 static inline struct perf_cgroup *
341 perf_cgroup_from_task(struct task_struct *task)
343 return container_of(task_subsys_state(task, perf_subsys_id),
344 struct perf_cgroup, css);
348 perf_cgroup_match(struct perf_event *event)
350 struct perf_event_context *ctx = event->ctx;
351 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
353 /* @event doesn't care about cgroup */
357 /* wants specific cgroup scope but @cpuctx isn't associated with any */
362 * Cgroup scoping is recursive. An event enabled for a cgroup is
363 * also enabled for all its descendant cgroups. If @cpuctx's
364 * cgroup is a descendant of @event's (the test covers identity
365 * case), it's a match.
367 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
368 event->cgrp->css.cgroup);
371 static inline bool perf_tryget_cgroup(struct perf_event *event)
373 return css_tryget(&event->cgrp->css);
376 static inline void perf_put_cgroup(struct perf_event *event)
378 css_put(&event->cgrp->css);
381 static inline void perf_detach_cgroup(struct perf_event *event)
383 perf_put_cgroup(event);
387 static inline int is_cgroup_event(struct perf_event *event)
389 return event->cgrp != NULL;
392 static inline u64 perf_cgroup_event_time(struct perf_event *event)
394 struct perf_cgroup_info *t;
396 t = per_cpu_ptr(event->cgrp->info, event->cpu);
400 static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
402 struct perf_cgroup_info *info;
407 info = this_cpu_ptr(cgrp->info);
409 info->time += now - info->timestamp;
410 info->timestamp = now;
413 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
415 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
417 __update_cgrp_time(cgrp_out);
420 static inline void update_cgrp_time_from_event(struct perf_event *event)
422 struct perf_cgroup *cgrp;
425 * ensure we access cgroup data only when needed and
426 * when we know the cgroup is pinned (css_get)
428 if (!is_cgroup_event(event))
431 cgrp = perf_cgroup_from_task(current);
433 * Do not update time when cgroup is not active
435 if (cgrp == event->cgrp)
436 __update_cgrp_time(event->cgrp);
440 perf_cgroup_set_timestamp(struct task_struct *task,
441 struct perf_event_context *ctx)
443 struct perf_cgroup *cgrp;
444 struct perf_cgroup_info *info;
447 * ctx->lock held by caller
448 * ensure we do not access cgroup data
449 * unless we have the cgroup pinned (css_get)
451 if (!task || !ctx->nr_cgroups)
454 cgrp = perf_cgroup_from_task(task);
455 info = this_cpu_ptr(cgrp->info);
456 info->timestamp = ctx->timestamp;
459 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
460 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
463 * reschedule events based on the cgroup constraint of task.
465 * mode SWOUT : schedule out everything
466 * mode SWIN : schedule in based on cgroup for next
468 void perf_cgroup_switch(struct task_struct *task, int mode)
470 struct perf_cpu_context *cpuctx;
475 * disable interrupts to avoid geting nr_cgroup
476 * changes via __perf_event_disable(). Also
479 local_irq_save(flags);
482 * we reschedule only in the presence of cgroup
483 * constrained events.
487 list_for_each_entry_rcu(pmu, &pmus, entry) {
488 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
489 if (cpuctx->unique_pmu != pmu)
490 continue; /* ensure we process each cpuctx once */
493 * perf_cgroup_events says at least one
494 * context on this CPU has cgroup events.
496 * ctx->nr_cgroups reports the number of cgroup
497 * events for a context.
499 if (cpuctx->ctx.nr_cgroups > 0) {
500 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
501 perf_pmu_disable(cpuctx->ctx.pmu);
503 if (mode & PERF_CGROUP_SWOUT) {
504 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
506 * must not be done before ctxswout due
507 * to event_filter_match() in event_sched_out()
512 if (mode & PERF_CGROUP_SWIN) {
513 WARN_ON_ONCE(cpuctx->cgrp);
515 * set cgrp before ctxsw in to allow
516 * event_filter_match() to not have to pass
519 cpuctx->cgrp = perf_cgroup_from_task(task);
520 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
522 perf_pmu_enable(cpuctx->ctx.pmu);
523 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
529 local_irq_restore(flags);
532 static inline void perf_cgroup_sched_out(struct task_struct *task,
533 struct task_struct *next)
535 struct perf_cgroup *cgrp1;
536 struct perf_cgroup *cgrp2 = NULL;
539 * we come here when we know perf_cgroup_events > 0
541 cgrp1 = perf_cgroup_from_task(task);
544 * next is NULL when called from perf_event_enable_on_exec()
545 * that will systematically cause a cgroup_switch()
548 cgrp2 = perf_cgroup_from_task(next);
551 * only schedule out current cgroup events if we know
552 * that we are switching to a different cgroup. Otherwise,
553 * do no touch the cgroup events.
556 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
559 static inline void perf_cgroup_sched_in(struct task_struct *prev,
560 struct task_struct *task)
562 struct perf_cgroup *cgrp1;
563 struct perf_cgroup *cgrp2 = NULL;
566 * we come here when we know perf_cgroup_events > 0
568 cgrp1 = perf_cgroup_from_task(task);
570 /* prev can never be NULL */
571 cgrp2 = perf_cgroup_from_task(prev);
574 * only need to schedule in cgroup events if we are changing
575 * cgroup during ctxsw. Cgroup events were not scheduled
576 * out of ctxsw out if that was not the case.
579 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
582 static inline int perf_cgroup_connect(int fd, struct perf_event *event,
583 struct perf_event_attr *attr,
584 struct perf_event *group_leader)
586 struct perf_cgroup *cgrp;
587 struct cgroup_subsys_state *css;
588 struct fd f = fdget(fd);
594 css = cgroup_css_from_dir(f.file, perf_subsys_id);
600 cgrp = container_of(css, struct perf_cgroup, css);
603 /* must be done before we fput() the file */
604 if (!perf_tryget_cgroup(event)) {
611 * all events in a group must monitor
612 * the same cgroup because a task belongs
613 * to only one perf cgroup at a time
615 if (group_leader && group_leader->cgrp != cgrp) {
616 perf_detach_cgroup(event);
625 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
627 struct perf_cgroup_info *t;
628 t = per_cpu_ptr(event->cgrp->info, event->cpu);
629 event->shadow_ctx_time = now - t->timestamp;
633 perf_cgroup_defer_enabled(struct perf_event *event)
636 * when the current task's perf cgroup does not match
637 * the event's, we need to remember to call the
638 * perf_mark_enable() function the first time a task with
639 * a matching perf cgroup is scheduled in.
641 if (is_cgroup_event(event) && !perf_cgroup_match(event))
642 event->cgrp_defer_enabled = 1;
646 perf_cgroup_mark_enabled(struct perf_event *event,
647 struct perf_event_context *ctx)
649 struct perf_event *sub;
650 u64 tstamp = perf_event_time(event);
652 if (!event->cgrp_defer_enabled)
655 event->cgrp_defer_enabled = 0;
657 event->tstamp_enabled = tstamp - event->total_time_enabled;
658 list_for_each_entry(sub, &event->sibling_list, group_entry) {
659 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
660 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
661 sub->cgrp_defer_enabled = 0;
665 #else /* !CONFIG_CGROUP_PERF */
668 perf_cgroup_match(struct perf_event *event)
673 static inline void perf_detach_cgroup(struct perf_event *event)
676 static inline int is_cgroup_event(struct perf_event *event)
681 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
686 static inline void update_cgrp_time_from_event(struct perf_event *event)
690 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
694 static inline void perf_cgroup_sched_out(struct task_struct *task,
695 struct task_struct *next)
699 static inline void perf_cgroup_sched_in(struct task_struct *prev,
700 struct task_struct *task)
704 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
705 struct perf_event_attr *attr,
706 struct perf_event *group_leader)
712 perf_cgroup_set_timestamp(struct task_struct *task,
713 struct perf_event_context *ctx)
718 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
723 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
727 static inline u64 perf_cgroup_event_time(struct perf_event *event)
733 perf_cgroup_defer_enabled(struct perf_event *event)
738 perf_cgroup_mark_enabled(struct perf_event *event,
739 struct perf_event_context *ctx)
745 * set default to be dependent on timer tick just
748 #define PERF_CPU_HRTIMER (1000 / HZ)
750 * function must be called with interrupts disbled
752 static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
754 struct perf_cpu_context *cpuctx;
755 enum hrtimer_restart ret = HRTIMER_NORESTART;
758 WARN_ON(!irqs_disabled());
760 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
762 rotations = perf_rotate_context(cpuctx);
765 * arm timer if needed
768 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
769 ret = HRTIMER_RESTART;
775 /* CPU is going down */
776 void perf_cpu_hrtimer_cancel(int cpu)
778 struct perf_cpu_context *cpuctx;
782 if (WARN_ON(cpu != smp_processor_id()))
785 local_irq_save(flags);
789 list_for_each_entry_rcu(pmu, &pmus, entry) {
790 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
792 if (pmu->task_ctx_nr == perf_sw_context)
795 hrtimer_cancel(&cpuctx->hrtimer);
800 local_irq_restore(flags);
803 static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
805 struct hrtimer *hr = &cpuctx->hrtimer;
806 struct pmu *pmu = cpuctx->ctx.pmu;
809 /* no multiplexing needed for SW PMU */
810 if (pmu->task_ctx_nr == perf_sw_context)
814 * check default is sane, if not set then force to
815 * default interval (1/tick)
817 timer = pmu->hrtimer_interval_ms;
819 timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
821 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
823 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
824 hr->function = perf_cpu_hrtimer_handler;
827 static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
829 struct hrtimer *hr = &cpuctx->hrtimer;
830 struct pmu *pmu = cpuctx->ctx.pmu;
833 if (pmu->task_ctx_nr == perf_sw_context)
836 if (hrtimer_active(hr))
839 if (!hrtimer_callback_running(hr))
840 __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
841 0, HRTIMER_MODE_REL_PINNED, 0);
844 void perf_pmu_disable(struct pmu *pmu)
846 int *count = this_cpu_ptr(pmu->pmu_disable_count);
848 pmu->pmu_disable(pmu);
851 void perf_pmu_enable(struct pmu *pmu)
853 int *count = this_cpu_ptr(pmu->pmu_disable_count);
855 pmu->pmu_enable(pmu);
858 static DEFINE_PER_CPU(struct list_head, rotation_list);
861 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
862 * because they're strictly cpu affine and rotate_start is called with IRQs
863 * disabled, while rotate_context is called from IRQ context.
865 static void perf_pmu_rotate_start(struct pmu *pmu)
867 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
868 struct list_head *head = &__get_cpu_var(rotation_list);
870 WARN_ON(!irqs_disabled());
872 if (list_empty(&cpuctx->rotation_list)) {
873 int was_empty = list_empty(head);
874 list_add(&cpuctx->rotation_list, head);
876 tick_nohz_full_kick();
880 static void get_ctx(struct perf_event_context *ctx)
882 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
885 static void put_ctx(struct perf_event_context *ctx)
887 if (atomic_dec_and_test(&ctx->refcount)) {
889 put_ctx(ctx->parent_ctx);
891 put_task_struct(ctx->task);
892 kfree_rcu(ctx, rcu_head);
896 static void unclone_ctx(struct perf_event_context *ctx)
898 if (ctx->parent_ctx) {
899 put_ctx(ctx->parent_ctx);
900 ctx->parent_ctx = NULL;
904 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
907 * only top level events have the pid namespace they were created in
910 event = event->parent;
912 return task_tgid_nr_ns(p, event->ns);
915 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
918 * only top level events have the pid namespace they were created in
921 event = event->parent;
923 return task_pid_nr_ns(p, event->ns);
927 * If we inherit events we want to return the parent event id
930 static u64 primary_event_id(struct perf_event *event)
935 id = event->parent->id;
941 * Get the perf_event_context for a task and lock it.
942 * This has to cope with with the fact that until it is locked,
943 * the context could get moved to another task.
945 static struct perf_event_context *
946 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
948 struct perf_event_context *ctx;
952 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
955 * If this context is a clone of another, it might
956 * get swapped for another underneath us by
957 * perf_event_task_sched_out, though the
958 * rcu_read_lock() protects us from any context
959 * getting freed. Lock the context and check if it
960 * got swapped before we could get the lock, and retry
961 * if so. If we locked the right context, then it
962 * can't get swapped on us any more.
964 raw_spin_lock_irqsave(&ctx->lock, *flags);
965 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
966 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
970 if (!atomic_inc_not_zero(&ctx->refcount)) {
971 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
980 * Get the context for a task and increment its pin_count so it
981 * can't get swapped to another task. This also increments its
982 * reference count so that the context can't get freed.
984 static struct perf_event_context *
985 perf_pin_task_context(struct task_struct *task, int ctxn)
987 struct perf_event_context *ctx;
990 ctx = perf_lock_task_context(task, ctxn, &flags);
993 raw_spin_unlock_irqrestore(&ctx->lock, flags);
998 static void perf_unpin_context(struct perf_event_context *ctx)
1000 unsigned long flags;
1002 raw_spin_lock_irqsave(&ctx->lock, flags);
1004 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1008 * Update the record of the current time in a context.
1010 static void update_context_time(struct perf_event_context *ctx)
1012 u64 now = perf_clock();
1014 ctx->time += now - ctx->timestamp;
1015 ctx->timestamp = now;
1018 static u64 perf_event_time(struct perf_event *event)
1020 struct perf_event_context *ctx = event->ctx;
1022 if (is_cgroup_event(event))
1023 return perf_cgroup_event_time(event);
1025 return ctx ? ctx->time : 0;
1029 * Update the total_time_enabled and total_time_running fields for a event.
1030 * The caller of this function needs to hold the ctx->lock.
1032 static void update_event_times(struct perf_event *event)
1034 struct perf_event_context *ctx = event->ctx;
1037 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1038 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1041 * in cgroup mode, time_enabled represents
1042 * the time the event was enabled AND active
1043 * tasks were in the monitored cgroup. This is
1044 * independent of the activity of the context as
1045 * there may be a mix of cgroup and non-cgroup events.
1047 * That is why we treat cgroup events differently
1050 if (is_cgroup_event(event))
1051 run_end = perf_cgroup_event_time(event);
1052 else if (ctx->is_active)
1053 run_end = ctx->time;
1055 run_end = event->tstamp_stopped;
1057 event->total_time_enabled = run_end - event->tstamp_enabled;
1059 if (event->state == PERF_EVENT_STATE_INACTIVE)
1060 run_end = event->tstamp_stopped;
1062 run_end = perf_event_time(event);
1064 event->total_time_running = run_end - event->tstamp_running;
1069 * Update total_time_enabled and total_time_running for all events in a group.
1071 static void update_group_times(struct perf_event *leader)
1073 struct perf_event *event;
1075 update_event_times(leader);
1076 list_for_each_entry(event, &leader->sibling_list, group_entry)
1077 update_event_times(event);
1080 static struct list_head *
1081 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1083 if (event->attr.pinned)
1084 return &ctx->pinned_groups;
1086 return &ctx->flexible_groups;
1090 * Add a event from the lists for its context.
1091 * Must be called with ctx->mutex and ctx->lock held.
1094 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1096 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1097 event->attach_state |= PERF_ATTACH_CONTEXT;
1100 * If we're a stand alone event or group leader, we go to the context
1101 * list, group events are kept attached to the group so that
1102 * perf_group_detach can, at all times, locate all siblings.
1104 if (event->group_leader == event) {
1105 struct list_head *list;
1107 if (is_software_event(event))
1108 event->group_flags |= PERF_GROUP_SOFTWARE;
1110 list = ctx_group_list(event, ctx);
1111 list_add_tail(&event->group_entry, list);
1114 if (is_cgroup_event(event))
1117 if (has_branch_stack(event))
1118 ctx->nr_branch_stack++;
1120 list_add_rcu(&event->event_entry, &ctx->event_list);
1121 if (!ctx->nr_events)
1122 perf_pmu_rotate_start(ctx->pmu);
1124 if (event->attr.inherit_stat)
1129 * Initialize event state based on the perf_event_attr::disabled.
1131 static inline void perf_event__state_init(struct perf_event *event)
1133 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1134 PERF_EVENT_STATE_INACTIVE;
1138 * Called at perf_event creation and when events are attached/detached from a
1141 static void perf_event__read_size(struct perf_event *event)
1143 int entry = sizeof(u64); /* value */
1147 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1148 size += sizeof(u64);
1150 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1151 size += sizeof(u64);
1153 if (event->attr.read_format & PERF_FORMAT_ID)
1154 entry += sizeof(u64);
1156 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1157 nr += event->group_leader->nr_siblings;
1158 size += sizeof(u64);
1162 event->read_size = size;
1165 static void perf_event__header_size(struct perf_event *event)
1167 struct perf_sample_data *data;
1168 u64 sample_type = event->attr.sample_type;
1171 perf_event__read_size(event);
1173 if (sample_type & PERF_SAMPLE_IP)
1174 size += sizeof(data->ip);
1176 if (sample_type & PERF_SAMPLE_ADDR)
1177 size += sizeof(data->addr);
1179 if (sample_type & PERF_SAMPLE_PERIOD)
1180 size += sizeof(data->period);
1182 if (sample_type & PERF_SAMPLE_WEIGHT)
1183 size += sizeof(data->weight);
1185 if (sample_type & PERF_SAMPLE_READ)
1186 size += event->read_size;
1188 if (sample_type & PERF_SAMPLE_DATA_SRC)
1189 size += sizeof(data->data_src.val);
1191 event->header_size = size;
1194 static void perf_event__id_header_size(struct perf_event *event)
1196 struct perf_sample_data *data;
1197 u64 sample_type = event->attr.sample_type;
1200 if (sample_type & PERF_SAMPLE_TID)
1201 size += sizeof(data->tid_entry);
1203 if (sample_type & PERF_SAMPLE_TIME)
1204 size += sizeof(data->time);
1206 if (sample_type & PERF_SAMPLE_ID)
1207 size += sizeof(data->id);
1209 if (sample_type & PERF_SAMPLE_STREAM_ID)
1210 size += sizeof(data->stream_id);
1212 if (sample_type & PERF_SAMPLE_CPU)
1213 size += sizeof(data->cpu_entry);
1215 event->id_header_size = size;
1218 static void perf_group_attach(struct perf_event *event)
1220 struct perf_event *group_leader = event->group_leader, *pos;
1223 * We can have double attach due to group movement in perf_event_open.
1225 if (event->attach_state & PERF_ATTACH_GROUP)
1228 event->attach_state |= PERF_ATTACH_GROUP;
1230 if (group_leader == event)
1233 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1234 !is_software_event(event))
1235 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1237 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1238 group_leader->nr_siblings++;
1240 perf_event__header_size(group_leader);
1242 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1243 perf_event__header_size(pos);
1247 * Remove a event from the lists for its context.
1248 * Must be called with ctx->mutex and ctx->lock held.
1251 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1253 struct perf_cpu_context *cpuctx;
1255 * We can have double detach due to exit/hot-unplug + close.
1257 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
1260 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1262 if (is_cgroup_event(event)) {
1264 cpuctx = __get_cpu_context(ctx);
1266 * if there are no more cgroup events
1267 * then cler cgrp to avoid stale pointer
1268 * in update_cgrp_time_from_cpuctx()
1270 if (!ctx->nr_cgroups)
1271 cpuctx->cgrp = NULL;
1274 if (has_branch_stack(event))
1275 ctx->nr_branch_stack--;
1278 if (event->attr.inherit_stat)
1281 list_del_rcu(&event->event_entry);
1283 if (event->group_leader == event)
1284 list_del_init(&event->group_entry);
1286 update_group_times(event);
1289 * If event was in error state, then keep it
1290 * that way, otherwise bogus counts will be
1291 * returned on read(). The only way to get out
1292 * of error state is by explicit re-enabling
1295 if (event->state > PERF_EVENT_STATE_OFF)
1296 event->state = PERF_EVENT_STATE_OFF;
1299 static void perf_group_detach(struct perf_event *event)
1301 struct perf_event *sibling, *tmp;
1302 struct list_head *list = NULL;
1305 * We can have double detach due to exit/hot-unplug + close.
1307 if (!(event->attach_state & PERF_ATTACH_GROUP))
1310 event->attach_state &= ~PERF_ATTACH_GROUP;
1313 * If this is a sibling, remove it from its group.
1315 if (event->group_leader != event) {
1316 list_del_init(&event->group_entry);
1317 event->group_leader->nr_siblings--;
1321 if (!list_empty(&event->group_entry))
1322 list = &event->group_entry;
1325 * If this was a group event with sibling events then
1326 * upgrade the siblings to singleton events by adding them
1327 * to whatever list we are on.
1329 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
1331 list_move_tail(&sibling->group_entry, list);
1332 sibling->group_leader = sibling;
1334 /* Inherit group flags from the previous leader */
1335 sibling->group_flags = event->group_flags;
1339 perf_event__header_size(event->group_leader);
1341 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1342 perf_event__header_size(tmp);
1346 event_filter_match(struct perf_event *event)
1348 return (event->cpu == -1 || event->cpu == smp_processor_id())
1349 && perf_cgroup_match(event);
1353 event_sched_out(struct perf_event *event,
1354 struct perf_cpu_context *cpuctx,
1355 struct perf_event_context *ctx)
1357 u64 tstamp = perf_event_time(event);
1360 * An event which could not be activated because of
1361 * filter mismatch still needs to have its timings
1362 * maintained, otherwise bogus information is return
1363 * via read() for time_enabled, time_running:
1365 if (event->state == PERF_EVENT_STATE_INACTIVE
1366 && !event_filter_match(event)) {
1367 delta = tstamp - event->tstamp_stopped;
1368 event->tstamp_running += delta;
1369 event->tstamp_stopped = tstamp;
1372 if (event->state != PERF_EVENT_STATE_ACTIVE)
1375 event->state = PERF_EVENT_STATE_INACTIVE;
1376 if (event->pending_disable) {
1377 event->pending_disable = 0;
1378 event->state = PERF_EVENT_STATE_OFF;
1380 event->tstamp_stopped = tstamp;
1381 event->pmu->del(event, 0);
1384 if (!is_software_event(event))
1385 cpuctx->active_oncpu--;
1387 if (event->attr.freq && event->attr.sample_freq)
1389 if (event->attr.exclusive || !cpuctx->active_oncpu)
1390 cpuctx->exclusive = 0;
1394 group_sched_out(struct perf_event *group_event,
1395 struct perf_cpu_context *cpuctx,
1396 struct perf_event_context *ctx)
1398 struct perf_event *event;
1399 int state = group_event->state;
1401 event_sched_out(group_event, cpuctx, ctx);
1404 * Schedule out siblings (if any):
1406 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1407 event_sched_out(event, cpuctx, ctx);
1409 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
1410 cpuctx->exclusive = 0;
1414 * Cross CPU call to remove a performance event
1416 * We disable the event on the hardware level first. After that we
1417 * remove it from the context list.
1419 static int __perf_remove_from_context(void *info)
1421 struct perf_event *event = info;
1422 struct perf_event_context *ctx = event->ctx;
1423 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1425 raw_spin_lock(&ctx->lock);
1426 event_sched_out(event, cpuctx, ctx);
1427 list_del_event(event, ctx);
1428 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1430 cpuctx->task_ctx = NULL;
1432 raw_spin_unlock(&ctx->lock);
1439 * Remove the event from a task's (or a CPU's) list of events.
1441 * CPU events are removed with a smp call. For task events we only
1442 * call when the task is on a CPU.
1444 * If event->ctx is a cloned context, callers must make sure that
1445 * every task struct that event->ctx->task could possibly point to
1446 * remains valid. This is OK when called from perf_release since
1447 * that only calls us on the top-level context, which can't be a clone.
1448 * When called from perf_event_exit_task, it's OK because the
1449 * context has been detached from its task.
1451 static void perf_remove_from_context(struct perf_event *event)
1453 struct perf_event_context *ctx = event->ctx;
1454 struct task_struct *task = ctx->task;
1456 lockdep_assert_held(&ctx->mutex);
1460 * Per cpu events are removed via an smp call and
1461 * the removal is always successful.
1463 cpu_function_call(event->cpu, __perf_remove_from_context, event);
1468 if (!task_function_call(task, __perf_remove_from_context, event))
1471 raw_spin_lock_irq(&ctx->lock);
1473 * If we failed to find a running task, but find the context active now
1474 * that we've acquired the ctx->lock, retry.
1476 if (ctx->is_active) {
1477 raw_spin_unlock_irq(&ctx->lock);
1482 * Since the task isn't running, its safe to remove the event, us
1483 * holding the ctx->lock ensures the task won't get scheduled in.
1485 list_del_event(event, ctx);
1486 raw_spin_unlock_irq(&ctx->lock);
1490 * Cross CPU call to disable a performance event
1492 int __perf_event_disable(void *info)
1494 struct perf_event *event = info;
1495 struct perf_event_context *ctx = event->ctx;
1496 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1499 * If this is a per-task event, need to check whether this
1500 * event's task is the current task on this cpu.
1502 * Can trigger due to concurrent perf_event_context_sched_out()
1503 * flipping contexts around.
1505 if (ctx->task && cpuctx->task_ctx != ctx)
1508 raw_spin_lock(&ctx->lock);
1511 * If the event is on, turn it off.
1512 * If it is in error state, leave it in error state.
1514 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1515 update_context_time(ctx);
1516 update_cgrp_time_from_event(event);
1517 update_group_times(event);
1518 if (event == event->group_leader)
1519 group_sched_out(event, cpuctx, ctx);
1521 event_sched_out(event, cpuctx, ctx);
1522 event->state = PERF_EVENT_STATE_OFF;
1525 raw_spin_unlock(&ctx->lock);
1533 * If event->ctx is a cloned context, callers must make sure that
1534 * every task struct that event->ctx->task could possibly point to
1535 * remains valid. This condition is satisifed when called through
1536 * perf_event_for_each_child or perf_event_for_each because they
1537 * hold the top-level event's child_mutex, so any descendant that
1538 * goes to exit will block in sync_child_event.
1539 * When called from perf_pending_event it's OK because event->ctx
1540 * is the current context on this CPU and preemption is disabled,
1541 * hence we can't get into perf_event_task_sched_out for this context.
1543 void perf_event_disable(struct perf_event *event)
1545 struct perf_event_context *ctx = event->ctx;
1546 struct task_struct *task = ctx->task;
1550 * Disable the event on the cpu that it's on
1552 cpu_function_call(event->cpu, __perf_event_disable, event);
1557 if (!task_function_call(task, __perf_event_disable, event))
1560 raw_spin_lock_irq(&ctx->lock);
1562 * If the event is still active, we need to retry the cross-call.
1564 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1565 raw_spin_unlock_irq(&ctx->lock);
1567 * Reload the task pointer, it might have been changed by
1568 * a concurrent perf_event_context_sched_out().
1575 * Since we have the lock this context can't be scheduled
1576 * in, so we can change the state safely.
1578 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1579 update_group_times(event);
1580 event->state = PERF_EVENT_STATE_OFF;
1582 raw_spin_unlock_irq(&ctx->lock);
1584 EXPORT_SYMBOL_GPL(perf_event_disable);
1586 static void perf_set_shadow_time(struct perf_event *event,
1587 struct perf_event_context *ctx,
1591 * use the correct time source for the time snapshot
1593 * We could get by without this by leveraging the
1594 * fact that to get to this function, the caller
1595 * has most likely already called update_context_time()
1596 * and update_cgrp_time_xx() and thus both timestamp
1597 * are identical (or very close). Given that tstamp is,
1598 * already adjusted for cgroup, we could say that:
1599 * tstamp - ctx->timestamp
1601 * tstamp - cgrp->timestamp.
1603 * Then, in perf_output_read(), the calculation would
1604 * work with no changes because:
1605 * - event is guaranteed scheduled in
1606 * - no scheduled out in between
1607 * - thus the timestamp would be the same
1609 * But this is a bit hairy.
1611 * So instead, we have an explicit cgroup call to remain
1612 * within the time time source all along. We believe it
1613 * is cleaner and simpler to understand.
1615 if (is_cgroup_event(event))
1616 perf_cgroup_set_shadow_time(event, tstamp);
1618 event->shadow_ctx_time = tstamp - ctx->timestamp;
1621 #define MAX_INTERRUPTS (~0ULL)
1623 static void perf_log_throttle(struct perf_event *event, int enable);
1626 event_sched_in(struct perf_event *event,
1627 struct perf_cpu_context *cpuctx,
1628 struct perf_event_context *ctx)
1630 u64 tstamp = perf_event_time(event);
1632 if (event->state <= PERF_EVENT_STATE_OFF)
1635 event->state = PERF_EVENT_STATE_ACTIVE;
1636 event->oncpu = smp_processor_id();
1639 * Unthrottle events, since we scheduled we might have missed several
1640 * ticks already, also for a heavily scheduling task there is little
1641 * guarantee it'll get a tick in a timely manner.
1643 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1644 perf_log_throttle(event, 1);
1645 event->hw.interrupts = 0;
1649 * The new state must be visible before we turn it on in the hardware:
1653 if (event->pmu->add(event, PERF_EF_START)) {
1654 event->state = PERF_EVENT_STATE_INACTIVE;
1659 event->tstamp_running += tstamp - event->tstamp_stopped;
1661 perf_set_shadow_time(event, ctx, tstamp);
1663 if (!is_software_event(event))
1664 cpuctx->active_oncpu++;
1666 if (event->attr.freq && event->attr.sample_freq)
1669 if (event->attr.exclusive)
1670 cpuctx->exclusive = 1;
1676 group_sched_in(struct perf_event *group_event,
1677 struct perf_cpu_context *cpuctx,
1678 struct perf_event_context *ctx)
1680 struct perf_event *event, *partial_group = NULL;
1681 struct pmu *pmu = group_event->pmu;
1682 u64 now = ctx->time;
1683 bool simulate = false;
1685 if (group_event->state == PERF_EVENT_STATE_OFF)
1688 pmu->start_txn(pmu);
1690 if (event_sched_in(group_event, cpuctx, ctx)) {
1691 pmu->cancel_txn(pmu);
1692 perf_cpu_hrtimer_restart(cpuctx);
1697 * Schedule in siblings as one group (if any):
1699 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1700 if (event_sched_in(event, cpuctx, ctx)) {
1701 partial_group = event;
1706 if (!pmu->commit_txn(pmu))
1711 * Groups can be scheduled in as one unit only, so undo any
1712 * partial group before returning:
1713 * The events up to the failed event are scheduled out normally,
1714 * tstamp_stopped will be updated.
1716 * The failed events and the remaining siblings need to have
1717 * their timings updated as if they had gone thru event_sched_in()
1718 * and event_sched_out(). This is required to get consistent timings
1719 * across the group. This also takes care of the case where the group
1720 * could never be scheduled by ensuring tstamp_stopped is set to mark
1721 * the time the event was actually stopped, such that time delta
1722 * calculation in update_event_times() is correct.
1724 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1725 if (event == partial_group)
1729 event->tstamp_running += now - event->tstamp_stopped;
1730 event->tstamp_stopped = now;
1732 event_sched_out(event, cpuctx, ctx);
1735 event_sched_out(group_event, cpuctx, ctx);
1737 pmu->cancel_txn(pmu);
1739 perf_cpu_hrtimer_restart(cpuctx);
1745 * Work out whether we can put this event group on the CPU now.
1747 static int group_can_go_on(struct perf_event *event,
1748 struct perf_cpu_context *cpuctx,
1752 * Groups consisting entirely of software events can always go on.
1754 if (event->group_flags & PERF_GROUP_SOFTWARE)
1757 * If an exclusive group is already on, no other hardware
1760 if (cpuctx->exclusive)
1763 * If this group is exclusive and there are already
1764 * events on the CPU, it can't go on.
1766 if (event->attr.exclusive && cpuctx->active_oncpu)
1769 * Otherwise, try to add it if all previous groups were able
1775 static void add_event_to_ctx(struct perf_event *event,
1776 struct perf_event_context *ctx)
1778 u64 tstamp = perf_event_time(event);
1780 list_add_event(event, ctx);
1781 perf_group_attach(event);
1782 event->tstamp_enabled = tstamp;
1783 event->tstamp_running = tstamp;
1784 event->tstamp_stopped = tstamp;
1787 static void task_ctx_sched_out(struct perf_event_context *ctx);
1789 ctx_sched_in(struct perf_event_context *ctx,
1790 struct perf_cpu_context *cpuctx,
1791 enum event_type_t event_type,
1792 struct task_struct *task);
1794 static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1795 struct perf_event_context *ctx,
1796 struct task_struct *task)
1798 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1800 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1801 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1803 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1807 * Cross CPU call to install and enable a performance event
1809 * Must be called with ctx->mutex held
1811 static int __perf_install_in_context(void *info)
1813 struct perf_event *event = info;
1814 struct perf_event_context *ctx = event->ctx;
1815 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1816 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1817 struct task_struct *task = current;
1819 perf_ctx_lock(cpuctx, task_ctx);
1820 perf_pmu_disable(cpuctx->ctx.pmu);
1823 * If there was an active task_ctx schedule it out.
1826 task_ctx_sched_out(task_ctx);
1829 * If the context we're installing events in is not the
1830 * active task_ctx, flip them.
1832 if (ctx->task && task_ctx != ctx) {
1834 raw_spin_unlock(&task_ctx->lock);
1835 raw_spin_lock(&ctx->lock);
1840 cpuctx->task_ctx = task_ctx;
1841 task = task_ctx->task;
1844 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
1846 update_context_time(ctx);
1848 * update cgrp time only if current cgrp
1849 * matches event->cgrp. Must be done before
1850 * calling add_event_to_ctx()
1852 update_cgrp_time_from_event(event);
1854 add_event_to_ctx(event, ctx);
1857 * Schedule everything back in
1859 perf_event_sched_in(cpuctx, task_ctx, task);
1861 perf_pmu_enable(cpuctx->ctx.pmu);
1862 perf_ctx_unlock(cpuctx, task_ctx);
1868 * Attach a performance event to a context
1870 * First we add the event to the list with the hardware enable bit
1871 * in event->hw_config cleared.
1873 * If the event is attached to a task which is on a CPU we use a smp
1874 * call to enable it in the task context. The task might have been
1875 * scheduled away, but we check this in the smp call again.
1878 perf_install_in_context(struct perf_event_context *ctx,
1879 struct perf_event *event,
1882 struct task_struct *task = ctx->task;
1884 lockdep_assert_held(&ctx->mutex);
1887 if (event->cpu != -1)
1892 * Per cpu events are installed via an smp call and
1893 * the install is always successful.
1895 cpu_function_call(cpu, __perf_install_in_context, event);
1900 if (!task_function_call(task, __perf_install_in_context, event))
1903 raw_spin_lock_irq(&ctx->lock);
1905 * If we failed to find a running task, but find the context active now
1906 * that we've acquired the ctx->lock, retry.
1908 if (ctx->is_active) {
1909 raw_spin_unlock_irq(&ctx->lock);
1914 * Since the task isn't running, its safe to add the event, us holding
1915 * the ctx->lock ensures the task won't get scheduled in.
1917 add_event_to_ctx(event, ctx);
1918 raw_spin_unlock_irq(&ctx->lock);
1922 * Put a event into inactive state and update time fields.
1923 * Enabling the leader of a group effectively enables all
1924 * the group members that aren't explicitly disabled, so we
1925 * have to update their ->tstamp_enabled also.
1926 * Note: this works for group members as well as group leaders
1927 * since the non-leader members' sibling_lists will be empty.
1929 static void __perf_event_mark_enabled(struct perf_event *event)
1931 struct perf_event *sub;
1932 u64 tstamp = perf_event_time(event);
1934 event->state = PERF_EVENT_STATE_INACTIVE;
1935 event->tstamp_enabled = tstamp - event->total_time_enabled;
1936 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1937 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1938 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1943 * Cross CPU call to enable a performance event
1945 static int __perf_event_enable(void *info)
1947 struct perf_event *event = info;
1948 struct perf_event_context *ctx = event->ctx;
1949 struct perf_event *leader = event->group_leader;
1950 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1953 if (WARN_ON_ONCE(!ctx->is_active))
1956 raw_spin_lock(&ctx->lock);
1957 update_context_time(ctx);
1959 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1963 * set current task's cgroup time reference point
1965 perf_cgroup_set_timestamp(current, ctx);
1967 __perf_event_mark_enabled(event);
1969 if (!event_filter_match(event)) {
1970 if (is_cgroup_event(event))
1971 perf_cgroup_defer_enabled(event);
1976 * If the event is in a group and isn't the group leader,
1977 * then don't put it on unless the group is on.
1979 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1982 if (!group_can_go_on(event, cpuctx, 1)) {
1985 if (event == leader)
1986 err = group_sched_in(event, cpuctx, ctx);
1988 err = event_sched_in(event, cpuctx, ctx);
1993 * If this event can't go on and it's part of a
1994 * group, then the whole group has to come off.
1996 if (leader != event) {
1997 group_sched_out(leader, cpuctx, ctx);
1998 perf_cpu_hrtimer_restart(cpuctx);
2000 if (leader->attr.pinned) {
2001 update_group_times(leader);
2002 leader->state = PERF_EVENT_STATE_ERROR;
2007 raw_spin_unlock(&ctx->lock);
2015 * If event->ctx is a cloned context, callers must make sure that
2016 * every task struct that event->ctx->task could possibly point to
2017 * remains valid. This condition is satisfied when called through
2018 * perf_event_for_each_child or perf_event_for_each as described
2019 * for perf_event_disable.
2021 void perf_event_enable(struct perf_event *event)
2023 struct perf_event_context *ctx = event->ctx;
2024 struct task_struct *task = ctx->task;
2028 * Enable the event on the cpu that it's on
2030 cpu_function_call(event->cpu, __perf_event_enable, event);
2034 raw_spin_lock_irq(&ctx->lock);
2035 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2039 * If the event is in error state, clear that first.
2040 * That way, if we see the event in error state below, we
2041 * know that it has gone back into error state, as distinct
2042 * from the task having been scheduled away before the
2043 * cross-call arrived.
2045 if (event->state == PERF_EVENT_STATE_ERROR)
2046 event->state = PERF_EVENT_STATE_OFF;
2049 if (!ctx->is_active) {
2050 __perf_event_mark_enabled(event);
2054 raw_spin_unlock_irq(&ctx->lock);
2056 if (!task_function_call(task, __perf_event_enable, event))
2059 raw_spin_lock_irq(&ctx->lock);
2062 * If the context is active and the event is still off,
2063 * we need to retry the cross-call.
2065 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
2067 * task could have been flipped by a concurrent
2068 * perf_event_context_sched_out()
2075 raw_spin_unlock_irq(&ctx->lock);
2077 EXPORT_SYMBOL_GPL(perf_event_enable);
2079 int perf_event_refresh(struct perf_event *event, int refresh)
2082 * not supported on inherited events
2084 if (event->attr.inherit || !is_sampling_event(event))
2087 atomic_add(refresh, &event->event_limit);
2088 perf_event_enable(event);
2092 EXPORT_SYMBOL_GPL(perf_event_refresh);
2094 static void ctx_sched_out(struct perf_event_context *ctx,
2095 struct perf_cpu_context *cpuctx,
2096 enum event_type_t event_type)
2098 struct perf_event *event;
2099 int is_active = ctx->is_active;
2101 ctx->is_active &= ~event_type;
2102 if (likely(!ctx->nr_events))
2105 update_context_time(ctx);
2106 update_cgrp_time_from_cpuctx(cpuctx);
2107 if (!ctx->nr_active)
2110 perf_pmu_disable(ctx->pmu);
2111 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
2112 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2113 group_sched_out(event, cpuctx, ctx);
2116 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
2117 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
2118 group_sched_out(event, cpuctx, ctx);
2120 perf_pmu_enable(ctx->pmu);
2124 * Test whether two contexts are equivalent, i.e. whether they
2125 * have both been cloned from the same version of the same context
2126 * and they both have the same number of enabled events.
2127 * If the number of enabled events is the same, then the set
2128 * of enabled events should be the same, because these are both
2129 * inherited contexts, therefore we can't access individual events
2130 * in them directly with an fd; we can only enable/disable all
2131 * events via prctl, or enable/disable all events in a family
2132 * via ioctl, which will have the same effect on both contexts.
2134 static int context_equiv(struct perf_event_context *ctx1,
2135 struct perf_event_context *ctx2)
2137 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
2138 && ctx1->parent_gen == ctx2->parent_gen
2139 && !ctx1->pin_count && !ctx2->pin_count;
2142 static void __perf_event_sync_stat(struct perf_event *event,
2143 struct perf_event *next_event)
2147 if (!event->attr.inherit_stat)
2151 * Update the event value, we cannot use perf_event_read()
2152 * because we're in the middle of a context switch and have IRQs
2153 * disabled, which upsets smp_call_function_single(), however
2154 * we know the event must be on the current CPU, therefore we
2155 * don't need to use it.
2157 switch (event->state) {
2158 case PERF_EVENT_STATE_ACTIVE:
2159 event->pmu->read(event);
2162 case PERF_EVENT_STATE_INACTIVE:
2163 update_event_times(event);
2171 * In order to keep per-task stats reliable we need to flip the event
2172 * values when we flip the contexts.
2174 value = local64_read(&next_event->count);
2175 value = local64_xchg(&event->count, value);
2176 local64_set(&next_event->count, value);
2178 swap(event->total_time_enabled, next_event->total_time_enabled);
2179 swap(event->total_time_running, next_event->total_time_running);
2182 * Since we swizzled the values, update the user visible data too.
2184 perf_event_update_userpage(event);
2185 perf_event_update_userpage(next_event);
2188 #define list_next_entry(pos, member) \
2189 list_entry(pos->member.next, typeof(*pos), member)
2191 static void perf_event_sync_stat(struct perf_event_context *ctx,
2192 struct perf_event_context *next_ctx)
2194 struct perf_event *event, *next_event;
2199 update_context_time(ctx);
2201 event = list_first_entry(&ctx->event_list,
2202 struct perf_event, event_entry);
2204 next_event = list_first_entry(&next_ctx->event_list,
2205 struct perf_event, event_entry);
2207 while (&event->event_entry != &ctx->event_list &&
2208 &next_event->event_entry != &next_ctx->event_list) {
2210 __perf_event_sync_stat(event, next_event);
2212 event = list_next_entry(event, event_entry);
2213 next_event = list_next_entry(next_event, event_entry);
2217 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2218 struct task_struct *next)
2220 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
2221 struct perf_event_context *next_ctx;
2222 struct perf_event_context *parent;
2223 struct perf_cpu_context *cpuctx;
2229 cpuctx = __get_cpu_context(ctx);
2230 if (!cpuctx->task_ctx)
2234 parent = rcu_dereference(ctx->parent_ctx);
2235 next_ctx = next->perf_event_ctxp[ctxn];
2236 if (parent && next_ctx &&
2237 rcu_dereference(next_ctx->parent_ctx) == parent) {
2239 * Looks like the two contexts are clones, so we might be
2240 * able to optimize the context switch. We lock both
2241 * contexts and check that they are clones under the
2242 * lock (including re-checking that neither has been
2243 * uncloned in the meantime). It doesn't matter which
2244 * order we take the locks because no other cpu could
2245 * be trying to lock both of these tasks.
2247 raw_spin_lock(&ctx->lock);
2248 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
2249 if (context_equiv(ctx, next_ctx)) {
2251 * XXX do we need a memory barrier of sorts
2252 * wrt to rcu_dereference() of perf_event_ctxp
2254 task->perf_event_ctxp[ctxn] = next_ctx;
2255 next->perf_event_ctxp[ctxn] = ctx;
2257 next_ctx->task = task;
2260 perf_event_sync_stat(ctx, next_ctx);
2262 raw_spin_unlock(&next_ctx->lock);
2263 raw_spin_unlock(&ctx->lock);
2268 raw_spin_lock(&ctx->lock);
2269 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2270 cpuctx->task_ctx = NULL;
2271 raw_spin_unlock(&ctx->lock);
2275 #define for_each_task_context_nr(ctxn) \
2276 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2279 * Called from scheduler to remove the events of the current task,
2280 * with interrupts disabled.
2282 * We stop each event and update the event value in event->count.
2284 * This does not protect us against NMI, but disable()
2285 * sets the disabled bit in the control field of event _before_
2286 * accessing the event control register. If a NMI hits, then it will
2287 * not restart the event.
2289 void __perf_event_task_sched_out(struct task_struct *task,
2290 struct task_struct *next)
2294 for_each_task_context_nr(ctxn)
2295 perf_event_context_sched_out(task, ctxn, next);
2298 * if cgroup events exist on this CPU, then we need
2299 * to check if we have to switch out PMU state.
2300 * cgroup event are system-wide mode only
2302 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2303 perf_cgroup_sched_out(task, next);
2306 static void task_ctx_sched_out(struct perf_event_context *ctx)
2308 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2310 if (!cpuctx->task_ctx)
2313 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2316 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2317 cpuctx->task_ctx = NULL;
2321 * Called with IRQs disabled
2323 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2324 enum event_type_t event_type)
2326 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
2330 ctx_pinned_sched_in(struct perf_event_context *ctx,
2331 struct perf_cpu_context *cpuctx)
2333 struct perf_event *event;
2335 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2336 if (event->state <= PERF_EVENT_STATE_OFF)
2338 if (!event_filter_match(event))
2341 /* may need to reset tstamp_enabled */
2342 if (is_cgroup_event(event))
2343 perf_cgroup_mark_enabled(event, ctx);
2345 if (group_can_go_on(event, cpuctx, 1))
2346 group_sched_in(event, cpuctx, ctx);
2349 * If this pinned group hasn't been scheduled,
2350 * put it in error state.
2352 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2353 update_group_times(event);
2354 event->state = PERF_EVENT_STATE_ERROR;
2360 ctx_flexible_sched_in(struct perf_event_context *ctx,
2361 struct perf_cpu_context *cpuctx)
2363 struct perf_event *event;
2366 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2367 /* Ignore events in OFF or ERROR state */
2368 if (event->state <= PERF_EVENT_STATE_OFF)
2371 * Listen to the 'cpu' scheduling filter constraint
2374 if (!event_filter_match(event))
2377 /* may need to reset tstamp_enabled */
2378 if (is_cgroup_event(event))
2379 perf_cgroup_mark_enabled(event, ctx);
2381 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2382 if (group_sched_in(event, cpuctx, ctx))
2389 ctx_sched_in(struct perf_event_context *ctx,
2390 struct perf_cpu_context *cpuctx,
2391 enum event_type_t event_type,
2392 struct task_struct *task)
2395 int is_active = ctx->is_active;
2397 ctx->is_active |= event_type;
2398 if (likely(!ctx->nr_events))
2402 ctx->timestamp = now;
2403 perf_cgroup_set_timestamp(task, ctx);
2405 * First go through the list and put on any pinned groups
2406 * in order to give them the best chance of going on.
2408 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
2409 ctx_pinned_sched_in(ctx, cpuctx);
2411 /* Then walk through the lower prio flexible groups */
2412 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
2413 ctx_flexible_sched_in(ctx, cpuctx);
2416 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2417 enum event_type_t event_type,
2418 struct task_struct *task)
2420 struct perf_event_context *ctx = &cpuctx->ctx;
2422 ctx_sched_in(ctx, cpuctx, event_type, task);
2425 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2426 struct task_struct *task)
2428 struct perf_cpu_context *cpuctx;
2430 cpuctx = __get_cpu_context(ctx);
2431 if (cpuctx->task_ctx == ctx)
2434 perf_ctx_lock(cpuctx, ctx);
2435 perf_pmu_disable(ctx->pmu);
2437 * We want to keep the following priority order:
2438 * cpu pinned (that don't need to move), task pinned,
2439 * cpu flexible, task flexible.
2441 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2444 cpuctx->task_ctx = ctx;
2446 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2448 perf_pmu_enable(ctx->pmu);
2449 perf_ctx_unlock(cpuctx, ctx);
2452 * Since these rotations are per-cpu, we need to ensure the
2453 * cpu-context we got scheduled on is actually rotating.
2455 perf_pmu_rotate_start(ctx->pmu);
2459 * When sampling the branck stack in system-wide, it may be necessary
2460 * to flush the stack on context switch. This happens when the branch
2461 * stack does not tag its entries with the pid of the current task.
2462 * Otherwise it becomes impossible to associate a branch entry with a
2463 * task. This ambiguity is more likely to appear when the branch stack
2464 * supports priv level filtering and the user sets it to monitor only
2465 * at the user level (which could be a useful measurement in system-wide
2466 * mode). In that case, the risk is high of having a branch stack with
2467 * branch from multiple tasks. Flushing may mean dropping the existing
2468 * entries or stashing them somewhere in the PMU specific code layer.
2470 * This function provides the context switch callback to the lower code
2471 * layer. It is invoked ONLY when there is at least one system-wide context
2472 * with at least one active event using taken branch sampling.
2474 static void perf_branch_stack_sched_in(struct task_struct *prev,
2475 struct task_struct *task)
2477 struct perf_cpu_context *cpuctx;
2479 unsigned long flags;
2481 /* no need to flush branch stack if not changing task */
2485 local_irq_save(flags);
2489 list_for_each_entry_rcu(pmu, &pmus, entry) {
2490 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2493 * check if the context has at least one
2494 * event using PERF_SAMPLE_BRANCH_STACK
2496 if (cpuctx->ctx.nr_branch_stack > 0
2497 && pmu->flush_branch_stack) {
2499 pmu = cpuctx->ctx.pmu;
2501 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2503 perf_pmu_disable(pmu);
2505 pmu->flush_branch_stack();
2507 perf_pmu_enable(pmu);
2509 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2515 local_irq_restore(flags);
2519 * Called from scheduler to add the events of the current task
2520 * with interrupts disabled.
2522 * We restore the event value and then enable it.
2524 * This does not protect us against NMI, but enable()
2525 * sets the enabled bit in the control field of event _before_
2526 * accessing the event control register. If a NMI hits, then it will
2527 * keep the event running.
2529 void __perf_event_task_sched_in(struct task_struct *prev,
2530 struct task_struct *task)
2532 struct perf_event_context *ctx;
2535 for_each_task_context_nr(ctxn) {
2536 ctx = task->perf_event_ctxp[ctxn];
2540 perf_event_context_sched_in(ctx, task);
2543 * if cgroup events exist on this CPU, then we need
2544 * to check if we have to switch in PMU state.
2545 * cgroup event are system-wide mode only
2547 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2548 perf_cgroup_sched_in(prev, task);
2550 /* check for system-wide branch_stack events */
2551 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2552 perf_branch_stack_sched_in(prev, task);
2555 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2557 u64 frequency = event->attr.sample_freq;
2558 u64 sec = NSEC_PER_SEC;
2559 u64 divisor, dividend;
2561 int count_fls, nsec_fls, frequency_fls, sec_fls;
2563 count_fls = fls64(count);
2564 nsec_fls = fls64(nsec);
2565 frequency_fls = fls64(frequency);
2569 * We got @count in @nsec, with a target of sample_freq HZ
2570 * the target period becomes:
2573 * period = -------------------
2574 * @nsec * sample_freq
2579 * Reduce accuracy by one bit such that @a and @b converge
2580 * to a similar magnitude.
2582 #define REDUCE_FLS(a, b) \
2584 if (a##_fls > b##_fls) { \
2594 * Reduce accuracy until either term fits in a u64, then proceed with
2595 * the other, so that finally we can do a u64/u64 division.
2597 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2598 REDUCE_FLS(nsec, frequency);
2599 REDUCE_FLS(sec, count);
2602 if (count_fls + sec_fls > 64) {
2603 divisor = nsec * frequency;
2605 while (count_fls + sec_fls > 64) {
2606 REDUCE_FLS(count, sec);
2610 dividend = count * sec;
2612 dividend = count * sec;
2614 while (nsec_fls + frequency_fls > 64) {
2615 REDUCE_FLS(nsec, frequency);
2619 divisor = nsec * frequency;
2625 return div64_u64(dividend, divisor);
2628 static DEFINE_PER_CPU(int, perf_throttled_count);
2629 static DEFINE_PER_CPU(u64, perf_throttled_seq);
2631 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
2633 struct hw_perf_event *hwc = &event->hw;
2634 s64 period, sample_period;
2637 period = perf_calculate_period(event, nsec, count);
2639 delta = (s64)(period - hwc->sample_period);
2640 delta = (delta + 7) / 8; /* low pass filter */
2642 sample_period = hwc->sample_period + delta;
2647 hwc->sample_period = sample_period;
2649 if (local64_read(&hwc->period_left) > 8*sample_period) {
2651 event->pmu->stop(event, PERF_EF_UPDATE);
2653 local64_set(&hwc->period_left, 0);
2656 event->pmu->start(event, PERF_EF_RELOAD);
2661 * combine freq adjustment with unthrottling to avoid two passes over the
2662 * events. At the same time, make sure, having freq events does not change
2663 * the rate of unthrottling as that would introduce bias.
2665 static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2668 struct perf_event *event;
2669 struct hw_perf_event *hwc;
2670 u64 now, period = TICK_NSEC;
2674 * only need to iterate over all events iff:
2675 * - context have events in frequency mode (needs freq adjust)
2676 * - there are events to unthrottle on this cpu
2678 if (!(ctx->nr_freq || needs_unthr))
2681 raw_spin_lock(&ctx->lock);
2682 perf_pmu_disable(ctx->pmu);
2684 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2685 if (event->state != PERF_EVENT_STATE_ACTIVE)
2688 if (!event_filter_match(event))
2693 if (needs_unthr && hwc->interrupts == MAX_INTERRUPTS) {
2694 hwc->interrupts = 0;
2695 perf_log_throttle(event, 1);
2696 event->pmu->start(event, 0);
2699 if (!event->attr.freq || !event->attr.sample_freq)
2703 * stop the event and update event->count
2705 event->pmu->stop(event, PERF_EF_UPDATE);
2707 now = local64_read(&event->count);
2708 delta = now - hwc->freq_count_stamp;
2709 hwc->freq_count_stamp = now;
2713 * reload only if value has changed
2714 * we have stopped the event so tell that
2715 * to perf_adjust_period() to avoid stopping it
2719 perf_adjust_period(event, period, delta, false);
2721 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
2724 perf_pmu_enable(ctx->pmu);
2725 raw_spin_unlock(&ctx->lock);
2729 * Round-robin a context's events:
2731 static void rotate_ctx(struct perf_event_context *ctx)
2734 * Rotate the first entry last of non-pinned groups. Rotation might be
2735 * disabled by the inheritance code.
2737 if (!ctx->rotate_disable)
2738 list_rotate_left(&ctx->flexible_groups);
2742 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2743 * because they're strictly cpu affine and rotate_start is called with IRQs
2744 * disabled, while rotate_context is called from IRQ context.
2746 static int perf_rotate_context(struct perf_cpu_context *cpuctx)
2748 struct perf_event_context *ctx = NULL;
2749 int rotate = 0, remove = 1;
2751 if (cpuctx->ctx.nr_events) {
2753 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2757 ctx = cpuctx->task_ctx;
2758 if (ctx && ctx->nr_events) {
2760 if (ctx->nr_events != ctx->nr_active)
2767 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2768 perf_pmu_disable(cpuctx->ctx.pmu);
2770 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2772 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
2774 rotate_ctx(&cpuctx->ctx);
2778 perf_event_sched_in(cpuctx, ctx, current);
2780 perf_pmu_enable(cpuctx->ctx.pmu);
2781 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2784 list_del_init(&cpuctx->rotation_list);
2789 #ifdef CONFIG_NO_HZ_FULL
2790 bool perf_event_can_stop_tick(void)
2792 if (list_empty(&__get_cpu_var(rotation_list)))
2799 void perf_event_task_tick(void)
2801 struct list_head *head = &__get_cpu_var(rotation_list);
2802 struct perf_cpu_context *cpuctx, *tmp;
2803 struct perf_event_context *ctx;
2806 WARN_ON(!irqs_disabled());
2808 __this_cpu_inc(perf_throttled_seq);
2809 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2811 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2813 perf_adjust_freq_unthr_context(ctx, throttled);
2815 ctx = cpuctx->task_ctx;
2817 perf_adjust_freq_unthr_context(ctx, throttled);
2821 static int event_enable_on_exec(struct perf_event *event,
2822 struct perf_event_context *ctx)
2824 if (!event->attr.enable_on_exec)
2827 event->attr.enable_on_exec = 0;
2828 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2831 __perf_event_mark_enabled(event);
2837 * Enable all of a task's events that have been marked enable-on-exec.
2838 * This expects task == current.
2840 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2842 struct perf_event *event;
2843 unsigned long flags;
2847 local_irq_save(flags);
2848 if (!ctx || !ctx->nr_events)
2852 * We must ctxsw out cgroup events to avoid conflict
2853 * when invoking perf_task_event_sched_in() later on
2854 * in this function. Otherwise we end up trying to
2855 * ctxswin cgroup events which are already scheduled
2858 perf_cgroup_sched_out(current, NULL);
2860 raw_spin_lock(&ctx->lock);
2861 task_ctx_sched_out(ctx);
2863 list_for_each_entry(event, &ctx->event_list, event_entry) {
2864 ret = event_enable_on_exec(event, ctx);
2870 * Unclone this context if we enabled any event.
2875 raw_spin_unlock(&ctx->lock);
2878 * Also calls ctxswin for cgroup events, if any:
2880 perf_event_context_sched_in(ctx, ctx->task);
2882 local_irq_restore(flags);
2886 * Cross CPU call to read the hardware event
2888 static void __perf_event_read(void *info)
2890 struct perf_event *event = info;
2891 struct perf_event_context *ctx = event->ctx;
2892 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2895 * If this is a task context, we need to check whether it is
2896 * the current task context of this cpu. If not it has been
2897 * scheduled out before the smp call arrived. In that case
2898 * event->count would have been updated to a recent sample
2899 * when the event was scheduled out.
2901 if (ctx->task && cpuctx->task_ctx != ctx)
2904 raw_spin_lock(&ctx->lock);
2905 if (ctx->is_active) {
2906 update_context_time(ctx);
2907 update_cgrp_time_from_event(event);
2909 update_event_times(event);
2910 if (event->state == PERF_EVENT_STATE_ACTIVE)
2911 event->pmu->read(event);
2912 raw_spin_unlock(&ctx->lock);
2915 static inline u64 perf_event_count(struct perf_event *event)
2917 return local64_read(&event->count) + atomic64_read(&event->child_count);
2920 static u64 perf_event_read(struct perf_event *event)
2923 * If event is enabled and currently active on a CPU, update the
2924 * value in the event structure:
2926 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2927 smp_call_function_single(event->oncpu,
2928 __perf_event_read, event, 1);
2929 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2930 struct perf_event_context *ctx = event->ctx;
2931 unsigned long flags;
2933 raw_spin_lock_irqsave(&ctx->lock, flags);
2935 * may read while context is not active
2936 * (e.g., thread is blocked), in that case
2937 * we cannot update context time
2939 if (ctx->is_active) {
2940 update_context_time(ctx);
2941 update_cgrp_time_from_event(event);
2943 update_event_times(event);
2944 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2947 return perf_event_count(event);
2951 * Initialize the perf_event context in a task_struct:
2953 static void __perf_event_init_context(struct perf_event_context *ctx)
2955 raw_spin_lock_init(&ctx->lock);
2956 mutex_init(&ctx->mutex);
2957 INIT_LIST_HEAD(&ctx->pinned_groups);
2958 INIT_LIST_HEAD(&ctx->flexible_groups);
2959 INIT_LIST_HEAD(&ctx->event_list);
2960 atomic_set(&ctx->refcount, 1);
2963 static struct perf_event_context *
2964 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2966 struct perf_event_context *ctx;
2968 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2972 __perf_event_init_context(ctx);
2975 get_task_struct(task);
2982 static struct task_struct *
2983 find_lively_task_by_vpid(pid_t vpid)
2985 struct task_struct *task;
2992 task = find_task_by_vpid(vpid);
2994 get_task_struct(task);
2998 return ERR_PTR(-ESRCH);
3000 /* Reuse ptrace permission checks for now. */
3002 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3007 put_task_struct(task);
3008 return ERR_PTR(err);
3013 * Returns a matching context with refcount and pincount.
3015 static struct perf_event_context *
3016 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
3018 struct perf_event_context *ctx;
3019 struct perf_cpu_context *cpuctx;
3020 unsigned long flags;
3024 /* Must be root to operate on a CPU event: */
3025 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3026 return ERR_PTR(-EACCES);
3029 * We could be clever and allow to attach a event to an
3030 * offline CPU and activate it when the CPU comes up, but
3033 if (!cpu_online(cpu))
3034 return ERR_PTR(-ENODEV);
3036 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
3045 ctxn = pmu->task_ctx_nr;
3050 ctx = perf_lock_task_context(task, ctxn, &flags);
3054 raw_spin_unlock_irqrestore(&ctx->lock, flags);
3056 ctx = alloc_perf_context(pmu, task);
3062 mutex_lock(&task->perf_event_mutex);
3064 * If it has already passed perf_event_exit_task().
3065 * we must see PF_EXITING, it takes this mutex too.
3067 if (task->flags & PF_EXITING)
3069 else if (task->perf_event_ctxp[ctxn])
3074 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
3076 mutex_unlock(&task->perf_event_mutex);
3078 if (unlikely(err)) {
3090 return ERR_PTR(err);
3093 static void perf_event_free_filter(struct perf_event *event);
3095 static void free_event_rcu(struct rcu_head *head)
3097 struct perf_event *event;
3099 event = container_of(head, struct perf_event, rcu_head);
3101 put_pid_ns(event->ns);
3102 perf_event_free_filter(event);
3106 static void ring_buffer_put(struct ring_buffer *rb);
3107 static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb);
3109 static void free_event(struct perf_event *event)
3111 irq_work_sync(&event->pending);
3113 if (!event->parent) {
3114 if (event->attach_state & PERF_ATTACH_TASK)
3115 static_key_slow_dec_deferred(&perf_sched_events);
3116 if (event->attr.mmap || event->attr.mmap_data)
3117 atomic_dec(&nr_mmap_events);
3118 if (event->attr.comm)
3119 atomic_dec(&nr_comm_events);
3120 if (event->attr.task)
3121 atomic_dec(&nr_task_events);
3122 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3123 put_callchain_buffers();
3124 if (is_cgroup_event(event)) {
3125 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
3126 static_key_slow_dec_deferred(&perf_sched_events);
3129 if (has_branch_stack(event)) {
3130 static_key_slow_dec_deferred(&perf_sched_events);
3131 /* is system-wide event */
3132 if (!(event->attach_state & PERF_ATTACH_TASK)) {
3133 atomic_dec(&per_cpu(perf_branch_stack_events,
3140 struct ring_buffer *rb;
3143 * Can happen when we close an event with re-directed output.
3145 * Since we have a 0 refcount, perf_mmap_close() will skip
3146 * over us; possibly making our ring_buffer_put() the last.
3148 mutex_lock(&event->mmap_mutex);
3151 rcu_assign_pointer(event->rb, NULL);
3152 ring_buffer_detach(event, rb);
3153 ring_buffer_put(rb); /* could be last */
3155 mutex_unlock(&event->mmap_mutex);
3158 if (is_cgroup_event(event))
3159 perf_detach_cgroup(event);
3162 event->destroy(event);
3165 put_ctx(event->ctx);
3167 call_rcu(&event->rcu_head, free_event_rcu);
3170 int perf_event_release_kernel(struct perf_event *event)
3172 struct perf_event_context *ctx = event->ctx;
3174 WARN_ON_ONCE(ctx->parent_ctx);
3176 * There are two ways this annotation is useful:
3178 * 1) there is a lock recursion from perf_event_exit_task
3179 * see the comment there.
3181 * 2) there is a lock-inversion with mmap_sem through
3182 * perf_event_read_group(), which takes faults while
3183 * holding ctx->mutex, however this is called after
3184 * the last filedesc died, so there is no possibility
3185 * to trigger the AB-BA case.
3187 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
3188 raw_spin_lock_irq(&ctx->lock);
3189 perf_group_detach(event);
3190 raw_spin_unlock_irq(&ctx->lock);
3191 perf_remove_from_context(event);
3192 mutex_unlock(&ctx->mutex);
3198 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3201 * Called when the last reference to the file is gone.
3203 static void put_event(struct perf_event *event)
3205 struct task_struct *owner;
3207 if (!atomic_long_dec_and_test(&event->refcount))
3211 owner = ACCESS_ONCE(event->owner);
3213 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3214 * !owner it means the list deletion is complete and we can indeed
3215 * free this event, otherwise we need to serialize on
3216 * owner->perf_event_mutex.
3218 smp_read_barrier_depends();
3221 * Since delayed_put_task_struct() also drops the last
3222 * task reference we can safely take a new reference
3223 * while holding the rcu_read_lock().
3225 get_task_struct(owner);
3230 mutex_lock(&owner->perf_event_mutex);
3232 * We have to re-check the event->owner field, if it is cleared
3233 * we raced with perf_event_exit_task(), acquiring the mutex
3234 * ensured they're done, and we can proceed with freeing the
3238 list_del_init(&event->owner_entry);
3239 mutex_unlock(&owner->perf_event_mutex);
3240 put_task_struct(owner);
3243 perf_event_release_kernel(event);
3246 static int perf_release(struct inode *inode, struct file *file)
3248 put_event(file->private_data);
3252 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
3254 struct perf_event *child;
3260 mutex_lock(&event->child_mutex);
3261 total += perf_event_read(event);
3262 *enabled += event->total_time_enabled +
3263 atomic64_read(&event->child_total_time_enabled);
3264 *running += event->total_time_running +
3265 atomic64_read(&event->child_total_time_running);
3267 list_for_each_entry(child, &event->child_list, child_list) {
3268 total += perf_event_read(child);
3269 *enabled += child->total_time_enabled;
3270 *running += child->total_time_running;
3272 mutex_unlock(&event->child_mutex);
3276 EXPORT_SYMBOL_GPL(perf_event_read_value);
3278 static int perf_event_read_group(struct perf_event *event,
3279 u64 read_format, char __user *buf)
3281 struct perf_event *leader = event->group_leader, *sub;
3282 int n = 0, size = 0, ret = -EFAULT;
3283 struct perf_event_context *ctx = leader->ctx;
3285 u64 count, enabled, running;
3287 mutex_lock(&ctx->mutex);
3288 count = perf_event_read_value(leader, &enabled, &running);
3290 values[n++] = 1 + leader->nr_siblings;
3291 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3292 values[n++] = enabled;
3293 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3294 values[n++] = running;
3295 values[n++] = count;
3296 if (read_format & PERF_FORMAT_ID)
3297 values[n++] = primary_event_id(leader);
3299 size = n * sizeof(u64);
3301 if (copy_to_user(buf, values, size))
3306 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3309 values[n++] = perf_event_read_value(sub, &enabled, &running);
3310 if (read_format & PERF_FORMAT_ID)
3311 values[n++] = primary_event_id(sub);
3313 size = n * sizeof(u64);
3315 if (copy_to_user(buf + ret, values, size)) {
3323 mutex_unlock(&ctx->mutex);
3328 static int perf_event_read_one(struct perf_event *event,
3329 u64 read_format, char __user *buf)
3331 u64 enabled, running;
3335 values[n++] = perf_event_read_value(event, &enabled, &running);
3336 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3337 values[n++] = enabled;
3338 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3339 values[n++] = running;
3340 if (read_format & PERF_FORMAT_ID)
3341 values[n++] = primary_event_id(event);
3343 if (copy_to_user(buf, values, n * sizeof(u64)))
3346 return n * sizeof(u64);
3350 * Read the performance event - simple non blocking version for now
3353 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3355 u64 read_format = event->attr.read_format;
3359 * Return end-of-file for a read on a event that is in
3360 * error state (i.e. because it was pinned but it couldn't be
3361 * scheduled on to the CPU at some point).
3363 if (event->state == PERF_EVENT_STATE_ERROR)
3366 if (count < event->read_size)
3369 WARN_ON_ONCE(event->ctx->parent_ctx);
3370 if (read_format & PERF_FORMAT_GROUP)
3371 ret = perf_event_read_group(event, read_format, buf);
3373 ret = perf_event_read_one(event, read_format, buf);
3379 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3381 struct perf_event *event = file->private_data;
3383 return perf_read_hw(event, buf, count);
3386 static unsigned int perf_poll(struct file *file, poll_table *wait)
3388 struct perf_event *event = file->private_data;
3389 struct ring_buffer *rb;
3390 unsigned int events = POLL_HUP;
3393 * Pin the event->rb by taking event->mmap_mutex; otherwise
3394 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
3396 mutex_lock(&event->mmap_mutex);
3399 events = atomic_xchg(&rb->poll, 0);
3400 mutex_unlock(&event->mmap_mutex);
3402 poll_wait(file, &event->waitq, wait);
3407 static void perf_event_reset(struct perf_event *event)
3409 (void)perf_event_read(event);
3410 local64_set(&event->count, 0);
3411 perf_event_update_userpage(event);
3415 * Holding the top-level event's child_mutex means that any
3416 * descendant process that has inherited this event will block
3417 * in sync_child_event if it goes to exit, thus satisfying the
3418 * task existence requirements of perf_event_enable/disable.
3420 static void perf_event_for_each_child(struct perf_event *event,
3421 void (*func)(struct perf_event *))
3423 struct perf_event *child;
3425 WARN_ON_ONCE(event->ctx->parent_ctx);
3426 mutex_lock(&event->child_mutex);
3428 list_for_each_entry(child, &event->child_list, child_list)
3430 mutex_unlock(&event->child_mutex);
3433 static void perf_event_for_each(struct perf_event *event,
3434 void (*func)(struct perf_event *))
3436 struct perf_event_context *ctx = event->ctx;
3437 struct perf_event *sibling;
3439 WARN_ON_ONCE(ctx->parent_ctx);
3440 mutex_lock(&ctx->mutex);
3441 event = event->group_leader;
3443 perf_event_for_each_child(event, func);
3444 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3445 perf_event_for_each_child(sibling, func);
3446 mutex_unlock(&ctx->mutex);
3449 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3451 struct perf_event_context *ctx = event->ctx;
3455 if (!is_sampling_event(event))
3458 if (copy_from_user(&value, arg, sizeof(value)))
3464 raw_spin_lock_irq(&ctx->lock);
3465 if (event->attr.freq) {
3466 if (value > sysctl_perf_event_sample_rate) {
3471 event->attr.sample_freq = value;
3473 event->attr.sample_period = value;
3474 event->hw.sample_period = value;
3477 raw_spin_unlock_irq(&ctx->lock);
3482 static const struct file_operations perf_fops;
3484 static inline int perf_fget_light(int fd, struct fd *p)
3486 struct fd f = fdget(fd);
3490 if (f.file->f_op != &perf_fops) {
3498 static int perf_event_set_output(struct perf_event *event,
3499 struct perf_event *output_event);
3500 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3502 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3504 struct perf_event *event = file->private_data;
3505 void (*func)(struct perf_event *);
3509 case PERF_EVENT_IOC_ENABLE:
3510 func = perf_event_enable;
3512 case PERF_EVENT_IOC_DISABLE:
3513 func = perf_event_disable;
3515 case PERF_EVENT_IOC_RESET:
3516 func = perf_event_reset;
3519 case PERF_EVENT_IOC_REFRESH:
3520 return perf_event_refresh(event, arg);
3522 case PERF_EVENT_IOC_PERIOD:
3523 return perf_event_period(event, (u64 __user *)arg);
3525 case PERF_EVENT_IOC_SET_OUTPUT:
3529 struct perf_event *output_event;
3531 ret = perf_fget_light(arg, &output);
3534 output_event = output.file->private_data;
3535 ret = perf_event_set_output(event, output_event);
3538 ret = perf_event_set_output(event, NULL);
3543 case PERF_EVENT_IOC_SET_FILTER:
3544 return perf_event_set_filter(event, (void __user *)arg);
3550 if (flags & PERF_IOC_FLAG_GROUP)
3551 perf_event_for_each(event, func);
3553 perf_event_for_each_child(event, func);
3558 int perf_event_task_enable(void)
3560 struct perf_event *event;
3562 mutex_lock(¤t->perf_event_mutex);
3563 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3564 perf_event_for_each_child(event, perf_event_enable);
3565 mutex_unlock(¤t->perf_event_mutex);
3570 int perf_event_task_disable(void)
3572 struct perf_event *event;
3574 mutex_lock(¤t->perf_event_mutex);
3575 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3576 perf_event_for_each_child(event, perf_event_disable);
3577 mutex_unlock(¤t->perf_event_mutex);
3582 static int perf_event_index(struct perf_event *event)
3584 if (event->hw.state & PERF_HES_STOPPED)
3587 if (event->state != PERF_EVENT_STATE_ACTIVE)
3590 return event->pmu->event_idx(event);
3593 static void calc_timer_values(struct perf_event *event,
3600 *now = perf_clock();
3601 ctx_time = event->shadow_ctx_time + *now;
3602 *enabled = ctx_time - event->tstamp_enabled;
3603 *running = ctx_time - event->tstamp_running;
3606 void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
3611 * Callers need to ensure there can be no nesting of this function, otherwise
3612 * the seqlock logic goes bad. We can not serialize this because the arch
3613 * code calls this from NMI context.
3615 void perf_event_update_userpage(struct perf_event *event)
3617 struct perf_event_mmap_page *userpg;
3618 struct ring_buffer *rb;
3619 u64 enabled, running, now;
3623 * compute total_time_enabled, total_time_running
3624 * based on snapshot values taken when the event
3625 * was last scheduled in.
3627 * we cannot simply called update_context_time()
3628 * because of locking issue as we can be called in
3631 calc_timer_values(event, &now, &enabled, &running);
3632 rb = rcu_dereference(event->rb);
3636 userpg = rb->user_page;
3639 * Disable preemption so as to not let the corresponding user-space
3640 * spin too long if we get preempted.
3645 userpg->index = perf_event_index(event);
3646 userpg->offset = perf_event_count(event);
3648 userpg->offset -= local64_read(&event->hw.prev_count);
3650 userpg->time_enabled = enabled +
3651 atomic64_read(&event->child_total_time_enabled);
3653 userpg->time_running = running +
3654 atomic64_read(&event->child_total_time_running);
3656 arch_perf_update_userpage(userpg, now);
3665 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3667 struct perf_event *event = vma->vm_file->private_data;
3668 struct ring_buffer *rb;
3669 int ret = VM_FAULT_SIGBUS;
3671 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3672 if (vmf->pgoff == 0)
3678 rb = rcu_dereference(event->rb);
3682 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3685 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
3689 get_page(vmf->page);
3690 vmf->page->mapping = vma->vm_file->f_mapping;
3691 vmf->page->index = vmf->pgoff;
3700 static void ring_buffer_attach(struct perf_event *event,
3701 struct ring_buffer *rb)
3703 unsigned long flags;
3705 if (!list_empty(&event->rb_entry))
3708 spin_lock_irqsave(&rb->event_lock, flags);
3709 if (list_empty(&event->rb_entry))
3710 list_add(&event->rb_entry, &rb->event_list);
3711 spin_unlock_irqrestore(&rb->event_lock, flags);
3714 static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb)
3716 unsigned long flags;
3718 if (list_empty(&event->rb_entry))
3721 spin_lock_irqsave(&rb->event_lock, flags);
3722 list_del_init(&event->rb_entry);
3723 wake_up_all(&event->waitq);
3724 spin_unlock_irqrestore(&rb->event_lock, flags);
3727 static void ring_buffer_wakeup(struct perf_event *event)
3729 struct ring_buffer *rb;
3732 rb = rcu_dereference(event->rb);
3734 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3735 wake_up_all(&event->waitq);
3740 static void rb_free_rcu(struct rcu_head *rcu_head)
3742 struct ring_buffer *rb;
3744 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3748 static struct ring_buffer *ring_buffer_get(struct perf_event *event)
3750 struct ring_buffer *rb;
3753 rb = rcu_dereference(event->rb);
3755 if (!atomic_inc_not_zero(&rb->refcount))
3763 static void ring_buffer_put(struct ring_buffer *rb)
3765 if (!atomic_dec_and_test(&rb->refcount))
3768 WARN_ON_ONCE(!list_empty(&rb->event_list));
3770 call_rcu(&rb->rcu_head, rb_free_rcu);
3773 static void perf_mmap_open(struct vm_area_struct *vma)
3775 struct perf_event *event = vma->vm_file->private_data;
3777 atomic_inc(&event->mmap_count);
3778 atomic_inc(&event->rb->mmap_count);
3782 * A buffer can be mmap()ed multiple times; either directly through the same
3783 * event, or through other events by use of perf_event_set_output().
3785 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3786 * the buffer here, where we still have a VM context. This means we need
3787 * to detach all events redirecting to us.
3789 static void perf_mmap_close(struct vm_area_struct *vma)
3791 struct perf_event *event = vma->vm_file->private_data;
3793 struct ring_buffer *rb = event->rb;
3794 struct user_struct *mmap_user = rb->mmap_user;
3795 int mmap_locked = rb->mmap_locked;
3796 unsigned long size = perf_data_size(rb);
3798 atomic_dec(&rb->mmap_count);
3800 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
3803 /* Detach current event from the buffer. */
3804 rcu_assign_pointer(event->rb, NULL);
3805 ring_buffer_detach(event, rb);
3806 mutex_unlock(&event->mmap_mutex);
3808 /* If there's still other mmap()s of this buffer, we're done. */
3809 if (atomic_read(&rb->mmap_count)) {
3810 ring_buffer_put(rb); /* can't be last */
3815 * No other mmap()s, detach from all other events that might redirect
3816 * into the now unreachable buffer. Somewhat complicated by the
3817 * fact that rb::event_lock otherwise nests inside mmap_mutex.
3821 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
3822 if (!atomic_long_inc_not_zero(&event->refcount)) {
3824 * This event is en-route to free_event() which will
3825 * detach it and remove it from the list.
3831 mutex_lock(&event->mmap_mutex);
3833 * Check we didn't race with perf_event_set_output() which can
3834 * swizzle the rb from under us while we were waiting to
3835 * acquire mmap_mutex.
3837 * If we find a different rb; ignore this event, a next
3838 * iteration will no longer find it on the list. We have to
3839 * still restart the iteration to make sure we're not now
3840 * iterating the wrong list.
3842 if (event->rb == rb) {
3843 rcu_assign_pointer(event->rb, NULL);
3844 ring_buffer_detach(event, rb);
3845 ring_buffer_put(rb); /* can't be last, we still have one */
3847 mutex_unlock(&event->mmap_mutex);
3851 * Restart the iteration; either we're on the wrong list or
3852 * destroyed its integrity by doing a deletion.
3859 * It could be there's still a few 0-ref events on the list; they'll
3860 * get cleaned up by free_event() -- they'll also still have their
3861 * ref on the rb and will free it whenever they are done with it.
3863 * Aside from that, this buffer is 'fully' detached and unmapped,
3864 * undo the VM accounting.
3867 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
3868 vma->vm_mm->pinned_vm -= mmap_locked;
3869 free_uid(mmap_user);
3871 ring_buffer_put(rb); /* could be last */
3874 static const struct vm_operations_struct perf_mmap_vmops = {
3875 .open = perf_mmap_open,
3876 .close = perf_mmap_close,
3877 .fault = perf_mmap_fault,
3878 .page_mkwrite = perf_mmap_fault,
3881 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3883 struct perf_event *event = file->private_data;
3884 unsigned long user_locked, user_lock_limit;
3885 struct user_struct *user = current_user();
3886 unsigned long locked, lock_limit;
3887 struct ring_buffer *rb;
3888 unsigned long vma_size;
3889 unsigned long nr_pages;
3890 long user_extra, extra;
3891 int ret = 0, flags = 0;
3894 * Don't allow mmap() of inherited per-task counters. This would
3895 * create a performance issue due to all children writing to the
3898 if (event->cpu == -1 && event->attr.inherit)
3901 if (!(vma->vm_flags & VM_SHARED))
3904 vma_size = vma->vm_end - vma->vm_start;
3905 nr_pages = (vma_size / PAGE_SIZE) - 1;
3908 * If we have rb pages ensure they're a power-of-two number, so we
3909 * can do bitmasks instead of modulo.
3911 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3914 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3917 if (vma->vm_pgoff != 0)
3920 WARN_ON_ONCE(event->ctx->parent_ctx);
3922 mutex_lock(&event->mmap_mutex);
3924 if (event->rb->nr_pages != nr_pages) {
3929 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
3931 * Raced against perf_mmap_close() through
3932 * perf_event_set_output(). Try again, hope for better
3935 mutex_unlock(&event->mmap_mutex);
3942 user_extra = nr_pages + 1;
3943 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3946 * Increase the limit linearly with more CPUs:
3948 user_lock_limit *= num_online_cpus();
3950 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3953 if (user_locked > user_lock_limit)
3954 extra = user_locked - user_lock_limit;
3956 lock_limit = rlimit(RLIMIT_MEMLOCK);
3957 lock_limit >>= PAGE_SHIFT;
3958 locked = vma->vm_mm->pinned_vm + extra;
3960 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3961 !capable(CAP_IPC_LOCK)) {
3968 if (vma->vm_flags & VM_WRITE)
3969 flags |= RING_BUFFER_WRITABLE;
3971 rb = rb_alloc(nr_pages,
3972 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3980 atomic_set(&rb->mmap_count, 1);
3981 rb->mmap_locked = extra;
3982 rb->mmap_user = get_current_user();
3984 atomic_long_add(user_extra, &user->locked_vm);
3985 vma->vm_mm->pinned_vm += extra;
3987 ring_buffer_attach(event, rb);
3988 rcu_assign_pointer(event->rb, rb);
3990 perf_event_update_userpage(event);
3994 atomic_inc(&event->mmap_count);
3995 mutex_unlock(&event->mmap_mutex);
3998 * Since pinned accounting is per vm we cannot allow fork() to copy our
4001 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
4002 vma->vm_ops = &perf_mmap_vmops;
4007 static int perf_fasync(int fd, struct file *filp, int on)
4009 struct inode *inode = file_inode(filp);
4010 struct perf_event *event = filp->private_data;
4013 mutex_lock(&inode->i_mutex);
4014 retval = fasync_helper(fd, filp, on, &event->fasync);
4015 mutex_unlock(&inode->i_mutex);
4023 static const struct file_operations perf_fops = {
4024 .llseek = no_llseek,
4025 .release = perf_release,
4028 .unlocked_ioctl = perf_ioctl,
4029 .compat_ioctl = perf_ioctl,
4031 .fasync = perf_fasync,
4037 * If there's data, ensure we set the poll() state and publish everything
4038 * to user-space before waking everybody up.
4041 void perf_event_wakeup(struct perf_event *event)
4043 ring_buffer_wakeup(event);
4045 if (event->pending_kill) {
4046 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
4047 event->pending_kill = 0;
4051 static void perf_pending_event(struct irq_work *entry)
4053 struct perf_event *event = container_of(entry,
4054 struct perf_event, pending);
4056 if (event->pending_disable) {
4057 event->pending_disable = 0;
4058 __perf_event_disable(event);
4061 if (event->pending_wakeup) {
4062 event->pending_wakeup = 0;
4063 perf_event_wakeup(event);
4068 * We assume there is only KVM supporting the callbacks.
4069 * Later on, we might change it to a list if there is
4070 * another virtualization implementation supporting the callbacks.
4072 struct perf_guest_info_callbacks *perf_guest_cbs;
4074 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4076 perf_guest_cbs = cbs;
4079 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4081 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4083 perf_guest_cbs = NULL;
4086 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4089 perf_output_sample_regs(struct perf_output_handle *handle,
4090 struct pt_regs *regs, u64 mask)
4094 for_each_set_bit(bit, (const unsigned long *) &mask,
4095 sizeof(mask) * BITS_PER_BYTE) {
4098 val = perf_reg_value(regs, bit);
4099 perf_output_put(handle, val);
4103 static void perf_sample_regs_user(struct perf_regs_user *regs_user,
4104 struct pt_regs *regs)
4106 if (!user_mode(regs)) {
4108 regs = task_pt_regs(current);
4114 regs_user->regs = regs;
4115 regs_user->abi = perf_reg_abi(current);
4120 * Get remaining task size from user stack pointer.
4122 * It'd be better to take stack vma map and limit this more
4123 * precisly, but there's no way to get it safely under interrupt,
4124 * so using TASK_SIZE as limit.
4126 static u64 perf_ustack_task_size(struct pt_regs *regs)
4128 unsigned long addr = perf_user_stack_pointer(regs);
4130 if (!addr || addr >= TASK_SIZE)
4133 return TASK_SIZE - addr;
4137 perf_sample_ustack_size(u16 stack_size, u16 header_size,
4138 struct pt_regs *regs)
4142 /* No regs, no stack pointer, no dump. */
4147 * Check if we fit in with the requested stack size into the:
4149 * If we don't, we limit the size to the TASK_SIZE.
4151 * - remaining sample size
4152 * If we don't, we customize the stack size to
4153 * fit in to the remaining sample size.
4156 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
4157 stack_size = min(stack_size, (u16) task_size);
4159 /* Current header size plus static size and dynamic size. */
4160 header_size += 2 * sizeof(u64);
4162 /* Do we fit in with the current stack dump size? */
4163 if ((u16) (header_size + stack_size) < header_size) {
4165 * If we overflow the maximum size for the sample,
4166 * we customize the stack dump size to fit in.
4168 stack_size = USHRT_MAX - header_size - sizeof(u64);
4169 stack_size = round_up(stack_size, sizeof(u64));
4176 perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4177 struct pt_regs *regs)
4179 /* Case of a kernel thread, nothing to dump */
4182 perf_output_put(handle, size);
4191 * - the size requested by user or the best one we can fit
4192 * in to the sample max size
4194 * - user stack dump data
4196 * - the actual dumped size
4200 perf_output_put(handle, dump_size);
4203 sp = perf_user_stack_pointer(regs);
4204 rem = __output_copy_user(handle, (void *) sp, dump_size);
4205 dyn_size = dump_size - rem;
4207 perf_output_skip(handle, rem);
4210 perf_output_put(handle, dyn_size);
4214 static void __perf_event_header__init_id(struct perf_event_header *header,
4215 struct perf_sample_data *data,
4216 struct perf_event *event)
4218 u64 sample_type = event->attr.sample_type;
4220 data->type = sample_type;
4221 header->size += event->id_header_size;
4223 if (sample_type & PERF_SAMPLE_TID) {
4224 /* namespace issues */
4225 data->tid_entry.pid = perf_event_pid(event, current);
4226 data->tid_entry.tid = perf_event_tid(event, current);
4229 if (sample_type & PERF_SAMPLE_TIME)
4230 data->time = perf_clock();
4232 if (sample_type & PERF_SAMPLE_ID)
4233 data->id = primary_event_id(event);
4235 if (sample_type & PERF_SAMPLE_STREAM_ID)
4236 data->stream_id = event->id;
4238 if (sample_type & PERF_SAMPLE_CPU) {
4239 data->cpu_entry.cpu = raw_smp_processor_id();
4240 data->cpu_entry.reserved = 0;
4244 void perf_event_header__init_id(struct perf_event_header *header,
4245 struct perf_sample_data *data,
4246 struct perf_event *event)
4248 if (event->attr.sample_id_all)
4249 __perf_event_header__init_id(header, data, event);
4252 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4253 struct perf_sample_data *data)
4255 u64 sample_type = data->type;
4257 if (sample_type & PERF_SAMPLE_TID)
4258 perf_output_put(handle, data->tid_entry);
4260 if (sample_type & PERF_SAMPLE_TIME)
4261 perf_output_put(handle, data->time);
4263 if (sample_type & PERF_SAMPLE_ID)
4264 perf_output_put(handle, data->id);
4266 if (sample_type & PERF_SAMPLE_STREAM_ID)
4267 perf_output_put(handle, data->stream_id);
4269 if (sample_type & PERF_SAMPLE_CPU)
4270 perf_output_put(handle, data->cpu_entry);
4273 void perf_event__output_id_sample(struct perf_event *event,
4274 struct perf_output_handle *handle,
4275 struct perf_sample_data *sample)
4277 if (event->attr.sample_id_all)
4278 __perf_event__output_id_sample(handle, sample);
4281 static void perf_output_read_one(struct perf_output_handle *handle,
4282 struct perf_event *event,
4283 u64 enabled, u64 running)
4285 u64 read_format = event->attr.read_format;
4289 values[n++] = perf_event_count(event);
4290 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4291 values[n++] = enabled +
4292 atomic64_read(&event->child_total_time_enabled);
4294 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4295 values[n++] = running +
4296 atomic64_read(&event->child_total_time_running);
4298 if (read_format & PERF_FORMAT_ID)
4299 values[n++] = primary_event_id(event);
4301 __output_copy(handle, values, n * sizeof(u64));
4305 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4307 static void perf_output_read_group(struct perf_output_handle *handle,
4308 struct perf_event *event,
4309 u64 enabled, u64 running)
4311 struct perf_event *leader = event->group_leader, *sub;
4312 u64 read_format = event->attr.read_format;
4316 values[n++] = 1 + leader->nr_siblings;
4318 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4319 values[n++] = enabled;
4321 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4322 values[n++] = running;
4324 if (leader != event)
4325 leader->pmu->read(leader);
4327 values[n++] = perf_event_count(leader);
4328 if (read_format & PERF_FORMAT_ID)
4329 values[n++] = primary_event_id(leader);
4331 __output_copy(handle, values, n * sizeof(u64));
4333 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4337 sub->pmu->read(sub);
4339 values[n++] = perf_event_count(sub);
4340 if (read_format & PERF_FORMAT_ID)
4341 values[n++] = primary_event_id(sub);
4343 __output_copy(handle, values, n * sizeof(u64));
4347 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4348 PERF_FORMAT_TOTAL_TIME_RUNNING)
4350 static void perf_output_read(struct perf_output_handle *handle,
4351 struct perf_event *event)
4353 u64 enabled = 0, running = 0, now;
4354 u64 read_format = event->attr.read_format;
4357 * compute total_time_enabled, total_time_running
4358 * based on snapshot values taken when the event
4359 * was last scheduled in.
4361 * we cannot simply called update_context_time()
4362 * because of locking issue as we are called in
4365 if (read_format & PERF_FORMAT_TOTAL_TIMES)
4366 calc_timer_values(event, &now, &enabled, &running);
4368 if (event->attr.read_format & PERF_FORMAT_GROUP)
4369 perf_output_read_group(handle, event, enabled, running);
4371 perf_output_read_one(handle, event, enabled, running);
4374 void perf_output_sample(struct perf_output_handle *handle,
4375 struct perf_event_header *header,
4376 struct perf_sample_data *data,
4377 struct perf_event *event)
4379 u64 sample_type = data->type;
4381 perf_output_put(handle, *header);
4383 if (sample_type & PERF_SAMPLE_IP)
4384 perf_output_put(handle, data->ip);
4386 if (sample_type & PERF_SAMPLE_TID)
4387 perf_output_put(handle, data->tid_entry);
4389 if (sample_type & PERF_SAMPLE_TIME)
4390 perf_output_put(handle, data->time);
4392 if (sample_type & PERF_SAMPLE_ADDR)
4393 perf_output_put(handle, data->addr);
4395 if (sample_type & PERF_SAMPLE_ID)
4396 perf_output_put(handle, data->id);
4398 if (sample_type & PERF_SAMPLE_STREAM_ID)
4399 perf_output_put(handle, data->stream_id);
4401 if (sample_type & PERF_SAMPLE_CPU)
4402 perf_output_put(handle, data->cpu_entry);
4404 if (sample_type & PERF_SAMPLE_PERIOD)
4405 perf_output_put(handle, data->period);
4407 if (sample_type & PERF_SAMPLE_READ)
4408 perf_output_read(handle, event);
4410 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4411 if (data->callchain) {
4414 if (data->callchain)
4415 size += data->callchain->nr;
4417 size *= sizeof(u64);
4419 __output_copy(handle, data->callchain, size);
4422 perf_output_put(handle, nr);
4426 if (sample_type & PERF_SAMPLE_RAW) {
4428 perf_output_put(handle, data->raw->size);
4429 __output_copy(handle, data->raw->data,
4436 .size = sizeof(u32),
4439 perf_output_put(handle, raw);
4443 if (!event->attr.watermark) {
4444 int wakeup_events = event->attr.wakeup_events;
4446 if (wakeup_events) {
4447 struct ring_buffer *rb = handle->rb;
4448 int events = local_inc_return(&rb->events);
4450 if (events >= wakeup_events) {
4451 local_sub(wakeup_events, &rb->events);
4452 local_inc(&rb->wakeup);
4457 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4458 if (data->br_stack) {
4461 size = data->br_stack->nr
4462 * sizeof(struct perf_branch_entry);
4464 perf_output_put(handle, data->br_stack->nr);
4465 perf_output_copy(handle, data->br_stack->entries, size);
4468 * we always store at least the value of nr
4471 perf_output_put(handle, nr);
4475 if (sample_type & PERF_SAMPLE_REGS_USER) {
4476 u64 abi = data->regs_user.abi;
4479 * If there are no regs to dump, notice it through
4480 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4482 perf_output_put(handle, abi);
4485 u64 mask = event->attr.sample_regs_user;
4486 perf_output_sample_regs(handle,
4487 data->regs_user.regs,
4492 if (sample_type & PERF_SAMPLE_STACK_USER)
4493 perf_output_sample_ustack(handle,
4494 data->stack_user_size,
4495 data->regs_user.regs);
4497 if (sample_type & PERF_SAMPLE_WEIGHT)
4498 perf_output_put(handle, data->weight);
4500 if (sample_type & PERF_SAMPLE_DATA_SRC)
4501 perf_output_put(handle, data->data_src.val);
4504 void perf_prepare_sample(struct perf_event_header *header,
4505 struct perf_sample_data *data,
4506 struct perf_event *event,
4507 struct pt_regs *regs)
4509 u64 sample_type = event->attr.sample_type;
4511 header->type = PERF_RECORD_SAMPLE;
4512 header->size = sizeof(*header) + event->header_size;
4515 header->misc |= perf_misc_flags(regs);
4517 __perf_event_header__init_id(header, data, event);
4519 if (sample_type & PERF_SAMPLE_IP)
4520 data->ip = perf_instruction_pointer(regs);
4522 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4525 data->callchain = perf_callchain(event, regs);
4527 if (data->callchain)
4528 size += data->callchain->nr;
4530 header->size += size * sizeof(u64);
4533 if (sample_type & PERF_SAMPLE_RAW) {
4534 int size = sizeof(u32);
4537 size += data->raw->size;
4539 size += sizeof(u32);
4541 WARN_ON_ONCE(size & (sizeof(u64)-1));
4542 header->size += size;
4545 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4546 int size = sizeof(u64); /* nr */
4547 if (data->br_stack) {
4548 size += data->br_stack->nr
4549 * sizeof(struct perf_branch_entry);
4551 header->size += size;
4554 if (sample_type & PERF_SAMPLE_REGS_USER) {
4555 /* regs dump ABI info */
4556 int size = sizeof(u64);
4558 perf_sample_regs_user(&data->regs_user, regs);
4560 if (data->regs_user.regs) {
4561 u64 mask = event->attr.sample_regs_user;
4562 size += hweight64(mask) * sizeof(u64);
4565 header->size += size;
4568 if (sample_type & PERF_SAMPLE_STACK_USER) {
4570 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4571 * processed as the last one or have additional check added
4572 * in case new sample type is added, because we could eat
4573 * up the rest of the sample size.
4575 struct perf_regs_user *uregs = &data->regs_user;
4576 u16 stack_size = event->attr.sample_stack_user;
4577 u16 size = sizeof(u64);
4580 perf_sample_regs_user(uregs, regs);
4582 stack_size = perf_sample_ustack_size(stack_size, header->size,
4586 * If there is something to dump, add space for the dump
4587 * itself and for the field that tells the dynamic size,
4588 * which is how many have been actually dumped.
4591 size += sizeof(u64) + stack_size;
4593 data->stack_user_size = stack_size;
4594 header->size += size;
4598 static void perf_event_output(struct perf_event *event,
4599 struct perf_sample_data *data,
4600 struct pt_regs *regs)
4602 struct perf_output_handle handle;
4603 struct perf_event_header header;
4605 /* protect the callchain buffers */
4608 perf_prepare_sample(&header, data, event, regs);
4610 if (perf_output_begin(&handle, event, header.size))
4613 perf_output_sample(&handle, &header, data, event);
4615 perf_output_end(&handle);
4625 struct perf_read_event {
4626 struct perf_event_header header;
4633 perf_event_read_event(struct perf_event *event,
4634 struct task_struct *task)
4636 struct perf_output_handle handle;
4637 struct perf_sample_data sample;
4638 struct perf_read_event read_event = {
4640 .type = PERF_RECORD_READ,
4642 .size = sizeof(read_event) + event->read_size,
4644 .pid = perf_event_pid(event, task),
4645 .tid = perf_event_tid(event, task),
4649 perf_event_header__init_id(&read_event.header, &sample, event);
4650 ret = perf_output_begin(&handle, event, read_event.header.size);
4654 perf_output_put(&handle, read_event);
4655 perf_output_read(&handle, event);
4656 perf_event__output_id_sample(event, &handle, &sample);
4658 perf_output_end(&handle);
4661 typedef int (perf_event_aux_match_cb)(struct perf_event *event, void *data);
4662 typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
4665 perf_event_aux_ctx(struct perf_event_context *ctx,
4666 perf_event_aux_match_cb match,
4667 perf_event_aux_output_cb output,
4670 struct perf_event *event;
4672 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4673 if (event->state < PERF_EVENT_STATE_INACTIVE)
4675 if (!event_filter_match(event))
4677 if (match(event, data))
4678 output(event, data);
4683 perf_event_aux(perf_event_aux_match_cb match,
4684 perf_event_aux_output_cb output,
4686 struct perf_event_context *task_ctx)
4688 struct perf_cpu_context *cpuctx;
4689 struct perf_event_context *ctx;
4694 list_for_each_entry_rcu(pmu, &pmus, entry) {
4695 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4696 if (cpuctx->unique_pmu != pmu)
4698 perf_event_aux_ctx(&cpuctx->ctx, match, output, data);
4701 ctxn = pmu->task_ctx_nr;
4704 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4706 perf_event_aux_ctx(ctx, match, output, data);
4708 put_cpu_ptr(pmu->pmu_cpu_context);
4713 perf_event_aux_ctx(task_ctx, match, output, data);
4720 * task tracking -- fork/exit
4722 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4725 struct perf_task_event {
4726 struct task_struct *task;
4727 struct perf_event_context *task_ctx;
4730 struct perf_event_header header;
4740 static void perf_event_task_output(struct perf_event *event,
4743 struct perf_task_event *task_event = data;
4744 struct perf_output_handle handle;
4745 struct perf_sample_data sample;
4746 struct task_struct *task = task_event->task;
4747 int ret, size = task_event->event_id.header.size;
4749 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4751 ret = perf_output_begin(&handle, event,
4752 task_event->event_id.header.size);
4756 task_event->event_id.pid = perf_event_pid(event, task);
4757 task_event->event_id.ppid = perf_event_pid(event, current);
4759 task_event->event_id.tid = perf_event_tid(event, task);
4760 task_event->event_id.ptid = perf_event_tid(event, current);
4762 perf_output_put(&handle, task_event->event_id);
4764 perf_event__output_id_sample(event, &handle, &sample);
4766 perf_output_end(&handle);
4768 task_event->event_id.header.size = size;
4771 static int perf_event_task_match(struct perf_event *event,
4772 void *data __maybe_unused)
4774 return event->attr.comm || event->attr.mmap ||
4775 event->attr.mmap_data || event->attr.task;
4778 static void perf_event_task(struct task_struct *task,
4779 struct perf_event_context *task_ctx,
4782 struct perf_task_event task_event;
4784 if (!atomic_read(&nr_comm_events) &&
4785 !atomic_read(&nr_mmap_events) &&
4786 !atomic_read(&nr_task_events))
4789 task_event = (struct perf_task_event){
4791 .task_ctx = task_ctx,
4794 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4796 .size = sizeof(task_event.event_id),
4802 .time = perf_clock(),
4806 perf_event_aux(perf_event_task_match,
4807 perf_event_task_output,
4812 void perf_event_fork(struct task_struct *task)
4814 perf_event_task(task, NULL, 1);
4821 struct perf_comm_event {
4822 struct task_struct *task;
4827 struct perf_event_header header;
4834 static void perf_event_comm_output(struct perf_event *event,
4837 struct perf_comm_event *comm_event = data;
4838 struct perf_output_handle handle;
4839 struct perf_sample_data sample;
4840 int size = comm_event->event_id.header.size;
4843 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4844 ret = perf_output_begin(&handle, event,
4845 comm_event->event_id.header.size);
4850 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4851 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4853 perf_output_put(&handle, comm_event->event_id);
4854 __output_copy(&handle, comm_event->comm,
4855 comm_event->comm_size);
4857 perf_event__output_id_sample(event, &handle, &sample);
4859 perf_output_end(&handle);
4861 comm_event->event_id.header.size = size;
4864 static int perf_event_comm_match(struct perf_event *event,
4865 void *data __maybe_unused)
4867 return event->attr.comm;
4870 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4872 char comm[TASK_COMM_LEN];
4875 memset(comm, 0, sizeof(comm));
4876 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4877 size = ALIGN(strlen(comm)+1, sizeof(u64));
4879 comm_event->comm = comm;
4880 comm_event->comm_size = size;
4882 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4884 perf_event_aux(perf_event_comm_match,
4885 perf_event_comm_output,
4890 void perf_event_comm(struct task_struct *task)
4892 struct perf_comm_event comm_event;
4893 struct perf_event_context *ctx;
4897 for_each_task_context_nr(ctxn) {
4898 ctx = task->perf_event_ctxp[ctxn];
4902 perf_event_enable_on_exec(ctx);
4906 if (!atomic_read(&nr_comm_events))
4909 comm_event = (struct perf_comm_event){
4915 .type = PERF_RECORD_COMM,
4924 perf_event_comm_event(&comm_event);
4931 struct perf_mmap_event {
4932 struct vm_area_struct *vma;
4934 const char *file_name;
4938 struct perf_event_header header;
4948 static void perf_event_mmap_output(struct perf_event *event,
4951 struct perf_mmap_event *mmap_event = data;
4952 struct perf_output_handle handle;
4953 struct perf_sample_data sample;
4954 int size = mmap_event->event_id.header.size;
4957 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4958 ret = perf_output_begin(&handle, event,
4959 mmap_event->event_id.header.size);
4963 mmap_event->event_id.pid = perf_event_pid(event, current);
4964 mmap_event->event_id.tid = perf_event_tid(event, current);
4966 perf_output_put(&handle, mmap_event->event_id);
4967 __output_copy(&handle, mmap_event->file_name,
4968 mmap_event->file_size);
4970 perf_event__output_id_sample(event, &handle, &sample);
4972 perf_output_end(&handle);
4974 mmap_event->event_id.header.size = size;
4977 static int perf_event_mmap_match(struct perf_event *event,
4980 struct perf_mmap_event *mmap_event = data;
4981 struct vm_area_struct *vma = mmap_event->vma;
4982 int executable = vma->vm_flags & VM_EXEC;
4984 return (!executable && event->attr.mmap_data) ||
4985 (executable && event->attr.mmap);
4988 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4990 struct vm_area_struct *vma = mmap_event->vma;
4991 struct file *file = vma->vm_file;
4997 memset(tmp, 0, sizeof(tmp));
5001 * d_path works from the end of the rb backwards, so we
5002 * need to add enough zero bytes after the string to handle
5003 * the 64bit alignment we do later.
5005 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
5007 name = strncpy(tmp, "//enomem", sizeof(tmp));
5010 name = d_path(&file->f_path, buf, PATH_MAX);
5012 name = strncpy(tmp, "//toolong", sizeof(tmp));
5016 if (arch_vma_name(mmap_event->vma)) {
5017 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
5019 tmp[sizeof(tmp) - 1] = '\0';
5024 name = strncpy(tmp, "[vdso]", sizeof(tmp));
5026 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
5027 vma->vm_end >= vma->vm_mm->brk) {
5028 name = strncpy(tmp, "[heap]", sizeof(tmp));
5030 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
5031 vma->vm_end >= vma->vm_mm->start_stack) {
5032 name = strncpy(tmp, "[stack]", sizeof(tmp));
5036 name = strncpy(tmp, "//anon", sizeof(tmp));
5041 size = ALIGN(strlen(name)+1, sizeof(u64));
5043 mmap_event->file_name = name;
5044 mmap_event->file_size = size;
5046 if (!(vma->vm_flags & VM_EXEC))
5047 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
5049 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
5051 perf_event_aux(perf_event_mmap_match,
5052 perf_event_mmap_output,
5059 void perf_event_mmap(struct vm_area_struct *vma)
5061 struct perf_mmap_event mmap_event;
5063 if (!atomic_read(&nr_mmap_events))
5066 mmap_event = (struct perf_mmap_event){
5072 .type = PERF_RECORD_MMAP,
5073 .misc = PERF_RECORD_MISC_USER,
5078 .start = vma->vm_start,
5079 .len = vma->vm_end - vma->vm_start,
5080 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
5084 perf_event_mmap_event(&mmap_event);
5088 * IRQ throttle logging
5091 static void perf_log_throttle(struct perf_event *event, int enable)
5093 struct perf_output_handle handle;
5094 struct perf_sample_data sample;
5098 struct perf_event_header header;
5102 } throttle_event = {
5104 .type = PERF_RECORD_THROTTLE,
5106 .size = sizeof(throttle_event),
5108 .time = perf_clock(),
5109 .id = primary_event_id(event),
5110 .stream_id = event->id,
5114 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
5116 perf_event_header__init_id(&throttle_event.header, &sample, event);
5118 ret = perf_output_begin(&handle, event,
5119 throttle_event.header.size);
5123 perf_output_put(&handle, throttle_event);
5124 perf_event__output_id_sample(event, &handle, &sample);
5125 perf_output_end(&handle);
5129 * Generic event overflow handling, sampling.
5132 static int __perf_event_overflow(struct perf_event *event,
5133 int throttle, struct perf_sample_data *data,
5134 struct pt_regs *regs)
5136 int events = atomic_read(&event->event_limit);
5137 struct hw_perf_event *hwc = &event->hw;
5142 * Non-sampling counters might still use the PMI to fold short
5143 * hardware counters, ignore those.
5145 if (unlikely(!is_sampling_event(event)))
5148 seq = __this_cpu_read(perf_throttled_seq);
5149 if (seq != hwc->interrupts_seq) {
5150 hwc->interrupts_seq = seq;
5151 hwc->interrupts = 1;
5154 if (unlikely(throttle
5155 && hwc->interrupts >= max_samples_per_tick)) {
5156 __this_cpu_inc(perf_throttled_count);
5157 hwc->interrupts = MAX_INTERRUPTS;
5158 perf_log_throttle(event, 0);
5163 if (event->attr.freq) {
5164 u64 now = perf_clock();
5165 s64 delta = now - hwc->freq_time_stamp;
5167 hwc->freq_time_stamp = now;
5169 if (delta > 0 && delta < 2*TICK_NSEC)
5170 perf_adjust_period(event, delta, hwc->last_period, true);
5174 * XXX event_limit might not quite work as expected on inherited
5178 event->pending_kill = POLL_IN;
5179 if (events && atomic_dec_and_test(&event->event_limit)) {
5181 event->pending_kill = POLL_HUP;
5182 event->pending_disable = 1;
5183 irq_work_queue(&event->pending);
5186 if (event->overflow_handler)
5187 event->overflow_handler(event, data, regs);
5189 perf_event_output(event, data, regs);
5191 if (event->fasync && event->pending_kill) {
5192 event->pending_wakeup = 1;
5193 irq_work_queue(&event->pending);
5199 int perf_event_overflow(struct perf_event *event,
5200 struct perf_sample_data *data,
5201 struct pt_regs *regs)
5203 return __perf_event_overflow(event, 1, data, regs);
5207 * Generic software event infrastructure
5210 struct swevent_htable {
5211 struct swevent_hlist *swevent_hlist;
5212 struct mutex hlist_mutex;
5215 /* Recursion avoidance in each contexts */
5216 int recursion[PERF_NR_CONTEXTS];
5219 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5222 * We directly increment event->count and keep a second value in
5223 * event->hw.period_left to count intervals. This period event
5224 * is kept in the range [-sample_period, 0] so that we can use the
5228 u64 perf_swevent_set_period(struct perf_event *event)
5230 struct hw_perf_event *hwc = &event->hw;
5231 u64 period = hwc->last_period;
5235 hwc->last_period = hwc->sample_period;
5238 old = val = local64_read(&hwc->period_left);
5242 nr = div64_u64(period + val, period);
5243 offset = nr * period;
5245 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
5251 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
5252 struct perf_sample_data *data,
5253 struct pt_regs *regs)
5255 struct hw_perf_event *hwc = &event->hw;
5259 overflow = perf_swevent_set_period(event);
5261 if (hwc->interrupts == MAX_INTERRUPTS)
5264 for (; overflow; overflow--) {
5265 if (__perf_event_overflow(event, throttle,
5268 * We inhibit the overflow from happening when
5269 * hwc->interrupts == MAX_INTERRUPTS.
5277 static void perf_swevent_event(struct perf_event *event, u64 nr,
5278 struct perf_sample_data *data,
5279 struct pt_regs *regs)
5281 struct hw_perf_event *hwc = &event->hw;
5283 local64_add(nr, &event->count);
5288 if (!is_sampling_event(event))
5291 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5293 return perf_swevent_overflow(event, 1, data, regs);
5295 data->period = event->hw.last_period;
5297 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
5298 return perf_swevent_overflow(event, 1, data, regs);
5300 if (local64_add_negative(nr, &hwc->period_left))
5303 perf_swevent_overflow(event, 0, data, regs);
5306 static int perf_exclude_event(struct perf_event *event,
5307 struct pt_regs *regs)
5309 if (event->hw.state & PERF_HES_STOPPED)
5313 if (event->attr.exclude_user && user_mode(regs))
5316 if (event->attr.exclude_kernel && !user_mode(regs))
5323 static int perf_swevent_match(struct perf_event *event,
5324 enum perf_type_id type,
5326 struct perf_sample_data *data,
5327 struct pt_regs *regs)
5329 if (event->attr.type != type)
5332 if (event->attr.config != event_id)
5335 if (perf_exclude_event(event, regs))
5341 static inline u64 swevent_hash(u64 type, u32 event_id)
5343 u64 val = event_id | (type << 32);
5345 return hash_64(val, SWEVENT_HLIST_BITS);
5348 static inline struct hlist_head *
5349 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
5351 u64 hash = swevent_hash(type, event_id);
5353 return &hlist->heads[hash];
5356 /* For the read side: events when they trigger */
5357 static inline struct hlist_head *
5358 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
5360 struct swevent_hlist *hlist;
5362 hlist = rcu_dereference(swhash->swevent_hlist);
5366 return __find_swevent_head(hlist, type, event_id);
5369 /* For the event head insertion and removal in the hlist */
5370 static inline struct hlist_head *
5371 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
5373 struct swevent_hlist *hlist;
5374 u32 event_id = event->attr.config;
5375 u64 type = event->attr.type;
5378 * Event scheduling is always serialized against hlist allocation
5379 * and release. Which makes the protected version suitable here.
5380 * The context lock guarantees that.
5382 hlist = rcu_dereference_protected(swhash->swevent_hlist,
5383 lockdep_is_held(&event->ctx->lock));
5387 return __find_swevent_head(hlist, type, event_id);
5390 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5392 struct perf_sample_data *data,
5393 struct pt_regs *regs)
5395 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5396 struct perf_event *event;
5397 struct hlist_head *head;
5400 head = find_swevent_head_rcu(swhash, type, event_id);
5404 hlist_for_each_entry_rcu(event, head, hlist_entry) {
5405 if (perf_swevent_match(event, type, event_id, data, regs))
5406 perf_swevent_event(event, nr, data, regs);
5412 int perf_swevent_get_recursion_context(void)
5414 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5416 return get_recursion_context(swhash->recursion);
5418 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
5420 inline void perf_swevent_put_recursion_context(int rctx)
5422 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5424 put_recursion_context(swhash->recursion, rctx);
5427 void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
5429 struct perf_sample_data data;
5432 preempt_disable_notrace();
5433 rctx = perf_swevent_get_recursion_context();
5437 perf_sample_data_init(&data, addr, 0);
5439 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
5441 perf_swevent_put_recursion_context(rctx);
5442 preempt_enable_notrace();
5445 static void perf_swevent_read(struct perf_event *event)
5449 static int perf_swevent_add(struct perf_event *event, int flags)
5451 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5452 struct hw_perf_event *hwc = &event->hw;
5453 struct hlist_head *head;
5455 if (is_sampling_event(event)) {
5456 hwc->last_period = hwc->sample_period;
5457 perf_swevent_set_period(event);
5460 hwc->state = !(flags & PERF_EF_START);
5462 head = find_swevent_head(swhash, event);
5463 if (WARN_ON_ONCE(!head))
5466 hlist_add_head_rcu(&event->hlist_entry, head);
5471 static void perf_swevent_del(struct perf_event *event, int flags)
5473 hlist_del_rcu(&event->hlist_entry);
5476 static void perf_swevent_start(struct perf_event *event, int flags)
5478 event->hw.state = 0;
5481 static void perf_swevent_stop(struct perf_event *event, int flags)
5483 event->hw.state = PERF_HES_STOPPED;
5486 /* Deref the hlist from the update side */
5487 static inline struct swevent_hlist *
5488 swevent_hlist_deref(struct swevent_htable *swhash)
5490 return rcu_dereference_protected(swhash->swevent_hlist,
5491 lockdep_is_held(&swhash->hlist_mutex));
5494 static void swevent_hlist_release(struct swevent_htable *swhash)
5496 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
5501 rcu_assign_pointer(swhash->swevent_hlist, NULL);
5502 kfree_rcu(hlist, rcu_head);
5505 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5507 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5509 mutex_lock(&swhash->hlist_mutex);
5511 if (!--swhash->hlist_refcount)
5512 swevent_hlist_release(swhash);
5514 mutex_unlock(&swhash->hlist_mutex);
5517 static void swevent_hlist_put(struct perf_event *event)
5521 if (event->cpu != -1) {
5522 swevent_hlist_put_cpu(event, event->cpu);
5526 for_each_possible_cpu(cpu)
5527 swevent_hlist_put_cpu(event, cpu);
5530 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5532 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5535 mutex_lock(&swhash->hlist_mutex);
5537 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
5538 struct swevent_hlist *hlist;
5540 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5545 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5547 swhash->hlist_refcount++;
5549 mutex_unlock(&swhash->hlist_mutex);
5554 static int swevent_hlist_get(struct perf_event *event)
5557 int cpu, failed_cpu;
5559 if (event->cpu != -1)
5560 return swevent_hlist_get_cpu(event, event->cpu);
5563 for_each_possible_cpu(cpu) {
5564 err = swevent_hlist_get_cpu(event, cpu);
5574 for_each_possible_cpu(cpu) {
5575 if (cpu == failed_cpu)
5577 swevent_hlist_put_cpu(event, cpu);
5584 struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
5586 static void sw_perf_event_destroy(struct perf_event *event)
5588 u64 event_id = event->attr.config;
5590 WARN_ON(event->parent);
5592 static_key_slow_dec(&perf_swevent_enabled[event_id]);
5593 swevent_hlist_put(event);
5596 static int perf_swevent_init(struct perf_event *event)
5598 u64 event_id = event->attr.config;
5600 if (event->attr.type != PERF_TYPE_SOFTWARE)
5604 * no branch sampling for software events
5606 if (has_branch_stack(event))
5610 case PERF_COUNT_SW_CPU_CLOCK:
5611 case PERF_COUNT_SW_TASK_CLOCK:
5618 if (event_id >= PERF_COUNT_SW_MAX)
5621 if (!event->parent) {
5624 err = swevent_hlist_get(event);
5628 static_key_slow_inc(&perf_swevent_enabled[event_id]);
5629 event->destroy = sw_perf_event_destroy;
5635 static int perf_swevent_event_idx(struct perf_event *event)
5640 static struct pmu perf_swevent = {
5641 .task_ctx_nr = perf_sw_context,
5643 .event_init = perf_swevent_init,
5644 .add = perf_swevent_add,
5645 .del = perf_swevent_del,
5646 .start = perf_swevent_start,
5647 .stop = perf_swevent_stop,
5648 .read = perf_swevent_read,
5650 .event_idx = perf_swevent_event_idx,
5653 #ifdef CONFIG_EVENT_TRACING
5655 static int perf_tp_filter_match(struct perf_event *event,
5656 struct perf_sample_data *data)
5658 void *record = data->raw->data;
5660 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5665 static int perf_tp_event_match(struct perf_event *event,
5666 struct perf_sample_data *data,
5667 struct pt_regs *regs)
5669 if (event->hw.state & PERF_HES_STOPPED)
5672 * All tracepoints are from kernel-space.
5674 if (event->attr.exclude_kernel)
5677 if (!perf_tp_filter_match(event, data))
5683 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5684 struct pt_regs *regs, struct hlist_head *head, int rctx,
5685 struct task_struct *task)
5687 struct perf_sample_data data;
5688 struct perf_event *event;
5690 struct perf_raw_record raw = {
5695 perf_sample_data_init(&data, addr, 0);
5698 hlist_for_each_entry_rcu(event, head, hlist_entry) {
5699 if (perf_tp_event_match(event, &data, regs))
5700 perf_swevent_event(event, count, &data, regs);
5704 * If we got specified a target task, also iterate its context and
5705 * deliver this event there too.
5707 if (task && task != current) {
5708 struct perf_event_context *ctx;
5709 struct trace_entry *entry = record;
5712 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
5716 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5717 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5719 if (event->attr.config != entry->type)
5721 if (perf_tp_event_match(event, &data, regs))
5722 perf_swevent_event(event, count, &data, regs);
5728 perf_swevent_put_recursion_context(rctx);
5730 EXPORT_SYMBOL_GPL(perf_tp_event);
5732 static void tp_perf_event_destroy(struct perf_event *event)
5734 perf_trace_destroy(event);
5737 static int perf_tp_event_init(struct perf_event *event)
5741 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5745 * no branch sampling for tracepoint events
5747 if (has_branch_stack(event))
5750 err = perf_trace_init(event);
5754 event->destroy = tp_perf_event_destroy;
5759 static struct pmu perf_tracepoint = {
5760 .task_ctx_nr = perf_sw_context,
5762 .event_init = perf_tp_event_init,
5763 .add = perf_trace_add,
5764 .del = perf_trace_del,
5765 .start = perf_swevent_start,
5766 .stop = perf_swevent_stop,
5767 .read = perf_swevent_read,
5769 .event_idx = perf_swevent_event_idx,
5772 static inline void perf_tp_register(void)
5774 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5777 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5782 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5785 filter_str = strndup_user(arg, PAGE_SIZE);
5786 if (IS_ERR(filter_str))
5787 return PTR_ERR(filter_str);
5789 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5795 static void perf_event_free_filter(struct perf_event *event)
5797 ftrace_profile_free_filter(event);
5802 static inline void perf_tp_register(void)
5806 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5811 static void perf_event_free_filter(struct perf_event *event)
5815 #endif /* CONFIG_EVENT_TRACING */
5817 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5818 void perf_bp_event(struct perf_event *bp, void *data)
5820 struct perf_sample_data sample;
5821 struct pt_regs *regs = data;
5823 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
5825 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5826 perf_swevent_event(bp, 1, &sample, regs);
5831 * hrtimer based swevent callback
5834 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5836 enum hrtimer_restart ret = HRTIMER_RESTART;
5837 struct perf_sample_data data;
5838 struct pt_regs *regs;
5839 struct perf_event *event;
5842 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5844 if (event->state != PERF_EVENT_STATE_ACTIVE)
5845 return HRTIMER_NORESTART;
5847 event->pmu->read(event);
5849 perf_sample_data_init(&data, 0, event->hw.last_period);
5850 regs = get_irq_regs();
5852 if (regs && !perf_exclude_event(event, regs)) {
5853 if (!(event->attr.exclude_idle && is_idle_task(current)))
5854 if (__perf_event_overflow(event, 1, &data, regs))
5855 ret = HRTIMER_NORESTART;
5858 period = max_t(u64, 10000, event->hw.sample_period);
5859 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5864 static void perf_swevent_start_hrtimer(struct perf_event *event)
5866 struct hw_perf_event *hwc = &event->hw;
5869 if (!is_sampling_event(event))
5872 period = local64_read(&hwc->period_left);
5877 local64_set(&hwc->period_left, 0);
5879 period = max_t(u64, 10000, hwc->sample_period);
5881 __hrtimer_start_range_ns(&hwc->hrtimer,
5882 ns_to_ktime(period), 0,
5883 HRTIMER_MODE_REL_PINNED, 0);
5886 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5888 struct hw_perf_event *hwc = &event->hw;
5890 if (is_sampling_event(event)) {
5891 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5892 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5894 hrtimer_cancel(&hwc->hrtimer);
5898 static void perf_swevent_init_hrtimer(struct perf_event *event)
5900 struct hw_perf_event *hwc = &event->hw;
5902 if (!is_sampling_event(event))
5905 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5906 hwc->hrtimer.function = perf_swevent_hrtimer;
5909 * Since hrtimers have a fixed rate, we can do a static freq->period
5910 * mapping and avoid the whole period adjust feedback stuff.
5912 if (event->attr.freq) {
5913 long freq = event->attr.sample_freq;
5915 event->attr.sample_period = NSEC_PER_SEC / freq;
5916 hwc->sample_period = event->attr.sample_period;
5917 local64_set(&hwc->period_left, hwc->sample_period);
5918 hwc->last_period = hwc->sample_period;
5919 event->attr.freq = 0;
5924 * Software event: cpu wall time clock
5927 static void cpu_clock_event_update(struct perf_event *event)
5932 now = local_clock();
5933 prev = local64_xchg(&event->hw.prev_count, now);
5934 local64_add(now - prev, &event->count);
5937 static void cpu_clock_event_start(struct perf_event *event, int flags)
5939 local64_set(&event->hw.prev_count, local_clock());
5940 perf_swevent_start_hrtimer(event);
5943 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5945 perf_swevent_cancel_hrtimer(event);
5946 cpu_clock_event_update(event);
5949 static int cpu_clock_event_add(struct perf_event *event, int flags)
5951 if (flags & PERF_EF_START)
5952 cpu_clock_event_start(event, flags);
5957 static void cpu_clock_event_del(struct perf_event *event, int flags)
5959 cpu_clock_event_stop(event, flags);
5962 static void cpu_clock_event_read(struct perf_event *event)
5964 cpu_clock_event_update(event);
5967 static int cpu_clock_event_init(struct perf_event *event)
5969 if (event->attr.type != PERF_TYPE_SOFTWARE)
5972 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5976 * no branch sampling for software events
5978 if (has_branch_stack(event))
5981 perf_swevent_init_hrtimer(event);
5986 static struct pmu perf_cpu_clock = {
5987 .task_ctx_nr = perf_sw_context,
5989 .event_init = cpu_clock_event_init,
5990 .add = cpu_clock_event_add,
5991 .del = cpu_clock_event_del,
5992 .start = cpu_clock_event_start,
5993 .stop = cpu_clock_event_stop,
5994 .read = cpu_clock_event_read,
5996 .event_idx = perf_swevent_event_idx,
6000 * Software event: task time clock
6003 static void task_clock_event_update(struct perf_event *event, u64 now)
6008 prev = local64_xchg(&event->hw.prev_count, now);
6010 local64_add(delta, &event->count);
6013 static void task_clock_event_start(struct perf_event *event, int flags)
6015 local64_set(&event->hw.prev_count, event->ctx->time);
6016 perf_swevent_start_hrtimer(event);
6019 static void task_clock_event_stop(struct perf_event *event, int flags)
6021 perf_swevent_cancel_hrtimer(event);
6022 task_clock_event_update(event, event->ctx->time);
6025 static int task_clock_event_add(struct perf_event *event, int flags)
6027 if (flags & PERF_EF_START)
6028 task_clock_event_start(event, flags);
6033 static void task_clock_event_del(struct perf_event *event, int flags)
6035 task_clock_event_stop(event, PERF_EF_UPDATE);
6038 static void task_clock_event_read(struct perf_event *event)
6040 u64 now = perf_clock();
6041 u64 delta = now - event->ctx->timestamp;
6042 u64 time = event->ctx->time + delta;
6044 task_clock_event_update(event, time);
6047 static int task_clock_event_init(struct perf_event *event)
6049 if (event->attr.type != PERF_TYPE_SOFTWARE)
6052 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
6056 * no branch sampling for software events
6058 if (has_branch_stack(event))
6061 perf_swevent_init_hrtimer(event);
6066 static struct pmu perf_task_clock = {
6067 .task_ctx_nr = perf_sw_context,
6069 .event_init = task_clock_event_init,
6070 .add = task_clock_event_add,
6071 .del = task_clock_event_del,
6072 .start = task_clock_event_start,
6073 .stop = task_clock_event_stop,
6074 .read = task_clock_event_read,
6076 .event_idx = perf_swevent_event_idx,
6079 static void perf_pmu_nop_void(struct pmu *pmu)
6083 static int perf_pmu_nop_int(struct pmu *pmu)
6088 static void perf_pmu_start_txn(struct pmu *pmu)
6090 perf_pmu_disable(pmu);
6093 static int perf_pmu_commit_txn(struct pmu *pmu)
6095 perf_pmu_enable(pmu);
6099 static void perf_pmu_cancel_txn(struct pmu *pmu)
6101 perf_pmu_enable(pmu);
6104 static int perf_event_idx_default(struct perf_event *event)
6106 return event->hw.idx + 1;
6110 * Ensures all contexts with the same task_ctx_nr have the same
6111 * pmu_cpu_context too.
6113 static void *find_pmu_context(int ctxn)
6120 list_for_each_entry(pmu, &pmus, entry) {
6121 if (pmu->task_ctx_nr == ctxn)
6122 return pmu->pmu_cpu_context;
6128 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
6132 for_each_possible_cpu(cpu) {
6133 struct perf_cpu_context *cpuctx;
6135 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6137 if (cpuctx->unique_pmu == old_pmu)
6138 cpuctx->unique_pmu = pmu;
6142 static void free_pmu_context(struct pmu *pmu)
6146 mutex_lock(&pmus_lock);
6148 * Like a real lame refcount.
6150 list_for_each_entry(i, &pmus, entry) {
6151 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
6152 update_pmu_context(i, pmu);
6157 free_percpu(pmu->pmu_cpu_context);
6159 mutex_unlock(&pmus_lock);
6161 static struct idr pmu_idr;
6164 type_show(struct device *dev, struct device_attribute *attr, char *page)
6166 struct pmu *pmu = dev_get_drvdata(dev);
6168 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6172 perf_event_mux_interval_ms_show(struct device *dev,
6173 struct device_attribute *attr,
6176 struct pmu *pmu = dev_get_drvdata(dev);
6178 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6182 perf_event_mux_interval_ms_store(struct device *dev,
6183 struct device_attribute *attr,
6184 const char *buf, size_t count)
6186 struct pmu *pmu = dev_get_drvdata(dev);
6187 int timer, cpu, ret;
6189 ret = kstrtoint(buf, 0, &timer);
6196 /* same value, noting to do */
6197 if (timer == pmu->hrtimer_interval_ms)
6200 pmu->hrtimer_interval_ms = timer;
6202 /* update all cpuctx for this PMU */
6203 for_each_possible_cpu(cpu) {
6204 struct perf_cpu_context *cpuctx;
6205 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6206 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6208 if (hrtimer_active(&cpuctx->hrtimer))
6209 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6215 #define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store)
6217 static struct device_attribute pmu_dev_attrs[] = {
6219 __ATTR_RW(perf_event_mux_interval_ms),
6223 static int pmu_bus_running;
6224 static struct bus_type pmu_bus = {
6225 .name = "event_source",
6226 .dev_attrs = pmu_dev_attrs,
6229 static void pmu_dev_release(struct device *dev)
6234 static int pmu_dev_alloc(struct pmu *pmu)
6238 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6242 pmu->dev->groups = pmu->attr_groups;
6243 device_initialize(pmu->dev);
6244 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6248 dev_set_drvdata(pmu->dev, pmu);
6249 pmu->dev->bus = &pmu_bus;
6250 pmu->dev->release = pmu_dev_release;
6251 ret = device_add(pmu->dev);
6259 put_device(pmu->dev);
6263 static struct lock_class_key cpuctx_mutex;
6264 static struct lock_class_key cpuctx_lock;
6266 int perf_pmu_register(struct pmu *pmu, const char *name, int type)
6270 mutex_lock(&pmus_lock);
6272 pmu->pmu_disable_count = alloc_percpu(int);
6273 if (!pmu->pmu_disable_count)
6282 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6290 if (pmu_bus_running) {
6291 ret = pmu_dev_alloc(pmu);
6297 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6298 if (pmu->pmu_cpu_context)
6299 goto got_cpu_context;
6302 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6303 if (!pmu->pmu_cpu_context)
6306 for_each_possible_cpu(cpu) {
6307 struct perf_cpu_context *cpuctx;
6309 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6310 __perf_event_init_context(&cpuctx->ctx);
6311 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
6312 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
6313 cpuctx->ctx.type = cpu_context;
6314 cpuctx->ctx.pmu = pmu;
6316 __perf_cpu_hrtimer_init(cpuctx, cpu);
6318 INIT_LIST_HEAD(&cpuctx->rotation_list);
6319 cpuctx->unique_pmu = pmu;
6323 if (!pmu->start_txn) {
6324 if (pmu->pmu_enable) {
6326 * If we have pmu_enable/pmu_disable calls, install
6327 * transaction stubs that use that to try and batch
6328 * hardware accesses.
6330 pmu->start_txn = perf_pmu_start_txn;
6331 pmu->commit_txn = perf_pmu_commit_txn;
6332 pmu->cancel_txn = perf_pmu_cancel_txn;
6334 pmu->start_txn = perf_pmu_nop_void;
6335 pmu->commit_txn = perf_pmu_nop_int;
6336 pmu->cancel_txn = perf_pmu_nop_void;
6340 if (!pmu->pmu_enable) {
6341 pmu->pmu_enable = perf_pmu_nop_void;
6342 pmu->pmu_disable = perf_pmu_nop_void;
6345 if (!pmu->event_idx)
6346 pmu->event_idx = perf_event_idx_default;
6348 list_add_rcu(&pmu->entry, &pmus);
6351 mutex_unlock(&pmus_lock);
6356 device_del(pmu->dev);
6357 put_device(pmu->dev);
6360 if (pmu->type >= PERF_TYPE_MAX)
6361 idr_remove(&pmu_idr, pmu->type);
6364 free_percpu(pmu->pmu_disable_count);
6368 void perf_pmu_unregister(struct pmu *pmu)
6370 mutex_lock(&pmus_lock);
6371 list_del_rcu(&pmu->entry);
6372 mutex_unlock(&pmus_lock);
6375 * We dereference the pmu list under both SRCU and regular RCU, so
6376 * synchronize against both of those.
6378 synchronize_srcu(&pmus_srcu);
6381 free_percpu(pmu->pmu_disable_count);
6382 if (pmu->type >= PERF_TYPE_MAX)
6383 idr_remove(&pmu_idr, pmu->type);
6384 device_del(pmu->dev);
6385 put_device(pmu->dev);
6386 free_pmu_context(pmu);
6389 struct pmu *perf_init_event(struct perf_event *event)
6391 struct pmu *pmu = NULL;
6395 idx = srcu_read_lock(&pmus_srcu);
6398 pmu = idr_find(&pmu_idr, event->attr.type);
6402 ret = pmu->event_init(event);
6408 list_for_each_entry_rcu(pmu, &pmus, entry) {
6410 ret = pmu->event_init(event);
6414 if (ret != -ENOENT) {
6419 pmu = ERR_PTR(-ENOENT);
6421 srcu_read_unlock(&pmus_srcu, idx);
6427 * Allocate and initialize a event structure
6429 static struct perf_event *
6430 perf_event_alloc(struct perf_event_attr *attr, int cpu,
6431 struct task_struct *task,
6432 struct perf_event *group_leader,
6433 struct perf_event *parent_event,
6434 perf_overflow_handler_t overflow_handler,
6438 struct perf_event *event;
6439 struct hw_perf_event *hwc;
6442 if ((unsigned)cpu >= nr_cpu_ids) {
6443 if (!task || cpu != -1)
6444 return ERR_PTR(-EINVAL);
6447 event = kzalloc(sizeof(*event), GFP_KERNEL);
6449 return ERR_PTR(-ENOMEM);
6452 * Single events are their own group leaders, with an
6453 * empty sibling list:
6456 group_leader = event;
6458 mutex_init(&event->child_mutex);
6459 INIT_LIST_HEAD(&event->child_list);
6461 INIT_LIST_HEAD(&event->group_entry);
6462 INIT_LIST_HEAD(&event->event_entry);
6463 INIT_LIST_HEAD(&event->sibling_list);
6464 INIT_LIST_HEAD(&event->rb_entry);
6466 init_waitqueue_head(&event->waitq);
6467 init_irq_work(&event->pending, perf_pending_event);
6469 mutex_init(&event->mmap_mutex);
6471 atomic_long_set(&event->refcount, 1);
6473 event->attr = *attr;
6474 event->group_leader = group_leader;
6478 event->parent = parent_event;
6480 event->ns = get_pid_ns(task_active_pid_ns(current));
6481 event->id = atomic64_inc_return(&perf_event_id);
6483 event->state = PERF_EVENT_STATE_INACTIVE;
6486 event->attach_state = PERF_ATTACH_TASK;
6488 if (attr->type == PERF_TYPE_TRACEPOINT)
6489 event->hw.tp_target = task;
6490 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6492 * hw_breakpoint is a bit difficult here..
6494 else if (attr->type == PERF_TYPE_BREAKPOINT)
6495 event->hw.bp_target = task;
6499 if (!overflow_handler && parent_event) {
6500 overflow_handler = parent_event->overflow_handler;
6501 context = parent_event->overflow_handler_context;
6504 event->overflow_handler = overflow_handler;
6505 event->overflow_handler_context = context;
6507 perf_event__state_init(event);
6512 hwc->sample_period = attr->sample_period;
6513 if (attr->freq && attr->sample_freq)
6514 hwc->sample_period = 1;
6515 hwc->last_period = hwc->sample_period;
6517 local64_set(&hwc->period_left, hwc->sample_period);
6520 * we currently do not support PERF_FORMAT_GROUP on inherited events
6522 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6525 pmu = perf_init_event(event);
6531 else if (IS_ERR(pmu))
6536 put_pid_ns(event->ns);
6538 return ERR_PTR(err);
6541 if (!event->parent) {
6542 if (event->attach_state & PERF_ATTACH_TASK)
6543 static_key_slow_inc(&perf_sched_events.key);
6544 if (event->attr.mmap || event->attr.mmap_data)
6545 atomic_inc(&nr_mmap_events);
6546 if (event->attr.comm)
6547 atomic_inc(&nr_comm_events);
6548 if (event->attr.task)
6549 atomic_inc(&nr_task_events);
6550 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6551 err = get_callchain_buffers();
6554 return ERR_PTR(err);
6557 if (has_branch_stack(event)) {
6558 static_key_slow_inc(&perf_sched_events.key);
6559 if (!(event->attach_state & PERF_ATTACH_TASK))
6560 atomic_inc(&per_cpu(perf_branch_stack_events,
6568 static int perf_copy_attr(struct perf_event_attr __user *uattr,
6569 struct perf_event_attr *attr)
6574 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6578 * zero the full structure, so that a short copy will be nice.
6580 memset(attr, 0, sizeof(*attr));
6582 ret = get_user(size, &uattr->size);
6586 if (size > PAGE_SIZE) /* silly large */
6589 if (!size) /* abi compat */
6590 size = PERF_ATTR_SIZE_VER0;
6592 if (size < PERF_ATTR_SIZE_VER0)
6596 * If we're handed a bigger struct than we know of,
6597 * ensure all the unknown bits are 0 - i.e. new
6598 * user-space does not rely on any kernel feature
6599 * extensions we dont know about yet.
6601 if (size > sizeof(*attr)) {
6602 unsigned char __user *addr;
6603 unsigned char __user *end;
6606 addr = (void __user *)uattr + sizeof(*attr);
6607 end = (void __user *)uattr + size;
6609 for (; addr < end; addr++) {
6610 ret = get_user(val, addr);
6616 size = sizeof(*attr);
6619 ret = copy_from_user(attr, uattr, size);
6623 if (attr->__reserved_1)
6626 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6629 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6632 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
6633 u64 mask = attr->branch_sample_type;
6635 /* only using defined bits */
6636 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6639 /* at least one branch bit must be set */
6640 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6643 /* propagate priv level, when not set for branch */
6644 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6646 /* exclude_kernel checked on syscall entry */
6647 if (!attr->exclude_kernel)
6648 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6650 if (!attr->exclude_user)
6651 mask |= PERF_SAMPLE_BRANCH_USER;
6653 if (!attr->exclude_hv)
6654 mask |= PERF_SAMPLE_BRANCH_HV;
6656 * adjust user setting (for HW filter setup)
6658 attr->branch_sample_type = mask;
6660 /* privileged levels capture (kernel, hv): check permissions */
6661 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
6662 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6666 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
6667 ret = perf_reg_validate(attr->sample_regs_user);
6672 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
6673 if (!arch_perf_have_user_stack_dump())
6677 * We have __u32 type for the size, but so far
6678 * we can only use __u16 as maximum due to the
6679 * __u16 sample size limit.
6681 if (attr->sample_stack_user >= USHRT_MAX)
6683 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
6691 put_user(sizeof(*attr), &uattr->size);
6697 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6699 struct ring_buffer *rb = NULL, *old_rb = NULL;
6705 /* don't allow circular references */
6706 if (event == output_event)
6710 * Don't allow cross-cpu buffers
6712 if (output_event->cpu != event->cpu)
6716 * If its not a per-cpu rb, it must be the same task.
6718 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6722 mutex_lock(&event->mmap_mutex);
6723 /* Can't redirect output if we've got an active mmap() */
6724 if (atomic_read(&event->mmap_count))
6730 /* get the rb we want to redirect to */
6731 rb = ring_buffer_get(output_event);
6737 ring_buffer_detach(event, old_rb);
6740 ring_buffer_attach(event, rb);
6742 rcu_assign_pointer(event->rb, rb);
6745 ring_buffer_put(old_rb);
6747 * Since we detached before setting the new rb, so that we
6748 * could attach the new rb, we could have missed a wakeup.
6751 wake_up_all(&event->waitq);
6756 mutex_unlock(&event->mmap_mutex);
6763 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6765 * @attr_uptr: event_id type attributes for monitoring/sampling
6768 * @group_fd: group leader event fd
6770 SYSCALL_DEFINE5(perf_event_open,
6771 struct perf_event_attr __user *, attr_uptr,
6772 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6774 struct perf_event *group_leader = NULL, *output_event = NULL;
6775 struct perf_event *event, *sibling;
6776 struct perf_event_attr attr;
6777 struct perf_event_context *ctx;
6778 struct file *event_file = NULL;
6779 struct fd group = {NULL, 0};
6780 struct task_struct *task = NULL;
6786 /* for future expandability... */
6787 if (flags & ~PERF_FLAG_ALL)
6790 err = perf_copy_attr(attr_uptr, &attr);
6794 if (!attr.exclude_kernel) {
6795 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6800 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6805 * In cgroup mode, the pid argument is used to pass the fd
6806 * opened to the cgroup directory in cgroupfs. The cpu argument
6807 * designates the cpu on which to monitor threads from that
6810 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6813 event_fd = get_unused_fd();
6817 if (group_fd != -1) {
6818 err = perf_fget_light(group_fd, &group);
6821 group_leader = group.file->private_data;
6822 if (flags & PERF_FLAG_FD_OUTPUT)
6823 output_event = group_leader;
6824 if (flags & PERF_FLAG_FD_NO_GROUP)
6825 group_leader = NULL;
6828 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
6829 task = find_lively_task_by_vpid(pid);
6831 err = PTR_ERR(task);
6838 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
6840 if (IS_ERR(event)) {
6841 err = PTR_ERR(event);
6845 if (flags & PERF_FLAG_PID_CGROUP) {
6846 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6851 * - that has cgroup constraint on event->cpu
6852 * - that may need work on context switch
6854 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6855 static_key_slow_inc(&perf_sched_events.key);
6859 * Special case software events and allow them to be part of
6860 * any hardware group.
6865 (is_software_event(event) != is_software_event(group_leader))) {
6866 if (is_software_event(event)) {
6868 * If event and group_leader are not both a software
6869 * event, and event is, then group leader is not.
6871 * Allow the addition of software events to !software
6872 * groups, this is safe because software events never
6875 pmu = group_leader->pmu;
6876 } else if (is_software_event(group_leader) &&
6877 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6879 * In case the group is a pure software group, and we
6880 * try to add a hardware event, move the whole group to
6881 * the hardware context.
6888 * Get the target context (task or percpu):
6890 ctx = find_get_context(pmu, task, event->cpu);
6897 put_task_struct(task);
6902 * Look up the group leader (we will attach this event to it):
6908 * Do not allow a recursive hierarchy (this new sibling
6909 * becoming part of another group-sibling):
6911 if (group_leader->group_leader != group_leader)
6914 * Do not allow to attach to a group in a different
6915 * task or CPU context:
6918 if (group_leader->ctx->type != ctx->type)
6921 if (group_leader->ctx != ctx)
6926 * Only a group leader can be exclusive or pinned
6928 if (attr.exclusive || attr.pinned)
6933 err = perf_event_set_output(event, output_event);
6938 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6939 if (IS_ERR(event_file)) {
6940 err = PTR_ERR(event_file);
6945 struct perf_event_context *gctx = group_leader->ctx;
6947 mutex_lock(&gctx->mutex);
6948 perf_remove_from_context(group_leader);
6951 * Removing from the context ends up with disabled
6952 * event. What we want here is event in the initial
6953 * startup state, ready to be add into new context.
6955 perf_event__state_init(group_leader);
6956 list_for_each_entry(sibling, &group_leader->sibling_list,
6958 perf_remove_from_context(sibling);
6959 perf_event__state_init(sibling);
6962 mutex_unlock(&gctx->mutex);
6966 WARN_ON_ONCE(ctx->parent_ctx);
6967 mutex_lock(&ctx->mutex);
6971 perf_install_in_context(ctx, group_leader, event->cpu);
6973 list_for_each_entry(sibling, &group_leader->sibling_list,
6975 perf_install_in_context(ctx, sibling, event->cpu);
6980 perf_install_in_context(ctx, event, event->cpu);
6982 perf_unpin_context(ctx);
6983 mutex_unlock(&ctx->mutex);
6987 event->owner = current;
6989 mutex_lock(¤t->perf_event_mutex);
6990 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6991 mutex_unlock(¤t->perf_event_mutex);
6994 * Precalculate sample_data sizes
6996 perf_event__header_size(event);
6997 perf_event__id_header_size(event);
7000 * Drop the reference on the group_event after placing the
7001 * new event on the sibling_list. This ensures destruction
7002 * of the group leader will find the pointer to itself in
7003 * perf_group_detach().
7006 fd_install(event_fd, event_file);
7010 perf_unpin_context(ctx);
7017 put_task_struct(task);
7021 put_unused_fd(event_fd);
7026 * perf_event_create_kernel_counter
7028 * @attr: attributes of the counter to create
7029 * @cpu: cpu in which the counter is bound
7030 * @task: task to profile (NULL for percpu)
7033 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
7034 struct task_struct *task,
7035 perf_overflow_handler_t overflow_handler,
7038 struct perf_event_context *ctx;
7039 struct perf_event *event;
7043 * Get the target context (task or percpu):
7046 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
7047 overflow_handler, context);
7048 if (IS_ERR(event)) {
7049 err = PTR_ERR(event);
7053 ctx = find_get_context(event->pmu, task, cpu);
7059 WARN_ON_ONCE(ctx->parent_ctx);
7060 mutex_lock(&ctx->mutex);
7061 perf_install_in_context(ctx, event, cpu);
7063 perf_unpin_context(ctx);
7064 mutex_unlock(&ctx->mutex);
7071 return ERR_PTR(err);
7073 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
7075 void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
7077 struct perf_event_context *src_ctx;
7078 struct perf_event_context *dst_ctx;
7079 struct perf_event *event, *tmp;
7082 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
7083 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
7085 mutex_lock(&src_ctx->mutex);
7086 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
7088 perf_remove_from_context(event);
7090 list_add(&event->event_entry, &events);
7092 mutex_unlock(&src_ctx->mutex);
7096 mutex_lock(&dst_ctx->mutex);
7097 list_for_each_entry_safe(event, tmp, &events, event_entry) {
7098 list_del(&event->event_entry);
7099 if (event->state >= PERF_EVENT_STATE_OFF)
7100 event->state = PERF_EVENT_STATE_INACTIVE;
7101 perf_install_in_context(dst_ctx, event, dst_cpu);
7104 mutex_unlock(&dst_ctx->mutex);
7106 EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
7108 static void sync_child_event(struct perf_event *child_event,
7109 struct task_struct *child)
7111 struct perf_event *parent_event = child_event->parent;
7114 if (child_event->attr.inherit_stat)
7115 perf_event_read_event(child_event, child);
7117 child_val = perf_event_count(child_event);
7120 * Add back the child's count to the parent's count:
7122 atomic64_add(child_val, &parent_event->child_count);
7123 atomic64_add(child_event->total_time_enabled,
7124 &parent_event->child_total_time_enabled);
7125 atomic64_add(child_event->total_time_running,
7126 &parent_event->child_total_time_running);
7129 * Remove this event from the parent's list
7131 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7132 mutex_lock(&parent_event->child_mutex);
7133 list_del_init(&child_event->child_list);
7134 mutex_unlock(&parent_event->child_mutex);
7137 * Release the parent event, if this was the last
7140 put_event(parent_event);
7144 __perf_event_exit_task(struct perf_event *child_event,
7145 struct perf_event_context *child_ctx,
7146 struct task_struct *child)
7148 if (child_event->parent) {
7149 raw_spin_lock_irq(&child_ctx->lock);
7150 perf_group_detach(child_event);
7151 raw_spin_unlock_irq(&child_ctx->lock);
7154 perf_remove_from_context(child_event);
7157 * It can happen that the parent exits first, and has events
7158 * that are still around due to the child reference. These
7159 * events need to be zapped.
7161 if (child_event->parent) {
7162 sync_child_event(child_event, child);
7163 free_event(child_event);
7167 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
7169 struct perf_event *child_event, *tmp;
7170 struct perf_event_context *child_ctx;
7171 unsigned long flags;
7173 if (likely(!child->perf_event_ctxp[ctxn])) {
7174 perf_event_task(child, NULL, 0);
7178 local_irq_save(flags);
7180 * We can't reschedule here because interrupts are disabled,
7181 * and either child is current or it is a task that can't be
7182 * scheduled, so we are now safe from rescheduling changing
7185 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
7188 * Take the context lock here so that if find_get_context is
7189 * reading child->perf_event_ctxp, we wait until it has
7190 * incremented the context's refcount before we do put_ctx below.
7192 raw_spin_lock(&child_ctx->lock);
7193 task_ctx_sched_out(child_ctx);
7194 child->perf_event_ctxp[ctxn] = NULL;
7196 * If this context is a clone; unclone it so it can't get
7197 * swapped to another process while we're removing all
7198 * the events from it.
7200 unclone_ctx(child_ctx);
7201 update_context_time(child_ctx);
7202 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
7205 * Report the task dead after unscheduling the events so that we
7206 * won't get any samples after PERF_RECORD_EXIT. We can however still
7207 * get a few PERF_RECORD_READ events.
7209 perf_event_task(child, child_ctx, 0);
7212 * We can recurse on the same lock type through:
7214 * __perf_event_exit_task()
7215 * sync_child_event()
7217 * mutex_lock(&ctx->mutex)
7219 * But since its the parent context it won't be the same instance.
7221 mutex_lock(&child_ctx->mutex);
7224 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
7226 __perf_event_exit_task(child_event, child_ctx, child);
7228 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
7230 __perf_event_exit_task(child_event, child_ctx, child);
7233 * If the last event was a group event, it will have appended all
7234 * its siblings to the list, but we obtained 'tmp' before that which
7235 * will still point to the list head terminating the iteration.
7237 if (!list_empty(&child_ctx->pinned_groups) ||
7238 !list_empty(&child_ctx->flexible_groups))
7241 mutex_unlock(&child_ctx->mutex);
7247 * When a child task exits, feed back event values to parent events.
7249 void perf_event_exit_task(struct task_struct *child)
7251 struct perf_event *event, *tmp;
7254 mutex_lock(&child->perf_event_mutex);
7255 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
7257 list_del_init(&event->owner_entry);
7260 * Ensure the list deletion is visible before we clear
7261 * the owner, closes a race against perf_release() where
7262 * we need to serialize on the owner->perf_event_mutex.
7265 event->owner = NULL;
7267 mutex_unlock(&child->perf_event_mutex);
7269 for_each_task_context_nr(ctxn)
7270 perf_event_exit_task_context(child, ctxn);
7273 static void perf_free_event(struct perf_event *event,
7274 struct perf_event_context *ctx)
7276 struct perf_event *parent = event->parent;
7278 if (WARN_ON_ONCE(!parent))
7281 mutex_lock(&parent->child_mutex);
7282 list_del_init(&event->child_list);
7283 mutex_unlock(&parent->child_mutex);
7287 perf_group_detach(event);
7288 list_del_event(event, ctx);
7293 * free an unexposed, unused context as created by inheritance by
7294 * perf_event_init_task below, used by fork() in case of fail.
7296 void perf_event_free_task(struct task_struct *task)
7298 struct perf_event_context *ctx;
7299 struct perf_event *event, *tmp;
7302 for_each_task_context_nr(ctxn) {
7303 ctx = task->perf_event_ctxp[ctxn];
7307 mutex_lock(&ctx->mutex);
7309 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
7311 perf_free_event(event, ctx);
7313 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
7315 perf_free_event(event, ctx);
7317 if (!list_empty(&ctx->pinned_groups) ||
7318 !list_empty(&ctx->flexible_groups))
7321 mutex_unlock(&ctx->mutex);
7327 void perf_event_delayed_put(struct task_struct *task)
7331 for_each_task_context_nr(ctxn)
7332 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
7336 * inherit a event from parent task to child task:
7338 static struct perf_event *
7339 inherit_event(struct perf_event *parent_event,
7340 struct task_struct *parent,
7341 struct perf_event_context *parent_ctx,
7342 struct task_struct *child,
7343 struct perf_event *group_leader,
7344 struct perf_event_context *child_ctx)
7346 struct perf_event *child_event;
7347 unsigned long flags;
7350 * Instead of creating recursive hierarchies of events,
7351 * we link inherited events back to the original parent,
7352 * which has a filp for sure, which we use as the reference
7355 if (parent_event->parent)
7356 parent_event = parent_event->parent;
7358 child_event = perf_event_alloc(&parent_event->attr,
7361 group_leader, parent_event,
7363 if (IS_ERR(child_event))
7366 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
7367 free_event(child_event);
7374 * Make the child state follow the state of the parent event,
7375 * not its attr.disabled bit. We hold the parent's mutex,
7376 * so we won't race with perf_event_{en, dis}able_family.
7378 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
7379 child_event->state = PERF_EVENT_STATE_INACTIVE;
7381 child_event->state = PERF_EVENT_STATE_OFF;
7383 if (parent_event->attr.freq) {
7384 u64 sample_period = parent_event->hw.sample_period;
7385 struct hw_perf_event *hwc = &child_event->hw;
7387 hwc->sample_period = sample_period;
7388 hwc->last_period = sample_period;
7390 local64_set(&hwc->period_left, sample_period);
7393 child_event->ctx = child_ctx;
7394 child_event->overflow_handler = parent_event->overflow_handler;
7395 child_event->overflow_handler_context
7396 = parent_event->overflow_handler_context;
7399 * Precalculate sample_data sizes
7401 perf_event__header_size(child_event);
7402 perf_event__id_header_size(child_event);
7405 * Link it up in the child's context:
7407 raw_spin_lock_irqsave(&child_ctx->lock, flags);
7408 add_event_to_ctx(child_event, child_ctx);
7409 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
7412 * Link this into the parent event's child list
7414 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7415 mutex_lock(&parent_event->child_mutex);
7416 list_add_tail(&child_event->child_list, &parent_event->child_list);
7417 mutex_unlock(&parent_event->child_mutex);
7422 static int inherit_group(struct perf_event *parent_event,
7423 struct task_struct *parent,
7424 struct perf_event_context *parent_ctx,
7425 struct task_struct *child,
7426 struct perf_event_context *child_ctx)
7428 struct perf_event *leader;
7429 struct perf_event *sub;
7430 struct perf_event *child_ctr;
7432 leader = inherit_event(parent_event, parent, parent_ctx,
7433 child, NULL, child_ctx);
7435 return PTR_ERR(leader);
7436 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7437 child_ctr = inherit_event(sub, parent, parent_ctx,
7438 child, leader, child_ctx);
7439 if (IS_ERR(child_ctr))
7440 return PTR_ERR(child_ctr);
7446 inherit_task_group(struct perf_event *event, struct task_struct *parent,
7447 struct perf_event_context *parent_ctx,
7448 struct task_struct *child, int ctxn,
7452 struct perf_event_context *child_ctx;
7454 if (!event->attr.inherit) {
7459 child_ctx = child->perf_event_ctxp[ctxn];
7462 * This is executed from the parent task context, so
7463 * inherit events that have been marked for cloning.
7464 * First allocate and initialize a context for the
7468 child_ctx = alloc_perf_context(event->pmu, child);
7472 child->perf_event_ctxp[ctxn] = child_ctx;
7475 ret = inherit_group(event, parent, parent_ctx,
7485 * Initialize the perf_event context in task_struct
7487 int perf_event_init_context(struct task_struct *child, int ctxn)
7489 struct perf_event_context *child_ctx, *parent_ctx;
7490 struct perf_event_context *cloned_ctx;
7491 struct perf_event *event;
7492 struct task_struct *parent = current;
7493 int inherited_all = 1;
7494 unsigned long flags;
7497 if (likely(!parent->perf_event_ctxp[ctxn]))
7501 * If the parent's context is a clone, pin it so it won't get
7504 parent_ctx = perf_pin_task_context(parent, ctxn);
7507 * No need to check if parent_ctx != NULL here; since we saw
7508 * it non-NULL earlier, the only reason for it to become NULL
7509 * is if we exit, and since we're currently in the middle of
7510 * a fork we can't be exiting at the same time.
7514 * Lock the parent list. No need to lock the child - not PID
7515 * hashed yet and not running, so nobody can access it.
7517 mutex_lock(&parent_ctx->mutex);
7520 * We dont have to disable NMIs - we are only looking at
7521 * the list, not manipulating it:
7523 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
7524 ret = inherit_task_group(event, parent, parent_ctx,
7525 child, ctxn, &inherited_all);
7531 * We can't hold ctx->lock when iterating the ->flexible_group list due
7532 * to allocations, but we need to prevent rotation because
7533 * rotate_ctx() will change the list from interrupt context.
7535 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7536 parent_ctx->rotate_disable = 1;
7537 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7539 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
7540 ret = inherit_task_group(event, parent, parent_ctx,
7541 child, ctxn, &inherited_all);
7546 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7547 parent_ctx->rotate_disable = 0;
7549 child_ctx = child->perf_event_ctxp[ctxn];
7551 if (child_ctx && inherited_all) {
7553 * Mark the child context as a clone of the parent
7554 * context, or of whatever the parent is a clone of.
7556 * Note that if the parent is a clone, the holding of
7557 * parent_ctx->lock avoids it from being uncloned.
7559 cloned_ctx = parent_ctx->parent_ctx;
7561 child_ctx->parent_ctx = cloned_ctx;
7562 child_ctx->parent_gen = parent_ctx->parent_gen;
7564 child_ctx->parent_ctx = parent_ctx;
7565 child_ctx->parent_gen = parent_ctx->generation;
7567 get_ctx(child_ctx->parent_ctx);
7570 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7571 mutex_unlock(&parent_ctx->mutex);
7573 perf_unpin_context(parent_ctx);
7574 put_ctx(parent_ctx);
7580 * Initialize the perf_event context in task_struct
7582 int perf_event_init_task(struct task_struct *child)
7586 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7587 mutex_init(&child->perf_event_mutex);
7588 INIT_LIST_HEAD(&child->perf_event_list);
7590 for_each_task_context_nr(ctxn) {
7591 ret = perf_event_init_context(child, ctxn);
7599 static void __init perf_event_init_all_cpus(void)
7601 struct swevent_htable *swhash;
7604 for_each_possible_cpu(cpu) {
7605 swhash = &per_cpu(swevent_htable, cpu);
7606 mutex_init(&swhash->hlist_mutex);
7607 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
7611 static void __cpuinit perf_event_init_cpu(int cpu)
7613 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7615 mutex_lock(&swhash->hlist_mutex);
7616 if (swhash->hlist_refcount > 0) {
7617 struct swevent_hlist *hlist;
7619 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7621 rcu_assign_pointer(swhash->swevent_hlist, hlist);
7623 mutex_unlock(&swhash->hlist_mutex);
7626 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7627 static void perf_pmu_rotate_stop(struct pmu *pmu)
7629 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7631 WARN_ON(!irqs_disabled());
7633 list_del_init(&cpuctx->rotation_list);
7636 static void __perf_event_exit_context(void *__info)
7638 struct perf_event_context *ctx = __info;
7639 struct perf_event *event, *tmp;
7641 perf_pmu_rotate_stop(ctx->pmu);
7643 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
7644 __perf_remove_from_context(event);
7645 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
7646 __perf_remove_from_context(event);
7649 static void perf_event_exit_cpu_context(int cpu)
7651 struct perf_event_context *ctx;
7655 idx = srcu_read_lock(&pmus_srcu);
7656 list_for_each_entry_rcu(pmu, &pmus, entry) {
7657 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
7659 mutex_lock(&ctx->mutex);
7660 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7661 mutex_unlock(&ctx->mutex);
7663 srcu_read_unlock(&pmus_srcu, idx);
7666 static void perf_event_exit_cpu(int cpu)
7668 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7670 mutex_lock(&swhash->hlist_mutex);
7671 swevent_hlist_release(swhash);
7672 mutex_unlock(&swhash->hlist_mutex);
7674 perf_event_exit_cpu_context(cpu);
7677 static inline void perf_event_exit_cpu(int cpu) { }
7681 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7685 for_each_online_cpu(cpu)
7686 perf_event_exit_cpu(cpu);
7692 * Run the perf reboot notifier at the very last possible moment so that
7693 * the generic watchdog code runs as long as possible.
7695 static struct notifier_block perf_reboot_notifier = {
7696 .notifier_call = perf_reboot,
7697 .priority = INT_MIN,
7700 static int __cpuinit
7701 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7703 unsigned int cpu = (long)hcpu;
7705 switch (action & ~CPU_TASKS_FROZEN) {
7707 case CPU_UP_PREPARE:
7708 case CPU_DOWN_FAILED:
7709 perf_event_init_cpu(cpu);
7712 case CPU_UP_CANCELED:
7713 case CPU_DOWN_PREPARE:
7714 perf_event_exit_cpu(cpu);
7723 void __init perf_event_init(void)
7729 perf_event_init_all_cpus();
7730 init_srcu_struct(&pmus_srcu);
7731 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7732 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7733 perf_pmu_register(&perf_task_clock, NULL, -1);
7735 perf_cpu_notifier(perf_cpu_notify);
7736 register_reboot_notifier(&perf_reboot_notifier);
7738 ret = init_hw_breakpoint();
7739 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7741 /* do not patch jump label more than once per second */
7742 jump_label_rate_limit(&perf_sched_events, HZ);
7745 * Build time assertion that we keep the data_head at the intended
7746 * location. IOW, validation we got the __reserved[] size right.
7748 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
7752 static int __init perf_event_sysfs_init(void)
7757 mutex_lock(&pmus_lock);
7759 ret = bus_register(&pmu_bus);
7763 list_for_each_entry(pmu, &pmus, entry) {
7764 if (!pmu->name || pmu->type < 0)
7767 ret = pmu_dev_alloc(pmu);
7768 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7770 pmu_bus_running = 1;
7774 mutex_unlock(&pmus_lock);
7778 device_initcall(perf_event_sysfs_init);
7780 #ifdef CONFIG_CGROUP_PERF
7781 static struct cgroup_subsys_state *perf_cgroup_css_alloc(struct cgroup *cont)
7783 struct perf_cgroup *jc;
7785 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
7787 return ERR_PTR(-ENOMEM);
7789 jc->info = alloc_percpu(struct perf_cgroup_info);
7792 return ERR_PTR(-ENOMEM);
7798 static void perf_cgroup_css_free(struct cgroup *cont)
7800 struct perf_cgroup *jc;
7801 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7802 struct perf_cgroup, css);
7803 free_percpu(jc->info);
7807 static int __perf_cgroup_move(void *info)
7809 struct task_struct *task = info;
7810 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7814 static void perf_cgroup_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
7816 struct task_struct *task;
7818 cgroup_taskset_for_each(task, cgrp, tset)
7819 task_function_call(task, __perf_cgroup_move, task);
7822 static void perf_cgroup_exit(struct cgroup *cgrp, struct cgroup *old_cgrp,
7823 struct task_struct *task)
7826 * cgroup_exit() is called in the copy_process() failure path.
7827 * Ignore this case since the task hasn't ran yet, this avoids
7828 * trying to poke a half freed task state from generic code.
7830 if (!(task->flags & PF_EXITING))
7833 task_function_call(task, __perf_cgroup_move, task);
7836 struct cgroup_subsys perf_subsys = {
7837 .name = "perf_event",
7838 .subsys_id = perf_subsys_id,
7839 .css_alloc = perf_cgroup_css_alloc,
7840 .css_free = perf_cgroup_css_free,
7841 .exit = perf_cgroup_exit,
7842 .attach = perf_cgroup_attach,
7844 #endif /* CONFIG_CGROUP_PERF */