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);
363 if (cpuctx->unique_pmu != pmu)
364 continue; /* ensure we process each cpuctx once */
367 * perf_cgroup_events says at least one
368 * context on this CPU has cgroup events.
370 * ctx->nr_cgroups reports the number of cgroup
371 * events for a context.
373 if (cpuctx->ctx.nr_cgroups > 0) {
374 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
375 perf_pmu_disable(cpuctx->ctx.pmu);
377 if (mode & PERF_CGROUP_SWOUT) {
378 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
380 * must not be done before ctxswout due
381 * to event_filter_match() in event_sched_out()
386 if (mode & PERF_CGROUP_SWIN) {
387 WARN_ON_ONCE(cpuctx->cgrp);
389 * set cgrp before ctxsw in to allow
390 * event_filter_match() to not have to pass
393 cpuctx->cgrp = perf_cgroup_from_task(task);
394 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
396 perf_pmu_enable(cpuctx->ctx.pmu);
397 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
403 local_irq_restore(flags);
406 static inline void perf_cgroup_sched_out(struct task_struct *task,
407 struct task_struct *next)
409 struct perf_cgroup *cgrp1;
410 struct perf_cgroup *cgrp2 = NULL;
413 * we come here when we know perf_cgroup_events > 0
415 cgrp1 = perf_cgroup_from_task(task);
418 * next is NULL when called from perf_event_enable_on_exec()
419 * that will systematically cause a cgroup_switch()
422 cgrp2 = perf_cgroup_from_task(next);
425 * only schedule out current cgroup events if we know
426 * that we are switching to a different cgroup. Otherwise,
427 * do no touch the cgroup events.
430 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
433 static inline void perf_cgroup_sched_in(struct task_struct *prev,
434 struct task_struct *task)
436 struct perf_cgroup *cgrp1;
437 struct perf_cgroup *cgrp2 = NULL;
440 * we come here when we know perf_cgroup_events > 0
442 cgrp1 = perf_cgroup_from_task(task);
444 /* prev can never be NULL */
445 cgrp2 = perf_cgroup_from_task(prev);
448 * only need to schedule in cgroup events if we are changing
449 * cgroup during ctxsw. Cgroup events were not scheduled
450 * out of ctxsw out if that was not the case.
453 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
456 static inline int perf_cgroup_connect(int fd, struct perf_event *event,
457 struct perf_event_attr *attr,
458 struct perf_event *group_leader)
460 struct perf_cgroup *cgrp;
461 struct cgroup_subsys_state *css;
463 int ret = 0, fput_needed;
465 file = fget_light(fd, &fput_needed);
469 css = cgroup_css_from_dir(file, perf_subsys_id);
475 cgrp = container_of(css, struct perf_cgroup, css);
478 /* must be done before we fput() the file */
479 if (!perf_tryget_cgroup(event)) {
486 * all events in a group must monitor
487 * the same cgroup because a task belongs
488 * to only one perf cgroup at a time
490 if (group_leader && group_leader->cgrp != cgrp) {
491 perf_detach_cgroup(event);
495 fput_light(file, fput_needed);
500 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
502 struct perf_cgroup_info *t;
503 t = per_cpu_ptr(event->cgrp->info, event->cpu);
504 event->shadow_ctx_time = now - t->timestamp;
508 perf_cgroup_defer_enabled(struct perf_event *event)
511 * when the current task's perf cgroup does not match
512 * the event's, we need to remember to call the
513 * perf_mark_enable() function the first time a task with
514 * a matching perf cgroup is scheduled in.
516 if (is_cgroup_event(event) && !perf_cgroup_match(event))
517 event->cgrp_defer_enabled = 1;
521 perf_cgroup_mark_enabled(struct perf_event *event,
522 struct perf_event_context *ctx)
524 struct perf_event *sub;
525 u64 tstamp = perf_event_time(event);
527 if (!event->cgrp_defer_enabled)
530 event->cgrp_defer_enabled = 0;
532 event->tstamp_enabled = tstamp - event->total_time_enabled;
533 list_for_each_entry(sub, &event->sibling_list, group_entry) {
534 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
535 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
536 sub->cgrp_defer_enabled = 0;
540 #else /* !CONFIG_CGROUP_PERF */
543 perf_cgroup_match(struct perf_event *event)
548 static inline void perf_detach_cgroup(struct perf_event *event)
551 static inline int is_cgroup_event(struct perf_event *event)
556 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
561 static inline void update_cgrp_time_from_event(struct perf_event *event)
565 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
569 static inline void perf_cgroup_sched_out(struct task_struct *task,
570 struct task_struct *next)
574 static inline void perf_cgroup_sched_in(struct task_struct *prev,
575 struct task_struct *task)
579 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
580 struct perf_event_attr *attr,
581 struct perf_event *group_leader)
587 perf_cgroup_set_timestamp(struct task_struct *task,
588 struct perf_event_context *ctx)
593 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
598 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
602 static inline u64 perf_cgroup_event_time(struct perf_event *event)
608 perf_cgroup_defer_enabled(struct perf_event *event)
613 perf_cgroup_mark_enabled(struct perf_event *event,
614 struct perf_event_context *ctx)
619 void perf_pmu_disable(struct pmu *pmu)
621 int *count = this_cpu_ptr(pmu->pmu_disable_count);
623 pmu->pmu_disable(pmu);
626 void perf_pmu_enable(struct pmu *pmu)
628 int *count = this_cpu_ptr(pmu->pmu_disable_count);
630 pmu->pmu_enable(pmu);
633 static DEFINE_PER_CPU(struct list_head, rotation_list);
636 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
637 * because they're strictly cpu affine and rotate_start is called with IRQs
638 * disabled, while rotate_context is called from IRQ context.
640 static void perf_pmu_rotate_start(struct pmu *pmu)
642 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
643 struct list_head *head = &__get_cpu_var(rotation_list);
645 WARN_ON(!irqs_disabled());
647 if (list_empty(&cpuctx->rotation_list))
648 list_add(&cpuctx->rotation_list, head);
651 static void get_ctx(struct perf_event_context *ctx)
653 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
656 static void put_ctx(struct perf_event_context *ctx)
658 if (atomic_dec_and_test(&ctx->refcount)) {
660 put_ctx(ctx->parent_ctx);
662 put_task_struct(ctx->task);
663 kfree_rcu(ctx, rcu_head);
667 static void unclone_ctx(struct perf_event_context *ctx)
669 if (ctx->parent_ctx) {
670 put_ctx(ctx->parent_ctx);
671 ctx->parent_ctx = NULL;
675 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
678 * only top level events have the pid namespace they were created in
681 event = event->parent;
683 return task_tgid_nr_ns(p, event->ns);
686 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
689 * only top level events have the pid namespace they were created in
692 event = event->parent;
694 return task_pid_nr_ns(p, event->ns);
698 * If we inherit events we want to return the parent event id
701 static u64 primary_event_id(struct perf_event *event)
706 id = event->parent->id;
712 * Get the perf_event_context for a task and lock it.
713 * This has to cope with with the fact that until it is locked,
714 * the context could get moved to another task.
716 static struct perf_event_context *
717 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
719 struct perf_event_context *ctx;
723 * One of the few rules of preemptible RCU is that one cannot do
724 * rcu_read_unlock() while holding a scheduler (or nested) lock when
725 * part of the read side critical section was preemptible -- see
726 * rcu_read_unlock_special().
728 * Since ctx->lock nests under rq->lock we must ensure the entire read
729 * side critical section is non-preemptible.
733 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
736 * If this context is a clone of another, it might
737 * get swapped for another underneath us by
738 * perf_event_task_sched_out, though the
739 * rcu_read_lock() protects us from any context
740 * getting freed. Lock the context and check if it
741 * got swapped before we could get the lock, and retry
742 * if so. If we locked the right context, then it
743 * can't get swapped on us any more.
745 raw_spin_lock_irqsave(&ctx->lock, *flags);
746 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
747 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
753 if (!atomic_inc_not_zero(&ctx->refcount)) {
754 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
764 * Get the context for a task and increment its pin_count so it
765 * can't get swapped to another task. This also increments its
766 * reference count so that the context can't get freed.
768 static struct perf_event_context *
769 perf_pin_task_context(struct task_struct *task, int ctxn)
771 struct perf_event_context *ctx;
774 ctx = perf_lock_task_context(task, ctxn, &flags);
777 raw_spin_unlock_irqrestore(&ctx->lock, flags);
782 static void perf_unpin_context(struct perf_event_context *ctx)
786 raw_spin_lock_irqsave(&ctx->lock, flags);
788 raw_spin_unlock_irqrestore(&ctx->lock, flags);
792 * Update the record of the current time in a context.
794 static void update_context_time(struct perf_event_context *ctx)
796 u64 now = perf_clock();
798 ctx->time += now - ctx->timestamp;
799 ctx->timestamp = now;
802 static u64 perf_event_time(struct perf_event *event)
804 struct perf_event_context *ctx = event->ctx;
806 if (is_cgroup_event(event))
807 return perf_cgroup_event_time(event);
809 return ctx ? ctx->time : 0;
813 * Update the total_time_enabled and total_time_running fields for a event.
814 * The caller of this function needs to hold the ctx->lock.
816 static void update_event_times(struct perf_event *event)
818 struct perf_event_context *ctx = event->ctx;
821 if (event->state < PERF_EVENT_STATE_INACTIVE ||
822 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
825 * in cgroup mode, time_enabled represents
826 * the time the event was enabled AND active
827 * tasks were in the monitored cgroup. This is
828 * independent of the activity of the context as
829 * there may be a mix of cgroup and non-cgroup events.
831 * That is why we treat cgroup events differently
834 if (is_cgroup_event(event))
835 run_end = perf_event_time(event);
836 else if (ctx->is_active)
839 run_end = event->tstamp_stopped;
841 event->total_time_enabled = run_end - event->tstamp_enabled;
843 if (event->state == PERF_EVENT_STATE_INACTIVE)
844 run_end = event->tstamp_stopped;
846 run_end = perf_event_time(event);
848 event->total_time_running = run_end - event->tstamp_running;
853 * Update total_time_enabled and total_time_running for all events in a group.
855 static void update_group_times(struct perf_event *leader)
857 struct perf_event *event;
859 update_event_times(leader);
860 list_for_each_entry(event, &leader->sibling_list, group_entry)
861 update_event_times(event);
864 static struct list_head *
865 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
867 if (event->attr.pinned)
868 return &ctx->pinned_groups;
870 return &ctx->flexible_groups;
874 * Add a event from the lists for its context.
875 * Must be called with ctx->mutex and ctx->lock held.
878 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
880 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
881 event->attach_state |= PERF_ATTACH_CONTEXT;
884 * If we're a stand alone event or group leader, we go to the context
885 * list, group events are kept attached to the group so that
886 * perf_group_detach can, at all times, locate all siblings.
888 if (event->group_leader == event) {
889 struct list_head *list;
891 if (is_software_event(event))
892 event->group_flags |= PERF_GROUP_SOFTWARE;
894 list = ctx_group_list(event, ctx);
895 list_add_tail(&event->group_entry, list);
898 if (is_cgroup_event(event))
901 list_add_rcu(&event->event_entry, &ctx->event_list);
903 perf_pmu_rotate_start(ctx->pmu);
905 if (event->attr.inherit_stat)
910 * Initialize event state based on the perf_event_attr::disabled.
912 static inline void perf_event__state_init(struct perf_event *event)
914 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
915 PERF_EVENT_STATE_INACTIVE;
919 * Called at perf_event creation and when events are attached/detached from a
922 static void perf_event__read_size(struct perf_event *event)
924 int entry = sizeof(u64); /* value */
928 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
931 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
934 if (event->attr.read_format & PERF_FORMAT_ID)
935 entry += sizeof(u64);
937 if (event->attr.read_format & PERF_FORMAT_GROUP) {
938 nr += event->group_leader->nr_siblings;
943 event->read_size = size;
946 static void perf_event__header_size(struct perf_event *event)
948 struct perf_sample_data *data;
949 u64 sample_type = event->attr.sample_type;
952 perf_event__read_size(event);
954 if (sample_type & PERF_SAMPLE_IP)
955 size += sizeof(data->ip);
957 if (sample_type & PERF_SAMPLE_ADDR)
958 size += sizeof(data->addr);
960 if (sample_type & PERF_SAMPLE_PERIOD)
961 size += sizeof(data->period);
963 if (sample_type & PERF_SAMPLE_READ)
964 size += event->read_size;
966 event->header_size = size;
969 static void perf_event__id_header_size(struct perf_event *event)
971 struct perf_sample_data *data;
972 u64 sample_type = event->attr.sample_type;
975 if (sample_type & PERF_SAMPLE_TID)
976 size += sizeof(data->tid_entry);
978 if (sample_type & PERF_SAMPLE_TIME)
979 size += sizeof(data->time);
981 if (sample_type & PERF_SAMPLE_ID)
982 size += sizeof(data->id);
984 if (sample_type & PERF_SAMPLE_STREAM_ID)
985 size += sizeof(data->stream_id);
987 if (sample_type & PERF_SAMPLE_CPU)
988 size += sizeof(data->cpu_entry);
990 event->id_header_size = size;
993 static void perf_group_attach(struct perf_event *event)
995 struct perf_event *group_leader = event->group_leader, *pos;
998 * We can have double attach due to group movement in perf_event_open.
1000 if (event->attach_state & PERF_ATTACH_GROUP)
1003 event->attach_state |= PERF_ATTACH_GROUP;
1005 if (group_leader == event)
1008 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1009 !is_software_event(event))
1010 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1012 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1013 group_leader->nr_siblings++;
1015 perf_event__header_size(group_leader);
1017 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1018 perf_event__header_size(pos);
1022 * Remove a event from the lists for its context.
1023 * Must be called with ctx->mutex and ctx->lock held.
1026 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1028 struct perf_cpu_context *cpuctx;
1030 * We can have double detach due to exit/hot-unplug + close.
1032 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
1035 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1037 if (is_cgroup_event(event)) {
1039 cpuctx = __get_cpu_context(ctx);
1041 * if there are no more cgroup events
1042 * then cler cgrp to avoid stale pointer
1043 * in update_cgrp_time_from_cpuctx()
1045 if (!ctx->nr_cgroups)
1046 cpuctx->cgrp = NULL;
1050 if (event->attr.inherit_stat)
1053 list_del_rcu(&event->event_entry);
1055 if (event->group_leader == event)
1056 list_del_init(&event->group_entry);
1058 update_group_times(event);
1061 * If event was in error state, then keep it
1062 * that way, otherwise bogus counts will be
1063 * returned on read(). The only way to get out
1064 * of error state is by explicit re-enabling
1067 if (event->state > PERF_EVENT_STATE_OFF)
1068 event->state = PERF_EVENT_STATE_OFF;
1071 static void perf_group_detach(struct perf_event *event)
1073 struct perf_event *sibling, *tmp;
1074 struct list_head *list = NULL;
1077 * We can have double detach due to exit/hot-unplug + close.
1079 if (!(event->attach_state & PERF_ATTACH_GROUP))
1082 event->attach_state &= ~PERF_ATTACH_GROUP;
1085 * If this is a sibling, remove it from its group.
1087 if (event->group_leader != event) {
1088 list_del_init(&event->group_entry);
1089 event->group_leader->nr_siblings--;
1093 if (!list_empty(&event->group_entry))
1094 list = &event->group_entry;
1097 * If this was a group event with sibling events then
1098 * upgrade the siblings to singleton events by adding them
1099 * to whatever list we are on.
1101 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
1103 list_move_tail(&sibling->group_entry, list);
1104 sibling->group_leader = sibling;
1106 /* Inherit group flags from the previous leader */
1107 sibling->group_flags = event->group_flags;
1111 perf_event__header_size(event->group_leader);
1113 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1114 perf_event__header_size(tmp);
1118 event_filter_match(struct perf_event *event)
1120 return (event->cpu == -1 || event->cpu == smp_processor_id())
1121 && perf_cgroup_match(event);
1125 event_sched_out(struct perf_event *event,
1126 struct perf_cpu_context *cpuctx,
1127 struct perf_event_context *ctx)
1129 u64 tstamp = perf_event_time(event);
1132 * An event which could not be activated because of
1133 * filter mismatch still needs to have its timings
1134 * maintained, otherwise bogus information is return
1135 * via read() for time_enabled, time_running:
1137 if (event->state == PERF_EVENT_STATE_INACTIVE
1138 && !event_filter_match(event)) {
1139 delta = tstamp - event->tstamp_stopped;
1140 event->tstamp_running += delta;
1141 event->tstamp_stopped = tstamp;
1144 if (event->state != PERF_EVENT_STATE_ACTIVE)
1147 event->state = PERF_EVENT_STATE_INACTIVE;
1148 if (event->pending_disable) {
1149 event->pending_disable = 0;
1150 event->state = PERF_EVENT_STATE_OFF;
1152 event->tstamp_stopped = tstamp;
1153 event->pmu->del(event, 0);
1156 if (!is_software_event(event))
1157 cpuctx->active_oncpu--;
1159 if (event->attr.exclusive || !cpuctx->active_oncpu)
1160 cpuctx->exclusive = 0;
1164 group_sched_out(struct perf_event *group_event,
1165 struct perf_cpu_context *cpuctx,
1166 struct perf_event_context *ctx)
1168 struct perf_event *event;
1169 int state = group_event->state;
1171 event_sched_out(group_event, cpuctx, ctx);
1174 * Schedule out siblings (if any):
1176 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1177 event_sched_out(event, cpuctx, ctx);
1179 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
1180 cpuctx->exclusive = 0;
1184 * Cross CPU call to remove a performance event
1186 * We disable the event on the hardware level first. After that we
1187 * remove it from the context list.
1189 static int __perf_remove_from_context(void *info)
1191 struct perf_event *event = info;
1192 struct perf_event_context *ctx = event->ctx;
1193 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1195 raw_spin_lock(&ctx->lock);
1196 event_sched_out(event, cpuctx, ctx);
1197 list_del_event(event, ctx);
1198 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1200 cpuctx->task_ctx = NULL;
1202 raw_spin_unlock(&ctx->lock);
1209 * Remove the event from a task's (or a CPU's) list of events.
1211 * CPU events are removed with a smp call. For task events we only
1212 * call when the task is on a CPU.
1214 * If event->ctx is a cloned context, callers must make sure that
1215 * every task struct that event->ctx->task could possibly point to
1216 * remains valid. This is OK when called from perf_release since
1217 * that only calls us on the top-level context, which can't be a clone.
1218 * When called from perf_event_exit_task, it's OK because the
1219 * context has been detached from its task.
1221 static void perf_remove_from_context(struct perf_event *event)
1223 struct perf_event_context *ctx = event->ctx;
1224 struct task_struct *task = ctx->task;
1226 lockdep_assert_held(&ctx->mutex);
1230 * Per cpu events are removed via an smp call and
1231 * the removal is always successful.
1233 cpu_function_call(event->cpu, __perf_remove_from_context, event);
1238 if (!task_function_call(task, __perf_remove_from_context, event))
1241 raw_spin_lock_irq(&ctx->lock);
1243 * If we failed to find a running task, but find the context active now
1244 * that we've acquired the ctx->lock, retry.
1246 if (ctx->is_active) {
1247 raw_spin_unlock_irq(&ctx->lock);
1252 * Since the task isn't running, its safe to remove the event, us
1253 * holding the ctx->lock ensures the task won't get scheduled in.
1255 list_del_event(event, ctx);
1256 raw_spin_unlock_irq(&ctx->lock);
1260 * Cross CPU call to disable a performance event
1262 static int __perf_event_disable(void *info)
1264 struct perf_event *event = info;
1265 struct perf_event_context *ctx = event->ctx;
1266 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1269 * If this is a per-task event, need to check whether this
1270 * event's task is the current task on this cpu.
1272 * Can trigger due to concurrent perf_event_context_sched_out()
1273 * flipping contexts around.
1275 if (ctx->task && cpuctx->task_ctx != ctx)
1278 raw_spin_lock(&ctx->lock);
1281 * If the event is on, turn it off.
1282 * If it is in error state, leave it in error state.
1284 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1285 update_context_time(ctx);
1286 update_cgrp_time_from_event(event);
1287 update_group_times(event);
1288 if (event == event->group_leader)
1289 group_sched_out(event, cpuctx, ctx);
1291 event_sched_out(event, cpuctx, ctx);
1292 event->state = PERF_EVENT_STATE_OFF;
1295 raw_spin_unlock(&ctx->lock);
1303 * If event->ctx is a cloned context, callers must make sure that
1304 * every task struct that event->ctx->task could possibly point to
1305 * remains valid. This condition is satisifed when called through
1306 * perf_event_for_each_child or perf_event_for_each because they
1307 * hold the top-level event's child_mutex, so any descendant that
1308 * goes to exit will block in sync_child_event.
1309 * When called from perf_pending_event it's OK because event->ctx
1310 * is the current context on this CPU and preemption is disabled,
1311 * hence we can't get into perf_event_task_sched_out for this context.
1313 void perf_event_disable(struct perf_event *event)
1315 struct perf_event_context *ctx = event->ctx;
1316 struct task_struct *task = ctx->task;
1320 * Disable the event on the cpu that it's on
1322 cpu_function_call(event->cpu, __perf_event_disable, event);
1327 if (!task_function_call(task, __perf_event_disable, event))
1330 raw_spin_lock_irq(&ctx->lock);
1332 * If the event is still active, we need to retry the cross-call.
1334 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1335 raw_spin_unlock_irq(&ctx->lock);
1337 * Reload the task pointer, it might have been changed by
1338 * a concurrent perf_event_context_sched_out().
1345 * Since we have the lock this context can't be scheduled
1346 * in, so we can change the state safely.
1348 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1349 update_group_times(event);
1350 event->state = PERF_EVENT_STATE_OFF;
1352 raw_spin_unlock_irq(&ctx->lock);
1355 static void perf_set_shadow_time(struct perf_event *event,
1356 struct perf_event_context *ctx,
1360 * use the correct time source for the time snapshot
1362 * We could get by without this by leveraging the
1363 * fact that to get to this function, the caller
1364 * has most likely already called update_context_time()
1365 * and update_cgrp_time_xx() and thus both timestamp
1366 * are identical (or very close). Given that tstamp is,
1367 * already adjusted for cgroup, we could say that:
1368 * tstamp - ctx->timestamp
1370 * tstamp - cgrp->timestamp.
1372 * Then, in perf_output_read(), the calculation would
1373 * work with no changes because:
1374 * - event is guaranteed scheduled in
1375 * - no scheduled out in between
1376 * - thus the timestamp would be the same
1378 * But this is a bit hairy.
1380 * So instead, we have an explicit cgroup call to remain
1381 * within the time time source all along. We believe it
1382 * is cleaner and simpler to understand.
1384 if (is_cgroup_event(event))
1385 perf_cgroup_set_shadow_time(event, tstamp);
1387 event->shadow_ctx_time = tstamp - ctx->timestamp;
1390 #define MAX_INTERRUPTS (~0ULL)
1392 static void perf_log_throttle(struct perf_event *event, int enable);
1395 event_sched_in(struct perf_event *event,
1396 struct perf_cpu_context *cpuctx,
1397 struct perf_event_context *ctx)
1399 u64 tstamp = perf_event_time(event);
1401 if (event->state <= PERF_EVENT_STATE_OFF)
1404 event->state = PERF_EVENT_STATE_ACTIVE;
1405 event->oncpu = smp_processor_id();
1408 * Unthrottle events, since we scheduled we might have missed several
1409 * ticks already, also for a heavily scheduling task there is little
1410 * guarantee it'll get a tick in a timely manner.
1412 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1413 perf_log_throttle(event, 1);
1414 event->hw.interrupts = 0;
1418 * The new state must be visible before we turn it on in the hardware:
1422 if (event->pmu->add(event, PERF_EF_START)) {
1423 event->state = PERF_EVENT_STATE_INACTIVE;
1428 event->tstamp_running += tstamp - event->tstamp_stopped;
1430 perf_set_shadow_time(event, ctx, tstamp);
1432 if (!is_software_event(event))
1433 cpuctx->active_oncpu++;
1436 if (event->attr.exclusive)
1437 cpuctx->exclusive = 1;
1443 group_sched_in(struct perf_event *group_event,
1444 struct perf_cpu_context *cpuctx,
1445 struct perf_event_context *ctx)
1447 struct perf_event *event, *partial_group = NULL;
1448 struct pmu *pmu = group_event->pmu;
1449 u64 now = ctx->time;
1450 bool simulate = false;
1452 if (group_event->state == PERF_EVENT_STATE_OFF)
1455 pmu->start_txn(pmu);
1457 if (event_sched_in(group_event, cpuctx, ctx)) {
1458 pmu->cancel_txn(pmu);
1463 * Schedule in siblings as one group (if any):
1465 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1466 if (event_sched_in(event, cpuctx, ctx)) {
1467 partial_group = event;
1472 if (!pmu->commit_txn(pmu))
1477 * Groups can be scheduled in as one unit only, so undo any
1478 * partial group before returning:
1479 * The events up to the failed event are scheduled out normally,
1480 * tstamp_stopped will be updated.
1482 * The failed events and the remaining siblings need to have
1483 * their timings updated as if they had gone thru event_sched_in()
1484 * and event_sched_out(). This is required to get consistent timings
1485 * across the group. This also takes care of the case where the group
1486 * could never be scheduled by ensuring tstamp_stopped is set to mark
1487 * the time the event was actually stopped, such that time delta
1488 * calculation in update_event_times() is correct.
1490 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1491 if (event == partial_group)
1495 event->tstamp_running += now - event->tstamp_stopped;
1496 event->tstamp_stopped = now;
1498 event_sched_out(event, cpuctx, ctx);
1501 event_sched_out(group_event, cpuctx, ctx);
1503 pmu->cancel_txn(pmu);
1509 * Work out whether we can put this event group on the CPU now.
1511 static int group_can_go_on(struct perf_event *event,
1512 struct perf_cpu_context *cpuctx,
1516 * Groups consisting entirely of software events can always go on.
1518 if (event->group_flags & PERF_GROUP_SOFTWARE)
1521 * If an exclusive group is already on, no other hardware
1524 if (cpuctx->exclusive)
1527 * If this group is exclusive and there are already
1528 * events on the CPU, it can't go on.
1530 if (event->attr.exclusive && cpuctx->active_oncpu)
1533 * Otherwise, try to add it if all previous groups were able
1539 static void add_event_to_ctx(struct perf_event *event,
1540 struct perf_event_context *ctx)
1542 u64 tstamp = perf_event_time(event);
1544 list_add_event(event, ctx);
1545 perf_group_attach(event);
1546 event->tstamp_enabled = tstamp;
1547 event->tstamp_running = tstamp;
1548 event->tstamp_stopped = tstamp;
1551 static void task_ctx_sched_out(struct perf_event_context *ctx);
1553 ctx_sched_in(struct perf_event_context *ctx,
1554 struct perf_cpu_context *cpuctx,
1555 enum event_type_t event_type,
1556 struct task_struct *task);
1558 static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1559 struct perf_event_context *ctx,
1560 struct task_struct *task)
1562 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1564 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1565 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1567 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1571 * Cross CPU call to install and enable a performance event
1573 * Must be called with ctx->mutex held
1575 static int __perf_install_in_context(void *info)
1577 struct perf_event *event = info;
1578 struct perf_event_context *ctx = event->ctx;
1579 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1580 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1581 struct task_struct *task = current;
1583 perf_ctx_lock(cpuctx, task_ctx);
1584 perf_pmu_disable(cpuctx->ctx.pmu);
1587 * If there was an active task_ctx schedule it out.
1590 task_ctx_sched_out(task_ctx);
1593 * If the context we're installing events in is not the
1594 * active task_ctx, flip them.
1596 if (ctx->task && task_ctx != ctx) {
1598 raw_spin_unlock(&task_ctx->lock);
1599 raw_spin_lock(&ctx->lock);
1604 cpuctx->task_ctx = task_ctx;
1605 task = task_ctx->task;
1608 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
1610 update_context_time(ctx);
1612 * update cgrp time only if current cgrp
1613 * matches event->cgrp. Must be done before
1614 * calling add_event_to_ctx()
1616 update_cgrp_time_from_event(event);
1618 add_event_to_ctx(event, ctx);
1621 * Schedule everything back in
1623 perf_event_sched_in(cpuctx, task_ctx, task);
1625 perf_pmu_enable(cpuctx->ctx.pmu);
1626 perf_ctx_unlock(cpuctx, task_ctx);
1632 * Attach a performance event to a context
1634 * First we add the event to the list with the hardware enable bit
1635 * in event->hw_config cleared.
1637 * If the event is attached to a task which is on a CPU we use a smp
1638 * call to enable it in the task context. The task might have been
1639 * scheduled away, but we check this in the smp call again.
1642 perf_install_in_context(struct perf_event_context *ctx,
1643 struct perf_event *event,
1646 struct task_struct *task = ctx->task;
1648 lockdep_assert_held(&ctx->mutex);
1654 * Per cpu events are installed via an smp call and
1655 * the install is always successful.
1657 cpu_function_call(cpu, __perf_install_in_context, event);
1662 if (!task_function_call(task, __perf_install_in_context, event))
1665 raw_spin_lock_irq(&ctx->lock);
1667 * If we failed to find a running task, but find the context active now
1668 * that we've acquired the ctx->lock, retry.
1670 if (ctx->is_active) {
1671 raw_spin_unlock_irq(&ctx->lock);
1676 * Since the task isn't running, its safe to add the event, us holding
1677 * the ctx->lock ensures the task won't get scheduled in.
1679 add_event_to_ctx(event, ctx);
1680 raw_spin_unlock_irq(&ctx->lock);
1684 * Put a event into inactive state and update time fields.
1685 * Enabling the leader of a group effectively enables all
1686 * the group members that aren't explicitly disabled, so we
1687 * have to update their ->tstamp_enabled also.
1688 * Note: this works for group members as well as group leaders
1689 * since the non-leader members' sibling_lists will be empty.
1691 static void __perf_event_mark_enabled(struct perf_event *event,
1692 struct perf_event_context *ctx)
1694 struct perf_event *sub;
1695 u64 tstamp = perf_event_time(event);
1697 event->state = PERF_EVENT_STATE_INACTIVE;
1698 event->tstamp_enabled = tstamp - event->total_time_enabled;
1699 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1700 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1701 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1706 * Cross CPU call to enable a performance event
1708 static int __perf_event_enable(void *info)
1710 struct perf_event *event = info;
1711 struct perf_event_context *ctx = event->ctx;
1712 struct perf_event *leader = event->group_leader;
1713 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1717 * There's a time window between 'ctx->is_active' check
1718 * in perf_event_enable function and this place having:
1720 * - ctx->lock unlocked
1722 * where the task could be killed and 'ctx' deactivated
1723 * by perf_event_exit_task.
1725 if (!ctx->is_active)
1728 raw_spin_lock(&ctx->lock);
1729 update_context_time(ctx);
1731 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1735 * set current task's cgroup time reference point
1737 perf_cgroup_set_timestamp(current, ctx);
1739 __perf_event_mark_enabled(event, ctx);
1741 if (!event_filter_match(event)) {
1742 if (is_cgroup_event(event))
1743 perf_cgroup_defer_enabled(event);
1748 * If the event is in a group and isn't the group leader,
1749 * then don't put it on unless the group is on.
1751 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1754 if (!group_can_go_on(event, cpuctx, 1)) {
1757 if (event == leader)
1758 err = group_sched_in(event, cpuctx, ctx);
1760 err = event_sched_in(event, cpuctx, ctx);
1765 * If this event can't go on and it's part of a
1766 * group, then the whole group has to come off.
1768 if (leader != event)
1769 group_sched_out(leader, cpuctx, ctx);
1770 if (leader->attr.pinned) {
1771 update_group_times(leader);
1772 leader->state = PERF_EVENT_STATE_ERROR;
1777 raw_spin_unlock(&ctx->lock);
1785 * If event->ctx is a cloned context, callers must make sure that
1786 * every task struct that event->ctx->task could possibly point to
1787 * remains valid. This condition is satisfied when called through
1788 * perf_event_for_each_child or perf_event_for_each as described
1789 * for perf_event_disable.
1791 void perf_event_enable(struct perf_event *event)
1793 struct perf_event_context *ctx = event->ctx;
1794 struct task_struct *task = ctx->task;
1798 * Enable the event on the cpu that it's on
1800 cpu_function_call(event->cpu, __perf_event_enable, event);
1804 raw_spin_lock_irq(&ctx->lock);
1805 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1809 * If the event is in error state, clear that first.
1810 * That way, if we see the event in error state below, we
1811 * know that it has gone back into error state, as distinct
1812 * from the task having been scheduled away before the
1813 * cross-call arrived.
1815 if (event->state == PERF_EVENT_STATE_ERROR)
1816 event->state = PERF_EVENT_STATE_OFF;
1819 if (!ctx->is_active) {
1820 __perf_event_mark_enabled(event, ctx);
1824 raw_spin_unlock_irq(&ctx->lock);
1826 if (!task_function_call(task, __perf_event_enable, event))
1829 raw_spin_lock_irq(&ctx->lock);
1832 * If the context is active and the event is still off,
1833 * we need to retry the cross-call.
1835 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1837 * task could have been flipped by a concurrent
1838 * perf_event_context_sched_out()
1845 raw_spin_unlock_irq(&ctx->lock);
1848 int perf_event_refresh(struct perf_event *event, int refresh)
1851 * not supported on inherited events
1853 if (event->attr.inherit || !is_sampling_event(event))
1856 atomic_add(refresh, &event->event_limit);
1857 perf_event_enable(event);
1861 EXPORT_SYMBOL_GPL(perf_event_refresh);
1863 static void ctx_sched_out(struct perf_event_context *ctx,
1864 struct perf_cpu_context *cpuctx,
1865 enum event_type_t event_type)
1867 struct perf_event *event;
1868 int is_active = ctx->is_active;
1870 ctx->is_active &= ~event_type;
1871 if (likely(!ctx->nr_events))
1874 update_context_time(ctx);
1875 update_cgrp_time_from_cpuctx(cpuctx);
1876 if (!ctx->nr_active)
1879 perf_pmu_disable(ctx->pmu);
1880 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
1881 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1882 group_sched_out(event, cpuctx, ctx);
1885 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
1886 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1887 group_sched_out(event, cpuctx, ctx);
1889 perf_pmu_enable(ctx->pmu);
1893 * Test whether two contexts are equivalent, i.e. whether they
1894 * have both been cloned from the same version of the same context
1895 * and they both have the same number of enabled events.
1896 * If the number of enabled events is the same, then the set
1897 * of enabled events should be the same, because these are both
1898 * inherited contexts, therefore we can't access individual events
1899 * in them directly with an fd; we can only enable/disable all
1900 * events via prctl, or enable/disable all events in a family
1901 * via ioctl, which will have the same effect on both contexts.
1903 static int context_equiv(struct perf_event_context *ctx1,
1904 struct perf_event_context *ctx2)
1906 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1907 && ctx1->parent_gen == ctx2->parent_gen
1908 && !ctx1->pin_count && !ctx2->pin_count;
1911 static void __perf_event_sync_stat(struct perf_event *event,
1912 struct perf_event *next_event)
1916 if (!event->attr.inherit_stat)
1920 * Update the event value, we cannot use perf_event_read()
1921 * because we're in the middle of a context switch and have IRQs
1922 * disabled, which upsets smp_call_function_single(), however
1923 * we know the event must be on the current CPU, therefore we
1924 * don't need to use it.
1926 switch (event->state) {
1927 case PERF_EVENT_STATE_ACTIVE:
1928 event->pmu->read(event);
1931 case PERF_EVENT_STATE_INACTIVE:
1932 update_event_times(event);
1940 * In order to keep per-task stats reliable we need to flip the event
1941 * values when we flip the contexts.
1943 value = local64_read(&next_event->count);
1944 value = local64_xchg(&event->count, value);
1945 local64_set(&next_event->count, value);
1947 swap(event->total_time_enabled, next_event->total_time_enabled);
1948 swap(event->total_time_running, next_event->total_time_running);
1951 * Since we swizzled the values, update the user visible data too.
1953 perf_event_update_userpage(event);
1954 perf_event_update_userpage(next_event);
1957 #define list_next_entry(pos, member) \
1958 list_entry(pos->member.next, typeof(*pos), member)
1960 static void perf_event_sync_stat(struct perf_event_context *ctx,
1961 struct perf_event_context *next_ctx)
1963 struct perf_event *event, *next_event;
1968 update_context_time(ctx);
1970 event = list_first_entry(&ctx->event_list,
1971 struct perf_event, event_entry);
1973 next_event = list_first_entry(&next_ctx->event_list,
1974 struct perf_event, event_entry);
1976 while (&event->event_entry != &ctx->event_list &&
1977 &next_event->event_entry != &next_ctx->event_list) {
1979 __perf_event_sync_stat(event, next_event);
1981 event = list_next_entry(event, event_entry);
1982 next_event = list_next_entry(next_event, event_entry);
1986 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1987 struct task_struct *next)
1989 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1990 struct perf_event_context *next_ctx;
1991 struct perf_event_context *parent;
1992 struct perf_cpu_context *cpuctx;
1998 cpuctx = __get_cpu_context(ctx);
1999 if (!cpuctx->task_ctx)
2003 parent = rcu_dereference(ctx->parent_ctx);
2004 next_ctx = next->perf_event_ctxp[ctxn];
2005 if (parent && next_ctx &&
2006 rcu_dereference(next_ctx->parent_ctx) == parent) {
2008 * Looks like the two contexts are clones, so we might be
2009 * able to optimize the context switch. We lock both
2010 * contexts and check that they are clones under the
2011 * lock (including re-checking that neither has been
2012 * uncloned in the meantime). It doesn't matter which
2013 * order we take the locks because no other cpu could
2014 * be trying to lock both of these tasks.
2016 raw_spin_lock(&ctx->lock);
2017 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
2018 if (context_equiv(ctx, next_ctx)) {
2020 * XXX do we need a memory barrier of sorts
2021 * wrt to rcu_dereference() of perf_event_ctxp
2023 task->perf_event_ctxp[ctxn] = next_ctx;
2024 next->perf_event_ctxp[ctxn] = ctx;
2026 next_ctx->task = task;
2029 perf_event_sync_stat(ctx, next_ctx);
2031 raw_spin_unlock(&next_ctx->lock);
2032 raw_spin_unlock(&ctx->lock);
2037 raw_spin_lock(&ctx->lock);
2038 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2039 cpuctx->task_ctx = NULL;
2040 raw_spin_unlock(&ctx->lock);
2044 #define for_each_task_context_nr(ctxn) \
2045 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2048 * Called from scheduler to remove the events of the current task,
2049 * with interrupts disabled.
2051 * We stop each event and update the event value in event->count.
2053 * This does not protect us against NMI, but disable()
2054 * sets the disabled bit in the control field of event _before_
2055 * accessing the event control register. If a NMI hits, then it will
2056 * not restart the event.
2058 void __perf_event_task_sched_out(struct task_struct *task,
2059 struct task_struct *next)
2063 for_each_task_context_nr(ctxn)
2064 perf_event_context_sched_out(task, ctxn, next);
2067 * if cgroup events exist on this CPU, then we need
2068 * to check if we have to switch out PMU state.
2069 * cgroup event are system-wide mode only
2071 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2072 perf_cgroup_sched_out(task, next);
2075 static void task_ctx_sched_out(struct perf_event_context *ctx)
2077 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2079 if (!cpuctx->task_ctx)
2082 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2085 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2086 cpuctx->task_ctx = NULL;
2090 * Called with IRQs disabled
2092 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2093 enum event_type_t event_type)
2095 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
2099 ctx_pinned_sched_in(struct perf_event_context *ctx,
2100 struct perf_cpu_context *cpuctx)
2102 struct perf_event *event;
2104 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2105 if (event->state <= PERF_EVENT_STATE_OFF)
2107 if (!event_filter_match(event))
2110 /* may need to reset tstamp_enabled */
2111 if (is_cgroup_event(event))
2112 perf_cgroup_mark_enabled(event, ctx);
2114 if (group_can_go_on(event, cpuctx, 1))
2115 group_sched_in(event, cpuctx, ctx);
2118 * If this pinned group hasn't been scheduled,
2119 * put it in error state.
2121 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2122 update_group_times(event);
2123 event->state = PERF_EVENT_STATE_ERROR;
2129 ctx_flexible_sched_in(struct perf_event_context *ctx,
2130 struct perf_cpu_context *cpuctx)
2132 struct perf_event *event;
2135 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2136 /* Ignore events in OFF or ERROR state */
2137 if (event->state <= PERF_EVENT_STATE_OFF)
2140 * Listen to the 'cpu' scheduling filter constraint
2143 if (!event_filter_match(event))
2146 /* may need to reset tstamp_enabled */
2147 if (is_cgroup_event(event))
2148 perf_cgroup_mark_enabled(event, ctx);
2150 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2151 if (group_sched_in(event, cpuctx, ctx))
2158 ctx_sched_in(struct perf_event_context *ctx,
2159 struct perf_cpu_context *cpuctx,
2160 enum event_type_t event_type,
2161 struct task_struct *task)
2164 int is_active = ctx->is_active;
2166 ctx->is_active |= event_type;
2167 if (likely(!ctx->nr_events))
2171 ctx->timestamp = now;
2172 perf_cgroup_set_timestamp(task, ctx);
2174 * First go through the list and put on any pinned groups
2175 * in order to give them the best chance of going on.
2177 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
2178 ctx_pinned_sched_in(ctx, cpuctx);
2180 /* Then walk through the lower prio flexible groups */
2181 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
2182 ctx_flexible_sched_in(ctx, cpuctx);
2185 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2186 enum event_type_t event_type,
2187 struct task_struct *task)
2189 struct perf_event_context *ctx = &cpuctx->ctx;
2191 ctx_sched_in(ctx, cpuctx, event_type, task);
2194 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2195 struct task_struct *task)
2197 struct perf_cpu_context *cpuctx;
2199 cpuctx = __get_cpu_context(ctx);
2200 if (cpuctx->task_ctx == ctx)
2203 perf_ctx_lock(cpuctx, ctx);
2204 perf_pmu_disable(ctx->pmu);
2206 * We want to keep the following priority order:
2207 * cpu pinned (that don't need to move), task pinned,
2208 * cpu flexible, task flexible.
2210 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2213 cpuctx->task_ctx = ctx;
2215 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2217 perf_pmu_enable(ctx->pmu);
2218 perf_ctx_unlock(cpuctx, ctx);
2221 * Since these rotations are per-cpu, we need to ensure the
2222 * cpu-context we got scheduled on is actually rotating.
2224 perf_pmu_rotate_start(ctx->pmu);
2228 * Called from scheduler to add the events of the current task
2229 * with interrupts disabled.
2231 * We restore the event value and then enable it.
2233 * This does not protect us against NMI, but enable()
2234 * sets the enabled bit in the control field of event _before_
2235 * accessing the event control register. If a NMI hits, then it will
2236 * keep the event running.
2238 void __perf_event_task_sched_in(struct task_struct *prev,
2239 struct task_struct *task)
2241 struct perf_event_context *ctx;
2244 for_each_task_context_nr(ctxn) {
2245 ctx = task->perf_event_ctxp[ctxn];
2249 perf_event_context_sched_in(ctx, task);
2252 * if cgroup events exist on this CPU, then we need
2253 * to check if we have to switch in PMU state.
2254 * cgroup event are system-wide mode only
2256 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2257 perf_cgroup_sched_in(prev, task);
2260 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2262 u64 frequency = event->attr.sample_freq;
2263 u64 sec = NSEC_PER_SEC;
2264 u64 divisor, dividend;
2266 int count_fls, nsec_fls, frequency_fls, sec_fls;
2268 count_fls = fls64(count);
2269 nsec_fls = fls64(nsec);
2270 frequency_fls = fls64(frequency);
2274 * We got @count in @nsec, with a target of sample_freq HZ
2275 * the target period becomes:
2278 * period = -------------------
2279 * @nsec * sample_freq
2284 * Reduce accuracy by one bit such that @a and @b converge
2285 * to a similar magnitude.
2287 #define REDUCE_FLS(a, b) \
2289 if (a##_fls > b##_fls) { \
2299 * Reduce accuracy until either term fits in a u64, then proceed with
2300 * the other, so that finally we can do a u64/u64 division.
2302 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2303 REDUCE_FLS(nsec, frequency);
2304 REDUCE_FLS(sec, count);
2307 if (count_fls + sec_fls > 64) {
2308 divisor = nsec * frequency;
2310 while (count_fls + sec_fls > 64) {
2311 REDUCE_FLS(count, sec);
2315 dividend = count * sec;
2317 dividend = count * sec;
2319 while (nsec_fls + frequency_fls > 64) {
2320 REDUCE_FLS(nsec, frequency);
2324 divisor = nsec * frequency;
2330 return div64_u64(dividend, divisor);
2333 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
2335 struct hw_perf_event *hwc = &event->hw;
2336 s64 period, sample_period;
2339 period = perf_calculate_period(event, nsec, count);
2341 delta = (s64)(period - hwc->sample_period);
2342 delta = (delta + 7) / 8; /* low pass filter */
2344 sample_period = hwc->sample_period + delta;
2349 hwc->sample_period = sample_period;
2351 if (local64_read(&hwc->period_left) > 8*sample_period) {
2352 event->pmu->stop(event, PERF_EF_UPDATE);
2353 local64_set(&hwc->period_left, 0);
2354 event->pmu->start(event, PERF_EF_RELOAD);
2358 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
2360 struct perf_event *event;
2361 struct hw_perf_event *hwc;
2362 u64 interrupts, now;
2365 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2366 if (event->state != PERF_EVENT_STATE_ACTIVE)
2369 if (!event_filter_match(event))
2374 interrupts = hwc->interrupts;
2375 hwc->interrupts = 0;
2378 * unthrottle events on the tick
2380 if (interrupts == MAX_INTERRUPTS) {
2381 perf_log_throttle(event, 1);
2382 event->pmu->start(event, 0);
2385 if (!event->attr.freq || !event->attr.sample_freq)
2388 event->pmu->read(event);
2389 now = local64_read(&event->count);
2390 delta = now - hwc->freq_count_stamp;
2391 hwc->freq_count_stamp = now;
2394 perf_adjust_period(event, period, delta);
2399 * Round-robin a context's events:
2401 static void rotate_ctx(struct perf_event_context *ctx)
2404 * Rotate the first entry last of non-pinned groups. Rotation might be
2405 * disabled by the inheritance code.
2407 if (!ctx->rotate_disable)
2408 list_rotate_left(&ctx->flexible_groups);
2412 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2413 * because they're strictly cpu affine and rotate_start is called with IRQs
2414 * disabled, while rotate_context is called from IRQ context.
2416 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
2418 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
2419 struct perf_event_context *ctx = NULL;
2420 int rotate = 0, remove = 1;
2422 if (cpuctx->ctx.nr_events) {
2424 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2428 ctx = cpuctx->task_ctx;
2429 if (ctx && ctx->nr_events) {
2431 if (ctx->nr_events != ctx->nr_active)
2435 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2436 perf_pmu_disable(cpuctx->ctx.pmu);
2437 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
2439 perf_ctx_adjust_freq(ctx, interval);
2444 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2446 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
2448 rotate_ctx(&cpuctx->ctx);
2452 perf_event_sched_in(cpuctx, ctx, current);
2456 list_del_init(&cpuctx->rotation_list);
2458 perf_pmu_enable(cpuctx->ctx.pmu);
2459 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2462 void perf_event_task_tick(void)
2464 struct list_head *head = &__get_cpu_var(rotation_list);
2465 struct perf_cpu_context *cpuctx, *tmp;
2467 WARN_ON(!irqs_disabled());
2469 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2470 if (cpuctx->jiffies_interval == 1 ||
2471 !(jiffies % cpuctx->jiffies_interval))
2472 perf_rotate_context(cpuctx);
2476 static int event_enable_on_exec(struct perf_event *event,
2477 struct perf_event_context *ctx)
2479 if (!event->attr.enable_on_exec)
2482 event->attr.enable_on_exec = 0;
2483 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2486 __perf_event_mark_enabled(event, ctx);
2492 * Enable all of a task's events that have been marked enable-on-exec.
2493 * This expects task == current.
2495 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2497 struct perf_event *event;
2498 unsigned long flags;
2502 local_irq_save(flags);
2503 if (!ctx || !ctx->nr_events)
2507 * We must ctxsw out cgroup events to avoid conflict
2508 * when invoking perf_task_event_sched_in() later on
2509 * in this function. Otherwise we end up trying to
2510 * ctxswin cgroup events which are already scheduled
2513 perf_cgroup_sched_out(current, NULL);
2515 raw_spin_lock(&ctx->lock);
2516 task_ctx_sched_out(ctx);
2518 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2519 ret = event_enable_on_exec(event, ctx);
2524 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2525 ret = event_enable_on_exec(event, ctx);
2531 * Unclone this context if we enabled any event.
2536 raw_spin_unlock(&ctx->lock);
2539 * Also calls ctxswin for cgroup events, if any:
2541 perf_event_context_sched_in(ctx, ctx->task);
2543 local_irq_restore(flags);
2547 * Cross CPU call to read the hardware event
2549 static void __perf_event_read(void *info)
2551 struct perf_event *event = info;
2552 struct perf_event_context *ctx = event->ctx;
2553 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2556 * If this is a task context, we need to check whether it is
2557 * the current task context of this cpu. If not it has been
2558 * scheduled out before the smp call arrived. In that case
2559 * event->count would have been updated to a recent sample
2560 * when the event was scheduled out.
2562 if (ctx->task && cpuctx->task_ctx != ctx)
2565 raw_spin_lock(&ctx->lock);
2566 if (ctx->is_active) {
2567 update_context_time(ctx);
2568 update_cgrp_time_from_event(event);
2570 update_event_times(event);
2571 if (event->state == PERF_EVENT_STATE_ACTIVE)
2572 event->pmu->read(event);
2573 raw_spin_unlock(&ctx->lock);
2576 static inline u64 perf_event_count(struct perf_event *event)
2578 return local64_read(&event->count) + atomic64_read(&event->child_count);
2581 static u64 perf_event_read(struct perf_event *event)
2584 * If event is enabled and currently active on a CPU, update the
2585 * value in the event structure:
2587 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2588 smp_call_function_single(event->oncpu,
2589 __perf_event_read, event, 1);
2590 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2591 struct perf_event_context *ctx = event->ctx;
2592 unsigned long flags;
2594 raw_spin_lock_irqsave(&ctx->lock, flags);
2596 * may read while context is not active
2597 * (e.g., thread is blocked), in that case
2598 * we cannot update context time
2600 if (ctx->is_active) {
2601 update_context_time(ctx);
2602 update_cgrp_time_from_event(event);
2604 update_event_times(event);
2605 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2608 return perf_event_count(event);
2615 struct callchain_cpus_entries {
2616 struct rcu_head rcu_head;
2617 struct perf_callchain_entry *cpu_entries[0];
2620 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
2621 static atomic_t nr_callchain_events;
2622 static DEFINE_MUTEX(callchain_mutex);
2623 struct callchain_cpus_entries *callchain_cpus_entries;
2626 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
2627 struct pt_regs *regs)
2631 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
2632 struct pt_regs *regs)
2636 static void release_callchain_buffers_rcu(struct rcu_head *head)
2638 struct callchain_cpus_entries *entries;
2641 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
2643 for_each_possible_cpu(cpu)
2644 kfree(entries->cpu_entries[cpu]);
2649 static void release_callchain_buffers(void)
2651 struct callchain_cpus_entries *entries;
2653 entries = callchain_cpus_entries;
2654 rcu_assign_pointer(callchain_cpus_entries, NULL);
2655 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2658 static int alloc_callchain_buffers(void)
2662 struct callchain_cpus_entries *entries;
2665 * We can't use the percpu allocation API for data that can be
2666 * accessed from NMI. Use a temporary manual per cpu allocation
2667 * until that gets sorted out.
2669 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
2671 entries = kzalloc(size, GFP_KERNEL);
2675 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
2677 for_each_possible_cpu(cpu) {
2678 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2680 if (!entries->cpu_entries[cpu])
2684 rcu_assign_pointer(callchain_cpus_entries, entries);
2689 for_each_possible_cpu(cpu)
2690 kfree(entries->cpu_entries[cpu]);
2696 static int get_callchain_buffers(void)
2701 mutex_lock(&callchain_mutex);
2703 count = atomic_inc_return(&nr_callchain_events);
2704 if (WARN_ON_ONCE(count < 1)) {
2710 /* If the allocation failed, give up */
2711 if (!callchain_cpus_entries)
2716 err = alloc_callchain_buffers();
2718 release_callchain_buffers();
2720 mutex_unlock(&callchain_mutex);
2725 static void put_callchain_buffers(void)
2727 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2728 release_callchain_buffers();
2729 mutex_unlock(&callchain_mutex);
2733 static int get_recursion_context(int *recursion)
2741 else if (in_softirq())
2746 if (recursion[rctx])
2755 static inline void put_recursion_context(int *recursion, int rctx)
2761 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2764 struct callchain_cpus_entries *entries;
2766 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2770 entries = rcu_dereference(callchain_cpus_entries);
2774 cpu = smp_processor_id();
2776 return &entries->cpu_entries[cpu][*rctx];
2780 put_callchain_entry(int rctx)
2782 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2785 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2788 struct perf_callchain_entry *entry;
2791 entry = get_callchain_entry(&rctx);
2800 if (!user_mode(regs)) {
2801 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2802 perf_callchain_kernel(entry, regs);
2804 regs = task_pt_regs(current);
2810 perf_callchain_store(entry, PERF_CONTEXT_USER);
2811 perf_callchain_user(entry, regs);
2815 put_callchain_entry(rctx);
2821 * Initialize the perf_event context in a task_struct:
2823 static void __perf_event_init_context(struct perf_event_context *ctx)
2825 raw_spin_lock_init(&ctx->lock);
2826 mutex_init(&ctx->mutex);
2827 INIT_LIST_HEAD(&ctx->pinned_groups);
2828 INIT_LIST_HEAD(&ctx->flexible_groups);
2829 INIT_LIST_HEAD(&ctx->event_list);
2830 atomic_set(&ctx->refcount, 1);
2833 static struct perf_event_context *
2834 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2836 struct perf_event_context *ctx;
2838 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2842 __perf_event_init_context(ctx);
2845 get_task_struct(task);
2852 static struct task_struct *
2853 find_lively_task_by_vpid(pid_t vpid)
2855 struct task_struct *task;
2862 task = find_task_by_vpid(vpid);
2864 get_task_struct(task);
2868 return ERR_PTR(-ESRCH);
2870 /* Reuse ptrace permission checks for now. */
2872 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2877 put_task_struct(task);
2878 return ERR_PTR(err);
2883 * Returns a matching context with refcount and pincount.
2885 static struct perf_event_context *
2886 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2888 struct perf_event_context *ctx;
2889 struct perf_cpu_context *cpuctx;
2890 unsigned long flags;
2894 /* Must be root to operate on a CPU event: */
2895 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2896 return ERR_PTR(-EACCES);
2899 * We could be clever and allow to attach a event to an
2900 * offline CPU and activate it when the CPU comes up, but
2903 if (!cpu_online(cpu))
2904 return ERR_PTR(-ENODEV);
2906 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2915 ctxn = pmu->task_ctx_nr;
2920 ctx = perf_lock_task_context(task, ctxn, &flags);
2924 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2926 ctx = alloc_perf_context(pmu, task);
2932 mutex_lock(&task->perf_event_mutex);
2934 * If it has already passed perf_event_exit_task().
2935 * we must see PF_EXITING, it takes this mutex too.
2937 if (task->flags & PF_EXITING)
2939 else if (task->perf_event_ctxp[ctxn])
2944 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2946 mutex_unlock(&task->perf_event_mutex);
2948 if (unlikely(err)) {
2960 return ERR_PTR(err);
2963 static void perf_event_free_filter(struct perf_event *event);
2965 static void free_event_rcu(struct rcu_head *head)
2967 struct perf_event *event;
2969 event = container_of(head, struct perf_event, rcu_head);
2971 put_pid_ns(event->ns);
2972 perf_event_free_filter(event);
2976 static void ring_buffer_put(struct ring_buffer *rb);
2977 static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb);
2979 static void free_event(struct perf_event *event)
2981 irq_work_sync(&event->pending);
2983 if (!event->parent) {
2984 if (event->attach_state & PERF_ATTACH_TASK)
2985 jump_label_dec(&perf_sched_events);
2986 if (event->attr.mmap || event->attr.mmap_data)
2987 atomic_dec(&nr_mmap_events);
2988 if (event->attr.comm)
2989 atomic_dec(&nr_comm_events);
2990 if (event->attr.task)
2991 atomic_dec(&nr_task_events);
2992 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2993 put_callchain_buffers();
2994 if (is_cgroup_event(event)) {
2995 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
2996 jump_label_dec(&perf_sched_events);
3001 struct ring_buffer *rb;
3004 * Can happen when we close an event with re-directed output.
3006 * Since we have a 0 refcount, perf_mmap_close() will skip
3007 * over us; possibly making our ring_buffer_put() the last.
3009 mutex_lock(&event->mmap_mutex);
3012 rcu_assign_pointer(event->rb, NULL);
3013 ring_buffer_detach(event, rb);
3014 ring_buffer_put(rb); /* could be last */
3016 mutex_unlock(&event->mmap_mutex);
3019 if (is_cgroup_event(event))
3020 perf_detach_cgroup(event);
3023 event->destroy(event);
3026 put_ctx(event->ctx);
3028 call_rcu(&event->rcu_head, free_event_rcu);
3031 int perf_event_release_kernel(struct perf_event *event)
3033 struct perf_event_context *ctx = event->ctx;
3035 WARN_ON_ONCE(ctx->parent_ctx);
3037 * There are two ways this annotation is useful:
3039 * 1) there is a lock recursion from perf_event_exit_task
3040 * see the comment there.
3042 * 2) there is a lock-inversion with mmap_sem through
3043 * perf_event_read_group(), which takes faults while
3044 * holding ctx->mutex, however this is called after
3045 * the last filedesc died, so there is no possibility
3046 * to trigger the AB-BA case.
3048 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
3049 raw_spin_lock_irq(&ctx->lock);
3050 perf_group_detach(event);
3051 raw_spin_unlock_irq(&ctx->lock);
3052 perf_remove_from_context(event);
3053 mutex_unlock(&ctx->mutex);
3059 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3062 * Called when the last reference to the file is gone.
3064 static void put_event(struct perf_event *event)
3066 struct task_struct *owner;
3068 if (!atomic_long_dec_and_test(&event->refcount))
3072 owner = ACCESS_ONCE(event->owner);
3074 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3075 * !owner it means the list deletion is complete and we can indeed
3076 * free this event, otherwise we need to serialize on
3077 * owner->perf_event_mutex.
3079 smp_read_barrier_depends();
3082 * Since delayed_put_task_struct() also drops the last
3083 * task reference we can safely take a new reference
3084 * while holding the rcu_read_lock().
3086 get_task_struct(owner);
3091 mutex_lock(&owner->perf_event_mutex);
3093 * We have to re-check the event->owner field, if it is cleared
3094 * we raced with perf_event_exit_task(), acquiring the mutex
3095 * ensured they're done, and we can proceed with freeing the
3099 list_del_init(&event->owner_entry);
3100 mutex_unlock(&owner->perf_event_mutex);
3101 put_task_struct(owner);
3104 perf_event_release_kernel(event);
3107 static int perf_release(struct inode *inode, struct file *file)
3109 put_event(file->private_data);
3113 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
3115 struct perf_event *child;
3121 mutex_lock(&event->child_mutex);
3122 total += perf_event_read(event);
3123 *enabled += event->total_time_enabled +
3124 atomic64_read(&event->child_total_time_enabled);
3125 *running += event->total_time_running +
3126 atomic64_read(&event->child_total_time_running);
3128 list_for_each_entry(child, &event->child_list, child_list) {
3129 total += perf_event_read(child);
3130 *enabled += child->total_time_enabled;
3131 *running += child->total_time_running;
3133 mutex_unlock(&event->child_mutex);
3137 EXPORT_SYMBOL_GPL(perf_event_read_value);
3139 static int perf_event_read_group(struct perf_event *event,
3140 u64 read_format, char __user *buf)
3142 struct perf_event *leader = event->group_leader, *sub;
3143 int n = 0, size = 0, ret = -EFAULT;
3144 struct perf_event_context *ctx = leader->ctx;
3146 u64 count, enabled, running;
3148 mutex_lock(&ctx->mutex);
3149 count = perf_event_read_value(leader, &enabled, &running);
3151 values[n++] = 1 + leader->nr_siblings;
3152 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3153 values[n++] = enabled;
3154 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3155 values[n++] = running;
3156 values[n++] = count;
3157 if (read_format & PERF_FORMAT_ID)
3158 values[n++] = primary_event_id(leader);
3160 size = n * sizeof(u64);
3162 if (copy_to_user(buf, values, size))
3167 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3170 values[n++] = perf_event_read_value(sub, &enabled, &running);
3171 if (read_format & PERF_FORMAT_ID)
3172 values[n++] = primary_event_id(sub);
3174 size = n * sizeof(u64);
3176 if (copy_to_user(buf + ret, values, size)) {
3184 mutex_unlock(&ctx->mutex);
3189 static int perf_event_read_one(struct perf_event *event,
3190 u64 read_format, char __user *buf)
3192 u64 enabled, running;
3196 values[n++] = perf_event_read_value(event, &enabled, &running);
3197 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3198 values[n++] = enabled;
3199 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3200 values[n++] = running;
3201 if (read_format & PERF_FORMAT_ID)
3202 values[n++] = primary_event_id(event);
3204 if (copy_to_user(buf, values, n * sizeof(u64)))
3207 return n * sizeof(u64);
3211 * Read the performance event - simple non blocking version for now
3214 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3216 u64 read_format = event->attr.read_format;
3220 * Return end-of-file for a read on a event that is in
3221 * error state (i.e. because it was pinned but it couldn't be
3222 * scheduled on to the CPU at some point).
3224 if (event->state == PERF_EVENT_STATE_ERROR)
3227 if (count < event->read_size)
3230 WARN_ON_ONCE(event->ctx->parent_ctx);
3231 if (read_format & PERF_FORMAT_GROUP)
3232 ret = perf_event_read_group(event, read_format, buf);
3234 ret = perf_event_read_one(event, read_format, buf);
3240 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3242 struct perf_event *event = file->private_data;
3244 return perf_read_hw(event, buf, count);
3247 static unsigned int perf_poll(struct file *file, poll_table *wait)
3249 struct perf_event *event = file->private_data;
3250 struct ring_buffer *rb;
3251 unsigned int events = POLL_HUP;
3254 * Pin the event->rb by taking event->mmap_mutex; otherwise
3255 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
3257 mutex_lock(&event->mmap_mutex);
3260 events = atomic_xchg(&rb->poll, 0);
3261 mutex_unlock(&event->mmap_mutex);
3263 poll_wait(file, &event->waitq, wait);
3268 static void perf_event_reset(struct perf_event *event)
3270 (void)perf_event_read(event);
3271 local64_set(&event->count, 0);
3272 perf_event_update_userpage(event);
3276 * Holding the top-level event's child_mutex means that any
3277 * descendant process that has inherited this event will block
3278 * in sync_child_event if it goes to exit, thus satisfying the
3279 * task existence requirements of perf_event_enable/disable.
3281 static void perf_event_for_each_child(struct perf_event *event,
3282 void (*func)(struct perf_event *))
3284 struct perf_event *child;
3286 WARN_ON_ONCE(event->ctx->parent_ctx);
3287 mutex_lock(&event->child_mutex);
3289 list_for_each_entry(child, &event->child_list, child_list)
3291 mutex_unlock(&event->child_mutex);
3294 static void perf_event_for_each(struct perf_event *event,
3295 void (*func)(struct perf_event *))
3297 struct perf_event_context *ctx = event->ctx;
3298 struct perf_event *sibling;
3300 WARN_ON_ONCE(ctx->parent_ctx);
3301 mutex_lock(&ctx->mutex);
3302 event = event->group_leader;
3304 perf_event_for_each_child(event, func);
3306 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3307 perf_event_for_each_child(event, func);
3308 mutex_unlock(&ctx->mutex);
3311 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3313 struct perf_event_context *ctx = event->ctx;
3317 if (!is_sampling_event(event))
3320 if (copy_from_user(&value, arg, sizeof(value)))
3326 raw_spin_lock_irq(&ctx->lock);
3327 if (event->attr.freq) {
3328 if (value > sysctl_perf_event_sample_rate) {
3333 event->attr.sample_freq = value;
3335 event->attr.sample_period = value;
3336 event->hw.sample_period = value;
3339 raw_spin_unlock_irq(&ctx->lock);
3344 static const struct file_operations perf_fops;
3346 static struct file *perf_fget_light(int fd, int *fput_needed)
3350 file = fget_light(fd, fput_needed);
3352 return ERR_PTR(-EBADF);
3354 if (file->f_op != &perf_fops) {
3355 fput_light(file, *fput_needed);
3357 return ERR_PTR(-EBADF);
3363 static int perf_event_set_output(struct perf_event *event,
3364 struct perf_event *output_event);
3365 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3367 static long perf_ioctl(struct file *file, unsigne