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/sysfs.h>
22 #include <linux/dcache.h>
23 #include <linux/percpu.h>
24 #include <linux/ptrace.h>
25 #include <linux/reboot.h>
26 #include <linux/vmstat.h>
27 #include <linux/device.h>
28 #include <linux/export.h>
29 #include <linux/vmalloc.h>
30 #include <linux/hardirq.h>
31 #include <linux/rculist.h>
32 #include <linux/uaccess.h>
33 #include <linux/syscalls.h>
34 #include <linux/anon_inodes.h>
35 #include <linux/kernel_stat.h>
36 #include <linux/perf_event.h>
37 #include <linux/ftrace_event.h>
38 #include <linux/hw_breakpoint.h>
39 #include <linux/compat.h>
43 #include <asm/irq_regs.h>
45 struct remote_function_call {
46 struct task_struct *p;
47 int (*func)(void *info);
52 static void remote_function(void *data)
54 struct remote_function_call *tfc = data;
55 struct task_struct *p = tfc->p;
59 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
63 tfc->ret = tfc->func(tfc->info);
67 * task_function_call - call a function on the cpu on which a task runs
68 * @p: the task to evaluate
69 * @func: the function to be called
70 * @info: the function call argument
72 * Calls the function @func when the task is currently running. This might
73 * be on the current CPU, which just calls the function directly
75 * returns: @func return value, or
76 * -ESRCH - when the process isn't running
77 * -EAGAIN - when the process moved away
80 task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
82 struct remote_function_call data = {
86 .ret = -ESRCH, /* No such (running) process */
90 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
96 * cpu_function_call - call a function on the cpu
97 * @func: the function to be called
98 * @info: the function call argument
100 * Calls the function @func on the remote cpu.
102 * returns: @func return value or -ENXIO when the cpu is offline
104 static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
106 struct remote_function_call data = {
110 .ret = -ENXIO, /* No such CPU */
113 smp_call_function_single(cpu, remote_function, &data, 1);
118 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
119 PERF_FLAG_FD_OUTPUT |\
120 PERF_FLAG_PID_CGROUP)
123 EVENT_FLEXIBLE = 0x1,
125 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
129 * perf_sched_events : >0 events exist
130 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
132 struct jump_label_key perf_sched_events __read_mostly;
133 static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
135 static atomic_t nr_mmap_events __read_mostly;
136 static atomic_t nr_comm_events __read_mostly;
137 static atomic_t nr_task_events __read_mostly;
139 static LIST_HEAD(pmus);
140 static DEFINE_MUTEX(pmus_lock);
141 static struct srcu_struct pmus_srcu;
144 * perf event paranoia level:
145 * -1 - not paranoid at all
146 * 0 - disallow raw tracepoint access for unpriv
147 * 1 - disallow cpu events for unpriv
148 * 2 - disallow kernel profiling for unpriv
150 int sysctl_perf_event_paranoid __read_mostly = 1;
152 /* Minimum for 512 kiB + 1 user control page */
153 int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
156 * max perf event sample rate
158 #define DEFAULT_MAX_SAMPLE_RATE 100000
159 int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
160 static int max_samples_per_tick __read_mostly =
161 DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
163 int perf_proc_update_handler(struct ctl_table *table, int write,
164 void __user *buffer, size_t *lenp,
167 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
172 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
177 static atomic64_t perf_event_id;
179 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
180 enum event_type_t event_type);
182 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
183 enum event_type_t event_type,
184 struct task_struct *task);
186 static void update_context_time(struct perf_event_context *ctx);
187 static u64 perf_event_time(struct perf_event *event);
189 void __weak perf_event_print_debug(void) { }
191 extern __weak const char *perf_pmu_name(void)
196 static inline u64 perf_clock(void)
198 return local_clock();
201 static inline struct perf_cpu_context *
202 __get_cpu_context(struct perf_event_context *ctx)
204 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
207 static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
208 struct perf_event_context *ctx)
210 raw_spin_lock(&cpuctx->ctx.lock);
212 raw_spin_lock(&ctx->lock);
215 static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
216 struct perf_event_context *ctx)
219 raw_spin_unlock(&ctx->lock);
220 raw_spin_unlock(&cpuctx->ctx.lock);
223 #ifdef CONFIG_CGROUP_PERF
226 * Must ensure cgroup is pinned (css_get) before calling
227 * this function. In other words, we cannot call this function
228 * if there is no cgroup event for the current CPU context.
230 static inline struct perf_cgroup *
231 perf_cgroup_from_task(struct task_struct *task)
233 return container_of(task_subsys_state(task, perf_subsys_id),
234 struct perf_cgroup, css);
238 perf_cgroup_match(struct perf_event *event)
240 struct perf_event_context *ctx = event->ctx;
241 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
243 return !event->cgrp || event->cgrp == cpuctx->cgrp;
246 static inline bool perf_tryget_cgroup(struct perf_event *event)
248 return css_tryget(&event->cgrp->css);
251 static inline void perf_put_cgroup(struct perf_event *event)
253 css_put(&event->cgrp->css);
256 static inline void perf_detach_cgroup(struct perf_event *event)
258 perf_put_cgroup(event);
262 static inline int is_cgroup_event(struct perf_event *event)
264 return event->cgrp != NULL;
267 static inline u64 perf_cgroup_event_time(struct perf_event *event)
269 struct perf_cgroup_info *t;
271 t = per_cpu_ptr(event->cgrp->info, event->cpu);
275 static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
277 struct perf_cgroup_info *info;
282 info = this_cpu_ptr(cgrp->info);
284 info->time += now - info->timestamp;
285 info->timestamp = now;
288 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
290 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
292 __update_cgrp_time(cgrp_out);
295 static inline void update_cgrp_time_from_event(struct perf_event *event)
297 struct perf_cgroup *cgrp;
300 * ensure we access cgroup data only when needed and
301 * when we know the cgroup is pinned (css_get)
303 if (!is_cgroup_event(event))
306 cgrp = perf_cgroup_from_task(current);
308 * Do not update time when cgroup is not active
310 if (cgrp == event->cgrp)
311 __update_cgrp_time(event->cgrp);
315 perf_cgroup_set_timestamp(struct task_struct *task,
316 struct perf_event_context *ctx)
318 struct perf_cgroup *cgrp;
319 struct perf_cgroup_info *info;
322 * ctx->lock held by caller
323 * ensure we do not access cgroup data
324 * unless we have the cgroup pinned (css_get)
326 if (!task || !ctx->nr_cgroups)
329 cgrp = perf_cgroup_from_task(task);
330 info = this_cpu_ptr(cgrp->info);
331 info->timestamp = ctx->timestamp;
334 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
335 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
338 * reschedule events based on the cgroup constraint of task.
340 * mode SWOUT : schedule out everything
341 * mode SWIN : schedule in based on cgroup for next
343 void perf_cgroup_switch(struct task_struct *task, int mode)
345 struct perf_cpu_context *cpuctx;
350 * disable interrupts to avoid geting nr_cgroup
351 * changes via __perf_event_disable(). Also
354 local_irq_save(flags);
357 * we reschedule only in the presence of cgroup
358 * constrained events.
362 list_for_each_entry_rcu(pmu, &pmus, entry) {
363 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
364 if (cpuctx->unique_pmu != pmu)
365 continue; /* ensure we process each cpuctx once */
368 * perf_cgroup_events says at least one
369 * context on this CPU has cgroup events.
371 * ctx->nr_cgroups reports the number of cgroup
372 * events for a context.
374 if (cpuctx->ctx.nr_cgroups > 0) {
375 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
376 perf_pmu_disable(cpuctx->ctx.pmu);
378 if (mode & PERF_CGROUP_SWOUT) {
379 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
381 * must not be done before ctxswout due
382 * to event_filter_match() in event_sched_out()
387 if (mode & PERF_CGROUP_SWIN) {
388 WARN_ON_ONCE(cpuctx->cgrp);
390 * set cgrp before ctxsw in to allow
391 * event_filter_match() to not have to pass
394 cpuctx->cgrp = perf_cgroup_from_task(task);
395 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
397 perf_pmu_enable(cpuctx->ctx.pmu);
398 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
404 local_irq_restore(flags);
407 static inline void perf_cgroup_sched_out(struct task_struct *task,
408 struct task_struct *next)
410 struct perf_cgroup *cgrp1;
411 struct perf_cgroup *cgrp2 = NULL;
414 * we come here when we know perf_cgroup_events > 0
416 cgrp1 = perf_cgroup_from_task(task);
419 * next is NULL when called from perf_event_enable_on_exec()
420 * that will systematically cause a cgroup_switch()
423 cgrp2 = perf_cgroup_from_task(next);
426 * only schedule out current cgroup events if we know
427 * that we are switching to a different cgroup. Otherwise,
428 * do no touch the cgroup events.
431 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
434 static inline void perf_cgroup_sched_in(struct task_struct *prev,
435 struct task_struct *task)
437 struct perf_cgroup *cgrp1;
438 struct perf_cgroup *cgrp2 = NULL;
441 * we come here when we know perf_cgroup_events > 0
443 cgrp1 = perf_cgroup_from_task(task);
445 /* prev can never be NULL */
446 cgrp2 = perf_cgroup_from_task(prev);
449 * only need to schedule in cgroup events if we are changing
450 * cgroup during ctxsw. Cgroup events were not scheduled
451 * out of ctxsw out if that was not the case.
454 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
457 static inline int perf_cgroup_connect(int fd, struct perf_event *event,
458 struct perf_event_attr *attr,
459 struct perf_event *group_leader)
461 struct perf_cgroup *cgrp;
462 struct cgroup_subsys_state *css;
464 int ret = 0, fput_needed;
466 file = fget_light(fd, &fput_needed);
470 css = cgroup_css_from_dir(file, perf_subsys_id);
476 cgrp = container_of(css, struct perf_cgroup, css);
479 /* must be done before we fput() the file */
480 if (!perf_tryget_cgroup(event)) {
487 * all events in a group must monitor
488 * the same cgroup because a task belongs
489 * to only one perf cgroup at a time
491 if (group_leader && group_leader->cgrp != cgrp) {
492 perf_detach_cgroup(event);
496 fput_light(file, fput_needed);
501 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
503 struct perf_cgroup_info *t;
504 t = per_cpu_ptr(event->cgrp->info, event->cpu);
505 event->shadow_ctx_time = now - t->timestamp;
509 perf_cgroup_defer_enabled(struct perf_event *event)
512 * when the current task's perf cgroup does not match
513 * the event's, we need to remember to call the
514 * perf_mark_enable() function the first time a task with
515 * a matching perf cgroup is scheduled in.
517 if (is_cgroup_event(event) && !perf_cgroup_match(event))
518 event->cgrp_defer_enabled = 1;
522 perf_cgroup_mark_enabled(struct perf_event *event,
523 struct perf_event_context *ctx)
525 struct perf_event *sub;
526 u64 tstamp = perf_event_time(event);
528 if (!event->cgrp_defer_enabled)
531 event->cgrp_defer_enabled = 0;
533 event->tstamp_enabled = tstamp - event->total_time_enabled;
534 list_for_each_entry(sub, &event->sibling_list, group_entry) {
535 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
536 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
537 sub->cgrp_defer_enabled = 0;
541 #else /* !CONFIG_CGROUP_PERF */
544 perf_cgroup_match(struct perf_event *event)
549 static inline void perf_detach_cgroup(struct perf_event *event)
552 static inline int is_cgroup_event(struct perf_event *event)
557 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
562 static inline void update_cgrp_time_from_event(struct perf_event *event)
566 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
570 static inline void perf_cgroup_sched_out(struct task_struct *task,
571 struct task_struct *next)
575 static inline void perf_cgroup_sched_in(struct task_struct *prev,
576 struct task_struct *task)
580 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
581 struct perf_event_attr *attr,
582 struct perf_event *group_leader)
588 perf_cgroup_set_timestamp(struct task_struct *task,
589 struct perf_event_context *ctx)
594 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
599 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
603 static inline u64 perf_cgroup_event_time(struct perf_event *event)
609 perf_cgroup_defer_enabled(struct perf_event *event)
614 perf_cgroup_mark_enabled(struct perf_event *event,
615 struct perf_event_context *ctx)
620 void perf_pmu_disable(struct pmu *pmu)
622 int *count = this_cpu_ptr(pmu->pmu_disable_count);
624 pmu->pmu_disable(pmu);
627 void perf_pmu_enable(struct pmu *pmu)
629 int *count = this_cpu_ptr(pmu->pmu_disable_count);
631 pmu->pmu_enable(pmu);
634 static DEFINE_PER_CPU(struct list_head, rotation_list);
637 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
638 * because they're strictly cpu affine and rotate_start is called with IRQs
639 * disabled, while rotate_context is called from IRQ context.
641 static void perf_pmu_rotate_start(struct pmu *pmu)
643 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
644 struct list_head *head = &__get_cpu_var(rotation_list);
646 WARN_ON(!irqs_disabled());
648 if (list_empty(&cpuctx->rotation_list))
649 list_add(&cpuctx->rotation_list, head);
652 static void get_ctx(struct perf_event_context *ctx)
654 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
657 static void put_ctx(struct perf_event_context *ctx)
659 if (atomic_dec_and_test(&ctx->refcount)) {
661 put_ctx(ctx->parent_ctx);
663 put_task_struct(ctx->task);
664 kfree_rcu(ctx, rcu_head);
668 static void unclone_ctx(struct perf_event_context *ctx)
670 if (ctx->parent_ctx) {
671 put_ctx(ctx->parent_ctx);
672 ctx->parent_ctx = NULL;
676 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
679 * only top level events have the pid namespace they were created in
682 event = event->parent;
684 return task_tgid_nr_ns(p, event->ns);
687 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
690 * only top level events have the pid namespace they were created in
693 event = event->parent;
695 return task_pid_nr_ns(p, event->ns);
699 * If we inherit events we want to return the parent event id
702 static u64 primary_event_id(struct perf_event *event)
707 id = event->parent->id;
713 * Get the perf_event_context for a task and lock it.
714 * This has to cope with with the fact that until it is locked,
715 * the context could get moved to another task.
717 static struct perf_event_context *
718 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
720 struct perf_event_context *ctx;
724 * One of the few rules of preemptible RCU is that one cannot do
725 * rcu_read_unlock() while holding a scheduler (or nested) lock when
726 * part of the read side critical section was preemptible -- see
727 * rcu_read_unlock_special().
729 * Since ctx->lock nests under rq->lock we must ensure the entire read
730 * side critical section is non-preemptible.
734 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
737 * If this context is a clone of another, it might
738 * get swapped for another underneath us by
739 * perf_event_task_sched_out, though the
740 * rcu_read_lock() protects us from any context
741 * getting freed. Lock the context and check if it
742 * got swapped before we could get the lock, and retry
743 * if so. If we locked the right context, then it
744 * can't get swapped on us any more.
746 raw_spin_lock_irqsave(&ctx->lock, *flags);
747 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
748 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
754 if (!atomic_inc_not_zero(&ctx->refcount)) {
755 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
765 * Get the context for a task and increment its pin_count so it
766 * can't get swapped to another task. This also increments its
767 * reference count so that the context can't get freed.
769 static struct perf_event_context *
770 perf_pin_task_context(struct task_struct *task, int ctxn)
772 struct perf_event_context *ctx;
775 ctx = perf_lock_task_context(task, ctxn, &flags);
778 raw_spin_unlock_irqrestore(&ctx->lock, flags);
783 static void perf_unpin_context(struct perf_event_context *ctx)
787 raw_spin_lock_irqsave(&ctx->lock, flags);
789 raw_spin_unlock_irqrestore(&ctx->lock, flags);
793 * Update the record of the current time in a context.
795 static void update_context_time(struct perf_event_context *ctx)
797 u64 now = perf_clock();
799 ctx->time += now - ctx->timestamp;
800 ctx->timestamp = now;
803 static u64 perf_event_time(struct perf_event *event)
805 struct perf_event_context *ctx = event->ctx;
807 if (is_cgroup_event(event))
808 return perf_cgroup_event_time(event);
810 return ctx ? ctx->time : 0;
814 * Update the total_time_enabled and total_time_running fields for a event.
815 * The caller of this function needs to hold the ctx->lock.
817 static void update_event_times(struct perf_event *event)
819 struct perf_event_context *ctx = event->ctx;
822 if (event->state < PERF_EVENT_STATE_INACTIVE ||
823 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
826 * in cgroup mode, time_enabled represents
827 * the time the event was enabled AND active
828 * tasks were in the monitored cgroup. This is
829 * independent of the activity of the context as
830 * there may be a mix of cgroup and non-cgroup events.
832 * That is why we treat cgroup events differently
835 if (is_cgroup_event(event))
836 run_end = perf_event_time(event);
837 else if (ctx->is_active)
840 run_end = event->tstamp_stopped;
842 event->total_time_enabled = run_end - event->tstamp_enabled;
844 if (event->state == PERF_EVENT_STATE_INACTIVE)
845 run_end = event->tstamp_stopped;
847 run_end = perf_event_time(event);
849 event->total_time_running = run_end - event->tstamp_running;
854 * Update total_time_enabled and total_time_running for all events in a group.
856 static void update_group_times(struct perf_event *leader)
858 struct perf_event *event;
860 update_event_times(leader);
861 list_for_each_entry(event, &leader->sibling_list, group_entry)
862 update_event_times(event);
865 static struct list_head *
866 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
868 if (event->attr.pinned)
869 return &ctx->pinned_groups;
871 return &ctx->flexible_groups;
875 * Add a event from the lists for its context.
876 * Must be called with ctx->mutex and ctx->lock held.
879 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
881 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
882 event->attach_state |= PERF_ATTACH_CONTEXT;
885 * If we're a stand alone event or group leader, we go to the context
886 * list, group events are kept attached to the group so that
887 * perf_group_detach can, at all times, locate all siblings.
889 if (event->group_leader == event) {
890 struct list_head *list;
892 if (is_software_event(event))
893 event->group_flags |= PERF_GROUP_SOFTWARE;
895 list = ctx_group_list(event, ctx);
896 list_add_tail(&event->group_entry, list);
899 if (is_cgroup_event(event))
902 list_add_rcu(&event->event_entry, &ctx->event_list);
904 perf_pmu_rotate_start(ctx->pmu);
906 if (event->attr.inherit_stat)
911 * Initialize event state based on the perf_event_attr::disabled.
913 static inline void perf_event__state_init(struct perf_event *event)
915 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
916 PERF_EVENT_STATE_INACTIVE;
920 * Called at perf_event creation and when events are attached/detached from a
923 static void perf_event__read_size(struct perf_event *event)
925 int entry = sizeof(u64); /* value */
929 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
932 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
935 if (event->attr.read_format & PERF_FORMAT_ID)
936 entry += sizeof(u64);
938 if (event->attr.read_format & PERF_FORMAT_GROUP) {
939 nr += event->group_leader->nr_siblings;
944 event->read_size = size;
947 static void perf_event__header_size(struct perf_event *event)
949 struct perf_sample_data *data;
950 u64 sample_type = event->attr.sample_type;
953 perf_event__read_size(event);
955 if (sample_type & PERF_SAMPLE_IP)
956 size += sizeof(data->ip);
958 if (sample_type & PERF_SAMPLE_ADDR)
959 size += sizeof(data->addr);
961 if (sample_type & PERF_SAMPLE_PERIOD)
962 size += sizeof(data->period);
964 if (sample_type & PERF_SAMPLE_READ)
965 size += event->read_size;
967 event->header_size = size;
970 static void perf_event__id_header_size(struct perf_event *event)
972 struct perf_sample_data *data;
973 u64 sample_type = event->attr.sample_type;
976 if (sample_type & PERF_SAMPLE_TID)
977 size += sizeof(data->tid_entry);
979 if (sample_type & PERF_SAMPLE_TIME)
980 size += sizeof(data->time);
982 if (sample_type & PERF_SAMPLE_ID)
983 size += sizeof(data->id);
985 if (sample_type & PERF_SAMPLE_STREAM_ID)
986 size += sizeof(data->stream_id);
988 if (sample_type & PERF_SAMPLE_CPU)
989 size += sizeof(data->cpu_entry);
991 event->id_header_size = size;
994 static void perf_group_attach(struct perf_event *event)
996 struct perf_event *group_leader = event->group_leader, *pos;
999 * We can have double attach due to group movement in perf_event_open.
1001 if (event->attach_state & PERF_ATTACH_GROUP)
1004 event->attach_state |= PERF_ATTACH_GROUP;
1006 if (group_leader == event)
1009 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1010 !is_software_event(event))
1011 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1013 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1014 group_leader->nr_siblings++;
1016 perf_event__header_size(group_leader);
1018 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1019 perf_event__header_size(pos);
1023 * Remove a event from the lists for its context.
1024 * Must be called with ctx->mutex and ctx->lock held.
1027 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1029 struct perf_cpu_context *cpuctx;
1031 * We can have double detach due to exit/hot-unplug + close.
1033 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
1036 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1038 if (is_cgroup_event(event)) {
1040 cpuctx = __get_cpu_context(ctx);
1042 * if there are no more cgroup events
1043 * then cler cgrp to avoid stale pointer
1044 * in update_cgrp_time_from_cpuctx()
1046 if (!ctx->nr_cgroups)
1047 cpuctx->cgrp = NULL;
1051 if (event->attr.inherit_stat)
1054 list_del_rcu(&event->event_entry);
1056 if (event->group_leader == event)
1057 list_del_init(&event->group_entry);
1059 update_group_times(event);
1062 * If event was in error state, then keep it
1063 * that way, otherwise bogus counts will be
1064 * returned on read(). The only way to get out
1065 * of error state is by explicit re-enabling
1068 if (event->state > PERF_EVENT_STATE_OFF)
1069 event->state = PERF_EVENT_STATE_OFF;
1072 static void perf_group_detach(struct perf_event *event)
1074 struct perf_event *sibling, *tmp;
1075 struct list_head *list = NULL;
1078 * We can have double detach due to exit/hot-unplug + close.
1080 if (!(event->attach_state & PERF_ATTACH_GROUP))
1083 event->attach_state &= ~PERF_ATTACH_GROUP;
1086 * If this is a sibling, remove it from its group.
1088 if (event->group_leader != event) {
1089 list_del_init(&event->group_entry);
1090 event->group_leader->nr_siblings--;
1094 if (!list_empty(&event->group_entry))
1095 list = &event->group_entry;
1098 * If this was a group event with sibling events then
1099 * upgrade the siblings to singleton events by adding them
1100 * to whatever list we are on.
1102 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
1104 list_move_tail(&sibling->group_entry, list);
1105 sibling->group_leader = sibling;
1107 /* Inherit group flags from the previous leader */
1108 sibling->group_flags = event->group_flags;
1112 perf_event__header_size(event->group_leader);
1114 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1115 perf_event__header_size(tmp);
1119 event_filter_match(struct perf_event *event)
1121 return (event->cpu == -1 || event->cpu == smp_processor_id())
1122 && perf_cgroup_match(event);
1126 event_sched_out(struct perf_event *event,
1127 struct perf_cpu_context *cpuctx,
1128 struct perf_event_context *ctx)
1130 u64 tstamp = perf_event_time(event);
1133 * An event which could not be activated because of
1134 * filter mismatch still needs to have its timings
1135 * maintained, otherwise bogus information is return
1136 * via read() for time_enabled, time_running:
1138 if (event->state == PERF_EVENT_STATE_INACTIVE
1139 && !event_filter_match(event)) {
1140 delta = tstamp - event->tstamp_stopped;
1141 event->tstamp_running += delta;
1142 event->tstamp_stopped = tstamp;
1145 if (event->state != PERF_EVENT_STATE_ACTIVE)
1148 event->state = PERF_EVENT_STATE_INACTIVE;
1149 if (event->pending_disable) {
1150 event->pending_disable = 0;
1151 event->state = PERF_EVENT_STATE_OFF;
1153 event->tstamp_stopped = tstamp;
1154 event->pmu->del(event, 0);
1157 if (!is_software_event(event))
1158 cpuctx->active_oncpu--;
1160 if (event->attr.exclusive || !cpuctx->active_oncpu)
1161 cpuctx->exclusive = 0;
1165 group_sched_out(struct perf_event *group_event,
1166 struct perf_cpu_context *cpuctx,
1167 struct perf_event_context *ctx)
1169 struct perf_event *event;
1170 int state = group_event->state;
1172 event_sched_out(group_event, cpuctx, ctx);
1175 * Schedule out siblings (if any):
1177 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1178 event_sched_out(event, cpuctx, ctx);
1180 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
1181 cpuctx->exclusive = 0;
1184 struct remove_event {
1185 struct perf_event *event;
1190 * Cross CPU call to remove a performance event
1192 * We disable the event on the hardware level first. After that we
1193 * remove it from the context list.
1195 static int __perf_remove_from_context(void *info)
1197 struct remove_event *re = info;
1198 struct perf_event *event = re->event;
1199 struct perf_event_context *ctx = event->ctx;
1200 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1202 raw_spin_lock(&ctx->lock);
1203 event_sched_out(event, cpuctx, ctx);
1204 if (re->detach_group)
1205 perf_group_detach(event);
1206 list_del_event(event, ctx);
1207 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1209 cpuctx->task_ctx = NULL;
1211 raw_spin_unlock(&ctx->lock);
1218 * Remove the event from a task's (or a CPU's) list of events.
1220 * CPU events are removed with a smp call. For task events we only
1221 * call when the task is on a CPU.
1223 * If event->ctx is a cloned context, callers must make sure that
1224 * every task struct that event->ctx->task could possibly point to
1225 * remains valid. This is OK when called from perf_release since
1226 * that only calls us on the top-level context, which can't be a clone.
1227 * When called from perf_event_exit_task, it's OK because the
1228 * context has been detached from its task.
1230 static void perf_remove_from_context(struct perf_event *event, bool detach_group)
1232 struct perf_event_context *ctx = event->ctx;
1233 struct task_struct *task = ctx->task;
1234 struct remove_event re = {
1236 .detach_group = detach_group,
1239 lockdep_assert_held(&ctx->mutex);
1243 * Per cpu events are removed via an smp call and
1244 * the removal is always successful.
1246 cpu_function_call(event->cpu, __perf_remove_from_context, &re);
1251 if (!task_function_call(task, __perf_remove_from_context, &re))
1254 raw_spin_lock_irq(&ctx->lock);
1256 * If we failed to find a running task, but find the context active now
1257 * that we've acquired the ctx->lock, retry.
1259 if (ctx->is_active) {
1260 raw_spin_unlock_irq(&ctx->lock);
1265 * Since the task isn't running, its safe to remove the event, us
1266 * holding the ctx->lock ensures the task won't get scheduled in.
1269 perf_group_detach(event);
1270 list_del_event(event, ctx);
1271 raw_spin_unlock_irq(&ctx->lock);
1275 * Cross CPU call to disable a performance event
1277 static int __perf_event_disable(void *info)
1279 struct perf_event *event = info;
1280 struct perf_event_context *ctx = event->ctx;
1281 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1284 * If this is a per-task event, need to check whether this
1285 * event's task is the current task on this cpu.
1287 * Can trigger due to concurrent perf_event_context_sched_out()
1288 * flipping contexts around.
1290 if (ctx->task && cpuctx->task_ctx != ctx)
1293 raw_spin_lock(&ctx->lock);
1296 * If the event is on, turn it off.
1297 * If it is in error state, leave it in error state.
1299 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1300 update_context_time(ctx);
1301 update_cgrp_time_from_event(event);
1302 update_group_times(event);
1303 if (event == event->group_leader)
1304 group_sched_out(event, cpuctx, ctx);
1306 event_sched_out(event, cpuctx, ctx);
1307 event->state = PERF_EVENT_STATE_OFF;
1310 raw_spin_unlock(&ctx->lock);
1318 * If event->ctx is a cloned context, callers must make sure that
1319 * every task struct that event->ctx->task could possibly point to
1320 * remains valid. This condition is satisifed when called through
1321 * perf_event_for_each_child or perf_event_for_each because they
1322 * hold the top-level event's child_mutex, so any descendant that
1323 * goes to exit will block in sync_child_event.
1324 * When called from perf_pending_event it's OK because event->ctx
1325 * is the current context on this CPU and preemption is disabled,
1326 * hence we can't get into perf_event_task_sched_out for this context.
1328 void perf_event_disable(struct perf_event *event)
1330 struct perf_event_context *ctx = event->ctx;
1331 struct task_struct *task = ctx->task;
1335 * Disable the event on the cpu that it's on
1337 cpu_function_call(event->cpu, __perf_event_disable, event);
1342 if (!task_function_call(task, __perf_event_disable, event))
1345 raw_spin_lock_irq(&ctx->lock);
1347 * If the event is still active, we need to retry the cross-call.
1349 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1350 raw_spin_unlock_irq(&ctx->lock);
1352 * Reload the task pointer, it might have been changed by
1353 * a concurrent perf_event_context_sched_out().
1360 * Since we have the lock this context can't be scheduled
1361 * in, so we can change the state safely.
1363 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1364 update_group_times(event);
1365 event->state = PERF_EVENT_STATE_OFF;
1367 raw_spin_unlock_irq(&ctx->lock);
1370 static void perf_set_shadow_time(struct perf_event *event,
1371 struct perf_event_context *ctx,
1375 * use the correct time source for the time snapshot
1377 * We could get by without this by leveraging the
1378 * fact that to get to this function, the caller
1379 * has most likely already called update_context_time()
1380 * and update_cgrp_time_xx() and thus both timestamp
1381 * are identical (or very close). Given that tstamp is,
1382 * already adjusted for cgroup, we could say that:
1383 * tstamp - ctx->timestamp
1385 * tstamp - cgrp->timestamp.
1387 * Then, in perf_output_read(), the calculation would
1388 * work with no changes because:
1389 * - event is guaranteed scheduled in
1390 * - no scheduled out in between
1391 * - thus the timestamp would be the same
1393 * But this is a bit hairy.
1395 * So instead, we have an explicit cgroup call to remain
1396 * within the time time source all along. We believe it
1397 * is cleaner and simpler to understand.
1399 if (is_cgroup_event(event))
1400 perf_cgroup_set_shadow_time(event, tstamp);
1402 event->shadow_ctx_time = tstamp - ctx->timestamp;
1405 #define MAX_INTERRUPTS (~0ULL)
1407 static void perf_log_throttle(struct perf_event *event, int enable);
1410 event_sched_in(struct perf_event *event,
1411 struct perf_cpu_context *cpuctx,
1412 struct perf_event_context *ctx)
1414 u64 tstamp = perf_event_time(event);
1416 if (event->state <= PERF_EVENT_STATE_OFF)
1419 event->state = PERF_EVENT_STATE_ACTIVE;
1420 event->oncpu = smp_processor_id();
1423 * Unthrottle events, since we scheduled we might have missed several
1424 * ticks already, also for a heavily scheduling task there is little
1425 * guarantee it'll get a tick in a timely manner.
1427 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1428 perf_log_throttle(event, 1);
1429 event->hw.interrupts = 0;
1433 * The new state must be visible before we turn it on in the hardware:
1437 if (event->pmu->add(event, PERF_EF_START)) {
1438 event->state = PERF_EVENT_STATE_INACTIVE;
1443 event->tstamp_running += tstamp - event->tstamp_stopped;
1445 perf_set_shadow_time(event, ctx, tstamp);
1447 if (!is_software_event(event))
1448 cpuctx->active_oncpu++;
1451 if (event->attr.exclusive)
1452 cpuctx->exclusive = 1;
1458 group_sched_in(struct perf_event *group_event,
1459 struct perf_cpu_context *cpuctx,
1460 struct perf_event_context *ctx)
1462 struct perf_event *event, *partial_group = NULL;
1463 struct pmu *pmu = group_event->pmu;
1464 u64 now = ctx->time;
1465 bool simulate = false;
1467 if (group_event->state == PERF_EVENT_STATE_OFF)
1470 pmu->start_txn(pmu);
1472 if (event_sched_in(group_event, cpuctx, ctx)) {
1473 pmu->cancel_txn(pmu);
1478 * Schedule in siblings as one group (if any):
1480 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1481 if (event_sched_in(event, cpuctx, ctx)) {
1482 partial_group = event;
1487 if (!pmu->commit_txn(pmu))
1492 * Groups can be scheduled in as one unit only, so undo any
1493 * partial group before returning:
1494 * The events up to the failed event are scheduled out normally,
1495 * tstamp_stopped will be updated.
1497 * The failed events and the remaining siblings need to have
1498 * their timings updated as if they had gone thru event_sched_in()
1499 * and event_sched_out(). This is required to get consistent timings
1500 * across the group. This also takes care of the case where the group
1501 * could never be scheduled by ensuring tstamp_stopped is set to mark
1502 * the time the event was actually stopped, such that time delta
1503 * calculation in update_event_times() is correct.
1505 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1506 if (event == partial_group)
1510 event->tstamp_running += now - event->tstamp_stopped;
1511 event->tstamp_stopped = now;
1513 event_sched_out(event, cpuctx, ctx);
1516 event_sched_out(group_event, cpuctx, ctx);
1518 pmu->cancel_txn(pmu);
1524 * Work out whether we can put this event group on the CPU now.
1526 static int group_can_go_on(struct perf_event *event,
1527 struct perf_cpu_context *cpuctx,
1531 * Groups consisting entirely of software events can always go on.
1533 if (event->group_flags & PERF_GROUP_SOFTWARE)
1536 * If an exclusive group is already on, no other hardware
1539 if (cpuctx->exclusive)
1542 * If this group is exclusive and there are already
1543 * events on the CPU, it can't go on.
1545 if (event->attr.exclusive && cpuctx->active_oncpu)
1548 * Otherwise, try to add it if all previous groups were able
1554 static void add_event_to_ctx(struct perf_event *event,
1555 struct perf_event_context *ctx)
1557 u64 tstamp = perf_event_time(event);
1559 list_add_event(event, ctx);
1560 perf_group_attach(event);
1561 event->tstamp_enabled = tstamp;
1562 event->tstamp_running = tstamp;
1563 event->tstamp_stopped = tstamp;
1566 static void task_ctx_sched_out(struct perf_event_context *ctx);
1568 ctx_sched_in(struct perf_event_context *ctx,
1569 struct perf_cpu_context *cpuctx,
1570 enum event_type_t event_type,
1571 struct task_struct *task);
1573 static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1574 struct perf_event_context *ctx,
1575 struct task_struct *task)
1577 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1579 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1580 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1582 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1586 * Cross CPU call to install and enable a performance event
1588 * Must be called with ctx->mutex held
1590 static int __perf_install_in_context(void *info)
1592 struct perf_event *event = info;
1593 struct perf_event_context *ctx = event->ctx;
1594 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1595 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1596 struct task_struct *task = current;
1598 perf_ctx_lock(cpuctx, task_ctx);
1599 perf_pmu_disable(cpuctx->ctx.pmu);
1602 * If there was an active task_ctx schedule it out.
1605 task_ctx_sched_out(task_ctx);
1608 * If the context we're installing events in is not the
1609 * active task_ctx, flip them.
1611 if (ctx->task && task_ctx != ctx) {
1613 raw_spin_unlock(&task_ctx->lock);
1614 raw_spin_lock(&ctx->lock);
1619 cpuctx->task_ctx = task_ctx;
1620 task = task_ctx->task;
1623 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
1625 update_context_time(ctx);
1627 * update cgrp time only if current cgrp
1628 * matches event->cgrp. Must be done before
1629 * calling add_event_to_ctx()
1631 update_cgrp_time_from_event(event);
1633 add_event_to_ctx(event, ctx);
1636 * Schedule everything back in
1638 perf_event_sched_in(cpuctx, task_ctx, task);
1640 perf_pmu_enable(cpuctx->ctx.pmu);
1641 perf_ctx_unlock(cpuctx, task_ctx);
1647 * Attach a performance event to a context
1649 * First we add the event to the list with the hardware enable bit
1650 * in event->hw_config cleared.
1652 * If the event is attached to a task which is on a CPU we use a smp
1653 * call to enable it in the task context. The task might have been
1654 * scheduled away, but we check this in the smp call again.
1657 perf_install_in_context(struct perf_event_context *ctx,
1658 struct perf_event *event,
1661 struct task_struct *task = ctx->task;
1663 lockdep_assert_held(&ctx->mutex);
1669 * Per cpu events are installed via an smp call and
1670 * the install is always successful.
1672 cpu_function_call(cpu, __perf_install_in_context, event);
1677 if (!task_function_call(task, __perf_install_in_context, event))
1680 raw_spin_lock_irq(&ctx->lock);
1682 * If we failed to find a running task, but find the context active now
1683 * that we've acquired the ctx->lock, retry.
1685 if (ctx->is_active) {
1686 raw_spin_unlock_irq(&ctx->lock);
1688 * Reload the task pointer, it might have been changed by
1689 * a concurrent perf_event_context_sched_out().
1693 * Reload the task pointer, it might have been changed by
1694 * a concurrent perf_event_context_sched_out().
1701 * Since the task isn't running, its safe to add the event, us holding
1702 * the ctx->lock ensures the task won't get scheduled in.
1704 add_event_to_ctx(event, ctx);
1705 raw_spin_unlock_irq(&ctx->lock);
1709 * Put a event into inactive state and update time fields.
1710 * Enabling the leader of a group effectively enables all
1711 * the group members that aren't explicitly disabled, so we
1712 * have to update their ->tstamp_enabled also.
1713 * Note: this works for group members as well as group leaders
1714 * since the non-leader members' sibling_lists will be empty.
1716 static void __perf_event_mark_enabled(struct perf_event *event,
1717 struct perf_event_context *ctx)
1719 struct perf_event *sub;
1720 u64 tstamp = perf_event_time(event);
1722 event->state = PERF_EVENT_STATE_INACTIVE;
1723 event->tstamp_enabled = tstamp - event->total_time_enabled;
1724 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1725 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1726 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1731 * Cross CPU call to enable a performance event
1733 static int __perf_event_enable(void *info)
1735 struct perf_event *event = info;
1736 struct perf_event_context *ctx = event->ctx;
1737 struct perf_event *leader = event->group_leader;
1738 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1742 * There's a time window between 'ctx->is_active' check
1743 * in perf_event_enable function and this place having:
1745 * - ctx->lock unlocked
1747 * where the task could be killed and 'ctx' deactivated
1748 * by perf_event_exit_task.
1750 if (!ctx->is_active)
1753 raw_spin_lock(&ctx->lock);
1754 update_context_time(ctx);
1756 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1760 * set current task's cgroup time reference point
1762 perf_cgroup_set_timestamp(current, ctx);
1764 __perf_event_mark_enabled(event, ctx);
1766 if (!event_filter_match(event)) {
1767 if (is_cgroup_event(event))
1768 perf_cgroup_defer_enabled(event);
1773 * If the event is in a group and isn't the group leader,
1774 * then don't put it on unless the group is on.
1776 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1779 if (!group_can_go_on(event, cpuctx, 1)) {
1782 if (event == leader)
1783 err = group_sched_in(event, cpuctx, ctx);
1785 err = event_sched_in(event, cpuctx, ctx);
1790 * If this event can't go on and it's part of a
1791 * group, then the whole group has to come off.
1793 if (leader != event)
1794 group_sched_out(leader, cpuctx, ctx);
1795 if (leader->attr.pinned) {
1796 update_group_times(leader);
1797 leader->state = PERF_EVENT_STATE_ERROR;
1802 raw_spin_unlock(&ctx->lock);
1810 * If event->ctx is a cloned context, callers must make sure that
1811 * every task struct that event->ctx->task could possibly point to
1812 * remains valid. This condition is satisfied when called through
1813 * perf_event_for_each_child or perf_event_for_each as described
1814 * for perf_event_disable.
1816 void perf_event_enable(struct perf_event *event)
1818 struct perf_event_context *ctx = event->ctx;
1819 struct task_struct *task = ctx->task;
1823 * Enable the event on the cpu that it's on
1825 cpu_function_call(event->cpu, __perf_event_enable, event);
1829 raw_spin_lock_irq(&ctx->lock);
1830 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1834 * If the event is in error state, clear that first.
1835 * That way, if we see the event in error state below, we
1836 * know that it has gone back into error state, as distinct
1837 * from the task having been scheduled away before the
1838 * cross-call arrived.
1840 if (event->state == PERF_EVENT_STATE_ERROR)
1841 event->state = PERF_EVENT_STATE_OFF;
1844 if (!ctx->is_active) {
1845 __perf_event_mark_enabled(event, ctx);
1849 raw_spin_unlock_irq(&ctx->lock);
1851 if (!task_function_call(task, __perf_event_enable, event))
1854 raw_spin_lock_irq(&ctx->lock);
1857 * If the context is active and the event is still off,
1858 * we need to retry the cross-call.
1860 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1862 * task could have been flipped by a concurrent
1863 * perf_event_context_sched_out()
1870 raw_spin_unlock_irq(&ctx->lock);
1873 int perf_event_refresh(struct perf_event *event, int refresh)
1876 * not supported on inherited events
1878 if (event->attr.inherit || !is_sampling_event(event))
1881 atomic_add(refresh, &event->event_limit);
1882 perf_event_enable(event);
1886 EXPORT_SYMBOL_GPL(perf_event_refresh);
1888 static void ctx_sched_out(struct perf_event_context *ctx,
1889 struct perf_cpu_context *cpuctx,
1890 enum event_type_t event_type)
1892 struct perf_event *event;
1893 int is_active = ctx->is_active;
1895 ctx->is_active &= ~event_type;
1896 if (likely(!ctx->nr_events))
1899 update_context_time(ctx);
1900 update_cgrp_time_from_cpuctx(cpuctx);
1901 if (!ctx->nr_active)
1904 perf_pmu_disable(ctx->pmu);
1905 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
1906 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1907 group_sched_out(event, cpuctx, ctx);
1910 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
1911 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1912 group_sched_out(event, cpuctx, ctx);
1914 perf_pmu_enable(ctx->pmu);
1918 * Test whether two contexts are equivalent, i.e. whether they
1919 * have both been cloned from the same version of the same context
1920 * and they both have the same number of enabled events.
1921 * If the number of enabled events is the same, then the set
1922 * of enabled events should be the same, because these are both
1923 * inherited contexts, therefore we can't access individual events
1924 * in them directly with an fd; we can only enable/disable all
1925 * events via prctl, or enable/disable all events in a family
1926 * via ioctl, which will have the same effect on both contexts.
1928 static int context_equiv(struct perf_event_context *ctx1,
1929 struct perf_event_context *ctx2)
1931 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1932 && ctx1->parent_gen == ctx2->parent_gen
1933 && !ctx1->pin_count && !ctx2->pin_count;
1936 static void __perf_event_sync_stat(struct perf_event *event,
1937 struct perf_event *next_event)
1941 if (!event->attr.inherit_stat)
1945 * Update the event value, we cannot use perf_event_read()
1946 * because we're in the middle of a context switch and have IRQs
1947 * disabled, which upsets smp_call_function_single(), however
1948 * we know the event must be on the current CPU, therefore we
1949 * don't need to use it.
1951 switch (event->state) {
1952 case PERF_EVENT_STATE_ACTIVE:
1953 event->pmu->read(event);
1956 case PERF_EVENT_STATE_INACTIVE:
1957 update_event_times(event);
1965 * In order to keep per-task stats reliable we need to flip the event
1966 * values when we flip the contexts.
1968 value = local64_read(&next_event->count);
1969 value = local64_xchg(&event->count, value);
1970 local64_set(&next_event->count, value);
1972 swap(event->total_time_enabled, next_event->total_time_enabled);
1973 swap(event->total_time_running, next_event->total_time_running);
1976 * Since we swizzled the values, update the user visible data too.
1978 perf_event_update_userpage(event);
1979 perf_event_update_userpage(next_event);
1982 #define list_next_entry(pos, member) \
1983 list_entry(pos->member.next, typeof(*pos), member)
1985 static void perf_event_sync_stat(struct perf_event_context *ctx,
1986 struct perf_event_context *next_ctx)
1988 struct perf_event *event, *next_event;
1993 update_context_time(ctx);
1995 event = list_first_entry(&ctx->event_list,
1996 struct perf_event, event_entry);
1998 next_event = list_first_entry(&next_ctx->event_list,
1999 struct perf_event, event_entry);
2001 while (&event->event_entry != &ctx->event_list &&
2002 &next_event->event_entry != &next_ctx->event_list) {
2004 __perf_event_sync_stat(event, next_event);
2006 event = list_next_entry(event, event_entry);
2007 next_event = list_next_entry(next_event, event_entry);
2011 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2012 struct task_struct *next)
2014 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
2015 struct perf_event_context *next_ctx;
2016 struct perf_event_context *parent;
2017 struct perf_cpu_context *cpuctx;
2023 cpuctx = __get_cpu_context(ctx);
2024 if (!cpuctx->task_ctx)
2028 parent = rcu_dereference(ctx->parent_ctx);
2029 next_ctx = next->perf_event_ctxp[ctxn];
2030 if (parent && next_ctx &&
2031 rcu_dereference(next_ctx->parent_ctx) == parent) {
2033 * Looks like the two contexts are clones, so we might be
2034 * able to optimize the context switch. We lock both
2035 * contexts and check that they are clones under the
2036 * lock (including re-checking that neither has been
2037 * uncloned in the meantime). It doesn't matter which
2038 * order we take the locks because no other cpu could
2039 * be trying to lock both of these tasks.
2041 raw_spin_lock(&ctx->lock);
2042 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
2043 if (context_equiv(ctx, next_ctx)) {
2045 * XXX do we need a memory barrier of sorts
2046 * wrt to rcu_dereference() of perf_event_ctxp
2048 task->perf_event_ctxp[ctxn] = next_ctx;
2049 next->perf_event_ctxp[ctxn] = ctx;
2051 next_ctx->task = task;
2054 perf_event_sync_stat(ctx, next_ctx);
2056 raw_spin_unlock(&next_ctx->lock);
2057 raw_spin_unlock(&ctx->lock);
2062 raw_spin_lock(&ctx->lock);
2063 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2064 cpuctx->task_ctx = NULL;
2065 raw_spin_unlock(&ctx->lock);
2069 #define for_each_task_context_nr(ctxn) \
2070 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2073 * Called from scheduler to remove the events of the current task,
2074 * with interrupts disabled.
2076 * We stop each event and update the event value in event->count.
2078 * This does not protect us against NMI, but disable()
2079 * sets the disabled bit in the control field of event _before_
2080 * accessing the event control register. If a NMI hits, then it will
2081 * not restart the event.
2083 void __perf_event_task_sched_out(struct task_struct *task,
2084 struct task_struct *next)
2088 for_each_task_context_nr(ctxn)
2089 perf_event_context_sched_out(task, ctxn, next);
2092 * if cgroup events exist on this CPU, then we need
2093 * to check if we have to switch out PMU state.
2094 * cgroup event are system-wide mode only
2096 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2097 perf_cgroup_sched_out(task, next);
2100 static void task_ctx_sched_out(struct perf_event_context *ctx)
2102 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2104 if (!cpuctx->task_ctx)
2107 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2110 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2111 cpuctx->task_ctx = NULL;
2115 * Called with IRQs disabled
2117 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2118 enum event_type_t event_type)
2120 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
2124 ctx_pinned_sched_in(struct perf_event_context *ctx,
2125 struct perf_cpu_context *cpuctx)
2127 struct perf_event *event;
2129 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2130 if (event->state <= PERF_EVENT_STATE_OFF)
2132 if (!event_filter_match(event))
2135 /* may need to reset tstamp_enabled */
2136 if (is_cgroup_event(event))
2137 perf_cgroup_mark_enabled(event, ctx);
2139 if (group_can_go_on(event, cpuctx, 1))
2140 group_sched_in(event, cpuctx, ctx);
2143 * If this pinned group hasn't been scheduled,
2144 * put it in error state.
2146 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2147 update_group_times(event);
2148 event->state = PERF_EVENT_STATE_ERROR;
2154 ctx_flexible_sched_in(struct perf_event_context *ctx,
2155 struct perf_cpu_context *cpuctx)
2157 struct perf_event *event;
2160 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2161 /* Ignore events in OFF or ERROR state */
2162 if (event->state <= PERF_EVENT_STATE_OFF)
2165 * Listen to the 'cpu' scheduling filter constraint
2168 if (!event_filter_match(event))
2171 /* may need to reset tstamp_enabled */
2172 if (is_cgroup_event(event))
2173 perf_cgroup_mark_enabled(event, ctx);
2175 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2176 if (group_sched_in(event, cpuctx, ctx))
2183 ctx_sched_in(struct perf_event_context *ctx,
2184 struct perf_cpu_context *cpuctx,
2185 enum event_type_t event_type,
2186 struct task_struct *task)
2189 int is_active = ctx->is_active;
2191 ctx->is_active |= event_type;
2192 if (likely(!ctx->nr_events))
2196 ctx->timestamp = now;
2197 perf_cgroup_set_timestamp(task, ctx);
2199 * First go through the list and put on any pinned groups
2200 * in order to give them the best chance of going on.
2202 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
2203 ctx_pinned_sched_in(ctx, cpuctx);
2205 /* Then walk through the lower prio flexible groups */
2206 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
2207 ctx_flexible_sched_in(ctx, cpuctx);
2210 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2211 enum event_type_t event_type,
2212 struct task_struct *task)
2214 struct perf_event_context *ctx = &cpuctx->ctx;
2216 ctx_sched_in(ctx, cpuctx, event_type, task);
2219 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2220 struct task_struct *task)
2222 struct perf_cpu_context *cpuctx;
2224 cpuctx = __get_cpu_context(ctx);
2225 if (cpuctx->task_ctx == ctx)
2228 perf_ctx_lock(cpuctx, ctx);
2229 perf_pmu_disable(ctx->pmu);
2231 * We want to keep the following priority order:
2232 * cpu pinned (that don't need to move), task pinned,
2233 * cpu flexible, task flexible.
2235 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2238 cpuctx->task_ctx = ctx;
2240 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2242 perf_pmu_enable(ctx->pmu);
2243 perf_ctx_unlock(cpuctx, ctx);
2246 * Since these rotations are per-cpu, we need to ensure the
2247 * cpu-context we got scheduled on is actually rotating.
2249 perf_pmu_rotate_start(ctx->pmu);
2253 * Called from scheduler to add the events of the current task
2254 * with interrupts disabled.
2256 * We restore the event value and then enable it.
2258 * This does not protect us against NMI, but enable()
2259 * sets the enabled bit in the control field of event _before_
2260 * accessing the event control register. If a NMI hits, then it will
2261 * keep the event running.
2263 void __perf_event_task_sched_in(struct task_struct *prev,
2264 struct task_struct *task)
2266 struct perf_event_context *ctx;
2269 for_each_task_context_nr(ctxn) {
2270 ctx = task->perf_event_ctxp[ctxn];
2274 perf_event_context_sched_in(ctx, task);
2277 * if cgroup events exist on this CPU, then we need
2278 * to check if we have to switch in PMU state.
2279 * cgroup event are system-wide mode only
2281 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2282 perf_cgroup_sched_in(prev, task);
2285 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2287 u64 frequency = event->attr.sample_freq;
2288 u64 sec = NSEC_PER_SEC;
2289 u64 divisor, dividend;
2291 int count_fls, nsec_fls, frequency_fls, sec_fls;
2293 count_fls = fls64(count);
2294 nsec_fls = fls64(nsec);
2295 frequency_fls = fls64(frequency);
2299 * We got @count in @nsec, with a target of sample_freq HZ
2300 * the target period becomes:
2303 * period = -------------------
2304 * @nsec * sample_freq
2309 * Reduce accuracy by one bit such that @a and @b converge
2310 * to a similar magnitude.
2312 #define REDUCE_FLS(a, b) \
2314 if (a##_fls > b##_fls) { \
2324 * Reduce accuracy until either term fits in a u64, then proceed with
2325 * the other, so that finally we can do a u64/u64 division.
2327 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2328 REDUCE_FLS(nsec, frequency);
2329 REDUCE_FLS(sec, count);
2332 if (count_fls + sec_fls > 64) {
2333 divisor = nsec * frequency;
2335 while (count_fls + sec_fls > 64) {
2336 REDUCE_FLS(count, sec);
2340 dividend = count * sec;
2342 dividend = count * sec;
2344 while (nsec_fls + frequency_fls > 64) {
2345 REDUCE_FLS(nsec, frequency);
2349 divisor = nsec * frequency;
2355 return div64_u64(dividend, divisor);
2358 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
2360 struct hw_perf_event *hwc = &event->hw;
2361 s64 period, sample_period;
2364 period = perf_calculate_period(event, nsec, count);
2366 delta = (s64)(period - hwc->sample_period);
2367 delta = (delta + 7) / 8; /* low pass filter */
2369 sample_period = hwc->sample_period + delta;
2374 hwc->sample_period = sample_period;
2376 if (local64_read(&hwc->period_left) > 8*sample_period) {
2377 event->pmu->stop(event, PERF_EF_UPDATE);
2378 local64_set(&hwc->period_left, 0);
2379 event->pmu->start(event, PERF_EF_RELOAD);
2383 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
2385 struct perf_event *event;
2386 struct hw_perf_event *hwc;
2387 u64 interrupts, now;
2390 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2391 if (event->state != PERF_EVENT_STATE_ACTIVE)
2394 if (!event_filter_match(event))
2399 interrupts = hwc->interrupts;
2400 hwc->interrupts = 0;
2403 * unthrottle events on the tick
2405 if (interrupts == MAX_INTERRUPTS) {
2406 perf_log_throttle(event, 1);
2407 event->pmu->start(event, 0);
2410 if (!event->attr.freq || !event->attr.sample_freq)
2413 event->pmu->read(event);
2414 now = local64_read(&event->count);
2415 delta = now - hwc->freq_count_stamp;
2416 hwc->freq_count_stamp = now;
2419 perf_adjust_period(event, period, delta);
2424 * Round-robin a context's events:
2426 static void rotate_ctx(struct perf_event_context *ctx)
2429 * Rotate the first entry last of non-pinned groups. Rotation might be
2430 * disabled by the inheritance code.
2432 if (!ctx->rotate_disable)
2433 list_rotate_left(&ctx->flexible_groups);
2437 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2438 * because they're strictly cpu affine and rotate_start is called with IRQs
2439 * disabled, while rotate_context is called from IRQ context.
2441 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
2443 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
2444 struct perf_event_context *ctx = NULL;
2445 int rotate = 0, remove = 1;
2447 if (cpuctx->ctx.nr_events) {
2449 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2453 ctx = cpuctx->task_ctx;
2454 if (ctx && ctx->nr_events) {
2456 if (ctx->nr_events != ctx->nr_active)
2460 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2461 perf_pmu_disable(cpuctx->ctx.pmu);
2462 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
2464 perf_ctx_adjust_freq(ctx, interval);
2469 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2471 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
2473 rotate_ctx(&cpuctx->ctx);
2477 perf_event_sched_in(cpuctx, ctx, current);
2481 list_del_init(&cpuctx->rotation_list);
2483 perf_pmu_enable(cpuctx->ctx.pmu);
2484 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2487 void perf_event_task_tick(void)
2489 struct list_head *head = &__get_cpu_var(rotation_list);
2490 struct perf_cpu_context *cpuctx, *tmp;
2492 WARN_ON(!irqs_disabled());
2494 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2495 if (cpuctx->jiffies_interval == 1 ||
2496 !(jiffies % cpuctx->jiffies_interval))
2497 perf_rotate_context(cpuctx);
2501 static int event_enable_on_exec(struct perf_event *event,
2502 struct perf_event_context *ctx)
2504 if (!event->attr.enable_on_exec)
2507 event->attr.enable_on_exec = 0;
2508 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2511 __perf_event_mark_enabled(event, ctx);
2517 * Enable all of a task's events that have been marked enable-on-exec.
2518 * This expects task == current.
2520 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2522 struct perf_event *event;
2523 unsigned long flags;
2527 local_irq_save(flags);
2528 if (!ctx || !ctx->nr_events)
2532 * We must ctxsw out cgroup events to avoid conflict
2533 * when invoking perf_task_event_sched_in() later on
2534 * in this function. Otherwise we end up trying to
2535 * ctxswin cgroup events which are already scheduled
2538 perf_cgroup_sched_out(current, NULL);
2540 raw_spin_lock(&ctx->lock);
2541 task_ctx_sched_out(ctx);
2543 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2544 ret = event_enable_on_exec(event, ctx);
2549 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2550 ret = event_enable_on_exec(event, ctx);
2556 * Unclone this context if we enabled any event.
2561 raw_spin_unlock(&ctx->lock);
2564 * Also calls ctxswin for cgroup events, if any:
2566 perf_event_context_sched_in(ctx, ctx->task);
2568 local_irq_restore(flags);
2572 * Cross CPU call to read the hardware event
2574 static void __perf_event_read(void *info)
2576 struct perf_event *event = info;
2577 struct perf_event_context *ctx = event->ctx;
2578 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2581 * If this is a task context, we need to check whether it is
2582 * the current task context of this cpu. If not it has been
2583 * scheduled out before the smp call arrived. In that case
2584 * event->count would have been updated to a recent sample
2585 * when the event was scheduled out.
2587 if (ctx->task && cpuctx->task_ctx != ctx)
2590 raw_spin_lock(&ctx->lock);
2591 if (ctx->is_active) {
2592 update_context_time(ctx);
2593 update_cgrp_time_from_event(event);
2595 update_event_times(event);
2596 if (event->state == PERF_EVENT_STATE_ACTIVE)
2597 event->pmu->read(event);
2598 raw_spin_unlock(&ctx->lock);
2601 static inline u64 perf_event_count(struct perf_event *event)
2603 return local64_read(&event->count) + atomic64_read(&event->child_count);
2606 static u64 perf_event_read(struct perf_event *event)
2609 * If event is enabled and currently active on a CPU, update the
2610 * value in the event structure:
2612 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2613 smp_call_function_single(event->oncpu,
2614 __perf_event_read, event, 1);
2615 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2616 struct perf_event_context *ctx = event->ctx;
2617 unsigned long flags;
2619 raw_spin_lock_irqsave(&ctx->lock, flags);
2621 * may read while context is not active
2622 * (e.g., thread is blocked), in that case
2623 * we cannot update context time
2625 if (ctx->is_active) {
2626 update_context_time(ctx);
2627 update_cgrp_time_from_event(event);
2629 update_event_times(event);
2630 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2633 return perf_event_count(event);
2640 struct callchain_cpus_entries {
2641 struct rcu_head rcu_head;
2642 struct perf_callchain_entry *cpu_entries[0];
2645 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
2646 static atomic_t nr_callchain_events;
2647 static DEFINE_MUTEX(callchain_mutex);
2648 struct callchain_cpus_entries *callchain_cpus_entries;
2651 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
2652 struct pt_regs *regs)
2656 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
2657 struct pt_regs *regs)
2661 static void release_callchain_buffers_rcu(struct rcu_head *head)
2663 struct callchain_cpus_entries *entries;
2666 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
2668 for_each_possible_cpu(cpu)
2669 kfree(entries->cpu_entries[cpu]);
2674 static void release_callchain_buffers(void)
2676 struct callchain_cpus_entries *entries;
2678 entries = callchain_cpus_entries;
2679 rcu_assign_pointer(callchain_cpus_entries, NULL);
2680 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2683 static int alloc_callchain_buffers(void)
2687 struct callchain_cpus_entries *entries;
2690 * We can't use the percpu allocation API for data that can be
2691 * accessed from NMI. Use a temporary manual per cpu allocation
2692 * until that gets sorted out.
2694 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
2696 entries = kzalloc(size, GFP_KERNEL);
2700 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
2702 for_each_possible_cpu(cpu) {
2703 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2705 if (!entries->cpu_entries[cpu])
2709 rcu_assign_pointer(callchain_cpus_entries, entries);
2714 for_each_possible_cpu(cpu)
2715 kfree(entries->cpu_entries[cpu]);
2721 static int get_callchain_buffers(void)
2726 mutex_lock(&callchain_mutex);
2728 count = atomic_inc_return(&nr_callchain_events);
2729 if (WARN_ON_ONCE(count < 1)) {
2735 /* If the allocation failed, give up */
2736 if (!callchain_cpus_entries)
2741 err = alloc_callchain_buffers();
2743 release_callchain_buffers();
2745 mutex_unlock(&callchain_mutex);
2750 static void put_callchain_buffers(void)
2752 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2753 release_callchain_buffers();
2754 mutex_unlock(&callchain_mutex);
2758 static int get_recursion_context(int *recursion)
2766 else if (in_softirq())
2771 if (recursion[rctx])
2780 static inline void put_recursion_context(int *recursion, int rctx)
2786 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2789 struct callchain_cpus_entries *entries;
2791 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2795 entries = rcu_dereference(callchain_cpus_entries);
2799 cpu = smp_processor_id();
2801 return &entries->cpu_entries[cpu][*rctx];
2805 put_callchain_entry(int rctx)
2807 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2810 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2813 struct perf_callchain_entry *entry;
2816 entry = get_callchain_entry(&rctx);
2825 if (!user_mode(regs)) {
2826 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2827 perf_callchain_kernel(entry, regs);
2829 regs = task_pt_regs(current);
2835 perf_callchain_store(entry, PERF_CONTEXT_USER);
2836 perf_callchain_user(entry, regs);
2840 put_callchain_entry(rctx);
2846 * Initialize the perf_event context in a task_struct:
2848 static void __perf_event_init_context(struct perf_event_context *ctx)
2850 raw_spin_lock_init(&ctx->lock);
2851 mutex_init(&ctx->mutex);
2852 INIT_LIST_HEAD(&ctx->pinned_groups);
2853 INIT_LIST_HEAD(&ctx->flexible_groups);
2854 INIT_LIST_HEAD(&ctx->event_list);
2855 atomic_set(&ctx->refcount, 1);
2858 static struct perf_event_context *
2859 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2861 struct perf_event_context *ctx;
2863 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2867 __perf_event_init_context(ctx);
2870 get_task_struct(task);
2877 static struct task_struct *
2878 find_lively_task_by_vpid(pid_t vpid)
2880 struct task_struct *task;
2887 task = find_task_by_vpid(vpid);
2889 get_task_struct(task);
2893 return ERR_PTR(-ESRCH);
2895 /* Reuse ptrace permission checks for now. */
2897 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2902 put_task_struct(task);
2903 return ERR_PTR(err);
2908 * Returns a matching context with refcount and pincount.
2910 static struct perf_event_context *
2911 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2913 struct perf_event_context *ctx;
2914 struct perf_cpu_context *cpuctx;
2915 unsigned long flags;
2919 /* Must be root to operate on a CPU event: */
2920 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2921 return ERR_PTR(-EACCES);
2924 * We could be clever and allow to attach a event to an
2925 * offline CPU and activate it when the CPU comes up, but
2928 if (!cpu_online(cpu))
2929 return ERR_PTR(-ENODEV);
2931 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2940 ctxn = pmu->task_ctx_nr;
2945 ctx = perf_lock_task_context(task, ctxn, &flags);
2949 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2951 ctx = alloc_perf_context(pmu, task);
2957 mutex_lock(&task->perf_event_mutex);
2959 * If it has already passed perf_event_exit_task().
2960 * we must see PF_EXITING, it takes this mutex too.
2962 if (task->flags & PF_EXITING)
2964 else if (task->perf_event_ctxp[ctxn])
2969 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2971 mutex_unlock(&task->perf_event_mutex);
2973 if (unlikely(err)) {
2985 return ERR_PTR(err);
2988 static void perf_event_free_filter(struct perf_event *event);
2990 static void free_event_rcu(struct rcu_head *head)
2992 struct perf_event *event;
2994 event = container_of(head, struct perf_event, rcu_head);
2996 put_pid_ns(event->ns);
2997 perf_event_free_filter(event);
3001 static void ring_buffer_put(struct ring_buffer *rb);
3002 static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb);
3004 static void free_event(struct perf_event *event)
3006 irq_work_sync(&event->pending);
3008 if (!event->parent) {
3009 if (event->attach_state & PERF_ATTACH_TASK)
3010 jump_label_dec(&perf_sched_events);
3011 if (event->attr.mmap || event->attr.mmap_data)
3012 atomic_dec(&nr_mmap_events);
3013 if (event->attr.comm)
3014 atomic_dec(&nr_comm_events);
3015 if (event->attr.task)
3016 atomic_dec(&nr_task_events);
3017 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3018 put_callchain_buffers();
3019 if (is_cgroup_event(event)) {
3020 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
3021 jump_label_dec(&perf_sched_events);
3026 struct ring_buffer *rb;
3029 * Can happen when we close an event with re-directed output.
3031 * Since we have a 0 refcount, perf_mmap_close() will skip
3032 * over us; possibly making our ring_buffer_put() the last.
3034 mutex_lock(&event->mmap_mutex);
3037 rcu_assign_pointer(event->rb, NULL);
3038 ring_buffer_detach(event, rb);
3039 ring_buffer_put(rb); /* could be last */
3041 mutex_unlock(&event->mmap_mutex);
3044 if (is_cgroup_event(event))
3045 perf_detach_cgroup(event);
3048 event->destroy(event);
3051 put_ctx(event->ctx);
3053 call_rcu(&event->rcu_head, free_event_rcu);
3056 int perf_event_release_kernel(struct perf_event *event)
3058 struct perf_event_context *ctx = event->ctx;
3060 WARN_ON_ONCE(ctx->parent_ctx);
3062 * There are two ways this annotation is useful:
3064 * 1) there is a lock recursion from perf_event_exit_task
3065 * see the comment there.
3067 * 2) there is a lock-inversion with mmap_sem through
3068 * perf_event_read_group(), which takes faults while
3069 * holding ctx->mutex, however this is called after
3070 * the last filedesc died, so there is no possibility
3071 * to trigger the AB-BA case.
3073 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
3074 perf_remove_from_context(event, true);
3075 mutex_unlock(&ctx->mutex);
3081 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3084 * Called when the last reference to the file is gone.
3086 static void put_event(struct perf_event *event)
3088 struct task_struct *owner;
3090 if (!atomic_long_dec_and_test(&event->refcount))
3094 owner = ACCESS_ONCE(event->owner);
3096 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3097 * !owner it means the list deletion is complete and we can indeed
3098 * free this event, otherwise we need to serialize on
3099 * owner->perf_event_mutex.
3101 smp_read_barrier_depends();
3104 * Since delayed_put_task_struct() also drops the last
3105 * task reference we can safely take a new reference
3106 * while holding the rcu_read_lock().
3108 get_task_struct(owner);
3113 mutex_lock(&owner->perf_event_mutex);
3115 * We have to re-check the event->owner field, if it is cleared
3116 * we raced with perf_event_exit_task(), acquiring the mutex
3117 * ensured they're done, and we can proceed with freeing the
3121 list_del_init(&event->owner_entry);
3122 mutex_unlock(&owner->perf_event_mutex);
3123 put_task_struct(owner);
3126 perf_event_release_kernel(event);
3129 static int perf_release(struct inode *inode, struct file *file)
3131 put_event(file->private_data);
3135 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
3137 struct perf_event *child;
3143 mutex_lock(&event->child_mutex);
3144 total += perf_event_read(event);
3145 *enabled += event->total_time_enabled +
3146 atomic64_read(&event->child_total_time_enabled);
3147 *running += event->total_time_running +
3148 atomic64_read(&event->child_total_time_running);
3150 list_for_each_entry(child, &event->child_list, child_list) {
3151 total += perf_event_read(child);
3152 *enabled += child->total_time_enabled;
3153 *running += child->total_time_running;
3155 mutex_unlock(&event->child_mutex);
3159 EXPORT_SYMBOL_GPL(perf_event_read_value);
3161 static int perf_event_read_group(struct perf_event *event,
3162 u64 read_format, char __user *buf)
3164 struct perf_event *leader = event->group_leader, *sub;
3165 int n = 0, size = 0, ret = -EFAULT;
3166 struct perf_event_context *ctx = leader->ctx;
3168 u64 count, enabled, running;
3170 mutex_lock(&ctx->mutex);
3171 count = perf_event_read_value(leader, &enabled, &running);
3173 values[n++] = 1 + leader->nr_siblings;
3174 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3175 values[n++] = enabled;
3176 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3177 values[n++] = running;
3178 values[n++] = count;
3179 if (read_format & PERF_FORMAT_ID)
3180 values[n++] = primary_event_id(leader);
3182 size = n * sizeof(u64);
3184 if (copy_to_user(buf, values, size))
3189 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3192 values[n++] = perf_event_read_value(sub, &enabled, &running);
3193 if (read_format & PERF_FORMAT_ID)
3194 values[n++] = primary_event_id(sub);
3196 size = n * sizeof(u64);
3198 if (copy_to_user(buf + ret, values, size)) {
3206 mutex_unlock(&ctx->mutex);
3211 static int perf_event_read_one(struct perf_event *event,
3212 u64 read_format, char __user *buf)
3214 u64 enabled, running;
3218 values[n++] = perf_event_read_value(event, &enabled, &running);
3219 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3220 values[n++] = enabled;
3221 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3222 values[n++] = running;
3223 if (read_format & PERF_FORMAT_ID)
3224 values[n++] = primary_event_id(event);
3226 if (copy_to_user(buf, values, n * sizeof(u64)))
3229 return n * sizeof(u64);
3233 * Read the performance event - simple non blocking version for now
3236 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3238 u64 read_format = event->attr.read_format;
3242 * Return end-of-file for a read on a event that is in
3243 * error state (i.e. because it was pinned but it couldn't be
3244 * scheduled on to the CPU at some point).
3246 if (event->state == PERF_EVENT_STATE_ERROR)
3249 if (count < event->read_size)
3252 WARN_ON_ONCE(event->ctx->parent_ctx);
3253 if (read_format & PERF_FORMAT_GROUP)
3254 ret = perf_event_read_group(event, read_format, buf);
3256 ret = perf_event_read_one(event, read_format, buf);
3262 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3264 struct perf_event *event = file->private_data;
3266 return perf_read_hw(event, buf, count);
3269 static unsigned int perf_poll(struct file *file, poll_table *wait)
3271 struct perf_event *event = file->private_data;
3272 struct ring_buffer *rb;
3273 unsigned int events = POLL_HUP;
3276 * Pin the event->rb by taking event->mmap_mutex; otherwise
3277 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
3279 mutex_lock(&event->mmap_mutex);
3282 events = atomic_xchg(&rb->poll, 0);
3283 mutex_unlock(&event->mmap_mutex);
3285 poll_wait(file, &event->waitq, wait);
3290 static void perf_event_reset(struct perf_event *event)
3292 (void)perf_event_read(event);
3293 local64_set(&event->count, 0);
3294 perf_event_update_userpage(event);
3298 * Holding the top-level event's child_mutex means that any
3299 * descendant process that has inherited this event will block
3300 * in sync_child_event if it goes to exit, thus satisfying the
3301 * task existence requirements of perf_event_enable/disable.
3303 static void perf_event_for_each_child(struct perf_event *event,
3304 void (*func)(struct perf_event *))
3306 struct perf_event *child;
3308 WARN_ON_ONCE(event->ctx->parent_ctx);
3309 mutex_lock(&event->child_mutex);
3311 list_for_each_entry(child, &event->child_list, child_list)
3313 mutex_unlock(&event->child_mutex);
3316 static void perf_event_for_each(struct perf_event *event,
3317 void (*func)(struct perf_event *))
3319 struct perf_event_context *ctx = event->ctx;
3320 struct perf_event *sibling;
3322 WARN_ON_ONCE(ctx->parent_ctx);
3323 mutex_lock(&ctx->mutex);
3324 event = event->group_leader;
3326 perf_event_for_each_child(event, func);
3328 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3329 perf_event_for_each_child(event, func);
3330 mutex_unlock(&ctx->mutex);
3333 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3335 struct perf_event_context *ctx = event->ctx;
3339 if (!is_sampling_event(event))
3342 if (copy_from_user(&value, arg, sizeof(value)))
3348 raw_spin_lock_irq(&ctx->lock);
3349 if (event->attr.freq) {
3350 if (value > sysctl_perf_event_sample_rate) {
3355 event->attr.sample_freq = value;
3357 event->attr.sample_period = value;
3358 event->hw.sample_period = value;
3361 raw_spin_unlock_irq(&ctx->lock);
3366 static const struct file_operations perf_fops;
3368 static struct file *perf_fget_light(int fd, int *fput_needed)
3372 file = fget_light(fd, fput_needed);
3374 return ERR_PTR(-EBADF);
3376 if (file->f_op != &perf_fops) {
3377 fput_light(file, *fput_needed);
3379 return ERR_PTR(-EBADF);
3385 static int perf_event_set_output(struct perf_event *event,
3386 struct perf_event *output_event);
3387 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3389 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3391 struct perf_event *event = file->private_data;
3392 void (*func)(struct perf_event *);
3396 case PERF_EVENT_IOC_ENABLE:
3397 func = perf_event_enable;
3399 case PERF_EVENT_IOC_DISABLE:
3400 func = perf_event_disable;
3402 case PERF_EVENT_IOC_RESET:
3403 func = perf_event_reset;
3406 case PERF_EVENT_IOC_REFRESH:
3407 return perf_event_refresh(event, arg);
3409 case PERF_EVENT_IOC_PERIOD:
3410 return perf_event_period(event, (u64 __user *)arg);
3412 case PERF_EVENT_IOC_SET_OUTPUT:
3414 struct file *output_file = NULL;
3415 struct perf_event *output_event = NULL;
3416 int fput_needed = 0;
3420 output_file = perf_fget_light(arg, &fput_needed);
3421 if (IS_ERR(output_file))
3422 return PTR_ERR(output_file);
3423 output_event = output_file->private_data;
3426 ret = perf_event_set_output(event, output_event);
3428 fput_light(output_file, fput_needed);
3433 case PERF_EVENT_IOC_SET_FILTER:
3434 return perf_event_set_filter(event, (void __user *)arg);
3440 if (flags & PERF_IOC_FLAG_GROUP)
3441 perf_event_for_each(event, func);
3443 perf_event_for_each_child(event, func);
3448 #ifdef CONFIG_COMPAT
3449 static long perf_compat_ioctl(struct file *file, unsigned int cmd,
3452 switch (_IOC_NR(cmd)) {
3453 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
3454 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
3455 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
3456 cmd &= ~IOCSIZE_MASK;
3457 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
3461 return perf_ioctl(file, cmd, arg);
3464 # define perf_compat_ioctl NULL
3467 int perf_event_task_enable(void)
3469 struct perf_event *event;
3471 mutex_lock(¤t->perf_event_mutex);
3472 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3473 perf_event_for_each_child(event, perf_event_enable);
3474 mutex_unlock(¤t->perf_event_mutex);
3479 int perf_event_task_disable(void)
3481 struct perf_event *event;
3483 mutex_lock(¤t->perf_event_mutex);
3484 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3485 perf_event_for_each_child(event, perf_event_disable);
3486 mutex_unlock(¤t->perf_event_mutex);
3491 #ifndef PERF_EVENT_INDEX_OFFSET
3492 # define PERF_EVENT_INDEX_OFFSET 0
3495 static int perf_event_index(struct perf_event *event)
3497 if (event->hw.state & PERF_HES_STOPPED)
3500 if (event->state != PERF_EVENT_STATE_ACTIVE)
3503 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
3506 static void calc_timer_values(struct perf_event *event,
3513 ctx_time = event->shadow_ctx_time + now;
3514 *enabled = ctx_time - event->tstamp_enabled;
3515 *running = ctx_time - event->tstamp_running;
3519 * Callers need to ensure there can be no nesting of this function, otherwise
3520 * the seqlock logic goes bad. We can not serialize this because the arch
3521 * code calls this from NMI context.
3523 void perf_event_update_userpage(struct perf_event *event)
3525 struct perf_event_mmap_page *userpg;
3526 struct ring_buffer *rb;
3527 u64 enabled, running;
3531 * compute total_time_enabled, total_time_running
3532 * based on snapshot values taken when the event
3533 * was last scheduled in.
3535 * we cannot simply called update_context_time()
3536 * because of locking issue as we can be called in
3539 calc_timer_values(event, &enabled, &running);
3540 rb = rcu_dereference(event->rb);
3544 userpg = rb->user_page;
3547 * Disable preemption so as to not let the corresponding user-space
3548 * spin too long if we get preempted.
3553 userpg->index = perf_event_index(event);
3554 userpg->offset = perf_event_count(event);
3555 if (event->state == PERF_EVENT_STATE_ACTIVE)
3556 userpg->offset -= local64_read(&event->hw.prev_count);
3558 userpg->time_enabled = enabled +
3559 atomic64_read(&event->child_total_time_enabled);
3561 userpg->time_running = running +
3562 atomic64_read(&event->child_total_time_running);
3571 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3573 struct perf_event *event = vma->vm_file->private_data;
3574 struct ring_buffer *rb;
3575 int ret = VM_FAULT_SIGBUS;
3577 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3578 if (vmf->pgoff == 0)
3584 rb = rcu_dereference(event->rb);
3588 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3591 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
3595 get_page(vmf->page);
3596 vmf->page->mapping = vma->vm_file->f_mapping;
3597 vmf->page->index = vmf->pgoff;
3606 static void ring_buffer_attach(struct perf_event *event,
3607 struct ring_buffer *rb)
3609 unsigned long flags;
3611 if (!list_empty(&event->rb_entry))
3614 spin_lock_irqsave(&rb->event_lock, flags);
3615 if (list_empty(&event->rb_entry))
3616 list_add(&event->rb_entry, &rb->event_list);
3617 spin_unlock_irqrestore(&rb->event_lock, flags);
3620 static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb)
3622 unsigned long flags;
3624 if (list_empty(&event->rb_entry))
3627 spin_lock_irqsave(&rb->event_lock, flags);
3628 list_del_init(&event->rb_entry);
3629 wake_up_all(&event->waitq);
3630 spin_unlock_irqrestore(&rb->event_lock, flags);
3633 static void ring_buffer_wakeup(struct perf_event *event)
3635 struct ring_buffer *rb;
3638 rb = rcu_dereference(event->rb);
3640 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3641 wake_up_all(&event->waitq);
3646 static void rb_free_rcu(struct rcu_head *rcu_head)
3648 struct ring_buffer *rb;
3650 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3654 static struct ring_buffer *ring_buffer_get(struct perf_event *event)
3656 struct ring_buffer *rb;
3659 rb = rcu_dereference(event->rb);
3661 if (!atomic_inc_not_zero(&rb->refcount))
3669 static void ring_buffer_put(struct ring_buffer *rb)
3671 if (!atomic_dec_and_test(&rb->refcount))
3674 WARN_ON_ONCE(!list_empty(&rb->event_list));
3676 call_rcu(&rb->rcu_head, rb_free_rcu);
3679 static void perf_mmap_open(struct vm_area_struct *vma)
3681 struct perf_event *event = vma->vm_file->private_data;
3683 atomic_inc(&event->mmap_count);
3684 atomic_inc(&event->rb->mmap_count);
3688 * A buffer can be mmap()ed multiple times; either directly through the same
3689 * event, or through other events by use of perf_event_set_output().
3691 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3692 * the buffer here, where we still have a VM context. This means we need
3693 * to detach all events redirecting to us.
3695 static void perf_mmap_close(struct vm_area_struct *vma)
3697 struct perf_event *event = vma->vm_file->private_data;
3699 struct ring_buffer *rb = event->rb;
3700 struct user_struct *mmap_user = rb->mmap_user;
3701 int mmap_locked = rb->mmap_locked;
3702 unsigned long size = perf_data_size(rb);
3704 atomic_dec(&rb->mmap_count);
3706 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
3709 /* Detach current event from the buffer. */
3710 rcu_assign_pointer(event->rb, NULL);
3711 ring_buffer_detach(event, rb);
3712 mutex_unlock(&event->mmap_mutex);
3714 /* If there's still other mmap()s of this buffer, we're done. */
3715 if (atomic_read(&rb->mmap_count)) {
3716 ring_buffer_put(rb); /* can't be last */
3721 * No other mmap()s, detach from all other events that might redirect
3722 * into the now unreachable buffer. Somewhat complicated by the
3723 * fact that rb::event_lock otherwise nests inside mmap_mutex.
3727 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
3728 if (!atomic_long_inc_not_zero(&event->refcount)) {
3730 * This event is en-route to free_event() which will
3731 * detach it and remove it from the list.
3737 mutex_lock(&event->mmap_mutex);
3739 * Check we didn't race with perf_event_set_output() which can
3740 * swizzle the rb from under us while we were waiting to
3741 * acquire mmap_mutex.
3743 * If we find a different rb; ignore this event, a next
3744 * iteration will no longer find it on the list. We have to
3745 * still restart the iteration to make sure we're not now
3746 * iterating the wrong list.
3748 if (event->rb == rb) {
3749 rcu_assign_pointer(event->rb, NULL);
3750 ring_buffer_detach(event, rb);
3751 ring_buffer_put(rb); /* can't be last, we still have one */
3753 mutex_unlock(&event->mmap_mutex);
3757 * Restart the iteration; either we're on the wrong list or
3758 * destroyed its integrity by doing a deletion.
3765 * It could be there's still a few 0-ref events on the list; they'll
3766 * get cleaned up by free_event() -- they'll also still have their
3767 * ref on the rb and will free it whenever they are done with it.
3769 * Aside from that, this buffer is 'fully' detached and unmapped,
3770 * undo the VM accounting.
3773 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
3774 vma->vm_mm->pinned_vm -= mmap_locked;
3775 free_uid(mmap_user);
3777 ring_buffer_put(rb); /* could be last */
3780 static const struct vm_operations_struct perf_mmap_vmops = {
3781 .open = perf_mmap_open,
3782 .close = perf_mmap_close,
3783 .fault = perf_mmap_fault,
3784 .page_mkwrite = perf_mmap_fault,
3787 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3789 struct perf_event *event = file->private_data;
3790 unsigned long user_locked, user_lock_limit;
3791 struct user_struct *user = current_user();
3792 unsigned long locked, lock_limit;
3793 struct ring_buffer *rb;
3794 unsigned long vma_size;
3795 unsigned long nr_pages;
3796 long user_extra, extra;
3797 int ret = 0, flags = 0;
3800 * Don't allow mmap() of inherited per-task counters. This would
3801 * create a performance issue due to all children writing to the
3804 if (event->cpu == -1 && event->attr.inherit)
3807 if (!(vma->vm_flags & VM_SHARED))
3810 vma_size = vma->vm_end - vma->vm_start;
3811 nr_pages = (vma_size / PAGE_SIZE) - 1;
3814 * If we have rb pages ensure they're a power-of-two number, so we
3815 * can do bitmasks instead of modulo.
3817 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3820 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3823 if (vma->vm_pgoff != 0)
3826 WARN_ON_ONCE(event->ctx->parent_ctx);
3828 mutex_lock(&event->mmap_mutex);
3830 if (event->rb->nr_pages != nr_pages) {
3835 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
3837 * Raced against perf_mmap_close() through
3838 * perf_event_set_output(). Try again, hope for better
3841 mutex_unlock(&event->mmap_mutex);
3848 user_extra = nr_pages + 1;
3849 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3852 * Increase the limit linearly with more CPUs:
3854 user_lock_limit *= num_online_cpus();
3856 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3859 if (user_locked > user_lock_limit)
3860 extra = user_locked - user_lock_limit;
3862 lock_limit = rlimit(RLIMIT_MEMLOCK);
3863 lock_limit >>= PAGE_SHIFT;
3864 locked = vma->vm_mm->pinned_vm + extra;
3866 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3867 !capable(CAP_IPC_LOCK)) {
3874 if (vma->vm_flags & VM_WRITE)
3875 flags |= RING_BUFFER_WRITABLE;
3877 rb = rb_alloc(nr_pages,
3878 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3886 atomic_set(&rb->mmap_count, 1);
3887 rb->mmap_locked = extra;
3888 rb->mmap_user = get_current_user();
3890 atomic_long_add(user_extra, &user->locked_vm);
3891 vma->vm_mm->pinned_vm += extra;
3893 ring_buffer_attach(event, rb);
3894 rcu_assign_pointer(event->rb, rb);
3898 atomic_inc(&event->mmap_count);
3899 mutex_unlock(&event->mmap_mutex);
3902 * Since pinned accounting is per vm we cannot allow fork() to copy our
3905 vma->vm_flags |= VM_DONTCOPY | VM_RESERVED;
3906 vma->vm_ops = &perf_mmap_vmops;
3911 static int perf_fasync(int fd, struct file *filp, int on)
3913 struct inode *inode = filp->f_path.dentry->d_inode;
3914 struct perf_event *event = filp->private_data;
3917 mutex_lock(&inode->i_mutex);
3918 retval = fasync_helper(fd, filp, on, &event->fasync);
3919 mutex_unlock(&inode->i_mutex);
3927 static const struct file_operations perf_fops = {
3928 .llseek = no_llseek,
3929 .release = perf_release,
3932 .unlocked_ioctl = perf_ioctl,
3933 .compat_ioctl = perf_compat_ioctl,
3935 .fasync = perf_fasync,
3941 * If there's data, ensure we set the poll() state and publish everything
3942 * to user-space before waking everybody up.
3945 void perf_event_wakeup(struct perf_event *event)
3947 ring_buffer_wakeup(event);
3949 if (event->pending_kill) {
3950 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3951 event->pending_kill = 0;
3955 static void perf_pending_event(struct irq_work *entry)
3957 struct perf_event *event = container_of(entry,
3958 struct perf_event, pending);
3961 rctx = perf_swevent_get_recursion_context();
3963 * If we 'fail' here, that's OK, it means recursion is already disabled
3964 * and we won't recurse 'further'.
3967 if (event->pending_disable) {
3968 event->pending_disable = 0;
3969 __perf_event_disable(event);
3972 if (event->pending_wakeup) {
3973 event->pending_wakeup = 0;
3974 perf_event_wakeup(event);
3978 perf_swevent_put_recursion_context(rctx);
3982 * We assume there is only KVM supporting the callbacks.
3983 * Later on, we might change it to a list if there is
3984 * another virtualization implementation supporting the callbacks.
3986 struct perf_guest_info_callbacks *perf_guest_cbs;
3988 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3990 perf_guest_cbs = cbs;
3993 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3995 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3997 perf_guest_cbs = NULL;
4000 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4002 static void __perf_event_header__init_id(struct perf_event_header *header,
4003 struct perf_sample_data *data,
4004 struct perf_event *event)
4006 u64 sample_type = event->attr.sample_type;
4008 data->type = sample_type;
4009 header->size += event->id_header_size;
4011 if (sample_type & PERF_SAMPLE_TID) {
4012 /* namespace issues */
4013 data->tid_entry.pid = perf_event_pid(event, current);
4014 data->tid_entry.tid = perf_event_tid(event, current);
4017 if (sample_type & PERF_SAMPLE_TIME)
4018 data->time = perf_clock();
4020 if (sample_type & PERF_SAMPLE_ID)
4021 data->id = primary_event_id(event);
4023 if (sample_type & PERF_SAMPLE_STREAM_ID)
4024 data->stream_id = event->id;
4026 if (sample_type & PERF_SAMPLE_CPU) {
4027 data->cpu_entry.cpu = raw_smp_processor_id();
4028 data->cpu_entry.reserved = 0;
4032 void perf_event_header__init_id(struct perf_event_header *header,
4033 struct perf_sample_data *data,
4034 struct perf_event *event)
4036 if (event->attr.sample_id_all)
4037 __perf_event_header__init_id(header, data, event);
4040 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4041 struct perf_sample_data *data)
4043 u64 sample_type = data->type;
4045 if (sample_type & PERF_SAMPLE_TID)
4046 perf_output_put(handle, data->tid_entry);
4048 if (sample_type & PERF_SAMPLE_TIME)
4049 perf_output_put(handle, data->time);
4051 if (sample_type & PERF_SAMPLE_ID)
4052 perf_output_put(handle, data->id);
4054 if (sample_type & PERF_SAMPLE_STREAM_ID)
4055 perf_output_put(handle, data->stream_id);
4057 if (sample_type & PERF_SAMPLE_CPU)
4058 perf_output_put(handle, data->cpu_entry);
4061 void perf_event__output_id_sample(struct perf_event *event,
4062 struct perf_output_handle *handle,
4063 struct perf_sample_data *sample)
4065 if (event->attr.sample_id_all)
4066 __perf_event__output_id_sample(handle, sample);
4069 static void perf_output_read_one(struct perf_output_handle *handle,
4070 struct perf_event *event,
4071 u64 enabled, u64 running)
4073 u64 read_format = event->attr.read_format;
4077 values[n++] = perf_event_count(event);
4078 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4079 values[n++] = enabled +
4080 atomic64_read(&event->child_total_time_enabled);
4082 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4083 values[n++] = running +
4084 atomic64_read(&event->child_total_time_running);
4086 if (read_format & PERF_FORMAT_ID)
4087 values[n++] = primary_event_id(event);
4089 __output_copy(handle, values, n * sizeof(u64));
4093 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4095 static void perf_output_read_group(struct perf_output_handle *handle,
4096 struct perf_event *event,
4097 u64 enabled, u64 running)
4099 struct perf_event *leader = event->group_leader, *sub;
4100 u64 read_format = event->attr.read_format;
4104 values[n++] = 1 + leader->nr_siblings;
4106 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4107 values[n++] = enabled;
4109 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4110 values[n++] = running;
4112 if (leader != event)
4113 leader->pmu->read(leader);
4115 values[n++] = perf_event_count(leader);
4116 if (read_format & PERF_FORMAT_ID)
4117 values[n++] = primary_event_id(leader);
4119 __output_copy(handle, values, n * sizeof(u64));
4121 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4125 sub->pmu->read(sub);
4127 values[n++] = perf_event_count(sub);
4128 if (read_format & PERF_FORMAT_ID)
4129 values[n++] = primary_event_id(sub);
4131 __output_copy(handle, values, n * sizeof(u64));
4135 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4136 PERF_FORMAT_TOTAL_TIME_RUNNING)
4138 static void perf_output_read(struct perf_output_handle *handle,
4139 struct perf_event *event)
4141 u64 enabled = 0, running = 0;
4142 u64 read_format = event->attr.read_format;
4145 * compute total_time_enabled, total_time_running
4146 * based on snapshot values taken when the event
4147 * was last scheduled in.
4149 * we cannot simply called update_context_time()
4150 * because of locking issue as we are called in
4153 if (read_format & PERF_FORMAT_TOTAL_TIMES)
4154 calc_timer_values(event, &enabled, &running);
4156 if (event->attr.read_format & PERF_FORMAT_GROUP)
4157 perf_output_read_group(handle, event, enabled, running);
4159 perf_output_read_one(handle, event, enabled, running);
4162 void perf_output_sample(struct perf_output_handle *handle,
4163 struct perf_event_header *header,
4164 struct perf_sample_data *data,
4165 struct perf_event *event)
4167 u64 sample_type = data->type;
4169 perf_output_put(handle, *header);
4171 if (sample_type & PERF_SAMPLE_IP)
4172 perf_output_put(handle, data->ip);
4174 if (sample_type & PERF_SAMPLE_TID)
4175 perf_output_put(handle, data->tid_entry);
4177 if (sample_type & PERF_SAMPLE_TIME)
4178 perf_output_put(handle, data->time);
4180 if (sample_type & PERF_SAMPLE_ADDR)
4181 perf_output_put(handle, data->addr);
4183 if (sample_type & PERF_SAMPLE_ID)
4184 perf_output_put(handle, data->id);
4186 if (sample_type & PERF_SAMPLE_STREAM_ID)
4187 perf_output_put(handle, data->stream_id);
4189 if (sample_type & PERF_SAMPLE_CPU)
4190 perf_output_put(handle, data->cpu_entry);
4192 if (sample_type & PERF_SAMPLE_PERIOD)
4193 perf_output_put(handle, data->period);
4195 if (sample_type & PERF_SAMPLE_READ)
4196 perf_output_read(handle, event);
4198 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4199 if (data->callchain) {
4202 if (data->callchain)
4203 size += data->callchain->nr;
4205 size *= sizeof(u64);
4207 __output_copy(handle, data->callchain, size);
4210 perf_output_put(handle, nr);
4214 if (sample_type & PERF_SAMPLE_RAW) {
4216 perf_output_put(handle, data->raw->size);
4217 __output_copy(handle, data->raw->data,
4224 .size = sizeof(u32),
4227 perf_output_put(handle, raw);
4231 if (!event->attr.watermark) {
4232 int wakeup_events = event->attr.wakeup_events;
4234 if (wakeup_events) {
4235 struct ring_buffer *rb = handle->rb;
4236 int events = local_inc_return(&rb->events);
4238 if (events >= wakeup_events) {
4239 local_sub(wakeup_events, &rb->events);
4240 local_inc(&rb->wakeup);
4246 void perf_prepare_sample(struct perf_event_header *header,
4247 struct perf_sample_data *data,
4248 struct perf_event *event,
4249 struct pt_regs *regs)
4251 u64 sample_type = event->attr.sample_type;
4253 header->type = PERF_RECORD_SAMPLE;
4254 header->size = sizeof(*header) + event->header_size;
4257 header->misc |= perf_misc_flags(regs);
4259 __perf_event_header__init_id(header, data, event);
4261 if (sample_type & PERF_SAMPLE_IP)
4262 data->ip = perf_instruction_pointer(regs);
4264 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4267 data->callchain = perf_callchain(regs);
4269 if (data->callchain)
4270 size += data->callchain->nr;
4272 header->size += size * sizeof(u64);
4275 if (sample_type & PERF_SAMPLE_RAW) {
4276 int size = sizeof(u32);
4279 size += data->raw->size;
4281 size += sizeof(u32);
4283 WARN_ON_ONCE(size & (sizeof(u64)-1));
4284 header->size += size;
4288 static void perf_event_output(struct perf_event *event,
4289 struct perf_sample_data *data,
4290 struct pt_regs *regs)
4292 struct perf_output_handle handle;
4293 struct perf_event_header header;
4295 /* protect the callchain buffers */
4298 perf_prepare_sample(&header, data, event, regs);
4300 if (perf_output_begin(&handle, event, header.size))
4303 perf_output_sample(&handle, &header, data, event);
4305 perf_output_end(&handle);
4315 struct perf_read_event {
4316 struct perf_event_header header;
4323 perf_event_read_event(struct perf_event *event,
4324 struct task_struct *task)
4326 struct perf_output_handle handle;
4327 struct perf_sample_data sample;
4328 struct perf_read_event read_event = {
4330 .type = PERF_RECORD_READ,
4332 .size = sizeof(read_event) + event->read_size,
4334 .pid = perf_event_pid(event, task),
4335 .tid = perf_event_tid(event, task),
4339 perf_event_header__init_id(&read_event.header, &sample, event);
4340 ret = perf_output_begin(&handle, event, read_event.header.size);
4344 perf_output_put(&handle, read_event);
4345 perf_output_read(&handle, event);
4346 perf_event__output_id_sample(event, &handle, &sample);
4348 perf_output_end(&handle);
4352 * task tracking -- fork/exit
4354 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4357 struct perf_task_event {
4358 struct task_struct *task;
4359 struct perf_event_context *task_ctx;
4362 struct perf_event_header header;
4372 static void perf_event_task_output(struct perf_event *event,
4373 struct perf_task_event *task_event)
4375 struct perf_output_handle handle;
4376 struct perf_sample_data sample;
4377 struct task_struct *task = task_event->task;
4378 int ret, size = task_event->event_id.header.size;
4380 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4382 ret = perf_output_begin(&handle, event,
4383 task_event->event_id.header.size);
4387 task_event->event_id.pid = perf_event_pid(event, task);
4388 task_event->event_id.ppid = perf_event_pid(event, current);
4390 task_event->event_id.tid = perf_event_tid(event, task);
4391 task_event->event_id.ptid = perf_event_tid(event, current);
4393 perf_output_put(&handle, task_event->event_id);
4395 perf_event__output_id_sample(event, &handle, &sample);
4397 perf_output_end(&handle);
4399 task_event->event_id.header.size = size;
4402 static int perf_event_task_match(struct perf_event *event)
4404 if (event->state < PERF_EVENT_STATE_INACTIVE)
4407 if (!event_filter_match(event))
4410 if (event->attr.comm || event->attr.mmap ||
4411 event->attr.mmap_data || event->attr.task)
4417 static void perf_event_task_ctx(struct perf_event_context *ctx,
4418 struct perf_task_event *task_event)
4420 struct perf_event *event;
4422 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4423 if (perf_event_task_match(event))
4424 perf_event_task_output(event, task_event);
4428 static void perf_event_task_event(struct perf_task_event *task_event)
4430 struct perf_cpu_context *cpuctx;
4431 struct perf_event_context *ctx;
4436 list_for_each_entry_rcu(pmu, &pmus, entry) {
4437 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4438 if (cpuctx->unique_pmu != pmu)
4440 perf_event_task_ctx(&cpuctx->ctx, task_event);
4442 ctx = task_event->task_ctx;
4444 ctxn = pmu->task_ctx_nr;
4447 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4450 perf_event_task_ctx(ctx, task_event);
4452 put_cpu_ptr(pmu->pmu_cpu_context);
4457 static void perf_event_task(struct task_struct *task,
4458 struct perf_event_context *task_ctx,
4461 struct perf_task_event task_event;
4463 if (!atomic_read(&nr_comm_events) &&
4464 !atomic_read(&nr_mmap_events) &&
4465 !atomic_read(&nr_task_events))
4468 task_event = (struct perf_task_event){
4470 .task_ctx = task_ctx,
4473 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4475 .size = sizeof(task_event.event_id),
4481 .time = perf_clock(),
4485 perf_event_task_event(&task_event);
4488 void perf_event_fork(struct task_struct *task)
4490 perf_event_task(task, NULL, 1);
4497 struct perf_comm_event {
4498 struct task_struct *task;
4503 struct perf_event_header header;
4510 static void perf_event_comm_output(struct perf_event *event,
4511 struct perf_comm_event *comm_event)
4513 struct perf_output_handle handle;
4514 struct perf_sample_data sample;
4515 int size = comm_event->event_id.header.size;
4518 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4519 ret = perf_output_begin(&handle, event,
4520 comm_event->event_id.header.size);
4525 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4526 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4528 perf_output_put(&handle, comm_event->event_id);
4529 __output_copy(&handle, comm_event->comm,
4530 comm_event->comm_size);
4532 perf_event__output_id_sample(event, &handle, &sample);
4534 perf_output_end(&handle);
4536 comm_event->event_id.header.size = size;
4539 static int perf_event_comm_match(struct perf_event *event)
4541 if (event->state < PERF_EVENT_STATE_INACTIVE)
4544 if (!event_filter_match(event))
4547 if (event->attr.comm)
4553 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4554 struct perf_comm_event *comm_event)
4556 struct perf_event *event;
4558 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4559 if (perf_event_comm_match(event))
4560 perf_event_comm_output(event, comm_event);
4564 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4566 struct perf_cpu_context *cpuctx;
4567 struct perf_event_context *ctx;
4568 char comm[TASK_COMM_LEN];
4573 memset(comm, 0, sizeof(comm));
4574 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4575 size = ALIGN(strlen(comm)+1, sizeof(u64));
4577 comm_event->comm = comm;
4578 comm_event->comm_size = size;
4580 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4582 list_for_each_entry_rcu(pmu, &pmus, entry) {
4583 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4584 if (cpuctx->unique_pmu != pmu)
4586 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4588 ctxn = pmu->task_ctx_nr;
4592 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4594 perf_event_comm_ctx(ctx, comm_event);
4596 put_cpu_ptr(pmu->pmu_cpu_context);
4601 void perf_event_comm(struct task_struct *task)
4603 struct perf_comm_event comm_event;
4604 struct perf_event_context *ctx;
4607 for_each_task_context_nr(ctxn) {
4608 ctx = task->perf_event_ctxp[ctxn];
4612 perf_event_enable_on_exec(ctx);
4615 if (!atomic_read(&nr_comm_events))
4618 comm_event = (struct perf_comm_event){
4624 .type = PERF_RECORD_COMM,
4633 perf_event_comm_event(&comm_event);
4640 struct perf_mmap_event {
4641 struct vm_area_struct *vma;
4643 const char *file_name;
4647 struct perf_event_header header;
4657 static void perf_event_mmap_output(struct perf_event *event,
4658 struct perf_mmap_event *mmap_event)
4660 struct perf_output_handle handle;
4661 struct perf_sample_data sample;
4662 int size = mmap_event->event_id.header.size;
4665 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4666 ret = perf_output_begin(&handle, event,
4667 mmap_event->event_id.header.size);
4671 mmap_event->event_id.pid = perf_event_pid(event, current);
4672 mmap_event->event_id.tid = perf_event_tid(event, current);
4674 perf_output_put(&handle, mmap_event->event_id);
4675 __output_copy(&handle, mmap_event->file_name,
4676 mmap_event->file_size);
4678 perf_event__output_id_sample(event, &handle, &sample);
4680 perf_output_end(&handle);
4682 mmap_event->event_id.header.size = size;
4685 static int perf_event_mmap_match(struct perf_event *event,
4686 struct perf_mmap_event *mmap_event,
4689 if (event->state < PERF_EVENT_STATE_INACTIVE)
4692 if (!event_filter_match(event))
4695 if ((!executable && event->attr.mmap_data) ||
4696 (executable && event->attr.mmap))
4702 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4703 struct perf_mmap_event *mmap_event,
4706 struct perf_event *event;
4708 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4709 if (perf_event_mmap_match(event, mmap_event, executable))
4710 perf_event_mmap_output(event, mmap_event);
4714 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4716 struct perf_cpu_context *cpuctx;
4717 struct perf_event_context *ctx;
4718 struct vm_area_struct *vma = mmap_event->vma;
4719 struct file *file = vma->vm_file;
4727 memset(tmp, 0, sizeof(tmp));
4731 * d_path works from the end of the rb backwards, so we
4732 * need to add enough zero bytes after the string to handle
4733 * the 64bit alignment we do later.
4735 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4737 name = strncpy(tmp, "//enomem", sizeof(tmp));
4740 name = d_path(&file->f_path, buf, PATH_MAX);
4742 name = strncpy(tmp, "//toolong", sizeof(tmp));
4746 if (arch_vma_name(mmap_event->vma)) {
4747 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4753 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4755 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4756 vma->vm_end >= vma->vm_mm->brk) {
4757 name = strncpy(tmp, "[heap]", sizeof(tmp));
4759 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4760 vma->vm_end >= vma->vm_mm->start_stack) {
4761 name = strncpy(tmp, "[stack]", sizeof(tmp));
4765 name = strncpy(tmp, "//anon", sizeof(tmp));
4770 size = ALIGN(strlen(name)+1, sizeof(u64));
4772 mmap_event->file_name = name;
4773 mmap_event->file_size = size;
4775 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4778 list_for_each_entry_rcu(pmu, &pmus, entry) {
4779 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4780 if (cpuctx->unique_pmu != pmu)
4782 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4783 vma->vm_flags & VM_EXEC);
4785 ctxn = pmu->task_ctx_nr;
4789 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4791 perf_event_mmap_ctx(ctx, mmap_event,
4792 vma->vm_flags & VM_EXEC);
4795 put_cpu_ptr(pmu->pmu_cpu_context);
4802 void perf_event_mmap(struct vm_area_struct *vma)
4804 struct perf_mmap_event mmap_event;
4806 if (!atomic_read(&nr_mmap_events))
4809 mmap_event = (struct perf_mmap_event){
4815 .type = PERF_RECORD_MMAP,
4816 .misc = PERF_RECORD_MISC_USER,
4821 .start = vma->vm_start,
4822 .len = vma->vm_end - vma->vm_start,
4823 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4827 perf_event_mmap_event(&mmap_event);
4831 * IRQ throttle logging
4834 static void perf_log_throttle(struct perf_event *event, int enable)
4836 struct perf_output_handle handle;
4837 struct perf_sample_data sample;
4841 struct perf_event_header header;
4845 } throttle_event = {
4847 .type = PERF_RECORD_THROTTLE,
4849 .size = sizeof(throttle_event),
4851 .time = perf_clock(),
4852 .id = primary_event_id(event),
4853 .stream_id = event->id,
4857 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4859 perf_event_header__init_id(&throttle_event.header, &sample, event);
4861 ret = perf_output_begin(&handle, event,
4862 throttle_event.header.size);
4866 perf_output_put(&handle, throttle_event);
4867 perf_event__output_id_sample(event, &handle, &sample);
4868 perf_output_end(&handle);
4872 * Generic event overflow handling, sampling.
4875 static int __perf_event_overflow(struct perf_event *event,
4876 int throttle, struct perf_sample_data *data,
4877 struct pt_regs *regs)
4879 int events = atomic_read(&event->event_limit);
4880 struct hw_perf_event *hwc = &event->hw;
4884 * Non-sampling counters might still use the PMI to fold short
4885 * hardware counters, ignore those.
4887 if (unlikely(!is_sampling_event(event)))
4890 if (unlikely(hwc->interrupts >= max_samples_per_tick)) {
4892 hwc->interrupts = MAX_INTERRUPTS;
4893 perf_log_throttle(event, 0);
4899 if (event->attr.freq) {
4900 u64 now = perf_clock();
4901 s64 delta = now - hwc->freq_time_stamp;
4903 hwc->freq_time_stamp = now;
4905 if (delta > 0 && delta < 2*TICK_NSEC)
4906 perf_adjust_period(event, delta, hwc->last_period);
4910 * XXX event_limit might not quite work as expected on inherited
4914 event->pending_kill = POLL_IN;
4915 if (events && atomic_dec_and_test(&event->event_limit)) {
4917 event->pending_kill = POLL_HUP;
4918 event->pending_disable = 1;
4919 irq_work_queue(&event->pending);
4922 if (event->overflow_handler)
4923 event->overflow_handler(event, data, regs);
4925 perf_event_output(event, data, regs);
4927 if (event->fasync && event->pending_kill) {
4928 event->pending_wakeup = 1;
4929 irq_work_queue(&event->pending);
4935 int perf_event_overflow(struct perf_event *event,
4936 struct perf_sample_data *data,
4937 struct pt_regs *regs)
4939 return __perf_event_overflow(event, 1, data, regs);
4943 * Generic software event infrastructure
4946 struct swevent_htable {
4947 struct swevent_hlist *swevent_hlist;
4948 struct mutex hlist_mutex;
4951 /* Recursion avoidance in each contexts */
4952 int recursion[PERF_NR_CONTEXTS];
4954 /* Keeps track of cpu being initialized/exited */
4958 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4961 * We directly increment event->count and keep a second value in
4962 * event->hw.period_left to count intervals. This period event
4963 * is kept in the range [-sample_period, 0] so that we can use the
4967 static u64 perf_swevent_set_period(struct perf_event *event)
4969 struct hw_perf_event *hwc = &event->hw;
4970 u64 period = hwc->last_period;
4974 hwc->last_period = hwc->sample_period;
4977 old = val = local64_read(&hwc->period_left);
4981 nr = div64_u64(period + val, period);
4982 offset = nr * period;
4984 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4990 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4991 struct perf_sample_data *data,
4992 struct pt_regs *regs)
4994 struct hw_perf_event *hwc = &event->hw;
4997 data->period = event->hw.last_period;
4999 overflow = perf_swevent_set_period(event);
5001 if (hwc->interrupts == MAX_INTERRUPTS)
5004 for (; overflow; overflow--) {
5005 if (__perf_event_overflow(event, throttle,
5008 * We inhibit the overflow from happening when
5009 * hwc->interrupts == MAX_INTERRUPTS.
5017 static void perf_swevent_event(struct perf_event *event, u64 nr,
5018 struct perf_sample_data *data,
5019 struct pt_regs *regs)
5021 struct hw_perf_event *hwc = &event->hw;
5023 local64_add(nr, &event->count);
5028 if (!is_sampling_event(event))
5031 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
5032 return perf_swevent_overflow(event, 1, data, regs);
5034 if (local64_add_negative(nr, &hwc->period_left))
5037 perf_swevent_overflow(event, 0, data, regs);
5040 static int perf_exclude_event(struct perf_event *event,
5041 struct pt_regs *regs)
5043 if (event->hw.state & PERF_HES_STOPPED)
5047 if (event->attr.exclude_user && user_mode(regs))
5050 if (event->attr.exclude_kernel && !user_mode(regs))
5057 static int perf_swevent_match(struct perf_event *event,
5058 enum perf_type_id type,
5060 struct perf_sample_data *data,
5061 struct pt_regs *regs)
5063 if (event->attr.type != type)
5066 if (event->attr.config != event_id)
5069 if (perf_exclude_event(event, regs))
5075 static inline u64 swevent_hash(u64 type, u32 event_id)
5077 u64 val = event_id | (type << 32);
5079 return hash_64(val, SWEVENT_HLIST_BITS);
5082 static inline struct hlist_head *
5083 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
5085 u64 hash = swevent_hash(type, event_id);
5087 return &hlist->heads[hash];
5090 /* For the read side: events when they trigger */
5091 static inline struct hlist_head *
5092 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
5094 struct swevent_hlist *hlist;
5096 hlist = rcu_dereference(swhash->swevent_hlist);
5100 return __find_swevent_head(hlist, type, event_id);
5103 /* For the event head insertion and removal in the hlist */
5104 static inline struct hlist_head *
5105 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
5107 struct swevent_hlist *hlist;
5108 u32 event_id = event->attr.config;
5109 u64 type = event->attr.type;
5112 * Event scheduling is always serialized against hlist allocation
5113 * and release. Which makes the protected version suitable here.
5114 * The context lock guarantees that.
5116 hlist = rcu_dereference_protected(swhash->swevent_hlist,
5117 lockdep_is_held(&event->ctx->lock));
5121 return __find_swevent_head(hlist, type, event_id);
5124 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5126 struct perf_sample_data *data,
5127 struct pt_regs *regs)
5129 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5130 struct perf_event *event;
5131 struct hlist_node *node;
5132 struct hlist_head *head;
5135 head = find_swevent_head_rcu(swhash, type, event_id);
5139 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5140 if (perf_swevent_match(event, type, event_id, data, regs))
5141 perf_swevent_event(event, nr, data, regs);
5147 int perf_swevent_get_recursion_context(void)
5149 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5151 return get_recursion_context(swhash->recursion);
5153 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
5155 inline void perf_swevent_put_recursion_context(int rctx)
5157 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5159 put_recursion_context(swhash->recursion, rctx);
5162 void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
5164 struct perf_sample_data data;
5167 preempt_disable_notrace();
5168 rctx = perf_swevent_get_recursion_context();
5172 perf_sample_data_init(&data, addr);
5174 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
5176 perf_swevent_put_recursion_context(rctx);
5177 preempt_enable_notrace();
5180 static void perf_swevent_read(struct perf_event *event)
5184 static int perf_swevent_add(struct perf_event *event, int flags)
5186 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5187 struct hw_perf_event *hwc = &event->hw;
5188 struct hlist_head *head;
5190 if (is_sampling_event(event)) {
5191 hwc->last_period = hwc->sample_period;
5192 perf_swevent_set_period(event);
5195 hwc->state = !(flags & PERF_EF_START);
5197 head = find_swevent_head(swhash, event);
5200 * We can race with cpu hotplug code. Do not
5201 * WARN if the cpu just got unplugged.
5203 WARN_ON_ONCE(swhash->online);
5207 hlist_add_head_rcu(&event->hlist_entry, head);
5212 static void perf_swevent_del(struct perf_event *event, int flags)
5214 hlist_del_rcu(&event->hlist_entry);
5217 static void perf_swevent_start(struct perf_event *event, int flags)
5219 event->hw.state = 0;
5222 static void perf_swevent_stop(struct perf_event *event, int flags)
5224 event->hw.state = PERF_HES_STOPPED;
5227 /* Deref the hlist from the update side */
5228 static inline struct swevent_hlist *
5229 swevent_hlist_deref(struct swevent_htable *swhash)
5231 return rcu_dereference_protected(swhash->swevent_hlist,
5232 lockdep_is_held(&swhash->hlist_mutex));
5235 static void swevent_hlist_release(struct swevent_htable *swhash)
5237 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
5242 rcu_assign_pointer(swhash->swevent_hlist, NULL);
5243 kfree_rcu(hlist, rcu_head);
5246 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5248 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5250 mutex_lock(&swhash->hlist_mutex);
5252 if (!--swhash->hlist_refcount)
5253 swevent_hlist_release(swhash);
5255 mutex_unlock(&swhash->hlist_mutex);
5258 static void swevent_hlist_put(struct perf_event *event)
5262 if (event->cpu != -1) {
5263 swevent_hlist_put_cpu(event, event->cpu);
5267 for_each_possible_cpu(cpu)
5268 swevent_hlist_put_cpu(event, cpu);
5271 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5273 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5276 mutex_lock(&swhash->hlist_mutex);
5278 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
5279 struct swevent_hlist *hlist;
5281 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5286 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5288 swhash->hlist_refcount++;
5290 mutex_unlock(&swhash->hlist_mutex);
5295 static int swevent_hlist_get(struct perf_event *event)
5298 int cpu, failed_cpu;
5300 if (event->cpu != -1)
5301 return swevent_hlist_get_cpu(event, event->cpu);
5304 for_each_possible_cpu(cpu) {
5305 err = swevent_hlist_get_cpu(event, cpu);
5315 for_each_possible_cpu(cpu) {
5316 if (cpu == failed_cpu)
5318 swevent_hlist_put_cpu(event, cpu);
5325 struct jump_label_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
5327 static void sw_perf_event_destroy(struct perf_event *event)
5329 u64 event_id = event->attr.config;
5331 WARN_ON(event->parent);
5333 jump_label_dec(&perf_swevent_enabled[event_id]);
5334 swevent_hlist_put(event);
5337 static int perf_swevent_init(struct perf_event *event)
5339 u64 event_id = event->attr.config;
5341 if (event->attr.type != PERF_TYPE_SOFTWARE)
5345 case PERF_COUNT_SW_CPU_CLOCK:
5346 case PERF_COUNT_SW_TASK_CLOCK:
5353 if (event_id >= PERF_COUNT_SW_MAX)
5356 if (!event->parent) {
5359 err = swevent_hlist_get(event);
5363 jump_label_inc(&perf_swevent_enabled[event_id]);
5364 event->destroy = sw_perf_event_destroy;
5370 static struct pmu perf_swevent = {
5371 .task_ctx_nr = perf_sw_context,
5373 .event_init = perf_swevent_init,
5374 .add = perf_swevent_add,
5375 .del = perf_swevent_del,
5376 .start = perf_swevent_start,
5377 .stop = perf_swevent_stop,
5378 .read = perf_swevent_read,
5381 #ifdef CONFIG_EVENT_TRACING
5383 static int perf_tp_filter_match(struct perf_event *event,
5384 struct perf_sample_data *data)
5386 void *record = data->raw->data;
5388 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5393 static int perf_tp_event_match(struct perf_event *event,
5394 struct perf_sample_data *data,
5395 struct pt_regs *regs)
5397 if (event->hw.state & PERF_HES_STOPPED)
5400 * All tracepoints are from kernel-space.
5402 if (event->attr.exclude_kernel)
5405 if (!perf_tp_filter_match(event, data))
5411 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5412 struct pt_regs *regs, struct hlist_head *head, int rctx)
5414 struct perf_sample_data data;
5415 struct perf_event *event;
5416 struct hlist_node *node;
5418 struct perf_raw_record raw = {
5423 perf_sample_data_init(&data, addr);
5426 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5427 if (perf_tp_event_match(event, &data, regs))
5428 perf_swevent_event(event, count, &data, regs);
5431 perf_swevent_put_recursion_context(rctx);
5433 EXPORT_SYMBOL_GPL(perf_tp_event);
5435 static void tp_perf_event_destroy(struct perf_event *event)
5437 perf_trace_destroy(event);
5440 static int perf_tp_event_init(struct perf_event *event)
5444 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5447 err = perf_trace_init(event);
5451 event->destroy = tp_perf_event_destroy;
5456 static struct pmu perf_tracepoint = {
5457 .task_ctx_nr = perf_sw_context,
5459 .event_init = perf_tp_event_init,
5460 .add = perf_trace_add,
5461 .del = perf_trace_del,
5462 .start = perf_swevent_start,
5463 .stop = perf_swevent_stop,
5464 .read = perf_swevent_read,
5467 static inline void perf_tp_register(void)
5469 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5472 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5477 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5480 filter_str = strndup_user(arg, PAGE_SIZE);
5481 if (IS_ERR(filter_str))
5482 return PTR_ERR(filter_str);
5484 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5490 static void perf_event_free_filter(struct perf_event *event)
5492 ftrace_profile_free_filter(event);
5497 static inline void perf_tp_register(void)
5501 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5506 static void perf_event_free_filter(struct perf_event *event)
5510 #endif /* CONFIG_EVENT_TRACING */
5512 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5513 void perf_bp_event(struct perf_event *bp, void *data)
5515 struct perf_sample_data sample;
5516 struct pt_regs *regs = data;
5518 perf_sample_data_init(&sample, bp->attr.bp_addr);
5520 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5521 perf_swevent_event(bp, 1, &sample, regs);
5526 * hrtimer based swevent callback
5529 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5531 enum hrtimer_restart ret = HRTIMER_RESTART;
5532 struct perf_sample_data data;
5533 struct pt_regs *regs;
5534 struct perf_event *event;
5537 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5539 if (event->state != PERF_EVENT_STATE_ACTIVE)
5540 return HRTIMER_NORESTART;
5542 event->pmu->read(event);
5544 perf_sample_data_init(&data, 0);
5545 data.period = event->hw.last_period;
5546 regs = get_irq_regs();
5548 if (regs && !perf_exclude_event(event, regs)) {
5549 if (!(event->attr.exclude_idle && current->pid == 0))
5550 if (perf_event_overflow(event, &data, regs))
5551 ret = HRTIMER_NORESTART;
5554 period = max_t(u64, 10000, event->hw.sample_period);
5555 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5560 static void perf_swevent_start_hrtimer(struct perf_event *event)
5562 struct hw_perf_event *hwc = &event->hw;
5565 if (!is_sampling_event(event))
5568 period = local64_read(&hwc->period_left);
5573 local64_set(&hwc->period_left, 0);
5575 period = max_t(u64, 10000, hwc->sample_period);
5577 __hrtimer_start_range_ns(&hwc->hrtimer,
5578 ns_to_ktime(period), 0,
5579 HRTIMER_MODE_REL_PINNED, 0);
5582 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5584 struct hw_perf_event *hwc = &event->hw;
5586 if (is_sampling_event(event)) {
5587 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5588 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5590 hrtimer_cancel(&hwc->hrtimer);
5594 static void perf_swevent_init_hrtimer(struct perf_event *event)
5596 struct hw_perf_event *hwc = &event->hw;
5598 if (!is_sampling_event(event))
5601 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5602 hwc->hrtimer.function = perf_swevent_hrtimer;
5605 * Since hrtimers have a fixed rate, we can do a static freq->period
5606 * mapping and avoid the whole period adjust feedback stuff.
5608 if (event->attr.freq) {
5609 long freq = event->attr.sample_freq;
5611 event->attr.sample_period = NSEC_PER_SEC / freq;
5612 hwc->sample_period = event->attr.sample_period;
5613 local64_set(&hwc->period_left, hwc->sample_period);
5614 event->attr.freq = 0;
5619 * Software event: cpu wall time clock
5622 static void cpu_clock_event_update(struct perf_event *event)
5627 now = local_clock();
5628 prev = local64_xchg(&event->hw.prev_count, now);
5629 local64_add(now - prev, &event->count);
5632 static void cpu_clock_event_start(struct perf_event *event, int flags)
5634 local64_set(&event->hw.prev_count, local_clock());
5635 perf_swevent_start_hrtimer(event);
5638 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5640 perf_swevent_cancel_hrtimer(event);
5641 cpu_clock_event_update(event);
5644 static int cpu_clock_event_add(struct perf_event *event, int flags)
5646 if (flags & PERF_EF_START)
5647 cpu_clock_event_start(event, flags);
5652 static void cpu_clock_event_del(struct perf_event *event, int flags)
5654 cpu_clock_event_stop(event, flags);
5657 static void cpu_clock_event_read(struct perf_event *event)
5659 cpu_clock_event_update(event);
5662 static int cpu_clock_event_init(struct perf_event *event)
5664 if (event->attr.type != PERF_TYPE_SOFTWARE)
5667 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5670 perf_swevent_init_hrtimer(event);
5675 static struct pmu perf_cpu_clock = {
5676 .task_ctx_nr = perf_sw_context,
5678 .event_init = cpu_clock_event_init,
5679 .add = cpu_clock_event_add,
5680 .del = cpu_clock_event_del,
5681 .start = cpu_clock_event_start,
5682 .stop = cpu_clock_event_stop,
5683 .read = cpu_clock_event_read,
5687 * Software event: task time clock
5690 static void task_clock_event_update(struct perf_event *event, u64 now)
5695 prev = local64_xchg(&event->hw.prev_count, now);
5697 local64_add(delta, &event->count);
5700 static void task_clock_event_start(struct perf_event *event, int flags)
5702 local64_set(&event->hw.prev_count, event->ctx->time);
5703 perf_swevent_start_hrtimer(event);
5706 static void task_clock_event_stop(struct perf_event *event, int flags)
5708 perf_swevent_cancel_hrtimer(event);
5709 task_clock_event_update(event, event->ctx->time);
5712 static int task_clock_event_add(struct perf_event *event, int flags)
5714 if (flags & PERF_EF_START)
5715 task_clock_event_start(event, flags);
5720 static void task_clock_event_del(struct perf_event *event, int flags)
5722 task_clock_event_stop(event, PERF_EF_UPDATE);
5725 static void task_clock_event_read(struct perf_event *event)
5727 u64 now = perf_clock();
5728 u64 delta = now - event->ctx->timestamp;
5729 u64 time = event->ctx->time + delta;
5731 task_clock_event_update(event, time);
5734 static int task_clock_event_init(struct perf_event *event)
5736 if (event->attr.type != PERF_TYPE_SOFTWARE)
5739 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5742 perf_swevent_init_hrtimer(event);
5747 static struct pmu perf_task_clock = {
5748 .task_ctx_nr = perf_sw_context,
5750 .event_init = task_clock_event_init,
5751 .add = task_clock_event_add,
5752 .del = task_clock_event_del,
5753 .start = task_clock_event_start,
5754 .stop = task_clock_event_stop,
5755 .read = task_clock_event_read,
5758 static void perf_pmu_nop_void(struct pmu *pmu)
5762 static int perf_pmu_nop_int(struct pmu *pmu)
5767 static void perf_pmu_start_txn(struct pmu *pmu)
5769 perf_pmu_disable(pmu);
5772 static int perf_pmu_commit_txn(struct pmu *pmu)
5774 perf_pmu_enable(pmu);
5778 static void perf_pmu_cancel_txn(struct pmu *pmu)
5780 perf_pmu_enable(pmu);
5784 * Ensures all contexts with the same task_ctx_nr have the same
5785 * pmu_cpu_context too.
5787 static void *find_pmu_context(int ctxn)
5794 list_for_each_entry(pmu, &pmus, entry) {
5795 if (pmu->task_ctx_nr == ctxn)
5796 return pmu->pmu_cpu_context;
5802 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5806 for_each_possible_cpu(cpu) {
5807 struct perf_cpu_context *cpuctx;
5809 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5811 if (cpuctx->unique_pmu == old_pmu)
5812 cpuctx->unique_pmu = pmu;
5816 static void free_pmu_context(struct pmu *pmu)
5820 mutex_lock(&pmus_lock);
5822 * Like a real lame refcount.
5824 list_for_each_entry(i, &pmus, entry) {
5825 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5826 update_pmu_context(i, pmu);
5831 free_percpu(pmu->pmu_cpu_context);
5833 mutex_unlock(&pmus_lock);
5835 static struct idr pmu_idr;
5838 type_show(struct device *dev, struct device_attribute *attr, char *page)
5840 struct pmu *pmu = dev_get_drvdata(dev);
5842 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5845 static struct device_attribute pmu_dev_attrs[] = {
5850 static int pmu_bus_running;
5851 static struct bus_type pmu_bus = {
5852 .name = "event_source",
5853 .dev_attrs = pmu_dev_attrs,
5856 static void pmu_dev_release(struct device *dev)
5861 static int pmu_dev_alloc(struct pmu *pmu)
5865 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5869 device_initialize(pmu->dev);
5870 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5874 dev_set_drvdata(pmu->dev, pmu);
5875 pmu->dev->bus = &pmu_bus;
5876 pmu->dev->release = pmu_dev_release;
5877 ret = device_add(pmu->dev);
5885 put_device(pmu->dev);
5889 static struct lock_class_key cpuctx_mutex;
5890 static struct lock_class_key cpuctx_lock;
5892 int perf_pmu_register(struct pmu *pmu, char *name, int type)
5896 mutex_lock(&pmus_lock);
5898 pmu->pmu_disable_count = alloc_percpu(int);
5899 if (!pmu->pmu_disable_count)
5908 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5912 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5920 if (pmu_bus_running) {
5921 ret = pmu_dev_alloc(pmu);
5927 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5928 if (pmu->pmu_cpu_context)
5929 goto got_cpu_context;
5932 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5933 if (!pmu->pmu_cpu_context)
5936 for_each_possible_cpu(cpu) {
5937 struct perf_cpu_context *cpuctx;
5939 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5940 __perf_event_init_context(&cpuctx->ctx);
5941 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
5942 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
5943 cpuctx->ctx.type = cpu_context;
5944 cpuctx->ctx.pmu = pmu;
5945 cpuctx->jiffies_interval = 1;
5946 INIT_LIST_HEAD(&cpuctx->rotation_list);
5947 cpuctx->unique_pmu = pmu;
5951 if (!pmu->start_txn) {
5952 if (pmu->pmu_enable) {
5954 * If we have pmu_enable/pmu_disable calls, install
5955 * transaction stubs that use that to try and batch
5956 * hardware accesses.
5958 pmu->start_txn = perf_pmu_start_txn;
5959 pmu->commit_txn = perf_pmu_commit_txn;
5960 pmu->cancel_txn = perf_pmu_cancel_txn;
5962 pmu->start_txn = perf_pmu_nop_void;
5963 pmu->commit_txn = perf_pmu_nop_int;
5964 pmu->cancel_txn = perf_pmu_nop_void;
5968 if (!pmu->pmu_enable) {
5969 pmu->pmu_enable = perf_pmu_nop_void;
5970 pmu->pmu_disable = perf_pmu_nop_void;
5973 list_add_rcu(&pmu->entry, &pmus);
5976 mutex_unlock(&pmus_lock);
5981 device_del(pmu->dev);
5982 put_device(pmu->dev);
5985 if (pmu->type >= PERF_TYPE_MAX)
5986 idr_remove(&pmu_idr, pmu->type);
5989 free_percpu(pmu->pmu_disable_count);
5993 void perf_pmu_unregister(struct pmu *pmu)
5995 mutex_lock(&pmus_lock);
5996 list_del_rcu(&pmu->entry);
5997 mutex_unlock(&pmus_lock);
6000 * We dereference the pmu list under both SRCU and regular RCU, so
6001 * synchronize against both of those.
6003 synchronize_srcu(&pmus_srcu);
6006 free_percpu(pmu->pmu_disable_count);
6007 if (pmu->type >= PERF_TYPE_MAX)
6008 idr_remove(&pmu_idr, pmu->type);
6009 device_del(pmu->dev);
6010 put_device(pmu->dev);
6011 free_pmu_context(pmu);
6014 struct pmu *perf_init_event(struct perf_event *event)
6016 struct pmu *pmu = NULL;
6020 idx = srcu_read_lock(&pmus_srcu);
6023 pmu = idr_find(&pmu_idr, event->attr.type);
6027 ret = pmu->event_init(event);
6033 list_for_each_entry_rcu(pmu, &pmus, entry) {
6035 ret = pmu->event_init(event);
6039 if (ret != -ENOENT) {
6044 pmu = ERR_PTR(-ENOENT);
6046 srcu_read_unlock(&pmus_srcu, idx);
6052 * Allocate and initialize a event structure
6054 static struct perf_event *
6055 perf_event_alloc(struct perf_event_attr *attr, int cpu,
6056 struct task_struct *task,
6057 struct perf_event *group_leader,
6058 struct perf_event *parent_event,
6059 perf_overflow_handler_t overflow_handler,
6063 struct perf_event *event;
6064 struct hw_perf_event *hwc;
6067 if ((unsigned)cpu >= nr_cpu_ids) {
6068 if (!task || cpu != -1)
6069 return ERR_PTR(-EINVAL);
6072 event = kzalloc(sizeof(*event), GFP_KERNEL);
6074 return ERR_PTR(-ENOMEM);
6077 * Single events are their own group leaders, with an
6078 * empty sibling list:
6081 group_leader = event;
6083 mutex_init(&event->child_mutex);
6084 INIT_LIST_HEAD(&event->child_list);
6086 INIT_LIST_HEAD(&event->group_entry);
6087 INIT_LIST_HEAD(&event->event_entry);
6088 INIT_LIST_HEAD(&event->sibling_list);
6089 INIT_LIST_HEAD(&event->rb_entry);
6091 init_waitqueue_head(&event->waitq);
6092 init_irq_work(&event->pending, perf_pending_event);
6094 mutex_init(&event->mmap_mutex);
6096 atomic_long_set(&event->refcount, 1);
6098 event->attr = *attr;
6099 event->group_leader = group_leader;
6103 event->parent = parent_event;
6105 event->ns = get_pid_ns(current->nsproxy->pid_ns);
6106 event->id = atomic64_inc_return(&perf_event_id);
6108 event->state = PERF_EVENT_STATE_INACTIVE;
6111 event->attach_state = PERF_ATTACH_TASK;
6112 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6114 * hw_breakpoint is a bit difficult here..
6116 if (attr->type == PERF_TYPE_BREAKPOINT)
6117 event->hw.bp_target = task;
6121 if (!overflow_handler && parent_event) {
6122 overflow_handler = parent_event->overflow_handler;
6123 context = parent_event->overflow_handler_context;
6126 event->overflow_handler = overflow_handler;
6127 event->overflow_handler_context = context;
6129 perf_event__state_init(event);
6134 hwc->sample_period = attr->sample_period;
6135 if (attr->freq && attr->sample_freq)
6136 hwc->sample_period = 1;
6137 hwc->last_period = hwc->sample_period;
6139 local64_set(&hwc->period_left, hwc->sample_period);
6142 * we currently do not support PERF_FORMAT_GROUP on inherited events
6144 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6147 pmu = perf_init_event(event);
6153 else if (IS_ERR(pmu))
6158 put_pid_ns(event->ns);
6160 return ERR_PTR(err);
6163 if (!event->parent) {
6164 if (event->attach_state & PERF_ATTACH_TASK)
6165 jump_label_inc(&perf_sched_events);
6166 if (event->attr.mmap || event->attr.mmap_data)
6167 atomic_inc(&nr_mmap_events);
6168 if (event->attr.comm)
6169 atomic_inc(&nr_comm_events);
6170 if (event->attr.task)
6171 atomic_inc(&nr_task_events);
6172 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6173 err = get_callchain_buffers();
6176 return ERR_PTR(err);
6184 static int perf_copy_attr(struct perf_event_attr __user *uattr,
6185 struct perf_event_attr *attr)
6190 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6194 * zero the full structure, so that a short copy will be nice.
6196 memset(attr, 0, sizeof(*attr));
6198 ret = get_user(size, &uattr->size);
6202 if (size > PAGE_SIZE) /* silly large */
6205 if (!size) /* abi compat */
6206 size = PERF_ATTR_SIZE_VER0;
6208 if (size < PERF_ATTR_SIZE_VER0)
6212 * If we're handed a bigger struct than we know of,
6213 * ensure all the unknown bits are 0 - i.e. new
6214 * user-space does not rely on any kernel feature
6215 * extensions we dont know about yet.
6217 if (size > sizeof(*attr)) {
6218 unsigned char __user *addr;
6219 unsigned char __user *end;
6222 addr = (void __user *)uattr + sizeof(*attr);
6223 end = (void __user *)uattr + size;
6225 for (; addr < end; addr++) {
6226 ret = get_user(val, addr);
6232 size = sizeof(*attr);
6235 ret = copy_from_user(attr, uattr, size);
6239 if (attr->__reserved_1)
6242 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6245 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6252 put_user(sizeof(*attr), &uattr->size);
6258 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6260 struct ring_buffer *rb = NULL, *old_rb = NULL;
6266 /* don't allow circular references */
6267 if (event == output_event)
6271 * Don't allow cross-cpu buffers
6273 if (output_event->cpu != event->cpu)
6277 * If its not a per-cpu rb, it must be the same task.
6279 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6283 mutex_lock(&event->mmap_mutex);
6284 /* Can't redirect output if we've got an active mmap() */
6285 if (atomic_read(&event->mmap_count))
6291 /* get the rb we want to redirect to */
6292 rb = ring_buffer_get(output_event);
6298 ring_buffer_detach(event, old_rb);
6301 ring_buffer_attach(event, rb);
6303 rcu_assign_pointer(event->rb, rb);
6306 ring_buffer_put(old_rb);
6308 * Since we detached before setting the new rb, so that we
6309 * could attach the new rb, we could have missed a wakeup.
6312 wake_up_all(&event->waitq);
6317 mutex_unlock(&event->mmap_mutex);
6324 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6326 * @attr_uptr: event_id type attributes for monitoring/sampling
6329 * @group_fd: group leader event fd
6331 SYSCALL_DEFINE5(perf_event_open,
6332 struct perf_event_attr __user *, attr_uptr,
6333 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6335 struct perf_event *group_leader = NULL, *output_event = NULL;
6336 struct perf_event *event, *sibling;
6337 struct perf_event_attr attr;
6338 struct perf_event_context *ctx;
6339 struct file *event_file = NULL;
6340 struct file *group_file = NULL;
6341 struct task_struct *task = NULL;
6345 int fput_needed = 0;
6348 /* for future expandability... */
6349 if (flags & ~PERF_FLAG_ALL)
6352 err = perf_copy_attr(attr_uptr, &attr);
6356 if (!attr.exclude_kernel) {
6357 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6362 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6365 if (attr.sample_period & (1ULL << 63))
6370 * In cgroup mode, the pid argument is used to pass the fd
6371 * opened to the cgroup directory in cgroupfs. The cpu argument
6372 * designates the cpu on which to monitor threads from that
6375 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6378 event_fd = get_unused_fd_flags(O_RDWR);
6382 if (group_fd != -1) {
6383 group_file = perf_fget_light(group_fd, &fput_needed);
6384 if (IS_ERR(group_file)) {
6385 err = PTR_ERR(group_file);
6388 group_leader = group_file->private_data;
6389 if (flags & PERF_FLAG_FD_OUTPUT)
6390 output_event = group_leader;
6391 if (flags & PERF_FLAG_FD_NO_GROUP)
6392 group_leader = NULL;
6395 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
6396 task = find_lively_task_by_vpid(pid);
6398 err = PTR_ERR(task);
6403 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
6405 if (IS_ERR(event)) {
6406 err = PTR_ERR(event);
6410 if (flags & PERF_FLAG_PID_CGROUP) {
6411 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6416 * - that has cgroup constraint on event->cpu
6417 * - that may need work on context switch
6419 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6420 jump_label_inc(&perf_sched_events);
6424 * Special case software events and allow them to be part of
6425 * any hardware group.
6430 (is_software_event(event) != is_software_event(group_leader))) {
6431 if (is_software_event(event)) {
6433 * If event and group_leader are not both a software
6434 * event, and event is, then group leader is not.
6436 * Allow the addition of software events to !software
6437 * groups, this is safe because software events never
6440 pmu = group_leader->pmu;
6441 } else if (is_software_event(group_leader) &&
6442 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6444 * In case the group is a pure software group, and we
6445 * try to add a hardware event, move the whole group to
6446 * the hardware context.
6453 * Get the target context (task or percpu):
6455 ctx = find_get_context(pmu, task, cpu);
6462 put_task_struct(task);
6467 * Look up the group leader (we will attach this event to it):
6473 * Do not allow a recursive hierarchy (this new sibling
6474 * becoming part of another group-sibling):
6476 if (group_leader->group_leader != group_leader)
6479 * Do not allow to attach to a group in a different
6480 * task or CPU context:
6483 if (group_leader->ctx->type != ctx->type)
6486 if (group_leader->ctx != ctx)
6491 * Only a group leader can be exclusive or pinned
6493 if (attr.exclusive || attr.pinned)
6498 err = perf_event_set_output(event, output_event);
6503 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6504 if (IS_ERR(event_file)) {
6505 err = PTR_ERR(event_file);
6510 struct perf_event_context *gctx = group_leader->ctx;
6512 mutex_lock(&gctx->mutex);
6513 perf_remove_from_context(group_leader, false);
6516 * Removing from the context ends up with disabled
6517 * event. What we want here is event in the initial
6518 * startup state, ready to be add into new context.
6520 perf_event__state_init(group_leader);
6521 list_for_each_entry(sibling, &group_leader->sibling_list,
6523 perf_remove_from_context(sibling, false);
6524 perf_event__state_init(sibling);
6527 mutex_unlock(&gctx->mutex);
6531 WARN_ON_ONCE(ctx->parent_ctx);
6532 mutex_lock(&ctx->mutex);
6535 perf_install_in_context(ctx, group_leader, cpu);
6537 list_for_each_entry(sibling, &group_leader->sibling_list,
6539 perf_install_in_context(ctx, sibling, cpu);
6544 perf_install_in_context(ctx, event, cpu);
6546 perf_unpin_context(ctx);
6547 mutex_unlock(&ctx->mutex);
6549 event->owner = current;
6551 mutex_lock(¤t->perf_event_mutex);
6552 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6553 mutex_unlock(¤t->perf_event_mutex);
6556 * Precalculate sample_data sizes
6558 perf_event__header_size(event);
6559 perf_event__id_header_size(event);
6562 * Drop the reference on the group_event after placing the
6563 * new event on the sibling_list. This ensures destruction
6564 * of the group leader will find the pointer to itself in
6565 * perf_group_detach().
6567 fput_light(group_file, fput_needed);
6568 fd_install(event_fd, event_file);
6572 perf_unpin_context(ctx);
6578 put_task_struct(task);
6580 fput_light(group_file, fput_needed);
6582 put_unused_fd(event_fd);
6587 * perf_event_create_kernel_counter
6589 * @attr: attributes of the counter to create
6590 * @cpu: cpu in which the counter is bound
6591 * @task: task to profile (NULL for percpu)
6594 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6595 struct task_struct *task,
6596 perf_overflow_handler_t overflow_handler,
6599 struct perf_event_context *ctx;
6600 struct perf_event *event;
6604 * Get the target context (task or percpu):
6607 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
6608 overflow_handler, context);
6609 if (IS_ERR(event)) {
6610 err = PTR_ERR(event);
6614 ctx = find_get_context(event->pmu, task, cpu);
6620 WARN_ON_ONCE(ctx->parent_ctx);
6621 mutex_lock(&ctx->mutex);
6622 perf_install_in_context(ctx, event, cpu);
6624 perf_unpin_context(ctx);
6625 mutex_unlock(&ctx->mutex);
6632 return ERR_PTR(err);
6634 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6636 static void sync_child_event(struct perf_event *child_event,
6637 struct task_struct *child)
6639 struct perf_event *parent_event = child_event->parent;
6642 if (child_event->attr.inherit_stat)
6643 perf_event_read_event(child_event, child);
6645 child_val = perf_event_count(child_event);
6648 * Add back the child's count to the parent's count:
6650 atomic64_add(child_val, &parent_event->child_count);
6651 atomic64_add(child_event->total_time_enabled,
6652 &parent_event->child_total_time_enabled);
6653 atomic64_add(child_event->total_time_running,
6654 &parent_event->child_total_time_running);
6657 * Remove this event from the parent's list
6659 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6660 mutex_lock(&parent_event->child_mutex);
6661 list_del_init(&child_event->child_list);
6662 mutex_unlock(&parent_event->child_mutex);
6665 * Release the parent event, if this was the last
6668 put_event(parent_event);
6672 __perf_event_exit_task(struct perf_event *child_event,
6673 struct perf_event_context *child_ctx,
6674 struct task_struct *child)
6676 perf_remove_from_context(child_event, !!child_event->parent);
6679 * It can happen that the parent exits first, and has events
6680 * that are still around due to the child reference. These
6681 * events need to be zapped.
6683 if (child_event->parent) {
6684 sync_child_event(child_event, child);
6685 free_event(child_event);
6689 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6691 struct perf_event *child_event, *tmp;
6692 struct perf_event_context *child_ctx;
6693 unsigned long flags;
6695 if (likely(!child->perf_event_ctxp[ctxn])) {
6696 perf_event_task(child, NULL, 0);
6700 local_irq_save(flags);
6702 * We can't reschedule here because interrupts are disabled,
6703 * and either child is current or it is a task that can't be
6704 * scheduled, so we are now safe from rescheduling changing
6707 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6710 * Take the context lock here so that if find_get_context is
6711 * reading child->perf_event_ctxp, we wait until it has
6712 * incremented the context's refcount before we do put_ctx below.
6714 raw_spin_lock(&child_ctx->lock);
6715 task_ctx_sched_out(child_ctx);
6716 child->perf_event_ctxp[ctxn] = NULL;
6718 * If this context is a clone; unclone it so it can't get
6719 * swapped to another process while we're removing all
6720 * the events from it.
6722 unclone_ctx(child_ctx);
6723 update_context_time(child_ctx);
6724 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6727 * Report the task dead after unscheduling the events so that we
6728 * won't get any samples after PERF_RECORD_EXIT. We can however still
6729 * get a few PERF_RECORD_READ events.
6731 perf_event_task(child, child_ctx, 0);
6734 * We can recurse on the same lock type through:
6736 * __perf_event_exit_task()
6737 * sync_child_event()
6739 * mutex_lock(&ctx->mutex)
6741 * But since its the parent context it won't be the same instance.
6743 mutex_lock(&child_ctx->mutex);
6746 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6748 __perf_event_exit_task(child_event, child_ctx, child);
6750 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6752 __perf_event_exit_task(child_event, child_ctx, child);
6755 * If the last event was a group event, it will have appended all
6756 * its siblings to the list, but we obtained 'tmp' before that which
6757 * will still point to the list head terminating the iteration.
6759 if (!list_empty(&child_ctx->pinned_groups) ||
6760 !list_empty(&child_ctx->flexible_groups))
6763 mutex_unlock(&child_ctx->mutex);
6769 * When a child task exits, feed back event values to parent events.
6771 void perf_event_exit_task(struct task_struct *child)
6773 struct perf_event *event, *tmp;
6776 mutex_lock(&child->perf_event_mutex);
6777 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6779 list_del_init(&event->owner_entry);
6782 * Ensure the list deletion is visible before we clear
6783 * the owner, closes a race against perf_release() where
6784 * we need to serialize on the owner->perf_event_mutex.
6787 event->owner = NULL;
6789 mutex_unlock(&child->perf_event_mutex);
6791 for_each_task_context_nr(ctxn)
6792 perf_event_exit_task_context(child, ctxn);
6795 static void perf_free_event(struct perf_event *event,
6796 struct perf_event_context *ctx)
6798 struct perf_event *parent = event->parent;
6800 if (WARN_ON_ONCE(!parent))
6803 mutex_lock(&parent->child_mutex);
6804 list_del_init(&event->child_list);
6805 mutex_unlock(&parent->child_mutex);
6809 perf_group_detach(event);
6810 list_del_event(event, ctx);
6815 * free an unexposed, unused context as created by inheritance by
6816 * perf_event_init_task below, used by fork() in case of fail.
6818 void perf_event_free_task(struct task_struct *task)
6820 struct perf_event_context *ctx;
6821 struct perf_event *event, *tmp;
6824 for_each_task_context_nr(ctxn) {
6825 ctx = task->perf_event_ctxp[ctxn];
6829 mutex_lock(&ctx->mutex);
6831 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6833 perf_free_event(event, ctx);
6835 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6837 perf_free_event(event, ctx);
6839 if (!list_empty(&ctx->pinned_groups) ||
6840 !list_empty(&ctx->flexible_groups))
6843 mutex_unlock(&ctx->mutex);
6849 void perf_event_delayed_put(struct task_struct *task)
6853 for_each_task_context_nr(ctxn)
6854 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6858 * inherit a event from parent task to child task:
6860 static struct perf_event *
6861 inherit_event(struct perf_event *parent_event,
6862 struct task_struct *parent,
6863 struct perf_event_context *parent_ctx,
6864 struct task_struct *child,
6865 struct perf_event *group_leader,
6866 struct perf_event_context *child_ctx)
6868 struct perf_event *child_event;
6869 unsigned long flags;
6872 * Instead of creating recursive hierarchies of events,
6873 * we link inherited events back to the original parent,
6874 * which has a filp for sure, which we use as the reference
6877 if (parent_event->parent)
6878 parent_event = parent_event->parent;
6880 child_event = perf_event_alloc(&parent_event->attr,
6883 group_leader, parent_event,
6885 if (IS_ERR(child_event))
6888 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
6889 free_event(child_event);
6896 * Make the child state follow the state of the parent event,
6897 * not its attr.disabled bit. We hold the parent's mutex,
6898 * so we won't race with perf_event_{en, dis}able_family.
6900 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6901 child_event->state = PERF_EVENT_STATE_INACTIVE;
6903 child_event->state = PERF_EVENT_STATE_OFF;
6905 if (parent_event->attr.freq) {
6906 u64 sample_period = parent_event->hw.sample_period;
6907 struct hw_perf_event *hwc = &child_event->hw;
6909 hwc->sample_period = sample_period;
6910 hwc->last_period = sample_period;
6912 local64_set(&hwc->period_left, sample_period);
6915 child_event->ctx = child_ctx;
6916 child_event->overflow_handler = parent_event->overflow_handler;
6917 child_event->overflow_handler_context
6918 = parent_event->overflow_handler_context;
6921 * Precalculate sample_data sizes
6923 perf_event__header_size(child_event);
6924 perf_event__id_header_size(child_event);
6927 * Link it up in the child's context:
6929 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6930 add_event_to_ctx(child_event, child_ctx);
6931 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6934 * Link this into the parent event's child list
6936 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6937 mutex_lock(&parent_event->child_mutex);
6938 list_add_tail(&child_event->child_list, &parent_event->child_list);
6939 mutex_unlock(&parent_event->child_mutex);
6944 static int inherit_group(struct perf_event *parent_event,
6945 struct task_struct *parent,
6946 struct perf_event_context *parent_ctx,
6947 struct task_struct *child,
6948 struct perf_event_context *child_ctx)
6950 struct perf_event *leader;
6951 struct perf_event *sub;
6952 struct perf_event *child_ctr;
6954 leader = inherit_event(parent_event, parent, parent_ctx,
6955 child, NULL, child_ctx);
6957 return PTR_ERR(leader);
6958 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6959 child_ctr = inherit_event(sub, parent, parent_ctx,
6960 child, leader, child_ctx);
6961 if (IS_ERR(child_ctr))
6962 return PTR_ERR(child_ctr);
6968 inherit_task_group(struct perf_event *event, struct task_struct *parent,
6969 struct perf_event_context *parent_ctx,
6970 struct task_struct *child, int ctxn,
6974 struct perf_event_context *child_ctx;
6976 if (!event->attr.inherit) {
6981 child_ctx = child->perf_event_ctxp[ctxn];
6984 * This is executed from the parent task context, so
6985 * inherit events that have been marked for cloning.
6986 * First allocate and initialize a context for the
6990 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
6994 child->perf_event_ctxp[ctxn] = child_ctx;
6997 ret = inherit_group(event, parent, parent_ctx,
7007 * Initialize the perf_event context in task_struct
7009 int perf_event_init_context(struct task_struct *child, int ctxn)
7011 struct perf_event_context *child_ctx, *parent_ctx;
7012 struct perf_event_context *cloned_ctx;
7013 struct perf_event *event;
7014 struct task_struct *parent = current;
7015 int inherited_all = 1;
7016 unsigned long flags;
7019 if (likely(!parent->perf_event_ctxp[ctxn]))
7023 * If the parent's context is a clone, pin it so it won't get
7026 parent_ctx = perf_pin_task_context(parent, ctxn);
7029 * No need to check if parent_ctx != NULL here; since we saw
7030 * it non-NULL earlier, the only reason for it to become NULL
7031 * is if we exit, and since we're currently in the middle of
7032 * a fork we can't be exiting at the same time.
7036 * Lock the parent list. No need to lock the child - not PID
7037 * hashed yet and not running, so nobody can access it.
7039 mutex_lock(&parent_ctx->mutex);
7042 * We dont have to disable NMIs - we are only looking at
7043 * the list, not manipulating it:
7045 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
7046 ret = inherit_task_group(event, parent, parent_ctx,
7047 child, ctxn, &inherited_all);
7053 * We can't hold ctx->lock when iterating the ->flexible_group list due
7054 * to allocations, but we need to prevent rotation because
7055 * rotate_ctx() will change the list from interrupt context.
7057 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7058 parent_ctx->rotate_disable = 1;
7059 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7061 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
7062 ret = inherit_task_group(event, parent, parent_ctx,
7063 child, ctxn, &inherited_all);
7068 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7069 parent_ctx->rotate_disable = 0;
7071 child_ctx = child->perf_event_ctxp[ctxn];
7073 if (child_ctx && inherited_all) {
7075 * Mark the child context as a clone of the parent
7076 * context, or of whatever the parent is a clone of.
7078 * Note that if the parent is a clone, the holding of
7079 * parent_ctx->lock avoids it from being uncloned.
7081 cloned_ctx = parent_ctx->parent_ctx;
7083 child_ctx->parent_ctx = cloned_ctx;
7084 child_ctx->parent_gen = parent_ctx->parent_gen;
7086 child_ctx->parent_ctx = parent_ctx;
7087 child_ctx->parent_gen = parent_ctx->generation;
7089 get_ctx(child_ctx->parent_ctx);
7092 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7093 mutex_unlock(&parent_ctx->mutex);
7095 perf_unpin_context(parent_ctx);
7096 put_ctx(parent_ctx);
7102 * Initialize the perf_event context in task_struct
7104 int perf_event_init_task(struct task_struct *child)
7108 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7109 mutex_init(&child->perf_event_mutex);
7110 INIT_LIST_HEAD(&child->perf_event_list);
7112 for_each_task_context_nr(ctxn) {
7113 ret = perf_event_init_context(child, ctxn);
7115 perf_event_free_task(child);
7123 static void __init perf_event_init_all_cpus(void)
7125 struct swevent_htable *swhash;
7128 for_each_possible_cpu(cpu) {
7129 swhash = &per_cpu(swevent_htable, cpu);
7130 mutex_init(&swhash->hlist_mutex);
7131 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
7135 static void __cpuinit perf_event_init_cpu(int cpu)
7137 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7139 mutex_lock(&swhash->hlist_mutex);
7140 swhash->online = true;
7141 if (swhash->hlist_refcount > 0) {
7142 struct swevent_hlist *hlist;
7144 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7146 rcu_assign_pointer(swhash->swevent_hlist, hlist);
7148 mutex_unlock(&swhash->hlist_mutex);
7151 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7152 static void perf_pmu_rotate_stop(struct pmu *pmu)
7154 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7156 WARN_ON(!irqs_disabled());
7158 list_del_init(&cpuctx->rotation_list);
7161 static void __perf_event_exit_context(void *__info)
7163 struct remove_event re = { .detach_group = false };
7164 struct perf_event_context *ctx = __info;
7166 perf_pmu_rotate_stop(ctx->pmu);
7169 list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry)
7170 __perf_remove_from_context(&re);
7174 static void perf_event_exit_cpu_context(int cpu)
7176 struct perf_event_context *ctx;
7180 idx = srcu_read_lock(&pmus_srcu);
7181 list_for_each_entry_rcu(pmu, &pmus, entry) {
7182 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
7184 mutex_lock(&ctx->mutex);
7185 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7186 mutex_unlock(&ctx->mutex);
7188 srcu_read_unlock(&pmus_srcu, idx);
7191 static void perf_event_exit_cpu(int cpu)
7193 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7195 perf_event_exit_cpu_context(cpu);
7197 mutex_lock(&swhash->hlist_mutex);
7198 swhash->online = false;
7199 swevent_hlist_release(swhash);
7200 mutex_unlock(&swhash->hlist_mutex);
7203 static inline void perf_event_exit_cpu(int cpu) { }
7207 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7211 for_each_online_cpu(cpu)
7212 perf_event_exit_cpu(cpu);
7218 * Run the perf reboot notifier at the very last possible moment so that
7219 * the generic watchdog code runs as long as possible.
7221 static struct notifier_block perf_reboot_notifier = {
7222 .notifier_call = perf_reboot,
7223 .priority = INT_MIN,
7226 static int __cpuinit
7227 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7229 unsigned int cpu = (long)hcpu;
7231 switch (action & ~CPU_TASKS_FROZEN) {
7233 case CPU_UP_PREPARE:
7234 case CPU_DOWN_FAILED:
7235 perf_event_init_cpu(cpu);
7238 case CPU_UP_CANCELED:
7239 case CPU_DOWN_PREPARE:
7240 perf_event_exit_cpu(cpu);
7250 void __init perf_event_init(void)
7256 perf_event_init_all_cpus();
7257 init_srcu_struct(&pmus_srcu);
7258 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7259 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7260 perf_pmu_register(&perf_task_clock, NULL, -1);
7262 perf_cpu_notifier(perf_cpu_notify);
7263 register_reboot_notifier(&perf_reboot_notifier);
7265 ret = init_hw_breakpoint();
7266 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7269 static int __init perf_event_sysfs_init(void)
7274 mutex_lock(&pmus_lock);
7276 ret = bus_register(&pmu_bus);
7280 list_for_each_entry(pmu, &pmus, entry) {
7281 if (!pmu->name || pmu->type < 0)
7284 ret = pmu_dev_alloc(pmu);
7285 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7287 pmu_bus_running = 1;
7291 mutex_unlock(&pmus_lock);
7295 device_initcall(perf_event_sysfs_init);
7297 #ifdef CONFIG_CGROUP_PERF
7298 static struct cgroup_subsys_state *perf_cgroup_create(
7299 struct cgroup_subsys *ss, struct cgroup *cont)
7301 struct perf_cgroup *jc;
7303 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
7305 return ERR_PTR(-ENOMEM);
7307 jc->info = alloc_percpu(struct perf_cgroup_info);
7310 return ERR_PTR(-ENOMEM);
7316 static void perf_cgroup_destroy(struct cgroup_subsys *ss,
7317 struct cgroup *cont)
7319 struct perf_cgroup *jc;
7320 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7321 struct perf_cgroup, css);
7322 free_percpu(jc->info);
7326 static int __perf_cgroup_move(void *info)
7328 struct task_struct *task = info;
7329 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7334 perf_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *task)
7336 task_function_call(task, __perf_cgroup_move, task);
7339 static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7340 struct cgroup *old_cgrp, struct task_struct *task)
7343 * cgroup_exit() is called in the copy_process() failure path.
7344 * Ignore this case since the task hasn't ran yet, this avoids
7345 * trying to poke a half freed task state from generic code.
7347 if (!(task->flags & PF_EXITING))
7350 perf_cgroup_attach_task(cgrp, task);
7353 struct cgroup_subsys perf_subsys = {
7354 .name = "perf_event",
7355 .subsys_id = perf_subsys_id,
7356 .create = perf_cgroup_create,
7357 .destroy = perf_cgroup_destroy,
7358 .exit = perf_cgroup_exit,
7359 .attach_task = perf_cgroup_attach_task,
7361 #endif /* CONFIG_CGROUP_PERF */