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;
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;
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_css(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);
596 css = css_from_dir(f.file->f_dentry, &perf_subsys);
602 cgrp = container_of(css, struct perf_cgroup, css);
605 /* must be done before we fput() the file */
606 if (!perf_tryget_cgroup(event)) {
613 * all events in a group must monitor
614 * the same cgroup because a task belongs
615 * to only one perf cgroup at a time
617 if (group_leader && group_leader->cgrp != cgrp) {
618 perf_detach_cgroup(event);
628 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
630 struct perf_cgroup_info *t;
631 t = per_cpu_ptr(event->cgrp->info, event->cpu);
632 event->shadow_ctx_time = now - t->timestamp;
636 perf_cgroup_defer_enabled(struct perf_event *event)
639 * when the current task's perf cgroup does not match
640 * the event's, we need to remember to call the
641 * perf_mark_enable() function the first time a task with
642 * a matching perf cgroup is scheduled in.
644 if (is_cgroup_event(event) && !perf_cgroup_match(event))
645 event->cgrp_defer_enabled = 1;
649 perf_cgroup_mark_enabled(struct perf_event *event,
650 struct perf_event_context *ctx)
652 struct perf_event *sub;
653 u64 tstamp = perf_event_time(event);
655 if (!event->cgrp_defer_enabled)
658 event->cgrp_defer_enabled = 0;
660 event->tstamp_enabled = tstamp - event->total_time_enabled;
661 list_for_each_entry(sub, &event->sibling_list, group_entry) {
662 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
663 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
664 sub->cgrp_defer_enabled = 0;
668 #else /* !CONFIG_CGROUP_PERF */
671 perf_cgroup_match(struct perf_event *event)
676 static inline void perf_detach_cgroup(struct perf_event *event)
679 static inline int is_cgroup_event(struct perf_event *event)
684 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
689 static inline void update_cgrp_time_from_event(struct perf_event *event)
693 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
697 static inline void perf_cgroup_sched_out(struct task_struct *task,
698 struct task_struct *next)
702 static inline void perf_cgroup_sched_in(struct task_struct *prev,
703 struct task_struct *task)
707 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
708 struct perf_event_attr *attr,
709 struct perf_event *group_leader)
715 perf_cgroup_set_timestamp(struct task_struct *task,
716 struct perf_event_context *ctx)
721 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
726 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
730 static inline u64 perf_cgroup_event_time(struct perf_event *event)
736 perf_cgroup_defer_enabled(struct perf_event *event)
741 perf_cgroup_mark_enabled(struct perf_event *event,
742 struct perf_event_context *ctx)
748 * set default to be dependent on timer tick just
751 #define PERF_CPU_HRTIMER (1000 / HZ)
753 * function must be called with interrupts disbled
755 static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
757 struct perf_cpu_context *cpuctx;
758 enum hrtimer_restart ret = HRTIMER_NORESTART;
761 WARN_ON(!irqs_disabled());
763 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
765 rotations = perf_rotate_context(cpuctx);
768 * arm timer if needed
771 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
772 ret = HRTIMER_RESTART;
778 /* CPU is going down */
779 void perf_cpu_hrtimer_cancel(int cpu)
781 struct perf_cpu_context *cpuctx;
785 if (WARN_ON(cpu != smp_processor_id()))
788 local_irq_save(flags);
792 list_for_each_entry_rcu(pmu, &pmus, entry) {
793 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
795 if (pmu->task_ctx_nr == perf_sw_context)
798 hrtimer_cancel(&cpuctx->hrtimer);
803 local_irq_restore(flags);
806 static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
808 struct hrtimer *hr = &cpuctx->hrtimer;
809 struct pmu *pmu = cpuctx->ctx.pmu;
812 /* no multiplexing needed for SW PMU */
813 if (pmu->task_ctx_nr == perf_sw_context)
817 * check default is sane, if not set then force to
818 * default interval (1/tick)
820 timer = pmu->hrtimer_interval_ms;
822 timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
824 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
826 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
827 hr->function = perf_cpu_hrtimer_handler;
830 static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
832 struct hrtimer *hr = &cpuctx->hrtimer;
833 struct pmu *pmu = cpuctx->ctx.pmu;
836 if (pmu->task_ctx_nr == perf_sw_context)
839 if (hrtimer_active(hr))
842 if (!hrtimer_callback_running(hr))
843 __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
844 0, HRTIMER_MODE_REL_PINNED, 0);
847 void perf_pmu_disable(struct pmu *pmu)
849 int *count = this_cpu_ptr(pmu->pmu_disable_count);
851 pmu->pmu_disable(pmu);
854 void perf_pmu_enable(struct pmu *pmu)
856 int *count = this_cpu_ptr(pmu->pmu_disable_count);
858 pmu->pmu_enable(pmu);
861 static DEFINE_PER_CPU(struct list_head, rotation_list);
864 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
865 * because they're strictly cpu affine and rotate_start is called with IRQs
866 * disabled, while rotate_context is called from IRQ context.
868 static void perf_pmu_rotate_start(struct pmu *pmu)
870 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
871 struct list_head *head = &__get_cpu_var(rotation_list);
873 WARN_ON(!irqs_disabled());
875 if (list_empty(&cpuctx->rotation_list)) {
876 int was_empty = list_empty(head);
877 list_add(&cpuctx->rotation_list, head);
879 tick_nohz_full_kick();
883 static void get_ctx(struct perf_event_context *ctx)
885 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
888 static void put_ctx(struct perf_event_context *ctx)
890 if (atomic_dec_and_test(&ctx->refcount)) {
892 put_ctx(ctx->parent_ctx);
894 put_task_struct(ctx->task);
895 kfree_rcu(ctx, rcu_head);
899 static void unclone_ctx(struct perf_event_context *ctx)
901 if (ctx->parent_ctx) {
902 put_ctx(ctx->parent_ctx);
903 ctx->parent_ctx = NULL;
907 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
910 * only top level events have the pid namespace they were created in
913 event = event->parent;
915 return task_tgid_nr_ns(p, event->ns);
918 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
921 * only top level events have the pid namespace they were created in
924 event = event->parent;
926 return task_pid_nr_ns(p, event->ns);
930 * If we inherit events we want to return the parent event id
933 static u64 primary_event_id(struct perf_event *event)
938 id = event->parent->id;
944 * Get the perf_event_context for a task and lock it.
945 * This has to cope with with the fact that until it is locked,
946 * the context could get moved to another task.
948 static struct perf_event_context *
949 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
951 struct perf_event_context *ctx;
955 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
958 * If this context is a clone of another, it might
959 * get swapped for another underneath us by
960 * perf_event_task_sched_out, though the
961 * rcu_read_lock() protects us from any context
962 * getting freed. Lock the context and check if it
963 * got swapped before we could get the lock, and retry
964 * if so. If we locked the right context, then it
965 * can't get swapped on us any more.
967 raw_spin_lock_irqsave(&ctx->lock, *flags);
968 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
969 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
973 if (!atomic_inc_not_zero(&ctx->refcount)) {
974 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
983 * Get the context for a task and increment its pin_count so it
984 * can't get swapped to another task. This also increments its
985 * reference count so that the context can't get freed.
987 static struct perf_event_context *
988 perf_pin_task_context(struct task_struct *task, int ctxn)
990 struct perf_event_context *ctx;
993 ctx = perf_lock_task_context(task, ctxn, &flags);
996 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1001 static void perf_unpin_context(struct perf_event_context *ctx)
1003 unsigned long flags;
1005 raw_spin_lock_irqsave(&ctx->lock, flags);
1007 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1011 * Update the record of the current time in a context.
1013 static void update_context_time(struct perf_event_context *ctx)
1015 u64 now = perf_clock();
1017 ctx->time += now - ctx->timestamp;
1018 ctx->timestamp = now;
1021 static u64 perf_event_time(struct perf_event *event)
1023 struct perf_event_context *ctx = event->ctx;
1025 if (is_cgroup_event(event))
1026 return perf_cgroup_event_time(event);
1028 return ctx ? ctx->time : 0;
1032 * Update the total_time_enabled and total_time_running fields for a event.
1033 * The caller of this function needs to hold the ctx->lock.
1035 static void update_event_times(struct perf_event *event)
1037 struct perf_event_context *ctx = event->ctx;
1040 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1041 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1044 * in cgroup mode, time_enabled represents
1045 * the time the event was enabled AND active
1046 * tasks were in the monitored cgroup. This is
1047 * independent of the activity of the context as
1048 * there may be a mix of cgroup and non-cgroup events.
1050 * That is why we treat cgroup events differently
1053 if (is_cgroup_event(event))
1054 run_end = perf_cgroup_event_time(event);
1055 else if (ctx->is_active)
1056 run_end = ctx->time;
1058 run_end = event->tstamp_stopped;
1060 event->total_time_enabled = run_end - event->tstamp_enabled;
1062 if (event->state == PERF_EVENT_STATE_INACTIVE)
1063 run_end = event->tstamp_stopped;
1065 run_end = perf_event_time(event);
1067 event->total_time_running = run_end - event->tstamp_running;
1072 * Update total_time_enabled and total_time_running for all events in a group.
1074 static void update_group_times(struct perf_event *leader)
1076 struct perf_event *event;
1078 update_event_times(leader);
1079 list_for_each_entry(event, &leader->sibling_list, group_entry)
1080 update_event_times(event);
1083 static struct list_head *
1084 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1086 if (event->attr.pinned)
1087 return &ctx->pinned_groups;
1089 return &ctx->flexible_groups;
1093 * Add a event from the lists for its context.
1094 * Must be called with ctx->mutex and ctx->lock held.
1097 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1099 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1100 event->attach_state |= PERF_ATTACH_CONTEXT;
1103 * If we're a stand alone event or group leader, we go to the context
1104 * list, group events are kept attached to the group so that
1105 * perf_group_detach can, at all times, locate all siblings.
1107 if (event->group_leader == event) {
1108 struct list_head *list;
1110 if (is_software_event(event))
1111 event->group_flags |= PERF_GROUP_SOFTWARE;
1113 list = ctx_group_list(event, ctx);
1114 list_add_tail(&event->group_entry, list);
1117 if (is_cgroup_event(event))
1120 if (has_branch_stack(event))
1121 ctx->nr_branch_stack++;
1123 list_add_rcu(&event->event_entry, &ctx->event_list);
1124 if (!ctx->nr_events)
1125 perf_pmu_rotate_start(ctx->pmu);
1127 if (event->attr.inherit_stat)
1132 * Initialize event state based on the perf_event_attr::disabled.
1134 static inline void perf_event__state_init(struct perf_event *event)
1136 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1137 PERF_EVENT_STATE_INACTIVE;
1141 * Called at perf_event creation and when events are attached/detached from a
1144 static void perf_event__read_size(struct perf_event *event)
1146 int entry = sizeof(u64); /* value */
1150 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1151 size += sizeof(u64);
1153 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1154 size += sizeof(u64);
1156 if (event->attr.read_format & PERF_FORMAT_ID)
1157 entry += sizeof(u64);
1159 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1160 nr += event->group_leader->nr_siblings;
1161 size += sizeof(u64);
1165 event->read_size = size;
1168 static void perf_event__header_size(struct perf_event *event)
1170 struct perf_sample_data *data;
1171 u64 sample_type = event->attr.sample_type;
1174 perf_event__read_size(event);
1176 if (sample_type & PERF_SAMPLE_IP)
1177 size += sizeof(data->ip);
1179 if (sample_type & PERF_SAMPLE_ADDR)
1180 size += sizeof(data->addr);
1182 if (sample_type & PERF_SAMPLE_PERIOD)
1183 size += sizeof(data->period);
1185 if (sample_type & PERF_SAMPLE_WEIGHT)
1186 size += sizeof(data->weight);
1188 if (sample_type & PERF_SAMPLE_READ)
1189 size += event->read_size;
1191 if (sample_type & PERF_SAMPLE_DATA_SRC)
1192 size += sizeof(data->data_src.val);
1194 event->header_size = size;
1197 static void perf_event__id_header_size(struct perf_event *event)
1199 struct perf_sample_data *data;
1200 u64 sample_type = event->attr.sample_type;
1203 if (sample_type & PERF_SAMPLE_TID)
1204 size += sizeof(data->tid_entry);
1206 if (sample_type & PERF_SAMPLE_TIME)
1207 size += sizeof(data->time);
1209 if (sample_type & PERF_SAMPLE_ID)
1210 size += sizeof(data->id);
1212 if (sample_type & PERF_SAMPLE_STREAM_ID)
1213 size += sizeof(data->stream_id);
1215 if (sample_type & PERF_SAMPLE_CPU)
1216 size += sizeof(data->cpu_entry);
1218 event->id_header_size = size;
1221 static void perf_group_attach(struct perf_event *event)
1223 struct perf_event *group_leader = event->group_leader, *pos;
1226 * We can have double attach due to group movement in perf_event_open.
1228 if (event->attach_state & PERF_ATTACH_GROUP)
1231 event->attach_state |= PERF_ATTACH_GROUP;
1233 if (group_leader == event)
1236 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1237 !is_software_event(event))
1238 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1240 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1241 group_leader->nr_siblings++;
1243 perf_event__header_size(group_leader);
1245 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1246 perf_event__header_size(pos);
1250 * Remove a event from the lists for its context.
1251 * Must be called with ctx->mutex and ctx->lock held.
1254 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1256 struct perf_cpu_context *cpuctx;
1258 * We can have double detach due to exit/hot-unplug + close.
1260 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
1263 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1265 if (is_cgroup_event(event)) {
1267 cpuctx = __get_cpu_context(ctx);
1269 * if there are no more cgroup events
1270 * then cler cgrp to avoid stale pointer
1271 * in update_cgrp_time_from_cpuctx()
1273 if (!ctx->nr_cgroups)
1274 cpuctx->cgrp = NULL;
1277 if (has_branch_stack(event))
1278 ctx->nr_branch_stack--;
1281 if (event->attr.inherit_stat)
1284 list_del_rcu(&event->event_entry);
1286 if (event->group_leader == event)
1287 list_del_init(&event->group_entry);
1289 update_group_times(event);
1292 * If event was in error state, then keep it
1293 * that way, otherwise bogus counts will be
1294 * returned on read(). The only way to get out
1295 * of error state is by explicit re-enabling
1298 if (event->state > PERF_EVENT_STATE_OFF)
1299 event->state = PERF_EVENT_STATE_OFF;
1302 static void perf_group_detach(struct perf_event *event)
1304 struct perf_event *sibling, *tmp;
1305 struct list_head *list = NULL;
1308 * We can have double detach due to exit/hot-unplug + close.
1310 if (!(event->attach_state & PERF_ATTACH_GROUP))
1313 event->attach_state &= ~PERF_ATTACH_GROUP;
1316 * If this is a sibling, remove it from its group.
1318 if (event->group_leader != event) {
1319 list_del_init(&event->group_entry);
1320 event->group_leader->nr_siblings--;
1324 if (!list_empty(&event->group_entry))
1325 list = &event->group_entry;
1328 * If this was a group event with sibling events then
1329 * upgrade the siblings to singleton events by adding them
1330 * to whatever list we are on.
1332 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
1334 list_move_tail(&sibling->group_entry, list);
1335 sibling->group_leader = sibling;
1337 /* Inherit group flags from the previous leader */
1338 sibling->group_flags = event->group_flags;
1342 perf_event__header_size(event->group_leader);
1344 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1345 perf_event__header_size(tmp);
1349 event_filter_match(struct perf_event *event)
1351 return (event->cpu == -1 || event->cpu == smp_processor_id())
1352 && perf_cgroup_match(event);
1356 event_sched_out(struct perf_event *event,
1357 struct perf_cpu_context *cpuctx,
1358 struct perf_event_context *ctx)
1360 u64 tstamp = perf_event_time(event);
1363 * An event which could not be activated because of
1364 * filter mismatch still needs to have its timings
1365 * maintained, otherwise bogus information is return
1366 * via read() for time_enabled, time_running:
1368 if (event->state == PERF_EVENT_STATE_INACTIVE
1369 && !event_filter_match(event)) {
1370 delta = tstamp - event->tstamp_stopped;
1371 event->tstamp_running += delta;
1372 event->tstamp_stopped = tstamp;
1375 if (event->state != PERF_EVENT_STATE_ACTIVE)
1378 event->state = PERF_EVENT_STATE_INACTIVE;
1379 if (event->pending_disable) {
1380 event->pending_disable = 0;
1381 event->state = PERF_EVENT_STATE_OFF;
1383 event->tstamp_stopped = tstamp;
1384 event->pmu->del(event, 0);
1387 if (!is_software_event(event))
1388 cpuctx->active_oncpu--;
1390 if (event->attr.freq && event->attr.sample_freq)
1392 if (event->attr.exclusive || !cpuctx->active_oncpu)
1393 cpuctx->exclusive = 0;
1397 group_sched_out(struct perf_event *group_event,
1398 struct perf_cpu_context *cpuctx,
1399 struct perf_event_context *ctx)
1401 struct perf_event *event;
1402 int state = group_event->state;
1404 event_sched_out(group_event, cpuctx, ctx);
1407 * Schedule out siblings (if any):
1409 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1410 event_sched_out(event, cpuctx, ctx);
1412 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
1413 cpuctx->exclusive = 0;
1417 * Cross CPU call to remove a performance event
1419 * We disable the event on the hardware level first. After that we
1420 * remove it from the context list.
1422 static int __perf_remove_from_context(void *info)
1424 struct perf_event *event = info;
1425 struct perf_event_context *ctx = event->ctx;
1426 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1428 raw_spin_lock(&ctx->lock);
1429 event_sched_out(event, cpuctx, ctx);
1430 list_del_event(event, ctx);
1431 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1433 cpuctx->task_ctx = NULL;
1435 raw_spin_unlock(&ctx->lock);
1442 * Remove the event from a task's (or a CPU's) list of events.
1444 * CPU events are removed with a smp call. For task events we only
1445 * call when the task is on a CPU.
1447 * If event->ctx is a cloned context, callers must make sure that
1448 * every task struct that event->ctx->task could possibly point to
1449 * remains valid. This is OK when called from perf_release since
1450 * that only calls us on the top-level context, which can't be a clone.
1451 * When called from perf_event_exit_task, it's OK because the
1452 * context has been detached from its task.
1454 static void perf_remove_from_context(struct perf_event *event)
1456 struct perf_event_context *ctx = event->ctx;
1457 struct task_struct *task = ctx->task;
1459 lockdep_assert_held(&ctx->mutex);
1463 * Per cpu events are removed via an smp call and
1464 * the removal is always successful.
1466 cpu_function_call(event->cpu, __perf_remove_from_context, event);
1471 if (!task_function_call(task, __perf_remove_from_context, event))
1474 raw_spin_lock_irq(&ctx->lock);
1476 * If we failed to find a running task, but find the context active now
1477 * that we've acquired the ctx->lock, retry.
1479 if (ctx->is_active) {
1480 raw_spin_unlock_irq(&ctx->lock);
1485 * Since the task isn't running, its safe to remove the event, us
1486 * holding the ctx->lock ensures the task won't get scheduled in.
1488 list_del_event(event, ctx);
1489 raw_spin_unlock_irq(&ctx->lock);
1493 * Cross CPU call to disable a performance event
1495 int __perf_event_disable(void *info)
1497 struct perf_event *event = info;
1498 struct perf_event_context *ctx = event->ctx;
1499 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1502 * If this is a per-task event, need to check whether this
1503 * event's task is the current task on this cpu.
1505 * Can trigger due to concurrent perf_event_context_sched_out()
1506 * flipping contexts around.
1508 if (ctx->task && cpuctx->task_ctx != ctx)
1511 raw_spin_lock(&ctx->lock);
1514 * If the event is on, turn it off.
1515 * If it is in error state, leave it in error state.
1517 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1518 update_context_time(ctx);
1519 update_cgrp_time_from_event(event);
1520 update_group_times(event);
1521 if (event == event->group_leader)
1522 group_sched_out(event, cpuctx, ctx);
1524 event_sched_out(event, cpuctx, ctx);
1525 event->state = PERF_EVENT_STATE_OFF;
1528 raw_spin_unlock(&ctx->lock);
1536 * If event->ctx is a cloned context, callers must make sure that
1537 * every task struct that event->ctx->task could possibly point to
1538 * remains valid. This condition is satisifed when called through
1539 * perf_event_for_each_child or perf_event_for_each because they
1540 * hold the top-level event's child_mutex, so any descendant that
1541 * goes to exit will block in sync_child_event.
1542 * When called from perf_pending_event it's OK because event->ctx
1543 * is the current context on this CPU and preemption is disabled,
1544 * hence we can't get into perf_event_task_sched_out for this context.
1546 void perf_event_disable(struct perf_event *event)
1548 struct perf_event_context *ctx = event->ctx;
1549 struct task_struct *task = ctx->task;
1553 * Disable the event on the cpu that it's on
1555 cpu_function_call(event->cpu, __perf_event_disable, event);
1560 if (!task_function_call(task, __perf_event_disable, event))
1563 raw_spin_lock_irq(&ctx->lock);
1565 * If the event is still active, we need to retry the cross-call.
1567 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1568 raw_spin_unlock_irq(&ctx->lock);
1570 * Reload the task pointer, it might have been changed by
1571 * a concurrent perf_event_context_sched_out().
1578 * Since we have the lock this context can't be scheduled
1579 * in, so we can change the state safely.
1581 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1582 update_group_times(event);
1583 event->state = PERF_EVENT_STATE_OFF;
1585 raw_spin_unlock_irq(&ctx->lock);
1587 EXPORT_SYMBOL_GPL(perf_event_disable);
1589 static void perf_set_shadow_time(struct perf_event *event,
1590 struct perf_event_context *ctx,
1594 * use the correct time source for the time snapshot
1596 * We could get by without this by leveraging the
1597 * fact that to get to this function, the caller
1598 * has most likely already called update_context_time()
1599 * and update_cgrp_time_xx() and thus both timestamp
1600 * are identical (or very close). Given that tstamp is,
1601 * already adjusted for cgroup, we could say that:
1602 * tstamp - ctx->timestamp
1604 * tstamp - cgrp->timestamp.
1606 * Then, in perf_output_read(), the calculation would
1607 * work with no changes because:
1608 * - event is guaranteed scheduled in
1609 * - no scheduled out in between
1610 * - thus the timestamp would be the same
1612 * But this is a bit hairy.
1614 * So instead, we have an explicit cgroup call to remain
1615 * within the time time source all along. We believe it
1616 * is cleaner and simpler to understand.
1618 if (is_cgroup_event(event))
1619 perf_cgroup_set_shadow_time(event, tstamp);
1621 event->shadow_ctx_time = tstamp - ctx->timestamp;
1624 #define MAX_INTERRUPTS (~0ULL)
1626 static void perf_log_throttle(struct perf_event *event, int enable);
1629 event_sched_in(struct perf_event *event,
1630 struct perf_cpu_context *cpuctx,
1631 struct perf_event_context *ctx)
1633 u64 tstamp = perf_event_time(event);
1635 if (event->state <= PERF_EVENT_STATE_OFF)
1638 event->state = PERF_EVENT_STATE_ACTIVE;
1639 event->oncpu = smp_processor_id();
1642 * Unthrottle events, since we scheduled we might have missed several
1643 * ticks already, also for a heavily scheduling task there is little
1644 * guarantee it'll get a tick in a timely manner.
1646 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1647 perf_log_throttle(event, 1);
1648 event->hw.interrupts = 0;
1652 * The new state must be visible before we turn it on in the hardware:
1656 if (event->pmu->add(event, PERF_EF_START)) {
1657 event->state = PERF_EVENT_STATE_INACTIVE;
1662 event->tstamp_running += tstamp - event->tstamp_stopped;
1664 perf_set_shadow_time(event, ctx, tstamp);
1666 if (!is_software_event(event))
1667 cpuctx->active_oncpu++;
1669 if (event->attr.freq && event->attr.sample_freq)
1672 if (event->attr.exclusive)
1673 cpuctx->exclusive = 1;
1679 group_sched_in(struct perf_event *group_event,
1680 struct perf_cpu_context *cpuctx,
1681 struct perf_event_context *ctx)
1683 struct perf_event *event, *partial_group = NULL;
1684 struct pmu *pmu = group_event->pmu;
1685 u64 now = ctx->time;
1686 bool simulate = false;
1688 if (group_event->state == PERF_EVENT_STATE_OFF)
1691 pmu->start_txn(pmu);
1693 if (event_sched_in(group_event, cpuctx, ctx)) {
1694 pmu->cancel_txn(pmu);
1695 perf_cpu_hrtimer_restart(cpuctx);
1700 * Schedule in siblings as one group (if any):
1702 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1703 if (event_sched_in(event, cpuctx, ctx)) {
1704 partial_group = event;
1709 if (!pmu->commit_txn(pmu))
1714 * Groups can be scheduled in as one unit only, so undo any
1715 * partial group before returning:
1716 * The events up to the failed event are scheduled out normally,
1717 * tstamp_stopped will be updated.
1719 * The failed events and the remaining siblings need to have
1720 * their timings updated as if they had gone thru event_sched_in()
1721 * and event_sched_out(). This is required to get consistent timings
1722 * across the group. This also takes care of the case where the group
1723 * could never be scheduled by ensuring tstamp_stopped is set to mark
1724 * the time the event was actually stopped, such that time delta
1725 * calculation in update_event_times() is correct.
1727 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1728 if (event == partial_group)
1732 event->tstamp_running += now - event->tstamp_stopped;
1733 event->tstamp_stopped = now;
1735 event_sched_out(event, cpuctx, ctx);
1738 event_sched_out(group_event, cpuctx, ctx);
1740 pmu->cancel_txn(pmu);
1742 perf_cpu_hrtimer_restart(cpuctx);
1748 * Work out whether we can put this event group on the CPU now.
1750 static int group_can_go_on(struct perf_event *event,
1751 struct perf_cpu_context *cpuctx,
1755 * Groups consisting entirely of software events can always go on.
1757 if (event->group_flags & PERF_GROUP_SOFTWARE)
1760 * If an exclusive group is already on, no other hardware
1763 if (cpuctx->exclusive)
1766 * If this group is exclusive and there are already
1767 * events on the CPU, it can't go on.
1769 if (event->attr.exclusive && cpuctx->active_oncpu)
1772 * Otherwise, try to add it if all previous groups were able
1778 static void add_event_to_ctx(struct perf_event *event,
1779 struct perf_event_context *ctx)
1781 u64 tstamp = perf_event_time(event);
1783 list_add_event(event, ctx);
1784 perf_group_attach(event);
1785 event->tstamp_enabled = tstamp;
1786 event->tstamp_running = tstamp;
1787 event->tstamp_stopped = tstamp;
1790 static void task_ctx_sched_out(struct perf_event_context *ctx);
1792 ctx_sched_in(struct perf_event_context *ctx,
1793 struct perf_cpu_context *cpuctx,
1794 enum event_type_t event_type,
1795 struct task_struct *task);
1797 static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1798 struct perf_event_context *ctx,
1799 struct task_struct *task)
1801 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1803 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1804 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1806 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1810 * Cross CPU call to install and enable a performance event
1812 * Must be called with ctx->mutex held
1814 static int __perf_install_in_context(void *info)
1816 struct perf_event *event = info;
1817 struct perf_event_context *ctx = event->ctx;
1818 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1819 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1820 struct task_struct *task = current;
1822 perf_ctx_lock(cpuctx, task_ctx);
1823 perf_pmu_disable(cpuctx->ctx.pmu);
1826 * If there was an active task_ctx schedule it out.
1829 task_ctx_sched_out(task_ctx);
1832 * If the context we're installing events in is not the
1833 * active task_ctx, flip them.
1835 if (ctx->task && task_ctx != ctx) {
1837 raw_spin_unlock(&task_ctx->lock);
1838 raw_spin_lock(&ctx->lock);
1843 cpuctx->task_ctx = task_ctx;
1844 task = task_ctx->task;
1847 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
1849 update_context_time(ctx);
1851 * update cgrp time only if current cgrp
1852 * matches event->cgrp. Must be done before
1853 * calling add_event_to_ctx()
1855 update_cgrp_time_from_event(event);
1857 add_event_to_ctx(event, ctx);
1860 * Schedule everything back in
1862 perf_event_sched_in(cpuctx, task_ctx, task);
1864 perf_pmu_enable(cpuctx->ctx.pmu);
1865 perf_ctx_unlock(cpuctx, task_ctx);
1871 * Attach a performance event to a context
1873 * First we add the event to the list with the hardware enable bit
1874 * in event->hw_config cleared.
1876 * If the event is attached to a task which is on a CPU we use a smp
1877 * call to enable it in the task context. The task might have been
1878 * scheduled away, but we check this in the smp call again.
1881 perf_install_in_context(struct perf_event_context *ctx,
1882 struct perf_event *event,
1885 struct task_struct *task = ctx->task;
1887 lockdep_assert_held(&ctx->mutex);
1890 if (event->cpu != -1)
1895 * Per cpu events are installed via an smp call and
1896 * the install is always successful.
1898 cpu_function_call(cpu, __perf_install_in_context, event);
1903 if (!task_function_call(task, __perf_install_in_context, event))
1906 raw_spin_lock_irq(&ctx->lock);
1908 * If we failed to find a running task, but find the context active now
1909 * that we've acquired the ctx->lock, retry.
1911 if (ctx->is_active) {
1912 raw_spin_unlock_irq(&ctx->lock);
1917 * Since the task isn't running, its safe to add the event, us holding
1918 * the ctx->lock ensures the task won't get scheduled in.
1920 add_event_to_ctx(event, ctx);
1921 raw_spin_unlock_irq(&ctx->lock);
1925 * Put a event into inactive state and update time fields.
1926 * Enabling the leader of a group effectively enables all
1927 * the group members that aren't explicitly disabled, so we
1928 * have to update their ->tstamp_enabled also.
1929 * Note: this works for group members as well as group leaders
1930 * since the non-leader members' sibling_lists will be empty.
1932 static void __perf_event_mark_enabled(struct perf_event *event)
1934 struct perf_event *sub;
1935 u64 tstamp = perf_event_time(event);
1937 event->state = PERF_EVENT_STATE_INACTIVE;
1938 event->tstamp_enabled = tstamp - event->total_time_enabled;
1939 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1940 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1941 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1946 * Cross CPU call to enable a performance event
1948 static int __perf_event_enable(void *info)
1950 struct perf_event *event = info;
1951 struct perf_event_context *ctx = event->ctx;
1952 struct perf_event *leader = event->group_leader;
1953 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1956 if (WARN_ON_ONCE(!ctx->is_active))
1959 raw_spin_lock(&ctx->lock);
1960 update_context_time(ctx);
1962 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1966 * set current task's cgroup time reference point
1968 perf_cgroup_set_timestamp(current, ctx);
1970 __perf_event_mark_enabled(event);
1972 if (!event_filter_match(event)) {
1973 if (is_cgroup_event(event))
1974 perf_cgroup_defer_enabled(event);
1979 * If the event is in a group and isn't the group leader,
1980 * then don't put it on unless the group is on.
1982 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1985 if (!group_can_go_on(event, cpuctx, 1)) {
1988 if (event == leader)
1989 err = group_sched_in(event, cpuctx, ctx);
1991 err = event_sched_in(event, cpuctx, ctx);
1996 * If this event can't go on and it's part of a
1997 * group, then the whole group has to come off.
1999 if (leader != event) {
2000 group_sched_out(leader, cpuctx, ctx);
2001 perf_cpu_hrtimer_restart(cpuctx);
2003 if (leader->attr.pinned) {
2004 update_group_times(leader);
2005 leader->state = PERF_EVENT_STATE_ERROR;
2010 raw_spin_unlock(&ctx->lock);
2018 * If event->ctx is a cloned context, callers must make sure that
2019 * every task struct that event->ctx->task could possibly point to
2020 * remains valid. This condition is satisfied when called through
2021 * perf_event_for_each_child or perf_event_for_each as described
2022 * for perf_event_disable.
2024 void perf_event_enable(struct perf_event *event)
2026 struct perf_event_context *ctx = event->ctx;
2027 struct task_struct *task = ctx->task;
2031 * Enable the event on the cpu that it's on
2033 cpu_function_call(event->cpu, __perf_event_enable, event);
2037 raw_spin_lock_irq(&ctx->lock);
2038 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2042 * If the event is in error state, clear that first.
2043 * That way, if we see the event in error state below, we
2044 * know that it has gone back into error state, as distinct
2045 * from the task having been scheduled away before the
2046 * cross-call arrived.
2048 if (event->state == PERF_EVENT_STATE_ERROR)
2049 event->state = PERF_EVENT_STATE_OFF;
2052 if (!ctx->is_active) {
2053 __perf_event_mark_enabled(event);
2057 raw_spin_unlock_irq(&ctx->lock);
2059 if (!task_function_call(task, __perf_event_enable, event))
2062 raw_spin_lock_irq(&ctx->lock);
2065 * If the context is active and the event is still off,
2066 * we need to retry the cross-call.
2068 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
2070 * task could have been flipped by a concurrent
2071 * perf_event_context_sched_out()
2078 raw_spin_unlock_irq(&ctx->lock);
2080 EXPORT_SYMBOL_GPL(perf_event_enable);
2082 int perf_event_refresh(struct perf_event *event, int refresh)
2085 * not supported on inherited events
2087 if (event->attr.inherit || !is_sampling_event(event))
2090 atomic_add(refresh, &event->event_limit);
2091 perf_event_enable(event);
2095 EXPORT_SYMBOL_GPL(perf_event_refresh);
2097 static void ctx_sched_out(struct perf_event_context *ctx,
2098 struct perf_cpu_context *cpuctx,
2099 enum event_type_t event_type)
2101 struct perf_event *event;
2102 int is_active = ctx->is_active;
2104 ctx->is_active &= ~event_type;
2105 if (likely(!ctx->nr_events))
2108 update_context_time(ctx);
2109 update_cgrp_time_from_cpuctx(cpuctx);
2110 if (!ctx->nr_active)
2113 perf_pmu_disable(ctx->pmu);
2114 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
2115 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2116 group_sched_out(event, cpuctx, ctx);
2119 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
2120 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
2121 group_sched_out(event, cpuctx, ctx);
2123 perf_pmu_enable(ctx->pmu);
2127 * Test whether two contexts are equivalent, i.e. whether they
2128 * have both been cloned from the same version of the same context
2129 * and they both have the same number of enabled events.
2130 * If the number of enabled events is the same, then the set
2131 * of enabled events should be the same, because these are both
2132 * inherited contexts, therefore we can't access individual events
2133 * in them directly with an fd; we can only enable/disable all
2134 * events via prctl, or enable/disable all events in a family
2135 * via ioctl, which will have the same effect on both contexts.
2137 static int context_equiv(struct perf_event_context *ctx1,
2138 struct perf_event_context *ctx2)
2140 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
2141 && ctx1->parent_gen == ctx2->parent_gen
2142 && !ctx1->pin_count && !ctx2->pin_count;
2145 static void __perf_event_sync_stat(struct perf_event *event,
2146 struct perf_event *next_event)
2150 if (!event->attr.inherit_stat)
2154 * Update the event value, we cannot use perf_event_read()
2155 * because we're in the middle of a context switch and have IRQs
2156 * disabled, which upsets smp_call_function_single(), however
2157 * we know the event must be on the current CPU, therefore we
2158 * don't need to use it.
2160 switch (event->state) {
2161 case PERF_EVENT_STATE_ACTIVE:
2162 event->pmu->read(event);
2165 case PERF_EVENT_STATE_INACTIVE:
2166 update_event_times(event);
2174 * In order to keep per-task stats reliable we need to flip the event
2175 * values when we flip the contexts.
2177 value = local64_read(&next_event->count);
2178 value = local64_xchg(&event->count, value);
2179 local64_set(&next_event->count, value);
2181 swap(event->total_time_enabled, next_event->total_time_enabled);
2182 swap(event->total_time_running, next_event->total_time_running);
2185 * Since we swizzled the values, update the user visible data too.
2187 perf_event_update_userpage(event);
2188 perf_event_update_userpage(next_event);
2191 #define list_next_entry(pos, member) \
2192 list_entry(pos->member.next, typeof(*pos), member)
2194 static void perf_event_sync_stat(struct perf_event_context *ctx,
2195 struct perf_event_context *next_ctx)
2197 struct perf_event *event, *next_event;
2202 update_context_time(ctx);
2204 event = list_first_entry(&ctx->event_list,
2205 struct perf_event, event_entry);
2207 next_event = list_first_entry(&next_ctx->event_list,
2208 struct perf_event, event_entry);
2210 while (&event->event_entry != &ctx->event_list &&
2211 &next_event->event_entry != &next_ctx->event_list) {
2213 __perf_event_sync_stat(event, next_event);
2215 event = list_next_entry(event, event_entry);
2216 next_event = list_next_entry(next_event, event_entry);
2220 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2221 struct task_struct *next)
2223 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
2224 struct perf_event_context *next_ctx;
2225 struct perf_event_context *parent;
2226 struct perf_cpu_context *cpuctx;
2232 cpuctx = __get_cpu_context(ctx);
2233 if (!cpuctx->task_ctx)
2237 parent = rcu_dereference(ctx->parent_ctx);
2238 next_ctx = next->perf_event_ctxp[ctxn];
2239 if (parent && next_ctx &&
2240 rcu_dereference(next_ctx->parent_ctx) == parent) {
2242 * Looks like the two contexts are clones, so we might be
2243 * able to optimize the context switch. We lock both
2244 * contexts and check that they are clones under the
2245 * lock (including re-checking that neither has been
2246 * uncloned in the meantime). It doesn't matter which
2247 * order we take the locks because no other cpu could
2248 * be trying to lock both of these tasks.
2250 raw_spin_lock(&ctx->lock);
2251 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
2252 if (context_equiv(ctx, next_ctx)) {
2254 * XXX do we need a memory barrier of sorts
2255 * wrt to rcu_dereference() of perf_event_ctxp
2257 task->perf_event_ctxp[ctxn] = next_ctx;
2258 next->perf_event_ctxp[ctxn] = ctx;
2260 next_ctx->task = task;
2263 perf_event_sync_stat(ctx, next_ctx);
2265 raw_spin_unlock(&next_ctx->lock);
2266 raw_spin_unlock(&ctx->lock);
2271 raw_spin_lock(&ctx->lock);
2272 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2273 cpuctx->task_ctx = NULL;
2274 raw_spin_unlock(&ctx->lock);
2278 #define for_each_task_context_nr(ctxn) \
2279 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2282 * Called from scheduler to remove the events of the current task,
2283 * with interrupts disabled.
2285 * We stop each event and update the event value in event->count.
2287 * This does not protect us against NMI, but disable()
2288 * sets the disabled bit in the control field of event _before_
2289 * accessing the event control register. If a NMI hits, then it will
2290 * not restart the event.
2292 void __perf_event_task_sched_out(struct task_struct *task,
2293 struct task_struct *next)
2297 for_each_task_context_nr(ctxn)
2298 perf_event_context_sched_out(task, ctxn, next);
2301 * if cgroup events exist on this CPU, then we need
2302 * to check if we have to switch out PMU state.
2303 * cgroup event are system-wide mode only
2305 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2306 perf_cgroup_sched_out(task, next);
2309 static void task_ctx_sched_out(struct perf_event_context *ctx)
2311 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2313 if (!cpuctx->task_ctx)
2316 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2319 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2320 cpuctx->task_ctx = NULL;
2324 * Called with IRQs disabled
2326 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2327 enum event_type_t event_type)
2329 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
2333 ctx_pinned_sched_in(struct perf_event_context *ctx,
2334 struct perf_cpu_context *cpuctx)
2336 struct perf_event *event;
2338 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2339 if (event->state <= PERF_EVENT_STATE_OFF)
2341 if (!event_filter_match(event))
2344 /* may need to reset tstamp_enabled */
2345 if (is_cgroup_event(event))
2346 perf_cgroup_mark_enabled(event, ctx);
2348 if (group_can_go_on(event, cpuctx, 1))
2349 group_sched_in(event, cpuctx, ctx);
2352 * If this pinned group hasn't been scheduled,
2353 * put it in error state.
2355 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2356 update_group_times(event);
2357 event->state = PERF_EVENT_STATE_ERROR;
2363 ctx_flexible_sched_in(struct perf_event_context *ctx,
2364 struct perf_cpu_context *cpuctx)
2366 struct perf_event *event;
2369 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2370 /* Ignore events in OFF or ERROR state */
2371 if (event->state <= PERF_EVENT_STATE_OFF)
2374 * Listen to the 'cpu' scheduling filter constraint
2377 if (!event_filter_match(event))
2380 /* may need to reset tstamp_enabled */
2381 if (is_cgroup_event(event))
2382 perf_cgroup_mark_enabled(event, ctx);
2384 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2385 if (group_sched_in(event, cpuctx, ctx))
2392 ctx_sched_in(struct perf_event_context *ctx,
2393 struct perf_cpu_context *cpuctx,
2394 enum event_type_t event_type,
2395 struct task_struct *task)
2398 int is_active = ctx->is_active;
2400 ctx->is_active |= event_type;
2401 if (likely(!ctx->nr_events))
2405 ctx->timestamp = now;
2406 perf_cgroup_set_timestamp(task, ctx);
2408 * First go through the list and put on any pinned groups
2409 * in order to give them the best chance of going on.
2411 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
2412 ctx_pinned_sched_in(ctx, cpuctx);
2414 /* Then walk through the lower prio flexible groups */
2415 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
2416 ctx_flexible_sched_in(ctx, cpuctx);
2419 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2420 enum event_type_t event_type,
2421 struct task_struct *task)
2423 struct perf_event_context *ctx = &cpuctx->ctx;
2425 ctx_sched_in(ctx, cpuctx, event_type, task);
2428 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2429 struct task_struct *task)
2431 struct perf_cpu_context *cpuctx;
2433 cpuctx = __get_cpu_context(ctx);
2434 if (cpuctx->task_ctx == ctx)
2437 perf_ctx_lock(cpuctx, ctx);
2438 perf_pmu_disable(ctx->pmu);
2440 * We want to keep the following priority order:
2441 * cpu pinned (that don't need to move), task pinned,
2442 * cpu flexible, task flexible.
2444 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2447 cpuctx->task_ctx = ctx;
2449 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2451 perf_pmu_enable(ctx->pmu);
2452 perf_ctx_unlock(cpuctx, ctx);
2455 * Since these rotations are per-cpu, we need to ensure the
2456 * cpu-context we got scheduled on is actually rotating.
2458 perf_pmu_rotate_start(ctx->pmu);
2462 * When sampling the branck stack in system-wide, it may be necessary
2463 * to flush the stack on context switch. This happens when the branch
2464 * stack does not tag its entries with the pid of the current task.
2465 * Otherwise it becomes impossible to associate a branch entry with a
2466 * task. This ambiguity is more likely to appear when the branch stack
2467 * supports priv level filtering and the user sets it to monitor only
2468 * at the user level (which could be a useful measurement in system-wide
2469 * mode). In that case, the risk is high of having a branch stack with
2470 * branch from multiple tasks. Flushing may mean dropping the existing
2471 * entries or stashing them somewhere in the PMU specific code layer.
2473 * This function provides the context switch callback to the lower code
2474 * layer. It is invoked ONLY when there is at least one system-wide context
2475 * with at least one active event using taken branch sampling.
2477 static void perf_branch_stack_sched_in(struct task_struct *prev,
2478 struct task_struct *task)
2480 struct perf_cpu_context *cpuctx;
2482 unsigned long flags;
2484 /* no need to flush branch stack if not changing task */
2488 local_irq_save(flags);
2492 list_for_each_entry_rcu(pmu, &pmus, entry) {
2493 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2496 * check if the context has at least one
2497 * event using PERF_SAMPLE_BRANCH_STACK
2499 if (cpuctx->ctx.nr_branch_stack > 0
2500 && pmu->flush_branch_stack) {
2502 pmu = cpuctx->ctx.pmu;
2504 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2506 perf_pmu_disable(pmu);
2508 pmu->flush_branch_stack();
2510 perf_pmu_enable(pmu);
2512 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2518 local_irq_restore(flags);
2522 * Called from scheduler to add the events of the current task
2523 * with interrupts disabled.
2525 * We restore the event value and then enable it.
2527 * This does not protect us against NMI, but enable()
2528 * sets the enabled bit in the control field of event _before_
2529 * accessing the event control register. If a NMI hits, then it will
2530 * keep the event running.
2532 void __perf_event_task_sched_in(struct task_struct *prev,
2533 struct task_struct *task)
2535 struct perf_event_context *ctx;
2538 for_each_task_context_nr(ctxn) {
2539 ctx = task->perf_event_ctxp[ctxn];
2543 perf_event_context_sched_in(ctx, task);
2546 * if cgroup events exist on this CPU, then we need
2547 * to check if we have to switch in PMU state.
2548 * cgroup event are system-wide mode only
2550 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2551 perf_cgroup_sched_in(prev, task);
2553 /* check for system-wide branch_stack events */
2554 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2555 perf_branch_stack_sched_in(prev, task);
2558 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2560 u64 frequency = event->attr.sample_freq;
2561 u64 sec = NSEC_PER_SEC;
2562 u64 divisor, dividend;
2564 int count_fls, nsec_fls, frequency_fls, sec_fls;
2566 count_fls = fls64(count);
2567 nsec_fls = fls64(nsec);
2568 frequency_fls = fls64(frequency);
2572 * We got @count in @nsec, with a target of sample_freq HZ
2573 * the target period becomes:
2576 * period = -------------------
2577 * @nsec * sample_freq
2582 * Reduce accuracy by one bit such that @a and @b converge
2583 * to a similar magnitude.
2585 #define REDUCE_FLS(a, b) \
2587 if (a##_fls > b##_fls) { \
2597 * Reduce accuracy until either term fits in a u64, then proceed with
2598 * the other, so that finally we can do a u64/u64 division.
2600 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2601 REDUCE_FLS(nsec, frequency);
2602 REDUCE_FLS(sec, count);
2605 if (count_fls + sec_fls > 64) {
2606 divisor = nsec * frequency;
2608 while (count_fls + sec_fls > 64) {
2609 REDUCE_FLS(count, sec);
2613 dividend = count * sec;
2615 dividend = count * sec;
2617 while (nsec_fls + frequency_fls > 64) {
2618 REDUCE_FLS(nsec, frequency);
2622 divisor = nsec * frequency;
2628 return div64_u64(dividend, divisor);
2631 static DEFINE_PER_CPU(int, perf_throttled_count);
2632 static DEFINE_PER_CPU(u64, perf_throttled_seq);
2634 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
2636 struct hw_perf_event *hwc = &event->hw;
2637 s64 period, sample_period;
2640 period = perf_calculate_period(event, nsec, count);
2642 delta = (s64)(period - hwc->sample_period);
2643 delta = (delta + 7) / 8; /* low pass filter */
2645 sample_period = hwc->sample_period + delta;
2650 hwc->sample_period = sample_period;
2652 if (local64_read(&hwc->period_left) > 8*sample_period) {
2654 event->pmu->stop(event, PERF_EF_UPDATE);
2656 local64_set(&hwc->period_left, 0);
2659 event->pmu->start(event, PERF_EF_RELOAD);
2664 * combine freq adjustment with unthrottling to avoid two passes over the
2665 * events. At the same time, make sure, having freq events does not change
2666 * the rate of unthrottling as that would introduce bias.
2668 static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2671 struct perf_event *event;
2672 struct hw_perf_event *hwc;
2673 u64 now, period = TICK_NSEC;
2677 * only need to iterate over all events iff:
2678 * - context have events in frequency mode (needs freq adjust)
2679 * - there are events to unthrottle on this cpu
2681 if (!(ctx->nr_freq || needs_unthr))
2684 raw_spin_lock(&ctx->lock);
2685 perf_pmu_disable(ctx->pmu);
2687 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2688 if (event->state != PERF_EVENT_STATE_ACTIVE)
2691 if (!event_filter_match(event))
2696 if (needs_unthr && hwc->interrupts == MAX_INTERRUPTS) {
2697 hwc->interrupts = 0;
2698 perf_log_throttle(event, 1);
2699 event->pmu->start(event, 0);
2702 if (!event->attr.freq || !event->attr.sample_freq)
2706 * stop the event and update event->count
2708 event->pmu->stop(event, PERF_EF_UPDATE);
2710 now = local64_read(&event->count);
2711 delta = now - hwc->freq_count_stamp;
2712 hwc->freq_count_stamp = now;
2716 * reload only if value has changed
2717 * we have stopped the event so tell that
2718 * to perf_adjust_period() to avoid stopping it
2722 perf_adjust_period(event, period, delta, false);
2724 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
2727 perf_pmu_enable(ctx->pmu);
2728 raw_spin_unlock(&ctx->lock);
2732 * Round-robin a context's events:
2734 static void rotate_ctx(struct perf_event_context *ctx)
2737 * Rotate the first entry last of non-pinned groups. Rotation might be
2738 * disabled by the inheritance code.
2740 if (!ctx->rotate_disable)
2741 list_rotate_left(&ctx->flexible_groups);
2745 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2746 * because they're strictly cpu affine and rotate_start is called with IRQs
2747 * disabled, while rotate_context is called from IRQ context.
2749 static int perf_rotate_context(struct perf_cpu_context *cpuctx)
2751 struct perf_event_context *ctx = NULL;
2752 int rotate = 0, remove = 1;
2754 if (cpuctx->ctx.nr_events) {
2756 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2760 ctx = cpuctx->task_ctx;
2761 if (ctx && ctx->nr_events) {
2763 if (ctx->nr_events != ctx->nr_active)
2770 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2771 perf_pmu_disable(cpuctx->ctx.pmu);
2773 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2775 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
2777 rotate_ctx(&cpuctx->ctx);
2781 perf_event_sched_in(cpuctx, ctx, current);
2783 perf_pmu_enable(cpuctx->ctx.pmu);
2784 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2787 list_del_init(&cpuctx->rotation_list);
2792 #ifdef CONFIG_NO_HZ_FULL
2793 bool perf_event_can_stop_tick(void)
2795 if (list_empty(&__get_cpu_var(rotation_list)))
2802 void perf_event_task_tick(void)
2804 struct list_head *head = &__get_cpu_var(rotation_list);
2805 struct perf_cpu_context *cpuctx, *tmp;
2806 struct perf_event_context *ctx;
2809 WARN_ON(!irqs_disabled());
2811 __this_cpu_inc(perf_throttled_seq);
2812 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2814 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2816 perf_adjust_freq_unthr_context(ctx, throttled);
2818 ctx = cpuctx->task_ctx;
2820 perf_adjust_freq_unthr_context(ctx, throttled);
2824 static int event_enable_on_exec(struct perf_event *event,
2825 struct perf_event_context *ctx)
2827 if (!event->attr.enable_on_exec)
2830 event->attr.enable_on_exec = 0;
2831 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2834 __perf_event_mark_enabled(event);
2840 * Enable all of a task's events that have been marked enable-on-exec.
2841 * This expects task == current.
2843 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2845 struct perf_event *event;
2846 unsigned long flags;
2850 local_irq_save(flags);
2851 if (!ctx || !ctx->nr_events)
2855 * We must ctxsw out cgroup events to avoid conflict
2856 * when invoking perf_task_event_sched_in() later on
2857 * in this function. Otherwise we end up trying to
2858 * ctxswin cgroup events which are already scheduled
2861 perf_cgroup_sched_out(current, NULL);
2863 raw_spin_lock(&ctx->lock);
2864 task_ctx_sched_out(ctx);
2866 list_for_each_entry(event, &ctx->event_list, event_entry) {
2867 ret = event_enable_on_exec(event, ctx);
2873 * Unclone this context if we enabled any event.
2878 raw_spin_unlock(&ctx->lock);
2881 * Also calls ctxswin for cgroup events, if any:
2883 perf_event_context_sched_in(ctx, ctx->task);
2885 local_irq_restore(flags);
2889 * Cross CPU call to read the hardware event
2891 static void __perf_event_read(void *info)
2893 struct perf_event *event = info;
2894 struct perf_event_context *ctx = event->ctx;
2895 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2898 * If this is a task context, we need to check whether it is
2899 * the current task context of this cpu. If not it has been
2900 * scheduled out before the smp call arrived. In that case
2901 * event->count would have been updated to a recent sample
2902 * when the event was scheduled out.
2904 if (ctx->task && cpuctx->task_ctx != ctx)
2907 raw_spin_lock(&ctx->lock);
2908 if (ctx->is_active) {
2909 update_context_time(ctx);
2910 update_cgrp_time_from_event(event);
2912 update_event_times(event);
2913 if (event->state == PERF_EVENT_STATE_ACTIVE)
2914 event->pmu->read(event);
2915 raw_spin_unlock(&ctx->lock);
2918 static inline u64 perf_event_count(struct perf_event *event)
2920 return local64_read(&event->count) + atomic64_read(&event->child_count);
2923 static u64 perf_event_read(struct perf_event *event)
2926 * If event is enabled and currently active on a CPU, update the
2927 * value in the event structure:
2929 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2930 smp_call_function_single(event->oncpu,
2931 __perf_event_read, event, 1);
2932 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2933 struct perf_event_context *ctx = event->ctx;
2934 unsigned long flags;
2936 raw_spin_lock_irqsave(&ctx->lock, flags);
2938 * may read while context is not active
2939 * (e.g., thread is blocked), in that case
2940 * we cannot update context time
2942 if (ctx->is_active) {
2943 update_context_time(ctx);
2944 update_cgrp_time_from_event(event);
2946 update_event_times(event);
2947 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2950 return perf_event_count(event);
2954 * Initialize the perf_event context in a task_struct:
2956 static void __perf_event_init_context(struct perf_event_context *ctx)
2958 raw_spin_lock_init(&ctx->lock);
2959 mutex_init(&ctx->mutex);
2960 INIT_LIST_HEAD(&ctx->pinned_groups);
2961 INIT_LIST_HEAD(&ctx->flexible_groups);
2962 INIT_LIST_HEAD(&ctx->event_list);
2963 atomic_set(&ctx->refcount, 1);
2966 static struct perf_event_context *
2967 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2969 struct perf_event_context *ctx;
2971 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2975 __perf_event_init_context(ctx);
2978 get_task_struct(task);
2985 static struct task_struct *
2986 find_lively_task_by_vpid(pid_t vpid)
2988 struct task_struct *task;
2995 task = find_task_by_vpid(vpid);
2997 get_task_struct(task);
3001 return ERR_PTR(-ESRCH);
3003 /* Reuse ptrace permission checks for now. */
3005 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3010 put_task_struct(task);
3011 return ERR_PTR(err);
3016 * Returns a matching context with refcount and pincount.
3018 static struct perf_event_context *
3019 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
3021 struct perf_event_context *ctx;
3022 struct perf_cpu_context *cpuctx;
3023 unsigned long flags;
3027 /* Must be root to operate on a CPU event: */
3028 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3029 return ERR_PTR(-EACCES);
3032 * We could be clever and allow to attach a event to an
3033 * offline CPU and activate it when the CPU comes up, but
3036 if (!cpu_online(cpu))
3037 return ERR_PTR(-ENODEV);
3039 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
3048 ctxn = pmu->task_ctx_nr;
3053 ctx = perf_lock_task_context(task, ctxn, &flags);
3057 raw_spin_unlock_irqrestore(&ctx->lock, flags);
3059 ctx = alloc_perf_context(pmu, task);
3065 mutex_lock(&task->perf_event_mutex);
3067 * If it has already passed perf_event_exit_task().
3068 * we must see PF_EXITING, it takes this mutex too.
3070 if (task->flags & PF_EXITING)
3072 else if (task->perf_event_ctxp[ctxn])
3077 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
3079 mutex_unlock(&task->perf_event_mutex);
3081 if (unlikely(err)) {
3093 return ERR_PTR(err);
3096 static void perf_event_free_filter(struct perf_event *event);
3098 static void free_event_rcu(struct rcu_head *head)
3100 struct perf_event *event;
3102 event = container_of(head, struct perf_event, rcu_head);
3104 put_pid_ns(event->ns);
3105 perf_event_free_filter(event);
3109 static void ring_buffer_put(struct ring_buffer *rb);
3110 static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb);
3112 static void free_event(struct perf_event *event)
3114 irq_work_sync(&event->pending);
3116 if (!event->parent) {
3117 if (event->attach_state & PERF_ATTACH_TASK)
3118 static_key_slow_dec_deferred(&perf_sched_events);
3119 if (event->attr.mmap || event->attr.mmap_data)
3120 atomic_dec(&nr_mmap_events);
3121 if (event->attr.comm)
3122 atomic_dec(&nr_comm_events);
3123 if (event->attr.task)
3124 atomic_dec(&nr_task_events);
3125 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3126 put_callchain_buffers();
3127 if (is_cgroup_event(event)) {
3128 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
3129 static_key_slow_dec_deferred(&perf_sched_events);
3132 if (has_branch_stack(event)) {
3133 static_key_slow_dec_deferred(&perf_sched_events);
3134 /* is system-wide event */
3135 if (!(event->attach_state & PERF_ATTACH_TASK)) {
3136 atomic_dec(&per_cpu(perf_branch_stack_events,
3143 struct ring_buffer *rb;
3146 * Can happen when we close an event with re-directed output.
3148 * Since we have a 0 refcount, perf_mmap_close() will skip
3149 * over us; possibly making our ring_buffer_put() the last.
3151 mutex_lock(&event->mmap_mutex);
3154 rcu_assign_pointer(event->rb, NULL);
3155 ring_buffer_detach(event, rb);
3156 ring_buffer_put(rb); /* could be last */
3158 mutex_unlock(&event->mmap_mutex);
3161 if (is_cgroup_event(event))
3162 perf_detach_cgroup(event);
3165 event->destroy(event);
3168 put_ctx(event->ctx);
3170 call_rcu(&event->rcu_head, free_event_rcu);
3173 int perf_event_release_kernel(struct perf_event *event)
3175 struct perf_event_context *ctx = event->ctx;
3177 WARN_ON_ONCE(ctx->parent_ctx);
3179 * There are two ways this annotation is useful:
3181 * 1) there is a lock recursion from perf_event_exit_task
3182 * see the comment there.
3184 * 2) there is a lock-inversion with mmap_sem through
3185 * perf_event_read_group(), which takes faults while
3186 * holding ctx->mutex, however this is called after
3187 * the last filedesc died, so there is no possibility
3188 * to trigger the AB-BA case.
3190 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
3191 raw_spin_lock_irq(&ctx->lock);
3192 perf_group_detach(event);
3193 raw_spin_unlock_irq(&ctx->lock);
3194 perf_remove_from_context(event);
3195 mutex_unlock(&ctx->mutex);
3201 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3204 * Called when the last reference to the file is gone.
3206 static void put_event(struct perf_event *event)
3208 struct task_struct *owner;
3210 if (!atomic_long_dec_and_test(&event->refcount))
3214 owner = ACCESS_ONCE(event->owner);
3216 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3217 * !owner it means the list deletion is complete and we can indeed
3218 * free this event, otherwise we need to serialize on
3219 * owner->perf_event_mutex.
3221 smp_read_barrier_depends();
3224 * Since delayed_put_task_struct() also drops the last
3225 * task reference we can safely take a new reference
3226 * while holding the rcu_read_lock().
3228 get_task_struct(owner);
3233 mutex_lock(&owner->perf_event_mutex);
3235 * We have to re-check the event->owner field, if it is cleared
3236 * we raced with perf_event_exit_task(), acquiring the mutex
3237 * ensured they're done, and we can proceed with freeing the
3241 list_del_init(&event->owner_entry);
3242 mutex_unlock(&owner->perf_event_mutex);
3243 put_task_struct(owner);
3246 perf_event_release_kernel(event);
3249 static int perf_release(struct inode *inode, struct file *file)
3251 put_event(file->private_data);
3255 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
3257 struct perf_event *child;
3263 mutex_lock(&event->child_mutex);
3264 total += perf_event_read(event);
3265 *enabled += event->total_time_enabled +
3266 atomic64_read(&event->child_total_time_enabled);
3267 *running += event->total_time_running +
3268 atomic64_read(&event->child_total_time_running);
3270 list_for_each_entry(child, &event->child_list, child_list) {
3271 total += perf_event_read(child);
3272 *enabled += child->total_time_enabled;
3273 *running += child->total_time_running;
3275 mutex_unlock(&event->child_mutex);
3279 EXPORT_SYMBOL_GPL(perf_event_read_value);
3281 static int perf_event_read_group(struct perf_event *event,
3282 u64 read_format, char __user *buf)
3284 struct perf_event *leader = event->group_leader, *sub;
3285 int n = 0, size = 0, ret = -EFAULT;
3286 struct perf_event_context *ctx = leader->ctx;
3288 u64 count, enabled, running;
3290 mutex_lock(&ctx->mutex);
3291 count = perf_event_read_value(leader, &enabled, &running);
3293 values[n++] = 1 + leader->nr_siblings;
3294 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3295 values[n++] = enabled;
3296 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3297 values[n++] = running;
3298 values[n++] = count;
3299 if (read_format & PERF_FORMAT_ID)
3300 values[n++] = primary_event_id(leader);
3302 size = n * sizeof(u64);
3304 if (copy_to_user(buf, values, size))
3309 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3312 values[n++] = perf_event_read_value(sub, &enabled, &running);
3313 if (read_format & PERF_FORMAT_ID)
3314 values[n++] = primary_event_id(sub);
3316 size = n * sizeof(u64);
3318 if (copy_to_user(buf + ret, values, size)) {
3326 mutex_unlock(&ctx->mutex);
3331 static int perf_event_read_one(struct perf_event *event,
3332 u64 read_format, char __user *buf)
3334 u64 enabled, running;
3338 values[n++] = perf_event_read_value(event, &enabled, &running);
3339 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3340 values[n++] = enabled;
3341 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3342 values[n++] = running;
3343 if (read_format & PERF_FORMAT_ID)
3344 values[n++] = primary_event_id(event);
3346 if (copy_to_user(buf, values, n * sizeof(u64)))
3349 return n * sizeof(u64);
3353 * Read the performance event - simple non blocking version for now
3356 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3358 u64 read_format = event->attr.read_format;
3362 * Return end-of-file for a read on a event that is in
3363 * error state (i.e. because it was pinned but it couldn't be
3364 * scheduled on to the CPU at some point).
3366 if (event->state == PERF_EVENT_STATE_ERROR)
3369 if (count < event->read_size)
3372 WARN_ON_ONCE(event->ctx->parent_ctx);
3373 if (read_format & PERF_FORMAT_GROUP)
3374 ret = perf_event_read_group(event, read_format, buf);
3376 ret = perf_event_read_one(event, read_format, buf);
3382 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3384 struct perf_event *event = file->private_data;
3386 return perf_read_hw(event, buf, count);
3389 static unsigned int perf_poll(struct file *file, poll_table *wait)
3391 struct perf_event *event = file->private_data;
3392 struct ring_buffer *rb;
3393 unsigned int events = POLL_HUP;
3396 * Pin the event->rb by taking event->mmap_mutex; otherwise
3397 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
3399 mutex_lock(&event->mmap_mutex);
3402 events = atomic_xchg(&rb->poll, 0);
3403 mutex_unlock(&event->mmap_mutex);
3405 poll_wait(file, &event->waitq, wait);
3410 static void perf_event_reset(struct perf_event *event)
3412 (void)perf_event_read(event);
3413 local64_set(&event->count, 0);
3414 perf_event_update_userpage(event);
3418 * Holding the top-level event's child_mutex means that any
3419 * descendant process that has inherited this event will block
3420 * in sync_child_event if it goes to exit, thus satisfying the
3421 * task existence requirements of perf_event_enable/disable.
3423 static void perf_event_for_each_child(struct perf_event *event,
3424 void (*func)(struct perf_event *))
3426 struct perf_event *child;
3428 WARN_ON_ONCE(event->ctx->parent_ctx);
3429 mutex_lock(&event->child_mutex);
3431 list_for_each_entry(child, &event->child_list, child_list)
3433 mutex_unlock(&event->child_mutex);
3436 static void perf_event_for_each(struct perf_event *event,
3437 void (*func)(struct perf_event *))
3439 struct perf_event_context *ctx = event->ctx;
3440 struct perf_event *sibling;
3442 WARN_ON_ONCE(ctx->parent_ctx);
3443 mutex_lock(&ctx->mutex);
3444 event = event->group_leader;
3446 perf_event_for_each_child(event, func);
3447 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3448 perf_event_for_each_child(sibling, func);
3449 mutex_unlock(&ctx->mutex);
3452 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3454 struct perf_event_context *ctx = event->ctx;
3458 if (!is_sampling_event(event))
3461 if (copy_from_user(&value, arg, sizeof(value)))
3467 raw_spin_lock_irq(&ctx->lock);
3468 if (event->attr.freq) {
3469 if (value > sysctl_perf_event_sample_rate) {
3474 event->attr.sample_freq = value;
3476 event->attr.sample_period = value;
3477 event->hw.sample_period = value;
3480 raw_spin_unlock_irq(&ctx->lock);
3485 static const struct file_operations perf_fops;
3487 static inline int perf_fget_light(int fd, struct fd *p)
3489 struct fd f = fdget(fd);
3493 if (f.file->f_op != &perf_fops) {
3501 static int perf_event_set_output(struct perf_event *event,
3502 struct perf_event *output_event);
3503 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3505 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3507 struct perf_event *event = file->private_data;
3508 void (*func)(struct perf_event *);
3512 case PERF_EVENT_IOC_ENABLE:
3513 func = perf_event_enable;
3515 case PERF_EVENT_IOC_DISABLE:
3516 func = perf_event_disable;
3518 case PERF_EVENT_IOC_RESET:
3519 func = perf_event_reset;
3522 case PERF_EVENT_IOC_REFRESH:
3523 return perf_event_refresh(event, arg);
3525 case PERF_EVENT_IOC_PERIOD:
3526 return perf_event_period(event, (u64 __user *)arg);
3528 case PERF_EVENT_IOC_SET_OUTPUT:
3532 struct perf_event *output_event;
3534 ret = perf_fget_light(arg, &output);
3537 output_event = output.file->private_data;
3538 ret = perf_event_set_output(event, output_event);
3541 ret = perf_event_set_output(event, NULL);
3546 case PERF_EVENT_IOC_SET_FILTER:
3547 return perf_event_set_filter(event, (void __user *)arg);
3553 if (flags & PERF_IOC_FLAG_GROUP)
3554 perf_event_for_each(event, func);
3556 perf_event_for_each_child(event, func);
3561 int perf_event_task_enable(void)
3563 struct perf_event *event;
3565 mutex_lock(¤t->perf_event_mutex);
3566 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3567 perf_event_for_each_child(event, perf_event_enable);
3568 mutex_unlock(¤t->perf_event_mutex);
3573 int perf_event_task_disable(void)
3575 struct perf_event *event;
3577 mutex_lock(¤t->perf_event_mutex);
3578 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3579 perf_event_for_each_child(event, perf_event_disable);
3580 mutex_unlock(¤t->perf_event_mutex);
3585 static int perf_event_index(struct perf_event *event)
3587 if (event->hw.state & PERF_HES_STOPPED)
3590 if (event->state != PERF_EVENT_STATE_ACTIVE)
3593 return event->pmu->event_idx(event);
3596 static void calc_timer_values(struct perf_event *event,
3603 *now = perf_clock();
3604 ctx_time = event->shadow_ctx_time + *now;
3605 *enabled = ctx_time - event->tstamp_enabled;
3606 *running = ctx_time - event->tstamp_running;
3609 void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
3614 * Callers need to ensure there can be no nesting of this function, otherwise
3615 * the seqlock logic goes bad. We can not serialize this because the arch
3616 * code calls this from NMI context.
3618 void perf_event_update_userpage(struct perf_event *event)
3620 struct perf_event_mmap_page *userpg;
3621 struct ring_buffer *rb;
3622 u64 enabled, running, now;
3626 * compute total_time_enabled, total_time_running
3627 * based on snapshot values taken when the event
3628 * was last scheduled in.
3630 * we cannot simply called update_context_time()
3631 * because of locking issue as we can be called in
3634 calc_timer_values(event, &now, &enabled, &running);
3635 rb = rcu_dereference(event->rb);
3639 userpg = rb->user_page;
3642 * Disable preemption so as to not let the corresponding user-space
3643 * spin too long if we get preempted.
3648 userpg->index = perf_event_index(event);
3649 userpg->offset = perf_event_count(event);
3651 userpg->offset -= local64_read(&event->hw.prev_count);
3653 userpg->time_enabled = enabled +
3654 atomic64_read(&event->child_total_time_enabled);
3656 userpg->time_running = running +
3657 atomic64_read(&event->child_total_time_running);
3659 arch_perf_update_userpage(userpg, now);
3668 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3670 struct perf_event *event = vma->vm_file->private_data;
3671 struct ring_buffer *rb;
3672 int ret = VM_FAULT_SIGBUS;
3674 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3675 if (vmf->pgoff == 0)
3681 rb = rcu_dereference(event->rb);
3685 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3688 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
3692 get_page(vmf->page);
3693 vmf->page->mapping = vma->vm_file->f_mapping;
3694 vmf->page->index = vmf->pgoff;
3703 static void ring_buffer_attach(struct perf_event *event,
3704 struct ring_buffer *rb)
3706 unsigned long flags;
3708 if (!list_empty(&event->rb_entry))
3711 spin_lock_irqsave(&rb->event_lock, flags);
3712 if (list_empty(&event->rb_entry))
3713 list_add(&event->rb_entry, &rb->event_list);
3714 spin_unlock_irqrestore(&rb->event_lock, flags);
3717 static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb)
3719 unsigned long flags;
3721 if (list_empty(&event->rb_entry))
3724 spin_lock_irqsave(&rb->event_lock, flags);
3725 list_del_init(&event->rb_entry);
3726 wake_up_all(&event->waitq);
3727 spin_unlock_irqrestore(&rb->event_lock, flags);
3730 static void ring_buffer_wakeup(struct perf_event *event)
3732 struct ring_buffer *rb;
3735 rb = rcu_dereference(event->rb);
3737 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3738 wake_up_all(&event->waitq);
3743 static void rb_free_rcu(struct rcu_head *rcu_head)
3745 struct ring_buffer *rb;
3747 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3751 static struct ring_buffer *ring_buffer_get(struct perf_event *event)
3753 struct ring_buffer *rb;
3756 rb = rcu_dereference(event->rb);
3758 if (!atomic_inc_not_zero(&rb->refcount))
3766 static void ring_buffer_put(struct ring_buffer *rb)
3768 if (!atomic_dec_and_test(&rb->refcount))
3771 WARN_ON_ONCE(!list_empty(&rb->event_list));
3773 call_rcu(&rb->rcu_head, rb_free_rcu);
3776 static void perf_mmap_open(struct vm_area_struct *vma)
3778 struct perf_event *event = vma->vm_file->private_data;
3780 atomic_inc(&event->mmap_count);
3781 atomic_inc(&event->rb->mmap_count);
3785 * A buffer can be mmap()ed multiple times; either directly through the same
3786 * event, or through other events by use of perf_event_set_output().
3788 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3789 * the buffer here, where we still have a VM context. This means we need
3790 * to detach all events redirecting to us.
3792 static void perf_mmap_close(struct vm_area_struct *vma)
3794 struct perf_event *event = vma->vm_file->private_data;
3796 struct ring_buffer *rb = event->rb;
3797 struct user_struct *mmap_user = rb->mmap_user;
3798 int mmap_locked = rb->mmap_locked;
3799 unsigned long size = perf_data_size(rb);
3801 atomic_dec(&rb->mmap_count);
3803 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
3806 /* Detach current event from the buffer. */
3807 rcu_assign_pointer(event->rb, NULL);
3808 ring_buffer_detach(event, rb);
3809 mutex_unlock(&event->mmap_mutex);
3811 /* If there's still other mmap()s of this buffer, we're done. */
3812 if (atomic_read(&rb->mmap_count)) {
3813 ring_buffer_put(rb); /* can't be last */
3818 * No other mmap()s, detach from all other events that might redirect
3819 * into the now unreachable buffer. Somewhat complicated by the
3820 * fact that rb::event_lock otherwise nests inside mmap_mutex.
3824 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
3825 if (!atomic_long_inc_not_zero(&event->refcount)) {
3827 * This event is en-route to free_event() which will
3828 * detach it and remove it from the list.
3834 mutex_lock(&event->mmap_mutex);
3836 * Check we didn't race with perf_event_set_output() which can
3837 * swizzle the rb from under us while we were waiting to
3838 * acquire mmap_mutex.
3840 * If we find a different rb; ignore this event, a next
3841 * iteration will no longer find it on the list. We have to
3842 * still restart the iteration to make sure we're not now
3843 * iterating the wrong list.
3845 if (event->rb == rb) {
3846 rcu_assign_pointer(event->rb, NULL);
3847 ring_buffer_detach(event, rb);
3848 ring_buffer_put(rb); /* can't be last, we still have one */
3850 mutex_unlock(&event->mmap_mutex);
3854 * Restart the iteration; either we're on the wrong list or
3855 * destroyed its integrity by doing a deletion.
3862 * It could be there's still a few 0-ref events on the list; they'll
3863 * get cleaned up by free_event() -- they'll also still have their
3864 * ref on the rb and will free it whenever they are done with it.
3866 * Aside from that, this buffer is 'fully' detached and unmapped,
3867 * undo the VM accounting.
3870 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
3871 vma->vm_mm->pinned_vm -= mmap_locked;
3872 free_uid(mmap_user);
3874 ring_buffer_put(rb); /* could be last */
3877 static const struct vm_operations_struct perf_mmap_vmops = {
3878 .open = perf_mmap_open,
3879 .close = perf_mmap_close,
3880 .fault = perf_mmap_fault,
3881 .page_mkwrite = perf_mmap_fault,
3884 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3886 struct perf_event *event = file->private_data;
3887 unsigned long user_locked, user_lock_limit;
3888 struct user_struct *user = current_user();
3889 unsigned long locked, lock_limit;
3890 struct ring_buffer *rb;
3891 unsigned long vma_size;
3892 unsigned long nr_pages;
3893 long user_extra, extra;
3894 int ret = 0, flags = 0;
3897 * Don't allow mmap() of inherited per-task counters. This would
3898 * create a performance issue due to all children writing to the
3901 if (event->cpu == -1 && event->attr.inherit)
3904 if (!(vma->vm_flags & VM_SHARED))
3907 vma_size = vma->vm_end - vma->vm_start;
3908 nr_pages = (vma_size / PAGE_SIZE) - 1;
3911 * If we have rb pages ensure they're a power-of-two number, so we
3912 * can do bitmasks instead of modulo.
3914 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3917 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3920 if (vma->vm_pgoff != 0)
3923 WARN_ON_ONCE(event->ctx->parent_ctx);
3925 mutex_lock(&event->mmap_mutex);
3927 if (event->rb->nr_pages != nr_pages) {
3932 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
3934 * Raced against perf_mmap_close() through
3935 * perf_event_set_output(). Try again, hope for better
3938 mutex_unlock(&event->mmap_mutex);
3945 user_extra = nr_pages + 1;
3946 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3949 * Increase the limit linearly with more CPUs:
3951 user_lock_limit *= num_online_cpus();
3953 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3956 if (user_locked > user_lock_limit)
3957 extra = user_locked - user_lock_limit;
3959 lock_limit = rlimit(RLIMIT_MEMLOCK);
3960 lock_limit >>= PAGE_SHIFT;
3961 locked = vma->vm_mm->pinned_vm + extra;
3963 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3964 !capable(CAP_IPC_LOCK)) {
3971 if (vma->vm_flags & VM_WRITE)
3972 flags |= RING_BUFFER_WRITABLE;
3974 rb = rb_alloc(nr_pages,
3975 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3983 atomic_set(&rb->mmap_count, 1);
3984 rb->mmap_locked = extra;
3985 rb->mmap_user = get_current_user();
3987 atomic_long_add(user_extra, &user->locked_vm);
3988 vma->vm_mm->pinned_vm += extra;
3990 ring_buffer_attach(event, rb);
3991 rcu_assign_pointer(event->rb, rb);
3993 perf_event_update_userpage(event);
3997 atomic_inc(&event->mmap_count);
3998 mutex_unlock(&event->mmap_mutex);
4001 * Since pinned accounting is per vm we cannot allow fork() to copy our
4004 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
4005 vma->vm_ops = &perf_mmap_vmops;
4010 static int perf_fasync(int fd, struct file *filp, int on)
4012 struct inode *inode = file_inode(filp);
4013 struct perf_event *event = filp->private_data;
4016 mutex_lock(&inode->i_mutex);
4017 retval = fasync_helper(fd, filp, on, &event->fasync);
4018 mutex_unlock(&inode->i_mutex);
4026 static const struct file_operations perf_fops = {
4027 .llseek = no_llseek,
4028 .release = perf_release,
4031 .unlocked_ioctl = perf_ioctl,
4032 .compat_ioctl = perf_ioctl,
4034 .fasync = perf_fasync,
4040 * If there's data, ensure we set the poll() state and publish everything
4041 * to user-space before waking everybody up.
4044 void perf_event_wakeup(struct perf_event *event)
4046 ring_buffer_wakeup(event);
4048 if (event->pending_kill) {
4049 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
4050 event->pending_kill = 0;
4054 static void perf_pending_event(struct irq_work *entry)
4056 struct perf_event *event = container_of(entry,
4057 struct perf_event, pending);
4059 if (event->pending_disable) {
4060 event->pending_disable = 0;
4061 __perf_event_disable(event);
4064 if (event->pending_wakeup) {
4065 event->pending_wakeup = 0;
4066 perf_event_wakeup(event);
4071 * We assume there is only KVM supporting the callbacks.
4072 * Later on, we might change it to a list if there is
4073 * another virtualization implementation supporting the callbacks.
4075 struct perf_guest_info_callbacks *perf_guest_cbs;
4077 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4079 perf_guest_cbs = cbs;
4082 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4084 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4086 perf_guest_cbs = NULL;
4089 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4092 perf_output_sample_regs(struct perf_output_handle *handle,
4093 struct pt_regs *regs, u64 mask)
4097 for_each_set_bit(bit, (const unsigned long *) &mask,
4098 sizeof(mask) * BITS_PER_BYTE) {
4101 val = perf_reg_value(regs, bit);
4102 perf_output_put(handle, val);
4106 static void perf_sample_regs_user(struct perf_regs_user *regs_user,
4107 struct pt_regs *regs)
4109 if (!user_mode(regs)) {
4111 regs = task_pt_regs(current);
4117 regs_user->regs = regs;
4118 regs_user->abi = perf_reg_abi(current);
4123 * Get remaining task size from user stack pointer.
4125 * It'd be better to take stack vma map and limit this more
4126 * precisly, but there's no way to get it safely under interrupt,
4127 * so using TASK_SIZE as limit.
4129 static u64 perf_ustack_task_size(struct pt_regs *regs)
4131 unsigned long addr = perf_user_stack_pointer(regs);
4133 if (!addr || addr >= TASK_SIZE)
4136 return TASK_SIZE - addr;
4140 perf_sample_ustack_size(u16 stack_size, u16 header_size,
4141 struct pt_regs *regs)
4145 /* No regs, no stack pointer, no dump. */
4150 * Check if we fit in with the requested stack size into the:
4152 * If we don't, we limit the size to the TASK_SIZE.
4154 * - remaining sample size
4155 * If we don't, we customize the stack size to
4156 * fit in to the remaining sample size.
4159 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
4160 stack_size = min(stack_size, (u16) task_size);
4162 /* Current header size plus static size and dynamic size. */
4163 header_size += 2 * sizeof(u64);
4165 /* Do we fit in with the current stack dump size? */
4166 if ((u16) (header_size + stack_size) < header_size) {
4168 * If we overflow the maximum size for the sample,
4169 * we customize the stack dump size to fit in.
4171 stack_size = USHRT_MAX - header_size - sizeof(u64);
4172 stack_size = round_up(stack_size, sizeof(u64));
4179 perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4180 struct pt_regs *regs)
4182 /* Case of a kernel thread, nothing to dump */
4185 perf_output_put(handle, size);
4194 * - the size requested by user or the best one we can fit
4195 * in to the sample max size
4197 * - user stack dump data
4199 * - the actual dumped size
4203 perf_output_put(handle, dump_size);
4206 sp = perf_user_stack_pointer(regs);
4207 rem = __output_copy_user(handle, (void *) sp, dump_size);
4208 dyn_size = dump_size - rem;
4210 perf_output_skip(handle, rem);
4213 perf_output_put(handle, dyn_size);
4217 static void __perf_event_header__init_id(struct perf_event_header *header,
4218 struct perf_sample_data *data,
4219 struct perf_event *event)
4221 u64 sample_type = event->attr.sample_type;
4223 data->type = sample_type;
4224 header->size += event->id_header_size;
4226 if (sample_type & PERF_SAMPLE_TID) {
4227 /* namespace issues */
4228 data->tid_entry.pid = perf_event_pid(event, current);
4229 data->tid_entry.tid = perf_event_tid(event, current);
4232 if (sample_type & PERF_SAMPLE_TIME)
4233 data->time = perf_clock();
4235 if (sample_type & PERF_SAMPLE_ID)
4236 data->id = primary_event_id(event);
4238 if (sample_type & PERF_SAMPLE_STREAM_ID)
4239 data->stream_id = event->id;
4241 if (sample_type & PERF_SAMPLE_CPU) {
4242 data->cpu_entry.cpu = raw_smp_processor_id();
4243 data->cpu_entry.reserved = 0;
4247 void perf_event_header__init_id(struct perf_event_header *header,
4248 struct perf_sample_data *data,
4249 struct perf_event *event)
4251 if (event->attr.sample_id_all)
4252 __perf_event_header__init_id(header, data, event);
4255 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4256 struct perf_sample_data *data)
4258 u64 sample_type = data->type;
4260 if (sample_type & PERF_SAMPLE_TID)
4261 perf_output_put(handle, data->tid_entry);
4263 if (sample_type & PERF_SAMPLE_TIME)
4264 perf_output_put(handle, data->time);
4266 if (sample_type & PERF_SAMPLE_ID)
4267 perf_output_put(handle, data->id);
4269 if (sample_type & PERF_SAMPLE_STREAM_ID)
4270 perf_output_put(handle, data->stream_id);
4272 if (sample_type & PERF_SAMPLE_CPU)
4273 perf_output_put(handle, data->cpu_entry);
4276 void perf_event__output_id_sample(struct perf_event *event,
4277 struct perf_output_handle *handle,
4278 struct perf_sample_data *sample)
4280 if (event->attr.sample_id_all)
4281 __perf_event__output_id_sample(handle, sample);
4284 static void perf_output_read_one(struct perf_output_handle *handle,
4285 struct perf_event *event,
4286 u64 enabled, u64 running)
4288 u64 read_format = event->attr.read_format;
4292 values[n++] = perf_event_count(event);
4293 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4294 values[n++] = enabled +
4295 atomic64_read(&event->child_total_time_enabled);
4297 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4298 values[n++] = running +
4299 atomic64_read(&event->child_total_time_running);
4301 if (read_format & PERF_FORMAT_ID)
4302 values[n++] = primary_event_id(event);
4304 __output_copy(handle, values, n * sizeof(u64));
4308 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4310 static void perf_output_read_group(struct perf_output_handle *handle,
4311 struct perf_event *event,
4312 u64 enabled, u64 running)
4314 struct perf_event *leader = event->group_leader, *sub;
4315 u64 read_format = event->attr.read_format;
4319 values[n++] = 1 + leader->nr_siblings;
4321 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4322 values[n++] = enabled;
4324 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4325 values[n++] = running;
4327 if (leader != event)
4328 leader->pmu->read(leader);
4330 values[n++] = perf_event_count(leader);
4331 if (read_format & PERF_FORMAT_ID)
4332 values[n++] = primary_event_id(leader);
4334 __output_copy(handle, values, n * sizeof(u64));
4336 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4340 sub->pmu->read(sub);
4342 values[n++] = perf_event_count(sub);
4343 if (read_format & PERF_FORMAT_ID)
4344 values[n++] = primary_event_id(sub);
4346 __output_copy(handle, values, n * sizeof(u64));
4350 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4351 PERF_FORMAT_TOTAL_TIME_RUNNING)
4353 static void perf_output_read(struct perf_output_handle *handle,
4354 struct perf_event *event)
4356 u64 enabled = 0, running = 0, now;
4357 u64 read_format = event->attr.read_format;
4360 * compute total_time_enabled, total_time_running
4361 * based on snapshot values taken when the event
4362 * was last scheduled in.
4364 * we cannot simply called update_context_time()
4365 * because of locking issue as we are called in
4368 if (read_format & PERF_FORMAT_TOTAL_TIMES)
4369 calc_timer_values(event, &now, &enabled, &running);
4371 if (event->attr.read_format & PERF_FORMAT_GROUP)
4372 perf_output_read_group(handle, event, enabled, running);
4374 perf_output_read_one(handle, event, enabled, running);
4377 void perf_output_sample(struct perf_output_handle *handle,
4378 struct perf_event_header *header,
4379 struct perf_sample_data *data,
4380 struct perf_event *event)
4382 u64 sample_type = data->type;
4384 perf_output_put(handle, *header);
4386 if (sample_type & PERF_SAMPLE_IP)
4387 perf_output_put(handle, data->ip);
4389 if (sample_type & PERF_SAMPLE_TID)
4390 perf_output_put(handle, data->tid_entry);
4392 if (sample_type & PERF_SAMPLE_TIME)
4393 perf_output_put(handle, data->time);
4395 if (sample_type & PERF_SAMPLE_ADDR)
4396 perf_output_put(handle, data->addr);
4398 if (sample_type & PERF_SAMPLE_ID)
4399 perf_output_put(handle, data->id);
4401 if (sample_type & PERF_SAMPLE_STREAM_ID)
4402 perf_output_put(handle, data->stream_id);
4404 if (sample_type & PERF_SAMPLE_CPU)
4405 perf_output_put(handle, data->cpu_entry);
4407 if (sample_type & PERF_SAMPLE_PERIOD)
4408 perf_output_put(handle, data->period);
4410 if (sample_type & PERF_SAMPLE_READ)
4411 perf_output_read(handle, event);
4413 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4414 if (data->callchain) {
4417 if (data->callchain)
4418 size += data->callchain->nr;
4420 size *= sizeof(u64);
4422 __output_copy(handle, data->callchain, size);
4425 perf_output_put(handle, nr);
4429 if (sample_type & PERF_SAMPLE_RAW) {
4431 perf_output_put(handle, data->raw->size);
4432 __output_copy(handle, data->raw->data,
4439 .size = sizeof(u32),
4442 perf_output_put(handle, raw);
4446 if (!event->attr.watermark) {
4447 int wakeup_events = event->attr.wakeup_events;
4449 if (wakeup_events) {
4450 struct ring_buffer *rb = handle->rb;
4451 int events = local_inc_return(&rb->events);
4453 if (events >= wakeup_events) {
4454 local_sub(wakeup_events, &rb->events);
4455 local_inc(&rb->wakeup);
4460 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4461 if (data->br_stack) {
4464 size = data->br_stack->nr
4465 * sizeof(struct perf_branch_entry);
4467 perf_output_put(handle, data->br_stack->nr);
4468 perf_output_copy(handle, data->br_stack->entries, size);
4471 * we always store at least the value of nr
4474 perf_output_put(handle, nr);
4478 if (sample_type & PERF_SAMPLE_REGS_USER) {
4479 u64 abi = data->regs_user.abi;
4482 * If there are no regs to dump, notice it through
4483 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4485 perf_output_put(handle, abi);
4488 u64 mask = event->attr.sample_regs_user;
4489 perf_output_sample_regs(handle,
4490 data->regs_user.regs,
4495 if (sample_type & PERF_SAMPLE_STACK_USER)
4496 perf_output_sample_ustack(handle,
4497 data->stack_user_size,
4498 data->regs_user.regs);
4500 if (sample_type & PERF_SAMPLE_WEIGHT)
4501 perf_output_put(handle, data->weight);
4503 if (sample_type & PERF_SAMPLE_DATA_SRC)
4504 perf_output_put(handle, data->data_src.val);
4507 void perf_prepare_sample(struct perf_event_header *header,
4508 struct perf_sample_data *data,
4509 struct perf_event *event,
4510 struct pt_regs *regs)
4512 u64 sample_type = event->attr.sample_type;
4514 header->type = PERF_RECORD_SAMPLE;
4515 header->size = sizeof(*header) + event->header_size;
4518 header->misc |= perf_misc_flags(regs);
4520 __perf_event_header__init_id(header, data, event);
4522 if (sample_type & PERF_SAMPLE_IP)
4523 data->ip = perf_instruction_pointer(regs);
4525 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4528 data->callchain = perf_callchain(event, regs);
4530 if (data->callchain)
4531 size += data->callchain->nr;
4533 header->size += size * sizeof(u64);
4536 if (sample_type & PERF_SAMPLE_RAW) {
4537 int size = sizeof(u32);
4540 size += data->raw->size;
4542 size += sizeof(u32);
4544 WARN_ON_ONCE(size & (sizeof(u64)-1));
4545 header->size += size;
4548 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4549 int size = sizeof(u64); /* nr */
4550 if (data->br_stack) {
4551 size += data->br_stack->nr
4552 * sizeof(struct perf_branch_entry);
4554 header->size += size;
4557 if (sample_type & PERF_SAMPLE_REGS_USER) {
4558 /* regs dump ABI info */
4559 int size = sizeof(u64);
4561 perf_sample_regs_user(&data->regs_user, regs);
4563 if (data->regs_user.regs) {
4564 u64 mask = event->attr.sample_regs_user;
4565 size += hweight64(mask) * sizeof(u64);
4568 header->size += size;
4571 if (sample_type & PERF_SAMPLE_STACK_USER) {
4573 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4574 * processed as the last one or have additional check added
4575 * in case new sample type is added, because we could eat
4576 * up the rest of the sample size.
4578 struct perf_regs_user *uregs = &data->regs_user;
4579 u16 stack_size = event->attr.sample_stack_user;
4580 u16 size = sizeof(u64);
4583 perf_sample_regs_user(uregs, regs);
4585 stack_size = perf_sample_ustack_size(stack_size, header->size,
4589 * If there is something to dump, add space for the dump
4590 * itself and for the field that tells the dynamic size,
4591 * which is how many have been actually dumped.
4594 size += sizeof(u64) + stack_size;
4596 data->stack_user_size = stack_size;
4597 header->size += size;
4601 static void perf_event_output(struct perf_event *event,
4602 struct perf_sample_data *data,
4603 struct pt_regs *regs)
4605 struct perf_output_handle handle;
4606 struct perf_event_header header;
4608 /* protect the callchain buffers */
4611 perf_prepare_sample(&header, data, event, regs);
4613 if (perf_output_begin(&handle, event, header.size))
4616 perf_output_sample(&handle, &header, data, event);
4618 perf_output_end(&handle);
4628 struct perf_read_event {
4629 struct perf_event_header header;
4636 perf_event_read_event(struct perf_event *event,
4637 struct task_struct *task)
4639 struct perf_output_handle handle;
4640 struct perf_sample_data sample;
4641 struct perf_read_event read_event = {
4643 .type = PERF_RECORD_READ,
4645 .size = sizeof(read_event) + event->read_size,
4647 .pid = perf_event_pid(event, task),
4648 .tid = perf_event_tid(event, task),
4652 perf_event_header__init_id(&read_event.header, &sample, event);
4653 ret = perf_output_begin(&handle, event, read_event.header.size);
4657 perf_output_put(&handle, read_event);
4658 perf_output_read(&handle, event);
4659 perf_event__output_id_sample(event, &handle, &sample);
4661 perf_output_end(&handle);
4664 typedef int (perf_event_aux_match_cb)(struct perf_event *event, void *data);
4665 typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
4668 perf_event_aux_ctx(struct perf_event_context *ctx,
4669 perf_event_aux_match_cb match,
4670 perf_event_aux_output_cb output,
4673 struct perf_event *event;
4675 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4676 if (event->state < PERF_EVENT_STATE_INACTIVE)
4678 if (!event_filter_match(event))
4680 if (match(event, data))
4681 output(event, data);
4686 perf_event_aux(perf_event_aux_match_cb match,
4687 perf_event_aux_output_cb output,
4689 struct perf_event_context *task_ctx)
4691 struct perf_cpu_context *cpuctx;
4692 struct perf_event_context *ctx;
4697 list_for_each_entry_rcu(pmu, &pmus, entry) {
4698 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4699 if (cpuctx->unique_pmu != pmu)
4701 perf_event_aux_ctx(&cpuctx->ctx, match, output, data);
4704 ctxn = pmu->task_ctx_nr;
4707 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4709 perf_event_aux_ctx(ctx, match, output, data);
4711 put_cpu_ptr(pmu->pmu_cpu_context);
4716 perf_event_aux_ctx(task_ctx, match, output, data);
4723 * task tracking -- fork/exit
4725 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4728 struct perf_task_event {
4729 struct task_struct *task;
4730 struct perf_event_context *task_ctx;
4733 struct perf_event_header header;
4743 static void perf_event_task_output(struct perf_event *event,
4746 struct perf_task_event *task_event = data;
4747 struct perf_output_handle handle;
4748 struct perf_sample_data sample;
4749 struct task_struct *task = task_event->task;
4750 int ret, size = task_event->event_id.header.size;
4752 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4754 ret = perf_output_begin(&handle, event,
4755 task_event->event_id.header.size);
4759 task_event->event_id.pid = perf_event_pid(event, task);
4760 task_event->event_id.ppid = perf_event_pid(event, current);
4762 task_event->event_id.tid = perf_event_tid(event, task);
4763 task_event->event_id.ptid = perf_event_tid(event, current);
4765 perf_output_put(&handle, task_event->event_id);
4767 perf_event__output_id_sample(event, &handle, &sample);
4769 perf_output_end(&handle);
4771 task_event->event_id.header.size = size;
4774 static int perf_event_task_match(struct perf_event *event,
4775 void *data __maybe_unused)
4777 return event->attr.comm || event->attr.mmap ||
4778 event->attr.mmap_data || event->attr.task;
4781 static void perf_event_task(struct task_struct *task,
4782 struct perf_event_context *task_ctx,
4785 struct perf_task_event task_event;
4787 if (!atomic_read(&nr_comm_events) &&
4788 !atomic_read(&nr_mmap_events) &&
4789 !atomic_read(&nr_task_events))
4792 task_event = (struct perf_task_event){
4794 .task_ctx = task_ctx,
4797 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4799 .size = sizeof(task_event.event_id),
4805 .time = perf_clock(),
4809 perf_event_aux(perf_event_task_match,
4810 perf_event_task_output,
4815 void perf_event_fork(struct task_struct *task)
4817 perf_event_task(task, NULL, 1);
4824 struct perf_comm_event {
4825 struct task_struct *task;
4830 struct perf_event_header header;
4837 static void perf_event_comm_output(struct perf_event *event,
4840 struct perf_comm_event *comm_event = data;
4841 struct perf_output_handle handle;
4842 struct perf_sample_data sample;
4843 int size = comm_event->event_id.header.size;
4846 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4847 ret = perf_output_begin(&handle, event,
4848 comm_event->event_id.header.size);
4853 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4854 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4856 perf_output_put(&handle, comm_event->event_id);
4857 __output_copy(&handle, comm_event->comm,
4858 comm_event->comm_size);
4860 perf_event__output_id_sample(event, &handle, &sample);
4862 perf_output_end(&handle);
4864 comm_event->event_id.header.size = size;
4867 static int perf_event_comm_match(struct perf_event *event,
4868 void *data __maybe_unused)
4870 return event->attr.comm;
4873 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4875 char comm[TASK_COMM_LEN];
4878 memset(comm, 0, sizeof(comm));
4879 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4880 size = ALIGN(strlen(comm)+1, sizeof(u64));
4882 comm_event->comm = comm;
4883 comm_event->comm_size = size;
4885 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4887 perf_event_aux(perf_event_comm_match,
4888 perf_event_comm_output,
4893 void perf_event_comm(struct task_struct *task)
4895 struct perf_comm_event comm_event;
4896 struct perf_event_context *ctx;
4900 for_each_task_context_nr(ctxn) {
4901 ctx = task->perf_event_ctxp[ctxn];
4905 perf_event_enable_on_exec(ctx);
4909 if (!atomic_read(&nr_comm_events))
4912 comm_event = (struct perf_comm_event){
4918 .type = PERF_RECORD_COMM,
4927 perf_event_comm_event(&comm_event);
4934 struct perf_mmap_event {
4935 struct vm_area_struct *vma;
4937 const char *file_name;
4941 struct perf_event_header header;
4951 static void perf_event_mmap_output(struct perf_event *event,
4954 struct perf_mmap_event *mmap_event = data;
4955 struct perf_output_handle handle;
4956 struct perf_sample_data sample;
4957 int size = mmap_event->event_id.header.size;
4960 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4961 ret = perf_output_begin(&handle, event,
4962 mmap_event->event_id.header.size);
4966 mmap_event->event_id.pid = perf_event_pid(event, current);
4967 mmap_event->event_id.tid = perf_event_tid(event, current);
4969 perf_output_put(&handle, mmap_event->event_id);
4970 __output_copy(&handle, mmap_event->file_name,
4971 mmap_event->file_size);
4973 perf_event__output_id_sample(event, &handle, &sample);
4975 perf_output_end(&handle);
4977 mmap_event->event_id.header.size = size;
4980 static int perf_event_mmap_match(struct perf_event *event,
4983 struct perf_mmap_event *mmap_event = data;
4984 struct vm_area_struct *vma = mmap_event->vma;
4985 int executable = vma->vm_flags & VM_EXEC;
4987 return (!executable && event->attr.mmap_data) ||
4988 (executable && event->attr.mmap);
4991 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4993 struct vm_area_struct *vma = mmap_event->vma;
4994 struct file *file = vma->vm_file;
5000 memset(tmp, 0, sizeof(tmp));
5004 * d_path works from the end of the rb backwards, so we
5005 * need to add enough zero bytes after the string to handle
5006 * the 64bit alignment we do later.
5008 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
5010 name = strncpy(tmp, "//enomem", sizeof(tmp));
5013 name = d_path(&file->f_path, buf, PATH_MAX);
5015 name = strncpy(tmp, "//toolong", sizeof(tmp));
5019 if (arch_vma_name(mmap_event->vma)) {
5020 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
5022 tmp[sizeof(tmp) - 1] = '\0';
5027 name = strncpy(tmp, "[vdso]", sizeof(tmp));
5029 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
5030 vma->vm_end >= vma->vm_mm->brk) {
5031 name = strncpy(tmp, "[heap]", sizeof(tmp));
5033 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
5034 vma->vm_end >= vma->vm_mm->start_stack) {
5035 name = strncpy(tmp, "[stack]", sizeof(tmp));
5039 name = strncpy(tmp, "//anon", sizeof(tmp));
5044 size = ALIGN(strlen(name)+1, sizeof(u64));
5046 mmap_event->file_name = name;
5047 mmap_event->file_size = size;
5049 if (!(vma->vm_flags & VM_EXEC))
5050 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
5052 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
5054 perf_event_aux(perf_event_mmap_match,
5055 perf_event_mmap_output,
5062 void perf_event_mmap(struct vm_area_struct *vma)
5064 struct perf_mmap_event mmap_event;
5066 if (!atomic_read(&nr_mmap_events))
5069 mmap_event = (struct perf_mmap_event){
5075 .type = PERF_RECORD_MMAP,
5076 .misc = PERF_RECORD_MISC_USER,
5081 .start = vma->vm_start,
5082 .len = vma->vm_end - vma->vm_start,
5083 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
5087 perf_event_mmap_event(&mmap_event);
5091 * IRQ throttle logging
5094 static void perf_log_throttle(struct perf_event *event, int enable)
5096 struct perf_output_handle handle;
5097 struct perf_sample_data sample;
5101 struct perf_event_header header;
5105 } throttle_event = {
5107 .type = PERF_RECORD_THROTTLE,
5109 .size = sizeof(throttle_event),
5111 .time = perf_clock(),
5112 .id = primary_event_id(event),
5113 .stream_id = event->id,
5117 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
5119 perf_event_header__init_id(&throttle_event.header, &sample, event);
5121 ret = perf_output_begin(&handle, event,
5122 throttle_event.header.size);
5126 perf_output_put(&handle, throttle_event);
5127 perf_event__output_id_sample(event, &handle, &sample);
5128 perf_output_end(&handle);
5132 * Generic event overflow handling, sampling.
5135 static int __perf_event_overflow(struct perf_event *event,
5136 int throttle, struct perf_sample_data *data,
5137 struct pt_regs *regs)
5139 int events = atomic_read(&event->event_limit);
5140 struct hw_perf_event *hwc = &event->hw;
5145 * Non-sampling counters might still use the PMI to fold short
5146 * hardware counters, ignore those.
5148 if (unlikely(!is_sampling_event(event)))
5151 seq = __this_cpu_read(perf_throttled_seq);
5152 if (seq != hwc->interrupts_seq) {
5153 hwc->interrupts_seq = seq;
5154 hwc->interrupts = 1;
5157 if (unlikely(throttle
5158 && hwc->interrupts >= max_samples_per_tick)) {
5159 __this_cpu_inc(perf_throttled_count);
5160 hwc->interrupts = MAX_INTERRUPTS;
5161 perf_log_throttle(event, 0);
5166 if (event->attr.freq) {
5167 u64 now = perf_clock();
5168 s64 delta = now - hwc->freq_time_stamp;
5170 hwc->freq_time_stamp = now;
5172 if (delta > 0 && delta < 2*TICK_NSEC)
5173 perf_adjust_period(event, delta, hwc->last_period, true);
5177 * XXX event_limit might not quite work as expected on inherited
5181 event->pending_kill = POLL_IN;
5182 if (events && atomic_dec_and_test(&event->event_limit)) {
5184 event->pending_kill = POLL_HUP;
5185 event->pending_disable = 1;
5186 irq_work_queue(&event->pending);
5189 if (event->overflow_handler)
5190 event->overflow_handler(event, data, regs);
5192 perf_event_output(event, data, regs);
5194 if (event->fasync && event->pending_kill) {
5195 event->pending_wakeup = 1;
5196 irq_work_queue(&event->pending);
5202 int perf_event_overflow(struct perf_event *event,
5203 struct perf_sample_data *data,
5204 struct pt_regs *regs)
5206 return __perf_event_overflow(event, 1, data, regs);
5210 * Generic software event infrastructure
5213 struct swevent_htable {
5214 struct swevent_hlist *swevent_hlist;
5215 struct mutex hlist_mutex;
5218 /* Recursion avoidance in each contexts */
5219 int recursion[PERF_NR_CONTEXTS];
5222 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5225 * We directly increment event->count and keep a second value in
5226 * event->hw.period_left to count intervals. This period event
5227 * is kept in the range [-sample_period, 0] so that we can use the
5231 u64 perf_swevent_set_period(struct perf_event *event)
5233 struct hw_perf_event *hwc = &event->hw;
5234 u64 period = hwc->last_period;
5238 hwc->last_period = hwc->sample_period;
5241 old = val = local64_read(&hwc->period_left);
5245 nr = div64_u64(period + val, period);
5246 offset = nr * period;
5248 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
5254 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
5255 struct perf_sample_data *data,
5256 struct pt_regs *regs)
5258 struct hw_perf_event *hwc = &event->hw;
5262 overflow = perf_swevent_set_period(event);
5264 if (hwc->interrupts == MAX_INTERRUPTS)
5267 for (; overflow; overflow--) {
5268 if (__perf_event_overflow(event, throttle,
5271 * We inhibit the overflow from happening when
5272 * hwc->interrupts == MAX_INTERRUPTS.
5280 static void perf_swevent_event(struct perf_event *event, u64 nr,
5281 struct perf_sample_data *data,
5282 struct pt_regs *regs)
5284 struct hw_perf_event *hwc = &event->hw;
5286 local64_add(nr, &event->count);
5291 if (!is_sampling_event(event))
5294 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5296 return perf_swevent_overflow(event, 1, data, regs);
5298 data->period = event->hw.last_period;
5300 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
5301 return perf_swevent_overflow(event, 1, data, regs);
5303 if (local64_add_negative(nr, &hwc->period_left))
5306 perf_swevent_overflow(event, 0, data, regs);
5309 static int perf_exclude_event(struct perf_event *event,
5310 struct pt_regs *regs)
5312 if (event->hw.state & PERF_HES_STOPPED)
5316 if (event->attr.exclude_user && user_mode(regs))
5319 if (event->attr.exclude_kernel && !user_mode(regs))
5326 static int perf_swevent_match(struct perf_event *event,
5327 enum perf_type_id type,
5329 struct perf_sample_data *data,
5330 struct pt_regs *regs)
5332 if (event->attr.type != type)
5335 if (event->attr.config != event_id)
5338 if (perf_exclude_event(event, regs))
5344 static inline u64 swevent_hash(u64 type, u32 event_id)
5346 u64 val = event_id | (type << 32);
5348 return hash_64(val, SWEVENT_HLIST_BITS);
5351 static inline struct hlist_head *
5352 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
5354 u64 hash = swevent_hash(type, event_id);
5356 return &hlist->heads[hash];
5359 /* For the read side: events when they trigger */
5360 static inline struct hlist_head *
5361 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
5363 struct swevent_hlist *hlist;
5365 hlist = rcu_dereference(swhash->swevent_hlist);
5369 return __find_swevent_head(hlist, type, event_id);
5372 /* For the event head insertion and removal in the hlist */
5373 static inline struct hlist_head *
5374 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
5376 struct swevent_hlist *hlist;
5377 u32 event_id = event->attr.config;
5378 u64 type = event->attr.type;
5381 * Event scheduling is always serialized against hlist allocation
5382 * and release. Which makes the protected version suitable here.
5383 * The context lock guarantees that.
5385 hlist = rcu_dereference_protected(swhash->swevent_hlist,
5386 lockdep_is_held(&event->ctx->lock));
5390 return __find_swevent_head(hlist, type, event_id);
5393 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5395 struct perf_sample_data *data,
5396 struct pt_regs *regs)
5398 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5399 struct perf_event *event;
5400 struct hlist_head *head;
5403 head = find_swevent_head_rcu(swhash, type, event_id);
5407 hlist_for_each_entry_rcu(event, head, hlist_entry) {
5408 if (perf_swevent_match(event, type, event_id, data, regs))
5409 perf_swevent_event(event, nr, data, regs);
5415 int perf_swevent_get_recursion_context(void)
5417 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5419 return get_recursion_context(swhash->recursion);
5421 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
5423 inline void perf_swevent_put_recursion_context(int rctx)
5425 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5427 put_recursion_context(swhash->recursion, rctx);
5430 void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
5432 struct perf_sample_data data;
5435 preempt_disable_notrace();
5436 rctx = perf_swevent_get_recursion_context();
5440 perf_sample_data_init(&data, addr, 0);
5442 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
5444 perf_swevent_put_recursion_context(rctx);
5445 preempt_enable_notrace();
5448 static void perf_swevent_read(struct perf_event *event)
5452 static int perf_swevent_add(struct perf_event *event, int flags)
5454 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5455 struct hw_perf_event *hwc = &event->hw;
5456 struct hlist_head *head;
5458 if (is_sampling_event(event)) {
5459 hwc->last_period = hwc->sample_period;
5460 perf_swevent_set_period(event);
5463 hwc->state = !(flags & PERF_EF_START);
5465 head = find_swevent_head(swhash, event);
5466 if (WARN_ON_ONCE(!head))
5469 hlist_add_head_rcu(&event->hlist_entry, head);
5474 static void perf_swevent_del(struct perf_event *event, int flags)
5476 hlist_del_rcu(&event->hlist_entry);
5479 static void perf_swevent_start(struct perf_event *event, int flags)
5481 event->hw.state = 0;
5484 static void perf_swevent_stop(struct perf_event *event, int flags)
5486 event->hw.state = PERF_HES_STOPPED;
5489 /* Deref the hlist from the update side */
5490 static inline struct swevent_hlist *
5491 swevent_hlist_deref(struct swevent_htable *swhash)
5493 return rcu_dereference_protected(swhash->swevent_hlist,
5494 lockdep_is_held(&swhash->hlist_mutex));
5497 static void swevent_hlist_release(struct swevent_htable *swhash)
5499 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
5504 rcu_assign_pointer(swhash->swevent_hlist, NULL);
5505 kfree_rcu(hlist, rcu_head);
5508 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5510 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5512 mutex_lock(&swhash->hlist_mutex);
5514 if (!--swhash->hlist_refcount)
5515 swevent_hlist_release(swhash);
5517 mutex_unlock(&swhash->hlist_mutex);
5520 static void swevent_hlist_put(struct perf_event *event)
5524 if (event->cpu != -1) {
5525 swevent_hlist_put_cpu(event, event->cpu);
5529 for_each_possible_cpu(cpu)
5530 swevent_hlist_put_cpu(event, cpu);
5533 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5535 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5538 mutex_lock(&swhash->hlist_mutex);
5540 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
5541 struct swevent_hlist *hlist;
5543 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5548 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5550 swhash->hlist_refcount++;
5552 mutex_unlock(&swhash->hlist_mutex);
5557 static int swevent_hlist_get(struct perf_event *event)
5560 int cpu, failed_cpu;
5562 if (event->cpu != -1)
5563 return swevent_hlist_get_cpu(event, event->cpu);
5566 for_each_possible_cpu(cpu) {
5567 err = swevent_hlist_get_cpu(event, cpu);
5577 for_each_possible_cpu(cpu) {
5578 if (cpu == failed_cpu)
5580 swevent_hlist_put_cpu(event, cpu);
5587 struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
5589 static void sw_perf_event_destroy(struct perf_event *event)
5591 u64 event_id = event->attr.config;
5593 WARN_ON(event->parent);
5595 static_key_slow_dec(&perf_swevent_enabled[event_id]);
5596 swevent_hlist_put(event);
5599 static int perf_swevent_init(struct perf_event *event)
5601 u64 event_id = event->attr.config;
5603 if (event->attr.type != PERF_TYPE_SOFTWARE)
5607 * no branch sampling for software events
5609 if (has_branch_stack(event))
5613 case PERF_COUNT_SW_CPU_CLOCK:
5614 case PERF_COUNT_SW_TASK_CLOCK:
5621 if (event_id >= PERF_COUNT_SW_MAX)
5624 if (!event->parent) {
5627 err = swevent_hlist_get(event);
5631 static_key_slow_inc(&perf_swevent_enabled[event_id]);
5632 event->destroy = sw_perf_event_destroy;
5638 static int perf_swevent_event_idx(struct perf_event *event)
5643 static struct pmu perf_swevent = {
5644 .task_ctx_nr = perf_sw_context,
5646 .event_init = perf_swevent_init,
5647 .add = perf_swevent_add,
5648 .del = perf_swevent_del,
5649 .start = perf_swevent_start,
5650 .stop = perf_swevent_stop,
5651 .read = perf_swevent_read,
5653 .event_idx = perf_swevent_event_idx,
5656 #ifdef CONFIG_EVENT_TRACING
5658 static int perf_tp_filter_match(struct perf_event *event,
5659 struct perf_sample_data *data)
5661 void *record = data->raw->data;
5663 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5668 static int perf_tp_event_match(struct perf_event *event,
5669 struct perf_sample_data *data,
5670 struct pt_regs *regs)
5672 if (event->hw.state & PERF_HES_STOPPED)
5675 * All tracepoints are from kernel-space.
5677 if (event->attr.exclude_kernel)
5680 if (!perf_tp_filter_match(event, data))
5686 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5687 struct pt_regs *regs, struct hlist_head *head, int rctx,
5688 struct task_struct *task)
5690 struct perf_sample_data data;
5691 struct perf_event *event;
5693 struct perf_raw_record raw = {
5698 perf_sample_data_init(&data, addr, 0);
5701 hlist_for_each_entry_rcu(event, head, hlist_entry) {
5702 if (perf_tp_event_match(event, &data, regs))
5703 perf_swevent_event(event, count, &data, regs);
5707 * If we got specified a target task, also iterate its context and
5708 * deliver this event there too.
5710 if (task && task != current) {
5711 struct perf_event_context *ctx;
5712 struct trace_entry *entry = record;
5715 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
5719 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5720 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5722 if (event->attr.config != entry->type)
5724 if (perf_tp_event_match(event, &data, regs))
5725 perf_swevent_event(event, count, &data, regs);
5731 perf_swevent_put_recursion_context(rctx);
5733 EXPORT_SYMBOL_GPL(perf_tp_event);
5735 static void tp_perf_event_destroy(struct perf_event *event)
5737 perf_trace_destroy(event);
5740 static int perf_tp_event_init(struct perf_event *event)
5744 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5748 * no branch sampling for tracepoint events
5750 if (has_branch_stack(event))
5753 err = perf_trace_init(event);
5757 event->destroy = tp_perf_event_destroy;
5762 static struct pmu perf_tracepoint = {
5763 .task_ctx_nr = perf_sw_context,
5765 .event_init = perf_tp_event_init,
5766 .add = perf_trace_add,
5767 .del = perf_trace_del,
5768 .start = perf_swevent_start,
5769 .stop = perf_swevent_stop,
5770 .read = perf_swevent_read,
5772 .event_idx = perf_swevent_event_idx,
5775 static inline void perf_tp_register(void)
5777 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5780 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5785 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5788 filter_str = strndup_user(arg, PAGE_SIZE);
5789 if (IS_ERR(filter_str))
5790 return PTR_ERR(filter_str);
5792 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5798 static void perf_event_free_filter(struct perf_event *event)
5800 ftrace_profile_free_filter(event);
5805 static inline void perf_tp_register(void)
5809 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5814 static void perf_event_free_filter(struct perf_event *event)
5818 #endif /* CONFIG_EVENT_TRACING */
5820 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5821 void perf_bp_event(struct perf_event *bp, void *data)
5823 struct perf_sample_data sample;
5824 struct pt_regs *regs = data;
5826 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
5828 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5829 perf_swevent_event(bp, 1, &sample, regs);
5834 * hrtimer based swevent callback
5837 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5839 enum hrtimer_restart ret = HRTIMER_RESTART;
5840 struct perf_sample_data data;
5841 struct pt_regs *regs;
5842 struct perf_event *event;
5845 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5847 if (event->state != PERF_EVENT_STATE_ACTIVE)
5848 return HRTIMER_NORESTART;
5850 event->pmu->read(event);
5852 perf_sample_data_init(&data, 0, event->hw.last_period);
5853 regs = get_irq_regs();
5855 if (regs && !perf_exclude_event(event, regs)) {
5856 if (!(event->attr.exclude_idle && is_idle_task(current)))
5857 if (__perf_event_overflow(event, 1, &data, regs))
5858 ret = HRTIMER_NORESTART;
5861 period = max_t(u64, 10000, event->hw.sample_period);
5862 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5867 static void perf_swevent_start_hrtimer(struct perf_event *event)
5869 struct hw_perf_event *hwc = &event->hw;
5872 if (!is_sampling_event(event))
5875 period = local64_read(&hwc->period_left);
5880 local64_set(&hwc->period_left, 0);
5882 period = max_t(u64, 10000, hwc->sample_period);
5884 __hrtimer_start_range_ns(&hwc->hrtimer,
5885 ns_to_ktime(period), 0,
5886 HRTIMER_MODE_REL_PINNED, 0);
5889 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5891 struct hw_perf_event *hwc = &event->hw;
5893 if (is_sampling_event(event)) {
5894 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5895 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5897 hrtimer_cancel(&hwc->hrtimer);
5901 static void perf_swevent_init_hrtimer(struct perf_event *event)
5903 struct hw_perf_event *hwc = &event->hw;
5905 if (!is_sampling_event(event))
5908 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5909 hwc->hrtimer.function = perf_swevent_hrtimer;
5912 * Since hrtimers have a fixed rate, we can do a static freq->period
5913 * mapping and avoid the whole period adjust feedback stuff.
5915 if (event->attr.freq) {
5916 long freq = event->attr.sample_freq;
5918 event->attr.sample_period = NSEC_PER_SEC / freq;
5919 hwc->sample_period = event->attr.sample_period;
5920 local64_set(&hwc->period_left, hwc->sample_period);
5921 hwc->last_period = hwc->sample_period;
5922 event->attr.freq = 0;
5927 * Software event: cpu wall time clock
5930 static void cpu_clock_event_update(struct perf_event *event)
5935 now = local_clock();
5936 prev = local64_xchg(&event->hw.prev_count, now);
5937 local64_add(now - prev, &event->count);
5940 static void cpu_clock_event_start(struct perf_event *event, int flags)
5942 local64_set(&event->hw.prev_count, local_clock());
5943 perf_swevent_start_hrtimer(event);
5946 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5948 perf_swevent_cancel_hrtimer(event);
5949 cpu_clock_event_update(event);
5952 static int cpu_clock_event_add(struct perf_event *event, int flags)
5954 if (flags & PERF_EF_START)
5955 cpu_clock_event_start(event, flags);
5960 static void cpu_clock_event_del(struct perf_event *event, int flags)
5962 cpu_clock_event_stop(event, flags);
5965 static void cpu_clock_event_read(struct perf_event *event)
5967 cpu_clock_event_update(event);
5970 static int cpu_clock_event_init(struct perf_event *event)
5972 if (event->attr.type != PERF_TYPE_SOFTWARE)
5975 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5979 * no branch sampling for software events
5981 if (has_branch_stack(event))
5984 perf_swevent_init_hrtimer(event);
5989 static struct pmu perf_cpu_clock = {
5990 .task_ctx_nr = perf_sw_context,
5992 .event_init = cpu_clock_event_init,
5993 .add = cpu_clock_event_add,
5994 .del = cpu_clock_event_del,
5995 .start = cpu_clock_event_start,
5996 .stop = cpu_clock_event_stop,
5997 .read = cpu_clock_event_read,
5999 .event_idx = perf_swevent_event_idx,
6003 * Software event: task time clock
6006 static void task_clock_event_update(struct perf_event *event, u64 now)
6011 prev = local64_xchg(&event->hw.prev_count, now);
6013 local64_add(delta, &event->count);
6016 static void task_clock_event_start(struct perf_event *event, int flags)
6018 local64_set(&event->hw.prev_count, event->ctx->time);
6019 perf_swevent_start_hrtimer(event);
6022 static void task_clock_event_stop(struct perf_event *event, int flags)
6024 perf_swevent_cancel_hrtimer(event);
6025 task_clock_event_update(event, event->ctx->time);
6028 static int task_clock_event_add(struct perf_event *event, int flags)
6030 if (flags & PERF_EF_START)
6031 task_clock_event_start(event, flags);
6036 static void task_clock_event_del(struct perf_event *event, int flags)
6038 task_clock_event_stop(event, PERF_EF_UPDATE);
6041 static void task_clock_event_read(struct perf_event *event)
6043 u64 now = perf_clock();
6044 u64 delta = now - event->ctx->timestamp;
6045 u64 time = event->ctx->time + delta;
6047 task_clock_event_update(event, time);
6050 static int task_clock_event_init(struct perf_event *event)
6052 if (event->attr.type != PERF_TYPE_SOFTWARE)
6055 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
6059 * no branch sampling for software events
6061 if (has_branch_stack(event))
6064 perf_swevent_init_hrtimer(event);
6069 static struct pmu perf_task_clock = {
6070 .task_ctx_nr = perf_sw_context,
6072 .event_init = task_clock_event_init,
6073 .add = task_clock_event_add,
6074 .del = task_clock_event_del,
6075 .start = task_clock_event_start,
6076 .stop = task_clock_event_stop,
6077 .read = task_clock_event_read,
6079 .event_idx = perf_swevent_event_idx,
6082 static void perf_pmu_nop_void(struct pmu *pmu)
6086 static int perf_pmu_nop_int(struct pmu *pmu)
6091 static void perf_pmu_start_txn(struct pmu *pmu)
6093 perf_pmu_disable(pmu);
6096 static int perf_pmu_commit_txn(struct pmu *pmu)
6098 perf_pmu_enable(pmu);
6102 static void perf_pmu_cancel_txn(struct pmu *pmu)
6104 perf_pmu_enable(pmu);
6107 static int perf_event_idx_default(struct perf_event *event)
6109 return event->hw.idx + 1;
6113 * Ensures all contexts with the same task_ctx_nr have the same
6114 * pmu_cpu_context too.
6116 static void *find_pmu_context(int ctxn)
6123 list_for_each_entry(pmu, &pmus, entry) {
6124 if (pmu->task_ctx_nr == ctxn)
6125 return pmu->pmu_cpu_context;
6131 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
6135 for_each_possible_cpu(cpu) {
6136 struct perf_cpu_context *cpuctx;
6138 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6140 if (cpuctx->unique_pmu == old_pmu)
6141 cpuctx->unique_pmu = pmu;
6145 static void free_pmu_context(struct pmu *pmu)
6149 mutex_lock(&pmus_lock);
6151 * Like a real lame refcount.
6153 list_for_each_entry(i, &pmus, entry) {
6154 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
6155 update_pmu_context(i, pmu);
6160 free_percpu(pmu->pmu_cpu_context);
6162 mutex_unlock(&pmus_lock);
6164 static struct idr pmu_idr;
6167 type_show(struct device *dev, struct device_attribute *attr, char *page)
6169 struct pmu *pmu = dev_get_drvdata(dev);
6171 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6175 perf_event_mux_interval_ms_show(struct device *dev,
6176 struct device_attribute *attr,
6179 struct pmu *pmu = dev_get_drvdata(dev);
6181 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6185 perf_event_mux_interval_ms_store(struct device *dev,
6186 struct device_attribute *attr,
6187 const char *buf, size_t count)
6189 struct pmu *pmu = dev_get_drvdata(dev);
6190 int timer, cpu, ret;
6192 ret = kstrtoint(buf, 0, &timer);
6199 /* same value, noting to do */
6200 if (timer == pmu->hrtimer_interval_ms)
6203 pmu->hrtimer_interval_ms = timer;
6205 /* update all cpuctx for this PMU */
6206 for_each_possible_cpu(cpu) {
6207 struct perf_cpu_context *cpuctx;
6208 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6209 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6211 if (hrtimer_active(&cpuctx->hrtimer))
6212 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6218 #define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store)
6220 static struct device_attribute pmu_dev_attrs[] = {
6222 __ATTR_RW(perf_event_mux_interval_ms),
6226 static int pmu_bus_running;
6227 static struct bus_type pmu_bus = {
6228 .name = "event_source",
6229 .dev_attrs = pmu_dev_attrs,
6232 static void pmu_dev_release(struct device *dev)
6237 static int pmu_dev_alloc(struct pmu *pmu)
6241 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6245 pmu->dev->groups = pmu->attr_groups;
6246 device_initialize(pmu->dev);
6247 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6251 dev_set_drvdata(pmu->dev, pmu);
6252 pmu->dev->bus = &pmu_bus;
6253 pmu->dev->release = pmu_dev_release;
6254 ret = device_add(pmu->dev);
6262 put_device(pmu->dev);
6266 static struct lock_class_key cpuctx_mutex;
6267 static struct lock_class_key cpuctx_lock;
6269 int perf_pmu_register(struct pmu *pmu, const char *name, int type)
6273 mutex_lock(&pmus_lock);
6275 pmu->pmu_disable_count = alloc_percpu(int);
6276 if (!pmu->pmu_disable_count)
6285 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6293 if (pmu_bus_running) {
6294 ret = pmu_dev_alloc(pmu);
6300 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6301 if (pmu->pmu_cpu_context)
6302 goto got_cpu_context;
6305 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6306 if (!pmu->pmu_cpu_context)
6309 for_each_possible_cpu(cpu) {
6310 struct perf_cpu_context *cpuctx;
6312 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6313 __perf_event_init_context(&cpuctx->ctx);
6314 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
6315 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
6316 cpuctx->ctx.type = cpu_context;
6317 cpuctx->ctx.pmu = pmu;
6319 __perf_cpu_hrtimer_init(cpuctx, cpu);
6321 INIT_LIST_HEAD(&cpuctx->rotation_list);
6322 cpuctx->unique_pmu = pmu;
6326 if (!pmu->start_txn) {
6327 if (pmu->pmu_enable) {
6329 * If we have pmu_enable/pmu_disable calls, install
6330 * transaction stubs that use that to try and batch
6331 * hardware accesses.
6333 pmu->start_txn = perf_pmu_start_txn;
6334 pmu->commit_txn = perf_pmu_commit_txn;
6335 pmu->cancel_txn = perf_pmu_cancel_txn;
6337 pmu->start_txn = perf_pmu_nop_void;
6338 pmu->commit_txn = perf_pmu_nop_int;
6339 pmu->cancel_txn = perf_pmu_nop_void;
6343 if (!pmu->pmu_enable) {
6344 pmu->pmu_enable = perf_pmu_nop_void;
6345 pmu->pmu_disable = perf_pmu_nop_void;
6348 if (!pmu->event_idx)
6349 pmu->event_idx = perf_event_idx_default;
6351 list_add_rcu(&pmu->entry, &pmus);
6354 mutex_unlock(&pmus_lock);
6359 device_del(pmu->dev);
6360 put_device(pmu->dev);
6363 if (pmu->type >= PERF_TYPE_MAX)
6364 idr_remove(&pmu_idr, pmu->type);
6367 free_percpu(pmu->pmu_disable_count);
6371 void perf_pmu_unregister(struct pmu *pmu)
6373 mutex_lock(&pmus_lock);
6374 list_del_rcu(&pmu->entry);
6375 mutex_unlock(&pmus_lock);
6378 * We dereference the pmu list under both SRCU and regular RCU, so
6379 * synchronize against both of those.
6381 synchronize_srcu(&pmus_srcu);
6384 free_percpu(pmu->pmu_disable_count);
6385 if (pmu->type >= PERF_TYPE_MAX)
6386 idr_remove(&pmu_idr, pmu->type);
6387 device_del(pmu->dev);
6388 put_device(pmu->dev);
6389 free_pmu_context(pmu);
6392 struct pmu *perf_init_event(struct perf_event *event)
6394 struct pmu *pmu = NULL;
6398 idx = srcu_read_lock(&pmus_srcu);
6401 pmu = idr_find(&pmu_idr, event->attr.type);
6405 ret = pmu->event_init(event);
6411 list_for_each_entry_rcu(pmu, &pmus, entry) {
6413 ret = pmu->event_init(event);
6417 if (ret != -ENOENT) {
6422 pmu = ERR_PTR(-ENOENT);
6424 srcu_read_unlock(&pmus_srcu, idx);
6430 * Allocate and initialize a event structure
6432 static struct perf_event *
6433 perf_event_alloc(struct perf_event_attr *attr, int cpu,
6434 struct task_struct *task,
6435 struct perf_event *group_leader,
6436 struct perf_event *parent_event,
6437 perf_overflow_handler_t overflow_handler,
6441 struct perf_event *event;
6442 struct hw_perf_event *hwc;
6445 if ((unsigned)cpu >= nr_cpu_ids) {
6446 if (!task || cpu != -1)
6447 return ERR_PTR(-EINVAL);
6450 event = kzalloc(sizeof(*event), GFP_KERNEL);
6452 return ERR_PTR(-ENOMEM);
6455 * Single events are their own group leaders, with an
6456 * empty sibling list:
6459 group_leader = event;
6461 mutex_init(&event->child_mutex);
6462 INIT_LIST_HEAD(&event->child_list);
6464 INIT_LIST_HEAD(&event->group_entry);
6465 INIT_LIST_HEAD(&event->event_entry);
6466 INIT_LIST_HEAD(&event->sibling_list);
6467 INIT_LIST_HEAD(&event->rb_entry);
6469 init_waitqueue_head(&event->waitq);
6470 init_irq_work(&event->pending, perf_pending_event);
6472 mutex_init(&event->mmap_mutex);
6474 atomic_long_set(&event->refcount, 1);
6476 event->attr = *attr;
6477 event->group_leader = group_leader;
6481 event->parent = parent_event;
6483 event->ns = get_pid_ns(task_active_pid_ns(current));
6484 event->id = atomic64_inc_return(&perf_event_id);
6486 event->state = PERF_EVENT_STATE_INACTIVE;
6489 event->attach_state = PERF_ATTACH_TASK;
6491 if (attr->type == PERF_TYPE_TRACEPOINT)
6492 event->hw.tp_target = task;
6493 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6495 * hw_breakpoint is a bit difficult here..
6497 else if (attr->type == PERF_TYPE_BREAKPOINT)
6498 event->hw.bp_target = task;
6502 if (!overflow_handler && parent_event) {
6503 overflow_handler = parent_event->overflow_handler;
6504 context = parent_event->overflow_handler_context;
6507 event->overflow_handler = overflow_handler;
6508 event->overflow_handler_context = context;
6510 perf_event__state_init(event);
6515 hwc->sample_period = attr->sample_period;
6516 if (attr->freq && attr->sample_freq)
6517 hwc->sample_period = 1;
6518 hwc->last_period = hwc->sample_period;
6520 local64_set(&hwc->period_left, hwc->sample_period);
6523 * we currently do not support PERF_FORMAT_GROUP on inherited events
6525 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6528 pmu = perf_init_event(event);
6534 else if (IS_ERR(pmu))
6539 put_pid_ns(event->ns);
6541 return ERR_PTR(err);
6544 if (!event->parent) {
6545 if (event->attach_state & PERF_ATTACH_TASK)
6546 static_key_slow_inc(&perf_sched_events.key);
6547 if (event->attr.mmap || event->attr.mmap_data)
6548 atomic_inc(&nr_mmap_events);
6549 if (event->attr.comm)
6550 atomic_inc(&nr_comm_events);
6551 if (event->attr.task)
6552 atomic_inc(&nr_task_events);
6553 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6554 err = get_callchain_buffers();
6557 return ERR_PTR(err);
6560 if (has_branch_stack(event)) {
6561 static_key_slow_inc(&perf_sched_events.key);
6562 if (!(event->attach_state & PERF_ATTACH_TASK))
6563 atomic_inc(&per_cpu(perf_branch_stack_events,
6571 static int perf_copy_attr(struct perf_event_attr __user *uattr,
6572 struct perf_event_attr *attr)
6577 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6581 * zero the full structure, so that a short copy will be nice.
6583 memset(attr, 0, sizeof(*attr));
6585 ret = get_user(size, &uattr->size);
6589 if (size > PAGE_SIZE) /* silly large */
6592 if (!size) /* abi compat */
6593 size = PERF_ATTR_SIZE_VER0;
6595 if (size < PERF_ATTR_SIZE_VER0)
6599 * If we're handed a bigger struct than we know of,
6600 * ensure all the unknown bits are 0 - i.e. new
6601 * user-space does not rely on any kernel feature
6602 * extensions we dont know about yet.
6604 if (size > sizeof(*attr)) {
6605 unsigned char __user *addr;
6606 unsigned char __user *end;
6609 addr = (void __user *)uattr + sizeof(*attr);
6610 end = (void __user *)uattr + size;
6612 for (; addr < end; addr++) {
6613 ret = get_user(val, addr);
6619 size = sizeof(*attr);
6622 ret = copy_from_user(attr, uattr, size);
6626 if (attr->__reserved_1)
6629 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6632 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6635 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
6636 u64 mask = attr->branch_sample_type;
6638 /* only using defined bits */
6639 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6642 /* at least one branch bit must be set */
6643 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6646 /* propagate priv level, when not set for branch */
6647 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6649 /* exclude_kernel checked on syscall entry */
6650 if (!attr->exclude_kernel)
6651 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6653 if (!attr->exclude_user)
6654 mask |= PERF_SAMPLE_BRANCH_USER;
6656 if (!attr->exclude_hv)
6657 mask |= PERF_SAMPLE_BRANCH_HV;
6659 * adjust user setting (for HW filter setup)
6661 attr->branch_sample_type = mask;
6663 /* privileged levels capture (kernel, hv): check permissions */
6664 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
6665 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6669 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
6670 ret = perf_reg_validate(attr->sample_regs_user);
6675 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
6676 if (!arch_perf_have_user_stack_dump())
6680 * We have __u32 type for the size, but so far
6681 * we can only use __u16 as maximum due to the
6682 * __u16 sample size limit.
6684 if (attr->sample_stack_user >= USHRT_MAX)
6686 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
6694 put_user(sizeof(*attr), &uattr->size);
6700 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6702 struct ring_buffer *rb = NULL, *old_rb = NULL;
6708 /* don't allow circular references */
6709 if (event == output_event)
6713 * Don't allow cross-cpu buffers
6715 if (output_event->cpu != event->cpu)
6719 * If its not a per-cpu rb, it must be the same task.
6721 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6725 mutex_lock(&event->mmap_mutex);
6726 /* Can't redirect output if we've got an active mmap() */
6727 if (atomic_read(&event->mmap_count))
6733 /* get the rb we want to redirect to */
6734 rb = ring_buffer_get(output_event);
6740 ring_buffer_detach(event, old_rb);
6743 ring_buffer_attach(event, rb);
6745 rcu_assign_pointer(event->rb, rb);
6748 ring_buffer_put(old_rb);
6750 * Since we detached before setting the new rb, so that we
6751 * could attach the new rb, we could have missed a wakeup.
6754 wake_up_all(&event->waitq);
6759 mutex_unlock(&event->mmap_mutex);
6766 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6768 * @attr_uptr: event_id type attributes for monitoring/sampling
6771 * @group_fd: group leader event fd
6773 SYSCALL_DEFINE5(perf_event_open,
6774 struct perf_event_attr __user *, attr_uptr,
6775 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6777 struct perf_event *group_leader = NULL, *output_event = NULL;
6778 struct perf_event *event, *sibling;
6779 struct perf_event_attr attr;
6780 struct perf_event_context *ctx;
6781 struct file *event_file = NULL;
6782 struct fd group = {NULL, 0};
6783 struct task_struct *task = NULL;
6789 /* for future expandability... */
6790 if (flags & ~PERF_FLAG_ALL)
6793 err = perf_copy_attr(attr_uptr, &attr);
6797 if (!attr.exclude_kernel) {
6798 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6803 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6808 * In cgroup mode, the pid argument is used to pass the fd
6809 * opened to the cgroup directory in cgroupfs. The cpu argument
6810 * designates the cpu on which to monitor threads from that
6813 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6816 event_fd = get_unused_fd();
6820 if (group_fd != -1) {
6821 err = perf_fget_light(group_fd, &group);
6824 group_leader = group.file->private_data;
6825 if (flags & PERF_FLAG_FD_OUTPUT)
6826 output_event = group_leader;
6827 if (flags & PERF_FLAG_FD_NO_GROUP)
6828 group_leader = NULL;
6831 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
6832 task = find_lively_task_by_vpid(pid);
6834 err = PTR_ERR(task);
6841 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
6843 if (IS_ERR(event)) {
6844 err = PTR_ERR(event);
6848 if (flags & PERF_FLAG_PID_CGROUP) {
6849 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6854 * - that has cgroup constraint on event->cpu
6855 * - that may need work on context switch
6857 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6858 static_key_slow_inc(&perf_sched_events.key);
6862 * Special case software events and allow them to be part of
6863 * any hardware group.
6868 (is_software_event(event) != is_software_event(group_leader))) {
6869 if (is_software_event(event)) {
6871 * If event and group_leader are not both a software
6872 * event, and event is, then group leader is not.
6874 * Allow the addition of software events to !software
6875 * groups, this is safe because software events never
6878 pmu = group_leader->pmu;
6879 } else if (is_software_event(group_leader) &&
6880 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6882 * In case the group is a pure software group, and we
6883 * try to add a hardware event, move the whole group to
6884 * the hardware context.
6891 * Get the target context (task or percpu):
6893 ctx = find_get_context(pmu, task, event->cpu);
6900 put_task_struct(task);
6905 * Look up the group leader (we will attach this event to it):
6911 * Do not allow a recursive hierarchy (this new sibling
6912 * becoming part of another group-sibling):
6914 if (group_leader->group_leader != group_leader)
6917 * Do not allow to attach to a group in a different
6918 * task or CPU context:
6921 if (group_leader->ctx->type != ctx->type)
6924 if (group_leader->ctx != ctx)
6929 * Only a group leader can be exclusive or pinned
6931 if (attr.exclusive || attr.pinned)
6936 err = perf_event_set_output(event, output_event);
6941 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6942 if (IS_ERR(event_file)) {
6943 err = PTR_ERR(event_file);
6948 struct perf_event_context *gctx = group_leader->ctx;
6950 mutex_lock(&gctx->mutex);
6951 perf_remove_from_context(group_leader);
6954 * Removing from the context ends up with disabled
6955 * event. What we want here is event in the initial
6956 * startup state, ready to be add into new context.
6958 perf_event__state_init(group_leader);
6959 list_for_each_entry(sibling, &group_leader->sibling_list,
6961 perf_remove_from_context(sibling);
6962 perf_event__state_init(sibling);
6965 mutex_unlock(&gctx->mutex);
6969 WARN_ON_ONCE(ctx->parent_ctx);
6970 mutex_lock(&ctx->mutex);
6974 perf_install_in_context(ctx, group_leader, event->cpu);
6976 list_for_each_entry(sibling, &group_leader->sibling_list,
6978 perf_install_in_context(ctx, sibling, event->cpu);
6983 perf_install_in_context(ctx, event, event->cpu);
6985 perf_unpin_context(ctx);
6986 mutex_unlock(&ctx->mutex);
6990 event->owner = current;
6992 mutex_lock(¤t->perf_event_mutex);
6993 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6994 mutex_unlock(¤t->perf_event_mutex);
6997 * Precalculate sample_data sizes
6999 perf_event__header_size(event);
7000 perf_event__id_header_size(event);
7003 * Drop the reference on the group_event after placing the
7004 * new event on the sibling_list. This ensures destruction
7005 * of the group leader will find the pointer to itself in
7006 * perf_group_detach().
7009 fd_install(event_fd, event_file);
7013 perf_unpin_context(ctx);
7020 put_task_struct(task);
7024 put_unused_fd(event_fd);
7029 * perf_event_create_kernel_counter
7031 * @attr: attributes of the counter to create
7032 * @cpu: cpu in which the counter is bound
7033 * @task: task to profile (NULL for percpu)
7036 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
7037 struct task_struct *task,
7038 perf_overflow_handler_t overflow_handler,
7041 struct perf_event_context *ctx;
7042 struct perf_event *event;
7046 * Get the target context (task or percpu):
7049 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
7050 overflow_handler, context);
7051 if (IS_ERR(event)) {
7052 err = PTR_ERR(event);
7056 ctx = find_get_context(event->pmu, task, cpu);
7062 WARN_ON_ONCE(ctx->parent_ctx);
7063 mutex_lock(&ctx->mutex);
7064 perf_install_in_context(ctx, event, cpu);
7066 perf_unpin_context(ctx);
7067 mutex_unlock(&ctx->mutex);
7074 return ERR_PTR(err);
7076 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
7078 void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
7080 struct perf_event_context *src_ctx;
7081 struct perf_event_context *dst_ctx;
7082 struct perf_event *event, *tmp;
7085 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
7086 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
7088 mutex_lock(&src_ctx->mutex);
7089 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
7091 perf_remove_from_context(event);
7093 list_add(&event->event_entry, &events);
7095 mutex_unlock(&src_ctx->mutex);
7099 mutex_lock(&dst_ctx->mutex);
7100 list_for_each_entry_safe(event, tmp, &events, event_entry) {
7101 list_del(&event->event_entry);
7102 if (event->state >= PERF_EVENT_STATE_OFF)
7103 event->state = PERF_EVENT_STATE_INACTIVE;
7104 perf_install_in_context(dst_ctx, event, dst_cpu);
7107 mutex_unlock(&dst_ctx->mutex);
7109 EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
7111 static void sync_child_event(struct perf_event *child_event,
7112 struct task_struct *child)
7114 struct perf_event *parent_event = child_event->parent;
7117 if (child_event->attr.inherit_stat)
7118 perf_event_read_event(child_event, child);
7120 child_val = perf_event_count(child_event);
7123 * Add back the child's count to the parent's count:
7125 atomic64_add(child_val, &parent_event->child_count);
7126 atomic64_add(child_event->total_time_enabled,
7127 &parent_event->child_total_time_enabled);
7128 atomic64_add(child_event->total_time_running,
7129 &parent_event->child_total_time_running);
7132 * Remove this event from the parent's list
7134 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7135 mutex_lock(&parent_event->child_mutex);
7136 list_del_init(&child_event->child_list);
7137 mutex_unlock(&parent_event->child_mutex);
7140 * Release the parent event, if this was the last
7143 put_event(parent_event);
7147 __perf_event_exit_task(struct perf_event *child_event,
7148 struct perf_event_context *child_ctx,
7149 struct task_struct *child)
7151 if (child_event->parent) {
7152 raw_spin_lock_irq(&child_ctx->lock);
7153 perf_group_detach(child_event);
7154 raw_spin_unlock_irq(&child_ctx->lock);
7157 perf_remove_from_context(child_event);
7160 * It can happen that the parent exits first, and has events
7161 * that are still around due to the child reference. These
7162 * events need to be zapped.
7164 if (child_event->parent) {
7165 sync_child_event(child_event, child);
7166 free_event(child_event);
7170 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
7172 struct perf_event *child_event, *tmp;
7173 struct perf_event_context *child_ctx;
7174 unsigned long flags;
7176 if (likely(!child->perf_event_ctxp[ctxn])) {
7177 perf_event_task(child, NULL, 0);
7181 local_irq_save(flags);
7183 * We can't reschedule here because interrupts are disabled,
7184 * and either child is current or it is a task that can't be
7185 * scheduled, so we are now safe from rescheduling changing
7188 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
7191 * Take the context lock here so that if find_get_context is
7192 * reading child->perf_event_ctxp, we wait until it has
7193 * incremented the context's refcount before we do put_ctx below.
7195 raw_spin_lock(&child_ctx->lock);
7196 task_ctx_sched_out(child_ctx);
7197 child->perf_event_ctxp[ctxn] = NULL;
7199 * If this context is a clone; unclone it so it can't get
7200 * swapped to another process while we're removing all
7201 * the events from it.
7203 unclone_ctx(child_ctx);
7204 update_context_time(child_ctx);
7205 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
7208 * Report the task dead after unscheduling the events so that we
7209 * won't get any samples after PERF_RECORD_EXIT. We can however still
7210 * get a few PERF_RECORD_READ events.
7212 perf_event_task(child, child_ctx, 0);
7215 * We can recurse on the same lock type through:
7217 * __perf_event_exit_task()
7218 * sync_child_event()
7220 * mutex_lock(&ctx->mutex)
7222 * But since its the parent context it won't be the same instance.
7224 mutex_lock(&child_ctx->mutex);
7227 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
7229 __perf_event_exit_task(child_event, child_ctx, child);
7231 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
7233 __perf_event_exit_task(child_event, child_ctx, child);
7236 * If the last event was a group event, it will have appended all
7237 * its siblings to the list, but we obtained 'tmp' before that which
7238 * will still point to the list head terminating the iteration.
7240 if (!list_empty(&child_ctx->pinned_groups) ||
7241 !list_empty(&child_ctx->flexible_groups))
7244 mutex_unlock(&child_ctx->mutex);
7250 * When a child task exits, feed back event values to parent events.
7252 void perf_event_exit_task(struct task_struct *child)
7254 struct perf_event *event, *tmp;
7257 mutex_lock(&child->perf_event_mutex);
7258 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
7260 list_del_init(&event->owner_entry);
7263 * Ensure the list deletion is visible before we clear
7264 * the owner, closes a race against perf_release() where
7265 * we need to serialize on the owner->perf_event_mutex.
7268 event->owner = NULL;
7270 mutex_unlock(&child->perf_event_mutex);
7272 for_each_task_context_nr(ctxn)
7273 perf_event_exit_task_context(child, ctxn);
7276 static void perf_free_event(struct perf_event *event,
7277 struct perf_event_context *ctx)
7279 struct perf_event *parent = event->parent;
7281 if (WARN_ON_ONCE(!parent))
7284 mutex_lock(&parent->child_mutex);
7285 list_del_init(&event->child_list);
7286 mutex_unlock(&parent->child_mutex);
7290 perf_group_detach(event);
7291 list_del_event(event, ctx);
7296 * free an unexposed, unused context as created by inheritance by
7297 * perf_event_init_task below, used by fork() in case of fail.
7299 void perf_event_free_task(struct task_struct *task)
7301 struct perf_event_context *ctx;
7302 struct perf_event *event, *tmp;
7305 for_each_task_context_nr(ctxn) {
7306 ctx = task->perf_event_ctxp[ctxn];
7310 mutex_lock(&ctx->mutex);
7312 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
7314 perf_free_event(event, ctx);
7316 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
7318 perf_free_event(event, ctx);
7320 if (!list_empty(&ctx->pinned_groups) ||
7321 !list_empty(&ctx->flexible_groups))
7324 mutex_unlock(&ctx->mutex);
7330 void perf_event_delayed_put(struct task_struct *task)
7334 for_each_task_context_nr(ctxn)
7335 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
7339 * inherit a event from parent task to child task:
7341 static struct perf_event *
7342 inherit_event(struct perf_event *parent_event,
7343 struct task_struct *parent,
7344 struct perf_event_context *parent_ctx,
7345 struct task_struct *child,
7346 struct perf_event *group_leader,
7347 struct perf_event_context *child_ctx)
7349 struct perf_event *child_event;
7350 unsigned long flags;
7353 * Instead of creating recursive hierarchies of events,
7354 * we link inherited events back to the original parent,
7355 * which has a filp for sure, which we use as the reference
7358 if (parent_event->parent)
7359 parent_event = parent_event->parent;
7361 child_event = perf_event_alloc(&parent_event->attr,
7364 group_leader, parent_event,
7366 if (IS_ERR(child_event))
7369 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
7370 free_event(child_event);
7377 * Make the child state follow the state of the parent event,
7378 * not its attr.disabled bit. We hold the parent's mutex,
7379 * so we won't race with perf_event_{en, dis}able_family.
7381 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
7382 child_event->state = PERF_EVENT_STATE_INACTIVE;
7384 child_event->state = PERF_EVENT_STATE_OFF;
7386 if (parent_event->attr.freq) {
7387 u64 sample_period = parent_event->hw.sample_period;
7388 struct hw_perf_event *hwc = &child_event->hw;
7390 hwc->sample_period = sample_period;
7391 hwc->last_period = sample_period;
7393 local64_set(&hwc->period_left, sample_period);
7396 child_event->ctx = child_ctx;
7397 child_event->overflow_handler = parent_event->overflow_handler;
7398 child_event->overflow_handler_context
7399 = parent_event->overflow_handler_context;
7402 * Precalculate sample_data sizes
7404 perf_event__header_size(child_event);
7405 perf_event__id_header_size(child_event);
7408 * Link it up in the child's context:
7410 raw_spin_lock_irqsave(&child_ctx->lock, flags);
7411 add_event_to_ctx(child_event, child_ctx);
7412 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
7415 * Link this into the parent event's child list
7417 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7418 mutex_lock(&parent_event->child_mutex);
7419 list_add_tail(&child_event->child_list, &parent_event->child_list);
7420 mutex_unlock(&parent_event->child_mutex);
7425 static int inherit_group(struct perf_event *parent_event,
7426 struct task_struct *parent,
7427 struct perf_event_context *parent_ctx,
7428 struct task_struct *child,
7429 struct perf_event_context *child_ctx)
7431 struct perf_event *leader;
7432 struct perf_event *sub;
7433 struct perf_event *child_ctr;
7435 leader = inherit_event(parent_event, parent, parent_ctx,
7436 child, NULL, child_ctx);
7438 return PTR_ERR(leader);
7439 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7440 child_ctr = inherit_event(sub, parent, parent_ctx,
7441 child, leader, child_ctx);
7442 if (IS_ERR(child_ctr))
7443 return PTR_ERR(child_ctr);
7449 inherit_task_group(struct perf_event *event, struct task_struct *parent,
7450 struct perf_event_context *parent_ctx,
7451 struct task_struct *child, int ctxn,
7455 struct perf_event_context *child_ctx;
7457 if (!event->attr.inherit) {
7462 child_ctx = child->perf_event_ctxp[ctxn];
7465 * This is executed from the parent task context, so
7466 * inherit events that have been marked for cloning.
7467 * First allocate and initialize a context for the
7471 child_ctx = alloc_perf_context(event->pmu, child);
7475 child->perf_event_ctxp[ctxn] = child_ctx;
7478 ret = inherit_group(event, parent, parent_ctx,
7488 * Initialize the perf_event context in task_struct
7490 int perf_event_init_context(struct task_struct *child, int ctxn)
7492 struct perf_event_context *child_ctx, *parent_ctx;
7493 struct perf_event_context *cloned_ctx;
7494 struct perf_event *event;
7495 struct task_struct *parent = current;
7496 int inherited_all = 1;
7497 unsigned long flags;
7500 if (likely(!parent->perf_event_ctxp[ctxn]))
7504 * If the parent's context is a clone, pin it so it won't get
7507 parent_ctx = perf_pin_task_context(parent, ctxn);
7510 * No need to check if parent_ctx != NULL here; since we saw
7511 * it non-NULL earlier, the only reason for it to become NULL
7512 * is if we exit, and since we're currently in the middle of
7513 * a fork we can't be exiting at the same time.
7517 * Lock the parent list. No need to lock the child - not PID
7518 * hashed yet and not running, so nobody can access it.
7520 mutex_lock(&parent_ctx->mutex);
7523 * We dont have to disable NMIs - we are only looking at
7524 * the list, not manipulating it:
7526 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
7527 ret = inherit_task_group(event, parent, parent_ctx,
7528 child, ctxn, &inherited_all);
7534 * We can't hold ctx->lock when iterating the ->flexible_group list due
7535 * to allocations, but we need to prevent rotation because
7536 * rotate_ctx() will change the list from interrupt context.
7538 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7539 parent_ctx->rotate_disable = 1;
7540 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7542 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
7543 ret = inherit_task_group(event, parent, parent_ctx,
7544 child, ctxn, &inherited_all);
7549 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7550 parent_ctx->rotate_disable = 0;
7552 child_ctx = child->perf_event_ctxp[ctxn];
7554 if (child_ctx && inherited_all) {
7556 * Mark the child context as a clone of the parent
7557 * context, or of whatever the parent is a clone of.
7559 * Note that if the parent is a clone, the holding of
7560 * parent_ctx->lock avoids it from being uncloned.
7562 cloned_ctx = parent_ctx->parent_ctx;
7564 child_ctx->parent_ctx = cloned_ctx;
7565 child_ctx->parent_gen = parent_ctx->parent_gen;
7567 child_ctx->parent_ctx = parent_ctx;
7568 child_ctx->parent_gen = parent_ctx->generation;
7570 get_ctx(child_ctx->parent_ctx);
7573 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7574 mutex_unlock(&parent_ctx->mutex);
7576 perf_unpin_context(parent_ctx);
7577 put_ctx(parent_ctx);
7583 * Initialize the perf_event context in task_struct
7585 int perf_event_init_task(struct task_struct *child)
7589 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7590 mutex_init(&child->perf_event_mutex);
7591 INIT_LIST_HEAD(&child->perf_event_list);
7593 for_each_task_context_nr(ctxn) {
7594 ret = perf_event_init_context(child, ctxn);
7602 static void __init perf_event_init_all_cpus(void)
7604 struct swevent_htable *swhash;
7607 for_each_possible_cpu(cpu) {
7608 swhash = &per_cpu(swevent_htable, cpu);
7609 mutex_init(&swhash->hlist_mutex);
7610 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
7614 static void __cpuinit perf_event_init_cpu(int cpu)
7616 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7618 mutex_lock(&swhash->hlist_mutex);
7619 if (swhash->hlist_refcount > 0) {
7620 struct swevent_hlist *hlist;
7622 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7624 rcu_assign_pointer(swhash->swevent_hlist, hlist);
7626 mutex_unlock(&swhash->hlist_mutex);
7629 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7630 static void perf_pmu_rotate_stop(struct pmu *pmu)
7632 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7634 WARN_ON(!irqs_disabled());
7636 list_del_init(&cpuctx->rotation_list);
7639 static void __perf_event_exit_context(void *__info)
7641 struct perf_event_context *ctx = __info;
7642 struct perf_event *event, *tmp;
7644 perf_pmu_rotate_stop(ctx->pmu);
7646 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
7647 __perf_remove_from_context(event);
7648 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
7649 __perf_remove_from_context(event);
7652 static void perf_event_exit_cpu_context(int cpu)
7654 struct perf_event_context *ctx;
7658 idx = srcu_read_lock(&pmus_srcu);
7659 list_for_each_entry_rcu(pmu, &pmus, entry) {
7660 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
7662 mutex_lock(&ctx->mutex);
7663 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7664 mutex_unlock(&ctx->mutex);
7666 srcu_read_unlock(&pmus_srcu, idx);
7669 static void perf_event_exit_cpu(int cpu)
7671 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7673 mutex_lock(&swhash->hlist_mutex);
7674 swevent_hlist_release(swhash);
7675 mutex_unlock(&swhash->hlist_mutex);
7677 perf_event_exit_cpu_context(cpu);
7680 static inline void perf_event_exit_cpu(int cpu) { }
7684 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7688 for_each_online_cpu(cpu)
7689 perf_event_exit_cpu(cpu);
7695 * Run the perf reboot notifier at the very last possible moment so that
7696 * the generic watchdog code runs as long as possible.
7698 static struct notifier_block perf_reboot_notifier = {
7699 .notifier_call = perf_reboot,
7700 .priority = INT_MIN,
7703 static int __cpuinit
7704 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7706 unsigned int cpu = (long)hcpu;
7708 switch (action & ~CPU_TASKS_FROZEN) {
7710 case CPU_UP_PREPARE:
7711 case CPU_DOWN_FAILED:
7712 perf_event_init_cpu(cpu);
7715 case CPU_UP_CANCELED:
7716 case CPU_DOWN_PREPARE:
7717 perf_event_exit_cpu(cpu);
7726 void __init perf_event_init(void)
7732 perf_event_init_all_cpus();
7733 init_srcu_struct(&pmus_srcu);
7734 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7735 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7736 perf_pmu_register(&perf_task_clock, NULL, -1);
7738 perf_cpu_notifier(perf_cpu_notify);
7739 register_reboot_notifier(&perf_reboot_notifier);
7741 ret = init_hw_breakpoint();
7742 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7744 /* do not patch jump label more than once per second */
7745 jump_label_rate_limit(&perf_sched_events, HZ);
7748 * Build time assertion that we keep the data_head at the intended
7749 * location. IOW, validation we got the __reserved[] size right.
7751 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
7755 static int __init perf_event_sysfs_init(void)
7760 mutex_lock(&pmus_lock);
7762 ret = bus_register(&pmu_bus);
7766 list_for_each_entry(pmu, &pmus, entry) {
7767 if (!pmu->name || pmu->type < 0)
7770 ret = pmu_dev_alloc(pmu);
7771 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7773 pmu_bus_running = 1;
7777 mutex_unlock(&pmus_lock);
7781 device_initcall(perf_event_sysfs_init);
7783 #ifdef CONFIG_CGROUP_PERF
7784 static struct cgroup_subsys_state *
7785 perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
7787 struct perf_cgroup *jc;
7789 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
7791 return ERR_PTR(-ENOMEM);
7793 jc->info = alloc_percpu(struct perf_cgroup_info);
7796 return ERR_PTR(-ENOMEM);
7802 static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
7804 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
7806 free_percpu(jc->info);
7810 static int __perf_cgroup_move(void *info)
7812 struct task_struct *task = info;
7813 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7817 static void perf_cgroup_attach(struct cgroup_subsys_state *css,
7818 struct cgroup_taskset *tset)
7820 struct task_struct *task;
7822 cgroup_taskset_for_each(task, css, tset)
7823 task_function_call(task, __perf_cgroup_move, task);
7826 static void perf_cgroup_exit(struct cgroup_subsys_state *css,
7827 struct cgroup_subsys_state *old_css,
7828 struct task_struct *task)
7831 * cgroup_exit() is called in the copy_process() failure path.
7832 * Ignore this case since the task hasn't ran yet, this avoids
7833 * trying to poke a half freed task state from generic code.
7835 if (!(task->flags & PF_EXITING))
7838 task_function_call(task, __perf_cgroup_move, task);
7841 struct cgroup_subsys perf_subsys = {
7842 .name = "perf_event",
7843 .subsys_id = perf_subsys_id,
7844 .css_alloc = perf_cgroup_css_alloc,
7845 .css_free = perf_cgroup_css_free,
7846 .exit = perf_cgroup_exit,
7847 .attach = perf_cgroup_attach,
7849 #endif /* CONFIG_CGROUP_PERF */