2 * Performance events core code:
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/idr.h>
17 #include <linux/file.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/hash.h>
21 #include <linux/sysfs.h>
22 #include <linux/dcache.h>
23 #include <linux/percpu.h>
24 #include <linux/ptrace.h>
25 #include <linux/reboot.h>
26 #include <linux/vmstat.h>
27 #include <linux/device.h>
28 #include <linux/export.h>
29 #include <linux/vmalloc.h>
30 #include <linux/hardirq.h>
31 #include <linux/rculist.h>
32 #include <linux/uaccess.h>
33 #include <linux/syscalls.h>
34 #include <linux/anon_inodes.h>
35 #include <linux/kernel_stat.h>
36 #include <linux/perf_event.h>
37 #include <linux/ftrace_event.h>
38 #include <linux/hw_breakpoint.h>
42 #include <asm/irq_regs.h>
44 struct remote_function_call {
45 struct task_struct *p;
46 int (*func)(void *info);
51 static void remote_function(void *data)
53 struct remote_function_call *tfc = data;
54 struct task_struct *p = tfc->p;
58 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
62 tfc->ret = tfc->func(tfc->info);
66 * task_function_call - call a function on the cpu on which a task runs
67 * @p: the task to evaluate
68 * @func: the function to be called
69 * @info: the function call argument
71 * Calls the function @func when the task is currently running. This might
72 * be on the current CPU, which just calls the function directly
74 * returns: @func return value, or
75 * -ESRCH - when the process isn't running
76 * -EAGAIN - when the process moved away
79 task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
81 struct remote_function_call data = {
85 .ret = -ESRCH, /* No such (running) process */
89 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
95 * cpu_function_call - call a function on the cpu
96 * @func: the function to be called
97 * @info: the function call argument
99 * Calls the function @func on the remote cpu.
101 * returns: @func return value or -ENXIO when the cpu is offline
103 static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
105 struct remote_function_call data = {
109 .ret = -ENXIO, /* No such CPU */
112 smp_call_function_single(cpu, remote_function, &data, 1);
117 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
118 PERF_FLAG_FD_OUTPUT |\
119 PERF_FLAG_PID_CGROUP)
122 EVENT_FLEXIBLE = 0x1,
124 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
128 * perf_sched_events : >0 events exist
129 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
131 struct jump_label_key perf_sched_events __read_mostly;
132 static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
134 static atomic_t nr_mmap_events __read_mostly;
135 static atomic_t nr_comm_events __read_mostly;
136 static atomic_t nr_task_events __read_mostly;
138 static LIST_HEAD(pmus);
139 static DEFINE_MUTEX(pmus_lock);
140 static struct srcu_struct pmus_srcu;
143 * perf event paranoia level:
144 * -1 - not paranoid at all
145 * 0 - disallow raw tracepoint access for unpriv
146 * 1 - disallow cpu events for unpriv
147 * 2 - disallow kernel profiling for unpriv
149 int sysctl_perf_event_paranoid __read_mostly = 1;
151 /* Minimum for 512 kiB + 1 user control page */
152 int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
155 * max perf event sample rate
157 #define DEFAULT_MAX_SAMPLE_RATE 100000
158 int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
159 static int max_samples_per_tick __read_mostly =
160 DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
162 int perf_proc_update_handler(struct ctl_table *table, int write,
163 void __user *buffer, size_t *lenp,
166 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
171 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
176 static atomic64_t perf_event_id;
178 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
179 enum event_type_t event_type);
181 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
182 enum event_type_t event_type,
183 struct task_struct *task);
185 static void update_context_time(struct perf_event_context *ctx);
186 static u64 perf_event_time(struct perf_event *event);
188 void __weak perf_event_print_debug(void) { }
190 extern __weak const char *perf_pmu_name(void)
195 static inline u64 perf_clock(void)
197 return local_clock();
200 static inline struct perf_cpu_context *
201 __get_cpu_context(struct perf_event_context *ctx)
203 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
206 static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
207 struct perf_event_context *ctx)
209 raw_spin_lock(&cpuctx->ctx.lock);
211 raw_spin_lock(&ctx->lock);
214 static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
215 struct perf_event_context *ctx)
218 raw_spin_unlock(&ctx->lock);
219 raw_spin_unlock(&cpuctx->ctx.lock);
222 #ifdef CONFIG_CGROUP_PERF
225 * Must ensure cgroup is pinned (css_get) before calling
226 * this function. In other words, we cannot call this function
227 * if there is no cgroup event for the current CPU context.
229 static inline struct perf_cgroup *
230 perf_cgroup_from_task(struct task_struct *task)
232 return container_of(task_subsys_state(task, perf_subsys_id),
233 struct perf_cgroup, css);
237 perf_cgroup_match(struct perf_event *event)
239 struct perf_event_context *ctx = event->ctx;
240 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
242 return !event->cgrp || event->cgrp == cpuctx->cgrp;
245 static inline bool perf_tryget_cgroup(struct perf_event *event)
247 return css_tryget(&event->cgrp->css);
250 static inline void perf_put_cgroup(struct perf_event *event)
252 css_put(&event->cgrp->css);
255 static inline void perf_detach_cgroup(struct perf_event *event)
257 perf_put_cgroup(event);
261 static inline int is_cgroup_event(struct perf_event *event)
263 return event->cgrp != NULL;
266 static inline u64 perf_cgroup_event_time(struct perf_event *event)
268 struct perf_cgroup_info *t;
270 t = per_cpu_ptr(event->cgrp->info, event->cpu);
274 static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
276 struct perf_cgroup_info *info;
281 info = this_cpu_ptr(cgrp->info);
283 info->time += now - info->timestamp;
284 info->timestamp = now;
287 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
289 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
291 __update_cgrp_time(cgrp_out);
294 static inline void update_cgrp_time_from_event(struct perf_event *event)
296 struct perf_cgroup *cgrp;
299 * ensure we access cgroup data only when needed and
300 * when we know the cgroup is pinned (css_get)
302 if (!is_cgroup_event(event))
305 cgrp = perf_cgroup_from_task(current);
307 * Do not update time when cgroup is not active
309 if (cgrp == event->cgrp)
310 __update_cgrp_time(event->cgrp);
314 perf_cgroup_set_timestamp(struct task_struct *task,
315 struct perf_event_context *ctx)
317 struct perf_cgroup *cgrp;
318 struct perf_cgroup_info *info;
321 * ctx->lock held by caller
322 * ensure we do not access cgroup data
323 * unless we have the cgroup pinned (css_get)
325 if (!task || !ctx->nr_cgroups)
328 cgrp = perf_cgroup_from_task(task);
329 info = this_cpu_ptr(cgrp->info);
330 info->timestamp = ctx->timestamp;
333 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
334 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
337 * reschedule events based on the cgroup constraint of task.
339 * mode SWOUT : schedule out everything
340 * mode SWIN : schedule in based on cgroup for next
342 void perf_cgroup_switch(struct task_struct *task, int mode)
344 struct perf_cpu_context *cpuctx;
349 * disable interrupts to avoid geting nr_cgroup
350 * changes via __perf_event_disable(). Also
353 local_irq_save(flags);
356 * we reschedule only in the presence of cgroup
357 * constrained events.
361 list_for_each_entry_rcu(pmu, &pmus, entry) {
362 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
365 * perf_cgroup_events says at least one
366 * context on this CPU has cgroup events.
368 * ctx->nr_cgroups reports the number of cgroup
369 * events for a context.
371 if (cpuctx->ctx.nr_cgroups > 0) {
372 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
373 perf_pmu_disable(cpuctx->ctx.pmu);
375 if (mode & PERF_CGROUP_SWOUT) {
376 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
378 * must not be done before ctxswout due
379 * to event_filter_match() in event_sched_out()
384 if (mode & PERF_CGROUP_SWIN) {
385 WARN_ON_ONCE(cpuctx->cgrp);
386 /* set cgrp before ctxsw in to
387 * allow event_filter_match() to not
388 * have to pass task around
390 cpuctx->cgrp = perf_cgroup_from_task(task);
391 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
393 perf_pmu_enable(cpuctx->ctx.pmu);
394 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
400 local_irq_restore(flags);
403 static inline void perf_cgroup_sched_out(struct task_struct *task,
404 struct task_struct *next)
406 struct perf_cgroup *cgrp1;
407 struct perf_cgroup *cgrp2 = NULL;
410 * we come here when we know perf_cgroup_events > 0
412 cgrp1 = perf_cgroup_from_task(task);
415 * next is NULL when called from perf_event_enable_on_exec()
416 * that will systematically cause a cgroup_switch()
419 cgrp2 = perf_cgroup_from_task(next);
422 * only schedule out current cgroup events if we know
423 * that we are switching to a different cgroup. Otherwise,
424 * do no touch the cgroup events.
427 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
430 static inline void perf_cgroup_sched_in(struct task_struct *prev,
431 struct task_struct *task)
433 struct perf_cgroup *cgrp1;
434 struct perf_cgroup *cgrp2 = NULL;
437 * we come here when we know perf_cgroup_events > 0
439 cgrp1 = perf_cgroup_from_task(task);
441 /* prev can never be NULL */
442 cgrp2 = perf_cgroup_from_task(prev);
445 * only need to schedule in cgroup events if we are changing
446 * cgroup during ctxsw. Cgroup events were not scheduled
447 * out of ctxsw out if that was not the case.
450 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
453 static inline int perf_cgroup_connect(int fd, struct perf_event *event,
454 struct perf_event_attr *attr,
455 struct perf_event *group_leader)
457 struct perf_cgroup *cgrp;
458 struct cgroup_subsys_state *css;
460 int ret = 0, fput_needed;
462 file = fget_light(fd, &fput_needed);
466 css = cgroup_css_from_dir(file, perf_subsys_id);
472 cgrp = container_of(css, struct perf_cgroup, css);
475 /* must be done before we fput() the file */
476 if (!perf_tryget_cgroup(event)) {
483 * all events in a group must monitor
484 * the same cgroup because a task belongs
485 * to only one perf cgroup at a time
487 if (group_leader && group_leader->cgrp != cgrp) {
488 perf_detach_cgroup(event);
492 fput_light(file, fput_needed);
497 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
499 struct perf_cgroup_info *t;
500 t = per_cpu_ptr(event->cgrp->info, event->cpu);
501 event->shadow_ctx_time = now - t->timestamp;
505 perf_cgroup_defer_enabled(struct perf_event *event)
508 * when the current task's perf cgroup does not match
509 * the event's, we need to remember to call the
510 * perf_mark_enable() function the first time a task with
511 * a matching perf cgroup is scheduled in.
513 if (is_cgroup_event(event) && !perf_cgroup_match(event))
514 event->cgrp_defer_enabled = 1;
518 perf_cgroup_mark_enabled(struct perf_event *event,
519 struct perf_event_context *ctx)
521 struct perf_event *sub;
522 u64 tstamp = perf_event_time(event);
524 if (!event->cgrp_defer_enabled)
527 event->cgrp_defer_enabled = 0;
529 event->tstamp_enabled = tstamp - event->total_time_enabled;
530 list_for_each_entry(sub, &event->sibling_list, group_entry) {
531 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
532 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
533 sub->cgrp_defer_enabled = 0;
537 #else /* !CONFIG_CGROUP_PERF */
540 perf_cgroup_match(struct perf_event *event)
545 static inline void perf_detach_cgroup(struct perf_event *event)
548 static inline int is_cgroup_event(struct perf_event *event)
553 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
558 static inline void update_cgrp_time_from_event(struct perf_event *event)
562 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
566 static inline void perf_cgroup_sched_out(struct task_struct *task,
567 struct task_struct *next)
571 static inline void perf_cgroup_sched_in(struct task_struct *prev,
572 struct task_struct *task)
576 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
577 struct perf_event_attr *attr,
578 struct perf_event *group_leader)
584 perf_cgroup_set_timestamp(struct task_struct *task,
585 struct perf_event_context *ctx)
590 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
595 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
599 static inline u64 perf_cgroup_event_time(struct perf_event *event)
605 perf_cgroup_defer_enabled(struct perf_event *event)
610 perf_cgroup_mark_enabled(struct perf_event *event,
611 struct perf_event_context *ctx)
616 void perf_pmu_disable(struct pmu *pmu)
618 int *count = this_cpu_ptr(pmu->pmu_disable_count);
620 pmu->pmu_disable(pmu);
623 void perf_pmu_enable(struct pmu *pmu)
625 int *count = this_cpu_ptr(pmu->pmu_disable_count);
627 pmu->pmu_enable(pmu);
630 static DEFINE_PER_CPU(struct list_head, rotation_list);
633 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
634 * because they're strictly cpu affine and rotate_start is called with IRQs
635 * disabled, while rotate_context is called from IRQ context.
637 static void perf_pmu_rotate_start(struct pmu *pmu)
639 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
640 struct list_head *head = &__get_cpu_var(rotation_list);
642 WARN_ON(!irqs_disabled());
644 if (list_empty(&cpuctx->rotation_list))
645 list_add(&cpuctx->rotation_list, head);
648 static void get_ctx(struct perf_event_context *ctx)
650 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
653 static void put_ctx(struct perf_event_context *ctx)
655 if (atomic_dec_and_test(&ctx->refcount)) {
657 put_ctx(ctx->parent_ctx);
659 put_task_struct(ctx->task);
660 kfree_rcu(ctx, rcu_head);
664 static void unclone_ctx(struct perf_event_context *ctx)
666 if (ctx->parent_ctx) {
667 put_ctx(ctx->parent_ctx);
668 ctx->parent_ctx = NULL;
672 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
675 * only top level events have the pid namespace they were created in
678 event = event->parent;
680 return task_tgid_nr_ns(p, event->ns);
683 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
686 * only top level events have the pid namespace they were created in
689 event = event->parent;
691 return task_pid_nr_ns(p, event->ns);
695 * If we inherit events we want to return the parent event id
698 static u64 primary_event_id(struct perf_event *event)
703 id = event->parent->id;
709 * Get the perf_event_context for a task and lock it.
710 * This has to cope with with the fact that until it is locked,
711 * the context could get moved to another task.
713 static struct perf_event_context *
714 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
716 struct perf_event_context *ctx;
720 * One of the few rules of preemptible RCU is that one cannot do
721 * rcu_read_unlock() while holding a scheduler (or nested) lock when
722 * part of the read side critical section was preemptible -- see
723 * rcu_read_unlock_special().
725 * Since ctx->lock nests under rq->lock we must ensure the entire read
726 * side critical section is non-preemptible.
730 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
733 * If this context is a clone of another, it might
734 * get swapped for another underneath us by
735 * perf_event_task_sched_out, though the
736 * rcu_read_lock() protects us from any context
737 * getting freed. Lock the context and check if it
738 * got swapped before we could get the lock, and retry
739 * if so. If we locked the right context, then it
740 * can't get swapped on us any more.
742 raw_spin_lock_irqsave(&ctx->lock, *flags);
743 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
744 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
750 if (!atomic_inc_not_zero(&ctx->refcount)) {
751 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
761 * Get the context for a task and increment its pin_count so it
762 * can't get swapped to another task. This also increments its
763 * reference count so that the context can't get freed.
765 static struct perf_event_context *
766 perf_pin_task_context(struct task_struct *task, int ctxn)
768 struct perf_event_context *ctx;
771 ctx = perf_lock_task_context(task, ctxn, &flags);
774 raw_spin_unlock_irqrestore(&ctx->lock, flags);
779 static void perf_unpin_context(struct perf_event_context *ctx)
783 raw_spin_lock_irqsave(&ctx->lock, flags);
785 raw_spin_unlock_irqrestore(&ctx->lock, flags);
789 * Update the record of the current time in a context.
791 static void update_context_time(struct perf_event_context *ctx)
793 u64 now = perf_clock();
795 ctx->time += now - ctx->timestamp;
796 ctx->timestamp = now;
799 static u64 perf_event_time(struct perf_event *event)
801 struct perf_event_context *ctx = event->ctx;
803 if (is_cgroup_event(event))
804 return perf_cgroup_event_time(event);
806 return ctx ? ctx->time : 0;
810 * Update the total_time_enabled and total_time_running fields for a event.
811 * The caller of this function needs to hold the ctx->lock.
813 static void update_event_times(struct perf_event *event)
815 struct perf_event_context *ctx = event->ctx;
818 if (event->state < PERF_EVENT_STATE_INACTIVE ||
819 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
822 * in cgroup mode, time_enabled represents
823 * the time the event was enabled AND active
824 * tasks were in the monitored cgroup. This is
825 * independent of the activity of the context as
826 * there may be a mix of cgroup and non-cgroup events.
828 * That is why we treat cgroup events differently
831 if (is_cgroup_event(event))
832 run_end = perf_event_time(event);
833 else if (ctx->is_active)
836 run_end = event->tstamp_stopped;
838 event->total_time_enabled = run_end - event->tstamp_enabled;
840 if (event->state == PERF_EVENT_STATE_INACTIVE)
841 run_end = event->tstamp_stopped;
843 run_end = perf_event_time(event);
845 event->total_time_running = run_end - event->tstamp_running;
850 * Update total_time_enabled and total_time_running for all events in a group.
852 static void update_group_times(struct perf_event *leader)
854 struct perf_event *event;
856 update_event_times(leader);
857 list_for_each_entry(event, &leader->sibling_list, group_entry)
858 update_event_times(event);
861 static struct list_head *
862 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
864 if (event->attr.pinned)
865 return &ctx->pinned_groups;
867 return &ctx->flexible_groups;
871 * Add a event from the lists for its context.
872 * Must be called with ctx->mutex and ctx->lock held.
875 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
877 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
878 event->attach_state |= PERF_ATTACH_CONTEXT;
881 * If we're a stand alone event or group leader, we go to the context
882 * list, group events are kept attached to the group so that
883 * perf_group_detach can, at all times, locate all siblings.
885 if (event->group_leader == event) {
886 struct list_head *list;
888 if (is_software_event(event))
889 event->group_flags |= PERF_GROUP_SOFTWARE;
891 list = ctx_group_list(event, ctx);
892 list_add_tail(&event->group_entry, list);
895 if (is_cgroup_event(event))
898 list_add_rcu(&event->event_entry, &ctx->event_list);
900 perf_pmu_rotate_start(ctx->pmu);
902 if (event->attr.inherit_stat)
907 * Initialize event state based on the perf_event_attr::disabled.
909 static inline void perf_event__state_init(struct perf_event *event)
911 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
912 PERF_EVENT_STATE_INACTIVE;
916 * Called at perf_event creation and when events are attached/detached from a
919 static void perf_event__read_size(struct perf_event *event)
921 int entry = sizeof(u64); /* value */
925 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
928 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
931 if (event->attr.read_format & PERF_FORMAT_ID)
932 entry += sizeof(u64);
934 if (event->attr.read_format & PERF_FORMAT_GROUP) {
935 nr += event->group_leader->nr_siblings;
940 event->read_size = size;
943 static void perf_event__header_size(struct perf_event *event)
945 struct perf_sample_data *data;
946 u64 sample_type = event->attr.sample_type;
949 perf_event__read_size(event);
951 if (sample_type & PERF_SAMPLE_IP)
952 size += sizeof(data->ip);
954 if (sample_type & PERF_SAMPLE_ADDR)
955 size += sizeof(data->addr);
957 if (sample_type & PERF_SAMPLE_PERIOD)
958 size += sizeof(data->period);
960 if (sample_type & PERF_SAMPLE_READ)
961 size += event->read_size;
963 event->header_size = size;
966 static void perf_event__id_header_size(struct perf_event *event)
968 struct perf_sample_data *data;
969 u64 sample_type = event->attr.sample_type;
972 if (sample_type & PERF_SAMPLE_TID)
973 size += sizeof(data->tid_entry);
975 if (sample_type & PERF_SAMPLE_TIME)
976 size += sizeof(data->time);
978 if (sample_type & PERF_SAMPLE_ID)
979 size += sizeof(data->id);
981 if (sample_type & PERF_SAMPLE_STREAM_ID)
982 size += sizeof(data->stream_id);
984 if (sample_type & PERF_SAMPLE_CPU)
985 size += sizeof(data->cpu_entry);
987 event->id_header_size = size;
990 static void perf_group_attach(struct perf_event *event)
992 struct perf_event *group_leader = event->group_leader, *pos;
995 * We can have double attach due to group movement in perf_event_open.
997 if (event->attach_state & PERF_ATTACH_GROUP)
1000 event->attach_state |= PERF_ATTACH_GROUP;
1002 if (group_leader == event)
1005 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1006 !is_software_event(event))
1007 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1009 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1010 group_leader->nr_siblings++;
1012 perf_event__header_size(group_leader);
1014 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1015 perf_event__header_size(pos);
1019 * Remove a event from the lists for its context.
1020 * Must be called with ctx->mutex and ctx->lock held.
1023 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1025 struct perf_cpu_context *cpuctx;
1027 * We can have double detach due to exit/hot-unplug + close.
1029 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
1032 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1034 if (is_cgroup_event(event)) {
1036 cpuctx = __get_cpu_context(ctx);
1038 * if there are no more cgroup events
1039 * then cler cgrp to avoid stale pointer
1040 * in update_cgrp_time_from_cpuctx()
1042 if (!ctx->nr_cgroups)
1043 cpuctx->cgrp = NULL;
1047 if (event->attr.inherit_stat)
1050 list_del_rcu(&event->event_entry);
1052 if (event->group_leader == event)
1053 list_del_init(&event->group_entry);
1055 update_group_times(event);
1058 * If event was in error state, then keep it
1059 * that way, otherwise bogus counts will be
1060 * returned on read(). The only way to get out
1061 * of error state is by explicit re-enabling
1064 if (event->state > PERF_EVENT_STATE_OFF)
1065 event->state = PERF_EVENT_STATE_OFF;
1068 static void perf_group_detach(struct perf_event *event)
1070 struct perf_event *sibling, *tmp;
1071 struct list_head *list = NULL;
1074 * We can have double detach due to exit/hot-unplug + close.
1076 if (!(event->attach_state & PERF_ATTACH_GROUP))
1079 event->attach_state &= ~PERF_ATTACH_GROUP;
1082 * If this is a sibling, remove it from its group.
1084 if (event->group_leader != event) {
1085 list_del_init(&event->group_entry);
1086 event->group_leader->nr_siblings--;
1090 if (!list_empty(&event->group_entry))
1091 list = &event->group_entry;
1094 * If this was a group event with sibling events then
1095 * upgrade the siblings to singleton events by adding them
1096 * to whatever list we are on.
1098 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
1100 list_move_tail(&sibling->group_entry, list);
1101 sibling->group_leader = sibling;
1103 /* Inherit group flags from the previous leader */
1104 sibling->group_flags = event->group_flags;
1108 perf_event__header_size(event->group_leader);
1110 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1111 perf_event__header_size(tmp);
1115 event_filter_match(struct perf_event *event)
1117 return (event->cpu == -1 || event->cpu == smp_processor_id())
1118 && perf_cgroup_match(event);
1122 event_sched_out(struct perf_event *event,
1123 struct perf_cpu_context *cpuctx,
1124 struct perf_event_context *ctx)
1126 u64 tstamp = perf_event_time(event);
1129 * An event which could not be activated because of
1130 * filter mismatch still needs to have its timings
1131 * maintained, otherwise bogus information is return
1132 * via read() for time_enabled, time_running:
1134 if (event->state == PERF_EVENT_STATE_INACTIVE
1135 && !event_filter_match(event)) {
1136 delta = tstamp - event->tstamp_stopped;
1137 event->tstamp_running += delta;
1138 event->tstamp_stopped = tstamp;
1141 if (event->state != PERF_EVENT_STATE_ACTIVE)
1144 event->state = PERF_EVENT_STATE_INACTIVE;
1145 if (event->pending_disable) {
1146 event->pending_disable = 0;
1147 event->state = PERF_EVENT_STATE_OFF;
1149 event->tstamp_stopped = tstamp;
1150 event->pmu->del(event, 0);
1153 if (!is_software_event(event))
1154 cpuctx->active_oncpu--;
1156 if (event->attr.exclusive || !cpuctx->active_oncpu)
1157 cpuctx->exclusive = 0;
1161 group_sched_out(struct perf_event *group_event,
1162 struct perf_cpu_context *cpuctx,
1163 struct perf_event_context *ctx)
1165 struct perf_event *event;
1166 int state = group_event->state;
1168 event_sched_out(group_event, cpuctx, ctx);
1171 * Schedule out siblings (if any):
1173 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1174 event_sched_out(event, cpuctx, ctx);
1176 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
1177 cpuctx->exclusive = 0;
1181 * Cross CPU call to remove a performance event
1183 * We disable the event on the hardware level first. After that we
1184 * remove it from the context list.
1186 static int __perf_remove_from_context(void *info)
1188 struct perf_event *event = info;
1189 struct perf_event_context *ctx = event->ctx;
1190 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1192 raw_spin_lock(&ctx->lock);
1193 event_sched_out(event, cpuctx, ctx);
1194 list_del_event(event, ctx);
1195 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1197 cpuctx->task_ctx = NULL;
1199 raw_spin_unlock(&ctx->lock);
1206 * Remove the event from a task's (or a CPU's) list of events.
1208 * CPU events are removed with a smp call. For task events we only
1209 * call when the task is on a CPU.
1211 * If event->ctx is a cloned context, callers must make sure that
1212 * every task struct that event->ctx->task could possibly point to
1213 * remains valid. This is OK when called from perf_release since
1214 * that only calls us on the top-level context, which can't be a clone.
1215 * When called from perf_event_exit_task, it's OK because the
1216 * context has been detached from its task.
1218 static void perf_remove_from_context(struct perf_event *event)
1220 struct perf_event_context *ctx = event->ctx;
1221 struct task_struct *task = ctx->task;
1223 lockdep_assert_held(&ctx->mutex);
1227 * Per cpu events are removed via an smp call and
1228 * the removal is always successful.
1230 cpu_function_call(event->cpu, __perf_remove_from_context, event);
1235 if (!task_function_call(task, __perf_remove_from_context, event))
1238 raw_spin_lock_irq(&ctx->lock);
1240 * If we failed to find a running task, but find the context active now
1241 * that we've acquired the ctx->lock, retry.
1243 if (ctx->is_active) {
1244 raw_spin_unlock_irq(&ctx->lock);
1249 * Since the task isn't running, its safe to remove the event, us
1250 * holding the ctx->lock ensures the task won't get scheduled in.
1252 list_del_event(event, ctx);
1253 raw_spin_unlock_irq(&ctx->lock);
1257 * Cross CPU call to disable a performance event
1259 static int __perf_event_disable(void *info)
1261 struct perf_event *event = info;
1262 struct perf_event_context *ctx = event->ctx;
1263 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1266 * If this is a per-task event, need to check whether this
1267 * event's task is the current task on this cpu.
1269 * Can trigger due to concurrent perf_event_context_sched_out()
1270 * flipping contexts around.
1272 if (ctx->task && cpuctx->task_ctx != ctx)
1275 raw_spin_lock(&ctx->lock);
1278 * If the event is on, turn it off.
1279 * If it is in error state, leave it in error state.
1281 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1282 update_context_time(ctx);
1283 update_cgrp_time_from_event(event);
1284 update_group_times(event);
1285 if (event == event->group_leader)
1286 group_sched_out(event, cpuctx, ctx);
1288 event_sched_out(event, cpuctx, ctx);
1289 event->state = PERF_EVENT_STATE_OFF;
1292 raw_spin_unlock(&ctx->lock);
1300 * If event->ctx is a cloned context, callers must make sure that
1301 * every task struct that event->ctx->task could possibly point to
1302 * remains valid. This condition is satisifed when called through
1303 * perf_event_for_each_child or perf_event_for_each because they
1304 * hold the top-level event's child_mutex, so any descendant that
1305 * goes to exit will block in sync_child_event.
1306 * When called from perf_pending_event it's OK because event->ctx
1307 * is the current context on this CPU and preemption is disabled,
1308 * hence we can't get into perf_event_task_sched_out for this context.
1310 void perf_event_disable(struct perf_event *event)
1312 struct perf_event_context *ctx = event->ctx;
1313 struct task_struct *task = ctx->task;
1317 * Disable the event on the cpu that it's on
1319 cpu_function_call(event->cpu, __perf_event_disable, event);
1324 if (!task_function_call(task, __perf_event_disable, event))
1327 raw_spin_lock_irq(&ctx->lock);
1329 * If the event is still active, we need to retry the cross-call.
1331 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1332 raw_spin_unlock_irq(&ctx->lock);
1334 * Reload the task pointer, it might have been changed by
1335 * a concurrent perf_event_context_sched_out().
1342 * Since we have the lock this context can't be scheduled
1343 * in, so we can change the state safely.
1345 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1346 update_group_times(event);
1347 event->state = PERF_EVENT_STATE_OFF;
1349 raw_spin_unlock_irq(&ctx->lock);
1352 static void perf_set_shadow_time(struct perf_event *event,
1353 struct perf_event_context *ctx,
1357 * use the correct time source for the time snapshot
1359 * We could get by without this by leveraging the
1360 * fact that to get to this function, the caller
1361 * has most likely already called update_context_time()
1362 * and update_cgrp_time_xx() and thus both timestamp
1363 * are identical (or very close). Given that tstamp is,
1364 * already adjusted for cgroup, we could say that:
1365 * tstamp - ctx->timestamp
1367 * tstamp - cgrp->timestamp.
1369 * Then, in perf_output_read(), the calculation would
1370 * work with no changes because:
1371 * - event is guaranteed scheduled in
1372 * - no scheduled out in between
1373 * - thus the timestamp would be the same
1375 * But this is a bit hairy.
1377 * So instead, we have an explicit cgroup call to remain
1378 * within the time time source all along. We believe it
1379 * is cleaner and simpler to understand.
1381 if (is_cgroup_event(event))
1382 perf_cgroup_set_shadow_time(event, tstamp);
1384 event->shadow_ctx_time = tstamp - ctx->timestamp;
1387 #define MAX_INTERRUPTS (~0ULL)
1389 static void perf_log_throttle(struct perf_event *event, int enable);
1392 event_sched_in(struct perf_event *event,
1393 struct perf_cpu_context *cpuctx,
1394 struct perf_event_context *ctx)
1396 u64 tstamp = perf_event_time(event);
1398 if (event->state <= PERF_EVENT_STATE_OFF)
1401 event->state = PERF_EVENT_STATE_ACTIVE;
1402 event->oncpu = smp_processor_id();
1405 * Unthrottle events, since we scheduled we might have missed several
1406 * ticks already, also for a heavily scheduling task there is little
1407 * guarantee it'll get a tick in a timely manner.
1409 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1410 perf_log_throttle(event, 1);
1411 event->hw.interrupts = 0;
1415 * The new state must be visible before we turn it on in the hardware:
1419 if (event->pmu->add(event, PERF_EF_START)) {
1420 event->state = PERF_EVENT_STATE_INACTIVE;
1425 event->tstamp_running += tstamp - event->tstamp_stopped;
1427 perf_set_shadow_time(event, ctx, tstamp);
1429 if (!is_software_event(event))
1430 cpuctx->active_oncpu++;
1433 if (event->attr.exclusive)
1434 cpuctx->exclusive = 1;
1440 group_sched_in(struct perf_event *group_event,
1441 struct perf_cpu_context *cpuctx,
1442 struct perf_event_context *ctx)
1444 struct perf_event *event, *partial_group = NULL;
1445 struct pmu *pmu = group_event->pmu;
1446 u64 now = ctx->time;
1447 bool simulate = false;
1449 if (group_event->state == PERF_EVENT_STATE_OFF)
1452 pmu->start_txn(pmu);
1454 if (event_sched_in(group_event, cpuctx, ctx)) {
1455 pmu->cancel_txn(pmu);
1460 * Schedule in siblings as one group (if any):
1462 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1463 if (event_sched_in(event, cpuctx, ctx)) {
1464 partial_group = event;
1469 if (!pmu->commit_txn(pmu))
1474 * Groups can be scheduled in as one unit only, so undo any
1475 * partial group before returning:
1476 * The events up to the failed event are scheduled out normally,
1477 * tstamp_stopped will be updated.
1479 * The failed events and the remaining siblings need to have
1480 * their timings updated as if they had gone thru event_sched_in()
1481 * and event_sched_out(). This is required to get consistent timings
1482 * across the group. This also takes care of the case where the group
1483 * could never be scheduled by ensuring tstamp_stopped is set to mark
1484 * the time the event was actually stopped, such that time delta
1485 * calculation in update_event_times() is correct.
1487 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1488 if (event == partial_group)
1492 event->tstamp_running += now - event->tstamp_stopped;
1493 event->tstamp_stopped = now;
1495 event_sched_out(event, cpuctx, ctx);
1498 event_sched_out(group_event, cpuctx, ctx);
1500 pmu->cancel_txn(pmu);
1506 * Work out whether we can put this event group on the CPU now.
1508 static int group_can_go_on(struct perf_event *event,
1509 struct perf_cpu_context *cpuctx,
1513 * Groups consisting entirely of software events can always go on.
1515 if (event->group_flags & PERF_GROUP_SOFTWARE)
1518 * If an exclusive group is already on, no other hardware
1521 if (cpuctx->exclusive)
1524 * If this group is exclusive and there are already
1525 * events on the CPU, it can't go on.
1527 if (event->attr.exclusive && cpuctx->active_oncpu)
1530 * Otherwise, try to add it if all previous groups were able
1536 static void add_event_to_ctx(struct perf_event *event,
1537 struct perf_event_context *ctx)
1539 u64 tstamp = perf_event_time(event);
1541 list_add_event(event, ctx);
1542 perf_group_attach(event);
1543 event->tstamp_enabled = tstamp;
1544 event->tstamp_running = tstamp;
1545 event->tstamp_stopped = tstamp;
1548 static void task_ctx_sched_out(struct perf_event_context *ctx);
1550 ctx_sched_in(struct perf_event_context *ctx,
1551 struct perf_cpu_context *cpuctx,
1552 enum event_type_t event_type,
1553 struct task_struct *task);
1555 static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1556 struct perf_event_context *ctx,
1557 struct task_struct *task)
1559 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1561 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1562 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1564 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1568 * Cross CPU call to install and enable a performance event
1570 * Must be called with ctx->mutex held
1572 static int __perf_install_in_context(void *info)
1574 struct perf_event *event = info;
1575 struct perf_event_context *ctx = event->ctx;
1576 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1577 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1578 struct task_struct *task = current;
1580 perf_ctx_lock(cpuctx, task_ctx);
1581 perf_pmu_disable(cpuctx->ctx.pmu);
1584 * If there was an active task_ctx schedule it out.
1587 task_ctx_sched_out(task_ctx);
1590 * If the context we're installing events in is not the
1591 * active task_ctx, flip them.
1593 if (ctx->task && task_ctx != ctx) {
1595 raw_spin_unlock(&task_ctx->lock);
1596 raw_spin_lock(&ctx->lock);
1601 cpuctx->task_ctx = task_ctx;
1602 task = task_ctx->task;
1605 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
1607 update_context_time(ctx);
1609 * update cgrp time only if current cgrp
1610 * matches event->cgrp. Must be done before
1611 * calling add_event_to_ctx()
1613 update_cgrp_time_from_event(event);
1615 add_event_to_ctx(event, ctx);
1618 * Schedule everything back in
1620 perf_event_sched_in(cpuctx, task_ctx, task);
1622 perf_pmu_enable(cpuctx->ctx.pmu);
1623 perf_ctx_unlock(cpuctx, task_ctx);
1629 * Attach a performance event to a context
1631 * First we add the event to the list with the hardware enable bit
1632 * in event->hw_config cleared.
1634 * If the event is attached to a task which is on a CPU we use a smp
1635 * call to enable it in the task context. The task might have been
1636 * scheduled away, but we check this in the smp call again.
1639 perf_install_in_context(struct perf_event_context *ctx,
1640 struct perf_event *event,
1643 struct task_struct *task = ctx->task;
1645 lockdep_assert_held(&ctx->mutex);
1651 * Per cpu events are installed via an smp call and
1652 * the install is always successful.
1654 cpu_function_call(cpu, __perf_install_in_context, event);
1659 if (!task_function_call(task, __perf_install_in_context, event))
1662 raw_spin_lock_irq(&ctx->lock);
1664 * If we failed to find a running task, but find the context active now
1665 * that we've acquired the ctx->lock, retry.
1667 if (ctx->is_active) {
1668 raw_spin_unlock_irq(&ctx->lock);
1673 * Since the task isn't running, its safe to add the event, us holding
1674 * the ctx->lock ensures the task won't get scheduled in.
1676 add_event_to_ctx(event, ctx);
1677 raw_spin_unlock_irq(&ctx->lock);
1681 * Put a event into inactive state and update time fields.
1682 * Enabling the leader of a group effectively enables all
1683 * the group members that aren't explicitly disabled, so we
1684 * have to update their ->tstamp_enabled also.
1685 * Note: this works for group members as well as group leaders
1686 * since the non-leader members' sibling_lists will be empty.
1688 static void __perf_event_mark_enabled(struct perf_event *event,
1689 struct perf_event_context *ctx)
1691 struct perf_event *sub;
1692 u64 tstamp = perf_event_time(event);
1694 event->state = PERF_EVENT_STATE_INACTIVE;
1695 event->tstamp_enabled = tstamp - event->total_time_enabled;
1696 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1697 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1698 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1703 * Cross CPU call to enable a performance event
1705 static int __perf_event_enable(void *info)
1707 struct perf_event *event = info;
1708 struct perf_event_context *ctx = event->ctx;
1709 struct perf_event *leader = event->group_leader;
1710 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1714 * There's a time window between 'ctx->is_active' check
1715 * in perf_event_enable function and this place having:
1717 * - ctx->lock unlocked
1719 * where the task could be killed and 'ctx' deactivated
1720 * by perf_event_exit_task.
1722 if (!ctx->is_active)
1725 raw_spin_lock(&ctx->lock);
1726 update_context_time(ctx);
1728 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1732 * set current task's cgroup time reference point
1734 perf_cgroup_set_timestamp(current, ctx);
1736 __perf_event_mark_enabled(event, ctx);
1738 if (!event_filter_match(event)) {
1739 if (is_cgroup_event(event))
1740 perf_cgroup_defer_enabled(event);
1745 * If the event is in a group and isn't the group leader,
1746 * then don't put it on unless the group is on.
1748 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1751 if (!group_can_go_on(event, cpuctx, 1)) {
1754 if (event == leader)
1755 err = group_sched_in(event, cpuctx, ctx);
1757 err = event_sched_in(event, cpuctx, ctx);
1762 * If this event can't go on and it's part of a
1763 * group, then the whole group has to come off.
1765 if (leader != event)
1766 group_sched_out(leader, cpuctx, ctx);
1767 if (leader->attr.pinned) {
1768 update_group_times(leader);
1769 leader->state = PERF_EVENT_STATE_ERROR;
1774 raw_spin_unlock(&ctx->lock);
1782 * If event->ctx is a cloned context, callers must make sure that
1783 * every task struct that event->ctx->task could possibly point to
1784 * remains valid. This condition is satisfied when called through
1785 * perf_event_for_each_child or perf_event_for_each as described
1786 * for perf_event_disable.
1788 void perf_event_enable(struct perf_event *event)
1790 struct perf_event_context *ctx = event->ctx;
1791 struct task_struct *task = ctx->task;
1795 * Enable the event on the cpu that it's on
1797 cpu_function_call(event->cpu, __perf_event_enable, event);
1801 raw_spin_lock_irq(&ctx->lock);
1802 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1806 * If the event is in error state, clear that first.
1807 * That way, if we see the event in error state below, we
1808 * know that it has gone back into error state, as distinct
1809 * from the task having been scheduled away before the
1810 * cross-call arrived.
1812 if (event->state == PERF_EVENT_STATE_ERROR)
1813 event->state = PERF_EVENT_STATE_OFF;
1816 if (!ctx->is_active) {
1817 __perf_event_mark_enabled(event, ctx);
1821 raw_spin_unlock_irq(&ctx->lock);
1823 if (!task_function_call(task, __perf_event_enable, event))
1826 raw_spin_lock_irq(&ctx->lock);
1829 * If the context is active and the event is still off,
1830 * we need to retry the cross-call.
1832 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1834 * task could have been flipped by a concurrent
1835 * perf_event_context_sched_out()
1842 raw_spin_unlock_irq(&ctx->lock);
1845 int perf_event_refresh(struct perf_event *event, int refresh)
1848 * not supported on inherited events
1850 if (event->attr.inherit || !is_sampling_event(event))
1853 atomic_add(refresh, &event->event_limit);
1854 perf_event_enable(event);
1858 EXPORT_SYMBOL_GPL(perf_event_refresh);
1860 static void ctx_sched_out(struct perf_event_context *ctx,
1861 struct perf_cpu_context *cpuctx,
1862 enum event_type_t event_type)
1864 struct perf_event *event;
1865 int is_active = ctx->is_active;
1867 ctx->is_active &= ~event_type;
1868 if (likely(!ctx->nr_events))
1871 update_context_time(ctx);
1872 update_cgrp_time_from_cpuctx(cpuctx);
1873 if (!ctx->nr_active)
1876 perf_pmu_disable(ctx->pmu);
1877 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
1878 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1879 group_sched_out(event, cpuctx, ctx);
1882 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
1883 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1884 group_sched_out(event, cpuctx, ctx);
1886 perf_pmu_enable(ctx->pmu);
1890 * Test whether two contexts are equivalent, i.e. whether they
1891 * have both been cloned from the same version of the same context
1892 * and they both have the same number of enabled events.
1893 * If the number of enabled events is the same, then the set
1894 * of enabled events should be the same, because these are both
1895 * inherited contexts, therefore we can't access individual events
1896 * in them directly with an fd; we can only enable/disable all
1897 * events via prctl, or enable/disable all events in a family
1898 * via ioctl, which will have the same effect on both contexts.
1900 static int context_equiv(struct perf_event_context *ctx1,
1901 struct perf_event_context *ctx2)
1903 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1904 && ctx1->parent_gen == ctx2->parent_gen
1905 && !ctx1->pin_count && !ctx2->pin_count;
1908 static void __perf_event_sync_stat(struct perf_event *event,
1909 struct perf_event *next_event)
1913 if (!event->attr.inherit_stat)
1917 * Update the event value, we cannot use perf_event_read()
1918 * because we're in the middle of a context switch and have IRQs
1919 * disabled, which upsets smp_call_function_single(), however
1920 * we know the event must be on the current CPU, therefore we
1921 * don't need to use it.
1923 switch (event->state) {
1924 case PERF_EVENT_STATE_ACTIVE:
1925 event->pmu->read(event);
1928 case PERF_EVENT_STATE_INACTIVE:
1929 update_event_times(event);
1937 * In order to keep per-task stats reliable we need to flip the event
1938 * values when we flip the contexts.
1940 value = local64_read(&next_event->count);
1941 value = local64_xchg(&event->count, value);
1942 local64_set(&next_event->count, value);
1944 swap(event->total_time_enabled, next_event->total_time_enabled);
1945 swap(event->total_time_running, next_event->total_time_running);
1948 * Since we swizzled the values, update the user visible data too.
1950 perf_event_update_userpage(event);
1951 perf_event_update_userpage(next_event);
1954 #define list_next_entry(pos, member) \
1955 list_entry(pos->member.next, typeof(*pos), member)
1957 static void perf_event_sync_stat(struct perf_event_context *ctx,
1958 struct perf_event_context *next_ctx)
1960 struct perf_event *event, *next_event;
1965 update_context_time(ctx);
1967 event = list_first_entry(&ctx->event_list,
1968 struct perf_event, event_entry);
1970 next_event = list_first_entry(&next_ctx->event_list,
1971 struct perf_event, event_entry);
1973 while (&event->event_entry != &ctx->event_list &&
1974 &next_event->event_entry != &next_ctx->event_list) {
1976 __perf_event_sync_stat(event, next_event);
1978 event = list_next_entry(event, event_entry);
1979 next_event = list_next_entry(next_event, event_entry);
1983 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1984 struct task_struct *next)
1986 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1987 struct perf_event_context *next_ctx;
1988 struct perf_event_context *parent;
1989 struct perf_cpu_context *cpuctx;
1995 cpuctx = __get_cpu_context(ctx);
1996 if (!cpuctx->task_ctx)
2000 parent = rcu_dereference(ctx->parent_ctx);
2001 next_ctx = next->perf_event_ctxp[ctxn];
2002 if (parent && next_ctx &&
2003 rcu_dereference(next_ctx->parent_ctx) == parent) {
2005 * Looks like the two contexts are clones, so we might be
2006 * able to optimize the context switch. We lock both
2007 * contexts and check that they are clones under the
2008 * lock (including re-checking that neither has been
2009 * uncloned in the meantime). It doesn't matter which
2010 * order we take the locks because no other cpu could
2011 * be trying to lock both of these tasks.
2013 raw_spin_lock(&ctx->lock);
2014 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
2015 if (context_equiv(ctx, next_ctx)) {
2017 * XXX do we need a memory barrier of sorts
2018 * wrt to rcu_dereference() of perf_event_ctxp
2020 task->perf_event_ctxp[ctxn] = next_ctx;
2021 next->perf_event_ctxp[ctxn] = ctx;
2023 next_ctx->task = task;
2026 perf_event_sync_stat(ctx, next_ctx);
2028 raw_spin_unlock(&next_ctx->lock);
2029 raw_spin_unlock(&ctx->lock);
2034 raw_spin_lock(&ctx->lock);
2035 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2036 cpuctx->task_ctx = NULL;
2037 raw_spin_unlock(&ctx->lock);
2041 #define for_each_task_context_nr(ctxn) \
2042 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2045 * Called from scheduler to remove the events of the current task,
2046 * with interrupts disabled.
2048 * We stop each event and update the event value in event->count.
2050 * This does not protect us against NMI, but disable()
2051 * sets the disabled bit in the control field of event _before_
2052 * accessing the event control register. If a NMI hits, then it will
2053 * not restart the event.
2055 void __perf_event_task_sched_out(struct task_struct *task,
2056 struct task_struct *next)
2060 for_each_task_context_nr(ctxn)
2061 perf_event_context_sched_out(task, ctxn, next);
2064 * if cgroup events exist on this CPU, then we need
2065 * to check if we have to switch out PMU state.
2066 * cgroup event are system-wide mode only
2068 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2069 perf_cgroup_sched_out(task, next);
2072 static void task_ctx_sched_out(struct perf_event_context *ctx)
2074 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2076 if (!cpuctx->task_ctx)
2079 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2082 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2083 cpuctx->task_ctx = NULL;
2087 * Called with IRQs disabled
2089 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2090 enum event_type_t event_type)
2092 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
2096 ctx_pinned_sched_in(struct perf_event_context *ctx,
2097 struct perf_cpu_context *cpuctx)
2099 struct perf_event *event;
2101 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2102 if (event->state <= PERF_EVENT_STATE_OFF)
2104 if (!event_filter_match(event))
2107 /* may need to reset tstamp_enabled */
2108 if (is_cgroup_event(event))
2109 perf_cgroup_mark_enabled(event, ctx);
2111 if (group_can_go_on(event, cpuctx, 1))
2112 group_sched_in(event, cpuctx, ctx);
2115 * If this pinned group hasn't been scheduled,
2116 * put it in error state.
2118 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2119 update_group_times(event);
2120 event->state = PERF_EVENT_STATE_ERROR;
2126 ctx_flexible_sched_in(struct perf_event_context *ctx,
2127 struct perf_cpu_context *cpuctx)
2129 struct perf_event *event;
2132 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2133 /* Ignore events in OFF or ERROR state */
2134 if (event->state <= PERF_EVENT_STATE_OFF)
2137 * Listen to the 'cpu' scheduling filter constraint
2140 if (!event_filter_match(event))
2143 /* may need to reset tstamp_enabled */
2144 if (is_cgroup_event(event))
2145 perf_cgroup_mark_enabled(event, ctx);
2147 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2148 if (group_sched_in(event, cpuctx, ctx))
2155 ctx_sched_in(struct perf_event_context *ctx,
2156 struct perf_cpu_context *cpuctx,
2157 enum event_type_t event_type,
2158 struct task_struct *task)
2161 int is_active = ctx->is_active;
2163 ctx->is_active |= event_type;
2164 if (likely(!ctx->nr_events))
2168 ctx->timestamp = now;
2169 perf_cgroup_set_timestamp(task, ctx);
2171 * First go through the list and put on any pinned groups
2172 * in order to give them the best chance of going on.
2174 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
2175 ctx_pinned_sched_in(ctx, cpuctx);
2177 /* Then walk through the lower prio flexible groups */
2178 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
2179 ctx_flexible_sched_in(ctx, cpuctx);
2182 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2183 enum event_type_t event_type,
2184 struct task_struct *task)
2186 struct perf_event_context *ctx = &cpuctx->ctx;
2188 ctx_sched_in(ctx, cpuctx, event_type, task);
2191 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2192 struct task_struct *task)
2194 struct perf_cpu_context *cpuctx;
2196 cpuctx = __get_cpu_context(ctx);
2197 if (cpuctx->task_ctx == ctx)
2200 perf_ctx_lock(cpuctx, ctx);
2201 perf_pmu_disable(ctx->pmu);
2203 * We want to keep the following priority order:
2204 * cpu pinned (that don't need to move), task pinned,
2205 * cpu flexible, task flexible.
2207 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2210 cpuctx->task_ctx = ctx;
2212 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2214 perf_pmu_enable(ctx->pmu);
2215 perf_ctx_unlock(cpuctx, ctx);
2218 * Since these rotations are per-cpu, we need to ensure the
2219 * cpu-context we got scheduled on is actually rotating.
2221 perf_pmu_rotate_start(ctx->pmu);
2225 * Called from scheduler to add the events of the current task
2226 * with interrupts disabled.
2228 * We restore the event value and then enable it.
2230 * This does not protect us against NMI, but enable()
2231 * sets the enabled bit in the control field of event _before_
2232 * accessing the event control register. If a NMI hits, then it will
2233 * keep the event running.
2235 void __perf_event_task_sched_in(struct task_struct *prev,
2236 struct task_struct *task)
2238 struct perf_event_context *ctx;
2241 for_each_task_context_nr(ctxn) {
2242 ctx = task->perf_event_ctxp[ctxn];
2246 perf_event_context_sched_in(ctx, task);
2249 * if cgroup events exist on this CPU, then we need
2250 * to check if we have to switch in PMU state.
2251 * cgroup event are system-wide mode only
2253 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2254 perf_cgroup_sched_in(prev, task);
2257 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2259 u64 frequency = event->attr.sample_freq;
2260 u64 sec = NSEC_PER_SEC;
2261 u64 divisor, dividend;
2263 int count_fls, nsec_fls, frequency_fls, sec_fls;
2265 count_fls = fls64(count);
2266 nsec_fls = fls64(nsec);
2267 frequency_fls = fls64(frequency);
2271 * We got @count in @nsec, with a target of sample_freq HZ
2272 * the target period becomes:
2275 * period = -------------------
2276 * @nsec * sample_freq
2281 * Reduce accuracy by one bit such that @a and @b converge
2282 * to a similar magnitude.
2284 #define REDUCE_FLS(a, b) \
2286 if (a##_fls > b##_fls) { \
2296 * Reduce accuracy until either term fits in a u64, then proceed with
2297 * the other, so that finally we can do a u64/u64 division.
2299 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2300 REDUCE_FLS(nsec, frequency);
2301 REDUCE_FLS(sec, count);
2304 if (count_fls + sec_fls > 64) {
2305 divisor = nsec * frequency;
2307 while (count_fls + sec_fls > 64) {
2308 REDUCE_FLS(count, sec);
2312 dividend = count * sec;
2314 dividend = count * sec;
2316 while (nsec_fls + frequency_fls > 64) {
2317 REDUCE_FLS(nsec, frequency);
2321 divisor = nsec * frequency;
2327 return div64_u64(dividend, divisor);
2330 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
2332 struct hw_perf_event *hwc = &event->hw;
2333 s64 period, sample_period;
2336 period = perf_calculate_period(event, nsec, count);
2338 delta = (s64)(period - hwc->sample_period);
2339 delta = (delta + 7) / 8; /* low pass filter */
2341 sample_period = hwc->sample_period + delta;
2346 hwc->sample_period = sample_period;
2348 if (local64_read(&hwc->period_left) > 8*sample_period) {
2349 event->pmu->stop(event, PERF_EF_UPDATE);
2350 local64_set(&hwc->period_left, 0);
2351 event->pmu->start(event, PERF_EF_RELOAD);
2355 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
2357 struct perf_event *event;
2358 struct hw_perf_event *hwc;
2359 u64 interrupts, now;
2362 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2363 if (event->state != PERF_EVENT_STATE_ACTIVE)
2366 if (!event_filter_match(event))
2371 interrupts = hwc->interrupts;
2372 hwc->interrupts = 0;
2375 * unthrottle events on the tick
2377 if (interrupts == MAX_INTERRUPTS) {
2378 perf_log_throttle(event, 1);
2379 event->pmu->start(event, 0);
2382 if (!event->attr.freq || !event->attr.sample_freq)
2385 event->pmu->read(event);
2386 now = local64_read(&event->count);
2387 delta = now - hwc->freq_count_stamp;
2388 hwc->freq_count_stamp = now;
2391 perf_adjust_period(event, period, delta);
2396 * Round-robin a context's events:
2398 static void rotate_ctx(struct perf_event_context *ctx)
2401 * Rotate the first entry last of non-pinned groups. Rotation might be
2402 * disabled by the inheritance code.
2404 if (!ctx->rotate_disable)
2405 list_rotate_left(&ctx->flexible_groups);
2409 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2410 * because they're strictly cpu affine and rotate_start is called with IRQs
2411 * disabled, while rotate_context is called from IRQ context.
2413 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
2415 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
2416 struct perf_event_context *ctx = NULL;
2417 int rotate = 0, remove = 1;
2419 if (cpuctx->ctx.nr_events) {
2421 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2425 ctx = cpuctx->task_ctx;
2426 if (ctx && ctx->nr_events) {
2428 if (ctx->nr_events != ctx->nr_active)
2432 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2433 perf_pmu_disable(cpuctx->ctx.pmu);
2434 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
2436 perf_ctx_adjust_freq(ctx, interval);
2441 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2443 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
2445 rotate_ctx(&cpuctx->ctx);
2449 perf_event_sched_in(cpuctx, ctx, current);
2453 list_del_init(&cpuctx->rotation_list);
2455 perf_pmu_enable(cpuctx->ctx.pmu);
2456 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2459 void perf_event_task_tick(void)
2461 struct list_head *head = &__get_cpu_var(rotation_list);
2462 struct perf_cpu_context *cpuctx, *tmp;
2464 WARN_ON(!irqs_disabled());
2466 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2467 if (cpuctx->jiffies_interval == 1 ||
2468 !(jiffies % cpuctx->jiffies_interval))
2469 perf_rotate_context(cpuctx);
2473 static int event_enable_on_exec(struct perf_event *event,
2474 struct perf_event_context *ctx)
2476 if (!event->attr.enable_on_exec)
2479 event->attr.enable_on_exec = 0;
2480 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2483 __perf_event_mark_enabled(event, ctx);
2489 * Enable all of a task's events that have been marked enable-on-exec.
2490 * This expects task == current.
2492 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2494 struct perf_event *event;
2495 unsigned long flags;
2499 local_irq_save(flags);
2500 if (!ctx || !ctx->nr_events)
2504 * We must ctxsw out cgroup events to avoid conflict
2505 * when invoking perf_task_event_sched_in() later on
2506 * in this function. Otherwise we end up trying to
2507 * ctxswin cgroup events which are already scheduled
2510 perf_cgroup_sched_out(current, NULL);
2512 raw_spin_lock(&ctx->lock);
2513 task_ctx_sched_out(ctx);
2515 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2516 ret = event_enable_on_exec(event, ctx);
2521 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2522 ret = event_enable_on_exec(event, ctx);
2528 * Unclone this context if we enabled any event.
2533 raw_spin_unlock(&ctx->lock);
2536 * Also calls ctxswin for cgroup events, if any:
2538 perf_event_context_sched_in(ctx, ctx->task);
2540 local_irq_restore(flags);
2544 * Cross CPU call to read the hardware event
2546 static void __perf_event_read(void *info)
2548 struct perf_event *event = info;
2549 struct perf_event_context *ctx = event->ctx;
2550 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2553 * If this is a task context, we need to check whether it is
2554 * the current task context of this cpu. If not it has been
2555 * scheduled out before the smp call arrived. In that case
2556 * event->count would have been updated to a recent sample
2557 * when the event was scheduled out.
2559 if (ctx->task && cpuctx->task_ctx != ctx)
2562 raw_spin_lock(&ctx->lock);
2563 if (ctx->is_active) {
2564 update_context_time(ctx);
2565 update_cgrp_time_from_event(event);
2567 update_event_times(event);
2568 if (event->state == PERF_EVENT_STATE_ACTIVE)
2569 event->pmu->read(event);
2570 raw_spin_unlock(&ctx->lock);
2573 static inline u64 perf_event_count(struct perf_event *event)
2575 return local64_read(&event->count) + atomic64_read(&event->child_count);
2578 static u64 perf_event_read(struct perf_event *event)
2581 * If event is enabled and currently active on a CPU, update the
2582 * value in the event structure:
2584 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2585 smp_call_function_single(event->oncpu,
2586 __perf_event_read, event, 1);
2587 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2588 struct perf_event_context *ctx = event->ctx;
2589 unsigned long flags;
2591 raw_spin_lock_irqsave(&ctx->lock, flags);
2593 * may read while context is not active
2594 * (e.g., thread is blocked), in that case
2595 * we cannot update context time
2597 if (ctx->is_active) {
2598 update_context_time(ctx);
2599 update_cgrp_time_from_event(event);
2601 update_event_times(event);
2602 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2605 return perf_event_count(event);
2612 struct callchain_cpus_entries {
2613 struct rcu_head rcu_head;
2614 struct perf_callchain_entry *cpu_entries[0];
2617 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
2618 static atomic_t nr_callchain_events;
2619 static DEFINE_MUTEX(callchain_mutex);
2620 struct callchain_cpus_entries *callchain_cpus_entries;
2623 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
2624 struct pt_regs *regs)
2628 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
2629 struct pt_regs *regs)
2633 static void release_callchain_buffers_rcu(struct rcu_head *head)
2635 struct callchain_cpus_entries *entries;
2638 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
2640 for_each_possible_cpu(cpu)
2641 kfree(entries->cpu_entries[cpu]);
2646 static void release_callchain_buffers(void)
2648 struct callchain_cpus_entries *entries;
2650 entries = callchain_cpus_entries;
2651 rcu_assign_pointer(callchain_cpus_entries, NULL);
2652 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2655 static int alloc_callchain_buffers(void)
2659 struct callchain_cpus_entries *entries;
2662 * We can't use the percpu allocation API for data that can be
2663 * accessed from NMI. Use a temporary manual per cpu allocation
2664 * until that gets sorted out.
2666 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
2668 entries = kzalloc(size, GFP_KERNEL);
2672 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
2674 for_each_possible_cpu(cpu) {
2675 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2677 if (!entries->cpu_entries[cpu])
2681 rcu_assign_pointer(callchain_cpus_entries, entries);
2686 for_each_possible_cpu(cpu)
2687 kfree(entries->cpu_entries[cpu]);
2693 static int get_callchain_buffers(void)
2698 mutex_lock(&callchain_mutex);
2700 count = atomic_inc_return(&nr_callchain_events);
2701 if (WARN_ON_ONCE(count < 1)) {
2707 /* If the allocation failed, give up */
2708 if (!callchain_cpus_entries)
2713 err = alloc_callchain_buffers();
2715 release_callchain_buffers();
2717 mutex_unlock(&callchain_mutex);
2722 static void put_callchain_buffers(void)
2724 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2725 release_callchain_buffers();
2726 mutex_unlock(&callchain_mutex);
2730 static int get_recursion_context(int *recursion)
2738 else if (in_softirq())
2743 if (recursion[rctx])
2752 static inline void put_recursion_context(int *recursion, int rctx)
2758 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2761 struct callchain_cpus_entries *entries;
2763 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2767 entries = rcu_dereference(callchain_cpus_entries);
2771 cpu = smp_processor_id();
2773 return &entries->cpu_entries[cpu][*rctx];
2777 put_callchain_entry(int rctx)
2779 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2782 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2785 struct perf_callchain_entry *entry;
2788 entry = get_callchain_entry(&rctx);
2797 if (!user_mode(regs)) {
2798 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2799 perf_callchain_kernel(entry, regs);
2801 regs = task_pt_regs(current);
2807 perf_callchain_store(entry, PERF_CONTEXT_USER);
2808 perf_callchain_user(entry, regs);
2812 put_callchain_entry(rctx);
2818 * Initialize the perf_event context in a task_struct:
2820 static void __perf_event_init_context(struct perf_event_context *ctx)
2822 raw_spin_lock_init(&ctx->lock);
2823 mutex_init(&ctx->mutex);
2824 INIT_LIST_HEAD(&ctx->pinned_groups);
2825 INIT_LIST_HEAD(&ctx->flexible_groups);
2826 INIT_LIST_HEAD(&ctx->event_list);
2827 atomic_set(&ctx->refcount, 1);
2830 static struct perf_event_context *
2831 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2833 struct perf_event_context *ctx;
2835 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2839 __perf_event_init_context(ctx);
2842 get_task_struct(task);
2849 static struct task_struct *
2850 find_lively_task_by_vpid(pid_t vpid)
2852 struct task_struct *task;
2859 task = find_task_by_vpid(vpid);
2861 get_task_struct(task);
2865 return ERR_PTR(-ESRCH);
2867 /* Reuse ptrace permission checks for now. */
2869 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2874 put_task_struct(task);
2875 return ERR_PTR(err);
2880 * Returns a matching context with refcount and pincount.
2882 static struct perf_event_context *
2883 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2885 struct perf_event_context *ctx;
2886 struct perf_cpu_context *cpuctx;
2887 unsigned long flags;
2891 /* Must be root to operate on a CPU event: */
2892 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2893 return ERR_PTR(-EACCES);
2896 * We could be clever and allow to attach a event to an
2897 * offline CPU and activate it when the CPU comes up, but
2900 if (!cpu_online(cpu))
2901 return ERR_PTR(-ENODEV);
2903 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2912 ctxn = pmu->task_ctx_nr;
2917 ctx = perf_lock_task_context(task, ctxn, &flags);
2921 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2923 ctx = alloc_perf_context(pmu, task);
2929 mutex_lock(&task->perf_event_mutex);
2931 * If it has already passed perf_event_exit_task().
2932 * we must see PF_EXITING, it takes this mutex too.
2934 if (task->flags & PF_EXITING)
2936 else if (task->perf_event_ctxp[ctxn])
2941 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2943 mutex_unlock(&task->perf_event_mutex);
2945 if (unlikely(err)) {
2957 return ERR_PTR(err);
2960 static void perf_event_free_filter(struct perf_event *event);
2962 static void free_event_rcu(struct rcu_head *head)
2964 struct perf_event *event;
2966 event = container_of(head, struct perf_event, rcu_head);
2968 put_pid_ns(event->ns);
2969 perf_event_free_filter(event);
2973 static void ring_buffer_put(struct ring_buffer *rb);
2974 static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb);
2976 static void free_event(struct perf_event *event)
2978 irq_work_sync(&event->pending);
2980 if (!event->parent) {
2981 if (event->attach_state & PERF_ATTACH_TASK)
2982 jump_label_dec(&perf_sched_events);
2983 if (event->attr.mmap || event->attr.mmap_data)
2984 atomic_dec(&nr_mmap_events);
2985 if (event->attr.comm)
2986 atomic_dec(&nr_comm_events);
2987 if (event->attr.task)
2988 atomic_dec(&nr_task_events);
2989 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2990 put_callchain_buffers();
2991 if (is_cgroup_event(event)) {
2992 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
2993 jump_label_dec(&perf_sched_events);
2998 struct ring_buffer *rb;
3001 * Can happen when we close an event with re-directed output.
3003 * Since we have a 0 refcount, perf_mmap_close() will skip
3004 * over us; possibly making our ring_buffer_put() the last.
3006 mutex_lock(&event->mmap_mutex);
3009 rcu_assign_pointer(event->rb, NULL);
3010 ring_buffer_detach(event, rb);
3011 ring_buffer_put(rb); /* could be last */
3013 mutex_unlock(&event->mmap_mutex);
3016 if (is_cgroup_event(event))
3017 perf_detach_cgroup(event);
3020 event->destroy(event);
3023 put_ctx(event->ctx);
3025 call_rcu(&event->rcu_head, free_event_rcu);
3028 int perf_event_release_kernel(struct perf_event *event)
3030 struct perf_event_context *ctx = event->ctx;
3032 WARN_ON_ONCE(ctx->parent_ctx);
3034 * There are two ways this annotation is useful:
3036 * 1) there is a lock recursion from perf_event_exit_task
3037 * see the comment there.
3039 * 2) there is a lock-inversion with mmap_sem through
3040 * perf_event_read_group(), which takes faults while
3041 * holding ctx->mutex, however this is called after
3042 * the last filedesc died, so there is no possibility
3043 * to trigger the AB-BA case.
3045 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
3046 raw_spin_lock_irq(&ctx->lock);
3047 perf_group_detach(event);
3048 raw_spin_unlock_irq(&ctx->lock);
3049 perf_remove_from_context(event);
3050 mutex_unlock(&ctx->mutex);
3056 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3059 * Called when the last reference to the file is gone.
3061 static void put_event(struct perf_event *event)
3063 struct task_struct *owner;
3065 if (!atomic_long_dec_and_test(&event->refcount))
3069 owner = ACCESS_ONCE(event->owner);
3071 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3072 * !owner it means the list deletion is complete and we can indeed
3073 * free this event, otherwise we need to serialize on
3074 * owner->perf_event_mutex.
3076 smp_read_barrier_depends();
3079 * Since delayed_put_task_struct() also drops the last
3080 * task reference we can safely take a new reference
3081 * while holding the rcu_read_lock().
3083 get_task_struct(owner);
3088 mutex_lock(&owner->perf_event_mutex);
3090 * We have to re-check the event->owner field, if it is cleared
3091 * we raced with perf_event_exit_task(), acquiring the mutex
3092 * ensured they're done, and we can proceed with freeing the
3096 list_del_init(&event->owner_entry);
3097 mutex_unlock(&owner->perf_event_mutex);
3098 put_task_struct(owner);
3101 perf_event_release_kernel(event);
3104 static int perf_release(struct inode *inode, struct file *file)
3106 put_event(file->private_data);
3110 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
3112 struct perf_event *child;
3118 mutex_lock(&event->child_mutex);
3119 total += perf_event_read(event);
3120 *enabled += event->total_time_enabled +
3121 atomic64_read(&event->child_total_time_enabled);
3122 *running += event->total_time_running +
3123 atomic64_read(&event->child_total_time_running);
3125 list_for_each_entry(child, &event->child_list, child_list) {
3126 total += perf_event_read(child);
3127 *enabled += child->total_time_enabled;
3128 *running += child->total_time_running;
3130 mutex_unlock(&event->child_mutex);
3134 EXPORT_SYMBOL_GPL(perf_event_read_value);
3136 static int perf_event_read_group(struct perf_event *event,
3137 u64 read_format, char __user *buf)
3139 struct perf_event *leader = event->group_leader, *sub;
3140 int n = 0, size = 0, ret = -EFAULT;
3141 struct perf_event_context *ctx = leader->ctx;
3143 u64 count, enabled, running;
3145 mutex_lock(&ctx->mutex);
3146 count = perf_event_read_value(leader, &enabled, &running);
3148 values[n++] = 1 + leader->nr_siblings;
3149 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3150 values[n++] = enabled;
3151 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3152 values[n++] = running;
3153 values[n++] = count;
3154 if (read_format & PERF_FORMAT_ID)
3155 values[n++] = primary_event_id(leader);
3157 size = n * sizeof(u64);
3159 if (copy_to_user(buf, values, size))
3164 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3167 values[n++] = perf_event_read_value(sub, &enabled, &running);
3168 if (read_format & PERF_FORMAT_ID)
3169 values[n++] = primary_event_id(sub);
3171 size = n * sizeof(u64);
3173 if (copy_to_user(buf + ret, values, size)) {
3181 mutex_unlock(&ctx->mutex);
3186 static int perf_event_read_one(struct perf_event *event,
3187 u64 read_format, char __user *buf)
3189 u64 enabled, running;
3193 values[n++] = perf_event_read_value(event, &enabled, &running);
3194 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3195 values[n++] = enabled;
3196 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3197 values[n++] = running;
3198 if (read_format & PERF_FORMAT_ID)
3199 values[n++] = primary_event_id(event);
3201 if (copy_to_user(buf, values, n * sizeof(u64)))
3204 return n * sizeof(u64);
3208 * Read the performance event - simple non blocking version for now
3211 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3213 u64 read_format = event->attr.read_format;
3217 * Return end-of-file for a read on a event that is in
3218 * error state (i.e. because it was pinned but it couldn't be
3219 * scheduled on to the CPU at some point).
3221 if (event->state == PERF_EVENT_STATE_ERROR)
3224 if (count < event->read_size)
3227 WARN_ON_ONCE(event->ctx->parent_ctx);
3228 if (read_format & PERF_FORMAT_GROUP)
3229 ret = perf_event_read_group(event, read_format, buf);
3231 ret = perf_event_read_one(event, read_format, buf);
3237 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3239 struct perf_event *event = file->private_data;
3241 return perf_read_hw(event, buf, count);
3244 static unsigned int perf_poll(struct file *file, poll_table *wait)
3246 struct perf_event *event = file->private_data;
3247 struct ring_buffer *rb;
3248 unsigned int events = POLL_HUP;
3251 * Pin the event->rb by taking event->mmap_mutex; otherwise
3252 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
3254 mutex_lock(&event->mmap_mutex);
3257 events = atomic_xchg(&rb->poll, 0);
3258 mutex_unlock(&event->mmap_mutex);
3260 poll_wait(file, &event->waitq, wait);
3265 static void perf_event_reset(struct perf_event *event)
3267 (void)perf_event_read(event);
3268 local64_set(&event->count, 0);
3269 perf_event_update_userpage(event);
3273 * Holding the top-level event's child_mutex means that any
3274 * descendant process that has inherited this event will block
3275 * in sync_child_event if it goes to exit, thus satisfying the
3276 * task existence requirements of perf_event_enable/disable.
3278 static void perf_event_for_each_child(struct perf_event *event,
3279 void (*func)(struct perf_event *))
3281 struct perf_event *child;
3283 WARN_ON_ONCE(event->ctx->parent_ctx);
3284 mutex_lock(&event->child_mutex);
3286 list_for_each_entry(child, &event->child_list, child_list)
3288 mutex_unlock(&event->child_mutex);
3291 static void perf_event_for_each(struct perf_event *event,
3292 void (*func)(struct perf_event *))
3294 struct perf_event_context *ctx = event->ctx;
3295 struct perf_event *sibling;
3297 WARN_ON_ONCE(ctx->parent_ctx);
3298 mutex_lock(&ctx->mutex);
3299 event = event->group_leader;
3301 perf_event_for_each_child(event, func);
3303 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3304 perf_event_for_each_child(event, func);
3305 mutex_unlock(&ctx->mutex);
3308 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3310 struct perf_event_context *ctx = event->ctx;
3314 if (!is_sampling_event(event))
3317 if (copy_from_user(&value, arg, sizeof(value)))
3323 raw_spin_lock_irq(&ctx->lock);
3324 if (event->attr.freq) {
3325 if (value > sysctl_perf_event_sample_rate) {
3330 event->attr.sample_freq = value;
3332 event->attr.sample_period = value;
3333 event->hw.sample_period = value;
3336 raw_spin_unlock_irq(&ctx->lock);
3341 static const struct file_operations perf_fops;
3343 static struct file *perf_fget_light(int fd, int *fput_needed)
3347 file = fget_light(fd, fput_needed);
3349 return ERR_PTR(-EBADF);
3351 if (file->f_op != &perf_fops) {
3352 fput_light(file, *fput_needed);
3354 return ERR_PTR(-EBADF);
3360 static int perf_event_set_output(struct perf_event *event,
3361 struct perf_event *output_event);
3362 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3364 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3366 struct perf_event *event = file->private_data;
3367 void (*func)(struct perf_event *);
3371 case PERF_EVENT_IOC_ENABLE:
3372 func = perf_event_enable;
3374 case PERF_EVENT_IOC_DISABLE:
3375 func = perf_event_disable;
3377 case PERF_EVENT_IOC_RESET:
3378 func = perf_event_reset;
3381 case PERF_EVENT_IOC_REFRESH:
3382 return perf_event_refresh(event, arg);
3384 case PERF_EVENT_IOC_PERIOD:
3385 return perf_event_period(event, (u64 __user *)arg);
3387 case PERF_EVENT_IOC_SET_OUTPUT:
3389 struct file *output_file = NULL;
3390 struct perf_event *output_event = NULL;
3391 int fput_needed = 0;
3395 output_file = perf_fget_light(arg, &fput_needed);
3396 if (IS_ERR(output_file))
3397 return PTR_ERR(output_file);
3398 output_event = output_file->private_data;
3401 ret = perf_event_set_output(event, output_event);
3403 fput_light(output_file, fput_needed);
3408 case PERF_EVENT_IOC_SET_FILTER:
3409 return perf_event_set_filter(event, (void __user *)arg);
3415 if (flags & PERF_IOC_FLAG_GROUP)
3416 perf_event_for_each(event, func);
3418 perf_event_for_each_child(event, func);
3423 int perf_event_task_enable(void)
3425 struct perf_event *event;
3427 mutex_lock(¤t->perf_event_mutex);
3428 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3429 perf_event_for_each_child(event, perf_event_enable);
3430 mutex_unlock(¤t->perf_event_mutex);
3435 int perf_event_task_disable(void)
3437 struct perf_event *event;
3439 mutex_lock(¤t->perf_event_mutex);
3440 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3441 perf_event_for_each_child(event, perf_event_disable);
3442 mutex_unlock(¤t->perf_event_mutex);
3447 #ifndef PERF_EVENT_INDEX_OFFSET
3448 # define PERF_EVENT_INDEX_OFFSET 0
3451 static int perf_event_index(struct perf_event *event)
3453 if (event->hw.state & PERF_HES_STOPPED)
3456 if (event->state != PERF_EVENT_STATE_ACTIVE)
3459 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
3462 static void calc_timer_values(struct perf_event *event,
3469 ctx_time = event->shadow_ctx_time + now;
3470 *enabled = ctx_time - event->tstamp_enabled;
3471 *running = ctx_time - event->tstamp_running;
3475 * Callers need to ensure there can be no nesting of this function, otherwise
3476 * the seqlock logic goes bad. We can not serialize this because the arch
3477 * code calls this from NMI context.
3479 void perf_event_update_userpage(struct perf_event *event)
3481 struct perf_event_mmap_page *userpg;
3482 struct ring_buffer *rb;
3483 u64 enabled, running;
3487 * compute total_time_enabled, total_time_running
3488 * based on snapshot values taken when the event
3489 * was last scheduled in.
3491 * we cannot simply called update_context_time()
3492 * because of locking issue as we can be called in
3495 calc_timer_values(event, &enabled, &running);
3496 rb = rcu_dereference(event->rb);
3500 userpg = rb->user_page;
3503 * Disable preemption so as to not let the corresponding user-space
3504 * spin too long if we get preempted.
3509 userpg->index = perf_event_index(event);
3510 userpg->offset = perf_event_count(event);
3511 if (event->state == PERF_EVENT_STATE_ACTIVE)
3512 userpg->offset -= local64_read(&event->hw.prev_count);
3514 userpg->time_enabled = enabled +
3515 atomic64_read(&event->child_total_time_enabled);
3517 userpg->time_running = running +
3518 atomic64_read(&event->child_total_time_running);
3527 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3529 struct perf_event *event = vma->vm_file->private_data;
3530 struct ring_buffer *rb;
3531 int ret = VM_FAULT_SIGBUS;
3533 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3534 if (vmf->pgoff == 0)
3540 rb = rcu_dereference(event->rb);
3544 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3547 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
3551 get_page(vmf->page);
3552 vmf->page->mapping = vma->vm_file->f_mapping;
3553 vmf->page->index = vmf->pgoff;
3562 static void ring_buffer_attach(struct perf_event *event,
3563 struct ring_buffer *rb)
3565 unsigned long flags;
3567 if (!list_empty(&event->rb_entry))
3570 spin_lock_irqsave(&rb->event_lock, flags);
3571 if (list_empty(&event->rb_entry))
3572 list_add(&event->rb_entry, &rb->event_list);
3573 spin_unlock_irqrestore(&rb->event_lock, flags);
3576 static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb)
3578 unsigned long flags;
3580 if (list_empty(&event->rb_entry))
3583 spin_lock_irqsave(&rb->event_lock, flags);
3584 list_del_init(&event->rb_entry);
3585 wake_up_all(&event->waitq);
3586 spin_unlock_irqrestore(&rb->event_lock, flags);
3589 static void ring_buffer_wakeup(struct perf_event *event)
3591 struct ring_buffer *rb;
3594 rb = rcu_dereference(event->rb);
3596 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3597 wake_up_all(&event->waitq);
3602 static void rb_free_rcu(struct rcu_head *rcu_head)
3604 struct ring_buffer *rb;
3606 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3610 static struct ring_buffer *ring_buffer_get(struct perf_event *event)
3612 struct ring_buffer *rb;
3615 rb = rcu_dereference(event->rb);
3617 if (!atomic_inc_not_zero(&rb->refcount))
3625 static void ring_buffer_put(struct ring_buffer *rb)
3627 if (!atomic_dec_and_test(&rb->refcount))
3630 WARN_ON_ONCE(!list_empty(&rb->event_list));
3632 call_rcu(&rb->rcu_head, rb_free_rcu);
3635 static void perf_mmap_open(struct vm_area_struct *vma)
3637 struct perf_event *event = vma->vm_file->private_data;
3639 atomic_inc(&event->mmap_count);
3640 atomic_inc(&event->rb->mmap_count);
3644 * A buffer can be mmap()ed multiple times; either directly through the same
3645 * event, or through other events by use of perf_event_set_output().
3647 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3648 * the buffer here, where we still have a VM context. This means we need
3649 * to detach all events redirecting to us.
3651 static void perf_mmap_close(struct vm_area_struct *vma)
3653 struct perf_event *event = vma->vm_file->private_data;
3655 struct ring_buffer *rb = event->rb;
3656 struct user_struct *mmap_user = rb->mmap_user;
3657 int mmap_locked = rb->mmap_locked;
3658 unsigned long size = perf_data_size(rb);
3660 atomic_dec(&rb->mmap_count);
3662 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
3665 /* Detach current event from the buffer. */
3666 rcu_assign_pointer(event->rb, NULL);
3667 ring_buffer_detach(event, rb);
3668 mutex_unlock(&event->mmap_mutex);
3670 /* If there's still other mmap()s of this buffer, we're done. */
3671 if (atomic_read(&rb->mmap_count)) {
3672 ring_buffer_put(rb); /* can't be last */
3677 * No other mmap()s, detach from all other events that might redirect
3678 * into the now unreachable buffer. Somewhat complicated by the
3679 * fact that rb::event_lock otherwise nests inside mmap_mutex.
3683 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
3684 if (!atomic_long_inc_not_zero(&event->refcount)) {
3686 * This event is en-route to free_event() which will
3687 * detach it and remove it from the list.
3693 mutex_lock(&event->mmap_mutex);
3695 * Check we didn't race with perf_event_set_output() which can
3696 * swizzle the rb from under us while we were waiting to
3697 * acquire mmap_mutex.
3699 * If we find a different rb; ignore this event, a next
3700 * iteration will no longer find it on the list. We have to
3701 * still restart the iteration to make sure we're not now
3702 * iterating the wrong list.
3704 if (event->rb == rb) {
3705 rcu_assign_pointer(event->rb, NULL);
3706 ring_buffer_detach(event, rb);
3707 ring_buffer_put(rb); /* can't be last, we still have one */
3709 mutex_unlock(&event->mmap_mutex);
3713 * Restart the iteration; either we're on the wrong list or
3714 * destroyed its integrity by doing a deletion.
3721 * It could be there's still a few 0-ref events on the list; they'll
3722 * get cleaned up by free_event() -- they'll also still have their
3723 * ref on the rb and will free it whenever they are done with it.
3725 * Aside from that, this buffer is 'fully' detached and unmapped,
3726 * undo the VM accounting.
3729 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
3730 vma->vm_mm->pinned_vm -= mmap_locked;
3731 free_uid(mmap_user);
3733 ring_buffer_put(rb); /* could be last */
3736 static const struct vm_operations_struct perf_mmap_vmops = {
3737 .open = perf_mmap_open,
3738 .close = perf_mmap_close,
3739 .fault = perf_mmap_fault,
3740 .page_mkwrite = perf_mmap_fault,
3743 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3745 struct perf_event *event = file->private_data;
3746 unsigned long user_locked, user_lock_limit;
3747 struct user_struct *user = current_user();
3748 unsigned long locked, lock_limit;
3749 struct ring_buffer *rb;
3750 unsigned long vma_size;
3751 unsigned long nr_pages;
3752 long user_extra, extra;
3753 int ret = 0, flags = 0;
3756 * Don't allow mmap() of inherited per-task counters. This would
3757 * create a performance issue due to all children writing to the
3760 if (event->cpu == -1 && event->attr.inherit)
3763 if (!(vma->vm_flags & VM_SHARED))
3766 vma_size = vma->vm_end - vma->vm_start;
3767 nr_pages = (vma_size / PAGE_SIZE) - 1;
3770 * If we have rb pages ensure they're a power-of-two number, so we
3771 * can do bitmasks instead of modulo.
3773 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3776 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3779 if (vma->vm_pgoff != 0)
3782 WARN_ON_ONCE(event->ctx->parent_ctx);
3784 mutex_lock(&event->mmap_mutex);
3786 if (event->rb->nr_pages != nr_pages) {
3791 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
3793 * Raced against perf_mmap_close() through
3794 * perf_event_set_output(). Try again, hope for better
3797 mutex_unlock(&event->mmap_mutex);
3804 user_extra = nr_pages + 1;
3805 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3808 * Increase the limit linearly with more CPUs:
3810 user_lock_limit *= num_online_cpus();
3812 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3815 if (user_locked > user_lock_limit)
3816 extra = user_locked - user_lock_limit;
3818 lock_limit = rlimit(RLIMIT_MEMLOCK);
3819 lock_limit >>= PAGE_SHIFT;
3820 locked = vma->vm_mm->pinned_vm + extra;
3822 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3823 !capable(CAP_IPC_LOCK)) {
3830 if (vma->vm_flags & VM_WRITE)
3831 flags |= RING_BUFFER_WRITABLE;
3833 rb = rb_alloc(nr_pages,
3834 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3842 atomic_set(&rb->mmap_count, 1);
3843 rb->mmap_locked = extra;
3844 rb->mmap_user = get_current_user();
3846 atomic_long_add(user_extra, &user->locked_vm);
3847 vma->vm_mm->pinned_vm += extra;
3849 ring_buffer_attach(event, rb);
3850 rcu_assign_pointer(event->rb, rb);
3854 atomic_inc(&event->mmap_count);
3855 mutex_unlock(&event->mmap_mutex);
3858 * Since pinned accounting is per vm we cannot allow fork() to copy our
3861 vma->vm_flags |= VM_DONTCOPY | VM_RESERVED;
3862 vma->vm_ops = &perf_mmap_vmops;
3867 static int perf_fasync(int fd, struct file *filp, int on)
3869 struct inode *inode = filp->f_path.dentry->d_inode;
3870 struct perf_event *event = filp->private_data;
3873 mutex_lock(&inode->i_mutex);
3874 retval = fasync_helper(fd, filp, on, &event->fasync);
3875 mutex_unlock(&inode->i_mutex);
3883 static const struct file_operations perf_fops = {
3884 .llseek = no_llseek,
3885 .release = perf_release,
3888 .unlocked_ioctl = perf_ioctl,
3889 .compat_ioctl = perf_ioctl,
3891 .fasync = perf_fasync,
3897 * If there's data, ensure we set the poll() state and publish everything
3898 * to user-space before waking everybody up.
3901 void perf_event_wakeup(struct perf_event *event)
3903 ring_buffer_wakeup(event);
3905 if (event->pending_kill) {
3906 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3907 event->pending_kill = 0;
3911 static void perf_pending_event(struct irq_work *entry)
3913 struct perf_event *event = container_of(entry,
3914 struct perf_event, pending);
3916 if (event->pending_disable) {
3917 event->pending_disable = 0;
3918 __perf_event_disable(event);
3921 if (event->pending_wakeup) {
3922 event->pending_wakeup = 0;
3923 perf_event_wakeup(event);
3928 * We assume there is only KVM supporting the callbacks.
3929 * Later on, we might change it to a list if there is
3930 * another virtualization implementation supporting the callbacks.
3932 struct perf_guest_info_callbacks *perf_guest_cbs;
3934 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3936 perf_guest_cbs = cbs;
3939 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3941 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3943 perf_guest_cbs = NULL;
3946 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3948 static void __perf_event_header__init_id(struct perf_event_header *header,
3949 struct perf_sample_data *data,
3950 struct perf_event *event)
3952 u64 sample_type = event->attr.sample_type;
3954 data->type = sample_type;
3955 header->size += event->id_header_size;
3957 if (sample_type & PERF_SAMPLE_TID) {
3958 /* namespace issues */
3959 data->tid_entry.pid = perf_event_pid(event, current);
3960 data->tid_entry.tid = perf_event_tid(event, current);
3963 if (sample_type & PERF_SAMPLE_TIME)
3964 data->time = perf_clock();
3966 if (sample_type & PERF_SAMPLE_ID)
3967 data->id = primary_event_id(event);
3969 if (sample_type & PERF_SAMPLE_STREAM_ID)
3970 data->stream_id = event->id;
3972 if (sample_type & PERF_SAMPLE_CPU) {
3973 data->cpu_entry.cpu = raw_smp_processor_id();
3974 data->cpu_entry.reserved = 0;
3978 void perf_event_header__init_id(struct perf_event_header *header,
3979 struct perf_sample_data *data,
3980 struct perf_event *event)
3982 if (event->attr.sample_id_all)
3983 __perf_event_header__init_id(header, data, event);
3986 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3987 struct perf_sample_data *data)
3989 u64 sample_type = data->type;
3991 if (sample_type & PERF_SAMPLE_TID)
3992 perf_output_put(handle, data->tid_entry);
3994 if (sample_type & PERF_SAMPLE_TIME)
3995 perf_output_put(handle, data->time);
3997 if (sample_type & PERF_SAMPLE_ID)
3998 perf_output_put(handle, data->id);
4000 if (sample_type & PERF_SAMPLE_STREAM_ID)
4001 perf_output_put(handle, data->stream_id);
4003 if (sample_type & PERF_SAMPLE_CPU)
4004 perf_output_put(handle, data->cpu_entry);
4007 void perf_event__output_id_sample(struct perf_event *event,
4008 struct perf_output_handle *handle,
4009 struct perf_sample_data *sample)
4011 if (event->attr.sample_id_all)
4012 __perf_event__output_id_sample(handle, sample);
4015 static void perf_output_read_one(struct perf_output_handle *handle,
4016 struct perf_event *event,
4017 u64 enabled, u64 running)
4019 u64 read_format = event->attr.read_format;
4023 values[n++] = perf_event_count(event);
4024 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4025 values[n++] = enabled +
4026 atomic64_read(&event->child_total_time_enabled);
4028 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4029 values[n++] = running +
4030 atomic64_read(&event->child_total_time_running);
4032 if (read_format & PERF_FORMAT_ID)
4033 values[n++] = primary_event_id(event);
4035 __output_copy(handle, values, n * sizeof(u64));
4039 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4041 static void perf_output_read_group(struct perf_output_handle *handle,
4042 struct perf_event *event,
4043 u64 enabled, u64 running)
4045 struct perf_event *leader = event->group_leader, *sub;
4046 u64 read_format = event->attr.read_format;
4050 values[n++] = 1 + leader->nr_siblings;
4052 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4053 values[n++] = enabled;
4055 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4056 values[n++] = running;
4058 if (leader != event)
4059 leader->pmu->read(leader);
4061 values[n++] = perf_event_count(leader);
4062 if (read_format & PERF_FORMAT_ID)
4063 values[n++] = primary_event_id(leader);
4065 __output_copy(handle, values, n * sizeof(u64));
4067 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4071 sub->pmu->read(sub);
4073 values[n++] = perf_event_count(sub);
4074 if (read_format & PERF_FORMAT_ID)
4075 values[n++] = primary_event_id(sub);
4077 __output_copy(handle, values, n * sizeof(u64));
4081 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4082 PERF_FORMAT_TOTAL_TIME_RUNNING)
4084 static void perf_output_read(struct perf_output_handle *handle,
4085 struct perf_event *event)
4087 u64 enabled = 0, running = 0;
4088 u64 read_format = event->attr.read_format;
4091 * compute total_time_enabled, total_time_running
4092 * based on snapshot values taken when the event
4093 * was last scheduled in.
4095 * we cannot simply called update_context_time()
4096 * because of locking issue as we are called in
4099 if (read_format & PERF_FORMAT_TOTAL_TIMES)
4100 calc_timer_values(event, &enabled, &running);
4102 if (event->attr.read_format & PERF_FORMAT_GROUP)
4103 perf_output_read_group(handle, event, enabled, running);
4105 perf_output_read_one(handle, event, enabled, running);
4108 void perf_output_sample(struct perf_output_handle *handle,
4109 struct perf_event_header *header,
4110 struct perf_sample_data *data,
4111 struct perf_event *event)
4113 u64 sample_type = data->type;
4115 perf_output_put(handle, *header);
4117 if (sample_type & PERF_SAMPLE_IP)
4118 perf_output_put(handle, data->ip);
4120 if (sample_type & PERF_SAMPLE_TID)
4121 perf_output_put(handle, data->tid_entry);
4123 if (sample_type & PERF_SAMPLE_TIME)
4124 perf_output_put(handle, data->time);
4126 if (sample_type & PERF_SAMPLE_ADDR)
4127 perf_output_put(handle, data->addr);
4129 if (sample_type & PERF_SAMPLE_ID)
4130 perf_output_put(handle, data->id);
4132 if (sample_type & PERF_SAMPLE_STREAM_ID)
4133 perf_output_put(handle, data->stream_id);
4135 if (sample_type & PERF_SAMPLE_CPU)
4136 perf_output_put(handle, data->cpu_entry);
4138 if (sample_type & PERF_SAMPLE_PERIOD)
4139 perf_output_put(handle, data->period);
4141 if (sample_type & PERF_SAMPLE_READ)
4142 perf_output_read(handle, event);
4144 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4145 if (data->callchain) {
4148 if (data->callchain)
4149 size += data->callchain->nr;
4151 size *= sizeof(u64);
4153 __output_copy(handle, data->callchain, size);
4156 perf_output_put(handle, nr);
4160 if (sample_type & PERF_SAMPLE_RAW) {
4162 perf_output_put(handle, data->raw->size);
4163 __output_copy(handle, data->raw->data,
4170 .size = sizeof(u32),
4173 perf_output_put(handle, raw);
4177 if (!event->attr.watermark) {
4178 int wakeup_events = event->attr.wakeup_events;
4180 if (wakeup_events) {
4181 struct ring_buffer *rb = handle->rb;
4182 int events = local_inc_return(&rb->events);
4184 if (events >= wakeup_events) {
4185 local_sub(wakeup_events, &rb->events);
4186 local_inc(&rb->wakeup);
4192 void perf_prepare_sample(struct perf_event_header *header,
4193 struct perf_sample_data *data,
4194 struct perf_event *event,
4195 struct pt_regs *regs)
4197 u64 sample_type = event->attr.sample_type;
4199 header->type = PERF_RECORD_SAMPLE;
4200 header->size = sizeof(*header) + event->header_size;
4203 header->misc |= perf_misc_flags(regs);
4205 __perf_event_header__init_id(header, data, event);
4207 if (sample_type & PERF_SAMPLE_IP)
4208 data->ip = perf_instruction_pointer(regs);
4210 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4213 data->callchain = perf_callchain(regs);
4215 if (data->callchain)
4216 size += data->callchain->nr;
4218 header->size += size * sizeof(u64);
4221 if (sample_type & PERF_SAMPLE_RAW) {
4222 int size = sizeof(u32);
4225 size += data->raw->size;
4227 size += sizeof(u32);
4229 WARN_ON_ONCE(size & (sizeof(u64)-1));
4230 header->size += size;
4234 static void perf_event_output(struct perf_event *event,
4235 struct perf_sample_data *data,
4236 struct pt_regs *regs)
4238 struct perf_output_handle handle;
4239 struct perf_event_header header;
4241 /* protect the callchain buffers */
4244 perf_prepare_sample(&header, data, event, regs);
4246 if (perf_output_begin(&handle, event, header.size))
4249 perf_output_sample(&handle, &header, data, event);
4251 perf_output_end(&handle);
4261 struct perf_read_event {
4262 struct perf_event_header header;
4269 perf_event_read_event(struct perf_event *event,
4270 struct task_struct *task)
4272 struct perf_output_handle handle;
4273 struct perf_sample_data sample;
4274 struct perf_read_event read_event = {
4276 .type = PERF_RECORD_READ,
4278 .size = sizeof(read_event) + event->read_size,
4280 .pid = perf_event_pid(event, task),
4281 .tid = perf_event_tid(event, task),
4285 perf_event_header__init_id(&read_event.header, &sample, event);
4286 ret = perf_output_begin(&handle, event, read_event.header.size);
4290 perf_output_put(&handle, read_event);
4291 perf_output_read(&handle, event);
4292 perf_event__output_id_sample(event, &handle, &sample);
4294 perf_output_end(&handle);
4298 * task tracking -- fork/exit
4300 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4303 struct perf_task_event {
4304 struct task_struct *task;
4305 struct perf_event_context *task_ctx;
4308 struct perf_event_header header;
4318 static void perf_event_task_output(struct perf_event *event,
4319 struct perf_task_event *task_event)
4321 struct perf_output_handle handle;
4322 struct perf_sample_data sample;
4323 struct task_struct *task = task_event->task;
4324 int ret, size = task_event->event_id.header.size;
4326 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4328 ret = perf_output_begin(&handle, event,
4329 task_event->event_id.header.size);
4333 task_event->event_id.pid = perf_event_pid(event, task);
4334 task_event->event_id.ppid = perf_event_pid(event, current);
4336 task_event->event_id.tid = perf_event_tid(event, task);
4337 task_event->event_id.ptid = perf_event_tid(event, current);
4339 perf_output_put(&handle, task_event->event_id);
4341 perf_event__output_id_sample(event, &handle, &sample);
4343 perf_output_end(&handle);
4345 task_event->event_id.header.size = size;
4348 static int perf_event_task_match(struct perf_event *event)
4350 if (event->state < PERF_EVENT_STATE_INACTIVE)
4353 if (!event_filter_match(event))
4356 if (event->attr.comm || event->attr.mmap ||
4357 event->attr.mmap_data || event->attr.task)
4363 static void perf_event_task_ctx(struct perf_event_context *ctx,
4364 struct perf_task_event *task_event)
4366 struct perf_event *event;
4368 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4369 if (perf_event_task_match(event))
4370 perf_event_task_output(event, task_event);
4374 static void perf_event_task_event(struct perf_task_event *task_event)
4376 struct perf_cpu_context *cpuctx;
4377 struct perf_event_context *ctx;
4382 list_for_each_entry_rcu(pmu, &pmus, entry) {
4383 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4384 if (cpuctx->active_pmu != pmu)
4386 perf_event_task_ctx(&cpuctx->ctx, task_event);
4388 ctx = task_event->task_ctx;
4390 ctxn = pmu->task_ctx_nr;
4393 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4396 perf_event_task_ctx(ctx, task_event);
4398 put_cpu_ptr(pmu->pmu_cpu_context);
4403 static void perf_event_task(struct task_struct *task,
4404 struct perf_event_context *task_ctx,
4407 struct perf_task_event task_event;
4409 if (!atomic_read(&nr_comm_events) &&
4410 !atomic_read(&nr_mmap_events) &&
4411 !atomic_read(&nr_task_events))
4414 task_event = (struct perf_task_event){
4416 .task_ctx = task_ctx,
4419 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4421 .size = sizeof(task_event.event_id),
4427 .time = perf_clock(),
4431 perf_event_task_event(&task_event);
4434 void perf_event_fork(struct task_struct *task)
4436 perf_event_task(task, NULL, 1);
4443 struct perf_comm_event {
4444 struct task_struct *task;
4449 struct perf_event_header header;
4456 static void perf_event_comm_output(struct perf_event *event,
4457 struct perf_comm_event *comm_event)
4459 struct perf_output_handle handle;
4460 struct perf_sample_data sample;
4461 int size = comm_event->event_id.header.size;
4464 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4465 ret = perf_output_begin(&handle, event,
4466 comm_event->event_id.header.size);
4471 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4472 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4474 perf_output_put(&handle, comm_event->event_id);
4475 __output_copy(&handle, comm_event->comm,
4476 comm_event->comm_size);
4478 perf_event__output_id_sample(event, &handle, &sample);
4480 perf_output_end(&handle);
4482 comm_event->event_id.header.size = size;
4485 static int perf_event_comm_match(struct perf_event *event)
4487 if (event->state < PERF_EVENT_STATE_INACTIVE)
4490 if (!event_filter_match(event))
4493 if (event->attr.comm)
4499 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4500 struct perf_comm_event *comm_event)
4502 struct perf_event *event;
4504 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4505 if (perf_event_comm_match(event))
4506 perf_event_comm_output(event, comm_event);
4510 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4512 struct perf_cpu_context *cpuctx;
4513 struct perf_event_context *ctx;
4514 char comm[TASK_COMM_LEN];
4519 memset(comm, 0, sizeof(comm));
4520 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4521 size = ALIGN(strlen(comm)+1, sizeof(u64));
4523 comm_event->comm = comm;
4524 comm_event->comm_size = size;
4526 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4528 list_for_each_entry_rcu(pmu, &pmus, entry) {
4529 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4530 if (cpuctx->active_pmu != pmu)
4532 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4534 ctxn = pmu->task_ctx_nr;
4538 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4540 perf_event_comm_ctx(ctx, comm_event);
4542 put_cpu_ptr(pmu->pmu_cpu_context);
4547 void perf_event_comm(struct task_struct *task)
4549 struct perf_comm_event comm_event;
4550 struct perf_event_context *ctx;
4553 for_each_task_context_nr(ctxn) {
4554 ctx = task->perf_event_ctxp[ctxn];
4558 perf_event_enable_on_exec(ctx);
4561 if (!atomic_read(&nr_comm_events))
4564 comm_event = (struct perf_comm_event){
4570 .type = PERF_RECORD_COMM,
4579 perf_event_comm_event(&comm_event);
4586 struct perf_mmap_event {
4587 struct vm_area_struct *vma;
4589 const char *file_name;
4593 struct perf_event_header header;
4603 static void perf_event_mmap_output(struct perf_event *event,
4604 struct perf_mmap_event *mmap_event)
4606 struct perf_output_handle handle;
4607 struct perf_sample_data sample;
4608 int size = mmap_event->event_id.header.size;
4611 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4612 ret = perf_output_begin(&handle, event,
4613 mmap_event->event_id.header.size);
4617 mmap_event->event_id.pid = perf_event_pid(event, current);
4618 mmap_event->event_id.tid = perf_event_tid(event, current);
4620 perf_output_put(&handle, mmap_event->event_id);
4621 __output_copy(&handle, mmap_event->file_name,
4622 mmap_event->file_size);
4624 perf_event__output_id_sample(event, &handle, &sample);
4626 perf_output_end(&handle);
4628 mmap_event->event_id.header.size = size;
4631 static int perf_event_mmap_match(struct perf_event *event,
4632 struct perf_mmap_event *mmap_event,
4635 if (event->state < PERF_EVENT_STATE_INACTIVE)
4638 if (!event_filter_match(event))
4641 if ((!executable && event->attr.mmap_data) ||
4642 (executable && event->attr.mmap))
4648 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4649 struct perf_mmap_event *mmap_event,
4652 struct perf_event *event;
4654 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4655 if (perf_event_mmap_match(event, mmap_event, executable))
4656 perf_event_mmap_output(event, mmap_event);
4660 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4662 struct perf_cpu_context *cpuctx;
4663 struct perf_event_context *ctx;
4664 struct vm_area_struct *vma = mmap_event->vma;
4665 struct file *file = vma->vm_file;
4673 memset(tmp, 0, sizeof(tmp));
4677 * d_path works from the end of the rb backwards, so we
4678 * need to add enough zero bytes after the string to handle
4679 * the 64bit alignment we do later.
4681 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4683 name = strncpy(tmp, "//enomem", sizeof(tmp));
4686 name = d_path(&file->f_path, buf, PATH_MAX);
4688 name = strncpy(tmp, "//toolong", sizeof(tmp));
4692 if (arch_vma_name(mmap_event->vma)) {
4693 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4699 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4701 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4702 vma->vm_end >= vma->vm_mm->brk) {
4703 name = strncpy(tmp, "[heap]", sizeof(tmp));
4705 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4706 vma->vm_end >= vma->vm_mm->start_stack) {
4707 name = strncpy(tmp, "[stack]", sizeof(tmp));
4711 name = strncpy(tmp, "//anon", sizeof(tmp));
4716 size = ALIGN(strlen(name)+1, sizeof(u64));
4718 mmap_event->file_name = name;
4719 mmap_event->file_size = size;
4721 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4724 list_for_each_entry_rcu(pmu, &pmus, entry) {
4725 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4726 if (cpuctx->active_pmu != pmu)
4728 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4729 vma->vm_flags & VM_EXEC);
4731 ctxn = pmu->task_ctx_nr;
4735 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4737 perf_event_mmap_ctx(ctx, mmap_event,
4738 vma->vm_flags & VM_EXEC);
4741 put_cpu_ptr(pmu->pmu_cpu_context);
4748 void perf_event_mmap(struct vm_area_struct *vma)
4750 struct perf_mmap_event mmap_event;
4752 if (!atomic_read(&nr_mmap_events))
4755 mmap_event = (struct perf_mmap_event){
4761 .type = PERF_RECORD_MMAP,
4762 .misc = PERF_RECORD_MISC_USER,
4767 .start = vma->vm_start,
4768 .len = vma->vm_end - vma->vm_start,
4769 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4773 perf_event_mmap_event(&mmap_event);
4777 * IRQ throttle logging
4780 static void perf_log_throttle(struct perf_event *event, int enable)
4782 struct perf_output_handle handle;
4783 struct perf_sample_data sample;
4787 struct perf_event_header header;
4791 } throttle_event = {
4793 .type = PERF_RECORD_THROTTLE,
4795 .size = sizeof(throttle_event),
4797 .time = perf_clock(),
4798 .id = primary_event_id(event),
4799 .stream_id = event->id,
4803 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4805 perf_event_header__init_id(&throttle_event.header, &sample, event);
4807 ret = perf_output_begin(&handle, event,
4808 throttle_event.header.size);
4812 perf_output_put(&handle, throttle_event);
4813 perf_event__output_id_sample(event, &handle, &sample);
4814 perf_output_end(&handle);
4818 * Generic event overflow handling, sampling.
4821 static int __perf_event_overflow(struct perf_event *event,
4822 int throttle, struct perf_sample_data *data,
4823 struct pt_regs *regs)
4825 int events = atomic_read(&event->event_limit);
4826 struct hw_perf_event *hwc = &event->hw;
4830 * Non-sampling counters might still use the PMI to fold short
4831 * hardware counters, ignore those.
4833 if (unlikely(!is_sampling_event(event)))
4836 if (unlikely(hwc->interrupts >= max_samples_per_tick)) {
4838 hwc->interrupts = MAX_INTERRUPTS;
4839 perf_log_throttle(event, 0);
4845 if (event->attr.freq) {
4846 u64 now = perf_clock();
4847 s64 delta = now - hwc->freq_time_stamp;
4849 hwc->freq_time_stamp = now;
4851 if (delta > 0 && delta < 2*TICK_NSEC)
4852 perf_adjust_period(event, delta, hwc->last_period);
4856 * XXX event_limit might not quite work as expected on inherited
4860 event->pending_kill = POLL_IN;
4861 if (events && atomic_dec_and_test(&event->event_limit)) {
4863 event->pending_kill = POLL_HUP;
4864 event->pending_disable = 1;
4865 irq_work_queue(&event->pending);
4868 if (event->overflow_handler)
4869 event->overflow_handler(event, data, regs);
4871 perf_event_output(event, data, regs);
4873 if (event->fasync && event->pending_kill) {
4874 event->pending_wakeup = 1;
4875 irq_work_queue(&event->pending);
4881 int perf_event_overflow(struct perf_event *event,
4882 struct perf_sample_data *data,
4883 struct pt_regs *regs)
4885 return __perf_event_overflow(event, 1, data, regs);
4889 * Generic software event infrastructure
4892 struct swevent_htable {
4893 struct swevent_hlist *swevent_hlist;
4894 struct mutex hlist_mutex;
4897 /* Recursion avoidance in each contexts */
4898 int recursion[PERF_NR_CONTEXTS];
4901 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4904 * We directly increment event->count and keep a second value in
4905 * event->hw.period_left to count intervals. This period event
4906 * is kept in the range [-sample_period, 0] so that we can use the
4910 static u64 perf_swevent_set_period(struct perf_event *event)
4912 struct hw_perf_event *hwc = &event->hw;
4913 u64 period = hwc->last_period;
4917 hwc->last_period = hwc->sample_period;
4920 old = val = local64_read(&hwc->period_left);
4924 nr = div64_u64(period + val, period);
4925 offset = nr * period;
4927 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4933 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4934 struct perf_sample_data *data,
4935 struct pt_regs *regs)
4937 struct hw_perf_event *hwc = &event->hw;
4940 data->period = event->hw.last_period;
4942 overflow = perf_swevent_set_period(event);
4944 if (hwc->interrupts == MAX_INTERRUPTS)
4947 for (; overflow; overflow--) {
4948 if (__perf_event_overflow(event, throttle,
4951 * We inhibit the overflow from happening when
4952 * hwc->interrupts == MAX_INTERRUPTS.
4960 static void perf_swevent_event(struct perf_event *event, u64 nr,
4961 struct perf_sample_data *data,
4962 struct pt_regs *regs)
4964 struct hw_perf_event *hwc = &event->hw;
4966 local64_add(nr, &event->count);
4971 if (!is_sampling_event(event))
4974 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4975 return perf_swevent_overflow(event, 1, data, regs);
4977 if (local64_add_negative(nr, &hwc->period_left))
4980 perf_swevent_overflow(event, 0, data, regs);
4983 static int perf_exclude_event(struct perf_event *event,
4984 struct pt_regs *regs)
4986 if (event->hw.state & PERF_HES_STOPPED)
4990 if (event->attr.exclude_user && user_mode(regs))
4993 if (event->attr.exclude_kernel && !user_mode(regs))
5000 static int perf_swevent_match(struct perf_event *event,
5001 enum perf_type_id type,
5003 struct perf_sample_data *data,
5004 struct pt_regs *regs)
5006 if (event->attr.type != type)
5009 if (event->attr.config != event_id)
5012 if (perf_exclude_event(event, regs))
5018 static inline u64 swevent_hash(u64 type, u32 event_id)
5020 u64 val = event_id | (type << 32);
5022 return hash_64(val, SWEVENT_HLIST_BITS);
5025 static inline struct hlist_head *
5026 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
5028 u64 hash = swevent_hash(type, event_id);
5030 return &hlist->heads[hash];
5033 /* For the read side: events when they trigger */
5034 static inline struct hlist_head *
5035 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
5037 struct swevent_hlist *hlist;
5039 hlist = rcu_dereference(swhash->swevent_hlist);
5043 return __find_swevent_head(hlist, type, event_id);
5046 /* For the event head insertion and removal in the hlist */
5047 static inline struct hlist_head *
5048 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
5050 struct swevent_hlist *hlist;
5051 u32 event_id = event->attr.config;
5052 u64 type = event->attr.type;
5055 * Event scheduling is always serialized against hlist allocation
5056 * and release. Which makes the protected version suitable here.
5057 * The context lock guarantees that.
5059 hlist = rcu_dereference_protected(swhash->swevent_hlist,
5060 lockdep_is_held(&event->ctx->lock));
5064 return __find_swevent_head(hlist, type, event_id);
5067 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5069 struct perf_sample_data *data,
5070 struct pt_regs *regs)
5072 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5073 struct perf_event *event;
5074 struct hlist_node *node;
5075 struct hlist_head *head;
5078 head = find_swevent_head_rcu(swhash, type, event_id);
5082 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5083 if (perf_swevent_match(event, type, event_id, data, regs))
5084 perf_swevent_event(event, nr, data, regs);
5090 int perf_swevent_get_recursion_context(void)
5092 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5094 return get_recursion_context(swhash->recursion);
5096 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
5098 inline void perf_swevent_put_recursion_context(int rctx)
5100 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5102 put_recursion_context(swhash->recursion, rctx);
5105 void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
5107 struct perf_sample_data data;
5110 preempt_disable_notrace();
5111 rctx = perf_swevent_get_recursion_context();
5115 perf_sample_data_init(&data, addr);
5117 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
5119 perf_swevent_put_recursion_context(rctx);
5120 preempt_enable_notrace();
5123 static void perf_swevent_read(struct perf_event *event)
5127 static int perf_swevent_add(struct perf_event *event, int flags)
5129 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5130 struct hw_perf_event *hwc = &event->hw;
5131 struct hlist_head *head;
5133 if (is_sampling_event(event)) {
5134 hwc->last_period = hwc->sample_period;
5135 perf_swevent_set_period(event);
5138 hwc->state = !(flags & PERF_EF_START);
5140 head = find_swevent_head(swhash, event);
5141 if (WARN_ON_ONCE(!head))
5144 hlist_add_head_rcu(&event->hlist_entry, head);
5149 static void perf_swevent_del(struct perf_event *event, int flags)
5151 hlist_del_rcu(&event->hlist_entry);
5154 static void perf_swevent_start(struct perf_event *event, int flags)
5156 event->hw.state = 0;
5159 static void perf_swevent_stop(struct perf_event *event, int flags)
5161 event->hw.state = PERF_HES_STOPPED;
5164 /* Deref the hlist from the update side */
5165 static inline struct swevent_hlist *
5166 swevent_hlist_deref(struct swevent_htable *swhash)
5168 return rcu_dereference_protected(swhash->swevent_hlist,
5169 lockdep_is_held(&swhash->hlist_mutex));
5172 static void swevent_hlist_release(struct swevent_htable *swhash)
5174 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
5179 rcu_assign_pointer(swhash->swevent_hlist, NULL);
5180 kfree_rcu(hlist, rcu_head);
5183 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5185 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5187 mutex_lock(&swhash->hlist_mutex);
5189 if (!--swhash->hlist_refcount)
5190 swevent_hlist_release(swhash);
5192 mutex_unlock(&swhash->hlist_mutex);
5195 static void swevent_hlist_put(struct perf_event *event)
5199 if (event->cpu != -1) {
5200 swevent_hlist_put_cpu(event, event->cpu);
5204 for_each_possible_cpu(cpu)
5205 swevent_hlist_put_cpu(event, cpu);
5208 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5210 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5213 mutex_lock(&swhash->hlist_mutex);
5215 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
5216 struct swevent_hlist *hlist;
5218 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5223 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5225 swhash->hlist_refcount++;
5227 mutex_unlock(&swhash->hlist_mutex);
5232 static int swevent_hlist_get(struct perf_event *event)
5235 int cpu, failed_cpu;
5237 if (event->cpu != -1)
5238 return swevent_hlist_get_cpu(event, event->cpu);
5241 for_each_possible_cpu(cpu) {
5242 err = swevent_hlist_get_cpu(event, cpu);
5252 for_each_possible_cpu(cpu) {
5253 if (cpu == failed_cpu)
5255 swevent_hlist_put_cpu(event, cpu);
5262 struct jump_label_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
5264 static void sw_perf_event_destroy(struct perf_event *event)
5266 u64 event_id = event->attr.config;
5268 WARN_ON(event->parent);
5270 jump_label_dec(&perf_swevent_enabled[event_id]);
5271 swevent_hlist_put(event);
5274 static int perf_swevent_init(struct perf_event *event)
5276 u64 event_id = event->attr.config;
5278 if (event->attr.type != PERF_TYPE_SOFTWARE)
5282 case PERF_COUNT_SW_CPU_CLOCK:
5283 case PERF_COUNT_SW_TASK_CLOCK:
5290 if (event_id >= PERF_COUNT_SW_MAX)
5293 if (!event->parent) {
5296 err = swevent_hlist_get(event);
5300 jump_label_inc(&perf_swevent_enabled[event_id]);
5301 event->destroy = sw_perf_event_destroy;
5307 static struct pmu perf_swevent = {
5308 .task_ctx_nr = perf_sw_context,
5310 .event_init = perf_swevent_init,
5311 .add = perf_swevent_add,
5312 .del = perf_swevent_del,
5313 .start = perf_swevent_start,
5314 .stop = perf_swevent_stop,
5315 .read = perf_swevent_read,
5318 #ifdef CONFIG_EVENT_TRACING
5320 static int perf_tp_filter_match(struct perf_event *event,
5321 struct perf_sample_data *data)
5323 void *record = data->raw->data;
5325 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5330 static int perf_tp_event_match(struct perf_event *event,
5331 struct perf_sample_data *data,
5332 struct pt_regs *regs)
5334 if (event->hw.state & PERF_HES_STOPPED)
5337 * All tracepoints are from kernel-space.
5339 if (event->attr.exclude_kernel)
5342 if (!perf_tp_filter_match(event, data))
5348 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5349 struct pt_regs *regs, struct hlist_head *head, int rctx)
5351 struct perf_sample_data data;
5352 struct perf_event *event;
5353 struct hlist_node *node;
5355 struct perf_raw_record raw = {
5360 perf_sample_data_init(&data, addr);
5363 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5364 if (perf_tp_event_match(event, &data, regs))
5365 perf_swevent_event(event, count, &data, regs);
5368 perf_swevent_put_recursion_context(rctx);
5370 EXPORT_SYMBOL_GPL(perf_tp_event);
5372 static void tp_perf_event_destroy(struct perf_event *event)
5374 perf_trace_destroy(event);
5377 static int perf_tp_event_init(struct perf_event *event)
5381 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5384 err = perf_trace_init(event);
5388 event->destroy = tp_perf_event_destroy;
5393 static struct pmu perf_tracepoint = {
5394 .task_ctx_nr = perf_sw_context,
5396 .event_init = perf_tp_event_init,
5397 .add = perf_trace_add,
5398 .del = perf_trace_del,
5399 .start = perf_swevent_start,
5400 .stop = perf_swevent_stop,
5401 .read = perf_swevent_read,
5404 static inline void perf_tp_register(void)
5406 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5409 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5414 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5417 filter_str = strndup_user(arg, PAGE_SIZE);
5418 if (IS_ERR(filter_str))
5419 return PTR_ERR(filter_str);
5421 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5427 static void perf_event_free_filter(struct perf_event *event)
5429 ftrace_profile_free_filter(event);
5434 static inline void perf_tp_register(void)
5438 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5443 static void perf_event_free_filter(struct perf_event *event)
5447 #endif /* CONFIG_EVENT_TRACING */
5449 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5450 void perf_bp_event(struct perf_event *bp, void *data)
5452 struct perf_sample_data sample;
5453 struct pt_regs *regs = data;
5455 perf_sample_data_init(&sample, bp->attr.bp_addr);
5457 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5458 perf_swevent_event(bp, 1, &sample, regs);
5463 * hrtimer based swevent callback
5466 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5468 enum hrtimer_restart ret = HRTIMER_RESTART;
5469 struct perf_sample_data data;
5470 struct pt_regs *regs;
5471 struct perf_event *event;
5474 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5476 if (event->state != PERF_EVENT_STATE_ACTIVE)
5477 return HRTIMER_NORESTART;
5479 event->pmu->read(event);
5481 perf_sample_data_init(&data, 0);
5482 data.period = event->hw.last_period;
5483 regs = get_irq_regs();
5485 if (regs && !perf_exclude_event(event, regs)) {
5486 if (!(event->attr.exclude_idle && current->pid == 0))
5487 if (perf_event_overflow(event, &data, regs))
5488 ret = HRTIMER_NORESTART;
5491 period = max_t(u64, 10000, event->hw.sample_period);
5492 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5497 static void perf_swevent_start_hrtimer(struct perf_event *event)
5499 struct hw_perf_event *hwc = &event->hw;
5502 if (!is_sampling_event(event))
5505 period = local64_read(&hwc->period_left);
5510 local64_set(&hwc->period_left, 0);
5512 period = max_t(u64, 10000, hwc->sample_period);
5514 __hrtimer_start_range_ns(&hwc->hrtimer,
5515 ns_to_ktime(period), 0,
5516 HRTIMER_MODE_REL_PINNED, 0);
5519 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5521 struct hw_perf_event *hwc = &event->hw;
5523 if (is_sampling_event(event)) {
5524 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5525 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5527 hrtimer_cancel(&hwc->hrtimer);
5531 static void perf_swevent_init_hrtimer(struct perf_event *event)
5533 struct hw_perf_event *hwc = &event->hw;
5535 if (!is_sampling_event(event))
5538 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5539 hwc->hrtimer.function = perf_swevent_hrtimer;
5542 * Since hrtimers have a fixed rate, we can do a static freq->period
5543 * mapping and avoid the whole period adjust feedback stuff.
5545 if (event->attr.freq) {
5546 long freq = event->attr.sample_freq;
5548 event->attr.sample_period = NSEC_PER_SEC / freq;
5549 hwc->sample_period = event->attr.sample_period;
5550 local64_set(&hwc->period_left, hwc->sample_period);
5551 event->attr.freq = 0;
5556 * Software event: cpu wall time clock
5559 static void cpu_clock_event_update(struct perf_event *event)
5564 now = local_clock();
5565 prev = local64_xchg(&event->hw.prev_count, now);
5566 local64_add(now - prev, &event->count);
5569 static void cpu_clock_event_start(struct perf_event *event, int flags)
5571 local64_set(&event->hw.prev_count, local_clock());
5572 perf_swevent_start_hrtimer(event);
5575 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5577 perf_swevent_cancel_hrtimer(event);
5578 cpu_clock_event_update(event);
5581 static int cpu_clock_event_add(struct perf_event *event, int flags)
5583 if (flags & PERF_EF_START)
5584 cpu_clock_event_start(event, flags);
5589 static void cpu_clock_event_del(struct perf_event *event, int flags)
5591 cpu_clock_event_stop(event, flags);
5594 static void cpu_clock_event_read(struct perf_event *event)
5596 cpu_clock_event_update(event);
5599 static int cpu_clock_event_init(struct perf_event *event)
5601 if (event->attr.type != PERF_TYPE_SOFTWARE)
5604 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5607 perf_swevent_init_hrtimer(event);
5612 static struct pmu perf_cpu_clock = {
5613 .task_ctx_nr = perf_sw_context,
5615 .event_init = cpu_clock_event_init,
5616 .add = cpu_clock_event_add,
5617 .del = cpu_clock_event_del,
5618 .start = cpu_clock_event_start,
5619 .stop = cpu_clock_event_stop,
5620 .read = cpu_clock_event_read,
5624 * Software event: task time clock
5627 static void task_clock_event_update(struct perf_event *event, u64 now)
5632 prev = local64_xchg(&event->hw.prev_count, now);
5634 local64_add(delta, &event->count);
5637 static void task_clock_event_start(struct perf_event *event, int flags)
5639 local64_set(&event->hw.prev_count, event->ctx->time);
5640 perf_swevent_start_hrtimer(event);
5643 static void task_clock_event_stop(struct perf_event *event, int flags)
5645 perf_swevent_cancel_hrtimer(event);
5646 task_clock_event_update(event, event->ctx->time);
5649 static int task_clock_event_add(struct perf_event *event, int flags)
5651 if (flags & PERF_EF_START)
5652 task_clock_event_start(event, flags);
5657 static void task_clock_event_del(struct perf_event *event, int flags)
5659 task_clock_event_stop(event, PERF_EF_UPDATE);
5662 static void task_clock_event_read(struct perf_event *event)
5664 u64 now = perf_clock();
5665 u64 delta = now - event->ctx->timestamp;
5666 u64 time = event->ctx->time + delta;
5668 task_clock_event_update(event, time);
5671 static int task_clock_event_init(struct perf_event *event)
5673 if (event->attr.type != PERF_TYPE_SOFTWARE)
5676 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5679 perf_swevent_init_hrtimer(event);
5684 static struct pmu perf_task_clock = {
5685 .task_ctx_nr = perf_sw_context,
5687 .event_init = task_clock_event_init,
5688 .add = task_clock_event_add,
5689 .del = task_clock_event_del,
5690 .start = task_clock_event_start,
5691 .stop = task_clock_event_stop,
5692 .read = task_clock_event_read,
5695 static void perf_pmu_nop_void(struct pmu *pmu)
5699 static int perf_pmu_nop_int(struct pmu *pmu)
5704 static void perf_pmu_start_txn(struct pmu *pmu)
5706 perf_pmu_disable(pmu);
5709 static int perf_pmu_commit_txn(struct pmu *pmu)
5711 perf_pmu_enable(pmu);
5715 static void perf_pmu_cancel_txn(struct pmu *pmu)
5717 perf_pmu_enable(pmu);
5721 * Ensures all contexts with the same task_ctx_nr have the same
5722 * pmu_cpu_context too.
5724 static void *find_pmu_context(int ctxn)
5731 list_for_each_entry(pmu, &pmus, entry) {
5732 if (pmu->task_ctx_nr == ctxn)
5733 return pmu->pmu_cpu_context;
5739 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5743 for_each_possible_cpu(cpu) {
5744 struct perf_cpu_context *cpuctx;
5746 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5748 if (cpuctx->active_pmu == old_pmu)
5749 cpuctx->active_pmu = pmu;
5753 static void free_pmu_context(struct pmu *pmu)
5757 mutex_lock(&pmus_lock);
5759 * Like a real lame refcount.
5761 list_for_each_entry(i, &pmus, entry) {
5762 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5763 update_pmu_context(i, pmu);
5768 free_percpu(pmu->pmu_cpu_context);
5770 mutex_unlock(&pmus_lock);
5772 static struct idr pmu_idr;
5775 type_show(struct device *dev, struct device_attribute *attr, char *page)
5777 struct pmu *pmu = dev_get_drvdata(dev);
5779 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5782 static struct device_attribute pmu_dev_attrs[] = {
5787 static int pmu_bus_running;
5788 static struct bus_type pmu_bus = {
5789 .name = "event_source",
5790 .dev_attrs = pmu_dev_attrs,
5793 static void pmu_dev_release(struct device *dev)
5798 static int pmu_dev_alloc(struct pmu *pmu)
5802 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5806 device_initialize(pmu->dev);
5807 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5811 dev_set_drvdata(pmu->dev, pmu);
5812 pmu->dev->bus = &pmu_bus;
5813 pmu->dev->release = pmu_dev_release;
5814 ret = device_add(pmu->dev);
5822 put_device(pmu->dev);
5826 static struct lock_class_key cpuctx_mutex;
5827 static struct lock_class_key cpuctx_lock;
5829 int perf_pmu_register(struct pmu *pmu, char *name, int type)
5833 mutex_lock(&pmus_lock);
5835 pmu->pmu_disable_count = alloc_percpu(int);
5836 if (!pmu->pmu_disable_count)
5845 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5849 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5857 if (pmu_bus_running) {
5858 ret = pmu_dev_alloc(pmu);
5864 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5865 if (pmu->pmu_cpu_context)
5866 goto got_cpu_context;
5869 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5870 if (!pmu->pmu_cpu_context)
5873 for_each_possible_cpu(cpu) {
5874 struct perf_cpu_context *cpuctx;
5876 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5877 __perf_event_init_context(&cpuctx->ctx);
5878 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
5879 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
5880 cpuctx->ctx.type = cpu_context;
5881 cpuctx->ctx.pmu = pmu;
5882 cpuctx->jiffies_interval = 1;
5883 INIT_LIST_HEAD(&cpuctx->rotation_list);
5884 cpuctx->active_pmu = pmu;
5888 if (!pmu->start_txn) {
5889 if (pmu->pmu_enable) {
5891 * If we have pmu_enable/pmu_disable calls, install
5892 * transaction stubs that use that to try and batch
5893 * hardware accesses.
5895 pmu->start_txn = perf_pmu_start_txn;
5896 pmu->commit_txn = perf_pmu_commit_txn;
5897 pmu->cancel_txn = perf_pmu_cancel_txn;
5899 pmu->start_txn = perf_pmu_nop_void;
5900 pmu->commit_txn = perf_pmu_nop_int;
5901 pmu->cancel_txn = perf_pmu_nop_void;
5905 if (!pmu->pmu_enable) {
5906 pmu->pmu_enable = perf_pmu_nop_void;
5907 pmu->pmu_disable = perf_pmu_nop_void;
5910 list_add_rcu(&pmu->entry, &pmus);
5913 mutex_unlock(&pmus_lock);
5918 device_del(pmu->dev);
5919 put_device(pmu->dev);
5922 if (pmu->type >= PERF_TYPE_MAX)
5923 idr_remove(&pmu_idr, pmu->type);
5926 free_percpu(pmu->pmu_disable_count);
5930 void perf_pmu_unregister(struct pmu *pmu)
5932 mutex_lock(&pmus_lock);
5933 list_del_rcu(&pmu->entry);
5934 mutex_unlock(&pmus_lock);
5937 * We dereference the pmu list under both SRCU and regular RCU, so
5938 * synchronize against both of those.
5940 synchronize_srcu(&pmus_srcu);
5943 free_percpu(pmu->pmu_disable_count);
5944 if (pmu->type >= PERF_TYPE_MAX)
5945 idr_remove(&pmu_idr, pmu->type);
5946 device_del(pmu->dev);
5947 put_device(pmu->dev);
5948 free_pmu_context(pmu);
5951 struct pmu *perf_init_event(struct perf_event *event)
5953 struct pmu *pmu = NULL;
5957 idx = srcu_read_lock(&pmus_srcu);
5960 pmu = idr_find(&pmu_idr, event->attr.type);
5964 ret = pmu->event_init(event);
5970 list_for_each_entry_rcu(pmu, &pmus, entry) {
5972 ret = pmu->event_init(event);
5976 if (ret != -ENOENT) {
5981 pmu = ERR_PTR(-ENOENT);
5983 srcu_read_unlock(&pmus_srcu, idx);
5989 * Allocate and initialize a event structure
5991 static struct perf_event *
5992 perf_event_alloc(struct perf_event_attr *attr, int cpu,
5993 struct task_struct *task,
5994 struct perf_event *group_leader,
5995 struct perf_event *parent_event,
5996 perf_overflow_handler_t overflow_handler,
6000 struct perf_event *event;
6001 struct hw_perf_event *hwc;
6004 if ((unsigned)cpu >= nr_cpu_ids) {
6005 if (!task || cpu != -1)
6006 return ERR_PTR(-EINVAL);
6009 event = kzalloc(sizeof(*event), GFP_KERNEL);
6011 return ERR_PTR(-ENOMEM);
6014 * Single events are their own group leaders, with an
6015 * empty sibling list:
6018 group_leader = event;
6020 mutex_init(&event->child_mutex);
6021 INIT_LIST_HEAD(&event->child_list);
6023 INIT_LIST_HEAD(&event->group_entry);
6024 INIT_LIST_HEAD(&event->event_entry);
6025 INIT_LIST_HEAD(&event->sibling_list);
6026 INIT_LIST_HEAD(&event->rb_entry);
6028 init_waitqueue_head(&event->waitq);
6029 init_irq_work(&event->pending, perf_pending_event);
6031 mutex_init(&event->mmap_mutex);
6033 atomic_long_set(&event->refcount, 1);
6035 event->attr = *attr;
6036 event->group_leader = group_leader;
6040 event->parent = parent_event;
6042 event->ns = get_pid_ns(current->nsproxy->pid_ns);
6043 event->id = atomic64_inc_return(&perf_event_id);
6045 event->state = PERF_EVENT_STATE_INACTIVE;
6048 event->attach_state = PERF_ATTACH_TASK;
6049 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6051 * hw_breakpoint is a bit difficult here..
6053 if (attr->type == PERF_TYPE_BREAKPOINT)
6054 event->hw.bp_target = task;
6058 if (!overflow_handler && parent_event) {
6059 overflow_handler = parent_event->overflow_handler;
6060 context = parent_event->overflow_handler_context;
6063 event->overflow_handler = overflow_handler;
6064 event->overflow_handler_context = context;
6066 perf_event__state_init(event);
6071 hwc->sample_period = attr->sample_period;
6072 if (attr->freq && attr->sample_freq)
6073 hwc->sample_period = 1;
6074 hwc->last_period = hwc->sample_period;
6076 local64_set(&hwc->period_left, hwc->sample_period);
6079 * we currently do not support PERF_FORMAT_GROUP on inherited events
6081 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6084 pmu = perf_init_event(event);
6090 else if (IS_ERR(pmu))
6095 put_pid_ns(event->ns);
6097 return ERR_PTR(err);
6100 if (!event->parent) {
6101 if (event->attach_state & PERF_ATTACH_TASK)
6102 jump_label_inc(&perf_sched_events);
6103 if (event->attr.mmap || event->attr.mmap_data)
6104 atomic_inc(&nr_mmap_events);
6105 if (event->attr.comm)
6106 atomic_inc(&nr_comm_events);
6107 if (event->attr.task)
6108 atomic_inc(&nr_task_events);
6109 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6110 err = get_callchain_buffers();
6113 return ERR_PTR(err);
6121 static int perf_copy_attr(struct perf_event_attr __user *uattr,
6122 struct perf_event_attr *attr)
6127 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6131 * zero the full structure, so that a short copy will be nice.
6133 memset(attr, 0, sizeof(*attr));
6135 ret = get_user(size, &uattr->size);
6139 if (size > PAGE_SIZE) /* silly large */
6142 if (!size) /* abi compat */
6143 size = PERF_ATTR_SIZE_VER0;
6145 if (size < PERF_ATTR_SIZE_VER0)
6149 * If we're handed a bigger struct than we know of,
6150 * ensure all the unknown bits are 0 - i.e. new
6151 * user-space does not rely on any kernel feature
6152 * extensions we dont know about yet.
6154 if (size > sizeof(*attr)) {
6155 unsigned char __user *addr;
6156 unsigned char __user *end;
6159 addr = (void __user *)uattr + sizeof(*attr);
6160 end = (void __user *)uattr + size;
6162 for (; addr < end; addr++) {
6163 ret = get_user(val, addr);
6169 size = sizeof(*attr);
6172 ret = copy_from_user(attr, uattr, size);
6176 if (attr->__reserved_1)
6179 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6182 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6189 put_user(sizeof(*attr), &uattr->size);
6195 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6197 struct ring_buffer *rb = NULL, *old_rb = NULL;
6203 /* don't allow circular references */
6204 if (event == output_event)
6208 * Don't allow cross-cpu buffers
6210 if (output_event->cpu != event->cpu)
6214 * If its not a per-cpu rb, it must be the same task.
6216 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6220 mutex_lock(&event->mmap_mutex);
6221 /* Can't redirect output if we've got an active mmap() */
6222 if (atomic_read(&event->mmap_count))
6228 /* get the rb we want to redirect to */
6229 rb = ring_buffer_get(output_event);
6235 ring_buffer_detach(event, old_rb);
6238 ring_buffer_attach(event, rb);
6240 rcu_assign_pointer(event->rb, rb);
6243 ring_buffer_put(old_rb);
6245 * Since we detached before setting the new rb, so that we
6246 * could attach the new rb, we could have missed a wakeup.
6249 wake_up_all(&event->waitq);
6254 mutex_unlock(&event->mmap_mutex);
6261 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6263 * @attr_uptr: event_id type attributes for monitoring/sampling
6266 * @group_fd: group leader event fd
6268 SYSCALL_DEFINE5(perf_event_open,
6269 struct perf_event_attr __user *, attr_uptr,
6270 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6272 struct perf_event *group_leader = NULL, *output_event = NULL;
6273 struct perf_event *event, *sibling;
6274 struct perf_event_attr attr;
6275 struct perf_event_context *ctx;
6276 struct file *event_file = NULL;
6277 struct file *group_file = NULL;
6278 struct task_struct *task = NULL;
6282 int fput_needed = 0;
6285 /* for future expandability... */
6286 if (flags & ~PERF_FLAG_ALL)
6289 err = perf_copy_attr(attr_uptr, &attr);
6293 if (!attr.exclude_kernel) {
6294 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6299 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6304 * In cgroup mode, the pid argument is used to pass the fd
6305 * opened to the cgroup directory in cgroupfs. The cpu argument
6306 * designates the cpu on which to monitor threads from that
6309 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6312 event_fd = get_unused_fd_flags(O_RDWR);
6316 if (group_fd != -1) {
6317 group_file = perf_fget_light(group_fd, &fput_needed);
6318 if (IS_ERR(group_file)) {
6319 err = PTR_ERR(group_file);
6322 group_leader = group_file->private_data;
6323 if (flags & PERF_FLAG_FD_OUTPUT)
6324 output_event = group_leader;
6325 if (flags & PERF_FLAG_FD_NO_GROUP)
6326 group_leader = NULL;
6329 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
6330 task = find_lively_task_by_vpid(pid);
6332 err = PTR_ERR(task);
6337 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
6339 if (IS_ERR(event)) {
6340 err = PTR_ERR(event);
6344 if (flags & PERF_FLAG_PID_CGROUP) {
6345 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6350 * - that has cgroup constraint on event->cpu
6351 * - that may need work on context switch
6353 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6354 jump_label_inc(&perf_sched_events);
6358 * Special case software events and allow them to be part of
6359 * any hardware group.
6364 (is_software_event(event) != is_software_event(group_leader))) {
6365 if (is_software_event(event)) {
6367 * If event and group_leader are not both a software
6368 * event, and event is, then group leader is not.
6370 * Allow the addition of software events to !software
6371 * groups, this is safe because software events never
6374 pmu = group_leader->pmu;
6375 } else if (is_software_event(group_leader) &&
6376 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6378 * In case the group is a pure software group, and we
6379 * try to add a hardware event, move the whole group to
6380 * the hardware context.
6387 * Get the target context (task or percpu):
6389 ctx = find_get_context(pmu, task, cpu);
6396 put_task_struct(task);
6401 * Look up the group leader (we will attach this event to it):
6407 * Do not allow a recursive hierarchy (this new sibling
6408 * becoming part of another group-sibling):
6410 if (group_leader->group_leader != group_leader)
6413 * Do not allow to attach to a group in a different
6414 * task or CPU context:
6417 if (group_leader->ctx->type != ctx->type)
6420 if (group_leader->ctx != ctx)
6425 * Only a group leader can be exclusive or pinned
6427 if (attr.exclusive || attr.pinned)
6432 err = perf_event_set_output(event, output_event);
6437 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6438 if (IS_ERR(event_file)) {
6439 err = PTR_ERR(event_file);
6444 struct perf_event_context *gctx = group_leader->ctx;
6446 mutex_lock(&gctx->mutex);
6447 perf_remove_from_context(group_leader);
6450 * Removing from the context ends up with disabled
6451 * event. What we want here is event in the initial
6452 * startup state, ready to be add into new context.
6454 perf_event__state_init(group_leader);
6455 list_for_each_entry(sibling, &group_leader->sibling_list,
6457 perf_remove_from_context(sibling);
6458 perf_event__state_init(sibling);
6461 mutex_unlock(&gctx->mutex);
6465 WARN_ON_ONCE(ctx->parent_ctx);
6466 mutex_lock(&ctx->mutex);
6469 perf_install_in_context(ctx, group_leader, cpu);
6471 list_for_each_entry(sibling, &group_leader->sibling_list,
6473 perf_install_in_context(ctx, sibling, cpu);
6478 perf_install_in_context(ctx, event, cpu);
6480 perf_unpin_context(ctx);
6481 mutex_unlock(&ctx->mutex);
6483 event->owner = current;
6485 mutex_lock(¤t->perf_event_mutex);
6486 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6487 mutex_unlock(¤t->perf_event_mutex);
6490 * Precalculate sample_data sizes
6492 perf_event__header_size(event);
6493 perf_event__id_header_size(event);
6496 * Drop the reference on the group_event after placing the
6497 * new event on the sibling_list. This ensures destruction
6498 * of the group leader will find the pointer to itself in
6499 * perf_group_detach().
6501 fput_light(group_file, fput_needed);
6502 fd_install(event_fd, event_file);
6506 perf_unpin_context(ctx);
6512 put_task_struct(task);
6514 fput_light(group_file, fput_needed);
6516 put_unused_fd(event_fd);
6521 * perf_event_create_kernel_counter
6523 * @attr: attributes of the counter to create
6524 * @cpu: cpu in which the counter is bound
6525 * @task: task to profile (NULL for percpu)
6528 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6529 struct task_struct *task,
6530 perf_overflow_handler_t overflow_handler,
6533 struct perf_event_context *ctx;
6534 struct perf_event *event;
6538 * Get the target context (task or percpu):
6541 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
6542 overflow_handler, context);
6543 if (IS_ERR(event)) {
6544 err = PTR_ERR(event);
6548 ctx = find_get_context(event->pmu, task, cpu);
6554 WARN_ON_ONCE(ctx->parent_ctx);
6555 mutex_lock(&ctx->mutex);
6556 perf_install_in_context(ctx, event, cpu);
6558 perf_unpin_context(ctx);
6559 mutex_unlock(&ctx->mutex);
6566 return ERR_PTR(err);
6568 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6570 static void sync_child_event(struct perf_event *child_event,
6571 struct task_struct *child)
6573 struct perf_event *parent_event = child_event->parent;
6576 if (child_event->attr.inherit_stat)
6577 perf_event_read_event(child_event, child);
6579 child_val = perf_event_count(child_event);
6582 * Add back the child's count to the parent's count:
6584 atomic64_add(child_val, &parent_event->child_count);
6585 atomic64_add(child_event->total_time_enabled,
6586 &parent_event->child_total_time_enabled);
6587 atomic64_add(child_event->total_time_running,
6588 &parent_event->child_total_time_running);
6591 * Remove this event from the parent's list
6593 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6594 mutex_lock(&parent_event->child_mutex);
6595 list_del_init(&child_event->child_list);
6596 mutex_unlock(&parent_event->child_mutex);
6599 * Release the parent event, if this was the last
6602 put_event(parent_event);
6606 __perf_event_exit_task(struct perf_event *child_event,
6607 struct perf_event_context *child_ctx,
6608 struct task_struct *child)
6610 if (child_event->parent) {
6611 raw_spin_lock_irq(&child_ctx->lock);
6612 perf_group_detach(child_event);
6613 raw_spin_unlock_irq(&child_ctx->lock);
6616 perf_remove_from_context(child_event);
6619 * It can happen that the parent exits first, and has events
6620 * that are still around due to the child reference. These
6621 * events need to be zapped.
6623 if (child_event->parent) {
6624 sync_child_event(child_event, child);
6625 free_event(child_event);
6629 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6631 struct perf_event *child_event, *tmp;
6632 struct perf_event_context *child_ctx;
6633 unsigned long flags;
6635 if (likely(!child->perf_event_ctxp[ctxn])) {
6636 perf_event_task(child, NULL, 0);
6640 local_irq_save(flags);
6642 * We can't reschedule here because interrupts are disabled,
6643 * and either child is current or it is a task that can't be
6644 * scheduled, so we are now safe from rescheduling changing
6647 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6650 * Take the context lock here so that if find_get_context is
6651 * reading child->perf_event_ctxp, we wait until it has
6652 * incremented the context's refcount before we do put_ctx below.
6654 raw_spin_lock(&child_ctx->lock);
6655 task_ctx_sched_out(child_ctx);
6656 child->perf_event_ctxp[ctxn] = NULL;
6658 * If this context is a clone; unclone it so it can't get
6659 * swapped to another process while we're removing all
6660 * the events from it.
6662 unclone_ctx(child_ctx);
6663 update_context_time(child_ctx);
6664 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6667 * Report the task dead after unscheduling the events so that we
6668 * won't get any samples after PERF_RECORD_EXIT. We can however still
6669 * get a few PERF_RECORD_READ events.
6671 perf_event_task(child, child_ctx, 0);
6674 * We can recurse on the same lock type through:
6676 * __perf_event_exit_task()
6677 * sync_child_event()
6679 * mutex_lock(&ctx->mutex)
6681 * But since its the parent context it won't be the same instance.
6683 mutex_lock(&child_ctx->mutex);
6686 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6688 __perf_event_exit_task(child_event, child_ctx, child);
6690 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6692 __perf_event_exit_task(child_event, child_ctx, child);
6695 * If the last event was a group event, it will have appended all
6696 * its siblings to the list, but we obtained 'tmp' before that which
6697 * will still point to the list head terminating the iteration.
6699 if (!list_empty(&child_ctx->pinned_groups) ||
6700 !list_empty(&child_ctx->flexible_groups))
6703 mutex_unlock(&child_ctx->mutex);
6709 * When a child task exits, feed back event values to parent events.
6711 void perf_event_exit_task(struct task_struct *child)
6713 struct perf_event *event, *tmp;
6716 mutex_lock(&child->perf_event_mutex);
6717 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6719 list_del_init(&event->owner_entry);
6722 * Ensure the list deletion is visible before we clear
6723 * the owner, closes a race against perf_release() where
6724 * we need to serialize on the owner->perf_event_mutex.
6727 event->owner = NULL;
6729 mutex_unlock(&child->perf_event_mutex);
6731 for_each_task_context_nr(ctxn)
6732 perf_event_exit_task_context(child, ctxn);
6735 static void perf_free_event(struct perf_event *event,
6736 struct perf_event_context *ctx)
6738 struct perf_event *parent = event->parent;
6740 if (WARN_ON_ONCE(!parent))
6743 mutex_lock(&parent->child_mutex);
6744 list_del_init(&event->child_list);
6745 mutex_unlock(&parent->child_mutex);
6749 perf_group_detach(event);
6750 list_del_event(event, ctx);
6755 * free an unexposed, unused context as created by inheritance by
6756 * perf_event_init_task below, used by fork() in case of fail.
6758 void perf_event_free_task(struct task_struct *task)
6760 struct perf_event_context *ctx;
6761 struct perf_event *event, *tmp;
6764 for_each_task_context_nr(ctxn) {
6765 ctx = task->perf_event_ctxp[ctxn];
6769 mutex_lock(&ctx->mutex);
6771 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6773 perf_free_event(event, ctx);
6775 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6777 perf_free_event(event, ctx);
6779 if (!list_empty(&ctx->pinned_groups) ||
6780 !list_empty(&ctx->flexible_groups))
6783 mutex_unlock(&ctx->mutex);
6789 void perf_event_delayed_put(struct task_struct *task)
6793 for_each_task_context_nr(ctxn)
6794 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6798 * inherit a event from parent task to child task:
6800 static struct perf_event *
6801 inherit_event(struct perf_event *parent_event,
6802 struct task_struct *parent,
6803 struct perf_event_context *parent_ctx,
6804 struct task_struct *child,
6805 struct perf_event *group_leader,
6806 struct perf_event_context *child_ctx)
6808 struct perf_event *child_event;
6809 unsigned long flags;
6812 * Instead of creating recursive hierarchies of events,
6813 * we link inherited events back to the original parent,
6814 * which has a filp for sure, which we use as the reference
6817 if (parent_event->parent)
6818 parent_event = parent_event->parent;
6820 child_event = perf_event_alloc(&parent_event->attr,
6823 group_leader, parent_event,
6825 if (IS_ERR(child_event))
6828 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
6829 free_event(child_event);
6836 * Make the child state follow the state of the parent event,
6837 * not its attr.disabled bit. We hold the parent's mutex,
6838 * so we won't race with perf_event_{en, dis}able_family.
6840 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6841 child_event->state = PERF_EVENT_STATE_INACTIVE;
6843 child_event->state = PERF_EVENT_STATE_OFF;
6845 if (parent_event->attr.freq) {
6846 u64 sample_period = parent_event->hw.sample_period;
6847 struct hw_perf_event *hwc = &child_event->hw;
6849 hwc->sample_period = sample_period;
6850 hwc->last_period = sample_period;
6852 local64_set(&hwc->period_left, sample_period);
6855 child_event->ctx = child_ctx;
6856 child_event->overflow_handler = parent_event->overflow_handler;
6857 child_event->overflow_handler_context
6858 = parent_event->overflow_handler_context;
6861 * Precalculate sample_data sizes
6863 perf_event__header_size(child_event);
6864 perf_event__id_header_size(child_event);
6867 * Link it up in the child's context:
6869 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6870 add_event_to_ctx(child_event, child_ctx);
6871 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6874 * Link this into the parent event's child list
6876 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6877 mutex_lock(&parent_event->child_mutex);
6878 list_add_tail(&child_event->child_list, &parent_event->child_list);
6879 mutex_unlock(&parent_event->child_mutex);
6884 static int inherit_group(struct perf_event *parent_event,
6885 struct task_struct *parent,
6886 struct perf_event_context *parent_ctx,
6887 struct task_struct *child,
6888 struct perf_event_context *child_ctx)
6890 struct perf_event *leader;
6891 struct perf_event *sub;
6892 struct perf_event *child_ctr;
6894 leader = inherit_event(parent_event, parent, parent_ctx,
6895 child, NULL, child_ctx);
6897 return PTR_ERR(leader);
6898 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6899 child_ctr = inherit_event(sub, parent, parent_ctx,
6900 child, leader, child_ctx);
6901 if (IS_ERR(child_ctr))
6902 return PTR_ERR(child_ctr);
6908 inherit_task_group(struct perf_event *event, struct task_struct *parent,
6909 struct perf_event_context *parent_ctx,
6910 struct task_struct *child, int ctxn,
6914 struct perf_event_context *child_ctx;
6916 if (!event->attr.inherit) {
6921 child_ctx = child->perf_event_ctxp[ctxn];
6924 * This is executed from the parent task context, so
6925 * inherit events that have been marked for cloning.
6926 * First allocate and initialize a context for the
6930 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
6934 child->perf_event_ctxp[ctxn] = child_ctx;
6937 ret = inherit_group(event, parent, parent_ctx,
6947 * Initialize the perf_event context in task_struct
6949 int perf_event_init_context(struct task_struct *child, int ctxn)
6951 struct perf_event_context *child_ctx, *parent_ctx;
6952 struct perf_event_context *cloned_ctx;
6953 struct perf_event *event;
6954 struct task_struct *parent = current;
6955 int inherited_all = 1;
6956 unsigned long flags;
6959 if (likely(!parent->perf_event_ctxp[ctxn]))
6963 * If the parent's context is a clone, pin it so it won't get
6966 parent_ctx = perf_pin_task_context(parent, ctxn);
6969 * No need to check if parent_ctx != NULL here; since we saw
6970 * it non-NULL earlier, the only reason for it to become NULL
6971 * is if we exit, and since we're currently in the middle of
6972 * a fork we can't be exiting at the same time.
6976 * Lock the parent list. No need to lock the child - not PID
6977 * hashed yet and not running, so nobody can access it.
6979 mutex_lock(&parent_ctx->mutex);
6982 * We dont have to disable NMIs - we are only looking at
6983 * the list, not manipulating it:
6985 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
6986 ret = inherit_task_group(event, parent, parent_ctx,
6987 child, ctxn, &inherited_all);
6993 * We can't hold ctx->lock when iterating the ->flexible_group list due
6994 * to allocations, but we need to prevent rotation because
6995 * rotate_ctx() will change the list from interrupt context.
6997 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6998 parent_ctx->rotate_disable = 1;
6999 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7001 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
7002 ret = inherit_task_group(event, parent, parent_ctx,
7003 child, ctxn, &inherited_all);
7008 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7009 parent_ctx->rotate_disable = 0;
7011 child_ctx = child->perf_event_ctxp[ctxn];
7013 if (child_ctx && inherited_all) {
7015 * Mark the child context as a clone of the parent
7016 * context, or of whatever the parent is a clone of.
7018 * Note that if the parent is a clone, the holding of
7019 * parent_ctx->lock avoids it from being uncloned.
7021 cloned_ctx = parent_ctx->parent_ctx;
7023 child_ctx->parent_ctx = cloned_ctx;
7024 child_ctx->parent_gen = parent_ctx->parent_gen;
7026 child_ctx->parent_ctx = parent_ctx;
7027 child_ctx->parent_gen = parent_ctx->generation;
7029 get_ctx(child_ctx->parent_ctx);
7032 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7033 mutex_unlock(&parent_ctx->mutex);
7035 perf_unpin_context(parent_ctx);
7036 put_ctx(parent_ctx);
7042 * Initialize the perf_event context in task_struct
7044 int perf_event_init_task(struct task_struct *child)
7048 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7049 mutex_init(&child->perf_event_mutex);
7050 INIT_LIST_HEAD(&child->perf_event_list);
7052 for_each_task_context_nr(ctxn) {
7053 ret = perf_event_init_context(child, ctxn);
7061 static void __init perf_event_init_all_cpus(void)
7063 struct swevent_htable *swhash;
7066 for_each_possible_cpu(cpu) {
7067 swhash = &per_cpu(swevent_htable, cpu);
7068 mutex_init(&swhash->hlist_mutex);
7069 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
7073 static void __cpuinit perf_event_init_cpu(int cpu)
7075 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7077 mutex_lock(&swhash->hlist_mutex);
7078 if (swhash->hlist_refcount > 0) {
7079 struct swevent_hlist *hlist;
7081 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7083 rcu_assign_pointer(swhash->swevent_hlist, hlist);
7085 mutex_unlock(&swhash->hlist_mutex);
7088 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7089 static void perf_pmu_rotate_stop(struct pmu *pmu)
7091 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7093 WARN_ON(!irqs_disabled());
7095 list_del_init(&cpuctx->rotation_list);
7098 static void __perf_event_exit_context(void *__info)
7100 struct perf_event_context *ctx = __info;
7101 struct perf_event *event, *tmp;
7103 perf_pmu_rotate_stop(ctx->pmu);
7105 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
7106 __perf_remove_from_context(event);
7107 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
7108 __perf_remove_from_context(event);
7111 static void perf_event_exit_cpu_context(int cpu)
7113 struct perf_event_context *ctx;
7117 idx = srcu_read_lock(&pmus_srcu);
7118 list_for_each_entry_rcu(pmu, &pmus, entry) {
7119 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
7121 mutex_lock(&ctx->mutex);
7122 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7123 mutex_unlock(&ctx->mutex);
7125 srcu_read_unlock(&pmus_srcu, idx);
7128 static void perf_event_exit_cpu(int cpu)
7130 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7132 mutex_lock(&swhash->hlist_mutex);
7133 swevent_hlist_release(swhash);
7134 mutex_unlock(&swhash->hlist_mutex);
7136 perf_event_exit_cpu_context(cpu);
7139 static inline void perf_event_exit_cpu(int cpu) { }
7143 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7147 for_each_online_cpu(cpu)
7148 perf_event_exit_cpu(cpu);
7154 * Run the perf reboot notifier at the very last possible moment so that
7155 * the generic watchdog code runs as long as possible.
7157 static struct notifier_block perf_reboot_notifier = {
7158 .notifier_call = perf_reboot,
7159 .priority = INT_MIN,
7162 static int __cpuinit
7163 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7165 unsigned int cpu = (long)hcpu;
7167 switch (action & ~CPU_TASKS_FROZEN) {
7169 case CPU_UP_PREPARE:
7170 case CPU_DOWN_FAILED:
7171 perf_event_init_cpu(cpu);
7174 case CPU_UP_CANCELED:
7175 case CPU_DOWN_PREPARE:
7176 perf_event_exit_cpu(cpu);
7186 void __init perf_event_init(void)
7192 perf_event_init_all_cpus();
7193 init_srcu_struct(&pmus_srcu);
7194 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7195 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7196 perf_pmu_register(&perf_task_clock, NULL, -1);
7198 perf_cpu_notifier(perf_cpu_notify);
7199 register_reboot_notifier(&perf_reboot_notifier);
7201 ret = init_hw_breakpoint();
7202 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7205 static int __init perf_event_sysfs_init(void)
7210 mutex_lock(&pmus_lock);
7212 ret = bus_register(&pmu_bus);
7216 list_for_each_entry(pmu, &pmus, entry) {
7217 if (!pmu->name || pmu->type < 0)
7220 ret = pmu_dev_alloc(pmu);
7221 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7223 pmu_bus_running = 1;
7227 mutex_unlock(&pmus_lock);
7231 device_initcall(perf_event_sysfs_init);
7233 #ifdef CONFIG_CGROUP_PERF
7234 static struct cgroup_subsys_state *perf_cgroup_create(
7235 struct cgroup_subsys *ss, struct cgroup *cont)
7237 struct perf_cgroup *jc;
7239 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
7241 return ERR_PTR(-ENOMEM);
7243 jc->info = alloc_percpu(struct perf_cgroup_info);
7246 return ERR_PTR(-ENOMEM);
7252 static void perf_cgroup_destroy(struct cgroup_subsys *ss,
7253 struct cgroup *cont)
7255 struct perf_cgroup *jc;
7256 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7257 struct perf_cgroup, css);
7258 free_percpu(jc->info);
7262 static int __perf_cgroup_move(void *info)
7264 struct task_struct *task = info;
7265 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7270 perf_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *task)
7272 task_function_call(task, __perf_cgroup_move, task);
7275 static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7276 struct cgroup *old_cgrp, struct task_struct *task)
7279 * cgroup_exit() is called in the copy_process() failure path.
7280 * Ignore this case since the task hasn't ran yet, this avoids
7281 * trying to poke a half freed task state from generic code.
7283 if (!(task->flags & PF_EXITING))
7286 perf_cgroup_attach_task(cgrp, task);
7289 struct cgroup_subsys perf_subsys = {
7290 .name = "perf_event",
7291 .subsys_id = perf_subsys_id,
7292 .create = perf_cgroup_create,
7293 .destroy = perf_cgroup_destroy,
7294 .exit = perf_cgroup_exit,
7295 .attach_task = perf_cgroup_attach_task,
7297 #endif /* CONFIG_CGROUP_PERF */