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 return !event->cgrp || event->cgrp == cpuctx->cgrp;
272 static inline bool perf_tryget_cgroup(struct perf_event *event)
274 return css_tryget(&event->cgrp->css);
277 static inline void perf_put_cgroup(struct perf_event *event)
279 css_put(&event->cgrp->css);
282 static inline void perf_detach_cgroup(struct perf_event *event)
284 perf_put_cgroup(event);
288 static inline int is_cgroup_event(struct perf_event *event)
290 return event->cgrp != NULL;
293 static inline u64 perf_cgroup_event_time(struct perf_event *event)
295 struct perf_cgroup_info *t;
297 t = per_cpu_ptr(event->cgrp->info, event->cpu);
301 static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
303 struct perf_cgroup_info *info;
308 info = this_cpu_ptr(cgrp->info);
310 info->time += now - info->timestamp;
311 info->timestamp = now;
314 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
316 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
318 __update_cgrp_time(cgrp_out);
321 static inline void update_cgrp_time_from_event(struct perf_event *event)
323 struct perf_cgroup *cgrp;
326 * ensure we access cgroup data only when needed and
327 * when we know the cgroup is pinned (css_get)
329 if (!is_cgroup_event(event))
332 cgrp = perf_cgroup_from_task(current);
334 * Do not update time when cgroup is not active
336 if (cgrp == event->cgrp)
337 __update_cgrp_time(event->cgrp);
341 perf_cgroup_set_timestamp(struct task_struct *task,
342 struct perf_event_context *ctx)
344 struct perf_cgroup *cgrp;
345 struct perf_cgroup_info *info;
348 * ctx->lock held by caller
349 * ensure we do not access cgroup data
350 * unless we have the cgroup pinned (css_get)
352 if (!task || !ctx->nr_cgroups)
355 cgrp = perf_cgroup_from_task(task);
356 info = this_cpu_ptr(cgrp->info);
357 info->timestamp = ctx->timestamp;
360 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
361 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
364 * reschedule events based on the cgroup constraint of task.
366 * mode SWOUT : schedule out everything
367 * mode SWIN : schedule in based on cgroup for next
369 void perf_cgroup_switch(struct task_struct *task, int mode)
371 struct perf_cpu_context *cpuctx;
376 * disable interrupts to avoid geting nr_cgroup
377 * changes via __perf_event_disable(). Also
380 local_irq_save(flags);
383 * we reschedule only in the presence of cgroup
384 * constrained events.
388 list_for_each_entry_rcu(pmu, &pmus, entry) {
389 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
390 if (cpuctx->unique_pmu != pmu)
391 continue; /* ensure we process each cpuctx once */
394 * perf_cgroup_events says at least one
395 * context on this CPU has cgroup events.
397 * ctx->nr_cgroups reports the number of cgroup
398 * events for a context.
400 if (cpuctx->ctx.nr_cgroups > 0) {
401 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
402 perf_pmu_disable(cpuctx->ctx.pmu);
404 if (mode & PERF_CGROUP_SWOUT) {
405 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
407 * must not be done before ctxswout due
408 * to event_filter_match() in event_sched_out()
413 if (mode & PERF_CGROUP_SWIN) {
414 WARN_ON_ONCE(cpuctx->cgrp);
416 * set cgrp before ctxsw in to allow
417 * event_filter_match() to not have to pass
420 cpuctx->cgrp = perf_cgroup_from_task(task);
421 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
423 perf_pmu_enable(cpuctx->ctx.pmu);
424 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
430 local_irq_restore(flags);
433 static inline void perf_cgroup_sched_out(struct task_struct *task,
434 struct task_struct *next)
436 struct perf_cgroup *cgrp1;
437 struct perf_cgroup *cgrp2 = NULL;
440 * we come here when we know perf_cgroup_events > 0
442 cgrp1 = perf_cgroup_from_task(task);
445 * next is NULL when called from perf_event_enable_on_exec()
446 * that will systematically cause a cgroup_switch()
449 cgrp2 = perf_cgroup_from_task(next);
452 * only schedule out current cgroup events if we know
453 * that we are switching to a different cgroup. Otherwise,
454 * do no touch the cgroup events.
457 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
460 static inline void perf_cgroup_sched_in(struct task_struct *prev,
461 struct task_struct *task)
463 struct perf_cgroup *cgrp1;
464 struct perf_cgroup *cgrp2 = NULL;
467 * we come here when we know perf_cgroup_events > 0
469 cgrp1 = perf_cgroup_from_task(task);
471 /* prev can never be NULL */
472 cgrp2 = perf_cgroup_from_task(prev);
475 * only need to schedule in cgroup events if we are changing
476 * cgroup during ctxsw. Cgroup events were not scheduled
477 * out of ctxsw out if that was not the case.
480 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
483 static inline int perf_cgroup_connect(int fd, struct perf_event *event,
484 struct perf_event_attr *attr,
485 struct perf_event *group_leader)
487 struct perf_cgroup *cgrp;
488 struct cgroup_subsys_state *css;
489 struct fd f = fdget(fd);
495 css = cgroup_css_from_dir(f.file, perf_subsys_id);
501 cgrp = container_of(css, struct perf_cgroup, css);
504 /* must be done before we fput() the file */
505 if (!perf_tryget_cgroup(event)) {
512 * all events in a group must monitor
513 * the same cgroup because a task belongs
514 * to only one perf cgroup at a time
516 if (group_leader && group_leader->cgrp != cgrp) {
517 perf_detach_cgroup(event);
526 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
528 struct perf_cgroup_info *t;
529 t = per_cpu_ptr(event->cgrp->info, event->cpu);
530 event->shadow_ctx_time = now - t->timestamp;
534 perf_cgroup_defer_enabled(struct perf_event *event)
537 * when the current task's perf cgroup does not match
538 * the event's, we need to remember to call the
539 * perf_mark_enable() function the first time a task with
540 * a matching perf cgroup is scheduled in.
542 if (is_cgroup_event(event) && !perf_cgroup_match(event))
543 event->cgrp_defer_enabled = 1;
547 perf_cgroup_mark_enabled(struct perf_event *event,
548 struct perf_event_context *ctx)
550 struct perf_event *sub;
551 u64 tstamp = perf_event_time(event);
553 if (!event->cgrp_defer_enabled)
556 event->cgrp_defer_enabled = 0;
558 event->tstamp_enabled = tstamp - event->total_time_enabled;
559 list_for_each_entry(sub, &event->sibling_list, group_entry) {
560 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
561 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
562 sub->cgrp_defer_enabled = 0;
566 #else /* !CONFIG_CGROUP_PERF */
569 perf_cgroup_match(struct perf_event *event)
574 static inline void perf_detach_cgroup(struct perf_event *event)
577 static inline int is_cgroup_event(struct perf_event *event)
582 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
587 static inline void update_cgrp_time_from_event(struct perf_event *event)
591 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
595 static inline void perf_cgroup_sched_out(struct task_struct *task,
596 struct task_struct *next)
600 static inline void perf_cgroup_sched_in(struct task_struct *prev,
601 struct task_struct *task)
605 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
606 struct perf_event_attr *attr,
607 struct perf_event *group_leader)
613 perf_cgroup_set_timestamp(struct task_struct *task,
614 struct perf_event_context *ctx)
619 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
624 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
628 static inline u64 perf_cgroup_event_time(struct perf_event *event)
634 perf_cgroup_defer_enabled(struct perf_event *event)
639 perf_cgroup_mark_enabled(struct perf_event *event,
640 struct perf_event_context *ctx)
645 void perf_pmu_disable(struct pmu *pmu)
647 int *count = this_cpu_ptr(pmu->pmu_disable_count);
649 pmu->pmu_disable(pmu);
652 void perf_pmu_enable(struct pmu *pmu)
654 int *count = this_cpu_ptr(pmu->pmu_disable_count);
656 pmu->pmu_enable(pmu);
659 static DEFINE_PER_CPU(struct list_head, rotation_list);
662 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
663 * because they're strictly cpu affine and rotate_start is called with IRQs
664 * disabled, while rotate_context is called from IRQ context.
666 static void perf_pmu_rotate_start(struct pmu *pmu)
668 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
669 struct list_head *head = &__get_cpu_var(rotation_list);
671 WARN_ON(!irqs_disabled());
673 if (list_empty(&cpuctx->rotation_list))
674 list_add(&cpuctx->rotation_list, head);
677 static void get_ctx(struct perf_event_context *ctx)
679 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
682 static void put_ctx(struct perf_event_context *ctx)
684 if (atomic_dec_and_test(&ctx->refcount)) {
686 put_ctx(ctx->parent_ctx);
688 put_task_struct(ctx->task);
689 kfree_rcu(ctx, rcu_head);
693 static void unclone_ctx(struct perf_event_context *ctx)
695 if (ctx->parent_ctx) {
696 put_ctx(ctx->parent_ctx);
697 ctx->parent_ctx = NULL;
701 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
704 * only top level events have the pid namespace they were created in
707 event = event->parent;
709 return task_tgid_nr_ns(p, event->ns);
712 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
715 * only top level events have the pid namespace they were created in
718 event = event->parent;
720 return task_pid_nr_ns(p, event->ns);
724 * If we inherit events we want to return the parent event id
727 static u64 primary_event_id(struct perf_event *event)
732 id = event->parent->id;
738 * Get the perf_event_context for a task and lock it.
739 * This has to cope with with the fact that until it is locked,
740 * the context could get moved to another task.
742 static struct perf_event_context *
743 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
745 struct perf_event_context *ctx;
749 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
752 * If this context is a clone of another, it might
753 * get swapped for another underneath us by
754 * perf_event_task_sched_out, though the
755 * rcu_read_lock() protects us from any context
756 * getting freed. Lock the context and check if it
757 * got swapped before we could get the lock, and retry
758 * if so. If we locked the right context, then it
759 * can't get swapped on us any more.
761 raw_spin_lock_irqsave(&ctx->lock, *flags);
762 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
763 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
767 if (!atomic_inc_not_zero(&ctx->refcount)) {
768 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
777 * Get the context for a task and increment its pin_count so it
778 * can't get swapped to another task. This also increments its
779 * reference count so that the context can't get freed.
781 static struct perf_event_context *
782 perf_pin_task_context(struct task_struct *task, int ctxn)
784 struct perf_event_context *ctx;
787 ctx = perf_lock_task_context(task, ctxn, &flags);
790 raw_spin_unlock_irqrestore(&ctx->lock, flags);
795 static void perf_unpin_context(struct perf_event_context *ctx)
799 raw_spin_lock_irqsave(&ctx->lock, flags);
801 raw_spin_unlock_irqrestore(&ctx->lock, flags);
805 * Update the record of the current time in a context.
807 static void update_context_time(struct perf_event_context *ctx)
809 u64 now = perf_clock();
811 ctx->time += now - ctx->timestamp;
812 ctx->timestamp = now;
815 static u64 perf_event_time(struct perf_event *event)
817 struct perf_event_context *ctx = event->ctx;
819 if (is_cgroup_event(event))
820 return perf_cgroup_event_time(event);
822 return ctx ? ctx->time : 0;
826 * Update the total_time_enabled and total_time_running fields for a event.
827 * The caller of this function needs to hold the ctx->lock.
829 static void update_event_times(struct perf_event *event)
831 struct perf_event_context *ctx = event->ctx;
834 if (event->state < PERF_EVENT_STATE_INACTIVE ||
835 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
838 * in cgroup mode, time_enabled represents
839 * the time the event was enabled AND active
840 * tasks were in the monitored cgroup. This is
841 * independent of the activity of the context as
842 * there may be a mix of cgroup and non-cgroup events.
844 * That is why we treat cgroup events differently
847 if (is_cgroup_event(event))
848 run_end = perf_cgroup_event_time(event);
849 else if (ctx->is_active)
852 run_end = event->tstamp_stopped;
854 event->total_time_enabled = run_end - event->tstamp_enabled;
856 if (event->state == PERF_EVENT_STATE_INACTIVE)
857 run_end = event->tstamp_stopped;
859 run_end = perf_event_time(event);
861 event->total_time_running = run_end - event->tstamp_running;
866 * Update total_time_enabled and total_time_running for all events in a group.
868 static void update_group_times(struct perf_event *leader)
870 struct perf_event *event;
872 update_event_times(leader);
873 list_for_each_entry(event, &leader->sibling_list, group_entry)
874 update_event_times(event);
877 static struct list_head *
878 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
880 if (event->attr.pinned)
881 return &ctx->pinned_groups;
883 return &ctx->flexible_groups;
887 * Add a event from the lists for its context.
888 * Must be called with ctx->mutex and ctx->lock held.
891 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
893 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
894 event->attach_state |= PERF_ATTACH_CONTEXT;
897 * If we're a stand alone event or group leader, we go to the context
898 * list, group events are kept attached to the group so that
899 * perf_group_detach can, at all times, locate all siblings.
901 if (event->group_leader == event) {
902 struct list_head *list;
904 if (is_software_event(event))
905 event->group_flags |= PERF_GROUP_SOFTWARE;
907 list = ctx_group_list(event, ctx);
908 list_add_tail(&event->group_entry, list);
911 if (is_cgroup_event(event))
914 if (has_branch_stack(event))
915 ctx->nr_branch_stack++;
917 list_add_rcu(&event->event_entry, &ctx->event_list);
919 perf_pmu_rotate_start(ctx->pmu);
921 if (event->attr.inherit_stat)
926 * Initialize event state based on the perf_event_attr::disabled.
928 static inline void perf_event__state_init(struct perf_event *event)
930 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
931 PERF_EVENT_STATE_INACTIVE;
935 * Called at perf_event creation and when events are attached/detached from a
938 static void perf_event__read_size(struct perf_event *event)
940 int entry = sizeof(u64); /* value */
944 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
947 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
950 if (event->attr.read_format & PERF_FORMAT_ID)
951 entry += sizeof(u64);
953 if (event->attr.read_format & PERF_FORMAT_GROUP) {
954 nr += event->group_leader->nr_siblings;
959 event->read_size = size;
962 static void perf_event__header_size(struct perf_event *event)
964 struct perf_sample_data *data;
965 u64 sample_type = event->attr.sample_type;
968 perf_event__read_size(event);
970 if (sample_type & PERF_SAMPLE_IP)
971 size += sizeof(data->ip);
973 if (sample_type & PERF_SAMPLE_ADDR)
974 size += sizeof(data->addr);
976 if (sample_type & PERF_SAMPLE_PERIOD)
977 size += sizeof(data->period);
979 if (sample_type & PERF_SAMPLE_WEIGHT)
980 size += sizeof(data->weight);
982 if (sample_type & PERF_SAMPLE_READ)
983 size += event->read_size;
985 if (sample_type & PERF_SAMPLE_DATA_SRC)
986 size += sizeof(data->data_src.val);
988 event->header_size = size;
991 static void perf_event__id_header_size(struct perf_event *event)
993 struct perf_sample_data *data;
994 u64 sample_type = event->attr.sample_type;
997 if (sample_type & PERF_SAMPLE_TID)
998 size += sizeof(data->tid_entry);
1000 if (sample_type & PERF_SAMPLE_TIME)
1001 size += sizeof(data->time);
1003 if (sample_type & PERF_SAMPLE_ID)
1004 size += sizeof(data->id);
1006 if (sample_type & PERF_SAMPLE_STREAM_ID)
1007 size += sizeof(data->stream_id);
1009 if (sample_type & PERF_SAMPLE_CPU)
1010 size += sizeof(data->cpu_entry);
1012 event->id_header_size = size;
1015 static void perf_group_attach(struct perf_event *event)
1017 struct perf_event *group_leader = event->group_leader, *pos;
1020 * We can have double attach due to group movement in perf_event_open.
1022 if (event->attach_state & PERF_ATTACH_GROUP)
1025 event->attach_state |= PERF_ATTACH_GROUP;
1027 if (group_leader == event)
1030 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1031 !is_software_event(event))
1032 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1034 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1035 group_leader->nr_siblings++;
1037 perf_event__header_size(group_leader);
1039 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1040 perf_event__header_size(pos);
1044 * Remove a event from the lists for its context.
1045 * Must be called with ctx->mutex and ctx->lock held.
1048 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1050 struct perf_cpu_context *cpuctx;
1052 * We can have double detach due to exit/hot-unplug + close.
1054 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
1057 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1059 if (is_cgroup_event(event)) {
1061 cpuctx = __get_cpu_context(ctx);
1063 * if there are no more cgroup events
1064 * then cler cgrp to avoid stale pointer
1065 * in update_cgrp_time_from_cpuctx()
1067 if (!ctx->nr_cgroups)
1068 cpuctx->cgrp = NULL;
1071 if (has_branch_stack(event))
1072 ctx->nr_branch_stack--;
1075 if (event->attr.inherit_stat)
1078 list_del_rcu(&event->event_entry);
1080 if (event->group_leader == event)
1081 list_del_init(&event->group_entry);
1083 update_group_times(event);
1086 * If event was in error state, then keep it
1087 * that way, otherwise bogus counts will be
1088 * returned on read(). The only way to get out
1089 * of error state is by explicit re-enabling
1092 if (event->state > PERF_EVENT_STATE_OFF)
1093 event->state = PERF_EVENT_STATE_OFF;
1096 static void perf_group_detach(struct perf_event *event)
1098 struct perf_event *sibling, *tmp;
1099 struct list_head *list = NULL;
1102 * We can have double detach due to exit/hot-unplug + close.
1104 if (!(event->attach_state & PERF_ATTACH_GROUP))
1107 event->attach_state &= ~PERF_ATTACH_GROUP;
1110 * If this is a sibling, remove it from its group.
1112 if (event->group_leader != event) {
1113 list_del_init(&event->group_entry);
1114 event->group_leader->nr_siblings--;
1118 if (!list_empty(&event->group_entry))
1119 list = &event->group_entry;
1122 * If this was a group event with sibling events then
1123 * upgrade the siblings to singleton events by adding them
1124 * to whatever list we are on.
1126 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
1128 list_move_tail(&sibling->group_entry, list);
1129 sibling->group_leader = sibling;
1131 /* Inherit group flags from the previous leader */
1132 sibling->group_flags = event->group_flags;
1136 perf_event__header_size(event->group_leader);
1138 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1139 perf_event__header_size(tmp);
1143 event_filter_match(struct perf_event *event)
1145 return (event->cpu == -1 || event->cpu == smp_processor_id())
1146 && perf_cgroup_match(event);
1150 event_sched_out(struct perf_event *event,
1151 struct perf_cpu_context *cpuctx,
1152 struct perf_event_context *ctx)
1154 u64 tstamp = perf_event_time(event);
1157 * An event which could not be activated because of
1158 * filter mismatch still needs to have its timings
1159 * maintained, otherwise bogus information is return
1160 * via read() for time_enabled, time_running:
1162 if (event->state == PERF_EVENT_STATE_INACTIVE
1163 && !event_filter_match(event)) {
1164 delta = tstamp - event->tstamp_stopped;
1165 event->tstamp_running += delta;
1166 event->tstamp_stopped = tstamp;
1169 if (event->state != PERF_EVENT_STATE_ACTIVE)
1172 event->state = PERF_EVENT_STATE_INACTIVE;
1173 if (event->pending_disable) {
1174 event->pending_disable = 0;
1175 event->state = PERF_EVENT_STATE_OFF;
1177 event->tstamp_stopped = tstamp;
1178 event->pmu->del(event, 0);
1181 if (!is_software_event(event))
1182 cpuctx->active_oncpu--;
1184 if (event->attr.freq && event->attr.sample_freq)
1186 if (event->attr.exclusive || !cpuctx->active_oncpu)
1187 cpuctx->exclusive = 0;
1191 group_sched_out(struct perf_event *group_event,
1192 struct perf_cpu_context *cpuctx,
1193 struct perf_event_context *ctx)
1195 struct perf_event *event;
1196 int state = group_event->state;
1198 event_sched_out(group_event, cpuctx, ctx);
1201 * Schedule out siblings (if any):
1203 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1204 event_sched_out(event, cpuctx, ctx);
1206 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
1207 cpuctx->exclusive = 0;
1211 * Cross CPU call to remove a performance event
1213 * We disable the event on the hardware level first. After that we
1214 * remove it from the context list.
1216 static int __perf_remove_from_context(void *info)
1218 struct perf_event *event = info;
1219 struct perf_event_context *ctx = event->ctx;
1220 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1222 raw_spin_lock(&ctx->lock);
1223 event_sched_out(event, cpuctx, ctx);
1224 list_del_event(event, ctx);
1225 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1227 cpuctx->task_ctx = NULL;
1229 raw_spin_unlock(&ctx->lock);
1236 * Remove the event from a task's (or a CPU's) list of events.
1238 * CPU events are removed with a smp call. For task events we only
1239 * call when the task is on a CPU.
1241 * If event->ctx is a cloned context, callers must make sure that
1242 * every task struct that event->ctx->task could possibly point to
1243 * remains valid. This is OK when called from perf_release since
1244 * that only calls us on the top-level context, which can't be a clone.
1245 * When called from perf_event_exit_task, it's OK because the
1246 * context has been detached from its task.
1248 static void perf_remove_from_context(struct perf_event *event)
1250 struct perf_event_context *ctx = event->ctx;
1251 struct task_struct *task = ctx->task;
1253 lockdep_assert_held(&ctx->mutex);
1257 * Per cpu events are removed via an smp call and
1258 * the removal is always successful.
1260 cpu_function_call(event->cpu, __perf_remove_from_context, event);
1265 if (!task_function_call(task, __perf_remove_from_context, event))
1268 raw_spin_lock_irq(&ctx->lock);
1270 * If we failed to find a running task, but find the context active now
1271 * that we've acquired the ctx->lock, retry.
1273 if (ctx->is_active) {
1274 raw_spin_unlock_irq(&ctx->lock);
1279 * Since the task isn't running, its safe to remove the event, us
1280 * holding the ctx->lock ensures the task won't get scheduled in.
1282 list_del_event(event, ctx);
1283 raw_spin_unlock_irq(&ctx->lock);
1287 * Cross CPU call to disable a performance event
1289 int __perf_event_disable(void *info)
1291 struct perf_event *event = info;
1292 struct perf_event_context *ctx = event->ctx;
1293 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1296 * If this is a per-task event, need to check whether this
1297 * event's task is the current task on this cpu.
1299 * Can trigger due to concurrent perf_event_context_sched_out()
1300 * flipping contexts around.
1302 if (ctx->task && cpuctx->task_ctx != ctx)
1305 raw_spin_lock(&ctx->lock);
1308 * If the event is on, turn it off.
1309 * If it is in error state, leave it in error state.
1311 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1312 update_context_time(ctx);
1313 update_cgrp_time_from_event(event);
1314 update_group_times(event);
1315 if (event == event->group_leader)
1316 group_sched_out(event, cpuctx, ctx);
1318 event_sched_out(event, cpuctx, ctx);
1319 event->state = PERF_EVENT_STATE_OFF;
1322 raw_spin_unlock(&ctx->lock);
1330 * If event->ctx is a cloned context, callers must make sure that
1331 * every task struct that event->ctx->task could possibly point to
1332 * remains valid. This condition is satisifed when called through
1333 * perf_event_for_each_child or perf_event_for_each because they
1334 * hold the top-level event's child_mutex, so any descendant that
1335 * goes to exit will block in sync_child_event.
1336 * When called from perf_pending_event it's OK because event->ctx
1337 * is the current context on this CPU and preemption is disabled,
1338 * hence we can't get into perf_event_task_sched_out for this context.
1340 void perf_event_disable(struct perf_event *event)
1342 struct perf_event_context *ctx = event->ctx;
1343 struct task_struct *task = ctx->task;
1347 * Disable the event on the cpu that it's on
1349 cpu_function_call(event->cpu, __perf_event_disable, event);
1354 if (!task_function_call(task, __perf_event_disable, event))
1357 raw_spin_lock_irq(&ctx->lock);
1359 * If the event is still active, we need to retry the cross-call.
1361 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1362 raw_spin_unlock_irq(&ctx->lock);
1364 * Reload the task pointer, it might have been changed by
1365 * a concurrent perf_event_context_sched_out().
1372 * Since we have the lock this context can't be scheduled
1373 * in, so we can change the state safely.
1375 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1376 update_group_times(event);
1377 event->state = PERF_EVENT_STATE_OFF;
1379 raw_spin_unlock_irq(&ctx->lock);
1381 EXPORT_SYMBOL_GPL(perf_event_disable);
1383 static void perf_set_shadow_time(struct perf_event *event,
1384 struct perf_event_context *ctx,
1388 * use the correct time source for the time snapshot
1390 * We could get by without this by leveraging the
1391 * fact that to get to this function, the caller
1392 * has most likely already called update_context_time()
1393 * and update_cgrp_time_xx() and thus both timestamp
1394 * are identical (or very close). Given that tstamp is,
1395 * already adjusted for cgroup, we could say that:
1396 * tstamp - ctx->timestamp
1398 * tstamp - cgrp->timestamp.
1400 * Then, in perf_output_read(), the calculation would
1401 * work with no changes because:
1402 * - event is guaranteed scheduled in
1403 * - no scheduled out in between
1404 * - thus the timestamp would be the same
1406 * But this is a bit hairy.
1408 * So instead, we have an explicit cgroup call to remain
1409 * within the time time source all along. We believe it
1410 * is cleaner and simpler to understand.
1412 if (is_cgroup_event(event))
1413 perf_cgroup_set_shadow_time(event, tstamp);
1415 event->shadow_ctx_time = tstamp - ctx->timestamp;
1418 #define MAX_INTERRUPTS (~0ULL)
1420 static void perf_log_throttle(struct perf_event *event, int enable);
1423 event_sched_in(struct perf_event *event,
1424 struct perf_cpu_context *cpuctx,
1425 struct perf_event_context *ctx)
1427 u64 tstamp = perf_event_time(event);
1429 if (event->state <= PERF_EVENT_STATE_OFF)
1432 event->state = PERF_EVENT_STATE_ACTIVE;
1433 event->oncpu = smp_processor_id();
1436 * Unthrottle events, since we scheduled we might have missed several
1437 * ticks already, also for a heavily scheduling task there is little
1438 * guarantee it'll get a tick in a timely manner.
1440 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1441 perf_log_throttle(event, 1);
1442 event->hw.interrupts = 0;
1446 * The new state must be visible before we turn it on in the hardware:
1450 if (event->pmu->add(event, PERF_EF_START)) {
1451 event->state = PERF_EVENT_STATE_INACTIVE;
1456 event->tstamp_running += tstamp - event->tstamp_stopped;
1458 perf_set_shadow_time(event, ctx, tstamp);
1460 if (!is_software_event(event))
1461 cpuctx->active_oncpu++;
1463 if (event->attr.freq && event->attr.sample_freq)
1466 if (event->attr.exclusive)
1467 cpuctx->exclusive = 1;
1473 group_sched_in(struct perf_event *group_event,
1474 struct perf_cpu_context *cpuctx,
1475 struct perf_event_context *ctx)
1477 struct perf_event *event, *partial_group = NULL;
1478 struct pmu *pmu = group_event->pmu;
1479 u64 now = ctx->time;
1480 bool simulate = false;
1482 if (group_event->state == PERF_EVENT_STATE_OFF)
1485 pmu->start_txn(pmu);
1487 if (event_sched_in(group_event, cpuctx, ctx)) {
1488 pmu->cancel_txn(pmu);
1493 * Schedule in siblings as one group (if any):
1495 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1496 if (event_sched_in(event, cpuctx, ctx)) {
1497 partial_group = event;
1502 if (!pmu->commit_txn(pmu))
1507 * Groups can be scheduled in as one unit only, so undo any
1508 * partial group before returning:
1509 * The events up to the failed event are scheduled out normally,
1510 * tstamp_stopped will be updated.
1512 * The failed events and the remaining siblings need to have
1513 * their timings updated as if they had gone thru event_sched_in()
1514 * and event_sched_out(). This is required to get consistent timings
1515 * across the group. This also takes care of the case where the group
1516 * could never be scheduled by ensuring tstamp_stopped is set to mark
1517 * the time the event was actually stopped, such that time delta
1518 * calculation in update_event_times() is correct.
1520 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1521 if (event == partial_group)
1525 event->tstamp_running += now - event->tstamp_stopped;
1526 event->tstamp_stopped = now;
1528 event_sched_out(event, cpuctx, ctx);
1531 event_sched_out(group_event, cpuctx, ctx);
1533 pmu->cancel_txn(pmu);
1539 * Work out whether we can put this event group on the CPU now.
1541 static int group_can_go_on(struct perf_event *event,
1542 struct perf_cpu_context *cpuctx,
1546 * Groups consisting entirely of software events can always go on.
1548 if (event->group_flags & PERF_GROUP_SOFTWARE)
1551 * If an exclusive group is already on, no other hardware
1554 if (cpuctx->exclusive)
1557 * If this group is exclusive and there are already
1558 * events on the CPU, it can't go on.
1560 if (event->attr.exclusive && cpuctx->active_oncpu)
1563 * Otherwise, try to add it if all previous groups were able
1569 static void add_event_to_ctx(struct perf_event *event,
1570 struct perf_event_context *ctx)
1572 u64 tstamp = perf_event_time(event);
1574 list_add_event(event, ctx);
1575 perf_group_attach(event);
1576 event->tstamp_enabled = tstamp;
1577 event->tstamp_running = tstamp;
1578 event->tstamp_stopped = tstamp;
1581 static void task_ctx_sched_out(struct perf_event_context *ctx);
1583 ctx_sched_in(struct perf_event_context *ctx,
1584 struct perf_cpu_context *cpuctx,
1585 enum event_type_t event_type,
1586 struct task_struct *task);
1588 static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1589 struct perf_event_context *ctx,
1590 struct task_struct *task)
1592 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1594 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1595 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1597 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1601 * Cross CPU call to install and enable a performance event
1603 * Must be called with ctx->mutex held
1605 static int __perf_install_in_context(void *info)
1607 struct perf_event *event = info;
1608 struct perf_event_context *ctx = event->ctx;
1609 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1610 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1611 struct task_struct *task = current;
1613 perf_ctx_lock(cpuctx, task_ctx);
1614 perf_pmu_disable(cpuctx->ctx.pmu);
1617 * If there was an active task_ctx schedule it out.
1620 task_ctx_sched_out(task_ctx);
1623 * If the context we're installing events in is not the
1624 * active task_ctx, flip them.
1626 if (ctx->task && task_ctx != ctx) {
1628 raw_spin_unlock(&task_ctx->lock);
1629 raw_spin_lock(&ctx->lock);
1634 cpuctx->task_ctx = task_ctx;
1635 task = task_ctx->task;
1638 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
1640 update_context_time(ctx);
1642 * update cgrp time only if current cgrp
1643 * matches event->cgrp. Must be done before
1644 * calling add_event_to_ctx()
1646 update_cgrp_time_from_event(event);
1648 add_event_to_ctx(event, ctx);
1651 * Schedule everything back in
1653 perf_event_sched_in(cpuctx, task_ctx, task);
1655 perf_pmu_enable(cpuctx->ctx.pmu);
1656 perf_ctx_unlock(cpuctx, task_ctx);
1662 * Attach a performance event to a context
1664 * First we add the event to the list with the hardware enable bit
1665 * in event->hw_config cleared.
1667 * If the event is attached to a task which is on a CPU we use a smp
1668 * call to enable it in the task context. The task might have been
1669 * scheduled away, but we check this in the smp call again.
1672 perf_install_in_context(struct perf_event_context *ctx,
1673 struct perf_event *event,
1676 struct task_struct *task = ctx->task;
1678 lockdep_assert_held(&ctx->mutex);
1681 if (event->cpu != -1)
1686 * Per cpu events are installed via an smp call and
1687 * the install is always successful.
1689 cpu_function_call(cpu, __perf_install_in_context, event);
1694 if (!task_function_call(task, __perf_install_in_context, event))
1697 raw_spin_lock_irq(&ctx->lock);
1699 * If we failed to find a running task, but find the context active now
1700 * that we've acquired the ctx->lock, retry.
1702 if (ctx->is_active) {
1703 raw_spin_unlock_irq(&ctx->lock);
1708 * Since the task isn't running, its safe to add the event, us holding
1709 * the ctx->lock ensures the task won't get scheduled in.
1711 add_event_to_ctx(event, ctx);
1712 raw_spin_unlock_irq(&ctx->lock);
1716 * Put a event into inactive state and update time fields.
1717 * Enabling the leader of a group effectively enables all
1718 * the group members that aren't explicitly disabled, so we
1719 * have to update their ->tstamp_enabled also.
1720 * Note: this works for group members as well as group leaders
1721 * since the non-leader members' sibling_lists will be empty.
1723 static void __perf_event_mark_enabled(struct perf_event *event)
1725 struct perf_event *sub;
1726 u64 tstamp = perf_event_time(event);
1728 event->state = PERF_EVENT_STATE_INACTIVE;
1729 event->tstamp_enabled = tstamp - event->total_time_enabled;
1730 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1731 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1732 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1737 * Cross CPU call to enable a performance event
1739 static int __perf_event_enable(void *info)
1741 struct perf_event *event = info;
1742 struct perf_event_context *ctx = event->ctx;
1743 struct perf_event *leader = event->group_leader;
1744 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1747 if (WARN_ON_ONCE(!ctx->is_active))
1750 raw_spin_lock(&ctx->lock);
1751 update_context_time(ctx);
1753 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1757 * set current task's cgroup time reference point
1759 perf_cgroup_set_timestamp(current, ctx);
1761 __perf_event_mark_enabled(event);
1763 if (!event_filter_match(event)) {
1764 if (is_cgroup_event(event))
1765 perf_cgroup_defer_enabled(event);
1770 * If the event is in a group and isn't the group leader,
1771 * then don't put it on unless the group is on.
1773 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1776 if (!group_can_go_on(event, cpuctx, 1)) {
1779 if (event == leader)
1780 err = group_sched_in(event, cpuctx, ctx);
1782 err = event_sched_in(event, cpuctx, ctx);
1787 * If this event can't go on and it's part of a
1788 * group, then the whole group has to come off.
1790 if (leader != event)
1791 group_sched_out(leader, cpuctx, ctx);
1792 if (leader->attr.pinned) {
1793 update_group_times(leader);
1794 leader->state = PERF_EVENT_STATE_ERROR;
1799 raw_spin_unlock(&ctx->lock);
1807 * If event->ctx is a cloned context, callers must make sure that
1808 * every task struct that event->ctx->task could possibly point to
1809 * remains valid. This condition is satisfied when called through
1810 * perf_event_for_each_child or perf_event_for_each as described
1811 * for perf_event_disable.
1813 void perf_event_enable(struct perf_event *event)
1815 struct perf_event_context *ctx = event->ctx;
1816 struct task_struct *task = ctx->task;
1820 * Enable the event on the cpu that it's on
1822 cpu_function_call(event->cpu, __perf_event_enable, event);
1826 raw_spin_lock_irq(&ctx->lock);
1827 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1831 * If the event is in error state, clear that first.
1832 * That way, if we see the event in error state below, we
1833 * know that it has gone back into error state, as distinct
1834 * from the task having been scheduled away before the
1835 * cross-call arrived.
1837 if (event->state == PERF_EVENT_STATE_ERROR)
1838 event->state = PERF_EVENT_STATE_OFF;
1841 if (!ctx->is_active) {
1842 __perf_event_mark_enabled(event);
1846 raw_spin_unlock_irq(&ctx->lock);
1848 if (!task_function_call(task, __perf_event_enable, event))
1851 raw_spin_lock_irq(&ctx->lock);
1854 * If the context is active and the event is still off,
1855 * we need to retry the cross-call.
1857 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1859 * task could have been flipped by a concurrent
1860 * perf_event_context_sched_out()
1867 raw_spin_unlock_irq(&ctx->lock);
1869 EXPORT_SYMBOL_GPL(perf_event_enable);
1871 int perf_event_refresh(struct perf_event *event, int refresh)
1874 * not supported on inherited events
1876 if (event->attr.inherit || !is_sampling_event(event))
1879 atomic_add(refresh, &event->event_limit);
1880 perf_event_enable(event);
1884 EXPORT_SYMBOL_GPL(perf_event_refresh);
1886 static void ctx_sched_out(struct perf_event_context *ctx,
1887 struct perf_cpu_context *cpuctx,
1888 enum event_type_t event_type)
1890 struct perf_event *event;
1891 int is_active = ctx->is_active;
1893 ctx->is_active &= ~event_type;
1894 if (likely(!ctx->nr_events))
1897 update_context_time(ctx);
1898 update_cgrp_time_from_cpuctx(cpuctx);
1899 if (!ctx->nr_active)
1902 perf_pmu_disable(ctx->pmu);
1903 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
1904 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1905 group_sched_out(event, cpuctx, ctx);
1908 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
1909 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1910 group_sched_out(event, cpuctx, ctx);
1912 perf_pmu_enable(ctx->pmu);
1916 * Test whether two contexts are equivalent, i.e. whether they
1917 * have both been cloned from the same version of the same context
1918 * and they both have the same number of enabled events.
1919 * If the number of enabled events is the same, then the set
1920 * of enabled events should be the same, because these are both
1921 * inherited contexts, therefore we can't access individual events
1922 * in them directly with an fd; we can only enable/disable all
1923 * events via prctl, or enable/disable all events in a family
1924 * via ioctl, which will have the same effect on both contexts.
1926 static int context_equiv(struct perf_event_context *ctx1,
1927 struct perf_event_context *ctx2)
1929 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1930 && ctx1->parent_gen == ctx2->parent_gen
1931 && !ctx1->pin_count && !ctx2->pin_count;
1934 static void __perf_event_sync_stat(struct perf_event *event,
1935 struct perf_event *next_event)
1939 if (!event->attr.inherit_stat)
1943 * Update the event value, we cannot use perf_event_read()
1944 * because we're in the middle of a context switch and have IRQs
1945 * disabled, which upsets smp_call_function_single(), however
1946 * we know the event must be on the current CPU, therefore we
1947 * don't need to use it.
1949 switch (event->state) {
1950 case PERF_EVENT_STATE_ACTIVE:
1951 event->pmu->read(event);
1954 case PERF_EVENT_STATE_INACTIVE:
1955 update_event_times(event);
1963 * In order to keep per-task stats reliable we need to flip the event
1964 * values when we flip the contexts.
1966 value = local64_read(&next_event->count);
1967 value = local64_xchg(&event->count, value);
1968 local64_set(&next_event->count, value);
1970 swap(event->total_time_enabled, next_event->total_time_enabled);
1971 swap(event->total_time_running, next_event->total_time_running);
1974 * Since we swizzled the values, update the user visible data too.
1976 perf_event_update_userpage(event);
1977 perf_event_update_userpage(next_event);
1980 #define list_next_entry(pos, member) \
1981 list_entry(pos->member.next, typeof(*pos), member)
1983 static void perf_event_sync_stat(struct perf_event_context *ctx,
1984 struct perf_event_context *next_ctx)
1986 struct perf_event *event, *next_event;
1991 update_context_time(ctx);
1993 event = list_first_entry(&ctx->event_list,
1994 struct perf_event, event_entry);
1996 next_event = list_first_entry(&next_ctx->event_list,
1997 struct perf_event, event_entry);
1999 while (&event->event_entry != &ctx->event_list &&
2000 &next_event->event_entry != &next_ctx->event_list) {
2002 __perf_event_sync_stat(event, next_event);
2004 event = list_next_entry(event, event_entry);
2005 next_event = list_next_entry(next_event, event_entry);
2009 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2010 struct task_struct *next)
2012 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
2013 struct perf_event_context *next_ctx;
2014 struct perf_event_context *parent;
2015 struct perf_cpu_context *cpuctx;
2021 cpuctx = __get_cpu_context(ctx);
2022 if (!cpuctx->task_ctx)
2026 parent = rcu_dereference(ctx->parent_ctx);
2027 next_ctx = next->perf_event_ctxp[ctxn];
2028 if (parent && next_ctx &&
2029 rcu_dereference(next_ctx->parent_ctx) == parent) {
2031 * Looks like the two contexts are clones, so we might be
2032 * able to optimize the context switch. We lock both
2033 * contexts and check that they are clones under the
2034 * lock (including re-checking that neither has been
2035 * uncloned in the meantime). It doesn't matter which
2036 * order we take the locks because no other cpu could
2037 * be trying to lock both of these tasks.
2039 raw_spin_lock(&ctx->lock);
2040 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
2041 if (context_equiv(ctx, next_ctx)) {
2043 * XXX do we need a memory barrier of sorts
2044 * wrt to rcu_dereference() of perf_event_ctxp
2046 task->perf_event_ctxp[ctxn] = next_ctx;
2047 next->perf_event_ctxp[ctxn] = ctx;
2049 next_ctx->task = task;
2052 perf_event_sync_stat(ctx, next_ctx);
2054 raw_spin_unlock(&next_ctx->lock);
2055 raw_spin_unlock(&ctx->lock);
2060 raw_spin_lock(&ctx->lock);
2061 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2062 cpuctx->task_ctx = NULL;
2063 raw_spin_unlock(&ctx->lock);
2067 #define for_each_task_context_nr(ctxn) \
2068 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2071 * Called from scheduler to remove the events of the current task,
2072 * with interrupts disabled.
2074 * We stop each event and update the event value in event->count.
2076 * This does not protect us against NMI, but disable()
2077 * sets the disabled bit in the control field of event _before_
2078 * accessing the event control register. If a NMI hits, then it will
2079 * not restart the event.
2081 void __perf_event_task_sched_out(struct task_struct *task,
2082 struct task_struct *next)
2086 for_each_task_context_nr(ctxn)
2087 perf_event_context_sched_out(task, ctxn, next);
2090 * if cgroup events exist on this CPU, then we need
2091 * to check if we have to switch out PMU state.
2092 * cgroup event are system-wide mode only
2094 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2095 perf_cgroup_sched_out(task, next);
2098 static void task_ctx_sched_out(struct perf_event_context *ctx)
2100 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2102 if (!cpuctx->task_ctx)
2105 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2108 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2109 cpuctx->task_ctx = NULL;
2113 * Called with IRQs disabled
2115 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2116 enum event_type_t event_type)
2118 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
2122 ctx_pinned_sched_in(struct perf_event_context *ctx,
2123 struct perf_cpu_context *cpuctx)
2125 struct perf_event *event;
2127 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2128 if (event->state <= PERF_EVENT_STATE_OFF)
2130 if (!event_filter_match(event))
2133 /* may need to reset tstamp_enabled */
2134 if (is_cgroup_event(event))
2135 perf_cgroup_mark_enabled(event, ctx);
2137 if (group_can_go_on(event, cpuctx, 1))
2138 group_sched_in(event, cpuctx, ctx);
2141 * If this pinned group hasn't been scheduled,
2142 * put it in error state.
2144 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2145 update_group_times(event);
2146 event->state = PERF_EVENT_STATE_ERROR;
2152 ctx_flexible_sched_in(struct perf_event_context *ctx,
2153 struct perf_cpu_context *cpuctx)
2155 struct perf_event *event;
2158 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2159 /* Ignore events in OFF or ERROR state */
2160 if (event->state <= PERF_EVENT_STATE_OFF)
2163 * Listen to the 'cpu' scheduling filter constraint
2166 if (!event_filter_match(event))
2169 /* may need to reset tstamp_enabled */
2170 if (is_cgroup_event(event))
2171 perf_cgroup_mark_enabled(event, ctx);
2173 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2174 if (group_sched_in(event, cpuctx, ctx))
2181 ctx_sched_in(struct perf_event_context *ctx,
2182 struct perf_cpu_context *cpuctx,
2183 enum event_type_t event_type,
2184 struct task_struct *task)
2187 int is_active = ctx->is_active;
2189 ctx->is_active |= event_type;
2190 if (likely(!ctx->nr_events))
2194 ctx->timestamp = now;
2195 perf_cgroup_set_timestamp(task, ctx);
2197 * First go through the list and put on any pinned groups
2198 * in order to give them the best chance of going on.
2200 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
2201 ctx_pinned_sched_in(ctx, cpuctx);
2203 /* Then walk through the lower prio flexible groups */
2204 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
2205 ctx_flexible_sched_in(ctx, cpuctx);
2208 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2209 enum event_type_t event_type,
2210 struct task_struct *task)
2212 struct perf_event_context *ctx = &cpuctx->ctx;
2214 ctx_sched_in(ctx, cpuctx, event_type, task);
2217 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2218 struct task_struct *task)
2220 struct perf_cpu_context *cpuctx;
2222 cpuctx = __get_cpu_context(ctx);
2223 if (cpuctx->task_ctx == ctx)
2226 perf_ctx_lock(cpuctx, ctx);
2227 perf_pmu_disable(ctx->pmu);
2229 * We want to keep the following priority order:
2230 * cpu pinned (that don't need to move), task pinned,
2231 * cpu flexible, task flexible.
2233 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2236 cpuctx->task_ctx = ctx;
2238 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2240 perf_pmu_enable(ctx->pmu);
2241 perf_ctx_unlock(cpuctx, ctx);
2244 * Since these rotations are per-cpu, we need to ensure the
2245 * cpu-context we got scheduled on is actually rotating.
2247 perf_pmu_rotate_start(ctx->pmu);
2251 * When sampling the branck stack in system-wide, it may be necessary
2252 * to flush the stack on context switch. This happens when the branch
2253 * stack does not tag its entries with the pid of the current task.
2254 * Otherwise it becomes impossible to associate a branch entry with a
2255 * task. This ambiguity is more likely to appear when the branch stack
2256 * supports priv level filtering and the user sets it to monitor only
2257 * at the user level (which could be a useful measurement in system-wide
2258 * mode). In that case, the risk is high of having a branch stack with
2259 * branch from multiple tasks. Flushing may mean dropping the existing
2260 * entries or stashing them somewhere in the PMU specific code layer.
2262 * This function provides the context switch callback to the lower code
2263 * layer. It is invoked ONLY when there is at least one system-wide context
2264 * with at least one active event using taken branch sampling.
2266 static void perf_branch_stack_sched_in(struct task_struct *prev,
2267 struct task_struct *task)
2269 struct perf_cpu_context *cpuctx;
2271 unsigned long flags;
2273 /* no need to flush branch stack if not changing task */
2277 local_irq_save(flags);
2281 list_for_each_entry_rcu(pmu, &pmus, entry) {
2282 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2285 * check if the context has at least one
2286 * event using PERF_SAMPLE_BRANCH_STACK
2288 if (cpuctx->ctx.nr_branch_stack > 0
2289 && pmu->flush_branch_stack) {
2291 pmu = cpuctx->ctx.pmu;
2293 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2295 perf_pmu_disable(pmu);
2297 pmu->flush_branch_stack();
2299 perf_pmu_enable(pmu);
2301 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2307 local_irq_restore(flags);
2311 * Called from scheduler to add the events of the current task
2312 * with interrupts disabled.
2314 * We restore the event value and then enable it.
2316 * This does not protect us against NMI, but enable()
2317 * sets the enabled bit in the control field of event _before_
2318 * accessing the event control register. If a NMI hits, then it will
2319 * keep the event running.
2321 void __perf_event_task_sched_in(struct task_struct *prev,
2322 struct task_struct *task)
2324 struct perf_event_context *ctx;
2327 for_each_task_context_nr(ctxn) {
2328 ctx = task->perf_event_ctxp[ctxn];
2332 perf_event_context_sched_in(ctx, task);
2335 * if cgroup events exist on this CPU, then we need
2336 * to check if we have to switch in PMU state.
2337 * cgroup event are system-wide mode only
2339 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2340 perf_cgroup_sched_in(prev, task);
2342 /* check for system-wide branch_stack events */
2343 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2344 perf_branch_stack_sched_in(prev, task);
2347 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2349 u64 frequency = event->attr.sample_freq;
2350 u64 sec = NSEC_PER_SEC;
2351 u64 divisor, dividend;
2353 int count_fls, nsec_fls, frequency_fls, sec_fls;
2355 count_fls = fls64(count);
2356 nsec_fls = fls64(nsec);
2357 frequency_fls = fls64(frequency);
2361 * We got @count in @nsec, with a target of sample_freq HZ
2362 * the target period becomes:
2365 * period = -------------------
2366 * @nsec * sample_freq
2371 * Reduce accuracy by one bit such that @a and @b converge
2372 * to a similar magnitude.
2374 #define REDUCE_FLS(a, b) \
2376 if (a##_fls > b##_fls) { \
2386 * Reduce accuracy until either term fits in a u64, then proceed with
2387 * the other, so that finally we can do a u64/u64 division.
2389 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2390 REDUCE_FLS(nsec, frequency);
2391 REDUCE_FLS(sec, count);
2394 if (count_fls + sec_fls > 64) {
2395 divisor = nsec * frequency;
2397 while (count_fls + sec_fls > 64) {
2398 REDUCE_FLS(count, sec);
2402 dividend = count * sec;
2404 dividend = count * sec;
2406 while (nsec_fls + frequency_fls > 64) {
2407 REDUCE_FLS(nsec, frequency);
2411 divisor = nsec * frequency;
2417 return div64_u64(dividend, divisor);
2420 static DEFINE_PER_CPU(int, perf_throttled_count);
2421 static DEFINE_PER_CPU(u64, perf_throttled_seq);
2423 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
2425 struct hw_perf_event *hwc = &event->hw;
2426 s64 period, sample_period;
2429 period = perf_calculate_period(event, nsec, count);
2431 delta = (s64)(period - hwc->sample_period);
2432 delta = (delta + 7) / 8; /* low pass filter */
2434 sample_period = hwc->sample_period + delta;
2439 hwc->sample_period = sample_period;
2441 if (local64_read(&hwc->period_left) > 8*sample_period) {
2443 event->pmu->stop(event, PERF_EF_UPDATE);
2445 local64_set(&hwc->period_left, 0);
2448 event->pmu->start(event, PERF_EF_RELOAD);
2453 * combine freq adjustment with unthrottling to avoid two passes over the
2454 * events. At the same time, make sure, having freq events does not change
2455 * the rate of unthrottling as that would introduce bias.
2457 static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2460 struct perf_event *event;
2461 struct hw_perf_event *hwc;
2462 u64 now, period = TICK_NSEC;
2466 * only need to iterate over all events iff:
2467 * - context have events in frequency mode (needs freq adjust)
2468 * - there are events to unthrottle on this cpu
2470 if (!(ctx->nr_freq || needs_unthr))
2473 raw_spin_lock(&ctx->lock);
2474 perf_pmu_disable(ctx->pmu);
2476 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2477 if (event->state != PERF_EVENT_STATE_ACTIVE)
2480 if (!event_filter_match(event))
2485 if (needs_unthr && hwc->interrupts == MAX_INTERRUPTS) {
2486 hwc->interrupts = 0;
2487 perf_log_throttle(event, 1);
2488 event->pmu->start(event, 0);
2491 if (!event->attr.freq || !event->attr.sample_freq)
2495 * stop the event and update event->count
2497 event->pmu->stop(event, PERF_EF_UPDATE);
2499 now = local64_read(&event->count);
2500 delta = now - hwc->freq_count_stamp;
2501 hwc->freq_count_stamp = now;
2505 * reload only if value has changed
2506 * we have stopped the event so tell that
2507 * to perf_adjust_period() to avoid stopping it
2511 perf_adjust_period(event, period, delta, false);
2513 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
2516 perf_pmu_enable(ctx->pmu);
2517 raw_spin_unlock(&ctx->lock);
2521 * Round-robin a context's events:
2523 static void rotate_ctx(struct perf_event_context *ctx)
2526 * Rotate the first entry last of non-pinned groups. Rotation might be
2527 * disabled by the inheritance code.
2529 if (!ctx->rotate_disable)
2530 list_rotate_left(&ctx->flexible_groups);
2534 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2535 * because they're strictly cpu affine and rotate_start is called with IRQs
2536 * disabled, while rotate_context is called from IRQ context.
2538 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
2540 struct perf_event_context *ctx = NULL;
2541 int rotate = 0, remove = 1;
2543 if (cpuctx->ctx.nr_events) {
2545 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2549 ctx = cpuctx->task_ctx;
2550 if (ctx && ctx->nr_events) {
2552 if (ctx->nr_events != ctx->nr_active)
2559 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2560 perf_pmu_disable(cpuctx->ctx.pmu);
2562 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2564 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
2566 rotate_ctx(&cpuctx->ctx);
2570 perf_event_sched_in(cpuctx, ctx, current);
2572 perf_pmu_enable(cpuctx->ctx.pmu);
2573 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2576 list_del_init(&cpuctx->rotation_list);
2579 void perf_event_task_tick(void)
2581 struct list_head *head = &__get_cpu_var(rotation_list);
2582 struct perf_cpu_context *cpuctx, *tmp;
2583 struct perf_event_context *ctx;
2586 WARN_ON(!irqs_disabled());
2588 __this_cpu_inc(perf_throttled_seq);
2589 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2591 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2593 perf_adjust_freq_unthr_context(ctx, throttled);
2595 ctx = cpuctx->task_ctx;
2597 perf_adjust_freq_unthr_context(ctx, throttled);
2599 if (cpuctx->jiffies_interval == 1 ||
2600 !(jiffies % cpuctx->jiffies_interval))
2601 perf_rotate_context(cpuctx);
2605 static int event_enable_on_exec(struct perf_event *event,
2606 struct perf_event_context *ctx)
2608 if (!event->attr.enable_on_exec)
2611 event->attr.enable_on_exec = 0;
2612 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2615 __perf_event_mark_enabled(event);
2621 * Enable all of a task's events that have been marked enable-on-exec.
2622 * This expects task == current.
2624 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2626 struct perf_event *event;
2627 unsigned long flags;
2631 local_irq_save(flags);
2632 if (!ctx || !ctx->nr_events)
2636 * We must ctxsw out cgroup events to avoid conflict
2637 * when invoking perf_task_event_sched_in() later on
2638 * in this function. Otherwise we end up trying to
2639 * ctxswin cgroup events which are already scheduled
2642 perf_cgroup_sched_out(current, NULL);
2644 raw_spin_lock(&ctx->lock);
2645 task_ctx_sched_out(ctx);
2647 list_for_each_entry(event, &ctx->event_list, event_entry) {
2648 ret = event_enable_on_exec(event, ctx);
2654 * Unclone this context if we enabled any event.
2659 raw_spin_unlock(&ctx->lock);
2662 * Also calls ctxswin for cgroup events, if any:
2664 perf_event_context_sched_in(ctx, ctx->task);
2666 local_irq_restore(flags);
2670 * Cross CPU call to read the hardware event
2672 static void __perf_event_read(void *info)
2674 struct perf_event *event = info;
2675 struct perf_event_context *ctx = event->ctx;
2676 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2679 * If this is a task context, we need to check whether it is
2680 * the current task context of this cpu. If not it has been
2681 * scheduled out before the smp call arrived. In that case
2682 * event->count would have been updated to a recent sample
2683 * when the event was scheduled out.
2685 if (ctx->task && cpuctx->task_ctx != ctx)
2688 raw_spin_lock(&ctx->lock);
2689 if (ctx->is_active) {
2690 update_context_time(ctx);
2691 update_cgrp_time_from_event(event);
2693 update_event_times(event);
2694 if (event->state == PERF_EVENT_STATE_ACTIVE)
2695 event->pmu->read(event);
2696 raw_spin_unlock(&ctx->lock);
2699 static inline u64 perf_event_count(struct perf_event *event)
2701 return local64_read(&event->count) + atomic64_read(&event->child_count);
2704 static u64 perf_event_read(struct perf_event *event)
2707 * If event is enabled and currently active on a CPU, update the
2708 * value in the event structure:
2710 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2711 smp_call_function_single(event->oncpu,
2712 __perf_event_read, event, 1);
2713 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2714 struct perf_event_context *ctx = event->ctx;
2715 unsigned long flags;
2717 raw_spin_lock_irqsave(&ctx->lock, flags);
2719 * may read while context is not active
2720 * (e.g., thread is blocked), in that case
2721 * we cannot update context time
2723 if (ctx->is_active) {
2724 update_context_time(ctx);
2725 update_cgrp_time_from_event(event);
2727 update_event_times(event);
2728 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2731 return perf_event_count(event);
2735 * Initialize the perf_event context in a task_struct:
2737 static void __perf_event_init_context(struct perf_event_context *ctx)
2739 raw_spin_lock_init(&ctx->lock);
2740 mutex_init(&ctx->mutex);
2741 INIT_LIST_HEAD(&ctx->pinned_groups);
2742 INIT_LIST_HEAD(&ctx->flexible_groups);
2743 INIT_LIST_HEAD(&ctx->event_list);
2744 atomic_set(&ctx->refcount, 1);
2747 static struct perf_event_context *
2748 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2750 struct perf_event_context *ctx;
2752 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2756 __perf_event_init_context(ctx);
2759 get_task_struct(task);
2766 static struct task_struct *
2767 find_lively_task_by_vpid(pid_t vpid)
2769 struct task_struct *task;
2776 task = find_task_by_vpid(vpid);
2778 get_task_struct(task);
2782 return ERR_PTR(-ESRCH);
2784 /* Reuse ptrace permission checks for now. */
2786 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2791 put_task_struct(task);
2792 return ERR_PTR(err);
2797 * Returns a matching context with refcount and pincount.
2799 static struct perf_event_context *
2800 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2802 struct perf_event_context *ctx;
2803 struct perf_cpu_context *cpuctx;
2804 unsigned long flags;
2808 /* Must be root to operate on a CPU event: */
2809 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2810 return ERR_PTR(-EACCES);
2813 * We could be clever and allow to attach a event to an
2814 * offline CPU and activate it when the CPU comes up, but
2817 if (!cpu_online(cpu))
2818 return ERR_PTR(-ENODEV);
2820 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2829 ctxn = pmu->task_ctx_nr;
2834 ctx = perf_lock_task_context(task, ctxn, &flags);
2838 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2840 ctx = alloc_perf_context(pmu, task);
2846 mutex_lock(&task->perf_event_mutex);
2848 * If it has already passed perf_event_exit_task().
2849 * we must see PF_EXITING, it takes this mutex too.
2851 if (task->flags & PF_EXITING)
2853 else if (task->perf_event_ctxp[ctxn])
2858 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2860 mutex_unlock(&task->perf_event_mutex);
2862 if (unlikely(err)) {
2874 return ERR_PTR(err);
2877 static void perf_event_free_filter(struct perf_event *event);
2879 static void free_event_rcu(struct rcu_head *head)
2881 struct perf_event *event;
2883 event = container_of(head, struct perf_event, rcu_head);
2885 put_pid_ns(event->ns);
2886 perf_event_free_filter(event);
2890 static void ring_buffer_put(struct ring_buffer *rb);
2892 static void free_event(struct perf_event *event)
2894 irq_work_sync(&event->pending);
2896 if (!event->parent) {
2897 if (event->attach_state & PERF_ATTACH_TASK)
2898 static_key_slow_dec_deferred(&perf_sched_events);
2899 if (event->attr.mmap || event->attr.mmap_data)
2900 atomic_dec(&nr_mmap_events);
2901 if (event->attr.comm)
2902 atomic_dec(&nr_comm_events);
2903 if (event->attr.task)
2904 atomic_dec(&nr_task_events);
2905 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2906 put_callchain_buffers();
2907 if (is_cgroup_event(event)) {
2908 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
2909 static_key_slow_dec_deferred(&perf_sched_events);
2912 if (has_branch_stack(event)) {
2913 static_key_slow_dec_deferred(&perf_sched_events);
2914 /* is system-wide event */
2915 if (!(event->attach_state & PERF_ATTACH_TASK))
2916 atomic_dec(&per_cpu(perf_branch_stack_events,
2922 ring_buffer_put(event->rb);
2926 if (is_cgroup_event(event))
2927 perf_detach_cgroup(event);
2930 event->destroy(event);
2933 put_ctx(event->ctx);
2935 call_rcu(&event->rcu_head, free_event_rcu);
2938 int perf_event_release_kernel(struct perf_event *event)
2940 struct perf_event_context *ctx = event->ctx;
2942 WARN_ON_ONCE(ctx->parent_ctx);
2944 * There are two ways this annotation is useful:
2946 * 1) there is a lock recursion from perf_event_exit_task
2947 * see the comment there.
2949 * 2) there is a lock-inversion with mmap_sem through
2950 * perf_event_read_group(), which takes faults while
2951 * holding ctx->mutex, however this is called after
2952 * the last filedesc died, so there is no possibility
2953 * to trigger the AB-BA case.
2955 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2956 raw_spin_lock_irq(&ctx->lock);
2957 perf_group_detach(event);
2958 raw_spin_unlock_irq(&ctx->lock);
2959 perf_remove_from_context(event);
2960 mutex_unlock(&ctx->mutex);
2966 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2969 * Called when the last reference to the file is gone.
2971 static void put_event(struct perf_event *event)
2973 struct task_struct *owner;
2975 if (!atomic_long_dec_and_test(&event->refcount))
2979 owner = ACCESS_ONCE(event->owner);
2981 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2982 * !owner it means the list deletion is complete and we can indeed
2983 * free this event, otherwise we need to serialize on
2984 * owner->perf_event_mutex.
2986 smp_read_barrier_depends();
2989 * Since delayed_put_task_struct() also drops the last
2990 * task reference we can safely take a new reference
2991 * while holding the rcu_read_lock().
2993 get_task_struct(owner);
2998 mutex_lock(&owner->perf_event_mutex);
3000 * We have to re-check the event->owner field, if it is cleared
3001 * we raced with perf_event_exit_task(), acquiring the mutex
3002 * ensured they're done, and we can proceed with freeing the
3006 list_del_init(&event->owner_entry);
3007 mutex_unlock(&owner->perf_event_mutex);
3008 put_task_struct(owner);
3011 perf_event_release_kernel(event);
3014 static int perf_release(struct inode *inode, struct file *file)
3016 put_event(file->private_data);
3020 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
3022 struct perf_event *child;
3028 mutex_lock(&event->child_mutex);
3029 total += perf_event_read(event);
3030 *enabled += event->total_time_enabled +
3031 atomic64_read(&event->child_total_time_enabled);
3032 *running += event->total_time_running +
3033 atomic64_read(&event->child_total_time_running);
3035 list_for_each_entry(child, &event->child_list, child_list) {
3036 total += perf_event_read(child);
3037 *enabled += child->total_time_enabled;
3038 *running += child->total_time_running;
3040 mutex_unlock(&event->child_mutex);
3044 EXPORT_SYMBOL_GPL(perf_event_read_value);
3046 static int perf_event_read_group(struct perf_event *event,
3047 u64 read_format, char __user *buf)
3049 struct perf_event *leader = event->group_leader, *sub;
3050 int n = 0, size = 0, ret = -EFAULT;
3051 struct perf_event_context *ctx = leader->ctx;
3053 u64 count, enabled, running;
3055 mutex_lock(&ctx->mutex);
3056 count = perf_event_read_value(leader, &enabled, &running);
3058 values[n++] = 1 + leader->nr_siblings;
3059 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3060 values[n++] = enabled;
3061 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3062 values[n++] = running;
3063 values[n++] = count;
3064 if (read_format & PERF_FORMAT_ID)
3065 values[n++] = primary_event_id(leader);
3067 size = n * sizeof(u64);
3069 if (copy_to_user(buf, values, size))
3074 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3077 values[n++] = perf_event_read_value(sub, &enabled, &running);
3078 if (read_format & PERF_FORMAT_ID)
3079 values[n++] = primary_event_id(sub);
3081 size = n * sizeof(u64);
3083 if (copy_to_user(buf + ret, values, size)) {
3091 mutex_unlock(&ctx->mutex);
3096 static int perf_event_read_one(struct perf_event *event,
3097 u64 read_format, char __user *buf)
3099 u64 enabled, running;
3103 values[n++] = perf_event_read_value(event, &enabled, &running);
3104 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3105 values[n++] = enabled;
3106 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3107 values[n++] = running;
3108 if (read_format & PERF_FORMAT_ID)
3109 values[n++] = primary_event_id(event);
3111 if (copy_to_user(buf, values, n * sizeof(u64)))
3114 return n * sizeof(u64);
3118 * Read the performance event - simple non blocking version for now
3121 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3123 u64 read_format = event->attr.read_format;
3127 * Return end-of-file for a read on a event that is in
3128 * error state (i.e. because it was pinned but it couldn't be
3129 * scheduled on to the CPU at some point).
3131 if (event->state == PERF_EVENT_STATE_ERROR)
3134 if (count < event->read_size)
3137 WARN_ON_ONCE(event->ctx->parent_ctx);
3138 if (read_format & PERF_FORMAT_GROUP)
3139 ret = perf_event_read_group(event, read_format, buf);
3141 ret = perf_event_read_one(event, read_format, buf);
3147 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3149 struct perf_event *event = file->private_data;
3151 return perf_read_hw(event, buf, count);
3154 static unsigned int perf_poll(struct file *file, poll_table *wait)
3156 struct perf_event *event = file->private_data;
3157 struct ring_buffer *rb;
3158 unsigned int events = POLL_HUP;
3161 * Race between perf_event_set_output() and perf_poll(): perf_poll()
3162 * grabs the rb reference but perf_event_set_output() overrides it.
3163 * Here is the timeline for two threads T1, T2:
3164 * t0: T1, rb = rcu_dereference(event->rb)
3165 * t1: T2, old_rb = event->rb
3166 * t2: T2, event->rb = new rb
3167 * t3: T2, ring_buffer_detach(old_rb)
3168 * t4: T1, ring_buffer_attach(rb1)
3169 * t5: T1, poll_wait(event->waitq)
3171 * To avoid this problem, we grab mmap_mutex in perf_poll()
3172 * thereby ensuring that the assignment of the new ring buffer
3173 * and the detachment of the old buffer appear atomic to perf_poll()
3175 mutex_lock(&event->mmap_mutex);
3178 rb = rcu_dereference(event->rb);
3180 ring_buffer_attach(event, rb);
3181 events = atomic_xchg(&rb->poll, 0);
3185 mutex_unlock(&event->mmap_mutex);
3187 poll_wait(file, &event->waitq, wait);
3192 static void perf_event_reset(struct perf_event *event)
3194 (void)perf_event_read(event);
3195 local64_set(&event->count, 0);
3196 perf_event_update_userpage(event);
3200 * Holding the top-level event's child_mutex means that any
3201 * descendant process that has inherited this event will block
3202 * in sync_child_event if it goes to exit, thus satisfying the
3203 * task existence requirements of perf_event_enable/disable.
3205 static void perf_event_for_each_child(struct perf_event *event,
3206 void (*func)(struct perf_event *))
3208 struct perf_event *child;
3210 WARN_ON_ONCE(event->ctx->parent_ctx);
3211 mutex_lock(&event->child_mutex);
3213 list_for_each_entry(child, &event->child_list, child_list)
3215 mutex_unlock(&event->child_mutex);
3218 static void perf_event_for_each(struct perf_event *event,
3219 void (*func)(struct perf_event *))
3221 struct perf_event_context *ctx = event->ctx;
3222 struct perf_event *sibling;
3224 WARN_ON_ONCE(ctx->parent_ctx);
3225 mutex_lock(&ctx->mutex);
3226 event = event->group_leader;
3228 perf_event_for_each_child(event, func);
3229 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3230 perf_event_for_each_child(sibling, func);
3231 mutex_unlock(&ctx->mutex);
3234 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3236 struct perf_event_context *ctx = event->ctx;
3240 if (!is_sampling_event(event))
3243 if (copy_from_user(&value, arg, sizeof(value)))
3249 raw_spin_lock_irq(&ctx->lock);
3250 if (event->attr.freq) {
3251 if (value > sysctl_perf_event_sample_rate) {
3256 event->attr.sample_freq = value;
3258 event->attr.sample_period = value;
3259 event->hw.sample_period = value;
3262 raw_spin_unlock_irq(&ctx->lock);
3267 static const struct file_operations perf_fops;
3269 static inline int perf_fget_light(int fd, struct fd *p)
3271 struct fd f = fdget(fd);
3275 if (f.file->f_op != &perf_fops) {
3283 static int perf_event_set_output(struct perf_event *event,
3284 struct perf_event *output_event);
3285 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3287 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3289 struct perf_event *event = file->private_data;
3290 void (*func)(struct perf_event *);
3294 case PERF_EVENT_IOC_ENABLE:
3295 func = perf_event_enable;
3297 case PERF_EVENT_IOC_DISABLE:
3298 func = perf_event_disable;
3300 case PERF_EVENT_IOC_RESET:
3301 func = perf_event_reset;
3304 case PERF_EVENT_IOC_REFRESH:
3305 return perf_event_refresh(event, arg);
3307 case PERF_EVENT_IOC_PERIOD:
3308 return perf_event_period(event, (u64 __user *)arg);
3310 case PERF_EVENT_IOC_SET_OUTPUT:
3314 struct perf_event *output_event;
3316 ret = perf_fget_light(arg, &output);
3319 output_event = output.file->private_data;
3320 ret = perf_event_set_output(event, output_event);
3323 ret = perf_event_set_output(event, NULL);
3328 case PERF_EVENT_IOC_SET_FILTER:
3329 return perf_event_set_filter(event, (void __user *)arg);
3335 if (flags & PERF_IOC_FLAG_GROUP)
3336 perf_event_for_each(event, func);
3338 perf_event_for_each_child(event, func);
3343 int perf_event_task_enable(void)
3345 struct perf_event *event;
3347 mutex_lock(¤t->perf_event_mutex);
3348 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3349 perf_event_for_each_child(event, perf_event_enable);
3350 mutex_unlock(¤t->perf_event_mutex);
3355 int perf_event_task_disable(void)
3357 struct perf_event *event;
3359 mutex_lock(¤t->perf_event_mutex);
3360 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3361 perf_event_for_each_child(event, perf_event_disable);
3362 mutex_unlock(¤t->perf_event_mutex);
3367 static int perf_event_index(struct perf_event *event)
3369 if (event->hw.state & PERF_HES_STOPPED)
3372 if (event->state != PERF_EVENT_STATE_ACTIVE)
3375 return event->pmu->event_idx(event);
3378 static void calc_timer_values(struct perf_event *event,
3385 *now = perf_clock();
3386 ctx_time = event->shadow_ctx_time + *now;
3387 *enabled = ctx_time - event->tstamp_enabled;
3388 *running = ctx_time - event->tstamp_running;
3391 void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
3396 * Callers need to ensure there can be no nesting of this function, otherwise
3397 * the seqlock logic goes bad. We can not serialize this because the arch
3398 * code calls this from NMI context.
3400 void perf_event_update_userpage(struct perf_event *event)
3402 struct perf_event_mmap_page *userpg;
3403 struct ring_buffer *rb;
3404 u64 enabled, running, now;
3408 * compute total_time_enabled, total_time_running
3409 * based on snapshot values taken when the event
3410 * was last scheduled in.
3412 * we cannot simply called update_context_time()
3413 * because of locking issue as we can be called in
3416 calc_timer_values(event, &now, &enabled, &running);
3417 rb = rcu_dereference(event->rb);
3421 userpg = rb->user_page;
3424 * Disable preemption so as to not let the corresponding user-space
3425 * spin too long if we get preempted.
3430 userpg->index = perf_event_index(event);
3431 userpg->offset = perf_event_count(event);
3433 userpg->offset -= local64_read(&event->hw.prev_count);
3435 userpg->time_enabled = enabled +
3436 atomic64_read(&event->child_total_time_enabled);
3438 userpg->time_running = running +
3439 atomic64_read(&event->child_total_time_running);
3441 arch_perf_update_userpage(userpg, now);
3450 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3452 struct perf_event *event = vma->vm_file->private_data;
3453 struct ring_buffer *rb;
3454 int ret = VM_FAULT_SIGBUS;
3456 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3457 if (vmf->pgoff == 0)
3463 rb = rcu_dereference(event->rb);
3467 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3470 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
3474 get_page(vmf->page);
3475 vmf->page->mapping = vma->vm_file->f_mapping;
3476 vmf->page->index = vmf->pgoff;
3485 static void ring_buffer_attach(struct perf_event *event,
3486 struct ring_buffer *rb)
3488 unsigned long flags;
3490 if (!list_empty(&event->rb_entry))
3493 spin_lock_irqsave(&rb->event_lock, flags);
3494 if (!list_empty(&event->rb_entry))
3497 list_add(&event->rb_entry, &rb->event_list);
3499 spin_unlock_irqrestore(&rb->event_lock, flags);
3502 static void ring_buffer_detach(struct perf_event *event,
3503 struct ring_buffer *rb)
3505 unsigned long flags;
3507 if (list_empty(&event->rb_entry))
3510 spin_lock_irqsave(&rb->event_lock, flags);
3511 list_del_init(&event->rb_entry);
3512 wake_up_all(&event->waitq);
3513 spin_unlock_irqrestore(&rb->event_lock, flags);
3516 static void ring_buffer_wakeup(struct perf_event *event)
3518 struct ring_buffer *rb;
3521 rb = rcu_dereference(event->rb);
3525 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3526 wake_up_all(&event->waitq);
3532 static void rb_free_rcu(struct rcu_head *rcu_head)
3534 struct ring_buffer *rb;
3536 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3540 static struct ring_buffer *ring_buffer_get(struct perf_event *event)
3542 struct ring_buffer *rb;
3545 rb = rcu_dereference(event->rb);
3547 if (!atomic_inc_not_zero(&rb->refcount))
3555 static void ring_buffer_put(struct ring_buffer *rb)
3557 struct perf_event *event, *n;
3558 unsigned long flags;
3560 if (!atomic_dec_and_test(&rb->refcount))
3563 spin_lock_irqsave(&rb->event_lock, flags);
3564 list_for_each_entry_safe(event, n, &rb->event_list, rb_entry) {
3565 list_del_init(&event->rb_entry);
3566 wake_up_all(&event->waitq);
3568 spin_unlock_irqrestore(&rb->event_lock, flags);
3570 call_rcu(&rb->rcu_head, rb_free_rcu);
3573 static void perf_mmap_open(struct vm_area_struct *vma)
3575 struct perf_event *event = vma->vm_file->private_data;
3577 atomic_inc(&event->mmap_count);
3580 static void perf_mmap_close(struct vm_area_struct *vma)
3582 struct perf_event *event = vma->vm_file->private_data;
3584 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
3585 unsigned long size = perf_data_size(event->rb);
3586 struct user_struct *user = event->mmap_user;
3587 struct ring_buffer *rb = event->rb;
3589 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
3590 vma->vm_mm->pinned_vm -= event->mmap_locked;
3591 rcu_assign_pointer(event->rb, NULL);
3592 ring_buffer_detach(event, rb);
3593 mutex_unlock(&event->mmap_mutex);
3595 ring_buffer_put(rb);
3600 static const struct vm_operations_struct perf_mmap_vmops = {
3601 .open = perf_mmap_open,
3602 .close = perf_mmap_close,
3603 .fault = perf_mmap_fault,
3604 .page_mkwrite = perf_mmap_fault,
3607 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3609 struct perf_event *event = file->private_data;
3610 unsigned long user_locked, user_lock_limit;
3611 struct user_struct *user = current_user();
3612 unsigned long locked, lock_limit;
3613 struct ring_buffer *rb;
3614 unsigned long vma_size;
3615 unsigned long nr_pages;
3616 long user_extra, extra;
3617 int ret = 0, flags = 0;
3620 * Don't allow mmap() of inherited per-task counters. This would
3621 * create a performance issue due to all children writing to the
3624 if (event->cpu == -1 && event->attr.inherit)
3627 if (!(vma->vm_flags & VM_SHARED))
3630 vma_size = vma->vm_end - vma->vm_start;
3631 nr_pages = (vma_size / PAGE_SIZE) - 1;
3634 * If we have rb pages ensure they're a power-of-two number, so we
3635 * can do bitmasks instead of modulo.
3637 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3640 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3643 if (vma->vm_pgoff != 0)
3646 WARN_ON_ONCE(event->ctx->parent_ctx);
3647 mutex_lock(&event->mmap_mutex);
3649 if (event->rb->nr_pages == nr_pages)
3650 atomic_inc(&event->rb->refcount);
3656 user_extra = nr_pages + 1;
3657 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3660 * Increase the limit linearly with more CPUs:
3662 user_lock_limit *= num_online_cpus();
3664 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3667 if (user_locked > user_lock_limit)
3668 extra = user_locked - user_lock_limit;
3670 lock_limit = rlimit(RLIMIT_MEMLOCK);
3671 lock_limit >>= PAGE_SHIFT;
3672 locked = vma->vm_mm->pinned_vm + extra;
3674 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3675 !capable(CAP_IPC_LOCK)) {
3682 if (vma->vm_flags & VM_WRITE)
3683 flags |= RING_BUFFER_WRITABLE;
3685 rb = rb_alloc(nr_pages,
3686 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3693 rcu_assign_pointer(event->rb, rb);
3695 atomic_long_add(user_extra, &user->locked_vm);
3696 event->mmap_locked = extra;
3697 event->mmap_user = get_current_user();
3698 vma->vm_mm->pinned_vm += event->mmap_locked;
3700 perf_event_update_userpage(event);
3704 atomic_inc(&event->mmap_count);
3705 mutex_unlock(&event->mmap_mutex);
3707 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3708 vma->vm_ops = &perf_mmap_vmops;
3713 static int perf_fasync(int fd, struct file *filp, int on)
3715 struct inode *inode = file_inode(filp);
3716 struct perf_event *event = filp->private_data;
3719 mutex_lock(&inode->i_mutex);
3720 retval = fasync_helper(fd, filp, on, &event->fasync);
3721 mutex_unlock(&inode->i_mutex);
3729 static const struct file_operations perf_fops = {
3730 .llseek = no_llseek,
3731 .release = perf_release,
3734 .unlocked_ioctl = perf_ioctl,
3735 .compat_ioctl = perf_ioctl,
3737 .fasync = perf_fasync,
3743 * If there's data, ensure we set the poll() state and publish everything
3744 * to user-space before waking everybody up.
3747 void perf_event_wakeup(struct perf_event *event)
3749 ring_buffer_wakeup(event);
3751 if (event->pending_kill) {
3752 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3753 event->pending_kill = 0;
3757 static void perf_pending_event(struct irq_work *entry)
3759 struct perf_event *event = container_of(entry,
3760 struct perf_event, pending);
3762 if (event->pending_disable) {
3763 event->pending_disable = 0;
3764 __perf_event_disable(event);
3767 if (event->pending_wakeup) {
3768 event->pending_wakeup = 0;
3769 perf_event_wakeup(event);
3774 * We assume there is only KVM supporting the callbacks.
3775 * Later on, we might change it to a list if there is
3776 * another virtualization implementation supporting the callbacks.
3778 struct perf_guest_info_callbacks *perf_guest_cbs;
3780 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3782 perf_guest_cbs = cbs;
3785 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3787 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3789 perf_guest_cbs = NULL;
3792 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3795 perf_output_sample_regs(struct perf_output_handle *handle,
3796 struct pt_regs *regs, u64 mask)
3800 for_each_set_bit(bit, (const unsigned long *) &mask,
3801 sizeof(mask) * BITS_PER_BYTE) {
3804 val = perf_reg_value(regs, bit);
3805 perf_output_put(handle, val);
3809 static void perf_sample_regs_user(struct perf_regs_user *regs_user,
3810 struct pt_regs *regs)
3812 if (!user_mode(regs)) {
3814 regs = task_pt_regs(current);
3820 regs_user->regs = regs;
3821 regs_user->abi = perf_reg_abi(current);
3826 * Get remaining task size from user stack pointer.
3828 * It'd be better to take stack vma map and limit this more
3829 * precisly, but there's no way to get it safely under interrupt,
3830 * so using TASK_SIZE as limit.
3832 static u64 perf_ustack_task_size(struct pt_regs *regs)
3834 unsigned long addr = perf_user_stack_pointer(regs);
3836 if (!addr || addr >= TASK_SIZE)
3839 return TASK_SIZE - addr;
3843 perf_sample_ustack_size(u16 stack_size, u16 header_size,
3844 struct pt_regs *regs)
3848 /* No regs, no stack pointer, no dump. */
3853 * Check if we fit in with the requested stack size into the:
3855 * If we don't, we limit the size to the TASK_SIZE.
3857 * - remaining sample size
3858 * If we don't, we customize the stack size to
3859 * fit in to the remaining sample size.
3862 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
3863 stack_size = min(stack_size, (u16) task_size);
3865 /* Current header size plus static size and dynamic size. */
3866 header_size += 2 * sizeof(u64);
3868 /* Do we fit in with the current stack dump size? */
3869 if ((u16) (header_size + stack_size) < header_size) {
3871 * If we overflow the maximum size for the sample,
3872 * we customize the stack dump size to fit in.
3874 stack_size = USHRT_MAX - header_size - sizeof(u64);
3875 stack_size = round_up(stack_size, sizeof(u64));
3882 perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
3883 struct pt_regs *regs)
3885 /* Case of a kernel thread, nothing to dump */
3888 perf_output_put(handle, size);
3897 * - the size requested by user or the best one we can fit
3898 * in to the sample max size
3900 * - user stack dump data
3902 * - the actual dumped size
3906 perf_output_put(handle, dump_size);
3909 sp = perf_user_stack_pointer(regs);
3910 rem = __output_copy_user(handle, (void *) sp, dump_size);
3911 dyn_size = dump_size - rem;
3913 perf_output_skip(handle, rem);
3916 perf_output_put(handle, dyn_size);
3920 static void __perf_event_header__init_id(struct perf_event_header *header,
3921 struct perf_sample_data *data,
3922 struct perf_event *event)
3924 u64 sample_type = event->attr.sample_type;
3926 data->type = sample_type;
3927 header->size += event->id_header_size;
3929 if (sample_type & PERF_SAMPLE_TID) {
3930 /* namespace issues */
3931 data->tid_entry.pid = perf_event_pid(event, current);
3932 data->tid_entry.tid = perf_event_tid(event, current);
3935 if (sample_type & PERF_SAMPLE_TIME)
3936 data->time = perf_clock();
3938 if (sample_type & PERF_SAMPLE_ID)
3939 data->id = primary_event_id(event);
3941 if (sample_type & PERF_SAMPLE_STREAM_ID)
3942 data->stream_id = event->id;
3944 if (sample_type & PERF_SAMPLE_CPU) {
3945 data->cpu_entry.cpu = raw_smp_processor_id();
3946 data->cpu_entry.reserved = 0;
3950 void perf_event_header__init_id(struct perf_event_header *header,
3951 struct perf_sample_data *data,
3952 struct perf_event *event)
3954 if (event->attr.sample_id_all)
3955 __perf_event_header__init_id(header, data, event);
3958 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3959 struct perf_sample_data *data)
3961 u64 sample_type = data->type;
3963 if (sample_type & PERF_SAMPLE_TID)
3964 perf_output_put(handle, data->tid_entry);
3966 if (sample_type & PERF_SAMPLE_TIME)
3967 perf_output_put(handle, data->time);
3969 if (sample_type & PERF_SAMPLE_ID)
3970 perf_output_put(handle, data->id);
3972 if (sample_type & PERF_SAMPLE_STREAM_ID)
3973 perf_output_put(handle, data->stream_id);
3975 if (sample_type & PERF_SAMPLE_CPU)
3976 perf_output_put(handle, data->cpu_entry);
3979 void perf_event__output_id_sample(struct perf_event *event,
3980 struct perf_output_handle *handle,
3981 struct perf_sample_data *sample)
3983 if (event->attr.sample_id_all)
3984 __perf_event__output_id_sample(handle, sample);
3987 static void perf_output_read_one(struct perf_output_handle *handle,
3988 struct perf_event *event,
3989 u64 enabled, u64 running)
3991 u64 read_format = event->attr.read_format;
3995 values[n++] = perf_event_count(event);
3996 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3997 values[n++] = enabled +
3998 atomic64_read(&event->child_total_time_enabled);
4000 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4001 values[n++] = running +
4002 atomic64_read(&event->child_total_time_running);
4004 if (read_format & PERF_FORMAT_ID)
4005 values[n++] = primary_event_id(event);
4007 __output_copy(handle, values, n * sizeof(u64));
4011 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4013 static void perf_output_read_group(struct perf_output_handle *handle,
4014 struct perf_event *event,
4015 u64 enabled, u64 running)
4017 struct perf_event *leader = event->group_leader, *sub;
4018 u64 read_format = event->attr.read_format;
4022 values[n++] = 1 + leader->nr_siblings;
4024 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4025 values[n++] = enabled;
4027 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4028 values[n++] = running;
4030 if (leader != event)
4031 leader->pmu->read(leader);
4033 values[n++] = perf_event_count(leader);
4034 if (read_format & PERF_FORMAT_ID)
4035 values[n++] = primary_event_id(leader);
4037 __output_copy(handle, values, n * sizeof(u64));
4039 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4043 sub->pmu->read(sub);
4045 values[n++] = perf_event_count(sub);
4046 if (read_format & PERF_FORMAT_ID)
4047 values[n++] = primary_event_id(sub);
4049 __output_copy(handle, values, n * sizeof(u64));
4053 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4054 PERF_FORMAT_TOTAL_TIME_RUNNING)
4056 static void perf_output_read(struct perf_output_handle *handle,
4057 struct perf_event *event)
4059 u64 enabled = 0, running = 0, now;
4060 u64 read_format = event->attr.read_format;
4063 * compute total_time_enabled, total_time_running
4064 * based on snapshot values taken when the event
4065 * was last scheduled in.
4067 * we cannot simply called update_context_time()
4068 * because of locking issue as we are called in
4071 if (read_format & PERF_FORMAT_TOTAL_TIMES)
4072 calc_timer_values(event, &now, &enabled, &running);
4074 if (event->attr.read_format & PERF_FORMAT_GROUP)
4075 perf_output_read_group(handle, event, enabled, running);
4077 perf_output_read_one(handle, event, enabled, running);
4080 void perf_output_sample(struct perf_output_handle *handle,
4081 struct perf_event_header *header,
4082 struct perf_sample_data *data,
4083 struct perf_event *event)
4085 u64 sample_type = data->type;
4087 perf_output_put(handle, *header);
4089 if (sample_type & PERF_SAMPLE_IP)
4090 perf_output_put(handle, data->ip);
4092 if (sample_type & PERF_SAMPLE_TID)
4093 perf_output_put(handle, data->tid_entry);
4095 if (sample_type & PERF_SAMPLE_TIME)
4096 perf_output_put(handle, data->time);
4098 if (sample_type & PERF_SAMPLE_ADDR)
4099 perf_output_put(handle, data->addr);
4101 if (sample_type & PERF_SAMPLE_ID)
4102 perf_output_put(handle, data->id);
4104 if (sample_type & PERF_SAMPLE_STREAM_ID)
4105 perf_output_put(handle, data->stream_id);
4107 if (sample_type & PERF_SAMPLE_CPU)
4108 perf_output_put(handle, data->cpu_entry);
4110 if (sample_type & PERF_SAMPLE_PERIOD)
4111 perf_output_put(handle, data->period);
4113 if (sample_type & PERF_SAMPLE_READ)
4114 perf_output_read(handle, event);
4116 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4117 if (data->callchain) {
4120 if (data->callchain)
4121 size += data->callchain->nr;
4123 size *= sizeof(u64);
4125 __output_copy(handle, data->callchain, size);
4128 perf_output_put(handle, nr);
4132 if (sample_type & PERF_SAMPLE_RAW) {
4134 perf_output_put(handle, data->raw->size);
4135 __output_copy(handle, data->raw->data,
4142 .size = sizeof(u32),
4145 perf_output_put(handle, raw);
4149 if (!event->attr.watermark) {
4150 int wakeup_events = event->attr.wakeup_events;
4152 if (wakeup_events) {
4153 struct ring_buffer *rb = handle->rb;
4154 int events = local_inc_return(&rb->events);
4156 if (events >= wakeup_events) {
4157 local_sub(wakeup_events, &rb->events);
4158 local_inc(&rb->wakeup);
4163 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4164 if (data->br_stack) {
4167 size = data->br_stack->nr
4168 * sizeof(struct perf_branch_entry);
4170 perf_output_put(handle, data->br_stack->nr);
4171 perf_output_copy(handle, data->br_stack->entries, size);
4174 * we always store at least the value of nr
4177 perf_output_put(handle, nr);
4181 if (sample_type & PERF_SAMPLE_REGS_USER) {
4182 u64 abi = data->regs_user.abi;
4185 * If there are no regs to dump, notice it through
4186 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4188 perf_output_put(handle, abi);
4191 u64 mask = event->attr.sample_regs_user;
4192 perf_output_sample_regs(handle,
4193 data->regs_user.regs,
4198 if (sample_type & PERF_SAMPLE_STACK_USER)
4199 perf_output_sample_ustack(handle,
4200 data->stack_user_size,
4201 data->regs_user.regs);
4203 if (sample_type & PERF_SAMPLE_WEIGHT)
4204 perf_output_put(handle, data->weight);
4206 if (sample_type & PERF_SAMPLE_DATA_SRC)
4207 perf_output_put(handle, data->data_src.val);
4210 void perf_prepare_sample(struct perf_event_header *header,
4211 struct perf_sample_data *data,
4212 struct perf_event *event,
4213 struct pt_regs *regs)
4215 u64 sample_type = event->attr.sample_type;
4217 header->type = PERF_RECORD_SAMPLE;
4218 header->size = sizeof(*header) + event->header_size;
4221 header->misc |= perf_misc_flags(regs);
4223 __perf_event_header__init_id(header, data, event);
4225 if (sample_type & PERF_SAMPLE_IP)
4226 data->ip = perf_instruction_pointer(regs);
4228 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4231 data->callchain = perf_callchain(event, regs);
4233 if (data->callchain)
4234 size += data->callchain->nr;
4236 header->size += size * sizeof(u64);
4239 if (sample_type & PERF_SAMPLE_RAW) {
4240 int size = sizeof(u32);
4243 size += data->raw->size;
4245 size += sizeof(u32);
4247 WARN_ON_ONCE(size & (sizeof(u64)-1));
4248 header->size += size;
4251 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4252 int size = sizeof(u64); /* nr */
4253 if (data->br_stack) {
4254 size += data->br_stack->nr
4255 * sizeof(struct perf_branch_entry);
4257 header->size += size;
4260 if (sample_type & PERF_SAMPLE_REGS_USER) {
4261 /* regs dump ABI info */
4262 int size = sizeof(u64);
4264 perf_sample_regs_user(&data->regs_user, regs);
4266 if (data->regs_user.regs) {
4267 u64 mask = event->attr.sample_regs_user;
4268 size += hweight64(mask) * sizeof(u64);
4271 header->size += size;
4274 if (sample_type & PERF_SAMPLE_STACK_USER) {
4276 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4277 * processed as the last one or have additional check added
4278 * in case new sample type is added, because we could eat
4279 * up the rest of the sample size.
4281 struct perf_regs_user *uregs = &data->regs_user;
4282 u16 stack_size = event->attr.sample_stack_user;
4283 u16 size = sizeof(u64);
4286 perf_sample_regs_user(uregs, regs);
4288 stack_size = perf_sample_ustack_size(stack_size, header->size,
4292 * If there is something to dump, add space for the dump
4293 * itself and for the field that tells the dynamic size,
4294 * which is how many have been actually dumped.
4297 size += sizeof(u64) + stack_size;
4299 data->stack_user_size = stack_size;
4300 header->size += size;
4304 static void perf_event_output(struct perf_event *event,
4305 struct perf_sample_data *data,
4306 struct pt_regs *regs)
4308 struct perf_output_handle handle;
4309 struct perf_event_header header;
4311 /* protect the callchain buffers */
4314 perf_prepare_sample(&header, data, event, regs);
4316 if (perf_output_begin(&handle, event, header.size))
4319 perf_output_sample(&handle, &header, data, event);
4321 perf_output_end(&handle);
4331 struct perf_read_event {
4332 struct perf_event_header header;
4339 perf_event_read_event(struct perf_event *event,
4340 struct task_struct *task)
4342 struct perf_output_handle handle;
4343 struct perf_sample_data sample;
4344 struct perf_read_event read_event = {
4346 .type = PERF_RECORD_READ,
4348 .size = sizeof(read_event) + event->read_size,
4350 .pid = perf_event_pid(event, task),
4351 .tid = perf_event_tid(event, task),
4355 perf_event_header__init_id(&read_event.header, &sample, event);
4356 ret = perf_output_begin(&handle, event, read_event.header.size);
4360 perf_output_put(&handle, read_event);
4361 perf_output_read(&handle, event);
4362 perf_event__output_id_sample(event, &handle, &sample);
4364 perf_output_end(&handle);
4368 * task tracking -- fork/exit
4370 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4373 struct perf_task_event {
4374 struct task_struct *task;
4375 struct perf_event_context *task_ctx;
4378 struct perf_event_header header;
4388 static void perf_event_task_output(struct perf_event *event,
4389 struct perf_task_event *task_event)
4391 struct perf_output_handle handle;
4392 struct perf_sample_data sample;
4393 struct task_struct *task = task_event->task;
4394 int ret, size = task_event->event_id.header.size;
4396 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4398 ret = perf_output_begin(&handle, event,
4399 task_event->event_id.header.size);
4403 task_event->event_id.pid = perf_event_pid(event, task);
4404 task_event->event_id.ppid = perf_event_pid(event, current);
4406 task_event->event_id.tid = perf_event_tid(event, task);
4407 task_event->event_id.ptid = perf_event_tid(event, current);
4409 perf_output_put(&handle, task_event->event_id);
4411 perf_event__output_id_sample(event, &handle, &sample);
4413 perf_output_end(&handle);
4415 task_event->event_id.header.size = size;
4418 static int perf_event_task_match(struct perf_event *event)
4420 if (event->state < PERF_EVENT_STATE_INACTIVE)
4423 if (!event_filter_match(event))
4426 if (event->attr.comm || event->attr.mmap ||
4427 event->attr.mmap_data || event->attr.task)
4433 static void perf_event_task_ctx(struct perf_event_context *ctx,
4434 struct perf_task_event *task_event)
4436 struct perf_event *event;
4438 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4439 if (perf_event_task_match(event))
4440 perf_event_task_output(event, task_event);
4444 static void perf_event_task_event(struct perf_task_event *task_event)
4446 struct perf_cpu_context *cpuctx;
4447 struct perf_event_context *ctx;
4452 list_for_each_entry_rcu(pmu, &pmus, entry) {
4453 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4454 if (cpuctx->unique_pmu != pmu)
4456 perf_event_task_ctx(&cpuctx->ctx, task_event);
4458 ctx = task_event->task_ctx;
4460 ctxn = pmu->task_ctx_nr;
4463 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4465 perf_event_task_ctx(ctx, task_event);
4468 put_cpu_ptr(pmu->pmu_cpu_context);
4470 if (task_event->task_ctx)
4471 perf_event_task_ctx(task_event->task_ctx, task_event);
4476 static void perf_event_task(struct task_struct *task,
4477 struct perf_event_context *task_ctx,
4480 struct perf_task_event task_event;
4482 if (!atomic_read(&nr_comm_events) &&
4483 !atomic_read(&nr_mmap_events) &&
4484 !atomic_read(&nr_task_events))
4487 task_event = (struct perf_task_event){
4489 .task_ctx = task_ctx,
4492 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4494 .size = sizeof(task_event.event_id),
4500 .time = perf_clock(),
4504 perf_event_task_event(&task_event);
4507 void perf_event_fork(struct task_struct *task)
4509 perf_event_task(task, NULL, 1);
4516 struct perf_comm_event {
4517 struct task_struct *task;
4522 struct perf_event_header header;
4529 static void perf_event_comm_output(struct perf_event *event,
4530 struct perf_comm_event *comm_event)
4532 struct perf_output_handle handle;
4533 struct perf_sample_data sample;
4534 int size = comm_event->event_id.header.size;
4537 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4538 ret = perf_output_begin(&handle, event,
4539 comm_event->event_id.header.size);
4544 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4545 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4547 perf_output_put(&handle, comm_event->event_id);
4548 __output_copy(&handle, comm_event->comm,
4549 comm_event->comm_size);
4551 perf_event__output_id_sample(event, &handle, &sample);
4553 perf_output_end(&handle);
4555 comm_event->event_id.header.size = size;
4558 static int perf_event_comm_match(struct perf_event *event)
4560 if (event->state < PERF_EVENT_STATE_INACTIVE)
4563 if (!event_filter_match(event))
4566 if (event->attr.comm)
4572 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4573 struct perf_comm_event *comm_event)
4575 struct perf_event *event;
4577 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4578 if (perf_event_comm_match(event))
4579 perf_event_comm_output(event, comm_event);
4583 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4585 struct perf_cpu_context *cpuctx;
4586 struct perf_event_context *ctx;
4587 char comm[TASK_COMM_LEN];
4592 memset(comm, 0, sizeof(comm));
4593 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4594 size = ALIGN(strlen(comm)+1, sizeof(u64));
4596 comm_event->comm = comm;
4597 comm_event->comm_size = size;
4599 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4601 list_for_each_entry_rcu(pmu, &pmus, entry) {
4602 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4603 if (cpuctx->unique_pmu != pmu)
4605 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4607 ctxn = pmu->task_ctx_nr;
4611 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4613 perf_event_comm_ctx(ctx, comm_event);
4615 put_cpu_ptr(pmu->pmu_cpu_context);
4620 void perf_event_comm(struct task_struct *task)
4622 struct perf_comm_event comm_event;
4623 struct perf_event_context *ctx;
4626 for_each_task_context_nr(ctxn) {
4627 ctx = task->perf_event_ctxp[ctxn];
4631 perf_event_enable_on_exec(ctx);
4634 if (!atomic_read(&nr_comm_events))
4637 comm_event = (struct perf_comm_event){
4643 .type = PERF_RECORD_COMM,
4652 perf_event_comm_event(&comm_event);
4659 struct perf_mmap_event {
4660 struct vm_area_struct *vma;
4662 const char *file_name;
4666 struct perf_event_header header;
4676 static void perf_event_mmap_output(struct perf_event *event,
4677 struct perf_mmap_event *mmap_event)
4679 struct perf_output_handle handle;
4680 struct perf_sample_data sample;
4681 int size = mmap_event->event_id.header.size;
4684 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4685 ret = perf_output_begin(&handle, event,
4686 mmap_event->event_id.header.size);
4690 mmap_event->event_id.pid = perf_event_pid(event, current);
4691 mmap_event->event_id.tid = perf_event_tid(event, current);
4693 perf_output_put(&handle, mmap_event->event_id);
4694 __output_copy(&handle, mmap_event->file_name,
4695 mmap_event->file_size);
4697 perf_event__output_id_sample(event, &handle, &sample);
4699 perf_output_end(&handle);
4701 mmap_event->event_id.header.size = size;
4704 static int perf_event_mmap_match(struct perf_event *event,
4705 struct perf_mmap_event *mmap_event,
4708 if (event->state < PERF_EVENT_STATE_INACTIVE)
4711 if (!event_filter_match(event))
4714 if ((!executable && event->attr.mmap_data) ||
4715 (executable && event->attr.mmap))
4721 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4722 struct perf_mmap_event *mmap_event,
4725 struct perf_event *event;
4727 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4728 if (perf_event_mmap_match(event, mmap_event, executable))
4729 perf_event_mmap_output(event, mmap_event);
4733 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4735 struct perf_cpu_context *cpuctx;
4736 struct perf_event_context *ctx;
4737 struct vm_area_struct *vma = mmap_event->vma;
4738 struct file *file = vma->vm_file;
4746 memset(tmp, 0, sizeof(tmp));
4750 * d_path works from the end of the rb backwards, so we
4751 * need to add enough zero bytes after the string to handle
4752 * the 64bit alignment we do later.
4754 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4756 name = strncpy(tmp, "//enomem", sizeof(tmp));
4759 name = d_path(&file->f_path, buf, PATH_MAX);
4761 name = strncpy(tmp, "//toolong", sizeof(tmp));
4765 if (arch_vma_name(mmap_event->vma)) {
4766 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4768 tmp[sizeof(tmp) - 1] = '\0';
4773 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4775 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4776 vma->vm_end >= vma->vm_mm->brk) {
4777 name = strncpy(tmp, "[heap]", sizeof(tmp));
4779 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4780 vma->vm_end >= vma->vm_mm->start_stack) {
4781 name = strncpy(tmp, "[stack]", sizeof(tmp));
4785 name = strncpy(tmp, "//anon", sizeof(tmp));
4790 size = ALIGN(strlen(name)+1, sizeof(u64));
4792 mmap_event->file_name = name;
4793 mmap_event->file_size = size;
4795 if (!(vma->vm_flags & VM_EXEC))
4796 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
4798 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4801 list_for_each_entry_rcu(pmu, &pmus, entry) {
4802 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4803 if (cpuctx->unique_pmu != pmu)
4805 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4806 vma->vm_flags & VM_EXEC);
4808 ctxn = pmu->task_ctx_nr;
4812 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4814 perf_event_mmap_ctx(ctx, mmap_event,
4815 vma->vm_flags & VM_EXEC);
4818 put_cpu_ptr(pmu->pmu_cpu_context);
4825 void perf_event_mmap(struct vm_area_struct *vma)
4827 struct perf_mmap_event mmap_event;
4829 if (!atomic_read(&nr_mmap_events))
4832 mmap_event = (struct perf_mmap_event){
4838 .type = PERF_RECORD_MMAP,
4839 .misc = PERF_RECORD_MISC_USER,
4844 .start = vma->vm_start,
4845 .len = vma->vm_end - vma->vm_start,
4846 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4850 perf_event_mmap_event(&mmap_event);
4854 * IRQ throttle logging
4857 static void perf_log_throttle(struct perf_event *event, int enable)
4859 struct perf_output_handle handle;
4860 struct perf_sample_data sample;
4864 struct perf_event_header header;
4868 } throttle_event = {
4870 .type = PERF_RECORD_THROTTLE,
4872 .size = sizeof(throttle_event),
4874 .time = perf_clock(),
4875 .id = primary_event_id(event),
4876 .stream_id = event->id,
4880 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4882 perf_event_header__init_id(&throttle_event.header, &sample, event);
4884 ret = perf_output_begin(&handle, event,
4885 throttle_event.header.size);
4889 perf_output_put(&handle, throttle_event);
4890 perf_event__output_id_sample(event, &handle, &sample);
4891 perf_output_end(&handle);
4895 * Generic event overflow handling, sampling.
4898 static int __perf_event_overflow(struct perf_event *event,
4899 int throttle, struct perf_sample_data *data,
4900 struct pt_regs *regs)
4902 int events = atomic_read(&event->event_limit);
4903 struct hw_perf_event *hwc = &event->hw;
4908 * Non-sampling counters might still use the PMI to fold short
4909 * hardware counters, ignore those.
4911 if (unlikely(!is_sampling_event(event)))
4914 seq = __this_cpu_read(perf_throttled_seq);
4915 if (seq != hwc->interrupts_seq) {
4916 hwc->interrupts_seq = seq;
4917 hwc->interrupts = 1;
4920 if (unlikely(throttle
4921 && hwc->interrupts >= max_samples_per_tick)) {
4922 __this_cpu_inc(perf_throttled_count);
4923 hwc->interrupts = MAX_INTERRUPTS;
4924 perf_log_throttle(event, 0);
4929 if (event->attr.freq) {
4930 u64 now = perf_clock();
4931 s64 delta = now - hwc->freq_time_stamp;
4933 hwc->freq_time_stamp = now;
4935 if (delta > 0 && delta < 2*TICK_NSEC)
4936 perf_adjust_period(event, delta, hwc->last_period, true);
4940 * XXX event_limit might not quite work as expected on inherited
4944 event->pending_kill = POLL_IN;
4945 if (events && atomic_dec_and_test(&event->event_limit)) {
4947 event->pending_kill = POLL_HUP;
4948 event->pending_disable = 1;
4949 irq_work_queue(&event->pending);
4952 if (event->overflow_handler)
4953 event->overflow_handler(event, data, regs);
4955 perf_event_output(event, data, regs);
4957 if (event->fasync && event->pending_kill) {
4958 event->pending_wakeup = 1;
4959 irq_work_queue(&event->pending);
4965 int perf_event_overflow(struct perf_event *event,
4966 struct perf_sample_data *data,
4967 struct pt_regs *regs)
4969 return __perf_event_overflow(event, 1, data, regs);
4973 * Generic software event infrastructure
4976 struct swevent_htable {
4977 struct swevent_hlist *swevent_hlist;
4978 struct mutex hlist_mutex;
4981 /* Recursion avoidance in each contexts */
4982 int recursion[PERF_NR_CONTEXTS];
4985 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4988 * We directly increment event->count and keep a second value in
4989 * event->hw.period_left to count intervals. This period event
4990 * is kept in the range [-sample_period, 0] so that we can use the
4994 static u64 perf_swevent_set_period(struct perf_event *event)
4996 struct hw_perf_event *hwc = &event->hw;
4997 u64 period = hwc->last_period;
5001 hwc->last_period = hwc->sample_period;
5004 old = val = local64_read(&hwc->period_left);
5008 nr = div64_u64(period + val, period);
5009 offset = nr * period;
5011 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
5017 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
5018 struct perf_sample_data *data,
5019 struct pt_regs *regs)
5021 struct hw_perf_event *hwc = &event->hw;
5025 overflow = perf_swevent_set_period(event);
5027 if (hwc->interrupts == MAX_INTERRUPTS)
5030 for (; overflow; overflow--) {
5031 if (__perf_event_overflow(event, throttle,
5034 * We inhibit the overflow from happening when
5035 * hwc->interrupts == MAX_INTERRUPTS.
5043 static void perf_swevent_event(struct perf_event *event, u64 nr,
5044 struct perf_sample_data *data,
5045 struct pt_regs *regs)
5047 struct hw_perf_event *hwc = &event->hw;
5049 local64_add(nr, &event->count);
5054 if (!is_sampling_event(event))
5057 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5059 return perf_swevent_overflow(event, 1, data, regs);
5061 data->period = event->hw.last_period;
5063 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
5064 return perf_swevent_overflow(event, 1, data, regs);
5066 if (local64_add_negative(nr, &hwc->period_left))
5069 perf_swevent_overflow(event, 0, data, regs);
5072 static int perf_exclude_event(struct perf_event *event,
5073 struct pt_regs *regs)
5075 if (event->hw.state & PERF_HES_STOPPED)
5079 if (event->attr.exclude_user && user_mode(regs))
5082 if (event->attr.exclude_kernel && !user_mode(regs))
5089 static int perf_swevent_match(struct perf_event *event,
5090 enum perf_type_id type,
5092 struct perf_sample_data *data,
5093 struct pt_regs *regs)
5095 if (event->attr.type != type)
5098 if (event->attr.config != event_id)
5101 if (perf_exclude_event(event, regs))
5107 static inline u64 swevent_hash(u64 type, u32 event_id)
5109 u64 val = event_id | (type << 32);
5111 return hash_64(val, SWEVENT_HLIST_BITS);
5114 static inline struct hlist_head *
5115 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
5117 u64 hash = swevent_hash(type, event_id);
5119 return &hlist->heads[hash];
5122 /* For the read side: events when they trigger */
5123 static inline struct hlist_head *
5124 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
5126 struct swevent_hlist *hlist;
5128 hlist = rcu_dereference(swhash->swevent_hlist);
5132 return __find_swevent_head(hlist, type, event_id);
5135 /* For the event head insertion and removal in the hlist */
5136 static inline struct hlist_head *
5137 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
5139 struct swevent_hlist *hlist;
5140 u32 event_id = event->attr.config;
5141 u64 type = event->attr.type;
5144 * Event scheduling is always serialized against hlist allocation
5145 * and release. Which makes the protected version suitable here.
5146 * The context lock guarantees that.
5148 hlist = rcu_dereference_protected(swhash->swevent_hlist,
5149 lockdep_is_held(&event->ctx->lock));
5153 return __find_swevent_head(hlist, type, event_id);
5156 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5158 struct perf_sample_data *data,
5159 struct pt_regs *regs)
5161 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5162 struct perf_event *event;
5163 struct hlist_head *head;
5166 head = find_swevent_head_rcu(swhash, type, event_id);
5170 hlist_for_each_entry_rcu(event, head, hlist_entry) {
5171 if (perf_swevent_match(event, type, event_id, data, regs))
5172 perf_swevent_event(event, nr, data, regs);
5178 int perf_swevent_get_recursion_context(void)
5180 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5182 return get_recursion_context(swhash->recursion);
5184 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
5186 inline void perf_swevent_put_recursion_context(int rctx)
5188 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5190 put_recursion_context(swhash->recursion, rctx);
5193 void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
5195 struct perf_sample_data data;
5198 preempt_disable_notrace();
5199 rctx = perf_swevent_get_recursion_context();
5203 perf_sample_data_init(&data, addr, 0);
5205 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
5207 perf_swevent_put_recursion_context(rctx);
5208 preempt_enable_notrace();
5211 static void perf_swevent_read(struct perf_event *event)
5215 static int perf_swevent_add(struct perf_event *event, int flags)
5217 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5218 struct hw_perf_event *hwc = &event->hw;
5219 struct hlist_head *head;
5221 if (is_sampling_event(event)) {
5222 hwc->last_period = hwc->sample_period;
5223 perf_swevent_set_period(event);
5226 hwc->state = !(flags & PERF_EF_START);
5228 head = find_swevent_head(swhash, event);
5229 if (WARN_ON_ONCE(!head))
5232 hlist_add_head_rcu(&event->hlist_entry, head);
5237 static void perf_swevent_del(struct perf_event *event, int flags)
5239 hlist_del_rcu(&event->hlist_entry);
5242 static void perf_swevent_start(struct perf_event *event, int flags)
5244 event->hw.state = 0;
5247 static void perf_swevent_stop(struct perf_event *event, int flags)
5249 event->hw.state = PERF_HES_STOPPED;
5252 /* Deref the hlist from the update side */
5253 static inline struct swevent_hlist *
5254 swevent_hlist_deref(struct swevent_htable *swhash)
5256 return rcu_dereference_protected(swhash->swevent_hlist,
5257 lockdep_is_held(&swhash->hlist_mutex));
5260 static void swevent_hlist_release(struct swevent_htable *swhash)
5262 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
5267 rcu_assign_pointer(swhash->swevent_hlist, NULL);
5268 kfree_rcu(hlist, rcu_head);
5271 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5273 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5275 mutex_lock(&swhash->hlist_mutex);
5277 if (!--swhash->hlist_refcount)
5278 swevent_hlist_release(swhash);
5280 mutex_unlock(&swhash->hlist_mutex);
5283 static void swevent_hlist_put(struct perf_event *event)
5287 if (event->cpu != -1) {
5288 swevent_hlist_put_cpu(event, event->cpu);
5292 for_each_possible_cpu(cpu)
5293 swevent_hlist_put_cpu(event, cpu);
5296 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5298 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5301 mutex_lock(&swhash->hlist_mutex);
5303 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
5304 struct swevent_hlist *hlist;
5306 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5311 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5313 swhash->hlist_refcount++;
5315 mutex_unlock(&swhash->hlist_mutex);
5320 static int swevent_hlist_get(struct perf_event *event)
5323 int cpu, failed_cpu;
5325 if (event->cpu != -1)
5326 return swevent_hlist_get_cpu(event, event->cpu);
5329 for_each_possible_cpu(cpu) {
5330 err = swevent_hlist_get_cpu(event, cpu);
5340 for_each_possible_cpu(cpu) {
5341 if (cpu == failed_cpu)
5343 swevent_hlist_put_cpu(event, cpu);
5350 struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
5352 static void sw_perf_event_destroy(struct perf_event *event)
5354 u64 event_id = event->attr.config;
5356 WARN_ON(event->parent);
5358 static_key_slow_dec(&perf_swevent_enabled[event_id]);
5359 swevent_hlist_put(event);
5362 static int perf_swevent_init(struct perf_event *event)
5364 u64 event_id = event->attr.config;
5366 if (event->attr.type != PERF_TYPE_SOFTWARE)
5370 * no branch sampling for software events
5372 if (has_branch_stack(event))
5376 case PERF_COUNT_SW_CPU_CLOCK:
5377 case PERF_COUNT_SW_TASK_CLOCK:
5384 if (event_id >= PERF_COUNT_SW_MAX)
5387 if (!event->parent) {
5390 err = swevent_hlist_get(event);
5394 static_key_slow_inc(&perf_swevent_enabled[event_id]);
5395 event->destroy = sw_perf_event_destroy;
5401 static int perf_swevent_event_idx(struct perf_event *event)
5406 static struct pmu perf_swevent = {
5407 .task_ctx_nr = perf_sw_context,
5409 .event_init = perf_swevent_init,
5410 .add = perf_swevent_add,
5411 .del = perf_swevent_del,
5412 .start = perf_swevent_start,
5413 .stop = perf_swevent_stop,
5414 .read = perf_swevent_read,
5416 .event_idx = perf_swevent_event_idx,
5419 #ifdef CONFIG_EVENT_TRACING
5421 static int perf_tp_filter_match(struct perf_event *event,
5422 struct perf_sample_data *data)
5424 void *record = data->raw->data;
5426 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5431 static int perf_tp_event_match(struct perf_event *event,
5432 struct perf_sample_data *data,
5433 struct pt_regs *regs)
5435 if (event->hw.state & PERF_HES_STOPPED)
5438 * All tracepoints are from kernel-space.
5440 if (event->attr.exclude_kernel)
5443 if (!perf_tp_filter_match(event, data))
5449 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5450 struct pt_regs *regs, struct hlist_head *head, int rctx,
5451 struct task_struct *task)
5453 struct perf_sample_data data;
5454 struct perf_event *event;
5456 struct perf_raw_record raw = {
5461 perf_sample_data_init(&data, addr, 0);
5464 hlist_for_each_entry_rcu(event, head, hlist_entry) {
5465 if (perf_tp_event_match(event, &data, regs))
5466 perf_swevent_event(event, count, &data, regs);
5470 * If we got specified a target task, also iterate its context and
5471 * deliver this event there too.
5473 if (task && task != current) {
5474 struct perf_event_context *ctx;
5475 struct trace_entry *entry = record;
5478 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
5482 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5483 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5485 if (event->attr.config != entry->type)
5487 if (perf_tp_event_match(event, &data, regs))
5488 perf_swevent_event(event, count, &data, regs);
5494 perf_swevent_put_recursion_context(rctx);
5496 EXPORT_SYMBOL_GPL(perf_tp_event);
5498 static void tp_perf_event_destroy(struct perf_event *event)
5500 perf_trace_destroy(event);
5503 static int perf_tp_event_init(struct perf_event *event)
5507 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5511 * no branch sampling for tracepoint events
5513 if (has_branch_stack(event))
5516 err = perf_trace_init(event);
5520 event->destroy = tp_perf_event_destroy;
5525 static struct pmu perf_tracepoint = {
5526 .task_ctx_nr = perf_sw_context,
5528 .event_init = perf_tp_event_init,
5529 .add = perf_trace_add,
5530 .del = perf_trace_del,
5531 .start = perf_swevent_start,
5532 .stop = perf_swevent_stop,
5533 .read = perf_swevent_read,
5535 .event_idx = perf_swevent_event_idx,
5538 static inline void perf_tp_register(void)
5540 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5543 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5548 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5551 filter_str = strndup_user(arg, PAGE_SIZE);
5552 if (IS_ERR(filter_str))
5553 return PTR_ERR(filter_str);
5555 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5561 static void perf_event_free_filter(struct perf_event *event)
5563 ftrace_profile_free_filter(event);
5568 static inline void perf_tp_register(void)
5572 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5577 static void perf_event_free_filter(struct perf_event *event)
5581 #endif /* CONFIG_EVENT_TRACING */
5583 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5584 void perf_bp_event(struct perf_event *bp, void *data)
5586 struct perf_sample_data sample;
5587 struct pt_regs *regs = data;
5589 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
5591 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5592 perf_swevent_event(bp, 1, &sample, regs);
5597 * hrtimer based swevent callback
5600 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5602 enum hrtimer_restart ret = HRTIMER_RESTART;
5603 struct perf_sample_data data;
5604 struct pt_regs *regs;
5605 struct perf_event *event;
5608 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5610 if (event->state != PERF_EVENT_STATE_ACTIVE)
5611 return HRTIMER_NORESTART;
5613 event->pmu->read(event);
5615 perf_sample_data_init(&data, 0, event->hw.last_period);
5616 regs = get_irq_regs();
5618 if (regs && !perf_exclude_event(event, regs)) {
5619 if (!(event->attr.exclude_idle && is_idle_task(current)))
5620 if (__perf_event_overflow(event, 1, &data, regs))
5621 ret = HRTIMER_NORESTART;
5624 period = max_t(u64, 10000, event->hw.sample_period);
5625 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5630 static void perf_swevent_start_hrtimer(struct perf_event *event)
5632 struct hw_perf_event *hwc = &event->hw;
5635 if (!is_sampling_event(event))
5638 period = local64_read(&hwc->period_left);
5643 local64_set(&hwc->period_left, 0);
5645 period = max_t(u64, 10000, hwc->sample_period);
5647 __hrtimer_start_range_ns(&hwc->hrtimer,
5648 ns_to_ktime(period), 0,
5649 HRTIMER_MODE_REL_PINNED, 0);
5652 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5654 struct hw_perf_event *hwc = &event->hw;
5656 if (is_sampling_event(event)) {
5657 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5658 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5660 hrtimer_cancel(&hwc->hrtimer);
5664 static void perf_swevent_init_hrtimer(struct perf_event *event)
5666 struct hw_perf_event *hwc = &event->hw;
5668 if (!is_sampling_event(event))
5671 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5672 hwc->hrtimer.function = perf_swevent_hrtimer;
5675 * Since hrtimers have a fixed rate, we can do a static freq->period
5676 * mapping and avoid the whole period adjust feedback stuff.
5678 if (event->attr.freq) {
5679 long freq = event->attr.sample_freq;
5681 event->attr.sample_period = NSEC_PER_SEC / freq;
5682 hwc->sample_period = event->attr.sample_period;
5683 local64_set(&hwc->period_left, hwc->sample_period);
5684 hwc->last_period = hwc->sample_period;
5685 event->attr.freq = 0;
5690 * Software event: cpu wall time clock
5693 static void cpu_clock_event_update(struct perf_event *event)
5698 now = local_clock();
5699 prev = local64_xchg(&event->hw.prev_count, now);
5700 local64_add(now - prev, &event->count);
5703 static void cpu_clock_event_start(struct perf_event *event, int flags)
5705 local64_set(&event->hw.prev_count, local_clock());
5706 perf_swevent_start_hrtimer(event);
5709 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5711 perf_swevent_cancel_hrtimer(event);
5712 cpu_clock_event_update(event);
5715 static int cpu_clock_event_add(struct perf_event *event, int flags)
5717 if (flags & PERF_EF_START)
5718 cpu_clock_event_start(event, flags);
5723 static void cpu_clock_event_del(struct perf_event *event, int flags)
5725 cpu_clock_event_stop(event, flags);
5728 static void cpu_clock_event_read(struct perf_event *event)
5730 cpu_clock_event_update(event);
5733 static int cpu_clock_event_init(struct perf_event *event)
5735 if (event->attr.type != PERF_TYPE_SOFTWARE)
5738 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5742 * no branch sampling for software events
5744 if (has_branch_stack(event))
5747 perf_swevent_init_hrtimer(event);
5752 static struct pmu perf_cpu_clock = {
5753 .task_ctx_nr = perf_sw_context,
5755 .event_init = cpu_clock_event_init,
5756 .add = cpu_clock_event_add,
5757 .del = cpu_clock_event_del,
5758 .start = cpu_clock_event_start,
5759 .stop = cpu_clock_event_stop,
5760 .read = cpu_clock_event_read,
5762 .event_idx = perf_swevent_event_idx,
5766 * Software event: task time clock
5769 static void task_clock_event_update(struct perf_event *event, u64 now)
5774 prev = local64_xchg(&event->hw.prev_count, now);
5776 local64_add(delta, &event->count);
5779 static void task_clock_event_start(struct perf_event *event, int flags)
5781 local64_set(&event->hw.prev_count, event->ctx->time);
5782 perf_swevent_start_hrtimer(event);
5785 static void task_clock_event_stop(struct perf_event *event, int flags)
5787 perf_swevent_cancel_hrtimer(event);
5788 task_clock_event_update(event, event->ctx->time);
5791 static int task_clock_event_add(struct perf_event *event, int flags)
5793 if (flags & PERF_EF_START)
5794 task_clock_event_start(event, flags);
5799 static void task_clock_event_del(struct perf_event *event, int flags)
5801 task_clock_event_stop(event, PERF_EF_UPDATE);
5804 static void task_clock_event_read(struct perf_event *event)
5806 u64 now = perf_clock();
5807 u64 delta = now - event->ctx->timestamp;
5808 u64 time = event->ctx->time + delta;
5810 task_clock_event_update(event, time);
5813 static int task_clock_event_init(struct perf_event *event)
5815 if (event->attr.type != PERF_TYPE_SOFTWARE)
5818 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5822 * no branch sampling for software events
5824 if (has_branch_stack(event))
5827 perf_swevent_init_hrtimer(event);
5832 static struct pmu perf_task_clock = {
5833 .task_ctx_nr = perf_sw_context,
5835 .event_init = task_clock_event_init,
5836 .add = task_clock_event_add,
5837 .del = task_clock_event_del,
5838 .start = task_clock_event_start,
5839 .stop = task_clock_event_stop,
5840 .read = task_clock_event_read,
5842 .event_idx = perf_swevent_event_idx,
5845 static void perf_pmu_nop_void(struct pmu *pmu)
5849 static int perf_pmu_nop_int(struct pmu *pmu)
5854 static void perf_pmu_start_txn(struct pmu *pmu)
5856 perf_pmu_disable(pmu);
5859 static int perf_pmu_commit_txn(struct pmu *pmu)
5861 perf_pmu_enable(pmu);
5865 static void perf_pmu_cancel_txn(struct pmu *pmu)
5867 perf_pmu_enable(pmu);
5870 static int perf_event_idx_default(struct perf_event *event)
5872 return event->hw.idx + 1;
5876 * Ensures all contexts with the same task_ctx_nr have the same
5877 * pmu_cpu_context too.
5879 static void *find_pmu_context(int ctxn)
5886 list_for_each_entry(pmu, &pmus, entry) {
5887 if (pmu->task_ctx_nr == ctxn)
5888 return pmu->pmu_cpu_context;
5894 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5898 for_each_possible_cpu(cpu) {
5899 struct perf_cpu_context *cpuctx;
5901 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5903 if (cpuctx->unique_pmu == old_pmu)
5904 cpuctx->unique_pmu = pmu;
5908 static void free_pmu_context(struct pmu *pmu)
5912 mutex_lock(&pmus_lock);
5914 * Like a real lame refcount.
5916 list_for_each_entry(i, &pmus, entry) {
5917 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5918 update_pmu_context(i, pmu);
5923 free_percpu(pmu->pmu_cpu_context);
5925 mutex_unlock(&pmus_lock);
5927 static struct idr pmu_idr;
5930 type_show(struct device *dev, struct device_attribute *attr, char *page)
5932 struct pmu *pmu = dev_get_drvdata(dev);
5934 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5937 static struct device_attribute pmu_dev_attrs[] = {
5942 static int pmu_bus_running;
5943 static struct bus_type pmu_bus = {
5944 .name = "event_source",
5945 .dev_attrs = pmu_dev_attrs,
5948 static void pmu_dev_release(struct device *dev)
5953 static int pmu_dev_alloc(struct pmu *pmu)
5957 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5961 pmu->dev->groups = pmu->attr_groups;
5962 device_initialize(pmu->dev);
5963 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5967 dev_set_drvdata(pmu->dev, pmu);
5968 pmu->dev->bus = &pmu_bus;
5969 pmu->dev->release = pmu_dev_release;
5970 ret = device_add(pmu->dev);
5978 put_device(pmu->dev);
5982 static struct lock_class_key cpuctx_mutex;
5983 static struct lock_class_key cpuctx_lock;
5985 int perf_pmu_register(struct pmu *pmu, char *name, int type)
5989 mutex_lock(&pmus_lock);
5991 pmu->pmu_disable_count = alloc_percpu(int);
5992 if (!pmu->pmu_disable_count)
6001 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6009 if (pmu_bus_running) {
6010 ret = pmu_dev_alloc(pmu);
6016 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6017 if (pmu->pmu_cpu_context)
6018 goto got_cpu_context;
6021 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6022 if (!pmu->pmu_cpu_context)
6025 for_each_possible_cpu(cpu) {
6026 struct perf_cpu_context *cpuctx;
6028 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6029 __perf_event_init_context(&cpuctx->ctx);
6030 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
6031 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
6032 cpuctx->ctx.type = cpu_context;
6033 cpuctx->ctx.pmu = pmu;
6034 cpuctx->jiffies_interval = 1;
6035 INIT_LIST_HEAD(&cpuctx->rotation_list);
6036 cpuctx->unique_pmu = pmu;
6040 if (!pmu->start_txn) {
6041 if (pmu->pmu_enable) {
6043 * If we have pmu_enable/pmu_disable calls, install
6044 * transaction stubs that use that to try and batch
6045 * hardware accesses.
6047 pmu->start_txn = perf_pmu_start_txn;
6048 pmu->commit_txn = perf_pmu_commit_txn;
6049 pmu->cancel_txn = perf_pmu_cancel_txn;
6051 pmu->start_txn = perf_pmu_nop_void;
6052 pmu->commit_txn = perf_pmu_nop_int;
6053 pmu->cancel_txn = perf_pmu_nop_void;
6057 if (!pmu->pmu_enable) {
6058 pmu->pmu_enable = perf_pmu_nop_void;
6059 pmu->pmu_disable = perf_pmu_nop_void;
6062 if (!pmu->event_idx)
6063 pmu->event_idx = perf_event_idx_default;
6065 list_add_rcu(&pmu->entry, &pmus);
6068 mutex_unlock(&pmus_lock);
6073 device_del(pmu->dev);
6074 put_device(pmu->dev);
6077 if (pmu->type >= PERF_TYPE_MAX)
6078 idr_remove(&pmu_idr, pmu->type);
6081 free_percpu(pmu->pmu_disable_count);
6085 void perf_pmu_unregister(struct pmu *pmu)
6087 mutex_lock(&pmus_lock);
6088 list_del_rcu(&pmu->entry);
6089 mutex_unlock(&pmus_lock);
6092 * We dereference the pmu list under both SRCU and regular RCU, so
6093 * synchronize against both of those.
6095 synchronize_srcu(&pmus_srcu);
6098 free_percpu(pmu->pmu_disable_count);
6099 if (pmu->type >= PERF_TYPE_MAX)
6100 idr_remove(&pmu_idr, pmu->type);
6101 device_del(pmu->dev);
6102 put_device(pmu->dev);
6103 free_pmu_context(pmu);
6106 struct pmu *perf_init_event(struct perf_event *event)
6108 struct pmu *pmu = NULL;
6112 idx = srcu_read_lock(&pmus_srcu);
6115 pmu = idr_find(&pmu_idr, event->attr.type);
6119 ret = pmu->event_init(event);
6125 list_for_each_entry_rcu(pmu, &pmus, entry) {
6127 ret = pmu->event_init(event);
6131 if (ret != -ENOENT) {
6136 pmu = ERR_PTR(-ENOENT);
6138 srcu_read_unlock(&pmus_srcu, idx);
6144 * Allocate and initialize a event structure
6146 static struct perf_event *
6147 perf_event_alloc(struct perf_event_attr *attr, int cpu,
6148 struct task_struct *task,
6149 struct perf_event *group_leader,
6150 struct perf_event *parent_event,
6151 perf_overflow_handler_t overflow_handler,
6155 struct perf_event *event;
6156 struct hw_perf_event *hwc;
6159 if ((unsigned)cpu >= nr_cpu_ids) {
6160 if (!task || cpu != -1)
6161 return ERR_PTR(-EINVAL);
6164 event = kzalloc(sizeof(*event), GFP_KERNEL);
6166 return ERR_PTR(-ENOMEM);
6169 * Single events are their own group leaders, with an
6170 * empty sibling list:
6173 group_leader = event;
6175 mutex_init(&event->child_mutex);
6176 INIT_LIST_HEAD(&event->child_list);
6178 INIT_LIST_HEAD(&event->group_entry);
6179 INIT_LIST_HEAD(&event->event_entry);
6180 INIT_LIST_HEAD(&event->sibling_list);
6181 INIT_LIST_HEAD(&event->rb_entry);
6183 init_waitqueue_head(&event->waitq);
6184 init_irq_work(&event->pending, perf_pending_event);
6186 mutex_init(&event->mmap_mutex);
6188 atomic_long_set(&event->refcount, 1);
6190 event->attr = *attr;
6191 event->group_leader = group_leader;
6195 event->parent = parent_event;
6197 event->ns = get_pid_ns(task_active_pid_ns(current));
6198 event->id = atomic64_inc_return(&perf_event_id);
6200 event->state = PERF_EVENT_STATE_INACTIVE;
6203 event->attach_state = PERF_ATTACH_TASK;
6205 if (attr->type == PERF_TYPE_TRACEPOINT)
6206 event->hw.tp_target = task;
6207 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6209 * hw_breakpoint is a bit difficult here..
6211 else if (attr->type == PERF_TYPE_BREAKPOINT)
6212 event->hw.bp_target = task;
6216 if (!overflow_handler && parent_event) {
6217 overflow_handler = parent_event->overflow_handler;
6218 context = parent_event->overflow_handler_context;
6221 event->overflow_handler = overflow_handler;
6222 event->overflow_handler_context = context;
6224 perf_event__state_init(event);
6229 hwc->sample_period = attr->sample_period;
6230 if (attr->freq && attr->sample_freq)
6231 hwc->sample_period = 1;
6232 hwc->last_period = hwc->sample_period;
6234 local64_set(&hwc->period_left, hwc->sample_period);
6237 * we currently do not support PERF_FORMAT_GROUP on inherited events
6239 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6242 pmu = perf_init_event(event);
6248 else if (IS_ERR(pmu))
6253 put_pid_ns(event->ns);
6255 return ERR_PTR(err);
6258 if (!event->parent) {
6259 if (event->attach_state & PERF_ATTACH_TASK)
6260 static_key_slow_inc(&perf_sched_events.key);
6261 if (event->attr.mmap || event->attr.mmap_data)
6262 atomic_inc(&nr_mmap_events);
6263 if (event->attr.comm)
6264 atomic_inc(&nr_comm_events);
6265 if (event->attr.task)
6266 atomic_inc(&nr_task_events);
6267 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6268 err = get_callchain_buffers();
6271 return ERR_PTR(err);
6274 if (has_branch_stack(event)) {
6275 static_key_slow_inc(&perf_sched_events.key);
6276 if (!(event->attach_state & PERF_ATTACH_TASK))
6277 atomic_inc(&per_cpu(perf_branch_stack_events,
6285 static int perf_copy_attr(struct perf_event_attr __user *uattr,
6286 struct perf_event_attr *attr)
6291 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6295 * zero the full structure, so that a short copy will be nice.
6297 memset(attr, 0, sizeof(*attr));
6299 ret = get_user(size, &uattr->size);
6303 if (size > PAGE_SIZE) /* silly large */
6306 if (!size) /* abi compat */
6307 size = PERF_ATTR_SIZE_VER0;
6309 if (size < PERF_ATTR_SIZE_VER0)
6313 * If we're handed a bigger struct than we know of,
6314 * ensure all the unknown bits are 0 - i.e. new
6315 * user-space does not rely on any kernel feature
6316 * extensions we dont know about yet.
6318 if (size > sizeof(*attr)) {
6319 unsigned char __user *addr;
6320 unsigned char __user *end;
6323 addr = (void __user *)uattr + sizeof(*attr);
6324 end = (void __user *)uattr + size;
6326 for (; addr < end; addr++) {
6327 ret = get_user(val, addr);
6333 size = sizeof(*attr);
6336 ret = copy_from_user(attr, uattr, size);
6340 if (attr->__reserved_1)
6343 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6346 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6349 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
6350 u64 mask = attr->branch_sample_type;
6352 /* only using defined bits */
6353 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6356 /* at least one branch bit must be set */
6357 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6360 /* kernel level capture: check permissions */
6361 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
6362 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6365 /* propagate priv level, when not set for branch */
6366 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6368 /* exclude_kernel checked on syscall entry */
6369 if (!attr->exclude_kernel)
6370 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6372 if (!attr->exclude_user)
6373 mask |= PERF_SAMPLE_BRANCH_USER;
6375 if (!attr->exclude_hv)
6376 mask |= PERF_SAMPLE_BRANCH_HV;
6378 * adjust user setting (for HW filter setup)
6380 attr->branch_sample_type = mask;
6384 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
6385 ret = perf_reg_validate(attr->sample_regs_user);
6390 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
6391 if (!arch_perf_have_user_stack_dump())
6395 * We have __u32 type for the size, but so far
6396 * we can only use __u16 as maximum due to the
6397 * __u16 sample size limit.
6399 if (attr->sample_stack_user >= USHRT_MAX)
6401 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
6409 put_user(sizeof(*attr), &uattr->size);
6415 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6417 struct ring_buffer *rb = NULL, *old_rb = NULL;
6423 /* don't allow circular references */
6424 if (event == output_event)
6428 * Don't allow cross-cpu buffers
6430 if (output_event->cpu != event->cpu)
6434 * If its not a per-cpu rb, it must be the same task.
6436 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6440 mutex_lock(&event->mmap_mutex);
6441 /* Can't redirect output if we've got an active mmap() */
6442 if (atomic_read(&event->mmap_count))
6446 /* get the rb we want to redirect to */
6447 rb = ring_buffer_get(output_event);
6453 rcu_assign_pointer(event->rb, rb);
6455 ring_buffer_detach(event, old_rb);
6458 mutex_unlock(&event->mmap_mutex);
6461 ring_buffer_put(old_rb);
6467 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6469 * @attr_uptr: event_id type attributes for monitoring/sampling
6472 * @group_fd: group leader event fd
6474 SYSCALL_DEFINE5(perf_event_open,
6475 struct perf_event_attr __user *, attr_uptr,
6476 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6478 struct perf_event *group_leader = NULL, *output_event = NULL;
6479 struct perf_event *event, *sibling;
6480 struct perf_event_attr attr;
6481 struct perf_event_context *ctx;
6482 struct file *event_file = NULL;
6483 struct fd group = {NULL, 0};
6484 struct task_struct *task = NULL;
6490 /* for future expandability... */
6491 if (flags & ~PERF_FLAG_ALL)
6494 err = perf_copy_attr(attr_uptr, &attr);
6498 if (!attr.exclude_kernel) {
6499 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6504 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6509 * In cgroup mode, the pid argument is used to pass the fd
6510 * opened to the cgroup directory in cgroupfs. The cpu argument
6511 * designates the cpu on which to monitor threads from that
6514 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6517 event_fd = get_unused_fd();
6521 if (group_fd != -1) {
6522 err = perf_fget_light(group_fd, &group);
6525 group_leader = group.file->private_data;
6526 if (flags & PERF_FLAG_FD_OUTPUT)
6527 output_event = group_leader;
6528 if (flags & PERF_FLAG_FD_NO_GROUP)
6529 group_leader = NULL;
6532 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
6533 task = find_lively_task_by_vpid(pid);
6535 err = PTR_ERR(task);
6542 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
6544 if (IS_ERR(event)) {
6545 err = PTR_ERR(event);
6549 if (flags & PERF_FLAG_PID_CGROUP) {
6550 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6555 * - that has cgroup constraint on event->cpu
6556 * - that may need work on context switch
6558 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6559 static_key_slow_inc(&perf_sched_events.key);
6563 * Special case software events and allow them to be part of
6564 * any hardware group.
6569 (is_software_event(event) != is_software_event(group_leader))) {
6570 if (is_software_event(event)) {
6572 * If event and group_leader are not both a software
6573 * event, and event is, then group leader is not.
6575 * Allow the addition of software events to !software
6576 * groups, this is safe because software events never
6579 pmu = group_leader->pmu;
6580 } else if (is_software_event(group_leader) &&
6581 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6583 * In case the group is a pure software group, and we
6584 * try to add a hardware event, move the whole group to
6585 * the hardware context.
6592 * Get the target context (task or percpu):
6594 ctx = find_get_context(pmu, task, event->cpu);
6601 put_task_struct(task);
6606 * Look up the group leader (we will attach this event to it):
6612 * Do not allow a recursive hierarchy (this new sibling
6613 * becoming part of another group-sibling):
6615 if (group_leader->group_leader != group_leader)
6618 * Do not allow to attach to a group in a different
6619 * task or CPU context:
6622 if (group_leader->ctx->type != ctx->type)
6625 if (group_leader->ctx != ctx)
6630 * Only a group leader can be exclusive or pinned
6632 if (attr.exclusive || attr.pinned)
6637 err = perf_event_set_output(event, output_event);
6642 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6643 if (IS_ERR(event_file)) {
6644 err = PTR_ERR(event_file);
6649 struct perf_event_context *gctx = group_leader->ctx;
6651 mutex_lock(&gctx->mutex);
6652 perf_remove_from_context(group_leader);
6655 * Removing from the context ends up with disabled
6656 * event. What we want here is event in the initial
6657 * startup state, ready to be add into new context.
6659 perf_event__state_init(group_leader);
6660 list_for_each_entry(sibling, &group_leader->sibling_list,
6662 perf_remove_from_context(sibling);
6663 perf_event__state_init(sibling);
6666 mutex_unlock(&gctx->mutex);
6670 WARN_ON_ONCE(ctx->parent_ctx);
6671 mutex_lock(&ctx->mutex);
6675 perf_install_in_context(ctx, group_leader, event->cpu);
6677 list_for_each_entry(sibling, &group_leader->sibling_list,
6679 perf_install_in_context(ctx, sibling, event->cpu);
6684 perf_install_in_context(ctx, event, event->cpu);
6686 perf_unpin_context(ctx);
6687 mutex_unlock(&ctx->mutex);
6691 event->owner = current;
6693 mutex_lock(¤t->perf_event_mutex);
6694 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6695 mutex_unlock(¤t->perf_event_mutex);
6698 * Precalculate sample_data sizes
6700 perf_event__header_size(event);
6701 perf_event__id_header_size(event);
6704 * Drop the reference on the group_event after placing the
6705 * new event on the sibling_list. This ensures destruction
6706 * of the group leader will find the pointer to itself in
6707 * perf_group_detach().
6710 fd_install(event_fd, event_file);
6714 perf_unpin_context(ctx);
6721 put_task_struct(task);
6725 put_unused_fd(event_fd);
6730 * perf_event_create_kernel_counter
6732 * @attr: attributes of the counter to create
6733 * @cpu: cpu in which the counter is bound
6734 * @task: task to profile (NULL for percpu)
6737 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6738 struct task_struct *task,
6739 perf_overflow_handler_t overflow_handler,
6742 struct perf_event_context *ctx;
6743 struct perf_event *event;
6747 * Get the target context (task or percpu):
6750 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
6751 overflow_handler, context);
6752 if (IS_ERR(event)) {
6753 err = PTR_ERR(event);
6757 ctx = find_get_context(event->pmu, task, cpu);
6763 WARN_ON_ONCE(ctx->parent_ctx);
6764 mutex_lock(&ctx->mutex);
6765 perf_install_in_context(ctx, event, cpu);
6767 perf_unpin_context(ctx);
6768 mutex_unlock(&ctx->mutex);
6775 return ERR_PTR(err);
6777 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6779 void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
6781 struct perf_event_context *src_ctx;
6782 struct perf_event_context *dst_ctx;
6783 struct perf_event *event, *tmp;
6786 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
6787 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
6789 mutex_lock(&src_ctx->mutex);
6790 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
6792 perf_remove_from_context(event);
6794 list_add(&event->event_entry, &events);
6796 mutex_unlock(&src_ctx->mutex);
6800 mutex_lock(&dst_ctx->mutex);
6801 list_for_each_entry_safe(event, tmp, &events, event_entry) {
6802 list_del(&event->event_entry);
6803 if (event->state >= PERF_EVENT_STATE_OFF)
6804 event->state = PERF_EVENT_STATE_INACTIVE;
6805 perf_install_in_context(dst_ctx, event, dst_cpu);
6808 mutex_unlock(&dst_ctx->mutex);
6810 EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
6812 static void sync_child_event(struct perf_event *child_event,
6813 struct task_struct *child)
6815 struct perf_event *parent_event = child_event->parent;
6818 if (child_event->attr.inherit_stat)
6819 perf_event_read_event(child_event, child);
6821 child_val = perf_event_count(child_event);
6824 * Add back the child's count to the parent's count:
6826 atomic64_add(child_val, &parent_event->child_count);
6827 atomic64_add(child_event->total_time_enabled,
6828 &parent_event->child_total_time_enabled);
6829 atomic64_add(child_event->total_time_running,
6830 &parent_event->child_total_time_running);
6833 * Remove this event from the parent's list
6835 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6836 mutex_lock(&parent_event->child_mutex);
6837 list_del_init(&child_event->child_list);
6838 mutex_unlock(&parent_event->child_mutex);
6841 * Release the parent event, if this was the last
6844 put_event(parent_event);
6848 __perf_event_exit_task(struct perf_event *child_event,
6849 struct perf_event_context *child_ctx,
6850 struct task_struct *child)
6852 if (child_event->parent) {
6853 raw_spin_lock_irq(&child_ctx->lock);
6854 perf_group_detach(child_event);
6855 raw_spin_unlock_irq(&child_ctx->lock);
6858 perf_remove_from_context(child_event);
6861 * It can happen that the parent exits first, and has events
6862 * that are still around due to the child reference. These
6863 * events need to be zapped.
6865 if (child_event->parent) {
6866 sync_child_event(child_event, child);
6867 free_event(child_event);
6871 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6873 struct perf_event *child_event, *tmp;
6874 struct perf_event_context *child_ctx;
6875 unsigned long flags;
6877 if (likely(!child->perf_event_ctxp[ctxn])) {
6878 perf_event_task(child, NULL, 0);
6882 local_irq_save(flags);
6884 * We can't reschedule here because interrupts are disabled,
6885 * and either child is current or it is a task that can't be
6886 * scheduled, so we are now safe from rescheduling changing
6889 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6892 * Take the context lock here so that if find_get_context is
6893 * reading child->perf_event_ctxp, we wait until it has
6894 * incremented the context's refcount before we do put_ctx below.
6896 raw_spin_lock(&child_ctx->lock);
6897 task_ctx_sched_out(child_ctx);
6898 child->perf_event_ctxp[ctxn] = NULL;
6900 * If this context is a clone; unclone it so it can't get
6901 * swapped to another process while we're removing all
6902 * the events from it.
6904 unclone_ctx(child_ctx);
6905 update_context_time(child_ctx);
6906 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6909 * Report the task dead after unscheduling the events so that we
6910 * won't get any samples after PERF_RECORD_EXIT. We can however still
6911 * get a few PERF_RECORD_READ events.
6913 perf_event_task(child, child_ctx, 0);
6916 * We can recurse on the same lock type through:
6918 * __perf_event_exit_task()
6919 * sync_child_event()
6921 * mutex_lock(&ctx->mutex)
6923 * But since its the parent context it won't be the same instance.
6925 mutex_lock(&child_ctx->mutex);
6928 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6930 __perf_event_exit_task(child_event, child_ctx, child);
6932 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6934 __perf_event_exit_task(child_event, child_ctx, child);
6937 * If the last event was a group event, it will have appended all
6938 * its siblings to the list, but we obtained 'tmp' before that which
6939 * will still point to the list head terminating the iteration.
6941 if (!list_empty(&child_ctx->pinned_groups) ||
6942 !list_empty(&child_ctx->flexible_groups))
6945 mutex_unlock(&child_ctx->mutex);
6951 * When a child task exits, feed back event values to parent events.
6953 void perf_event_exit_task(struct task_struct *child)
6955 struct perf_event *event, *tmp;
6958 mutex_lock(&child->perf_event_mutex);
6959 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6961 list_del_init(&event->owner_entry);
6964 * Ensure the list deletion is visible before we clear
6965 * the owner, closes a race against perf_release() where
6966 * we need to serialize on the owner->perf_event_mutex.
6969 event->owner = NULL;
6971 mutex_unlock(&child->perf_event_mutex);
6973 for_each_task_context_nr(ctxn)
6974 perf_event_exit_task_context(child, ctxn);
6977 static void perf_free_event(struct perf_event *event,
6978 struct perf_event_context *ctx)
6980 struct perf_event *parent = event->parent;
6982 if (WARN_ON_ONCE(!parent))
6985 mutex_lock(&parent->child_mutex);
6986 list_del_init(&event->child_list);
6987 mutex_unlock(&parent->child_mutex);
6991 perf_group_detach(event);
6992 list_del_event(event, ctx);
6997 * free an unexposed, unused context as created by inheritance by
6998 * perf_event_init_task below, used by fork() in case of fail.
7000 void perf_event_free_task(struct task_struct *task)
7002 struct perf_event_context *ctx;
7003 struct perf_event *event, *tmp;
7006 for_each_task_context_nr(ctxn) {
7007 ctx = task->perf_event_ctxp[ctxn];
7011 mutex_lock(&ctx->mutex);
7013 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
7015 perf_free_event(event, ctx);
7017 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
7019 perf_free_event(event, ctx);
7021 if (!list_empty(&ctx->pinned_groups) ||
7022 !list_empty(&ctx->flexible_groups))
7025 mutex_unlock(&ctx->mutex);
7031 void perf_event_delayed_put(struct task_struct *task)
7035 for_each_task_context_nr(ctxn)
7036 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
7040 * inherit a event from parent task to child task:
7042 static struct perf_event *
7043 inherit_event(struct perf_event *parent_event,
7044 struct task_struct *parent,
7045 struct perf_event_context *parent_ctx,
7046 struct task_struct *child,
7047 struct perf_event *group_leader,
7048 struct perf_event_context *child_ctx)
7050 struct perf_event *child_event;
7051 unsigned long flags;
7054 * Instead of creating recursive hierarchies of events,
7055 * we link inherited events back to the original parent,
7056 * which has a filp for sure, which we use as the reference
7059 if (parent_event->parent)
7060 parent_event = parent_event->parent;
7062 child_event = perf_event_alloc(&parent_event->attr,
7065 group_leader, parent_event,
7067 if (IS_ERR(child_event))
7070 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
7071 free_event(child_event);
7078 * Make the child state follow the state of the parent event,
7079 * not its attr.disabled bit. We hold the parent's mutex,
7080 * so we won't race with perf_event_{en, dis}able_family.
7082 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
7083 child_event->state = PERF_EVENT_STATE_INACTIVE;
7085 child_event->state = PERF_EVENT_STATE_OFF;
7087 if (parent_event->attr.freq) {
7088 u64 sample_period = parent_event->hw.sample_period;
7089 struct hw_perf_event *hwc = &child_event->hw;
7091 hwc->sample_period = sample_period;
7092 hwc->last_period = sample_period;
7094 local64_set(&hwc->period_left, sample_period);
7097 child_event->ctx = child_ctx;
7098 child_event->overflow_handler = parent_event->overflow_handler;
7099 child_event->overflow_handler_context
7100 = parent_event->overflow_handler_context;
7103 * Precalculate sample_data sizes
7105 perf_event__header_size(child_event);
7106 perf_event__id_header_size(child_event);
7109 * Link it up in the child's context:
7111 raw_spin_lock_irqsave(&child_ctx->lock, flags);
7112 add_event_to_ctx(child_event, child_ctx);
7113 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
7116 * Link this into the parent event's child list
7118 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7119 mutex_lock(&parent_event->child_mutex);
7120 list_add_tail(&child_event->child_list, &parent_event->child_list);
7121 mutex_unlock(&parent_event->child_mutex);
7126 static int inherit_group(struct perf_event *parent_event,
7127 struct task_struct *parent,
7128 struct perf_event_context *parent_ctx,
7129 struct task_struct *child,
7130 struct perf_event_context *child_ctx)
7132 struct perf_event *leader;
7133 struct perf_event *sub;
7134 struct perf_event *child_ctr;
7136 leader = inherit_event(parent_event, parent, parent_ctx,
7137 child, NULL, child_ctx);
7139 return PTR_ERR(leader);
7140 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7141 child_ctr = inherit_event(sub, parent, parent_ctx,
7142 child, leader, child_ctx);
7143 if (IS_ERR(child_ctr))
7144 return PTR_ERR(child_ctr);
7150 inherit_task_group(struct perf_event *event, struct task_struct *parent,
7151 struct perf_event_context *parent_ctx,
7152 struct task_struct *child, int ctxn,
7156 struct perf_event_context *child_ctx;
7158 if (!event->attr.inherit) {
7163 child_ctx = child->perf_event_ctxp[ctxn];
7166 * This is executed from the parent task context, so
7167 * inherit events that have been marked for cloning.
7168 * First allocate and initialize a context for the
7172 child_ctx = alloc_perf_context(event->pmu, child);
7176 child->perf_event_ctxp[ctxn] = child_ctx;
7179 ret = inherit_group(event, parent, parent_ctx,
7189 * Initialize the perf_event context in task_struct
7191 int perf_event_init_context(struct task_struct *child, int ctxn)
7193 struct perf_event_context *child_ctx, *parent_ctx;
7194 struct perf_event_context *cloned_ctx;
7195 struct perf_event *event;
7196 struct task_struct *parent = current;
7197 int inherited_all = 1;
7198 unsigned long flags;
7201 if (likely(!parent->perf_event_ctxp[ctxn]))
7205 * If the parent's context is a clone, pin it so it won't get
7208 parent_ctx = perf_pin_task_context(parent, ctxn);
7211 * No need to check if parent_ctx != NULL here; since we saw
7212 * it non-NULL earlier, the only reason for it to become NULL
7213 * is if we exit, and since we're currently in the middle of
7214 * a fork we can't be exiting at the same time.
7218 * Lock the parent list. No need to lock the child - not PID
7219 * hashed yet and not running, so nobody can access it.
7221 mutex_lock(&parent_ctx->mutex);
7224 * We dont have to disable NMIs - we are only looking at
7225 * the list, not manipulating it:
7227 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
7228 ret = inherit_task_group(event, parent, parent_ctx,
7229 child, ctxn, &inherited_all);
7235 * We can't hold ctx->lock when iterating the ->flexible_group list due
7236 * to allocations, but we need to prevent rotation because
7237 * rotate_ctx() will change the list from interrupt context.
7239 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7240 parent_ctx->rotate_disable = 1;
7241 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7243 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
7244 ret = inherit_task_group(event, parent, parent_ctx,
7245 child, ctxn, &inherited_all);
7250 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7251 parent_ctx->rotate_disable = 0;
7253 child_ctx = child->perf_event_ctxp[ctxn];
7255 if (child_ctx && inherited_all) {
7257 * Mark the child context as a clone of the parent
7258 * context, or of whatever the parent is a clone of.
7260 * Note that if the parent is a clone, the holding of
7261 * parent_ctx->lock avoids it from being uncloned.
7263 cloned_ctx = parent_ctx->parent_ctx;
7265 child_ctx->parent_ctx = cloned_ctx;
7266 child_ctx->parent_gen = parent_ctx->parent_gen;
7268 child_ctx->parent_ctx = parent_ctx;
7269 child_ctx->parent_gen = parent_ctx->generation;
7271 get_ctx(child_ctx->parent_ctx);
7274 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7275 mutex_unlock(&parent_ctx->mutex);
7277 perf_unpin_context(parent_ctx);
7278 put_ctx(parent_ctx);
7284 * Initialize the perf_event context in task_struct
7286 int perf_event_init_task(struct task_struct *child)
7290 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7291 mutex_init(&child->perf_event_mutex);
7292 INIT_LIST_HEAD(&child->perf_event_list);
7294 for_each_task_context_nr(ctxn) {
7295 ret = perf_event_init_context(child, ctxn);
7303 static void __init perf_event_init_all_cpus(void)
7305 struct swevent_htable *swhash;
7308 for_each_possible_cpu(cpu) {
7309 swhash = &per_cpu(swevent_htable, cpu);
7310 mutex_init(&swhash->hlist_mutex);
7311 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
7315 static void __cpuinit perf_event_init_cpu(int cpu)
7317 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7319 mutex_lock(&swhash->hlist_mutex);
7320 if (swhash->hlist_refcount > 0) {
7321 struct swevent_hlist *hlist;
7323 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7325 rcu_assign_pointer(swhash->swevent_hlist, hlist);
7327 mutex_unlock(&swhash->hlist_mutex);
7330 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7331 static void perf_pmu_rotate_stop(struct pmu *pmu)
7333 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7335 WARN_ON(!irqs_disabled());
7337 list_del_init(&cpuctx->rotation_list);
7340 static void __perf_event_exit_context(void *__info)
7342 struct perf_event_context *ctx = __info;
7343 struct perf_event *event, *tmp;
7345 perf_pmu_rotate_stop(ctx->pmu);
7347 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
7348 __perf_remove_from_context(event);
7349 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
7350 __perf_remove_from_context(event);
7353 static void perf_event_exit_cpu_context(int cpu)
7355 struct perf_event_context *ctx;
7359 idx = srcu_read_lock(&pmus_srcu);
7360 list_for_each_entry_rcu(pmu, &pmus, entry) {
7361 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
7363 mutex_lock(&ctx->mutex);
7364 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7365 mutex_unlock(&ctx->mutex);
7367 srcu_read_unlock(&pmus_srcu, idx);
7370 static void perf_event_exit_cpu(int cpu)
7372 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7374 mutex_lock(&swhash->hlist_mutex);
7375 swevent_hlist_release(swhash);
7376 mutex_unlock(&swhash->hlist_mutex);
7378 perf_event_exit_cpu_context(cpu);
7381 static inline void perf_event_exit_cpu(int cpu) { }
7385 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7389 for_each_online_cpu(cpu)
7390 perf_event_exit_cpu(cpu);
7396 * Run the perf reboot notifier at the very last possible moment so that
7397 * the generic watchdog code runs as long as possible.
7399 static struct notifier_block perf_reboot_notifier = {
7400 .notifier_call = perf_reboot,
7401 .priority = INT_MIN,
7404 static int __cpuinit
7405 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7407 unsigned int cpu = (long)hcpu;
7409 switch (action & ~CPU_TASKS_FROZEN) {
7411 case CPU_UP_PREPARE:
7412 case CPU_DOWN_FAILED:
7413 perf_event_init_cpu(cpu);
7416 case CPU_UP_CANCELED:
7417 case CPU_DOWN_PREPARE:
7418 perf_event_exit_cpu(cpu);
7428 void __init perf_event_init(void)
7434 perf_event_init_all_cpus();
7435 init_srcu_struct(&pmus_srcu);
7436 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7437 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7438 perf_pmu_register(&perf_task_clock, NULL, -1);
7440 perf_cpu_notifier(perf_cpu_notify);
7441 register_reboot_notifier(&perf_reboot_notifier);
7443 ret = init_hw_breakpoint();
7444 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7446 /* do not patch jump label more than once per second */
7447 jump_label_rate_limit(&perf_sched_events, HZ);
7450 * Build time assertion that we keep the data_head at the intended
7451 * location. IOW, validation we got the __reserved[] size right.
7453 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
7457 static int __init perf_event_sysfs_init(void)
7462 mutex_lock(&pmus_lock);
7464 ret = bus_register(&pmu_bus);
7468 list_for_each_entry(pmu, &pmus, entry) {
7469 if (!pmu->name || pmu->type < 0)
7472 ret = pmu_dev_alloc(pmu);
7473 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7475 pmu_bus_running = 1;
7479 mutex_unlock(&pmus_lock);
7483 device_initcall(perf_event_sysfs_init);
7485 #ifdef CONFIG_CGROUP_PERF
7486 static struct cgroup_subsys_state *perf_cgroup_css_alloc(struct cgroup *cont)
7488 struct perf_cgroup *jc;
7490 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
7492 return ERR_PTR(-ENOMEM);
7494 jc->info = alloc_percpu(struct perf_cgroup_info);
7497 return ERR_PTR(-ENOMEM);
7503 static void perf_cgroup_css_free(struct cgroup *cont)
7505 struct perf_cgroup *jc;
7506 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7507 struct perf_cgroup, css);
7508 free_percpu(jc->info);
7512 static int __perf_cgroup_move(void *info)
7514 struct task_struct *task = info;
7515 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7519 static void perf_cgroup_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
7521 struct task_struct *task;
7523 cgroup_taskset_for_each(task, cgrp, tset)
7524 task_function_call(task, __perf_cgroup_move, task);
7527 static void perf_cgroup_exit(struct cgroup *cgrp, struct cgroup *old_cgrp,
7528 struct task_struct *task)
7531 * cgroup_exit() is called in the copy_process() failure path.
7532 * Ignore this case since the task hasn't ran yet, this avoids
7533 * trying to poke a half freed task state from generic code.
7535 if (!(task->flags & PF_EXITING))
7538 task_function_call(task, __perf_cgroup_move, task);
7541 struct cgroup_subsys perf_subsys = {
7542 .name = "perf_event",
7543 .subsys_id = perf_subsys_id,
7544 .css_alloc = perf_cgroup_css_alloc,
7545 .css_free = perf_cgroup_css_free,
7546 .exit = perf_cgroup_exit,
7547 .attach = perf_cgroup_attach,
7550 * perf_event cgroup doesn't handle nesting correctly.
7551 * ctx->nr_cgroups adjustments should be propagated through the
7552 * cgroup hierarchy. Fix it and remove the following.
7554 .broken_hierarchy = true,
7556 #endif /* CONFIG_CGROUP_PERF */