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/vmalloc.h>
29 #include <linux/hardirq.h>
30 #include <linux/rculist.h>
31 #include <linux/uaccess.h>
32 #include <linux/syscalls.h>
33 #include <linux/anon_inodes.h>
34 #include <linux/kernel_stat.h>
35 #include <linux/perf_event.h>
36 #include <linux/ftrace_event.h>
37 #include <linux/hw_breakpoint.h>
39 #include <asm/irq_regs.h>
41 struct remote_function_call {
42 struct task_struct *p;
43 int (*func)(void *info);
48 static void remote_function(void *data)
50 struct remote_function_call *tfc = data;
51 struct task_struct *p = tfc->p;
55 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
59 tfc->ret = tfc->func(tfc->info);
63 * task_function_call - call a function on the cpu on which a task runs
64 * @p: the task to evaluate
65 * @func: the function to be called
66 * @info: the function call argument
68 * Calls the function @func when the task is currently running. This might
69 * be on the current CPU, which just calls the function directly
71 * returns: @func return value, or
72 * -ESRCH - when the process isn't running
73 * -EAGAIN - when the process moved away
76 task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
78 struct remote_function_call data = {
82 .ret = -ESRCH, /* No such (running) process */
86 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
92 * cpu_function_call - call a function on the cpu
93 * @func: the function to be called
94 * @info: the function call argument
96 * Calls the function @func on the remote cpu.
98 * returns: @func return value or -ENXIO when the cpu is offline
100 static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
102 struct remote_function_call data = {
106 .ret = -ENXIO, /* No such CPU */
109 smp_call_function_single(cpu, remote_function, &data, 1);
114 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
115 PERF_FLAG_FD_OUTPUT |\
116 PERF_FLAG_PID_CGROUP)
119 EVENT_FLEXIBLE = 0x1,
121 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
125 * perf_sched_events : >0 events exist
126 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
128 struct jump_label_key perf_sched_events __read_mostly;
129 static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
131 static atomic_t nr_mmap_events __read_mostly;
132 static atomic_t nr_comm_events __read_mostly;
133 static atomic_t nr_task_events __read_mostly;
135 static LIST_HEAD(pmus);
136 static DEFINE_MUTEX(pmus_lock);
137 static struct srcu_struct pmus_srcu;
140 * perf event paranoia level:
141 * -1 - not paranoid at all
142 * 0 - disallow raw tracepoint access for unpriv
143 * 1 - disallow cpu events for unpriv
144 * 2 - disallow kernel profiling for unpriv
146 int sysctl_perf_event_paranoid __read_mostly = 1;
148 /* Minimum for 512 kiB + 1 user control page */
149 int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
152 * max perf event sample rate
154 #define DEFAULT_MAX_SAMPLE_RATE 100000
155 int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
156 static int max_samples_per_tick __read_mostly =
157 DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
159 int perf_proc_update_handler(struct ctl_table *table, int write,
160 void __user *buffer, size_t *lenp,
163 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
168 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
173 static atomic64_t perf_event_id;
175 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
176 enum event_type_t event_type);
178 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
179 enum event_type_t event_type,
180 struct task_struct *task);
182 static void update_context_time(struct perf_event_context *ctx);
183 static u64 perf_event_time(struct perf_event *event);
185 void __weak perf_event_print_debug(void) { }
187 extern __weak const char *perf_pmu_name(void)
192 static inline u64 perf_clock(void)
194 return local_clock();
197 static inline struct perf_cpu_context *
198 __get_cpu_context(struct perf_event_context *ctx)
200 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
203 static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
204 struct perf_event_context *ctx)
206 raw_spin_lock(&cpuctx->ctx.lock);
208 raw_spin_lock(&ctx->lock);
211 static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
212 struct perf_event_context *ctx)
215 raw_spin_unlock(&ctx->lock);
216 raw_spin_unlock(&cpuctx->ctx.lock);
219 #ifdef CONFIG_CGROUP_PERF
222 * Must ensure cgroup is pinned (css_get) before calling
223 * this function. In other words, we cannot call this function
224 * if there is no cgroup event for the current CPU context.
226 static inline struct perf_cgroup *
227 perf_cgroup_from_task(struct task_struct *task)
229 return container_of(task_subsys_state(task, perf_subsys_id),
230 struct perf_cgroup, css);
234 perf_cgroup_match(struct perf_event *event)
236 struct perf_event_context *ctx = event->ctx;
237 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
239 return !event->cgrp || event->cgrp == cpuctx->cgrp;
242 static inline void perf_get_cgroup(struct perf_event *event)
244 css_get(&event->cgrp->css);
247 static inline void perf_put_cgroup(struct perf_event *event)
249 css_put(&event->cgrp->css);
252 static inline void perf_detach_cgroup(struct perf_event *event)
254 perf_put_cgroup(event);
258 static inline int is_cgroup_event(struct perf_event *event)
260 return event->cgrp != NULL;
263 static inline u64 perf_cgroup_event_time(struct perf_event *event)
265 struct perf_cgroup_info *t;
267 t = per_cpu_ptr(event->cgrp->info, event->cpu);
271 static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
273 struct perf_cgroup_info *info;
278 info = this_cpu_ptr(cgrp->info);
280 info->time += now - info->timestamp;
281 info->timestamp = now;
284 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
286 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
288 __update_cgrp_time(cgrp_out);
291 static inline void update_cgrp_time_from_event(struct perf_event *event)
293 struct perf_cgroup *cgrp;
296 * ensure we access cgroup data only when needed and
297 * when we know the cgroup is pinned (css_get)
299 if (!is_cgroup_event(event))
302 cgrp = perf_cgroup_from_task(current);
304 * Do not update time when cgroup is not active
306 if (cgrp == event->cgrp)
307 __update_cgrp_time(event->cgrp);
311 perf_cgroup_set_timestamp(struct task_struct *task,
312 struct perf_event_context *ctx)
314 struct perf_cgroup *cgrp;
315 struct perf_cgroup_info *info;
318 * ctx->lock held by caller
319 * ensure we do not access cgroup data
320 * unless we have the cgroup pinned (css_get)
322 if (!task || !ctx->nr_cgroups)
325 cgrp = perf_cgroup_from_task(task);
326 info = this_cpu_ptr(cgrp->info);
327 info->timestamp = ctx->timestamp;
330 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
331 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
334 * reschedule events based on the cgroup constraint of task.
336 * mode SWOUT : schedule out everything
337 * mode SWIN : schedule in based on cgroup for next
339 void perf_cgroup_switch(struct task_struct *task, int mode)
341 struct perf_cpu_context *cpuctx;
346 * disable interrupts to avoid geting nr_cgroup
347 * changes via __perf_event_disable(). Also
350 local_irq_save(flags);
353 * we reschedule only in the presence of cgroup
354 * constrained events.
358 list_for_each_entry_rcu(pmu, &pmus, entry) {
359 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
362 * perf_cgroup_events says at least one
363 * context on this CPU has cgroup events.
365 * ctx->nr_cgroups reports the number of cgroup
366 * events for a context.
368 if (cpuctx->ctx.nr_cgroups > 0) {
369 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
370 perf_pmu_disable(cpuctx->ctx.pmu);
372 if (mode & PERF_CGROUP_SWOUT) {
373 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
375 * must not be done before ctxswout due
376 * to event_filter_match() in event_sched_out()
381 if (mode & PERF_CGROUP_SWIN) {
382 WARN_ON_ONCE(cpuctx->cgrp);
383 /* set cgrp before ctxsw in to
384 * allow event_filter_match() to not
385 * have to pass task around
387 cpuctx->cgrp = perf_cgroup_from_task(task);
388 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
390 perf_pmu_enable(cpuctx->ctx.pmu);
391 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
397 local_irq_restore(flags);
400 static inline void perf_cgroup_sched_out(struct task_struct *task)
402 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
405 static inline void perf_cgroup_sched_in(struct task_struct *task)
407 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
410 static inline int perf_cgroup_connect(int fd, struct perf_event *event,
411 struct perf_event_attr *attr,
412 struct perf_event *group_leader)
414 struct perf_cgroup *cgrp;
415 struct cgroup_subsys_state *css;
417 int ret = 0, fput_needed;
419 file = fget_light(fd, &fput_needed);
423 css = cgroup_css_from_dir(file, perf_subsys_id);
429 cgrp = container_of(css, struct perf_cgroup, css);
432 /* must be done before we fput() the file */
433 perf_get_cgroup(event);
436 * all events in a group must monitor
437 * the same cgroup because a task belongs
438 * to only one perf cgroup at a time
440 if (group_leader && group_leader->cgrp != cgrp) {
441 perf_detach_cgroup(event);
445 fput_light(file, fput_needed);
450 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
452 struct perf_cgroup_info *t;
453 t = per_cpu_ptr(event->cgrp->info, event->cpu);
454 event->shadow_ctx_time = now - t->timestamp;
458 perf_cgroup_defer_enabled(struct perf_event *event)
461 * when the current task's perf cgroup does not match
462 * the event's, we need to remember to call the
463 * perf_mark_enable() function the first time a task with
464 * a matching perf cgroup is scheduled in.
466 if (is_cgroup_event(event) && !perf_cgroup_match(event))
467 event->cgrp_defer_enabled = 1;
471 perf_cgroup_mark_enabled(struct perf_event *event,
472 struct perf_event_context *ctx)
474 struct perf_event *sub;
475 u64 tstamp = perf_event_time(event);
477 if (!event->cgrp_defer_enabled)
480 event->cgrp_defer_enabled = 0;
482 event->tstamp_enabled = tstamp - event->total_time_enabled;
483 list_for_each_entry(sub, &event->sibling_list, group_entry) {
484 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
485 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
486 sub->cgrp_defer_enabled = 0;
490 #else /* !CONFIG_CGROUP_PERF */
493 perf_cgroup_match(struct perf_event *event)
498 static inline void perf_detach_cgroup(struct perf_event *event)
501 static inline int is_cgroup_event(struct perf_event *event)
506 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
511 static inline void update_cgrp_time_from_event(struct perf_event *event)
515 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
519 static inline void perf_cgroup_sched_out(struct task_struct *task)
523 static inline void perf_cgroup_sched_in(struct task_struct *task)
527 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
528 struct perf_event_attr *attr,
529 struct perf_event *group_leader)
535 perf_cgroup_set_timestamp(struct task_struct *task,
536 struct perf_event_context *ctx)
541 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
546 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
550 static inline u64 perf_cgroup_event_time(struct perf_event *event)
556 perf_cgroup_defer_enabled(struct perf_event *event)
561 perf_cgroup_mark_enabled(struct perf_event *event,
562 struct perf_event_context *ctx)
567 void perf_pmu_disable(struct pmu *pmu)
569 int *count = this_cpu_ptr(pmu->pmu_disable_count);
571 pmu->pmu_disable(pmu);
574 void perf_pmu_enable(struct pmu *pmu)
576 int *count = this_cpu_ptr(pmu->pmu_disable_count);
578 pmu->pmu_enable(pmu);
581 static DEFINE_PER_CPU(struct list_head, rotation_list);
584 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
585 * because they're strictly cpu affine and rotate_start is called with IRQs
586 * disabled, while rotate_context is called from IRQ context.
588 static void perf_pmu_rotate_start(struct pmu *pmu)
590 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
591 struct list_head *head = &__get_cpu_var(rotation_list);
593 WARN_ON(!irqs_disabled());
595 if (list_empty(&cpuctx->rotation_list))
596 list_add(&cpuctx->rotation_list, head);
599 static void get_ctx(struct perf_event_context *ctx)
601 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
604 static void put_ctx(struct perf_event_context *ctx)
606 if (atomic_dec_and_test(&ctx->refcount)) {
608 put_ctx(ctx->parent_ctx);
610 put_task_struct(ctx->task);
611 kfree_rcu(ctx, rcu_head);
615 static void unclone_ctx(struct perf_event_context *ctx)
617 if (ctx->parent_ctx) {
618 put_ctx(ctx->parent_ctx);
619 ctx->parent_ctx = NULL;
623 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
626 * only top level events have the pid namespace they were created in
629 event = event->parent;
631 return task_tgid_nr_ns(p, event->ns);
634 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
637 * only top level events have the pid namespace they were created in
640 event = event->parent;
642 return task_pid_nr_ns(p, event->ns);
646 * If we inherit events we want to return the parent event id
649 static u64 primary_event_id(struct perf_event *event)
654 id = event->parent->id;
660 * Get the perf_event_context for a task and lock it.
661 * This has to cope with with the fact that until it is locked,
662 * the context could get moved to another task.
664 static struct perf_event_context *
665 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
667 struct perf_event_context *ctx;
671 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
674 * If this context is a clone of another, it might
675 * get swapped for another underneath us by
676 * perf_event_task_sched_out, though the
677 * rcu_read_lock() protects us from any context
678 * getting freed. Lock the context and check if it
679 * got swapped before we could get the lock, and retry
680 * if so. If we locked the right context, then it
681 * can't get swapped on us any more.
683 raw_spin_lock_irqsave(&ctx->lock, *flags);
684 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
685 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
689 if (!atomic_inc_not_zero(&ctx->refcount)) {
690 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
699 * Get the context for a task and increment its pin_count so it
700 * can't get swapped to another task. This also increments its
701 * reference count so that the context can't get freed.
703 static struct perf_event_context *
704 perf_pin_task_context(struct task_struct *task, int ctxn)
706 struct perf_event_context *ctx;
709 ctx = perf_lock_task_context(task, ctxn, &flags);
712 raw_spin_unlock_irqrestore(&ctx->lock, flags);
717 static void perf_unpin_context(struct perf_event_context *ctx)
721 raw_spin_lock_irqsave(&ctx->lock, flags);
723 raw_spin_unlock_irqrestore(&ctx->lock, flags);
727 * Update the record of the current time in a context.
729 static void update_context_time(struct perf_event_context *ctx)
731 u64 now = perf_clock();
733 ctx->time += now - ctx->timestamp;
734 ctx->timestamp = now;
737 static u64 perf_event_time(struct perf_event *event)
739 struct perf_event_context *ctx = event->ctx;
741 if (is_cgroup_event(event))
742 return perf_cgroup_event_time(event);
744 return ctx ? ctx->time : 0;
748 * Update the total_time_enabled and total_time_running fields for a event.
750 static void update_event_times(struct perf_event *event)
752 struct perf_event_context *ctx = event->ctx;
755 if (event->state < PERF_EVENT_STATE_INACTIVE ||
756 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
759 * in cgroup mode, time_enabled represents
760 * the time the event was enabled AND active
761 * tasks were in the monitored cgroup. This is
762 * independent of the activity of the context as
763 * there may be a mix of cgroup and non-cgroup events.
765 * That is why we treat cgroup events differently
768 if (is_cgroup_event(event))
769 run_end = perf_event_time(event);
770 else if (ctx->is_active)
773 run_end = event->tstamp_stopped;
775 event->total_time_enabled = run_end - event->tstamp_enabled;
777 if (event->state == PERF_EVENT_STATE_INACTIVE)
778 run_end = event->tstamp_stopped;
780 run_end = perf_event_time(event);
782 event->total_time_running = run_end - event->tstamp_running;
787 * Update total_time_enabled and total_time_running for all events in a group.
789 static void update_group_times(struct perf_event *leader)
791 struct perf_event *event;
793 update_event_times(leader);
794 list_for_each_entry(event, &leader->sibling_list, group_entry)
795 update_event_times(event);
798 static struct list_head *
799 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
801 if (event->attr.pinned)
802 return &ctx->pinned_groups;
804 return &ctx->flexible_groups;
808 * Add a event from the lists for its context.
809 * Must be called with ctx->mutex and ctx->lock held.
812 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
814 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
815 event->attach_state |= PERF_ATTACH_CONTEXT;
818 * If we're a stand alone event or group leader, we go to the context
819 * list, group events are kept attached to the group so that
820 * perf_group_detach can, at all times, locate all siblings.
822 if (event->group_leader == event) {
823 struct list_head *list;
825 if (is_software_event(event))
826 event->group_flags |= PERF_GROUP_SOFTWARE;
828 list = ctx_group_list(event, ctx);
829 list_add_tail(&event->group_entry, list);
832 if (is_cgroup_event(event))
835 list_add_rcu(&event->event_entry, &ctx->event_list);
837 perf_pmu_rotate_start(ctx->pmu);
839 if (event->attr.inherit_stat)
844 * Called at perf_event creation and when events are attached/detached from a
847 static void perf_event__read_size(struct perf_event *event)
849 int entry = sizeof(u64); /* value */
853 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
856 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
859 if (event->attr.read_format & PERF_FORMAT_ID)
860 entry += sizeof(u64);
862 if (event->attr.read_format & PERF_FORMAT_GROUP) {
863 nr += event->group_leader->nr_siblings;
868 event->read_size = size;
871 static void perf_event__header_size(struct perf_event *event)
873 struct perf_sample_data *data;
874 u64 sample_type = event->attr.sample_type;
877 perf_event__read_size(event);
879 if (sample_type & PERF_SAMPLE_IP)
880 size += sizeof(data->ip);
882 if (sample_type & PERF_SAMPLE_ADDR)
883 size += sizeof(data->addr);
885 if (sample_type & PERF_SAMPLE_PERIOD)
886 size += sizeof(data->period);
888 if (sample_type & PERF_SAMPLE_READ)
889 size += event->read_size;
891 event->header_size = size;
894 static void perf_event__id_header_size(struct perf_event *event)
896 struct perf_sample_data *data;
897 u64 sample_type = event->attr.sample_type;
900 if (sample_type & PERF_SAMPLE_TID)
901 size += sizeof(data->tid_entry);
903 if (sample_type & PERF_SAMPLE_TIME)
904 size += sizeof(data->time);
906 if (sample_type & PERF_SAMPLE_ID)
907 size += sizeof(data->id);
909 if (sample_type & PERF_SAMPLE_STREAM_ID)
910 size += sizeof(data->stream_id);
912 if (sample_type & PERF_SAMPLE_CPU)
913 size += sizeof(data->cpu_entry);
915 event->id_header_size = size;
918 static void perf_group_attach(struct perf_event *event)
920 struct perf_event *group_leader = event->group_leader, *pos;
923 * We can have double attach due to group movement in perf_event_open.
925 if (event->attach_state & PERF_ATTACH_GROUP)
928 event->attach_state |= PERF_ATTACH_GROUP;
930 if (group_leader == event)
933 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
934 !is_software_event(event))
935 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
937 list_add_tail(&event->group_entry, &group_leader->sibling_list);
938 group_leader->nr_siblings++;
940 perf_event__header_size(group_leader);
942 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
943 perf_event__header_size(pos);
947 * Remove a event from the lists for its context.
948 * Must be called with ctx->mutex and ctx->lock held.
951 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
953 struct perf_cpu_context *cpuctx;
955 * We can have double detach due to exit/hot-unplug + close.
957 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
960 event->attach_state &= ~PERF_ATTACH_CONTEXT;
962 if (is_cgroup_event(event)) {
964 cpuctx = __get_cpu_context(ctx);
966 * if there are no more cgroup events
967 * then cler cgrp to avoid stale pointer
968 * in update_cgrp_time_from_cpuctx()
970 if (!ctx->nr_cgroups)
975 if (event->attr.inherit_stat)
978 list_del_rcu(&event->event_entry);
980 if (event->group_leader == event)
981 list_del_init(&event->group_entry);
983 update_group_times(event);
986 * If event was in error state, then keep it
987 * that way, otherwise bogus counts will be
988 * returned on read(). The only way to get out
989 * of error state is by explicit re-enabling
992 if (event->state > PERF_EVENT_STATE_OFF)
993 event->state = PERF_EVENT_STATE_OFF;
996 static void perf_group_detach(struct perf_event *event)
998 struct perf_event *sibling, *tmp;
999 struct list_head *list = NULL;
1002 * We can have double detach due to exit/hot-unplug + close.
1004 if (!(event->attach_state & PERF_ATTACH_GROUP))
1007 event->attach_state &= ~PERF_ATTACH_GROUP;
1010 * If this is a sibling, remove it from its group.
1012 if (event->group_leader != event) {
1013 list_del_init(&event->group_entry);
1014 event->group_leader->nr_siblings--;
1018 if (!list_empty(&event->group_entry))
1019 list = &event->group_entry;
1022 * If this was a group event with sibling events then
1023 * upgrade the siblings to singleton events by adding them
1024 * to whatever list we are on.
1026 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
1028 list_move_tail(&sibling->group_entry, list);
1029 sibling->group_leader = sibling;
1031 /* Inherit group flags from the previous leader */
1032 sibling->group_flags = event->group_flags;
1036 perf_event__header_size(event->group_leader);
1038 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1039 perf_event__header_size(tmp);
1043 event_filter_match(struct perf_event *event)
1045 return (event->cpu == -1 || event->cpu == smp_processor_id())
1046 && perf_cgroup_match(event);
1050 event_sched_out(struct perf_event *event,
1051 struct perf_cpu_context *cpuctx,
1052 struct perf_event_context *ctx)
1054 u64 tstamp = perf_event_time(event);
1057 * An event which could not be activated because of
1058 * filter mismatch still needs to have its timings
1059 * maintained, otherwise bogus information is return
1060 * via read() for time_enabled, time_running:
1062 if (event->state == PERF_EVENT_STATE_INACTIVE
1063 && !event_filter_match(event)) {
1064 delta = tstamp - event->tstamp_stopped;
1065 event->tstamp_running += delta;
1066 event->tstamp_stopped = tstamp;
1069 if (event->state != PERF_EVENT_STATE_ACTIVE)
1072 event->state = PERF_EVENT_STATE_INACTIVE;
1073 if (event->pending_disable) {
1074 event->pending_disable = 0;
1075 event->state = PERF_EVENT_STATE_OFF;
1077 event->tstamp_stopped = tstamp;
1078 event->pmu->del(event, 0);
1081 if (!is_software_event(event))
1082 cpuctx->active_oncpu--;
1084 if (event->attr.exclusive || !cpuctx->active_oncpu)
1085 cpuctx->exclusive = 0;
1089 group_sched_out(struct perf_event *group_event,
1090 struct perf_cpu_context *cpuctx,
1091 struct perf_event_context *ctx)
1093 struct perf_event *event;
1094 int state = group_event->state;
1096 event_sched_out(group_event, cpuctx, ctx);
1099 * Schedule out siblings (if any):
1101 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1102 event_sched_out(event, cpuctx, ctx);
1104 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
1105 cpuctx->exclusive = 0;
1109 * Cross CPU call to remove a performance event
1111 * We disable the event on the hardware level first. After that we
1112 * remove it from the context list.
1114 static int __perf_remove_from_context(void *info)
1116 struct perf_event *event = info;
1117 struct perf_event_context *ctx = event->ctx;
1118 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1120 raw_spin_lock(&ctx->lock);
1121 event_sched_out(event, cpuctx, ctx);
1122 list_del_event(event, ctx);
1123 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1125 cpuctx->task_ctx = NULL;
1127 raw_spin_unlock(&ctx->lock);
1134 * Remove the event from a task's (or a CPU's) list of events.
1136 * CPU events are removed with a smp call. For task events we only
1137 * call when the task is on a CPU.
1139 * If event->ctx is a cloned context, callers must make sure that
1140 * every task struct that event->ctx->task could possibly point to
1141 * remains valid. This is OK when called from perf_release since
1142 * that only calls us on the top-level context, which can't be a clone.
1143 * When called from perf_event_exit_task, it's OK because the
1144 * context has been detached from its task.
1146 static void perf_remove_from_context(struct perf_event *event)
1148 struct perf_event_context *ctx = event->ctx;
1149 struct task_struct *task = ctx->task;
1151 lockdep_assert_held(&ctx->mutex);
1155 * Per cpu events are removed via an smp call and
1156 * the removal is always successful.
1158 cpu_function_call(event->cpu, __perf_remove_from_context, event);
1163 if (!task_function_call(task, __perf_remove_from_context, event))
1166 raw_spin_lock_irq(&ctx->lock);
1168 * If we failed to find a running task, but find the context active now
1169 * that we've acquired the ctx->lock, retry.
1171 if (ctx->is_active) {
1172 raw_spin_unlock_irq(&ctx->lock);
1177 * Since the task isn't running, its safe to remove the event, us
1178 * holding the ctx->lock ensures the task won't get scheduled in.
1180 list_del_event(event, ctx);
1181 raw_spin_unlock_irq(&ctx->lock);
1185 * Cross CPU call to disable a performance event
1187 static int __perf_event_disable(void *info)
1189 struct perf_event *event = info;
1190 struct perf_event_context *ctx = event->ctx;
1191 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1194 * If this is a per-task event, need to check whether this
1195 * event's task is the current task on this cpu.
1197 * Can trigger due to concurrent perf_event_context_sched_out()
1198 * flipping contexts around.
1200 if (ctx->task && cpuctx->task_ctx != ctx)
1203 raw_spin_lock(&ctx->lock);
1206 * If the event is on, turn it off.
1207 * If it is in error state, leave it in error state.
1209 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1210 update_context_time(ctx);
1211 update_cgrp_time_from_event(event);
1212 update_group_times(event);
1213 if (event == event->group_leader)
1214 group_sched_out(event, cpuctx, ctx);
1216 event_sched_out(event, cpuctx, ctx);
1217 event->state = PERF_EVENT_STATE_OFF;
1220 raw_spin_unlock(&ctx->lock);
1228 * If event->ctx is a cloned context, callers must make sure that
1229 * every task struct that event->ctx->task could possibly point to
1230 * remains valid. This condition is satisifed when called through
1231 * perf_event_for_each_child or perf_event_for_each because they
1232 * hold the top-level event's child_mutex, so any descendant that
1233 * goes to exit will block in sync_child_event.
1234 * When called from perf_pending_event it's OK because event->ctx
1235 * is the current context on this CPU and preemption is disabled,
1236 * hence we can't get into perf_event_task_sched_out for this context.
1238 void perf_event_disable(struct perf_event *event)
1240 struct perf_event_context *ctx = event->ctx;
1241 struct task_struct *task = ctx->task;
1245 * Disable the event on the cpu that it's on
1247 cpu_function_call(event->cpu, __perf_event_disable, event);
1252 if (!task_function_call(task, __perf_event_disable, event))
1255 raw_spin_lock_irq(&ctx->lock);
1257 * If the event is still active, we need to retry the cross-call.
1259 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1260 raw_spin_unlock_irq(&ctx->lock);
1262 * Reload the task pointer, it might have been changed by
1263 * a concurrent perf_event_context_sched_out().
1270 * Since we have the lock this context can't be scheduled
1271 * in, so we can change the state safely.
1273 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1274 update_group_times(event);
1275 event->state = PERF_EVENT_STATE_OFF;
1277 raw_spin_unlock_irq(&ctx->lock);
1280 static void perf_set_shadow_time(struct perf_event *event,
1281 struct perf_event_context *ctx,
1285 * use the correct time source for the time snapshot
1287 * We could get by without this by leveraging the
1288 * fact that to get to this function, the caller
1289 * has most likely already called update_context_time()
1290 * and update_cgrp_time_xx() and thus both timestamp
1291 * are identical (or very close). Given that tstamp is,
1292 * already adjusted for cgroup, we could say that:
1293 * tstamp - ctx->timestamp
1295 * tstamp - cgrp->timestamp.
1297 * Then, in perf_output_read(), the calculation would
1298 * work with no changes because:
1299 * - event is guaranteed scheduled in
1300 * - no scheduled out in between
1301 * - thus the timestamp would be the same
1303 * But this is a bit hairy.
1305 * So instead, we have an explicit cgroup call to remain
1306 * within the time time source all along. We believe it
1307 * is cleaner and simpler to understand.
1309 if (is_cgroup_event(event))
1310 perf_cgroup_set_shadow_time(event, tstamp);
1312 event->shadow_ctx_time = tstamp - ctx->timestamp;
1315 #define MAX_INTERRUPTS (~0ULL)
1317 static void perf_log_throttle(struct perf_event *event, int enable);
1320 event_sched_in(struct perf_event *event,
1321 struct perf_cpu_context *cpuctx,
1322 struct perf_event_context *ctx)
1324 u64 tstamp = perf_event_time(event);
1326 if (event->state <= PERF_EVENT_STATE_OFF)
1329 event->state = PERF_EVENT_STATE_ACTIVE;
1330 event->oncpu = smp_processor_id();
1333 * Unthrottle events, since we scheduled we might have missed several
1334 * ticks already, also for a heavily scheduling task there is little
1335 * guarantee it'll get a tick in a timely manner.
1337 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1338 perf_log_throttle(event, 1);
1339 event->hw.interrupts = 0;
1343 * The new state must be visible before we turn it on in the hardware:
1347 if (event->pmu->add(event, PERF_EF_START)) {
1348 event->state = PERF_EVENT_STATE_INACTIVE;
1353 event->tstamp_running += tstamp - event->tstamp_stopped;
1355 perf_set_shadow_time(event, ctx, tstamp);
1357 if (!is_software_event(event))
1358 cpuctx->active_oncpu++;
1361 if (event->attr.exclusive)
1362 cpuctx->exclusive = 1;
1368 group_sched_in(struct perf_event *group_event,
1369 struct perf_cpu_context *cpuctx,
1370 struct perf_event_context *ctx)
1372 struct perf_event *event, *partial_group = NULL;
1373 struct pmu *pmu = group_event->pmu;
1374 u64 now = ctx->time;
1375 bool simulate = false;
1377 if (group_event->state == PERF_EVENT_STATE_OFF)
1380 pmu->start_txn(pmu);
1382 if (event_sched_in(group_event, cpuctx, ctx)) {
1383 pmu->cancel_txn(pmu);
1388 * Schedule in siblings as one group (if any):
1390 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1391 if (event_sched_in(event, cpuctx, ctx)) {
1392 partial_group = event;
1397 if (!pmu->commit_txn(pmu))
1402 * Groups can be scheduled in as one unit only, so undo any
1403 * partial group before returning:
1404 * The events up to the failed event are scheduled out normally,
1405 * tstamp_stopped will be updated.
1407 * The failed events and the remaining siblings need to have
1408 * their timings updated as if they had gone thru event_sched_in()
1409 * and event_sched_out(). This is required to get consistent timings
1410 * across the group. This also takes care of the case where the group
1411 * could never be scheduled by ensuring tstamp_stopped is set to mark
1412 * the time the event was actually stopped, such that time delta
1413 * calculation in update_event_times() is correct.
1415 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1416 if (event == partial_group)
1420 event->tstamp_running += now - event->tstamp_stopped;
1421 event->tstamp_stopped = now;
1423 event_sched_out(event, cpuctx, ctx);
1426 event_sched_out(group_event, cpuctx, ctx);
1428 pmu->cancel_txn(pmu);
1434 * Work out whether we can put this event group on the CPU now.
1436 static int group_can_go_on(struct perf_event *event,
1437 struct perf_cpu_context *cpuctx,
1441 * Groups consisting entirely of software events can always go on.
1443 if (event->group_flags & PERF_GROUP_SOFTWARE)
1446 * If an exclusive group is already on, no other hardware
1449 if (cpuctx->exclusive)
1452 * If this group is exclusive and there are already
1453 * events on the CPU, it can't go on.
1455 if (event->attr.exclusive && cpuctx->active_oncpu)
1458 * Otherwise, try to add it if all previous groups were able
1464 static void add_event_to_ctx(struct perf_event *event,
1465 struct perf_event_context *ctx)
1467 u64 tstamp = perf_event_time(event);
1469 list_add_event(event, ctx);
1470 perf_group_attach(event);
1471 event->tstamp_enabled = tstamp;
1472 event->tstamp_running = tstamp;
1473 event->tstamp_stopped = tstamp;
1476 static void task_ctx_sched_out(struct perf_event_context *ctx);
1478 ctx_sched_in(struct perf_event_context *ctx,
1479 struct perf_cpu_context *cpuctx,
1480 enum event_type_t event_type,
1481 struct task_struct *task);
1483 static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1484 struct perf_event_context *ctx,
1485 struct task_struct *task)
1487 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1489 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1490 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1492 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1496 * Cross CPU call to install and enable a performance event
1498 * Must be called with ctx->mutex held
1500 static int __perf_install_in_context(void *info)
1502 struct perf_event *event = info;
1503 struct perf_event_context *ctx = event->ctx;
1504 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1505 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1506 struct task_struct *task = current;
1508 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
1509 perf_pmu_disable(cpuctx->ctx.pmu);
1512 * If there was an active task_ctx schedule it out.
1515 task_ctx_sched_out(task_ctx);
1517 * If the context we're installing events in is not the
1518 * active task_ctx, flip them.
1520 if (ctx->task && task_ctx != ctx) {
1521 raw_spin_unlock(&cpuctx->ctx.lock);
1522 raw_spin_lock(&ctx->lock);
1523 cpuctx->task_ctx = task_ctx = ctx;
1525 task = task_ctx->task;
1527 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
1529 update_context_time(ctx);
1531 * update cgrp time only if current cgrp
1532 * matches event->cgrp. Must be done before
1533 * calling add_event_to_ctx()
1535 update_cgrp_time_from_event(event);
1537 add_event_to_ctx(event, ctx);
1540 * Schedule everything back in
1542 perf_event_sched_in(cpuctx, task_ctx, task);
1544 perf_pmu_enable(cpuctx->ctx.pmu);
1545 perf_ctx_unlock(cpuctx, task_ctx);
1551 * Attach a performance event to a context
1553 * First we add the event to the list with the hardware enable bit
1554 * in event->hw_config cleared.
1556 * If the event is attached to a task which is on a CPU we use a smp
1557 * call to enable it in the task context. The task might have been
1558 * scheduled away, but we check this in the smp call again.
1561 perf_install_in_context(struct perf_event_context *ctx,
1562 struct perf_event *event,
1565 struct task_struct *task = ctx->task;
1567 lockdep_assert_held(&ctx->mutex);
1573 * Per cpu events are installed via an smp call and
1574 * the install is always successful.
1576 cpu_function_call(cpu, __perf_install_in_context, event);
1581 if (!task_function_call(task, __perf_install_in_context, event))
1584 raw_spin_lock_irq(&ctx->lock);
1586 * If we failed to find a running task, but find the context active now
1587 * that we've acquired the ctx->lock, retry.
1589 if (ctx->is_active) {
1590 raw_spin_unlock_irq(&ctx->lock);
1595 * Since the task isn't running, its safe to add the event, us holding
1596 * the ctx->lock ensures the task won't get scheduled in.
1598 add_event_to_ctx(event, ctx);
1599 raw_spin_unlock_irq(&ctx->lock);
1603 * Put a event into inactive state and update time fields.
1604 * Enabling the leader of a group effectively enables all
1605 * the group members that aren't explicitly disabled, so we
1606 * have to update their ->tstamp_enabled also.
1607 * Note: this works for group members as well as group leaders
1608 * since the non-leader members' sibling_lists will be empty.
1610 static void __perf_event_mark_enabled(struct perf_event *event,
1611 struct perf_event_context *ctx)
1613 struct perf_event *sub;
1614 u64 tstamp = perf_event_time(event);
1616 event->state = PERF_EVENT_STATE_INACTIVE;
1617 event->tstamp_enabled = tstamp - event->total_time_enabled;
1618 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1619 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1620 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1625 * Cross CPU call to enable a performance event
1627 static int __perf_event_enable(void *info)
1629 struct perf_event *event = info;
1630 struct perf_event_context *ctx = event->ctx;
1631 struct perf_event *leader = event->group_leader;
1632 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1635 if (WARN_ON_ONCE(!ctx->is_active))
1638 raw_spin_lock(&ctx->lock);
1639 update_context_time(ctx);
1641 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1645 * set current task's cgroup time reference point
1647 perf_cgroup_set_timestamp(current, ctx);
1649 __perf_event_mark_enabled(event, ctx);
1651 if (!event_filter_match(event)) {
1652 if (is_cgroup_event(event))
1653 perf_cgroup_defer_enabled(event);
1658 * If the event is in a group and isn't the group leader,
1659 * then don't put it on unless the group is on.
1661 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1664 if (!group_can_go_on(event, cpuctx, 1)) {
1667 if (event == leader)
1668 err = group_sched_in(event, cpuctx, ctx);
1670 err = event_sched_in(event, cpuctx, ctx);
1675 * If this event can't go on and it's part of a
1676 * group, then the whole group has to come off.
1678 if (leader != event)
1679 group_sched_out(leader, cpuctx, ctx);
1680 if (leader->attr.pinned) {
1681 update_group_times(leader);
1682 leader->state = PERF_EVENT_STATE_ERROR;
1687 raw_spin_unlock(&ctx->lock);
1695 * If event->ctx is a cloned context, callers must make sure that
1696 * every task struct that event->ctx->task could possibly point to
1697 * remains valid. This condition is satisfied when called through
1698 * perf_event_for_each_child or perf_event_for_each as described
1699 * for perf_event_disable.
1701 void perf_event_enable(struct perf_event *event)
1703 struct perf_event_context *ctx = event->ctx;
1704 struct task_struct *task = ctx->task;
1708 * Enable the event on the cpu that it's on
1710 cpu_function_call(event->cpu, __perf_event_enable, event);
1714 raw_spin_lock_irq(&ctx->lock);
1715 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1719 * If the event is in error state, clear that first.
1720 * That way, if we see the event in error state below, we
1721 * know that it has gone back into error state, as distinct
1722 * from the task having been scheduled away before the
1723 * cross-call arrived.
1725 if (event->state == PERF_EVENT_STATE_ERROR)
1726 event->state = PERF_EVENT_STATE_OFF;
1729 if (!ctx->is_active) {
1730 __perf_event_mark_enabled(event, ctx);
1734 raw_spin_unlock_irq(&ctx->lock);
1736 if (!task_function_call(task, __perf_event_enable, event))
1739 raw_spin_lock_irq(&ctx->lock);
1742 * If the context is active and the event is still off,
1743 * we need to retry the cross-call.
1745 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1747 * task could have been flipped by a concurrent
1748 * perf_event_context_sched_out()
1755 raw_spin_unlock_irq(&ctx->lock);
1758 static int perf_event_refresh(struct perf_event *event, int refresh)
1761 * not supported on inherited events
1763 if (event->attr.inherit || !is_sampling_event(event))
1766 atomic_add(refresh, &event->event_limit);
1767 perf_event_enable(event);
1772 static void ctx_sched_out(struct perf_event_context *ctx,
1773 struct perf_cpu_context *cpuctx,
1774 enum event_type_t event_type)
1776 struct perf_event *event;
1777 int is_active = ctx->is_active;
1779 ctx->is_active &= ~event_type;
1780 if (likely(!ctx->nr_events))
1783 update_context_time(ctx);
1784 update_cgrp_time_from_cpuctx(cpuctx);
1785 if (!ctx->nr_active)
1788 perf_pmu_disable(ctx->pmu);
1789 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
1790 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1791 group_sched_out(event, cpuctx, ctx);
1794 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
1795 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1796 group_sched_out(event, cpuctx, ctx);
1798 perf_pmu_enable(ctx->pmu);
1802 * Test whether two contexts are equivalent, i.e. whether they
1803 * have both been cloned from the same version of the same context
1804 * and they both have the same number of enabled events.
1805 * If the number of enabled events is the same, then the set
1806 * of enabled events should be the same, because these are both
1807 * inherited contexts, therefore we can't access individual events
1808 * in them directly with an fd; we can only enable/disable all
1809 * events via prctl, or enable/disable all events in a family
1810 * via ioctl, which will have the same effect on both contexts.
1812 static int context_equiv(struct perf_event_context *ctx1,
1813 struct perf_event_context *ctx2)
1815 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1816 && ctx1->parent_gen == ctx2->parent_gen
1817 && !ctx1->pin_count && !ctx2->pin_count;
1820 static void __perf_event_sync_stat(struct perf_event *event,
1821 struct perf_event *next_event)
1825 if (!event->attr.inherit_stat)
1829 * Update the event value, we cannot use perf_event_read()
1830 * because we're in the middle of a context switch and have IRQs
1831 * disabled, which upsets smp_call_function_single(), however
1832 * we know the event must be on the current CPU, therefore we
1833 * don't need to use it.
1835 switch (event->state) {
1836 case PERF_EVENT_STATE_ACTIVE:
1837 event->pmu->read(event);
1840 case PERF_EVENT_STATE_INACTIVE:
1841 update_event_times(event);
1849 * In order to keep per-task stats reliable we need to flip the event
1850 * values when we flip the contexts.
1852 value = local64_read(&next_event->count);
1853 value = local64_xchg(&event->count, value);
1854 local64_set(&next_event->count, value);
1856 swap(event->total_time_enabled, next_event->total_time_enabled);
1857 swap(event->total_time_running, next_event->total_time_running);
1860 * Since we swizzled the values, update the user visible data too.
1862 perf_event_update_userpage(event);
1863 perf_event_update_userpage(next_event);
1866 #define list_next_entry(pos, member) \
1867 list_entry(pos->member.next, typeof(*pos), member)
1869 static void perf_event_sync_stat(struct perf_event_context *ctx,
1870 struct perf_event_context *next_ctx)
1872 struct perf_event *event, *next_event;
1877 update_context_time(ctx);
1879 event = list_first_entry(&ctx->event_list,
1880 struct perf_event, event_entry);
1882 next_event = list_first_entry(&next_ctx->event_list,
1883 struct perf_event, event_entry);
1885 while (&event->event_entry != &ctx->event_list &&
1886 &next_event->event_entry != &next_ctx->event_list) {
1888 __perf_event_sync_stat(event, next_event);
1890 event = list_next_entry(event, event_entry);
1891 next_event = list_next_entry(next_event, event_entry);
1895 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1896 struct task_struct *next)
1898 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1899 struct perf_event_context *next_ctx;
1900 struct perf_event_context *parent;
1901 struct perf_cpu_context *cpuctx;
1907 cpuctx = __get_cpu_context(ctx);
1908 if (!cpuctx->task_ctx)
1912 parent = rcu_dereference(ctx->parent_ctx);
1913 next_ctx = next->perf_event_ctxp[ctxn];
1914 if (parent && next_ctx &&
1915 rcu_dereference(next_ctx->parent_ctx) == parent) {
1917 * Looks like the two contexts are clones, so we might be
1918 * able to optimize the context switch. We lock both
1919 * contexts and check that they are clones under the
1920 * lock (including re-checking that neither has been
1921 * uncloned in the meantime). It doesn't matter which
1922 * order we take the locks because no other cpu could
1923 * be trying to lock both of these tasks.
1925 raw_spin_lock(&ctx->lock);
1926 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1927 if (context_equiv(ctx, next_ctx)) {
1929 * XXX do we need a memory barrier of sorts
1930 * wrt to rcu_dereference() of perf_event_ctxp
1932 task->perf_event_ctxp[ctxn] = next_ctx;
1933 next->perf_event_ctxp[ctxn] = ctx;
1935 next_ctx->task = task;
1938 perf_event_sync_stat(ctx, next_ctx);
1940 raw_spin_unlock(&next_ctx->lock);
1941 raw_spin_unlock(&ctx->lock);
1946 raw_spin_lock(&ctx->lock);
1947 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1948 cpuctx->task_ctx = NULL;
1949 raw_spin_unlock(&ctx->lock);
1953 #define for_each_task_context_nr(ctxn) \
1954 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1957 * Called from scheduler to remove the events of the current task,
1958 * with interrupts disabled.
1960 * We stop each event and update the event value in event->count.
1962 * This does not protect us against NMI, but disable()
1963 * sets the disabled bit in the control field of event _before_
1964 * accessing the event control register. If a NMI hits, then it will
1965 * not restart the event.
1967 void __perf_event_task_sched_out(struct task_struct *task,
1968 struct task_struct *next)
1972 for_each_task_context_nr(ctxn)
1973 perf_event_context_sched_out(task, ctxn, next);
1976 * if cgroup events exist on this CPU, then we need
1977 * to check if we have to switch out PMU state.
1978 * cgroup event are system-wide mode only
1980 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
1981 perf_cgroup_sched_out(task);
1984 static void task_ctx_sched_out(struct perf_event_context *ctx)
1986 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1988 if (!cpuctx->task_ctx)
1991 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1994 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1995 cpuctx->task_ctx = NULL;
1999 * Called with IRQs disabled
2001 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2002 enum event_type_t event_type)
2004 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
2008 ctx_pinned_sched_in(struct perf_event_context *ctx,
2009 struct perf_cpu_context *cpuctx)
2011 struct perf_event *event;
2013 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2014 if (event->state <= PERF_EVENT_STATE_OFF)
2016 if (!event_filter_match(event))
2019 /* may need to reset tstamp_enabled */
2020 if (is_cgroup_event(event))
2021 perf_cgroup_mark_enabled(event, ctx);
2023 if (group_can_go_on(event, cpuctx, 1))
2024 group_sched_in(event, cpuctx, ctx);
2027 * If this pinned group hasn't been scheduled,
2028 * put it in error state.
2030 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2031 update_group_times(event);
2032 event->state = PERF_EVENT_STATE_ERROR;
2038 ctx_flexible_sched_in(struct perf_event_context *ctx,
2039 struct perf_cpu_context *cpuctx)
2041 struct perf_event *event;
2044 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2045 /* Ignore events in OFF or ERROR state */
2046 if (event->state <= PERF_EVENT_STATE_OFF)
2049 * Listen to the 'cpu' scheduling filter constraint
2052 if (!event_filter_match(event))
2055 /* may need to reset tstamp_enabled */
2056 if (is_cgroup_event(event))
2057 perf_cgroup_mark_enabled(event, ctx);
2059 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2060 if (group_sched_in(event, cpuctx, ctx))
2067 ctx_sched_in(struct perf_event_context *ctx,
2068 struct perf_cpu_context *cpuctx,
2069 enum event_type_t event_type,
2070 struct task_struct *task)
2073 int is_active = ctx->is_active;
2075 ctx->is_active |= event_type;
2076 if (likely(!ctx->nr_events))
2080 ctx->timestamp = now;
2081 perf_cgroup_set_timestamp(task, ctx);
2083 * First go through the list and put on any pinned groups
2084 * in order to give them the best chance of going on.
2086 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
2087 ctx_pinned_sched_in(ctx, cpuctx);
2089 /* Then walk through the lower prio flexible groups */
2090 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
2091 ctx_flexible_sched_in(ctx, cpuctx);
2094 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2095 enum event_type_t event_type,
2096 struct task_struct *task)
2098 struct perf_event_context *ctx = &cpuctx->ctx;
2100 ctx_sched_in(ctx, cpuctx, event_type, task);
2103 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2104 struct task_struct *task)
2106 struct perf_cpu_context *cpuctx;
2108 cpuctx = __get_cpu_context(ctx);
2109 if (cpuctx->task_ctx == ctx)
2112 perf_ctx_lock(cpuctx, ctx);
2113 perf_pmu_disable(ctx->pmu);
2115 * We want to keep the following priority order:
2116 * cpu pinned (that don't need to move), task pinned,
2117 * cpu flexible, task flexible.
2119 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2121 perf_event_sched_in(cpuctx, ctx, task);
2123 cpuctx->task_ctx = ctx;
2125 perf_pmu_enable(ctx->pmu);
2126 perf_ctx_unlock(cpuctx, ctx);
2129 * Since these rotations are per-cpu, we need to ensure the
2130 * cpu-context we got scheduled on is actually rotating.
2132 perf_pmu_rotate_start(ctx->pmu);
2136 * Called from scheduler to add the events of the current task
2137 * with interrupts disabled.
2139 * We restore the event value and then enable it.
2141 * This does not protect us against NMI, but enable()
2142 * sets the enabled bit in the control field of event _before_
2143 * accessing the event control register. If a NMI hits, then it will
2144 * keep the event running.
2146 void __perf_event_task_sched_in(struct task_struct *task)
2148 struct perf_event_context *ctx;
2151 for_each_task_context_nr(ctxn) {
2152 ctx = task->perf_event_ctxp[ctxn];
2156 perf_event_context_sched_in(ctx, task);
2159 * if cgroup events exist on this CPU, then we need
2160 * to check if we have to switch in PMU state.
2161 * cgroup event are system-wide mode only
2163 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2164 perf_cgroup_sched_in(task);
2167 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2169 u64 frequency = event->attr.sample_freq;
2170 u64 sec = NSEC_PER_SEC;
2171 u64 divisor, dividend;
2173 int count_fls, nsec_fls, frequency_fls, sec_fls;
2175 count_fls = fls64(count);
2176 nsec_fls = fls64(nsec);
2177 frequency_fls = fls64(frequency);
2181 * We got @count in @nsec, with a target of sample_freq HZ
2182 * the target period becomes:
2185 * period = -------------------
2186 * @nsec * sample_freq
2191 * Reduce accuracy by one bit such that @a and @b converge
2192 * to a similar magnitude.
2194 #define REDUCE_FLS(a, b) \
2196 if (a##_fls > b##_fls) { \
2206 * Reduce accuracy until either term fits in a u64, then proceed with
2207 * the other, so that finally we can do a u64/u64 division.
2209 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2210 REDUCE_FLS(nsec, frequency);
2211 REDUCE_FLS(sec, count);
2214 if (count_fls + sec_fls > 64) {
2215 divisor = nsec * frequency;
2217 while (count_fls + sec_fls > 64) {
2218 REDUCE_FLS(count, sec);
2222 dividend = count * sec;
2224 dividend = count * sec;
2226 while (nsec_fls + frequency_fls > 64) {
2227 REDUCE_FLS(nsec, frequency);
2231 divisor = nsec * frequency;
2237 return div64_u64(dividend, divisor);
2240 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
2242 struct hw_perf_event *hwc = &event->hw;
2243 s64 period, sample_period;
2246 period = perf_calculate_period(event, nsec, count);
2248 delta = (s64)(period - hwc->sample_period);
2249 delta = (delta + 7) / 8; /* low pass filter */
2251 sample_period = hwc->sample_period + delta;
2256 hwc->sample_period = sample_period;
2258 if (local64_read(&hwc->period_left) > 8*sample_period) {
2259 event->pmu->stop(event, PERF_EF_UPDATE);
2260 local64_set(&hwc->period_left, 0);
2261 event->pmu->start(event, PERF_EF_RELOAD);
2265 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
2267 struct perf_event *event;
2268 struct hw_perf_event *hwc;
2269 u64 interrupts, now;
2272 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2273 if (event->state != PERF_EVENT_STATE_ACTIVE)
2276 if (!event_filter_match(event))
2281 interrupts = hwc->interrupts;
2282 hwc->interrupts = 0;
2285 * unthrottle events on the tick
2287 if (interrupts == MAX_INTERRUPTS) {
2288 perf_log_throttle(event, 1);
2289 event->pmu->start(event, 0);
2292 if (!event->attr.freq || !event->attr.sample_freq)
2295 event->pmu->read(event);
2296 now = local64_read(&event->count);
2297 delta = now - hwc->freq_count_stamp;
2298 hwc->freq_count_stamp = now;
2301 perf_adjust_period(event, period, delta);
2306 * Round-robin a context's events:
2308 static void rotate_ctx(struct perf_event_context *ctx)
2311 * Rotate the first entry last of non-pinned groups. Rotation might be
2312 * disabled by the inheritance code.
2314 if (!ctx->rotate_disable)
2315 list_rotate_left(&ctx->flexible_groups);
2319 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2320 * because they're strictly cpu affine and rotate_start is called with IRQs
2321 * disabled, while rotate_context is called from IRQ context.
2323 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
2325 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
2326 struct perf_event_context *ctx = NULL;
2327 int rotate = 0, remove = 1;
2329 if (cpuctx->ctx.nr_events) {
2331 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2335 ctx = cpuctx->task_ctx;
2336 if (ctx && ctx->nr_events) {
2338 if (ctx->nr_events != ctx->nr_active)
2342 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2343 perf_pmu_disable(cpuctx->ctx.pmu);
2344 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
2346 perf_ctx_adjust_freq(ctx, interval);
2351 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2353 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
2355 rotate_ctx(&cpuctx->ctx);
2359 perf_event_sched_in(cpuctx, ctx, current);
2363 list_del_init(&cpuctx->rotation_list);
2365 perf_pmu_enable(cpuctx->ctx.pmu);
2366 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2369 void perf_event_task_tick(void)
2371 struct list_head *head = &__get_cpu_var(rotation_list);
2372 struct perf_cpu_context *cpuctx, *tmp;
2374 WARN_ON(!irqs_disabled());
2376 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2377 if (cpuctx->jiffies_interval == 1 ||
2378 !(jiffies % cpuctx->jiffies_interval))
2379 perf_rotate_context(cpuctx);
2383 static int event_enable_on_exec(struct perf_event *event,
2384 struct perf_event_context *ctx)
2386 if (!event->attr.enable_on_exec)
2389 event->attr.enable_on_exec = 0;
2390 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2393 __perf_event_mark_enabled(event, ctx);
2399 * Enable all of a task's events that have been marked enable-on-exec.
2400 * This expects task == current.
2402 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2404 struct perf_event *event;
2405 unsigned long flags;
2409 local_irq_save(flags);
2410 if (!ctx || !ctx->nr_events)
2414 * We must ctxsw out cgroup events to avoid conflict
2415 * when invoking perf_task_event_sched_in() later on
2416 * in this function. Otherwise we end up trying to
2417 * ctxswin cgroup events which are already scheduled
2420 perf_cgroup_sched_out(current);
2422 raw_spin_lock(&ctx->lock);
2423 task_ctx_sched_out(ctx);
2425 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2426 ret = event_enable_on_exec(event, ctx);
2431 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2432 ret = event_enable_on_exec(event, ctx);
2438 * Unclone this context if we enabled any event.
2443 raw_spin_unlock(&ctx->lock);
2446 * Also calls ctxswin for cgroup events, if any:
2448 perf_event_context_sched_in(ctx, ctx->task);
2450 local_irq_restore(flags);
2454 * Cross CPU call to read the hardware event
2456 static void __perf_event_read(void *info)
2458 struct perf_event *event = info;
2459 struct perf_event_context *ctx = event->ctx;
2460 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2463 * If this is a task context, we need to check whether it is
2464 * the current task context of this cpu. If not it has been
2465 * scheduled out before the smp call arrived. In that case
2466 * event->count would have been updated to a recent sample
2467 * when the event was scheduled out.
2469 if (ctx->task && cpuctx->task_ctx != ctx)
2472 raw_spin_lock(&ctx->lock);
2473 if (ctx->is_active) {
2474 update_context_time(ctx);
2475 update_cgrp_time_from_event(event);
2477 update_event_times(event);
2478 if (event->state == PERF_EVENT_STATE_ACTIVE)
2479 event->pmu->read(event);
2480 raw_spin_unlock(&ctx->lock);
2483 static inline u64 perf_event_count(struct perf_event *event)
2485 return local64_read(&event->count) + atomic64_read(&event->child_count);
2488 static u64 perf_event_read(struct perf_event *event)
2491 * If event is enabled and currently active on a CPU, update the
2492 * value in the event structure:
2494 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2495 smp_call_function_single(event->oncpu,
2496 __perf_event_read, event, 1);
2497 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2498 struct perf_event_context *ctx = event->ctx;
2499 unsigned long flags;
2501 raw_spin_lock_irqsave(&ctx->lock, flags);
2503 * may read while context is not active
2504 * (e.g., thread is blocked), in that case
2505 * we cannot update context time
2507 if (ctx->is_active) {
2508 update_context_time(ctx);
2509 update_cgrp_time_from_event(event);
2511 update_event_times(event);
2512 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2515 return perf_event_count(event);
2522 struct callchain_cpus_entries {
2523 struct rcu_head rcu_head;
2524 struct perf_callchain_entry *cpu_entries[0];
2527 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
2528 static atomic_t nr_callchain_events;
2529 static DEFINE_MUTEX(callchain_mutex);
2530 struct callchain_cpus_entries *callchain_cpus_entries;
2533 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
2534 struct pt_regs *regs)
2538 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
2539 struct pt_regs *regs)
2543 static void release_callchain_buffers_rcu(struct rcu_head *head)
2545 struct callchain_cpus_entries *entries;
2548 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
2550 for_each_possible_cpu(cpu)
2551 kfree(entries->cpu_entries[cpu]);
2556 static void release_callchain_buffers(void)
2558 struct callchain_cpus_entries *entries;
2560 entries = callchain_cpus_entries;
2561 rcu_assign_pointer(callchain_cpus_entries, NULL);
2562 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2565 static int alloc_callchain_buffers(void)
2569 struct callchain_cpus_entries *entries;
2572 * We can't use the percpu allocation API for data that can be
2573 * accessed from NMI. Use a temporary manual per cpu allocation
2574 * until that gets sorted out.
2576 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
2578 entries = kzalloc(size, GFP_KERNEL);
2582 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
2584 for_each_possible_cpu(cpu) {
2585 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2587 if (!entries->cpu_entries[cpu])
2591 rcu_assign_pointer(callchain_cpus_entries, entries);
2596 for_each_possible_cpu(cpu)
2597 kfree(entries->cpu_entries[cpu]);
2603 static int get_callchain_buffers(void)
2608 mutex_lock(&callchain_mutex);
2610 count = atomic_inc_return(&nr_callchain_events);
2611 if (WARN_ON_ONCE(count < 1)) {
2617 /* If the allocation failed, give up */
2618 if (!callchain_cpus_entries)
2623 err = alloc_callchain_buffers();
2625 release_callchain_buffers();
2627 mutex_unlock(&callchain_mutex);
2632 static void put_callchain_buffers(void)
2634 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2635 release_callchain_buffers();
2636 mutex_unlock(&callchain_mutex);
2640 static int get_recursion_context(int *recursion)
2648 else if (in_softirq())
2653 if (recursion[rctx])
2662 static inline void put_recursion_context(int *recursion, int rctx)
2668 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2671 struct callchain_cpus_entries *entries;
2673 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2677 entries = rcu_dereference(callchain_cpus_entries);
2681 cpu = smp_processor_id();
2683 return &entries->cpu_entries[cpu][*rctx];
2687 put_callchain_entry(int rctx)
2689 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2692 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2695 struct perf_callchain_entry *entry;
2698 entry = get_callchain_entry(&rctx);
2707 if (!user_mode(regs)) {
2708 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2709 perf_callchain_kernel(entry, regs);
2711 regs = task_pt_regs(current);
2717 perf_callchain_store(entry, PERF_CONTEXT_USER);
2718 perf_callchain_user(entry, regs);
2722 put_callchain_entry(rctx);
2728 * Initialize the perf_event context in a task_struct:
2730 static void __perf_event_init_context(struct perf_event_context *ctx)
2732 raw_spin_lock_init(&ctx->lock);
2733 mutex_init(&ctx->mutex);
2734 INIT_LIST_HEAD(&ctx->pinned_groups);
2735 INIT_LIST_HEAD(&ctx->flexible_groups);
2736 INIT_LIST_HEAD(&ctx->event_list);
2737 atomic_set(&ctx->refcount, 1);
2740 static struct perf_event_context *
2741 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2743 struct perf_event_context *ctx;
2745 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2749 __perf_event_init_context(ctx);
2752 get_task_struct(task);
2759 static struct task_struct *
2760 find_lively_task_by_vpid(pid_t vpid)
2762 struct task_struct *task;
2769 task = find_task_by_vpid(vpid);
2771 get_task_struct(task);
2775 return ERR_PTR(-ESRCH);
2777 /* Reuse ptrace permission checks for now. */
2779 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2784 put_task_struct(task);
2785 return ERR_PTR(err);
2790 * Returns a matching context with refcount and pincount.
2792 static struct perf_event_context *
2793 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2795 struct perf_event_context *ctx;
2796 struct perf_cpu_context *cpuctx;
2797 unsigned long flags;
2801 /* Must be root to operate on a CPU event: */
2802 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2803 return ERR_PTR(-EACCES);
2806 * We could be clever and allow to attach a event to an
2807 * offline CPU and activate it when the CPU comes up, but
2810 if (!cpu_online(cpu))
2811 return ERR_PTR(-ENODEV);
2813 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2822 ctxn = pmu->task_ctx_nr;
2827 ctx = perf_lock_task_context(task, ctxn, &flags);
2831 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2833 ctx = alloc_perf_context(pmu, task);
2839 mutex_lock(&task->perf_event_mutex);
2841 * If it has already passed perf_event_exit_task().
2842 * we must see PF_EXITING, it takes this mutex too.
2844 if (task->flags & PF_EXITING)
2846 else if (task->perf_event_ctxp[ctxn])
2851 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2853 mutex_unlock(&task->perf_event_mutex);
2855 if (unlikely(err)) {
2867 return ERR_PTR(err);
2870 static void perf_event_free_filter(struct perf_event *event);
2872 static void free_event_rcu(struct rcu_head *head)
2874 struct perf_event *event;
2876 event = container_of(head, struct perf_event, rcu_head);
2878 put_pid_ns(event->ns);
2879 perf_event_free_filter(event);
2883 static void perf_buffer_put(struct perf_buffer *buffer);
2885 static void free_event(struct perf_event *event)
2887 irq_work_sync(&event->pending);
2889 if (!event->parent) {
2890 if (event->attach_state & PERF_ATTACH_TASK)
2891 jump_label_dec(&perf_sched_events);
2892 if (event->attr.mmap || event->attr.mmap_data)
2893 atomic_dec(&nr_mmap_events);
2894 if (event->attr.comm)
2895 atomic_dec(&nr_comm_events);
2896 if (event->attr.task)
2897 atomic_dec(&nr_task_events);
2898 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2899 put_callchain_buffers();
2900 if (is_cgroup_event(event)) {
2901 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
2902 jump_label_dec(&perf_sched_events);
2906 if (event->buffer) {
2907 perf_buffer_put(event->buffer);
2908 event->buffer = NULL;
2911 if (is_cgroup_event(event))
2912 perf_detach_cgroup(event);
2915 event->destroy(event);
2918 put_ctx(event->ctx);
2920 call_rcu(&event->rcu_head, free_event_rcu);
2923 int perf_event_release_kernel(struct perf_event *event)
2925 struct perf_event_context *ctx = event->ctx;
2927 WARN_ON_ONCE(ctx->parent_ctx);
2929 * There are two ways this annotation is useful:
2931 * 1) there is a lock recursion from perf_event_exit_task
2932 * see the comment there.
2934 * 2) there is a lock-inversion with mmap_sem through
2935 * perf_event_read_group(), which takes faults while
2936 * holding ctx->mutex, however this is called after
2937 * the last filedesc died, so there is no possibility
2938 * to trigger the AB-BA case.
2940 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2941 raw_spin_lock_irq(&ctx->lock);
2942 perf_group_detach(event);
2943 raw_spin_unlock_irq(&ctx->lock);
2944 perf_remove_from_context(event);
2945 mutex_unlock(&ctx->mutex);
2951 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2954 * Called when the last reference to the file is gone.
2956 static int perf_release(struct inode *inode, struct file *file)
2958 struct perf_event *event = file->private_data;
2959 struct task_struct *owner;
2961 file->private_data = NULL;
2964 owner = ACCESS_ONCE(event->owner);
2966 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2967 * !owner it means the list deletion is complete and we can indeed
2968 * free this event, otherwise we need to serialize on
2969 * owner->perf_event_mutex.
2971 smp_read_barrier_depends();
2974 * Since delayed_put_task_struct() also drops the last
2975 * task reference we can safely take a new reference
2976 * while holding the rcu_read_lock().
2978 get_task_struct(owner);
2983 mutex_lock(&owner->perf_event_mutex);
2985 * We have to re-check the event->owner field, if it is cleared
2986 * we raced with perf_event_exit_task(), acquiring the mutex
2987 * ensured they're done, and we can proceed with freeing the
2991 list_del_init(&event->owner_entry);
2992 mutex_unlock(&owner->perf_event_mutex);
2993 put_task_struct(owner);
2996 return perf_event_release_kernel(event);
2999 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
3001 struct perf_event *child;
3007 mutex_lock(&event->child_mutex);
3008 total += perf_event_read(event);
3009 *enabled += event->total_time_enabled +
3010 atomic64_read(&event->child_total_time_enabled);
3011 *running += event->total_time_running +
3012 atomic64_read(&event->child_total_time_running);
3014 list_for_each_entry(child, &event->child_list, child_list) {
3015 total += perf_event_read(child);
3016 *enabled += child->total_time_enabled;
3017 *running += child->total_time_running;
3019 mutex_unlock(&event->child_mutex);
3023 EXPORT_SYMBOL_GPL(perf_event_read_value);
3025 static int perf_event_read_group(struct perf_event *event,
3026 u64 read_format, char __user *buf)
3028 struct perf_event *leader = event->group_leader, *sub;
3029 int n = 0, size = 0, ret = -EFAULT;
3030 struct perf_event_context *ctx = leader->ctx;
3032 u64 count, enabled, running;
3034 mutex_lock(&ctx->mutex);
3035 count = perf_event_read_value(leader, &enabled, &running);
3037 values[n++] = 1 + leader->nr_siblings;
3038 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3039 values[n++] = enabled;
3040 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3041 values[n++] = running;
3042 values[n++] = count;
3043 if (read_format & PERF_FORMAT_ID)
3044 values[n++] = primary_event_id(leader);
3046 size = n * sizeof(u64);
3048 if (copy_to_user(buf, values, size))
3053 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3056 values[n++] = perf_event_read_value(sub, &enabled, &running);
3057 if (read_format & PERF_FORMAT_ID)
3058 values[n++] = primary_event_id(sub);
3060 size = n * sizeof(u64);
3062 if (copy_to_user(buf + ret, values, size)) {
3070 mutex_unlock(&ctx->mutex);
3075 static int perf_event_read_one(struct perf_event *event,
3076 u64 read_format, char __user *buf)
3078 u64 enabled, running;
3082 values[n++] = perf_event_read_value(event, &enabled, &running);
3083 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3084 values[n++] = enabled;
3085 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3086 values[n++] = running;
3087 if (read_format & PERF_FORMAT_ID)
3088 values[n++] = primary_event_id(event);
3090 if (copy_to_user(buf, values, n * sizeof(u64)))
3093 return n * sizeof(u64);
3097 * Read the performance event - simple non blocking version for now
3100 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3102 u64 read_format = event->attr.read_format;
3106 * Return end-of-file for a read on a event that is in
3107 * error state (i.e. because it was pinned but it couldn't be
3108 * scheduled on to the CPU at some point).
3110 if (event->state == PERF_EVENT_STATE_ERROR)
3113 if (count < event->read_size)
3116 WARN_ON_ONCE(event->ctx->parent_ctx);
3117 if (read_format & PERF_FORMAT_GROUP)
3118 ret = perf_event_read_group(event, read_format, buf);
3120 ret = perf_event_read_one(event, read_format, buf);
3126 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3128 struct perf_event *event = file->private_data;
3130 return perf_read_hw(event, buf, count);
3133 static unsigned int perf_poll(struct file *file, poll_table *wait)
3135 struct perf_event *event = file->private_data;
3136 struct perf_buffer *buffer;
3137 unsigned int events = POLL_HUP;
3140 buffer = rcu_dereference(event->buffer);
3142 events = atomic_xchg(&buffer->poll, 0);
3145 poll_wait(file, &event->waitq, wait);
3150 static void perf_event_reset(struct perf_event *event)
3152 (void)perf_event_read(event);
3153 local64_set(&event->count, 0);
3154 perf_event_update_userpage(event);
3158 * Holding the top-level event's child_mutex means that any
3159 * descendant process that has inherited this event will block
3160 * in sync_child_event if it goes to exit, thus satisfying the
3161 * task existence requirements of perf_event_enable/disable.
3163 static void perf_event_for_each_child(struct perf_event *event,
3164 void (*func)(struct perf_event *))
3166 struct perf_event *child;
3168 WARN_ON_ONCE(event->ctx->parent_ctx);
3169 mutex_lock(&event->child_mutex);
3171 list_for_each_entry(child, &event->child_list, child_list)
3173 mutex_unlock(&event->child_mutex);
3176 static void perf_event_for_each(struct perf_event *event,
3177 void (*func)(struct perf_event *))
3179 struct perf_event_context *ctx = event->ctx;
3180 struct perf_event *sibling;
3182 WARN_ON_ONCE(ctx->parent_ctx);
3183 mutex_lock(&ctx->mutex);
3184 event = event->group_leader;
3186 perf_event_for_each_child(event, func);
3188 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3189 perf_event_for_each_child(event, func);
3190 mutex_unlock(&ctx->mutex);
3193 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3195 struct perf_event_context *ctx = event->ctx;
3199 if (!is_sampling_event(event))
3202 if (copy_from_user(&value, arg, sizeof(value)))
3208 raw_spin_lock_irq(&ctx->lock);
3209 if (event->attr.freq) {
3210 if (value > sysctl_perf_event_sample_rate) {
3215 event->attr.sample_freq = value;
3217 event->attr.sample_period = value;
3218 event->hw.sample_period = value;
3221 raw_spin_unlock_irq(&ctx->lock);
3226 static const struct file_operations perf_fops;
3228 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
3232 file = fget_light(fd, fput_needed);
3234 return ERR_PTR(-EBADF);
3236 if (file->f_op != &perf_fops) {
3237 fput_light(file, *fput_needed);
3239 return ERR_PTR(-EBADF);
3242 return file->private_data;
3245 static int perf_event_set_output(struct perf_event *event,
3246 struct perf_event *output_event);
3247 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3249 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3251 struct perf_event *event = file->private_data;
3252 void (*func)(struct perf_event *);
3256 case PERF_EVENT_IOC_ENABLE:
3257 func = perf_event_enable;
3259 case PERF_EVENT_IOC_DISABLE:
3260 func = perf_event_disable;
3262 case PERF_EVENT_IOC_RESET:
3263 func = perf_event_reset;
3266 case PERF_EVENT_IOC_REFRESH:
3267 return perf_event_refresh(event, arg);
3269 case PERF_EVENT_IOC_PERIOD:
3270 return perf_event_period(event, (u64 __user *)arg);
3272 case PERF_EVENT_IOC_SET_OUTPUT:
3274 struct perf_event *output_event = NULL;
3275 int fput_needed = 0;
3279 output_event = perf_fget_light(arg, &fput_needed);
3280 if (IS_ERR(output_event))
3281 return PTR_ERR(output_event);
3284 ret = perf_event_set_output(event, output_event);
3286 fput_light(output_event->filp, fput_needed);
3291 case PERF_EVENT_IOC_SET_FILTER:
3292 return perf_event_set_filter(event, (void __user *)arg);
3298 if (flags & PERF_IOC_FLAG_GROUP)
3299 perf_event_for_each(event, func);
3301 perf_event_for_each_child(event, func);
3306 int perf_event_task_enable(void)
3308 struct perf_event *event;
3310 mutex_lock(¤t->perf_event_mutex);
3311 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3312 perf_event_for_each_child(event, perf_event_enable);
3313 mutex_unlock(¤t->perf_event_mutex);
3318 int perf_event_task_disable(void)
3320 struct perf_event *event;
3322 mutex_lock(¤t->perf_event_mutex);
3323 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3324 perf_event_for_each_child(event, perf_event_disable);
3325 mutex_unlock(¤t->perf_event_mutex);
3330 #ifndef PERF_EVENT_INDEX_OFFSET
3331 # define PERF_EVENT_INDEX_OFFSET 0
3334 static int perf_event_index(struct perf_event *event)
3336 if (event->hw.state & PERF_HES_STOPPED)
3339 if (event->state != PERF_EVENT_STATE_ACTIVE)
3342 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
3346 * Callers need to ensure there can be no nesting of this function, otherwise
3347 * the seqlock logic goes bad. We can not serialize this because the arch
3348 * code calls this from NMI context.
3350 void perf_event_update_userpage(struct perf_event *event)
3352 struct perf_event_mmap_page *userpg;
3353 struct perf_buffer *buffer;
3356 buffer = rcu_dereference(event->buffer);
3360 userpg = buffer->user_page;
3363 * Disable preemption so as to not let the corresponding user-space
3364 * spin too long if we get preempted.
3369 userpg->index = perf_event_index(event);
3370 userpg->offset = perf_event_count(event);
3371 if (event->state == PERF_EVENT_STATE_ACTIVE)
3372 userpg->offset -= local64_read(&event->hw.prev_count);
3374 userpg->time_enabled = event->total_time_enabled +
3375 atomic64_read(&event->child_total_time_enabled);
3377 userpg->time_running = event->total_time_running +
3378 atomic64_read(&event->child_total_time_running);
3387 static unsigned long perf_data_size(struct perf_buffer *buffer);
3390 perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
3392 long max_size = perf_data_size(buffer);
3395 buffer->watermark = min(max_size, watermark);
3397 if (!buffer->watermark)
3398 buffer->watermark = max_size / 2;
3400 if (flags & PERF_BUFFER_WRITABLE)
3401 buffer->writable = 1;
3403 atomic_set(&buffer->refcount, 1);
3406 #ifndef CONFIG_PERF_USE_VMALLOC
3409 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
3412 static struct page *
3413 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
3415 if (pgoff > buffer->nr_pages)
3419 return virt_to_page(buffer->user_page);
3421 return virt_to_page(buffer->data_pages[pgoff - 1]);
3424 static void *perf_mmap_alloc_page(int cpu)
3429 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
3430 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
3434 return page_address(page);
3437 static struct perf_buffer *
3438 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
3440 struct perf_buffer *buffer;
3444 size = sizeof(struct perf_buffer);
3445 size += nr_pages * sizeof(void *);
3447 buffer = kzalloc(size, GFP_KERNEL);
3451 buffer->user_page = perf_mmap_alloc_page(cpu);
3452 if (!buffer->user_page)
3453 goto fail_user_page;
3455 for (i = 0; i < nr_pages; i++) {
3456 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
3457 if (!buffer->data_pages[i])
3458 goto fail_data_pages;
3461 buffer->nr_pages = nr_pages;
3463 perf_buffer_init(buffer, watermark, flags);
3468 for (i--; i >= 0; i--)
3469 free_page((unsigned long)buffer->data_pages[i]);
3471 free_page((unsigned long)buffer->user_page);
3480 static void perf_mmap_free_page(unsigned long addr)
3482 struct page *page = virt_to_page((void *)addr);
3484 page->mapping = NULL;
3488 static void perf_buffer_free(struct perf_buffer *buffer)
3492 perf_mmap_free_page((unsigned long)buffer->user_page);
3493 for (i = 0; i < buffer->nr_pages; i++)
3494 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
3498 static inline int page_order(struct perf_buffer *buffer)
3506 * Back perf_mmap() with vmalloc memory.
3508 * Required for architectures that have d-cache aliasing issues.
3511 static inline int page_order(struct perf_buffer *buffer)
3513 return buffer->page_order;
3516 static struct page *
3517 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
3519 if (pgoff > (1UL << page_order(buffer)))
3522 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
3525 static void perf_mmap_unmark_page(void *addr)
3527 struct page *page = vmalloc_to_page(addr);
3529 page->mapping = NULL;
3532 static void perf_buffer_free_work(struct work_struct *work)
3534 struct perf_buffer *buffer;
3538 buffer = container_of(work, struct perf_buffer, work);
3539 nr = 1 << page_order(buffer);
3541 base = buffer->user_page;
3542 for (i = 0; i < nr + 1; i++)
3543 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
3549 static void perf_buffer_free(struct perf_buffer *buffer)
3551 schedule_work(&buffer->work);
3554 static struct perf_buffer *
3555 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
3557 struct perf_buffer *buffer;
3561 size = sizeof(struct perf_buffer);
3562 size += sizeof(void *);
3564 buffer = kzalloc(size, GFP_KERNEL);
3568 INIT_WORK(&buffer->work, perf_buffer_free_work);
3570 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
3574 buffer->user_page = all_buf;
3575 buffer->data_pages[0] = all_buf + PAGE_SIZE;
3576 buffer->page_order = ilog2(nr_pages);
3577 buffer->nr_pages = 1;
3579 perf_buffer_init(buffer, watermark, flags);
3592 static unsigned long perf_data_size(struct perf_buffer *buffer)
3594 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
3597 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3599 struct perf_event *event = vma->vm_file->private_data;
3600 struct perf_buffer *buffer;
3601 int ret = VM_FAULT_SIGBUS;
3603 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3604 if (vmf->pgoff == 0)
3610 buffer = rcu_dereference(event->buffer);
3614 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3617 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
3621 get_page(vmf->page);
3622 vmf->page->mapping = vma->vm_file->f_mapping;
3623 vmf->page->index = vmf->pgoff;
3632 static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
3634 struct perf_buffer *buffer;
3636 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
3637 perf_buffer_free(buffer);
3640 static struct perf_buffer *perf_buffer_get(struct perf_event *event)
3642 struct perf_buffer *buffer;
3645 buffer = rcu_dereference(event->buffer);
3647 if (!atomic_inc_not_zero(&buffer->refcount))
3655 static void perf_buffer_put(struct perf_buffer *buffer)
3657 if (!atomic_dec_and_test(&buffer->refcount))
3660 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
3663 static void perf_mmap_open(struct vm_area_struct *vma)
3665 struct perf_event *event = vma->vm_file->private_data;
3667 atomic_inc(&event->mmap_count);
3670 static void perf_mmap_close(struct vm_area_struct *vma)
3672 struct perf_event *event = vma->vm_file->private_data;
3674 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
3675 unsigned long size = perf_data_size(event->buffer);
3676 struct user_struct *user = event->mmap_user;
3677 struct perf_buffer *buffer = event->buffer;
3679 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
3680 vma->vm_mm->locked_vm -= event->mmap_locked;
3681 rcu_assign_pointer(event->buffer, NULL);
3682 mutex_unlock(&event->mmap_mutex);
3684 perf_buffer_put(buffer);
3689 static const struct vm_operations_struct perf_mmap_vmops = {
3690 .open = perf_mmap_open,
3691 .close = perf_mmap_close,
3692 .fault = perf_mmap_fault,
3693 .page_mkwrite = perf_mmap_fault,
3696 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3698 struct perf_event *event = file->private_data;
3699 unsigned long user_locked, user_lock_limit;
3700 struct user_struct *user = current_user();
3701 unsigned long locked, lock_limit;
3702 struct perf_buffer *buffer;
3703 unsigned long vma_size;
3704 unsigned long nr_pages;
3705 long user_extra, extra;
3706 int ret = 0, flags = 0;
3709 * Don't allow mmap() of inherited per-task counters. This would
3710 * create a performance issue due to all children writing to the
3713 if (event->cpu == -1 && event->attr.inherit)
3716 if (!(vma->vm_flags & VM_SHARED))
3719 vma_size = vma->vm_end - vma->vm_start;
3720 nr_pages = (vma_size / PAGE_SIZE) - 1;
3723 * If we have buffer pages ensure they're a power-of-two number, so we
3724 * can do bitmasks instead of modulo.
3726 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3729 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3732 if (vma->vm_pgoff != 0)
3735 WARN_ON_ONCE(event->ctx->parent_ctx);
3736 mutex_lock(&event->mmap_mutex);
3737 if (event->buffer) {
3738 if (event->buffer->nr_pages == nr_pages)
3739 atomic_inc(&event->buffer->refcount);
3745 user_extra = nr_pages + 1;
3746 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3749 * Increase the limit linearly with more CPUs:
3751 user_lock_limit *= num_online_cpus();
3753 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3756 if (user_locked > user_lock_limit)
3757 extra = user_locked - user_lock_limit;
3759 lock_limit = rlimit(RLIMIT_MEMLOCK);
3760 lock_limit >>= PAGE_SHIFT;
3761 locked = vma->vm_mm->locked_vm + extra;
3763 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3764 !capable(CAP_IPC_LOCK)) {
3769 WARN_ON(event->buffer);
3771 if (vma->vm_flags & VM_WRITE)
3772 flags |= PERF_BUFFER_WRITABLE;
3774 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3780 rcu_assign_pointer(event->buffer, buffer);
3782 atomic_long_add(user_extra, &user->locked_vm);
3783 event->mmap_locked = extra;
3784 event->mmap_user = get_current_user();
3785 vma->vm_mm->locked_vm += event->mmap_locked;
3789 atomic_inc(&event->mmap_count);
3790 mutex_unlock(&event->mmap_mutex);
3792 vma->vm_flags |= VM_RESERVED;
3793 vma->vm_ops = &perf_mmap_vmops;
3798 static int perf_fasync(int fd, struct file *filp, int on)
3800 struct inode *inode = filp->f_path.dentry->d_inode;
3801 struct perf_event *event = filp->private_data;
3804 mutex_lock(&inode->i_mutex);
3805 retval = fasync_helper(fd, filp, on, &event->fasync);
3806 mutex_unlock(&inode->i_mutex);
3814 static const struct file_operations perf_fops = {
3815 .llseek = no_llseek,
3816 .release = perf_release,
3819 .unlocked_ioctl = perf_ioctl,
3820 .compat_ioctl = perf_ioctl,
3822 .fasync = perf_fasync,
3828 * If there's data, ensure we set the poll() state and publish everything
3829 * to user-space before waking everybody up.
3832 void perf_event_wakeup(struct perf_event *event)
3834 wake_up_all(&event->waitq);
3836 if (event->pending_kill) {
3837 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3838 event->pending_kill = 0;
3842 static void perf_pending_event(struct irq_work *entry)
3844 struct perf_event *event = container_of(entry,
3845 struct perf_event, pending);
3847 if (event->pending_disable) {
3848 event->pending_disable = 0;
3849 __perf_event_disable(event);
3852 if (event->pending_wakeup) {
3853 event->pending_wakeup = 0;
3854 perf_event_wakeup(event);
3859 * We assume there is only KVM supporting the callbacks.
3860 * Later on, we might change it to a list if there is
3861 * another virtualization implementation supporting the callbacks.
3863 struct perf_guest_info_callbacks *perf_guest_cbs;
3865 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3867 perf_guest_cbs = cbs;
3870 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3872 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3874 perf_guest_cbs = NULL;
3877 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3882 static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
3883 unsigned long offset, unsigned long head)
3887 if (!buffer->writable)
3890 mask = perf_data_size(buffer) - 1;
3892 offset = (offset - tail) & mask;
3893 head = (head - tail) & mask;
3895 if ((int)(head - offset) < 0)
3901 static void perf_output_wakeup(struct perf_output_handle *handle)
3903 atomic_set(&handle->buffer->poll, POLL_IN);
3906 handle->event->pending_wakeup = 1;
3907 irq_work_queue(&handle->event->pending);
3909 perf_event_wakeup(handle->event);
3913 * We need to ensure a later event_id doesn't publish a head when a former
3914 * event isn't done writing. However since we need to deal with NMIs we
3915 * cannot fully serialize things.
3917 * We only publish the head (and generate a wakeup) when the outer-most
3920 static void perf_output_get_handle(struct perf_output_handle *handle)
3922 struct perf_buffer *buffer = handle->buffer;
3925 local_inc(&buffer->nest);
3926 handle->wakeup = local_read(&buffer->wakeup);
3929 static void perf_output_put_handle(struct perf_output_handle *handle)
3931 struct perf_buffer *buffer = handle->buffer;
3935 head = local_read(&buffer->head);
3938 * IRQ/NMI can happen here, which means we can miss a head update.
3941 if (!local_dec_and_test(&buffer->nest))
3945 * Publish the known good head. Rely on the full barrier implied
3946 * by atomic_dec_and_test() order the buffer->head read and this
3949 buffer->user_page->data_head = head;
3952 * Now check if we missed an update, rely on the (compiler)
3953 * barrier in atomic_dec_and_test() to re-read buffer->head.
3955 if (unlikely(head != local_read(&buffer->head))) {
3956 local_inc(&buffer->nest);
3960 if (handle->wakeup != local_read(&buffer->wakeup))
3961 perf_output_wakeup(handle);
3967 __always_inline void perf_output_copy(struct perf_output_handle *handle,
3968 const void *buf, unsigned int len)
3971 unsigned long size = min_t(unsigned long, handle->size, len);
3973 memcpy(handle->addr, buf, size);
3976 handle->addr += size;
3978 handle->size -= size;
3979 if (!handle->size) {
3980 struct perf_buffer *buffer = handle->buffer;
3983 handle->page &= buffer->nr_pages - 1;
3984 handle->addr = buffer->data_pages[handle->page];
3985 handle->size = PAGE_SIZE << page_order(buffer);
3990 static void __perf_event_header__init_id(struct perf_event_header *header,
3991 struct perf_sample_data *data,
3992 struct perf_event *event)
3994 u64 sample_type = event->attr.sample_type;
3996 data->type = sample_type;
3997 header->size += event->id_header_size;
3999 if (sample_type & PERF_SAMPLE_TID) {
4000 /* namespace issues */
4001 data->tid_entry.pid = perf_event_pid(event, current);
4002 data->tid_entry.tid = perf_event_tid(event, current);
4005 if (sample_type & PERF_SAMPLE_TIME)
4006 data->time = perf_clock();
4008 if (sample_type & PERF_SAMPLE_ID)
4009 data->id = primary_event_id(event);
4011 if (sample_type & PERF_SAMPLE_STREAM_ID)
4012 data->stream_id = event->id;
4014 if (sample_type & PERF_SAMPLE_CPU) {
4015 data->cpu_entry.cpu = raw_smp_processor_id();
4016 data->cpu_entry.reserved = 0;
4020 static void perf_event_header__init_id(struct perf_event_header *header,
4021 struct perf_sample_data *data,
4022 struct perf_event *event)
4024 if (event->attr.sample_id_all)
4025 __perf_event_header__init_id(header, data, event);
4028 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4029 struct perf_sample_data *data)
4031 u64 sample_type = data->type;
4033 if (sample_type & PERF_SAMPLE_TID)
4034 perf_output_put(handle, data->tid_entry);
4036 if (sample_type & PERF_SAMPLE_TIME)
4037 perf_output_put(handle, data->time);
4039 if (sample_type & PERF_SAMPLE_ID)
4040 perf_output_put(handle, data->id);
4042 if (sample_type & PERF_SAMPLE_STREAM_ID)
4043 perf_output_put(handle, data->stream_id);
4045 if (sample_type & PERF_SAMPLE_CPU)
4046 perf_output_put(handle, data->cpu_entry);
4049 static void perf_event__output_id_sample(struct perf_event *event,
4050 struct perf_output_handle *handle,
4051 struct perf_sample_data *sample)
4053 if (event->attr.sample_id_all)
4054 __perf_event__output_id_sample(handle, sample);
4057 int perf_output_begin(struct perf_output_handle *handle,
4058 struct perf_event *event, unsigned int size,
4059 int nmi, int sample)
4061 struct perf_buffer *buffer;
4062 unsigned long tail, offset, head;
4064 struct perf_sample_data sample_data;
4066 struct perf_event_header header;
4073 * For inherited events we send all the output towards the parent.
4076 event = event->parent;
4078 buffer = rcu_dereference(event->buffer);
4082 handle->buffer = buffer;
4083 handle->event = event;
4085 handle->sample = sample;
4087 if (!buffer->nr_pages)
4090 have_lost = local_read(&buffer->lost);
4092 lost_event.header.size = sizeof(lost_event);
4093 perf_event_header__init_id(&lost_event.header, &sample_data,
4095 size += lost_event.header.size;
4098 perf_output_get_handle(handle);
4102 * Userspace could choose to issue a mb() before updating the
4103 * tail pointer. So that all reads will be completed before the
4106 tail = ACCESS_ONCE(buffer->user_page->data_tail);
4108 offset = head = local_read(&buffer->head);
4110 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
4112 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
4114 if (head - local_read(&buffer->wakeup) > buffer->watermark)
4115 local_add(buffer->watermark, &buffer->wakeup);
4117 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
4118 handle->page &= buffer->nr_pages - 1;
4119 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
4120 handle->addr = buffer->data_pages[handle->page];
4121 handle->addr += handle->size;
4122 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
4125 lost_event.header.type = PERF_RECORD_LOST;
4126 lost_event.header.misc = 0;
4127 lost_event.id = event->id;
4128 lost_event.lost = local_xchg(&buffer->lost, 0);
4130 perf_output_put(handle, lost_event);
4131 perf_event__output_id_sample(event, handle, &sample_data);
4137 local_inc(&buffer->lost);
4138 perf_output_put_handle(handle);
4145 void perf_output_end(struct perf_output_handle *handle)
4147 struct perf_event *event = handle->event;
4148 struct perf_buffer *buffer = handle->buffer;
4150 int wakeup_events = event->attr.wakeup_events;
4152 if (handle->sample && wakeup_events) {
4153 int events = local_inc_return(&buffer->events);
4154 if (events >= wakeup_events) {
4155 local_sub(wakeup_events, &buffer->events);
4156 local_inc(&buffer->wakeup);
4160 perf_output_put_handle(handle);
4164 static void perf_output_read_one(struct perf_output_handle *handle,
4165 struct perf_event *event,
4166 u64 enabled, u64 running)
4168 u64 read_format = event->attr.read_format;
4172 values[n++] = perf_event_count(event);
4173 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4174 values[n++] = enabled +
4175 atomic64_read(&event->child_total_time_enabled);
4177 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4178 values[n++] = running +
4179 atomic64_read(&event->child_total_time_running);
4181 if (read_format & PERF_FORMAT_ID)
4182 values[n++] = primary_event_id(event);
4184 perf_output_copy(handle, values, n * sizeof(u64));
4188 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4190 static void perf_output_read_group(struct perf_output_handle *handle,
4191 struct perf_event *event,
4192 u64 enabled, u64 running)
4194 struct perf_event *leader = event->group_leader, *sub;
4195 u64 read_format = event->attr.read_format;
4199 values[n++] = 1 + leader->nr_siblings;
4201 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4202 values[n++] = enabled;
4204 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4205 values[n++] = running;
4207 if (leader != event)
4208 leader->pmu->read(leader);
4210 values[n++] = perf_event_count(leader);
4211 if (read_format & PERF_FORMAT_ID)
4212 values[n++] = primary_event_id(leader);
4214 perf_output_copy(handle, values, n * sizeof(u64));
4216 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4220 sub->pmu->read(sub);
4222 values[n++] = perf_event_count(sub);
4223 if (read_format & PERF_FORMAT_ID)
4224 values[n++] = primary_event_id(sub);
4226 perf_output_copy(handle, values, n * sizeof(u64));
4230 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4231 PERF_FORMAT_TOTAL_TIME_RUNNING)
4233 static void perf_output_read(struct perf_output_handle *handle,
4234 struct perf_event *event)
4236 u64 enabled = 0, running = 0, now, ctx_time;
4237 u64 read_format = event->attr.read_format;
4240 * compute total_time_enabled, total_time_running
4241 * based on snapshot values taken when the event
4242 * was last scheduled in.
4244 * we cannot simply called update_context_time()
4245 * because of locking issue as we are called in
4248 if (read_format & PERF_FORMAT_TOTAL_TIMES) {
4250 ctx_time = event->shadow_ctx_time + now;
4251 enabled = ctx_time - event->tstamp_enabled;
4252 running = ctx_time - event->tstamp_running;
4255 if (event->attr.read_format & PERF_FORMAT_GROUP)
4256 perf_output_read_group(handle, event, enabled, running);
4258 perf_output_read_one(handle, event, enabled, running);
4261 void perf_output_sample(struct perf_output_handle *handle,
4262 struct perf_event_header *header,
4263 struct perf_sample_data *data,
4264 struct perf_event *event)
4266 u64 sample_type = data->type;
4268 perf_output_put(handle, *header);
4270 if (sample_type & PERF_SAMPLE_IP)
4271 perf_output_put(handle, data->ip);
4273 if (sample_type & PERF_SAMPLE_TID)
4274 perf_output_put(handle, data->tid_entry);
4276 if (sample_type & PERF_SAMPLE_TIME)
4277 perf_output_put(handle, data->time);
4279 if (sample_type & PERF_SAMPLE_ADDR)
4280 perf_output_put(handle, data->addr);
4282 if (sample_type & PERF_SAMPLE_ID)
4283 perf_output_put(handle, data->id);
4285 if (sample_type & PERF_SAMPLE_STREAM_ID)
4286 perf_output_put(handle, data->stream_id);
4288 if (sample_type & PERF_SAMPLE_CPU)
4289 perf_output_put(handle, data->cpu_entry);
4291 if (sample_type & PERF_SAMPLE_PERIOD)
4292 perf_output_put(handle, data->period);
4294 if (sample_type & PERF_SAMPLE_READ)
4295 perf_output_read(handle, event);
4297 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4298 if (data->callchain) {
4301 if (data->callchain)
4302 size += data->callchain->nr;
4304 size *= sizeof(u64);
4306 perf_output_copy(handle, data->callchain, size);
4309 perf_output_put(handle, nr);
4313 if (sample_type & PERF_SAMPLE_RAW) {
4315 perf_output_put(handle, data->raw->size);
4316 perf_output_copy(handle, data->raw->data,
4323 .size = sizeof(u32),
4326 perf_output_put(handle, raw);
4331 void perf_prepare_sample(struct perf_event_header *header,
4332 struct perf_sample_data *data,
4333 struct perf_event *event,
4334 struct pt_regs *regs)
4336 u64 sample_type = event->attr.sample_type;
4338 header->type = PERF_RECORD_SAMPLE;
4339 header->size = sizeof(*header) + event->header_size;
4342 header->misc |= perf_misc_flags(regs);
4344 __perf_event_header__init_id(header, data, event);
4346 if (sample_type & PERF_SAMPLE_IP)
4347 data->ip = perf_instruction_pointer(regs);
4349 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4352 data->callchain = perf_callchain(regs);
4354 if (data->callchain)
4355 size += data->callchain->nr;
4357 header->size += size * sizeof(u64);
4360 if (sample_type & PERF_SAMPLE_RAW) {
4361 int size = sizeof(u32);
4364 size += data->raw->size;
4366 size += sizeof(u32);
4368 WARN_ON_ONCE(size & (sizeof(u64)-1));
4369 header->size += size;
4373 static void perf_event_output(struct perf_event *event, int nmi,
4374 struct perf_sample_data *data,
4375 struct pt_regs *regs)
4377 struct perf_output_handle handle;
4378 struct perf_event_header header;
4380 /* protect the callchain buffers */
4383 perf_prepare_sample(&header, data, event, regs);
4385 if (perf_output_begin(&handle, event, header.size, nmi, 1))
4388 perf_output_sample(&handle, &header, data, event);
4390 perf_output_end(&handle);
4400 struct perf_read_event {
4401 struct perf_event_header header;
4408 perf_event_read_event(struct perf_event *event,
4409 struct task_struct *task)
4411 struct perf_output_handle handle;
4412 struct perf_sample_data sample;
4413 struct perf_read_event read_event = {
4415 .type = PERF_RECORD_READ,
4417 .size = sizeof(read_event) + event->read_size,
4419 .pid = perf_event_pid(event, task),
4420 .tid = perf_event_tid(event, task),
4424 perf_event_header__init_id(&read_event.header, &sample, event);
4425 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
4429 perf_output_put(&handle, read_event);
4430 perf_output_read(&handle, event);
4431 perf_event__output_id_sample(event, &handle, &sample);
4433 perf_output_end(&handle);
4437 * task tracking -- fork/exit
4439 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4442 struct perf_task_event {
4443 struct task_struct *task;
4444 struct perf_event_context *task_ctx;
4447 struct perf_event_header header;
4457 static void perf_event_task_output(struct perf_event *event,
4458 struct perf_task_event *task_event)
4460 struct perf_output_handle handle;
4461 struct perf_sample_data sample;
4462 struct task_struct *task = task_event->task;
4463 int ret, size = task_event->event_id.header.size;
4465 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4467 ret = perf_output_begin(&handle, event,
4468 task_event->event_id.header.size, 0, 0);
4472 task_event->event_id.pid = perf_event_pid(event, task);
4473 task_event->event_id.ppid = perf_event_pid(event, current);
4475 task_event->event_id.tid = perf_event_tid(event, task);
4476 task_event->event_id.ptid = perf_event_tid(event, current);
4478 perf_output_put(&handle, task_event->event_id);
4480 perf_event__output_id_sample(event, &handle, &sample);
4482 perf_output_end(&handle);
4484 task_event->event_id.header.size = size;
4487 static int perf_event_task_match(struct perf_event *event)
4489 if (event->state < PERF_EVENT_STATE_INACTIVE)
4492 if (!event_filter_match(event))
4495 if (event->attr.comm || event->attr.mmap ||
4496 event->attr.mmap_data || event->attr.task)
4502 static void perf_event_task_ctx(struct perf_event_context *ctx,
4503 struct perf_task_event *task_event)
4505 struct perf_event *event;
4507 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4508 if (perf_event_task_match(event))
4509 perf_event_task_output(event, task_event);
4513 static void perf_event_task_event(struct perf_task_event *task_event)
4515 struct perf_cpu_context *cpuctx;
4516 struct perf_event_context *ctx;
4521 list_for_each_entry_rcu(pmu, &pmus, entry) {
4522 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4523 if (cpuctx->active_pmu != pmu)
4525 perf_event_task_ctx(&cpuctx->ctx, task_event);
4527 ctx = task_event->task_ctx;
4529 ctxn = pmu->task_ctx_nr;
4532 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4535 perf_event_task_ctx(ctx, task_event);
4537 put_cpu_ptr(pmu->pmu_cpu_context);
4542 static void perf_event_task(struct task_struct *task,
4543 struct perf_event_context *task_ctx,
4546 struct perf_task_event task_event;
4548 if (!atomic_read(&nr_comm_events) &&
4549 !atomic_read(&nr_mmap_events) &&
4550 !atomic_read(&nr_task_events))
4553 task_event = (struct perf_task_event){
4555 .task_ctx = task_ctx,
4558 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4560 .size = sizeof(task_event.event_id),
4566 .time = perf_clock(),
4570 perf_event_task_event(&task_event);
4573 void perf_event_fork(struct task_struct *task)
4575 perf_event_task(task, NULL, 1);
4582 struct perf_comm_event {
4583 struct task_struct *task;
4588 struct perf_event_header header;
4595 static void perf_event_comm_output(struct perf_event *event,
4596 struct perf_comm_event *comm_event)
4598 struct perf_output_handle handle;
4599 struct perf_sample_data sample;
4600 int size = comm_event->event_id.header.size;
4603 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4604 ret = perf_output_begin(&handle, event,
4605 comm_event->event_id.header.size, 0, 0);
4610 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4611 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4613 perf_output_put(&handle, comm_event->event_id);
4614 perf_output_copy(&handle, comm_event->comm,
4615 comm_event->comm_size);
4617 perf_event__output_id_sample(event, &handle, &sample);
4619 perf_output_end(&handle);
4621 comm_event->event_id.header.size = size;
4624 static int perf_event_comm_match(struct perf_event *event)
4626 if (event->state < PERF_EVENT_STATE_INACTIVE)
4629 if (!event_filter_match(event))
4632 if (event->attr.comm)
4638 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4639 struct perf_comm_event *comm_event)
4641 struct perf_event *event;
4643 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4644 if (perf_event_comm_match(event))
4645 perf_event_comm_output(event, comm_event);
4649 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4651 struct perf_cpu_context *cpuctx;
4652 struct perf_event_context *ctx;
4653 char comm[TASK_COMM_LEN];
4658 memset(comm, 0, sizeof(comm));
4659 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4660 size = ALIGN(strlen(comm)+1, sizeof(u64));
4662 comm_event->comm = comm;
4663 comm_event->comm_size = size;
4665 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4667 list_for_each_entry_rcu(pmu, &pmus, entry) {
4668 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4669 if (cpuctx->active_pmu != pmu)
4671 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4673 ctxn = pmu->task_ctx_nr;
4677 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4679 perf_event_comm_ctx(ctx, comm_event);
4681 put_cpu_ptr(pmu->pmu_cpu_context);
4686 void perf_event_comm(struct task_struct *task)
4688 struct perf_comm_event comm_event;
4689 struct perf_event_context *ctx;
4692 for_each_task_context_nr(ctxn) {
4693 ctx = task->perf_event_ctxp[ctxn];
4697 perf_event_enable_on_exec(ctx);
4700 if (!atomic_read(&nr_comm_events))
4703 comm_event = (struct perf_comm_event){
4709 .type = PERF_RECORD_COMM,
4718 perf_event_comm_event(&comm_event);
4725 struct perf_mmap_event {
4726 struct vm_area_struct *vma;
4728 const char *file_name;
4732 struct perf_event_header header;
4742 static void perf_event_mmap_output(struct perf_event *event,
4743 struct perf_mmap_event *mmap_event)
4745 struct perf_output_handle handle;
4746 struct perf_sample_data sample;
4747 int size = mmap_event->event_id.header.size;
4750 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4751 ret = perf_output_begin(&handle, event,
4752 mmap_event->event_id.header.size, 0, 0);
4756 mmap_event->event_id.pid = perf_event_pid(event, current);
4757 mmap_event->event_id.tid = perf_event_tid(event, current);
4759 perf_output_put(&handle, mmap_event->event_id);
4760 perf_output_copy(&handle, mmap_event->file_name,
4761 mmap_event->file_size);
4763 perf_event__output_id_sample(event, &handle, &sample);
4765 perf_output_end(&handle);
4767 mmap_event->event_id.header.size = size;
4770 static int perf_event_mmap_match(struct perf_event *event,
4771 struct perf_mmap_event *mmap_event,
4774 if (event->state < PERF_EVENT_STATE_INACTIVE)
4777 if (!event_filter_match(event))
4780 if ((!executable && event->attr.mmap_data) ||
4781 (executable && event->attr.mmap))
4787 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4788 struct perf_mmap_event *mmap_event,
4791 struct perf_event *event;
4793 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4794 if (perf_event_mmap_match(event, mmap_event, executable))
4795 perf_event_mmap_output(event, mmap_event);
4799 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4801 struct perf_cpu_context *cpuctx;
4802 struct perf_event_context *ctx;
4803 struct vm_area_struct *vma = mmap_event->vma;
4804 struct file *file = vma->vm_file;
4812 memset(tmp, 0, sizeof(tmp));
4816 * d_path works from the end of the buffer backwards, so we
4817 * need to add enough zero bytes after the string to handle
4818 * the 64bit alignment we do later.
4820 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4822 name = strncpy(tmp, "//enomem", sizeof(tmp));
4825 name = d_path(&file->f_path, buf, PATH_MAX);
4827 name = strncpy(tmp, "//toolong", sizeof(tmp));
4831 if (arch_vma_name(mmap_event->vma)) {
4832 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4838 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4840 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4841 vma->vm_end >= vma->vm_mm->brk) {
4842 name = strncpy(tmp, "[heap]", sizeof(tmp));
4844 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4845 vma->vm_end >= vma->vm_mm->start_stack) {
4846 name = strncpy(tmp, "[stack]", sizeof(tmp));
4850 name = strncpy(tmp, "//anon", sizeof(tmp));
4855 size = ALIGN(strlen(name)+1, sizeof(u64));
4857 mmap_event->file_name = name;
4858 mmap_event->file_size = size;
4860 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4863 list_for_each_entry_rcu(pmu, &pmus, entry) {
4864 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4865 if (cpuctx->active_pmu != pmu)
4867 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4868 vma->vm_flags & VM_EXEC);
4870 ctxn = pmu->task_ctx_nr;
4874 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4876 perf_event_mmap_ctx(ctx, mmap_event,
4877 vma->vm_flags & VM_EXEC);
4880 put_cpu_ptr(pmu->pmu_cpu_context);
4887 void perf_event_mmap(struct vm_area_struct *vma)
4889 struct perf_mmap_event mmap_event;
4891 if (!atomic_read(&nr_mmap_events))
4894 mmap_event = (struct perf_mmap_event){
4900 .type = PERF_RECORD_MMAP,
4901 .misc = PERF_RECORD_MISC_USER,
4906 .start = vma->vm_start,
4907 .len = vma->vm_end - vma->vm_start,
4908 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4912 perf_event_mmap_event(&mmap_event);
4916 * IRQ throttle logging
4919 static void perf_log_throttle(struct perf_event *event, int enable)
4921 struct perf_output_handle handle;
4922 struct perf_sample_data sample;
4926 struct perf_event_header header;
4930 } throttle_event = {
4932 .type = PERF_RECORD_THROTTLE,
4934 .size = sizeof(throttle_event),
4936 .time = perf_clock(),
4937 .id = primary_event_id(event),
4938 .stream_id = event->id,
4942 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4944 perf_event_header__init_id(&throttle_event.header, &sample, event);
4946 ret = perf_output_begin(&handle, event,
4947 throttle_event.header.size, 1, 0);
4951 perf_output_put(&handle, throttle_event);
4952 perf_event__output_id_sample(event, &handle, &sample);
4953 perf_output_end(&handle);
4957 * Generic event overflow handling, sampling.
4960 static int __perf_event_overflow(struct perf_event *event, int nmi,
4961 int throttle, struct perf_sample_data *data,
4962 struct pt_regs *regs)
4964 int events = atomic_read(&event->event_limit);
4965 struct hw_perf_event *hwc = &event->hw;
4969 * Non-sampling counters might still use the PMI to fold short
4970 * hardware counters, ignore those.
4972 if (unlikely(!is_sampling_event(event)))
4975 if (unlikely(hwc->interrupts >= max_samples_per_tick)) {
4977 hwc->interrupts = MAX_INTERRUPTS;
4978 perf_log_throttle(event, 0);
4984 if (event->attr.freq) {
4985 u64 now = perf_clock();
4986 s64 delta = now - hwc->freq_time_stamp;
4988 hwc->freq_time_stamp = now;
4990 if (delta > 0 && delta < 2*TICK_NSEC)
4991 perf_adjust_period(event, delta, hwc->last_period);
4995 * XXX event_limit might not quite work as expected on inherited
4999 event->pending_kill = POLL_IN;
5000 if (events && atomic_dec_and_test(&event->event_limit)) {
5002 event->pending_kill = POLL_HUP;
5004 event->pending_disable = 1;
5005 irq_work_queue(&event->pending);
5007 perf_event_disable(event);
5010 if (event->overflow_handler)
5011 event->overflow_handler(event, nmi, data, regs);
5013 perf_event_output(event, nmi, data, regs);
5015 if (event->fasync && event->pending_kill) {
5017 event->pending_wakeup = 1;
5018 irq_work_queue(&event->pending);
5020 perf_event_wakeup(event);
5026 int perf_event_overflow(struct perf_event *event, int nmi,
5027 struct perf_sample_data *data,
5028 struct pt_regs *regs)
5030 return __perf_event_overflow(event, nmi, 1, data, regs);
5034 * Generic software event infrastructure
5037 struct swevent_htable {
5038 struct swevent_hlist *swevent_hlist;
5039 struct mutex hlist_mutex;
5042 /* Recursion avoidance in each contexts */
5043 int recursion[PERF_NR_CONTEXTS];
5046 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5049 * We directly increment event->count and keep a second value in
5050 * event->hw.period_left to count intervals. This period event
5051 * is kept in the range [-sample_period, 0] so that we can use the
5055 static u64 perf_swevent_set_period(struct perf_event *event)
5057 struct hw_perf_event *hwc = &event->hw;
5058 u64 period = hwc->last_period;
5062 hwc->last_period = hwc->sample_period;
5065 old = val = local64_read(&hwc->period_left);
5069 nr = div64_u64(period + val, period);
5070 offset = nr * period;
5072 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
5078 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
5079 int nmi, struct perf_sample_data *data,
5080 struct pt_regs *regs)
5082 struct hw_perf_event *hwc = &event->hw;
5085 data->period = event->hw.last_period;
5087 overflow = perf_swevent_set_period(event);
5089 if (hwc->interrupts == MAX_INTERRUPTS)
5092 for (; overflow; overflow--) {
5093 if (__perf_event_overflow(event, nmi, throttle,
5096 * We inhibit the overflow from happening when
5097 * hwc->interrupts == MAX_INTERRUPTS.
5105 static void perf_swevent_event(struct perf_event *event, u64 nr,
5106 int nmi, struct perf_sample_data *data,
5107 struct pt_regs *regs)
5109 struct hw_perf_event *hwc = &event->hw;
5111 local64_add(nr, &event->count);
5116 if (!is_sampling_event(event))
5119 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
5120 return perf_swevent_overflow(event, 1, nmi, data, regs);
5122 if (local64_add_negative(nr, &hwc->period_left))
5125 perf_swevent_overflow(event, 0, nmi, data, regs);
5128 static int perf_exclude_event(struct perf_event *event,
5129 struct pt_regs *regs)
5131 if (event->hw.state & PERF_HES_STOPPED)
5135 if (event->attr.exclude_user && user_mode(regs))
5138 if (event->attr.exclude_kernel && !user_mode(regs))
5145 static int perf_swevent_match(struct perf_event *event,
5146 enum perf_type_id type,
5148 struct perf_sample_data *data,
5149 struct pt_regs *regs)
5151 if (event->attr.type != type)
5154 if (event->attr.config != event_id)
5157 if (perf_exclude_event(event, regs))
5163 static inline u64 swevent_hash(u64 type, u32 event_id)
5165 u64 val = event_id | (type << 32);
5167 return hash_64(val, SWEVENT_HLIST_BITS);
5170 static inline struct hlist_head *
5171 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
5173 u64 hash = swevent_hash(type, event_id);
5175 return &hlist->heads[hash];
5178 /* For the read side: events when they trigger */
5179 static inline struct hlist_head *
5180 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
5182 struct swevent_hlist *hlist;
5184 hlist = rcu_dereference(swhash->swevent_hlist);
5188 return __find_swevent_head(hlist, type, event_id);
5191 /* For the event head insertion and removal in the hlist */
5192 static inline struct hlist_head *
5193 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
5195 struct swevent_hlist *hlist;
5196 u32 event_id = event->attr.config;
5197 u64 type = event->attr.type;
5200 * Event scheduling is always serialized against hlist allocation
5201 * and release. Which makes the protected version suitable here.
5202 * The context lock guarantees that.
5204 hlist = rcu_dereference_protected(swhash->swevent_hlist,
5205 lockdep_is_held(&event->ctx->lock));
5209 return __find_swevent_head(hlist, type, event_id);
5212 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5214 struct perf_sample_data *data,
5215 struct pt_regs *regs)
5217 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5218 struct perf_event *event;
5219 struct hlist_node *node;
5220 struct hlist_head *head;
5223 head = find_swevent_head_rcu(swhash, type, event_id);
5227 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5228 if (perf_swevent_match(event, type, event_id, data, regs))
5229 perf_swevent_event(event, nr, nmi, data, regs);
5235 int perf_swevent_get_recursion_context(void)
5237 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5239 return get_recursion_context(swhash->recursion);
5241 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
5243 inline void perf_swevent_put_recursion_context(int rctx)
5245 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5247 put_recursion_context(swhash->recursion, rctx);
5250 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
5251 struct pt_regs *regs, u64 addr)
5253 struct perf_sample_data data;
5256 preempt_disable_notrace();
5257 rctx = perf_swevent_get_recursion_context();
5261 perf_sample_data_init(&data, addr);
5263 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
5265 perf_swevent_put_recursion_context(rctx);
5266 preempt_enable_notrace();
5269 static void perf_swevent_read(struct perf_event *event)
5273 static int perf_swevent_add(struct perf_event *event, int flags)
5275 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5276 struct hw_perf_event *hwc = &event->hw;
5277 struct hlist_head *head;
5279 if (is_sampling_event(event)) {
5280 hwc->last_period = hwc->sample_period;
5281 perf_swevent_set_period(event);
5284 hwc->state = !(flags & PERF_EF_START);
5286 head = find_swevent_head(swhash, event);
5287 if (WARN_ON_ONCE(!head))
5290 hlist_add_head_rcu(&event->hlist_entry, head);
5295 static void perf_swevent_del(struct perf_event *event, int flags)
5297 hlist_del_rcu(&event->hlist_entry);
5300 static void perf_swevent_start(struct perf_event *event, int flags)
5302 event->hw.state = 0;
5305 static void perf_swevent_stop(struct perf_event *event, int flags)
5307 event->hw.state = PERF_HES_STOPPED;
5310 /* Deref the hlist from the update side */
5311 static inline struct swevent_hlist *
5312 swevent_hlist_deref(struct swevent_htable *swhash)
5314 return rcu_dereference_protected(swhash->swevent_hlist,
5315 lockdep_is_held(&swhash->hlist_mutex));
5318 static void swevent_hlist_release(struct swevent_htable *swhash)
5320 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
5325 rcu_assign_pointer(swhash->swevent_hlist, NULL);
5326 kfree_rcu(hlist, rcu_head);
5329 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5331 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5333 mutex_lock(&swhash->hlist_mutex);
5335 if (!--swhash->hlist_refcount)
5336 swevent_hlist_release(swhash);
5338 mutex_unlock(&swhash->hlist_mutex);
5341 static void swevent_hlist_put(struct perf_event *event)
5345 if (event->cpu != -1) {
5346 swevent_hlist_put_cpu(event, event->cpu);
5350 for_each_possible_cpu(cpu)
5351 swevent_hlist_put_cpu(event, cpu);
5354 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5356 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5359 mutex_lock(&swhash->hlist_mutex);
5361 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
5362 struct swevent_hlist *hlist;
5364 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5369 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5371 swhash->hlist_refcount++;
5373 mutex_unlock(&swhash->hlist_mutex);
5378 static int swevent_hlist_get(struct perf_event *event)
5381 int cpu, failed_cpu;
5383 if (event->cpu != -1)
5384 return swevent_hlist_get_cpu(event, event->cpu);
5387 for_each_possible_cpu(cpu) {
5388 err = swevent_hlist_get_cpu(event, cpu);
5398 for_each_possible_cpu(cpu) {
5399 if (cpu == failed_cpu)
5401 swevent_hlist_put_cpu(event, cpu);
5408 struct jump_label_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
5410 static void sw_perf_event_destroy(struct perf_event *event)
5412 u64 event_id = event->attr.config;
5414 WARN_ON(event->parent);
5416 jump_label_dec(&perf_swevent_enabled[event_id]);
5417 swevent_hlist_put(event);
5420 static int perf_swevent_init(struct perf_event *event)
5422 int event_id = event->attr.config;
5424 if (event->attr.type != PERF_TYPE_SOFTWARE)
5428 case PERF_COUNT_SW_CPU_CLOCK:
5429 case PERF_COUNT_SW_TASK_CLOCK:
5436 if (event_id >= PERF_COUNT_SW_MAX)
5439 if (!event->parent) {
5442 err = swevent_hlist_get(event);
5446 jump_label_inc(&perf_swevent_enabled[event_id]);
5447 event->destroy = sw_perf_event_destroy;
5453 static struct pmu perf_swevent = {
5454 .task_ctx_nr = perf_sw_context,
5456 .event_init = perf_swevent_init,
5457 .add = perf_swevent_add,
5458 .del = perf_swevent_del,
5459 .start = perf_swevent_start,
5460 .stop = perf_swevent_stop,
5461 .read = perf_swevent_read,
5464 #ifdef CONFIG_EVENT_TRACING
5466 static int perf_tp_filter_match(struct perf_event *event,
5467 struct perf_sample_data *data)
5469 void *record = data->raw->data;
5471 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5476 static int perf_tp_event_match(struct perf_event *event,
5477 struct perf_sample_data *data,
5478 struct pt_regs *regs)
5480 if (event->hw.state & PERF_HES_STOPPED)
5483 * All tracepoints are from kernel-space.
5485 if (event->attr.exclude_kernel)
5488 if (!perf_tp_filter_match(event, data))
5494 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5495 struct pt_regs *regs, struct hlist_head *head, int rctx)
5497 struct perf_sample_data data;
5498 struct perf_event *event;
5499 struct hlist_node *node;
5501 struct perf_raw_record raw = {
5506 perf_sample_data_init(&data, addr);
5509 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5510 if (perf_tp_event_match(event, &data, regs))
5511 perf_swevent_event(event, count, 1, &data, regs);
5514 perf_swevent_put_recursion_context(rctx);
5516 EXPORT_SYMBOL_GPL(perf_tp_event);
5518 static void tp_perf_event_destroy(struct perf_event *event)
5520 perf_trace_destroy(event);
5523 static int perf_tp_event_init(struct perf_event *event)
5527 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5530 err = perf_trace_init(event);
5534 event->destroy = tp_perf_event_destroy;
5539 static struct pmu perf_tracepoint = {
5540 .task_ctx_nr = perf_sw_context,
5542 .event_init = perf_tp_event_init,
5543 .add = perf_trace_add,
5544 .del = perf_trace_del,
5545 .start = perf_swevent_start,
5546 .stop = perf_swevent_stop,
5547 .read = perf_swevent_read,
5550 static inline void perf_tp_register(void)
5552 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5555 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5560 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5563 filter_str = strndup_user(arg, PAGE_SIZE);
5564 if (IS_ERR(filter_str))
5565 return PTR_ERR(filter_str);
5567 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5573 static void perf_event_free_filter(struct perf_event *event)
5575 ftrace_profile_free_filter(event);
5580 static inline void perf_tp_register(void)
5584 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5589 static void perf_event_free_filter(struct perf_event *event)
5593 #endif /* CONFIG_EVENT_TRACING */
5595 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5596 void perf_bp_event(struct perf_event *bp, void *data)
5598 struct perf_sample_data sample;
5599 struct pt_regs *regs = data;
5601 perf_sample_data_init(&sample, bp->attr.bp_addr);
5603 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5604 perf_swevent_event(bp, 1, 1, &sample, regs);
5609 * hrtimer based swevent callback
5612 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5614 enum hrtimer_restart ret = HRTIMER_RESTART;
5615 struct perf_sample_data data;
5616 struct pt_regs *regs;
5617 struct perf_event *event;
5620 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5622 if (event->state != PERF_EVENT_STATE_ACTIVE)
5623 return HRTIMER_NORESTART;
5625 event->pmu->read(event);
5627 perf_sample_data_init(&data, 0);
5628 data.period = event->hw.last_period;
5629 regs = get_irq_regs();
5631 if (regs && !perf_exclude_event(event, regs)) {
5632 if (!(event->attr.exclude_idle && current->pid == 0))
5633 if (perf_event_overflow(event, 0, &data, regs))
5634 ret = HRTIMER_NORESTART;
5637 period = max_t(u64, 10000, event->hw.sample_period);
5638 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5643 static void perf_swevent_start_hrtimer(struct perf_event *event)
5645 struct hw_perf_event *hwc = &event->hw;
5648 if (!is_sampling_event(event))
5651 period = local64_read(&hwc->period_left);
5656 local64_set(&hwc->period_left, 0);
5658 period = max_t(u64, 10000, hwc->sample_period);
5660 __hrtimer_start_range_ns(&hwc->hrtimer,
5661 ns_to_ktime(period), 0,
5662 HRTIMER_MODE_REL_PINNED, 0);
5665 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5667 struct hw_perf_event *hwc = &event->hw;
5669 if (is_sampling_event(event)) {
5670 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5671 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5673 hrtimer_cancel(&hwc->hrtimer);
5677 static void perf_swevent_init_hrtimer(struct perf_event *event)
5679 struct hw_perf_event *hwc = &event->hw;
5681 if (!is_sampling_event(event))
5684 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5685 hwc->hrtimer.function = perf_swevent_hrtimer;
5688 * Since hrtimers have a fixed rate, we can do a static freq->period
5689 * mapping and avoid the whole period adjust feedback stuff.
5691 if (event->attr.freq) {
5692 long freq = event->attr.sample_freq;
5694 event->attr.sample_period = NSEC_PER_SEC / freq;
5695 hwc->sample_period = event->attr.sample_period;
5696 local64_set(&hwc->period_left, hwc->sample_period);
5697 event->attr.freq = 0;
5702 * Software event: cpu wall time clock
5705 static void cpu_clock_event_update(struct perf_event *event)
5710 now = local_clock();
5711 prev = local64_xchg(&event->hw.prev_count, now);
5712 local64_add(now - prev, &event->count);
5715 static void cpu_clock_event_start(struct perf_event *event, int flags)
5717 local64_set(&event->hw.prev_count, local_clock());
5718 perf_swevent_start_hrtimer(event);
5721 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5723 perf_swevent_cancel_hrtimer(event);
5724 cpu_clock_event_update(event);
5727 static int cpu_clock_event_add(struct perf_event *event, int flags)
5729 if (flags & PERF_EF_START)
5730 cpu_clock_event_start(event, flags);
5735 static void cpu_clock_event_del(struct perf_event *event, int flags)
5737 cpu_clock_event_stop(event, flags);
5740 static void cpu_clock_event_read(struct perf_event *event)
5742 cpu_clock_event_update(event);
5745 static int cpu_clock_event_init(struct perf_event *event)
5747 if (event->attr.type != PERF_TYPE_SOFTWARE)
5750 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5753 perf_swevent_init_hrtimer(event);
5758 static struct pmu perf_cpu_clock = {
5759 .task_ctx_nr = perf_sw_context,
5761 .event_init = cpu_clock_event_init,
5762 .add = cpu_clock_event_add,
5763 .del = cpu_clock_event_del,
5764 .start = cpu_clock_event_start,
5765 .stop = cpu_clock_event_stop,
5766 .read = cpu_clock_event_read,
5770 * Software event: task time clock
5773 static void task_clock_event_update(struct perf_event *event, u64 now)
5778 prev = local64_xchg(&event->hw.prev_count, now);
5780 local64_add(delta, &event->count);
5783 static void task_clock_event_start(struct perf_event *event, int flags)
5785 local64_set(&event->hw.prev_count, event->ctx->time);
5786 perf_swevent_start_hrtimer(event);
5789 static void task_clock_event_stop(struct perf_event *event, int flags)
5791 perf_swevent_cancel_hrtimer(event);
5792 task_clock_event_update(event, event->ctx->time);
5795 static int task_clock_event_add(struct perf_event *event, int flags)
5797 if (flags & PERF_EF_START)
5798 task_clock_event_start(event, flags);
5803 static void task_clock_event_del(struct perf_event *event, int flags)
5805 task_clock_event_stop(event, PERF_EF_UPDATE);
5808 static void task_clock_event_read(struct perf_event *event)
5810 u64 now = perf_clock();
5811 u64 delta = now - event->ctx->timestamp;
5812 u64 time = event->ctx->time + delta;
5814 task_clock_event_update(event, time);
5817 static int task_clock_event_init(struct perf_event *event)
5819 if (event->attr.type != PERF_TYPE_SOFTWARE)
5822 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5825 perf_swevent_init_hrtimer(event);
5830 static struct pmu perf_task_clock = {
5831 .task_ctx_nr = perf_sw_context,
5833 .event_init = task_clock_event_init,
5834 .add = task_clock_event_add,
5835 .del = task_clock_event_del,
5836 .start = task_clock_event_start,
5837 .stop = task_clock_event_stop,
5838 .read = task_clock_event_read,
5841 static void perf_pmu_nop_void(struct pmu *pmu)
5845 static int perf_pmu_nop_int(struct pmu *pmu)
5850 static void perf_pmu_start_txn(struct pmu *pmu)
5852 perf_pmu_disable(pmu);
5855 static int perf_pmu_commit_txn(struct pmu *pmu)
5857 perf_pmu_enable(pmu);
5861 static void perf_pmu_cancel_txn(struct pmu *pmu)
5863 perf_pmu_enable(pmu);
5867 * Ensures all contexts with the same task_ctx_nr have the same
5868 * pmu_cpu_context too.
5870 static void *find_pmu_context(int ctxn)
5877 list_for_each_entry(pmu, &pmus, entry) {
5878 if (pmu->task_ctx_nr == ctxn)
5879 return pmu->pmu_cpu_context;
5885 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5889 for_each_possible_cpu(cpu) {
5890 struct perf_cpu_context *cpuctx;
5892 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5894 if (cpuctx->active_pmu == old_pmu)
5895 cpuctx->active_pmu = pmu;
5899 static void free_pmu_context(struct pmu *pmu)
5903 mutex_lock(&pmus_lock);
5905 * Like a real lame refcount.
5907 list_for_each_entry(i, &pmus, entry) {
5908 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5909 update_pmu_context(i, pmu);
5914 free_percpu(pmu->pmu_cpu_context);
5916 mutex_unlock(&pmus_lock);
5918 static struct idr pmu_idr;
5921 type_show(struct device *dev, struct device_attribute *attr, char *page)
5923 struct pmu *pmu = dev_get_drvdata(dev);
5925 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5928 static struct device_attribute pmu_dev_attrs[] = {
5933 static int pmu_bus_running;
5934 static struct bus_type pmu_bus = {
5935 .name = "event_source",
5936 .dev_attrs = pmu_dev_attrs,
5939 static void pmu_dev_release(struct device *dev)
5944 static int pmu_dev_alloc(struct pmu *pmu)
5948 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5952 device_initialize(pmu->dev);
5953 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5957 dev_set_drvdata(pmu->dev, pmu);
5958 pmu->dev->bus = &pmu_bus;
5959 pmu->dev->release = pmu_dev_release;
5960 ret = device_add(pmu->dev);
5968 put_device(pmu->dev);
5972 static struct lock_class_key cpuctx_mutex;
5973 static struct lock_class_key cpuctx_lock;
5975 int perf_pmu_register(struct pmu *pmu, char *name, int type)
5979 mutex_lock(&pmus_lock);
5981 pmu->pmu_disable_count = alloc_percpu(int);
5982 if (!pmu->pmu_disable_count)
5991 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5995 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
6003 if (pmu_bus_running) {
6004 ret = pmu_dev_alloc(pmu);
6010 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6011 if (pmu->pmu_cpu_context)
6012 goto got_cpu_context;
6014 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6015 if (!pmu->pmu_cpu_context)
6018 for_each_possible_cpu(cpu) {
6019 struct perf_cpu_context *cpuctx;
6021 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6022 __perf_event_init_context(&cpuctx->ctx);
6023 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
6024 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
6025 cpuctx->ctx.type = cpu_context;
6026 cpuctx->ctx.pmu = pmu;
6027 cpuctx->jiffies_interval = 1;
6028 INIT_LIST_HEAD(&cpuctx->rotation_list);
6029 cpuctx->active_pmu = pmu;
6033 if (!pmu->start_txn) {
6034 if (pmu->pmu_enable) {
6036 * If we have pmu_enable/pmu_disable calls, install
6037 * transaction stubs that use that to try and batch
6038 * hardware accesses.
6040 pmu->start_txn = perf_pmu_start_txn;
6041 pmu->commit_txn = perf_pmu_commit_txn;
6042 pmu->cancel_txn = perf_pmu_cancel_txn;
6044 pmu->start_txn = perf_pmu_nop_void;
6045 pmu->commit_txn = perf_pmu_nop_int;
6046 pmu->cancel_txn = perf_pmu_nop_void;
6050 if (!pmu->pmu_enable) {
6051 pmu->pmu_enable = perf_pmu_nop_void;
6052 pmu->pmu_disable = perf_pmu_nop_void;
6055 list_add_rcu(&pmu->entry, &pmus);
6058 mutex_unlock(&pmus_lock);
6063 device_del(pmu->dev);
6064 put_device(pmu->dev);
6067 if (pmu->type >= PERF_TYPE_MAX)
6068 idr_remove(&pmu_idr, pmu->type);
6071 free_percpu(pmu->pmu_disable_count);
6075 void perf_pmu_unregister(struct pmu *pmu)
6077 mutex_lock(&pmus_lock);
6078 list_del_rcu(&pmu->entry);
6079 mutex_unlock(&pmus_lock);
6082 * We dereference the pmu list under both SRCU and regular RCU, so
6083 * synchronize against both of those.
6085 synchronize_srcu(&pmus_srcu);
6088 free_percpu(pmu->pmu_disable_count);
6089 if (pmu->type >= PERF_TYPE_MAX)
6090 idr_remove(&pmu_idr, pmu->type);
6091 device_del(pmu->dev);
6092 put_device(pmu->dev);
6093 free_pmu_context(pmu);
6096 struct pmu *perf_init_event(struct perf_event *event)
6098 struct pmu *pmu = NULL;
6102 idx = srcu_read_lock(&pmus_srcu);
6105 pmu = idr_find(&pmu_idr, event->attr.type);
6108 ret = pmu->event_init(event);
6114 list_for_each_entry_rcu(pmu, &pmus, entry) {
6115 ret = pmu->event_init(event);
6119 if (ret != -ENOENT) {
6124 pmu = ERR_PTR(-ENOENT);
6126 srcu_read_unlock(&pmus_srcu, idx);
6132 * Allocate and initialize a event structure
6134 static struct perf_event *
6135 perf_event_alloc(struct perf_event_attr *attr, int cpu,
6136 struct task_struct *task,
6137 struct perf_event *group_leader,
6138 struct perf_event *parent_event,
6139 perf_overflow_handler_t overflow_handler)
6142 struct perf_event *event;
6143 struct hw_perf_event *hwc;
6146 if ((unsigned)cpu >= nr_cpu_ids) {
6147 if (!task || cpu != -1)
6148 return ERR_PTR(-EINVAL);
6151 event = kzalloc(sizeof(*event), GFP_KERNEL);
6153 return ERR_PTR(-ENOMEM);
6156 * Single events are their own group leaders, with an
6157 * empty sibling list:
6160 group_leader = event;
6162 mutex_init(&event->child_mutex);
6163 INIT_LIST_HEAD(&event->child_list);
6165 INIT_LIST_HEAD(&event->group_entry);
6166 INIT_LIST_HEAD(&event->event_entry);
6167 INIT_LIST_HEAD(&event->sibling_list);
6168 init_waitqueue_head(&event->waitq);
6169 init_irq_work(&event->pending, perf_pending_event);
6171 mutex_init(&event->mmap_mutex);
6174 event->attr = *attr;
6175 event->group_leader = group_leader;
6179 event->parent = parent_event;
6181 event->ns = get_pid_ns(current->nsproxy->pid_ns);
6182 event->id = atomic64_inc_return(&perf_event_id);
6184 event->state = PERF_EVENT_STATE_INACTIVE;
6187 event->attach_state = PERF_ATTACH_TASK;
6188 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6190 * hw_breakpoint is a bit difficult here..
6192 if (attr->type == PERF_TYPE_BREAKPOINT)
6193 event->hw.bp_target = task;
6197 if (!overflow_handler && parent_event)
6198 overflow_handler = parent_event->overflow_handler;
6200 event->overflow_handler = overflow_handler;
6203 event->state = PERF_EVENT_STATE_OFF;
6208 hwc->sample_period = attr->sample_period;
6209 if (attr->freq && attr->sample_freq)
6210 hwc->sample_period = 1;
6211 hwc->last_period = hwc->sample_period;
6213 local64_set(&hwc->period_left, hwc->sample_period);
6216 * we currently do not support PERF_FORMAT_GROUP on inherited events
6218 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6221 pmu = perf_init_event(event);
6227 else if (IS_ERR(pmu))
6232 put_pid_ns(event->ns);
6234 return ERR_PTR(err);
6239 if (!event->parent) {
6240 if (event->attach_state & PERF_ATTACH_TASK)
6241 jump_label_inc(&perf_sched_events);
6242 if (event->attr.mmap || event->attr.mmap_data)
6243 atomic_inc(&nr_mmap_events);
6244 if (event->attr.comm)
6245 atomic_inc(&nr_comm_events);
6246 if (event->attr.task)
6247 atomic_inc(&nr_task_events);
6248 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6249 err = get_callchain_buffers();
6252 return ERR_PTR(err);
6260 static int perf_copy_attr(struct perf_event_attr __user *uattr,
6261 struct perf_event_attr *attr)
6266 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6270 * zero the full structure, so that a short copy will be nice.
6272 memset(attr, 0, sizeof(*attr));
6274 ret = get_user(size, &uattr->size);
6278 if (size > PAGE_SIZE) /* silly large */
6281 if (!size) /* abi compat */
6282 size = PERF_ATTR_SIZE_VER0;
6284 if (size < PERF_ATTR_SIZE_VER0)
6288 * If we're handed a bigger struct than we know of,
6289 * ensure all the unknown bits are 0 - i.e. new
6290 * user-space does not rely on any kernel feature
6291 * extensions we dont know about yet.
6293 if (size > sizeof(*attr)) {
6294 unsigned char __user *addr;
6295 unsigned char __user *end;
6298 addr = (void __user *)uattr + sizeof(*attr);
6299 end = (void __user *)uattr + size;
6301 for (; addr < end; addr++) {
6302 ret = get_user(val, addr);
6308 size = sizeof(*attr);
6311 ret = copy_from_user(attr, uattr, size);
6316 * If the type exists, the corresponding creation will verify
6319 if (attr->type >= PERF_TYPE_MAX)
6322 if (attr->__reserved_1)
6325 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6328 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6335 put_user(sizeof(*attr), &uattr->size);
6341 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6343 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
6349 /* don't allow circular references */
6350 if (event == output_event)
6354 * Don't allow cross-cpu buffers
6356 if (output_event->cpu != event->cpu)
6360 * If its not a per-cpu buffer, it must be the same task.
6362 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6366 mutex_lock(&event->mmap_mutex);
6367 /* Can't redirect output if we've got an active mmap() */
6368 if (atomic_read(&event->mmap_count))
6372 /* get the buffer we want to redirect to */
6373 buffer = perf_buffer_get(output_event);
6378 old_buffer = event->buffer;
6379 rcu_assign_pointer(event->buffer, buffer);
6382 mutex_unlock(&event->mmap_mutex);
6385 perf_buffer_put(old_buffer);
6391 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6393 * @attr_uptr: event_id type attributes for monitoring/sampling
6396 * @group_fd: group leader event fd
6398 SYSCALL_DEFINE5(perf_event_open,
6399 struct perf_event_attr __user *, attr_uptr,
6400 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6402 struct perf_event *group_leader = NULL, *output_event = NULL;
6403 struct perf_event *event, *sibling;
6404 struct perf_event_attr attr;
6405 struct perf_event_context *ctx;
6406 struct file *event_file = NULL;
6407 struct file *group_file = NULL;
6408 struct task_struct *task = NULL;
6412 int fput_needed = 0;
6415 /* for future expandability... */
6416 if (flags & ~PERF_FLAG_ALL)
6419 err = perf_copy_attr(attr_uptr, &attr);
6423 if (!attr.exclude_kernel) {
6424 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6429 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6434 * In cgroup mode, the pid argument is used to pass the fd
6435 * opened to the cgroup directory in cgroupfs. The cpu argument
6436 * designates the cpu on which to monitor threads from that
6439 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6442 event_fd = get_unused_fd_flags(O_RDWR);
6446 if (group_fd != -1) {
6447 group_leader = perf_fget_light(group_fd, &fput_needed);
6448 if (IS_ERR(group_leader)) {
6449 err = PTR_ERR(group_leader);
6452 group_file = group_leader->filp;
6453 if (flags & PERF_FLAG_FD_OUTPUT)
6454 output_event = group_leader;
6455 if (flags & PERF_FLAG_FD_NO_GROUP)
6456 group_leader = NULL;
6459 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
6460 task = find_lively_task_by_vpid(pid);
6462 err = PTR_ERR(task);
6467 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
6468 if (IS_ERR(event)) {
6469 err = PTR_ERR(event);
6473 if (flags & PERF_FLAG_PID_CGROUP) {
6474 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6479 * - that has cgroup constraint on event->cpu
6480 * - that may need work on context switch
6482 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6483 jump_label_inc(&perf_sched_events);
6487 * Special case software events and allow them to be part of
6488 * any hardware group.
6493 (is_software_event(event) != is_software_event(group_leader))) {
6494 if (is_software_event(event)) {
6496 * If event and group_leader are not both a software
6497 * event, and event is, then group leader is not.
6499 * Allow the addition of software events to !software
6500 * groups, this is safe because software events never
6503 pmu = group_leader->pmu;
6504 } else if (is_software_event(group_leader) &&
6505 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6507 * In case the group is a pure software group, and we
6508 * try to add a hardware event, move the whole group to
6509 * the hardware context.
6516 * Get the target context (task or percpu):
6518 ctx = find_get_context(pmu, task, cpu);
6525 put_task_struct(task);
6530 * Look up the group leader (we will attach this event to it):
6536 * Do not allow a recursive hierarchy (this new sibling
6537 * becoming part of another group-sibling):
6539 if (group_leader->group_leader != group_leader)
6542 * Do not allow to attach to a group in a different
6543 * task or CPU context:
6546 if (group_leader->ctx->type != ctx->type)
6549 if (group_leader->ctx != ctx)
6554 * Only a group leader can be exclusive or pinned
6556 if (attr.exclusive || attr.pinned)
6561 err = perf_event_set_output(event, output_event);
6566 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6567 if (IS_ERR(event_file)) {
6568 err = PTR_ERR(event_file);
6573 struct perf_event_context *gctx = group_leader->ctx;
6575 mutex_lock(&gctx->mutex);
6576 perf_remove_from_context(group_leader);
6577 list_for_each_entry(sibling, &group_leader->sibling_list,
6579 perf_remove_from_context(sibling);
6582 mutex_unlock(&gctx->mutex);
6586 event->filp = event_file;
6587 WARN_ON_ONCE(ctx->parent_ctx);
6588 mutex_lock(&ctx->mutex);
6591 perf_install_in_context(ctx, group_leader, cpu);
6593 list_for_each_entry(sibling, &group_leader->sibling_list,
6595 perf_install_in_context(ctx, sibling, cpu);
6600 perf_install_in_context(ctx, event, cpu);
6602 perf_unpin_context(ctx);
6603 mutex_unlock(&ctx->mutex);
6605 event->owner = current;
6607 mutex_lock(¤t->perf_event_mutex);
6608 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6609 mutex_unlock(¤t->perf_event_mutex);
6612 * Precalculate sample_data sizes
6614 perf_event__header_size(event);
6615 perf_event__id_header_size(event);
6618 * Drop the reference on the group_event after placing the
6619 * new event on the sibling_list. This ensures destruction
6620 * of the group leader will find the pointer to itself in
6621 * perf_group_detach().
6623 fput_light(group_file, fput_needed);
6624 fd_install(event_fd, event_file);
6628 perf_unpin_context(ctx);
6634 put_task_struct(task);
6636 fput_light(group_file, fput_needed);
6638 put_unused_fd(event_fd);
6643 * perf_event_create_kernel_counter
6645 * @attr: attributes of the counter to create
6646 * @cpu: cpu in which the counter is bound
6647 * @task: task to profile (NULL for percpu)
6650 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6651 struct task_struct *task,
6652 perf_overflow_handler_t overflow_handler)
6654 struct perf_event_context *ctx;
6655 struct perf_event *event;
6659 * Get the target context (task or percpu):
6662 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
6663 if (IS_ERR(event)) {
6664 err = PTR_ERR(event);
6668 ctx = find_get_context(event->pmu, task, cpu);
6675 WARN_ON_ONCE(ctx->parent_ctx);
6676 mutex_lock(&ctx->mutex);
6677 perf_install_in_context(ctx, event, cpu);
6679 perf_unpin_context(ctx);
6680 mutex_unlock(&ctx->mutex);
6687 return ERR_PTR(err);
6689 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6691 static void sync_child_event(struct perf_event *child_event,
6692 struct task_struct *child)
6694 struct perf_event *parent_event = child_event->parent;
6697 if (child_event->attr.inherit_stat)
6698 perf_event_read_event(child_event, child);
6700 child_val = perf_event_count(child_event);
6703 * Add back the child's count to the parent's count:
6705 atomic64_add(child_val, &parent_event->child_count);
6706 atomic64_add(child_event->total_time_enabled,
6707 &parent_event->child_total_time_enabled);
6708 atomic64_add(child_event->total_time_running,
6709 &parent_event->child_total_time_running);
6712 * Remove this event from the parent's list
6714 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6715 mutex_lock(&parent_event->child_mutex);
6716 list_del_init(&child_event->child_list);
6717 mutex_unlock(&parent_event->child_mutex);
6720 * Release the parent event, if this was the last
6723 fput(parent_event->filp);
6727 __perf_event_exit_task(struct perf_event *child_event,
6728 struct perf_event_context *child_ctx,
6729 struct task_struct *child)
6731 if (child_event->parent) {
6732 raw_spin_lock_irq(&child_ctx->lock);
6733 perf_group_detach(child_event);
6734 raw_spin_unlock_irq(&child_ctx->lock);
6737 perf_remove_from_context(child_event);
6740 * It can happen that the parent exits first, and has events
6741 * that are still around due to the child reference. These
6742 * events need to be zapped.
6744 if (child_event->parent) {
6745 sync_child_event(child_event, child);
6746 free_event(child_event);
6750 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6752 struct perf_event *child_event, *tmp;
6753 struct perf_event_context *child_ctx;
6754 unsigned long flags;
6756 if (likely(!child->perf_event_ctxp[ctxn])) {
6757 perf_event_task(child, NULL, 0);
6761 local_irq_save(flags);
6763 * We can't reschedule here because interrupts are disabled,
6764 * and either child is current or it is a task that can't be
6765 * scheduled, so we are now safe from rescheduling changing
6768 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6771 * Take the context lock here so that if find_get_context is
6772 * reading child->perf_event_ctxp, we wait until it has
6773 * incremented the context's refcount before we do put_ctx below.
6775 raw_spin_lock(&child_ctx->lock);
6776 task_ctx_sched_out(child_ctx);
6777 child->perf_event_ctxp[ctxn] = NULL;
6779 * If this context is a clone; unclone it so it can't get
6780 * swapped to another process while we're removing all
6781 * the events from it.
6783 unclone_ctx(child_ctx);
6784 update_context_time(child_ctx);
6785 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6788 * Report the task dead after unscheduling the events so that we
6789 * won't get any samples after PERF_RECORD_EXIT. We can however still
6790 * get a few PERF_RECORD_READ events.
6792 perf_event_task(child, child_ctx, 0);
6795 * We can recurse on the same lock type through:
6797 * __perf_event_exit_task()
6798 * sync_child_event()
6799 * fput(parent_event->filp)
6801 * mutex_lock(&ctx->mutex)
6803 * But since its the parent context it won't be the same instance.
6805 mutex_lock(&child_ctx->mutex);
6808 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6810 __perf_event_exit_task(child_event, child_ctx, child);
6812 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6814 __perf_event_exit_task(child_event, child_ctx, child);
6817 * If the last event was a group event, it will have appended all
6818 * its siblings to the list, but we obtained 'tmp' before that which
6819 * will still point to the list head terminating the iteration.
6821 if (!list_empty(&child_ctx->pinned_groups) ||
6822 !list_empty(&child_ctx->flexible_groups))
6825 mutex_unlock(&child_ctx->mutex);
6831 * When a child task exits, feed back event values to parent events.
6833 void perf_event_exit_task(struct task_struct *child)
6835 struct perf_event *event, *tmp;
6838 mutex_lock(&child->perf_event_mutex);
6839 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6841 list_del_init(&event->owner_entry);
6844 * Ensure the list deletion is visible before we clear
6845 * the owner, closes a race against perf_release() where
6846 * we need to serialize on the owner->perf_event_mutex.
6849 event->owner = NULL;
6851 mutex_unlock(&child->perf_event_mutex);
6853 for_each_task_context_nr(ctxn)
6854 perf_event_exit_task_context(child, ctxn);
6857 static void perf_free_event(struct perf_event *event,
6858 struct perf_event_context *ctx)
6860 struct perf_event *parent = event->parent;
6862 if (WARN_ON_ONCE(!parent))
6865 mutex_lock(&parent->child_mutex);
6866 list_del_init(&event->child_list);
6867 mutex_unlock(&parent->child_mutex);
6871 perf_group_detach(event);
6872 list_del_event(event, ctx);
6877 * free an unexposed, unused context as created by inheritance by
6878 * perf_event_init_task below, used by fork() in case of fail.
6880 void perf_event_free_task(struct task_struct *task)
6882 struct perf_event_context *ctx;
6883 struct perf_event *event, *tmp;
6886 for_each_task_context_nr(ctxn) {
6887 ctx = task->perf_event_ctxp[ctxn];
6891 mutex_lock(&ctx->mutex);
6893 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6895 perf_free_event(event, ctx);
6897 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6899 perf_free_event(event, ctx);
6901 if (!list_empty(&ctx->pinned_groups) ||
6902 !list_empty(&ctx->flexible_groups))
6905 mutex_unlock(&ctx->mutex);
6911 void perf_event_delayed_put(struct task_struct *task)
6915 for_each_task_context_nr(ctxn)
6916 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6920 * inherit a event from parent task to child task:
6922 static struct perf_event *
6923 inherit_event(struct perf_event *parent_event,
6924 struct task_struct *parent,
6925 struct perf_event_context *parent_ctx,
6926 struct task_struct *child,
6927 struct perf_event *group_leader,
6928 struct perf_event_context *child_ctx)
6930 struct perf_event *child_event;
6931 unsigned long flags;
6934 * Instead of creating recursive hierarchies of events,
6935 * we link inherited events back to the original parent,
6936 * which has a filp for sure, which we use as the reference
6939 if (parent_event->parent)
6940 parent_event = parent_event->parent;
6942 child_event = perf_event_alloc(&parent_event->attr,
6945 group_leader, parent_event,
6947 if (IS_ERR(child_event))
6952 * Make the child state follow the state of the parent event,
6953 * not its attr.disabled bit. We hold the parent's mutex,
6954 * so we won't race with perf_event_{en, dis}able_family.
6956 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6957 child_event->state = PERF_EVENT_STATE_INACTIVE;
6959 child_event->state = PERF_EVENT_STATE_OFF;
6961 if (parent_event->attr.freq) {
6962 u64 sample_period = parent_event->hw.sample_period;
6963 struct hw_perf_event *hwc = &child_event->hw;
6965 hwc->sample_period = sample_period;
6966 hwc->last_period = sample_period;
6968 local64_set(&hwc->period_left, sample_period);
6971 child_event->ctx = child_ctx;
6972 child_event->overflow_handler = parent_event->overflow_handler;
6975 * Precalculate sample_data sizes
6977 perf_event__header_size(child_event);
6978 perf_event__id_header_size(child_event);
6981 * Link it up in the child's context:
6983 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6984 add_event_to_ctx(child_event, child_ctx);
6985 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6988 * Get a reference to the parent filp - we will fput it
6989 * when the child event exits. This is safe to do because
6990 * we are in the parent and we know that the filp still
6991 * exists and has a nonzero count:
6993 atomic_long_inc(&parent_event->filp->f_count);
6996 * Link this into the parent event's child list
6998 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6999 mutex_lock(&parent_event->child_mutex);
7000 list_add_tail(&child_event->child_list, &parent_event->child_list);
7001 mutex_unlock(&parent_event->child_mutex);
7006 static int inherit_group(struct perf_event *parent_event,
7007 struct task_struct *parent,
7008 struct perf_event_context *parent_ctx,
7009 struct task_struct *child,
7010 struct perf_event_context *child_ctx)
7012 struct perf_event *leader;
7013 struct perf_event *sub;
7014 struct perf_event *child_ctr;
7016 leader = inherit_event(parent_event, parent, parent_ctx,
7017 child, NULL, child_ctx);
7019 return PTR_ERR(leader);
7020 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7021 child_ctr = inherit_event(sub, parent, parent_ctx,
7022 child, leader, child_ctx);
7023 if (IS_ERR(child_ctr))
7024 return PTR_ERR(child_ctr);
7030 inherit_task_group(struct perf_event *event, struct task_struct *parent,
7031 struct perf_event_context *parent_ctx,
7032 struct task_struct *child, int ctxn,
7036 struct perf_event_context *child_ctx;
7038 if (!event->attr.inherit) {
7043 child_ctx = child->perf_event_ctxp[ctxn];
7046 * This is executed from the parent task context, so
7047 * inherit events that have been marked for cloning.
7048 * First allocate and initialize a context for the
7052 child_ctx = alloc_perf_context(event->pmu, child);
7056 child->perf_event_ctxp[ctxn] = child_ctx;
7059 ret = inherit_group(event, parent, parent_ctx,
7069 * Initialize the perf_event context in task_struct
7071 int perf_event_init_context(struct task_struct *child, int ctxn)
7073 struct perf_event_context *child_ctx, *parent_ctx;
7074 struct perf_event_context *cloned_ctx;
7075 struct perf_event *event;
7076 struct task_struct *parent = current;
7077 int inherited_all = 1;
7078 unsigned long flags;
7081 if (likely(!parent->perf_event_ctxp[ctxn]))
7085 * If the parent's context is a clone, pin it so it won't get
7088 parent_ctx = perf_pin_task_context(parent, ctxn);
7091 * No need to check if parent_ctx != NULL here; since we saw
7092 * it non-NULL earlier, the only reason for it to become NULL
7093 * is if we exit, and since we're currently in the middle of
7094 * a fork we can't be exiting at the same time.
7098 * Lock the parent list. No need to lock the child - not PID
7099 * hashed yet and not running, so nobody can access it.
7101 mutex_lock(&parent_ctx->mutex);
7104 * We dont have to disable NMIs - we are only looking at
7105 * the list, not manipulating it:
7107 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
7108 ret = inherit_task_group(event, parent, parent_ctx,
7109 child, ctxn, &inherited_all);
7115 * We can't hold ctx->lock when iterating the ->flexible_group list due
7116 * to allocations, but we need to prevent rotation because
7117 * rotate_ctx() will change the list from interrupt context.
7119 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7120 parent_ctx->rotate_disable = 1;
7121 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7123 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
7124 ret = inherit_task_group(event, parent, parent_ctx,
7125 child, ctxn, &inherited_all);
7130 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7131 parent_ctx->rotate_disable = 0;
7133 child_ctx = child->perf_event_ctxp[ctxn];
7135 if (child_ctx && inherited_all) {
7137 * Mark the child context as a clone of the parent
7138 * context, or of whatever the parent is a clone of.
7140 * Note that if the parent is a clone, the holding of
7141 * parent_ctx->lock avoids it from being uncloned.
7143 cloned_ctx = parent_ctx->parent_ctx;
7145 child_ctx->parent_ctx = cloned_ctx;
7146 child_ctx->parent_gen = parent_ctx->parent_gen;
7148 child_ctx->parent_ctx = parent_ctx;
7149 child_ctx->parent_gen = parent_ctx->generation;
7151 get_ctx(child_ctx->parent_ctx);
7154 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7155 mutex_unlock(&parent_ctx->mutex);
7157 perf_unpin_context(parent_ctx);
7158 put_ctx(parent_ctx);
7164 * Initialize the perf_event context in task_struct
7166 int perf_event_init_task(struct task_struct *child)
7170 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7171 mutex_init(&child->perf_event_mutex);
7172 INIT_LIST_HEAD(&child->perf_event_list);
7174 for_each_task_context_nr(ctxn) {
7175 ret = perf_event_init_context(child, ctxn);
7183 static void __init perf_event_init_all_cpus(void)
7185 struct swevent_htable *swhash;
7188 for_each_possible_cpu(cpu) {
7189 swhash = &per_cpu(swevent_htable, cpu);
7190 mutex_init(&swhash->hlist_mutex);
7191 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
7195 static void __cpuinit perf_event_init_cpu(int cpu)
7197 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7199 mutex_lock(&swhash->hlist_mutex);
7200 if (swhash->hlist_refcount > 0) {
7201 struct swevent_hlist *hlist;
7203 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7205 rcu_assign_pointer(swhash->swevent_hlist, hlist);
7207 mutex_unlock(&swhash->hlist_mutex);
7210 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7211 static void perf_pmu_rotate_stop(struct pmu *pmu)
7213 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7215 WARN_ON(!irqs_disabled());
7217 list_del_init(&cpuctx->rotation_list);
7220 static void __perf_event_exit_context(void *__info)
7222 struct perf_event_context *ctx = __info;
7223 struct perf_event *event, *tmp;
7225 perf_pmu_rotate_stop(ctx->pmu);
7227 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
7228 __perf_remove_from_context(event);
7229 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
7230 __perf_remove_from_context(event);
7233 static void perf_event_exit_cpu_context(int cpu)
7235 struct perf_event_context *ctx;
7239 idx = srcu_read_lock(&pmus_srcu);
7240 list_for_each_entry_rcu(pmu, &pmus, entry) {
7241 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
7243 mutex_lock(&ctx->mutex);
7244 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7245 mutex_unlock(&ctx->mutex);
7247 srcu_read_unlock(&pmus_srcu, idx);
7250 static void perf_event_exit_cpu(int cpu)
7252 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7254 mutex_lock(&swhash->hlist_mutex);
7255 swevent_hlist_release(swhash);
7256 mutex_unlock(&swhash->hlist_mutex);
7258 perf_event_exit_cpu_context(cpu);
7261 static inline void perf_event_exit_cpu(int cpu) { }
7265 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7269 for_each_online_cpu(cpu)
7270 perf_event_exit_cpu(cpu);
7276 * Run the perf reboot notifier at the very last possible moment so that
7277 * the generic watchdog code runs as long as possible.
7279 static struct notifier_block perf_reboot_notifier = {
7280 .notifier_call = perf_reboot,
7281 .priority = INT_MIN,
7284 static int __cpuinit
7285 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7287 unsigned int cpu = (long)hcpu;
7289 switch (action & ~CPU_TASKS_FROZEN) {
7291 case CPU_UP_PREPARE:
7292 case CPU_DOWN_FAILED:
7293 perf_event_init_cpu(cpu);
7296 case CPU_UP_CANCELED:
7297 case CPU_DOWN_PREPARE:
7298 perf_event_exit_cpu(cpu);
7308 void __init perf_event_init(void)
7314 perf_event_init_all_cpus();
7315 init_srcu_struct(&pmus_srcu);
7316 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7317 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7318 perf_pmu_register(&perf_task_clock, NULL, -1);
7320 perf_cpu_notifier(perf_cpu_notify);
7321 register_reboot_notifier(&perf_reboot_notifier);
7323 ret = init_hw_breakpoint();
7324 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7327 static int __init perf_event_sysfs_init(void)
7332 mutex_lock(&pmus_lock);
7334 ret = bus_register(&pmu_bus);
7338 list_for_each_entry(pmu, &pmus, entry) {
7339 if (!pmu->name || pmu->type < 0)
7342 ret = pmu_dev_alloc(pmu);
7343 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7345 pmu_bus_running = 1;
7349 mutex_unlock(&pmus_lock);
7353 device_initcall(perf_event_sysfs_init);
7355 #ifdef CONFIG_CGROUP_PERF
7356 static struct cgroup_subsys_state *perf_cgroup_create(
7357 struct cgroup_subsys *ss, struct cgroup *cont)
7359 struct perf_cgroup *jc;
7361 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
7363 return ERR_PTR(-ENOMEM);
7365 jc->info = alloc_percpu(struct perf_cgroup_info);
7368 return ERR_PTR(-ENOMEM);
7374 static void perf_cgroup_destroy(struct cgroup_subsys *ss,
7375 struct cgroup *cont)
7377 struct perf_cgroup *jc;
7378 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7379 struct perf_cgroup, css);
7380 free_percpu(jc->info);
7384 static int __perf_cgroup_move(void *info)
7386 struct task_struct *task = info;
7387 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7391 static void perf_cgroup_move(struct task_struct *task)
7393 task_function_call(task, __perf_cgroup_move, task);
7396 static void perf_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
7397 struct cgroup *old_cgrp, struct task_struct *task,
7400 perf_cgroup_move(task);
7402 struct task_struct *c;
7404 list_for_each_entry_rcu(c, &task->thread_group, thread_group) {
7405 perf_cgroup_move(c);
7411 static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7412 struct cgroup *old_cgrp, struct task_struct *task)
7415 * cgroup_exit() is called in the copy_process() failure path.
7416 * Ignore this case since the task hasn't ran yet, this avoids
7417 * trying to poke a half freed task state from generic code.
7419 if (!(task->flags & PF_EXITING))
7422 perf_cgroup_move(task);
7425 struct cgroup_subsys perf_subsys = {
7426 .name = "perf_event",
7427 .subsys_id = perf_subsys_id,
7428 .create = perf_cgroup_create,
7429 .destroy = perf_cgroup_destroy,
7430 .exit = perf_cgroup_exit,
7431 .attach = perf_cgroup_attach,
7433 #endif /* CONFIG_CGROUP_PERF */