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;
1183 struct remove_event {
1184 struct perf_event *event;
1189 * Cross CPU call to remove a performance event
1191 * We disable the event on the hardware level first. After that we
1192 * remove it from the context list.
1194 static int __perf_remove_from_context(void *info)
1196 struct remove_event *re = info;
1197 struct perf_event *event = re->event;
1198 struct perf_event_context *ctx = event->ctx;
1199 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1201 raw_spin_lock(&ctx->lock);
1202 event_sched_out(event, cpuctx, ctx);
1203 if (re->detach_group)
1204 perf_group_detach(event);
1205 list_del_event(event, ctx);
1206 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1208 cpuctx->task_ctx = NULL;
1210 raw_spin_unlock(&ctx->lock);
1217 * Remove the event from a task's (or a CPU's) list of events.
1219 * CPU events are removed with a smp call. For task events we only
1220 * call when the task is on a CPU.
1222 * If event->ctx is a cloned context, callers must make sure that
1223 * every task struct that event->ctx->task could possibly point to
1224 * remains valid. This is OK when called from perf_release since
1225 * that only calls us on the top-level context, which can't be a clone.
1226 * When called from perf_event_exit_task, it's OK because the
1227 * context has been detached from its task.
1229 static void perf_remove_from_context(struct perf_event *event, bool detach_group)
1231 struct perf_event_context *ctx = event->ctx;
1232 struct task_struct *task = ctx->task;
1233 struct remove_event re = {
1235 .detach_group = detach_group,
1238 lockdep_assert_held(&ctx->mutex);
1242 * Per cpu events are removed via an smp call and
1243 * the removal is always successful.
1245 cpu_function_call(event->cpu, __perf_remove_from_context, &re);
1250 if (!task_function_call(task, __perf_remove_from_context, &re))
1253 raw_spin_lock_irq(&ctx->lock);
1255 * If we failed to find a running task, but find the context active now
1256 * that we've acquired the ctx->lock, retry.
1258 if (ctx->is_active) {
1259 raw_spin_unlock_irq(&ctx->lock);
1264 * Since the task isn't running, its safe to remove the event, us
1265 * holding the ctx->lock ensures the task won't get scheduled in.
1268 perf_group_detach(event);
1269 list_del_event(event, ctx);
1270 raw_spin_unlock_irq(&ctx->lock);
1274 * Cross CPU call to disable a performance event
1276 static int __perf_event_disable(void *info)
1278 struct perf_event *event = info;
1279 struct perf_event_context *ctx = event->ctx;
1280 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1283 * If this is a per-task event, need to check whether this
1284 * event's task is the current task on this cpu.
1286 * Can trigger due to concurrent perf_event_context_sched_out()
1287 * flipping contexts around.
1289 if (ctx->task && cpuctx->task_ctx != ctx)
1292 raw_spin_lock(&ctx->lock);
1295 * If the event is on, turn it off.
1296 * If it is in error state, leave it in error state.
1298 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1299 update_context_time(ctx);
1300 update_cgrp_time_from_event(event);
1301 update_group_times(event);
1302 if (event == event->group_leader)
1303 group_sched_out(event, cpuctx, ctx);
1305 event_sched_out(event, cpuctx, ctx);
1306 event->state = PERF_EVENT_STATE_OFF;
1309 raw_spin_unlock(&ctx->lock);
1317 * If event->ctx is a cloned context, callers must make sure that
1318 * every task struct that event->ctx->task could possibly point to
1319 * remains valid. This condition is satisifed when called through
1320 * perf_event_for_each_child or perf_event_for_each because they
1321 * hold the top-level event's child_mutex, so any descendant that
1322 * goes to exit will block in sync_child_event.
1323 * When called from perf_pending_event it's OK because event->ctx
1324 * is the current context on this CPU and preemption is disabled,
1325 * hence we can't get into perf_event_task_sched_out for this context.
1327 void perf_event_disable(struct perf_event *event)
1329 struct perf_event_context *ctx = event->ctx;
1330 struct task_struct *task = ctx->task;
1334 * Disable the event on the cpu that it's on
1336 cpu_function_call(event->cpu, __perf_event_disable, event);
1341 if (!task_function_call(task, __perf_event_disable, event))
1344 raw_spin_lock_irq(&ctx->lock);
1346 * If the event is still active, we need to retry the cross-call.
1348 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1349 raw_spin_unlock_irq(&ctx->lock);
1351 * Reload the task pointer, it might have been changed by
1352 * a concurrent perf_event_context_sched_out().
1359 * Since we have the lock this context can't be scheduled
1360 * in, so we can change the state safely.
1362 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1363 update_group_times(event);
1364 event->state = PERF_EVENT_STATE_OFF;
1366 raw_spin_unlock_irq(&ctx->lock);
1369 static void perf_set_shadow_time(struct perf_event *event,
1370 struct perf_event_context *ctx,
1374 * use the correct time source for the time snapshot
1376 * We could get by without this by leveraging the
1377 * fact that to get to this function, the caller
1378 * has most likely already called update_context_time()
1379 * and update_cgrp_time_xx() and thus both timestamp
1380 * are identical (or very close). Given that tstamp is,
1381 * already adjusted for cgroup, we could say that:
1382 * tstamp - ctx->timestamp
1384 * tstamp - cgrp->timestamp.
1386 * Then, in perf_output_read(), the calculation would
1387 * work with no changes because:
1388 * - event is guaranteed scheduled in
1389 * - no scheduled out in between
1390 * - thus the timestamp would be the same
1392 * But this is a bit hairy.
1394 * So instead, we have an explicit cgroup call to remain
1395 * within the time time source all along. We believe it
1396 * is cleaner and simpler to understand.
1398 if (is_cgroup_event(event))
1399 perf_cgroup_set_shadow_time(event, tstamp);
1401 event->shadow_ctx_time = tstamp - ctx->timestamp;
1404 #define MAX_INTERRUPTS (~0ULL)
1406 static void perf_log_throttle(struct perf_event *event, int enable);
1409 event_sched_in(struct perf_event *event,
1410 struct perf_cpu_context *cpuctx,
1411 struct perf_event_context *ctx)
1413 u64 tstamp = perf_event_time(event);
1415 if (event->state <= PERF_EVENT_STATE_OFF)
1418 event->state = PERF_EVENT_STATE_ACTIVE;
1419 event->oncpu = smp_processor_id();
1422 * Unthrottle events, since we scheduled we might have missed several
1423 * ticks already, also for a heavily scheduling task there is little
1424 * guarantee it'll get a tick in a timely manner.
1426 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1427 perf_log_throttle(event, 1);
1428 event->hw.interrupts = 0;
1432 * The new state must be visible before we turn it on in the hardware:
1436 if (event->pmu->add(event, PERF_EF_START)) {
1437 event->state = PERF_EVENT_STATE_INACTIVE;
1442 event->tstamp_running += tstamp - event->tstamp_stopped;
1444 perf_set_shadow_time(event, ctx, tstamp);
1446 if (!is_software_event(event))
1447 cpuctx->active_oncpu++;
1450 if (event->attr.exclusive)
1451 cpuctx->exclusive = 1;
1457 group_sched_in(struct perf_event *group_event,
1458 struct perf_cpu_context *cpuctx,
1459 struct perf_event_context *ctx)
1461 struct perf_event *event, *partial_group = NULL;
1462 struct pmu *pmu = group_event->pmu;
1463 u64 now = ctx->time;
1464 bool simulate = false;
1466 if (group_event->state == PERF_EVENT_STATE_OFF)
1469 pmu->start_txn(pmu);
1471 if (event_sched_in(group_event, cpuctx, ctx)) {
1472 pmu->cancel_txn(pmu);
1477 * Schedule in siblings as one group (if any):
1479 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1480 if (event_sched_in(event, cpuctx, ctx)) {
1481 partial_group = event;
1486 if (!pmu->commit_txn(pmu))
1491 * Groups can be scheduled in as one unit only, so undo any
1492 * partial group before returning:
1493 * The events up to the failed event are scheduled out normally,
1494 * tstamp_stopped will be updated.
1496 * The failed events and the remaining siblings need to have
1497 * their timings updated as if they had gone thru event_sched_in()
1498 * and event_sched_out(). This is required to get consistent timings
1499 * across the group. This also takes care of the case where the group
1500 * could never be scheduled by ensuring tstamp_stopped is set to mark
1501 * the time the event was actually stopped, such that time delta
1502 * calculation in update_event_times() is correct.
1504 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1505 if (event == partial_group)
1509 event->tstamp_running += now - event->tstamp_stopped;
1510 event->tstamp_stopped = now;
1512 event_sched_out(event, cpuctx, ctx);
1515 event_sched_out(group_event, cpuctx, ctx);
1517 pmu->cancel_txn(pmu);
1523 * Work out whether we can put this event group on the CPU now.
1525 static int group_can_go_on(struct perf_event *event,
1526 struct perf_cpu_context *cpuctx,
1530 * Groups consisting entirely of software events can always go on.
1532 if (event->group_flags & PERF_GROUP_SOFTWARE)
1535 * If an exclusive group is already on, no other hardware
1538 if (cpuctx->exclusive)
1541 * If this group is exclusive and there are already
1542 * events on the CPU, it can't go on.
1544 if (event->attr.exclusive && cpuctx->active_oncpu)
1547 * Otherwise, try to add it if all previous groups were able
1553 static void add_event_to_ctx(struct perf_event *event,
1554 struct perf_event_context *ctx)
1556 u64 tstamp = perf_event_time(event);
1558 list_add_event(event, ctx);
1559 perf_group_attach(event);
1560 event->tstamp_enabled = tstamp;
1561 event->tstamp_running = tstamp;
1562 event->tstamp_stopped = tstamp;
1565 static void task_ctx_sched_out(struct perf_event_context *ctx);
1567 ctx_sched_in(struct perf_event_context *ctx,
1568 struct perf_cpu_context *cpuctx,
1569 enum event_type_t event_type,
1570 struct task_struct *task);
1572 static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1573 struct perf_event_context *ctx,
1574 struct task_struct *task)
1576 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1578 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1579 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1581 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1585 * Cross CPU call to install and enable a performance event
1587 * Must be called with ctx->mutex held
1589 static int __perf_install_in_context(void *info)
1591 struct perf_event *event = info;
1592 struct perf_event_context *ctx = event->ctx;
1593 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1594 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1595 struct task_struct *task = current;
1597 perf_ctx_lock(cpuctx, task_ctx);
1598 perf_pmu_disable(cpuctx->ctx.pmu);
1601 * If there was an active task_ctx schedule it out.
1604 task_ctx_sched_out(task_ctx);
1607 * If the context we're installing events in is not the
1608 * active task_ctx, flip them.
1610 if (ctx->task && task_ctx != ctx) {
1612 raw_spin_unlock(&task_ctx->lock);
1613 raw_spin_lock(&ctx->lock);
1618 cpuctx->task_ctx = task_ctx;
1619 task = task_ctx->task;
1622 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
1624 update_context_time(ctx);
1626 * update cgrp time only if current cgrp
1627 * matches event->cgrp. Must be done before
1628 * calling add_event_to_ctx()
1630 update_cgrp_time_from_event(event);
1632 add_event_to_ctx(event, ctx);
1635 * Schedule everything back in
1637 perf_event_sched_in(cpuctx, task_ctx, task);
1639 perf_pmu_enable(cpuctx->ctx.pmu);
1640 perf_ctx_unlock(cpuctx, task_ctx);
1646 * Attach a performance event to a context
1648 * First we add the event to the list with the hardware enable bit
1649 * in event->hw_config cleared.
1651 * If the event is attached to a task which is on a CPU we use a smp
1652 * call to enable it in the task context. The task might have been
1653 * scheduled away, but we check this in the smp call again.
1656 perf_install_in_context(struct perf_event_context *ctx,
1657 struct perf_event *event,
1660 struct task_struct *task = ctx->task;
1662 lockdep_assert_held(&ctx->mutex);
1668 * Per cpu events are installed via an smp call and
1669 * the install is always successful.
1671 cpu_function_call(cpu, __perf_install_in_context, event);
1676 if (!task_function_call(task, __perf_install_in_context, event))
1679 raw_spin_lock_irq(&ctx->lock);
1681 * If we failed to find a running task, but find the context active now
1682 * that we've acquired the ctx->lock, retry.
1684 if (ctx->is_active) {
1685 raw_spin_unlock_irq(&ctx->lock);
1690 * Since the task isn't running, its safe to add the event, us holding
1691 * the ctx->lock ensures the task won't get scheduled in.
1693 add_event_to_ctx(event, ctx);
1694 raw_spin_unlock_irq(&ctx->lock);
1698 * Put a event into inactive state and update time fields.
1699 * Enabling the leader of a group effectively enables all
1700 * the group members that aren't explicitly disabled, so we
1701 * have to update their ->tstamp_enabled also.
1702 * Note: this works for group members as well as group leaders
1703 * since the non-leader members' sibling_lists will be empty.
1705 static void __perf_event_mark_enabled(struct perf_event *event,
1706 struct perf_event_context *ctx)
1708 struct perf_event *sub;
1709 u64 tstamp = perf_event_time(event);
1711 event->state = PERF_EVENT_STATE_INACTIVE;
1712 event->tstamp_enabled = tstamp - event->total_time_enabled;
1713 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1714 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1715 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1720 * Cross CPU call to enable a performance event
1722 static int __perf_event_enable(void *info)
1724 struct perf_event *event = info;
1725 struct perf_event_context *ctx = event->ctx;
1726 struct perf_event *leader = event->group_leader;
1727 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1731 * There's a time window between 'ctx->is_active' check
1732 * in perf_event_enable function and this place having:
1734 * - ctx->lock unlocked
1736 * where the task could be killed and 'ctx' deactivated
1737 * by perf_event_exit_task.
1739 if (!ctx->is_active)
1742 raw_spin_lock(&ctx->lock);
1743 update_context_time(ctx);
1745 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1749 * set current task's cgroup time reference point
1751 perf_cgroup_set_timestamp(current, ctx);
1753 __perf_event_mark_enabled(event, ctx);
1755 if (!event_filter_match(event)) {
1756 if (is_cgroup_event(event))
1757 perf_cgroup_defer_enabled(event);
1762 * If the event is in a group and isn't the group leader,
1763 * then don't put it on unless the group is on.
1765 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1768 if (!group_can_go_on(event, cpuctx, 1)) {
1771 if (event == leader)
1772 err = group_sched_in(event, cpuctx, ctx);
1774 err = event_sched_in(event, cpuctx, ctx);
1779 * If this event can't go on and it's part of a
1780 * group, then the whole group has to come off.
1782 if (leader != event)
1783 group_sched_out(leader, cpuctx, ctx);
1784 if (leader->attr.pinned) {
1785 update_group_times(leader);
1786 leader->state = PERF_EVENT_STATE_ERROR;
1791 raw_spin_unlock(&ctx->lock);
1799 * If event->ctx is a cloned context, callers must make sure that
1800 * every task struct that event->ctx->task could possibly point to
1801 * remains valid. This condition is satisfied when called through
1802 * perf_event_for_each_child or perf_event_for_each as described
1803 * for perf_event_disable.
1805 void perf_event_enable(struct perf_event *event)
1807 struct perf_event_context *ctx = event->ctx;
1808 struct task_struct *task = ctx->task;
1812 * Enable the event on the cpu that it's on
1814 cpu_function_call(event->cpu, __perf_event_enable, event);
1818 raw_spin_lock_irq(&ctx->lock);
1819 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1823 * If the event is in error state, clear that first.
1824 * That way, if we see the event in error state below, we
1825 * know that it has gone back into error state, as distinct
1826 * from the task having been scheduled away before the
1827 * cross-call arrived.
1829 if (event->state == PERF_EVENT_STATE_ERROR)
1830 event->state = PERF_EVENT_STATE_OFF;
1833 if (!ctx->is_active) {
1834 __perf_event_mark_enabled(event, ctx);
1838 raw_spin_unlock_irq(&ctx->lock);
1840 if (!task_function_call(task, __perf_event_enable, event))
1843 raw_spin_lock_irq(&ctx->lock);
1846 * If the context is active and the event is still off,
1847 * we need to retry the cross-call.
1849 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1851 * task could have been flipped by a concurrent
1852 * perf_event_context_sched_out()
1859 raw_spin_unlock_irq(&ctx->lock);
1862 int perf_event_refresh(struct perf_event *event, int refresh)
1865 * not supported on inherited events
1867 if (event->attr.inherit || !is_sampling_event(event))
1870 atomic_add(refresh, &event->event_limit);
1871 perf_event_enable(event);
1875 EXPORT_SYMBOL_GPL(perf_event_refresh);
1877 static void ctx_sched_out(struct perf_event_context *ctx,
1878 struct perf_cpu_context *cpuctx,
1879 enum event_type_t event_type)
1881 struct perf_event *event;
1882 int is_active = ctx->is_active;
1884 ctx->is_active &= ~event_type;
1885 if (likely(!ctx->nr_events))
1888 update_context_time(ctx);
1889 update_cgrp_time_from_cpuctx(cpuctx);
1890 if (!ctx->nr_active)
1893 perf_pmu_disable(ctx->pmu);
1894 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
1895 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1896 group_sched_out(event, cpuctx, ctx);
1899 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
1900 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1901 group_sched_out(event, cpuctx, ctx);
1903 perf_pmu_enable(ctx->pmu);
1907 * Test whether two contexts are equivalent, i.e. whether they
1908 * have both been cloned from the same version of the same context
1909 * and they both have the same number of enabled events.
1910 * If the number of enabled events is the same, then the set
1911 * of enabled events should be the same, because these are both
1912 * inherited contexts, therefore we can't access individual events
1913 * in them directly with an fd; we can only enable/disable all
1914 * events via prctl, or enable/disable all events in a family
1915 * via ioctl, which will have the same effect on both contexts.
1917 static int context_equiv(struct perf_event_context *ctx1,
1918 struct perf_event_context *ctx2)
1920 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1921 && ctx1->parent_gen == ctx2->parent_gen
1922 && !ctx1->pin_count && !ctx2->pin_count;
1925 static void __perf_event_sync_stat(struct perf_event *event,
1926 struct perf_event *next_event)
1930 if (!event->attr.inherit_stat)
1934 * Update the event value, we cannot use perf_event_read()
1935 * because we're in the middle of a context switch and have IRQs
1936 * disabled, which upsets smp_call_function_single(), however
1937 * we know the event must be on the current CPU, therefore we
1938 * don't need to use it.
1940 switch (event->state) {
1941 case PERF_EVENT_STATE_ACTIVE:
1942 event->pmu->read(event);
1945 case PERF_EVENT_STATE_INACTIVE:
1946 update_event_times(event);
1954 * In order to keep per-task stats reliable we need to flip the event
1955 * values when we flip the contexts.
1957 value = local64_read(&next_event->count);
1958 value = local64_xchg(&event->count, value);
1959 local64_set(&next_event->count, value);
1961 swap(event->total_time_enabled, next_event->total_time_enabled);
1962 swap(event->total_time_running, next_event->total_time_running);
1965 * Since we swizzled the values, update the user visible data too.
1967 perf_event_update_userpage(event);
1968 perf_event_update_userpage(next_event);
1971 #define list_next_entry(pos, member) \
1972 list_entry(pos->member.next, typeof(*pos), member)
1974 static void perf_event_sync_stat(struct perf_event_context *ctx,
1975 struct perf_event_context *next_ctx)
1977 struct perf_event *event, *next_event;
1982 update_context_time(ctx);
1984 event = list_first_entry(&ctx->event_list,
1985 struct perf_event, event_entry);
1987 next_event = list_first_entry(&next_ctx->event_list,
1988 struct perf_event, event_entry);
1990 while (&event->event_entry != &ctx->event_list &&
1991 &next_event->event_entry != &next_ctx->event_list) {
1993 __perf_event_sync_stat(event, next_event);
1995 event = list_next_entry(event, event_entry);
1996 next_event = list_next_entry(next_event, event_entry);
2000 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2001 struct task_struct *next)
2003 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
2004 struct perf_event_context *next_ctx;
2005 struct perf_event_context *parent;
2006 struct perf_cpu_context *cpuctx;
2012 cpuctx = __get_cpu_context(ctx);
2013 if (!cpuctx->task_ctx)
2017 parent = rcu_dereference(ctx->parent_ctx);
2018 next_ctx = next->perf_event_ctxp[ctxn];
2019 if (parent && next_ctx &&
2020 rcu_dereference(next_ctx->parent_ctx) == parent) {
2022 * Looks like the two contexts are clones, so we might be
2023 * able to optimize the context switch. We lock both
2024 * contexts and check that they are clones under the
2025 * lock (including re-checking that neither has been
2026 * uncloned in the meantime). It doesn't matter which
2027 * order we take the locks because no other cpu could
2028 * be trying to lock both of these tasks.
2030 raw_spin_lock(&ctx->lock);
2031 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
2032 if (context_equiv(ctx, next_ctx)) {
2034 * XXX do we need a memory barrier of sorts
2035 * wrt to rcu_dereference() of perf_event_ctxp
2037 task->perf_event_ctxp[ctxn] = next_ctx;
2038 next->perf_event_ctxp[ctxn] = ctx;
2040 next_ctx->task = task;
2043 perf_event_sync_stat(ctx, next_ctx);
2045 raw_spin_unlock(&next_ctx->lock);
2046 raw_spin_unlock(&ctx->lock);
2051 raw_spin_lock(&ctx->lock);
2052 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2053 cpuctx->task_ctx = NULL;
2054 raw_spin_unlock(&ctx->lock);
2058 #define for_each_task_context_nr(ctxn) \
2059 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2062 * Called from scheduler to remove the events of the current task,
2063 * with interrupts disabled.
2065 * We stop each event and update the event value in event->count.
2067 * This does not protect us against NMI, but disable()
2068 * sets the disabled bit in the control field of event _before_
2069 * accessing the event control register. If a NMI hits, then it will
2070 * not restart the event.
2072 void __perf_event_task_sched_out(struct task_struct *task,
2073 struct task_struct *next)
2077 for_each_task_context_nr(ctxn)
2078 perf_event_context_sched_out(task, ctxn, next);
2081 * if cgroup events exist on this CPU, then we need
2082 * to check if we have to switch out PMU state.
2083 * cgroup event are system-wide mode only
2085 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2086 perf_cgroup_sched_out(task, next);
2089 static void task_ctx_sched_out(struct perf_event_context *ctx)
2091 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2093 if (!cpuctx->task_ctx)
2096 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2099 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2100 cpuctx->task_ctx = NULL;
2104 * Called with IRQs disabled
2106 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2107 enum event_type_t event_type)
2109 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
2113 ctx_pinned_sched_in(struct perf_event_context *ctx,
2114 struct perf_cpu_context *cpuctx)
2116 struct perf_event *event;
2118 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2119 if (event->state <= PERF_EVENT_STATE_OFF)
2121 if (!event_filter_match(event))
2124 /* may need to reset tstamp_enabled */
2125 if (is_cgroup_event(event))
2126 perf_cgroup_mark_enabled(event, ctx);
2128 if (group_can_go_on(event, cpuctx, 1))
2129 group_sched_in(event, cpuctx, ctx);
2132 * If this pinned group hasn't been scheduled,
2133 * put it in error state.
2135 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2136 update_group_times(event);
2137 event->state = PERF_EVENT_STATE_ERROR;
2143 ctx_flexible_sched_in(struct perf_event_context *ctx,
2144 struct perf_cpu_context *cpuctx)
2146 struct perf_event *event;
2149 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2150 /* Ignore events in OFF or ERROR state */
2151 if (event->state <= PERF_EVENT_STATE_OFF)
2154 * Listen to the 'cpu' scheduling filter constraint
2157 if (!event_filter_match(event))
2160 /* may need to reset tstamp_enabled */
2161 if (is_cgroup_event(event))
2162 perf_cgroup_mark_enabled(event, ctx);
2164 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2165 if (group_sched_in(event, cpuctx, ctx))
2172 ctx_sched_in(struct perf_event_context *ctx,
2173 struct perf_cpu_context *cpuctx,
2174 enum event_type_t event_type,
2175 struct task_struct *task)
2178 int is_active = ctx->is_active;
2180 ctx->is_active |= event_type;
2181 if (likely(!ctx->nr_events))
2185 ctx->timestamp = now;
2186 perf_cgroup_set_timestamp(task, ctx);
2188 * First go through the list and put on any pinned groups
2189 * in order to give them the best chance of going on.
2191 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
2192 ctx_pinned_sched_in(ctx, cpuctx);
2194 /* Then walk through the lower prio flexible groups */
2195 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
2196 ctx_flexible_sched_in(ctx, cpuctx);
2199 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2200 enum event_type_t event_type,
2201 struct task_struct *task)
2203 struct perf_event_context *ctx = &cpuctx->ctx;
2205 ctx_sched_in(ctx, cpuctx, event_type, task);
2208 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2209 struct task_struct *task)
2211 struct perf_cpu_context *cpuctx;
2213 cpuctx = __get_cpu_context(ctx);
2214 if (cpuctx->task_ctx == ctx)
2217 perf_ctx_lock(cpuctx, ctx);
2218 perf_pmu_disable(ctx->pmu);
2220 * We want to keep the following priority order:
2221 * cpu pinned (that don't need to move), task pinned,
2222 * cpu flexible, task flexible.
2224 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2227 cpuctx->task_ctx = ctx;
2229 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2231 perf_pmu_enable(ctx->pmu);
2232 perf_ctx_unlock(cpuctx, ctx);
2235 * Since these rotations are per-cpu, we need to ensure the
2236 * cpu-context we got scheduled on is actually rotating.
2238 perf_pmu_rotate_start(ctx->pmu);
2242 * Called from scheduler to add the events of the current task
2243 * with interrupts disabled.
2245 * We restore the event value and then enable it.
2247 * This does not protect us against NMI, but enable()
2248 * sets the enabled bit in the control field of event _before_
2249 * accessing the event control register. If a NMI hits, then it will
2250 * keep the event running.
2252 void __perf_event_task_sched_in(struct task_struct *prev,
2253 struct task_struct *task)
2255 struct perf_event_context *ctx;
2258 for_each_task_context_nr(ctxn) {
2259 ctx = task->perf_event_ctxp[ctxn];
2263 perf_event_context_sched_in(ctx, task);
2266 * if cgroup events exist on this CPU, then we need
2267 * to check if we have to switch in PMU state.
2268 * cgroup event are system-wide mode only
2270 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2271 perf_cgroup_sched_in(prev, task);
2274 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2276 u64 frequency = event->attr.sample_freq;
2277 u64 sec = NSEC_PER_SEC;
2278 u64 divisor, dividend;
2280 int count_fls, nsec_fls, frequency_fls, sec_fls;
2282 count_fls = fls64(count);
2283 nsec_fls = fls64(nsec);
2284 frequency_fls = fls64(frequency);
2288 * We got @count in @nsec, with a target of sample_freq HZ
2289 * the target period becomes:
2292 * period = -------------------
2293 * @nsec * sample_freq
2298 * Reduce accuracy by one bit such that @a and @b converge
2299 * to a similar magnitude.
2301 #define REDUCE_FLS(a, b) \
2303 if (a##_fls > b##_fls) { \
2313 * Reduce accuracy until either term fits in a u64, then proceed with
2314 * the other, so that finally we can do a u64/u64 division.
2316 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2317 REDUCE_FLS(nsec, frequency);
2318 REDUCE_FLS(sec, count);
2321 if (count_fls + sec_fls > 64) {
2322 divisor = nsec * frequency;
2324 while (count_fls + sec_fls > 64) {
2325 REDUCE_FLS(count, sec);
2329 dividend = count * sec;
2331 dividend = count * sec;
2333 while (nsec_fls + frequency_fls > 64) {
2334 REDUCE_FLS(nsec, frequency);
2338 divisor = nsec * frequency;
2344 return div64_u64(dividend, divisor);
2347 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
2349 struct hw_perf_event *hwc = &event->hw;
2350 s64 period, sample_period;
2353 period = perf_calculate_period(event, nsec, count);
2355 delta = (s64)(period - hwc->sample_period);
2356 delta = (delta + 7) / 8; /* low pass filter */
2358 sample_period = hwc->sample_period + delta;
2363 hwc->sample_period = sample_period;
2365 if (local64_read(&hwc->period_left) > 8*sample_period) {
2366 event->pmu->stop(event, PERF_EF_UPDATE);
2367 local64_set(&hwc->period_left, 0);
2368 event->pmu->start(event, PERF_EF_RELOAD);
2372 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
2374 struct perf_event *event;
2375 struct hw_perf_event *hwc;
2376 u64 interrupts, now;
2379 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2380 if (event->state != PERF_EVENT_STATE_ACTIVE)
2383 if (!event_filter_match(event))
2388 interrupts = hwc->interrupts;
2389 hwc->interrupts = 0;
2392 * unthrottle events on the tick
2394 if (interrupts == MAX_INTERRUPTS) {
2395 perf_log_throttle(event, 1);
2396 event->pmu->start(event, 0);
2399 if (!event->attr.freq || !event->attr.sample_freq)
2402 event->pmu->read(event);
2403 now = local64_read(&event->count);
2404 delta = now - hwc->freq_count_stamp;
2405 hwc->freq_count_stamp = now;
2408 perf_adjust_period(event, period, delta);
2413 * Round-robin a context's events:
2415 static void rotate_ctx(struct perf_event_context *ctx)
2418 * Rotate the first entry last of non-pinned groups. Rotation might be
2419 * disabled by the inheritance code.
2421 if (!ctx->rotate_disable)
2422 list_rotate_left(&ctx->flexible_groups);
2426 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2427 * because they're strictly cpu affine and rotate_start is called with IRQs
2428 * disabled, while rotate_context is called from IRQ context.
2430 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
2432 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
2433 struct perf_event_context *ctx = NULL;
2434 int rotate = 0, remove = 1;
2436 if (cpuctx->ctx.nr_events) {
2438 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2442 ctx = cpuctx->task_ctx;
2443 if (ctx && ctx->nr_events) {
2445 if (ctx->nr_events != ctx->nr_active)
2449 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2450 perf_pmu_disable(cpuctx->ctx.pmu);
2451 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
2453 perf_ctx_adjust_freq(ctx, interval);
2458 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2460 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
2462 rotate_ctx(&cpuctx->ctx);
2466 perf_event_sched_in(cpuctx, ctx, current);
2470 list_del_init(&cpuctx->rotation_list);
2472 perf_pmu_enable(cpuctx->ctx.pmu);
2473 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2476 void perf_event_task_tick(void)
2478 struct list_head *head = &__get_cpu_var(rotation_list);
2479 struct perf_cpu_context *cpuctx, *tmp;
2481 WARN_ON(!irqs_disabled());
2483 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2484 if (cpuctx->jiffies_interval == 1 ||
2485 !(jiffies % cpuctx->jiffies_interval))
2486 perf_rotate_context(cpuctx);
2490 static int event_enable_on_exec(struct perf_event *event,
2491 struct perf_event_context *ctx)
2493 if (!event->attr.enable_on_exec)
2496 event->attr.enable_on_exec = 0;
2497 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2500 __perf_event_mark_enabled(event, ctx);
2506 * Enable all of a task's events that have been marked enable-on-exec.
2507 * This expects task == current.
2509 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2511 struct perf_event *event;
2512 unsigned long flags;
2516 local_irq_save(flags);
2517 if (!ctx || !ctx->nr_events)
2521 * We must ctxsw out cgroup events to avoid conflict
2522 * when invoking perf_task_event_sched_in() later on
2523 * in this function. Otherwise we end up trying to
2524 * ctxswin cgroup events which are already scheduled
2527 perf_cgroup_sched_out(current, NULL);
2529 raw_spin_lock(&ctx->lock);
2530 task_ctx_sched_out(ctx);
2532 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2533 ret = event_enable_on_exec(event, ctx);
2538 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2539 ret = event_enable_on_exec(event, ctx);
2545 * Unclone this context if we enabled any event.
2550 raw_spin_unlock(&ctx->lock);
2553 * Also calls ctxswin for cgroup events, if any:
2555 perf_event_context_sched_in(ctx, ctx->task);
2557 local_irq_restore(flags);
2561 * Cross CPU call to read the hardware event
2563 static void __perf_event_read(void *info)
2565 struct perf_event *event = info;
2566 struct perf_event_context *ctx = event->ctx;
2567 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2570 * If this is a task context, we need to check whether it is
2571 * the current task context of this cpu. If not it has been
2572 * scheduled out before the smp call arrived. In that case
2573 * event->count would have been updated to a recent sample
2574 * when the event was scheduled out.
2576 if (ctx->task && cpuctx->task_ctx != ctx)
2579 raw_spin_lock(&ctx->lock);
2580 if (ctx->is_active) {
2581 update_context_time(ctx);
2582 update_cgrp_time_from_event(event);
2584 update_event_times(event);
2585 if (event->state == PERF_EVENT_STATE_ACTIVE)
2586 event->pmu->read(event);
2587 raw_spin_unlock(&ctx->lock);
2590 static inline u64 perf_event_count(struct perf_event *event)
2592 return local64_read(&event->count) + atomic64_read(&event->child_count);
2595 static u64 perf_event_read(struct perf_event *event)
2598 * If event is enabled and currently active on a CPU, update the
2599 * value in the event structure:
2601 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2602 smp_call_function_single(event->oncpu,
2603 __perf_event_read, event, 1);
2604 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2605 struct perf_event_context *ctx = event->ctx;
2606 unsigned long flags;
2608 raw_spin_lock_irqsave(&ctx->lock, flags);
2610 * may read while context is not active
2611 * (e.g., thread is blocked), in that case
2612 * we cannot update context time
2614 if (ctx->is_active) {
2615 update_context_time(ctx);
2616 update_cgrp_time_from_event(event);
2618 update_event_times(event);
2619 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2622 return perf_event_count(event);
2629 struct callchain_cpus_entries {
2630 struct rcu_head rcu_head;
2631 struct perf_callchain_entry *cpu_entries[0];
2634 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
2635 static atomic_t nr_callchain_events;
2636 static DEFINE_MUTEX(callchain_mutex);
2637 struct callchain_cpus_entries *callchain_cpus_entries;
2640 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
2641 struct pt_regs *regs)
2645 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
2646 struct pt_regs *regs)
2650 static void release_callchain_buffers_rcu(struct rcu_head *head)
2652 struct callchain_cpus_entries *entries;
2655 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
2657 for_each_possible_cpu(cpu)
2658 kfree(entries->cpu_entries[cpu]);
2663 static void release_callchain_buffers(void)
2665 struct callchain_cpus_entries *entries;
2667 entries = callchain_cpus_entries;
2668 rcu_assign_pointer(callchain_cpus_entries, NULL);
2669 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2672 static int alloc_callchain_buffers(void)
2676 struct callchain_cpus_entries *entries;
2679 * We can't use the percpu allocation API for data that can be
2680 * accessed from NMI. Use a temporary manual per cpu allocation
2681 * until that gets sorted out.
2683 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
2685 entries = kzalloc(size, GFP_KERNEL);
2689 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
2691 for_each_possible_cpu(cpu) {
2692 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2694 if (!entries->cpu_entries[cpu])
2698 rcu_assign_pointer(callchain_cpus_entries, entries);
2703 for_each_possible_cpu(cpu)
2704 kfree(entries->cpu_entries[cpu]);
2710 static int get_callchain_buffers(void)
2715 mutex_lock(&callchain_mutex);
2717 count = atomic_inc_return(&nr_callchain_events);
2718 if (WARN_ON_ONCE(count < 1)) {
2724 /* If the allocation failed, give up */
2725 if (!callchain_cpus_entries)
2730 err = alloc_callchain_buffers();
2732 release_callchain_buffers();
2734 mutex_unlock(&callchain_mutex);
2739 static void put_callchain_buffers(void)
2741 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2742 release_callchain_buffers();
2743 mutex_unlock(&callchain_mutex);
2747 static int get_recursion_context(int *recursion)
2755 else if (in_softirq())
2760 if (recursion[rctx])
2769 static inline void put_recursion_context(int *recursion, int rctx)
2775 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2778 struct callchain_cpus_entries *entries;
2780 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2784 entries = rcu_dereference(callchain_cpus_entries);
2788 cpu = smp_processor_id();
2790 return &entries->cpu_entries[cpu][*rctx];
2794 put_callchain_entry(int rctx)
2796 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2799 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2802 struct perf_callchain_entry *entry;
2805 entry = get_callchain_entry(&rctx);
2814 if (!user_mode(regs)) {
2815 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2816 perf_callchain_kernel(entry, regs);
2818 regs = task_pt_regs(current);
2824 perf_callchain_store(entry, PERF_CONTEXT_USER);
2825 perf_callchain_user(entry, regs);
2829 put_callchain_entry(rctx);
2835 * Initialize the perf_event context in a task_struct:
2837 static void __perf_event_init_context(struct perf_event_context *ctx)
2839 raw_spin_lock_init(&ctx->lock);
2840 mutex_init(&ctx->mutex);
2841 INIT_LIST_HEAD(&ctx->pinned_groups);
2842 INIT_LIST_HEAD(&ctx->flexible_groups);
2843 INIT_LIST_HEAD(&ctx->event_list);
2844 atomic_set(&ctx->refcount, 1);
2847 static struct perf_event_context *
2848 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2850 struct perf_event_context *ctx;
2852 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2856 __perf_event_init_context(ctx);
2859 get_task_struct(task);
2866 static struct task_struct *
2867 find_lively_task_by_vpid(pid_t vpid)
2869 struct task_struct *task;
2876 task = find_task_by_vpid(vpid);
2878 get_task_struct(task);
2882 return ERR_PTR(-ESRCH);
2884 /* Reuse ptrace permission checks for now. */
2886 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2891 put_task_struct(task);
2892 return ERR_PTR(err);
2897 * Returns a matching context with refcount and pincount.
2899 static struct perf_event_context *
2900 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2902 struct perf_event_context *ctx;
2903 struct perf_cpu_context *cpuctx;
2904 unsigned long flags;
2908 /* Must be root to operate on a CPU event: */
2909 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2910 return ERR_PTR(-EACCES);
2913 * We could be clever and allow to attach a event to an
2914 * offline CPU and activate it when the CPU comes up, but
2917 if (!cpu_online(cpu))
2918 return ERR_PTR(-ENODEV);
2920 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2929 ctxn = pmu->task_ctx_nr;
2934 ctx = perf_lock_task_context(task, ctxn, &flags);
2938 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2940 ctx = alloc_perf_context(pmu, task);
2946 mutex_lock(&task->perf_event_mutex);
2948 * If it has already passed perf_event_exit_task().
2949 * we must see PF_EXITING, it takes this mutex too.
2951 if (task->flags & PF_EXITING)
2953 else if (task->perf_event_ctxp[ctxn])
2958 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2960 mutex_unlock(&task->perf_event_mutex);
2962 if (unlikely(err)) {
2974 return ERR_PTR(err);
2977 static void perf_event_free_filter(struct perf_event *event);
2979 static void free_event_rcu(struct rcu_head *head)
2981 struct perf_event *event;
2983 event = container_of(head, struct perf_event, rcu_head);
2985 put_pid_ns(event->ns);
2986 perf_event_free_filter(event);
2990 static void ring_buffer_put(struct ring_buffer *rb);
2991 static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb);
2993 static void free_event(struct perf_event *event)
2995 irq_work_sync(&event->pending);
2997 if (!event->parent) {
2998 if (event->attach_state & PERF_ATTACH_TASK)
2999 jump_label_dec(&perf_sched_events);
3000 if (event->attr.mmap || event->attr.mmap_data)
3001 atomic_dec(&nr_mmap_events);
3002 if (event->attr.comm)
3003 atomic_dec(&nr_comm_events);
3004 if (event->attr.task)
3005 atomic_dec(&nr_task_events);
3006 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3007 put_callchain_buffers();
3008 if (is_cgroup_event(event)) {
3009 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
3010 jump_label_dec(&perf_sched_events);
3015 struct ring_buffer *rb;
3018 * Can happen when we close an event with re-directed output.
3020 * Since we have a 0 refcount, perf_mmap_close() will skip
3021 * over us; possibly making our ring_buffer_put() the last.
3023 mutex_lock(&event->mmap_mutex);
3026 rcu_assign_pointer(event->rb, NULL);
3027 ring_buffer_detach(event, rb);
3028 ring_buffer_put(rb); /* could be last */
3030 mutex_unlock(&event->mmap_mutex);
3033 if (is_cgroup_event(event))
3034 perf_detach_cgroup(event);
3037 event->destroy(event);
3040 put_ctx(event->ctx);
3042 call_rcu(&event->rcu_head, free_event_rcu);
3045 int perf_event_release_kernel(struct perf_event *event)
3047 struct perf_event_context *ctx = event->ctx;
3049 WARN_ON_ONCE(ctx->parent_ctx);
3051 * There are two ways this annotation is useful:
3053 * 1) there is a lock recursion from perf_event_exit_task
3054 * see the comment there.
3056 * 2) there is a lock-inversion with mmap_sem through
3057 * perf_event_read_group(), which takes faults while
3058 * holding ctx->mutex, however this is called after
3059 * the last filedesc died, so there is no possibility
3060 * to trigger the AB-BA case.
3062 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
3063 perf_remove_from_context(event, true);
3064 mutex_unlock(&ctx->mutex);
3070 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3073 * Called when the last reference to the file is gone.
3075 static void put_event(struct perf_event *event)
3077 struct task_struct *owner;
3079 if (!atomic_long_dec_and_test(&event->refcount))
3083 owner = ACCESS_ONCE(event->owner);
3085 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3086 * !owner it means the list deletion is complete and we can indeed
3087 * free this event, otherwise we need to serialize on
3088 * owner->perf_event_mutex.
3090 smp_read_barrier_depends();
3093 * Since delayed_put_task_struct() also drops the last
3094 * task reference we can safely take a new reference
3095 * while holding the rcu_read_lock().
3097 get_task_struct(owner);
3102 mutex_lock(&owner->perf_event_mutex);
3104 * We have to re-check the event->owner field, if it is cleared
3105 * we raced with perf_event_exit_task(), acquiring the mutex
3106 * ensured they're done, and we can proceed with freeing the
3110 list_del_init(&event->owner_entry);
3111 mutex_unlock(&owner->perf_event_mutex);
3112 put_task_struct(owner);
3115 perf_event_release_kernel(event);
3118 static int perf_release(struct inode *inode, struct file *file)
3120 put_event(file->private_data);
3124 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
3126 struct perf_event *child;
3132 mutex_lock(&event->child_mutex);
3133 total += perf_event_read(event);
3134 *enabled += event->total_time_enabled +
3135 atomic64_read(&event->child_total_time_enabled);
3136 *running += event->total_time_running +
3137 atomic64_read(&event->child_total_time_running);
3139 list_for_each_entry(child, &event->child_list, child_list) {
3140 total += perf_event_read(child);
3141 *enabled += child->total_time_enabled;
3142 *running += child->total_time_running;
3144 mutex_unlock(&event->child_mutex);
3148 EXPORT_SYMBOL_GPL(perf_event_read_value);
3150 static int perf_event_read_group(struct perf_event *event,
3151 u64 read_format, char __user *buf)
3153 struct perf_event *leader = event->group_leader, *sub;
3154 int n = 0, size = 0, ret = -EFAULT;
3155 struct perf_event_context *ctx = leader->ctx;
3157 u64 count, enabled, running;
3159 mutex_lock(&ctx->mutex);
3160 count = perf_event_read_value(leader, &enabled, &running);
3162 values[n++] = 1 + leader->nr_siblings;
3163 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3164 values[n++] = enabled;
3165 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3166 values[n++] = running;
3167 values[n++] = count;
3168 if (read_format & PERF_FORMAT_ID)
3169 values[n++] = primary_event_id(leader);
3171 size = n * sizeof(u64);
3173 if (copy_to_user(buf, values, size))
3178 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3181 values[n++] = perf_event_read_value(sub, &enabled, &running);
3182 if (read_format & PERF_FORMAT_ID)
3183 values[n++] = primary_event_id(sub);
3185 size = n * sizeof(u64);
3187 if (copy_to_user(buf + ret, values, size)) {
3195 mutex_unlock(&ctx->mutex);
3200 static int perf_event_read_one(struct perf_event *event,
3201 u64 read_format, char __user *buf)
3203 u64 enabled, running;
3207 values[n++] = perf_event_read_value(event, &enabled, &running);
3208 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3209 values[n++] = enabled;
3210 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3211 values[n++] = running;
3212 if (read_format & PERF_FORMAT_ID)
3213 values[n++] = primary_event_id(event);
3215 if (copy_to_user(buf, values, n * sizeof(u64)))
3218 return n * sizeof(u64);
3222 * Read the performance event - simple non blocking version for now
3225 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3227 u64 read_format = event->attr.read_format;
3231 * Return end-of-file for a read on a event that is in
3232 * error state (i.e. because it was pinned but it couldn't be
3233 * scheduled on to the CPU at some point).
3235 if (event->state == PERF_EVENT_STATE_ERROR)
3238 if (count < event->read_size)
3241 WARN_ON_ONCE(event->ctx->parent_ctx);
3242 if (read_format & PERF_FORMAT_GROUP)
3243 ret = perf_event_read_group(event, read_format, buf);
3245 ret = perf_event_read_one(event, read_format, buf);
3251 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3253 struct perf_event *event = file->private_data;
3255 return perf_read_hw(event, buf, count);
3258 static unsigned int perf_poll(struct file *file, poll_table *wait)
3260 struct perf_event *event = file->private_data;
3261 struct ring_buffer *rb;
3262 unsigned int events = POLL_HUP;
3265 * Pin the event->rb by taking event->mmap_mutex; otherwise
3266 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
3268 mutex_lock(&event->mmap_mutex);
3271 events = atomic_xchg(&rb->poll, 0);
3272 mutex_unlock(&event->mmap_mutex);
3274 poll_wait(file, &event->waitq, wait);
3279 static void perf_event_reset(struct perf_event *event)
3281 (void)perf_event_read(event);
3282 local64_set(&event->count, 0);
3283 perf_event_update_userpage(event);
3287 * Holding the top-level event's child_mutex means that any
3288 * descendant process that has inherited this event will block
3289 * in sync_child_event if it goes to exit, thus satisfying the
3290 * task existence requirements of perf_event_enable/disable.
3292 static void perf_event_for_each_child(struct perf_event *event,
3293 void (*func)(struct perf_event *))
3295 struct perf_event *child;
3297 WARN_ON_ONCE(event->ctx->parent_ctx);
3298 mutex_lock(&event->child_mutex);
3300 list_for_each_entry(child, &event->child_list, child_list)
3302 mutex_unlock(&event->child_mutex);
3305 static void perf_event_for_each(struct perf_event *event,
3306 void (*func)(struct perf_event *))
3308 struct perf_event_context *ctx = event->ctx;
3309 struct perf_event *sibling;
3311 WARN_ON_ONCE(ctx->parent_ctx);
3312 mutex_lock(&ctx->mutex);
3313 event = event->group_leader;
3315 perf_event_for_each_child(event, func);
3317 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3318 perf_event_for_each_child(event, func);
3319 mutex_unlock(&ctx->mutex);
3322 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3324 struct perf_event_context *ctx = event->ctx;
3328 if (!is_sampling_event(event))
3331 if (copy_from_user(&value, arg, sizeof(value)))
3337 raw_spin_lock_irq(&ctx->lock);
3338 if (event->attr.freq) {
3339 if (value > sysctl_perf_event_sample_rate) {
3344 event->attr.sample_freq = value;
3346 event->attr.sample_period = value;
3347 event->hw.sample_period = value;
3350 raw_spin_unlock_irq(&ctx->lock);
3355 static const struct file_operations perf_fops;
3357 static struct file *perf_fget_light(int fd, int *fput_needed)
3361 file = fget_light(fd, fput_needed);
3363 return ERR_PTR(-EBADF);
3365 if (file->f_op != &perf_fops) {
3366 fput_light(file, *fput_needed);
3368 return ERR_PTR(-EBADF);
3374 static int perf_event_set_output(struct perf_event *event,
3375 struct perf_event *output_event);
3376 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3378 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3380 struct perf_event *event = file->private_data;
3381 void (*func)(struct perf_event *);
3385 case PERF_EVENT_IOC_ENABLE:
3386 func = perf_event_enable;
3388 case PERF_EVENT_IOC_DISABLE:
3389 func = perf_event_disable;
3391 case PERF_EVENT_IOC_RESET:
3392 func = perf_event_reset;
3395 case PERF_EVENT_IOC_REFRESH:
3396 return perf_event_refresh(event, arg);
3398 case PERF_EVENT_IOC_PERIOD:
3399 return perf_event_period(event, (u64 __user *)arg);
3401 case PERF_EVENT_IOC_SET_OUTPUT:
3403 struct file *output_file = NULL;
3404 struct perf_event *output_event = NULL;
3405 int fput_needed = 0;
3409 output_file = perf_fget_light(arg, &fput_needed);
3410 if (IS_ERR(output_file))
3411 return PTR_ERR(output_file);
3412 output_event = output_file->private_data;
3415 ret = perf_event_set_output(event, output_event);
3417 fput_light(output_file, fput_needed);
3422 case PERF_EVENT_IOC_SET_FILTER:
3423 return perf_event_set_filter(event, (void __user *)arg);
3429 if (flags & PERF_IOC_FLAG_GROUP)
3430 perf_event_for_each(event, func);
3432 perf_event_for_each_child(event, func);
3437 int perf_event_task_enable(void)
3439 struct perf_event *event;
3441 mutex_lock(¤t->perf_event_mutex);
3442 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3443 perf_event_for_each_child(event, perf_event_enable);
3444 mutex_unlock(¤t->perf_event_mutex);
3449 int perf_event_task_disable(void)
3451 struct perf_event *event;
3453 mutex_lock(¤t->perf_event_mutex);
3454 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3455 perf_event_for_each_child(event, perf_event_disable);
3456 mutex_unlock(¤t->perf_event_mutex);
3461 #ifndef PERF_EVENT_INDEX_OFFSET
3462 # define PERF_EVENT_INDEX_OFFSET 0
3465 static int perf_event_index(struct perf_event *event)
3467 if (event->hw.state & PERF_HES_STOPPED)
3470 if (event->state != PERF_EVENT_STATE_ACTIVE)
3473 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
3476 static void calc_timer_values(struct perf_event *event,
3483 ctx_time = event->shadow_ctx_time + now;
3484 *enabled = ctx_time - event->tstamp_enabled;
3485 *running = ctx_time - event->tstamp_running;
3489 * Callers need to ensure there can be no nesting of this function, otherwise
3490 * the seqlock logic goes bad. We can not serialize this because the arch
3491 * code calls this from NMI context.
3493 void perf_event_update_userpage(struct perf_event *event)
3495 struct perf_event_mmap_page *userpg;
3496 struct ring_buffer *rb;
3497 u64 enabled, running;
3501 * compute total_time_enabled, total_time_running
3502 * based on snapshot values taken when the event
3503 * was last scheduled in.
3505 * we cannot simply called update_context_time()
3506 * because of locking issue as we can be called in
3509 calc_timer_values(event, &enabled, &running);
3510 rb = rcu_dereference(event->rb);
3514 userpg = rb->user_page;
3517 * Disable preemption so as to not let the corresponding user-space
3518 * spin too long if we get preempted.
3523 userpg->index = perf_event_index(event);
3524 userpg->offset = perf_event_count(event);
3525 if (event->state == PERF_EVENT_STATE_ACTIVE)
3526 userpg->offset -= local64_read(&event->hw.prev_count);
3528 userpg->time_enabled = enabled +
3529 atomic64_read(&event->child_total_time_enabled);
3531 userpg->time_running = running +
3532 atomic64_read(&event->child_total_time_running);
3541 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3543 struct perf_event *event = vma->vm_file->private_data;
3544 struct ring_buffer *rb;
3545 int ret = VM_FAULT_SIGBUS;
3547 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3548 if (vmf->pgoff == 0)
3554 rb = rcu_dereference(event->rb);
3558 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3561 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
3565 get_page(vmf->page);
3566 vmf->page->mapping = vma->vm_file->f_mapping;
3567 vmf->page->index = vmf->pgoff;
3576 static void ring_buffer_attach(struct perf_event *event,
3577 struct ring_buffer *rb)
3579 unsigned long flags;
3581 if (!list_empty(&event->rb_entry))
3584 spin_lock_irqsave(&rb->event_lock, flags);
3585 if (list_empty(&event->rb_entry))
3586 list_add(&event->rb_entry, &rb->event_list);
3587 spin_unlock_irqrestore(&rb->event_lock, flags);
3590 static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb)
3592 unsigned long flags;
3594 if (list_empty(&event->rb_entry))
3597 spin_lock_irqsave(&rb->event_lock, flags);
3598 list_del_init(&event->rb_entry);
3599 wake_up_all(&event->waitq);
3600 spin_unlock_irqrestore(&rb->event_lock, flags);
3603 static void ring_buffer_wakeup(struct perf_event *event)
3605 struct ring_buffer *rb;
3608 rb = rcu_dereference(event->rb);
3610 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3611 wake_up_all(&event->waitq);
3616 static void rb_free_rcu(struct rcu_head *rcu_head)
3618 struct ring_buffer *rb;
3620 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3624 static struct ring_buffer *ring_buffer_get(struct perf_event *event)
3626 struct ring_buffer *rb;
3629 rb = rcu_dereference(event->rb);
3631 if (!atomic_inc_not_zero(&rb->refcount))
3639 static void ring_buffer_put(struct ring_buffer *rb)
3641 if (!atomic_dec_and_test(&rb->refcount))
3644 WARN_ON_ONCE(!list_empty(&rb->event_list));
3646 call_rcu(&rb->rcu_head, rb_free_rcu);
3649 static void perf_mmap_open(struct vm_area_struct *vma)
3651 struct perf_event *event = vma->vm_file->private_data;
3653 atomic_inc(&event->mmap_count);
3654 atomic_inc(&event->rb->mmap_count);
3658 * A buffer can be mmap()ed multiple times; either directly through the same
3659 * event, or through other events by use of perf_event_set_output().
3661 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3662 * the buffer here, where we still have a VM context. This means we need
3663 * to detach all events redirecting to us.
3665 static void perf_mmap_close(struct vm_area_struct *vma)
3667 struct perf_event *event = vma->vm_file->private_data;
3669 struct ring_buffer *rb = event->rb;
3670 struct user_struct *mmap_user = rb->mmap_user;
3671 int mmap_locked = rb->mmap_locked;
3672 unsigned long size = perf_data_size(rb);
3674 atomic_dec(&rb->mmap_count);
3676 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
3679 /* Detach current event from the buffer. */
3680 rcu_assign_pointer(event->rb, NULL);
3681 ring_buffer_detach(event, rb);
3682 mutex_unlock(&event->mmap_mutex);
3684 /* If there's still other mmap()s of this buffer, we're done. */
3685 if (atomic_read(&rb->mmap_count)) {
3686 ring_buffer_put(rb); /* can't be last */
3691 * No other mmap()s, detach from all other events that might redirect
3692 * into the now unreachable buffer. Somewhat complicated by the
3693 * fact that rb::event_lock otherwise nests inside mmap_mutex.
3697 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
3698 if (!atomic_long_inc_not_zero(&event->refcount)) {
3700 * This event is en-route to free_event() which will
3701 * detach it and remove it from the list.
3707 mutex_lock(&event->mmap_mutex);
3709 * Check we didn't race with perf_event_set_output() which can
3710 * swizzle the rb from under us while we were waiting to
3711 * acquire mmap_mutex.
3713 * If we find a different rb; ignore this event, a next
3714 * iteration will no longer find it on the list. We have to
3715 * still restart the iteration to make sure we're not now
3716 * iterating the wrong list.
3718 if (event->rb == rb) {
3719 rcu_assign_pointer(event->rb, NULL);
3720 ring_buffer_detach(event, rb);
3721 ring_buffer_put(rb); /* can't be last, we still have one */
3723 mutex_unlock(&event->mmap_mutex);
3727 * Restart the iteration; either we're on the wrong list or
3728 * destroyed its integrity by doing a deletion.
3735 * It could be there's still a few 0-ref events on the list; they'll
3736 * get cleaned up by free_event() -- they'll also still have their
3737 * ref on the rb and will free it whenever they are done with it.
3739 * Aside from that, this buffer is 'fully' detached and unmapped,
3740 * undo the VM accounting.
3743 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
3744 vma->vm_mm->pinned_vm -= mmap_locked;
3745 free_uid(mmap_user);
3747 ring_buffer_put(rb); /* could be last */
3750 static const struct vm_operations_struct perf_mmap_vmops = {
3751 .open = perf_mmap_open,
3752 .close = perf_mmap_close,
3753 .fault = perf_mmap_fault,
3754 .page_mkwrite = perf_mmap_fault,
3757 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3759 struct perf_event *event = file->private_data;
3760 unsigned long user_locked, user_lock_limit;
3761 struct user_struct *user = current_user();
3762 unsigned long locked, lock_limit;
3763 struct ring_buffer *rb;
3764 unsigned long vma_size;
3765 unsigned long nr_pages;
3766 long user_extra, extra;
3767 int ret = 0, flags = 0;
3770 * Don't allow mmap() of inherited per-task counters. This would
3771 * create a performance issue due to all children writing to the
3774 if (event->cpu == -1 && event->attr.inherit)
3777 if (!(vma->vm_flags & VM_SHARED))
3780 vma_size = vma->vm_end - vma->vm_start;
3781 nr_pages = (vma_size / PAGE_SIZE) - 1;
3784 * If we have rb pages ensure they're a power-of-two number, so we
3785 * can do bitmasks instead of modulo.
3787 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3790 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3793 if (vma->vm_pgoff != 0)
3796 WARN_ON_ONCE(event->ctx->parent_ctx);
3798 mutex_lock(&event->mmap_mutex);
3800 if (event->rb->nr_pages != nr_pages) {
3805 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
3807 * Raced against perf_mmap_close() through
3808 * perf_event_set_output(). Try again, hope for better
3811 mutex_unlock(&event->mmap_mutex);
3818 user_extra = nr_pages + 1;
3819 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3822 * Increase the limit linearly with more CPUs:
3824 user_lock_limit *= num_online_cpus();
3826 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3829 if (user_locked > user_lock_limit)
3830 extra = user_locked - user_lock_limit;
3832 lock_limit = rlimit(RLIMIT_MEMLOCK);
3833 lock_limit >>= PAGE_SHIFT;
3834 locked = vma->vm_mm->pinned_vm + extra;
3836 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3837 !capable(CAP_IPC_LOCK)) {
3844 if (vma->vm_flags & VM_WRITE)
3845 flags |= RING_BUFFER_WRITABLE;
3847 rb = rb_alloc(nr_pages,
3848 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3856 atomic_set(&rb->mmap_count, 1);
3857 rb->mmap_locked = extra;
3858 rb->mmap_user = get_current_user();
3860 atomic_long_add(user_extra, &user->locked_vm);
3861 vma->vm_mm->pinned_vm += extra;
3863 ring_buffer_attach(event, rb);
3864 rcu_assign_pointer(event->rb, rb);
3868 atomic_inc(&event->mmap_count);
3869 mutex_unlock(&event->mmap_mutex);
3872 * Since pinned accounting is per vm we cannot allow fork() to copy our
3875 vma->vm_flags |= VM_DONTCOPY | VM_RESERVED;
3876 vma->vm_ops = &perf_mmap_vmops;
3881 static int perf_fasync(int fd, struct file *filp, int on)
3883 struct inode *inode = filp->f_path.dentry->d_inode;
3884 struct perf_event *event = filp->private_data;
3887 mutex_lock(&inode->i_mutex);
3888 retval = fasync_helper(fd, filp, on, &event->fasync);
3889 mutex_unlock(&inode->i_mutex);
3897 static const struct file_operations perf_fops = {
3898 .llseek = no_llseek,
3899 .release = perf_release,
3902 .unlocked_ioctl = perf_ioctl,
3903 .compat_ioctl = perf_ioctl,
3905 .fasync = perf_fasync,
3911 * If there's data, ensure we set the poll() state and publish everything
3912 * to user-space before waking everybody up.
3915 void perf_event_wakeup(struct perf_event *event)
3917 ring_buffer_wakeup(event);
3919 if (event->pending_kill) {
3920 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3921 event->pending_kill = 0;
3925 static void perf_pending_event(struct irq_work *entry)
3927 struct perf_event *event = container_of(entry,
3928 struct perf_event, pending);
3930 if (event->pending_disable) {
3931 event->pending_disable = 0;
3932 __perf_event_disable(event);
3935 if (event->pending_wakeup) {
3936 event->pending_wakeup = 0;
3937 perf_event_wakeup(event);
3942 * We assume there is only KVM supporting the callbacks.
3943 * Later on, we might change it to a list if there is
3944 * another virtualization implementation supporting the callbacks.
3946 struct perf_guest_info_callbacks *perf_guest_cbs;
3948 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3950 perf_guest_cbs = cbs;
3953 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3955 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3957 perf_guest_cbs = NULL;
3960 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3962 static void __perf_event_header__init_id(struct perf_event_header *header,
3963 struct perf_sample_data *data,
3964 struct perf_event *event)
3966 u64 sample_type = event->attr.sample_type;
3968 data->type = sample_type;
3969 header->size += event->id_header_size;
3971 if (sample_type & PERF_SAMPLE_TID) {
3972 /* namespace issues */
3973 data->tid_entry.pid = perf_event_pid(event, current);
3974 data->tid_entry.tid = perf_event_tid(event, current);
3977 if (sample_type & PERF_SAMPLE_TIME)
3978 data->time = perf_clock();
3980 if (sample_type & PERF_SAMPLE_ID)
3981 data->id = primary_event_id(event);
3983 if (sample_type & PERF_SAMPLE_STREAM_ID)
3984 data->stream_id = event->id;
3986 if (sample_type & PERF_SAMPLE_CPU) {
3987 data->cpu_entry.cpu = raw_smp_processor_id();
3988 data->cpu_entry.reserved = 0;
3992 void perf_event_header__init_id(struct perf_event_header *header,
3993 struct perf_sample_data *data,
3994 struct perf_event *event)
3996 if (event->attr.sample_id_all)
3997 __perf_event_header__init_id(header, data, event);
4000 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4001 struct perf_sample_data *data)
4003 u64 sample_type = data->type;
4005 if (sample_type & PERF_SAMPLE_TID)
4006 perf_output_put(handle, data->tid_entry);
4008 if (sample_type & PERF_SAMPLE_TIME)
4009 perf_output_put(handle, data->time);
4011 if (sample_type & PERF_SAMPLE_ID)
4012 perf_output_put(handle, data->id);
4014 if (sample_type & PERF_SAMPLE_STREAM_ID)
4015 perf_output_put(handle, data->stream_id);
4017 if (sample_type & PERF_SAMPLE_CPU)
4018 perf_output_put(handle, data->cpu_entry);
4021 void perf_event__output_id_sample(struct perf_event *event,
4022 struct perf_output_handle *handle,
4023 struct perf_sample_data *sample)
4025 if (event->attr.sample_id_all)
4026 __perf_event__output_id_sample(handle, sample);
4029 static void perf_output_read_one(struct perf_output_handle *handle,
4030 struct perf_event *event,
4031 u64 enabled, u64 running)
4033 u64 read_format = event->attr.read_format;
4037 values[n++] = perf_event_count(event);
4038 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4039 values[n++] = enabled +
4040 atomic64_read(&event->child_total_time_enabled);
4042 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4043 values[n++] = running +
4044 atomic64_read(&event->child_total_time_running);
4046 if (read_format & PERF_FORMAT_ID)
4047 values[n++] = primary_event_id(event);
4049 __output_copy(handle, values, n * sizeof(u64));
4053 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4055 static void perf_output_read_group(struct perf_output_handle *handle,
4056 struct perf_event *event,
4057 u64 enabled, u64 running)
4059 struct perf_event *leader = event->group_leader, *sub;
4060 u64 read_format = event->attr.read_format;
4064 values[n++] = 1 + leader->nr_siblings;
4066 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4067 values[n++] = enabled;
4069 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4070 values[n++] = running;
4072 if (leader != event)
4073 leader->pmu->read(leader);
4075 values[n++] = perf_event_count(leader);
4076 if (read_format & PERF_FORMAT_ID)
4077 values[n++] = primary_event_id(leader);
4079 __output_copy(handle, values, n * sizeof(u64));
4081 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4085 sub->pmu->read(sub);
4087 values[n++] = perf_event_count(sub);
4088 if (read_format & PERF_FORMAT_ID)
4089 values[n++] = primary_event_id(sub);
4091 __output_copy(handle, values, n * sizeof(u64));
4095 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4096 PERF_FORMAT_TOTAL_TIME_RUNNING)
4098 static void perf_output_read(struct perf_output_handle *handle,
4099 struct perf_event *event)
4101 u64 enabled = 0, running = 0;
4102 u64 read_format = event->attr.read_format;
4105 * compute total_time_enabled, total_time_running
4106 * based on snapshot values taken when the event
4107 * was last scheduled in.
4109 * we cannot simply called update_context_time()
4110 * because of locking issue as we are called in
4113 if (read_format & PERF_FORMAT_TOTAL_TIMES)
4114 calc_timer_values(event, &enabled, &running);
4116 if (event->attr.read_format & PERF_FORMAT_GROUP)
4117 perf_output_read_group(handle, event, enabled, running);
4119 perf_output_read_one(handle, event, enabled, running);
4122 void perf_output_sample(struct perf_output_handle *handle,
4123 struct perf_event_header *header,
4124 struct perf_sample_data *data,
4125 struct perf_event *event)
4127 u64 sample_type = data->type;
4129 perf_output_put(handle, *header);
4131 if (sample_type & PERF_SAMPLE_IP)
4132 perf_output_put(handle, data->ip);
4134 if (sample_type & PERF_SAMPLE_TID)
4135 perf_output_put(handle, data->tid_entry);
4137 if (sample_type & PERF_SAMPLE_TIME)
4138 perf_output_put(handle, data->time);
4140 if (sample_type & PERF_SAMPLE_ADDR)
4141 perf_output_put(handle, data->addr);
4143 if (sample_type & PERF_SAMPLE_ID)
4144 perf_output_put(handle, data->id);
4146 if (sample_type & PERF_SAMPLE_STREAM_ID)
4147 perf_output_put(handle, data->stream_id);
4149 if (sample_type & PERF_SAMPLE_CPU)
4150 perf_output_put(handle, data->cpu_entry);
4152 if (sample_type & PERF_SAMPLE_PERIOD)
4153 perf_output_put(handle, data->period);
4155 if (sample_type & PERF_SAMPLE_READ)
4156 perf_output_read(handle, event);
4158 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4159 if (data->callchain) {
4162 if (data->callchain)
4163 size += data->callchain->nr;
4165 size *= sizeof(u64);
4167 __output_copy(handle, data->callchain, size);
4170 perf_output_put(handle, nr);
4174 if (sample_type & PERF_SAMPLE_RAW) {
4176 perf_output_put(handle, data->raw->size);
4177 __output_copy(handle, data->raw->data,
4184 .size = sizeof(u32),
4187 perf_output_put(handle, raw);
4191 if (!event->attr.watermark) {
4192 int wakeup_events = event->attr.wakeup_events;
4194 if (wakeup_events) {
4195 struct ring_buffer *rb = handle->rb;
4196 int events = local_inc_return(&rb->events);
4198 if (events >= wakeup_events) {
4199 local_sub(wakeup_events, &rb->events);
4200 local_inc(&rb->wakeup);
4206 void perf_prepare_sample(struct perf_event_header *header,
4207 struct perf_sample_data *data,
4208 struct perf_event *event,
4209 struct pt_regs *regs)
4211 u64 sample_type = event->attr.sample_type;
4213 header->type = PERF_RECORD_SAMPLE;
4214 header->size = sizeof(*header) + event->header_size;
4217 header->misc |= perf_misc_flags(regs);
4219 __perf_event_header__init_id(header, data, event);
4221 if (sample_type & PERF_SAMPLE_IP)
4222 data->ip = perf_instruction_pointer(regs);
4224 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4227 data->callchain = perf_callchain(regs);
4229 if (data->callchain)
4230 size += data->callchain->nr;
4232 header->size += size * sizeof(u64);
4235 if (sample_type & PERF_SAMPLE_RAW) {
4236 int size = sizeof(u32);
4239 size += data->raw->size;
4241 size += sizeof(u32);
4243 WARN_ON_ONCE(size & (sizeof(u64)-1));
4244 header->size += size;
4248 static void perf_event_output(struct perf_event *event,
4249 struct perf_sample_data *data,
4250 struct pt_regs *regs)
4252 struct perf_output_handle handle;
4253 struct perf_event_header header;
4255 /* protect the callchain buffers */
4258 perf_prepare_sample(&header, data, event, regs);
4260 if (perf_output_begin(&handle, event, header.size))
4263 perf_output_sample(&handle, &header, data, event);
4265 perf_output_end(&handle);
4275 struct perf_read_event {
4276 struct perf_event_header header;
4283 perf_event_read_event(struct perf_event *event,
4284 struct task_struct *task)
4286 struct perf_output_handle handle;
4287 struct perf_sample_data sample;
4288 struct perf_read_event read_event = {
4290 .type = PERF_RECORD_READ,
4292 .size = sizeof(read_event) + event->read_size,
4294 .pid = perf_event_pid(event, task),
4295 .tid = perf_event_tid(event, task),
4299 perf_event_header__init_id(&read_event.header, &sample, event);
4300 ret = perf_output_begin(&handle, event, read_event.header.size);
4304 perf_output_put(&handle, read_event);
4305 perf_output_read(&handle, event);
4306 perf_event__output_id_sample(event, &handle, &sample);
4308 perf_output_end(&handle);
4312 * task tracking -- fork/exit
4314 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4317 struct perf_task_event {
4318 struct task_struct *task;
4319 struct perf_event_context *task_ctx;
4322 struct perf_event_header header;
4332 static void perf_event_task_output(struct perf_event *event,
4333 struct perf_task_event *task_event)
4335 struct perf_output_handle handle;
4336 struct perf_sample_data sample;
4337 struct task_struct *task = task_event->task;
4338 int ret, size = task_event->event_id.header.size;
4340 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4342 ret = perf_output_begin(&handle, event,
4343 task_event->event_id.header.size);
4347 task_event->event_id.pid = perf_event_pid(event, task);
4348 task_event->event_id.ppid = perf_event_pid(event, current);
4350 task_event->event_id.tid = perf_event_tid(event, task);
4351 task_event->event_id.ptid = perf_event_tid(event, current);
4353 perf_output_put(&handle, task_event->event_id);
4355 perf_event__output_id_sample(event, &handle, &sample);
4357 perf_output_end(&handle);
4359 task_event->event_id.header.size = size;
4362 static int perf_event_task_match(struct perf_event *event)
4364 if (event->state < PERF_EVENT_STATE_INACTIVE)
4367 if (!event_filter_match(event))
4370 if (event->attr.comm || event->attr.mmap ||
4371 event->attr.mmap_data || event->attr.task)
4377 static void perf_event_task_ctx(struct perf_event_context *ctx,
4378 struct perf_task_event *task_event)
4380 struct perf_event *event;
4382 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4383 if (perf_event_task_match(event))
4384 perf_event_task_output(event, task_event);
4388 static void perf_event_task_event(struct perf_task_event *task_event)
4390 struct perf_cpu_context *cpuctx;
4391 struct perf_event_context *ctx;
4396 list_for_each_entry_rcu(pmu, &pmus, entry) {
4397 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4398 if (cpuctx->unique_pmu != pmu)
4400 perf_event_task_ctx(&cpuctx->ctx, task_event);
4402 ctx = task_event->task_ctx;
4404 ctxn = pmu->task_ctx_nr;
4407 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4410 perf_event_task_ctx(ctx, task_event);
4412 put_cpu_ptr(pmu->pmu_cpu_context);
4417 static void perf_event_task(struct task_struct *task,
4418 struct perf_event_context *task_ctx,
4421 struct perf_task_event task_event;
4423 if (!atomic_read(&nr_comm_events) &&
4424 !atomic_read(&nr_mmap_events) &&
4425 !atomic_read(&nr_task_events))
4428 task_event = (struct perf_task_event){
4430 .task_ctx = task_ctx,
4433 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4435 .size = sizeof(task_event.event_id),
4441 .time = perf_clock(),
4445 perf_event_task_event(&task_event);
4448 void perf_event_fork(struct task_struct *task)
4450 perf_event_task(task, NULL, 1);
4457 struct perf_comm_event {
4458 struct task_struct *task;
4463 struct perf_event_header header;
4470 static void perf_event_comm_output(struct perf_event *event,
4471 struct perf_comm_event *comm_event)
4473 struct perf_output_handle handle;
4474 struct perf_sample_data sample;
4475 int size = comm_event->event_id.header.size;
4478 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4479 ret = perf_output_begin(&handle, event,
4480 comm_event->event_id.header.size);
4485 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4486 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4488 perf_output_put(&handle, comm_event->event_id);
4489 __output_copy(&handle, comm_event->comm,
4490 comm_event->comm_size);
4492 perf_event__output_id_sample(event, &handle, &sample);
4494 perf_output_end(&handle);
4496 comm_event->event_id.header.size = size;
4499 static int perf_event_comm_match(struct perf_event *event)
4501 if (event->state < PERF_EVENT_STATE_INACTIVE)
4504 if (!event_filter_match(event))
4507 if (event->attr.comm)
4513 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4514 struct perf_comm_event *comm_event)
4516 struct perf_event *event;
4518 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4519 if (perf_event_comm_match(event))
4520 perf_event_comm_output(event, comm_event);
4524 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4526 struct perf_cpu_context *cpuctx;
4527 struct perf_event_context *ctx;
4528 char comm[TASK_COMM_LEN];
4533 memset(comm, 0, sizeof(comm));
4534 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4535 size = ALIGN(strlen(comm)+1, sizeof(u64));
4537 comm_event->comm = comm;
4538 comm_event->comm_size = size;
4540 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4542 list_for_each_entry_rcu(pmu, &pmus, entry) {
4543 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4544 if (cpuctx->unique_pmu != pmu)
4546 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4548 ctxn = pmu->task_ctx_nr;
4552 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4554 perf_event_comm_ctx(ctx, comm_event);
4556 put_cpu_ptr(pmu->pmu_cpu_context);
4561 void perf_event_comm(struct task_struct *task)
4563 struct perf_comm_event comm_event;
4564 struct perf_event_context *ctx;
4567 for_each_task_context_nr(ctxn) {
4568 ctx = task->perf_event_ctxp[ctxn];
4572 perf_event_enable_on_exec(ctx);
4575 if (!atomic_read(&nr_comm_events))
4578 comm_event = (struct perf_comm_event){
4584 .type = PERF_RECORD_COMM,
4593 perf_event_comm_event(&comm_event);
4600 struct perf_mmap_event {
4601 struct vm_area_struct *vma;
4603 const char *file_name;
4607 struct perf_event_header header;
4617 static void perf_event_mmap_output(struct perf_event *event,
4618 struct perf_mmap_event *mmap_event)
4620 struct perf_output_handle handle;
4621 struct perf_sample_data sample;
4622 int size = mmap_event->event_id.header.size;
4625 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4626 ret = perf_output_begin(&handle, event,
4627 mmap_event->event_id.header.size);
4631 mmap_event->event_id.pid = perf_event_pid(event, current);
4632 mmap_event->event_id.tid = perf_event_tid(event, current);
4634 perf_output_put(&handle, mmap_event->event_id);
4635 __output_copy(&handle, mmap_event->file_name,
4636 mmap_event->file_size);
4638 perf_event__output_id_sample(event, &handle, &sample);
4640 perf_output_end(&handle);
4642 mmap_event->event_id.header.size = size;
4645 static int perf_event_mmap_match(struct perf_event *event,
4646 struct perf_mmap_event *mmap_event,
4649 if (event->state < PERF_EVENT_STATE_INACTIVE)
4652 if (!event_filter_match(event))
4655 if ((!executable && event->attr.mmap_data) ||
4656 (executable && event->attr.mmap))
4662 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4663 struct perf_mmap_event *mmap_event,
4666 struct perf_event *event;
4668 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4669 if (perf_event_mmap_match(event, mmap_event, executable))
4670 perf_event_mmap_output(event, mmap_event);
4674 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4676 struct perf_cpu_context *cpuctx;
4677 struct perf_event_context *ctx;
4678 struct vm_area_struct *vma = mmap_event->vma;
4679 struct file *file = vma->vm_file;
4687 memset(tmp, 0, sizeof(tmp));
4691 * d_path works from the end of the rb backwards, so we
4692 * need to add enough zero bytes after the string to handle
4693 * the 64bit alignment we do later.
4695 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4697 name = strncpy(tmp, "//enomem", sizeof(tmp));
4700 name = d_path(&file->f_path, buf, PATH_MAX);
4702 name = strncpy(tmp, "//toolong", sizeof(tmp));
4706 if (arch_vma_name(mmap_event->vma)) {
4707 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4713 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4715 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4716 vma->vm_end >= vma->vm_mm->brk) {
4717 name = strncpy(tmp, "[heap]", sizeof(tmp));
4719 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4720 vma->vm_end >= vma->vm_mm->start_stack) {
4721 name = strncpy(tmp, "[stack]", sizeof(tmp));
4725 name = strncpy(tmp, "//anon", sizeof(tmp));
4730 size = ALIGN(strlen(name)+1, sizeof(u64));
4732 mmap_event->file_name = name;
4733 mmap_event->file_size = size;
4735 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4738 list_for_each_entry_rcu(pmu, &pmus, entry) {
4739 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4740 if (cpuctx->unique_pmu != pmu)
4742 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4743 vma->vm_flags & VM_EXEC);
4745 ctxn = pmu->task_ctx_nr;
4749 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4751 perf_event_mmap_ctx(ctx, mmap_event,
4752 vma->vm_flags & VM_EXEC);
4755 put_cpu_ptr(pmu->pmu_cpu_context);
4762 void perf_event_mmap(struct vm_area_struct *vma)
4764 struct perf_mmap_event mmap_event;
4766 if (!atomic_read(&nr_mmap_events))
4769 mmap_event = (struct perf_mmap_event){
4775 .type = PERF_RECORD_MMAP,
4776 .misc = PERF_RECORD_MISC_USER,
4781 .start = vma->vm_start,
4782 .len = vma->vm_end - vma->vm_start,
4783 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4787 perf_event_mmap_event(&mmap_event);
4791 * IRQ throttle logging
4794 static void perf_log_throttle(struct perf_event *event, int enable)
4796 struct perf_output_handle handle;
4797 struct perf_sample_data sample;
4801 struct perf_event_header header;
4805 } throttle_event = {
4807 .type = PERF_RECORD_THROTTLE,
4809 .size = sizeof(throttle_event),
4811 .time = perf_clock(),
4812 .id = primary_event_id(event),
4813 .stream_id = event->id,
4817 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4819 perf_event_header__init_id(&throttle_event.header, &sample, event);
4821 ret = perf_output_begin(&handle, event,
4822 throttle_event.header.size);
4826 perf_output_put(&handle, throttle_event);
4827 perf_event__output_id_sample(event, &handle, &sample);
4828 perf_output_end(&handle);
4832 * Generic event overflow handling, sampling.
4835 static int __perf_event_overflow(struct perf_event *event,
4836 int throttle, struct perf_sample_data *data,
4837 struct pt_regs *regs)
4839 int events = atomic_read(&event->event_limit);
4840 struct hw_perf_event *hwc = &event->hw;
4844 * Non-sampling counters might still use the PMI to fold short
4845 * hardware counters, ignore those.
4847 if (unlikely(!is_sampling_event(event)))
4850 if (unlikely(hwc->interrupts >= max_samples_per_tick)) {
4852 hwc->interrupts = MAX_INTERRUPTS;
4853 perf_log_throttle(event, 0);
4859 if (event->attr.freq) {
4860 u64 now = perf_clock();
4861 s64 delta = now - hwc->freq_time_stamp;
4863 hwc->freq_time_stamp = now;
4865 if (delta > 0 && delta < 2*TICK_NSEC)
4866 perf_adjust_period(event, delta, hwc->last_period);
4870 * XXX event_limit might not quite work as expected on inherited
4874 event->pending_kill = POLL_IN;
4875 if (events && atomic_dec_and_test(&event->event_limit)) {
4877 event->pending_kill = POLL_HUP;
4878 event->pending_disable = 1;
4879 irq_work_queue(&event->pending);
4882 if (event->overflow_handler)
4883 event->overflow_handler(event, data, regs);
4885 perf_event_output(event, data, regs);
4887 if (event->fasync && event->pending_kill) {
4888 event->pending_wakeup = 1;
4889 irq_work_queue(&event->pending);
4895 int perf_event_overflow(struct perf_event *event,
4896 struct perf_sample_data *data,
4897 struct pt_regs *regs)
4899 return __perf_event_overflow(event, 1, data, regs);
4903 * Generic software event infrastructure
4906 struct swevent_htable {
4907 struct swevent_hlist *swevent_hlist;
4908 struct mutex hlist_mutex;
4911 /* Recursion avoidance in each contexts */
4912 int recursion[PERF_NR_CONTEXTS];
4914 /* Keeps track of cpu being initialized/exited */
4918 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4921 * We directly increment event->count and keep a second value in
4922 * event->hw.period_left to count intervals. This period event
4923 * is kept in the range [-sample_period, 0] so that we can use the
4927 static u64 perf_swevent_set_period(struct perf_event *event)
4929 struct hw_perf_event *hwc = &event->hw;
4930 u64 period = hwc->last_period;
4934 hwc->last_period = hwc->sample_period;
4937 old = val = local64_read(&hwc->period_left);
4941 nr = div64_u64(period + val, period);
4942 offset = nr * period;
4944 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4950 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4951 struct perf_sample_data *data,
4952 struct pt_regs *regs)
4954 struct hw_perf_event *hwc = &event->hw;
4957 data->period = event->hw.last_period;
4959 overflow = perf_swevent_set_period(event);
4961 if (hwc->interrupts == MAX_INTERRUPTS)
4964 for (; overflow; overflow--) {
4965 if (__perf_event_overflow(event, throttle,
4968 * We inhibit the overflow from happening when
4969 * hwc->interrupts == MAX_INTERRUPTS.
4977 static void perf_swevent_event(struct perf_event *event, u64 nr,
4978 struct perf_sample_data *data,
4979 struct pt_regs *regs)
4981 struct hw_perf_event *hwc = &event->hw;
4983 local64_add(nr, &event->count);
4988 if (!is_sampling_event(event))
4991 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4992 return perf_swevent_overflow(event, 1, data, regs);
4994 if (local64_add_negative(nr, &hwc->period_left))
4997 perf_swevent_overflow(event, 0, data, regs);
5000 static int perf_exclude_event(struct perf_event *event,
5001 struct pt_regs *regs)
5003 if (event->hw.state & PERF_HES_STOPPED)
5007 if (event->attr.exclude_user && user_mode(regs))
5010 if (event->attr.exclude_kernel && !user_mode(regs))
5017 static int perf_swevent_match(struct perf_event *event,
5018 enum perf_type_id type,
5020 struct perf_sample_data *data,
5021 struct pt_regs *regs)
5023 if (event->attr.type != type)
5026 if (event->attr.config != event_id)
5029 if (perf_exclude_event(event, regs))
5035 static inline u64 swevent_hash(u64 type, u32 event_id)
5037 u64 val = event_id | (type << 32);
5039 return hash_64(val, SWEVENT_HLIST_BITS);
5042 static inline struct hlist_head *
5043 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
5045 u64 hash = swevent_hash(type, event_id);
5047 return &hlist->heads[hash];
5050 /* For the read side: events when they trigger */
5051 static inline struct hlist_head *
5052 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
5054 struct swevent_hlist *hlist;
5056 hlist = rcu_dereference(swhash->swevent_hlist);
5060 return __find_swevent_head(hlist, type, event_id);
5063 /* For the event head insertion and removal in the hlist */
5064 static inline struct hlist_head *
5065 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
5067 struct swevent_hlist *hlist;
5068 u32 event_id = event->attr.config;
5069 u64 type = event->attr.type;
5072 * Event scheduling is always serialized against hlist allocation
5073 * and release. Which makes the protected version suitable here.
5074 * The context lock guarantees that.
5076 hlist = rcu_dereference_protected(swhash->swevent_hlist,
5077 lockdep_is_held(&event->ctx->lock));
5081 return __find_swevent_head(hlist, type, event_id);
5084 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5086 struct perf_sample_data *data,
5087 struct pt_regs *regs)
5089 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5090 struct perf_event *event;
5091 struct hlist_node *node;
5092 struct hlist_head *head;
5095 head = find_swevent_head_rcu(swhash, type, event_id);
5099 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5100 if (perf_swevent_match(event, type, event_id, data, regs))
5101 perf_swevent_event(event, nr, data, regs);
5107 int perf_swevent_get_recursion_context(void)
5109 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5111 return get_recursion_context(swhash->recursion);
5113 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
5115 inline void perf_swevent_put_recursion_context(int rctx)
5117 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5119 put_recursion_context(swhash->recursion, rctx);
5122 void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
5124 struct perf_sample_data data;
5127 preempt_disable_notrace();
5128 rctx = perf_swevent_get_recursion_context();
5132 perf_sample_data_init(&data, addr);
5134 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
5136 perf_swevent_put_recursion_context(rctx);
5137 preempt_enable_notrace();
5140 static void perf_swevent_read(struct perf_event *event)
5144 static int perf_swevent_add(struct perf_event *event, int flags)
5146 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5147 struct hw_perf_event *hwc = &event->hw;
5148 struct hlist_head *head;
5150 if (is_sampling_event(event)) {
5151 hwc->last_period = hwc->sample_period;
5152 perf_swevent_set_period(event);
5155 hwc->state = !(flags & PERF_EF_START);
5157 head = find_swevent_head(swhash, event);
5160 * We can race with cpu hotplug code. Do not
5161 * WARN if the cpu just got unplugged.
5163 WARN_ON_ONCE(swhash->online);
5167 hlist_add_head_rcu(&event->hlist_entry, head);
5172 static void perf_swevent_del(struct perf_event *event, int flags)
5174 hlist_del_rcu(&event->hlist_entry);
5177 static void perf_swevent_start(struct perf_event *event, int flags)
5179 event->hw.state = 0;
5182 static void perf_swevent_stop(struct perf_event *event, int flags)
5184 event->hw.state = PERF_HES_STOPPED;
5187 /* Deref the hlist from the update side */
5188 static inline struct swevent_hlist *
5189 swevent_hlist_deref(struct swevent_htable *swhash)
5191 return rcu_dereference_protected(swhash->swevent_hlist,
5192 lockdep_is_held(&swhash->hlist_mutex));
5195 static void swevent_hlist_release(struct swevent_htable *swhash)
5197 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
5202 rcu_assign_pointer(swhash->swevent_hlist, NULL);
5203 kfree_rcu(hlist, rcu_head);
5206 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5208 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5210 mutex_lock(&swhash->hlist_mutex);
5212 if (!--swhash->hlist_refcount)
5213 swevent_hlist_release(swhash);
5215 mutex_unlock(&swhash->hlist_mutex);
5218 static void swevent_hlist_put(struct perf_event *event)
5222 if (event->cpu != -1) {
5223 swevent_hlist_put_cpu(event, event->cpu);
5227 for_each_possible_cpu(cpu)
5228 swevent_hlist_put_cpu(event, cpu);
5231 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5233 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5236 mutex_lock(&swhash->hlist_mutex);
5238 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
5239 struct swevent_hlist *hlist;
5241 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5246 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5248 swhash->hlist_refcount++;
5250 mutex_unlock(&swhash->hlist_mutex);
5255 static int swevent_hlist_get(struct perf_event *event)
5258 int cpu, failed_cpu;
5260 if (event->cpu != -1)
5261 return swevent_hlist_get_cpu(event, event->cpu);
5264 for_each_possible_cpu(cpu) {
5265 err = swevent_hlist_get_cpu(event, cpu);
5275 for_each_possible_cpu(cpu) {
5276 if (cpu == failed_cpu)
5278 swevent_hlist_put_cpu(event, cpu);
5285 struct jump_label_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
5287 static void sw_perf_event_destroy(struct perf_event *event)
5289 u64 event_id = event->attr.config;
5291 WARN_ON(event->parent);
5293 jump_label_dec(&perf_swevent_enabled[event_id]);
5294 swevent_hlist_put(event);
5297 static int perf_swevent_init(struct perf_event *event)
5299 u64 event_id = event->attr.config;
5301 if (event->attr.type != PERF_TYPE_SOFTWARE)
5305 case PERF_COUNT_SW_CPU_CLOCK:
5306 case PERF_COUNT_SW_TASK_CLOCK:
5313 if (event_id >= PERF_COUNT_SW_MAX)
5316 if (!event->parent) {
5319 err = swevent_hlist_get(event);
5323 jump_label_inc(&perf_swevent_enabled[event_id]);
5324 event->destroy = sw_perf_event_destroy;
5330 static struct pmu perf_swevent = {
5331 .task_ctx_nr = perf_sw_context,
5333 .event_init = perf_swevent_init,
5334 .add = perf_swevent_add,
5335 .del = perf_swevent_del,
5336 .start = perf_swevent_start,
5337 .stop = perf_swevent_stop,
5338 .read = perf_swevent_read,
5341 #ifdef CONFIG_EVENT_TRACING
5343 static int perf_tp_filter_match(struct perf_event *event,
5344 struct perf_sample_data *data)
5346 void *record = data->raw->data;
5348 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5353 static int perf_tp_event_match(struct perf_event *event,
5354 struct perf_sample_data *data,
5355 struct pt_regs *regs)
5357 if (event->hw.state & PERF_HES_STOPPED)
5360 * All tracepoints are from kernel-space.
5362 if (event->attr.exclude_kernel)
5365 if (!perf_tp_filter_match(event, data))
5371 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5372 struct pt_regs *regs, struct hlist_head *head, int rctx)
5374 struct perf_sample_data data;
5375 struct perf_event *event;
5376 struct hlist_node *node;
5378 struct perf_raw_record raw = {
5383 perf_sample_data_init(&data, addr);
5386 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5387 if (perf_tp_event_match(event, &data, regs))
5388 perf_swevent_event(event, count, &data, regs);
5391 perf_swevent_put_recursion_context(rctx);
5393 EXPORT_SYMBOL_GPL(perf_tp_event);
5395 static void tp_perf_event_destroy(struct perf_event *event)
5397 perf_trace_destroy(event);
5400 static int perf_tp_event_init(struct perf_event *event)
5404 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5407 err = perf_trace_init(event);
5411 event->destroy = tp_perf_event_destroy;
5416 static struct pmu perf_tracepoint = {
5417 .task_ctx_nr = perf_sw_context,
5419 .event_init = perf_tp_event_init,
5420 .add = perf_trace_add,
5421 .del = perf_trace_del,
5422 .start = perf_swevent_start,
5423 .stop = perf_swevent_stop,
5424 .read = perf_swevent_read,
5427 static inline void perf_tp_register(void)
5429 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5432 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5437 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5440 filter_str = strndup_user(arg, PAGE_SIZE);
5441 if (IS_ERR(filter_str))
5442 return PTR_ERR(filter_str);
5444 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5450 static void perf_event_free_filter(struct perf_event *event)
5452 ftrace_profile_free_filter(event);
5457 static inline void perf_tp_register(void)
5461 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5466 static void perf_event_free_filter(struct perf_event *event)
5470 #endif /* CONFIG_EVENT_TRACING */
5472 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5473 void perf_bp_event(struct perf_event *bp, void *data)
5475 struct perf_sample_data sample;
5476 struct pt_regs *regs = data;
5478 perf_sample_data_init(&sample, bp->attr.bp_addr);
5480 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5481 perf_swevent_event(bp, 1, &sample, regs);
5486 * hrtimer based swevent callback
5489 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5491 enum hrtimer_restart ret = HRTIMER_RESTART;
5492 struct perf_sample_data data;
5493 struct pt_regs *regs;
5494 struct perf_event *event;
5497 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5499 if (event->state != PERF_EVENT_STATE_ACTIVE)
5500 return HRTIMER_NORESTART;
5502 event->pmu->read(event);
5504 perf_sample_data_init(&data, 0);
5505 data.period = event->hw.last_period;
5506 regs = get_irq_regs();
5508 if (regs && !perf_exclude_event(event, regs)) {
5509 if (!(event->attr.exclude_idle && current->pid == 0))
5510 if (perf_event_overflow(event, &data, regs))
5511 ret = HRTIMER_NORESTART;
5514 period = max_t(u64, 10000, event->hw.sample_period);
5515 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5520 static void perf_swevent_start_hrtimer(struct perf_event *event)
5522 struct hw_perf_event *hwc = &event->hw;
5525 if (!is_sampling_event(event))
5528 period = local64_read(&hwc->period_left);
5533 local64_set(&hwc->period_left, 0);
5535 period = max_t(u64, 10000, hwc->sample_period);
5537 __hrtimer_start_range_ns(&hwc->hrtimer,
5538 ns_to_ktime(period), 0,
5539 HRTIMER_MODE_REL_PINNED, 0);
5542 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5544 struct hw_perf_event *hwc = &event->hw;
5546 if (is_sampling_event(event)) {
5547 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5548 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5550 hrtimer_cancel(&hwc->hrtimer);
5554 static void perf_swevent_init_hrtimer(struct perf_event *event)
5556 struct hw_perf_event *hwc = &event->hw;
5558 if (!is_sampling_event(event))
5561 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5562 hwc->hrtimer.function = perf_swevent_hrtimer;
5565 * Since hrtimers have a fixed rate, we can do a static freq->period
5566 * mapping and avoid the whole period adjust feedback stuff.
5568 if (event->attr.freq) {
5569 long freq = event->attr.sample_freq;
5571 event->attr.sample_period = NSEC_PER_SEC / freq;
5572 hwc->sample_period = event->attr.sample_period;
5573 local64_set(&hwc->period_left, hwc->sample_period);
5574 event->attr.freq = 0;
5579 * Software event: cpu wall time clock
5582 static void cpu_clock_event_update(struct perf_event *event)
5587 now = local_clock();
5588 prev = local64_xchg(&event->hw.prev_count, now);
5589 local64_add(now - prev, &event->count);
5592 static void cpu_clock_event_start(struct perf_event *event, int flags)
5594 local64_set(&event->hw.prev_count, local_clock());
5595 perf_swevent_start_hrtimer(event);
5598 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5600 perf_swevent_cancel_hrtimer(event);
5601 cpu_clock_event_update(event);
5604 static int cpu_clock_event_add(struct perf_event *event, int flags)
5606 if (flags & PERF_EF_START)
5607 cpu_clock_event_start(event, flags);
5612 static void cpu_clock_event_del(struct perf_event *event, int flags)
5614 cpu_clock_event_stop(event, flags);
5617 static void cpu_clock_event_read(struct perf_event *event)
5619 cpu_clock_event_update(event);
5622 static int cpu_clock_event_init(struct perf_event *event)
5624 if (event->attr.type != PERF_TYPE_SOFTWARE)
5627 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5630 perf_swevent_init_hrtimer(event);
5635 static struct pmu perf_cpu_clock = {
5636 .task_ctx_nr = perf_sw_context,
5638 .event_init = cpu_clock_event_init,
5639 .add = cpu_clock_event_add,
5640 .del = cpu_clock_event_del,
5641 .start = cpu_clock_event_start,
5642 .stop = cpu_clock_event_stop,
5643 .read = cpu_clock_event_read,
5647 * Software event: task time clock
5650 static void task_clock_event_update(struct perf_event *event, u64 now)
5655 prev = local64_xchg(&event->hw.prev_count, now);
5657 local64_add(delta, &event->count);
5660 static void task_clock_event_start(struct perf_event *event, int flags)
5662 local64_set(&event->hw.prev_count, event->ctx->time);
5663 perf_swevent_start_hrtimer(event);
5666 static void task_clock_event_stop(struct perf_event *event, int flags)
5668 perf_swevent_cancel_hrtimer(event);
5669 task_clock_event_update(event, event->ctx->time);
5672 static int task_clock_event_add(struct perf_event *event, int flags)
5674 if (flags & PERF_EF_START)
5675 task_clock_event_start(event, flags);
5680 static void task_clock_event_del(struct perf_event *event, int flags)
5682 task_clock_event_stop(event, PERF_EF_UPDATE);
5685 static void task_clock_event_read(struct perf_event *event)
5687 u64 now = perf_clock();
5688 u64 delta = now - event->ctx->timestamp;
5689 u64 time = event->ctx->time + delta;
5691 task_clock_event_update(event, time);
5694 static int task_clock_event_init(struct perf_event *event)
5696 if (event->attr.type != PERF_TYPE_SOFTWARE)
5699 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5702 perf_swevent_init_hrtimer(event);
5707 static struct pmu perf_task_clock = {
5708 .task_ctx_nr = perf_sw_context,
5710 .event_init = task_clock_event_init,
5711 .add = task_clock_event_add,
5712 .del = task_clock_event_del,
5713 .start = task_clock_event_start,
5714 .stop = task_clock_event_stop,
5715 .read = task_clock_event_read,
5718 static void perf_pmu_nop_void(struct pmu *pmu)
5722 static int perf_pmu_nop_int(struct pmu *pmu)
5727 static void perf_pmu_start_txn(struct pmu *pmu)
5729 perf_pmu_disable(pmu);
5732 static int perf_pmu_commit_txn(struct pmu *pmu)
5734 perf_pmu_enable(pmu);
5738 static void perf_pmu_cancel_txn(struct pmu *pmu)
5740 perf_pmu_enable(pmu);
5744 * Ensures all contexts with the same task_ctx_nr have the same
5745 * pmu_cpu_context too.
5747 static void *find_pmu_context(int ctxn)
5754 list_for_each_entry(pmu, &pmus, entry) {
5755 if (pmu->task_ctx_nr == ctxn)
5756 return pmu->pmu_cpu_context;
5762 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5766 for_each_possible_cpu(cpu) {
5767 struct perf_cpu_context *cpuctx;
5769 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5771 if (cpuctx->unique_pmu == old_pmu)
5772 cpuctx->unique_pmu = pmu;
5776 static void free_pmu_context(struct pmu *pmu)
5780 mutex_lock(&pmus_lock);
5782 * Like a real lame refcount.
5784 list_for_each_entry(i, &pmus, entry) {
5785 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5786 update_pmu_context(i, pmu);
5791 free_percpu(pmu->pmu_cpu_context);
5793 mutex_unlock(&pmus_lock);
5795 static struct idr pmu_idr;
5798 type_show(struct device *dev, struct device_attribute *attr, char *page)
5800 struct pmu *pmu = dev_get_drvdata(dev);
5802 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5805 static struct device_attribute pmu_dev_attrs[] = {
5810 static int pmu_bus_running;
5811 static struct bus_type pmu_bus = {
5812 .name = "event_source",
5813 .dev_attrs = pmu_dev_attrs,
5816 static void pmu_dev_release(struct device *dev)
5821 static int pmu_dev_alloc(struct pmu *pmu)
5825 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5829 device_initialize(pmu->dev);
5830 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5834 dev_set_drvdata(pmu->dev, pmu);
5835 pmu->dev->bus = &pmu_bus;
5836 pmu->dev->release = pmu_dev_release;
5837 ret = device_add(pmu->dev);
5845 put_device(pmu->dev);
5849 static struct lock_class_key cpuctx_mutex;
5850 static struct lock_class_key cpuctx_lock;
5852 int perf_pmu_register(struct pmu *pmu, char *name, int type)
5856 mutex_lock(&pmus_lock);
5858 pmu->pmu_disable_count = alloc_percpu(int);
5859 if (!pmu->pmu_disable_count)
5868 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5872 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5880 if (pmu_bus_running) {
5881 ret = pmu_dev_alloc(pmu);
5887 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5888 if (pmu->pmu_cpu_context)
5889 goto got_cpu_context;
5892 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5893 if (!pmu->pmu_cpu_context)
5896 for_each_possible_cpu(cpu) {
5897 struct perf_cpu_context *cpuctx;
5899 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5900 __perf_event_init_context(&cpuctx->ctx);
5901 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
5902 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
5903 cpuctx->ctx.type = cpu_context;
5904 cpuctx->ctx.pmu = pmu;
5905 cpuctx->jiffies_interval = 1;
5906 INIT_LIST_HEAD(&cpuctx->rotation_list);
5907 cpuctx->unique_pmu = pmu;
5911 if (!pmu->start_txn) {
5912 if (pmu->pmu_enable) {
5914 * If we have pmu_enable/pmu_disable calls, install
5915 * transaction stubs that use that to try and batch
5916 * hardware accesses.
5918 pmu->start_txn = perf_pmu_start_txn;
5919 pmu->commit_txn = perf_pmu_commit_txn;
5920 pmu->cancel_txn = perf_pmu_cancel_txn;
5922 pmu->start_txn = perf_pmu_nop_void;
5923 pmu->commit_txn = perf_pmu_nop_int;
5924 pmu->cancel_txn = perf_pmu_nop_void;
5928 if (!pmu->pmu_enable) {
5929 pmu->pmu_enable = perf_pmu_nop_void;
5930 pmu->pmu_disable = perf_pmu_nop_void;
5933 list_add_rcu(&pmu->entry, &pmus);
5936 mutex_unlock(&pmus_lock);
5941 device_del(pmu->dev);
5942 put_device(pmu->dev);
5945 if (pmu->type >= PERF_TYPE_MAX)
5946 idr_remove(&pmu_idr, pmu->type);
5949 free_percpu(pmu->pmu_disable_count);
5953 void perf_pmu_unregister(struct pmu *pmu)
5955 mutex_lock(&pmus_lock);
5956 list_del_rcu(&pmu->entry);
5957 mutex_unlock(&pmus_lock);
5960 * We dereference the pmu list under both SRCU and regular RCU, so
5961 * synchronize against both of those.
5963 synchronize_srcu(&pmus_srcu);
5966 free_percpu(pmu->pmu_disable_count);
5967 if (pmu->type >= PERF_TYPE_MAX)
5968 idr_remove(&pmu_idr, pmu->type);
5969 device_del(pmu->dev);
5970 put_device(pmu->dev);
5971 free_pmu_context(pmu);
5974 struct pmu *perf_init_event(struct perf_event *event)
5976 struct pmu *pmu = NULL;
5980 idx = srcu_read_lock(&pmus_srcu);
5983 pmu = idr_find(&pmu_idr, event->attr.type);
5987 ret = pmu->event_init(event);
5993 list_for_each_entry_rcu(pmu, &pmus, entry) {
5995 ret = pmu->event_init(event);
5999 if (ret != -ENOENT) {
6004 pmu = ERR_PTR(-ENOENT);
6006 srcu_read_unlock(&pmus_srcu, idx);
6012 * Allocate and initialize a event structure
6014 static struct perf_event *
6015 perf_event_alloc(struct perf_event_attr *attr, int cpu,
6016 struct task_struct *task,
6017 struct perf_event *group_leader,
6018 struct perf_event *parent_event,
6019 perf_overflow_handler_t overflow_handler,
6023 struct perf_event *event;
6024 struct hw_perf_event *hwc;
6027 if ((unsigned)cpu >= nr_cpu_ids) {
6028 if (!task || cpu != -1)
6029 return ERR_PTR(-EINVAL);
6032 event = kzalloc(sizeof(*event), GFP_KERNEL);
6034 return ERR_PTR(-ENOMEM);
6037 * Single events are their own group leaders, with an
6038 * empty sibling list:
6041 group_leader = event;
6043 mutex_init(&event->child_mutex);
6044 INIT_LIST_HEAD(&event->child_list);
6046 INIT_LIST_HEAD(&event->group_entry);
6047 INIT_LIST_HEAD(&event->event_entry);
6048 INIT_LIST_HEAD(&event->sibling_list);
6049 INIT_LIST_HEAD(&event->rb_entry);
6051 init_waitqueue_head(&event->waitq);
6052 init_irq_work(&event->pending, perf_pending_event);
6054 mutex_init(&event->mmap_mutex);
6056 atomic_long_set(&event->refcount, 1);
6058 event->attr = *attr;
6059 event->group_leader = group_leader;
6063 event->parent = parent_event;
6065 event->ns = get_pid_ns(current->nsproxy->pid_ns);
6066 event->id = atomic64_inc_return(&perf_event_id);
6068 event->state = PERF_EVENT_STATE_INACTIVE;
6071 event->attach_state = PERF_ATTACH_TASK;
6072 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6074 * hw_breakpoint is a bit difficult here..
6076 if (attr->type == PERF_TYPE_BREAKPOINT)
6077 event->hw.bp_target = task;
6081 if (!overflow_handler && parent_event) {
6082 overflow_handler = parent_event->overflow_handler;
6083 context = parent_event->overflow_handler_context;
6086 event->overflow_handler = overflow_handler;
6087 event->overflow_handler_context = context;
6089 perf_event__state_init(event);
6094 hwc->sample_period = attr->sample_period;
6095 if (attr->freq && attr->sample_freq)
6096 hwc->sample_period = 1;
6097 hwc->last_period = hwc->sample_period;
6099 local64_set(&hwc->period_left, hwc->sample_period);
6102 * we currently do not support PERF_FORMAT_GROUP on inherited events
6104 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6107 pmu = perf_init_event(event);
6113 else if (IS_ERR(pmu))
6118 put_pid_ns(event->ns);
6120 return ERR_PTR(err);
6123 if (!event->parent) {
6124 if (event->attach_state & PERF_ATTACH_TASK)
6125 jump_label_inc(&perf_sched_events);
6126 if (event->attr.mmap || event->attr.mmap_data)
6127 atomic_inc(&nr_mmap_events);
6128 if (event->attr.comm)
6129 atomic_inc(&nr_comm_events);
6130 if (event->attr.task)
6131 atomic_inc(&nr_task_events);
6132 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6133 err = get_callchain_buffers();
6136 return ERR_PTR(err);
6144 static int perf_copy_attr(struct perf_event_attr __user *uattr,
6145 struct perf_event_attr *attr)
6150 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6154 * zero the full structure, so that a short copy will be nice.
6156 memset(attr, 0, sizeof(*attr));
6158 ret = get_user(size, &uattr->size);
6162 if (size > PAGE_SIZE) /* silly large */
6165 if (!size) /* abi compat */
6166 size = PERF_ATTR_SIZE_VER0;
6168 if (size < PERF_ATTR_SIZE_VER0)
6172 * If we're handed a bigger struct than we know of,
6173 * ensure all the unknown bits are 0 - i.e. new
6174 * user-space does not rely on any kernel feature
6175 * extensions we dont know about yet.
6177 if (size > sizeof(*attr)) {
6178 unsigned char __user *addr;
6179 unsigned char __user *end;
6182 addr = (void __user *)uattr + sizeof(*attr);
6183 end = (void __user *)uattr + size;
6185 for (; addr < end; addr++) {
6186 ret = get_user(val, addr);
6192 size = sizeof(*attr);
6195 ret = copy_from_user(attr, uattr, size);
6199 if (attr->__reserved_1)
6202 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6205 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6212 put_user(sizeof(*attr), &uattr->size);
6218 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6220 struct ring_buffer *rb = NULL, *old_rb = NULL;
6226 /* don't allow circular references */
6227 if (event == output_event)
6231 * Don't allow cross-cpu buffers
6233 if (output_event->cpu != event->cpu)
6237 * If its not a per-cpu rb, it must be the same task.
6239 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6243 mutex_lock(&event->mmap_mutex);
6244 /* Can't redirect output if we've got an active mmap() */
6245 if (atomic_read(&event->mmap_count))
6251 /* get the rb we want to redirect to */
6252 rb = ring_buffer_get(output_event);
6258 ring_buffer_detach(event, old_rb);
6261 ring_buffer_attach(event, rb);
6263 rcu_assign_pointer(event->rb, rb);
6266 ring_buffer_put(old_rb);
6268 * Since we detached before setting the new rb, so that we
6269 * could attach the new rb, we could have missed a wakeup.
6272 wake_up_all(&event->waitq);
6277 mutex_unlock(&event->mmap_mutex);
6284 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6286 * @attr_uptr: event_id type attributes for monitoring/sampling
6289 * @group_fd: group leader event fd
6291 SYSCALL_DEFINE5(perf_event_open,
6292 struct perf_event_attr __user *, attr_uptr,
6293 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6295 struct perf_event *group_leader = NULL, *output_event = NULL;
6296 struct perf_event *event, *sibling;
6297 struct perf_event_attr attr;
6298 struct perf_event_context *ctx;
6299 struct file *event_file = NULL;
6300 struct file *group_file = NULL;
6301 struct task_struct *task = NULL;
6305 int fput_needed = 0;
6308 /* for future expandability... */
6309 if (flags & ~PERF_FLAG_ALL)
6312 err = perf_copy_attr(attr_uptr, &attr);
6316 if (!attr.exclude_kernel) {
6317 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6322 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6325 if (attr.sample_period & (1ULL << 63))
6330 * In cgroup mode, the pid argument is used to pass the fd
6331 * opened to the cgroup directory in cgroupfs. The cpu argument
6332 * designates the cpu on which to monitor threads from that
6335 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6338 event_fd = get_unused_fd_flags(O_RDWR);
6342 if (group_fd != -1) {
6343 group_file = perf_fget_light(group_fd, &fput_needed);
6344 if (IS_ERR(group_file)) {
6345 err = PTR_ERR(group_file);
6348 group_leader = group_file->private_data;
6349 if (flags & PERF_FLAG_FD_OUTPUT)
6350 output_event = group_leader;
6351 if (flags & PERF_FLAG_FD_NO_GROUP)
6352 group_leader = NULL;
6355 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
6356 task = find_lively_task_by_vpid(pid);
6358 err = PTR_ERR(task);
6363 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
6365 if (IS_ERR(event)) {
6366 err = PTR_ERR(event);
6370 if (flags & PERF_FLAG_PID_CGROUP) {
6371 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6376 * - that has cgroup constraint on event->cpu
6377 * - that may need work on context switch
6379 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6380 jump_label_inc(&perf_sched_events);
6384 * Special case software events and allow them to be part of
6385 * any hardware group.
6390 (is_software_event(event) != is_software_event(group_leader))) {
6391 if (is_software_event(event)) {
6393 * If event and group_leader are not both a software
6394 * event, and event is, then group leader is not.
6396 * Allow the addition of software events to !software
6397 * groups, this is safe because software events never
6400 pmu = group_leader->pmu;
6401 } else if (is_software_event(group_leader) &&
6402 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6404 * In case the group is a pure software group, and we
6405 * try to add a hardware event, move the whole group to
6406 * the hardware context.
6413 * Get the target context (task or percpu):
6415 ctx = find_get_context(pmu, task, cpu);
6422 put_task_struct(task);
6427 * Look up the group leader (we will attach this event to it):
6433 * Do not allow a recursive hierarchy (this new sibling
6434 * becoming part of another group-sibling):
6436 if (group_leader->group_leader != group_leader)
6439 * Do not allow to attach to a group in a different
6440 * task or CPU context:
6443 if (group_leader->ctx->type != ctx->type)
6446 if (group_leader->ctx != ctx)
6451 * Only a group leader can be exclusive or pinned
6453 if (attr.exclusive || attr.pinned)
6458 err = perf_event_set_output(event, output_event);
6463 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6464 if (IS_ERR(event_file)) {
6465 err = PTR_ERR(event_file);
6470 struct perf_event_context *gctx = group_leader->ctx;
6472 mutex_lock(&gctx->mutex);
6473 perf_remove_from_context(group_leader, false);
6476 * Removing from the context ends up with disabled
6477 * event. What we want here is event in the initial
6478 * startup state, ready to be add into new context.
6480 perf_event__state_init(group_leader);
6481 list_for_each_entry(sibling, &group_leader->sibling_list,
6483 perf_remove_from_context(sibling, false);
6484 perf_event__state_init(sibling);
6487 mutex_unlock(&gctx->mutex);
6491 WARN_ON_ONCE(ctx->parent_ctx);
6492 mutex_lock(&ctx->mutex);
6495 perf_install_in_context(ctx, group_leader, cpu);
6497 list_for_each_entry(sibling, &group_leader->sibling_list,
6499 perf_install_in_context(ctx, sibling, cpu);
6504 perf_install_in_context(ctx, event, cpu);
6506 perf_unpin_context(ctx);
6507 mutex_unlock(&ctx->mutex);
6509 event->owner = current;
6511 mutex_lock(¤t->perf_event_mutex);
6512 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6513 mutex_unlock(¤t->perf_event_mutex);
6516 * Precalculate sample_data sizes
6518 perf_event__header_size(event);
6519 perf_event__id_header_size(event);
6522 * Drop the reference on the group_event after placing the
6523 * new event on the sibling_list. This ensures destruction
6524 * of the group leader will find the pointer to itself in
6525 * perf_group_detach().
6527 fput_light(group_file, fput_needed);
6528 fd_install(event_fd, event_file);
6532 perf_unpin_context(ctx);
6538 put_task_struct(task);
6540 fput_light(group_file, fput_needed);
6542 put_unused_fd(event_fd);
6547 * perf_event_create_kernel_counter
6549 * @attr: attributes of the counter to create
6550 * @cpu: cpu in which the counter is bound
6551 * @task: task to profile (NULL for percpu)
6554 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6555 struct task_struct *task,
6556 perf_overflow_handler_t overflow_handler,
6559 struct perf_event_context *ctx;
6560 struct perf_event *event;
6564 * Get the target context (task or percpu):
6567 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
6568 overflow_handler, context);
6569 if (IS_ERR(event)) {
6570 err = PTR_ERR(event);
6574 ctx = find_get_context(event->pmu, task, cpu);
6580 WARN_ON_ONCE(ctx->parent_ctx);
6581 mutex_lock(&ctx->mutex);
6582 perf_install_in_context(ctx, event, cpu);
6584 perf_unpin_context(ctx);
6585 mutex_unlock(&ctx->mutex);
6592 return ERR_PTR(err);
6594 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6596 static void sync_child_event(struct perf_event *child_event,
6597 struct task_struct *child)
6599 struct perf_event *parent_event = child_event->parent;
6602 if (child_event->attr.inherit_stat)
6603 perf_event_read_event(child_event, child);
6605 child_val = perf_event_count(child_event);
6608 * Add back the child's count to the parent's count:
6610 atomic64_add(child_val, &parent_event->child_count);
6611 atomic64_add(child_event->total_time_enabled,
6612 &parent_event->child_total_time_enabled);
6613 atomic64_add(child_event->total_time_running,
6614 &parent_event->child_total_time_running);
6617 * Remove this event from the parent's list
6619 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6620 mutex_lock(&parent_event->child_mutex);
6621 list_del_init(&child_event->child_list);
6622 mutex_unlock(&parent_event->child_mutex);
6625 * Release the parent event, if this was the last
6628 put_event(parent_event);
6632 __perf_event_exit_task(struct perf_event *child_event,
6633 struct perf_event_context *child_ctx,
6634 struct task_struct *child)
6636 perf_remove_from_context(child_event, !!child_event->parent);
6639 * It can happen that the parent exits first, and has events
6640 * that are still around due to the child reference. These
6641 * events need to be zapped.
6643 if (child_event->parent) {
6644 sync_child_event(child_event, child);
6645 free_event(child_event);
6649 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6651 struct perf_event *child_event, *tmp;
6652 struct perf_event_context *child_ctx;
6653 unsigned long flags;
6655 if (likely(!child->perf_event_ctxp[ctxn])) {
6656 perf_event_task(child, NULL, 0);
6660 local_irq_save(flags);
6662 * We can't reschedule here because interrupts are disabled,
6663 * and either child is current or it is a task that can't be
6664 * scheduled, so we are now safe from rescheduling changing
6667 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6670 * Take the context lock here so that if find_get_context is
6671 * reading child->perf_event_ctxp, we wait until it has
6672 * incremented the context's refcount before we do put_ctx below.
6674 raw_spin_lock(&child_ctx->lock);
6675 task_ctx_sched_out(child_ctx);
6676 child->perf_event_ctxp[ctxn] = NULL;
6678 * If this context is a clone; unclone it so it can't get
6679 * swapped to another process while we're removing all
6680 * the events from it.
6682 unclone_ctx(child_ctx);
6683 update_context_time(child_ctx);
6684 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6687 * Report the task dead after unscheduling the events so that we
6688 * won't get any samples after PERF_RECORD_EXIT. We can however still
6689 * get a few PERF_RECORD_READ events.
6691 perf_event_task(child, child_ctx, 0);
6694 * We can recurse on the same lock type through:
6696 * __perf_event_exit_task()
6697 * sync_child_event()
6699 * mutex_lock(&ctx->mutex)
6701 * But since its the parent context it won't be the same instance.
6703 mutex_lock(&child_ctx->mutex);
6706 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6708 __perf_event_exit_task(child_event, child_ctx, child);
6710 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6712 __perf_event_exit_task(child_event, child_ctx, child);
6715 * If the last event was a group event, it will have appended all
6716 * its siblings to the list, but we obtained 'tmp' before that which
6717 * will still point to the list head terminating the iteration.
6719 if (!list_empty(&child_ctx->pinned_groups) ||
6720 !list_empty(&child_ctx->flexible_groups))
6723 mutex_unlock(&child_ctx->mutex);
6729 * When a child task exits, feed back event values to parent events.
6731 void perf_event_exit_task(struct task_struct *child)
6733 struct perf_event *event, *tmp;
6736 mutex_lock(&child->perf_event_mutex);
6737 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6739 list_del_init(&event->owner_entry);
6742 * Ensure the list deletion is visible before we clear
6743 * the owner, closes a race against perf_release() where
6744 * we need to serialize on the owner->perf_event_mutex.
6747 event->owner = NULL;
6749 mutex_unlock(&child->perf_event_mutex);
6751 for_each_task_context_nr(ctxn)
6752 perf_event_exit_task_context(child, ctxn);
6755 static void perf_free_event(struct perf_event *event,
6756 struct perf_event_context *ctx)
6758 struct perf_event *parent = event->parent;
6760 if (WARN_ON_ONCE(!parent))
6763 mutex_lock(&parent->child_mutex);
6764 list_del_init(&event->child_list);
6765 mutex_unlock(&parent->child_mutex);
6769 perf_group_detach(event);
6770 list_del_event(event, ctx);
6775 * free an unexposed, unused context as created by inheritance by
6776 * perf_event_init_task below, used by fork() in case of fail.
6778 void perf_event_free_task(struct task_struct *task)
6780 struct perf_event_context *ctx;
6781 struct perf_event *event, *tmp;
6784 for_each_task_context_nr(ctxn) {
6785 ctx = task->perf_event_ctxp[ctxn];
6789 mutex_lock(&ctx->mutex);
6791 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6793 perf_free_event(event, ctx);
6795 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6797 perf_free_event(event, ctx);
6799 if (!list_empty(&ctx->pinned_groups) ||
6800 !list_empty(&ctx->flexible_groups))
6803 mutex_unlock(&ctx->mutex);
6809 void perf_event_delayed_put(struct task_struct *task)
6813 for_each_task_context_nr(ctxn)
6814 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6818 * inherit a event from parent task to child task:
6820 static struct perf_event *
6821 inherit_event(struct perf_event *parent_event,
6822 struct task_struct *parent,
6823 struct perf_event_context *parent_ctx,
6824 struct task_struct *child,
6825 struct perf_event *group_leader,
6826 struct perf_event_context *child_ctx)
6828 struct perf_event *child_event;
6829 unsigned long flags;
6832 * Instead of creating recursive hierarchies of events,
6833 * we link inherited events back to the original parent,
6834 * which has a filp for sure, which we use as the reference
6837 if (parent_event->parent)
6838 parent_event = parent_event->parent;
6840 child_event = perf_event_alloc(&parent_event->attr,
6843 group_leader, parent_event,
6845 if (IS_ERR(child_event))
6848 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
6849 free_event(child_event);
6856 * Make the child state follow the state of the parent event,
6857 * not its attr.disabled bit. We hold the parent's mutex,
6858 * so we won't race with perf_event_{en, dis}able_family.
6860 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6861 child_event->state = PERF_EVENT_STATE_INACTIVE;
6863 child_event->state = PERF_EVENT_STATE_OFF;
6865 if (parent_event->attr.freq) {
6866 u64 sample_period = parent_event->hw.sample_period;
6867 struct hw_perf_event *hwc = &child_event->hw;
6869 hwc->sample_period = sample_period;
6870 hwc->last_period = sample_period;
6872 local64_set(&hwc->period_left, sample_period);
6875 child_event->ctx = child_ctx;
6876 child_event->overflow_handler = parent_event->overflow_handler;
6877 child_event->overflow_handler_context
6878 = parent_event->overflow_handler_context;
6881 * Precalculate sample_data sizes
6883 perf_event__header_size(child_event);
6884 perf_event__id_header_size(child_event);
6887 * Link it up in the child's context:
6889 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6890 add_event_to_ctx(child_event, child_ctx);
6891 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6894 * Link this into the parent event's child list
6896 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6897 mutex_lock(&parent_event->child_mutex);
6898 list_add_tail(&child_event->child_list, &parent_event->child_list);
6899 mutex_unlock(&parent_event->child_mutex);
6904 static int inherit_group(struct perf_event *parent_event,
6905 struct task_struct *parent,
6906 struct perf_event_context *parent_ctx,
6907 struct task_struct *child,
6908 struct perf_event_context *child_ctx)
6910 struct perf_event *leader;
6911 struct perf_event *sub;
6912 struct perf_event *child_ctr;
6914 leader = inherit_event(parent_event, parent, parent_ctx,
6915 child, NULL, child_ctx);
6917 return PTR_ERR(leader);
6918 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6919 child_ctr = inherit_event(sub, parent, parent_ctx,
6920 child, leader, child_ctx);
6921 if (IS_ERR(child_ctr))
6922 return PTR_ERR(child_ctr);
6928 inherit_task_group(struct perf_event *event, struct task_struct *parent,
6929 struct perf_event_context *parent_ctx,
6930 struct task_struct *child, int ctxn,
6934 struct perf_event_context *child_ctx;
6936 if (!event->attr.inherit) {
6941 child_ctx = child->perf_event_ctxp[ctxn];
6944 * This is executed from the parent task context, so
6945 * inherit events that have been marked for cloning.
6946 * First allocate and initialize a context for the
6950 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
6954 child->perf_event_ctxp[ctxn] = child_ctx;
6957 ret = inherit_group(event, parent, parent_ctx,
6967 * Initialize the perf_event context in task_struct
6969 int perf_event_init_context(struct task_struct *child, int ctxn)
6971 struct perf_event_context *child_ctx, *parent_ctx;
6972 struct perf_event_context *cloned_ctx;
6973 struct perf_event *event;
6974 struct task_struct *parent = current;
6975 int inherited_all = 1;
6976 unsigned long flags;
6979 if (likely(!parent->perf_event_ctxp[ctxn]))
6983 * If the parent's context is a clone, pin it so it won't get
6986 parent_ctx = perf_pin_task_context(parent, ctxn);
6989 * No need to check if parent_ctx != NULL here; since we saw
6990 * it non-NULL earlier, the only reason for it to become NULL
6991 * is if we exit, and since we're currently in the middle of
6992 * a fork we can't be exiting at the same time.
6996 * Lock the parent list. No need to lock the child - not PID
6997 * hashed yet and not running, so nobody can access it.
6999 mutex_lock(&parent_ctx->mutex);
7002 * We dont have to disable NMIs - we are only looking at
7003 * the list, not manipulating it:
7005 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
7006 ret = inherit_task_group(event, parent, parent_ctx,
7007 child, ctxn, &inherited_all);
7013 * We can't hold ctx->lock when iterating the ->flexible_group list due
7014 * to allocations, but we need to prevent rotation because
7015 * rotate_ctx() will change the list from interrupt context.
7017 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7018 parent_ctx->rotate_disable = 1;
7019 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7021 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
7022 ret = inherit_task_group(event, parent, parent_ctx,
7023 child, ctxn, &inherited_all);
7028 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7029 parent_ctx->rotate_disable = 0;
7031 child_ctx = child->perf_event_ctxp[ctxn];
7033 if (child_ctx && inherited_all) {
7035 * Mark the child context as a clone of the parent
7036 * context, or of whatever the parent is a clone of.
7038 * Note that if the parent is a clone, the holding of
7039 * parent_ctx->lock avoids it from being uncloned.
7041 cloned_ctx = parent_ctx->parent_ctx;
7043 child_ctx->parent_ctx = cloned_ctx;
7044 child_ctx->parent_gen = parent_ctx->parent_gen;
7046 child_ctx->parent_ctx = parent_ctx;
7047 child_ctx->parent_gen = parent_ctx->generation;
7049 get_ctx(child_ctx->parent_ctx);
7052 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7053 mutex_unlock(&parent_ctx->mutex);
7055 perf_unpin_context(parent_ctx);
7056 put_ctx(parent_ctx);
7062 * Initialize the perf_event context in task_struct
7064 int perf_event_init_task(struct task_struct *child)
7068 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7069 mutex_init(&child->perf_event_mutex);
7070 INIT_LIST_HEAD(&child->perf_event_list);
7072 for_each_task_context_nr(ctxn) {
7073 ret = perf_event_init_context(child, ctxn);
7081 static void __init perf_event_init_all_cpus(void)
7083 struct swevent_htable *swhash;
7086 for_each_possible_cpu(cpu) {
7087 swhash = &per_cpu(swevent_htable, cpu);
7088 mutex_init(&swhash->hlist_mutex);
7089 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
7093 static void __cpuinit perf_event_init_cpu(int cpu)
7095 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7097 mutex_lock(&swhash->hlist_mutex);
7098 swhash->online = true;
7099 if (swhash->hlist_refcount > 0) {
7100 struct swevent_hlist *hlist;
7102 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7104 rcu_assign_pointer(swhash->swevent_hlist, hlist);
7106 mutex_unlock(&swhash->hlist_mutex);
7109 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7110 static void perf_pmu_rotate_stop(struct pmu *pmu)
7112 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7114 WARN_ON(!irqs_disabled());
7116 list_del_init(&cpuctx->rotation_list);
7119 static void __perf_event_exit_context(void *__info)
7121 struct remove_event re = { .detach_group = false };
7122 struct perf_event_context *ctx = __info;
7124 perf_pmu_rotate_stop(ctx->pmu);
7127 list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry)
7128 __perf_remove_from_context(&re);
7132 static void perf_event_exit_cpu_context(int cpu)
7134 struct perf_event_context *ctx;
7138 idx = srcu_read_lock(&pmus_srcu);
7139 list_for_each_entry_rcu(pmu, &pmus, entry) {
7140 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
7142 mutex_lock(&ctx->mutex);
7143 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7144 mutex_unlock(&ctx->mutex);
7146 srcu_read_unlock(&pmus_srcu, idx);
7149 static void perf_event_exit_cpu(int cpu)
7151 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7153 perf_event_exit_cpu_context(cpu);
7155 mutex_lock(&swhash->hlist_mutex);
7156 swhash->online = false;
7157 swevent_hlist_release(swhash);
7158 mutex_unlock(&swhash->hlist_mutex);
7161 static inline void perf_event_exit_cpu(int cpu) { }
7165 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7169 for_each_online_cpu(cpu)
7170 perf_event_exit_cpu(cpu);
7176 * Run the perf reboot notifier at the very last possible moment so that
7177 * the generic watchdog code runs as long as possible.
7179 static struct notifier_block perf_reboot_notifier = {
7180 .notifier_call = perf_reboot,
7181 .priority = INT_MIN,
7184 static int __cpuinit
7185 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7187 unsigned int cpu = (long)hcpu;
7189 switch (action & ~CPU_TASKS_FROZEN) {
7191 case CPU_UP_PREPARE:
7192 case CPU_DOWN_FAILED:
7193 perf_event_init_cpu(cpu);
7196 case CPU_UP_CANCELED:
7197 case CPU_DOWN_PREPARE:
7198 perf_event_exit_cpu(cpu);
7208 void __init perf_event_init(void)
7214 perf_event_init_all_cpus();
7215 init_srcu_struct(&pmus_srcu);
7216 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7217 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7218 perf_pmu_register(&perf_task_clock, NULL, -1);
7220 perf_cpu_notifier(perf_cpu_notify);
7221 register_reboot_notifier(&perf_reboot_notifier);
7223 ret = init_hw_breakpoint();
7224 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7227 static int __init perf_event_sysfs_init(void)
7232 mutex_lock(&pmus_lock);
7234 ret = bus_register(&pmu_bus);
7238 list_for_each_entry(pmu, &pmus, entry) {
7239 if (!pmu->name || pmu->type < 0)
7242 ret = pmu_dev_alloc(pmu);
7243 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7245 pmu_bus_running = 1;
7249 mutex_unlock(&pmus_lock);
7253 device_initcall(perf_event_sysfs_init);
7255 #ifdef CONFIG_CGROUP_PERF
7256 static struct cgroup_subsys_state *perf_cgroup_create(
7257 struct cgroup_subsys *ss, struct cgroup *cont)
7259 struct perf_cgroup *jc;
7261 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
7263 return ERR_PTR(-ENOMEM);
7265 jc->info = alloc_percpu(struct perf_cgroup_info);
7268 return ERR_PTR(-ENOMEM);
7274 static void perf_cgroup_destroy(struct cgroup_subsys *ss,
7275 struct cgroup *cont)
7277 struct perf_cgroup *jc;
7278 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7279 struct perf_cgroup, css);
7280 free_percpu(jc->info);
7284 static int __perf_cgroup_move(void *info)
7286 struct task_struct *task = info;
7287 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7292 perf_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *task)
7294 task_function_call(task, __perf_cgroup_move, task);
7297 static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7298 struct cgroup *old_cgrp, struct task_struct *task)
7301 * cgroup_exit() is called in the copy_process() failure path.
7302 * Ignore this case since the task hasn't ran yet, this avoids
7303 * trying to poke a half freed task state from generic code.
7305 if (!(task->flags & PF_EXITING))
7308 perf_cgroup_attach_task(cgrp, task);
7311 struct cgroup_subsys perf_subsys = {
7312 .name = "perf_event",
7313 .subsys_id = perf_subsys_id,
7314 .create = perf_cgroup_create,
7315 .destroy = perf_cgroup_destroy,
7316 .exit = perf_cgroup_exit,
7317 .attach_task = perf_cgroup_attach_task,
7319 #endif /* CONFIG_CGROUP_PERF */