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 static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
3947 /* only the parent has fasync state */
3949 event = event->parent;
3950 return &event->fasync;
3953 void perf_event_wakeup(struct perf_event *event)
3955 ring_buffer_wakeup(event);
3957 if (event->pending_kill) {
3958 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
3959 event->pending_kill = 0;
3963 static void perf_pending_event(struct irq_work *entry)
3965 struct perf_event *event = container_of(entry,
3966 struct perf_event, pending);
3969 rctx = perf_swevent_get_recursion_context();
3971 * If we 'fail' here, that's OK, it means recursion is already disabled
3972 * and we won't recurse 'further'.
3975 if (event->pending_disable) {
3976 event->pending_disable = 0;
3977 __perf_event_disable(event);
3980 if (event->pending_wakeup) {
3981 event->pending_wakeup = 0;
3982 perf_event_wakeup(event);
3986 perf_swevent_put_recursion_context(rctx);
3990 * We assume there is only KVM supporting the callbacks.
3991 * Later on, we might change it to a list if there is
3992 * another virtualization implementation supporting the callbacks.
3994 struct perf_guest_info_callbacks *perf_guest_cbs;
3996 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3998 perf_guest_cbs = cbs;
4001 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4003 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4005 perf_guest_cbs = NULL;
4008 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4010 static void __perf_event_header__init_id(struct perf_event_header *header,
4011 struct perf_sample_data *data,
4012 struct perf_event *event)
4014 u64 sample_type = event->attr.sample_type;
4016 data->type = sample_type;
4017 header->size += event->id_header_size;
4019 if (sample_type & PERF_SAMPLE_TID) {
4020 /* namespace issues */
4021 data->tid_entry.pid = perf_event_pid(event, current);
4022 data->tid_entry.tid = perf_event_tid(event, current);
4025 if (sample_type & PERF_SAMPLE_TIME)
4026 data->time = perf_clock();
4028 if (sample_type & PERF_SAMPLE_ID)
4029 data->id = primary_event_id(event);
4031 if (sample_type & PERF_SAMPLE_STREAM_ID)
4032 data->stream_id = event->id;
4034 if (sample_type & PERF_SAMPLE_CPU) {
4035 data->cpu_entry.cpu = raw_smp_processor_id();
4036 data->cpu_entry.reserved = 0;
4040 void perf_event_header__init_id(struct perf_event_header *header,
4041 struct perf_sample_data *data,
4042 struct perf_event *event)
4044 if (event->attr.sample_id_all)
4045 __perf_event_header__init_id(header, data, event);
4048 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4049 struct perf_sample_data *data)
4051 u64 sample_type = data->type;
4053 if (sample_type & PERF_SAMPLE_TID)
4054 perf_output_put(handle, data->tid_entry);
4056 if (sample_type & PERF_SAMPLE_TIME)
4057 perf_output_put(handle, data->time);
4059 if (sample_type & PERF_SAMPLE_ID)
4060 perf_output_put(handle, data->id);
4062 if (sample_type & PERF_SAMPLE_STREAM_ID)
4063 perf_output_put(handle, data->stream_id);
4065 if (sample_type & PERF_SAMPLE_CPU)
4066 perf_output_put(handle, data->cpu_entry);
4069 void perf_event__output_id_sample(struct perf_event *event,
4070 struct perf_output_handle *handle,
4071 struct perf_sample_data *sample)
4073 if (event->attr.sample_id_all)
4074 __perf_event__output_id_sample(handle, sample);
4077 static void perf_output_read_one(struct perf_output_handle *handle,
4078 struct perf_event *event,
4079 u64 enabled, u64 running)
4081 u64 read_format = event->attr.read_format;
4085 values[n++] = perf_event_count(event);
4086 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4087 values[n++] = enabled +
4088 atomic64_read(&event->child_total_time_enabled);
4090 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4091 values[n++] = running +
4092 atomic64_read(&event->child_total_time_running);
4094 if (read_format & PERF_FORMAT_ID)
4095 values[n++] = primary_event_id(event);
4097 __output_copy(handle, values, n * sizeof(u64));
4101 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4103 static void perf_output_read_group(struct perf_output_handle *handle,
4104 struct perf_event *event,
4105 u64 enabled, u64 running)
4107 struct perf_event *leader = event->group_leader, *sub;
4108 u64 read_format = event->attr.read_format;
4112 values[n++] = 1 + leader->nr_siblings;
4114 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4115 values[n++] = enabled;
4117 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4118 values[n++] = running;
4120 if (leader != event)
4121 leader->pmu->read(leader);
4123 values[n++] = perf_event_count(leader);
4124 if (read_format & PERF_FORMAT_ID)
4125 values[n++] = primary_event_id(leader);
4127 __output_copy(handle, values, n * sizeof(u64));
4129 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4133 sub->pmu->read(sub);
4135 values[n++] = perf_event_count(sub);
4136 if (read_format & PERF_FORMAT_ID)
4137 values[n++] = primary_event_id(sub);
4139 __output_copy(handle, values, n * sizeof(u64));
4143 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4144 PERF_FORMAT_TOTAL_TIME_RUNNING)
4146 static void perf_output_read(struct perf_output_handle *handle,
4147 struct perf_event *event)
4149 u64 enabled = 0, running = 0;
4150 u64 read_format = event->attr.read_format;
4153 * compute total_time_enabled, total_time_running
4154 * based on snapshot values taken when the event
4155 * was last scheduled in.
4157 * we cannot simply called update_context_time()
4158 * because of locking issue as we are called in
4161 if (read_format & PERF_FORMAT_TOTAL_TIMES)
4162 calc_timer_values(event, &enabled, &running);
4164 if (event->attr.read_format & PERF_FORMAT_GROUP)
4165 perf_output_read_group(handle, event, enabled, running);
4167 perf_output_read_one(handle, event, enabled, running);
4170 void perf_output_sample(struct perf_output_handle *handle,
4171 struct perf_event_header *header,
4172 struct perf_sample_data *data,
4173 struct perf_event *event)
4175 u64 sample_type = data->type;
4177 perf_output_put(handle, *header);
4179 if (sample_type & PERF_SAMPLE_IP)
4180 perf_output_put(handle, data->ip);
4182 if (sample_type & PERF_SAMPLE_TID)
4183 perf_output_put(handle, data->tid_entry);
4185 if (sample_type & PERF_SAMPLE_TIME)
4186 perf_output_put(handle, data->time);
4188 if (sample_type & PERF_SAMPLE_ADDR)
4189 perf_output_put(handle, data->addr);
4191 if (sample_type & PERF_SAMPLE_ID)
4192 perf_output_put(handle, data->id);
4194 if (sample_type & PERF_SAMPLE_STREAM_ID)
4195 perf_output_put(handle, data->stream_id);
4197 if (sample_type & PERF_SAMPLE_CPU)
4198 perf_output_put(handle, data->cpu_entry);
4200 if (sample_type & PERF_SAMPLE_PERIOD)
4201 perf_output_put(handle, data->period);
4203 if (sample_type & PERF_SAMPLE_READ)
4204 perf_output_read(handle, event);
4206 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4207 if (data->callchain) {
4210 if (data->callchain)
4211 size += data->callchain->nr;
4213 size *= sizeof(u64);
4215 __output_copy(handle, data->callchain, size);
4218 perf_output_put(handle, nr);
4222 if (sample_type & PERF_SAMPLE_RAW) {
4224 perf_output_put(handle, data->raw->size);
4225 __output_copy(handle, data->raw->data,
4232 .size = sizeof(u32),
4235 perf_output_put(handle, raw);
4239 if (!event->attr.watermark) {
4240 int wakeup_events = event->attr.wakeup_events;
4242 if (wakeup_events) {
4243 struct ring_buffer *rb = handle->rb;
4244 int events = local_inc_return(&rb->events);
4246 if (events >= wakeup_events) {
4247 local_sub(wakeup_events, &rb->events);
4248 local_inc(&rb->wakeup);
4254 void perf_prepare_sample(struct perf_event_header *header,
4255 struct perf_sample_data *data,
4256 struct perf_event *event,
4257 struct pt_regs *regs)
4259 u64 sample_type = event->attr.sample_type;
4261 header->type = PERF_RECORD_SAMPLE;
4262 header->size = sizeof(*header) + event->header_size;
4265 header->misc |= perf_misc_flags(regs);
4267 __perf_event_header__init_id(header, data, event);
4269 if (sample_type & PERF_SAMPLE_IP)
4270 data->ip = perf_instruction_pointer(regs);
4272 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4275 data->callchain = perf_callchain(regs);
4277 if (data->callchain)
4278 size += data->callchain->nr;
4280 header->size += size * sizeof(u64);
4283 if (sample_type & PERF_SAMPLE_RAW) {
4284 int size = sizeof(u32);
4287 size += data->raw->size;
4289 size += sizeof(u32);
4291 WARN_ON_ONCE(size & (sizeof(u64)-1));
4292 header->size += size;
4296 static void perf_event_output(struct perf_event *event,
4297 struct perf_sample_data *data,
4298 struct pt_regs *regs)
4300 struct perf_output_handle handle;
4301 struct perf_event_header header;
4303 /* protect the callchain buffers */
4306 perf_prepare_sample(&header, data, event, regs);
4308 if (perf_output_begin(&handle, event, header.size))
4311 perf_output_sample(&handle, &header, data, event);
4313 perf_output_end(&handle);
4323 struct perf_read_event {
4324 struct perf_event_header header;
4331 perf_event_read_event(struct perf_event *event,
4332 struct task_struct *task)
4334 struct perf_output_handle handle;
4335 struct perf_sample_data sample;
4336 struct perf_read_event read_event = {
4338 .type = PERF_RECORD_READ,
4340 .size = sizeof(read_event) + event->read_size,
4342 .pid = perf_event_pid(event, task),
4343 .tid = perf_event_tid(event, task),
4347 perf_event_header__init_id(&read_event.header, &sample, event);
4348 ret = perf_output_begin(&handle, event, read_event.header.size);
4352 perf_output_put(&handle, read_event);
4353 perf_output_read(&handle, event);
4354 perf_event__output_id_sample(event, &handle, &sample);
4356 perf_output_end(&handle);
4360 * task tracking -- fork/exit
4362 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4365 struct perf_task_event {
4366 struct task_struct *task;
4367 struct perf_event_context *task_ctx;
4370 struct perf_event_header header;
4380 static void perf_event_task_output(struct perf_event *event,
4381 struct perf_task_event *task_event)
4383 struct perf_output_handle handle;
4384 struct perf_sample_data sample;
4385 struct task_struct *task = task_event->task;
4386 int ret, size = task_event->event_id.header.size;
4388 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4390 ret = perf_output_begin(&handle, event,
4391 task_event->event_id.header.size);
4395 task_event->event_id.pid = perf_event_pid(event, task);
4396 task_event->event_id.ppid = perf_event_pid(event, current);
4398 task_event->event_id.tid = perf_event_tid(event, task);
4399 task_event->event_id.ptid = perf_event_tid(event, current);
4401 perf_output_put(&handle, task_event->event_id);
4403 perf_event__output_id_sample(event, &handle, &sample);
4405 perf_output_end(&handle);
4407 task_event->event_id.header.size = size;
4410 static int perf_event_task_match(struct perf_event *event)
4412 if (event->state < PERF_EVENT_STATE_INACTIVE)
4415 if (!event_filter_match(event))
4418 if (event->attr.comm || event->attr.mmap ||
4419 event->attr.mmap_data || event->attr.task)
4425 static void perf_event_task_ctx(struct perf_event_context *ctx,
4426 struct perf_task_event *task_event)
4428 struct perf_event *event;
4430 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4431 if (perf_event_task_match(event))
4432 perf_event_task_output(event, task_event);
4436 static void perf_event_task_event(struct perf_task_event *task_event)
4438 struct perf_cpu_context *cpuctx;
4439 struct perf_event_context *ctx;
4444 list_for_each_entry_rcu(pmu, &pmus, entry) {
4445 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4446 if (cpuctx->unique_pmu != pmu)
4448 perf_event_task_ctx(&cpuctx->ctx, task_event);
4450 ctx = task_event->task_ctx;
4452 ctxn = pmu->task_ctx_nr;
4455 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4458 perf_event_task_ctx(ctx, task_event);
4460 put_cpu_ptr(pmu->pmu_cpu_context);
4465 static void perf_event_task(struct task_struct *task,
4466 struct perf_event_context *task_ctx,
4469 struct perf_task_event task_event;
4471 if (!atomic_read(&nr_comm_events) &&
4472 !atomic_read(&nr_mmap_events) &&
4473 !atomic_read(&nr_task_events))
4476 task_event = (struct perf_task_event){
4478 .task_ctx = task_ctx,
4481 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4483 .size = sizeof(task_event.event_id),
4489 .time = perf_clock(),
4493 perf_event_task_event(&task_event);
4496 void perf_event_fork(struct task_struct *task)
4498 perf_event_task(task, NULL, 1);
4505 struct perf_comm_event {
4506 struct task_struct *task;
4511 struct perf_event_header header;
4518 static void perf_event_comm_output(struct perf_event *event,
4519 struct perf_comm_event *comm_event)
4521 struct perf_output_handle handle;
4522 struct perf_sample_data sample;
4523 int size = comm_event->event_id.header.size;
4526 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4527 ret = perf_output_begin(&handle, event,
4528 comm_event->event_id.header.size);
4533 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4534 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4536 perf_output_put(&handle, comm_event->event_id);
4537 __output_copy(&handle, comm_event->comm,
4538 comm_event->comm_size);
4540 perf_event__output_id_sample(event, &handle, &sample);
4542 perf_output_end(&handle);
4544 comm_event->event_id.header.size = size;
4547 static int perf_event_comm_match(struct perf_event *event)
4549 if (event->state < PERF_EVENT_STATE_INACTIVE)
4552 if (!event_filter_match(event))
4555 if (event->attr.comm)
4561 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4562 struct perf_comm_event *comm_event)
4564 struct perf_event *event;
4566 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4567 if (perf_event_comm_match(event))
4568 perf_event_comm_output(event, comm_event);
4572 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4574 struct perf_cpu_context *cpuctx;
4575 struct perf_event_context *ctx;
4576 char comm[TASK_COMM_LEN];
4581 memset(comm, 0, sizeof(comm));
4582 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4583 size = ALIGN(strlen(comm)+1, sizeof(u64));
4585 comm_event->comm = comm;
4586 comm_event->comm_size = size;
4588 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4590 list_for_each_entry_rcu(pmu, &pmus, entry) {
4591 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4592 if (cpuctx->unique_pmu != pmu)
4594 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4596 ctxn = pmu->task_ctx_nr;
4600 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4602 perf_event_comm_ctx(ctx, comm_event);
4604 put_cpu_ptr(pmu->pmu_cpu_context);
4609 void perf_event_comm(struct task_struct *task)
4611 struct perf_comm_event comm_event;
4612 struct perf_event_context *ctx;
4615 for_each_task_context_nr(ctxn) {
4616 ctx = task->perf_event_ctxp[ctxn];
4620 perf_event_enable_on_exec(ctx);
4623 if (!atomic_read(&nr_comm_events))
4626 comm_event = (struct perf_comm_event){
4632 .type = PERF_RECORD_COMM,
4641 perf_event_comm_event(&comm_event);
4648 struct perf_mmap_event {
4649 struct vm_area_struct *vma;
4651 const char *file_name;
4655 struct perf_event_header header;
4665 static void perf_event_mmap_output(struct perf_event *event,
4666 struct perf_mmap_event *mmap_event)
4668 struct perf_output_handle handle;
4669 struct perf_sample_data sample;
4670 int size = mmap_event->event_id.header.size;
4673 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4674 ret = perf_output_begin(&handle, event,
4675 mmap_event->event_id.header.size);
4679 mmap_event->event_id.pid = perf_event_pid(event, current);
4680 mmap_event->event_id.tid = perf_event_tid(event, current);
4682 perf_output_put(&handle, mmap_event->event_id);
4683 __output_copy(&handle, mmap_event->file_name,
4684 mmap_event->file_size);
4686 perf_event__output_id_sample(event, &handle, &sample);
4688 perf_output_end(&handle);
4690 mmap_event->event_id.header.size = size;
4693 static int perf_event_mmap_match(struct perf_event *event,
4694 struct perf_mmap_event *mmap_event,
4697 if (event->state < PERF_EVENT_STATE_INACTIVE)
4700 if (!event_filter_match(event))
4703 if ((!executable && event->attr.mmap_data) ||
4704 (executable && event->attr.mmap))
4710 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4711 struct perf_mmap_event *mmap_event,
4714 struct perf_event *event;
4716 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4717 if (perf_event_mmap_match(event, mmap_event, executable))
4718 perf_event_mmap_output(event, mmap_event);
4722 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4724 struct perf_cpu_context *cpuctx;
4725 struct perf_event_context *ctx;
4726 struct vm_area_struct *vma = mmap_event->vma;
4727 struct file *file = vma->vm_file;
4735 memset(tmp, 0, sizeof(tmp));
4739 * d_path works from the end of the rb backwards, so we
4740 * need to add enough zero bytes after the string to handle
4741 * the 64bit alignment we do later.
4743 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4745 name = strncpy(tmp, "//enomem", sizeof(tmp));
4748 name = d_path(&file->f_path, buf, PATH_MAX);
4750 name = strncpy(tmp, "//toolong", sizeof(tmp));
4754 if (arch_vma_name(mmap_event->vma)) {
4755 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4761 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4763 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4764 vma->vm_end >= vma->vm_mm->brk) {
4765 name = strncpy(tmp, "[heap]", sizeof(tmp));
4767 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4768 vma->vm_end >= vma->vm_mm->start_stack) {
4769 name = strncpy(tmp, "[stack]", sizeof(tmp));
4773 name = strncpy(tmp, "//anon", sizeof(tmp));
4778 size = ALIGN(strlen(name)+1, sizeof(u64));
4780 mmap_event->file_name = name;
4781 mmap_event->file_size = size;
4783 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4786 list_for_each_entry_rcu(pmu, &pmus, entry) {
4787 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4788 if (cpuctx->unique_pmu != pmu)
4790 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4791 vma->vm_flags & VM_EXEC);
4793 ctxn = pmu->task_ctx_nr;
4797 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4799 perf_event_mmap_ctx(ctx, mmap_event,
4800 vma->vm_flags & VM_EXEC);
4803 put_cpu_ptr(pmu->pmu_cpu_context);
4810 void perf_event_mmap(struct vm_area_struct *vma)
4812 struct perf_mmap_event mmap_event;
4814 if (!atomic_read(&nr_mmap_events))
4817 mmap_event = (struct perf_mmap_event){
4823 .type = PERF_RECORD_MMAP,
4824 .misc = PERF_RECORD_MISC_USER,
4829 .start = vma->vm_start,
4830 .len = vma->vm_end - vma->vm_start,
4831 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4835 perf_event_mmap_event(&mmap_event);
4839 * IRQ throttle logging
4842 static void perf_log_throttle(struct perf_event *event, int enable)
4844 struct perf_output_handle handle;
4845 struct perf_sample_data sample;
4849 struct perf_event_header header;
4853 } throttle_event = {
4855 .type = PERF_RECORD_THROTTLE,
4857 .size = sizeof(throttle_event),
4859 .time = perf_clock(),
4860 .id = primary_event_id(event),
4861 .stream_id = event->id,
4865 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4867 perf_event_header__init_id(&throttle_event.header, &sample, event);
4869 ret = perf_output_begin(&handle, event,
4870 throttle_event.header.size);
4874 perf_output_put(&handle, throttle_event);
4875 perf_event__output_id_sample(event, &handle, &sample);
4876 perf_output_end(&handle);
4880 * Generic event overflow handling, sampling.
4883 static int __perf_event_overflow(struct perf_event *event,
4884 int throttle, struct perf_sample_data *data,
4885 struct pt_regs *regs)
4887 int events = atomic_read(&event->event_limit);
4888 struct hw_perf_event *hwc = &event->hw;
4892 * Non-sampling counters might still use the PMI to fold short
4893 * hardware counters, ignore those.
4895 if (unlikely(!is_sampling_event(event)))
4898 if (unlikely(hwc->interrupts >= max_samples_per_tick)) {
4900 hwc->interrupts = MAX_INTERRUPTS;
4901 perf_log_throttle(event, 0);
4907 if (event->attr.freq) {
4908 u64 now = perf_clock();
4909 s64 delta = now - hwc->freq_time_stamp;
4911 hwc->freq_time_stamp = now;
4913 if (delta > 0 && delta < 2*TICK_NSEC)
4914 perf_adjust_period(event, delta, hwc->last_period);
4918 * XXX event_limit might not quite work as expected on inherited
4922 event->pending_kill = POLL_IN;
4923 if (events && atomic_dec_and_test(&event->event_limit)) {
4925 event->pending_kill = POLL_HUP;
4926 event->pending_disable = 1;
4927 irq_work_queue(&event->pending);
4930 if (event->overflow_handler)
4931 event->overflow_handler(event, data, regs);
4933 perf_event_output(event, data, regs);
4935 if (*perf_event_fasync(event) && event->pending_kill) {
4936 event->pending_wakeup = 1;
4937 irq_work_queue(&event->pending);
4943 int perf_event_overflow(struct perf_event *event,
4944 struct perf_sample_data *data,
4945 struct pt_regs *regs)
4947 return __perf_event_overflow(event, 1, data, regs);
4951 * Generic software event infrastructure
4954 struct swevent_htable {
4955 struct swevent_hlist *swevent_hlist;
4956 struct mutex hlist_mutex;
4959 /* Recursion avoidance in each contexts */
4960 int recursion[PERF_NR_CONTEXTS];
4962 /* Keeps track of cpu being initialized/exited */
4966 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4969 * We directly increment event->count and keep a second value in
4970 * event->hw.period_left to count intervals. This period event
4971 * is kept in the range [-sample_period, 0] so that we can use the
4975 static u64 perf_swevent_set_period(struct perf_event *event)
4977 struct hw_perf_event *hwc = &event->hw;
4978 u64 period = hwc->last_period;
4982 hwc->last_period = hwc->sample_period;
4985 old = val = local64_read(&hwc->period_left);
4989 nr = div64_u64(period + val, period);
4990 offset = nr * period;
4992 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4998 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4999 struct perf_sample_data *data,
5000 struct pt_regs *regs)
5002 struct hw_perf_event *hwc = &event->hw;
5005 data->period = event->hw.last_period;
5007 overflow = perf_swevent_set_period(event);
5009 if (hwc->interrupts == MAX_INTERRUPTS)
5012 for (; overflow; overflow--) {
5013 if (__perf_event_overflow(event, throttle,
5016 * We inhibit the overflow from happening when
5017 * hwc->interrupts == MAX_INTERRUPTS.
5025 static void perf_swevent_event(struct perf_event *event, u64 nr,
5026 struct perf_sample_data *data,
5027 struct pt_regs *regs)
5029 struct hw_perf_event *hwc = &event->hw;
5031 local64_add(nr, &event->count);
5036 if (!is_sampling_event(event))
5039 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
5040 return perf_swevent_overflow(event, 1, data, regs);
5042 if (local64_add_negative(nr, &hwc->period_left))
5045 perf_swevent_overflow(event, 0, data, regs);
5048 static int perf_exclude_event(struct perf_event *event,
5049 struct pt_regs *regs)
5051 if (event->hw.state & PERF_HES_STOPPED)
5055 if (event->attr.exclude_user && user_mode(regs))
5058 if (event->attr.exclude_kernel && !user_mode(regs))
5065 static int perf_swevent_match(struct perf_event *event,
5066 enum perf_type_id type,
5068 struct perf_sample_data *data,
5069 struct pt_regs *regs)
5071 if (event->attr.type != type)
5074 if (event->attr.config != event_id)
5077 if (perf_exclude_event(event, regs))
5083 static inline u64 swevent_hash(u64 type, u32 event_id)
5085 u64 val = event_id | (type << 32);
5087 return hash_64(val, SWEVENT_HLIST_BITS);
5090 static inline struct hlist_head *
5091 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
5093 u64 hash = swevent_hash(type, event_id);
5095 return &hlist->heads[hash];
5098 /* For the read side: events when they trigger */
5099 static inline struct hlist_head *
5100 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
5102 struct swevent_hlist *hlist;
5104 hlist = rcu_dereference(swhash->swevent_hlist);
5108 return __find_swevent_head(hlist, type, event_id);
5111 /* For the event head insertion and removal in the hlist */
5112 static inline struct hlist_head *
5113 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
5115 struct swevent_hlist *hlist;
5116 u32 event_id = event->attr.config;
5117 u64 type = event->attr.type;
5120 * Event scheduling is always serialized against hlist allocation
5121 * and release. Which makes the protected version suitable here.
5122 * The context lock guarantees that.
5124 hlist = rcu_dereference_protected(swhash->swevent_hlist,
5125 lockdep_is_held(&event->ctx->lock));
5129 return __find_swevent_head(hlist, type, event_id);
5132 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5134 struct perf_sample_data *data,
5135 struct pt_regs *regs)
5137 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5138 struct perf_event *event;
5139 struct hlist_node *node;
5140 struct hlist_head *head;
5143 head = find_swevent_head_rcu(swhash, type, event_id);
5147 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5148 if (perf_swevent_match(event, type, event_id, data, regs))
5149 perf_swevent_event(event, nr, data, regs);
5155 int perf_swevent_get_recursion_context(void)
5157 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5159 return get_recursion_context(swhash->recursion);
5161 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
5163 inline void perf_swevent_put_recursion_context(int rctx)
5165 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5167 put_recursion_context(swhash->recursion, rctx);
5170 void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
5172 struct perf_sample_data data;
5175 preempt_disable_notrace();
5176 rctx = perf_swevent_get_recursion_context();
5180 perf_sample_data_init(&data, addr);
5182 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
5184 perf_swevent_put_recursion_context(rctx);
5185 preempt_enable_notrace();
5188 static void perf_swevent_read(struct perf_event *event)
5192 static int perf_swevent_add(struct perf_event *event, int flags)
5194 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5195 struct hw_perf_event *hwc = &event->hw;
5196 struct hlist_head *head;
5198 if (is_sampling_event(event)) {
5199 hwc->last_period = hwc->sample_period;
5200 perf_swevent_set_period(event);
5203 hwc->state = !(flags & PERF_EF_START);
5205 head = find_swevent_head(swhash, event);
5208 * We can race with cpu hotplug code. Do not
5209 * WARN if the cpu just got unplugged.
5211 WARN_ON_ONCE(swhash->online);
5215 hlist_add_head_rcu(&event->hlist_entry, head);
5220 static void perf_swevent_del(struct perf_event *event, int flags)
5222 hlist_del_rcu(&event->hlist_entry);
5225 static void perf_swevent_start(struct perf_event *event, int flags)
5227 event->hw.state = 0;
5230 static void perf_swevent_stop(struct perf_event *event, int flags)
5232 event->hw.state = PERF_HES_STOPPED;
5235 /* Deref the hlist from the update side */
5236 static inline struct swevent_hlist *
5237 swevent_hlist_deref(struct swevent_htable *swhash)
5239 return rcu_dereference_protected(swhash->swevent_hlist,
5240 lockdep_is_held(&swhash->hlist_mutex));
5243 static void swevent_hlist_release(struct swevent_htable *swhash)
5245 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
5250 rcu_assign_pointer(swhash->swevent_hlist, NULL);
5251 kfree_rcu(hlist, rcu_head);
5254 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5256 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5258 mutex_lock(&swhash->hlist_mutex);
5260 if (!--swhash->hlist_refcount)
5261 swevent_hlist_release(swhash);
5263 mutex_unlock(&swhash->hlist_mutex);
5266 static void swevent_hlist_put(struct perf_event *event)
5270 if (event->cpu != -1) {
5271 swevent_hlist_put_cpu(event, event->cpu);
5275 for_each_possible_cpu(cpu)
5276 swevent_hlist_put_cpu(event, cpu);
5279 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5281 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5284 mutex_lock(&swhash->hlist_mutex);
5286 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
5287 struct swevent_hlist *hlist;
5289 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5294 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5296 swhash->hlist_refcount++;
5298 mutex_unlock(&swhash->hlist_mutex);
5303 static int swevent_hlist_get(struct perf_event *event)
5306 int cpu, failed_cpu;
5308 if (event->cpu != -1)
5309 return swevent_hlist_get_cpu(event, event->cpu);
5312 for_each_possible_cpu(cpu) {
5313 err = swevent_hlist_get_cpu(event, cpu);
5323 for_each_possible_cpu(cpu) {
5324 if (cpu == failed_cpu)
5326 swevent_hlist_put_cpu(event, cpu);
5333 struct jump_label_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
5335 static void sw_perf_event_destroy(struct perf_event *event)
5337 u64 event_id = event->attr.config;
5339 WARN_ON(event->parent);
5341 jump_label_dec(&perf_swevent_enabled[event_id]);
5342 swevent_hlist_put(event);
5345 static int perf_swevent_init(struct perf_event *event)
5347 u64 event_id = event->attr.config;
5349 if (event->attr.type != PERF_TYPE_SOFTWARE)
5353 case PERF_COUNT_SW_CPU_CLOCK:
5354 case PERF_COUNT_SW_TASK_CLOCK:
5361 if (event_id >= PERF_COUNT_SW_MAX)
5364 if (!event->parent) {
5367 err = swevent_hlist_get(event);
5371 jump_label_inc(&perf_swevent_enabled[event_id]);
5372 event->destroy = sw_perf_event_destroy;
5378 static struct pmu perf_swevent = {
5379 .task_ctx_nr = perf_sw_context,
5381 .event_init = perf_swevent_init,
5382 .add = perf_swevent_add,
5383 .del = perf_swevent_del,
5384 .start = perf_swevent_start,
5385 .stop = perf_swevent_stop,
5386 .read = perf_swevent_read,
5389 #ifdef CONFIG_EVENT_TRACING
5391 static int perf_tp_filter_match(struct perf_event *event,
5392 struct perf_sample_data *data)
5394 void *record = data->raw->data;
5396 /* only top level events have filters set */
5398 event = event->parent;
5400 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5405 static int perf_tp_event_match(struct perf_event *event,
5406 struct perf_sample_data *data,
5407 struct pt_regs *regs)
5409 if (event->hw.state & PERF_HES_STOPPED)
5412 * All tracepoints are from kernel-space.
5414 if (event->attr.exclude_kernel)
5417 if (!perf_tp_filter_match(event, data))
5423 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5424 struct pt_regs *regs, struct hlist_head *head, int rctx)
5426 struct perf_sample_data data;
5427 struct perf_event *event;
5428 struct hlist_node *node;
5430 struct perf_raw_record raw = {
5435 perf_sample_data_init(&data, addr);
5438 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5439 if (perf_tp_event_match(event, &data, regs))
5440 perf_swevent_event(event, count, &data, regs);
5443 perf_swevent_put_recursion_context(rctx);
5445 EXPORT_SYMBOL_GPL(perf_tp_event);
5447 static void tp_perf_event_destroy(struct perf_event *event)
5449 perf_trace_destroy(event);
5452 static int perf_tp_event_init(struct perf_event *event)
5456 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5459 err = perf_trace_init(event);
5463 event->destroy = tp_perf_event_destroy;
5468 static struct pmu perf_tracepoint = {
5469 .task_ctx_nr = perf_sw_context,
5471 .event_init = perf_tp_event_init,
5472 .add = perf_trace_add,
5473 .del = perf_trace_del,
5474 .start = perf_swevent_start,
5475 .stop = perf_swevent_stop,
5476 .read = perf_swevent_read,
5479 static inline void perf_tp_register(void)
5481 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5484 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5489 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5492 filter_str = strndup_user(arg, PAGE_SIZE);
5493 if (IS_ERR(filter_str))
5494 return PTR_ERR(filter_str);
5496 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5502 static void perf_event_free_filter(struct perf_event *event)
5504 ftrace_profile_free_filter(event);
5509 static inline void perf_tp_register(void)
5513 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5518 static void perf_event_free_filter(struct perf_event *event)
5522 #endif /* CONFIG_EVENT_TRACING */
5524 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5525 void perf_bp_event(struct perf_event *bp, void *data)
5527 struct perf_sample_data sample;
5528 struct pt_regs *regs = data;
5530 perf_sample_data_init(&sample, bp->attr.bp_addr);
5532 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5533 perf_swevent_event(bp, 1, &sample, regs);
5538 * hrtimer based swevent callback
5541 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5543 enum hrtimer_restart ret = HRTIMER_RESTART;
5544 struct perf_sample_data data;
5545 struct pt_regs *regs;
5546 struct perf_event *event;
5549 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5551 if (event->state != PERF_EVENT_STATE_ACTIVE)
5552 return HRTIMER_NORESTART;
5554 event->pmu->read(event);
5556 perf_sample_data_init(&data, 0);
5557 data.period = event->hw.last_period;
5558 regs = get_irq_regs();
5560 if (regs && !perf_exclude_event(event, regs)) {
5561 if (!(event->attr.exclude_idle && current->pid == 0))
5562 if (perf_event_overflow(event, &data, regs))
5563 ret = HRTIMER_NORESTART;
5566 period = max_t(u64, 10000, event->hw.sample_period);
5567 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5572 static void perf_swevent_start_hrtimer(struct perf_event *event)
5574 struct hw_perf_event *hwc = &event->hw;
5577 if (!is_sampling_event(event))
5580 period = local64_read(&hwc->period_left);
5585 local64_set(&hwc->period_left, 0);
5587 period = max_t(u64, 10000, hwc->sample_period);
5589 __hrtimer_start_range_ns(&hwc->hrtimer,
5590 ns_to_ktime(period), 0,
5591 HRTIMER_MODE_REL_PINNED, 0);
5594 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5596 struct hw_perf_event *hwc = &event->hw;
5598 if (is_sampling_event(event)) {
5599 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5600 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5602 hrtimer_cancel(&hwc->hrtimer);
5606 static void perf_swevent_init_hrtimer(struct perf_event *event)
5608 struct hw_perf_event *hwc = &event->hw;
5610 if (!is_sampling_event(event))
5613 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5614 hwc->hrtimer.function = perf_swevent_hrtimer;
5617 * Since hrtimers have a fixed rate, we can do a static freq->period
5618 * mapping and avoid the whole period adjust feedback stuff.
5620 if (event->attr.freq) {
5621 long freq = event->attr.sample_freq;
5623 event->attr.sample_period = NSEC_PER_SEC / freq;
5624 hwc->sample_period = event->attr.sample_period;
5625 local64_set(&hwc->period_left, hwc->sample_period);
5626 event->attr.freq = 0;
5631 * Software event: cpu wall time clock
5634 static void cpu_clock_event_update(struct perf_event *event)
5639 now = local_clock();
5640 prev = local64_xchg(&event->hw.prev_count, now);
5641 local64_add(now - prev, &event->count);
5644 static void cpu_clock_event_start(struct perf_event *event, int flags)
5646 local64_set(&event->hw.prev_count, local_clock());
5647 perf_swevent_start_hrtimer(event);
5650 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5652 perf_swevent_cancel_hrtimer(event);
5653 cpu_clock_event_update(event);
5656 static int cpu_clock_event_add(struct perf_event *event, int flags)
5658 if (flags & PERF_EF_START)
5659 cpu_clock_event_start(event, flags);
5664 static void cpu_clock_event_del(struct perf_event *event, int flags)
5666 cpu_clock_event_stop(event, flags);
5669 static void cpu_clock_event_read(struct perf_event *event)
5671 cpu_clock_event_update(event);
5674 static int cpu_clock_event_init(struct perf_event *event)
5676 if (event->attr.type != PERF_TYPE_SOFTWARE)
5679 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5682 perf_swevent_init_hrtimer(event);
5687 static struct pmu perf_cpu_clock = {
5688 .task_ctx_nr = perf_sw_context,
5690 .event_init = cpu_clock_event_init,
5691 .add = cpu_clock_event_add,
5692 .del = cpu_clock_event_del,
5693 .start = cpu_clock_event_start,
5694 .stop = cpu_clock_event_stop,
5695 .read = cpu_clock_event_read,
5699 * Software event: task time clock
5702 static void task_clock_event_update(struct perf_event *event, u64 now)
5707 prev = local64_xchg(&event->hw.prev_count, now);
5709 local64_add(delta, &event->count);
5712 static void task_clock_event_start(struct perf_event *event, int flags)
5714 local64_set(&event->hw.prev_count, event->ctx->time);
5715 perf_swevent_start_hrtimer(event);
5718 static void task_clock_event_stop(struct perf_event *event, int flags)
5720 perf_swevent_cancel_hrtimer(event);
5721 task_clock_event_update(event, event->ctx->time);
5724 static int task_clock_event_add(struct perf_event *event, int flags)
5726 if (flags & PERF_EF_START)
5727 task_clock_event_start(event, flags);
5732 static void task_clock_event_del(struct perf_event *event, int flags)
5734 task_clock_event_stop(event, PERF_EF_UPDATE);
5737 static void task_clock_event_read(struct perf_event *event)
5739 u64 now = perf_clock();
5740 u64 delta = now - event->ctx->timestamp;
5741 u64 time = event->ctx->time + delta;
5743 task_clock_event_update(event, time);
5746 static int task_clock_event_init(struct perf_event *event)
5748 if (event->attr.type != PERF_TYPE_SOFTWARE)
5751 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5754 perf_swevent_init_hrtimer(event);
5759 static struct pmu perf_task_clock = {
5760 .task_ctx_nr = perf_sw_context,
5762 .event_init = task_clock_event_init,
5763 .add = task_clock_event_add,
5764 .del = task_clock_event_del,
5765 .start = task_clock_event_start,
5766 .stop = task_clock_event_stop,
5767 .read = task_clock_event_read,
5770 static void perf_pmu_nop_void(struct pmu *pmu)
5774 static int perf_pmu_nop_int(struct pmu *pmu)
5779 static void perf_pmu_start_txn(struct pmu *pmu)
5781 perf_pmu_disable(pmu);
5784 static int perf_pmu_commit_txn(struct pmu *pmu)
5786 perf_pmu_enable(pmu);
5790 static void perf_pmu_cancel_txn(struct pmu *pmu)
5792 perf_pmu_enable(pmu);
5796 * Ensures all contexts with the same task_ctx_nr have the same
5797 * pmu_cpu_context too.
5799 static void *find_pmu_context(int ctxn)
5806 list_for_each_entry(pmu, &pmus, entry) {
5807 if (pmu->task_ctx_nr == ctxn)
5808 return pmu->pmu_cpu_context;
5814 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5818 for_each_possible_cpu(cpu) {
5819 struct perf_cpu_context *cpuctx;
5821 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5823 if (cpuctx->unique_pmu == old_pmu)
5824 cpuctx->unique_pmu = pmu;
5828 static void free_pmu_context(struct pmu *pmu)
5832 mutex_lock(&pmus_lock);
5834 * Like a real lame refcount.
5836 list_for_each_entry(i, &pmus, entry) {
5837 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5838 update_pmu_context(i, pmu);
5843 free_percpu(pmu->pmu_cpu_context);
5845 mutex_unlock(&pmus_lock);
5847 static struct idr pmu_idr;
5850 type_show(struct device *dev, struct device_attribute *attr, char *page)
5852 struct pmu *pmu = dev_get_drvdata(dev);
5854 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5857 static struct device_attribute pmu_dev_attrs[] = {
5862 static int pmu_bus_running;
5863 static struct bus_type pmu_bus = {
5864 .name = "event_source",
5865 .dev_attrs = pmu_dev_attrs,
5868 static void pmu_dev_release(struct device *dev)
5873 static int pmu_dev_alloc(struct pmu *pmu)
5877 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5881 device_initialize(pmu->dev);
5882 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5886 dev_set_drvdata(pmu->dev, pmu);
5887 pmu->dev->bus = &pmu_bus;
5888 pmu->dev->release = pmu_dev_release;
5889 ret = device_add(pmu->dev);
5897 put_device(pmu->dev);
5901 static struct lock_class_key cpuctx_mutex;
5902 static struct lock_class_key cpuctx_lock;
5904 int perf_pmu_register(struct pmu *pmu, char *name, int type)
5908 mutex_lock(&pmus_lock);
5910 pmu->pmu_disable_count = alloc_percpu(int);
5911 if (!pmu->pmu_disable_count)
5920 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5924 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5932 if (pmu_bus_running) {
5933 ret = pmu_dev_alloc(pmu);
5939 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5940 if (pmu->pmu_cpu_context)
5941 goto got_cpu_context;
5944 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5945 if (!pmu->pmu_cpu_context)
5948 for_each_possible_cpu(cpu) {
5949 struct perf_cpu_context *cpuctx;
5951 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5952 __perf_event_init_context(&cpuctx->ctx);
5953 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
5954 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
5955 cpuctx->ctx.type = cpu_context;
5956 cpuctx->ctx.pmu = pmu;
5957 cpuctx->jiffies_interval = 1;
5958 INIT_LIST_HEAD(&cpuctx->rotation_list);
5959 cpuctx->unique_pmu = pmu;
5963 if (!pmu->start_txn) {
5964 if (pmu->pmu_enable) {
5966 * If we have pmu_enable/pmu_disable calls, install
5967 * transaction stubs that use that to try and batch
5968 * hardware accesses.
5970 pmu->start_txn = perf_pmu_start_txn;
5971 pmu->commit_txn = perf_pmu_commit_txn;
5972 pmu->cancel_txn = perf_pmu_cancel_txn;
5974 pmu->start_txn = perf_pmu_nop_void;
5975 pmu->commit_txn = perf_pmu_nop_int;
5976 pmu->cancel_txn = perf_pmu_nop_void;
5980 if (!pmu->pmu_enable) {
5981 pmu->pmu_enable = perf_pmu_nop_void;
5982 pmu->pmu_disable = perf_pmu_nop_void;
5985 list_add_rcu(&pmu->entry, &pmus);
5988 mutex_unlock(&pmus_lock);
5993 device_del(pmu->dev);
5994 put_device(pmu->dev);
5997 if (pmu->type >= PERF_TYPE_MAX)
5998 idr_remove(&pmu_idr, pmu->type);
6001 free_percpu(pmu->pmu_disable_count);
6005 void perf_pmu_unregister(struct pmu *pmu)
6007 mutex_lock(&pmus_lock);
6008 list_del_rcu(&pmu->entry);
6009 mutex_unlock(&pmus_lock);
6012 * We dereference the pmu list under both SRCU and regular RCU, so
6013 * synchronize against both of those.
6015 synchronize_srcu(&pmus_srcu);
6018 free_percpu(pmu->pmu_disable_count);
6019 if (pmu->type >= PERF_TYPE_MAX)
6020 idr_remove(&pmu_idr, pmu->type);
6021 device_del(pmu->dev);
6022 put_device(pmu->dev);
6023 free_pmu_context(pmu);
6026 struct pmu *perf_init_event(struct perf_event *event)
6028 struct pmu *pmu = NULL;
6032 idx = srcu_read_lock(&pmus_srcu);
6035 pmu = idr_find(&pmu_idr, event->attr.type);
6039 ret = pmu->event_init(event);
6045 list_for_each_entry_rcu(pmu, &pmus, entry) {
6047 ret = pmu->event_init(event);
6051 if (ret != -ENOENT) {
6056 pmu = ERR_PTR(-ENOENT);
6058 srcu_read_unlock(&pmus_srcu, idx);
6064 * Allocate and initialize a event structure
6066 static struct perf_event *
6067 perf_event_alloc(struct perf_event_attr *attr, int cpu,
6068 struct task_struct *task,
6069 struct perf_event *group_leader,
6070 struct perf_event *parent_event,
6071 perf_overflow_handler_t overflow_handler,
6075 struct perf_event *event;
6076 struct hw_perf_event *hwc;
6079 if ((unsigned)cpu >= nr_cpu_ids) {
6080 if (!task || cpu != -1)
6081 return ERR_PTR(-EINVAL);
6084 event = kzalloc(sizeof(*event), GFP_KERNEL);
6086 return ERR_PTR(-ENOMEM);
6089 * Single events are their own group leaders, with an
6090 * empty sibling list:
6093 group_leader = event;
6095 mutex_init(&event->child_mutex);
6096 INIT_LIST_HEAD(&event->child_list);
6098 INIT_LIST_HEAD(&event->group_entry);
6099 INIT_LIST_HEAD(&event->event_entry);
6100 INIT_LIST_HEAD(&event->sibling_list);
6101 INIT_LIST_HEAD(&event->rb_entry);
6103 init_waitqueue_head(&event->waitq);
6104 init_irq_work(&event->pending, perf_pending_event);
6106 mutex_init(&event->mmap_mutex);
6108 atomic_long_set(&event->refcount, 1);
6110 event->attr = *attr;
6111 event->group_leader = group_leader;
6115 event->parent = parent_event;
6117 event->ns = get_pid_ns(current->nsproxy->pid_ns);
6118 event->id = atomic64_inc_return(&perf_event_id);
6120 event->state = PERF_EVENT_STATE_INACTIVE;
6123 event->attach_state = PERF_ATTACH_TASK;
6124 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6126 * hw_breakpoint is a bit difficult here..
6128 if (attr->type == PERF_TYPE_BREAKPOINT)
6129 event->hw.bp_target = task;
6133 if (!overflow_handler && parent_event) {
6134 overflow_handler = parent_event->overflow_handler;
6135 context = parent_event->overflow_handler_context;
6138 event->overflow_handler = overflow_handler;
6139 event->overflow_handler_context = context;
6141 perf_event__state_init(event);
6146 hwc->sample_period = attr->sample_period;
6147 if (attr->freq && attr->sample_freq)
6148 hwc->sample_period = 1;
6149 hwc->last_period = hwc->sample_period;
6151 local64_set(&hwc->period_left, hwc->sample_period);
6154 * we currently do not support PERF_FORMAT_GROUP on inherited events
6156 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6159 pmu = perf_init_event(event);
6165 else if (IS_ERR(pmu))
6170 put_pid_ns(event->ns);
6172 return ERR_PTR(err);
6175 if (!event->parent) {
6176 if (event->attach_state & PERF_ATTACH_TASK)
6177 jump_label_inc(&perf_sched_events);
6178 if (event->attr.mmap || event->attr.mmap_data)
6179 atomic_inc(&nr_mmap_events);
6180 if (event->attr.comm)
6181 atomic_inc(&nr_comm_events);
6182 if (event->attr.task)
6183 atomic_inc(&nr_task_events);
6184 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6185 err = get_callchain_buffers();
6188 return ERR_PTR(err);
6196 static int perf_copy_attr(struct perf_event_attr __user *uattr,
6197 struct perf_event_attr *attr)
6202 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6206 * zero the full structure, so that a short copy will be nice.
6208 memset(attr, 0, sizeof(*attr));
6210 ret = get_user(size, &uattr->size);
6214 if (size > PAGE_SIZE) /* silly large */
6217 if (!size) /* abi compat */
6218 size = PERF_ATTR_SIZE_VER0;
6220 if (size < PERF_ATTR_SIZE_VER0)
6224 * If we're handed a bigger struct than we know of,
6225 * ensure all the unknown bits are 0 - i.e. new
6226 * user-space does not rely on any kernel feature
6227 * extensions we dont know about yet.
6229 if (size > sizeof(*attr)) {
6230 unsigned char __user *addr;
6231 unsigned char __user *end;
6234 addr = (void __user *)uattr + sizeof(*attr);
6235 end = (void __user *)uattr + size;
6237 for (; addr < end; addr++) {
6238 ret = get_user(val, addr);
6244 size = sizeof(*attr);
6247 ret = copy_from_user(attr, uattr, size);
6251 if (attr->__reserved_1)
6254 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6257 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6264 put_user(sizeof(*attr), &uattr->size);
6270 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6272 struct ring_buffer *rb = NULL, *old_rb = NULL;
6278 /* don't allow circular references */
6279 if (event == output_event)
6283 * Don't allow cross-cpu buffers
6285 if (output_event->cpu != event->cpu)
6289 * If its not a per-cpu rb, it must be the same task.
6291 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6295 mutex_lock(&event->mmap_mutex);
6296 /* Can't redirect output if we've got an active mmap() */
6297 if (atomic_read(&event->mmap_count))
6303 /* get the rb we want to redirect to */
6304 rb = ring_buffer_get(output_event);
6310 ring_buffer_detach(event, old_rb);
6313 ring_buffer_attach(event, rb);
6315 rcu_assign_pointer(event->rb, rb);
6318 ring_buffer_put(old_rb);
6320 * Since we detached before setting the new rb, so that we
6321 * could attach the new rb, we could have missed a wakeup.
6324 wake_up_all(&event->waitq);
6329 mutex_unlock(&event->mmap_mutex);
6336 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6338 * @attr_uptr: event_id type attributes for monitoring/sampling
6341 * @group_fd: group leader event fd
6343 SYSCALL_DEFINE5(perf_event_open,
6344 struct perf_event_attr __user *, attr_uptr,
6345 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6347 struct perf_event *group_leader = NULL, *output_event = NULL;
6348 struct perf_event *event, *sibling;
6349 struct perf_event_attr attr;
6350 struct perf_event_context *ctx;
6351 struct file *event_file = NULL;
6352 struct file *group_file = NULL;
6353 struct task_struct *task = NULL;
6357 int fput_needed = 0;
6360 /* for future expandability... */
6361 if (flags & ~PERF_FLAG_ALL)
6364 err = perf_copy_attr(attr_uptr, &attr);
6368 if (!attr.exclude_kernel) {
6369 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6374 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6377 if (attr.sample_period & (1ULL << 63))
6382 * In cgroup mode, the pid argument is used to pass the fd
6383 * opened to the cgroup directory in cgroupfs. The cpu argument
6384 * designates the cpu on which to monitor threads from that
6387 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6390 event_fd = get_unused_fd_flags(O_RDWR);
6394 if (group_fd != -1) {
6395 group_file = perf_fget_light(group_fd, &fput_needed);
6396 if (IS_ERR(group_file)) {
6397 err = PTR_ERR(group_file);
6400 group_leader = group_file->private_data;
6401 if (flags & PERF_FLAG_FD_OUTPUT)
6402 output_event = group_leader;
6403 if (flags & PERF_FLAG_FD_NO_GROUP)
6404 group_leader = NULL;
6407 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
6408 task = find_lively_task_by_vpid(pid);
6410 err = PTR_ERR(task);
6415 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
6417 if (IS_ERR(event)) {
6418 err = PTR_ERR(event);
6422 if (flags & PERF_FLAG_PID_CGROUP) {
6423 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6428 * - that has cgroup constraint on event->cpu
6429 * - that may need work on context switch
6431 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6432 jump_label_inc(&perf_sched_events);
6436 * Special case software events and allow them to be part of
6437 * any hardware group.
6442 (is_software_event(event) != is_software_event(group_leader))) {
6443 if (is_software_event(event)) {
6445 * If event and group_leader are not both a software
6446 * event, and event is, then group leader is not.
6448 * Allow the addition of software events to !software
6449 * groups, this is safe because software events never
6452 pmu = group_leader->pmu;
6453 } else if (is_software_event(group_leader) &&
6454 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6456 * In case the group is a pure software group, and we
6457 * try to add a hardware event, move the whole group to
6458 * the hardware context.
6465 * Get the target context (task or percpu):
6467 ctx = find_get_context(pmu, task, cpu);
6474 put_task_struct(task);
6479 * Look up the group leader (we will attach this event to it):
6485 * Do not allow a recursive hierarchy (this new sibling
6486 * becoming part of another group-sibling):
6488 if (group_leader->group_leader != group_leader)
6491 * Do not allow to attach to a group in a different
6492 * task or CPU context:
6495 if (group_leader->ctx->type != ctx->type)
6498 if (group_leader->ctx != ctx)
6503 * Only a group leader can be exclusive or pinned
6505 if (attr.exclusive || attr.pinned)
6510 err = perf_event_set_output(event, output_event);
6515 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6516 if (IS_ERR(event_file)) {
6517 err = PTR_ERR(event_file);
6522 struct perf_event_context *gctx = group_leader->ctx;
6524 mutex_lock(&gctx->mutex);
6525 perf_remove_from_context(group_leader, false);
6528 * Removing from the context ends up with disabled
6529 * event. What we want here is event in the initial
6530 * startup state, ready to be add into new context.
6532 perf_event__state_init(group_leader);
6533 list_for_each_entry(sibling, &group_leader->sibling_list,
6535 perf_remove_from_context(sibling, false);
6536 perf_event__state_init(sibling);
6539 mutex_unlock(&gctx->mutex);
6543 WARN_ON_ONCE(ctx->parent_ctx);
6544 mutex_lock(&ctx->mutex);
6547 perf_install_in_context(ctx, group_leader, cpu);
6549 list_for_each_entry(sibling, &group_leader->sibling_list,
6551 perf_install_in_context(ctx, sibling, cpu);
6556 perf_install_in_context(ctx, event, cpu);
6558 perf_unpin_context(ctx);
6559 mutex_unlock(&ctx->mutex);
6561 event->owner = current;
6563 mutex_lock(¤t->perf_event_mutex);
6564 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6565 mutex_unlock(¤t->perf_event_mutex);
6568 * Precalculate sample_data sizes
6570 perf_event__header_size(event);
6571 perf_event__id_header_size(event);
6574 * Drop the reference on the group_event after placing the
6575 * new event on the sibling_list. This ensures destruction
6576 * of the group leader will find the pointer to itself in
6577 * perf_group_detach().
6579 fput_light(group_file, fput_needed);
6580 fd_install(event_fd, event_file);
6584 perf_unpin_context(ctx);
6590 put_task_struct(task);
6592 fput_light(group_file, fput_needed);
6594 put_unused_fd(event_fd);
6599 * perf_event_create_kernel_counter
6601 * @attr: attributes of the counter to create
6602 * @cpu: cpu in which the counter is bound
6603 * @task: task to profile (NULL for percpu)
6606 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6607 struct task_struct *task,
6608 perf_overflow_handler_t overflow_handler,
6611 struct perf_event_context *ctx;
6612 struct perf_event *event;
6616 * Get the target context (task or percpu):
6619 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
6620 overflow_handler, context);
6621 if (IS_ERR(event)) {
6622 err = PTR_ERR(event);
6626 ctx = find_get_context(event->pmu, task, cpu);
6632 WARN_ON_ONCE(ctx->parent_ctx);
6633 mutex_lock(&ctx->mutex);
6634 perf_install_in_context(ctx, event, cpu);
6636 perf_unpin_context(ctx);
6637 mutex_unlock(&ctx->mutex);
6644 return ERR_PTR(err);
6646 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6648 static void sync_child_event(struct perf_event *child_event,
6649 struct task_struct *child)
6651 struct perf_event *parent_event = child_event->parent;
6654 if (child_event->attr.inherit_stat)
6655 perf_event_read_event(child_event, child);
6657 child_val = perf_event_count(child_event);
6660 * Add back the child's count to the parent's count:
6662 atomic64_add(child_val, &parent_event->child_count);
6663 atomic64_add(child_event->total_time_enabled,
6664 &parent_event->child_total_time_enabled);
6665 atomic64_add(child_event->total_time_running,
6666 &parent_event->child_total_time_running);
6669 * Remove this event from the parent's list
6671 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6672 mutex_lock(&parent_event->child_mutex);
6673 list_del_init(&child_event->child_list);
6674 mutex_unlock(&parent_event->child_mutex);
6677 * Release the parent event, if this was the last
6680 put_event(parent_event);
6684 __perf_event_exit_task(struct perf_event *child_event,
6685 struct perf_event_context *child_ctx,
6686 struct task_struct *child)
6688 perf_remove_from_context(child_event, !!child_event->parent);
6691 * It can happen that the parent exits first, and has events
6692 * that are still around due to the child reference. These
6693 * events need to be zapped.
6695 if (child_event->parent) {
6696 sync_child_event(child_event, child);
6697 free_event(child_event);
6701 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6703 struct perf_event *child_event, *tmp;
6704 struct perf_event_context *child_ctx;
6705 unsigned long flags;
6707 if (likely(!child->perf_event_ctxp[ctxn])) {
6708 perf_event_task(child, NULL, 0);
6712 local_irq_save(flags);
6714 * We can't reschedule here because interrupts are disabled,
6715 * and either child is current or it is a task that can't be
6716 * scheduled, so we are now safe from rescheduling changing
6719 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6722 * Take the context lock here so that if find_get_context is
6723 * reading child->perf_event_ctxp, we wait until it has
6724 * incremented the context's refcount before we do put_ctx below.
6726 raw_spin_lock(&child_ctx->lock);
6727 task_ctx_sched_out(child_ctx);
6728 child->perf_event_ctxp[ctxn] = NULL;
6730 * If this context is a clone; unclone it so it can't get
6731 * swapped to another process while we're removing all
6732 * the events from it.
6734 unclone_ctx(child_ctx);
6735 update_context_time(child_ctx);
6736 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6739 * Report the task dead after unscheduling the events so that we
6740 * won't get any samples after PERF_RECORD_EXIT. We can however still
6741 * get a few PERF_RECORD_READ events.
6743 perf_event_task(child, child_ctx, 0);
6746 * We can recurse on the same lock type through:
6748 * __perf_event_exit_task()
6749 * sync_child_event()
6751 * mutex_lock(&ctx->mutex)
6753 * But since its the parent context it won't be the same instance.
6755 mutex_lock(&child_ctx->mutex);
6758 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6760 __perf_event_exit_task(child_event, child_ctx, child);
6762 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6764 __perf_event_exit_task(child_event, child_ctx, child);
6767 * If the last event was a group event, it will have appended all
6768 * its siblings to the list, but we obtained 'tmp' before that which
6769 * will still point to the list head terminating the iteration.
6771 if (!list_empty(&child_ctx->pinned_groups) ||
6772 !list_empty(&child_ctx->flexible_groups))
6775 mutex_unlock(&child_ctx->mutex);
6781 * When a child task exits, feed back event values to parent events.
6783 void perf_event_exit_task(struct task_struct *child)
6785 struct perf_event *event, *tmp;
6788 mutex_lock(&child->perf_event_mutex);
6789 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6791 list_del_init(&event->owner_entry);
6794 * Ensure the list deletion is visible before we clear
6795 * the owner, closes a race against perf_release() where
6796 * we need to serialize on the owner->perf_event_mutex.
6799 event->owner = NULL;
6801 mutex_unlock(&child->perf_event_mutex);
6803 for_each_task_context_nr(ctxn)
6804 perf_event_exit_task_context(child, ctxn);
6807 static void perf_free_event(struct perf_event *event,
6808 struct perf_event_context *ctx)
6810 struct perf_event *parent = event->parent;
6812 if (WARN_ON_ONCE(!parent))
6815 mutex_lock(&parent->child_mutex);
6816 list_del_init(&event->child_list);
6817 mutex_unlock(&parent->child_mutex);
6821 perf_group_detach(event);
6822 list_del_event(event, ctx);
6827 * free an unexposed, unused context as created by inheritance by
6828 * perf_event_init_task below, used by fork() in case of fail.
6830 void perf_event_free_task(struct task_struct *task)
6832 struct perf_event_context *ctx;
6833 struct perf_event *event, *tmp;
6836 for_each_task_context_nr(ctxn) {
6837 ctx = task->perf_event_ctxp[ctxn];
6841 mutex_lock(&ctx->mutex);
6843 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6845 perf_free_event(event, ctx);
6847 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6849 perf_free_event(event, ctx);
6851 if (!list_empty(&ctx->pinned_groups) ||
6852 !list_empty(&ctx->flexible_groups))
6855 mutex_unlock(&ctx->mutex);
6861 void perf_event_delayed_put(struct task_struct *task)
6865 for_each_task_context_nr(ctxn)
6866 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6870 * inherit a event from parent task to child task:
6872 static struct perf_event *
6873 inherit_event(struct perf_event *parent_event,
6874 struct task_struct *parent,
6875 struct perf_event_context *parent_ctx,
6876 struct task_struct *child,
6877 struct perf_event *group_leader,
6878 struct perf_event_context *child_ctx)
6880 struct perf_event *child_event;
6881 unsigned long flags;
6884 * Instead of creating recursive hierarchies of events,
6885 * we link inherited events back to the original parent,
6886 * which has a filp for sure, which we use as the reference
6889 if (parent_event->parent)
6890 parent_event = parent_event->parent;
6892 child_event = perf_event_alloc(&parent_event->attr,
6895 group_leader, parent_event,
6897 if (IS_ERR(child_event))
6900 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
6901 free_event(child_event);
6908 * Make the child state follow the state of the parent event,
6909 * not its attr.disabled bit. We hold the parent's mutex,
6910 * so we won't race with perf_event_{en, dis}able_family.
6912 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6913 child_event->state = PERF_EVENT_STATE_INACTIVE;
6915 child_event->state = PERF_EVENT_STATE_OFF;
6917 if (parent_event->attr.freq) {
6918 u64 sample_period = parent_event->hw.sample_period;
6919 struct hw_perf_event *hwc = &child_event->hw;
6921 hwc->sample_period = sample_period;
6922 hwc->last_period = sample_period;
6924 local64_set(&hwc->period_left, sample_period);
6927 child_event->ctx = child_ctx;
6928 child_event->overflow_handler = parent_event->overflow_handler;
6929 child_event->overflow_handler_context
6930 = parent_event->overflow_handler_context;
6933 * Precalculate sample_data sizes
6935 perf_event__header_size(child_event);
6936 perf_event__id_header_size(child_event);
6939 * Link it up in the child's context:
6941 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6942 add_event_to_ctx(child_event, child_ctx);
6943 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6946 * Link this into the parent event's child list
6948 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6949 mutex_lock(&parent_event->child_mutex);
6950 list_add_tail(&child_event->child_list, &parent_event->child_list);
6951 mutex_unlock(&parent_event->child_mutex);
6956 static int inherit_group(struct perf_event *parent_event,
6957 struct task_struct *parent,
6958 struct perf_event_context *parent_ctx,
6959 struct task_struct *child,
6960 struct perf_event_context *child_ctx)
6962 struct perf_event *leader;
6963 struct perf_event *sub;
6964 struct perf_event *child_ctr;
6966 leader = inherit_event(parent_event, parent, parent_ctx,
6967 child, NULL, child_ctx);
6969 return PTR_ERR(leader);
6970 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6971 child_ctr = inherit_event(sub, parent, parent_ctx,
6972 child, leader, child_ctx);
6973 if (IS_ERR(child_ctr))
6974 return PTR_ERR(child_ctr);
6980 inherit_task_group(struct perf_event *event, struct task_struct *parent,
6981 struct perf_event_context *parent_ctx,
6982 struct task_struct *child, int ctxn,
6986 struct perf_event_context *child_ctx;
6988 if (!event->attr.inherit) {
6993 child_ctx = child->perf_event_ctxp[ctxn];
6996 * This is executed from the parent task context, so
6997 * inherit events that have been marked for cloning.
6998 * First allocate and initialize a context for the
7002 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
7006 child->perf_event_ctxp[ctxn] = child_ctx;
7009 ret = inherit_group(event, parent, parent_ctx,
7019 * Initialize the perf_event context in task_struct
7021 int perf_event_init_context(struct task_struct *child, int ctxn)
7023 struct perf_event_context *child_ctx, *parent_ctx;
7024 struct perf_event_context *cloned_ctx;
7025 struct perf_event *event;
7026 struct task_struct *parent = current;
7027 int inherited_all = 1;
7028 unsigned long flags;
7031 if (likely(!parent->perf_event_ctxp[ctxn]))
7035 * If the parent's context is a clone, pin it so it won't get
7038 parent_ctx = perf_pin_task_context(parent, ctxn);
7041 * No need to check if parent_ctx != NULL here; since we saw
7042 * it non-NULL earlier, the only reason for it to become NULL
7043 * is if we exit, and since we're currently in the middle of
7044 * a fork we can't be exiting at the same time.
7048 * Lock the parent list. No need to lock the child - not PID
7049 * hashed yet and not running, so nobody can access it.
7051 mutex_lock(&parent_ctx->mutex);
7054 * We dont have to disable NMIs - we are only looking at
7055 * the list, not manipulating it:
7057 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
7058 ret = inherit_task_group(event, parent, parent_ctx,
7059 child, ctxn, &inherited_all);
7065 * We can't hold ctx->lock when iterating the ->flexible_group list due
7066 * to allocations, but we need to prevent rotation because
7067 * rotate_ctx() will change the list from interrupt context.
7069 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7070 parent_ctx->rotate_disable = 1;
7071 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7073 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
7074 ret = inherit_task_group(event, parent, parent_ctx,
7075 child, ctxn, &inherited_all);
7080 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7081 parent_ctx->rotate_disable = 0;
7083 child_ctx = child->perf_event_ctxp[ctxn];
7085 if (child_ctx && inherited_all) {
7087 * Mark the child context as a clone of the parent
7088 * context, or of whatever the parent is a clone of.
7090 * Note that if the parent is a clone, the holding of
7091 * parent_ctx->lock avoids it from being uncloned.
7093 cloned_ctx = parent_ctx->parent_ctx;
7095 child_ctx->parent_ctx = cloned_ctx;
7096 child_ctx->parent_gen = parent_ctx->parent_gen;
7098 child_ctx->parent_ctx = parent_ctx;
7099 child_ctx->parent_gen = parent_ctx->generation;
7101 get_ctx(child_ctx->parent_ctx);
7104 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7105 mutex_unlock(&parent_ctx->mutex);
7107 perf_unpin_context(parent_ctx);
7108 put_ctx(parent_ctx);
7114 * Initialize the perf_event context in task_struct
7116 int perf_event_init_task(struct task_struct *child)
7120 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7121 mutex_init(&child->perf_event_mutex);
7122 INIT_LIST_HEAD(&child->perf_event_list);
7124 for_each_task_context_nr(ctxn) {
7125 ret = perf_event_init_context(child, ctxn);
7127 perf_event_free_task(child);
7135 static void __init perf_event_init_all_cpus(void)
7137 struct swevent_htable *swhash;
7140 for_each_possible_cpu(cpu) {
7141 swhash = &per_cpu(swevent_htable, cpu);
7142 mutex_init(&swhash->hlist_mutex);
7143 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
7147 static void __cpuinit perf_event_init_cpu(int cpu)
7149 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7151 mutex_lock(&swhash->hlist_mutex);
7152 swhash->online = true;
7153 if (swhash->hlist_refcount > 0) {
7154 struct swevent_hlist *hlist;
7156 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7158 rcu_assign_pointer(swhash->swevent_hlist, hlist);
7160 mutex_unlock(&swhash->hlist_mutex);
7163 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7164 static void perf_pmu_rotate_stop(struct pmu *pmu)
7166 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7168 WARN_ON(!irqs_disabled());
7170 list_del_init(&cpuctx->rotation_list);
7173 static void __perf_event_exit_context(void *__info)
7175 struct remove_event re = { .detach_group = false };
7176 struct perf_event_context *ctx = __info;
7178 perf_pmu_rotate_stop(ctx->pmu);
7181 list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry)
7182 __perf_remove_from_context(&re);
7186 static void perf_event_exit_cpu_context(int cpu)
7188 struct perf_event_context *ctx;
7192 idx = srcu_read_lock(&pmus_srcu);
7193 list_for_each_entry_rcu(pmu, &pmus, entry) {
7194 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
7196 mutex_lock(&ctx->mutex);
7197 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7198 mutex_unlock(&ctx->mutex);
7200 srcu_read_unlock(&pmus_srcu, idx);
7203 static void perf_event_exit_cpu(int cpu)
7205 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7207 perf_event_exit_cpu_context(cpu);
7209 mutex_lock(&swhash->hlist_mutex);
7210 swhash->online = false;
7211 swevent_hlist_release(swhash);
7212 mutex_unlock(&swhash->hlist_mutex);
7215 static inline void perf_event_exit_cpu(int cpu) { }
7219 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7223 for_each_online_cpu(cpu)
7224 perf_event_exit_cpu(cpu);
7230 * Run the perf reboot notifier at the very last possible moment so that
7231 * the generic watchdog code runs as long as possible.
7233 static struct notifier_block perf_reboot_notifier = {
7234 .notifier_call = perf_reboot,
7235 .priority = INT_MIN,
7238 static int __cpuinit
7239 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7241 unsigned int cpu = (long)hcpu;
7243 switch (action & ~CPU_TASKS_FROZEN) {
7245 case CPU_UP_PREPARE:
7246 case CPU_DOWN_FAILED:
7247 perf_event_init_cpu(cpu);
7250 case CPU_UP_CANCELED:
7251 case CPU_DOWN_PREPARE:
7252 perf_event_exit_cpu(cpu);
7262 void __init perf_event_init(void)
7268 perf_event_init_all_cpus();
7269 init_srcu_struct(&pmus_srcu);
7270 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7271 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7272 perf_pmu_register(&perf_task_clock, NULL, -1);
7274 perf_cpu_notifier(perf_cpu_notify);
7275 register_reboot_notifier(&perf_reboot_notifier);
7277 ret = init_hw_breakpoint();
7278 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7281 static int __init perf_event_sysfs_init(void)
7286 mutex_lock(&pmus_lock);
7288 ret = bus_register(&pmu_bus);
7292 list_for_each_entry(pmu, &pmus, entry) {
7293 if (!pmu->name || pmu->type < 0)
7296 ret = pmu_dev_alloc(pmu);
7297 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7299 pmu_bus_running = 1;
7303 mutex_unlock(&pmus_lock);
7307 device_initcall(perf_event_sysfs_init);
7309 #ifdef CONFIG_CGROUP_PERF
7310 static struct cgroup_subsys_state *perf_cgroup_create(
7311 struct cgroup_subsys *ss, struct cgroup *cont)
7313 struct perf_cgroup *jc;
7315 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
7317 return ERR_PTR(-ENOMEM);
7319 jc->info = alloc_percpu(struct perf_cgroup_info);
7322 return ERR_PTR(-ENOMEM);
7328 static void perf_cgroup_destroy(struct cgroup_subsys *ss,
7329 struct cgroup *cont)
7331 struct perf_cgroup *jc;
7332 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7333 struct perf_cgroup, css);
7334 free_percpu(jc->info);
7338 static int __perf_cgroup_move(void *info)
7340 struct task_struct *task = info;
7341 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7346 perf_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *task)
7348 task_function_call(task, __perf_cgroup_move, task);
7351 static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7352 struct cgroup *old_cgrp, struct task_struct *task)
7355 * cgroup_exit() is called in the copy_process() failure path.
7356 * Ignore this case since the task hasn't ran yet, this avoids
7357 * trying to poke a half freed task state from generic code.
7359 if (!(task->flags & PF_EXITING))
7362 perf_cgroup_attach_task(cgrp, task);
7365 struct cgroup_subsys perf_subsys = {
7366 .name = "perf_event",
7367 .subsys_id = perf_subsys_id,
7368 .create = perf_cgroup_create,
7369 .destroy = perf_cgroup_destroy,
7370 .exit = perf_cgroup_exit,
7371 .attach_task = perf_cgroup_attach_task,
7373 #endif /* CONFIG_CGROUP_PERF */