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 static void ring_buffer_attach(struct perf_event *event,
189 struct ring_buffer *rb);
191 void __weak perf_event_print_debug(void) { }
193 extern __weak const char *perf_pmu_name(void)
198 static inline u64 perf_clock(void)
200 return local_clock();
203 static inline struct perf_cpu_context *
204 __get_cpu_context(struct perf_event_context *ctx)
206 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
209 static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
210 struct perf_event_context *ctx)
212 raw_spin_lock(&cpuctx->ctx.lock);
214 raw_spin_lock(&ctx->lock);
217 static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
218 struct perf_event_context *ctx)
221 raw_spin_unlock(&ctx->lock);
222 raw_spin_unlock(&cpuctx->ctx.lock);
225 #ifdef CONFIG_CGROUP_PERF
228 * Must ensure cgroup is pinned (css_get) before calling
229 * this function. In other words, we cannot call this function
230 * if there is no cgroup event for the current CPU context.
232 static inline struct perf_cgroup *
233 perf_cgroup_from_task(struct task_struct *task)
235 return container_of(task_subsys_state(task, perf_subsys_id),
236 struct perf_cgroup, css);
240 perf_cgroup_match(struct perf_event *event)
242 struct perf_event_context *ctx = event->ctx;
243 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
245 return !event->cgrp || event->cgrp == cpuctx->cgrp;
248 static inline void perf_get_cgroup(struct perf_event *event)
250 css_get(&event->cgrp->css);
253 static inline void perf_put_cgroup(struct perf_event *event)
255 css_put(&event->cgrp->css);
258 static inline void perf_detach_cgroup(struct perf_event *event)
260 perf_put_cgroup(event);
264 static inline int is_cgroup_event(struct perf_event *event)
266 return event->cgrp != NULL;
269 static inline u64 perf_cgroup_event_time(struct perf_event *event)
271 struct perf_cgroup_info *t;
273 t = per_cpu_ptr(event->cgrp->info, event->cpu);
277 static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
279 struct perf_cgroup_info *info;
284 info = this_cpu_ptr(cgrp->info);
286 info->time += now - info->timestamp;
287 info->timestamp = now;
290 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
292 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
294 __update_cgrp_time(cgrp_out);
297 static inline void update_cgrp_time_from_event(struct perf_event *event)
299 struct perf_cgroup *cgrp;
302 * ensure we access cgroup data only when needed and
303 * when we know the cgroup is pinned (css_get)
305 if (!is_cgroup_event(event))
308 cgrp = perf_cgroup_from_task(current);
310 * Do not update time when cgroup is not active
312 if (cgrp == event->cgrp)
313 __update_cgrp_time(event->cgrp);
317 perf_cgroup_set_timestamp(struct task_struct *task,
318 struct perf_event_context *ctx)
320 struct perf_cgroup *cgrp;
321 struct perf_cgroup_info *info;
324 * ctx->lock held by caller
325 * ensure we do not access cgroup data
326 * unless we have the cgroup pinned (css_get)
328 if (!task || !ctx->nr_cgroups)
331 cgrp = perf_cgroup_from_task(task);
332 info = this_cpu_ptr(cgrp->info);
333 info->timestamp = ctx->timestamp;
336 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
337 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
340 * reschedule events based on the cgroup constraint of task.
342 * mode SWOUT : schedule out everything
343 * mode SWIN : schedule in based on cgroup for next
345 void perf_cgroup_switch(struct task_struct *task, int mode)
347 struct perf_cpu_context *cpuctx;
352 * disable interrupts to avoid geting nr_cgroup
353 * changes via __perf_event_disable(). Also
356 local_irq_save(flags);
359 * we reschedule only in the presence of cgroup
360 * constrained events.
364 list_for_each_entry_rcu(pmu, &pmus, entry) {
365 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
368 * perf_cgroup_events says at least one
369 * context on this CPU has cgroup events.
371 * ctx->nr_cgroups reports the number of cgroup
372 * events for a context.
374 if (cpuctx->ctx.nr_cgroups > 0) {
375 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
376 perf_pmu_disable(cpuctx->ctx.pmu);
378 if (mode & PERF_CGROUP_SWOUT) {
379 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
381 * must not be done before ctxswout due
382 * to event_filter_match() in event_sched_out()
387 if (mode & PERF_CGROUP_SWIN) {
388 WARN_ON_ONCE(cpuctx->cgrp);
389 /* set cgrp before ctxsw in to
390 * allow event_filter_match() to not
391 * have to pass task around
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 perf_get_cgroup(event);
482 * all events in a group must monitor
483 * the same cgroup because a task belongs
484 * to only one perf cgroup at a time
486 if (group_leader && group_leader->cgrp != cgrp) {
487 perf_detach_cgroup(event);
491 fput_light(file, fput_needed);
496 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
498 struct perf_cgroup_info *t;
499 t = per_cpu_ptr(event->cgrp->info, event->cpu);
500 event->shadow_ctx_time = now - t->timestamp;
504 perf_cgroup_defer_enabled(struct perf_event *event)
507 * when the current task's perf cgroup does not match
508 * the event's, we need to remember to call the
509 * perf_mark_enable() function the first time a task with
510 * a matching perf cgroup is scheduled in.
512 if (is_cgroup_event(event) && !perf_cgroup_match(event))
513 event->cgrp_defer_enabled = 1;
517 perf_cgroup_mark_enabled(struct perf_event *event,
518 struct perf_event_context *ctx)
520 struct perf_event *sub;
521 u64 tstamp = perf_event_time(event);
523 if (!event->cgrp_defer_enabled)
526 event->cgrp_defer_enabled = 0;
528 event->tstamp_enabled = tstamp - event->total_time_enabled;
529 list_for_each_entry(sub, &event->sibling_list, group_entry) {
530 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
531 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
532 sub->cgrp_defer_enabled = 0;
536 #else /* !CONFIG_CGROUP_PERF */
539 perf_cgroup_match(struct perf_event *event)
544 static inline void perf_detach_cgroup(struct perf_event *event)
547 static inline int is_cgroup_event(struct perf_event *event)
552 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
557 static inline void update_cgrp_time_from_event(struct perf_event *event)
561 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
565 static inline void perf_cgroup_sched_out(struct task_struct *task,
566 struct task_struct *next)
570 static inline void perf_cgroup_sched_in(struct task_struct *prev,
571 struct task_struct *task)
575 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
576 struct perf_event_attr *attr,
577 struct perf_event *group_leader)
583 perf_cgroup_set_timestamp(struct task_struct *task,
584 struct perf_event_context *ctx)
589 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
594 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
598 static inline u64 perf_cgroup_event_time(struct perf_event *event)
604 perf_cgroup_defer_enabled(struct perf_event *event)
609 perf_cgroup_mark_enabled(struct perf_event *event,
610 struct perf_event_context *ctx)
615 void perf_pmu_disable(struct pmu *pmu)
617 int *count = this_cpu_ptr(pmu->pmu_disable_count);
619 pmu->pmu_disable(pmu);
622 void perf_pmu_enable(struct pmu *pmu)
624 int *count = this_cpu_ptr(pmu->pmu_disable_count);
626 pmu->pmu_enable(pmu);
629 static DEFINE_PER_CPU(struct list_head, rotation_list);
632 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
633 * because they're strictly cpu affine and rotate_start is called with IRQs
634 * disabled, while rotate_context is called from IRQ context.
636 static void perf_pmu_rotate_start(struct pmu *pmu)
638 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
639 struct list_head *head = &__get_cpu_var(rotation_list);
641 WARN_ON(!irqs_disabled());
643 if (list_empty(&cpuctx->rotation_list))
644 list_add(&cpuctx->rotation_list, head);
647 static void get_ctx(struct perf_event_context *ctx)
649 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
652 static void put_ctx(struct perf_event_context *ctx)
654 if (atomic_dec_and_test(&ctx->refcount)) {
656 put_ctx(ctx->parent_ctx);
658 put_task_struct(ctx->task);
659 kfree_rcu(ctx, rcu_head);
663 static void unclone_ctx(struct perf_event_context *ctx)
665 if (ctx->parent_ctx) {
666 put_ctx(ctx->parent_ctx);
667 ctx->parent_ctx = NULL;
671 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
674 * only top level events have the pid namespace they were created in
677 event = event->parent;
679 return task_tgid_nr_ns(p, event->ns);
682 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
685 * only top level events have the pid namespace they were created in
688 event = event->parent;
690 return task_pid_nr_ns(p, event->ns);
694 * If we inherit events we want to return the parent event id
697 static u64 primary_event_id(struct perf_event *event)
702 id = event->parent->id;
708 * Get the perf_event_context for a task and lock it.
709 * This has to cope with with the fact that until it is locked,
710 * the context could get moved to another task.
712 static struct perf_event_context *
713 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
715 struct perf_event_context *ctx;
719 * One of the few rules of preemptible RCU is that one cannot do
720 * rcu_read_unlock() while holding a scheduler (or nested) lock when
721 * part of the read side critical section was preemptible -- see
722 * rcu_read_unlock_special().
724 * Since ctx->lock nests under rq->lock we must ensure the entire read
725 * side critical section is non-preemptible.
729 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
732 * If this context is a clone of another, it might
733 * get swapped for another underneath us by
734 * perf_event_task_sched_out, though the
735 * rcu_read_lock() protects us from any context
736 * getting freed. Lock the context and check if it
737 * got swapped before we could get the lock, and retry
738 * if so. If we locked the right context, then it
739 * can't get swapped on us any more.
741 raw_spin_lock_irqsave(&ctx->lock, *flags);
742 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
743 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
749 if (!atomic_inc_not_zero(&ctx->refcount)) {
750 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
760 * Get the context for a task and increment its pin_count so it
761 * can't get swapped to another task. This also increments its
762 * reference count so that the context can't get freed.
764 static struct perf_event_context *
765 perf_pin_task_context(struct task_struct *task, int ctxn)
767 struct perf_event_context *ctx;
770 ctx = perf_lock_task_context(task, ctxn, &flags);
773 raw_spin_unlock_irqrestore(&ctx->lock, flags);
778 static void perf_unpin_context(struct perf_event_context *ctx)
782 raw_spin_lock_irqsave(&ctx->lock, flags);
784 raw_spin_unlock_irqrestore(&ctx->lock, flags);
788 * Update the record of the current time in a context.
790 static void update_context_time(struct perf_event_context *ctx)
792 u64 now = perf_clock();
794 ctx->time += now - ctx->timestamp;
795 ctx->timestamp = now;
798 static u64 perf_event_time(struct perf_event *event)
800 struct perf_event_context *ctx = event->ctx;
802 if (is_cgroup_event(event))
803 return perf_cgroup_event_time(event);
805 return ctx ? ctx->time : 0;
809 * Update the total_time_enabled and total_time_running fields for a event.
810 * The caller of this function needs to hold the ctx->lock.
812 static void update_event_times(struct perf_event *event)
814 struct perf_event_context *ctx = event->ctx;
817 if (event->state < PERF_EVENT_STATE_INACTIVE ||
818 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
821 * in cgroup mode, time_enabled represents
822 * the time the event was enabled AND active
823 * tasks were in the monitored cgroup. This is
824 * independent of the activity of the context as
825 * there may be a mix of cgroup and non-cgroup events.
827 * That is why we treat cgroup events differently
830 if (is_cgroup_event(event))
831 run_end = perf_event_time(event);
832 else if (ctx->is_active)
835 run_end = event->tstamp_stopped;
837 event->total_time_enabled = run_end - event->tstamp_enabled;
839 if (event->state == PERF_EVENT_STATE_INACTIVE)
840 run_end = event->tstamp_stopped;
842 run_end = perf_event_time(event);
844 event->total_time_running = run_end - event->tstamp_running;
849 * Update total_time_enabled and total_time_running for all events in a group.
851 static void update_group_times(struct perf_event *leader)
853 struct perf_event *event;
855 update_event_times(leader);
856 list_for_each_entry(event, &leader->sibling_list, group_entry)
857 update_event_times(event);
860 static struct list_head *
861 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
863 if (event->attr.pinned)
864 return &ctx->pinned_groups;
866 return &ctx->flexible_groups;
870 * Add a event from the lists for its context.
871 * Must be called with ctx->mutex and ctx->lock held.
874 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
876 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
877 event->attach_state |= PERF_ATTACH_CONTEXT;
880 * If we're a stand alone event or group leader, we go to the context
881 * list, group events are kept attached to the group so that
882 * perf_group_detach can, at all times, locate all siblings.
884 if (event->group_leader == event) {
885 struct list_head *list;
887 if (is_software_event(event))
888 event->group_flags |= PERF_GROUP_SOFTWARE;
890 list = ctx_group_list(event, ctx);
891 list_add_tail(&event->group_entry, list);
894 if (is_cgroup_event(event))
897 list_add_rcu(&event->event_entry, &ctx->event_list);
899 perf_pmu_rotate_start(ctx->pmu);
901 if (event->attr.inherit_stat)
906 * Called at perf_event creation and when events are attached/detached from a
909 static void perf_event__read_size(struct perf_event *event)
911 int entry = sizeof(u64); /* value */
915 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
918 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
921 if (event->attr.read_format & PERF_FORMAT_ID)
922 entry += sizeof(u64);
924 if (event->attr.read_format & PERF_FORMAT_GROUP) {
925 nr += event->group_leader->nr_siblings;
930 event->read_size = size;
933 static void perf_event__header_size(struct perf_event *event)
935 struct perf_sample_data *data;
936 u64 sample_type = event->attr.sample_type;
939 perf_event__read_size(event);
941 if (sample_type & PERF_SAMPLE_IP)
942 size += sizeof(data->ip);
944 if (sample_type & PERF_SAMPLE_ADDR)
945 size += sizeof(data->addr);
947 if (sample_type & PERF_SAMPLE_PERIOD)
948 size += sizeof(data->period);
950 if (sample_type & PERF_SAMPLE_READ)
951 size += event->read_size;
953 event->header_size = size;
956 static void perf_event__id_header_size(struct perf_event *event)
958 struct perf_sample_data *data;
959 u64 sample_type = event->attr.sample_type;
962 if (sample_type & PERF_SAMPLE_TID)
963 size += sizeof(data->tid_entry);
965 if (sample_type & PERF_SAMPLE_TIME)
966 size += sizeof(data->time);
968 if (sample_type & PERF_SAMPLE_ID)
969 size += sizeof(data->id);
971 if (sample_type & PERF_SAMPLE_STREAM_ID)
972 size += sizeof(data->stream_id);
974 if (sample_type & PERF_SAMPLE_CPU)
975 size += sizeof(data->cpu_entry);
977 event->id_header_size = size;
980 static void perf_group_attach(struct perf_event *event)
982 struct perf_event *group_leader = event->group_leader, *pos;
985 * We can have double attach due to group movement in perf_event_open.
987 if (event->attach_state & PERF_ATTACH_GROUP)
990 event->attach_state |= PERF_ATTACH_GROUP;
992 if (group_leader == event)
995 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
996 !is_software_event(event))
997 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
999 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1000 group_leader->nr_siblings++;
1002 perf_event__header_size(group_leader);
1004 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1005 perf_event__header_size(pos);
1009 * Remove a event from the lists for its context.
1010 * Must be called with ctx->mutex and ctx->lock held.
1013 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1015 struct perf_cpu_context *cpuctx;
1017 * We can have double detach due to exit/hot-unplug + close.
1019 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
1022 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1024 if (is_cgroup_event(event)) {
1026 cpuctx = __get_cpu_context(ctx);
1028 * if there are no more cgroup events
1029 * then cler cgrp to avoid stale pointer
1030 * in update_cgrp_time_from_cpuctx()
1032 if (!ctx->nr_cgroups)
1033 cpuctx->cgrp = NULL;
1037 if (event->attr.inherit_stat)
1040 list_del_rcu(&event->event_entry);
1042 if (event->group_leader == event)
1043 list_del_init(&event->group_entry);
1045 update_group_times(event);
1048 * If event was in error state, then keep it
1049 * that way, otherwise bogus counts will be
1050 * returned on read(). The only way to get out
1051 * of error state is by explicit re-enabling
1054 if (event->state > PERF_EVENT_STATE_OFF)
1055 event->state = PERF_EVENT_STATE_OFF;
1058 static void perf_group_detach(struct perf_event *event)
1060 struct perf_event *sibling, *tmp;
1061 struct list_head *list = NULL;
1064 * We can have double detach due to exit/hot-unplug + close.
1066 if (!(event->attach_state & PERF_ATTACH_GROUP))
1069 event->attach_state &= ~PERF_ATTACH_GROUP;
1072 * If this is a sibling, remove it from its group.
1074 if (event->group_leader != event) {
1075 list_del_init(&event->group_entry);
1076 event->group_leader->nr_siblings--;
1080 if (!list_empty(&event->group_entry))
1081 list = &event->group_entry;
1084 * If this was a group event with sibling events then
1085 * upgrade the siblings to singleton events by adding them
1086 * to whatever list we are on.
1088 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
1090 list_move_tail(&sibling->group_entry, list);
1091 sibling->group_leader = sibling;
1093 /* Inherit group flags from the previous leader */
1094 sibling->group_flags = event->group_flags;
1098 perf_event__header_size(event->group_leader);
1100 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1101 perf_event__header_size(tmp);
1105 event_filter_match(struct perf_event *event)
1107 return (event->cpu == -1 || event->cpu == smp_processor_id())
1108 && perf_cgroup_match(event);
1112 event_sched_out(struct perf_event *event,
1113 struct perf_cpu_context *cpuctx,
1114 struct perf_event_context *ctx)
1116 u64 tstamp = perf_event_time(event);
1119 * An event which could not be activated because of
1120 * filter mismatch still needs to have its timings
1121 * maintained, otherwise bogus information is return
1122 * via read() for time_enabled, time_running:
1124 if (event->state == PERF_EVENT_STATE_INACTIVE
1125 && !event_filter_match(event)) {
1126 delta = tstamp - event->tstamp_stopped;
1127 event->tstamp_running += delta;
1128 event->tstamp_stopped = tstamp;
1131 if (event->state != PERF_EVENT_STATE_ACTIVE)
1134 event->state = PERF_EVENT_STATE_INACTIVE;
1135 if (event->pending_disable) {
1136 event->pending_disable = 0;
1137 event->state = PERF_EVENT_STATE_OFF;
1139 event->tstamp_stopped = tstamp;
1140 event->pmu->del(event, 0);
1143 if (!is_software_event(event))
1144 cpuctx->active_oncpu--;
1146 if (event->attr.exclusive || !cpuctx->active_oncpu)
1147 cpuctx->exclusive = 0;
1151 group_sched_out(struct perf_event *group_event,
1152 struct perf_cpu_context *cpuctx,
1153 struct perf_event_context *ctx)
1155 struct perf_event *event;
1156 int state = group_event->state;
1158 event_sched_out(group_event, cpuctx, ctx);
1161 * Schedule out siblings (if any):
1163 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1164 event_sched_out(event, cpuctx, ctx);
1166 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
1167 cpuctx->exclusive = 0;
1171 * Cross CPU call to remove a performance event
1173 * We disable the event on the hardware level first. After that we
1174 * remove it from the context list.
1176 static int __perf_remove_from_context(void *info)
1178 struct perf_event *event = info;
1179 struct perf_event_context *ctx = event->ctx;
1180 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1182 raw_spin_lock(&ctx->lock);
1183 event_sched_out(event, cpuctx, ctx);
1184 list_del_event(event, ctx);
1185 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1187 cpuctx->task_ctx = NULL;
1189 raw_spin_unlock(&ctx->lock);
1196 * Remove the event from a task's (or a CPU's) list of events.
1198 * CPU events are removed with a smp call. For task events we only
1199 * call when the task is on a CPU.
1201 * If event->ctx is a cloned context, callers must make sure that
1202 * every task struct that event->ctx->task could possibly point to
1203 * remains valid. This is OK when called from perf_release since
1204 * that only calls us on the top-level context, which can't be a clone.
1205 * When called from perf_event_exit_task, it's OK because the
1206 * context has been detached from its task.
1208 static void perf_remove_from_context(struct perf_event *event)
1210 struct perf_event_context *ctx = event->ctx;
1211 struct task_struct *task = ctx->task;
1213 lockdep_assert_held(&ctx->mutex);
1217 * Per cpu events are removed via an smp call and
1218 * the removal is always successful.
1220 cpu_function_call(event->cpu, __perf_remove_from_context, event);
1225 if (!task_function_call(task, __perf_remove_from_context, event))
1228 raw_spin_lock_irq(&ctx->lock);
1230 * If we failed to find a running task, but find the context active now
1231 * that we've acquired the ctx->lock, retry.
1233 if (ctx->is_active) {
1234 raw_spin_unlock_irq(&ctx->lock);
1239 * Since the task isn't running, its safe to remove the event, us
1240 * holding the ctx->lock ensures the task won't get scheduled in.
1242 list_del_event(event, ctx);
1243 raw_spin_unlock_irq(&ctx->lock);
1247 * Cross CPU call to disable a performance event
1249 static int __perf_event_disable(void *info)
1251 struct perf_event *event = info;
1252 struct perf_event_context *ctx = event->ctx;
1253 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1256 * If this is a per-task event, need to check whether this
1257 * event's task is the current task on this cpu.
1259 * Can trigger due to concurrent perf_event_context_sched_out()
1260 * flipping contexts around.
1262 if (ctx->task && cpuctx->task_ctx != ctx)
1265 raw_spin_lock(&ctx->lock);
1268 * If the event is on, turn it off.
1269 * If it is in error state, leave it in error state.
1271 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1272 update_context_time(ctx);
1273 update_cgrp_time_from_event(event);
1274 update_group_times(event);
1275 if (event == event->group_leader)
1276 group_sched_out(event, cpuctx, ctx);
1278 event_sched_out(event, cpuctx, ctx);
1279 event->state = PERF_EVENT_STATE_OFF;
1282 raw_spin_unlock(&ctx->lock);
1290 * If event->ctx is a cloned context, callers must make sure that
1291 * every task struct that event->ctx->task could possibly point to
1292 * remains valid. This condition is satisifed when called through
1293 * perf_event_for_each_child or perf_event_for_each because they
1294 * hold the top-level event's child_mutex, so any descendant that
1295 * goes to exit will block in sync_child_event.
1296 * When called from perf_pending_event it's OK because event->ctx
1297 * is the current context on this CPU and preemption is disabled,
1298 * hence we can't get into perf_event_task_sched_out for this context.
1300 void perf_event_disable(struct perf_event *event)
1302 struct perf_event_context *ctx = event->ctx;
1303 struct task_struct *task = ctx->task;
1307 * Disable the event on the cpu that it's on
1309 cpu_function_call(event->cpu, __perf_event_disable, event);
1314 if (!task_function_call(task, __perf_event_disable, event))
1317 raw_spin_lock_irq(&ctx->lock);
1319 * If the event is still active, we need to retry the cross-call.
1321 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1322 raw_spin_unlock_irq(&ctx->lock);
1324 * Reload the task pointer, it might have been changed by
1325 * a concurrent perf_event_context_sched_out().
1332 * Since we have the lock this context can't be scheduled
1333 * in, so we can change the state safely.
1335 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1336 update_group_times(event);
1337 event->state = PERF_EVENT_STATE_OFF;
1339 raw_spin_unlock_irq(&ctx->lock);
1342 static void perf_set_shadow_time(struct perf_event *event,
1343 struct perf_event_context *ctx,
1347 * use the correct time source for the time snapshot
1349 * We could get by without this by leveraging the
1350 * fact that to get to this function, the caller
1351 * has most likely already called update_context_time()
1352 * and update_cgrp_time_xx() and thus both timestamp
1353 * are identical (or very close). Given that tstamp is,
1354 * already adjusted for cgroup, we could say that:
1355 * tstamp - ctx->timestamp
1357 * tstamp - cgrp->timestamp.
1359 * Then, in perf_output_read(), the calculation would
1360 * work with no changes because:
1361 * - event is guaranteed scheduled in
1362 * - no scheduled out in between
1363 * - thus the timestamp would be the same
1365 * But this is a bit hairy.
1367 * So instead, we have an explicit cgroup call to remain
1368 * within the time time source all along. We believe it
1369 * is cleaner and simpler to understand.
1371 if (is_cgroup_event(event))
1372 perf_cgroup_set_shadow_time(event, tstamp);
1374 event->shadow_ctx_time = tstamp - ctx->timestamp;
1377 #define MAX_INTERRUPTS (~0ULL)
1379 static void perf_log_throttle(struct perf_event *event, int enable);
1382 event_sched_in(struct perf_event *event,
1383 struct perf_cpu_context *cpuctx,
1384 struct perf_event_context *ctx)
1386 u64 tstamp = perf_event_time(event);
1388 if (event->state <= PERF_EVENT_STATE_OFF)
1391 event->state = PERF_EVENT_STATE_ACTIVE;
1392 event->oncpu = smp_processor_id();
1395 * Unthrottle events, since we scheduled we might have missed several
1396 * ticks already, also for a heavily scheduling task there is little
1397 * guarantee it'll get a tick in a timely manner.
1399 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1400 perf_log_throttle(event, 1);
1401 event->hw.interrupts = 0;
1405 * The new state must be visible before we turn it on in the hardware:
1409 if (event->pmu->add(event, PERF_EF_START)) {
1410 event->state = PERF_EVENT_STATE_INACTIVE;
1415 event->tstamp_running += tstamp - event->tstamp_stopped;
1417 perf_set_shadow_time(event, ctx, tstamp);
1419 if (!is_software_event(event))
1420 cpuctx->active_oncpu++;
1423 if (event->attr.exclusive)
1424 cpuctx->exclusive = 1;
1430 group_sched_in(struct perf_event *group_event,
1431 struct perf_cpu_context *cpuctx,
1432 struct perf_event_context *ctx)
1434 struct perf_event *event, *partial_group = NULL;
1435 struct pmu *pmu = group_event->pmu;
1436 u64 now = ctx->time;
1437 bool simulate = false;
1439 if (group_event->state == PERF_EVENT_STATE_OFF)
1442 pmu->start_txn(pmu);
1444 if (event_sched_in(group_event, cpuctx, ctx)) {
1445 pmu->cancel_txn(pmu);
1450 * Schedule in siblings as one group (if any):
1452 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1453 if (event_sched_in(event, cpuctx, ctx)) {
1454 partial_group = event;
1459 if (!pmu->commit_txn(pmu))
1464 * Groups can be scheduled in as one unit only, so undo any
1465 * partial group before returning:
1466 * The events up to the failed event are scheduled out normally,
1467 * tstamp_stopped will be updated.
1469 * The failed events and the remaining siblings need to have
1470 * their timings updated as if they had gone thru event_sched_in()
1471 * and event_sched_out(). This is required to get consistent timings
1472 * across the group. This also takes care of the case where the group
1473 * could never be scheduled by ensuring tstamp_stopped is set to mark
1474 * the time the event was actually stopped, such that time delta
1475 * calculation in update_event_times() is correct.
1477 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1478 if (event == partial_group)
1482 event->tstamp_running += now - event->tstamp_stopped;
1483 event->tstamp_stopped = now;
1485 event_sched_out(event, cpuctx, ctx);
1488 event_sched_out(group_event, cpuctx, ctx);
1490 pmu->cancel_txn(pmu);
1496 * Work out whether we can put this event group on the CPU now.
1498 static int group_can_go_on(struct perf_event *event,
1499 struct perf_cpu_context *cpuctx,
1503 * Groups consisting entirely of software events can always go on.
1505 if (event->group_flags & PERF_GROUP_SOFTWARE)
1508 * If an exclusive group is already on, no other hardware
1511 if (cpuctx->exclusive)
1514 * If this group is exclusive and there are already
1515 * events on the CPU, it can't go on.
1517 if (event->attr.exclusive && cpuctx->active_oncpu)
1520 * Otherwise, try to add it if all previous groups were able
1526 static void add_event_to_ctx(struct perf_event *event,
1527 struct perf_event_context *ctx)
1529 u64 tstamp = perf_event_time(event);
1531 list_add_event(event, ctx);
1532 perf_group_attach(event);
1533 event->tstamp_enabled = tstamp;
1534 event->tstamp_running = tstamp;
1535 event->tstamp_stopped = tstamp;
1538 static void task_ctx_sched_out(struct perf_event_context *ctx);
1540 ctx_sched_in(struct perf_event_context *ctx,
1541 struct perf_cpu_context *cpuctx,
1542 enum event_type_t event_type,
1543 struct task_struct *task);
1545 static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1546 struct perf_event_context *ctx,
1547 struct task_struct *task)
1549 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1551 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1552 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1554 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1558 * Cross CPU call to install and enable a performance event
1560 * Must be called with ctx->mutex held
1562 static int __perf_install_in_context(void *info)
1564 struct perf_event *event = info;
1565 struct perf_event_context *ctx = event->ctx;
1566 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1567 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1568 struct task_struct *task = current;
1570 perf_ctx_lock(cpuctx, task_ctx);
1571 perf_pmu_disable(cpuctx->ctx.pmu);
1574 * If there was an active task_ctx schedule it out.
1577 task_ctx_sched_out(task_ctx);
1580 * If the context we're installing events in is not the
1581 * active task_ctx, flip them.
1583 if (ctx->task && task_ctx != ctx) {
1585 raw_spin_unlock(&task_ctx->lock);
1586 raw_spin_lock(&ctx->lock);
1591 cpuctx->task_ctx = task_ctx;
1592 task = task_ctx->task;
1595 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
1597 update_context_time(ctx);
1599 * update cgrp time only if current cgrp
1600 * matches event->cgrp. Must be done before
1601 * calling add_event_to_ctx()
1603 update_cgrp_time_from_event(event);
1605 add_event_to_ctx(event, ctx);
1608 * Schedule everything back in
1610 perf_event_sched_in(cpuctx, task_ctx, task);
1612 perf_pmu_enable(cpuctx->ctx.pmu);
1613 perf_ctx_unlock(cpuctx, task_ctx);
1619 * Attach a performance event to a context
1621 * First we add the event to the list with the hardware enable bit
1622 * in event->hw_config cleared.
1624 * If the event is attached to a task which is on a CPU we use a smp
1625 * call to enable it in the task context. The task might have been
1626 * scheduled away, but we check this in the smp call again.
1629 perf_install_in_context(struct perf_event_context *ctx,
1630 struct perf_event *event,
1633 struct task_struct *task = ctx->task;
1635 lockdep_assert_held(&ctx->mutex);
1641 * Per cpu events are installed via an smp call and
1642 * the install is always successful.
1644 cpu_function_call(cpu, __perf_install_in_context, event);
1649 if (!task_function_call(task, __perf_install_in_context, event))
1652 raw_spin_lock_irq(&ctx->lock);
1654 * If we failed to find a running task, but find the context active now
1655 * that we've acquired the ctx->lock, retry.
1657 if (ctx->is_active) {
1658 raw_spin_unlock_irq(&ctx->lock);
1663 * Since the task isn't running, its safe to add the event, us holding
1664 * the ctx->lock ensures the task won't get scheduled in.
1666 add_event_to_ctx(event, ctx);
1667 raw_spin_unlock_irq(&ctx->lock);
1671 * Put a event into inactive state and update time fields.
1672 * Enabling the leader of a group effectively enables all
1673 * the group members that aren't explicitly disabled, so we
1674 * have to update their ->tstamp_enabled also.
1675 * Note: this works for group members as well as group leaders
1676 * since the non-leader members' sibling_lists will be empty.
1678 static void __perf_event_mark_enabled(struct perf_event *event,
1679 struct perf_event_context *ctx)
1681 struct perf_event *sub;
1682 u64 tstamp = perf_event_time(event);
1684 event->state = PERF_EVENT_STATE_INACTIVE;
1685 event->tstamp_enabled = tstamp - event->total_time_enabled;
1686 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1687 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1688 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1693 * Cross CPU call to enable a performance event
1695 static int __perf_event_enable(void *info)
1697 struct perf_event *event = info;
1698 struct perf_event_context *ctx = event->ctx;
1699 struct perf_event *leader = event->group_leader;
1700 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1704 * There's a time window between 'ctx->is_active' check
1705 * in perf_event_enable function and this place having:
1707 * - ctx->lock unlocked
1709 * where the task could be killed and 'ctx' deactivated
1710 * by perf_event_exit_task.
1712 if (!ctx->is_active)
1715 raw_spin_lock(&ctx->lock);
1716 update_context_time(ctx);
1718 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1722 * set current task's cgroup time reference point
1724 perf_cgroup_set_timestamp(current, ctx);
1726 __perf_event_mark_enabled(event, ctx);
1728 if (!event_filter_match(event)) {
1729 if (is_cgroup_event(event))
1730 perf_cgroup_defer_enabled(event);
1735 * If the event is in a group and isn't the group leader,
1736 * then don't put it on unless the group is on.
1738 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1741 if (!group_can_go_on(event, cpuctx, 1)) {
1744 if (event == leader)
1745 err = group_sched_in(event, cpuctx, ctx);
1747 err = event_sched_in(event, cpuctx, ctx);
1752 * If this event can't go on and it's part of a
1753 * group, then the whole group has to come off.
1755 if (leader != event)
1756 group_sched_out(leader, cpuctx, ctx);
1757 if (leader->attr.pinned) {
1758 update_group_times(leader);
1759 leader->state = PERF_EVENT_STATE_ERROR;
1764 raw_spin_unlock(&ctx->lock);
1772 * If event->ctx is a cloned context, callers must make sure that
1773 * every task struct that event->ctx->task could possibly point to
1774 * remains valid. This condition is satisfied when called through
1775 * perf_event_for_each_child or perf_event_for_each as described
1776 * for perf_event_disable.
1778 void perf_event_enable(struct perf_event *event)
1780 struct perf_event_context *ctx = event->ctx;
1781 struct task_struct *task = ctx->task;
1785 * Enable the event on the cpu that it's on
1787 cpu_function_call(event->cpu, __perf_event_enable, event);
1791 raw_spin_lock_irq(&ctx->lock);
1792 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1796 * If the event is in error state, clear that first.
1797 * That way, if we see the event in error state below, we
1798 * know that it has gone back into error state, as distinct
1799 * from the task having been scheduled away before the
1800 * cross-call arrived.
1802 if (event->state == PERF_EVENT_STATE_ERROR)
1803 event->state = PERF_EVENT_STATE_OFF;
1806 if (!ctx->is_active) {
1807 __perf_event_mark_enabled(event, ctx);
1811 raw_spin_unlock_irq(&ctx->lock);
1813 if (!task_function_call(task, __perf_event_enable, event))
1816 raw_spin_lock_irq(&ctx->lock);
1819 * If the context is active and the event is still off,
1820 * we need to retry the cross-call.
1822 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1824 * task could have been flipped by a concurrent
1825 * perf_event_context_sched_out()
1832 raw_spin_unlock_irq(&ctx->lock);
1835 int perf_event_refresh(struct perf_event *event, int refresh)
1838 * not supported on inherited events
1840 if (event->attr.inherit || !is_sampling_event(event))
1843 atomic_add(refresh, &event->event_limit);
1844 perf_event_enable(event);
1848 EXPORT_SYMBOL_GPL(perf_event_refresh);
1850 static void ctx_sched_out(struct perf_event_context *ctx,
1851 struct perf_cpu_context *cpuctx,
1852 enum event_type_t event_type)
1854 struct perf_event *event;
1855 int is_active = ctx->is_active;
1857 ctx->is_active &= ~event_type;
1858 if (likely(!ctx->nr_events))
1861 update_context_time(ctx);
1862 update_cgrp_time_from_cpuctx(cpuctx);
1863 if (!ctx->nr_active)
1866 perf_pmu_disable(ctx->pmu);
1867 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
1868 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1869 group_sched_out(event, cpuctx, ctx);
1872 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
1873 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1874 group_sched_out(event, cpuctx, ctx);
1876 perf_pmu_enable(ctx->pmu);
1880 * Test whether two contexts are equivalent, i.e. whether they
1881 * have both been cloned from the same version of the same context
1882 * and they both have the same number of enabled events.
1883 * If the number of enabled events is the same, then the set
1884 * of enabled events should be the same, because these are both
1885 * inherited contexts, therefore we can't access individual events
1886 * in them directly with an fd; we can only enable/disable all
1887 * events via prctl, or enable/disable all events in a family
1888 * via ioctl, which will have the same effect on both contexts.
1890 static int context_equiv(struct perf_event_context *ctx1,
1891 struct perf_event_context *ctx2)
1893 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1894 && ctx1->parent_gen == ctx2->parent_gen
1895 && !ctx1->pin_count && !ctx2->pin_count;
1898 static void __perf_event_sync_stat(struct perf_event *event,
1899 struct perf_event *next_event)
1903 if (!event->attr.inherit_stat)
1907 * Update the event value, we cannot use perf_event_read()
1908 * because we're in the middle of a context switch and have IRQs
1909 * disabled, which upsets smp_call_function_single(), however
1910 * we know the event must be on the current CPU, therefore we
1911 * don't need to use it.
1913 switch (event->state) {
1914 case PERF_EVENT_STATE_ACTIVE:
1915 event->pmu->read(event);
1918 case PERF_EVENT_STATE_INACTIVE:
1919 update_event_times(event);
1927 * In order to keep per-task stats reliable we need to flip the event
1928 * values when we flip the contexts.
1930 value = local64_read(&next_event->count);
1931 value = local64_xchg(&event->count, value);
1932 local64_set(&next_event->count, value);
1934 swap(event->total_time_enabled, next_event->total_time_enabled);
1935 swap(event->total_time_running, next_event->total_time_running);
1938 * Since we swizzled the values, update the user visible data too.
1940 perf_event_update_userpage(event);
1941 perf_event_update_userpage(next_event);
1944 #define list_next_entry(pos, member) \
1945 list_entry(pos->member.next, typeof(*pos), member)
1947 static void perf_event_sync_stat(struct perf_event_context *ctx,
1948 struct perf_event_context *next_ctx)
1950 struct perf_event *event, *next_event;
1955 update_context_time(ctx);
1957 event = list_first_entry(&ctx->event_list,
1958 struct perf_event, event_entry);
1960 next_event = list_first_entry(&next_ctx->event_list,
1961 struct perf_event, event_entry);
1963 while (&event->event_entry != &ctx->event_list &&
1964 &next_event->event_entry != &next_ctx->event_list) {
1966 __perf_event_sync_stat(event, next_event);
1968 event = list_next_entry(event, event_entry);
1969 next_event = list_next_entry(next_event, event_entry);
1973 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1974 struct task_struct *next)
1976 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1977 struct perf_event_context *next_ctx;
1978 struct perf_event_context *parent;
1979 struct perf_cpu_context *cpuctx;
1985 cpuctx = __get_cpu_context(ctx);
1986 if (!cpuctx->task_ctx)
1990 parent = rcu_dereference(ctx->parent_ctx);
1991 next_ctx = next->perf_event_ctxp[ctxn];
1992 if (parent && next_ctx &&
1993 rcu_dereference(next_ctx->parent_ctx) == parent) {
1995 * Looks like the two contexts are clones, so we might be
1996 * able to optimize the context switch. We lock both
1997 * contexts and check that they are clones under the
1998 * lock (including re-checking that neither has been
1999 * uncloned in the meantime). It doesn't matter which
2000 * order we take the locks because no other cpu could
2001 * be trying to lock both of these tasks.
2003 raw_spin_lock(&ctx->lock);
2004 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
2005 if (context_equiv(ctx, next_ctx)) {
2007 * XXX do we need a memory barrier of sorts
2008 * wrt to rcu_dereference() of perf_event_ctxp
2010 task->perf_event_ctxp[ctxn] = next_ctx;
2011 next->perf_event_ctxp[ctxn] = ctx;
2013 next_ctx->task = task;
2016 perf_event_sync_stat(ctx, next_ctx);
2018 raw_spin_unlock(&next_ctx->lock);
2019 raw_spin_unlock(&ctx->lock);
2024 raw_spin_lock(&ctx->lock);
2025 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2026 cpuctx->task_ctx = NULL;
2027 raw_spin_unlock(&ctx->lock);
2031 #define for_each_task_context_nr(ctxn) \
2032 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2035 * Called from scheduler to remove the events of the current task,
2036 * with interrupts disabled.
2038 * We stop each event and update the event value in event->count.
2040 * This does not protect us against NMI, but disable()
2041 * sets the disabled bit in the control field of event _before_
2042 * accessing the event control register. If a NMI hits, then it will
2043 * not restart the event.
2045 void __perf_event_task_sched_out(struct task_struct *task,
2046 struct task_struct *next)
2050 for_each_task_context_nr(ctxn)
2051 perf_event_context_sched_out(task, ctxn, next);
2054 * if cgroup events exist on this CPU, then we need
2055 * to check if we have to switch out PMU state.
2056 * cgroup event are system-wide mode only
2058 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2059 perf_cgroup_sched_out(task, next);
2062 static void task_ctx_sched_out(struct perf_event_context *ctx)
2064 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2066 if (!cpuctx->task_ctx)
2069 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2072 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2073 cpuctx->task_ctx = NULL;
2077 * Called with IRQs disabled
2079 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2080 enum event_type_t event_type)
2082 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
2086 ctx_pinned_sched_in(struct perf_event_context *ctx,
2087 struct perf_cpu_context *cpuctx)
2089 struct perf_event *event;
2091 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2092 if (event->state <= PERF_EVENT_STATE_OFF)
2094 if (!event_filter_match(event))
2097 /* may need to reset tstamp_enabled */
2098 if (is_cgroup_event(event))
2099 perf_cgroup_mark_enabled(event, ctx);
2101 if (group_can_go_on(event, cpuctx, 1))
2102 group_sched_in(event, cpuctx, ctx);
2105 * If this pinned group hasn't been scheduled,
2106 * put it in error state.
2108 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2109 update_group_times(event);
2110 event->state = PERF_EVENT_STATE_ERROR;
2116 ctx_flexible_sched_in(struct perf_event_context *ctx,
2117 struct perf_cpu_context *cpuctx)
2119 struct perf_event *event;
2122 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2123 /* Ignore events in OFF or ERROR state */
2124 if (event->state <= PERF_EVENT_STATE_OFF)
2127 * Listen to the 'cpu' scheduling filter constraint
2130 if (!event_filter_match(event))
2133 /* may need to reset tstamp_enabled */
2134 if (is_cgroup_event(event))
2135 perf_cgroup_mark_enabled(event, ctx);
2137 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2138 if (group_sched_in(event, cpuctx, ctx))
2145 ctx_sched_in(struct perf_event_context *ctx,
2146 struct perf_cpu_context *cpuctx,
2147 enum event_type_t event_type,
2148 struct task_struct *task)
2151 int is_active = ctx->is_active;
2153 ctx->is_active |= event_type;
2154 if (likely(!ctx->nr_events))
2158 ctx->timestamp = now;
2159 perf_cgroup_set_timestamp(task, ctx);
2161 * First go through the list and put on any pinned groups
2162 * in order to give them the best chance of going on.
2164 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
2165 ctx_pinned_sched_in(ctx, cpuctx);
2167 /* Then walk through the lower prio flexible groups */
2168 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
2169 ctx_flexible_sched_in(ctx, cpuctx);
2172 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2173 enum event_type_t event_type,
2174 struct task_struct *task)
2176 struct perf_event_context *ctx = &cpuctx->ctx;
2178 ctx_sched_in(ctx, cpuctx, event_type, task);
2181 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2182 struct task_struct *task)
2184 struct perf_cpu_context *cpuctx;
2186 cpuctx = __get_cpu_context(ctx);
2187 if (cpuctx->task_ctx == ctx)
2190 perf_ctx_lock(cpuctx, ctx);
2191 perf_pmu_disable(ctx->pmu);
2193 * We want to keep the following priority order:
2194 * cpu pinned (that don't need to move), task pinned,
2195 * cpu flexible, task flexible.
2197 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2200 cpuctx->task_ctx = ctx;
2202 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2204 perf_pmu_enable(ctx->pmu);
2205 perf_ctx_unlock(cpuctx, ctx);
2208 * Since these rotations are per-cpu, we need to ensure the
2209 * cpu-context we got scheduled on is actually rotating.
2211 perf_pmu_rotate_start(ctx->pmu);
2215 * Called from scheduler to add the events of the current task
2216 * with interrupts disabled.
2218 * We restore the event value and then enable it.
2220 * This does not protect us against NMI, but enable()
2221 * sets the enabled bit in the control field of event _before_
2222 * accessing the event control register. If a NMI hits, then it will
2223 * keep the event running.
2225 void __perf_event_task_sched_in(struct task_struct *prev,
2226 struct task_struct *task)
2228 struct perf_event_context *ctx;
2231 for_each_task_context_nr(ctxn) {
2232 ctx = task->perf_event_ctxp[ctxn];
2236 perf_event_context_sched_in(ctx, task);
2239 * if cgroup events exist on this CPU, then we need
2240 * to check if we have to switch in PMU state.
2241 * cgroup event are system-wide mode only
2243 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2244 perf_cgroup_sched_in(prev, task);
2247 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2249 u64 frequency = event->attr.sample_freq;
2250 u64 sec = NSEC_PER_SEC;
2251 u64 divisor, dividend;
2253 int count_fls, nsec_fls, frequency_fls, sec_fls;
2255 count_fls = fls64(count);
2256 nsec_fls = fls64(nsec);
2257 frequency_fls = fls64(frequency);
2261 * We got @count in @nsec, with a target of sample_freq HZ
2262 * the target period becomes:
2265 * period = -------------------
2266 * @nsec * sample_freq
2271 * Reduce accuracy by one bit such that @a and @b converge
2272 * to a similar magnitude.
2274 #define REDUCE_FLS(a, b) \
2276 if (a##_fls > b##_fls) { \
2286 * Reduce accuracy until either term fits in a u64, then proceed with
2287 * the other, so that finally we can do a u64/u64 division.
2289 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2290 REDUCE_FLS(nsec, frequency);
2291 REDUCE_FLS(sec, count);
2294 if (count_fls + sec_fls > 64) {
2295 divisor = nsec * frequency;
2297 while (count_fls + sec_fls > 64) {
2298 REDUCE_FLS(count, sec);
2302 dividend = count * sec;
2304 dividend = count * sec;
2306 while (nsec_fls + frequency_fls > 64) {
2307 REDUCE_FLS(nsec, frequency);
2311 divisor = nsec * frequency;
2317 return div64_u64(dividend, divisor);
2320 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
2322 struct hw_perf_event *hwc = &event->hw;
2323 s64 period, sample_period;
2326 period = perf_calculate_period(event, nsec, count);
2328 delta = (s64)(period - hwc->sample_period);
2329 delta = (delta + 7) / 8; /* low pass filter */
2331 sample_period = hwc->sample_period + delta;
2336 hwc->sample_period = sample_period;
2338 if (local64_read(&hwc->period_left) > 8*sample_period) {
2339 event->pmu->stop(event, PERF_EF_UPDATE);
2340 local64_set(&hwc->period_left, 0);
2341 event->pmu->start(event, PERF_EF_RELOAD);
2345 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
2347 struct perf_event *event;
2348 struct hw_perf_event *hwc;
2349 u64 interrupts, now;
2352 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2353 if (event->state != PERF_EVENT_STATE_ACTIVE)
2356 if (!event_filter_match(event))
2361 interrupts = hwc->interrupts;
2362 hwc->interrupts = 0;
2365 * unthrottle events on the tick
2367 if (interrupts == MAX_INTERRUPTS) {
2368 perf_log_throttle(event, 1);
2369 event->pmu->start(event, 0);
2372 if (!event->attr.freq || !event->attr.sample_freq)
2375 event->pmu->read(event);
2376 now = local64_read(&event->count);
2377 delta = now - hwc->freq_count_stamp;
2378 hwc->freq_count_stamp = now;
2381 perf_adjust_period(event, period, delta);
2386 * Round-robin a context's events:
2388 static void rotate_ctx(struct perf_event_context *ctx)
2391 * Rotate the first entry last of non-pinned groups. Rotation might be
2392 * disabled by the inheritance code.
2394 if (!ctx->rotate_disable)
2395 list_rotate_left(&ctx->flexible_groups);
2399 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2400 * because they're strictly cpu affine and rotate_start is called with IRQs
2401 * disabled, while rotate_context is called from IRQ context.
2403 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
2405 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
2406 struct perf_event_context *ctx = NULL;
2407 int rotate = 0, remove = 1;
2409 if (cpuctx->ctx.nr_events) {
2411 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2415 ctx = cpuctx->task_ctx;
2416 if (ctx && ctx->nr_events) {
2418 if (ctx->nr_events != ctx->nr_active)
2422 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2423 perf_pmu_disable(cpuctx->ctx.pmu);
2424 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
2426 perf_ctx_adjust_freq(ctx, interval);
2431 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2433 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
2435 rotate_ctx(&cpuctx->ctx);
2439 perf_event_sched_in(cpuctx, ctx, current);
2443 list_del_init(&cpuctx->rotation_list);
2445 perf_pmu_enable(cpuctx->ctx.pmu);
2446 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2449 void perf_event_task_tick(void)
2451 struct list_head *head = &__get_cpu_var(rotation_list);
2452 struct perf_cpu_context *cpuctx, *tmp;
2454 WARN_ON(!irqs_disabled());
2456 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2457 if (cpuctx->jiffies_interval == 1 ||
2458 !(jiffies % cpuctx->jiffies_interval))
2459 perf_rotate_context(cpuctx);
2463 static int event_enable_on_exec(struct perf_event *event,
2464 struct perf_event_context *ctx)
2466 if (!event->attr.enable_on_exec)
2469 event->attr.enable_on_exec = 0;
2470 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2473 __perf_event_mark_enabled(event, ctx);
2479 * Enable all of a task's events that have been marked enable-on-exec.
2480 * This expects task == current.
2482 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2484 struct perf_event *event;
2485 unsigned long flags;
2489 local_irq_save(flags);
2490 if (!ctx || !ctx->nr_events)
2494 * We must ctxsw out cgroup events to avoid conflict
2495 * when invoking perf_task_event_sched_in() later on
2496 * in this function. Otherwise we end up trying to
2497 * ctxswin cgroup events which are already scheduled
2500 perf_cgroup_sched_out(current, NULL);
2502 raw_spin_lock(&ctx->lock);
2503 task_ctx_sched_out(ctx);
2505 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2506 ret = event_enable_on_exec(event, ctx);
2511 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2512 ret = event_enable_on_exec(event, ctx);
2518 * Unclone this context if we enabled any event.
2523 raw_spin_unlock(&ctx->lock);
2526 * Also calls ctxswin for cgroup events, if any:
2528 perf_event_context_sched_in(ctx, ctx->task);
2530 local_irq_restore(flags);
2534 * Cross CPU call to read the hardware event
2536 static void __perf_event_read(void *info)
2538 struct perf_event *event = info;
2539 struct perf_event_context *ctx = event->ctx;
2540 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2543 * If this is a task context, we need to check whether it is
2544 * the current task context of this cpu. If not it has been
2545 * scheduled out before the smp call arrived. In that case
2546 * event->count would have been updated to a recent sample
2547 * when the event was scheduled out.
2549 if (ctx->task && cpuctx->task_ctx != ctx)
2552 raw_spin_lock(&ctx->lock);
2553 if (ctx->is_active) {
2554 update_context_time(ctx);
2555 update_cgrp_time_from_event(event);
2557 update_event_times(event);
2558 if (event->state == PERF_EVENT_STATE_ACTIVE)
2559 event->pmu->read(event);
2560 raw_spin_unlock(&ctx->lock);
2563 static inline u64 perf_event_count(struct perf_event *event)
2565 return local64_read(&event->count) + atomic64_read(&event->child_count);
2568 static u64 perf_event_read(struct perf_event *event)
2571 * If event is enabled and currently active on a CPU, update the
2572 * value in the event structure:
2574 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2575 smp_call_function_single(event->oncpu,
2576 __perf_event_read, event, 1);
2577 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2578 struct perf_event_context *ctx = event->ctx;
2579 unsigned long flags;
2581 raw_spin_lock_irqsave(&ctx->lock, flags);
2583 * may read while context is not active
2584 * (e.g., thread is blocked), in that case
2585 * we cannot update context time
2587 if (ctx->is_active) {
2588 update_context_time(ctx);
2589 update_cgrp_time_from_event(event);
2591 update_event_times(event);
2592 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2595 return perf_event_count(event);
2602 struct callchain_cpus_entries {
2603 struct rcu_head rcu_head;
2604 struct perf_callchain_entry *cpu_entries[0];
2607 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
2608 static atomic_t nr_callchain_events;
2609 static DEFINE_MUTEX(callchain_mutex);
2610 struct callchain_cpus_entries *callchain_cpus_entries;
2613 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
2614 struct pt_regs *regs)
2618 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
2619 struct pt_regs *regs)
2623 static void release_callchain_buffers_rcu(struct rcu_head *head)
2625 struct callchain_cpus_entries *entries;
2628 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
2630 for_each_possible_cpu(cpu)
2631 kfree(entries->cpu_entries[cpu]);
2636 static void release_callchain_buffers(void)
2638 struct callchain_cpus_entries *entries;
2640 entries = callchain_cpus_entries;
2641 rcu_assign_pointer(callchain_cpus_entries, NULL);
2642 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2645 static int alloc_callchain_buffers(void)
2649 struct callchain_cpus_entries *entries;
2652 * We can't use the percpu allocation API for data that can be
2653 * accessed from NMI. Use a temporary manual per cpu allocation
2654 * until that gets sorted out.
2656 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
2658 entries = kzalloc(size, GFP_KERNEL);
2662 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
2664 for_each_possible_cpu(cpu) {
2665 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2667 if (!entries->cpu_entries[cpu])
2671 rcu_assign_pointer(callchain_cpus_entries, entries);
2676 for_each_possible_cpu(cpu)
2677 kfree(entries->cpu_entries[cpu]);
2683 static int get_callchain_buffers(void)
2688 mutex_lock(&callchain_mutex);
2690 count = atomic_inc_return(&nr_callchain_events);
2691 if (WARN_ON_ONCE(count < 1)) {
2697 /* If the allocation failed, give up */
2698 if (!callchain_cpus_entries)
2703 err = alloc_callchain_buffers();
2705 release_callchain_buffers();
2707 mutex_unlock(&callchain_mutex);
2712 static void put_callchain_buffers(void)
2714 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2715 release_callchain_buffers();
2716 mutex_unlock(&callchain_mutex);
2720 static int get_recursion_context(int *recursion)
2728 else if (in_softirq())
2733 if (recursion[rctx])
2742 static inline void put_recursion_context(int *recursion, int rctx)
2748 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2751 struct callchain_cpus_entries *entries;
2753 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2757 entries = rcu_dereference(callchain_cpus_entries);
2761 cpu = smp_processor_id();
2763 return &entries->cpu_entries[cpu][*rctx];
2767 put_callchain_entry(int rctx)
2769 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2772 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2775 struct perf_callchain_entry *entry;
2778 entry = get_callchain_entry(&rctx);
2787 if (!user_mode(regs)) {
2788 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2789 perf_callchain_kernel(entry, regs);
2791 regs = task_pt_regs(current);
2797 perf_callchain_store(entry, PERF_CONTEXT_USER);
2798 perf_callchain_user(entry, regs);
2802 put_callchain_entry(rctx);
2808 * Initialize the perf_event context in a task_struct:
2810 static void __perf_event_init_context(struct perf_event_context *ctx)
2812 raw_spin_lock_init(&ctx->lock);
2813 mutex_init(&ctx->mutex);
2814 INIT_LIST_HEAD(&ctx->pinned_groups);
2815 INIT_LIST_HEAD(&ctx->flexible_groups);
2816 INIT_LIST_HEAD(&ctx->event_list);
2817 atomic_set(&ctx->refcount, 1);
2820 static struct perf_event_context *
2821 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2823 struct perf_event_context *ctx;
2825 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2829 __perf_event_init_context(ctx);
2832 get_task_struct(task);
2839 static struct task_struct *
2840 find_lively_task_by_vpid(pid_t vpid)
2842 struct task_struct *task;
2849 task = find_task_by_vpid(vpid);
2851 get_task_struct(task);
2855 return ERR_PTR(-ESRCH);
2857 /* Reuse ptrace permission checks for now. */
2859 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2864 put_task_struct(task);
2865 return ERR_PTR(err);
2870 * Returns a matching context with refcount and pincount.
2872 static struct perf_event_context *
2873 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2875 struct perf_event_context *ctx;
2876 struct perf_cpu_context *cpuctx;
2877 unsigned long flags;
2881 /* Must be root to operate on a CPU event: */
2882 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2883 return ERR_PTR(-EACCES);
2886 * We could be clever and allow to attach a event to an
2887 * offline CPU and activate it when the CPU comes up, but
2890 if (!cpu_online(cpu))
2891 return ERR_PTR(-ENODEV);
2893 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2902 ctxn = pmu->task_ctx_nr;
2907 ctx = perf_lock_task_context(task, ctxn, &flags);
2911 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2913 ctx = alloc_perf_context(pmu, task);
2919 mutex_lock(&task->perf_event_mutex);
2921 * If it has already passed perf_event_exit_task().
2922 * we must see PF_EXITING, it takes this mutex too.
2924 if (task->flags & PF_EXITING)
2926 else if (task->perf_event_ctxp[ctxn])
2931 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2933 mutex_unlock(&task->perf_event_mutex);
2935 if (unlikely(err)) {
2947 return ERR_PTR(err);
2950 static void perf_event_free_filter(struct perf_event *event);
2952 static void free_event_rcu(struct rcu_head *head)
2954 struct perf_event *event;
2956 event = container_of(head, struct perf_event, rcu_head);
2958 put_pid_ns(event->ns);
2959 perf_event_free_filter(event);
2963 static bool ring_buffer_put(struct ring_buffer *rb);
2965 static void free_event(struct perf_event *event)
2967 irq_work_sync(&event->pending);
2969 if (!event->parent) {
2970 if (event->attach_state & PERF_ATTACH_TASK)
2971 jump_label_dec(&perf_sched_events);
2972 if (event->attr.mmap || event->attr.mmap_data)
2973 atomic_dec(&nr_mmap_events);
2974 if (event->attr.comm)
2975 atomic_dec(&nr_comm_events);
2976 if (event->attr.task)
2977 atomic_dec(&nr_task_events);
2978 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2979 put_callchain_buffers();
2980 if (is_cgroup_event(event)) {
2981 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
2982 jump_label_dec(&perf_sched_events);
2987 ring_buffer_put(event->rb);
2991 if (is_cgroup_event(event))
2992 perf_detach_cgroup(event);
2995 event->destroy(event);
2998 put_ctx(event->ctx);
3000 call_rcu(&event->rcu_head, free_event_rcu);
3003 int perf_event_release_kernel(struct perf_event *event)
3005 struct perf_event_context *ctx = event->ctx;
3007 WARN_ON_ONCE(ctx->parent_ctx);
3009 * There are two ways this annotation is useful:
3011 * 1) there is a lock recursion from perf_event_exit_task
3012 * see the comment there.
3014 * 2) there is a lock-inversion with mmap_sem through
3015 * perf_event_read_group(), which takes faults while
3016 * holding ctx->mutex, however this is called after
3017 * the last filedesc died, so there is no possibility
3018 * to trigger the AB-BA case.
3020 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
3021 raw_spin_lock_irq(&ctx->lock);
3022 perf_group_detach(event);
3023 raw_spin_unlock_irq(&ctx->lock);
3024 perf_remove_from_context(event);
3025 mutex_unlock(&ctx->mutex);
3031 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3034 * Called when the last reference to the file is gone.
3036 static void put_event(struct perf_event *event)
3038 struct task_struct *owner;
3040 if (!atomic_long_dec_and_test(&event->refcount))
3044 owner = ACCESS_ONCE(event->owner);
3046 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3047 * !owner it means the list deletion is complete and we can indeed
3048 * free this event, otherwise we need to serialize on
3049 * owner->perf_event_mutex.
3051 smp_read_barrier_depends();
3054 * Since delayed_put_task_struct() also drops the last
3055 * task reference we can safely take a new reference
3056 * while holding the rcu_read_lock().
3058 get_task_struct(owner);
3063 mutex_lock(&owner->perf_event_mutex);
3065 * We have to re-check the event->owner field, if it is cleared
3066 * we raced with perf_event_exit_task(), acquiring the mutex
3067 * ensured they're done, and we can proceed with freeing the
3071 list_del_init(&event->owner_entry);
3072 mutex_unlock(&owner->perf_event_mutex);
3073 put_task_struct(owner);
3076 perf_event_release_kernel(event);
3079 static int perf_release(struct inode *inode, struct file *file)
3081 put_event(file->private_data);
3085 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
3087 struct perf_event *child;
3093 mutex_lock(&event->child_mutex);
3094 total += perf_event_read(event);
3095 *enabled += event->total_time_enabled +
3096 atomic64_read(&event->child_total_time_enabled);
3097 *running += event->total_time_running +
3098 atomic64_read(&event->child_total_time_running);
3100 list_for_each_entry(child, &event->child_list, child_list) {
3101 total += perf_event_read(child);
3102 *enabled += child->total_time_enabled;
3103 *running += child->total_time_running;
3105 mutex_unlock(&event->child_mutex);
3109 EXPORT_SYMBOL_GPL(perf_event_read_value);
3111 static int perf_event_read_group(struct perf_event *event,
3112 u64 read_format, char __user *buf)
3114 struct perf_event *leader = event->group_leader, *sub;
3115 int n = 0, size = 0, ret = -EFAULT;
3116 struct perf_event_context *ctx = leader->ctx;
3118 u64 count, enabled, running;
3120 mutex_lock(&ctx->mutex);
3121 count = perf_event_read_value(leader, &enabled, &running);
3123 values[n++] = 1 + leader->nr_siblings;
3124 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3125 values[n++] = enabled;
3126 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3127 values[n++] = running;
3128 values[n++] = count;
3129 if (read_format & PERF_FORMAT_ID)
3130 values[n++] = primary_event_id(leader);
3132 size = n * sizeof(u64);
3134 if (copy_to_user(buf, values, size))
3139 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3142 values[n++] = perf_event_read_value(sub, &enabled, &running);
3143 if (read_format & PERF_FORMAT_ID)
3144 values[n++] = primary_event_id(sub);
3146 size = n * sizeof(u64);
3148 if (copy_to_user(buf + ret, values, size)) {
3156 mutex_unlock(&ctx->mutex);
3161 static int perf_event_read_one(struct perf_event *event,
3162 u64 read_format, char __user *buf)
3164 u64 enabled, running;
3168 values[n++] = perf_event_read_value(event, &enabled, &running);
3169 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3170 values[n++] = enabled;
3171 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3172 values[n++] = running;
3173 if (read_format & PERF_FORMAT_ID)
3174 values[n++] = primary_event_id(event);
3176 if (copy_to_user(buf, values, n * sizeof(u64)))
3179 return n * sizeof(u64);
3183 * Read the performance event - simple non blocking version for now
3186 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3188 u64 read_format = event->attr.read_format;
3192 * Return end-of-file for a read on a event that is in
3193 * error state (i.e. because it was pinned but it couldn't be
3194 * scheduled on to the CPU at some point).
3196 if (event->state == PERF_EVENT_STATE_ERROR)
3199 if (count < event->read_size)
3202 WARN_ON_ONCE(event->ctx->parent_ctx);
3203 if (read_format & PERF_FORMAT_GROUP)
3204 ret = perf_event_read_group(event, read_format, buf);
3206 ret = perf_event_read_one(event, read_format, buf);
3212 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3214 struct perf_event *event = file->private_data;
3216 return perf_read_hw(event, buf, count);
3219 static unsigned int perf_poll(struct file *file, poll_table *wait)
3221 struct perf_event *event = file->private_data;
3222 struct ring_buffer *rb;
3223 unsigned int events = POLL_HUP;
3226 * Race between perf_event_set_output() and perf_poll(): perf_poll()
3227 * grabs the rb reference but perf_event_set_output() overrides it.
3228 * Here is the timeline for two threads T1, T2:
3229 * t0: T1, rb = rcu_dereference(event->rb)
3230 * t1: T2, old_rb = event->rb
3231 * t2: T2, event->rb = new rb
3232 * t3: T2, ring_buffer_detach(old_rb)
3233 * t4: T1, ring_buffer_attach(rb1)
3234 * t5: T1, poll_wait(event->waitq)
3236 * To avoid this problem, we grab mmap_mutex in perf_poll()
3237 * thereby ensuring that the assignment of the new ring buffer
3238 * and the detachment of the old buffer appear atomic to perf_poll()
3240 mutex_lock(&event->mmap_mutex);
3243 rb = rcu_dereference(event->rb);
3245 ring_buffer_attach(event, rb);
3246 events = atomic_xchg(&rb->poll, 0);
3250 mutex_unlock(&event->mmap_mutex);
3252 poll_wait(file, &event->waitq, wait);
3257 static void perf_event_reset(struct perf_event *event)
3259 (void)perf_event_read(event);
3260 local64_set(&event->count, 0);
3261 perf_event_update_userpage(event);
3265 * Holding the top-level event's child_mutex means that any
3266 * descendant process that has inherited this event will block
3267 * in sync_child_event if it goes to exit, thus satisfying the
3268 * task existence requirements of perf_event_enable/disable.
3270 static void perf_event_for_each_child(struct perf_event *event,
3271 void (*func)(struct perf_event *))
3273 struct perf_event *child;
3275 WARN_ON_ONCE(event->ctx->parent_ctx);
3276 mutex_lock(&event->child_mutex);
3278 list_for_each_entry(child, &event->child_list, child_list)
3280 mutex_unlock(&event->child_mutex);
3283 static void perf_event_for_each(struct perf_event *event,
3284 void (*func)(struct perf_event *))
3286 struct perf_event_context *ctx = event->ctx;
3287 struct perf_event *sibling;
3289 WARN_ON_ONCE(ctx->parent_ctx);
3290 mutex_lock(&ctx->mutex);
3291 event = event->group_leader;
3293 perf_event_for_each_child(event, func);
3295 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3296 perf_event_for_each_child(event, func);
3297 mutex_unlock(&ctx->mutex);
3300 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3302 struct perf_event_context *ctx = event->ctx;
3306 if (!is_sampling_event(event))
3309 if (copy_from_user(&value, arg, sizeof(value)))
3315 raw_spin_lock_irq(&ctx->lock);
3316 if (event->attr.freq) {
3317 if (value > sysctl_perf_event_sample_rate) {
3322 event->attr.sample_freq = value;
3324 event->attr.sample_period = value;
3325 event->hw.sample_period = value;
3328 raw_spin_unlock_irq(&ctx->lock);
3333 static const struct file_operations perf_fops;
3335 static struct file *perf_fget_light(int fd, int *fput_needed)
3339 file = fget_light(fd, fput_needed);
3341 return ERR_PTR(-EBADF);
3343 if (file->f_op != &perf_fops) {
3344 fput_light(file, *fput_needed);
3346 return ERR_PTR(-EBADF);
3352 static int perf_event_set_output(struct perf_event *event,
3353 struct perf_event *output_event);
3354 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3356 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3358 struct perf_event *event = file->private_data;
3359 void (*func)(struct perf_event *);
3363 case PERF_EVENT_IOC_ENABLE:
3364 func = perf_event_enable;
3366 case PERF_EVENT_IOC_DISABLE:
3367 func = perf_event_disable;
3369 case PERF_EVENT_IOC_RESET:
3370 func = perf_event_reset;
3373 case PERF_EVENT_IOC_REFRESH:
3374 return perf_event_refresh(event, arg);
3376 case PERF_EVENT_IOC_PERIOD:
3377 return perf_event_period(event, (u64 __user *)arg);
3379 case PERF_EVENT_IOC_SET_OUTPUT:
3381 struct file *output_file = NULL;
3382 struct perf_event *output_event = NULL;
3383 int fput_needed = 0;
3387 output_file = perf_fget_light(arg, &fput_needed);
3388 if (IS_ERR(output_file))
3389 return PTR_ERR(output_file);
3390 output_event = output_file->private_data;
3393 ret = perf_event_set_output(event, output_event);
3395 fput_light(output_file, fput_needed);
3400 case PERF_EVENT_IOC_SET_FILTER:
3401 return perf_event_set_filter(event, (void __user *)arg);
3407 if (flags & PERF_IOC_FLAG_GROUP)
3408 perf_event_for_each(event, func);
3410 perf_event_for_each_child(event, func);
3415 int perf_event_task_enable(void)
3417 struct perf_event *event;
3419 mutex_lock(¤t->perf_event_mutex);
3420 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3421 perf_event_for_each_child(event, perf_event_enable);
3422 mutex_unlock(¤t->perf_event_mutex);
3427 int perf_event_task_disable(void)
3429 struct perf_event *event;
3431 mutex_lock(¤t->perf_event_mutex);
3432 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3433 perf_event_for_each_child(event, perf_event_disable);
3434 mutex_unlock(¤t->perf_event_mutex);
3439 #ifndef PERF_EVENT_INDEX_OFFSET
3440 # define PERF_EVENT_INDEX_OFFSET 0
3443 static int perf_event_index(struct perf_event *event)
3445 if (event->hw.state & PERF_HES_STOPPED)
3448 if (event->state != PERF_EVENT_STATE_ACTIVE)
3451 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
3454 static void calc_timer_values(struct perf_event *event,
3461 ctx_time = event->shadow_ctx_time + now;
3462 *enabled = ctx_time - event->tstamp_enabled;
3463 *running = ctx_time - event->tstamp_running;
3467 * Callers need to ensure there can be no nesting of this function, otherwise
3468 * the seqlock logic goes bad. We can not serialize this because the arch
3469 * code calls this from NMI context.
3471 void perf_event_update_userpage(struct perf_event *event)
3473 struct perf_event_mmap_page *userpg;
3474 struct ring_buffer *rb;
3475 u64 enabled, running;
3479 * compute total_time_enabled, total_time_running
3480 * based on snapshot values taken when the event
3481 * was last scheduled in.
3483 * we cannot simply called update_context_time()
3484 * because of locking issue as we can be called in
3487 calc_timer_values(event, &enabled, &running);
3488 rb = rcu_dereference(event->rb);
3492 userpg = rb->user_page;
3495 * Disable preemption so as to not let the corresponding user-space
3496 * spin too long if we get preempted.
3501 userpg->index = perf_event_index(event);
3502 userpg->offset = perf_event_count(event);
3503 if (event->state == PERF_EVENT_STATE_ACTIVE)
3504 userpg->offset -= local64_read(&event->hw.prev_count);
3506 userpg->time_enabled = enabled +
3507 atomic64_read(&event->child_total_time_enabled);
3509 userpg->time_running = running +
3510 atomic64_read(&event->child_total_time_running);
3519 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3521 struct perf_event *event = vma->vm_file->private_data;
3522 struct ring_buffer *rb;
3523 int ret = VM_FAULT_SIGBUS;
3525 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3526 if (vmf->pgoff == 0)
3532 rb = rcu_dereference(event->rb);
3536 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3539 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
3543 get_page(vmf->page);
3544 vmf->page->mapping = vma->vm_file->f_mapping;
3545 vmf->page->index = vmf->pgoff;
3554 static void ring_buffer_attach(struct perf_event *event,
3555 struct ring_buffer *rb)
3557 unsigned long flags;
3559 if (!list_empty(&event->rb_entry))
3562 spin_lock_irqsave(&rb->event_lock, flags);
3563 if (!list_empty(&event->rb_entry))
3566 list_add(&event->rb_entry, &rb->event_list);
3568 spin_unlock_irqrestore(&rb->event_lock, flags);
3571 static void ring_buffer_detach(struct perf_event *event,
3572 struct ring_buffer *rb)
3574 unsigned long flags;
3576 if (list_empty(&event->rb_entry))
3579 spin_lock_irqsave(&rb->event_lock, flags);
3580 list_del_init(&event->rb_entry);
3581 wake_up_all(&event->waitq);
3582 spin_unlock_irqrestore(&rb->event_lock, flags);
3585 static void ring_buffer_wakeup(struct perf_event *event)
3587 struct ring_buffer *rb;
3590 rb = rcu_dereference(event->rb);
3594 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3595 wake_up_all(&event->waitq);
3601 static void rb_free_rcu(struct rcu_head *rcu_head)
3603 struct ring_buffer *rb;
3605 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3609 static struct ring_buffer *ring_buffer_get(struct perf_event *event)
3611 struct ring_buffer *rb;
3614 rb = rcu_dereference(event->rb);
3616 if (!atomic_inc_not_zero(&rb->refcount))
3624 static bool ring_buffer_put(struct ring_buffer *rb)
3626 struct perf_event *event, *n;
3627 unsigned long flags;
3629 if (!atomic_dec_and_test(&rb->refcount))
3632 spin_lock_irqsave(&rb->event_lock, flags);
3633 list_for_each_entry_safe(event, n, &rb->event_list, rb_entry) {
3634 list_del_init(&event->rb_entry);
3635 wake_up_all(&event->waitq);
3637 spin_unlock_irqrestore(&rb->event_lock, flags);
3639 call_rcu(&rb->rcu_head, rb_free_rcu);
3643 static void perf_mmap_open(struct vm_area_struct *vma)
3645 struct perf_event *event = vma->vm_file->private_data;
3647 atomic_inc(&event->mmap_count);
3650 static void perf_mmap_close(struct vm_area_struct *vma)
3652 struct perf_event *event = vma->vm_file->private_data;
3654 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
3655 struct ring_buffer *rb = event->rb;
3656 struct user_struct *mmap_user = rb->mmap_user;
3657 int mmap_locked = rb->mmap_locked;
3658 unsigned long size = perf_data_size(rb);
3660 rcu_assign_pointer(event->rb, NULL);
3661 ring_buffer_detach(event, rb);
3662 mutex_unlock(&event->mmap_mutex);
3664 if (ring_buffer_put(rb)) {
3665 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
3666 vma->vm_mm->pinned_vm -= mmap_locked;
3667 free_uid(mmap_user);
3672 static const struct vm_operations_struct perf_mmap_vmops = {
3673 .open = perf_mmap_open,
3674 .close = perf_mmap_close,
3675 .fault = perf_mmap_fault,
3676 .page_mkwrite = perf_mmap_fault,
3679 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3681 struct perf_event *event = file->private_data;
3682 unsigned long user_locked, user_lock_limit;
3683 struct user_struct *user = current_user();
3684 unsigned long locked, lock_limit;
3685 struct ring_buffer *rb;
3686 unsigned long vma_size;
3687 unsigned long nr_pages;
3688 long user_extra, extra;
3689 int ret = 0, flags = 0;
3692 * Don't allow mmap() of inherited per-task counters. This would
3693 * create a performance issue due to all children writing to the
3696 if (event->cpu == -1 && event->attr.inherit)
3699 if (!(vma->vm_flags & VM_SHARED))
3702 vma_size = vma->vm_end - vma->vm_start;
3703 nr_pages = (vma_size / PAGE_SIZE) - 1;
3706 * If we have rb pages ensure they're a power-of-two number, so we
3707 * can do bitmasks instead of modulo.
3709 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3712 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3715 if (vma->vm_pgoff != 0)
3718 WARN_ON_ONCE(event->ctx->parent_ctx);
3719 mutex_lock(&event->mmap_mutex);
3721 if (event->rb->nr_pages != nr_pages)
3726 user_extra = nr_pages + 1;
3727 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3730 * Increase the limit linearly with more CPUs:
3732 user_lock_limit *= num_online_cpus();
3734 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3737 if (user_locked > user_lock_limit)
3738 extra = user_locked - user_lock_limit;
3740 lock_limit = rlimit(RLIMIT_MEMLOCK);
3741 lock_limit >>= PAGE_SHIFT;
3742 locked = vma->vm_mm->pinned_vm + extra;
3744 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3745 !capable(CAP_IPC_LOCK)) {
3752 if (vma->vm_flags & VM_WRITE)
3753 flags |= RING_BUFFER_WRITABLE;
3755 rb = rb_alloc(nr_pages,
3756 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3764 rb->mmap_locked = extra;
3765 rb->mmap_user = get_current_user();
3767 atomic_long_add(user_extra, &user->locked_vm);
3768 vma->vm_mm->pinned_vm += extra;
3770 rcu_assign_pointer(event->rb, rb);
3774 atomic_inc(&event->mmap_count);
3775 mutex_unlock(&event->mmap_mutex);
3777 vma->vm_flags |= VM_DONTCOPY | VM_RESERVED;
3778 vma->vm_ops = &perf_mmap_vmops;
3783 static int perf_fasync(int fd, struct file *filp, int on)
3785 struct inode *inode = filp->f_path.dentry->d_inode;
3786 struct perf_event *event = filp->private_data;
3789 mutex_lock(&inode->i_mutex);
3790 retval = fasync_helper(fd, filp, on, &event->fasync);
3791 mutex_unlock(&inode->i_mutex);
3799 static const struct file_operations perf_fops = {
3800 .llseek = no_llseek,
3801 .release = perf_release,
3804 .unlocked_ioctl = perf_ioctl,
3805 .compat_ioctl = perf_ioctl,
3807 .fasync = perf_fasync,
3813 * If there's data, ensure we set the poll() state and publish everything
3814 * to user-space before waking everybody up.
3817 void perf_event_wakeup(struct perf_event *event)
3819 ring_buffer_wakeup(event);
3821 if (event->pending_kill) {
3822 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3823 event->pending_kill = 0;
3827 static void perf_pending_event(struct irq_work *entry)
3829 struct perf_event *event = container_of(entry,
3830 struct perf_event, pending);
3832 if (event->pending_disable) {
3833 event->pending_disable = 0;
3834 __perf_event_disable(event);
3837 if (event->pending_wakeup) {
3838 event->pending_wakeup = 0;
3839 perf_event_wakeup(event);
3844 * We assume there is only KVM supporting the callbacks.
3845 * Later on, we might change it to a list if there is
3846 * another virtualization implementation supporting the callbacks.
3848 struct perf_guest_info_callbacks *perf_guest_cbs;
3850 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3852 perf_guest_cbs = cbs;
3855 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3857 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3859 perf_guest_cbs = NULL;
3862 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3864 static void __perf_event_header__init_id(struct perf_event_header *header,
3865 struct perf_sample_data *data,
3866 struct perf_event *event)
3868 u64 sample_type = event->attr.sample_type;
3870 data->type = sample_type;
3871 header->size += event->id_header_size;
3873 if (sample_type & PERF_SAMPLE_TID) {
3874 /* namespace issues */
3875 data->tid_entry.pid = perf_event_pid(event, current);
3876 data->tid_entry.tid = perf_event_tid(event, current);
3879 if (sample_type & PERF_SAMPLE_TIME)
3880 data->time = perf_clock();
3882 if (sample_type & PERF_SAMPLE_ID)
3883 data->id = primary_event_id(event);
3885 if (sample_type & PERF_SAMPLE_STREAM_ID)
3886 data->stream_id = event->id;
3888 if (sample_type & PERF_SAMPLE_CPU) {
3889 data->cpu_entry.cpu = raw_smp_processor_id();
3890 data->cpu_entry.reserved = 0;
3894 void perf_event_header__init_id(struct perf_event_header *header,
3895 struct perf_sample_data *data,
3896 struct perf_event *event)
3898 if (event->attr.sample_id_all)
3899 __perf_event_header__init_id(header, data, event);
3902 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3903 struct perf_sample_data *data)
3905 u64 sample_type = data->type;
3907 if (sample_type & PERF_SAMPLE_TID)
3908 perf_output_put(handle, data->tid_entry);
3910 if (sample_type & PERF_SAMPLE_TIME)
3911 perf_output_put(handle, data->time);
3913 if (sample_type & PERF_SAMPLE_ID)
3914 perf_output_put(handle, data->id);
3916 if (sample_type & PERF_SAMPLE_STREAM_ID)
3917 perf_output_put(handle, data->stream_id);
3919 if (sample_type & PERF_SAMPLE_CPU)
3920 perf_output_put(handle, data->cpu_entry);
3923 void perf_event__output_id_sample(struct perf_event *event,
3924 struct perf_output_handle *handle,
3925 struct perf_sample_data *sample)
3927 if (event->attr.sample_id_all)
3928 __perf_event__output_id_sample(handle, sample);
3931 static void perf_output_read_one(struct perf_output_handle *handle,
3932 struct perf_event *event,
3933 u64 enabled, u64 running)
3935 u64 read_format = event->attr.read_format;
3939 values[n++] = perf_event_count(event);
3940 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3941 values[n++] = enabled +
3942 atomic64_read(&event->child_total_time_enabled);
3944 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3945 values[n++] = running +
3946 atomic64_read(&event->child_total_time_running);
3948 if (read_format & PERF_FORMAT_ID)
3949 values[n++] = primary_event_id(event);
3951 __output_copy(handle, values, n * sizeof(u64));
3955 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3957 static void perf_output_read_group(struct perf_output_handle *handle,
3958 struct perf_event *event,
3959 u64 enabled, u64 running)
3961 struct perf_event *leader = event->group_leader, *sub;
3962 u64 read_format = event->attr.read_format;
3966 values[n++] = 1 + leader->nr_siblings;
3968 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3969 values[n++] = enabled;
3971 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3972 values[n++] = running;
3974 if (leader != event)
3975 leader->pmu->read(leader);
3977 values[n++] = perf_event_count(leader);
3978 if (read_format & PERF_FORMAT_ID)
3979 values[n++] = primary_event_id(leader);
3981 __output_copy(handle, values, n * sizeof(u64));
3983 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3987 sub->pmu->read(sub);
3989 values[n++] = perf_event_count(sub);
3990 if (read_format & PERF_FORMAT_ID)
3991 values[n++] = primary_event_id(sub);
3993 __output_copy(handle, values, n * sizeof(u64));
3997 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
3998 PERF_FORMAT_TOTAL_TIME_RUNNING)
4000 static void perf_output_read(struct perf_output_handle *handle,
4001 struct perf_event *event)
4003 u64 enabled = 0, running = 0;
4004 u64 read_format = event->attr.read_format;
4007 * compute total_time_enabled, total_time_running
4008 * based on snapshot values taken when the event
4009 * was last scheduled in.
4011 * we cannot simply called update_context_time()
4012 * because of locking issue as we are called in
4015 if (read_format & PERF_FORMAT_TOTAL_TIMES)
4016 calc_timer_values(event, &enabled, &running);
4018 if (event->attr.read_format & PERF_FORMAT_GROUP)
4019 perf_output_read_group(handle, event, enabled, running);
4021 perf_output_read_one(handle, event, enabled, running);
4024 void perf_output_sample(struct perf_output_handle *handle,
4025 struct perf_event_header *header,
4026 struct perf_sample_data *data,
4027 struct perf_event *event)
4029 u64 sample_type = data->type;
4031 perf_output_put(handle, *header);
4033 if (sample_type & PERF_SAMPLE_IP)
4034 perf_output_put(handle, data->ip);
4036 if (sample_type & PERF_SAMPLE_TID)
4037 perf_output_put(handle, data->tid_entry);
4039 if (sample_type & PERF_SAMPLE_TIME)
4040 perf_output_put(handle, data->time);
4042 if (sample_type & PERF_SAMPLE_ADDR)
4043 perf_output_put(handle, data->addr);
4045 if (sample_type & PERF_SAMPLE_ID)
4046 perf_output_put(handle, data->id);
4048 if (sample_type & PERF_SAMPLE_STREAM_ID)
4049 perf_output_put(handle, data->stream_id);
4051 if (sample_type & PERF_SAMPLE_CPU)
4052 perf_output_put(handle, data->cpu_entry);
4054 if (sample_type & PERF_SAMPLE_PERIOD)
4055 perf_output_put(handle, data->period);
4057 if (sample_type & PERF_SAMPLE_READ)
4058 perf_output_read(handle, event);
4060 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4061 if (data->callchain) {
4064 if (data->callchain)
4065 size += data->callchain->nr;
4067 size *= sizeof(u64);
4069 __output_copy(handle, data->callchain, size);
4072 perf_output_put(handle, nr);
4076 if (sample_type & PERF_SAMPLE_RAW) {
4078 perf_output_put(handle, data->raw->size);
4079 __output_copy(handle, data->raw->data,
4086 .size = sizeof(u32),
4089 perf_output_put(handle, raw);
4093 if (!event->attr.watermark) {
4094 int wakeup_events = event->attr.wakeup_events;
4096 if (wakeup_events) {
4097 struct ring_buffer *rb = handle->rb;
4098 int events = local_inc_return(&rb->events);
4100 if (events >= wakeup_events) {
4101 local_sub(wakeup_events, &rb->events);
4102 local_inc(&rb->wakeup);
4108 void perf_prepare_sample(struct perf_event_header *header,
4109 struct perf_sample_data *data,
4110 struct perf_event *event,
4111 struct pt_regs *regs)
4113 u64 sample_type = event->attr.sample_type;
4115 header->type = PERF_RECORD_SAMPLE;
4116 header->size = sizeof(*header) + event->header_size;
4119 header->misc |= perf_misc_flags(regs);
4121 __perf_event_header__init_id(header, data, event);
4123 if (sample_type & PERF_SAMPLE_IP)
4124 data->ip = perf_instruction_pointer(regs);
4126 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4129 data->callchain = perf_callchain(regs);
4131 if (data->callchain)
4132 size += data->callchain->nr;
4134 header->size += size * sizeof(u64);
4137 if (sample_type & PERF_SAMPLE_RAW) {
4138 int size = sizeof(u32);
4141 size += data->raw->size;
4143 size += sizeof(u32);
4145 WARN_ON_ONCE(size & (sizeof(u64)-1));
4146 header->size += size;
4150 static void perf_event_output(struct perf_event *event,
4151 struct perf_sample_data *data,
4152 struct pt_regs *regs)
4154 struct perf_output_handle handle;
4155 struct perf_event_header header;
4157 /* protect the callchain buffers */
4160 perf_prepare_sample(&header, data, event, regs);
4162 if (perf_output_begin(&handle, event, header.size))
4165 perf_output_sample(&handle, &header, data, event);
4167 perf_output_end(&handle);
4177 struct perf_read_event {
4178 struct perf_event_header header;
4185 perf_event_read_event(struct perf_event *event,
4186 struct task_struct *task)
4188 struct perf_output_handle handle;
4189 struct perf_sample_data sample;
4190 struct perf_read_event read_event = {
4192 .type = PERF_RECORD_READ,
4194 .size = sizeof(read_event) + event->read_size,
4196 .pid = perf_event_pid(event, task),
4197 .tid = perf_event_tid(event, task),
4201 perf_event_header__init_id(&read_event.header, &sample, event);
4202 ret = perf_output_begin(&handle, event, read_event.header.size);
4206 perf_output_put(&handle, read_event);
4207 perf_output_read(&handle, event);
4208 perf_event__output_id_sample(event, &handle, &sample);
4210 perf_output_end(&handle);
4214 * task tracking -- fork/exit
4216 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4219 struct perf_task_event {
4220 struct task_struct *task;
4221 struct perf_event_context *task_ctx;
4224 struct perf_event_header header;
4234 static void perf_event_task_output(struct perf_event *event,
4235 struct perf_task_event *task_event)
4237 struct perf_output_handle handle;
4238 struct perf_sample_data sample;
4239 struct task_struct *task = task_event->task;
4240 int ret, size = task_event->event_id.header.size;
4242 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4244 ret = perf_output_begin(&handle, event,
4245 task_event->event_id.header.size);
4249 task_event->event_id.pid = perf_event_pid(event, task);
4250 task_event->event_id.ppid = perf_event_pid(event, current);
4252 task_event->event_id.tid = perf_event_tid(event, task);
4253 task_event->event_id.ptid = perf_event_tid(event, current);
4255 perf_output_put(&handle, task_event->event_id);
4257 perf_event__output_id_sample(event, &handle, &sample);
4259 perf_output_end(&handle);
4261 task_event->event_id.header.size = size;
4264 static int perf_event_task_match(struct perf_event *event)
4266 if (event->state < PERF_EVENT_STATE_INACTIVE)
4269 if (!event_filter_match(event))
4272 if (event->attr.comm || event->attr.mmap ||
4273 event->attr.mmap_data || event->attr.task)
4279 static void perf_event_task_ctx(struct perf_event_context *ctx,
4280 struct perf_task_event *task_event)
4282 struct perf_event *event;
4284 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4285 if (perf_event_task_match(event))
4286 perf_event_task_output(event, task_event);
4290 static void perf_event_task_event(struct perf_task_event *task_event)
4292 struct perf_cpu_context *cpuctx;
4293 struct perf_event_context *ctx;
4298 list_for_each_entry_rcu(pmu, &pmus, entry) {
4299 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4300 if (cpuctx->active_pmu != pmu)
4302 perf_event_task_ctx(&cpuctx->ctx, task_event);
4304 ctx = task_event->task_ctx;
4306 ctxn = pmu->task_ctx_nr;
4309 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4312 perf_event_task_ctx(ctx, task_event);
4314 put_cpu_ptr(pmu->pmu_cpu_context);
4319 static void perf_event_task(struct task_struct *task,
4320 struct perf_event_context *task_ctx,
4323 struct perf_task_event task_event;
4325 if (!atomic_read(&nr_comm_events) &&
4326 !atomic_read(&nr_mmap_events) &&
4327 !atomic_read(&nr_task_events))
4330 task_event = (struct perf_task_event){
4332 .task_ctx = task_ctx,
4335 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4337 .size = sizeof(task_event.event_id),
4343 .time = perf_clock(),
4347 perf_event_task_event(&task_event);
4350 void perf_event_fork(struct task_struct *task)
4352 perf_event_task(task, NULL, 1);
4359 struct perf_comm_event {
4360 struct task_struct *task;
4365 struct perf_event_header header;
4372 static void perf_event_comm_output(struct perf_event *event,
4373 struct perf_comm_event *comm_event)
4375 struct perf_output_handle handle;
4376 struct perf_sample_data sample;
4377 int size = comm_event->event_id.header.size;
4380 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4381 ret = perf_output_begin(&handle, event,
4382 comm_event->event_id.header.size);
4387 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4388 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4390 perf_output_put(&handle, comm_event->event_id);
4391 __output_copy(&handle, comm_event->comm,
4392 comm_event->comm_size);
4394 perf_event__output_id_sample(event, &handle, &sample);
4396 perf_output_end(&handle);
4398 comm_event->event_id.header.size = size;
4401 static int perf_event_comm_match(struct perf_event *event)
4403 if (event->state < PERF_EVENT_STATE_INACTIVE)
4406 if (!event_filter_match(event))
4409 if (event->attr.comm)
4415 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4416 struct perf_comm_event *comm_event)
4418 struct perf_event *event;
4420 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4421 if (perf_event_comm_match(event))
4422 perf_event_comm_output(event, comm_event);
4426 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4428 struct perf_cpu_context *cpuctx;
4429 struct perf_event_context *ctx;
4430 char comm[TASK_COMM_LEN];
4435 memset(comm, 0, sizeof(comm));
4436 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4437 size = ALIGN(strlen(comm)+1, sizeof(u64));
4439 comm_event->comm = comm;
4440 comm_event->comm_size = size;
4442 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4444 list_for_each_entry_rcu(pmu, &pmus, entry) {
4445 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4446 if (cpuctx->active_pmu != pmu)
4448 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4450 ctxn = pmu->task_ctx_nr;
4454 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4456 perf_event_comm_ctx(ctx, comm_event);
4458 put_cpu_ptr(pmu->pmu_cpu_context);
4463 void perf_event_comm(struct task_struct *task)
4465 struct perf_comm_event comm_event;
4466 struct perf_event_context *ctx;
4469 for_each_task_context_nr(ctxn) {
4470 ctx = task->perf_event_ctxp[ctxn];
4474 perf_event_enable_on_exec(ctx);
4477 if (!atomic_read(&nr_comm_events))
4480 comm_event = (struct perf_comm_event){
4486 .type = PERF_RECORD_COMM,
4495 perf_event_comm_event(&comm_event);
4502 struct perf_mmap_event {
4503 struct vm_area_struct *vma;
4505 const char *file_name;
4509 struct perf_event_header header;
4519 static void perf_event_mmap_output(struct perf_event *event,
4520 struct perf_mmap_event *mmap_event)
4522 struct perf_output_handle handle;
4523 struct perf_sample_data sample;
4524 int size = mmap_event->event_id.header.size;
4527 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4528 ret = perf_output_begin(&handle, event,
4529 mmap_event->event_id.header.size);
4533 mmap_event->event_id.pid = perf_event_pid(event, current);
4534 mmap_event->event_id.tid = perf_event_tid(event, current);
4536 perf_output_put(&handle, mmap_event->event_id);
4537 __output_copy(&handle, mmap_event->file_name,
4538 mmap_event->file_size);
4540 perf_event__output_id_sample(event, &handle, &sample);
4542 perf_output_end(&handle);
4544 mmap_event->event_id.header.size = size;
4547 static int perf_event_mmap_match(struct perf_event *event,
4548 struct perf_mmap_event *mmap_event,
4551 if (event->state < PERF_EVENT_STATE_INACTIVE)
4554 if (!event_filter_match(event))
4557 if ((!executable && event->attr.mmap_data) ||
4558 (executable && event->attr.mmap))
4564 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4565 struct perf_mmap_event *mmap_event,
4568 struct perf_event *event;
4570 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4571 if (perf_event_mmap_match(event, mmap_event, executable))
4572 perf_event_mmap_output(event, mmap_event);
4576 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4578 struct perf_cpu_context *cpuctx;
4579 struct perf_event_context *ctx;
4580 struct vm_area_struct *vma = mmap_event->vma;
4581 struct file *file = vma->vm_file;
4589 memset(tmp, 0, sizeof(tmp));
4593 * d_path works from the end of the rb backwards, so we
4594 * need to add enough zero bytes after the string to handle
4595 * the 64bit alignment we do later.
4597 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4599 name = strncpy(tmp, "//enomem", sizeof(tmp));
4602 name = d_path(&file->f_path, buf, PATH_MAX);
4604 name = strncpy(tmp, "//toolong", sizeof(tmp));
4608 if (arch_vma_name(mmap_event->vma)) {
4609 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4615 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4617 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4618 vma->vm_end >= vma->vm_mm->brk) {
4619 name = strncpy(tmp, "[heap]", sizeof(tmp));
4621 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4622 vma->vm_end >= vma->vm_mm->start_stack) {
4623 name = strncpy(tmp, "[stack]", sizeof(tmp));
4627 name = strncpy(tmp, "//anon", sizeof(tmp));
4632 size = ALIGN(strlen(name)+1, sizeof(u64));
4634 mmap_event->file_name = name;
4635 mmap_event->file_size = size;
4637 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4640 list_for_each_entry_rcu(pmu, &pmus, entry) {
4641 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4642 if (cpuctx->active_pmu != pmu)
4644 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4645 vma->vm_flags & VM_EXEC);
4647 ctxn = pmu->task_ctx_nr;
4651 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4653 perf_event_mmap_ctx(ctx, mmap_event,
4654 vma->vm_flags & VM_EXEC);
4657 put_cpu_ptr(pmu->pmu_cpu_context);
4664 void perf_event_mmap(struct vm_area_struct *vma)
4666 struct perf_mmap_event mmap_event;
4668 if (!atomic_read(&nr_mmap_events))
4671 mmap_event = (struct perf_mmap_event){
4677 .type = PERF_RECORD_MMAP,
4678 .misc = PERF_RECORD_MISC_USER,
4683 .start = vma->vm_start,
4684 .len = vma->vm_end - vma->vm_start,
4685 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4689 perf_event_mmap_event(&mmap_event);
4693 * IRQ throttle logging
4696 static void perf_log_throttle(struct perf_event *event, int enable)
4698 struct perf_output_handle handle;
4699 struct perf_sample_data sample;
4703 struct perf_event_header header;
4707 } throttle_event = {
4709 .type = PERF_RECORD_THROTTLE,
4711 .size = sizeof(throttle_event),
4713 .time = perf_clock(),
4714 .id = primary_event_id(event),
4715 .stream_id = event->id,
4719 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4721 perf_event_header__init_id(&throttle_event.header, &sample, event);
4723 ret = perf_output_begin(&handle, event,
4724 throttle_event.header.size);
4728 perf_output_put(&handle, throttle_event);
4729 perf_event__output_id_sample(event, &handle, &sample);
4730 perf_output_end(&handle);
4734 * Generic event overflow handling, sampling.
4737 static int __perf_event_overflow(struct perf_event *event,
4738 int throttle, struct perf_sample_data *data,
4739 struct pt_regs *regs)
4741 int events = atomic_read(&event->event_limit);
4742 struct hw_perf_event *hwc = &event->hw;
4746 * Non-sampling counters might still use the PMI to fold short
4747 * hardware counters, ignore those.
4749 if (unlikely(!is_sampling_event(event)))
4752 if (unlikely(hwc->interrupts >= max_samples_per_tick)) {
4754 hwc->interrupts = MAX_INTERRUPTS;
4755 perf_log_throttle(event, 0);
4761 if (event->attr.freq) {
4762 u64 now = perf_clock();
4763 s64 delta = now - hwc->freq_time_stamp;
4765 hwc->freq_time_stamp = now;
4767 if (delta > 0 && delta < 2*TICK_NSEC)
4768 perf_adjust_period(event, delta, hwc->last_period);
4772 * XXX event_limit might not quite work as expected on inherited
4776 event->pending_kill = POLL_IN;
4777 if (events && atomic_dec_and_test(&event->event_limit)) {
4779 event->pending_kill = POLL_HUP;
4780 event->pending_disable = 1;
4781 irq_work_queue(&event->pending);
4784 if (event->overflow_handler)
4785 event->overflow_handler(event, data, regs);
4787 perf_event_output(event, data, regs);
4789 if (event->fasync && event->pending_kill) {
4790 event->pending_wakeup = 1;
4791 irq_work_queue(&event->pending);
4797 int perf_event_overflow(struct perf_event *event,
4798 struct perf_sample_data *data,
4799 struct pt_regs *regs)
4801 return __perf_event_overflow(event, 1, data, regs);
4805 * Generic software event infrastructure
4808 struct swevent_htable {
4809 struct swevent_hlist *swevent_hlist;
4810 struct mutex hlist_mutex;
4813 /* Recursion avoidance in each contexts */
4814 int recursion[PERF_NR_CONTEXTS];
4817 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4820 * We directly increment event->count and keep a second value in
4821 * event->hw.period_left to count intervals. This period event
4822 * is kept in the range [-sample_period, 0] so that we can use the
4826 static u64 perf_swevent_set_period(struct perf_event *event)
4828 struct hw_perf_event *hwc = &event->hw;
4829 u64 period = hwc->last_period;
4833 hwc->last_period = hwc->sample_period;
4836 old = val = local64_read(&hwc->period_left);
4840 nr = div64_u64(period + val, period);
4841 offset = nr * period;
4843 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4849 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4850 struct perf_sample_data *data,
4851 struct pt_regs *regs)
4853 struct hw_perf_event *hwc = &event->hw;
4856 data->period = event->hw.last_period;
4858 overflow = perf_swevent_set_period(event);
4860 if (hwc->interrupts == MAX_INTERRUPTS)
4863 for (; overflow; overflow--) {
4864 if (__perf_event_overflow(event, throttle,
4867 * We inhibit the overflow from happening when
4868 * hwc->interrupts == MAX_INTERRUPTS.
4876 static void perf_swevent_event(struct perf_event *event, u64 nr,
4877 struct perf_sample_data *data,
4878 struct pt_regs *regs)
4880 struct hw_perf_event *hwc = &event->hw;
4882 local64_add(nr, &event->count);
4887 if (!is_sampling_event(event))
4890 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4891 return perf_swevent_overflow(event, 1, data, regs);
4893 if (local64_add_negative(nr, &hwc->period_left))
4896 perf_swevent_overflow(event, 0, data, regs);
4899 static int perf_exclude_event(struct perf_event *event,
4900 struct pt_regs *regs)
4902 if (event->hw.state & PERF_HES_STOPPED)
4906 if (event->attr.exclude_user && user_mode(regs))
4909 if (event->attr.exclude_kernel && !user_mode(regs))
4916 static int perf_swevent_match(struct perf_event *event,
4917 enum perf_type_id type,
4919 struct perf_sample_data *data,
4920 struct pt_regs *regs)
4922 if (event->attr.type != type)
4925 if (event->attr.config != event_id)
4928 if (perf_exclude_event(event, regs))
4934 static inline u64 swevent_hash(u64 type, u32 event_id)
4936 u64 val = event_id | (type << 32);
4938 return hash_64(val, SWEVENT_HLIST_BITS);
4941 static inline struct hlist_head *
4942 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
4944 u64 hash = swevent_hash(type, event_id);
4946 return &hlist->heads[hash];
4949 /* For the read side: events when they trigger */
4950 static inline struct hlist_head *
4951 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
4953 struct swevent_hlist *hlist;
4955 hlist = rcu_dereference(swhash->swevent_hlist);
4959 return __find_swevent_head(hlist, type, event_id);
4962 /* For the event head insertion and removal in the hlist */
4963 static inline struct hlist_head *
4964 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
4966 struct swevent_hlist *hlist;
4967 u32 event_id = event->attr.config;
4968 u64 type = event->attr.type;
4971 * Event scheduling is always serialized against hlist allocation
4972 * and release. Which makes the protected version suitable here.
4973 * The context lock guarantees that.
4975 hlist = rcu_dereference_protected(swhash->swevent_hlist,
4976 lockdep_is_held(&event->ctx->lock));
4980 return __find_swevent_head(hlist, type, event_id);
4983 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4985 struct perf_sample_data *data,
4986 struct pt_regs *regs)
4988 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4989 struct perf_event *event;
4990 struct hlist_node *node;
4991 struct hlist_head *head;
4994 head = find_swevent_head_rcu(swhash, type, event_id);
4998 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4999 if (perf_swevent_match(event, type, event_id, data, regs))
5000 perf_swevent_event(event, nr, data, regs);
5006 int perf_swevent_get_recursion_context(void)
5008 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5010 return get_recursion_context(swhash->recursion);
5012 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
5014 inline void perf_swevent_put_recursion_context(int rctx)
5016 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5018 put_recursion_context(swhash->recursion, rctx);
5021 void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
5023 struct perf_sample_data data;
5026 preempt_disable_notrace();
5027 rctx = perf_swevent_get_recursion_context();
5031 perf_sample_data_init(&data, addr);
5033 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
5035 perf_swevent_put_recursion_context(rctx);
5036 preempt_enable_notrace();
5039 static void perf_swevent_read(struct perf_event *event)
5043 static int perf_swevent_add(struct perf_event *event, int flags)
5045 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5046 struct hw_perf_event *hwc = &event->hw;
5047 struct hlist_head *head;
5049 if (is_sampling_event(event)) {
5050 hwc->last_period = hwc->sample_period;
5051 perf_swevent_set_period(event);
5054 hwc->state = !(flags & PERF_EF_START);
5056 head = find_swevent_head(swhash, event);
5057 if (WARN_ON_ONCE(!head))
5060 hlist_add_head_rcu(&event->hlist_entry, head);
5065 static void perf_swevent_del(struct perf_event *event, int flags)
5067 hlist_del_rcu(&event->hlist_entry);
5070 static void perf_swevent_start(struct perf_event *event, int flags)
5072 event->hw.state = 0;
5075 static void perf_swevent_stop(struct perf_event *event, int flags)
5077 event->hw.state = PERF_HES_STOPPED;
5080 /* Deref the hlist from the update side */
5081 static inline struct swevent_hlist *
5082 swevent_hlist_deref(struct swevent_htable *swhash)
5084 return rcu_dereference_protected(swhash->swevent_hlist,
5085 lockdep_is_held(&swhash->hlist_mutex));
5088 static void swevent_hlist_release(struct swevent_htable *swhash)
5090 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
5095 rcu_assign_pointer(swhash->swevent_hlist, NULL);
5096 kfree_rcu(hlist, rcu_head);
5099 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5101 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5103 mutex_lock(&swhash->hlist_mutex);
5105 if (!--swhash->hlist_refcount)
5106 swevent_hlist_release(swhash);
5108 mutex_unlock(&swhash->hlist_mutex);
5111 static void swevent_hlist_put(struct perf_event *event)
5115 if (event->cpu != -1) {
5116 swevent_hlist_put_cpu(event, event->cpu);
5120 for_each_possible_cpu(cpu)
5121 swevent_hlist_put_cpu(event, cpu);
5124 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5126 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5129 mutex_lock(&swhash->hlist_mutex);
5131 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
5132 struct swevent_hlist *hlist;
5134 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5139 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5141 swhash->hlist_refcount++;
5143 mutex_unlock(&swhash->hlist_mutex);
5148 static int swevent_hlist_get(struct perf_event *event)
5151 int cpu, failed_cpu;
5153 if (event->cpu != -1)
5154 return swevent_hlist_get_cpu(event, event->cpu);
5157 for_each_possible_cpu(cpu) {
5158 err = swevent_hlist_get_cpu(event, cpu);
5168 for_each_possible_cpu(cpu) {
5169 if (cpu == failed_cpu)
5171 swevent_hlist_put_cpu(event, cpu);
5178 struct jump_label_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
5180 static void sw_perf_event_destroy(struct perf_event *event)
5182 u64 event_id = event->attr.config;
5184 WARN_ON(event->parent);
5186 jump_label_dec(&perf_swevent_enabled[event_id]);
5187 swevent_hlist_put(event);
5190 static int perf_swevent_init(struct perf_event *event)
5192 u64 event_id = event->attr.config;
5194 if (event->attr.type != PERF_TYPE_SOFTWARE)
5198 case PERF_COUNT_SW_CPU_CLOCK:
5199 case PERF_COUNT_SW_TASK_CLOCK:
5206 if (event_id >= PERF_COUNT_SW_MAX)
5209 if (!event->parent) {
5212 err = swevent_hlist_get(event);
5216 jump_label_inc(&perf_swevent_enabled[event_id]);
5217 event->destroy = sw_perf_event_destroy;
5223 static struct pmu perf_swevent = {
5224 .task_ctx_nr = perf_sw_context,
5226 .event_init = perf_swevent_init,
5227 .add = perf_swevent_add,
5228 .del = perf_swevent_del,
5229 .start = perf_swevent_start,
5230 .stop = perf_swevent_stop,
5231 .read = perf_swevent_read,
5234 #ifdef CONFIG_EVENT_TRACING
5236 static int perf_tp_filter_match(struct perf_event *event,
5237 struct perf_sample_data *data)
5239 void *record = data->raw->data;
5241 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5246 static int perf_tp_event_match(struct perf_event *event,
5247 struct perf_sample_data *data,
5248 struct pt_regs *regs)
5250 if (event->hw.state & PERF_HES_STOPPED)
5253 * All tracepoints are from kernel-space.
5255 if (event->attr.exclude_kernel)
5258 if (!perf_tp_filter_match(event, data))
5264 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5265 struct pt_regs *regs, struct hlist_head *head, int rctx)
5267 struct perf_sample_data data;
5268 struct perf_event *event;
5269 struct hlist_node *node;
5271 struct perf_raw_record raw = {
5276 perf_sample_data_init(&data, addr);
5279 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5280 if (perf_tp_event_match(event, &data, regs))
5281 perf_swevent_event(event, count, &data, regs);
5284 perf_swevent_put_recursion_context(rctx);
5286 EXPORT_SYMBOL_GPL(perf_tp_event);
5288 static void tp_perf_event_destroy(struct perf_event *event)
5290 perf_trace_destroy(event);
5293 static int perf_tp_event_init(struct perf_event *event)
5297 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5300 err = perf_trace_init(event);
5304 event->destroy = tp_perf_event_destroy;
5309 static struct pmu perf_tracepoint = {
5310 .task_ctx_nr = perf_sw_context,
5312 .event_init = perf_tp_event_init,
5313 .add = perf_trace_add,
5314 .del = perf_trace_del,
5315 .start = perf_swevent_start,
5316 .stop = perf_swevent_stop,
5317 .read = perf_swevent_read,
5320 static inline void perf_tp_register(void)
5322 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5325 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5330 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5333 filter_str = strndup_user(arg, PAGE_SIZE);
5334 if (IS_ERR(filter_str))
5335 return PTR_ERR(filter_str);
5337 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5343 static void perf_event_free_filter(struct perf_event *event)
5345 ftrace_profile_free_filter(event);
5350 static inline void perf_tp_register(void)
5354 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5359 static void perf_event_free_filter(struct perf_event *event)
5363 #endif /* CONFIG_EVENT_TRACING */
5365 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5366 void perf_bp_event(struct perf_event *bp, void *data)
5368 struct perf_sample_data sample;
5369 struct pt_regs *regs = data;
5371 perf_sample_data_init(&sample, bp->attr.bp_addr);
5373 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5374 perf_swevent_event(bp, 1, &sample, regs);
5379 * hrtimer based swevent callback
5382 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5384 enum hrtimer_restart ret = HRTIMER_RESTART;
5385 struct perf_sample_data data;
5386 struct pt_regs *regs;
5387 struct perf_event *event;
5390 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5392 if (event->state != PERF_EVENT_STATE_ACTIVE)
5393 return HRTIMER_NORESTART;
5395 event->pmu->read(event);
5397 perf_sample_data_init(&data, 0);
5398 data.period = event->hw.last_period;
5399 regs = get_irq_regs();
5401 if (regs && !perf_exclude_event(event, regs)) {
5402 if (!(event->attr.exclude_idle && current->pid == 0))
5403 if (perf_event_overflow(event, &data, regs))
5404 ret = HRTIMER_NORESTART;
5407 period = max_t(u64, 10000, event->hw.sample_period);
5408 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5413 static void perf_swevent_start_hrtimer(struct perf_event *event)
5415 struct hw_perf_event *hwc = &event->hw;
5418 if (!is_sampling_event(event))
5421 period = local64_read(&hwc->period_left);
5426 local64_set(&hwc->period_left, 0);
5428 period = max_t(u64, 10000, hwc->sample_period);
5430 __hrtimer_start_range_ns(&hwc->hrtimer,
5431 ns_to_ktime(period), 0,
5432 HRTIMER_MODE_REL_PINNED, 0);
5435 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5437 struct hw_perf_event *hwc = &event->hw;
5439 if (is_sampling_event(event)) {
5440 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5441 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5443 hrtimer_cancel(&hwc->hrtimer);
5447 static void perf_swevent_init_hrtimer(struct perf_event *event)
5449 struct hw_perf_event *hwc = &event->hw;
5451 if (!is_sampling_event(event))
5454 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5455 hwc->hrtimer.function = perf_swevent_hrtimer;
5458 * Since hrtimers have a fixed rate, we can do a static freq->period
5459 * mapping and avoid the whole period adjust feedback stuff.
5461 if (event->attr.freq) {
5462 long freq = event->attr.sample_freq;
5464 event->attr.sample_period = NSEC_PER_SEC / freq;
5465 hwc->sample_period = event->attr.sample_period;
5466 local64_set(&hwc->period_left, hwc->sample_period);
5467 event->attr.freq = 0;
5472 * Software event: cpu wall time clock
5475 static void cpu_clock_event_update(struct perf_event *event)
5480 now = local_clock();
5481 prev = local64_xchg(&event->hw.prev_count, now);
5482 local64_add(now - prev, &event->count);
5485 static void cpu_clock_event_start(struct perf_event *event, int flags)
5487 local64_set(&event->hw.prev_count, local_clock());
5488 perf_swevent_start_hrtimer(event);
5491 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5493 perf_swevent_cancel_hrtimer(event);
5494 cpu_clock_event_update(event);
5497 static int cpu_clock_event_add(struct perf_event *event, int flags)
5499 if (flags & PERF_EF_START)
5500 cpu_clock_event_start(event, flags);
5505 static void cpu_clock_event_del(struct perf_event *event, int flags)
5507 cpu_clock_event_stop(event, flags);
5510 static void cpu_clock_event_read(struct perf_event *event)
5512 cpu_clock_event_update(event);
5515 static int cpu_clock_event_init(struct perf_event *event)
5517 if (event->attr.type != PERF_TYPE_SOFTWARE)
5520 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5523 perf_swevent_init_hrtimer(event);
5528 static struct pmu perf_cpu_clock = {
5529 .task_ctx_nr = perf_sw_context,
5531 .event_init = cpu_clock_event_init,
5532 .add = cpu_clock_event_add,
5533 .del = cpu_clock_event_del,
5534 .start = cpu_clock_event_start,
5535 .stop = cpu_clock_event_stop,
5536 .read = cpu_clock_event_read,
5540 * Software event: task time clock
5543 static void task_clock_event_update(struct perf_event *event, u64 now)
5548 prev = local64_xchg(&event->hw.prev_count, now);
5550 local64_add(delta, &event->count);
5553 static void task_clock_event_start(struct perf_event *event, int flags)
5555 local64_set(&event->hw.prev_count, event->ctx->time);
5556 perf_swevent_start_hrtimer(event);
5559 static void task_clock_event_stop(struct perf_event *event, int flags)
5561 perf_swevent_cancel_hrtimer(event);
5562 task_clock_event_update(event, event->ctx->time);
5565 static int task_clock_event_add(struct perf_event *event, int flags)
5567 if (flags & PERF_EF_START)
5568 task_clock_event_start(event, flags);
5573 static void task_clock_event_del(struct perf_event *event, int flags)
5575 task_clock_event_stop(event, PERF_EF_UPDATE);
5578 static void task_clock_event_read(struct perf_event *event)
5580 u64 now = perf_clock();
5581 u64 delta = now - event->ctx->timestamp;
5582 u64 time = event->ctx->time + delta;
5584 task_clock_event_update(event, time);
5587 static int task_clock_event_init(struct perf_event *event)
5589 if (event->attr.type != PERF_TYPE_SOFTWARE)
5592 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5595 perf_swevent_init_hrtimer(event);
5600 static struct pmu perf_task_clock = {
5601 .task_ctx_nr = perf_sw_context,
5603 .event_init = task_clock_event_init,
5604 .add = task_clock_event_add,
5605 .del = task_clock_event_del,
5606 .start = task_clock_event_start,
5607 .stop = task_clock_event_stop,
5608 .read = task_clock_event_read,
5611 static void perf_pmu_nop_void(struct pmu *pmu)
5615 static int perf_pmu_nop_int(struct pmu *pmu)
5620 static void perf_pmu_start_txn(struct pmu *pmu)
5622 perf_pmu_disable(pmu);
5625 static int perf_pmu_commit_txn(struct pmu *pmu)
5627 perf_pmu_enable(pmu);
5631 static void perf_pmu_cancel_txn(struct pmu *pmu)
5633 perf_pmu_enable(pmu);
5637 * Ensures all contexts with the same task_ctx_nr have the same
5638 * pmu_cpu_context too.
5640 static void *find_pmu_context(int ctxn)
5647 list_for_each_entry(pmu, &pmus, entry) {
5648 if (pmu->task_ctx_nr == ctxn)
5649 return pmu->pmu_cpu_context;
5655 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5659 for_each_possible_cpu(cpu) {
5660 struct perf_cpu_context *cpuctx;
5662 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5664 if (cpuctx->active_pmu == old_pmu)
5665 cpuctx->active_pmu = pmu;
5669 static void free_pmu_context(struct pmu *pmu)
5673 mutex_lock(&pmus_lock);
5675 * Like a real lame refcount.
5677 list_for_each_entry(i, &pmus, entry) {
5678 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5679 update_pmu_context(i, pmu);
5684 free_percpu(pmu->pmu_cpu_context);
5686 mutex_unlock(&pmus_lock);
5688 static struct idr pmu_idr;
5691 type_show(struct device *dev, struct device_attribute *attr, char *page)
5693 struct pmu *pmu = dev_get_drvdata(dev);
5695 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5698 static struct device_attribute pmu_dev_attrs[] = {
5703 static int pmu_bus_running;
5704 static struct bus_type pmu_bus = {
5705 .name = "event_source",
5706 .dev_attrs = pmu_dev_attrs,
5709 static void pmu_dev_release(struct device *dev)
5714 static int pmu_dev_alloc(struct pmu *pmu)
5718 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5722 device_initialize(pmu->dev);
5723 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5727 dev_set_drvdata(pmu->dev, pmu);
5728 pmu->dev->bus = &pmu_bus;
5729 pmu->dev->release = pmu_dev_release;
5730 ret = device_add(pmu->dev);
5738 put_device(pmu->dev);
5742 static struct lock_class_key cpuctx_mutex;
5743 static struct lock_class_key cpuctx_lock;
5745 int perf_pmu_register(struct pmu *pmu, char *name, int type)
5749 mutex_lock(&pmus_lock);
5751 pmu->pmu_disable_count = alloc_percpu(int);
5752 if (!pmu->pmu_disable_count)
5761 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5765 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5773 if (pmu_bus_running) {
5774 ret = pmu_dev_alloc(pmu);
5780 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5781 if (pmu->pmu_cpu_context)
5782 goto got_cpu_context;
5785 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5786 if (!pmu->pmu_cpu_context)
5789 for_each_possible_cpu(cpu) {
5790 struct perf_cpu_context *cpuctx;
5792 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5793 __perf_event_init_context(&cpuctx->ctx);
5794 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
5795 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
5796 cpuctx->ctx.type = cpu_context;
5797 cpuctx->ctx.pmu = pmu;
5798 cpuctx->jiffies_interval = 1;
5799 INIT_LIST_HEAD(&cpuctx->rotation_list);
5800 cpuctx->active_pmu = pmu;
5804 if (!pmu->start_txn) {
5805 if (pmu->pmu_enable) {
5807 * If we have pmu_enable/pmu_disable calls, install
5808 * transaction stubs that use that to try and batch
5809 * hardware accesses.
5811 pmu->start_txn = perf_pmu_start_txn;
5812 pmu->commit_txn = perf_pmu_commit_txn;
5813 pmu->cancel_txn = perf_pmu_cancel_txn;
5815 pmu->start_txn = perf_pmu_nop_void;
5816 pmu->commit_txn = perf_pmu_nop_int;
5817 pmu->cancel_txn = perf_pmu_nop_void;
5821 if (!pmu->pmu_enable) {
5822 pmu->pmu_enable = perf_pmu_nop_void;
5823 pmu->pmu_disable = perf_pmu_nop_void;
5826 list_add_rcu(&pmu->entry, &pmus);
5829 mutex_unlock(&pmus_lock);
5834 device_del(pmu->dev);
5835 put_device(pmu->dev);
5838 if (pmu->type >= PERF_TYPE_MAX)
5839 idr_remove(&pmu_idr, pmu->type);
5842 free_percpu(pmu->pmu_disable_count);
5846 void perf_pmu_unregister(struct pmu *pmu)
5848 mutex_lock(&pmus_lock);
5849 list_del_rcu(&pmu->entry);
5850 mutex_unlock(&pmus_lock);
5853 * We dereference the pmu list under both SRCU and regular RCU, so
5854 * synchronize against both of those.
5856 synchronize_srcu(&pmus_srcu);
5859 free_percpu(pmu->pmu_disable_count);
5860 if (pmu->type >= PERF_TYPE_MAX)
5861 idr_remove(&pmu_idr, pmu->type);
5862 device_del(pmu->dev);
5863 put_device(pmu->dev);
5864 free_pmu_context(pmu);
5867 struct pmu *perf_init_event(struct perf_event *event)
5869 struct pmu *pmu = NULL;
5873 idx = srcu_read_lock(&pmus_srcu);
5876 pmu = idr_find(&pmu_idr, event->attr.type);
5880 ret = pmu->event_init(event);
5886 list_for_each_entry_rcu(pmu, &pmus, entry) {
5888 ret = pmu->event_init(event);
5892 if (ret != -ENOENT) {
5897 pmu = ERR_PTR(-ENOENT);
5899 srcu_read_unlock(&pmus_srcu, idx);
5905 * Allocate and initialize a event structure
5907 static struct perf_event *
5908 perf_event_alloc(struct perf_event_attr *attr, int cpu,
5909 struct task_struct *task,
5910 struct perf_event *group_leader,
5911 struct perf_event *parent_event,
5912 perf_overflow_handler_t overflow_handler,
5916 struct perf_event *event;
5917 struct hw_perf_event *hwc;
5920 if ((unsigned)cpu >= nr_cpu_ids) {
5921 if (!task || cpu != -1)
5922 return ERR_PTR(-EINVAL);
5925 event = kzalloc(sizeof(*event), GFP_KERNEL);
5927 return ERR_PTR(-ENOMEM);
5930 * Single events are their own group leaders, with an
5931 * empty sibling list:
5934 group_leader = event;
5936 mutex_init(&event->child_mutex);
5937 INIT_LIST_HEAD(&event->child_list);
5939 INIT_LIST_HEAD(&event->group_entry);
5940 INIT_LIST_HEAD(&event->event_entry);
5941 INIT_LIST_HEAD(&event->sibling_list);
5942 INIT_LIST_HEAD(&event->rb_entry);
5944 init_waitqueue_head(&event->waitq);
5945 init_irq_work(&event->pending, perf_pending_event);
5947 mutex_init(&event->mmap_mutex);
5949 atomic_long_set(&event->refcount, 1);
5951 event->attr = *attr;
5952 event->group_leader = group_leader;
5956 event->parent = parent_event;
5958 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5959 event->id = atomic64_inc_return(&perf_event_id);
5961 event->state = PERF_EVENT_STATE_INACTIVE;
5964 event->attach_state = PERF_ATTACH_TASK;
5965 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5967 * hw_breakpoint is a bit difficult here..
5969 if (attr->type == PERF_TYPE_BREAKPOINT)
5970 event->hw.bp_target = task;
5974 if (!overflow_handler && parent_event) {
5975 overflow_handler = parent_event->overflow_handler;
5976 context = parent_event->overflow_handler_context;
5979 event->overflow_handler = overflow_handler;
5980 event->overflow_handler_context = context;
5983 event->state = PERF_EVENT_STATE_OFF;
5988 hwc->sample_period = attr->sample_period;
5989 if (attr->freq && attr->sample_freq)
5990 hwc->sample_period = 1;
5991 hwc->last_period = hwc->sample_period;
5993 local64_set(&hwc->period_left, hwc->sample_period);
5996 * we currently do not support PERF_FORMAT_GROUP on inherited events
5998 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6001 pmu = perf_init_event(event);
6007 else if (IS_ERR(pmu))
6012 put_pid_ns(event->ns);
6014 return ERR_PTR(err);
6017 if (!event->parent) {
6018 if (event->attach_state & PERF_ATTACH_TASK)
6019 jump_label_inc(&perf_sched_events);
6020 if (event->attr.mmap || event->attr.mmap_data)
6021 atomic_inc(&nr_mmap_events);
6022 if (event->attr.comm)
6023 atomic_inc(&nr_comm_events);
6024 if (event->attr.task)
6025 atomic_inc(&nr_task_events);
6026 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6027 err = get_callchain_buffers();
6030 return ERR_PTR(err);
6038 static int perf_copy_attr(struct perf_event_attr __user *uattr,
6039 struct perf_event_attr *attr)
6044 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6048 * zero the full structure, so that a short copy will be nice.
6050 memset(attr, 0, sizeof(*attr));
6052 ret = get_user(size, &uattr->size);
6056 if (size > PAGE_SIZE) /* silly large */
6059 if (!size) /* abi compat */
6060 size = PERF_ATTR_SIZE_VER0;
6062 if (size < PERF_ATTR_SIZE_VER0)
6066 * If we're handed a bigger struct than we know of,
6067 * ensure all the unknown bits are 0 - i.e. new
6068 * user-space does not rely on any kernel feature
6069 * extensions we dont know about yet.
6071 if (size > sizeof(*attr)) {
6072 unsigned char __user *addr;
6073 unsigned char __user *end;
6076 addr = (void __user *)uattr + sizeof(*attr);
6077 end = (void __user *)uattr + size;
6079 for (; addr < end; addr++) {
6080 ret = get_user(val, addr);
6086 size = sizeof(*attr);
6089 ret = copy_from_user(attr, uattr, size);
6093 if (attr->__reserved_1)
6096 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6099 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6106 put_user(sizeof(*attr), &uattr->size);
6112 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6114 struct ring_buffer *rb = NULL, *old_rb = NULL;
6120 /* don't allow circular references */
6121 if (event == output_event)
6125 * Don't allow cross-cpu buffers
6127 if (output_event->cpu != event->cpu)
6131 * If its not a per-cpu rb, it must be the same task.
6133 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6137 mutex_lock(&event->mmap_mutex);
6138 /* Can't redirect output if we've got an active mmap() */
6139 if (atomic_read(&event->mmap_count))
6143 /* get the rb we want to redirect to */
6144 rb = ring_buffer_get(output_event);
6150 rcu_assign_pointer(event->rb, rb);
6152 ring_buffer_detach(event, old_rb);
6155 mutex_unlock(&event->mmap_mutex);
6158 ring_buffer_put(old_rb);
6164 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6166 * @attr_uptr: event_id type attributes for monitoring/sampling
6169 * @group_fd: group leader event fd
6171 SYSCALL_DEFINE5(perf_event_open,
6172 struct perf_event_attr __user *, attr_uptr,
6173 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6175 struct perf_event *group_leader = NULL, *output_event = NULL;
6176 struct perf_event *event, *sibling;
6177 struct perf_event_attr attr;
6178 struct perf_event_context *ctx;
6179 struct file *event_file = NULL;
6180 struct file *group_file = NULL;
6181 struct task_struct *task = NULL;
6185 int fput_needed = 0;
6188 /* for future expandability... */
6189 if (flags & ~PERF_FLAG_ALL)
6192 err = perf_copy_attr(attr_uptr, &attr);
6196 if (!attr.exclude_kernel) {
6197 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6202 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6207 * In cgroup mode, the pid argument is used to pass the fd
6208 * opened to the cgroup directory in cgroupfs. The cpu argument
6209 * designates the cpu on which to monitor threads from that
6212 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6215 event_fd = get_unused_fd_flags(O_RDWR);
6219 if (group_fd != -1) {
6220 group_file = perf_fget_light(group_fd, &fput_needed);
6221 if (IS_ERR(group_file)) {
6222 err = PTR_ERR(group_file);
6225 group_leader = group_file->private_data;
6226 if (flags & PERF_FLAG_FD_OUTPUT)
6227 output_event = group_leader;
6228 if (flags & PERF_FLAG_FD_NO_GROUP)
6229 group_leader = NULL;
6232 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
6233 task = find_lively_task_by_vpid(pid);
6235 err = PTR_ERR(task);
6240 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
6242 if (IS_ERR(event)) {
6243 err = PTR_ERR(event);
6247 if (flags & PERF_FLAG_PID_CGROUP) {
6248 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6253 * - that has cgroup constraint on event->cpu
6254 * - that may need work on context switch
6256 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6257 jump_label_inc(&perf_sched_events);
6261 * Special case software events and allow them to be part of
6262 * any hardware group.
6267 (is_software_event(event) != is_software_event(group_leader))) {
6268 if (is_software_event(event)) {
6270 * If event and group_leader are not both a software
6271 * event, and event is, then group leader is not.
6273 * Allow the addition of software events to !software
6274 * groups, this is safe because software events never
6277 pmu = group_leader->pmu;
6278 } else if (is_software_event(group_leader) &&
6279 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6281 * In case the group is a pure software group, and we
6282 * try to add a hardware event, move the whole group to
6283 * the hardware context.
6290 * Get the target context (task or percpu):
6292 ctx = find_get_context(pmu, task, cpu);
6299 put_task_struct(task);
6304 * Look up the group leader (we will attach this event to it):
6310 * Do not allow a recursive hierarchy (this new sibling
6311 * becoming part of another group-sibling):
6313 if (group_leader->group_leader != group_leader)
6316 * Do not allow to attach to a group in a different
6317 * task or CPU context:
6320 if (group_leader->ctx->type != ctx->type)
6323 if (group_leader->ctx != ctx)
6328 * Only a group leader can be exclusive or pinned
6330 if (attr.exclusive || attr.pinned)
6335 err = perf_event_set_output(event, output_event);
6340 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6341 if (IS_ERR(event_file)) {
6342 err = PTR_ERR(event_file);
6347 struct perf_event_context *gctx = group_leader->ctx;
6349 mutex_lock(&gctx->mutex);
6350 perf_remove_from_context(group_leader);
6351 list_for_each_entry(sibling, &group_leader->sibling_list,
6353 perf_remove_from_context(sibling);
6356 mutex_unlock(&gctx->mutex);
6360 WARN_ON_ONCE(ctx->parent_ctx);
6361 mutex_lock(&ctx->mutex);
6364 perf_install_in_context(ctx, group_leader, cpu);
6366 list_for_each_entry(sibling, &group_leader->sibling_list,
6368 perf_install_in_context(ctx, sibling, cpu);
6373 perf_install_in_context(ctx, event, cpu);
6375 perf_unpin_context(ctx);
6376 mutex_unlock(&ctx->mutex);
6378 event->owner = current;
6380 mutex_lock(¤t->perf_event_mutex);
6381 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6382 mutex_unlock(¤t->perf_event_mutex);
6385 * Precalculate sample_data sizes
6387 perf_event__header_size(event);
6388 perf_event__id_header_size(event);
6391 * Drop the reference on the group_event after placing the
6392 * new event on the sibling_list. This ensures destruction
6393 * of the group leader will find the pointer to itself in
6394 * perf_group_detach().
6396 fput_light(group_file, fput_needed);
6397 fd_install(event_fd, event_file);
6401 perf_unpin_context(ctx);
6407 put_task_struct(task);
6409 fput_light(group_file, fput_needed);
6411 put_unused_fd(event_fd);
6416 * perf_event_create_kernel_counter
6418 * @attr: attributes of the counter to create
6419 * @cpu: cpu in which the counter is bound
6420 * @task: task to profile (NULL for percpu)
6423 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6424 struct task_struct *task,
6425 perf_overflow_handler_t overflow_handler,
6428 struct perf_event_context *ctx;
6429 struct perf_event *event;
6433 * Get the target context (task or percpu):
6436 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
6437 overflow_handler, context);
6438 if (IS_ERR(event)) {
6439 err = PTR_ERR(event);
6443 ctx = find_get_context(event->pmu, task, cpu);
6449 WARN_ON_ONCE(ctx->parent_ctx);
6450 mutex_lock(&ctx->mutex);
6451 perf_install_in_context(ctx, event, cpu);
6453 perf_unpin_context(ctx);
6454 mutex_unlock(&ctx->mutex);
6461 return ERR_PTR(err);
6463 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6465 static void sync_child_event(struct perf_event *child_event,
6466 struct task_struct *child)
6468 struct perf_event *parent_event = child_event->parent;
6471 if (child_event->attr.inherit_stat)
6472 perf_event_read_event(child_event, child);
6474 child_val = perf_event_count(child_event);
6477 * Add back the child's count to the parent's count:
6479 atomic64_add(child_val, &parent_event->child_count);
6480 atomic64_add(child_event->total_time_enabled,
6481 &parent_event->child_total_time_enabled);
6482 atomic64_add(child_event->total_time_running,
6483 &parent_event->child_total_time_running);
6486 * Remove this event from the parent's list
6488 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6489 mutex_lock(&parent_event->child_mutex);
6490 list_del_init(&child_event->child_list);
6491 mutex_unlock(&parent_event->child_mutex);
6494 * Release the parent event, if this was the last
6497 put_event(parent_event);
6501 __perf_event_exit_task(struct perf_event *child_event,
6502 struct perf_event_context *child_ctx,
6503 struct task_struct *child)
6505 if (child_event->parent) {
6506 raw_spin_lock_irq(&child_ctx->lock);
6507 perf_group_detach(child_event);
6508 raw_spin_unlock_irq(&child_ctx->lock);
6511 perf_remove_from_context(child_event);
6514 * It can happen that the parent exits first, and has events
6515 * that are still around due to the child reference. These
6516 * events need to be zapped.
6518 if (child_event->parent) {
6519 sync_child_event(child_event, child);
6520 free_event(child_event);
6524 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6526 struct perf_event *child_event, *tmp;
6527 struct perf_event_context *child_ctx;
6528 unsigned long flags;
6530 if (likely(!child->perf_event_ctxp[ctxn])) {
6531 perf_event_task(child, NULL, 0);
6535 local_irq_save(flags);
6537 * We can't reschedule here because interrupts are disabled,
6538 * and either child is current or it is a task that can't be
6539 * scheduled, so we are now safe from rescheduling changing
6542 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6545 * Take the context lock here so that if find_get_context is
6546 * reading child->perf_event_ctxp, we wait until it has
6547 * incremented the context's refcount before we do put_ctx below.
6549 raw_spin_lock(&child_ctx->lock);
6550 task_ctx_sched_out(child_ctx);
6551 child->perf_event_ctxp[ctxn] = NULL;
6553 * If this context is a clone; unclone it so it can't get
6554 * swapped to another process while we're removing all
6555 * the events from it.
6557 unclone_ctx(child_ctx);
6558 update_context_time(child_ctx);
6559 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6562 * Report the task dead after unscheduling the events so that we
6563 * won't get any samples after PERF_RECORD_EXIT. We can however still
6564 * get a few PERF_RECORD_READ events.
6566 perf_event_task(child, child_ctx, 0);
6569 * We can recurse on the same lock type through:
6571 * __perf_event_exit_task()
6572 * sync_child_event()
6574 * mutex_lock(&ctx->mutex)
6576 * But since its the parent context it won't be the same instance.
6578 mutex_lock(&child_ctx->mutex);
6581 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6583 __perf_event_exit_task(child_event, child_ctx, child);
6585 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6587 __perf_event_exit_task(child_event, child_ctx, child);
6590 * If the last event was a group event, it will have appended all
6591 * its siblings to the list, but we obtained 'tmp' before that which
6592 * will still point to the list head terminating the iteration.
6594 if (!list_empty(&child_ctx->pinned_groups) ||
6595 !list_empty(&child_ctx->flexible_groups))
6598 mutex_unlock(&child_ctx->mutex);
6604 * When a child task exits, feed back event values to parent events.
6606 void perf_event_exit_task(struct task_struct *child)
6608 struct perf_event *event, *tmp;
6611 mutex_lock(&child->perf_event_mutex);
6612 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6614 list_del_init(&event->owner_entry);
6617 * Ensure the list deletion is visible before we clear
6618 * the owner, closes a race against perf_release() where
6619 * we need to serialize on the owner->perf_event_mutex.
6622 event->owner = NULL;
6624 mutex_unlock(&child->perf_event_mutex);
6626 for_each_task_context_nr(ctxn)
6627 perf_event_exit_task_context(child, ctxn);
6630 static void perf_free_event(struct perf_event *event,
6631 struct perf_event_context *ctx)
6633 struct perf_event *parent = event->parent;
6635 if (WARN_ON_ONCE(!parent))
6638 mutex_lock(&parent->child_mutex);
6639 list_del_init(&event->child_list);
6640 mutex_unlock(&parent->child_mutex);
6644 perf_group_detach(event);
6645 list_del_event(event, ctx);
6650 * free an unexposed, unused context as created by inheritance by
6651 * perf_event_init_task below, used by fork() in case of fail.
6653 void perf_event_free_task(struct task_struct *task)
6655 struct perf_event_context *ctx;
6656 struct perf_event *event, *tmp;
6659 for_each_task_context_nr(ctxn) {
6660 ctx = task->perf_event_ctxp[ctxn];
6664 mutex_lock(&ctx->mutex);
6666 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6668 perf_free_event(event, ctx);
6670 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6672 perf_free_event(event, ctx);
6674 if (!list_empty(&ctx->pinned_groups) ||
6675 !list_empty(&ctx->flexible_groups))
6678 mutex_unlock(&ctx->mutex);
6684 void perf_event_delayed_put(struct task_struct *task)
6688 for_each_task_context_nr(ctxn)
6689 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6693 * inherit a event from parent task to child task:
6695 static struct perf_event *
6696 inherit_event(struct perf_event *parent_event,
6697 struct task_struct *parent,
6698 struct perf_event_context *parent_ctx,
6699 struct task_struct *child,
6700 struct perf_event *group_leader,
6701 struct perf_event_context *child_ctx)
6703 struct perf_event *child_event;
6704 unsigned long flags;
6707 * Instead of creating recursive hierarchies of events,
6708 * we link inherited events back to the original parent,
6709 * which has a filp for sure, which we use as the reference
6712 if (parent_event->parent)
6713 parent_event = parent_event->parent;
6715 child_event = perf_event_alloc(&parent_event->attr,
6718 group_leader, parent_event,
6720 if (IS_ERR(child_event))
6723 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
6724 free_event(child_event);
6731 * Make the child state follow the state of the parent event,
6732 * not its attr.disabled bit. We hold the parent's mutex,
6733 * so we won't race with perf_event_{en, dis}able_family.
6735 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6736 child_event->state = PERF_EVENT_STATE_INACTIVE;
6738 child_event->state = PERF_EVENT_STATE_OFF;
6740 if (parent_event->attr.freq) {
6741 u64 sample_period = parent_event->hw.sample_period;
6742 struct hw_perf_event *hwc = &child_event->hw;
6744 hwc->sample_period = sample_period;
6745 hwc->last_period = sample_period;
6747 local64_set(&hwc->period_left, sample_period);
6750 child_event->ctx = child_ctx;
6751 child_event->overflow_handler = parent_event->overflow_handler;
6752 child_event->overflow_handler_context
6753 = parent_event->overflow_handler_context;
6756 * Precalculate sample_data sizes
6758 perf_event__header_size(child_event);
6759 perf_event__id_header_size(child_event);
6762 * Link it up in the child's context:
6764 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6765 add_event_to_ctx(child_event, child_ctx);
6766 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6769 * Link this into the parent event's child list
6771 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6772 mutex_lock(&parent_event->child_mutex);
6773 list_add_tail(&child_event->child_list, &parent_event->child_list);
6774 mutex_unlock(&parent_event->child_mutex);
6779 static int inherit_group(struct perf_event *parent_event,
6780 struct task_struct *parent,
6781 struct perf_event_context *parent_ctx,
6782 struct task_struct *child,
6783 struct perf_event_context *child_ctx)
6785 struct perf_event *leader;
6786 struct perf_event *sub;
6787 struct perf_event *child_ctr;
6789 leader = inherit_event(parent_event, parent, parent_ctx,
6790 child, NULL, child_ctx);
6792 return PTR_ERR(leader);
6793 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6794 child_ctr = inherit_event(sub, parent, parent_ctx,
6795 child, leader, child_ctx);
6796 if (IS_ERR(child_ctr))
6797 return PTR_ERR(child_ctr);
6803 inherit_task_group(struct perf_event *event, struct task_struct *parent,
6804 struct perf_event_context *parent_ctx,
6805 struct task_struct *child, int ctxn,
6809 struct perf_event_context *child_ctx;
6811 if (!event->attr.inherit) {
6816 child_ctx = child->perf_event_ctxp[ctxn];
6819 * This is executed from the parent task context, so
6820 * inherit events that have been marked for cloning.
6821 * First allocate and initialize a context for the
6825 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
6829 child->perf_event_ctxp[ctxn] = child_ctx;
6832 ret = inherit_group(event, parent, parent_ctx,
6842 * Initialize the perf_event context in task_struct
6844 int perf_event_init_context(struct task_struct *child, int ctxn)
6846 struct perf_event_context *child_ctx, *parent_ctx;
6847 struct perf_event_context *cloned_ctx;
6848 struct perf_event *event;
6849 struct task_struct *parent = current;
6850 int inherited_all = 1;
6851 unsigned long flags;
6854 if (likely(!parent->perf_event_ctxp[ctxn]))
6858 * If the parent's context is a clone, pin it so it won't get
6861 parent_ctx = perf_pin_task_context(parent, ctxn);
6864 * No need to check if parent_ctx != NULL here; since we saw
6865 * it non-NULL earlier, the only reason for it to become NULL
6866 * is if we exit, and since we're currently in the middle of
6867 * a fork we can't be exiting at the same time.
6871 * Lock the parent list. No need to lock the child - not PID
6872 * hashed yet and not running, so nobody can access it.
6874 mutex_lock(&parent_ctx->mutex);
6877 * We dont have to disable NMIs - we are only looking at
6878 * the list, not manipulating it:
6880 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
6881 ret = inherit_task_group(event, parent, parent_ctx,
6882 child, ctxn, &inherited_all);
6888 * We can't hold ctx->lock when iterating the ->flexible_group list due
6889 * to allocations, but we need to prevent rotation because
6890 * rotate_ctx() will change the list from interrupt context.
6892 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6893 parent_ctx->rotate_disable = 1;
6894 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6896 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
6897 ret = inherit_task_group(event, parent, parent_ctx,
6898 child, ctxn, &inherited_all);
6903 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6904 parent_ctx->rotate_disable = 0;
6906 child_ctx = child->perf_event_ctxp[ctxn];
6908 if (child_ctx && inherited_all) {
6910 * Mark the child context as a clone of the parent
6911 * context, or of whatever the parent is a clone of.
6913 * Note that if the parent is a clone, the holding of
6914 * parent_ctx->lock avoids it from being uncloned.
6916 cloned_ctx = parent_ctx->parent_ctx;
6918 child_ctx->parent_ctx = cloned_ctx;
6919 child_ctx->parent_gen = parent_ctx->parent_gen;
6921 child_ctx->parent_ctx = parent_ctx;
6922 child_ctx->parent_gen = parent_ctx->generation;
6924 get_ctx(child_ctx->parent_ctx);
6927 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6928 mutex_unlock(&parent_ctx->mutex);
6930 perf_unpin_context(parent_ctx);
6931 put_ctx(parent_ctx);
6937 * Initialize the perf_event context in task_struct
6939 int perf_event_init_task(struct task_struct *child)
6943 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
6944 mutex_init(&child->perf_event_mutex);
6945 INIT_LIST_HEAD(&child->perf_event_list);
6947 for_each_task_context_nr(ctxn) {
6948 ret = perf_event_init_context(child, ctxn);
6956 static void __init perf_event_init_all_cpus(void)
6958 struct swevent_htable *swhash;
6961 for_each_possible_cpu(cpu) {
6962 swhash = &per_cpu(swevent_htable, cpu);
6963 mutex_init(&swhash->hlist_mutex);
6964 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
6968 static void __cpuinit perf_event_init_cpu(int cpu)
6970 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6972 mutex_lock(&swhash->hlist_mutex);
6973 if (swhash->hlist_refcount > 0) {
6974 struct swevent_hlist *hlist;
6976 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6978 rcu_assign_pointer(swhash->swevent_hlist, hlist);
6980 mutex_unlock(&swhash->hlist_mutex);
6983 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
6984 static void perf_pmu_rotate_stop(struct pmu *pmu)
6986 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6988 WARN_ON(!irqs_disabled());
6990 list_del_init(&cpuctx->rotation_list);
6993 static void __perf_event_exit_context(void *__info)
6995 struct perf_event_context *ctx = __info;
6996 struct perf_event *event, *tmp;
6998 perf_pmu_rotate_stop(ctx->pmu);
7000 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
7001 __perf_remove_from_context(event);
7002 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
7003 __perf_remove_from_context(event);
7006 static void perf_event_exit_cpu_context(int cpu)
7008 struct perf_event_context *ctx;
7012 idx = srcu_read_lock(&pmus_srcu);
7013 list_for_each_entry_rcu(pmu, &pmus, entry) {
7014 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
7016 mutex_lock(&ctx->mutex);
7017 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7018 mutex_unlock(&ctx->mutex);
7020 srcu_read_unlock(&pmus_srcu, idx);
7023 static void perf_event_exit_cpu(int cpu)
7025 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7027 mutex_lock(&swhash->hlist_mutex);
7028 swevent_hlist_release(swhash);
7029 mutex_unlock(&swhash->hlist_mutex);
7031 perf_event_exit_cpu_context(cpu);
7034 static inline void perf_event_exit_cpu(int cpu) { }
7038 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7042 for_each_online_cpu(cpu)
7043 perf_event_exit_cpu(cpu);
7049 * Run the perf reboot notifier at the very last possible moment so that
7050 * the generic watchdog code runs as long as possible.
7052 static struct notifier_block perf_reboot_notifier = {
7053 .notifier_call = perf_reboot,
7054 .priority = INT_MIN,
7057 static int __cpuinit
7058 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7060 unsigned int cpu = (long)hcpu;
7062 switch (action & ~CPU_TASKS_FROZEN) {
7064 case CPU_UP_PREPARE:
7065 case CPU_DOWN_FAILED:
7066 perf_event_init_cpu(cpu);
7069 case CPU_UP_CANCELED:
7070 case CPU_DOWN_PREPARE:
7071 perf_event_exit_cpu(cpu);
7081 void __init perf_event_init(void)
7087 perf_event_init_all_cpus();
7088 init_srcu_struct(&pmus_srcu);
7089 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7090 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7091 perf_pmu_register(&perf_task_clock, NULL, -1);
7093 perf_cpu_notifier(perf_cpu_notify);
7094 register_reboot_notifier(&perf_reboot_notifier);
7096 ret = init_hw_breakpoint();
7097 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7100 static int __init perf_event_sysfs_init(void)
7105 mutex_lock(&pmus_lock);
7107 ret = bus_register(&pmu_bus);
7111 list_for_each_entry(pmu, &pmus, entry) {
7112 if (!pmu->name || pmu->type < 0)
7115 ret = pmu_dev_alloc(pmu);
7116 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7118 pmu_bus_running = 1;
7122 mutex_unlock(&pmus_lock);
7126 device_initcall(perf_event_sysfs_init);
7128 #ifdef CONFIG_CGROUP_PERF
7129 static struct cgroup_subsys_state *perf_cgroup_create(
7130 struct cgroup_subsys *ss, struct cgroup *cont)
7132 struct perf_cgroup *jc;
7134 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
7136 return ERR_PTR(-ENOMEM);
7138 jc->info = alloc_percpu(struct perf_cgroup_info);
7141 return ERR_PTR(-ENOMEM);
7147 static void perf_cgroup_destroy(struct cgroup_subsys *ss,
7148 struct cgroup *cont)
7150 struct perf_cgroup *jc;
7151 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7152 struct perf_cgroup, css);
7153 free_percpu(jc->info);
7157 static int __perf_cgroup_move(void *info)
7159 struct task_struct *task = info;
7160 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7165 perf_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *task)
7167 task_function_call(task, __perf_cgroup_move, task);
7170 static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7171 struct cgroup *old_cgrp, struct task_struct *task)
7174 * cgroup_exit() is called in the copy_process() failure path.
7175 * Ignore this case since the task hasn't ran yet, this avoids
7176 * trying to poke a half freed task state from generic code.
7178 if (!(task->flags & PF_EXITING))
7181 perf_cgroup_attach_task(cgrp, task);
7184 struct cgroup_subsys perf_subsys = {
7185 .name = "perf_event",
7186 .subsys_id = perf_subsys_id,
7187 .create = perf_cgroup_create,
7188 .destroy = perf_cgroup_destroy,
7189 .exit = perf_cgroup_exit,
7190 .attach_task = perf_cgroup_attach_task,
7192 #endif /* CONFIG_CGROUP_PERF */