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, unsigned int cmd, unsigned long arg)
3369 struct perf_event *event = file->private_data;
3370 void (*func)(struct perf_event *);
3374 case PERF_EVENT_IOC_ENABLE:
3375 func = perf_event_enable;
3377 case PERF_EVENT_IOC_DISABLE:
3378 func = perf_event_disable;
3380 case PERF_EVENT_IOC_RESET:
3381 func = perf_event_reset;
3384 case PERF_EVENT_IOC_REFRESH:
3385 return perf_event_refresh(event, arg);
3387 case PERF_EVENT_IOC_PERIOD:
3388 return perf_event_period(event, (u64 __user *)arg);
3390 case PERF_EVENT_IOC_SET_OUTPUT:
3392 struct file *output_file = NULL;
3393 struct perf_event *output_event = NULL;
3394 int fput_needed = 0;
3398 output_file = perf_fget_light(arg, &fput_needed);
3399 if (IS_ERR(output_file))
3400 return PTR_ERR(output_file);
3401 output_event = output_file->private_data;
3404 ret = perf_event_set_output(event, output_event);
3406 fput_light(output_file, fput_needed);
3411 case PERF_EVENT_IOC_SET_FILTER:
3412 return perf_event_set_filter(event, (void __user *)arg);
3418 if (flags & PERF_IOC_FLAG_GROUP)
3419 perf_event_for_each(event, func);
3421 perf_event_for_each_child(event, func);
3426 int perf_event_task_enable(void)
3428 struct perf_event *event;
3430 mutex_lock(¤t->perf_event_mutex);
3431 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3432 perf_event_for_each_child(event, perf_event_enable);
3433 mutex_unlock(¤t->perf_event_mutex);
3438 int perf_event_task_disable(void)
3440 struct perf_event *event;
3442 mutex_lock(¤t->perf_event_mutex);
3443 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3444 perf_event_for_each_child(event, perf_event_disable);
3445 mutex_unlock(¤t->perf_event_mutex);
3450 #ifndef PERF_EVENT_INDEX_OFFSET
3451 # define PERF_EVENT_INDEX_OFFSET 0
3454 static int perf_event_index(struct perf_event *event)
3456 if (event->hw.state & PERF_HES_STOPPED)
3459 if (event->state != PERF_EVENT_STATE_ACTIVE)
3462 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
3465 static void calc_timer_values(struct perf_event *event,
3472 ctx_time = event->shadow_ctx_time + now;
3473 *enabled = ctx_time - event->tstamp_enabled;
3474 *running = ctx_time - event->tstamp_running;
3478 * Callers need to ensure there can be no nesting of this function, otherwise
3479 * the seqlock logic goes bad. We can not serialize this because the arch
3480 * code calls this from NMI context.
3482 void perf_event_update_userpage(struct perf_event *event)
3484 struct perf_event_mmap_page *userpg;
3485 struct ring_buffer *rb;
3486 u64 enabled, running;
3490 * compute total_time_enabled, total_time_running
3491 * based on snapshot values taken when the event
3492 * was last scheduled in.
3494 * we cannot simply called update_context_time()
3495 * because of locking issue as we can be called in
3498 calc_timer_values(event, &enabled, &running);
3499 rb = rcu_dereference(event->rb);
3503 userpg = rb->user_page;
3506 * Disable preemption so as to not let the corresponding user-space
3507 * spin too long if we get preempted.
3512 userpg->index = perf_event_index(event);
3513 userpg->offset = perf_event_count(event);
3514 if (event->state == PERF_EVENT_STATE_ACTIVE)
3515 userpg->offset -= local64_read(&event->hw.prev_count);
3517 userpg->time_enabled = enabled +
3518 atomic64_read(&event->child_total_time_enabled);
3520 userpg->time_running = running +
3521 atomic64_read(&event->child_total_time_running);
3530 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3532 struct perf_event *event = vma->vm_file->private_data;
3533 struct ring_buffer *rb;
3534 int ret = VM_FAULT_SIGBUS;
3536 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3537 if (vmf->pgoff == 0)
3543 rb = rcu_dereference(event->rb);
3547 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3550 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
3554 get_page(vmf->page);
3555 vmf->page->mapping = vma->vm_file->f_mapping;
3556 vmf->page->index = vmf->pgoff;
3565 static void ring_buffer_attach(struct perf_event *event,
3566 struct ring_buffer *rb)
3568 unsigned long flags;
3570 if (!list_empty(&event->rb_entry))
3573 spin_lock_irqsave(&rb->event_lock, flags);
3574 if (list_empty(&event->rb_entry))
3575 list_add(&event->rb_entry, &rb->event_list);
3576 spin_unlock_irqrestore(&rb->event_lock, flags);
3579 static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb)
3581 unsigned long flags;
3583 if (list_empty(&event->rb_entry))
3586 spin_lock_irqsave(&rb->event_lock, flags);
3587 list_del_init(&event->rb_entry);
3588 wake_up_all(&event->waitq);
3589 spin_unlock_irqrestore(&rb->event_lock, flags);
3592 static void ring_buffer_wakeup(struct perf_event *event)
3594 struct ring_buffer *rb;
3597 rb = rcu_dereference(event->rb);
3599 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3600 wake_up_all(&event->waitq);
3605 static void rb_free_rcu(struct rcu_head *rcu_head)
3607 struct ring_buffer *rb;
3609 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3613 static struct ring_buffer *ring_buffer_get(struct perf_event *event)
3615 struct ring_buffer *rb;
3618 rb = rcu_dereference(event->rb);
3620 if (!atomic_inc_not_zero(&rb->refcount))
3628 static void ring_buffer_put(struct ring_buffer *rb)
3630 if (!atomic_dec_and_test(&rb->refcount))
3633 WARN_ON_ONCE(!list_empty(&rb->event_list));
3635 call_rcu(&rb->rcu_head, rb_free_rcu);
3638 static void perf_mmap_open(struct vm_area_struct *vma)
3640 struct perf_event *event = vma->vm_file->private_data;
3642 atomic_inc(&event->mmap_count);
3643 atomic_inc(&event->rb->mmap_count);
3647 * A buffer can be mmap()ed multiple times; either directly through the same
3648 * event, or through other events by use of perf_event_set_output().
3650 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3651 * the buffer here, where we still have a VM context. This means we need
3652 * to detach all events redirecting to us.
3654 static void perf_mmap_close(struct vm_area_struct *vma)
3656 struct perf_event *event = vma->vm_file->private_data;
3658 struct ring_buffer *rb = event->rb;
3659 struct user_struct *mmap_user = rb->mmap_user;
3660 int mmap_locked = rb->mmap_locked;
3661 unsigned long size = perf_data_size(rb);
3663 atomic_dec(&rb->mmap_count);
3665 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
3668 /* Detach current event from the buffer. */
3669 rcu_assign_pointer(event->rb, NULL);
3670 ring_buffer_detach(event, rb);
3671 mutex_unlock(&event->mmap_mutex);
3673 /* If there's still other mmap()s of this buffer, we're done. */
3674 if (atomic_read(&rb->mmap_count)) {
3675 ring_buffer_put(rb); /* can't be last */
3680 * No other mmap()s, detach from all other events that might redirect
3681 * into the now unreachable buffer. Somewhat complicated by the
3682 * fact that rb::event_lock otherwise nests inside mmap_mutex.
3686 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
3687 if (!atomic_long_inc_not_zero(&event->refcount)) {
3689 * This event is en-route to free_event() which will
3690 * detach it and remove it from the list.
3696 mutex_lock(&event->mmap_mutex);
3698 * Check we didn't race with perf_event_set_output() which can
3699 * swizzle the rb from under us while we were waiting to
3700 * acquire mmap_mutex.
3702 * If we find a different rb; ignore this event, a next
3703 * iteration will no longer find it on the list. We have to
3704 * still restart the iteration to make sure we're not now
3705 * iterating the wrong list.
3707 if (event->rb == rb) {
3708 rcu_assign_pointer(event->rb, NULL);
3709 ring_buffer_detach(event, rb);
3710 ring_buffer_put(rb); /* can't be last, we still have one */
3712 mutex_unlock(&event->mmap_mutex);
3716 * Restart the iteration; either we're on the wrong list or
3717 * destroyed its integrity by doing a deletion.
3724 * It could be there's still a few 0-ref events on the list; they'll
3725 * get cleaned up by free_event() -- they'll also still have their
3726 * ref on the rb and will free it whenever they are done with it.
3728 * Aside from that, this buffer is 'fully' detached and unmapped,
3729 * undo the VM accounting.
3732 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
3733 vma->vm_mm->pinned_vm -= mmap_locked;
3734 free_uid(mmap_user);
3736 ring_buffer_put(rb); /* could be last */
3739 static const struct vm_operations_struct perf_mmap_vmops = {
3740 .open = perf_mmap_open,
3741 .close = perf_mmap_close,
3742 .fault = perf_mmap_fault,
3743 .page_mkwrite = perf_mmap_fault,
3746 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3748 struct perf_event *event = file->private_data;
3749 unsigned long user_locked, user_lock_limit;
3750 struct user_struct *user = current_user();
3751 unsigned long locked, lock_limit;
3752 struct ring_buffer *rb;
3753 unsigned long vma_size;
3754 unsigned long nr_pages;
3755 long user_extra, extra;
3756 int ret = 0, flags = 0;
3759 * Don't allow mmap() of inherited per-task counters. This would
3760 * create a performance issue due to all children writing to the
3763 if (event->cpu == -1 && event->attr.inherit)
3766 if (!(vma->vm_flags & VM_SHARED))
3769 vma_size = vma->vm_end - vma->vm_start;
3770 nr_pages = (vma_size / PAGE_SIZE) - 1;
3773 * If we have rb pages ensure they're a power-of-two number, so we
3774 * can do bitmasks instead of modulo.
3776 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3779 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3782 if (vma->vm_pgoff != 0)
3785 WARN_ON_ONCE(event->ctx->parent_ctx);
3787 mutex_lock(&event->mmap_mutex);
3789 if (event->rb->nr_pages != nr_pages) {
3794 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
3796 * Raced against perf_mmap_close() through
3797 * perf_event_set_output(). Try again, hope for better
3800 mutex_unlock(&event->mmap_mutex);
3807 user_extra = nr_pages + 1;
3808 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3811 * Increase the limit linearly with more CPUs:
3813 user_lock_limit *= num_online_cpus();
3815 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3818 if (user_locked > user_lock_limit)
3819 extra = user_locked - user_lock_limit;
3821 lock_limit = rlimit(RLIMIT_MEMLOCK);
3822 lock_limit >>= PAGE_SHIFT;
3823 locked = vma->vm_mm->pinned_vm + extra;
3825 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3826 !capable(CAP_IPC_LOCK)) {
3833 if (vma->vm_flags & VM_WRITE)
3834 flags |= RING_BUFFER_WRITABLE;
3836 rb = rb_alloc(nr_pages,
3837 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3845 atomic_set(&rb->mmap_count, 1);
3846 rb->mmap_locked = extra;
3847 rb->mmap_user = get_current_user();
3849 atomic_long_add(user_extra, &user->locked_vm);
3850 vma->vm_mm->pinned_vm += extra;
3852 ring_buffer_attach(event, rb);
3853 rcu_assign_pointer(event->rb, rb);
3857 atomic_inc(&event->mmap_count);
3858 mutex_unlock(&event->mmap_mutex);
3861 * Since pinned accounting is per vm we cannot allow fork() to copy our
3864 vma->vm_flags |= VM_DONTCOPY | VM_RESERVED;
3865 vma->vm_ops = &perf_mmap_vmops;
3870 static int perf_fasync(int fd, struct file *filp, int on)
3872 struct inode *inode = filp->f_path.dentry->d_inode;
3873 struct perf_event *event = filp->private_data;
3876 mutex_lock(&inode->i_mutex);
3877 retval = fasync_helper(fd, filp, on, &event->fasync);
3878 mutex_unlock(&inode->i_mutex);
3886 static const struct file_operations perf_fops = {
3887 .llseek = no_llseek,
3888 .release = perf_release,
3891 .unlocked_ioctl = perf_ioctl,
3892 .compat_ioctl = perf_ioctl,
3894 .fasync = perf_fasync,
3900 * If there's data, ensure we set the poll() state and publish everything
3901 * to user-space before waking everybody up.
3904 void perf_event_wakeup(struct perf_event *event)
3906 ring_buffer_wakeup(event);
3908 if (event->pending_kill) {
3909 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3910 event->pending_kill = 0;
3914 static void perf_pending_event(struct irq_work *entry)
3916 struct perf_event *event = container_of(entry,
3917 struct perf_event, pending);
3919 if (event->pending_disable) {
3920 event->pending_disable = 0;
3921 __perf_event_disable(event);
3924 if (event->pending_wakeup) {
3925 event->pending_wakeup = 0;
3926 perf_event_wakeup(event);
3931 * We assume there is only KVM supporting the callbacks.
3932 * Later on, we might change it to a list if there is
3933 * another virtualization implementation supporting the callbacks.
3935 struct perf_guest_info_callbacks *perf_guest_cbs;
3937 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3939 perf_guest_cbs = cbs;
3942 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3944 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3946 perf_guest_cbs = NULL;
3949 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3951 static void __perf_event_header__init_id(struct perf_event_header *header,
3952 struct perf_sample_data *data,
3953 struct perf_event *event)
3955 u64 sample_type = event->attr.sample_type;
3957 data->type = sample_type;
3958 header->size += event->id_header_size;
3960 if (sample_type & PERF_SAMPLE_TID) {
3961 /* namespace issues */
3962 data->tid_entry.pid = perf_event_pid(event, current);
3963 data->tid_entry.tid = perf_event_tid(event, current);
3966 if (sample_type & PERF_SAMPLE_TIME)
3967 data->time = perf_clock();
3969 if (sample_type & PERF_SAMPLE_ID)
3970 data->id = primary_event_id(event);
3972 if (sample_type & PERF_SAMPLE_STREAM_ID)
3973 data->stream_id = event->id;
3975 if (sample_type & PERF_SAMPLE_CPU) {
3976 data->cpu_entry.cpu = raw_smp_processor_id();
3977 data->cpu_entry.reserved = 0;
3981 void perf_event_header__init_id(struct perf_event_header *header,
3982 struct perf_sample_data *data,
3983 struct perf_event *event)
3985 if (event->attr.sample_id_all)
3986 __perf_event_header__init_id(header, data, event);
3989 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3990 struct perf_sample_data *data)
3992 u64 sample_type = data->type;
3994 if (sample_type & PERF_SAMPLE_TID)
3995 perf_output_put(handle, data->tid_entry);
3997 if (sample_type & PERF_SAMPLE_TIME)
3998 perf_output_put(handle, data->time);
4000 if (sample_type & PERF_SAMPLE_ID)
4001 perf_output_put(handle, data->id);
4003 if (sample_type & PERF_SAMPLE_STREAM_ID)
4004 perf_output_put(handle, data->stream_id);
4006 if (sample_type & PERF_SAMPLE_CPU)
4007 perf_output_put(handle, data->cpu_entry);
4010 void perf_event__output_id_sample(struct perf_event *event,
4011 struct perf_output_handle *handle,
4012 struct perf_sample_data *sample)
4014 if (event->attr.sample_id_all)
4015 __perf_event__output_id_sample(handle, sample);
4018 static void perf_output_read_one(struct perf_output_handle *handle,
4019 struct perf_event *event,
4020 u64 enabled, u64 running)
4022 u64 read_format = event->attr.read_format;
4026 values[n++] = perf_event_count(event);
4027 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4028 values[n++] = enabled +
4029 atomic64_read(&event->child_total_time_enabled);
4031 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4032 values[n++] = running +
4033 atomic64_read(&event->child_total_time_running);
4035 if (read_format & PERF_FORMAT_ID)
4036 values[n++] = primary_event_id(event);
4038 __output_copy(handle, values, n * sizeof(u64));
4042 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4044 static void perf_output_read_group(struct perf_output_handle *handle,
4045 struct perf_event *event,
4046 u64 enabled, u64 running)
4048 struct perf_event *leader = event->group_leader, *sub;
4049 u64 read_format = event->attr.read_format;
4053 values[n++] = 1 + leader->nr_siblings;
4055 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4056 values[n++] = enabled;
4058 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4059 values[n++] = running;
4061 if (leader != event)
4062 leader->pmu->read(leader);
4064 values[n++] = perf_event_count(leader);
4065 if (read_format & PERF_FORMAT_ID)
4066 values[n++] = primary_event_id(leader);
4068 __output_copy(handle, values, n * sizeof(u64));
4070 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4074 sub->pmu->read(sub);
4076 values[n++] = perf_event_count(sub);
4077 if (read_format & PERF_FORMAT_ID)
4078 values[n++] = primary_event_id(sub);
4080 __output_copy(handle, values, n * sizeof(u64));
4084 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4085 PERF_FORMAT_TOTAL_TIME_RUNNING)
4087 static void perf_output_read(struct perf_output_handle *handle,
4088 struct perf_event *event)
4090 u64 enabled = 0, running = 0;
4091 u64 read_format = event->attr.read_format;
4094 * compute total_time_enabled, total_time_running
4095 * based on snapshot values taken when the event
4096 * was last scheduled in.
4098 * we cannot simply called update_context_time()
4099 * because of locking issue as we are called in
4102 if (read_format & PERF_FORMAT_TOTAL_TIMES)
4103 calc_timer_values(event, &enabled, &running);
4105 if (event->attr.read_format & PERF_FORMAT_GROUP)
4106 perf_output_read_group(handle, event, enabled, running);
4108 perf_output_read_one(handle, event, enabled, running);
4111 void perf_output_sample(struct perf_output_handle *handle,
4112 struct perf_event_header *header,
4113 struct perf_sample_data *data,
4114 struct perf_event *event)
4116 u64 sample_type = data->type;
4118 perf_output_put(handle, *header);
4120 if (sample_type & PERF_SAMPLE_IP)
4121 perf_output_put(handle, data->ip);
4123 if (sample_type & PERF_SAMPLE_TID)
4124 perf_output_put(handle, data->tid_entry);
4126 if (sample_type & PERF_SAMPLE_TIME)
4127 perf_output_put(handle, data->time);
4129 if (sample_type & PERF_SAMPLE_ADDR)
4130 perf_output_put(handle, data->addr);
4132 if (sample_type & PERF_SAMPLE_ID)
4133 perf_output_put(handle, data->id);
4135 if (sample_type & PERF_SAMPLE_STREAM_ID)
4136 perf_output_put(handle, data->stream_id);
4138 if (sample_type & PERF_SAMPLE_CPU)
4139 perf_output_put(handle, data->cpu_entry);
4141 if (sample_type & PERF_SAMPLE_PERIOD)
4142 perf_output_put(handle, data->period);
4144 if (sample_type & PERF_SAMPLE_READ)
4145 perf_output_read(handle, event);
4147 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4148 if (data->callchain) {
4151 if (data->callchain)
4152 size += data->callchain->nr;
4154 size *= sizeof(u64);
4156 __output_copy(handle, data->callchain, size);
4159 perf_output_put(handle, nr);
4163 if (sample_type & PERF_SAMPLE_RAW) {
4165 perf_output_put(handle, data->raw->size);
4166 __output_copy(handle, data->raw->data,
4173 .size = sizeof(u32),
4176 perf_output_put(handle, raw);
4180 if (!event->attr.watermark) {
4181 int wakeup_events = event->attr.wakeup_events;
4183 if (wakeup_events) {
4184 struct ring_buffer *rb = handle->rb;
4185 int events = local_inc_return(&rb->events);
4187 if (events >= wakeup_events) {
4188 local_sub(wakeup_events, &rb->events);
4189 local_inc(&rb->wakeup);
4195 void perf_prepare_sample(struct perf_event_header *header,
4196 struct perf_sample_data *data,
4197 struct perf_event *event,
4198 struct pt_regs *regs)
4200 u64 sample_type = event->attr.sample_type;
4202 header->type = PERF_RECORD_SAMPLE;
4203 header->size = sizeof(*header) + event->header_size;
4206 header->misc |= perf_misc_flags(regs);
4208 __perf_event_header__init_id(header, data, event);
4210 if (sample_type & PERF_SAMPLE_IP)
4211 data->ip = perf_instruction_pointer(regs);
4213 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4216 data->callchain = perf_callchain(regs);
4218 if (data->callchain)
4219 size += data->callchain->nr;
4221 header->size += size * sizeof(u64);
4224 if (sample_type & PERF_SAMPLE_RAW) {
4225 int size = sizeof(u32);
4228 size += data->raw->size;
4230 size += sizeof(u32);
4232 WARN_ON_ONCE(size & (sizeof(u64)-1));
4233 header->size += size;
4237 static void perf_event_output(struct perf_event *event,
4238 struct perf_sample_data *data,
4239 struct pt_regs *regs)
4241 struct perf_output_handle handle;
4242 struct perf_event_header header;
4244 /* protect the callchain buffers */
4247 perf_prepare_sample(&header, data, event, regs);
4249 if (perf_output_begin(&handle, event, header.size))
4252 perf_output_sample(&handle, &header, data, event);
4254 perf_output_end(&handle);
4264 struct perf_read_event {
4265 struct perf_event_header header;
4272 perf_event_read_event(struct perf_event *event,
4273 struct task_struct *task)
4275 struct perf_output_handle handle;
4276 struct perf_sample_data sample;
4277 struct perf_read_event read_event = {
4279 .type = PERF_RECORD_READ,
4281 .size = sizeof(read_event) + event->read_size,
4283 .pid = perf_event_pid(event, task),
4284 .tid = perf_event_tid(event, task),
4288 perf_event_header__init_id(&read_event.header, &sample, event);
4289 ret = perf_output_begin(&handle, event, read_event.header.size);
4293 perf_output_put(&handle, read_event);
4294 perf_output_read(&handle, event);
4295 perf_event__output_id_sample(event, &handle, &sample);
4297 perf_output_end(&handle);
4301 * task tracking -- fork/exit
4303 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4306 struct perf_task_event {
4307 struct task_struct *task;
4308 struct perf_event_context *task_ctx;
4311 struct perf_event_header header;
4321 static void perf_event_task_output(struct perf_event *event,
4322 struct perf_task_event *task_event)
4324 struct perf_output_handle handle;
4325 struct perf_sample_data sample;
4326 struct task_struct *task = task_event->task;
4327 int ret, size = task_event->event_id.header.size;
4329 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4331 ret = perf_output_begin(&handle, event,
4332 task_event->event_id.header.size);
4336 task_event->event_id.pid = perf_event_pid(event, task);
4337 task_event->event_id.ppid = perf_event_pid(event, current);
4339 task_event->event_id.tid = perf_event_tid(event, task);
4340 task_event->event_id.ptid = perf_event_tid(event, current);
4342 perf_output_put(&handle, task_event->event_id);
4344 perf_event__output_id_sample(event, &handle, &sample);
4346 perf_output_end(&handle);
4348 task_event->event_id.header.size = size;
4351 static int perf_event_task_match(struct perf_event *event)
4353 if (event->state < PERF_EVENT_STATE_INACTIVE)
4356 if (!event_filter_match(event))
4359 if (event->attr.comm || event->attr.mmap ||
4360 event->attr.mmap_data || event->attr.task)
4366 static void perf_event_task_ctx(struct perf_event_context *ctx,
4367 struct perf_task_event *task_event)
4369 struct perf_event *event;
4371 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4372 if (perf_event_task_match(event))
4373 perf_event_task_output(event, task_event);
4377 static void perf_event_task_event(struct perf_task_event *task_event)
4379 struct perf_cpu_context *cpuctx;
4380 struct perf_event_context *ctx;
4385 list_for_each_entry_rcu(pmu, &pmus, entry) {
4386 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4387 if (cpuctx->unique_pmu != pmu)
4389 perf_event_task_ctx(&cpuctx->ctx, task_event);
4391 ctx = task_event->task_ctx;
4393 ctxn = pmu->task_ctx_nr;
4396 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4399 perf_event_task_ctx(ctx, task_event);
4401 put_cpu_ptr(pmu->pmu_cpu_context);
4406 static void perf_event_task(struct task_struct *task,
4407 struct perf_event_context *task_ctx,
4410 struct perf_task_event task_event;
4412 if (!atomic_read(&nr_comm_events) &&
4413 !atomic_read(&nr_mmap_events) &&
4414 !atomic_read(&nr_task_events))
4417 task_event = (struct perf_task_event){
4419 .task_ctx = task_ctx,
4422 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4424 .size = sizeof(task_event.event_id),
4430 .time = perf_clock(),
4434 perf_event_task_event(&task_event);
4437 void perf_event_fork(struct task_struct *task)
4439 perf_event_task(task, NULL, 1);
4446 struct perf_comm_event {
4447 struct task_struct *task;
4452 struct perf_event_header header;
4459 static void perf_event_comm_output(struct perf_event *event,
4460 struct perf_comm_event *comm_event)
4462 struct perf_output_handle handle;
4463 struct perf_sample_data sample;
4464 int size = comm_event->event_id.header.size;
4467 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4468 ret = perf_output_begin(&handle, event,
4469 comm_event->event_id.header.size);
4474 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4475 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4477 perf_output_put(&handle, comm_event->event_id);
4478 __output_copy(&handle, comm_event->comm,
4479 comm_event->comm_size);
4481 perf_event__output_id_sample(event, &handle, &sample);
4483 perf_output_end(&handle);
4485 comm_event->event_id.header.size = size;
4488 static int perf_event_comm_match(struct perf_event *event)
4490 if (event->state < PERF_EVENT_STATE_INACTIVE)
4493 if (!event_filter_match(event))
4496 if (event->attr.comm)
4502 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4503 struct perf_comm_event *comm_event)
4505 struct perf_event *event;
4507 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4508 if (perf_event_comm_match(event))
4509 perf_event_comm_output(event, comm_event);
4513 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4515 struct perf_cpu_context *cpuctx;
4516 struct perf_event_context *ctx;
4517 char comm[TASK_COMM_LEN];
4522 memset(comm, 0, sizeof(comm));
4523 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4524 size = ALIGN(strlen(comm)+1, sizeof(u64));
4526 comm_event->comm = comm;
4527 comm_event->comm_size = size;
4529 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4531 list_for_each_entry_rcu(pmu, &pmus, entry) {
4532 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4533 if (cpuctx->unique_pmu != pmu)
4535 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4537 ctxn = pmu->task_ctx_nr;
4541 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4543 perf_event_comm_ctx(ctx, comm_event);
4545 put_cpu_ptr(pmu->pmu_cpu_context);
4550 void perf_event_comm(struct task_struct *task)
4552 struct perf_comm_event comm_event;
4553 struct perf_event_context *ctx;
4556 for_each_task_context_nr(ctxn) {
4557 ctx = task->perf_event_ctxp[ctxn];
4561 perf_event_enable_on_exec(ctx);
4564 if (!atomic_read(&nr_comm_events))
4567 comm_event = (struct perf_comm_event){
4573 .type = PERF_RECORD_COMM,
4582 perf_event_comm_event(&comm_event);
4589 struct perf_mmap_event {
4590 struct vm_area_struct *vma;
4592 const char *file_name;
4596 struct perf_event_header header;
4606 static void perf_event_mmap_output(struct perf_event *event,
4607 struct perf_mmap_event *mmap_event)
4609 struct perf_output_handle handle;
4610 struct perf_sample_data sample;
4611 int size = mmap_event->event_id.header.size;
4614 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4615 ret = perf_output_begin(&handle, event,
4616 mmap_event->event_id.header.size);
4620 mmap_event->event_id.pid = perf_event_pid(event, current);
4621 mmap_event->event_id.tid = perf_event_tid(event, current);
4623 perf_output_put(&handle, mmap_event->event_id);
4624 __output_copy(&handle, mmap_event->file_name,
4625 mmap_event->file_size);
4627 perf_event__output_id_sample(event, &handle, &sample);
4629 perf_output_end(&handle);
4631 mmap_event->event_id.header.size = size;
4634 static int perf_event_mmap_match(struct perf_event *event,
4635 struct perf_mmap_event *mmap_event,
4638 if (event->state < PERF_EVENT_STATE_INACTIVE)
4641 if (!event_filter_match(event))
4644 if ((!executable && event->attr.mmap_data) ||
4645 (executable && event->attr.mmap))
4651 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4652 struct perf_mmap_event *mmap_event,
4655 struct perf_event *event;
4657 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4658 if (perf_event_mmap_match(event, mmap_event, executable))
4659 perf_event_mmap_output(event, mmap_event);
4663 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4665 struct perf_cpu_context *cpuctx;
4666 struct perf_event_context *ctx;
4667 struct vm_area_struct *vma = mmap_event->vma;
4668 struct file *file = vma->vm_file;
4676 memset(tmp, 0, sizeof(tmp));
4680 * d_path works from the end of the rb backwards, so we
4681 * need to add enough zero bytes after the string to handle
4682 * the 64bit alignment we do later.
4684 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4686 name = strncpy(tmp, "//enomem", sizeof(tmp));
4689 name = d_path(&file->f_path, buf, PATH_MAX);
4691 name = strncpy(tmp, "//toolong", sizeof(tmp));
4695 if (arch_vma_name(mmap_event->vma)) {
4696 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4702 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4704 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4705 vma->vm_end >= vma->vm_mm->brk) {
4706 name = strncpy(tmp, "[heap]", sizeof(tmp));
4708 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4709 vma->vm_end >= vma->vm_mm->start_stack) {
4710 name = strncpy(tmp, "[stack]", sizeof(tmp));
4714 name = strncpy(tmp, "//anon", sizeof(tmp));
4719 size = ALIGN(strlen(name)+1, sizeof(u64));
4721 mmap_event->file_name = name;
4722 mmap_event->file_size = size;
4724 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4727 list_for_each_entry_rcu(pmu, &pmus, entry) {
4728 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4729 if (cpuctx->unique_pmu != pmu)
4731 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4732 vma->vm_flags & VM_EXEC);
4734 ctxn = pmu->task_ctx_nr;
4738 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4740 perf_event_mmap_ctx(ctx, mmap_event,
4741 vma->vm_flags & VM_EXEC);
4744 put_cpu_ptr(pmu->pmu_cpu_context);
4751 void perf_event_mmap(struct vm_area_struct *vma)
4753 struct perf_mmap_event mmap_event;
4755 if (!atomic_read(&nr_mmap_events))
4758 mmap_event = (struct perf_mmap_event){
4764 .type = PERF_RECORD_MMAP,
4765 .misc = PERF_RECORD_MISC_USER,
4770 .start = vma->vm_start,
4771 .len = vma->vm_end - vma->vm_start,
4772 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4776 perf_event_mmap_event(&mmap_event);
4780 * IRQ throttle logging
4783 static void perf_log_throttle(struct perf_event *event, int enable)
4785 struct perf_output_handle handle;
4786 struct perf_sample_data sample;
4790 struct perf_event_header header;
4794 } throttle_event = {
4796 .type = PERF_RECORD_THROTTLE,
4798 .size = sizeof(throttle_event),
4800 .time = perf_clock(),
4801 .id = primary_event_id(event),
4802 .stream_id = event->id,
4806 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4808 perf_event_header__init_id(&throttle_event.header, &sample, event);
4810 ret = perf_output_begin(&handle, event,
4811 throttle_event.header.size);
4815 perf_output_put(&handle, throttle_event);
4816 perf_event__output_id_sample(event, &handle, &sample);
4817 perf_output_end(&handle);
4821 * Generic event overflow handling, sampling.
4824 static int __perf_event_overflow(struct perf_event *event,
4825 int throttle, struct perf_sample_data *data,
4826 struct pt_regs *regs)
4828 int events = atomic_read(&event->event_limit);
4829 struct hw_perf_event *hwc = &event->hw;
4833 * Non-sampling counters might still use the PMI to fold short
4834 * hardware counters, ignore those.
4836 if (unlikely(!is_sampling_event(event)))
4839 if (unlikely(hwc->interrupts >= max_samples_per_tick)) {
4841 hwc->interrupts = MAX_INTERRUPTS;
4842 perf_log_throttle(event, 0);
4848 if (event->attr.freq) {
4849 u64 now = perf_clock();
4850 s64 delta = now - hwc->freq_time_stamp;
4852 hwc->freq_time_stamp = now;
4854 if (delta > 0 && delta < 2*TICK_NSEC)
4855 perf_adjust_period(event, delta, hwc->last_period);
4859 * XXX event_limit might not quite work as expected on inherited
4863 event->pending_kill = POLL_IN;
4864 if (events && atomic_dec_and_test(&event->event_limit)) {
4866 event->pending_kill = POLL_HUP;
4867 event->pending_disable = 1;
4868 irq_work_queue(&event->pending);
4871 if (event->overflow_handler)
4872 event->overflow_handler(event, data, regs);
4874 perf_event_output(event, data, regs);
4876 if (event->fasync && event->pending_kill) {
4877 event->pending_wakeup = 1;
4878 irq_work_queue(&event->pending);
4884 int perf_event_overflow(struct perf_event *event,
4885 struct perf_sample_data *data,
4886 struct pt_regs *regs)
4888 return __perf_event_overflow(event, 1, data, regs);
4892 * Generic software event infrastructure
4895 struct swevent_htable {
4896 struct swevent_hlist *swevent_hlist;
4897 struct mutex hlist_mutex;
4900 /* Recursion avoidance in each contexts */
4901 int recursion[PERF_NR_CONTEXTS];
4904 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4907 * We directly increment event->count and keep a second value in
4908 * event->hw.period_left to count intervals. This period event
4909 * is kept in the range [-sample_period, 0] so that we can use the
4913 static u64 perf_swevent_set_period(struct perf_event *event)
4915 struct hw_perf_event *hwc = &event->hw;
4916 u64 period = hwc->last_period;
4920 hwc->last_period = hwc->sample_period;
4923 old = val = local64_read(&hwc->period_left);
4927 nr = div64_u64(period + val, period);
4928 offset = nr * period;
4930 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4936 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4937 struct perf_sample_data *data,
4938 struct pt_regs *regs)
4940 struct hw_perf_event *hwc = &event->hw;
4943 data->period = event->hw.last_period;
4945 overflow = perf_swevent_set_period(event);
4947 if (hwc->interrupts == MAX_INTERRUPTS)
4950 for (; overflow; overflow--) {
4951 if (__perf_event_overflow(event, throttle,
4954 * We inhibit the overflow from happening when
4955 * hwc->interrupts == MAX_INTERRUPTS.
4963 static void perf_swevent_event(struct perf_event *event, u64 nr,
4964 struct perf_sample_data *data,
4965 struct pt_regs *regs)
4967 struct hw_perf_event *hwc = &event->hw;
4969 local64_add(nr, &event->count);
4974 if (!is_sampling_event(event))
4977 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4978 return perf_swevent_overflow(event, 1, data, regs);
4980 if (local64_add_negative(nr, &hwc->period_left))
4983 perf_swevent_overflow(event, 0, data, regs);
4986 static int perf_exclude_event(struct perf_event *event,
4987 struct pt_regs *regs)
4989 if (event->hw.state & PERF_HES_STOPPED)
4993 if (event->attr.exclude_user && user_mode(regs))
4996 if (event->attr.exclude_kernel && !user_mode(regs))
5003 static int perf_swevent_match(struct perf_event *event,
5004 enum perf_type_id type,
5006 struct perf_sample_data *data,
5007 struct pt_regs *regs)
5009 if (event->attr.type != type)
5012 if (event->attr.config != event_id)
5015 if (perf_exclude_event(event, regs))
5021 static inline u64 swevent_hash(u64 type, u32 event_id)
5023 u64 val = event_id | (type << 32);
5025 return hash_64(val, SWEVENT_HLIST_BITS);
5028 static inline struct hlist_head *
5029 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
5031 u64 hash = swevent_hash(type, event_id);
5033 return &hlist->heads[hash];
5036 /* For the read side: events when they trigger */
5037 static inline struct hlist_head *
5038 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
5040 struct swevent_hlist *hlist;
5042 hlist = rcu_dereference(swhash->swevent_hlist);
5046 return __find_swevent_head(hlist, type, event_id);
5049 /* For the event head insertion and removal in the hlist */
5050 static inline struct hlist_head *
5051 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
5053 struct swevent_hlist *hlist;
5054 u32 event_id = event->attr.config;
5055 u64 type = event->attr.type;
5058 * Event scheduling is always serialized against hlist allocation
5059 * and release. Which makes the protected version suitable here.
5060 * The context lock guarantees that.
5062 hlist = rcu_dereference_protected(swhash->swevent_hlist,
5063 lockdep_is_held(&event->ctx->lock));
5067 return __find_swevent_head(hlist, type, event_id);
5070 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5072 struct perf_sample_data *data,
5073 struct pt_regs *regs)
5075 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5076 struct perf_event *event;
5077 struct hlist_node *node;
5078 struct hlist_head *head;
5081 head = find_swevent_head_rcu(swhash, type, event_id);
5085 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5086 if (perf_swevent_match(event, type, event_id, data, regs))
5087 perf_swevent_event(event, nr, data, regs);
5093 int perf_swevent_get_recursion_context(void)
5095 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5097 return get_recursion_context(swhash->recursion);
5099 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
5101 inline void perf_swevent_put_recursion_context(int rctx)
5103 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5105 put_recursion_context(swhash->recursion, rctx);
5108 void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
5110 struct perf_sample_data data;
5113 preempt_disable_notrace();
5114 rctx = perf_swevent_get_recursion_context();
5118 perf_sample_data_init(&data, addr);
5120 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
5122 perf_swevent_put_recursion_context(rctx);
5123 preempt_enable_notrace();
5126 static void perf_swevent_read(struct perf_event *event)
5130 static int perf_swevent_add(struct perf_event *event, int flags)
5132 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5133 struct hw_perf_event *hwc = &event->hw;
5134 struct hlist_head *head;
5136 if (is_sampling_event(event)) {
5137 hwc->last_period = hwc->sample_period;
5138 perf_swevent_set_period(event);
5141 hwc->state = !(flags & PERF_EF_START);
5143 head = find_swevent_head(swhash, event);
5144 if (WARN_ON_ONCE(!head))
5147 hlist_add_head_rcu(&event->hlist_entry, head);
5152 static void perf_swevent_del(struct perf_event *event, int flags)
5154 hlist_del_rcu(&event->hlist_entry);
5157 static void perf_swevent_start(struct perf_event *event, int flags)
5159 event->hw.state = 0;
5162 static void perf_swevent_stop(struct perf_event *event, int flags)
5164 event->hw.state = PERF_HES_STOPPED;
5167 /* Deref the hlist from the update side */
5168 static inline struct swevent_hlist *
5169 swevent_hlist_deref(struct swevent_htable *swhash)
5171 return rcu_dereference_protected(swhash->swevent_hlist,
5172 lockdep_is_held(&swhash->hlist_mutex));
5175 static void swevent_hlist_release(struct swevent_htable *swhash)
5177 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
5182 rcu_assign_pointer(swhash->swevent_hlist, NULL);
5183 kfree_rcu(hlist, rcu_head);
5186 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5188 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5190 mutex_lock(&swhash->hlist_mutex);
5192 if (!--swhash->hlist_refcount)
5193 swevent_hlist_release(swhash);
5195 mutex_unlock(&swhash->hlist_mutex);
5198 static void swevent_hlist_put(struct perf_event *event)
5202 if (event->cpu != -1) {
5203 swevent_hlist_put_cpu(event, event->cpu);
5207 for_each_possible_cpu(cpu)
5208 swevent_hlist_put_cpu(event, cpu);
5211 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5213 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5216 mutex_lock(&swhash->hlist_mutex);
5218 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
5219 struct swevent_hlist *hlist;
5221 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5226 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5228 swhash->hlist_refcount++;
5230 mutex_unlock(&swhash->hlist_mutex);
5235 static int swevent_hlist_get(struct perf_event *event)
5238 int cpu, failed_cpu;
5240 if (event->cpu != -1)
5241 return swevent_hlist_get_cpu(event, event->cpu);
5244 for_each_possible_cpu(cpu) {
5245 err = swevent_hlist_get_cpu(event, cpu);
5255 for_each_possible_cpu(cpu) {
5256 if (cpu == failed_cpu)
5258 swevent_hlist_put_cpu(event, cpu);
5265 struct jump_label_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
5267 static void sw_perf_event_destroy(struct perf_event *event)
5269 u64 event_id = event->attr.config;
5271 WARN_ON(event->parent);
5273 jump_label_dec(&perf_swevent_enabled[event_id]);
5274 swevent_hlist_put(event);
5277 static int perf_swevent_init(struct perf_event *event)
5279 u64 event_id = event->attr.config;
5281 if (event->attr.type != PERF_TYPE_SOFTWARE)
5285 case PERF_COUNT_SW_CPU_CLOCK:
5286 case PERF_COUNT_SW_TASK_CLOCK:
5293 if (event_id >= PERF_COUNT_SW_MAX)
5296 if (!event->parent) {
5299 err = swevent_hlist_get(event);
5303 jump_label_inc(&perf_swevent_enabled[event_id]);
5304 event->destroy = sw_perf_event_destroy;
5310 static struct pmu perf_swevent = {
5311 .task_ctx_nr = perf_sw_context,
5313 .event_init = perf_swevent_init,
5314 .add = perf_swevent_add,
5315 .del = perf_swevent_del,
5316 .start = perf_swevent_start,
5317 .stop = perf_swevent_stop,
5318 .read = perf_swevent_read,
5321 #ifdef CONFIG_EVENT_TRACING
5323 static int perf_tp_filter_match(struct perf_event *event,
5324 struct perf_sample_data *data)
5326 void *record = data->raw->data;
5328 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5333 static int perf_tp_event_match(struct perf_event *event,
5334 struct perf_sample_data *data,
5335 struct pt_regs *regs)
5337 if (event->hw.state & PERF_HES_STOPPED)
5340 * All tracepoints are from kernel-space.
5342 if (event->attr.exclude_kernel)
5345 if (!perf_tp_filter_match(event, data))
5351 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5352 struct pt_regs *regs, struct hlist_head *head, int rctx)
5354 struct perf_sample_data data;
5355 struct perf_event *event;
5356 struct hlist_node *node;
5358 struct perf_raw_record raw = {
5363 perf_sample_data_init(&data, addr);
5366 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5367 if (perf_tp_event_match(event, &data, regs))
5368 perf_swevent_event(event, count, &data, regs);
5371 perf_swevent_put_recursion_context(rctx);
5373 EXPORT_SYMBOL_GPL(perf_tp_event);
5375 static void tp_perf_event_destroy(struct perf_event *event)
5377 perf_trace_destroy(event);
5380 static int perf_tp_event_init(struct perf_event *event)
5384 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5387 err = perf_trace_init(event);
5391 event->destroy = tp_perf_event_destroy;
5396 static struct pmu perf_tracepoint = {
5397 .task_ctx_nr = perf_sw_context,
5399 .event_init = perf_tp_event_init,
5400 .add = perf_trace_add,
5401 .del = perf_trace_del,
5402 .start = perf_swevent_start,
5403 .stop = perf_swevent_stop,
5404 .read = perf_swevent_read,
5407 static inline void perf_tp_register(void)
5409 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5412 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5417 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5420 filter_str = strndup_user(arg, PAGE_SIZE);
5421 if (IS_ERR(filter_str))
5422 return PTR_ERR(filter_str);
5424 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5430 static void perf_event_free_filter(struct perf_event *event)
5432 ftrace_profile_free_filter(event);
5437 static inline void perf_tp_register(void)
5441 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5446 static void perf_event_free_filter(struct perf_event *event)
5450 #endif /* CONFIG_EVENT_TRACING */
5452 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5453 void perf_bp_event(struct perf_event *bp, void *data)
5455 struct perf_sample_data sample;
5456 struct pt_regs *regs = data;
5458 perf_sample_data_init(&sample, bp->attr.bp_addr);
5460 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5461 perf_swevent_event(bp, 1, &sample, regs);
5466 * hrtimer based swevent callback
5469 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5471 enum hrtimer_restart ret = HRTIMER_RESTART;
5472 struct perf_sample_data data;
5473 struct pt_regs *regs;
5474 struct perf_event *event;
5477 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5479 if (event->state != PERF_EVENT_STATE_ACTIVE)
5480 return HRTIMER_NORESTART;
5482 event->pmu->read(event);
5484 perf_sample_data_init(&data, 0);
5485 data.period = event->hw.last_period;
5486 regs = get_irq_regs();
5488 if (regs && !perf_exclude_event(event, regs)) {
5489 if (!(event->attr.exclude_idle && current->pid == 0))
5490 if (perf_event_overflow(event, &data, regs))
5491 ret = HRTIMER_NORESTART;
5494 period = max_t(u64, 10000, event->hw.sample_period);
5495 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5500 static void perf_swevent_start_hrtimer(struct perf_event *event)
5502 struct hw_perf_event *hwc = &event->hw;
5505 if (!is_sampling_event(event))
5508 period = local64_read(&hwc->period_left);
5513 local64_set(&hwc->period_left, 0);
5515 period = max_t(u64, 10000, hwc->sample_period);
5517 __hrtimer_start_range_ns(&hwc->hrtimer,
5518 ns_to_ktime(period), 0,
5519 HRTIMER_MODE_REL_PINNED, 0);
5522 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5524 struct hw_perf_event *hwc = &event->hw;
5526 if (is_sampling_event(event)) {
5527 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5528 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5530 hrtimer_cancel(&hwc->hrtimer);
5534 static void perf_swevent_init_hrtimer(struct perf_event *event)
5536 struct hw_perf_event *hwc = &event->hw;
5538 if (!is_sampling_event(event))
5541 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5542 hwc->hrtimer.function = perf_swevent_hrtimer;
5545 * Since hrtimers have a fixed rate, we can do a static freq->period
5546 * mapping and avoid the whole period adjust feedback stuff.
5548 if (event->attr.freq) {
5549 long freq = event->attr.sample_freq;
5551 event->attr.sample_period = NSEC_PER_SEC / freq;
5552 hwc->sample_period = event->attr.sample_period;
5553 local64_set(&hwc->period_left, hwc->sample_period);
5554 event->attr.freq = 0;
5559 * Software event: cpu wall time clock
5562 static void cpu_clock_event_update(struct perf_event *event)
5567 now = local_clock();
5568 prev = local64_xchg(&event->hw.prev_count, now);
5569 local64_add(now - prev, &event->count);
5572 static void cpu_clock_event_start(struct perf_event *event, int flags)
5574 local64_set(&event->hw.prev_count, local_clock());
5575 perf_swevent_start_hrtimer(event);
5578 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5580 perf_swevent_cancel_hrtimer(event);
5581 cpu_clock_event_update(event);
5584 static int cpu_clock_event_add(struct perf_event *event, int flags)
5586 if (flags & PERF_EF_START)
5587 cpu_clock_event_start(event, flags);
5592 static void cpu_clock_event_del(struct perf_event *event, int flags)
5594 cpu_clock_event_stop(event, flags);
5597 static void cpu_clock_event_read(struct perf_event *event)
5599 cpu_clock_event_update(event);
5602 static int cpu_clock_event_init(struct perf_event *event)
5604 if (event->attr.type != PERF_TYPE_SOFTWARE)
5607 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5610 perf_swevent_init_hrtimer(event);
5615 static struct pmu perf_cpu_clock = {
5616 .task_ctx_nr = perf_sw_context,
5618 .event_init = cpu_clock_event_init,
5619 .add = cpu_clock_event_add,
5620 .del = cpu_clock_event_del,
5621 .start = cpu_clock_event_start,
5622 .stop = cpu_clock_event_stop,
5623 .read = cpu_clock_event_read,
5627 * Software event: task time clock
5630 static void task_clock_event_update(struct perf_event *event, u64 now)
5635 prev = local64_xchg(&event->hw.prev_count, now);
5637 local64_add(delta, &event->count);
5640 static void task_clock_event_start(struct perf_event *event, int flags)
5642 local64_set(&event->hw.prev_count, event->ctx->time);
5643 perf_swevent_start_hrtimer(event);
5646 static void task_clock_event_stop(struct perf_event *event, int flags)
5648 perf_swevent_cancel_hrtimer(event);
5649 task_clock_event_update(event, event->ctx->time);
5652 static int task_clock_event_add(struct perf_event *event, int flags)
5654 if (flags & PERF_EF_START)
5655 task_clock_event_start(event, flags);
5660 static void task_clock_event_del(struct perf_event *event, int flags)
5662 task_clock_event_stop(event, PERF_EF_UPDATE);
5665 static void task_clock_event_read(struct perf_event *event)
5667 u64 now = perf_clock();
5668 u64 delta = now - event->ctx->timestamp;
5669 u64 time = event->ctx->time + delta;
5671 task_clock_event_update(event, time);
5674 static int task_clock_event_init(struct perf_event *event)
5676 if (event->attr.type != PERF_TYPE_SOFTWARE)
5679 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5682 perf_swevent_init_hrtimer(event);
5687 static struct pmu perf_task_clock = {
5688 .task_ctx_nr = perf_sw_context,
5690 .event_init = task_clock_event_init,
5691 .add = task_clock_event_add,
5692 .del = task_clock_event_del,
5693 .start = task_clock_event_start,
5694 .stop = task_clock_event_stop,
5695 .read = task_clock_event_read,
5698 static void perf_pmu_nop_void(struct pmu *pmu)
5702 static int perf_pmu_nop_int(struct pmu *pmu)
5707 static void perf_pmu_start_txn(struct pmu *pmu)
5709 perf_pmu_disable(pmu);
5712 static int perf_pmu_commit_txn(struct pmu *pmu)
5714 perf_pmu_enable(pmu);
5718 static void perf_pmu_cancel_txn(struct pmu *pmu)
5720 perf_pmu_enable(pmu);
5724 * Ensures all contexts with the same task_ctx_nr have the same
5725 * pmu_cpu_context too.
5727 static void *find_pmu_context(int ctxn)
5734 list_for_each_entry(pmu, &pmus, entry) {
5735 if (pmu->task_ctx_nr == ctxn)
5736 return pmu->pmu_cpu_context;
5742 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5746 for_each_possible_cpu(cpu) {
5747 struct perf_cpu_context *cpuctx;
5749 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5751 if (cpuctx->unique_pmu == old_pmu)
5752 cpuctx->unique_pmu = pmu;
5756 static void free_pmu_context(struct pmu *pmu)
5760 mutex_lock(&pmus_lock);
5762 * Like a real lame refcount.
5764 list_for_each_entry(i, &pmus, entry) {
5765 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5766 update_pmu_context(i, pmu);
5771 free_percpu(pmu->pmu_cpu_context);
5773 mutex_unlock(&pmus_lock);
5775 static struct idr pmu_idr;
5778 type_show(struct device *dev, struct device_attribute *attr, char *page)
5780 struct pmu *pmu = dev_get_drvdata(dev);
5782 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5785 static struct device_attribute pmu_dev_attrs[] = {
5790 static int pmu_bus_running;
5791 static struct bus_type pmu_bus = {
5792 .name = "event_source",
5793 .dev_attrs = pmu_dev_attrs,
5796 static void pmu_dev_release(struct device *dev)
5801 static int pmu_dev_alloc(struct pmu *pmu)
5805 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5809 device_initialize(pmu->dev);
5810 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5814 dev_set_drvdata(pmu->dev, pmu);
5815 pmu->dev->bus = &pmu_bus;
5816 pmu->dev->release = pmu_dev_release;
5817 ret = device_add(pmu->dev);
5825 put_device(pmu->dev);
5829 static struct lock_class_key cpuctx_mutex;
5830 static struct lock_class_key cpuctx_lock;
5832 int perf_pmu_register(struct pmu *pmu, char *name, int type)
5836 mutex_lock(&pmus_lock);
5838 pmu->pmu_disable_count = alloc_percpu(int);
5839 if (!pmu->pmu_disable_count)
5848 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5852 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5860 if (pmu_bus_running) {
5861 ret = pmu_dev_alloc(pmu);
5867 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5868 if (pmu->pmu_cpu_context)
5869 goto got_cpu_context;
5872 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5873 if (!pmu->pmu_cpu_context)
5876 for_each_possible_cpu(cpu) {
5877 struct perf_cpu_context *cpuctx;
5879 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5880 __perf_event_init_context(&cpuctx->ctx);
5881 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
5882 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
5883 cpuctx->ctx.type = cpu_context;
5884 cpuctx->ctx.pmu = pmu;
5885 cpuctx->jiffies_interval = 1;
5886 INIT_LIST_HEAD(&cpuctx->rotation_list);
5887 cpuctx->unique_pmu = pmu;
5891 if (!pmu->start_txn) {
5892 if (pmu->pmu_enable) {
5894 * If we have pmu_enable/pmu_disable calls, install
5895 * transaction stubs that use that to try and batch
5896 * hardware accesses.
5898 pmu->start_txn = perf_pmu_start_txn;
5899 pmu->commit_txn = perf_pmu_commit_txn;
5900 pmu->cancel_txn = perf_pmu_cancel_txn;
5902 pmu->start_txn = perf_pmu_nop_void;
5903 pmu->commit_txn = perf_pmu_nop_int;
5904 pmu->cancel_txn = perf_pmu_nop_void;
5908 if (!pmu->pmu_enable) {
5909 pmu->pmu_enable = perf_pmu_nop_void;
5910 pmu->pmu_disable = perf_pmu_nop_void;
5913 list_add_rcu(&pmu->entry, &pmus);
5916 mutex_unlock(&pmus_lock);
5921 device_del(pmu->dev);
5922 put_device(pmu->dev);
5925 if (pmu->type >= PERF_TYPE_MAX)
5926 idr_remove(&pmu_idr, pmu->type);
5929 free_percpu(pmu->pmu_disable_count);
5933 void perf_pmu_unregister(struct pmu *pmu)
5935 mutex_lock(&pmus_lock);
5936 list_del_rcu(&pmu->entry);
5937 mutex_unlock(&pmus_lock);
5940 * We dereference the pmu list under both SRCU and regular RCU, so
5941 * synchronize against both of those.
5943 synchronize_srcu(&pmus_srcu);
5946 free_percpu(pmu->pmu_disable_count);
5947 if (pmu->type >= PERF_TYPE_MAX)
5948 idr_remove(&pmu_idr, pmu->type);
5949 device_del(pmu->dev);
5950 put_device(pmu->dev);
5951 free_pmu_context(pmu);
5954 struct pmu *perf_init_event(struct perf_event *event)
5956 struct pmu *pmu = NULL;
5960 idx = srcu_read_lock(&pmus_srcu);
5963 pmu = idr_find(&pmu_idr, event->attr.type);
5967 ret = pmu->event_init(event);
5973 list_for_each_entry_rcu(pmu, &pmus, entry) {
5975 ret = pmu->event_init(event);
5979 if (ret != -ENOENT) {
5984 pmu = ERR_PTR(-ENOENT);
5986 srcu_read_unlock(&pmus_srcu, idx);
5992 * Allocate and initialize a event structure
5994 static struct perf_event *
5995 perf_event_alloc(struct perf_event_attr *attr, int cpu,
5996 struct task_struct *task,
5997 struct perf_event *group_leader,
5998 struct perf_event *parent_event,
5999 perf_overflow_handler_t overflow_handler,
6003 struct perf_event *event;
6004 struct hw_perf_event *hwc;
6007 if ((unsigned)cpu >= nr_cpu_ids) {
6008 if (!task || cpu != -1)
6009 return ERR_PTR(-EINVAL);
6012 event = kzalloc(sizeof(*event), GFP_KERNEL);
6014 return ERR_PTR(-ENOMEM);
6017 * Single events are their own group leaders, with an
6018 * empty sibling list:
6021 group_leader = event;
6023 mutex_init(&event->child_mutex);
6024 INIT_LIST_HEAD(&event->child_list);
6026 INIT_LIST_HEAD(&event->group_entry);
6027 INIT_LIST_HEAD(&event->event_entry);
6028 INIT_LIST_HEAD(&event->sibling_list);
6029 INIT_LIST_HEAD(&event->rb_entry);
6031 init_waitqueue_head(&event->waitq);
6032 init_irq_work(&event->pending, perf_pending_event);
6034 mutex_init(&event->mmap_mutex);
6036 atomic_long_set(&event->refcount, 1);
6038 event->attr = *attr;
6039 event->group_leader = group_leader;
6043 event->parent = parent_event;
6045 event->ns = get_pid_ns(current->nsproxy->pid_ns);
6046 event->id = atomic64_inc_return(&perf_event_id);
6048 event->state = PERF_EVENT_STATE_INACTIVE;
6051 event->attach_state = PERF_ATTACH_TASK;
6052 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6054 * hw_breakpoint is a bit difficult here..
6056 if (attr->type == PERF_TYPE_BREAKPOINT)
6057 event->hw.bp_target = task;
6061 if (!overflow_handler && parent_event) {
6062 overflow_handler = parent_event->overflow_handler;
6063 context = parent_event->overflow_handler_context;
6066 event->overflow_handler = overflow_handler;
6067 event->overflow_handler_context = context;
6069 perf_event__state_init(event);
6074 hwc->sample_period = attr->sample_period;
6075 if (attr->freq && attr->sample_freq)
6076 hwc->sample_period = 1;
6077 hwc->last_period = hwc->sample_period;
6079 local64_set(&hwc->period_left, hwc->sample_period);
6082 * we currently do not support PERF_FORMAT_GROUP on inherited events
6084 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6087 pmu = perf_init_event(event);
6093 else if (IS_ERR(pmu))
6098 put_pid_ns(event->ns);
6100 return ERR_PTR(err);
6103 if (!event->parent) {
6104 if (event->attach_state & PERF_ATTACH_TASK)
6105 jump_label_inc(&perf_sched_events);
6106 if (event->attr.mmap || event->attr.mmap_data)
6107 atomic_inc(&nr_mmap_events);
6108 if (event->attr.comm)
6109 atomic_inc(&nr_comm_events);
6110 if (event->attr.task)
6111 atomic_inc(&nr_task_events);
6112 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6113 err = get_callchain_buffers();
6116 return ERR_PTR(err);
6124 static int perf_copy_attr(struct perf_event_attr __user *uattr,
6125 struct perf_event_attr *attr)
6130 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6134 * zero the full structure, so that a short copy will be nice.
6136 memset(attr, 0, sizeof(*attr));
6138 ret = get_user(size, &uattr->size);
6142 if (size > PAGE_SIZE) /* silly large */
6145 if (!size) /* abi compat */
6146 size = PERF_ATTR_SIZE_VER0;
6148 if (size < PERF_ATTR_SIZE_VER0)
6152 * If we're handed a bigger struct than we know of,
6153 * ensure all the unknown bits are 0 - i.e. new
6154 * user-space does not rely on any kernel feature
6155 * extensions we dont know about yet.
6157 if (size > sizeof(*attr)) {
6158 unsigned char __user *addr;
6159 unsigned char __user *end;
6162 addr = (void __user *)uattr + sizeof(*attr);
6163 end = (void __user *)uattr + size;
6165 for (; addr < end; addr++) {
6166 ret = get_user(val, addr);
6172 size = sizeof(*attr);
6175 ret = copy_from_user(attr, uattr, size);
6179 if (attr->__reserved_1)
6182 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6185 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6192 put_user(sizeof(*attr), &uattr->size);
6198 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6200 struct ring_buffer *rb = NULL, *old_rb = NULL;
6206 /* don't allow circular references */
6207 if (event == output_event)
6211 * Don't allow cross-cpu buffers
6213 if (output_event->cpu != event->cpu)
6217 * If its not a per-cpu rb, it must be the same task.
6219 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6223 mutex_lock(&event->mmap_mutex);
6224 /* Can't redirect output if we've got an active mmap() */
6225 if (atomic_read(&event->mmap_count))
6231 /* get the rb we want to redirect to */
6232 rb = ring_buffer_get(output_event);
6238 ring_buffer_detach(event, old_rb);
6241 ring_buffer_attach(event, rb);
6243 rcu_assign_pointer(event->rb, rb);
6246 ring_buffer_put(old_rb);
6248 * Since we detached before setting the new rb, so that we
6249 * could attach the new rb, we could have missed a wakeup.
6252 wake_up_all(&event->waitq);
6257 mutex_unlock(&event->mmap_mutex);
6264 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6266 * @attr_uptr: event_id type attributes for monitoring/sampling
6269 * @group_fd: group leader event fd
6271 SYSCALL_DEFINE5(perf_event_open,
6272 struct perf_event_attr __user *, attr_uptr,
6273 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6275 struct perf_event *group_leader = NULL, *output_event = NULL;
6276 struct perf_event *event, *sibling;
6277 struct perf_event_attr attr;
6278 struct perf_event_context *ctx;
6279 struct file *event_file = NULL;
6280 struct file *group_file = NULL;
6281 struct task_struct *task = NULL;
6285 int fput_needed = 0;
6288 /* for future expandability... */
6289 if (flags & ~PERF_FLAG_ALL)
6292 err = perf_copy_attr(attr_uptr, &attr);
6296 if (!attr.exclude_kernel) {
6297 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6302 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6307 * In cgroup mode, the pid argument is used to pass the fd
6308 * opened to the cgroup directory in cgroupfs. The cpu argument
6309 * designates the cpu on which to monitor threads from that
6312 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6315 event_fd = get_unused_fd_flags(O_RDWR);
6319 if (group_fd != -1) {
6320 group_file = perf_fget_light(group_fd, &fput_needed);
6321 if (IS_ERR(group_file)) {
6322 err = PTR_ERR(group_file);
6325 group_leader = group_file->private_data;
6326 if (flags & PERF_FLAG_FD_OUTPUT)
6327 output_event = group_leader;
6328 if (flags & PERF_FLAG_FD_NO_GROUP)
6329 group_leader = NULL;
6332 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
6333 task = find_lively_task_by_vpid(pid);
6335 err = PTR_ERR(task);
6340 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
6342 if (IS_ERR(event)) {
6343 err = PTR_ERR(event);
6347 if (flags & PERF_FLAG_PID_CGROUP) {
6348 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6353 * - that has cgroup constraint on event->cpu
6354 * - that may need work on context switch
6356 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6357 jump_label_inc(&perf_sched_events);
6361 * Special case software events and allow them to be part of
6362 * any hardware group.
6367 (is_software_event(event) != is_software_event(group_leader))) {
6368 if (is_software_event(event)) {
6370 * If event and group_leader are not both a software
6371 * event, and event is, then group leader is not.
6373 * Allow the addition of software events to !software
6374 * groups, this is safe because software events never
6377 pmu = group_leader->pmu;
6378 } else if (is_software_event(group_leader) &&
6379 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6381 * In case the group is a pure software group, and we
6382 * try to add a hardware event, move the whole group to
6383 * the hardware context.
6390 * Get the target context (task or percpu):
6392 ctx = find_get_context(pmu, task, cpu);
6399 put_task_struct(task);
6404 * Look up the group leader (we will attach this event to it):
6410 * Do not allow a recursive hierarchy (this new sibling
6411 * becoming part of another group-sibling):
6413 if (group_leader->group_leader != group_leader)
6416 * Do not allow to attach to a group in a different
6417 * task or CPU context:
6420 if (group_leader->ctx->type != ctx->type)
6423 if (group_leader->ctx != ctx)
6428 * Only a group leader can be exclusive or pinned
6430 if (attr.exclusive || attr.pinned)
6435 err = perf_event_set_output(event, output_event);
6440 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6441 if (IS_ERR(event_file)) {
6442 err = PTR_ERR(event_file);
6447 struct perf_event_context *gctx = group_leader->ctx;
6449 mutex_lock(&gctx->mutex);
6450 perf_remove_from_context(group_leader);
6453 * Removing from the context ends up with disabled
6454 * event. What we want here is event in the initial
6455 * startup state, ready to be add into new context.
6457 perf_event__state_init(group_leader);
6458 list_for_each_entry(sibling, &group_leader->sibling_list,
6460 perf_remove_from_context(sibling);
6461 perf_event__state_init(sibling);
6464 mutex_unlock(&gctx->mutex);
6468 WARN_ON_ONCE(ctx->parent_ctx);
6469 mutex_lock(&ctx->mutex);
6472 perf_install_in_context(ctx, group_leader, cpu);
6474 list_for_each_entry(sibling, &group_leader->sibling_list,
6476 perf_install_in_context(ctx, sibling, cpu);
6481 perf_install_in_context(ctx, event, cpu);
6483 perf_unpin_context(ctx);
6484 mutex_unlock(&ctx->mutex);
6486 event->owner = current;
6488 mutex_lock(¤t->perf_event_mutex);
6489 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6490 mutex_unlock(¤t->perf_event_mutex);
6493 * Precalculate sample_data sizes
6495 perf_event__header_size(event);
6496 perf_event__id_header_size(event);
6499 * Drop the reference on the group_event after placing the
6500 * new event on the sibling_list. This ensures destruction
6501 * of the group leader will find the pointer to itself in
6502 * perf_group_detach().
6504 fput_light(group_file, fput_needed);
6505 fd_install(event_fd, event_file);
6509 perf_unpin_context(ctx);
6515 put_task_struct(task);
6517 fput_light(group_file, fput_needed);
6519 put_unused_fd(event_fd);
6524 * perf_event_create_kernel_counter
6526 * @attr: attributes of the counter to create
6527 * @cpu: cpu in which the counter is bound
6528 * @task: task to profile (NULL for percpu)
6531 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6532 struct task_struct *task,
6533 perf_overflow_handler_t overflow_handler,
6536 struct perf_event_context *ctx;
6537 struct perf_event *event;
6541 * Get the target context (task or percpu):
6544 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
6545 overflow_handler, context);
6546 if (IS_ERR(event)) {
6547 err = PTR_ERR(event);
6551 ctx = find_get_context(event->pmu, task, cpu);
6557 WARN_ON_ONCE(ctx->parent_ctx);
6558 mutex_lock(&ctx->mutex);
6559 perf_install_in_context(ctx, event, cpu);
6561 perf_unpin_context(ctx);
6562 mutex_unlock(&ctx->mutex);
6569 return ERR_PTR(err);
6571 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6573 static void sync_child_event(struct perf_event *child_event,
6574 struct task_struct *child)
6576 struct perf_event *parent_event = child_event->parent;
6579 if (child_event->attr.inherit_stat)
6580 perf_event_read_event(child_event, child);
6582 child_val = perf_event_count(child_event);
6585 * Add back the child's count to the parent's count:
6587 atomic64_add(child_val, &parent_event->child_count);
6588 atomic64_add(child_event->total_time_enabled,
6589 &parent_event->child_total_time_enabled);
6590 atomic64_add(child_event->total_time_running,
6591 &parent_event->child_total_time_running);
6594 * Remove this event from the parent's list
6596 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6597 mutex_lock(&parent_event->child_mutex);
6598 list_del_init(&child_event->child_list);
6599 mutex_unlock(&parent_event->child_mutex);
6602 * Release the parent event, if this was the last
6605 put_event(parent_event);
6609 __perf_event_exit_task(struct perf_event *child_event,
6610 struct perf_event_context *child_ctx,
6611 struct task_struct *child)
6613 if (child_event->parent) {
6614 raw_spin_lock_irq(&child_ctx->lock);
6615 perf_group_detach(child_event);
6616 raw_spin_unlock_irq(&child_ctx->lock);
6619 perf_remove_from_context(child_event);
6622 * It can happen that the parent exits first, and has events
6623 * that are still around due to the child reference. These
6624 * events need to be zapped.
6626 if (child_event->parent) {
6627 sync_child_event(child_event, child);
6628 free_event(child_event);
6632 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6634 struct perf_event *child_event, *tmp;
6635 struct perf_event_context *child_ctx;
6636 unsigned long flags;
6638 if (likely(!child->perf_event_ctxp[ctxn])) {
6639 perf_event_task(child, NULL, 0);
6643 local_irq_save(flags);
6645 * We can't reschedule here because interrupts are disabled,
6646 * and either child is current or it is a task that can't be
6647 * scheduled, so we are now safe from rescheduling changing
6650 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6653 * Take the context lock here so that if find_get_context is
6654 * reading child->perf_event_ctxp, we wait until it has
6655 * incremented the context's refcount before we do put_ctx below.
6657 raw_spin_lock(&child_ctx->lock);
6658 task_ctx_sched_out(child_ctx);
6659 child->perf_event_ctxp[ctxn] = NULL;
6661 * If this context is a clone; unclone it so it can't get
6662 * swapped to another process while we're removing all
6663 * the events from it.
6665 unclone_ctx(child_ctx);
6666 update_context_time(child_ctx);
6667 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6670 * Report the task dead after unscheduling the events so that we
6671 * won't get any samples after PERF_RECORD_EXIT. We can however still
6672 * get a few PERF_RECORD_READ events.
6674 perf_event_task(child, child_ctx, 0);
6677 * We can recurse on the same lock type through:
6679 * __perf_event_exit_task()
6680 * sync_child_event()
6682 * mutex_lock(&ctx->mutex)
6684 * But since its the parent context it won't be the same instance.
6686 mutex_lock(&child_ctx->mutex);
6689 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6691 __perf_event_exit_task(child_event, child_ctx, child);
6693 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6695 __perf_event_exit_task(child_event, child_ctx, child);
6698 * If the last event was a group event, it will have appended all
6699 * its siblings to the list, but we obtained 'tmp' before that which
6700 * will still point to the list head terminating the iteration.
6702 if (!list_empty(&child_ctx->pinned_groups) ||
6703 !list_empty(&child_ctx->flexible_groups))
6706 mutex_unlock(&child_ctx->mutex);
6712 * When a child task exits, feed back event values to parent events.
6714 void perf_event_exit_task(struct task_struct *child)
6716 struct perf_event *event, *tmp;
6719 mutex_lock(&child->perf_event_mutex);
6720 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6722 list_del_init(&event->owner_entry);
6725 * Ensure the list deletion is visible before we clear
6726 * the owner, closes a race against perf_release() where
6727 * we need to serialize on the owner->perf_event_mutex.
6730 event->owner = NULL;
6732 mutex_unlock(&child->perf_event_mutex);
6734 for_each_task_context_nr(ctxn)
6735 perf_event_exit_task_context(child, ctxn);
6738 static void perf_free_event(struct perf_event *event,
6739 struct perf_event_context *ctx)
6741 struct perf_event *parent = event->parent;
6743 if (WARN_ON_ONCE(!parent))
6746 mutex_lock(&parent->child_mutex);
6747 list_del_init(&event->child_list);
6748 mutex_unlock(&parent->child_mutex);
6752 perf_group_detach(event);
6753 list_del_event(event, ctx);
6758 * free an unexposed, unused context as created by inheritance by
6759 * perf_event_init_task below, used by fork() in case of fail.
6761 void perf_event_free_task(struct task_struct *task)
6763 struct perf_event_context *ctx;
6764 struct perf_event *event, *tmp;
6767 for_each_task_context_nr(ctxn) {
6768 ctx = task->perf_event_ctxp[ctxn];
6772 mutex_lock(&ctx->mutex);
6774 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6776 perf_free_event(event, ctx);
6778 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6780 perf_free_event(event, ctx);
6782 if (!list_empty(&ctx->pinned_groups) ||
6783 !list_empty(&ctx->flexible_groups))
6786 mutex_unlock(&ctx->mutex);
6792 void perf_event_delayed_put(struct task_struct *task)
6796 for_each_task_context_nr(ctxn)
6797 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6801 * inherit a event from parent task to child task:
6803 static struct perf_event *
6804 inherit_event(struct perf_event *parent_event,
6805 struct task_struct *parent,
6806 struct perf_event_context *parent_ctx,
6807 struct task_struct *child,
6808 struct perf_event *group_leader,
6809 struct perf_event_context *child_ctx)
6811 struct perf_event *child_event;
6812 unsigned long flags;
6815 * Instead of creating recursive hierarchies of events,
6816 * we link inherited events back to the original parent,
6817 * which has a filp for sure, which we use as the reference
6820 if (parent_event->parent)
6821 parent_event = parent_event->parent;
6823 child_event = perf_event_alloc(&parent_event->attr,
6826 group_leader, parent_event,
6828 if (IS_ERR(child_event))
6831 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
6832 free_event(child_event);
6839 * Make the child state follow the state of the parent event,
6840 * not its attr.disabled bit. We hold the parent's mutex,
6841 * so we won't race with perf_event_{en, dis}able_family.
6843 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6844 child_event->state = PERF_EVENT_STATE_INACTIVE;
6846 child_event->state = PERF_EVENT_STATE_OFF;
6848 if (parent_event->attr.freq) {
6849 u64 sample_period = parent_event->hw.sample_period;
6850 struct hw_perf_event *hwc = &child_event->hw;
6852 hwc->sample_period = sample_period;
6853 hwc->last_period = sample_period;
6855 local64_set(&hwc->period_left, sample_period);
6858 child_event->ctx = child_ctx;
6859 child_event->overflow_handler = parent_event->overflow_handler;
6860 child_event->overflow_handler_context
6861 = parent_event->overflow_handler_context;
6864 * Precalculate sample_data sizes
6866 perf_event__header_size(child_event);
6867 perf_event__id_header_size(child_event);
6870 * Link it up in the child's context:
6872 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6873 add_event_to_ctx(child_event, child_ctx);
6874 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6877 * Link this into the parent event's child list
6879 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6880 mutex_lock(&parent_event->child_mutex);
6881 list_add_tail(&child_event->child_list, &parent_event->child_list);
6882 mutex_unlock(&parent_event->child_mutex);
6887 static int inherit_group(struct perf_event *parent_event,
6888 struct task_struct *parent,
6889 struct perf_event_context *parent_ctx,
6890 struct task_struct *child,
6891 struct perf_event_context *child_ctx)
6893 struct perf_event *leader;
6894 struct perf_event *sub;
6895 struct perf_event *child_ctr;
6897 leader = inherit_event(parent_event, parent, parent_ctx,
6898 child, NULL, child_ctx);
6900 return PTR_ERR(leader);
6901 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6902 child_ctr = inherit_event(sub, parent, parent_ctx,
6903 child, leader, child_ctx);
6904 if (IS_ERR(child_ctr))
6905 return PTR_ERR(child_ctr);
6911 inherit_task_group(struct perf_event *event, struct task_struct *parent,
6912 struct perf_event_context *parent_ctx,
6913 struct task_struct *child, int ctxn,
6917 struct perf_event_context *child_ctx;
6919 if (!event->attr.inherit) {
6924 child_ctx = child->perf_event_ctxp[ctxn];
6927 * This is executed from the parent task context, so
6928 * inherit events that have been marked for cloning.
6929 * First allocate and initialize a context for the
6933 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
6937 child->perf_event_ctxp[ctxn] = child_ctx;
6940 ret = inherit_group(event, parent, parent_ctx,
6950 * Initialize the perf_event context in task_struct
6952 int perf_event_init_context(struct task_struct *child, int ctxn)
6954 struct perf_event_context *child_ctx, *parent_ctx;
6955 struct perf_event_context *cloned_ctx;
6956 struct perf_event *event;
6957 struct task_struct *parent = current;
6958 int inherited_all = 1;
6959 unsigned long flags;
6962 if (likely(!parent->perf_event_ctxp[ctxn]))
6966 * If the parent's context is a clone, pin it so it won't get
6969 parent_ctx = perf_pin_task_context(parent, ctxn);
6972 * No need to check if parent_ctx != NULL here; since we saw
6973 * it non-NULL earlier, the only reason for it to become NULL
6974 * is if we exit, and since we're currently in the middle of
6975 * a fork we can't be exiting at the same time.
6979 * Lock the parent list. No need to lock the child - not PID
6980 * hashed yet and not running, so nobody can access it.
6982 mutex_lock(&parent_ctx->mutex);
6985 * We dont have to disable NMIs - we are only looking at
6986 * the list, not manipulating it:
6988 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
6989 ret = inherit_task_group(event, parent, parent_ctx,
6990 child, ctxn, &inherited_all);
6996 * We can't hold ctx->lock when iterating the ->flexible_group list due
6997 * to allocations, but we need to prevent rotation because
6998 * rotate_ctx() will change the list from interrupt context.
7000 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7001 parent_ctx->rotate_disable = 1;
7002 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7004 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
7005 ret = inherit_task_group(event, parent, parent_ctx,
7006 child, ctxn, &inherited_all);
7011 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7012 parent_ctx->rotate_disable = 0;
7014 child_ctx = child->perf_event_ctxp[ctxn];
7016 if (child_ctx && inherited_all) {
7018 * Mark the child context as a clone of the parent
7019 * context, or of whatever the parent is a clone of.
7021 * Note that if the parent is a clone, the holding of
7022 * parent_ctx->lock avoids it from being uncloned.
7024 cloned_ctx = parent_ctx->parent_ctx;
7026 child_ctx->parent_ctx = cloned_ctx;
7027 child_ctx->parent_gen = parent_ctx->parent_gen;
7029 child_ctx->parent_ctx = parent_ctx;
7030 child_ctx->parent_gen = parent_ctx->generation;
7032 get_ctx(child_ctx->parent_ctx);
7035 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7036 mutex_unlock(&parent_ctx->mutex);
7038 perf_unpin_context(parent_ctx);
7039 put_ctx(parent_ctx);
7045 * Initialize the perf_event context in task_struct
7047 int perf_event_init_task(struct task_struct *child)
7051 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7052 mutex_init(&child->perf_event_mutex);
7053 INIT_LIST_HEAD(&child->perf_event_list);
7055 for_each_task_context_nr(ctxn) {
7056 ret = perf_event_init_context(child, ctxn);
7064 static void __init perf_event_init_all_cpus(void)
7066 struct swevent_htable *swhash;
7069 for_each_possible_cpu(cpu) {
7070 swhash = &per_cpu(swevent_htable, cpu);
7071 mutex_init(&swhash->hlist_mutex);
7072 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
7076 static void __cpuinit perf_event_init_cpu(int cpu)
7078 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7080 mutex_lock(&swhash->hlist_mutex);
7081 if (swhash->hlist_refcount > 0) {
7082 struct swevent_hlist *hlist;
7084 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7086 rcu_assign_pointer(swhash->swevent_hlist, hlist);
7088 mutex_unlock(&swhash->hlist_mutex);
7091 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7092 static void perf_pmu_rotate_stop(struct pmu *pmu)
7094 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7096 WARN_ON(!irqs_disabled());
7098 list_del_init(&cpuctx->rotation_list);
7101 static void __perf_event_exit_context(void *__info)
7103 struct perf_event_context *ctx = __info;
7104 struct perf_event *event;
7106 perf_pmu_rotate_stop(ctx->pmu);
7109 list_for_each_entry_rcu(event, &ctx->event_list, event_entry)
7110 __perf_remove_from_context(event);
7114 static void perf_event_exit_cpu_context(int cpu)
7116 struct perf_event_context *ctx;
7120 idx = srcu_read_lock(&pmus_srcu);
7121 list_for_each_entry_rcu(pmu, &pmus, entry) {
7122 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
7124 mutex_lock(&ctx->mutex);
7125 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7126 mutex_unlock(&ctx->mutex);
7128 srcu_read_unlock(&pmus_srcu, idx);
7131 static void perf_event_exit_cpu(int cpu)
7133 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7135 perf_event_exit_cpu_context(cpu);
7137 mutex_lock(&swhash->hlist_mutex);
7138 swevent_hlist_release(swhash);
7139 mutex_unlock(&swhash->hlist_mutex);
7142 static inline void perf_event_exit_cpu(int cpu) { }
7146 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7150 for_each_online_cpu(cpu)
7151 perf_event_exit_cpu(cpu);
7157 * Run the perf reboot notifier at the very last possible moment so that
7158 * the generic watchdog code runs as long as possible.
7160 static struct notifier_block perf_reboot_notifier = {
7161 .notifier_call = perf_reboot,
7162 .priority = INT_MIN,
7165 static int __cpuinit
7166 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7168 unsigned int cpu = (long)hcpu;
7170 switch (action & ~CPU_TASKS_FROZEN) {
7172 case CPU_UP_PREPARE:
7173 case CPU_DOWN_FAILED:
7174 perf_event_init_cpu(cpu);
7177 case CPU_UP_CANCELED:
7178 case CPU_DOWN_PREPARE:
7179 perf_event_exit_cpu(cpu);
7189 void __init perf_event_init(void)
7195 perf_event_init_all_cpus();
7196 init_srcu_struct(&pmus_srcu);
7197 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7198 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7199 perf_pmu_register(&perf_task_clock, NULL, -1);
7201 perf_cpu_notifier(perf_cpu_notify);
7202 register_reboot_notifier(&perf_reboot_notifier);
7204 ret = init_hw_breakpoint();
7205 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7208 static int __init perf_event_sysfs_init(void)
7213 mutex_lock(&pmus_lock);
7215 ret = bus_register(&pmu_bus);
7219 list_for_each_entry(pmu, &pmus, entry) {
7220 if (!pmu->name || pmu->type < 0)
7223 ret = pmu_dev_alloc(pmu);
7224 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7226 pmu_bus_running = 1;
7230 mutex_unlock(&pmus_lock);
7234 device_initcall(perf_event_sysfs_init);
7236 #ifdef CONFIG_CGROUP_PERF
7237 static struct cgroup_subsys_state *perf_cgroup_create(
7238 struct cgroup_subsys *ss, struct cgroup *cont)
7240 struct perf_cgroup *jc;
7242 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
7244 return ERR_PTR(-ENOMEM);
7246 jc->info = alloc_percpu(struct perf_cgroup_info);
7249 return ERR_PTR(-ENOMEM);
7255 static void perf_cgroup_destroy(struct cgroup_subsys *ss,
7256 struct cgroup *cont)
7258 struct perf_cgroup *jc;
7259 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7260 struct perf_cgroup, css);
7261 free_percpu(jc->info);
7265 static int __perf_cgroup_move(void *info)
7267 struct task_struct *task = info;
7268 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7273 perf_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *task)
7275 task_function_call(task, __perf_cgroup_move, task);
7278 static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7279 struct cgroup *old_cgrp, struct task_struct *task)
7282 * cgroup_exit() is called in the copy_process() failure path.
7283 * Ignore this case since the task hasn't ran yet, this avoids
7284 * trying to poke a half freed task state from generic code.
7286 if (!(task->flags & PF_EXITING))
7289 perf_cgroup_attach_task(cgrp, task);
7292 struct cgroup_subsys perf_subsys = {
7293 .name = "perf_event",
7294 .subsys_id = perf_subsys_id,
7295 .create = perf_cgroup_create,
7296 .destroy = perf_cgroup_destroy,
7297 .exit = perf_cgroup_exit,
7298 .attach_task = perf_cgroup_attach_task,
7300 #endif /* CONFIG_CGROUP_PERF */