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>
39 #include <linux/mm_types.h>
40 #include <linux/cgroup.h>
44 #include <asm/irq_regs.h>
46 struct remote_function_call {
47 struct task_struct *p;
48 int (*func)(void *info);
53 static void remote_function(void *data)
55 struct remote_function_call *tfc = data;
56 struct task_struct *p = tfc->p;
60 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
64 tfc->ret = tfc->func(tfc->info);
68 * task_function_call - call a function on the cpu on which a task runs
69 * @p: the task to evaluate
70 * @func: the function to be called
71 * @info: the function call argument
73 * Calls the function @func when the task is currently running. This might
74 * be on the current CPU, which just calls the function directly
76 * returns: @func return value, or
77 * -ESRCH - when the process isn't running
78 * -EAGAIN - when the process moved away
81 task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
83 struct remote_function_call data = {
87 .ret = -ESRCH, /* No such (running) process */
91 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
97 * cpu_function_call - call a function on the cpu
98 * @func: the function to be called
99 * @info: the function call argument
101 * Calls the function @func on the remote cpu.
103 * returns: @func return value or -ENXIO when the cpu is offline
105 static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
107 struct remote_function_call data = {
111 .ret = -ENXIO, /* No such CPU */
114 smp_call_function_single(cpu, remote_function, &data, 1);
119 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
120 PERF_FLAG_FD_OUTPUT |\
121 PERF_FLAG_PID_CGROUP)
124 * branch priv levels that need permission checks
126 #define PERF_SAMPLE_BRANCH_PERM_PLM \
127 (PERF_SAMPLE_BRANCH_KERNEL |\
128 PERF_SAMPLE_BRANCH_HV)
131 EVENT_FLEXIBLE = 0x1,
133 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
137 * perf_sched_events : >0 events exist
138 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
140 struct static_key_deferred perf_sched_events __read_mostly;
141 static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
142 static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
144 static atomic_t nr_mmap_events __read_mostly;
145 static atomic_t nr_comm_events __read_mostly;
146 static atomic_t nr_task_events __read_mostly;
148 static LIST_HEAD(pmus);
149 static DEFINE_MUTEX(pmus_lock);
150 static struct srcu_struct pmus_srcu;
153 * perf event paranoia level:
154 * -1 - not paranoid at all
155 * 0 - disallow raw tracepoint access for unpriv
156 * 1 - disallow cpu events for unpriv
157 * 2 - disallow kernel profiling for unpriv
159 int sysctl_perf_event_paranoid __read_mostly = 1;
161 /* Minimum for 512 kiB + 1 user control page */
162 int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
165 * max perf event sample rate
167 #define DEFAULT_MAX_SAMPLE_RATE 100000
168 int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
169 static int max_samples_per_tick __read_mostly =
170 DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
172 int perf_proc_update_handler(struct ctl_table *table, int write,
173 void __user *buffer, size_t *lenp,
176 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
181 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
186 static atomic64_t perf_event_id;
188 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
189 enum event_type_t event_type);
191 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
192 enum event_type_t event_type,
193 struct task_struct *task);
195 static void update_context_time(struct perf_event_context *ctx);
196 static u64 perf_event_time(struct perf_event *event);
198 static void ring_buffer_attach(struct perf_event *event,
199 struct ring_buffer *rb);
201 void __weak perf_event_print_debug(void) { }
203 extern __weak const char *perf_pmu_name(void)
208 static inline u64 perf_clock(void)
210 return local_clock();
213 static inline struct perf_cpu_context *
214 __get_cpu_context(struct perf_event_context *ctx)
216 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
219 static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
220 struct perf_event_context *ctx)
222 raw_spin_lock(&cpuctx->ctx.lock);
224 raw_spin_lock(&ctx->lock);
227 static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
228 struct perf_event_context *ctx)
231 raw_spin_unlock(&ctx->lock);
232 raw_spin_unlock(&cpuctx->ctx.lock);
235 #ifdef CONFIG_CGROUP_PERF
238 * perf_cgroup_info keeps track of time_enabled for a cgroup.
239 * This is a per-cpu dynamically allocated data structure.
241 struct perf_cgroup_info {
247 struct cgroup_subsys_state css;
248 struct perf_cgroup_info __percpu *info;
252 * Must ensure cgroup is pinned (css_get) before calling
253 * this function. In other words, we cannot call this function
254 * if there is no cgroup event for the current CPU context.
256 static inline struct perf_cgroup *
257 perf_cgroup_from_task(struct task_struct *task)
259 return container_of(task_subsys_state(task, perf_subsys_id),
260 struct perf_cgroup, css);
264 perf_cgroup_match(struct perf_event *event)
266 struct perf_event_context *ctx = event->ctx;
267 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
269 /* @event doesn't care about cgroup */
273 /* wants specific cgroup scope but @cpuctx isn't associated with any */
278 * Cgroup scoping is recursive. An event enabled for a cgroup is
279 * also enabled for all its descendant cgroups. If @cpuctx's
280 * cgroup is a descendant of @event's (the test covers identity
281 * case), it's a match.
283 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
284 event->cgrp->css.cgroup);
287 static inline bool perf_tryget_cgroup(struct perf_event *event)
289 return css_tryget(&event->cgrp->css);
292 static inline void perf_put_cgroup(struct perf_event *event)
294 css_put(&event->cgrp->css);
297 static inline void perf_detach_cgroup(struct perf_event *event)
299 perf_put_cgroup(event);
303 static inline int is_cgroup_event(struct perf_event *event)
305 return event->cgrp != NULL;
308 static inline u64 perf_cgroup_event_time(struct perf_event *event)
310 struct perf_cgroup_info *t;
312 t = per_cpu_ptr(event->cgrp->info, event->cpu);
316 static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
318 struct perf_cgroup_info *info;
323 info = this_cpu_ptr(cgrp->info);
325 info->time += now - info->timestamp;
326 info->timestamp = now;
329 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
331 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
333 __update_cgrp_time(cgrp_out);
336 static inline void update_cgrp_time_from_event(struct perf_event *event)
338 struct perf_cgroup *cgrp;
341 * ensure we access cgroup data only when needed and
342 * when we know the cgroup is pinned (css_get)
344 if (!is_cgroup_event(event))
347 cgrp = perf_cgroup_from_task(current);
349 * Do not update time when cgroup is not active
351 if (cgrp == event->cgrp)
352 __update_cgrp_time(event->cgrp);
356 perf_cgroup_set_timestamp(struct task_struct *task,
357 struct perf_event_context *ctx)
359 struct perf_cgroup *cgrp;
360 struct perf_cgroup_info *info;
363 * ctx->lock held by caller
364 * ensure we do not access cgroup data
365 * unless we have the cgroup pinned (css_get)
367 if (!task || !ctx->nr_cgroups)
370 cgrp = perf_cgroup_from_task(task);
371 info = this_cpu_ptr(cgrp->info);
372 info->timestamp = ctx->timestamp;
375 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
376 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
379 * reschedule events based on the cgroup constraint of task.
381 * mode SWOUT : schedule out everything
382 * mode SWIN : schedule in based on cgroup for next
384 void perf_cgroup_switch(struct task_struct *task, int mode)
386 struct perf_cpu_context *cpuctx;
391 * disable interrupts to avoid geting nr_cgroup
392 * changes via __perf_event_disable(). Also
395 local_irq_save(flags);
398 * we reschedule only in the presence of cgroup
399 * constrained events.
403 list_for_each_entry_rcu(pmu, &pmus, entry) {
404 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
405 if (cpuctx->unique_pmu != pmu)
406 continue; /* ensure we process each cpuctx once */
409 * perf_cgroup_events says at least one
410 * context on this CPU has cgroup events.
412 * ctx->nr_cgroups reports the number of cgroup
413 * events for a context.
415 if (cpuctx->ctx.nr_cgroups > 0) {
416 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
417 perf_pmu_disable(cpuctx->ctx.pmu);
419 if (mode & PERF_CGROUP_SWOUT) {
420 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
422 * must not be done before ctxswout due
423 * to event_filter_match() in event_sched_out()
428 if (mode & PERF_CGROUP_SWIN) {
429 WARN_ON_ONCE(cpuctx->cgrp);
431 * set cgrp before ctxsw in to allow
432 * event_filter_match() to not have to pass
435 cpuctx->cgrp = perf_cgroup_from_task(task);
436 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
438 perf_pmu_enable(cpuctx->ctx.pmu);
439 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
445 local_irq_restore(flags);
448 static inline void perf_cgroup_sched_out(struct task_struct *task,
449 struct task_struct *next)
451 struct perf_cgroup *cgrp1;
452 struct perf_cgroup *cgrp2 = NULL;
455 * we come here when we know perf_cgroup_events > 0
457 cgrp1 = perf_cgroup_from_task(task);
460 * next is NULL when called from perf_event_enable_on_exec()
461 * that will systematically cause a cgroup_switch()
464 cgrp2 = perf_cgroup_from_task(next);
467 * only schedule out current cgroup events if we know
468 * that we are switching to a different cgroup. Otherwise,
469 * do no touch the cgroup events.
472 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
475 static inline void perf_cgroup_sched_in(struct task_struct *prev,
476 struct task_struct *task)
478 struct perf_cgroup *cgrp1;
479 struct perf_cgroup *cgrp2 = NULL;
482 * we come here when we know perf_cgroup_events > 0
484 cgrp1 = perf_cgroup_from_task(task);
486 /* prev can never be NULL */
487 cgrp2 = perf_cgroup_from_task(prev);
490 * only need to schedule in cgroup events if we are changing
491 * cgroup during ctxsw. Cgroup events were not scheduled
492 * out of ctxsw out if that was not the case.
495 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
498 static inline int perf_cgroup_connect(int fd, struct perf_event *event,
499 struct perf_event_attr *attr,
500 struct perf_event *group_leader)
502 struct perf_cgroup *cgrp;
503 struct cgroup_subsys_state *css;
504 struct fd f = fdget(fd);
510 css = cgroup_css_from_dir(f.file, perf_subsys_id);
516 cgrp = container_of(css, struct perf_cgroup, css);
519 /* must be done before we fput() the file */
520 if (!perf_tryget_cgroup(event)) {
527 * all events in a group must monitor
528 * the same cgroup because a task belongs
529 * to only one perf cgroup at a time
531 if (group_leader && group_leader->cgrp != cgrp) {
532 perf_detach_cgroup(event);
541 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
543 struct perf_cgroup_info *t;
544 t = per_cpu_ptr(event->cgrp->info, event->cpu);
545 event->shadow_ctx_time = now - t->timestamp;
549 perf_cgroup_defer_enabled(struct perf_event *event)
552 * when the current task's perf cgroup does not match
553 * the event's, we need to remember to call the
554 * perf_mark_enable() function the first time a task with
555 * a matching perf cgroup is scheduled in.
557 if (is_cgroup_event(event) && !perf_cgroup_match(event))
558 event->cgrp_defer_enabled = 1;
562 perf_cgroup_mark_enabled(struct perf_event *event,
563 struct perf_event_context *ctx)
565 struct perf_event *sub;
566 u64 tstamp = perf_event_time(event);
568 if (!event->cgrp_defer_enabled)
571 event->cgrp_defer_enabled = 0;
573 event->tstamp_enabled = tstamp - event->total_time_enabled;
574 list_for_each_entry(sub, &event->sibling_list, group_entry) {
575 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
576 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
577 sub->cgrp_defer_enabled = 0;
581 #else /* !CONFIG_CGROUP_PERF */
584 perf_cgroup_match(struct perf_event *event)
589 static inline void perf_detach_cgroup(struct perf_event *event)
592 static inline int is_cgroup_event(struct perf_event *event)
597 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
602 static inline void update_cgrp_time_from_event(struct perf_event *event)
606 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
610 static inline void perf_cgroup_sched_out(struct task_struct *task,
611 struct task_struct *next)
615 static inline void perf_cgroup_sched_in(struct task_struct *prev,
616 struct task_struct *task)
620 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
621 struct perf_event_attr *attr,
622 struct perf_event *group_leader)
628 perf_cgroup_set_timestamp(struct task_struct *task,
629 struct perf_event_context *ctx)
634 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
639 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
643 static inline u64 perf_cgroup_event_time(struct perf_event *event)
649 perf_cgroup_defer_enabled(struct perf_event *event)
654 perf_cgroup_mark_enabled(struct perf_event *event,
655 struct perf_event_context *ctx)
660 void perf_pmu_disable(struct pmu *pmu)
662 int *count = this_cpu_ptr(pmu->pmu_disable_count);
664 pmu->pmu_disable(pmu);
667 void perf_pmu_enable(struct pmu *pmu)
669 int *count = this_cpu_ptr(pmu->pmu_disable_count);
671 pmu->pmu_enable(pmu);
674 static DEFINE_PER_CPU(struct list_head, rotation_list);
677 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
678 * because they're strictly cpu affine and rotate_start is called with IRQs
679 * disabled, while rotate_context is called from IRQ context.
681 static void perf_pmu_rotate_start(struct pmu *pmu)
683 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
684 struct list_head *head = &__get_cpu_var(rotation_list);
686 WARN_ON(!irqs_disabled());
688 if (list_empty(&cpuctx->rotation_list))
689 list_add(&cpuctx->rotation_list, head);
692 static void get_ctx(struct perf_event_context *ctx)
694 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
697 static void put_ctx(struct perf_event_context *ctx)
699 if (atomic_dec_and_test(&ctx->refcount)) {
701 put_ctx(ctx->parent_ctx);
703 put_task_struct(ctx->task);
704 kfree_rcu(ctx, rcu_head);
708 static void unclone_ctx(struct perf_event_context *ctx)
710 if (ctx->parent_ctx) {
711 put_ctx(ctx->parent_ctx);
712 ctx->parent_ctx = NULL;
716 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
719 * only top level events have the pid namespace they were created in
722 event = event->parent;
724 return task_tgid_nr_ns(p, event->ns);
727 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
730 * only top level events have the pid namespace they were created in
733 event = event->parent;
735 return task_pid_nr_ns(p, event->ns);
739 * If we inherit events we want to return the parent event id
742 static u64 primary_event_id(struct perf_event *event)
747 id = event->parent->id;
753 * Get the perf_event_context for a task and lock it.
754 * This has to cope with with the fact that until it is locked,
755 * the context could get moved to another task.
757 static struct perf_event_context *
758 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
760 struct perf_event_context *ctx;
764 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
767 * If this context is a clone of another, it might
768 * get swapped for another underneath us by
769 * perf_event_task_sched_out, though the
770 * rcu_read_lock() protects us from any context
771 * getting freed. Lock the context and check if it
772 * got swapped before we could get the lock, and retry
773 * if so. If we locked the right context, then it
774 * can't get swapped on us any more.
776 raw_spin_lock_irqsave(&ctx->lock, *flags);
777 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
778 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
782 if (!atomic_inc_not_zero(&ctx->refcount)) {
783 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
792 * Get the context for a task and increment its pin_count so it
793 * can't get swapped to another task. This also increments its
794 * reference count so that the context can't get freed.
796 static struct perf_event_context *
797 perf_pin_task_context(struct task_struct *task, int ctxn)
799 struct perf_event_context *ctx;
802 ctx = perf_lock_task_context(task, ctxn, &flags);
805 raw_spin_unlock_irqrestore(&ctx->lock, flags);
810 static void perf_unpin_context(struct perf_event_context *ctx)
814 raw_spin_lock_irqsave(&ctx->lock, flags);
816 raw_spin_unlock_irqrestore(&ctx->lock, flags);
820 * Update the record of the current time in a context.
822 static void update_context_time(struct perf_event_context *ctx)
824 u64 now = perf_clock();
826 ctx->time += now - ctx->timestamp;
827 ctx->timestamp = now;
830 static u64 perf_event_time(struct perf_event *event)
832 struct perf_event_context *ctx = event->ctx;
834 if (is_cgroup_event(event))
835 return perf_cgroup_event_time(event);
837 return ctx ? ctx->time : 0;
841 * Update the total_time_enabled and total_time_running fields for a event.
842 * The caller of this function needs to hold the ctx->lock.
844 static void update_event_times(struct perf_event *event)
846 struct perf_event_context *ctx = event->ctx;
849 if (event->state < PERF_EVENT_STATE_INACTIVE ||
850 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
853 * in cgroup mode, time_enabled represents
854 * the time the event was enabled AND active
855 * tasks were in the monitored cgroup. This is
856 * independent of the activity of the context as
857 * there may be a mix of cgroup and non-cgroup events.
859 * That is why we treat cgroup events differently
862 if (is_cgroup_event(event))
863 run_end = perf_cgroup_event_time(event);
864 else if (ctx->is_active)
867 run_end = event->tstamp_stopped;
869 event->total_time_enabled = run_end - event->tstamp_enabled;
871 if (event->state == PERF_EVENT_STATE_INACTIVE)
872 run_end = event->tstamp_stopped;
874 run_end = perf_event_time(event);
876 event->total_time_running = run_end - event->tstamp_running;
881 * Update total_time_enabled and total_time_running for all events in a group.
883 static void update_group_times(struct perf_event *leader)
885 struct perf_event *event;
887 update_event_times(leader);
888 list_for_each_entry(event, &leader->sibling_list, group_entry)
889 update_event_times(event);
892 static struct list_head *
893 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
895 if (event->attr.pinned)
896 return &ctx->pinned_groups;
898 return &ctx->flexible_groups;
902 * Add a event from the lists for its context.
903 * Must be called with ctx->mutex and ctx->lock held.
906 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
908 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
909 event->attach_state |= PERF_ATTACH_CONTEXT;
912 * If we're a stand alone event or group leader, we go to the context
913 * list, group events are kept attached to the group so that
914 * perf_group_detach can, at all times, locate all siblings.
916 if (event->group_leader == event) {
917 struct list_head *list;
919 if (is_software_event(event))
920 event->group_flags |= PERF_GROUP_SOFTWARE;
922 list = ctx_group_list(event, ctx);
923 list_add_tail(&event->group_entry, list);
926 if (is_cgroup_event(event))
929 if (has_branch_stack(event))
930 ctx->nr_branch_stack++;
932 list_add_rcu(&event->event_entry, &ctx->event_list);
934 perf_pmu_rotate_start(ctx->pmu);
936 if (event->attr.inherit_stat)
941 * Initialize event state based on the perf_event_attr::disabled.
943 static inline void perf_event__state_init(struct perf_event *event)
945 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
946 PERF_EVENT_STATE_INACTIVE;
950 * Called at perf_event creation and when events are attached/detached from a
953 static void perf_event__read_size(struct perf_event *event)
955 int entry = sizeof(u64); /* value */
959 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
962 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
965 if (event->attr.read_format & PERF_FORMAT_ID)
966 entry += sizeof(u64);
968 if (event->attr.read_format & PERF_FORMAT_GROUP) {
969 nr += event->group_leader->nr_siblings;
974 event->read_size = size;
977 static void perf_event__header_size(struct perf_event *event)
979 struct perf_sample_data *data;
980 u64 sample_type = event->attr.sample_type;
983 perf_event__read_size(event);
985 if (sample_type & PERF_SAMPLE_IP)
986 size += sizeof(data->ip);
988 if (sample_type & PERF_SAMPLE_ADDR)
989 size += sizeof(data->addr);
991 if (sample_type & PERF_SAMPLE_PERIOD)
992 size += sizeof(data->period);
994 if (sample_type & PERF_SAMPLE_WEIGHT)
995 size += sizeof(data->weight);
997 if (sample_type & PERF_SAMPLE_READ)
998 size += event->read_size;
1000 if (sample_type & PERF_SAMPLE_DATA_SRC)
1001 size += sizeof(data->data_src.val);
1003 event->header_size = size;
1006 static void perf_event__id_header_size(struct perf_event *event)
1008 struct perf_sample_data *data;
1009 u64 sample_type = event->attr.sample_type;
1012 if (sample_type & PERF_SAMPLE_TID)
1013 size += sizeof(data->tid_entry);
1015 if (sample_type & PERF_SAMPLE_TIME)
1016 size += sizeof(data->time);
1018 if (sample_type & PERF_SAMPLE_ID)
1019 size += sizeof(data->id);
1021 if (sample_type & PERF_SAMPLE_STREAM_ID)
1022 size += sizeof(data->stream_id);
1024 if (sample_type & PERF_SAMPLE_CPU)
1025 size += sizeof(data->cpu_entry);
1027 event->id_header_size = size;
1030 static void perf_group_attach(struct perf_event *event)
1032 struct perf_event *group_leader = event->group_leader, *pos;
1035 * We can have double attach due to group movement in perf_event_open.
1037 if (event->attach_state & PERF_ATTACH_GROUP)
1040 event->attach_state |= PERF_ATTACH_GROUP;
1042 if (group_leader == event)
1045 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1046 !is_software_event(event))
1047 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1049 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1050 group_leader->nr_siblings++;
1052 perf_event__header_size(group_leader);
1054 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1055 perf_event__header_size(pos);
1059 * Remove a event from the lists for its context.
1060 * Must be called with ctx->mutex and ctx->lock held.
1063 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1065 struct perf_cpu_context *cpuctx;
1067 * We can have double detach due to exit/hot-unplug + close.
1069 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
1072 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1074 if (is_cgroup_event(event)) {
1076 cpuctx = __get_cpu_context(ctx);
1078 * if there are no more cgroup events
1079 * then cler cgrp to avoid stale pointer
1080 * in update_cgrp_time_from_cpuctx()
1082 if (!ctx->nr_cgroups)
1083 cpuctx->cgrp = NULL;
1086 if (has_branch_stack(event))
1087 ctx->nr_branch_stack--;
1090 if (event->attr.inherit_stat)
1093 list_del_rcu(&event->event_entry);
1095 if (event->group_leader == event)
1096 list_del_init(&event->group_entry);
1098 update_group_times(event);
1101 * If event was in error state, then keep it
1102 * that way, otherwise bogus counts will be
1103 * returned on read(). The only way to get out
1104 * of error state is by explicit re-enabling
1107 if (event->state > PERF_EVENT_STATE_OFF)
1108 event->state = PERF_EVENT_STATE_OFF;
1111 static void perf_group_detach(struct perf_event *event)
1113 struct perf_event *sibling, *tmp;
1114 struct list_head *list = NULL;
1117 * We can have double detach due to exit/hot-unplug + close.
1119 if (!(event->attach_state & PERF_ATTACH_GROUP))
1122 event->attach_state &= ~PERF_ATTACH_GROUP;
1125 * If this is a sibling, remove it from its group.
1127 if (event->group_leader != event) {
1128 list_del_init(&event->group_entry);
1129 event->group_leader->nr_siblings--;
1133 if (!list_empty(&event->group_entry))
1134 list = &event->group_entry;
1137 * If this was a group event with sibling events then
1138 * upgrade the siblings to singleton events by adding them
1139 * to whatever list we are on.
1141 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
1143 list_move_tail(&sibling->group_entry, list);
1144 sibling->group_leader = sibling;
1146 /* Inherit group flags from the previous leader */
1147 sibling->group_flags = event->group_flags;
1151 perf_event__header_size(event->group_leader);
1153 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1154 perf_event__header_size(tmp);
1158 event_filter_match(struct perf_event *event)
1160 return (event->cpu == -1 || event->cpu == smp_processor_id())
1161 && perf_cgroup_match(event);
1165 event_sched_out(struct perf_event *event,
1166 struct perf_cpu_context *cpuctx,
1167 struct perf_event_context *ctx)
1169 u64 tstamp = perf_event_time(event);
1172 * An event which could not be activated because of
1173 * filter mismatch still needs to have its timings
1174 * maintained, otherwise bogus information is return
1175 * via read() for time_enabled, time_running:
1177 if (event->state == PERF_EVENT_STATE_INACTIVE
1178 && !event_filter_match(event)) {
1179 delta = tstamp - event->tstamp_stopped;
1180 event->tstamp_running += delta;
1181 event->tstamp_stopped = tstamp;
1184 if (event->state != PERF_EVENT_STATE_ACTIVE)
1187 event->state = PERF_EVENT_STATE_INACTIVE;
1188 if (event->pending_disable) {
1189 event->pending_disable = 0;
1190 event->state = PERF_EVENT_STATE_OFF;
1192 event->tstamp_stopped = tstamp;
1193 event->pmu->del(event, 0);
1196 if (!is_software_event(event))
1197 cpuctx->active_oncpu--;
1199 if (event->attr.freq && event->attr.sample_freq)
1201 if (event->attr.exclusive || !cpuctx->active_oncpu)
1202 cpuctx->exclusive = 0;
1206 group_sched_out(struct perf_event *group_event,
1207 struct perf_cpu_context *cpuctx,
1208 struct perf_event_context *ctx)
1210 struct perf_event *event;
1211 int state = group_event->state;
1213 event_sched_out(group_event, cpuctx, ctx);
1216 * Schedule out siblings (if any):
1218 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1219 event_sched_out(event, cpuctx, ctx);
1221 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
1222 cpuctx->exclusive = 0;
1226 * Cross CPU call to remove a performance event
1228 * We disable the event on the hardware level first. After that we
1229 * remove it from the context list.
1231 static int __perf_remove_from_context(void *info)
1233 struct perf_event *event = info;
1234 struct perf_event_context *ctx = event->ctx;
1235 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1237 raw_spin_lock(&ctx->lock);
1238 event_sched_out(event, cpuctx, ctx);
1239 list_del_event(event, ctx);
1240 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1242 cpuctx->task_ctx = NULL;
1244 raw_spin_unlock(&ctx->lock);
1251 * Remove the event from a task's (or a CPU's) list of events.
1253 * CPU events are removed with a smp call. For task events we only
1254 * call when the task is on a CPU.
1256 * If event->ctx is a cloned context, callers must make sure that
1257 * every task struct that event->ctx->task could possibly point to
1258 * remains valid. This is OK when called from perf_release since
1259 * that only calls us on the top-level context, which can't be a clone.
1260 * When called from perf_event_exit_task, it's OK because the
1261 * context has been detached from its task.
1263 static void perf_remove_from_context(struct perf_event *event)
1265 struct perf_event_context *ctx = event->ctx;
1266 struct task_struct *task = ctx->task;
1268 lockdep_assert_held(&ctx->mutex);
1272 * Per cpu events are removed via an smp call and
1273 * the removal is always successful.
1275 cpu_function_call(event->cpu, __perf_remove_from_context, event);
1280 if (!task_function_call(task, __perf_remove_from_context, event))
1283 raw_spin_lock_irq(&ctx->lock);
1285 * If we failed to find a running task, but find the context active now
1286 * that we've acquired the ctx->lock, retry.
1288 if (ctx->is_active) {
1289 raw_spin_unlock_irq(&ctx->lock);
1294 * Since the task isn't running, its safe to remove the event, us
1295 * holding the ctx->lock ensures the task won't get scheduled in.
1297 list_del_event(event, ctx);
1298 raw_spin_unlock_irq(&ctx->lock);
1302 * Cross CPU call to disable a performance event
1304 int __perf_event_disable(void *info)
1306 struct perf_event *event = info;
1307 struct perf_event_context *ctx = event->ctx;
1308 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1311 * If this is a per-task event, need to check whether this
1312 * event's task is the current task on this cpu.
1314 * Can trigger due to concurrent perf_event_context_sched_out()
1315 * flipping contexts around.
1317 if (ctx->task && cpuctx->task_ctx != ctx)
1320 raw_spin_lock(&ctx->lock);
1323 * If the event is on, turn it off.
1324 * If it is in error state, leave it in error state.
1326 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1327 update_context_time(ctx);
1328 update_cgrp_time_from_event(event);
1329 update_group_times(event);
1330 if (event == event->group_leader)
1331 group_sched_out(event, cpuctx, ctx);
1333 event_sched_out(event, cpuctx, ctx);
1334 event->state = PERF_EVENT_STATE_OFF;
1337 raw_spin_unlock(&ctx->lock);
1345 * If event->ctx is a cloned context, callers must make sure that
1346 * every task struct that event->ctx->task could possibly point to
1347 * remains valid. This condition is satisifed when called through
1348 * perf_event_for_each_child or perf_event_for_each because they
1349 * hold the top-level event's child_mutex, so any descendant that
1350 * goes to exit will block in sync_child_event.
1351 * When called from perf_pending_event it's OK because event->ctx
1352 * is the current context on this CPU and preemption is disabled,
1353 * hence we can't get into perf_event_task_sched_out for this context.
1355 void perf_event_disable(struct perf_event *event)
1357 struct perf_event_context *ctx = event->ctx;
1358 struct task_struct *task = ctx->task;
1362 * Disable the event on the cpu that it's on
1364 cpu_function_call(event->cpu, __perf_event_disable, event);
1369 if (!task_function_call(task, __perf_event_disable, event))
1372 raw_spin_lock_irq(&ctx->lock);
1374 * If the event is still active, we need to retry the cross-call.
1376 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1377 raw_spin_unlock_irq(&ctx->lock);
1379 * Reload the task pointer, it might have been changed by
1380 * a concurrent perf_event_context_sched_out().
1387 * Since we have the lock this context can't be scheduled
1388 * in, so we can change the state safely.
1390 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1391 update_group_times(event);
1392 event->state = PERF_EVENT_STATE_OFF;
1394 raw_spin_unlock_irq(&ctx->lock);
1396 EXPORT_SYMBOL_GPL(perf_event_disable);
1398 static void perf_set_shadow_time(struct perf_event *event,
1399 struct perf_event_context *ctx,
1403 * use the correct time source for the time snapshot
1405 * We could get by without this by leveraging the
1406 * fact that to get to this function, the caller
1407 * has most likely already called update_context_time()
1408 * and update_cgrp_time_xx() and thus both timestamp
1409 * are identical (or very close). Given that tstamp is,
1410 * already adjusted for cgroup, we could say that:
1411 * tstamp - ctx->timestamp
1413 * tstamp - cgrp->timestamp.
1415 * Then, in perf_output_read(), the calculation would
1416 * work with no changes because:
1417 * - event is guaranteed scheduled in
1418 * - no scheduled out in between
1419 * - thus the timestamp would be the same
1421 * But this is a bit hairy.
1423 * So instead, we have an explicit cgroup call to remain
1424 * within the time time source all along. We believe it
1425 * is cleaner and simpler to understand.
1427 if (is_cgroup_event(event))
1428 perf_cgroup_set_shadow_time(event, tstamp);
1430 event->shadow_ctx_time = tstamp - ctx->timestamp;
1433 #define MAX_INTERRUPTS (~0ULL)
1435 static void perf_log_throttle(struct perf_event *event, int enable);
1438 event_sched_in(struct perf_event *event,
1439 struct perf_cpu_context *cpuctx,
1440 struct perf_event_context *ctx)
1442 u64 tstamp = perf_event_time(event);
1444 if (event->state <= PERF_EVENT_STATE_OFF)
1447 event->state = PERF_EVENT_STATE_ACTIVE;
1448 event->oncpu = smp_processor_id();
1451 * Unthrottle events, since we scheduled we might have missed several
1452 * ticks already, also for a heavily scheduling task there is little
1453 * guarantee it'll get a tick in a timely manner.
1455 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1456 perf_log_throttle(event, 1);
1457 event->hw.interrupts = 0;
1461 * The new state must be visible before we turn it on in the hardware:
1465 if (event->pmu->add(event, PERF_EF_START)) {
1466 event->state = PERF_EVENT_STATE_INACTIVE;
1471 event->tstamp_running += tstamp - event->tstamp_stopped;
1473 perf_set_shadow_time(event, ctx, tstamp);
1475 if (!is_software_event(event))
1476 cpuctx->active_oncpu++;
1478 if (event->attr.freq && event->attr.sample_freq)
1481 if (event->attr.exclusive)
1482 cpuctx->exclusive = 1;
1488 group_sched_in(struct perf_event *group_event,
1489 struct perf_cpu_context *cpuctx,
1490 struct perf_event_context *ctx)
1492 struct perf_event *event, *partial_group = NULL;
1493 struct pmu *pmu = group_event->pmu;
1494 u64 now = ctx->time;
1495 bool simulate = false;
1497 if (group_event->state == PERF_EVENT_STATE_OFF)
1500 pmu->start_txn(pmu);
1502 if (event_sched_in(group_event, cpuctx, ctx)) {
1503 pmu->cancel_txn(pmu);
1508 * Schedule in siblings as one group (if any):
1510 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1511 if (event_sched_in(event, cpuctx, ctx)) {
1512 partial_group = event;
1517 if (!pmu->commit_txn(pmu))
1522 * Groups can be scheduled in as one unit only, so undo any
1523 * partial group before returning:
1524 * The events up to the failed event are scheduled out normally,
1525 * tstamp_stopped will be updated.
1527 * The failed events and the remaining siblings need to have
1528 * their timings updated as if they had gone thru event_sched_in()
1529 * and event_sched_out(). This is required to get consistent timings
1530 * across the group. This also takes care of the case where the group
1531 * could never be scheduled by ensuring tstamp_stopped is set to mark
1532 * the time the event was actually stopped, such that time delta
1533 * calculation in update_event_times() is correct.
1535 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1536 if (event == partial_group)
1540 event->tstamp_running += now - event->tstamp_stopped;
1541 event->tstamp_stopped = now;
1543 event_sched_out(event, cpuctx, ctx);
1546 event_sched_out(group_event, cpuctx, ctx);
1548 pmu->cancel_txn(pmu);
1554 * Work out whether we can put this event group on the CPU now.
1556 static int group_can_go_on(struct perf_event *event,
1557 struct perf_cpu_context *cpuctx,
1561 * Groups consisting entirely of software events can always go on.
1563 if (event->group_flags & PERF_GROUP_SOFTWARE)
1566 * If an exclusive group is already on, no other hardware
1569 if (cpuctx->exclusive)
1572 * If this group is exclusive and there are already
1573 * events on the CPU, it can't go on.
1575 if (event->attr.exclusive && cpuctx->active_oncpu)
1578 * Otherwise, try to add it if all previous groups were able
1584 static void add_event_to_ctx(struct perf_event *event,
1585 struct perf_event_context *ctx)
1587 u64 tstamp = perf_event_time(event);
1589 list_add_event(event, ctx);
1590 perf_group_attach(event);
1591 event->tstamp_enabled = tstamp;
1592 event->tstamp_running = tstamp;
1593 event->tstamp_stopped = tstamp;
1596 static void task_ctx_sched_out(struct perf_event_context *ctx);
1598 ctx_sched_in(struct perf_event_context *ctx,
1599 struct perf_cpu_context *cpuctx,
1600 enum event_type_t event_type,
1601 struct task_struct *task);
1603 static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1604 struct perf_event_context *ctx,
1605 struct task_struct *task)
1607 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1609 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1610 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1612 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1616 * Cross CPU call to install and enable a performance event
1618 * Must be called with ctx->mutex held
1620 static int __perf_install_in_context(void *info)
1622 struct perf_event *event = info;
1623 struct perf_event_context *ctx = event->ctx;
1624 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1625 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1626 struct task_struct *task = current;
1628 perf_ctx_lock(cpuctx, task_ctx);
1629 perf_pmu_disable(cpuctx->ctx.pmu);
1632 * If there was an active task_ctx schedule it out.
1635 task_ctx_sched_out(task_ctx);
1638 * If the context we're installing events in is not the
1639 * active task_ctx, flip them.
1641 if (ctx->task && task_ctx != ctx) {
1643 raw_spin_unlock(&task_ctx->lock);
1644 raw_spin_lock(&ctx->lock);
1649 cpuctx->task_ctx = task_ctx;
1650 task = task_ctx->task;
1653 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
1655 update_context_time(ctx);
1657 * update cgrp time only if current cgrp
1658 * matches event->cgrp. Must be done before
1659 * calling add_event_to_ctx()
1661 update_cgrp_time_from_event(event);
1663 add_event_to_ctx(event, ctx);
1666 * Schedule everything back in
1668 perf_event_sched_in(cpuctx, task_ctx, task);
1670 perf_pmu_enable(cpuctx->ctx.pmu);
1671 perf_ctx_unlock(cpuctx, task_ctx);
1677 * Attach a performance event to a context
1679 * First we add the event to the list with the hardware enable bit
1680 * in event->hw_config cleared.
1682 * If the event is attached to a task which is on a CPU we use a smp
1683 * call to enable it in the task context. The task might have been
1684 * scheduled away, but we check this in the smp call again.
1687 perf_install_in_context(struct perf_event_context *ctx,
1688 struct perf_event *event,
1691 struct task_struct *task = ctx->task;
1693 lockdep_assert_held(&ctx->mutex);
1696 if (event->cpu != -1)
1701 * Per cpu events are installed via an smp call and
1702 * the install is always successful.
1704 cpu_function_call(cpu, __perf_install_in_context, event);
1709 if (!task_function_call(task, __perf_install_in_context, event))
1712 raw_spin_lock_irq(&ctx->lock);
1714 * If we failed to find a running task, but find the context active now
1715 * that we've acquired the ctx->lock, retry.
1717 if (ctx->is_active) {
1718 raw_spin_unlock_irq(&ctx->lock);
1723 * Since the task isn't running, its safe to add the event, us holding
1724 * the ctx->lock ensures the task won't get scheduled in.
1726 add_event_to_ctx(event, ctx);
1727 raw_spin_unlock_irq(&ctx->lock);
1731 * Put a event into inactive state and update time fields.
1732 * Enabling the leader of a group effectively enables all
1733 * the group members that aren't explicitly disabled, so we
1734 * have to update their ->tstamp_enabled also.
1735 * Note: this works for group members as well as group leaders
1736 * since the non-leader members' sibling_lists will be empty.
1738 static void __perf_event_mark_enabled(struct perf_event *event)
1740 struct perf_event *sub;
1741 u64 tstamp = perf_event_time(event);
1743 event->state = PERF_EVENT_STATE_INACTIVE;
1744 event->tstamp_enabled = tstamp - event->total_time_enabled;
1745 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1746 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1747 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1752 * Cross CPU call to enable a performance event
1754 static int __perf_event_enable(void *info)
1756 struct perf_event *event = info;
1757 struct perf_event_context *ctx = event->ctx;
1758 struct perf_event *leader = event->group_leader;
1759 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1762 if (WARN_ON_ONCE(!ctx->is_active))
1765 raw_spin_lock(&ctx->lock);
1766 update_context_time(ctx);
1768 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1772 * set current task's cgroup time reference point
1774 perf_cgroup_set_timestamp(current, ctx);
1776 __perf_event_mark_enabled(event);
1778 if (!event_filter_match(event)) {
1779 if (is_cgroup_event(event))
1780 perf_cgroup_defer_enabled(event);
1785 * If the event is in a group and isn't the group leader,
1786 * then don't put it on unless the group is on.
1788 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1791 if (!group_can_go_on(event, cpuctx, 1)) {
1794 if (event == leader)
1795 err = group_sched_in(event, cpuctx, ctx);
1797 err = event_sched_in(event, cpuctx, ctx);
1802 * If this event can't go on and it's part of a
1803 * group, then the whole group has to come off.
1805 if (leader != event)
1806 group_sched_out(leader, cpuctx, ctx);
1807 if (leader->attr.pinned) {
1808 update_group_times(leader);
1809 leader->state = PERF_EVENT_STATE_ERROR;
1814 raw_spin_unlock(&ctx->lock);
1822 * If event->ctx is a cloned context, callers must make sure that
1823 * every task struct that event->ctx->task could possibly point to
1824 * remains valid. This condition is satisfied when called through
1825 * perf_event_for_each_child or perf_event_for_each as described
1826 * for perf_event_disable.
1828 void perf_event_enable(struct perf_event *event)
1830 struct perf_event_context *ctx = event->ctx;
1831 struct task_struct *task = ctx->task;
1835 * Enable the event on the cpu that it's on
1837 cpu_function_call(event->cpu, __perf_event_enable, event);
1841 raw_spin_lock_irq(&ctx->lock);
1842 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1846 * If the event is in error state, clear that first.
1847 * That way, if we see the event in error state below, we
1848 * know that it has gone back into error state, as distinct
1849 * from the task having been scheduled away before the
1850 * cross-call arrived.
1852 if (event->state == PERF_EVENT_STATE_ERROR)
1853 event->state = PERF_EVENT_STATE_OFF;
1856 if (!ctx->is_active) {
1857 __perf_event_mark_enabled(event);
1861 raw_spin_unlock_irq(&ctx->lock);
1863 if (!task_function_call(task, __perf_event_enable, event))
1866 raw_spin_lock_irq(&ctx->lock);
1869 * If the context is active and the event is still off,
1870 * we need to retry the cross-call.
1872 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1874 * task could have been flipped by a concurrent
1875 * perf_event_context_sched_out()
1882 raw_spin_unlock_irq(&ctx->lock);
1884 EXPORT_SYMBOL_GPL(perf_event_enable);
1886 int perf_event_refresh(struct perf_event *event, int refresh)
1889 * not supported on inherited events
1891 if (event->attr.inherit || !is_sampling_event(event))
1894 atomic_add(refresh, &event->event_limit);
1895 perf_event_enable(event);
1899 EXPORT_SYMBOL_GPL(perf_event_refresh);
1901 static void ctx_sched_out(struct perf_event_context *ctx,
1902 struct perf_cpu_context *cpuctx,
1903 enum event_type_t event_type)
1905 struct perf_event *event;
1906 int is_active = ctx->is_active;
1908 ctx->is_active &= ~event_type;
1909 if (likely(!ctx->nr_events))
1912 update_context_time(ctx);
1913 update_cgrp_time_from_cpuctx(cpuctx);
1914 if (!ctx->nr_active)
1917 perf_pmu_disable(ctx->pmu);
1918 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
1919 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1920 group_sched_out(event, cpuctx, ctx);
1923 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
1924 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1925 group_sched_out(event, cpuctx, ctx);
1927 perf_pmu_enable(ctx->pmu);
1931 * Test whether two contexts are equivalent, i.e. whether they
1932 * have both been cloned from the same version of the same context
1933 * and they both have the same number of enabled events.
1934 * If the number of enabled events is the same, then the set
1935 * of enabled events should be the same, because these are both
1936 * inherited contexts, therefore we can't access individual events
1937 * in them directly with an fd; we can only enable/disable all
1938 * events via prctl, or enable/disable all events in a family
1939 * via ioctl, which will have the same effect on both contexts.
1941 static int context_equiv(struct perf_event_context *ctx1,
1942 struct perf_event_context *ctx2)
1944 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1945 && ctx1->parent_gen == ctx2->parent_gen
1946 && !ctx1->pin_count && !ctx2->pin_count;
1949 static void __perf_event_sync_stat(struct perf_event *event,
1950 struct perf_event *next_event)
1954 if (!event->attr.inherit_stat)
1958 * Update the event value, we cannot use perf_event_read()
1959 * because we're in the middle of a context switch and have IRQs
1960 * disabled, which upsets smp_call_function_single(), however
1961 * we know the event must be on the current CPU, therefore we
1962 * don't need to use it.
1964 switch (event->state) {
1965 case PERF_EVENT_STATE_ACTIVE:
1966 event->pmu->read(event);
1969 case PERF_EVENT_STATE_INACTIVE:
1970 update_event_times(event);
1978 * In order to keep per-task stats reliable we need to flip the event
1979 * values when we flip the contexts.
1981 value = local64_read(&next_event->count);
1982 value = local64_xchg(&event->count, value);
1983 local64_set(&next_event->count, value);
1985 swap(event->total_time_enabled, next_event->total_time_enabled);
1986 swap(event->total_time_running, next_event->total_time_running);
1989 * Since we swizzled the values, update the user visible data too.
1991 perf_event_update_userpage(event);
1992 perf_event_update_userpage(next_event);
1995 #define list_next_entry(pos, member) \
1996 list_entry(pos->member.next, typeof(*pos), member)
1998 static void perf_event_sync_stat(struct perf_event_context *ctx,
1999 struct perf_event_context *next_ctx)
2001 struct perf_event *event, *next_event;
2006 update_context_time(ctx);
2008 event = list_first_entry(&ctx->event_list,
2009 struct perf_event, event_entry);
2011 next_event = list_first_entry(&next_ctx->event_list,
2012 struct perf_event, event_entry);
2014 while (&event->event_entry != &ctx->event_list &&
2015 &next_event->event_entry != &next_ctx->event_list) {
2017 __perf_event_sync_stat(event, next_event);
2019 event = list_next_entry(event, event_entry);
2020 next_event = list_next_entry(next_event, event_entry);
2024 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2025 struct task_struct *next)
2027 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
2028 struct perf_event_context *next_ctx;
2029 struct perf_event_context *parent;
2030 struct perf_cpu_context *cpuctx;
2036 cpuctx = __get_cpu_context(ctx);
2037 if (!cpuctx->task_ctx)
2041 parent = rcu_dereference(ctx->parent_ctx);
2042 next_ctx = next->perf_event_ctxp[ctxn];
2043 if (parent && next_ctx &&
2044 rcu_dereference(next_ctx->parent_ctx) == parent) {
2046 * Looks like the two contexts are clones, so we might be
2047 * able to optimize the context switch. We lock both
2048 * contexts and check that they are clones under the
2049 * lock (including re-checking that neither has been
2050 * uncloned in the meantime). It doesn't matter which
2051 * order we take the locks because no other cpu could
2052 * be trying to lock both of these tasks.
2054 raw_spin_lock(&ctx->lock);
2055 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
2056 if (context_equiv(ctx, next_ctx)) {
2058 * XXX do we need a memory barrier of sorts
2059 * wrt to rcu_dereference() of perf_event_ctxp
2061 task->perf_event_ctxp[ctxn] = next_ctx;
2062 next->perf_event_ctxp[ctxn] = ctx;
2064 next_ctx->task = task;
2067 perf_event_sync_stat(ctx, next_ctx);
2069 raw_spin_unlock(&next_ctx->lock);
2070 raw_spin_unlock(&ctx->lock);
2075 raw_spin_lock(&ctx->lock);
2076 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2077 cpuctx->task_ctx = NULL;
2078 raw_spin_unlock(&ctx->lock);
2082 #define for_each_task_context_nr(ctxn) \
2083 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2086 * Called from scheduler to remove the events of the current task,
2087 * with interrupts disabled.
2089 * We stop each event and update the event value in event->count.
2091 * This does not protect us against NMI, but disable()
2092 * sets the disabled bit in the control field of event _before_
2093 * accessing the event control register. If a NMI hits, then it will
2094 * not restart the event.
2096 void __perf_event_task_sched_out(struct task_struct *task,
2097 struct task_struct *next)
2101 for_each_task_context_nr(ctxn)
2102 perf_event_context_sched_out(task, ctxn, next);
2105 * if cgroup events exist on this CPU, then we need
2106 * to check if we have to switch out PMU state.
2107 * cgroup event are system-wide mode only
2109 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2110 perf_cgroup_sched_out(task, next);
2113 static void task_ctx_sched_out(struct perf_event_context *ctx)
2115 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2117 if (!cpuctx->task_ctx)
2120 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2123 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2124 cpuctx->task_ctx = NULL;
2128 * Called with IRQs disabled
2130 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2131 enum event_type_t event_type)
2133 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
2137 ctx_pinned_sched_in(struct perf_event_context *ctx,
2138 struct perf_cpu_context *cpuctx)
2140 struct perf_event *event;
2142 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2143 if (event->state <= PERF_EVENT_STATE_OFF)
2145 if (!event_filter_match(event))
2148 /* may need to reset tstamp_enabled */
2149 if (is_cgroup_event(event))
2150 perf_cgroup_mark_enabled(event, ctx);
2152 if (group_can_go_on(event, cpuctx, 1))
2153 group_sched_in(event, cpuctx, ctx);
2156 * If this pinned group hasn't been scheduled,
2157 * put it in error state.
2159 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2160 update_group_times(event);
2161 event->state = PERF_EVENT_STATE_ERROR;
2167 ctx_flexible_sched_in(struct perf_event_context *ctx,
2168 struct perf_cpu_context *cpuctx)
2170 struct perf_event *event;
2173 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2174 /* Ignore events in OFF or ERROR state */
2175 if (event->state <= PERF_EVENT_STATE_OFF)
2178 * Listen to the 'cpu' scheduling filter constraint
2181 if (!event_filter_match(event))
2184 /* may need to reset tstamp_enabled */
2185 if (is_cgroup_event(event))
2186 perf_cgroup_mark_enabled(event, ctx);
2188 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2189 if (group_sched_in(event, cpuctx, ctx))
2196 ctx_sched_in(struct perf_event_context *ctx,
2197 struct perf_cpu_context *cpuctx,
2198 enum event_type_t event_type,
2199 struct task_struct *task)
2202 int is_active = ctx->is_active;
2204 ctx->is_active |= event_type;
2205 if (likely(!ctx->nr_events))
2209 ctx->timestamp = now;
2210 perf_cgroup_set_timestamp(task, ctx);
2212 * First go through the list and put on any pinned groups
2213 * in order to give them the best chance of going on.
2215 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
2216 ctx_pinned_sched_in(ctx, cpuctx);
2218 /* Then walk through the lower prio flexible groups */
2219 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
2220 ctx_flexible_sched_in(ctx, cpuctx);
2223 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2224 enum event_type_t event_type,
2225 struct task_struct *task)
2227 struct perf_event_context *ctx = &cpuctx->ctx;
2229 ctx_sched_in(ctx, cpuctx, event_type, task);
2232 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2233 struct task_struct *task)
2235 struct perf_cpu_context *cpuctx;
2237 cpuctx = __get_cpu_context(ctx);
2238 if (cpuctx->task_ctx == ctx)
2241 perf_ctx_lock(cpuctx, ctx);
2242 perf_pmu_disable(ctx->pmu);
2244 * We want to keep the following priority order:
2245 * cpu pinned (that don't need to move), task pinned,
2246 * cpu flexible, task flexible.
2248 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2251 cpuctx->task_ctx = ctx;
2253 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2255 perf_pmu_enable(ctx->pmu);
2256 perf_ctx_unlock(cpuctx, ctx);
2259 * Since these rotations are per-cpu, we need to ensure the
2260 * cpu-context we got scheduled on is actually rotating.
2262 perf_pmu_rotate_start(ctx->pmu);
2266 * When sampling the branck stack in system-wide, it may be necessary
2267 * to flush the stack on context switch. This happens when the branch
2268 * stack does not tag its entries with the pid of the current task.
2269 * Otherwise it becomes impossible to associate a branch entry with a
2270 * task. This ambiguity is more likely to appear when the branch stack
2271 * supports priv level filtering and the user sets it to monitor only
2272 * at the user level (which could be a useful measurement in system-wide
2273 * mode). In that case, the risk is high of having a branch stack with
2274 * branch from multiple tasks. Flushing may mean dropping the existing
2275 * entries or stashing them somewhere in the PMU specific code layer.
2277 * This function provides the context switch callback to the lower code
2278 * layer. It is invoked ONLY when there is at least one system-wide context
2279 * with at least one active event using taken branch sampling.
2281 static void perf_branch_stack_sched_in(struct task_struct *prev,
2282 struct task_struct *task)
2284 struct perf_cpu_context *cpuctx;
2286 unsigned long flags;
2288 /* no need to flush branch stack if not changing task */
2292 local_irq_save(flags);
2296 list_for_each_entry_rcu(pmu, &pmus, entry) {
2297 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2300 * check if the context has at least one
2301 * event using PERF_SAMPLE_BRANCH_STACK
2303 if (cpuctx->ctx.nr_branch_stack > 0
2304 && pmu->flush_branch_stack) {
2306 pmu = cpuctx->ctx.pmu;
2308 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2310 perf_pmu_disable(pmu);
2312 pmu->flush_branch_stack();
2314 perf_pmu_enable(pmu);
2316 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2322 local_irq_restore(flags);
2326 * Called from scheduler to add the events of the current task
2327 * with interrupts disabled.
2329 * We restore the event value and then enable it.
2331 * This does not protect us against NMI, but enable()
2332 * sets the enabled bit in the control field of event _before_
2333 * accessing the event control register. If a NMI hits, then it will
2334 * keep the event running.
2336 void __perf_event_task_sched_in(struct task_struct *prev,
2337 struct task_struct *task)
2339 struct perf_event_context *ctx;
2342 for_each_task_context_nr(ctxn) {
2343 ctx = task->perf_event_ctxp[ctxn];
2347 perf_event_context_sched_in(ctx, task);
2350 * if cgroup events exist on this CPU, then we need
2351 * to check if we have to switch in PMU state.
2352 * cgroup event are system-wide mode only
2354 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2355 perf_cgroup_sched_in(prev, task);
2357 /* check for system-wide branch_stack events */
2358 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2359 perf_branch_stack_sched_in(prev, task);
2362 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2364 u64 frequency = event->attr.sample_freq;
2365 u64 sec = NSEC_PER_SEC;
2366 u64 divisor, dividend;
2368 int count_fls, nsec_fls, frequency_fls, sec_fls;
2370 count_fls = fls64(count);
2371 nsec_fls = fls64(nsec);
2372 frequency_fls = fls64(frequency);
2376 * We got @count in @nsec, with a target of sample_freq HZ
2377 * the target period becomes:
2380 * period = -------------------
2381 * @nsec * sample_freq
2386 * Reduce accuracy by one bit such that @a and @b converge
2387 * to a similar magnitude.
2389 #define REDUCE_FLS(a, b) \
2391 if (a##_fls > b##_fls) { \
2401 * Reduce accuracy until either term fits in a u64, then proceed with
2402 * the other, so that finally we can do a u64/u64 division.
2404 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2405 REDUCE_FLS(nsec, frequency);
2406 REDUCE_FLS(sec, count);
2409 if (count_fls + sec_fls > 64) {
2410 divisor = nsec * frequency;
2412 while (count_fls + sec_fls > 64) {
2413 REDUCE_FLS(count, sec);
2417 dividend = count * sec;
2419 dividend = count * sec;
2421 while (nsec_fls + frequency_fls > 64) {
2422 REDUCE_FLS(nsec, frequency);
2426 divisor = nsec * frequency;
2432 return div64_u64(dividend, divisor);
2435 static DEFINE_PER_CPU(int, perf_throttled_count);
2436 static DEFINE_PER_CPU(u64, perf_throttled_seq);
2438 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
2440 struct hw_perf_event *hwc = &event->hw;
2441 s64 period, sample_period;
2444 period = perf_calculate_period(event, nsec, count);
2446 delta = (s64)(period - hwc->sample_period);
2447 delta = (delta + 7) / 8; /* low pass filter */
2449 sample_period = hwc->sample_period + delta;
2454 hwc->sample_period = sample_period;
2456 if (local64_read(&hwc->period_left) > 8*sample_period) {
2458 event->pmu->stop(event, PERF_EF_UPDATE);
2460 local64_set(&hwc->period_left, 0);
2463 event->pmu->start(event, PERF_EF_RELOAD);
2468 * combine freq adjustment with unthrottling to avoid two passes over the
2469 * events. At the same time, make sure, having freq events does not change
2470 * the rate of unthrottling as that would introduce bias.
2472 static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2475 struct perf_event *event;
2476 struct hw_perf_event *hwc;
2477 u64 now, period = TICK_NSEC;
2481 * only need to iterate over all events iff:
2482 * - context have events in frequency mode (needs freq adjust)
2483 * - there are events to unthrottle on this cpu
2485 if (!(ctx->nr_freq || needs_unthr))
2488 raw_spin_lock(&ctx->lock);
2489 perf_pmu_disable(ctx->pmu);
2491 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2492 if (event->state != PERF_EVENT_STATE_ACTIVE)
2495 if (!event_filter_match(event))
2500 if (needs_unthr && hwc->interrupts == MAX_INTERRUPTS) {
2501 hwc->interrupts = 0;
2502 perf_log_throttle(event, 1);
2503 event->pmu->start(event, 0);
2506 if (!event->attr.freq || !event->attr.sample_freq)
2510 * stop the event and update event->count
2512 event->pmu->stop(event, PERF_EF_UPDATE);
2514 now = local64_read(&event->count);
2515 delta = now - hwc->freq_count_stamp;
2516 hwc->freq_count_stamp = now;
2520 * reload only if value has changed
2521 * we have stopped the event so tell that
2522 * to perf_adjust_period() to avoid stopping it
2526 perf_adjust_period(event, period, delta, false);
2528 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
2531 perf_pmu_enable(ctx->pmu);
2532 raw_spin_unlock(&ctx->lock);
2536 * Round-robin a context's events:
2538 static void rotate_ctx(struct perf_event_context *ctx)
2541 * Rotate the first entry last of non-pinned groups. Rotation might be
2542 * disabled by the inheritance code.
2544 if (!ctx->rotate_disable)
2545 list_rotate_left(&ctx->flexible_groups);
2549 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2550 * because they're strictly cpu affine and rotate_start is called with IRQs
2551 * disabled, while rotate_context is called from IRQ context.
2553 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
2555 struct perf_event_context *ctx = NULL;
2556 int rotate = 0, remove = 1;
2558 if (cpuctx->ctx.nr_events) {
2560 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2564 ctx = cpuctx->task_ctx;
2565 if (ctx && ctx->nr_events) {
2567 if (ctx->nr_events != ctx->nr_active)
2574 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2575 perf_pmu_disable(cpuctx->ctx.pmu);
2577 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2579 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
2581 rotate_ctx(&cpuctx->ctx);
2585 perf_event_sched_in(cpuctx, ctx, current);
2587 perf_pmu_enable(cpuctx->ctx.pmu);
2588 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2591 list_del_init(&cpuctx->rotation_list);
2594 void perf_event_task_tick(void)
2596 struct list_head *head = &__get_cpu_var(rotation_list);
2597 struct perf_cpu_context *cpuctx, *tmp;
2598 struct perf_event_context *ctx;
2601 WARN_ON(!irqs_disabled());
2603 __this_cpu_inc(perf_throttled_seq);
2604 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2606 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2608 perf_adjust_freq_unthr_context(ctx, throttled);
2610 ctx = cpuctx->task_ctx;
2612 perf_adjust_freq_unthr_context(ctx, throttled);
2614 if (cpuctx->jiffies_interval == 1 ||
2615 !(jiffies % cpuctx->jiffies_interval))
2616 perf_rotate_context(cpuctx);
2620 static int event_enable_on_exec(struct perf_event *event,
2621 struct perf_event_context *ctx)
2623 if (!event->attr.enable_on_exec)
2626 event->attr.enable_on_exec = 0;
2627 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2630 __perf_event_mark_enabled(event);
2636 * Enable all of a task's events that have been marked enable-on-exec.
2637 * This expects task == current.
2639 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2641 struct perf_event *event;
2642 unsigned long flags;
2646 local_irq_save(flags);
2647 if (!ctx || !ctx->nr_events)
2651 * We must ctxsw out cgroup events to avoid conflict
2652 * when invoking perf_task_event_sched_in() later on
2653 * in this function. Otherwise we end up trying to
2654 * ctxswin cgroup events which are already scheduled
2657 perf_cgroup_sched_out(current, NULL);
2659 raw_spin_lock(&ctx->lock);
2660 task_ctx_sched_out(ctx);
2662 list_for_each_entry(event, &ctx->event_list, event_entry) {
2663 ret = event_enable_on_exec(event, ctx);
2669 * Unclone this context if we enabled any event.
2674 raw_spin_unlock(&ctx->lock);
2677 * Also calls ctxswin for cgroup events, if any:
2679 perf_event_context_sched_in(ctx, ctx->task);
2681 local_irq_restore(flags);
2685 * Cross CPU call to read the hardware event
2687 static void __perf_event_read(void *info)
2689 struct perf_event *event = info;
2690 struct perf_event_context *ctx = event->ctx;
2691 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2694 * If this is a task context, we need to check whether it is
2695 * the current task context of this cpu. If not it has been
2696 * scheduled out before the smp call arrived. In that case
2697 * event->count would have been updated to a recent sample
2698 * when the event was scheduled out.
2700 if (ctx->task && cpuctx->task_ctx != ctx)
2703 raw_spin_lock(&ctx->lock);
2704 if (ctx->is_active) {
2705 update_context_time(ctx);
2706 update_cgrp_time_from_event(event);
2708 update_event_times(event);
2709 if (event->state == PERF_EVENT_STATE_ACTIVE)
2710 event->pmu->read(event);
2711 raw_spin_unlock(&ctx->lock);
2714 static inline u64 perf_event_count(struct perf_event *event)
2716 return local64_read(&event->count) + atomic64_read(&event->child_count);
2719 static u64 perf_event_read(struct perf_event *event)
2722 * If event is enabled and currently active on a CPU, update the
2723 * value in the event structure:
2725 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2726 smp_call_function_single(event->oncpu,
2727 __perf_event_read, event, 1);
2728 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2729 struct perf_event_context *ctx = event->ctx;
2730 unsigned long flags;
2732 raw_spin_lock_irqsave(&ctx->lock, flags);
2734 * may read while context is not active
2735 * (e.g., thread is blocked), in that case
2736 * we cannot update context time
2738 if (ctx->is_active) {
2739 update_context_time(ctx);
2740 update_cgrp_time_from_event(event);
2742 update_event_times(event);
2743 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2746 return perf_event_count(event);
2750 * Initialize the perf_event context in a task_struct:
2752 static void __perf_event_init_context(struct perf_event_context *ctx)
2754 raw_spin_lock_init(&ctx->lock);
2755 mutex_init(&ctx->mutex);
2756 INIT_LIST_HEAD(&ctx->pinned_groups);
2757 INIT_LIST_HEAD(&ctx->flexible_groups);
2758 INIT_LIST_HEAD(&ctx->event_list);
2759 atomic_set(&ctx->refcount, 1);
2762 static struct perf_event_context *
2763 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2765 struct perf_event_context *ctx;
2767 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2771 __perf_event_init_context(ctx);
2774 get_task_struct(task);
2781 static struct task_struct *
2782 find_lively_task_by_vpid(pid_t vpid)
2784 struct task_struct *task;
2791 task = find_task_by_vpid(vpid);
2793 get_task_struct(task);
2797 return ERR_PTR(-ESRCH);
2799 /* Reuse ptrace permission checks for now. */
2801 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2806 put_task_struct(task);
2807 return ERR_PTR(err);
2812 * Returns a matching context with refcount and pincount.
2814 static struct perf_event_context *
2815 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2817 struct perf_event_context *ctx;
2818 struct perf_cpu_context *cpuctx;
2819 unsigned long flags;
2823 /* Must be root to operate on a CPU event: */
2824 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2825 return ERR_PTR(-EACCES);
2828 * We could be clever and allow to attach a event to an
2829 * offline CPU and activate it when the CPU comes up, but
2832 if (!cpu_online(cpu))
2833 return ERR_PTR(-ENODEV);
2835 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2844 ctxn = pmu->task_ctx_nr;
2849 ctx = perf_lock_task_context(task, ctxn, &flags);
2853 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2855 ctx = alloc_perf_context(pmu, task);
2861 mutex_lock(&task->perf_event_mutex);
2863 * If it has already passed perf_event_exit_task().
2864 * we must see PF_EXITING, it takes this mutex too.
2866 if (task->flags & PF_EXITING)
2868 else if (task->perf_event_ctxp[ctxn])
2873 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2875 mutex_unlock(&task->perf_event_mutex);
2877 if (unlikely(err)) {
2889 return ERR_PTR(err);
2892 static void perf_event_free_filter(struct perf_event *event);
2894 static void free_event_rcu(struct rcu_head *head)
2896 struct perf_event *event;
2898 event = container_of(head, struct perf_event, rcu_head);
2900 put_pid_ns(event->ns);
2901 perf_event_free_filter(event);
2905 static void ring_buffer_put(struct ring_buffer *rb);
2907 static void free_event(struct perf_event *event)
2909 irq_work_sync(&event->pending);
2911 if (!event->parent) {
2912 if (event->attach_state & PERF_ATTACH_TASK)
2913 static_key_slow_dec_deferred(&perf_sched_events);
2914 if (event->attr.mmap || event->attr.mmap_data)
2915 atomic_dec(&nr_mmap_events);
2916 if (event->attr.comm)
2917 atomic_dec(&nr_comm_events);
2918 if (event->attr.task)
2919 atomic_dec(&nr_task_events);
2920 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2921 put_callchain_buffers();
2922 if (is_cgroup_event(event)) {
2923 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
2924 static_key_slow_dec_deferred(&perf_sched_events);
2927 if (has_branch_stack(event)) {
2928 static_key_slow_dec_deferred(&perf_sched_events);
2929 /* is system-wide event */
2930 if (!(event->attach_state & PERF_ATTACH_TASK))
2931 atomic_dec(&per_cpu(perf_branch_stack_events,
2937 ring_buffer_put(event->rb);
2941 if (is_cgroup_event(event))
2942 perf_detach_cgroup(event);
2945 event->destroy(event);
2948 put_ctx(event->ctx);
2950 call_rcu(&event->rcu_head, free_event_rcu);
2953 int perf_event_release_kernel(struct perf_event *event)
2955 struct perf_event_context *ctx = event->ctx;
2957 WARN_ON_ONCE(ctx->parent_ctx);
2959 * There are two ways this annotation is useful:
2961 * 1) there is a lock recursion from perf_event_exit_task
2962 * see the comment there.
2964 * 2) there is a lock-inversion with mmap_sem through
2965 * perf_event_read_group(), which takes faults while
2966 * holding ctx->mutex, however this is called after
2967 * the last filedesc died, so there is no possibility
2968 * to trigger the AB-BA case.
2970 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2971 raw_spin_lock_irq(&ctx->lock);
2972 perf_group_detach(event);
2973 raw_spin_unlock_irq(&ctx->lock);
2974 perf_remove_from_context(event);
2975 mutex_unlock(&ctx->mutex);
2981 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2984 * Called when the last reference to the file is gone.
2986 static void put_event(struct perf_event *event)
2988 struct task_struct *owner;
2990 if (!atomic_long_dec_and_test(&event->refcount))
2994 owner = ACCESS_ONCE(event->owner);
2996 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2997 * !owner it means the list deletion is complete and we can indeed
2998 * free this event, otherwise we need to serialize on
2999 * owner->perf_event_mutex.
3001 smp_read_barrier_depends();
3004 * Since delayed_put_task_struct() also drops the last
3005 * task reference we can safely take a new reference
3006 * while holding the rcu_read_lock().
3008 get_task_struct(owner);
3013 mutex_lock(&owner->perf_event_mutex);
3015 * We have to re-check the event->owner field, if it is cleared
3016 * we raced with perf_event_exit_task(), acquiring the mutex
3017 * ensured they're done, and we can proceed with freeing the
3021 list_del_init(&event->owner_entry);
3022 mutex_unlock(&owner->perf_event_mutex);
3023 put_task_struct(owner);
3026 perf_event_release_kernel(event);
3029 static int perf_release(struct inode *inode, struct file *file)
3031 put_event(file->private_data);
3035 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
3037 struct perf_event *child;
3043 mutex_lock(&event->child_mutex);
3044 total += perf_event_read(event);
3045 *enabled += event->total_time_enabled +
3046 atomic64_read(&event->child_total_time_enabled);
3047 *running += event->total_time_running +
3048 atomic64_read(&event->child_total_time_running);
3050 list_for_each_entry(child, &event->child_list, child_list) {
3051 total += perf_event_read(child);
3052 *enabled += child->total_time_enabled;
3053 *running += child->total_time_running;
3055 mutex_unlock(&event->child_mutex);
3059 EXPORT_SYMBOL_GPL(perf_event_read_value);
3061 static int perf_event_read_group(struct perf_event *event,
3062 u64 read_format, char __user *buf)
3064 struct perf_event *leader = event->group_leader, *sub;
3065 int n = 0, size = 0, ret = -EFAULT;
3066 struct perf_event_context *ctx = leader->ctx;
3068 u64 count, enabled, running;
3070 mutex_lock(&ctx->mutex);
3071 count = perf_event_read_value(leader, &enabled, &running);
3073 values[n++] = 1 + leader->nr_siblings;
3074 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3075 values[n++] = enabled;
3076 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3077 values[n++] = running;
3078 values[n++] = count;
3079 if (read_format & PERF_FORMAT_ID)
3080 values[n++] = primary_event_id(leader);
3082 size = n * sizeof(u64);
3084 if (copy_to_user(buf, values, size))
3089 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3092 values[n++] = perf_event_read_value(sub, &enabled, &running);
3093 if (read_format & PERF_FORMAT_ID)
3094 values[n++] = primary_event_id(sub);
3096 size = n * sizeof(u64);
3098 if (copy_to_user(buf + ret, values, size)) {
3106 mutex_unlock(&ctx->mutex);
3111 static int perf_event_read_one(struct perf_event *event,
3112 u64 read_format, char __user *buf)
3114 u64 enabled, running;
3118 values[n++] = perf_event_read_value(event, &enabled, &running);
3119 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3120 values[n++] = enabled;
3121 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3122 values[n++] = running;
3123 if (read_format & PERF_FORMAT_ID)
3124 values[n++] = primary_event_id(event);
3126 if (copy_to_user(buf, values, n * sizeof(u64)))
3129 return n * sizeof(u64);
3133 * Read the performance event - simple non blocking version for now
3136 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3138 u64 read_format = event->attr.read_format;
3142 * Return end-of-file for a read on a event that is in
3143 * error state (i.e. because it was pinned but it couldn't be
3144 * scheduled on to the CPU at some point).
3146 if (event->state == PERF_EVENT_STATE_ERROR)
3149 if (count < event->read_size)
3152 WARN_ON_ONCE(event->ctx->parent_ctx);
3153 if (read_format & PERF_FORMAT_GROUP)
3154 ret = perf_event_read_group(event, read_format, buf);
3156 ret = perf_event_read_one(event, read_format, buf);
3162 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3164 struct perf_event *event = file->private_data;
3166 return perf_read_hw(event, buf, count);
3169 static unsigned int perf_poll(struct file *file, poll_table *wait)
3171 struct perf_event *event = file->private_data;
3172 struct ring_buffer *rb;
3173 unsigned int events = POLL_HUP;
3176 * Race between perf_event_set_output() and perf_poll(): perf_poll()
3177 * grabs the rb reference but perf_event_set_output() overrides it.
3178 * Here is the timeline for two threads T1, T2:
3179 * t0: T1, rb = rcu_dereference(event->rb)
3180 * t1: T2, old_rb = event->rb
3181 * t2: T2, event->rb = new rb
3182 * t3: T2, ring_buffer_detach(old_rb)
3183 * t4: T1, ring_buffer_attach(rb1)
3184 * t5: T1, poll_wait(event->waitq)
3186 * To avoid this problem, we grab mmap_mutex in perf_poll()
3187 * thereby ensuring that the assignment of the new ring buffer
3188 * and the detachment of the old buffer appear atomic to perf_poll()
3190 mutex_lock(&event->mmap_mutex);
3193 rb = rcu_dereference(event->rb);
3195 ring_buffer_attach(event, rb);
3196 events = atomic_xchg(&rb->poll, 0);
3200 mutex_unlock(&event->mmap_mutex);
3202 poll_wait(file, &event->waitq, wait);
3207 static void perf_event_reset(struct perf_event *event)
3209 (void)perf_event_read(event);
3210 local64_set(&event->count, 0);
3211 perf_event_update_userpage(event);
3215 * Holding the top-level event's child_mutex means that any
3216 * descendant process that has inherited this event will block
3217 * in sync_child_event if it goes to exit, thus satisfying the
3218 * task existence requirements of perf_event_enable/disable.
3220 static void perf_event_for_each_child(struct perf_event *event,
3221 void (*func)(struct perf_event *))
3223 struct perf_event *child;
3225 WARN_ON_ONCE(event->ctx->parent_ctx);
3226 mutex_lock(&event->child_mutex);
3228 list_for_each_entry(child, &event->child_list, child_list)
3230 mutex_unlock(&event->child_mutex);
3233 static void perf_event_for_each(struct perf_event *event,
3234 void (*func)(struct perf_event *))
3236 struct perf_event_context *ctx = event->ctx;
3237 struct perf_event *sibling;
3239 WARN_ON_ONCE(ctx->parent_ctx);
3240 mutex_lock(&ctx->mutex);
3241 event = event->group_leader;
3243 perf_event_for_each_child(event, func);
3244 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3245 perf_event_for_each_child(sibling, func);
3246 mutex_unlock(&ctx->mutex);
3249 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3251 struct perf_event_context *ctx = event->ctx;
3255 if (!is_sampling_event(event))
3258 if (copy_from_user(&value, arg, sizeof(value)))
3264 raw_spin_lock_irq(&ctx->lock);
3265 if (event->attr.freq) {
3266 if (value > sysctl_perf_event_sample_rate) {
3271 event->attr.sample_freq = value;
3273 event->attr.sample_period = value;
3274 event->hw.sample_period = value;
3277 raw_spin_unlock_irq(&ctx->lock);
3282 static const struct file_operations perf_fops;
3284 static inline int perf_fget_light(int fd, struct fd *p)
3286 struct fd f = fdget(fd);
3290 if (f.file->f_op != &perf_fops) {
3298 static int perf_event_set_output(struct perf_event *event,
3299 struct perf_event *output_event);
3300 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3302 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3304 struct perf_event *event = file->private_data;
3305 void (*func)(struct perf_event *);
3309 case PERF_EVENT_IOC_ENABLE:
3310 func = perf_event_enable;
3312 case PERF_EVENT_IOC_DISABLE:
3313 func = perf_event_disable;
3315 case PERF_EVENT_IOC_RESET:
3316 func = perf_event_reset;
3319 case PERF_EVENT_IOC_REFRESH:
3320 return perf_event_refresh(event, arg);
3322 case PERF_EVENT_IOC_PERIOD:
3323 return perf_event_period(event, (u64 __user *)arg);
3325 case PERF_EVENT_IOC_SET_OUTPUT:
3329 struct perf_event *output_event;
3331 ret = perf_fget_light(arg, &output);
3334 output_event = output.file->private_data;
3335 ret = perf_event_set_output(event, output_event);
3338 ret = perf_event_set_output(event, NULL);
3343 case PERF_EVENT_IOC_SET_FILTER:
3344 return perf_event_set_filter(event, (void __user *)arg);
3350 if (flags & PERF_IOC_FLAG_GROUP)
3351 perf_event_for_each(event, func);
3353 perf_event_for_each_child(event, func);
3358 int perf_event_task_enable(void)
3360 struct perf_event *event;
3362 mutex_lock(¤t->perf_event_mutex);
3363 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3364 perf_event_for_each_child(event, perf_event_enable);
3365 mutex_unlock(¤t->perf_event_mutex);
3370 int perf_event_task_disable(void)
3372 struct perf_event *event;
3374 mutex_lock(¤t->perf_event_mutex);
3375 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3376 perf_event_for_each_child(event, perf_event_disable);
3377 mutex_unlock(¤t->perf_event_mutex);
3382 static int perf_event_index(struct perf_event *event)
3384 if (event->hw.state & PERF_HES_STOPPED)
3387 if (event->state != PERF_EVENT_STATE_ACTIVE)
3390 return event->pmu->event_idx(event);
3393 static void calc_timer_values(struct perf_event *event,
3400 *now = perf_clock();
3401 ctx_time = event->shadow_ctx_time + *now;
3402 *enabled = ctx_time - event->tstamp_enabled;
3403 *running = ctx_time - event->tstamp_running;
3406 void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
3411 * Callers need to ensure there can be no nesting of this function, otherwise
3412 * the seqlock logic goes bad. We can not serialize this because the arch
3413 * code calls this from NMI context.
3415 void perf_event_update_userpage(struct perf_event *event)
3417 struct perf_event_mmap_page *userpg;
3418 struct ring_buffer *rb;
3419 u64 enabled, running, now;
3423 * compute total_time_enabled, total_time_running
3424 * based on snapshot values taken when the event
3425 * was last scheduled in.
3427 * we cannot simply called update_context_time()
3428 * because of locking issue as we can be called in
3431 calc_timer_values(event, &now, &enabled, &running);
3432 rb = rcu_dereference(event->rb);
3436 userpg = rb->user_page;
3439 * Disable preemption so as to not let the corresponding user-space
3440 * spin too long if we get preempted.
3445 userpg->index = perf_event_index(event);
3446 userpg->offset = perf_event_count(event);
3448 userpg->offset -= local64_read(&event->hw.prev_count);
3450 userpg->time_enabled = enabled +
3451 atomic64_read(&event->child_total_time_enabled);
3453 userpg->time_running = running +
3454 atomic64_read(&event->child_total_time_running);
3456 arch_perf_update_userpage(userpg, now);
3465 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3467 struct perf_event *event = vma->vm_file->private_data;
3468 struct ring_buffer *rb;
3469 int ret = VM_FAULT_SIGBUS;
3471 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3472 if (vmf->pgoff == 0)
3478 rb = rcu_dereference(event->rb);
3482 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3485 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
3489 get_page(vmf->page);
3490 vmf->page->mapping = vma->vm_file->f_mapping;
3491 vmf->page->index = vmf->pgoff;
3500 static void ring_buffer_attach(struct perf_event *event,
3501 struct ring_buffer *rb)
3503 unsigned long flags;
3505 if (!list_empty(&event->rb_entry))
3508 spin_lock_irqsave(&rb->event_lock, flags);
3509 if (!list_empty(&event->rb_entry))
3512 list_add(&event->rb_entry, &rb->event_list);
3514 spin_unlock_irqrestore(&rb->event_lock, flags);
3517 static void ring_buffer_detach(struct perf_event *event,
3518 struct ring_buffer *rb)
3520 unsigned long flags;
3522 if (list_empty(&event->rb_entry))
3525 spin_lock_irqsave(&rb->event_lock, flags);
3526 list_del_init(&event->rb_entry);
3527 wake_up_all(&event->waitq);
3528 spin_unlock_irqrestore(&rb->event_lock, flags);
3531 static void ring_buffer_wakeup(struct perf_event *event)
3533 struct ring_buffer *rb;
3536 rb = rcu_dereference(event->rb);
3540 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3541 wake_up_all(&event->waitq);
3547 static void rb_free_rcu(struct rcu_head *rcu_head)
3549 struct ring_buffer *rb;
3551 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3555 static struct ring_buffer *ring_buffer_get(struct perf_event *event)
3557 struct ring_buffer *rb;
3560 rb = rcu_dereference(event->rb);
3562 if (!atomic_inc_not_zero(&rb->refcount))
3570 static void ring_buffer_put(struct ring_buffer *rb)
3572 struct perf_event *event, *n;
3573 unsigned long flags;
3575 if (!atomic_dec_and_test(&rb->refcount))
3578 spin_lock_irqsave(&rb->event_lock, flags);
3579 list_for_each_entry_safe(event, n, &rb->event_list, rb_entry) {
3580 list_del_init(&event->rb_entry);
3581 wake_up_all(&event->waitq);
3583 spin_unlock_irqrestore(&rb->event_lock, flags);
3585 call_rcu(&rb->rcu_head, rb_free_rcu);
3588 static void perf_mmap_open(struct vm_area_struct *vma)
3590 struct perf_event *event = vma->vm_file->private_data;
3592 atomic_inc(&event->mmap_count);
3595 static void perf_mmap_close(struct vm_area_struct *vma)
3597 struct perf_event *event = vma->vm_file->private_data;
3599 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
3600 unsigned long size = perf_data_size(event->rb);
3601 struct user_struct *user = event->mmap_user;
3602 struct ring_buffer *rb = event->rb;
3604 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
3605 vma->vm_mm->pinned_vm -= event->mmap_locked;
3606 rcu_assign_pointer(event->rb, NULL);
3607 ring_buffer_detach(event, rb);
3608 mutex_unlock(&event->mmap_mutex);
3610 ring_buffer_put(rb);
3615 static const struct vm_operations_struct perf_mmap_vmops = {
3616 .open = perf_mmap_open,
3617 .close = perf_mmap_close,
3618 .fault = perf_mmap_fault,
3619 .page_mkwrite = perf_mmap_fault,
3622 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3624 struct perf_event *event = file->private_data;
3625 unsigned long user_locked, user_lock_limit;
3626 struct user_struct *user = current_user();
3627 unsigned long locked, lock_limit;
3628 struct ring_buffer *rb;
3629 unsigned long vma_size;
3630 unsigned long nr_pages;
3631 long user_extra, extra;
3632 int ret = 0, flags = 0;
3635 * Don't allow mmap() of inherited per-task counters. This would
3636 * create a performance issue due to all children writing to the
3639 if (event->cpu == -1 && event->attr.inherit)
3642 if (!(vma->vm_flags & VM_SHARED))
3645 vma_size = vma->vm_end - vma->vm_start;
3646 nr_pages = (vma_size / PAGE_SIZE) - 1;
3649 * If we have rb pages ensure they're a power-of-two number, so we
3650 * can do bitmasks instead of modulo.
3652 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3655 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3658 if (vma->vm_pgoff != 0)
3661 WARN_ON_ONCE(event->ctx->parent_ctx);
3662 mutex_lock(&event->mmap_mutex);
3664 if (event->rb->nr_pages == nr_pages)
3665 atomic_inc(&event->rb->refcount);
3671 user_extra = nr_pages + 1;
3672 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3675 * Increase the limit linearly with more CPUs:
3677 user_lock_limit *= num_online_cpus();
3679 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3682 if (user_locked > user_lock_limit)
3683 extra = user_locked - user_lock_limit;
3685 lock_limit = rlimit(RLIMIT_MEMLOCK);
3686 lock_limit >>= PAGE_SHIFT;
3687 locked = vma->vm_mm->pinned_vm + extra;
3689 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3690 !capable(CAP_IPC_LOCK)) {
3697 if (vma->vm_flags & VM_WRITE)
3698 flags |= RING_BUFFER_WRITABLE;
3700 rb = rb_alloc(nr_pages,
3701 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3708 rcu_assign_pointer(event->rb, rb);
3710 atomic_long_add(user_extra, &user->locked_vm);
3711 event->mmap_locked = extra;
3712 event->mmap_user = get_current_user();
3713 vma->vm_mm->pinned_vm += event->mmap_locked;
3715 perf_event_update_userpage(event);
3719 atomic_inc(&event->mmap_count);
3720 mutex_unlock(&event->mmap_mutex);
3722 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3723 vma->vm_ops = &perf_mmap_vmops;
3728 static int perf_fasync(int fd, struct file *filp, int on)
3730 struct inode *inode = file_inode(filp);
3731 struct perf_event *event = filp->private_data;
3734 mutex_lock(&inode->i_mutex);
3735 retval = fasync_helper(fd, filp, on, &event->fasync);
3736 mutex_unlock(&inode->i_mutex);
3744 static const struct file_operations perf_fops = {
3745 .llseek = no_llseek,
3746 .release = perf_release,
3749 .unlocked_ioctl = perf_ioctl,
3750 .compat_ioctl = perf_ioctl,
3752 .fasync = perf_fasync,
3758 * If there's data, ensure we set the poll() state and publish everything
3759 * to user-space before waking everybody up.
3762 void perf_event_wakeup(struct perf_event *event)
3764 ring_buffer_wakeup(event);
3766 if (event->pending_kill) {
3767 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3768 event->pending_kill = 0;
3772 static void perf_pending_event(struct irq_work *entry)
3774 struct perf_event *event = container_of(entry,
3775 struct perf_event, pending);
3777 if (event->pending_disable) {
3778 event->pending_disable = 0;
3779 __perf_event_disable(event);
3782 if (event->pending_wakeup) {
3783 event->pending_wakeup = 0;
3784 perf_event_wakeup(event);
3789 * We assume there is only KVM supporting the callbacks.
3790 * Later on, we might change it to a list if there is
3791 * another virtualization implementation supporting the callbacks.
3793 struct perf_guest_info_callbacks *perf_guest_cbs;
3795 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3797 perf_guest_cbs = cbs;
3800 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3802 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3804 perf_guest_cbs = NULL;
3807 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3810 perf_output_sample_regs(struct perf_output_handle *handle,
3811 struct pt_regs *regs, u64 mask)
3815 for_each_set_bit(bit, (const unsigned long *) &mask,
3816 sizeof(mask) * BITS_PER_BYTE) {
3819 val = perf_reg_value(regs, bit);
3820 perf_output_put(handle, val);
3824 static void perf_sample_regs_user(struct perf_regs_user *regs_user,
3825 struct pt_regs *regs)
3827 if (!user_mode(regs)) {
3829 regs = task_pt_regs(current);
3835 regs_user->regs = regs;
3836 regs_user->abi = perf_reg_abi(current);
3841 * Get remaining task size from user stack pointer.
3843 * It'd be better to take stack vma map and limit this more
3844 * precisly, but there's no way to get it safely under interrupt,
3845 * so using TASK_SIZE as limit.
3847 static u64 perf_ustack_task_size(struct pt_regs *regs)
3849 unsigned long addr = perf_user_stack_pointer(regs);
3851 if (!addr || addr >= TASK_SIZE)
3854 return TASK_SIZE - addr;
3858 perf_sample_ustack_size(u16 stack_size, u16 header_size,
3859 struct pt_regs *regs)
3863 /* No regs, no stack pointer, no dump. */
3868 * Check if we fit in with the requested stack size into the:
3870 * If we don't, we limit the size to the TASK_SIZE.
3872 * - remaining sample size
3873 * If we don't, we customize the stack size to
3874 * fit in to the remaining sample size.
3877 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
3878 stack_size = min(stack_size, (u16) task_size);
3880 /* Current header size plus static size and dynamic size. */
3881 header_size += 2 * sizeof(u64);
3883 /* Do we fit in with the current stack dump size? */
3884 if ((u16) (header_size + stack_size) < header_size) {
3886 * If we overflow the maximum size for the sample,
3887 * we customize the stack dump size to fit in.
3889 stack_size = USHRT_MAX - header_size - sizeof(u64);
3890 stack_size = round_up(stack_size, sizeof(u64));
3897 perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
3898 struct pt_regs *regs)
3900 /* Case of a kernel thread, nothing to dump */
3903 perf_output_put(handle, size);
3912 * - the size requested by user or the best one we can fit
3913 * in to the sample max size
3915 * - user stack dump data
3917 * - the actual dumped size
3921 perf_output_put(handle, dump_size);
3924 sp = perf_user_stack_pointer(regs);
3925 rem = __output_copy_user(handle, (void *) sp, dump_size);
3926 dyn_size = dump_size - rem;
3928 perf_output_skip(handle, rem);
3931 perf_output_put(handle, dyn_size);
3935 static void __perf_event_header__init_id(struct perf_event_header *header,
3936 struct perf_sample_data *data,
3937 struct perf_event *event)
3939 u64 sample_type = event->attr.sample_type;
3941 data->type = sample_type;
3942 header->size += event->id_header_size;
3944 if (sample_type & PERF_SAMPLE_TID) {
3945 /* namespace issues */
3946 data->tid_entry.pid = perf_event_pid(event, current);
3947 data->tid_entry.tid = perf_event_tid(event, current);
3950 if (sample_type & PERF_SAMPLE_TIME)
3951 data->time = perf_clock();
3953 if (sample_type & PERF_SAMPLE_ID)
3954 data->id = primary_event_id(event);
3956 if (sample_type & PERF_SAMPLE_STREAM_ID)
3957 data->stream_id = event->id;
3959 if (sample_type & PERF_SAMPLE_CPU) {
3960 data->cpu_entry.cpu = raw_smp_processor_id();
3961 data->cpu_entry.reserved = 0;
3965 void perf_event_header__init_id(struct perf_event_header *header,
3966 struct perf_sample_data *data,
3967 struct perf_event *event)
3969 if (event->attr.sample_id_all)
3970 __perf_event_header__init_id(header, data, event);
3973 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3974 struct perf_sample_data *data)
3976 u64 sample_type = data->type;
3978 if (sample_type & PERF_SAMPLE_TID)
3979 perf_output_put(handle, data->tid_entry);
3981 if (sample_type & PERF_SAMPLE_TIME)
3982 perf_output_put(handle, data->time);
3984 if (sample_type & PERF_SAMPLE_ID)
3985 perf_output_put(handle, data->id);
3987 if (sample_type & PERF_SAMPLE_STREAM_ID)
3988 perf_output_put(handle, data->stream_id);
3990 if (sample_type & PERF_SAMPLE_CPU)
3991 perf_output_put(handle, data->cpu_entry);
3994 void perf_event__output_id_sample(struct perf_event *event,
3995 struct perf_output_handle *handle,
3996 struct perf_sample_data *sample)
3998 if (event->attr.sample_id_all)
3999 __perf_event__output_id_sample(handle, sample);
4002 static void perf_output_read_one(struct perf_output_handle *handle,
4003 struct perf_event *event,
4004 u64 enabled, u64 running)
4006 u64 read_format = event->attr.read_format;
4010 values[n++] = perf_event_count(event);
4011 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4012 values[n++] = enabled +
4013 atomic64_read(&event->child_total_time_enabled);
4015 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4016 values[n++] = running +
4017 atomic64_read(&event->child_total_time_running);
4019 if (read_format & PERF_FORMAT_ID)
4020 values[n++] = primary_event_id(event);
4022 __output_copy(handle, values, n * sizeof(u64));
4026 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4028 static void perf_output_read_group(struct perf_output_handle *handle,
4029 struct perf_event *event,
4030 u64 enabled, u64 running)
4032 struct perf_event *leader = event->group_leader, *sub;
4033 u64 read_format = event->attr.read_format;
4037 values[n++] = 1 + leader->nr_siblings;
4039 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4040 values[n++] = enabled;
4042 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4043 values[n++] = running;
4045 if (leader != event)
4046 leader->pmu->read(leader);
4048 values[n++] = perf_event_count(leader);
4049 if (read_format & PERF_FORMAT_ID)
4050 values[n++] = primary_event_id(leader);
4052 __output_copy(handle, values, n * sizeof(u64));
4054 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4058 sub->pmu->read(sub);
4060 values[n++] = perf_event_count(sub);
4061 if (read_format & PERF_FORMAT_ID)
4062 values[n++] = primary_event_id(sub);
4064 __output_copy(handle, values, n * sizeof(u64));
4068 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4069 PERF_FORMAT_TOTAL_TIME_RUNNING)
4071 static void perf_output_read(struct perf_output_handle *handle,
4072 struct perf_event *event)
4074 u64 enabled = 0, running = 0, now;
4075 u64 read_format = event->attr.read_format;
4078 * compute total_time_enabled, total_time_running
4079 * based on snapshot values taken when the event
4080 * was last scheduled in.
4082 * we cannot simply called update_context_time()
4083 * because of locking issue as we are called in
4086 if (read_format & PERF_FORMAT_TOTAL_TIMES)
4087 calc_timer_values(event, &now, &enabled, &running);
4089 if (event->attr.read_format & PERF_FORMAT_GROUP)
4090 perf_output_read_group(handle, event, enabled, running);
4092 perf_output_read_one(handle, event, enabled, running);
4095 void perf_output_sample(struct perf_output_handle *handle,
4096 struct perf_event_header *header,
4097 struct perf_sample_data *data,
4098 struct perf_event *event)
4100 u64 sample_type = data->type;
4102 perf_output_put(handle, *header);
4104 if (sample_type & PERF_SAMPLE_IP)
4105 perf_output_put(handle, data->ip);
4107 if (sample_type & PERF_SAMPLE_TID)
4108 perf_output_put(handle, data->tid_entry);
4110 if (sample_type & PERF_SAMPLE_TIME)
4111 perf_output_put(handle, data->time);
4113 if (sample_type & PERF_SAMPLE_ADDR)
4114 perf_output_put(handle, data->addr);
4116 if (sample_type & PERF_SAMPLE_ID)
4117 perf_output_put(handle, data->id);
4119 if (sample_type & PERF_SAMPLE_STREAM_ID)
4120 perf_output_put(handle, data->stream_id);
4122 if (sample_type & PERF_SAMPLE_CPU)
4123 perf_output_put(handle, data->cpu_entry);
4125 if (sample_type & PERF_SAMPLE_PERIOD)
4126 perf_output_put(handle, data->period);
4128 if (sample_type & PERF_SAMPLE_READ)
4129 perf_output_read(handle, event);
4131 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4132 if (data->callchain) {
4135 if (data->callchain)
4136 size += data->callchain->nr;
4138 size *= sizeof(u64);
4140 __output_copy(handle, data->callchain, size);
4143 perf_output_put(handle, nr);
4147 if (sample_type & PERF_SAMPLE_RAW) {
4149 perf_output_put(handle, data->raw->size);
4150 __output_copy(handle, data->raw->data,
4157 .size = sizeof(u32),
4160 perf_output_put(handle, raw);
4164 if (!event->attr.watermark) {
4165 int wakeup_events = event->attr.wakeup_events;
4167 if (wakeup_events) {
4168 struct ring_buffer *rb = handle->rb;
4169 int events = local_inc_return(&rb->events);
4171 if (events >= wakeup_events) {
4172 local_sub(wakeup_events, &rb->events);
4173 local_inc(&rb->wakeup);
4178 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4179 if (data->br_stack) {
4182 size = data->br_stack->nr
4183 * sizeof(struct perf_branch_entry);
4185 perf_output_put(handle, data->br_stack->nr);
4186 perf_output_copy(handle, data->br_stack->entries, size);
4189 * we always store at least the value of nr
4192 perf_output_put(handle, nr);
4196 if (sample_type & PERF_SAMPLE_REGS_USER) {
4197 u64 abi = data->regs_user.abi;
4200 * If there are no regs to dump, notice it through
4201 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4203 perf_output_put(handle, abi);
4206 u64 mask = event->attr.sample_regs_user;
4207 perf_output_sample_regs(handle,
4208 data->regs_user.regs,
4213 if (sample_type & PERF_SAMPLE_STACK_USER)
4214 perf_output_sample_ustack(handle,
4215 data->stack_user_size,
4216 data->regs_user.regs);
4218 if (sample_type & PERF_SAMPLE_WEIGHT)
4219 perf_output_put(handle, data->weight);
4221 if (sample_type & PERF_SAMPLE_DATA_SRC)
4222 perf_output_put(handle, data->data_src.val);
4225 void perf_prepare_sample(struct perf_event_header *header,
4226 struct perf_sample_data *data,
4227 struct perf_event *event,
4228 struct pt_regs *regs)
4230 u64 sample_type = event->attr.sample_type;
4232 header->type = PERF_RECORD_SAMPLE;
4233 header->size = sizeof(*header) + event->header_size;
4236 header->misc |= perf_misc_flags(regs);
4238 __perf_event_header__init_id(header, data, event);
4240 if (sample_type & PERF_SAMPLE_IP)
4241 data->ip = perf_instruction_pointer(regs);
4243 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4246 data->callchain = perf_callchain(event, regs);
4248 if (data->callchain)
4249 size += data->callchain->nr;
4251 header->size += size * sizeof(u64);
4254 if (sample_type & PERF_SAMPLE_RAW) {
4255 int size = sizeof(u32);
4258 size += data->raw->size;
4260 size += sizeof(u32);
4262 WARN_ON_ONCE(size & (sizeof(u64)-1));
4263 header->size += size;
4266 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4267 int size = sizeof(u64); /* nr */
4268 if (data->br_stack) {
4269 size += data->br_stack->nr
4270 * sizeof(struct perf_branch_entry);
4272 header->size += size;
4275 if (sample_type & PERF_SAMPLE_REGS_USER) {
4276 /* regs dump ABI info */
4277 int size = sizeof(u64);
4279 perf_sample_regs_user(&data->regs_user, regs);
4281 if (data->regs_user.regs) {
4282 u64 mask = event->attr.sample_regs_user;
4283 size += hweight64(mask) * sizeof(u64);
4286 header->size += size;
4289 if (sample_type & PERF_SAMPLE_STACK_USER) {
4291 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4292 * processed as the last one or have additional check added
4293 * in case new sample type is added, because we could eat
4294 * up the rest of the sample size.
4296 struct perf_regs_user *uregs = &data->regs_user;
4297 u16 stack_size = event->attr.sample_stack_user;
4298 u16 size = sizeof(u64);
4301 perf_sample_regs_user(uregs, regs);
4303 stack_size = perf_sample_ustack_size(stack_size, header->size,
4307 * If there is something to dump, add space for the dump
4308 * itself and for the field that tells the dynamic size,
4309 * which is how many have been actually dumped.
4312 size += sizeof(u64) + stack_size;
4314 data->stack_user_size = stack_size;
4315 header->size += size;
4319 static void perf_event_output(struct perf_event *event,
4320 struct perf_sample_data *data,
4321 struct pt_regs *regs)
4323 struct perf_output_handle handle;
4324 struct perf_event_header header;
4326 /* protect the callchain buffers */
4329 perf_prepare_sample(&header, data, event, regs);
4331 if (perf_output_begin(&handle, event, header.size))
4334 perf_output_sample(&handle, &header, data, event);
4336 perf_output_end(&handle);
4346 struct perf_read_event {
4347 struct perf_event_header header;
4354 perf_event_read_event(struct perf_event *event,
4355 struct task_struct *task)
4357 struct perf_output_handle handle;
4358 struct perf_sample_data sample;
4359 struct perf_read_event read_event = {
4361 .type = PERF_RECORD_READ,
4363 .size = sizeof(read_event) + event->read_size,
4365 .pid = perf_event_pid(event, task),
4366 .tid = perf_event_tid(event, task),
4370 perf_event_header__init_id(&read_event.header, &sample, event);
4371 ret = perf_output_begin(&handle, event, read_event.header.size);
4375 perf_output_put(&handle, read_event);
4376 perf_output_read(&handle, event);
4377 perf_event__output_id_sample(event, &handle, &sample);
4379 perf_output_end(&handle);
4383 * task tracking -- fork/exit
4385 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4388 struct perf_task_event {
4389 struct task_struct *task;
4390 struct perf_event_context *task_ctx;
4393 struct perf_event_header header;
4403 static void perf_event_task_output(struct perf_event *event,
4404 struct perf_task_event *task_event)
4406 struct perf_output_handle handle;
4407 struct perf_sample_data sample;
4408 struct task_struct *task = task_event->task;
4409 int ret, size = task_event->event_id.header.size;
4411 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4413 ret = perf_output_begin(&handle, event,
4414 task_event->event_id.header.size);
4418 task_event->event_id.pid = perf_event_pid(event, task);
4419 task_event->event_id.ppid = perf_event_pid(event, current);
4421 task_event->event_id.tid = perf_event_tid(event, task);
4422 task_event->event_id.ptid = perf_event_tid(event, current);
4424 perf_output_put(&handle, task_event->event_id);
4426 perf_event__output_id_sample(event, &handle, &sample);
4428 perf_output_end(&handle);
4430 task_event->event_id.header.size = size;
4433 static int perf_event_task_match(struct perf_event *event)
4435 if (event->state < PERF_EVENT_STATE_INACTIVE)
4438 if (!event_filter_match(event))
4441 if (event->attr.comm || event->attr.mmap ||
4442 event->attr.mmap_data || event->attr.task)
4448 static void perf_event_task_ctx(struct perf_event_context *ctx,
4449 struct perf_task_event *task_event)
4451 struct perf_event *event;
4453 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4454 if (perf_event_task_match(event))
4455 perf_event_task_output(event, task_event);
4459 static void perf_event_task_event(struct perf_task_event *task_event)
4461 struct perf_cpu_context *cpuctx;
4462 struct perf_event_context *ctx;
4467 list_for_each_entry_rcu(pmu, &pmus, entry) {
4468 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4469 if (cpuctx->unique_pmu != pmu)
4471 perf_event_task_ctx(&cpuctx->ctx, task_event);
4473 ctx = task_event->task_ctx;
4475 ctxn = pmu->task_ctx_nr;
4478 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4480 perf_event_task_ctx(ctx, task_event);
4483 put_cpu_ptr(pmu->pmu_cpu_context);
4485 if (task_event->task_ctx)
4486 perf_event_task_ctx(task_event->task_ctx, task_event);
4491 static void perf_event_task(struct task_struct *task,
4492 struct perf_event_context *task_ctx,
4495 struct perf_task_event task_event;
4497 if (!atomic_read(&nr_comm_events) &&
4498 !atomic_read(&nr_mmap_events) &&
4499 !atomic_read(&nr_task_events))
4502 task_event = (struct perf_task_event){
4504 .task_ctx = task_ctx,
4507 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4509 .size = sizeof(task_event.event_id),
4515 .time = perf_clock(),
4519 perf_event_task_event(&task_event);
4522 void perf_event_fork(struct task_struct *task)
4524 perf_event_task(task, NULL, 1);
4531 struct perf_comm_event {
4532 struct task_struct *task;
4537 struct perf_event_header header;
4544 static void perf_event_comm_output(struct perf_event *event,
4545 struct perf_comm_event *comm_event)
4547 struct perf_output_handle handle;
4548 struct perf_sample_data sample;
4549 int size = comm_event->event_id.header.size;
4552 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4553 ret = perf_output_begin(&handle, event,
4554 comm_event->event_id.header.size);
4559 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4560 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4562 perf_output_put(&handle, comm_event->event_id);
4563 __output_copy(&handle, comm_event->comm,
4564 comm_event->comm_size);
4566 perf_event__output_id_sample(event, &handle, &sample);
4568 perf_output_end(&handle);
4570 comm_event->event_id.header.size = size;
4573 static int perf_event_comm_match(struct perf_event *event)
4575 if (event->state < PERF_EVENT_STATE_INACTIVE)
4578 if (!event_filter_match(event))
4581 if (event->attr.comm)
4587 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4588 struct perf_comm_event *comm_event)
4590 struct perf_event *event;
4592 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4593 if (perf_event_comm_match(event))
4594 perf_event_comm_output(event, comm_event);
4598 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4600 struct perf_cpu_context *cpuctx;
4601 struct perf_event_context *ctx;
4602 char comm[TASK_COMM_LEN];
4607 memset(comm, 0, sizeof(comm));
4608 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4609 size = ALIGN(strlen(comm)+1, sizeof(u64));
4611 comm_event->comm = comm;
4612 comm_event->comm_size = size;
4614 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4616 list_for_each_entry_rcu(pmu, &pmus, entry) {
4617 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4618 if (cpuctx->unique_pmu != pmu)
4620 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4622 ctxn = pmu->task_ctx_nr;
4626 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4628 perf_event_comm_ctx(ctx, comm_event);
4630 put_cpu_ptr(pmu->pmu_cpu_context);
4635 void perf_event_comm(struct task_struct *task)
4637 struct perf_comm_event comm_event;
4638 struct perf_event_context *ctx;
4642 for_each_task_context_nr(ctxn) {
4643 ctx = task->perf_event_ctxp[ctxn];
4647 perf_event_enable_on_exec(ctx);
4651 if (!atomic_read(&nr_comm_events))
4654 comm_event = (struct perf_comm_event){
4660 .type = PERF_RECORD_COMM,
4669 perf_event_comm_event(&comm_event);
4676 struct perf_mmap_event {
4677 struct vm_area_struct *vma;
4679 const char *file_name;
4683 struct perf_event_header header;
4693 static void perf_event_mmap_output(struct perf_event *event,
4694 struct perf_mmap_event *mmap_event)
4696 struct perf_output_handle handle;
4697 struct perf_sample_data sample;
4698 int size = mmap_event->event_id.header.size;
4701 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4702 ret = perf_output_begin(&handle, event,
4703 mmap_event->event_id.header.size);
4707 mmap_event->event_id.pid = perf_event_pid(event, current);
4708 mmap_event->event_id.tid = perf_event_tid(event, current);
4710 perf_output_put(&handle, mmap_event->event_id);
4711 __output_copy(&handle, mmap_event->file_name,
4712 mmap_event->file_size);
4714 perf_event__output_id_sample(event, &handle, &sample);
4716 perf_output_end(&handle);
4718 mmap_event->event_id.header.size = size;
4721 static int perf_event_mmap_match(struct perf_event *event,
4722 struct perf_mmap_event *mmap_event,
4725 if (event->state < PERF_EVENT_STATE_INACTIVE)
4728 if (!event_filter_match(event))
4731 if ((!executable && event->attr.mmap_data) ||
4732 (executable && event->attr.mmap))
4738 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4739 struct perf_mmap_event *mmap_event,
4742 struct perf_event *event;
4744 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4745 if (perf_event_mmap_match(event, mmap_event, executable))
4746 perf_event_mmap_output(event, mmap_event);
4750 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4752 struct perf_cpu_context *cpuctx;
4753 struct perf_event_context *ctx;
4754 struct vm_area_struct *vma = mmap_event->vma;
4755 struct file *file = vma->vm_file;
4763 memset(tmp, 0, sizeof(tmp));
4767 * d_path works from the end of the rb backwards, so we
4768 * need to add enough zero bytes after the string to handle
4769 * the 64bit alignment we do later.
4771 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4773 name = strncpy(tmp, "//enomem", sizeof(tmp));
4776 name = d_path(&file->f_path, buf, PATH_MAX);
4778 name = strncpy(tmp, "//toolong", sizeof(tmp));
4782 if (arch_vma_name(mmap_event->vma)) {
4783 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4785 tmp[sizeof(tmp) - 1] = '\0';
4790 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4792 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4793 vma->vm_end >= vma->vm_mm->brk) {
4794 name = strncpy(tmp, "[heap]", sizeof(tmp));
4796 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4797 vma->vm_end >= vma->vm_mm->start_stack) {
4798 name = strncpy(tmp, "[stack]", sizeof(tmp));
4802 name = strncpy(tmp, "//anon", sizeof(tmp));
4807 size = ALIGN(strlen(name)+1, sizeof(u64));
4809 mmap_event->file_name = name;
4810 mmap_event->file_size = size;
4812 if (!(vma->vm_flags & VM_EXEC))
4813 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
4815 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4818 list_for_each_entry_rcu(pmu, &pmus, entry) {
4819 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4820 if (cpuctx->unique_pmu != pmu)
4822 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4823 vma->vm_flags & VM_EXEC);
4825 ctxn = pmu->task_ctx_nr;
4829 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4831 perf_event_mmap_ctx(ctx, mmap_event,
4832 vma->vm_flags & VM_EXEC);
4835 put_cpu_ptr(pmu->pmu_cpu_context);
4842 void perf_event_mmap(struct vm_area_struct *vma)
4844 struct perf_mmap_event mmap_event;
4846 if (!atomic_read(&nr_mmap_events))
4849 mmap_event = (struct perf_mmap_event){
4855 .type = PERF_RECORD_MMAP,
4856 .misc = PERF_RECORD_MISC_USER,
4861 .start = vma->vm_start,
4862 .len = vma->vm_end - vma->vm_start,
4863 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4867 perf_event_mmap_event(&mmap_event);
4871 * IRQ throttle logging
4874 static void perf_log_throttle(struct perf_event *event, int enable)
4876 struct perf_output_handle handle;
4877 struct perf_sample_data sample;
4881 struct perf_event_header header;
4885 } throttle_event = {
4887 .type = PERF_RECORD_THROTTLE,
4889 .size = sizeof(throttle_event),
4891 .time = perf_clock(),
4892 .id = primary_event_id(event),
4893 .stream_id = event->id,
4897 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4899 perf_event_header__init_id(&throttle_event.header, &sample, event);
4901 ret = perf_output_begin(&handle, event,
4902 throttle_event.header.size);
4906 perf_output_put(&handle, throttle_event);
4907 perf_event__output_id_sample(event, &handle, &sample);
4908 perf_output_end(&handle);
4912 * Generic event overflow handling, sampling.
4915 static int __perf_event_overflow(struct perf_event *event,
4916 int throttle, struct perf_sample_data *data,
4917 struct pt_regs *regs)
4919 int events = atomic_read(&event->event_limit);
4920 struct hw_perf_event *hwc = &event->hw;
4925 * Non-sampling counters might still use the PMI to fold short
4926 * hardware counters, ignore those.
4928 if (unlikely(!is_sampling_event(event)))
4931 seq = __this_cpu_read(perf_throttled_seq);
4932 if (seq != hwc->interrupts_seq) {
4933 hwc->interrupts_seq = seq;
4934 hwc->interrupts = 1;
4937 if (unlikely(throttle
4938 && hwc->interrupts >= max_samples_per_tick)) {
4939 __this_cpu_inc(perf_throttled_count);
4940 hwc->interrupts = MAX_INTERRUPTS;
4941 perf_log_throttle(event, 0);
4946 if (event->attr.freq) {
4947 u64 now = perf_clock();
4948 s64 delta = now - hwc->freq_time_stamp;
4950 hwc->freq_time_stamp = now;
4952 if (delta > 0 && delta < 2*TICK_NSEC)
4953 perf_adjust_period(event, delta, hwc->last_period, true);
4957 * XXX event_limit might not quite work as expected on inherited
4961 event->pending_kill = POLL_IN;
4962 if (events && atomic_dec_and_test(&event->event_limit)) {
4964 event->pending_kill = POLL_HUP;
4965 event->pending_disable = 1;
4966 irq_work_queue(&event->pending);
4969 if (event->overflow_handler)
4970 event->overflow_handler(event, data, regs);
4972 perf_event_output(event, data, regs);
4974 if (event->fasync && event->pending_kill) {
4975 event->pending_wakeup = 1;
4976 irq_work_queue(&event->pending);
4982 int perf_event_overflow(struct perf_event *event,
4983 struct perf_sample_data *data,
4984 struct pt_regs *regs)
4986 return __perf_event_overflow(event, 1, data, regs);
4990 * Generic software event infrastructure
4993 struct swevent_htable {
4994 struct swevent_hlist *swevent_hlist;
4995 struct mutex hlist_mutex;
4998 /* Recursion avoidance in each contexts */
4999 int recursion[PERF_NR_CONTEXTS];
5002 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5005 * We directly increment event->count and keep a second value in
5006 * event->hw.period_left to count intervals. This period event
5007 * is kept in the range [-sample_period, 0] so that we can use the
5011 static u64 perf_swevent_set_period(struct perf_event *event)
5013 struct hw_perf_event *hwc = &event->hw;
5014 u64 period = hwc->last_period;
5018 hwc->last_period = hwc->sample_period;
5021 old = val = local64_read(&hwc->period_left);
5025 nr = div64_u64(period + val, period);
5026 offset = nr * period;
5028 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
5034 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
5035 struct perf_sample_data *data,
5036 struct pt_regs *regs)
5038 struct hw_perf_event *hwc = &event->hw;
5042 overflow = perf_swevent_set_period(event);
5044 if (hwc->interrupts == MAX_INTERRUPTS)
5047 for (; overflow; overflow--) {
5048 if (__perf_event_overflow(event, throttle,
5051 * We inhibit the overflow from happening when
5052 * hwc->interrupts == MAX_INTERRUPTS.
5060 static void perf_swevent_event(struct perf_event *event, u64 nr,
5061 struct perf_sample_data *data,
5062 struct pt_regs *regs)
5064 struct hw_perf_event *hwc = &event->hw;
5066 local64_add(nr, &event->count);
5071 if (!is_sampling_event(event))
5074 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5076 return perf_swevent_overflow(event, 1, data, regs);
5078 data->period = event->hw.last_period;
5080 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
5081 return perf_swevent_overflow(event, 1, data, regs);
5083 if (local64_add_negative(nr, &hwc->period_left))
5086 perf_swevent_overflow(event, 0, data, regs);
5089 static int perf_exclude_event(struct perf_event *event,
5090 struct pt_regs *regs)
5092 if (event->hw.state & PERF_HES_STOPPED)
5096 if (event->attr.exclude_user && user_mode(regs))
5099 if (event->attr.exclude_kernel && !user_mode(regs))
5106 static int perf_swevent_match(struct perf_event *event,
5107 enum perf_type_id type,
5109 struct perf_sample_data *data,
5110 struct pt_regs *regs)
5112 if (event->attr.type != type)
5115 if (event->attr.config != event_id)
5118 if (perf_exclude_event(event, regs))
5124 static inline u64 swevent_hash(u64 type, u32 event_id)
5126 u64 val = event_id | (type << 32);
5128 return hash_64(val, SWEVENT_HLIST_BITS);
5131 static inline struct hlist_head *
5132 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
5134 u64 hash = swevent_hash(type, event_id);
5136 return &hlist->heads[hash];
5139 /* For the read side: events when they trigger */
5140 static inline struct hlist_head *
5141 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
5143 struct swevent_hlist *hlist;
5145 hlist = rcu_dereference(swhash->swevent_hlist);
5149 return __find_swevent_head(hlist, type, event_id);
5152 /* For the event head insertion and removal in the hlist */
5153 static inline struct hlist_head *
5154 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
5156 struct swevent_hlist *hlist;
5157 u32 event_id = event->attr.config;
5158 u64 type = event->attr.type;
5161 * Event scheduling is always serialized against hlist allocation
5162 * and release. Which makes the protected version suitable here.
5163 * The context lock guarantees that.
5165 hlist = rcu_dereference_protected(swhash->swevent_hlist,
5166 lockdep_is_held(&event->ctx->lock));
5170 return __find_swevent_head(hlist, type, event_id);
5173 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5175 struct perf_sample_data *data,
5176 struct pt_regs *regs)
5178 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5179 struct perf_event *event;
5180 struct hlist_head *head;
5183 head = find_swevent_head_rcu(swhash, type, event_id);
5187 hlist_for_each_entry_rcu(event, head, hlist_entry) {
5188 if (perf_swevent_match(event, type, event_id, data, regs))
5189 perf_swevent_event(event, nr, data, regs);
5195 int perf_swevent_get_recursion_context(void)
5197 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5199 return get_recursion_context(swhash->recursion);
5201 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
5203 inline void perf_swevent_put_recursion_context(int rctx)
5205 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5207 put_recursion_context(swhash->recursion, rctx);
5210 void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
5212 struct perf_sample_data data;
5215 preempt_disable_notrace();
5216 rctx = perf_swevent_get_recursion_context();
5220 perf_sample_data_init(&data, addr, 0);
5222 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
5224 perf_swevent_put_recursion_context(rctx);
5225 preempt_enable_notrace();
5228 static void perf_swevent_read(struct perf_event *event)
5232 static int perf_swevent_add(struct perf_event *event, int flags)
5234 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5235 struct hw_perf_event *hwc = &event->hw;
5236 struct hlist_head *head;
5238 if (is_sampling_event(event)) {
5239 hwc->last_period = hwc->sample_period;
5240 perf_swevent_set_period(event);
5243 hwc->state = !(flags & PERF_EF_START);
5245 head = find_swevent_head(swhash, event);
5246 if (WARN_ON_ONCE(!head))
5249 hlist_add_head_rcu(&event->hlist_entry, head);
5254 static void perf_swevent_del(struct perf_event *event, int flags)
5256 hlist_del_rcu(&event->hlist_entry);
5259 static void perf_swevent_start(struct perf_event *event, int flags)
5261 event->hw.state = 0;
5264 static void perf_swevent_stop(struct perf_event *event, int flags)
5266 event->hw.state = PERF_HES_STOPPED;
5269 /* Deref the hlist from the update side */
5270 static inline struct swevent_hlist *
5271 swevent_hlist_deref(struct swevent_htable *swhash)
5273 return rcu_dereference_protected(swhash->swevent_hlist,
5274 lockdep_is_held(&swhash->hlist_mutex));
5277 static void swevent_hlist_release(struct swevent_htable *swhash)
5279 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
5284 rcu_assign_pointer(swhash->swevent_hlist, NULL);
5285 kfree_rcu(hlist, rcu_head);
5288 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5290 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5292 mutex_lock(&swhash->hlist_mutex);
5294 if (!--swhash->hlist_refcount)
5295 swevent_hlist_release(swhash);
5297 mutex_unlock(&swhash->hlist_mutex);
5300 static void swevent_hlist_put(struct perf_event *event)
5304 if (event->cpu != -1) {
5305 swevent_hlist_put_cpu(event, event->cpu);
5309 for_each_possible_cpu(cpu)
5310 swevent_hlist_put_cpu(event, cpu);
5313 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5315 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5318 mutex_lock(&swhash->hlist_mutex);
5320 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
5321 struct swevent_hlist *hlist;
5323 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5328 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5330 swhash->hlist_refcount++;
5332 mutex_unlock(&swhash->hlist_mutex);
5337 static int swevent_hlist_get(struct perf_event *event)
5340 int cpu, failed_cpu;
5342 if (event->cpu != -1)
5343 return swevent_hlist_get_cpu(event, event->cpu);
5346 for_each_possible_cpu(cpu) {
5347 err = swevent_hlist_get_cpu(event, cpu);
5357 for_each_possible_cpu(cpu) {
5358 if (cpu == failed_cpu)
5360 swevent_hlist_put_cpu(event, cpu);
5367 struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
5369 static void sw_perf_event_destroy(struct perf_event *event)
5371 u64 event_id = event->attr.config;
5373 WARN_ON(event->parent);
5375 static_key_slow_dec(&perf_swevent_enabled[event_id]);
5376 swevent_hlist_put(event);
5379 static int perf_swevent_init(struct perf_event *event)
5381 u64 event_id = event->attr.config;
5383 if (event->attr.type != PERF_TYPE_SOFTWARE)
5387 * no branch sampling for software events
5389 if (has_branch_stack(event))
5393 case PERF_COUNT_SW_CPU_CLOCK:
5394 case PERF_COUNT_SW_TASK_CLOCK:
5401 if (event_id >= PERF_COUNT_SW_MAX)
5404 if (!event->parent) {
5407 err = swevent_hlist_get(event);
5411 static_key_slow_inc(&perf_swevent_enabled[event_id]);
5412 event->destroy = sw_perf_event_destroy;
5418 static int perf_swevent_event_idx(struct perf_event *event)
5423 static struct pmu perf_swevent = {
5424 .task_ctx_nr = perf_sw_context,
5426 .event_init = perf_swevent_init,
5427 .add = perf_swevent_add,
5428 .del = perf_swevent_del,
5429 .start = perf_swevent_start,
5430 .stop = perf_swevent_stop,
5431 .read = perf_swevent_read,
5433 .event_idx = perf_swevent_event_idx,
5436 #ifdef CONFIG_EVENT_TRACING
5438 static int perf_tp_filter_match(struct perf_event *event,
5439 struct perf_sample_data *data)
5441 void *record = data->raw->data;
5443 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5448 static int perf_tp_event_match(struct perf_event *event,
5449 struct perf_sample_data *data,
5450 struct pt_regs *regs)
5452 if (event->hw.state & PERF_HES_STOPPED)
5455 * All tracepoints are from kernel-space.
5457 if (event->attr.exclude_kernel)
5460 if (!perf_tp_filter_match(event, data))
5466 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5467 struct pt_regs *regs, struct hlist_head *head, int rctx,
5468 struct task_struct *task)
5470 struct perf_sample_data data;
5471 struct perf_event *event;
5473 struct perf_raw_record raw = {
5478 perf_sample_data_init(&data, addr, 0);
5481 hlist_for_each_entry_rcu(event, head, hlist_entry) {
5482 if (perf_tp_event_match(event, &data, regs))
5483 perf_swevent_event(event, count, &data, regs);
5487 * If we got specified a target task, also iterate its context and
5488 * deliver this event there too.
5490 if (task && task != current) {
5491 struct perf_event_context *ctx;
5492 struct trace_entry *entry = record;
5495 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
5499 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5500 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5502 if (event->attr.config != entry->type)
5504 if (perf_tp_event_match(event, &data, regs))
5505 perf_swevent_event(event, count, &data, regs);
5511 perf_swevent_put_recursion_context(rctx);
5513 EXPORT_SYMBOL_GPL(perf_tp_event);
5515 static void tp_perf_event_destroy(struct perf_event *event)
5517 perf_trace_destroy(event);
5520 static int perf_tp_event_init(struct perf_event *event)
5524 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5528 * no branch sampling for tracepoint events
5530 if (has_branch_stack(event))
5533 err = perf_trace_init(event);
5537 event->destroy = tp_perf_event_destroy;
5542 static struct pmu perf_tracepoint = {
5543 .task_ctx_nr = perf_sw_context,
5545 .event_init = perf_tp_event_init,
5546 .add = perf_trace_add,
5547 .del = perf_trace_del,
5548 .start = perf_swevent_start,
5549 .stop = perf_swevent_stop,
5550 .read = perf_swevent_read,
5552 .event_idx = perf_swevent_event_idx,
5555 static inline void perf_tp_register(void)
5557 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5560 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5565 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5568 filter_str = strndup_user(arg, PAGE_SIZE);
5569 if (IS_ERR(filter_str))
5570 return PTR_ERR(filter_str);
5572 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5578 static void perf_event_free_filter(struct perf_event *event)
5580 ftrace_profile_free_filter(event);
5585 static inline void perf_tp_register(void)
5589 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5594 static void perf_event_free_filter(struct perf_event *event)
5598 #endif /* CONFIG_EVENT_TRACING */
5600 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5601 void perf_bp_event(struct perf_event *bp, void *data)
5603 struct perf_sample_data sample;
5604 struct pt_regs *regs = data;
5606 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
5608 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5609 perf_swevent_event(bp, 1, &sample, regs);
5614 * hrtimer based swevent callback
5617 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5619 enum hrtimer_restart ret = HRTIMER_RESTART;
5620 struct perf_sample_data data;
5621 struct pt_regs *regs;
5622 struct perf_event *event;
5625 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5627 if (event->state != PERF_EVENT_STATE_ACTIVE)
5628 return HRTIMER_NORESTART;
5630 event->pmu->read(event);
5632 perf_sample_data_init(&data, 0, event->hw.last_period);
5633 regs = get_irq_regs();
5635 if (regs && !perf_exclude_event(event, regs)) {
5636 if (!(event->attr.exclude_idle && is_idle_task(current)))
5637 if (__perf_event_overflow(event, 1, &data, regs))
5638 ret = HRTIMER_NORESTART;
5641 period = max_t(u64, 10000, event->hw.sample_period);
5642 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5647 static void perf_swevent_start_hrtimer(struct perf_event *event)
5649 struct hw_perf_event *hwc = &event->hw;
5652 if (!is_sampling_event(event))
5655 period = local64_read(&hwc->period_left);
5660 local64_set(&hwc->period_left, 0);
5662 period = max_t(u64, 10000, hwc->sample_period);
5664 __hrtimer_start_range_ns(&hwc->hrtimer,
5665 ns_to_ktime(period), 0,
5666 HRTIMER_MODE_REL_PINNED, 0);
5669 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5671 struct hw_perf_event *hwc = &event->hw;
5673 if (is_sampling_event(event)) {
5674 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5675 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5677 hrtimer_cancel(&hwc->hrtimer);
5681 static void perf_swevent_init_hrtimer(struct perf_event *event)
5683 struct hw_perf_event *hwc = &event->hw;
5685 if (!is_sampling_event(event))
5688 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5689 hwc->hrtimer.function = perf_swevent_hrtimer;
5692 * Since hrtimers have a fixed rate, we can do a static freq->period
5693 * mapping and avoid the whole period adjust feedback stuff.
5695 if (event->attr.freq) {
5696 long freq = event->attr.sample_freq;
5698 event->attr.sample_period = NSEC_PER_SEC / freq;
5699 hwc->sample_period = event->attr.sample_period;
5700 local64_set(&hwc->period_left, hwc->sample_period);
5701 hwc->last_period = hwc->sample_period;
5702 event->attr.freq = 0;
5707 * Software event: cpu wall time clock
5710 static void cpu_clock_event_update(struct perf_event *event)
5715 now = local_clock();
5716 prev = local64_xchg(&event->hw.prev_count, now);
5717 local64_add(now - prev, &event->count);
5720 static void cpu_clock_event_start(struct perf_event *event, int flags)
5722 local64_set(&event->hw.prev_count, local_clock());
5723 perf_swevent_start_hrtimer(event);
5726 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5728 perf_swevent_cancel_hrtimer(event);
5729 cpu_clock_event_update(event);
5732 static int cpu_clock_event_add(struct perf_event *event, int flags)
5734 if (flags & PERF_EF_START)
5735 cpu_clock_event_start(event, flags);
5740 static void cpu_clock_event_del(struct perf_event *event, int flags)
5742 cpu_clock_event_stop(event, flags);
5745 static void cpu_clock_event_read(struct perf_event *event)
5747 cpu_clock_event_update(event);
5750 static int cpu_clock_event_init(struct perf_event *event)
5752 if (event->attr.type != PERF_TYPE_SOFTWARE)
5755 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5759 * no branch sampling for software events
5761 if (has_branch_stack(event))
5764 perf_swevent_init_hrtimer(event);
5769 static struct pmu perf_cpu_clock = {
5770 .task_ctx_nr = perf_sw_context,
5772 .event_init = cpu_clock_event_init,
5773 .add = cpu_clock_event_add,
5774 .del = cpu_clock_event_del,
5775 .start = cpu_clock_event_start,
5776 .stop = cpu_clock_event_stop,
5777 .read = cpu_clock_event_read,
5779 .event_idx = perf_swevent_event_idx,
5783 * Software event: task time clock
5786 static void task_clock_event_update(struct perf_event *event, u64 now)
5791 prev = local64_xchg(&event->hw.prev_count, now);
5793 local64_add(delta, &event->count);
5796 static void task_clock_event_start(struct perf_event *event, int flags)
5798 local64_set(&event->hw.prev_count, event->ctx->time);
5799 perf_swevent_start_hrtimer(event);
5802 static void task_clock_event_stop(struct perf_event *event, int flags)
5804 perf_swevent_cancel_hrtimer(event);
5805 task_clock_event_update(event, event->ctx->time);
5808 static int task_clock_event_add(struct perf_event *event, int flags)
5810 if (flags & PERF_EF_START)
5811 task_clock_event_start(event, flags);
5816 static void task_clock_event_del(struct perf_event *event, int flags)
5818 task_clock_event_stop(event, PERF_EF_UPDATE);
5821 static void task_clock_event_read(struct perf_event *event)
5823 u64 now = perf_clock();
5824 u64 delta = now - event->ctx->timestamp;
5825 u64 time = event->ctx->time + delta;
5827 task_clock_event_update(event, time);
5830 static int task_clock_event_init(struct perf_event *event)
5832 if (event->attr.type != PERF_TYPE_SOFTWARE)
5835 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5839 * no branch sampling for software events
5841 if (has_branch_stack(event))
5844 perf_swevent_init_hrtimer(event);
5849 static struct pmu perf_task_clock = {
5850 .task_ctx_nr = perf_sw_context,
5852 .event_init = task_clock_event_init,
5853 .add = task_clock_event_add,
5854 .del = task_clock_event_del,
5855 .start = task_clock_event_start,
5856 .stop = task_clock_event_stop,
5857 .read = task_clock_event_read,
5859 .event_idx = perf_swevent_event_idx,
5862 static void perf_pmu_nop_void(struct pmu *pmu)
5866 static int perf_pmu_nop_int(struct pmu *pmu)
5871 static void perf_pmu_start_txn(struct pmu *pmu)
5873 perf_pmu_disable(pmu);
5876 static int perf_pmu_commit_txn(struct pmu *pmu)
5878 perf_pmu_enable(pmu);
5882 static void perf_pmu_cancel_txn(struct pmu *pmu)
5884 perf_pmu_enable(pmu);
5887 static int perf_event_idx_default(struct perf_event *event)
5889 return event->hw.idx + 1;
5893 * Ensures all contexts with the same task_ctx_nr have the same
5894 * pmu_cpu_context too.
5896 static void *find_pmu_context(int ctxn)
5903 list_for_each_entry(pmu, &pmus, entry) {
5904 if (pmu->task_ctx_nr == ctxn)
5905 return pmu->pmu_cpu_context;
5911 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5915 for_each_possible_cpu(cpu) {
5916 struct perf_cpu_context *cpuctx;
5918 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5920 if (cpuctx->unique_pmu == old_pmu)
5921 cpuctx->unique_pmu = pmu;
5925 static void free_pmu_context(struct pmu *pmu)
5929 mutex_lock(&pmus_lock);
5931 * Like a real lame refcount.
5933 list_for_each_entry(i, &pmus, entry) {
5934 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5935 update_pmu_context(i, pmu);
5940 free_percpu(pmu->pmu_cpu_context);
5942 mutex_unlock(&pmus_lock);
5944 static struct idr pmu_idr;
5947 type_show(struct device *dev, struct device_attribute *attr, char *page)
5949 struct pmu *pmu = dev_get_drvdata(dev);
5951 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5954 static struct device_attribute pmu_dev_attrs[] = {
5959 static int pmu_bus_running;
5960 static struct bus_type pmu_bus = {
5961 .name = "event_source",
5962 .dev_attrs = pmu_dev_attrs,
5965 static void pmu_dev_release(struct device *dev)
5970 static int pmu_dev_alloc(struct pmu *pmu)
5974 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5978 pmu->dev->groups = pmu->attr_groups;
5979 device_initialize(pmu->dev);
5980 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5984 dev_set_drvdata(pmu->dev, pmu);
5985 pmu->dev->bus = &pmu_bus;
5986 pmu->dev->release = pmu_dev_release;
5987 ret = device_add(pmu->dev);
5995 put_device(pmu->dev);
5999 static struct lock_class_key cpuctx_mutex;
6000 static struct lock_class_key cpuctx_lock;
6002 int perf_pmu_register(struct pmu *pmu, char *name, int type)
6006 mutex_lock(&pmus_lock);
6008 pmu->pmu_disable_count = alloc_percpu(int);
6009 if (!pmu->pmu_disable_count)
6018 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6026 if (pmu_bus_running) {
6027 ret = pmu_dev_alloc(pmu);
6033 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6034 if (pmu->pmu_cpu_context)
6035 goto got_cpu_context;
6038 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6039 if (!pmu->pmu_cpu_context)
6042 for_each_possible_cpu(cpu) {
6043 struct perf_cpu_context *cpuctx;
6045 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6046 __perf_event_init_context(&cpuctx->ctx);
6047 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
6048 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
6049 cpuctx->ctx.type = cpu_context;
6050 cpuctx->ctx.pmu = pmu;
6051 cpuctx->jiffies_interval = 1;
6052 INIT_LIST_HEAD(&cpuctx->rotation_list);
6053 cpuctx->unique_pmu = pmu;
6057 if (!pmu->start_txn) {
6058 if (pmu->pmu_enable) {
6060 * If we have pmu_enable/pmu_disable calls, install
6061 * transaction stubs that use that to try and batch
6062 * hardware accesses.
6064 pmu->start_txn = perf_pmu_start_txn;
6065 pmu->commit_txn = perf_pmu_commit_txn;
6066 pmu->cancel_txn = perf_pmu_cancel_txn;
6068 pmu->start_txn = perf_pmu_nop_void;
6069 pmu->commit_txn = perf_pmu_nop_int;
6070 pmu->cancel_txn = perf_pmu_nop_void;
6074 if (!pmu->pmu_enable) {
6075 pmu->pmu_enable = perf_pmu_nop_void;
6076 pmu->pmu_disable = perf_pmu_nop_void;
6079 if (!pmu->event_idx)
6080 pmu->event_idx = perf_event_idx_default;
6082 list_add_rcu(&pmu->entry, &pmus);
6085 mutex_unlock(&pmus_lock);
6090 device_del(pmu->dev);
6091 put_device(pmu->dev);
6094 if (pmu->type >= PERF_TYPE_MAX)
6095 idr_remove(&pmu_idr, pmu->type);
6098 free_percpu(pmu->pmu_disable_count);
6102 void perf_pmu_unregister(struct pmu *pmu)
6104 mutex_lock(&pmus_lock);
6105 list_del_rcu(&pmu->entry);
6106 mutex_unlock(&pmus_lock);
6109 * We dereference the pmu list under both SRCU and regular RCU, so
6110 * synchronize against both of those.
6112 synchronize_srcu(&pmus_srcu);
6115 free_percpu(pmu->pmu_disable_count);
6116 if (pmu->type >= PERF_TYPE_MAX)
6117 idr_remove(&pmu_idr, pmu->type);
6118 device_del(pmu->dev);
6119 put_device(pmu->dev);
6120 free_pmu_context(pmu);
6123 struct pmu *perf_init_event(struct perf_event *event)
6125 struct pmu *pmu = NULL;
6129 idx = srcu_read_lock(&pmus_srcu);
6132 pmu = idr_find(&pmu_idr, event->attr.type);
6136 ret = pmu->event_init(event);
6142 list_for_each_entry_rcu(pmu, &pmus, entry) {
6144 ret = pmu->event_init(event);
6148 if (ret != -ENOENT) {
6153 pmu = ERR_PTR(-ENOENT);
6155 srcu_read_unlock(&pmus_srcu, idx);
6161 * Allocate and initialize a event structure
6163 static struct perf_event *
6164 perf_event_alloc(struct perf_event_attr *attr, int cpu,
6165 struct task_struct *task,
6166 struct perf_event *group_leader,
6167 struct perf_event *parent_event,
6168 perf_overflow_handler_t overflow_handler,
6172 struct perf_event *event;
6173 struct hw_perf_event *hwc;
6176 if ((unsigned)cpu >= nr_cpu_ids) {
6177 if (!task || cpu != -1)
6178 return ERR_PTR(-EINVAL);
6181 event = kzalloc(sizeof(*event), GFP_KERNEL);
6183 return ERR_PTR(-ENOMEM);
6186 * Single events are their own group leaders, with an
6187 * empty sibling list:
6190 group_leader = event;
6192 mutex_init(&event->child_mutex);
6193 INIT_LIST_HEAD(&event->child_list);
6195 INIT_LIST_HEAD(&event->group_entry);
6196 INIT_LIST_HEAD(&event->event_entry);
6197 INIT_LIST_HEAD(&event->sibling_list);
6198 INIT_LIST_HEAD(&event->rb_entry);
6200 init_waitqueue_head(&event->waitq);
6201 init_irq_work(&event->pending, perf_pending_event);
6203 mutex_init(&event->mmap_mutex);
6205 atomic_long_set(&event->refcount, 1);
6207 event->attr = *attr;
6208 event->group_leader = group_leader;
6212 event->parent = parent_event;
6214 event->ns = get_pid_ns(task_active_pid_ns(current));
6215 event->id = atomic64_inc_return(&perf_event_id);
6217 event->state = PERF_EVENT_STATE_INACTIVE;
6220 event->attach_state = PERF_ATTACH_TASK;
6222 if (attr->type == PERF_TYPE_TRACEPOINT)
6223 event->hw.tp_target = task;
6224 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6226 * hw_breakpoint is a bit difficult here..
6228 else if (attr->type == PERF_TYPE_BREAKPOINT)
6229 event->hw.bp_target = task;
6233 if (!overflow_handler && parent_event) {
6234 overflow_handler = parent_event->overflow_handler;
6235 context = parent_event->overflow_handler_context;
6238 event->overflow_handler = overflow_handler;
6239 event->overflow_handler_context = context;
6241 perf_event__state_init(event);
6246 hwc->sample_period = attr->sample_period;
6247 if (attr->freq && attr->sample_freq)
6248 hwc->sample_period = 1;
6249 hwc->last_period = hwc->sample_period;
6251 local64_set(&hwc->period_left, hwc->sample_period);
6254 * we currently do not support PERF_FORMAT_GROUP on inherited events
6256 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6259 pmu = perf_init_event(event);
6265 else if (IS_ERR(pmu))
6270 put_pid_ns(event->ns);
6272 return ERR_PTR(err);
6275 if (!event->parent) {
6276 if (event->attach_state & PERF_ATTACH_TASK)
6277 static_key_slow_inc(&perf_sched_events.key);
6278 if (event->attr.mmap || event->attr.mmap_data)
6279 atomic_inc(&nr_mmap_events);
6280 if (event->attr.comm)
6281 atomic_inc(&nr_comm_events);
6282 if (event->attr.task)
6283 atomic_inc(&nr_task_events);
6284 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6285 err = get_callchain_buffers();
6288 return ERR_PTR(err);
6291 if (has_branch_stack(event)) {
6292 static_key_slow_inc(&perf_sched_events.key);
6293 if (!(event->attach_state & PERF_ATTACH_TASK))
6294 atomic_inc(&per_cpu(perf_branch_stack_events,
6302 static int perf_copy_attr(struct perf_event_attr __user *uattr,
6303 struct perf_event_attr *attr)
6308 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6312 * zero the full structure, so that a short copy will be nice.
6314 memset(attr, 0, sizeof(*attr));
6316 ret = get_user(size, &uattr->size);
6320 if (size > PAGE_SIZE) /* silly large */
6323 if (!size) /* abi compat */
6324 size = PERF_ATTR_SIZE_VER0;
6326 if (size < PERF_ATTR_SIZE_VER0)
6330 * If we're handed a bigger struct than we know of,
6331 * ensure all the unknown bits are 0 - i.e. new
6332 * user-space does not rely on any kernel feature
6333 * extensions we dont know about yet.
6335 if (size > sizeof(*attr)) {
6336 unsigned char __user *addr;
6337 unsigned char __user *end;
6340 addr = (void __user *)uattr + sizeof(*attr);
6341 end = (void __user *)uattr + size;
6343 for (; addr < end; addr++) {
6344 ret = get_user(val, addr);
6350 size = sizeof(*attr);
6353 ret = copy_from_user(attr, uattr, size);
6357 if (attr->__reserved_1)
6360 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6363 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6366 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
6367 u64 mask = attr->branch_sample_type;
6369 /* only using defined bits */
6370 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6373 /* at least one branch bit must be set */
6374 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6377 /* kernel level capture: check permissions */
6378 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
6379 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6382 /* propagate priv level, when not set for branch */
6383 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6385 /* exclude_kernel checked on syscall entry */
6386 if (!attr->exclude_kernel)
6387 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6389 if (!attr->exclude_user)
6390 mask |= PERF_SAMPLE_BRANCH_USER;
6392 if (!attr->exclude_hv)
6393 mask |= PERF_SAMPLE_BRANCH_HV;
6395 * adjust user setting (for HW filter setup)
6397 attr->branch_sample_type = mask;
6401 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
6402 ret = perf_reg_validate(attr->sample_regs_user);
6407 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
6408 if (!arch_perf_have_user_stack_dump())
6412 * We have __u32 type for the size, but so far
6413 * we can only use __u16 as maximum due to the
6414 * __u16 sample size limit.
6416 if (attr->sample_stack_user >= USHRT_MAX)
6418 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
6426 put_user(sizeof(*attr), &uattr->size);
6432 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6434 struct ring_buffer *rb = NULL, *old_rb = NULL;
6440 /* don't allow circular references */
6441 if (event == output_event)
6445 * Don't allow cross-cpu buffers
6447 if (output_event->cpu != event->cpu)
6451 * If its not a per-cpu rb, it must be the same task.
6453 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6457 mutex_lock(&event->mmap_mutex);
6458 /* Can't redirect output if we've got an active mmap() */
6459 if (atomic_read(&event->mmap_count))
6463 /* get the rb we want to redirect to */
6464 rb = ring_buffer_get(output_event);
6470 rcu_assign_pointer(event->rb, rb);
6472 ring_buffer_detach(event, old_rb);
6475 mutex_unlock(&event->mmap_mutex);
6478 ring_buffer_put(old_rb);
6484 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6486 * @attr_uptr: event_id type attributes for monitoring/sampling
6489 * @group_fd: group leader event fd
6491 SYSCALL_DEFINE5(perf_event_open,
6492 struct perf_event_attr __user *, attr_uptr,
6493 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6495 struct perf_event *group_leader = NULL, *output_event = NULL;
6496 struct perf_event *event, *sibling;
6497 struct perf_event_attr attr;
6498 struct perf_event_context *ctx;
6499 struct file *event_file = NULL;
6500 struct fd group = {NULL, 0};
6501 struct task_struct *task = NULL;
6507 /* for future expandability... */
6508 if (flags & ~PERF_FLAG_ALL)
6511 err = perf_copy_attr(attr_uptr, &attr);
6515 if (!attr.exclude_kernel) {
6516 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6521 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6526 * In cgroup mode, the pid argument is used to pass the fd
6527 * opened to the cgroup directory in cgroupfs. The cpu argument
6528 * designates the cpu on which to monitor threads from that
6531 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6534 event_fd = get_unused_fd();
6538 if (group_fd != -1) {
6539 err = perf_fget_light(group_fd, &group);
6542 group_leader = group.file->private_data;
6543 if (flags & PERF_FLAG_FD_OUTPUT)
6544 output_event = group_leader;
6545 if (flags & PERF_FLAG_FD_NO_GROUP)
6546 group_leader = NULL;
6549 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
6550 task = find_lively_task_by_vpid(pid);
6552 err = PTR_ERR(task);
6559 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
6561 if (IS_ERR(event)) {
6562 err = PTR_ERR(event);
6566 if (flags & PERF_FLAG_PID_CGROUP) {
6567 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6572 * - that has cgroup constraint on event->cpu
6573 * - that may need work on context switch
6575 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6576 static_key_slow_inc(&perf_sched_events.key);
6580 * Special case software events and allow them to be part of
6581 * any hardware group.
6586 (is_software_event(event) != is_software_event(group_leader))) {
6587 if (is_software_event(event)) {
6589 * If event and group_leader are not both a software
6590 * event, and event is, then group leader is not.
6592 * Allow the addition of software events to !software
6593 * groups, this is safe because software events never
6596 pmu = group_leader->pmu;
6597 } else if (is_software_event(group_leader) &&
6598 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6600 * In case the group is a pure software group, and we
6601 * try to add a hardware event, move the whole group to
6602 * the hardware context.
6609 * Get the target context (task or percpu):
6611 ctx = find_get_context(pmu, task, event->cpu);
6618 put_task_struct(task);
6623 * Look up the group leader (we will attach this event to it):
6629 * Do not allow a recursive hierarchy (this new sibling
6630 * becoming part of another group-sibling):
6632 if (group_leader->group_leader != group_leader)
6635 * Do not allow to attach to a group in a different
6636 * task or CPU context:
6639 if (group_leader->ctx->type != ctx->type)
6642 if (group_leader->ctx != ctx)
6647 * Only a group leader can be exclusive or pinned
6649 if (attr.exclusive || attr.pinned)
6654 err = perf_event_set_output(event, output_event);
6659 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6660 if (IS_ERR(event_file)) {
6661 err = PTR_ERR(event_file);
6666 struct perf_event_context *gctx = group_leader->ctx;
6668 mutex_lock(&gctx->mutex);
6669 perf_remove_from_context(group_leader);
6672 * Removing from the context ends up with disabled
6673 * event. What we want here is event in the initial
6674 * startup state, ready to be add into new context.
6676 perf_event__state_init(group_leader);
6677 list_for_each_entry(sibling, &group_leader->sibling_list,
6679 perf_remove_from_context(sibling);
6680 perf_event__state_init(sibling);
6683 mutex_unlock(&gctx->mutex);
6687 WARN_ON_ONCE(ctx->parent_ctx);
6688 mutex_lock(&ctx->mutex);
6692 perf_install_in_context(ctx, group_leader, event->cpu);
6694 list_for_each_entry(sibling, &group_leader->sibling_list,
6696 perf_install_in_context(ctx, sibling, event->cpu);
6701 perf_install_in_context(ctx, event, event->cpu);
6703 perf_unpin_context(ctx);
6704 mutex_unlock(&ctx->mutex);
6708 event->owner = current;
6710 mutex_lock(¤t->perf_event_mutex);
6711 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6712 mutex_unlock(¤t->perf_event_mutex);
6715 * Precalculate sample_data sizes
6717 perf_event__header_size(event);
6718 perf_event__id_header_size(event);
6721 * Drop the reference on the group_event after placing the
6722 * new event on the sibling_list. This ensures destruction
6723 * of the group leader will find the pointer to itself in
6724 * perf_group_detach().
6727 fd_install(event_fd, event_file);
6731 perf_unpin_context(ctx);
6738 put_task_struct(task);
6742 put_unused_fd(event_fd);
6747 * perf_event_create_kernel_counter
6749 * @attr: attributes of the counter to create
6750 * @cpu: cpu in which the counter is bound
6751 * @task: task to profile (NULL for percpu)
6754 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6755 struct task_struct *task,
6756 perf_overflow_handler_t overflow_handler,
6759 struct perf_event_context *ctx;
6760 struct perf_event *event;
6764 * Get the target context (task or percpu):
6767 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
6768 overflow_handler, context);
6769 if (IS_ERR(event)) {
6770 err = PTR_ERR(event);
6774 ctx = find_get_context(event->pmu, task, cpu);
6780 WARN_ON_ONCE(ctx->parent_ctx);
6781 mutex_lock(&ctx->mutex);
6782 perf_install_in_context(ctx, event, cpu);
6784 perf_unpin_context(ctx);
6785 mutex_unlock(&ctx->mutex);
6792 return ERR_PTR(err);
6794 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6796 void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
6798 struct perf_event_context *src_ctx;
6799 struct perf_event_context *dst_ctx;
6800 struct perf_event *event, *tmp;
6803 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
6804 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
6806 mutex_lock(&src_ctx->mutex);
6807 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
6809 perf_remove_from_context(event);
6811 list_add(&event->event_entry, &events);
6813 mutex_unlock(&src_ctx->mutex);
6817 mutex_lock(&dst_ctx->mutex);
6818 list_for_each_entry_safe(event, tmp, &events, event_entry) {
6819 list_del(&event->event_entry);
6820 if (event->state >= PERF_EVENT_STATE_OFF)
6821 event->state = PERF_EVENT_STATE_INACTIVE;
6822 perf_install_in_context(dst_ctx, event, dst_cpu);
6825 mutex_unlock(&dst_ctx->mutex);
6827 EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
6829 static void sync_child_event(struct perf_event *child_event,
6830 struct task_struct *child)
6832 struct perf_event *parent_event = child_event->parent;
6835 if (child_event->attr.inherit_stat)
6836 perf_event_read_event(child_event, child);
6838 child_val = perf_event_count(child_event);
6841 * Add back the child's count to the parent's count:
6843 atomic64_add(child_val, &parent_event->child_count);
6844 atomic64_add(child_event->total_time_enabled,
6845 &parent_event->child_total_time_enabled);
6846 atomic64_add(child_event->total_time_running,
6847 &parent_event->child_total_time_running);
6850 * Remove this event from the parent's list
6852 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6853 mutex_lock(&parent_event->child_mutex);
6854 list_del_init(&child_event->child_list);
6855 mutex_unlock(&parent_event->child_mutex);
6858 * Release the parent event, if this was the last
6861 put_event(parent_event);
6865 __perf_event_exit_task(struct perf_event *child_event,
6866 struct perf_event_context *child_ctx,
6867 struct task_struct *child)
6869 if (child_event->parent) {
6870 raw_spin_lock_irq(&child_ctx->lock);
6871 perf_group_detach(child_event);
6872 raw_spin_unlock_irq(&child_ctx->lock);
6875 perf_remove_from_context(child_event);
6878 * It can happen that the parent exits first, and has events
6879 * that are still around due to the child reference. These
6880 * events need to be zapped.
6882 if (child_event->parent) {
6883 sync_child_event(child_event, child);
6884 free_event(child_event);
6888 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6890 struct perf_event *child_event, *tmp;
6891 struct perf_event_context *child_ctx;
6892 unsigned long flags;
6894 if (likely(!child->perf_event_ctxp[ctxn])) {
6895 perf_event_task(child, NULL, 0);
6899 local_irq_save(flags);
6901 * We can't reschedule here because interrupts are disabled,
6902 * and either child is current or it is a task that can't be
6903 * scheduled, so we are now safe from rescheduling changing
6906 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6909 * Take the context lock here so that if find_get_context is
6910 * reading child->perf_event_ctxp, we wait until it has
6911 * incremented the context's refcount before we do put_ctx below.
6913 raw_spin_lock(&child_ctx->lock);
6914 task_ctx_sched_out(child_ctx);
6915 child->perf_event_ctxp[ctxn] = NULL;
6917 * If this context is a clone; unclone it so it can't get
6918 * swapped to another process while we're removing all
6919 * the events from it.
6921 unclone_ctx(child_ctx);
6922 update_context_time(child_ctx);
6923 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6926 * Report the task dead after unscheduling the events so that we
6927 * won't get any samples after PERF_RECORD_EXIT. We can however still
6928 * get a few PERF_RECORD_READ events.
6930 perf_event_task(child, child_ctx, 0);
6933 * We can recurse on the same lock type through:
6935 * __perf_event_exit_task()
6936 * sync_child_event()
6938 * mutex_lock(&ctx->mutex)
6940 * But since its the parent context it won't be the same instance.
6942 mutex_lock(&child_ctx->mutex);
6945 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6947 __perf_event_exit_task(child_event, child_ctx, child);
6949 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6951 __perf_event_exit_task(child_event, child_ctx, child);
6954 * If the last event was a group event, it will have appended all
6955 * its siblings to the list, but we obtained 'tmp' before that which
6956 * will still point to the list head terminating the iteration.
6958 if (!list_empty(&child_ctx->pinned_groups) ||
6959 !list_empty(&child_ctx->flexible_groups))
6962 mutex_unlock(&child_ctx->mutex);
6968 * When a child task exits, feed back event values to parent events.
6970 void perf_event_exit_task(struct task_struct *child)
6972 struct perf_event *event, *tmp;
6975 mutex_lock(&child->perf_event_mutex);
6976 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6978 list_del_init(&event->owner_entry);
6981 * Ensure the list deletion is visible before we clear
6982 * the owner, closes a race against perf_release() where
6983 * we need to serialize on the owner->perf_event_mutex.
6986 event->owner = NULL;
6988 mutex_unlock(&child->perf_event_mutex);
6990 for_each_task_context_nr(ctxn)
6991 perf_event_exit_task_context(child, ctxn);
6994 static void perf_free_event(struct perf_event *event,
6995 struct perf_event_context *ctx)
6997 struct perf_event *parent = event->parent;
6999 if (WARN_ON_ONCE(!parent))
7002 mutex_lock(&parent->child_mutex);
7003 list_del_init(&event->child_list);
7004 mutex_unlock(&parent->child_mutex);
7008 perf_group_detach(event);
7009 list_del_event(event, ctx);
7014 * free an unexposed, unused context as created by inheritance by
7015 * perf_event_init_task below, used by fork() in case of fail.
7017 void perf_event_free_task(struct task_struct *task)
7019 struct perf_event_context *ctx;
7020 struct perf_event *event, *tmp;
7023 for_each_task_context_nr(ctxn) {
7024 ctx = task->perf_event_ctxp[ctxn];
7028 mutex_lock(&ctx->mutex);
7030 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
7032 perf_free_event(event, ctx);
7034 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
7036 perf_free_event(event, ctx);
7038 if (!list_empty(&ctx->pinned_groups) ||
7039 !list_empty(&ctx->flexible_groups))
7042 mutex_unlock(&ctx->mutex);
7048 void perf_event_delayed_put(struct task_struct *task)
7052 for_each_task_context_nr(ctxn)
7053 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
7057 * inherit a event from parent task to child task:
7059 static struct perf_event *
7060 inherit_event(struct perf_event *parent_event,
7061 struct task_struct *parent,
7062 struct perf_event_context *parent_ctx,
7063 struct task_struct *child,
7064 struct perf_event *group_leader,
7065 struct perf_event_context *child_ctx)
7067 struct perf_event *child_event;
7068 unsigned long flags;
7071 * Instead of creating recursive hierarchies of events,
7072 * we link inherited events back to the original parent,
7073 * which has a filp for sure, which we use as the reference
7076 if (parent_event->parent)
7077 parent_event = parent_event->parent;
7079 child_event = perf_event_alloc(&parent_event->attr,
7082 group_leader, parent_event,
7084 if (IS_ERR(child_event))
7087 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
7088 free_event(child_event);
7095 * Make the child state follow the state of the parent event,
7096 * not its attr.disabled bit. We hold the parent's mutex,
7097 * so we won't race with perf_event_{en, dis}able_family.
7099 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
7100 child_event->state = PERF_EVENT_STATE_INACTIVE;
7102 child_event->state = PERF_EVENT_STATE_OFF;
7104 if (parent_event->attr.freq) {
7105 u64 sample_period = parent_event->hw.sample_period;
7106 struct hw_perf_event *hwc = &child_event->hw;
7108 hwc->sample_period = sample_period;
7109 hwc->last_period = sample_period;
7111 local64_set(&hwc->period_left, sample_period);
7114 child_event->ctx = child_ctx;
7115 child_event->overflow_handler = parent_event->overflow_handler;
7116 child_event->overflow_handler_context
7117 = parent_event->overflow_handler_context;
7120 * Precalculate sample_data sizes
7122 perf_event__header_size(child_event);
7123 perf_event__id_header_size(child_event);
7126 * Link it up in the child's context:
7128 raw_spin_lock_irqsave(&child_ctx->lock, flags);
7129 add_event_to_ctx(child_event, child_ctx);
7130 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
7133 * Link this into the parent event's child list
7135 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7136 mutex_lock(&parent_event->child_mutex);
7137 list_add_tail(&child_event->child_list, &parent_event->child_list);
7138 mutex_unlock(&parent_event->child_mutex);
7143 static int inherit_group(struct perf_event *parent_event,
7144 struct task_struct *parent,
7145 struct perf_event_context *parent_ctx,
7146 struct task_struct *child,
7147 struct perf_event_context *child_ctx)
7149 struct perf_event *leader;
7150 struct perf_event *sub;
7151 struct perf_event *child_ctr;
7153 leader = inherit_event(parent_event, parent, parent_ctx,
7154 child, NULL, child_ctx);
7156 return PTR_ERR(leader);
7157 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7158 child_ctr = inherit_event(sub, parent, parent_ctx,
7159 child, leader, child_ctx);
7160 if (IS_ERR(child_ctr))
7161 return PTR_ERR(child_ctr);
7167 inherit_task_group(struct perf_event *event, struct task_struct *parent,
7168 struct perf_event_context *parent_ctx,
7169 struct task_struct *child, int ctxn,
7173 struct perf_event_context *child_ctx;
7175 if (!event->attr.inherit) {
7180 child_ctx = child->perf_event_ctxp[ctxn];
7183 * This is executed from the parent task context, so
7184 * inherit events that have been marked for cloning.
7185 * First allocate and initialize a context for the
7189 child_ctx = alloc_perf_context(event->pmu, child);
7193 child->perf_event_ctxp[ctxn] = child_ctx;
7196 ret = inherit_group(event, parent, parent_ctx,
7206 * Initialize the perf_event context in task_struct
7208 int perf_event_init_context(struct task_struct *child, int ctxn)
7210 struct perf_event_context *child_ctx, *parent_ctx;
7211 struct perf_event_context *cloned_ctx;
7212 struct perf_event *event;
7213 struct task_struct *parent = current;
7214 int inherited_all = 1;
7215 unsigned long flags;
7218 if (likely(!parent->perf_event_ctxp[ctxn]))
7222 * If the parent's context is a clone, pin it so it won't get
7225 parent_ctx = perf_pin_task_context(parent, ctxn);
7228 * No need to check if parent_ctx != NULL here; since we saw
7229 * it non-NULL earlier, the only reason for it to become NULL
7230 * is if we exit, and since we're currently in the middle of
7231 * a fork we can't be exiting at the same time.
7235 * Lock the parent list. No need to lock the child - not PID
7236 * hashed yet and not running, so nobody can access it.
7238 mutex_lock(&parent_ctx->mutex);
7241 * We dont have to disable NMIs - we are only looking at
7242 * the list, not manipulating it:
7244 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
7245 ret = inherit_task_group(event, parent, parent_ctx,
7246 child, ctxn, &inherited_all);
7252 * We can't hold ctx->lock when iterating the ->flexible_group list due
7253 * to allocations, but we need to prevent rotation because
7254 * rotate_ctx() will change the list from interrupt context.
7256 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7257 parent_ctx->rotate_disable = 1;
7258 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7260 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
7261 ret = inherit_task_group(event, parent, parent_ctx,
7262 child, ctxn, &inherited_all);
7267 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7268 parent_ctx->rotate_disable = 0;
7270 child_ctx = child->perf_event_ctxp[ctxn];
7272 if (child_ctx && inherited_all) {
7274 * Mark the child context as a clone of the parent
7275 * context, or of whatever the parent is a clone of.
7277 * Note that if the parent is a clone, the holding of
7278 * parent_ctx->lock avoids it from being uncloned.
7280 cloned_ctx = parent_ctx->parent_ctx;
7282 child_ctx->parent_ctx = cloned_ctx;
7283 child_ctx->parent_gen = parent_ctx->parent_gen;
7285 child_ctx->parent_ctx = parent_ctx;
7286 child_ctx->parent_gen = parent_ctx->generation;
7288 get_ctx(child_ctx->parent_ctx);
7291 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7292 mutex_unlock(&parent_ctx->mutex);
7294 perf_unpin_context(parent_ctx);
7295 put_ctx(parent_ctx);
7301 * Initialize the perf_event context in task_struct
7303 int perf_event_init_task(struct task_struct *child)
7307 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7308 mutex_init(&child->perf_event_mutex);
7309 INIT_LIST_HEAD(&child->perf_event_list);
7311 for_each_task_context_nr(ctxn) {
7312 ret = perf_event_init_context(child, ctxn);
7320 static void __init perf_event_init_all_cpus(void)
7322 struct swevent_htable *swhash;
7325 for_each_possible_cpu(cpu) {
7326 swhash = &per_cpu(swevent_htable, cpu);
7327 mutex_init(&swhash->hlist_mutex);
7328 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
7332 static void __cpuinit perf_event_init_cpu(int cpu)
7334 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7336 mutex_lock(&swhash->hlist_mutex);
7337 if (swhash->hlist_refcount > 0) {
7338 struct swevent_hlist *hlist;
7340 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7342 rcu_assign_pointer(swhash->swevent_hlist, hlist);
7344 mutex_unlock(&swhash->hlist_mutex);
7347 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7348 static void perf_pmu_rotate_stop(struct pmu *pmu)
7350 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7352 WARN_ON(!irqs_disabled());
7354 list_del_init(&cpuctx->rotation_list);
7357 static void __perf_event_exit_context(void *__info)
7359 struct perf_event_context *ctx = __info;
7360 struct perf_event *event, *tmp;
7362 perf_pmu_rotate_stop(ctx->pmu);
7364 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
7365 __perf_remove_from_context(event);
7366 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
7367 __perf_remove_from_context(event);
7370 static void perf_event_exit_cpu_context(int cpu)
7372 struct perf_event_context *ctx;
7376 idx = srcu_read_lock(&pmus_srcu);
7377 list_for_each_entry_rcu(pmu, &pmus, entry) {
7378 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
7380 mutex_lock(&ctx->mutex);
7381 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7382 mutex_unlock(&ctx->mutex);
7384 srcu_read_unlock(&pmus_srcu, idx);
7387 static void perf_event_exit_cpu(int cpu)
7389 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7391 mutex_lock(&swhash->hlist_mutex);
7392 swevent_hlist_release(swhash);
7393 mutex_unlock(&swhash->hlist_mutex);
7395 perf_event_exit_cpu_context(cpu);
7398 static inline void perf_event_exit_cpu(int cpu) { }
7402 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7406 for_each_online_cpu(cpu)
7407 perf_event_exit_cpu(cpu);
7413 * Run the perf reboot notifier at the very last possible moment so that
7414 * the generic watchdog code runs as long as possible.
7416 static struct notifier_block perf_reboot_notifier = {
7417 .notifier_call = perf_reboot,
7418 .priority = INT_MIN,
7421 static int __cpuinit
7422 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7424 unsigned int cpu = (long)hcpu;
7426 switch (action & ~CPU_TASKS_FROZEN) {
7428 case CPU_UP_PREPARE:
7429 case CPU_DOWN_FAILED:
7430 perf_event_init_cpu(cpu);
7433 case CPU_UP_CANCELED:
7434 case CPU_DOWN_PREPARE:
7435 perf_event_exit_cpu(cpu);
7445 void __init perf_event_init(void)
7451 perf_event_init_all_cpus();
7452 init_srcu_struct(&pmus_srcu);
7453 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7454 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7455 perf_pmu_register(&perf_task_clock, NULL, -1);
7457 perf_cpu_notifier(perf_cpu_notify);
7458 register_reboot_notifier(&perf_reboot_notifier);
7460 ret = init_hw_breakpoint();
7461 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7463 /* do not patch jump label more than once per second */
7464 jump_label_rate_limit(&perf_sched_events, HZ);
7467 * Build time assertion that we keep the data_head at the intended
7468 * location. IOW, validation we got the __reserved[] size right.
7470 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
7474 static int __init perf_event_sysfs_init(void)
7479 mutex_lock(&pmus_lock);
7481 ret = bus_register(&pmu_bus);
7485 list_for_each_entry(pmu, &pmus, entry) {
7486 if (!pmu->name || pmu->type < 0)
7489 ret = pmu_dev_alloc(pmu);
7490 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7492 pmu_bus_running = 1;
7496 mutex_unlock(&pmus_lock);
7500 device_initcall(perf_event_sysfs_init);
7502 #ifdef CONFIG_CGROUP_PERF
7503 static struct cgroup_subsys_state *perf_cgroup_css_alloc(struct cgroup *cont)
7505 struct perf_cgroup *jc;
7507 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
7509 return ERR_PTR(-ENOMEM);
7511 jc->info = alloc_percpu(struct perf_cgroup_info);
7514 return ERR_PTR(-ENOMEM);
7520 static void perf_cgroup_css_free(struct cgroup *cont)
7522 struct perf_cgroup *jc;
7523 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7524 struct perf_cgroup, css);
7525 free_percpu(jc->info);
7529 static int __perf_cgroup_move(void *info)
7531 struct task_struct *task = info;
7532 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7536 static void perf_cgroup_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
7538 struct task_struct *task;
7540 cgroup_taskset_for_each(task, cgrp, tset)
7541 task_function_call(task, __perf_cgroup_move, task);
7544 static void perf_cgroup_exit(struct cgroup *cgrp, struct cgroup *old_cgrp,
7545 struct task_struct *task)
7548 * cgroup_exit() is called in the copy_process() failure path.
7549 * Ignore this case since the task hasn't ran yet, this avoids
7550 * trying to poke a half freed task state from generic code.
7552 if (!(task->flags & PF_EXITING))
7555 task_function_call(task, __perf_cgroup_move, task);
7558 struct cgroup_subsys perf_subsys = {
7559 .name = "perf_event",
7560 .subsys_id = perf_subsys_id,
7561 .css_alloc = perf_cgroup_css_alloc,
7562 .css_free = perf_cgroup_css_free,
7563 .exit = perf_cgroup_exit,
7564 .attach = perf_cgroup_attach,
7566 #endif /* CONFIG_CGROUP_PERF */