2 * Performance events core code:
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 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/vmalloc.h>
29 #include <linux/hardirq.h>
30 #include <linux/rculist.h>
31 #include <linux/uaccess.h>
32 #include <linux/syscalls.h>
33 #include <linux/anon_inodes.h>
34 #include <linux/kernel_stat.h>
35 #include <linux/perf_event.h>
36 #include <linux/ftrace_event.h>
37 #include <linux/hw_breakpoint.h>
39 #include <asm/irq_regs.h>
41 struct remote_function_call {
42 struct task_struct *p;
43 int (*func)(void *info);
48 static void remote_function(void *data)
50 struct remote_function_call *tfc = data;
51 struct task_struct *p = tfc->p;
55 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
59 tfc->ret = tfc->func(tfc->info);
63 * task_function_call - call a function on the cpu on which a task runs
64 * @p: the task to evaluate
65 * @func: the function to be called
66 * @info: the function call argument
68 * Calls the function @func when the task is currently running. This might
69 * be on the current CPU, which just calls the function directly
71 * returns: @func return value, or
72 * -ESRCH - when the process isn't running
73 * -EAGAIN - when the process moved away
76 task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
78 struct remote_function_call data = {
82 .ret = -ESRCH, /* No such (running) process */
86 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
92 * cpu_function_call - call a function on the cpu
93 * @func: the function to be called
94 * @info: the function call argument
96 * Calls the function @func on the remote cpu.
98 * returns: @func return value or -ENXIO when the cpu is offline
100 static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
102 struct remote_function_call data = {
106 .ret = -ENXIO, /* No such CPU */
109 smp_call_function_single(cpu, remote_function, &data, 1);
114 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
115 PERF_FLAG_FD_OUTPUT |\
116 PERF_FLAG_PID_CGROUP)
119 EVENT_FLEXIBLE = 0x1,
121 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
125 * perf_sched_events : >0 events exist
126 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
128 atomic_t perf_sched_events __read_mostly;
129 static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
131 static atomic_t nr_mmap_events __read_mostly;
132 static atomic_t nr_comm_events __read_mostly;
133 static atomic_t nr_task_events __read_mostly;
135 static LIST_HEAD(pmus);
136 static DEFINE_MUTEX(pmus_lock);
137 static struct srcu_struct pmus_srcu;
140 * perf event paranoia level:
141 * -1 - not paranoid at all
142 * 0 - disallow raw tracepoint access for unpriv
143 * 1 - disallow cpu events for unpriv
144 * 2 - disallow kernel profiling for unpriv
146 int sysctl_perf_event_paranoid __read_mostly = 1;
148 int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
151 * max perf event sample rate
153 #define DEFAULT_MAX_SAMPLE_RATE 100000
154 int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
155 static int max_samples_per_tick __read_mostly =
156 DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
158 int perf_proc_update_handler(struct ctl_table *table, int write,
159 void __user *buffer, size_t *lenp,
162 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
167 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
172 static atomic64_t perf_event_id;
174 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
175 enum event_type_t event_type);
177 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
178 enum event_type_t event_type,
179 struct task_struct *task);
181 static void update_context_time(struct perf_event_context *ctx);
182 static u64 perf_event_time(struct perf_event *event);
184 void __weak perf_event_print_debug(void) { }
186 extern __weak const char *perf_pmu_name(void)
191 static inline u64 perf_clock(void)
193 return local_clock();
196 static inline struct perf_cpu_context *
197 __get_cpu_context(struct perf_event_context *ctx)
199 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
202 #ifdef CONFIG_CGROUP_PERF
205 * Must ensure cgroup is pinned (css_get) before calling
206 * this function. In other words, we cannot call this function
207 * if there is no cgroup event for the current CPU context.
209 static inline struct perf_cgroup *
210 perf_cgroup_from_task(struct task_struct *task)
212 return container_of(task_subsys_state(task, perf_subsys_id),
213 struct perf_cgroup, css);
217 perf_cgroup_match(struct perf_event *event)
219 struct perf_event_context *ctx = event->ctx;
220 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
222 return !event->cgrp || event->cgrp == cpuctx->cgrp;
225 static inline void perf_get_cgroup(struct perf_event *event)
227 css_get(&event->cgrp->css);
230 static inline void perf_put_cgroup(struct perf_event *event)
232 css_put(&event->cgrp->css);
235 static inline void perf_detach_cgroup(struct perf_event *event)
237 perf_put_cgroup(event);
241 static inline int is_cgroup_event(struct perf_event *event)
243 return event->cgrp != NULL;
246 static inline u64 perf_cgroup_event_time(struct perf_event *event)
248 struct perf_cgroup_info *t;
250 t = per_cpu_ptr(event->cgrp->info, event->cpu);
254 static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
256 struct perf_cgroup_info *info;
261 info = this_cpu_ptr(cgrp->info);
263 info->time += now - info->timestamp;
264 info->timestamp = now;
267 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
269 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
271 __update_cgrp_time(cgrp_out);
274 static inline void update_cgrp_time_from_event(struct perf_event *event)
276 struct perf_cgroup *cgrp;
279 * ensure we access cgroup data only when needed and
280 * when we know the cgroup is pinned (css_get)
282 if (!is_cgroup_event(event))
285 cgrp = perf_cgroup_from_task(current);
287 * Do not update time when cgroup is not active
289 if (cgrp == event->cgrp)
290 __update_cgrp_time(event->cgrp);
294 perf_cgroup_set_timestamp(struct task_struct *task,
295 struct perf_event_context *ctx)
297 struct perf_cgroup *cgrp;
298 struct perf_cgroup_info *info;
301 * ctx->lock held by caller
302 * ensure we do not access cgroup data
303 * unless we have the cgroup pinned (css_get)
305 if (!task || !ctx->nr_cgroups)
308 cgrp = perf_cgroup_from_task(task);
309 info = this_cpu_ptr(cgrp->info);
310 info->timestamp = ctx->timestamp;
313 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
314 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
317 * reschedule events based on the cgroup constraint of task.
319 * mode SWOUT : schedule out everything
320 * mode SWIN : schedule in based on cgroup for next
322 void perf_cgroup_switch(struct task_struct *task, int mode)
324 struct perf_cpu_context *cpuctx;
329 * disable interrupts to avoid geting nr_cgroup
330 * changes via __perf_event_disable(). Also
333 local_irq_save(flags);
336 * we reschedule only in the presence of cgroup
337 * constrained events.
341 list_for_each_entry_rcu(pmu, &pmus, entry) {
343 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
345 perf_pmu_disable(cpuctx->ctx.pmu);
348 * perf_cgroup_events says at least one
349 * context on this CPU has cgroup events.
351 * ctx->nr_cgroups reports the number of cgroup
352 * events for a context.
354 if (cpuctx->ctx.nr_cgroups > 0) {
356 if (mode & PERF_CGROUP_SWOUT) {
357 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
359 * must not be done before ctxswout due
360 * to event_filter_match() in event_sched_out()
365 if (mode & PERF_CGROUP_SWIN) {
366 /* set cgrp before ctxsw in to
367 * allow event_filter_match() to not
368 * have to pass task around
370 cpuctx->cgrp = perf_cgroup_from_task(task);
371 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
375 perf_pmu_enable(cpuctx->ctx.pmu);
380 local_irq_restore(flags);
383 static inline void perf_cgroup_sched_out(struct task_struct *task)
385 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
388 static inline void perf_cgroup_sched_in(struct task_struct *task)
390 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
393 static inline int perf_cgroup_connect(int fd, struct perf_event *event,
394 struct perf_event_attr *attr,
395 struct perf_event *group_leader)
397 struct perf_cgroup *cgrp;
398 struct cgroup_subsys_state *css;
400 int ret = 0, fput_needed;
402 file = fget_light(fd, &fput_needed);
406 css = cgroup_css_from_dir(file, perf_subsys_id);
412 cgrp = container_of(css, struct perf_cgroup, css);
415 /* must be done before we fput() the file */
416 perf_get_cgroup(event);
419 * all events in a group must monitor
420 * the same cgroup because a task belongs
421 * to only one perf cgroup at a time
423 if (group_leader && group_leader->cgrp != cgrp) {
424 perf_detach_cgroup(event);
428 fput_light(file, fput_needed);
433 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
435 struct perf_cgroup_info *t;
436 t = per_cpu_ptr(event->cgrp->info, event->cpu);
437 event->shadow_ctx_time = now - t->timestamp;
441 perf_cgroup_defer_enabled(struct perf_event *event)
444 * when the current task's perf cgroup does not match
445 * the event's, we need to remember to call the
446 * perf_mark_enable() function the first time a task with
447 * a matching perf cgroup is scheduled in.
449 if (is_cgroup_event(event) && !perf_cgroup_match(event))
450 event->cgrp_defer_enabled = 1;
454 perf_cgroup_mark_enabled(struct perf_event *event,
455 struct perf_event_context *ctx)
457 struct perf_event *sub;
458 u64 tstamp = perf_event_time(event);
460 if (!event->cgrp_defer_enabled)
463 event->cgrp_defer_enabled = 0;
465 event->tstamp_enabled = tstamp - event->total_time_enabled;
466 list_for_each_entry(sub, &event->sibling_list, group_entry) {
467 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
468 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
469 sub->cgrp_defer_enabled = 0;
473 #else /* !CONFIG_CGROUP_PERF */
476 perf_cgroup_match(struct perf_event *event)
481 static inline void perf_detach_cgroup(struct perf_event *event)
484 static inline int is_cgroup_event(struct perf_event *event)
489 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
494 static inline void update_cgrp_time_from_event(struct perf_event *event)
498 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
502 static inline void perf_cgroup_sched_out(struct task_struct *task)
506 static inline void perf_cgroup_sched_in(struct task_struct *task)
510 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
511 struct perf_event_attr *attr,
512 struct perf_event *group_leader)
518 perf_cgroup_set_timestamp(struct task_struct *task,
519 struct perf_event_context *ctx)
524 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
529 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
533 static inline u64 perf_cgroup_event_time(struct perf_event *event)
539 perf_cgroup_defer_enabled(struct perf_event *event)
544 perf_cgroup_mark_enabled(struct perf_event *event,
545 struct perf_event_context *ctx)
550 void perf_pmu_disable(struct pmu *pmu)
552 int *count = this_cpu_ptr(pmu->pmu_disable_count);
554 pmu->pmu_disable(pmu);
557 void perf_pmu_enable(struct pmu *pmu)
559 int *count = this_cpu_ptr(pmu->pmu_disable_count);
561 pmu->pmu_enable(pmu);
564 static DEFINE_PER_CPU(struct list_head, rotation_list);
567 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
568 * because they're strictly cpu affine and rotate_start is called with IRQs
569 * disabled, while rotate_context is called from IRQ context.
571 static void perf_pmu_rotate_start(struct pmu *pmu)
573 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
574 struct list_head *head = &__get_cpu_var(rotation_list);
576 WARN_ON(!irqs_disabled());
578 if (list_empty(&cpuctx->rotation_list))
579 list_add(&cpuctx->rotation_list, head);
582 static void get_ctx(struct perf_event_context *ctx)
584 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
587 static void free_ctx(struct rcu_head *head)
589 struct perf_event_context *ctx;
591 ctx = container_of(head, struct perf_event_context, rcu_head);
595 static void put_ctx(struct perf_event_context *ctx)
597 if (atomic_dec_and_test(&ctx->refcount)) {
599 put_ctx(ctx->parent_ctx);
601 put_task_struct(ctx->task);
602 call_rcu(&ctx->rcu_head, free_ctx);
606 static void unclone_ctx(struct perf_event_context *ctx)
608 if (ctx->parent_ctx) {
609 put_ctx(ctx->parent_ctx);
610 ctx->parent_ctx = NULL;
614 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
617 * only top level events have the pid namespace they were created in
620 event = event->parent;
622 return task_tgid_nr_ns(p, event->ns);
625 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
628 * only top level events have the pid namespace they were created in
631 event = event->parent;
633 return task_pid_nr_ns(p, event->ns);
637 * If we inherit events we want to return the parent event id
640 static u64 primary_event_id(struct perf_event *event)
645 id = event->parent->id;
651 * Get the perf_event_context for a task and lock it.
652 * This has to cope with with the fact that until it is locked,
653 * the context could get moved to another task.
655 static struct perf_event_context *
656 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
658 struct perf_event_context *ctx;
662 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
665 * If this context is a clone of another, it might
666 * get swapped for another underneath us by
667 * perf_event_task_sched_out, though the
668 * rcu_read_lock() protects us from any context
669 * getting freed. Lock the context and check if it
670 * got swapped before we could get the lock, and retry
671 * if so. If we locked the right context, then it
672 * can't get swapped on us any more.
674 raw_spin_lock_irqsave(&ctx->lock, *flags);
675 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
676 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
680 if (!atomic_inc_not_zero(&ctx->refcount)) {
681 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
690 * Get the context for a task and increment its pin_count so it
691 * can't get swapped to another task. This also increments its
692 * reference count so that the context can't get freed.
694 static struct perf_event_context *
695 perf_pin_task_context(struct task_struct *task, int ctxn)
697 struct perf_event_context *ctx;
700 ctx = perf_lock_task_context(task, ctxn, &flags);
703 raw_spin_unlock_irqrestore(&ctx->lock, flags);
708 static void perf_unpin_context(struct perf_event_context *ctx)
712 raw_spin_lock_irqsave(&ctx->lock, flags);
714 raw_spin_unlock_irqrestore(&ctx->lock, flags);
718 * Update the record of the current time in a context.
720 static void update_context_time(struct perf_event_context *ctx)
722 u64 now = perf_clock();
724 ctx->time += now - ctx->timestamp;
725 ctx->timestamp = now;
728 static u64 perf_event_time(struct perf_event *event)
730 struct perf_event_context *ctx = event->ctx;
732 if (is_cgroup_event(event))
733 return perf_cgroup_event_time(event);
735 return ctx ? ctx->time : 0;
739 * Update the total_time_enabled and total_time_running fields for a event.
741 static void update_event_times(struct perf_event *event)
743 struct perf_event_context *ctx = event->ctx;
746 if (event->state < PERF_EVENT_STATE_INACTIVE ||
747 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
750 * in cgroup mode, time_enabled represents
751 * the time the event was enabled AND active
752 * tasks were in the monitored cgroup. This is
753 * independent of the activity of the context as
754 * there may be a mix of cgroup and non-cgroup events.
756 * That is why we treat cgroup events differently
759 if (is_cgroup_event(event))
760 run_end = perf_event_time(event);
761 else if (ctx->is_active)
764 run_end = event->tstamp_stopped;
766 event->total_time_enabled = run_end - event->tstamp_enabled;
768 if (event->state == PERF_EVENT_STATE_INACTIVE)
769 run_end = event->tstamp_stopped;
771 run_end = perf_event_time(event);
773 event->total_time_running = run_end - event->tstamp_running;
778 * Update total_time_enabled and total_time_running for all events in a group.
780 static void update_group_times(struct perf_event *leader)
782 struct perf_event *event;
784 update_event_times(leader);
785 list_for_each_entry(event, &leader->sibling_list, group_entry)
786 update_event_times(event);
789 static struct list_head *
790 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
792 if (event->attr.pinned)
793 return &ctx->pinned_groups;
795 return &ctx->flexible_groups;
799 * Add a event from the lists for its context.
800 * Must be called with ctx->mutex and ctx->lock held.
803 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
805 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
806 event->attach_state |= PERF_ATTACH_CONTEXT;
809 * If we're a stand alone event or group leader, we go to the context
810 * list, group events are kept attached to the group so that
811 * perf_group_detach can, at all times, locate all siblings.
813 if (event->group_leader == event) {
814 struct list_head *list;
816 if (is_software_event(event))
817 event->group_flags |= PERF_GROUP_SOFTWARE;
819 list = ctx_group_list(event, ctx);
820 list_add_tail(&event->group_entry, list);
823 if (is_cgroup_event(event))
826 list_add_rcu(&event->event_entry, &ctx->event_list);
828 perf_pmu_rotate_start(ctx->pmu);
830 if (event->attr.inherit_stat)
835 * Called at perf_event creation and when events are attached/detached from a
838 static void perf_event__read_size(struct perf_event *event)
840 int entry = sizeof(u64); /* value */
844 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
847 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
850 if (event->attr.read_format & PERF_FORMAT_ID)
851 entry += sizeof(u64);
853 if (event->attr.read_format & PERF_FORMAT_GROUP) {
854 nr += event->group_leader->nr_siblings;
859 event->read_size = size;
862 static void perf_event__header_size(struct perf_event *event)
864 struct perf_sample_data *data;
865 u64 sample_type = event->attr.sample_type;
868 perf_event__read_size(event);
870 if (sample_type & PERF_SAMPLE_IP)
871 size += sizeof(data->ip);
873 if (sample_type & PERF_SAMPLE_ADDR)
874 size += sizeof(data->addr);
876 if (sample_type & PERF_SAMPLE_PERIOD)
877 size += sizeof(data->period);
879 if (sample_type & PERF_SAMPLE_READ)
880 size += event->read_size;
882 event->header_size = size;
885 static void perf_event__id_header_size(struct perf_event *event)
887 struct perf_sample_data *data;
888 u64 sample_type = event->attr.sample_type;
891 if (sample_type & PERF_SAMPLE_TID)
892 size += sizeof(data->tid_entry);
894 if (sample_type & PERF_SAMPLE_TIME)
895 size += sizeof(data->time);
897 if (sample_type & PERF_SAMPLE_ID)
898 size += sizeof(data->id);
900 if (sample_type & PERF_SAMPLE_STREAM_ID)
901 size += sizeof(data->stream_id);
903 if (sample_type & PERF_SAMPLE_CPU)
904 size += sizeof(data->cpu_entry);
906 event->id_header_size = size;
909 static void perf_group_attach(struct perf_event *event)
911 struct perf_event *group_leader = event->group_leader, *pos;
914 * We can have double attach due to group movement in perf_event_open.
916 if (event->attach_state & PERF_ATTACH_GROUP)
919 event->attach_state |= PERF_ATTACH_GROUP;
921 if (group_leader == event)
924 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
925 !is_software_event(event))
926 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
928 list_add_tail(&event->group_entry, &group_leader->sibling_list);
929 group_leader->nr_siblings++;
931 perf_event__header_size(group_leader);
933 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
934 perf_event__header_size(pos);
938 * Remove a event from the lists for its context.
939 * Must be called with ctx->mutex and ctx->lock held.
942 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
944 struct perf_cpu_context *cpuctx;
946 * We can have double detach due to exit/hot-unplug + close.
948 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
951 event->attach_state &= ~PERF_ATTACH_CONTEXT;
953 if (is_cgroup_event(event)) {
955 cpuctx = __get_cpu_context(ctx);
957 * if there are no more cgroup events
958 * then cler cgrp to avoid stale pointer
959 * in update_cgrp_time_from_cpuctx()
961 if (!ctx->nr_cgroups)
966 if (event->attr.inherit_stat)
969 list_del_rcu(&event->event_entry);
971 if (event->group_leader == event)
972 list_del_init(&event->group_entry);
974 update_group_times(event);
977 * If event was in error state, then keep it
978 * that way, otherwise bogus counts will be
979 * returned on read(). The only way to get out
980 * of error state is by explicit re-enabling
983 if (event->state > PERF_EVENT_STATE_OFF)
984 event->state = PERF_EVENT_STATE_OFF;
987 static void perf_group_detach(struct perf_event *event)
989 struct perf_event *sibling, *tmp;
990 struct list_head *list = NULL;
993 * We can have double detach due to exit/hot-unplug + close.
995 if (!(event->attach_state & PERF_ATTACH_GROUP))
998 event->attach_state &= ~PERF_ATTACH_GROUP;
1001 * If this is a sibling, remove it from its group.
1003 if (event->group_leader != event) {
1004 list_del_init(&event->group_entry);
1005 event->group_leader->nr_siblings--;
1009 if (!list_empty(&event->group_entry))
1010 list = &event->group_entry;
1013 * If this was a group event with sibling events then
1014 * upgrade the siblings to singleton events by adding them
1015 * to whatever list we are on.
1017 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
1019 list_move_tail(&sibling->group_entry, list);
1020 sibling->group_leader = sibling;
1022 /* Inherit group flags from the previous leader */
1023 sibling->group_flags = event->group_flags;
1027 perf_event__header_size(event->group_leader);
1029 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1030 perf_event__header_size(tmp);
1034 event_filter_match(struct perf_event *event)
1036 return (event->cpu == -1 || event->cpu == smp_processor_id())
1037 && perf_cgroup_match(event);
1041 event_sched_out(struct perf_event *event,
1042 struct perf_cpu_context *cpuctx,
1043 struct perf_event_context *ctx)
1045 u64 tstamp = perf_event_time(event);
1048 * An event which could not be activated because of
1049 * filter mismatch still needs to have its timings
1050 * maintained, otherwise bogus information is return
1051 * via read() for time_enabled, time_running:
1053 if (event->state == PERF_EVENT_STATE_INACTIVE
1054 && !event_filter_match(event)) {
1055 delta = tstamp - event->tstamp_stopped;
1056 event->tstamp_running += delta;
1057 event->tstamp_stopped = tstamp;
1060 if (event->state != PERF_EVENT_STATE_ACTIVE)
1063 event->state = PERF_EVENT_STATE_INACTIVE;
1064 if (event->pending_disable) {
1065 event->pending_disable = 0;
1066 event->state = PERF_EVENT_STATE_OFF;
1068 event->tstamp_stopped = tstamp;
1069 event->pmu->del(event, 0);
1072 if (!is_software_event(event))
1073 cpuctx->active_oncpu--;
1075 if (event->attr.exclusive || !cpuctx->active_oncpu)
1076 cpuctx->exclusive = 0;
1080 group_sched_out(struct perf_event *group_event,
1081 struct perf_cpu_context *cpuctx,
1082 struct perf_event_context *ctx)
1084 struct perf_event *event;
1085 int state = group_event->state;
1087 event_sched_out(group_event, cpuctx, ctx);
1090 * Schedule out siblings (if any):
1092 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1093 event_sched_out(event, cpuctx, ctx);
1095 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
1096 cpuctx->exclusive = 0;
1100 * Cross CPU call to remove a performance event
1102 * We disable the event on the hardware level first. After that we
1103 * remove it from the context list.
1105 static int __perf_remove_from_context(void *info)
1107 struct perf_event *event = info;
1108 struct perf_event_context *ctx = event->ctx;
1109 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1111 raw_spin_lock(&ctx->lock);
1112 event_sched_out(event, cpuctx, ctx);
1113 list_del_event(event, ctx);
1114 raw_spin_unlock(&ctx->lock);
1121 * Remove the event from a task's (or a CPU's) list of events.
1123 * CPU events are removed with a smp call. For task events we only
1124 * call when the task is on a CPU.
1126 * If event->ctx is a cloned context, callers must make sure that
1127 * every task struct that event->ctx->task could possibly point to
1128 * remains valid. This is OK when called from perf_release since
1129 * that only calls us on the top-level context, which can't be a clone.
1130 * When called from perf_event_exit_task, it's OK because the
1131 * context has been detached from its task.
1133 static void perf_remove_from_context(struct perf_event *event)
1135 struct perf_event_context *ctx = event->ctx;
1136 struct task_struct *task = ctx->task;
1138 lockdep_assert_held(&ctx->mutex);
1142 * Per cpu events are removed via an smp call and
1143 * the removal is always successful.
1145 cpu_function_call(event->cpu, __perf_remove_from_context, event);
1150 if (!task_function_call(task, __perf_remove_from_context, event))
1153 raw_spin_lock_irq(&ctx->lock);
1155 * If we failed to find a running task, but find the context active now
1156 * that we've acquired the ctx->lock, retry.
1158 if (ctx->is_active) {
1159 raw_spin_unlock_irq(&ctx->lock);
1164 * Since the task isn't running, its safe to remove the event, us
1165 * holding the ctx->lock ensures the task won't get scheduled in.
1167 list_del_event(event, ctx);
1168 raw_spin_unlock_irq(&ctx->lock);
1172 * Cross CPU call to disable a performance event
1174 static int __perf_event_disable(void *info)
1176 struct perf_event *event = info;
1177 struct perf_event_context *ctx = event->ctx;
1178 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1181 * If this is a per-task event, need to check whether this
1182 * event's task is the current task on this cpu.
1184 * Can trigger due to concurrent perf_event_context_sched_out()
1185 * flipping contexts around.
1187 if (ctx->task && cpuctx->task_ctx != ctx)
1190 raw_spin_lock(&ctx->lock);
1193 * If the event is on, turn it off.
1194 * If it is in error state, leave it in error state.
1196 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1197 update_context_time(ctx);
1198 update_cgrp_time_from_event(event);
1199 update_group_times(event);
1200 if (event == event->group_leader)
1201 group_sched_out(event, cpuctx, ctx);
1203 event_sched_out(event, cpuctx, ctx);
1204 event->state = PERF_EVENT_STATE_OFF;
1207 raw_spin_unlock(&ctx->lock);
1215 * If event->ctx is a cloned context, callers must make sure that
1216 * every task struct that event->ctx->task could possibly point to
1217 * remains valid. This condition is satisifed when called through
1218 * perf_event_for_each_child or perf_event_for_each because they
1219 * hold the top-level event's child_mutex, so any descendant that
1220 * goes to exit will block in sync_child_event.
1221 * When called from perf_pending_event it's OK because event->ctx
1222 * is the current context on this CPU and preemption is disabled,
1223 * hence we can't get into perf_event_task_sched_out for this context.
1225 void perf_event_disable(struct perf_event *event)
1227 struct perf_event_context *ctx = event->ctx;
1228 struct task_struct *task = ctx->task;
1232 * Disable the event on the cpu that it's on
1234 cpu_function_call(event->cpu, __perf_event_disable, event);
1239 if (!task_function_call(task, __perf_event_disable, event))
1242 raw_spin_lock_irq(&ctx->lock);
1244 * If the event is still active, we need to retry the cross-call.
1246 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1247 raw_spin_unlock_irq(&ctx->lock);
1249 * Reload the task pointer, it might have been changed by
1250 * a concurrent perf_event_context_sched_out().
1257 * Since we have the lock this context can't be scheduled
1258 * in, so we can change the state safely.
1260 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1261 update_group_times(event);
1262 event->state = PERF_EVENT_STATE_OFF;
1264 raw_spin_unlock_irq(&ctx->lock);
1267 static void perf_set_shadow_time(struct perf_event *event,
1268 struct perf_event_context *ctx,
1272 * use the correct time source for the time snapshot
1274 * We could get by without this by leveraging the
1275 * fact that to get to this function, the caller
1276 * has most likely already called update_context_time()
1277 * and update_cgrp_time_xx() and thus both timestamp
1278 * are identical (or very close). Given that tstamp is,
1279 * already adjusted for cgroup, we could say that:
1280 * tstamp - ctx->timestamp
1282 * tstamp - cgrp->timestamp.
1284 * Then, in perf_output_read(), the calculation would
1285 * work with no changes because:
1286 * - event is guaranteed scheduled in
1287 * - no scheduled out in between
1288 * - thus the timestamp would be the same
1290 * But this is a bit hairy.
1292 * So instead, we have an explicit cgroup call to remain
1293 * within the time time source all along. We believe it
1294 * is cleaner and simpler to understand.
1296 if (is_cgroup_event(event))
1297 perf_cgroup_set_shadow_time(event, tstamp);
1299 event->shadow_ctx_time = tstamp - ctx->timestamp;
1302 #define MAX_INTERRUPTS (~0ULL)
1304 static void perf_log_throttle(struct perf_event *event, int enable);
1307 event_sched_in(struct perf_event *event,
1308 struct perf_cpu_context *cpuctx,
1309 struct perf_event_context *ctx)
1311 u64 tstamp = perf_event_time(event);
1313 if (event->state <= PERF_EVENT_STATE_OFF)
1316 event->state = PERF_EVENT_STATE_ACTIVE;
1317 event->oncpu = smp_processor_id();
1320 * Unthrottle events, since we scheduled we might have missed several
1321 * ticks already, also for a heavily scheduling task there is little
1322 * guarantee it'll get a tick in a timely manner.
1324 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1325 perf_log_throttle(event, 1);
1326 event->hw.interrupts = 0;
1330 * The new state must be visible before we turn it on in the hardware:
1334 if (event->pmu->add(event, PERF_EF_START)) {
1335 event->state = PERF_EVENT_STATE_INACTIVE;
1340 event->tstamp_running += tstamp - event->tstamp_stopped;
1342 perf_set_shadow_time(event, ctx, tstamp);
1344 if (!is_software_event(event))
1345 cpuctx->active_oncpu++;
1348 if (event->attr.exclusive)
1349 cpuctx->exclusive = 1;
1355 group_sched_in(struct perf_event *group_event,
1356 struct perf_cpu_context *cpuctx,
1357 struct perf_event_context *ctx)
1359 struct perf_event *event, *partial_group = NULL;
1360 struct pmu *pmu = group_event->pmu;
1361 u64 now = ctx->time;
1362 bool simulate = false;
1364 if (group_event->state == PERF_EVENT_STATE_OFF)
1367 pmu->start_txn(pmu);
1369 if (event_sched_in(group_event, cpuctx, ctx)) {
1370 pmu->cancel_txn(pmu);
1375 * Schedule in siblings as one group (if any):
1377 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1378 if (event_sched_in(event, cpuctx, ctx)) {
1379 partial_group = event;
1384 if (!pmu->commit_txn(pmu))
1389 * Groups can be scheduled in as one unit only, so undo any
1390 * partial group before returning:
1391 * The events up to the failed event are scheduled out normally,
1392 * tstamp_stopped will be updated.
1394 * The failed events and the remaining siblings need to have
1395 * their timings updated as if they had gone thru event_sched_in()
1396 * and event_sched_out(). This is required to get consistent timings
1397 * across the group. This also takes care of the case where the group
1398 * could never be scheduled by ensuring tstamp_stopped is set to mark
1399 * the time the event was actually stopped, such that time delta
1400 * calculation in update_event_times() is correct.
1402 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1403 if (event == partial_group)
1407 event->tstamp_running += now - event->tstamp_stopped;
1408 event->tstamp_stopped = now;
1410 event_sched_out(event, cpuctx, ctx);
1413 event_sched_out(group_event, cpuctx, ctx);
1415 pmu->cancel_txn(pmu);
1421 * Work out whether we can put this event group on the CPU now.
1423 static int group_can_go_on(struct perf_event *event,
1424 struct perf_cpu_context *cpuctx,
1428 * Groups consisting entirely of software events can always go on.
1430 if (event->group_flags & PERF_GROUP_SOFTWARE)
1433 * If an exclusive group is already on, no other hardware
1436 if (cpuctx->exclusive)
1439 * If this group is exclusive and there are already
1440 * events on the CPU, it can't go on.
1442 if (event->attr.exclusive && cpuctx->active_oncpu)
1445 * Otherwise, try to add it if all previous groups were able
1451 static void add_event_to_ctx(struct perf_event *event,
1452 struct perf_event_context *ctx)
1454 u64 tstamp = perf_event_time(event);
1456 list_add_event(event, ctx);
1457 perf_group_attach(event);
1458 event->tstamp_enabled = tstamp;
1459 event->tstamp_running = tstamp;
1460 event->tstamp_stopped = tstamp;
1463 static void perf_event_context_sched_in(struct perf_event_context *ctx,
1464 struct task_struct *tsk);
1467 * Cross CPU call to install and enable a performance event
1469 * Must be called with ctx->mutex held
1471 static int __perf_install_in_context(void *info)
1473 struct perf_event *event = info;
1474 struct perf_event_context *ctx = event->ctx;
1475 struct perf_event *leader = event->group_leader;
1476 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1480 * In case we're installing a new context to an already running task,
1481 * could also happen before perf_event_task_sched_in() on architectures
1482 * which do context switches with IRQs enabled.
1484 if (ctx->task && !cpuctx->task_ctx)
1485 perf_event_context_sched_in(ctx, ctx->task);
1487 raw_spin_lock(&ctx->lock);
1489 update_context_time(ctx);
1491 * update cgrp time only if current cgrp
1492 * matches event->cgrp. Must be done before
1493 * calling add_event_to_ctx()
1495 update_cgrp_time_from_event(event);
1497 add_event_to_ctx(event, ctx);
1499 if (!event_filter_match(event))
1503 * Don't put the event on if it is disabled or if
1504 * it is in a group and the group isn't on.
1506 if (event->state != PERF_EVENT_STATE_INACTIVE ||
1507 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
1511 * An exclusive event can't go on if there are already active
1512 * hardware events, and no hardware event can go on if there
1513 * is already an exclusive event on.
1515 if (!group_can_go_on(event, cpuctx, 1))
1518 err = event_sched_in(event, cpuctx, ctx);
1522 * This event couldn't go on. If it is in a group
1523 * then we have to pull the whole group off.
1524 * If the event group is pinned then put it in error state.
1526 if (leader != event)
1527 group_sched_out(leader, cpuctx, ctx);
1528 if (leader->attr.pinned) {
1529 update_group_times(leader);
1530 leader->state = PERF_EVENT_STATE_ERROR;
1535 raw_spin_unlock(&ctx->lock);
1541 * Attach a performance event to a context
1543 * First we add the event to the list with the hardware enable bit
1544 * in event->hw_config cleared.
1546 * If the event is attached to a task which is on a CPU we use a smp
1547 * call to enable it in the task context. The task might have been
1548 * scheduled away, but we check this in the smp call again.
1551 perf_install_in_context(struct perf_event_context *ctx,
1552 struct perf_event *event,
1555 struct task_struct *task = ctx->task;
1557 lockdep_assert_held(&ctx->mutex);
1563 * Per cpu events are installed via an smp call and
1564 * the install is always successful.
1566 cpu_function_call(cpu, __perf_install_in_context, event);
1571 if (!task_function_call(task, __perf_install_in_context, event))
1574 raw_spin_lock_irq(&ctx->lock);
1576 * If we failed to find a running task, but find the context active now
1577 * that we've acquired the ctx->lock, retry.
1579 if (ctx->is_active) {
1580 raw_spin_unlock_irq(&ctx->lock);
1585 * Since the task isn't running, its safe to add the event, us holding
1586 * the ctx->lock ensures the task won't get scheduled in.
1588 add_event_to_ctx(event, ctx);
1589 raw_spin_unlock_irq(&ctx->lock);
1593 * Put a event into inactive state and update time fields.
1594 * Enabling the leader of a group effectively enables all
1595 * the group members that aren't explicitly disabled, so we
1596 * have to update their ->tstamp_enabled also.
1597 * Note: this works for group members as well as group leaders
1598 * since the non-leader members' sibling_lists will be empty.
1600 static void __perf_event_mark_enabled(struct perf_event *event,
1601 struct perf_event_context *ctx)
1603 struct perf_event *sub;
1604 u64 tstamp = perf_event_time(event);
1606 event->state = PERF_EVENT_STATE_INACTIVE;
1607 event->tstamp_enabled = tstamp - event->total_time_enabled;
1608 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1609 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1610 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1615 * Cross CPU call to enable a performance event
1617 static int __perf_event_enable(void *info)
1619 struct perf_event *event = info;
1620 struct perf_event_context *ctx = event->ctx;
1621 struct perf_event *leader = event->group_leader;
1622 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1625 if (WARN_ON_ONCE(!ctx->is_active))
1628 raw_spin_lock(&ctx->lock);
1629 update_context_time(ctx);
1631 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1635 * set current task's cgroup time reference point
1637 perf_cgroup_set_timestamp(current, ctx);
1639 __perf_event_mark_enabled(event, ctx);
1641 if (!event_filter_match(event)) {
1642 if (is_cgroup_event(event))
1643 perf_cgroup_defer_enabled(event);
1648 * If the event is in a group and isn't the group leader,
1649 * then don't put it on unless the group is on.
1651 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1654 if (!group_can_go_on(event, cpuctx, 1)) {
1657 if (event == leader)
1658 err = group_sched_in(event, cpuctx, ctx);
1660 err = event_sched_in(event, cpuctx, ctx);
1665 * If this event can't go on and it's part of a
1666 * group, then the whole group has to come off.
1668 if (leader != event)
1669 group_sched_out(leader, cpuctx, ctx);
1670 if (leader->attr.pinned) {
1671 update_group_times(leader);
1672 leader->state = PERF_EVENT_STATE_ERROR;
1677 raw_spin_unlock(&ctx->lock);
1685 * If event->ctx is a cloned context, callers must make sure that
1686 * every task struct that event->ctx->task could possibly point to
1687 * remains valid. This condition is satisfied when called through
1688 * perf_event_for_each_child or perf_event_for_each as described
1689 * for perf_event_disable.
1691 void perf_event_enable(struct perf_event *event)
1693 struct perf_event_context *ctx = event->ctx;
1694 struct task_struct *task = ctx->task;
1698 * Enable the event on the cpu that it's on
1700 cpu_function_call(event->cpu, __perf_event_enable, event);
1704 raw_spin_lock_irq(&ctx->lock);
1705 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1709 * If the event is in error state, clear that first.
1710 * That way, if we see the event in error state below, we
1711 * know that it has gone back into error state, as distinct
1712 * from the task having been scheduled away before the
1713 * cross-call arrived.
1715 if (event->state == PERF_EVENT_STATE_ERROR)
1716 event->state = PERF_EVENT_STATE_OFF;
1719 if (!ctx->is_active) {
1720 __perf_event_mark_enabled(event, ctx);
1724 raw_spin_unlock_irq(&ctx->lock);
1726 if (!task_function_call(task, __perf_event_enable, event))
1729 raw_spin_lock_irq(&ctx->lock);
1732 * If the context is active and the event is still off,
1733 * we need to retry the cross-call.
1735 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1737 * task could have been flipped by a concurrent
1738 * perf_event_context_sched_out()
1745 raw_spin_unlock_irq(&ctx->lock);
1748 static int perf_event_refresh(struct perf_event *event, int refresh)
1751 * not supported on inherited events
1753 if (event->attr.inherit || !is_sampling_event(event))
1756 atomic_add(refresh, &event->event_limit);
1757 perf_event_enable(event);
1762 static void ctx_sched_out(struct perf_event_context *ctx,
1763 struct perf_cpu_context *cpuctx,
1764 enum event_type_t event_type)
1766 struct perf_event *event;
1768 raw_spin_lock(&ctx->lock);
1769 perf_pmu_disable(ctx->pmu);
1771 if (likely(!ctx->nr_events))
1773 update_context_time(ctx);
1774 update_cgrp_time_from_cpuctx(cpuctx);
1776 if (!ctx->nr_active)
1779 if (event_type & EVENT_PINNED) {
1780 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1781 group_sched_out(event, cpuctx, ctx);
1784 if (event_type & EVENT_FLEXIBLE) {
1785 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1786 group_sched_out(event, cpuctx, ctx);
1789 perf_pmu_enable(ctx->pmu);
1790 raw_spin_unlock(&ctx->lock);
1794 * Test whether two contexts are equivalent, i.e. whether they
1795 * have both been cloned from the same version of the same context
1796 * and they both have the same number of enabled events.
1797 * If the number of enabled events is the same, then the set
1798 * of enabled events should be the same, because these are both
1799 * inherited contexts, therefore we can't access individual events
1800 * in them directly with an fd; we can only enable/disable all
1801 * events via prctl, or enable/disable all events in a family
1802 * via ioctl, which will have the same effect on both contexts.
1804 static int context_equiv(struct perf_event_context *ctx1,
1805 struct perf_event_context *ctx2)
1807 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1808 && ctx1->parent_gen == ctx2->parent_gen
1809 && !ctx1->pin_count && !ctx2->pin_count;
1812 static void __perf_event_sync_stat(struct perf_event *event,
1813 struct perf_event *next_event)
1817 if (!event->attr.inherit_stat)
1821 * Update the event value, we cannot use perf_event_read()
1822 * because we're in the middle of a context switch and have IRQs
1823 * disabled, which upsets smp_call_function_single(), however
1824 * we know the event must be on the current CPU, therefore we
1825 * don't need to use it.
1827 switch (event->state) {
1828 case PERF_EVENT_STATE_ACTIVE:
1829 event->pmu->read(event);
1832 case PERF_EVENT_STATE_INACTIVE:
1833 update_event_times(event);
1841 * In order to keep per-task stats reliable we need to flip the event
1842 * values when we flip the contexts.
1844 value = local64_read(&next_event->count);
1845 value = local64_xchg(&event->count, value);
1846 local64_set(&next_event->count, value);
1848 swap(event->total_time_enabled, next_event->total_time_enabled);
1849 swap(event->total_time_running, next_event->total_time_running);
1852 * Since we swizzled the values, update the user visible data too.
1854 perf_event_update_userpage(event);
1855 perf_event_update_userpage(next_event);
1858 #define list_next_entry(pos, member) \
1859 list_entry(pos->member.next, typeof(*pos), member)
1861 static void perf_event_sync_stat(struct perf_event_context *ctx,
1862 struct perf_event_context *next_ctx)
1864 struct perf_event *event, *next_event;
1869 update_context_time(ctx);
1871 event = list_first_entry(&ctx->event_list,
1872 struct perf_event, event_entry);
1874 next_event = list_first_entry(&next_ctx->event_list,
1875 struct perf_event, event_entry);
1877 while (&event->event_entry != &ctx->event_list &&
1878 &next_event->event_entry != &next_ctx->event_list) {
1880 __perf_event_sync_stat(event, next_event);
1882 event = list_next_entry(event, event_entry);
1883 next_event = list_next_entry(next_event, event_entry);
1887 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1888 struct task_struct *next)
1890 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1891 struct perf_event_context *next_ctx;
1892 struct perf_event_context *parent;
1893 struct perf_cpu_context *cpuctx;
1899 cpuctx = __get_cpu_context(ctx);
1900 if (!cpuctx->task_ctx)
1904 parent = rcu_dereference(ctx->parent_ctx);
1905 next_ctx = next->perf_event_ctxp[ctxn];
1906 if (parent && next_ctx &&
1907 rcu_dereference(next_ctx->parent_ctx) == parent) {
1909 * Looks like the two contexts are clones, so we might be
1910 * able to optimize the context switch. We lock both
1911 * contexts and check that they are clones under the
1912 * lock (including re-checking that neither has been
1913 * uncloned in the meantime). It doesn't matter which
1914 * order we take the locks because no other cpu could
1915 * be trying to lock both of these tasks.
1917 raw_spin_lock(&ctx->lock);
1918 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1919 if (context_equiv(ctx, next_ctx)) {
1921 * XXX do we need a memory barrier of sorts
1922 * wrt to rcu_dereference() of perf_event_ctxp
1924 task->perf_event_ctxp[ctxn] = next_ctx;
1925 next->perf_event_ctxp[ctxn] = ctx;
1927 next_ctx->task = task;
1930 perf_event_sync_stat(ctx, next_ctx);
1932 raw_spin_unlock(&next_ctx->lock);
1933 raw_spin_unlock(&ctx->lock);
1938 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1939 cpuctx->task_ctx = NULL;
1943 #define for_each_task_context_nr(ctxn) \
1944 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1947 * Called from scheduler to remove the events of the current task,
1948 * with interrupts disabled.
1950 * We stop each event and update the event value in event->count.
1952 * This does not protect us against NMI, but disable()
1953 * sets the disabled bit in the control field of event _before_
1954 * accessing the event control register. If a NMI hits, then it will
1955 * not restart the event.
1957 void __perf_event_task_sched_out(struct task_struct *task,
1958 struct task_struct *next)
1962 for_each_task_context_nr(ctxn)
1963 perf_event_context_sched_out(task, ctxn, next);
1966 * if cgroup events exist on this CPU, then we need
1967 * to check if we have to switch out PMU state.
1968 * cgroup event are system-wide mode only
1970 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
1971 perf_cgroup_sched_out(task);
1974 static void task_ctx_sched_out(struct perf_event_context *ctx,
1975 enum event_type_t event_type)
1977 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1979 if (!cpuctx->task_ctx)
1982 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1985 ctx_sched_out(ctx, cpuctx, event_type);
1986 cpuctx->task_ctx = NULL;
1990 * Called with IRQs disabled
1992 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1993 enum event_type_t event_type)
1995 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
1999 ctx_pinned_sched_in(struct perf_event_context *ctx,
2000 struct perf_cpu_context *cpuctx)
2002 struct perf_event *event;
2004 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2005 if (event->state <= PERF_EVENT_STATE_OFF)
2007 if (!event_filter_match(event))
2010 /* may need to reset tstamp_enabled */
2011 if (is_cgroup_event(event))
2012 perf_cgroup_mark_enabled(event, ctx);
2014 if (group_can_go_on(event, cpuctx, 1))
2015 group_sched_in(event, cpuctx, ctx);
2018 * If this pinned group hasn't been scheduled,
2019 * put it in error state.
2021 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2022 update_group_times(event);
2023 event->state = PERF_EVENT_STATE_ERROR;
2029 ctx_flexible_sched_in(struct perf_event_context *ctx,
2030 struct perf_cpu_context *cpuctx)
2032 struct perf_event *event;
2035 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2036 /* Ignore events in OFF or ERROR state */
2037 if (event->state <= PERF_EVENT_STATE_OFF)
2040 * Listen to the 'cpu' scheduling filter constraint
2043 if (!event_filter_match(event))
2046 /* may need to reset tstamp_enabled */
2047 if (is_cgroup_event(event))
2048 perf_cgroup_mark_enabled(event, ctx);
2050 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2051 if (group_sched_in(event, cpuctx, ctx))
2058 ctx_sched_in(struct perf_event_context *ctx,
2059 struct perf_cpu_context *cpuctx,
2060 enum event_type_t event_type,
2061 struct task_struct *task)
2065 raw_spin_lock(&ctx->lock);
2067 if (likely(!ctx->nr_events))
2071 ctx->timestamp = now;
2072 perf_cgroup_set_timestamp(task, ctx);
2074 * First go through the list and put on any pinned groups
2075 * in order to give them the best chance of going on.
2077 if (event_type & EVENT_PINNED)
2078 ctx_pinned_sched_in(ctx, cpuctx);
2080 /* Then walk through the lower prio flexible groups */
2081 if (event_type & EVENT_FLEXIBLE)
2082 ctx_flexible_sched_in(ctx, cpuctx);
2085 raw_spin_unlock(&ctx->lock);
2088 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2089 enum event_type_t event_type,
2090 struct task_struct *task)
2092 struct perf_event_context *ctx = &cpuctx->ctx;
2094 ctx_sched_in(ctx, cpuctx, event_type, task);
2097 static void task_ctx_sched_in(struct perf_event_context *ctx,
2098 enum event_type_t event_type)
2100 struct perf_cpu_context *cpuctx;
2102 cpuctx = __get_cpu_context(ctx);
2103 if (cpuctx->task_ctx == ctx)
2106 ctx_sched_in(ctx, cpuctx, event_type, NULL);
2107 cpuctx->task_ctx = ctx;
2110 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2111 struct task_struct *task)
2113 struct perf_cpu_context *cpuctx;
2115 cpuctx = __get_cpu_context(ctx);
2116 if (cpuctx->task_ctx == ctx)
2119 perf_pmu_disable(ctx->pmu);
2121 * We want to keep the following priority order:
2122 * cpu pinned (that don't need to move), task pinned,
2123 * cpu flexible, task flexible.
2125 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2127 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2128 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2129 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2131 cpuctx->task_ctx = ctx;
2134 * Since these rotations are per-cpu, we need to ensure the
2135 * cpu-context we got scheduled on is actually rotating.
2137 perf_pmu_rotate_start(ctx->pmu);
2138 perf_pmu_enable(ctx->pmu);
2142 * Called from scheduler to add the events of the current task
2143 * with interrupts disabled.
2145 * We restore the event value and then enable it.
2147 * This does not protect us against NMI, but enable()
2148 * sets the enabled bit in the control field of event _before_
2149 * accessing the event control register. If a NMI hits, then it will
2150 * keep the event running.
2152 void __perf_event_task_sched_in(struct task_struct *task)
2154 struct perf_event_context *ctx;
2157 for_each_task_context_nr(ctxn) {
2158 ctx = task->perf_event_ctxp[ctxn];
2162 perf_event_context_sched_in(ctx, task);
2165 * if cgroup events exist on this CPU, then we need
2166 * to check if we have to switch in PMU state.
2167 * cgroup event are system-wide mode only
2169 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2170 perf_cgroup_sched_in(task);
2173 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2175 u64 frequency = event->attr.sample_freq;
2176 u64 sec = NSEC_PER_SEC;
2177 u64 divisor, dividend;
2179 int count_fls, nsec_fls, frequency_fls, sec_fls;
2181 count_fls = fls64(count);
2182 nsec_fls = fls64(nsec);
2183 frequency_fls = fls64(frequency);
2187 * We got @count in @nsec, with a target of sample_freq HZ
2188 * the target period becomes:
2191 * period = -------------------
2192 * @nsec * sample_freq
2197 * Reduce accuracy by one bit such that @a and @b converge
2198 * to a similar magnitude.
2200 #define REDUCE_FLS(a, b) \
2202 if (a##_fls > b##_fls) { \
2212 * Reduce accuracy until either term fits in a u64, then proceed with
2213 * the other, so that finally we can do a u64/u64 division.
2215 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2216 REDUCE_FLS(nsec, frequency);
2217 REDUCE_FLS(sec, count);
2220 if (count_fls + sec_fls > 64) {
2221 divisor = nsec * frequency;
2223 while (count_fls + sec_fls > 64) {
2224 REDUCE_FLS(count, sec);
2228 dividend = count * sec;
2230 dividend = count * sec;
2232 while (nsec_fls + frequency_fls > 64) {
2233 REDUCE_FLS(nsec, frequency);
2237 divisor = nsec * frequency;
2243 return div64_u64(dividend, divisor);
2246 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
2248 struct hw_perf_event *hwc = &event->hw;
2249 s64 period, sample_period;
2252 period = perf_calculate_period(event, nsec, count);
2254 delta = (s64)(period - hwc->sample_period);
2255 delta = (delta + 7) / 8; /* low pass filter */
2257 sample_period = hwc->sample_period + delta;
2262 hwc->sample_period = sample_period;
2264 if (local64_read(&hwc->period_left) > 8*sample_period) {
2265 event->pmu->stop(event, PERF_EF_UPDATE);
2266 local64_set(&hwc->period_left, 0);
2267 event->pmu->start(event, PERF_EF_RELOAD);
2271 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
2273 struct perf_event *event;
2274 struct hw_perf_event *hwc;
2275 u64 interrupts, now;
2278 raw_spin_lock(&ctx->lock);
2279 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2280 if (event->state != PERF_EVENT_STATE_ACTIVE)
2283 if (!event_filter_match(event))
2288 interrupts = hwc->interrupts;
2289 hwc->interrupts = 0;
2292 * unthrottle events on the tick
2294 if (interrupts == MAX_INTERRUPTS) {
2295 perf_log_throttle(event, 1);
2296 event->pmu->start(event, 0);
2299 if (!event->attr.freq || !event->attr.sample_freq)
2302 event->pmu->read(event);
2303 now = local64_read(&event->count);
2304 delta = now - hwc->freq_count_stamp;
2305 hwc->freq_count_stamp = now;
2308 perf_adjust_period(event, period, delta);
2310 raw_spin_unlock(&ctx->lock);
2314 * Round-robin a context's events:
2316 static void rotate_ctx(struct perf_event_context *ctx)
2318 raw_spin_lock(&ctx->lock);
2321 * Rotate the first entry last of non-pinned groups. Rotation might be
2322 * disabled by the inheritance code.
2324 if (!ctx->rotate_disable)
2325 list_rotate_left(&ctx->flexible_groups);
2327 raw_spin_unlock(&ctx->lock);
2331 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2332 * because they're strictly cpu affine and rotate_start is called with IRQs
2333 * disabled, while rotate_context is called from IRQ context.
2335 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
2337 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
2338 struct perf_event_context *ctx = NULL;
2339 int rotate = 0, remove = 1;
2341 if (cpuctx->ctx.nr_events) {
2343 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2347 ctx = cpuctx->task_ctx;
2348 if (ctx && ctx->nr_events) {
2350 if (ctx->nr_events != ctx->nr_active)
2354 perf_pmu_disable(cpuctx->ctx.pmu);
2355 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
2357 perf_ctx_adjust_freq(ctx, interval);
2362 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2364 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
2366 rotate_ctx(&cpuctx->ctx);
2370 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, current);
2372 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
2376 list_del_init(&cpuctx->rotation_list);
2378 perf_pmu_enable(cpuctx->ctx.pmu);
2381 void perf_event_task_tick(void)
2383 struct list_head *head = &__get_cpu_var(rotation_list);
2384 struct perf_cpu_context *cpuctx, *tmp;
2386 WARN_ON(!irqs_disabled());
2388 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2389 if (cpuctx->jiffies_interval == 1 ||
2390 !(jiffies % cpuctx->jiffies_interval))
2391 perf_rotate_context(cpuctx);
2395 static int event_enable_on_exec(struct perf_event *event,
2396 struct perf_event_context *ctx)
2398 if (!event->attr.enable_on_exec)
2401 event->attr.enable_on_exec = 0;
2402 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2405 __perf_event_mark_enabled(event, ctx);
2411 * Enable all of a task's events that have been marked enable-on-exec.
2412 * This expects task == current.
2414 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2416 struct perf_event *event;
2417 unsigned long flags;
2421 local_irq_save(flags);
2422 if (!ctx || !ctx->nr_events)
2425 task_ctx_sched_out(ctx, EVENT_ALL);
2427 raw_spin_lock(&ctx->lock);
2429 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2430 ret = event_enable_on_exec(event, ctx);
2435 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2436 ret = event_enable_on_exec(event, ctx);
2442 * Unclone this context if we enabled any event.
2447 raw_spin_unlock(&ctx->lock);
2449 perf_event_context_sched_in(ctx, ctx->task);
2451 local_irq_restore(flags);
2455 * Cross CPU call to read the hardware event
2457 static void __perf_event_read(void *info)
2459 struct perf_event *event = info;
2460 struct perf_event_context *ctx = event->ctx;
2461 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2464 * If this is a task context, we need to check whether it is
2465 * the current task context of this cpu. If not it has been
2466 * scheduled out before the smp call arrived. In that case
2467 * event->count would have been updated to a recent sample
2468 * when the event was scheduled out.
2470 if (ctx->task && cpuctx->task_ctx != ctx)
2473 raw_spin_lock(&ctx->lock);
2474 if (ctx->is_active) {
2475 update_context_time(ctx);
2476 update_cgrp_time_from_event(event);
2478 update_event_times(event);
2479 if (event->state == PERF_EVENT_STATE_ACTIVE)
2480 event->pmu->read(event);
2481 raw_spin_unlock(&ctx->lock);
2484 static inline u64 perf_event_count(struct perf_event *event)
2486 return local64_read(&event->count) + atomic64_read(&event->child_count);
2489 static u64 perf_event_read(struct perf_event *event)
2492 * If event is enabled and currently active on a CPU, update the
2493 * value in the event structure:
2495 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2496 smp_call_function_single(event->oncpu,
2497 __perf_event_read, event, 1);
2498 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2499 struct perf_event_context *ctx = event->ctx;
2500 unsigned long flags;
2502 raw_spin_lock_irqsave(&ctx->lock, flags);
2504 * may read while context is not active
2505 * (e.g., thread is blocked), in that case
2506 * we cannot update context time
2508 if (ctx->is_active) {
2509 update_context_time(ctx);
2510 update_cgrp_time_from_event(event);
2512 update_event_times(event);
2513 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2516 return perf_event_count(event);
2523 struct callchain_cpus_entries {
2524 struct rcu_head rcu_head;
2525 struct perf_callchain_entry *cpu_entries[0];
2528 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
2529 static atomic_t nr_callchain_events;
2530 static DEFINE_MUTEX(callchain_mutex);
2531 struct callchain_cpus_entries *callchain_cpus_entries;
2534 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
2535 struct pt_regs *regs)
2539 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
2540 struct pt_regs *regs)
2544 static void release_callchain_buffers_rcu(struct rcu_head *head)
2546 struct callchain_cpus_entries *entries;
2549 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
2551 for_each_possible_cpu(cpu)
2552 kfree(entries->cpu_entries[cpu]);
2557 static void release_callchain_buffers(void)
2559 struct callchain_cpus_entries *entries;
2561 entries = callchain_cpus_entries;
2562 rcu_assign_pointer(callchain_cpus_entries, NULL);
2563 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2566 static int alloc_callchain_buffers(void)
2570 struct callchain_cpus_entries *entries;
2573 * We can't use the percpu allocation API for data that can be
2574 * accessed from NMI. Use a temporary manual per cpu allocation
2575 * until that gets sorted out.
2577 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
2579 entries = kzalloc(size, GFP_KERNEL);
2583 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
2585 for_each_possible_cpu(cpu) {
2586 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2588 if (!entries->cpu_entries[cpu])
2592 rcu_assign_pointer(callchain_cpus_entries, entries);
2597 for_each_possible_cpu(cpu)
2598 kfree(entries->cpu_entries[cpu]);
2604 static int get_callchain_buffers(void)
2609 mutex_lock(&callchain_mutex);
2611 count = atomic_inc_return(&nr_callchain_events);
2612 if (WARN_ON_ONCE(count < 1)) {
2618 /* If the allocation failed, give up */
2619 if (!callchain_cpus_entries)
2624 err = alloc_callchain_buffers();
2626 release_callchain_buffers();
2628 mutex_unlock(&callchain_mutex);
2633 static void put_callchain_buffers(void)
2635 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2636 release_callchain_buffers();
2637 mutex_unlock(&callchain_mutex);
2641 static int get_recursion_context(int *recursion)
2649 else if (in_softirq())
2654 if (recursion[rctx])
2663 static inline void put_recursion_context(int *recursion, int rctx)
2669 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2672 struct callchain_cpus_entries *entries;
2674 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2678 entries = rcu_dereference(callchain_cpus_entries);
2682 cpu = smp_processor_id();
2684 return &entries->cpu_entries[cpu][*rctx];
2688 put_callchain_entry(int rctx)
2690 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2693 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2696 struct perf_callchain_entry *entry;
2699 entry = get_callchain_entry(&rctx);
2708 if (!user_mode(regs)) {
2709 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2710 perf_callchain_kernel(entry, regs);
2712 regs = task_pt_regs(current);
2718 perf_callchain_store(entry, PERF_CONTEXT_USER);
2719 perf_callchain_user(entry, regs);
2723 put_callchain_entry(rctx);
2729 * Initialize the perf_event context in a task_struct:
2731 static void __perf_event_init_context(struct perf_event_context *ctx)
2733 raw_spin_lock_init(&ctx->lock);
2734 mutex_init(&ctx->mutex);
2735 INIT_LIST_HEAD(&ctx->pinned_groups);
2736 INIT_LIST_HEAD(&ctx->flexible_groups);
2737 INIT_LIST_HEAD(&ctx->event_list);
2738 atomic_set(&ctx->refcount, 1);
2741 static struct perf_event_context *
2742 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2744 struct perf_event_context *ctx;
2746 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2750 __perf_event_init_context(ctx);
2753 get_task_struct(task);
2760 static struct task_struct *
2761 find_lively_task_by_vpid(pid_t vpid)
2763 struct task_struct *task;
2770 task = find_task_by_vpid(vpid);
2772 get_task_struct(task);
2776 return ERR_PTR(-ESRCH);
2778 /* Reuse ptrace permission checks for now. */
2780 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2785 put_task_struct(task);
2786 return ERR_PTR(err);
2791 * Returns a matching context with refcount and pincount.
2793 static struct perf_event_context *
2794 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2796 struct perf_event_context *ctx;
2797 struct perf_cpu_context *cpuctx;
2798 unsigned long flags;
2802 /* Must be root to operate on a CPU event: */
2803 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2804 return ERR_PTR(-EACCES);
2807 * We could be clever and allow to attach a event to an
2808 * offline CPU and activate it when the CPU comes up, but
2811 if (!cpu_online(cpu))
2812 return ERR_PTR(-ENODEV);
2814 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2823 ctxn = pmu->task_ctx_nr;
2828 ctx = perf_lock_task_context(task, ctxn, &flags);
2832 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2836 ctx = alloc_perf_context(pmu, task);
2844 mutex_lock(&task->perf_event_mutex);
2846 * If it has already passed perf_event_exit_task().
2847 * we must see PF_EXITING, it takes this mutex too.
2849 if (task->flags & PF_EXITING)
2851 else if (task->perf_event_ctxp[ctxn])
2855 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2857 mutex_unlock(&task->perf_event_mutex);
2859 if (unlikely(err)) {
2860 put_task_struct(task);
2872 return ERR_PTR(err);
2875 static void perf_event_free_filter(struct perf_event *event);
2877 static void free_event_rcu(struct rcu_head *head)
2879 struct perf_event *event;
2881 event = container_of(head, struct perf_event, rcu_head);
2883 put_pid_ns(event->ns);
2884 perf_event_free_filter(event);
2888 static void perf_buffer_put(struct perf_buffer *buffer);
2890 static void free_event(struct perf_event *event)
2892 irq_work_sync(&event->pending);
2894 if (!event->parent) {
2895 if (event->attach_state & PERF_ATTACH_TASK)
2896 jump_label_dec(&perf_sched_events);
2897 if (event->attr.mmap || event->attr.mmap_data)
2898 atomic_dec(&nr_mmap_events);
2899 if (event->attr.comm)
2900 atomic_dec(&nr_comm_events);
2901 if (event->attr.task)
2902 atomic_dec(&nr_task_events);
2903 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2904 put_callchain_buffers();
2905 if (is_cgroup_event(event)) {
2906 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
2907 jump_label_dec(&perf_sched_events);
2911 if (event->buffer) {
2912 perf_buffer_put(event->buffer);
2913 event->buffer = NULL;
2916 if (is_cgroup_event(event))
2917 perf_detach_cgroup(event);
2920 event->destroy(event);
2923 put_ctx(event->ctx);
2925 call_rcu(&event->rcu_head, free_event_rcu);
2928 int perf_event_release_kernel(struct perf_event *event)
2930 struct perf_event_context *ctx = event->ctx;
2933 * Remove from the PMU, can't get re-enabled since we got
2934 * here because the last ref went.
2936 perf_event_disable(event);
2938 WARN_ON_ONCE(ctx->parent_ctx);
2940 * There are two ways this annotation is useful:
2942 * 1) there is a lock recursion from perf_event_exit_task
2943 * see the comment there.
2945 * 2) there is a lock-inversion with mmap_sem through
2946 * perf_event_read_group(), which takes faults while
2947 * holding ctx->mutex, however this is called after
2948 * the last filedesc died, so there is no possibility
2949 * to trigger the AB-BA case.
2951 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2952 raw_spin_lock_irq(&ctx->lock);
2953 perf_group_detach(event);
2954 list_del_event(event, ctx);
2955 raw_spin_unlock_irq(&ctx->lock);
2956 mutex_unlock(&ctx->mutex);
2962 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2965 * Called when the last reference to the file is gone.
2967 static int perf_release(struct inode *inode, struct file *file)
2969 struct perf_event *event = file->private_data;
2970 struct task_struct *owner;
2972 file->private_data = NULL;
2975 owner = ACCESS_ONCE(event->owner);
2977 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2978 * !owner it means the list deletion is complete and we can indeed
2979 * free this event, otherwise we need to serialize on
2980 * owner->perf_event_mutex.
2982 smp_read_barrier_depends();
2985 * Since delayed_put_task_struct() also drops the last
2986 * task reference we can safely take a new reference
2987 * while holding the rcu_read_lock().
2989 get_task_struct(owner);
2994 mutex_lock(&owner->perf_event_mutex);
2996 * We have to re-check the event->owner field, if it is cleared
2997 * we raced with perf_event_exit_task(), acquiring the mutex
2998 * ensured they're done, and we can proceed with freeing the
3002 list_del_init(&event->owner_entry);
3003 mutex_unlock(&owner->perf_event_mutex);
3004 put_task_struct(owner);
3007 return perf_event_release_kernel(event);
3010 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
3012 struct perf_event *child;
3018 mutex_lock(&event->child_mutex);
3019 total += perf_event_read(event);
3020 *enabled += event->total_time_enabled +
3021 atomic64_read(&event->child_total_time_enabled);
3022 *running += event->total_time_running +
3023 atomic64_read(&event->child_total_time_running);
3025 list_for_each_entry(child, &event->child_list, child_list) {
3026 total += perf_event_read(child);
3027 *enabled += child->total_time_enabled;
3028 *running += child->total_time_running;
3030 mutex_unlock(&event->child_mutex);
3034 EXPORT_SYMBOL_GPL(perf_event_read_value);
3036 static int perf_event_read_group(struct perf_event *event,
3037 u64 read_format, char __user *buf)
3039 struct perf_event *leader = event->group_leader, *sub;
3040 int n = 0, size = 0, ret = -EFAULT;
3041 struct perf_event_context *ctx = leader->ctx;
3043 u64 count, enabled, running;
3045 mutex_lock(&ctx->mutex);
3046 count = perf_event_read_value(leader, &enabled, &running);
3048 values[n++] = 1 + leader->nr_siblings;
3049 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3050 values[n++] = enabled;
3051 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3052 values[n++] = running;
3053 values[n++] = count;
3054 if (read_format & PERF_FORMAT_ID)
3055 values[n++] = primary_event_id(leader);
3057 size = n * sizeof(u64);
3059 if (copy_to_user(buf, values, size))
3064 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3067 values[n++] = perf_event_read_value(sub, &enabled, &running);
3068 if (read_format & PERF_FORMAT_ID)
3069 values[n++] = primary_event_id(sub);
3071 size = n * sizeof(u64);
3073 if (copy_to_user(buf + ret, values, size)) {
3081 mutex_unlock(&ctx->mutex);
3086 static int perf_event_read_one(struct perf_event *event,
3087 u64 read_format, char __user *buf)
3089 u64 enabled, running;
3093 values[n++] = perf_event_read_value(event, &enabled, &running);
3094 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3095 values[n++] = enabled;
3096 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3097 values[n++] = running;
3098 if (read_format & PERF_FORMAT_ID)
3099 values[n++] = primary_event_id(event);
3101 if (copy_to_user(buf, values, n * sizeof(u64)))
3104 return n * sizeof(u64);
3108 * Read the performance event - simple non blocking version for now
3111 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3113 u64 read_format = event->attr.read_format;
3117 * Return end-of-file for a read on a event that is in
3118 * error state (i.e. because it was pinned but it couldn't be
3119 * scheduled on to the CPU at some point).
3121 if (event->state == PERF_EVENT_STATE_ERROR)
3124 if (count < event->read_size)
3127 WARN_ON_ONCE(event->ctx->parent_ctx);
3128 if (read_format & PERF_FORMAT_GROUP)
3129 ret = perf_event_read_group(event, read_format, buf);
3131 ret = perf_event_read_one(event, read_format, buf);
3137 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3139 struct perf_event *event = file->private_data;
3141 return perf_read_hw(event, buf, count);
3144 static unsigned int perf_poll(struct file *file, poll_table *wait)
3146 struct perf_event *event = file->private_data;
3147 struct perf_buffer *buffer;
3148 unsigned int events = POLL_HUP;
3151 buffer = rcu_dereference(event->buffer);
3153 events = atomic_xchg(&buffer->poll, 0);
3156 poll_wait(file, &event->waitq, wait);
3161 static void perf_event_reset(struct perf_event *event)
3163 (void)perf_event_read(event);
3164 local64_set(&event->count, 0);
3165 perf_event_update_userpage(event);
3169 * Holding the top-level event's child_mutex means that any
3170 * descendant process that has inherited this event will block
3171 * in sync_child_event if it goes to exit, thus satisfying the
3172 * task existence requirements of perf_event_enable/disable.
3174 static void perf_event_for_each_child(struct perf_event *event,
3175 void (*func)(struct perf_event *))
3177 struct perf_event *child;
3179 WARN_ON_ONCE(event->ctx->parent_ctx);
3180 mutex_lock(&event->child_mutex);
3182 list_for_each_entry(child, &event->child_list, child_list)
3184 mutex_unlock(&event->child_mutex);
3187 static void perf_event_for_each(struct perf_event *event,
3188 void (*func)(struct perf_event *))
3190 struct perf_event_context *ctx = event->ctx;
3191 struct perf_event *sibling;
3193 WARN_ON_ONCE(ctx->parent_ctx);
3194 mutex_lock(&ctx->mutex);
3195 event = event->group_leader;
3197 perf_event_for_each_child(event, func);
3199 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3200 perf_event_for_each_child(event, func);
3201 mutex_unlock(&ctx->mutex);
3204 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3206 struct perf_event_context *ctx = event->ctx;
3210 if (!is_sampling_event(event))
3213 if (copy_from_user(&value, arg, sizeof(value)))
3219 raw_spin_lock_irq(&ctx->lock);
3220 if (event->attr.freq) {
3221 if (value > sysctl_perf_event_sample_rate) {
3226 event->attr.sample_freq = value;
3228 event->attr.sample_period = value;
3229 event->hw.sample_period = value;
3232 raw_spin_unlock_irq(&ctx->lock);
3237 static const struct file_operations perf_fops;
3239 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
3243 file = fget_light(fd, fput_needed);
3245 return ERR_PTR(-EBADF);
3247 if (file->f_op != &perf_fops) {
3248 fput_light(file, *fput_needed);
3250 return ERR_PTR(-EBADF);
3253 return file->private_data;
3256 static int perf_event_set_output(struct perf_event *event,
3257 struct perf_event *output_event);
3258 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3260 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3262 struct perf_event *event = file->private_data;
3263 void (*func)(struct perf_event *);
3267 case PERF_EVENT_IOC_ENABLE:
3268 func = perf_event_enable;
3270 case PERF_EVENT_IOC_DISABLE:
3271 func = perf_event_disable;
3273 case PERF_EVENT_IOC_RESET:
3274 func = perf_event_reset;
3277 case PERF_EVENT_IOC_REFRESH:
3278 return perf_event_refresh(event, arg);
3280 case PERF_EVENT_IOC_PERIOD:
3281 return perf_event_period(event, (u64 __user *)arg);
3283 case PERF_EVENT_IOC_SET_OUTPUT:
3285 struct perf_event *output_event = NULL;
3286 int fput_needed = 0;
3290 output_event = perf_fget_light(arg, &fput_needed);
3291 if (IS_ERR(output_event))
3292 return PTR_ERR(output_event);
3295 ret = perf_event_set_output(event, output_event);
3297 fput_light(output_event->filp, fput_needed);
3302 case PERF_EVENT_IOC_SET_FILTER:
3303 return perf_event_set_filter(event, (void __user *)arg);
3309 if (flags & PERF_IOC_FLAG_GROUP)
3310 perf_event_for_each(event, func);
3312 perf_event_for_each_child(event, func);
3317 int perf_event_task_enable(void)
3319 struct perf_event *event;
3321 mutex_lock(¤t->perf_event_mutex);
3322 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3323 perf_event_for_each_child(event, perf_event_enable);
3324 mutex_unlock(¤t->perf_event_mutex);
3329 int perf_event_task_disable(void)
3331 struct perf_event *event;
3333 mutex_lock(¤t->perf_event_mutex);
3334 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3335 perf_event_for_each_child(event, perf_event_disable);
3336 mutex_unlock(¤t->perf_event_mutex);
3341 #ifndef PERF_EVENT_INDEX_OFFSET
3342 # define PERF_EVENT_INDEX_OFFSET 0
3345 static int perf_event_index(struct perf_event *event)
3347 if (event->hw.state & PERF_HES_STOPPED)
3350 if (event->state != PERF_EVENT_STATE_ACTIVE)
3353 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
3357 * Callers need to ensure there can be no nesting of this function, otherwise
3358 * the seqlock logic goes bad. We can not serialize this because the arch
3359 * code calls this from NMI context.
3361 void perf_event_update_userpage(struct perf_event *event)
3363 struct perf_event_mmap_page *userpg;
3364 struct perf_buffer *buffer;
3367 buffer = rcu_dereference(event->buffer);
3371 userpg = buffer->user_page;
3374 * Disable preemption so as to not let the corresponding user-space
3375 * spin too long if we get preempted.
3380 userpg->index = perf_event_index(event);
3381 userpg->offset = perf_event_count(event);
3382 if (event->state == PERF_EVENT_STATE_ACTIVE)
3383 userpg->offset -= local64_read(&event->hw.prev_count);
3385 userpg->time_enabled = event->total_time_enabled +
3386 atomic64_read(&event->child_total_time_enabled);
3388 userpg->time_running = event->total_time_running +
3389 atomic64_read(&event->child_total_time_running);
3398 static unsigned long perf_data_size(struct perf_buffer *buffer);
3401 perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
3403 long max_size = perf_data_size(buffer);
3406 buffer->watermark = min(max_size, watermark);
3408 if (!buffer->watermark)
3409 buffer->watermark = max_size / 2;
3411 if (flags & PERF_BUFFER_WRITABLE)
3412 buffer->writable = 1;
3414 atomic_set(&buffer->refcount, 1);
3417 #ifndef CONFIG_PERF_USE_VMALLOC
3420 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
3423 static struct page *
3424 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
3426 if (pgoff > buffer->nr_pages)
3430 return virt_to_page(buffer->user_page);
3432 return virt_to_page(buffer->data_pages[pgoff - 1]);
3435 static void *perf_mmap_alloc_page(int cpu)
3440 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
3441 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
3445 return page_address(page);
3448 static struct perf_buffer *
3449 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
3451 struct perf_buffer *buffer;
3455 size = sizeof(struct perf_buffer);
3456 size += nr_pages * sizeof(void *);
3458 buffer = kzalloc(size, GFP_KERNEL);
3462 buffer->user_page = perf_mmap_alloc_page(cpu);
3463 if (!buffer->user_page)
3464 goto fail_user_page;
3466 for (i = 0; i < nr_pages; i++) {
3467 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
3468 if (!buffer->data_pages[i])
3469 goto fail_data_pages;
3472 buffer->nr_pages = nr_pages;
3474 perf_buffer_init(buffer, watermark, flags);
3479 for (i--; i >= 0; i--)
3480 free_page((unsigned long)buffer->data_pages[i]);
3482 free_page((unsigned long)buffer->user_page);
3491 static void perf_mmap_free_page(unsigned long addr)
3493 struct page *page = virt_to_page((void *)addr);
3495 page->mapping = NULL;
3499 static void perf_buffer_free(struct perf_buffer *buffer)
3503 perf_mmap_free_page((unsigned long)buffer->user_page);
3504 for (i = 0; i < buffer->nr_pages; i++)
3505 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
3509 static inline int page_order(struct perf_buffer *buffer)
3517 * Back perf_mmap() with vmalloc memory.
3519 * Required for architectures that have d-cache aliasing issues.
3522 static inline int page_order(struct perf_buffer *buffer)
3524 return buffer->page_order;
3527 static struct page *
3528 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
3530 if (pgoff > (1UL << page_order(buffer)))
3533 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
3536 static void perf_mmap_unmark_page(void *addr)
3538 struct page *page = vmalloc_to_page(addr);
3540 page->mapping = NULL;
3543 static void perf_buffer_free_work(struct work_struct *work)
3545 struct perf_buffer *buffer;
3549 buffer = container_of(work, struct perf_buffer, work);
3550 nr = 1 << page_order(buffer);
3552 base = buffer->user_page;
3553 for (i = 0; i < nr + 1; i++)
3554 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
3560 static void perf_buffer_free(struct perf_buffer *buffer)
3562 schedule_work(&buffer->work);
3565 static struct perf_buffer *
3566 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
3568 struct perf_buffer *buffer;
3572 size = sizeof(struct perf_buffer);
3573 size += sizeof(void *);
3575 buffer = kzalloc(size, GFP_KERNEL);
3579 INIT_WORK(&buffer->work, perf_buffer_free_work);
3581 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
3585 buffer->user_page = all_buf;
3586 buffer->data_pages[0] = all_buf + PAGE_SIZE;
3587 buffer->page_order = ilog2(nr_pages);
3588 buffer->nr_pages = 1;
3590 perf_buffer_init(buffer, watermark, flags);
3603 static unsigned long perf_data_size(struct perf_buffer *buffer)
3605 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
3608 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3610 struct perf_event *event = vma->vm_file->private_data;
3611 struct perf_buffer *buffer;
3612 int ret = VM_FAULT_SIGBUS;
3614 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3615 if (vmf->pgoff == 0)
3621 buffer = rcu_dereference(event->buffer);
3625 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3628 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
3632 get_page(vmf->page);
3633 vmf->page->mapping = vma->vm_file->f_mapping;
3634 vmf->page->index = vmf->pgoff;
3643 static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
3645 struct perf_buffer *buffer;
3647 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
3648 perf_buffer_free(buffer);
3651 static struct perf_buffer *perf_buffer_get(struct perf_event *event)
3653 struct perf_buffer *buffer;
3656 buffer = rcu_dereference(event->buffer);
3658 if (!atomic_inc_not_zero(&buffer->refcount))
3666 static void perf_buffer_put(struct perf_buffer *buffer)
3668 if (!atomic_dec_and_test(&buffer->refcount))
3671 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
3674 static void perf_mmap_open(struct vm_area_struct *vma)
3676 struct perf_event *event = vma->vm_file->private_data;
3678 atomic_inc(&event->mmap_count);
3681 static void perf_mmap_close(struct vm_area_struct *vma)
3683 struct perf_event *event = vma->vm_file->private_data;
3685 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
3686 unsigned long size = perf_data_size(event->buffer);
3687 struct user_struct *user = event->mmap_user;
3688 struct perf_buffer *buffer = event->buffer;
3690 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
3691 vma->vm_mm->locked_vm -= event->mmap_locked;
3692 rcu_assign_pointer(event->buffer, NULL);
3693 mutex_unlock(&event->mmap_mutex);
3695 perf_buffer_put(buffer);
3700 static const struct vm_operations_struct perf_mmap_vmops = {
3701 .open = perf_mmap_open,
3702 .close = perf_mmap_close,
3703 .fault = perf_mmap_fault,
3704 .page_mkwrite = perf_mmap_fault,
3707 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3709 struct perf_event *event = file->private_data;
3710 unsigned long user_locked, user_lock_limit;
3711 struct user_struct *user = current_user();
3712 unsigned long locked, lock_limit;
3713 struct perf_buffer *buffer;
3714 unsigned long vma_size;
3715 unsigned long nr_pages;
3716 long user_extra, extra;
3717 int ret = 0, flags = 0;
3720 * Don't allow mmap() of inherited per-task counters. This would
3721 * create a performance issue due to all children writing to the
3724 if (event->cpu == -1 && event->attr.inherit)
3727 if (!(vma->vm_flags & VM_SHARED))
3730 vma_size = vma->vm_end - vma->vm_start;
3731 nr_pages = (vma_size / PAGE_SIZE) - 1;
3734 * If we have buffer pages ensure they're a power-of-two number, so we
3735 * can do bitmasks instead of modulo.
3737 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3740 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3743 if (vma->vm_pgoff != 0)
3746 WARN_ON_ONCE(event->ctx->parent_ctx);
3747 mutex_lock(&event->mmap_mutex);
3748 if (event->buffer) {
3749 if (event->buffer->nr_pages == nr_pages)
3750 atomic_inc(&event->buffer->refcount);
3756 user_extra = nr_pages + 1;
3757 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3760 * Increase the limit linearly with more CPUs:
3762 user_lock_limit *= num_online_cpus();
3764 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3767 if (user_locked > user_lock_limit)
3768 extra = user_locked - user_lock_limit;
3770 lock_limit = rlimit(RLIMIT_MEMLOCK);
3771 lock_limit >>= PAGE_SHIFT;
3772 locked = vma->vm_mm->locked_vm + extra;
3774 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3775 !capable(CAP_IPC_LOCK)) {
3780 WARN_ON(event->buffer);
3782 if (vma->vm_flags & VM_WRITE)
3783 flags |= PERF_BUFFER_WRITABLE;
3785 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3791 rcu_assign_pointer(event->buffer, buffer);
3793 atomic_long_add(user_extra, &user->locked_vm);
3794 event->mmap_locked = extra;
3795 event->mmap_user = get_current_user();
3796 vma->vm_mm->locked_vm += event->mmap_locked;
3800 atomic_inc(&event->mmap_count);
3801 mutex_unlock(&event->mmap_mutex);
3803 vma->vm_flags |= VM_RESERVED;
3804 vma->vm_ops = &perf_mmap_vmops;
3809 static int perf_fasync(int fd, struct file *filp, int on)
3811 struct inode *inode = filp->f_path.dentry->d_inode;
3812 struct perf_event *event = filp->private_data;
3815 mutex_lock(&inode->i_mutex);
3816 retval = fasync_helper(fd, filp, on, &event->fasync);
3817 mutex_unlock(&inode->i_mutex);
3825 static const struct file_operations perf_fops = {
3826 .llseek = no_llseek,
3827 .release = perf_release,
3830 .unlocked_ioctl = perf_ioctl,
3831 .compat_ioctl = perf_ioctl,
3833 .fasync = perf_fasync,
3839 * If there's data, ensure we set the poll() state and publish everything
3840 * to user-space before waking everybody up.
3843 void perf_event_wakeup(struct perf_event *event)
3845 wake_up_all(&event->waitq);
3847 if (event->pending_kill) {
3848 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3849 event->pending_kill = 0;
3853 static void perf_pending_event(struct irq_work *entry)
3855 struct perf_event *event = container_of(entry,
3856 struct perf_event, pending);
3858 if (event->pending_disable) {
3859 event->pending_disable = 0;
3860 __perf_event_disable(event);
3863 if (event->pending_wakeup) {
3864 event->pending_wakeup = 0;
3865 perf_event_wakeup(event);
3870 * We assume there is only KVM supporting the callbacks.
3871 * Later on, we might change it to a list if there is
3872 * another virtualization implementation supporting the callbacks.
3874 struct perf_guest_info_callbacks *perf_guest_cbs;
3876 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3878 perf_guest_cbs = cbs;
3881 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3883 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3885 perf_guest_cbs = NULL;
3888 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3893 static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
3894 unsigned long offset, unsigned long head)
3898 if (!buffer->writable)
3901 mask = perf_data_size(buffer) - 1;
3903 offset = (offset - tail) & mask;
3904 head = (head - tail) & mask;
3906 if ((int)(head - offset) < 0)
3912 static void perf_output_wakeup(struct perf_output_handle *handle)
3914 atomic_set(&handle->buffer->poll, POLL_IN);
3917 handle->event->pending_wakeup = 1;
3918 irq_work_queue(&handle->event->pending);
3920 perf_event_wakeup(handle->event);
3924 * We need to ensure a later event_id doesn't publish a head when a former
3925 * event isn't done writing. However since we need to deal with NMIs we
3926 * cannot fully serialize things.
3928 * We only publish the head (and generate a wakeup) when the outer-most
3931 static void perf_output_get_handle(struct perf_output_handle *handle)
3933 struct perf_buffer *buffer = handle->buffer;
3936 local_inc(&buffer->nest);
3937 handle->wakeup = local_read(&buffer->wakeup);
3940 static void perf_output_put_handle(struct perf_output_handle *handle)
3942 struct perf_buffer *buffer = handle->buffer;
3946 head = local_read(&buffer->head);
3949 * IRQ/NMI can happen here, which means we can miss a head update.
3952 if (!local_dec_and_test(&buffer->nest))
3956 * Publish the known good head. Rely on the full barrier implied
3957 * by atomic_dec_and_test() order the buffer->head read and this
3960 buffer->user_page->data_head = head;
3963 * Now check if we missed an update, rely on the (compiler)
3964 * barrier in atomic_dec_and_test() to re-read buffer->head.
3966 if (unlikely(head != local_read(&buffer->head))) {
3967 local_inc(&buffer->nest);
3971 if (handle->wakeup != local_read(&buffer->wakeup))
3972 perf_output_wakeup(handle);
3978 __always_inline void perf_output_copy(struct perf_output_handle *handle,
3979 const void *buf, unsigned int len)
3982 unsigned long size = min_t(unsigned long, handle->size, len);
3984 memcpy(handle->addr, buf, size);
3987 handle->addr += size;
3989 handle->size -= size;
3990 if (!handle->size) {
3991 struct perf_buffer *buffer = handle->buffer;
3994 handle->page &= buffer->nr_pages - 1;
3995 handle->addr = buffer->data_pages[handle->page];
3996 handle->size = PAGE_SIZE << page_order(buffer);
4001 static void __perf_event_header__init_id(struct perf_event_header *header,
4002 struct perf_sample_data *data,
4003 struct perf_event *event)
4005 u64 sample_type = event->attr.sample_type;
4007 data->type = sample_type;
4008 header->size += event->id_header_size;
4010 if (sample_type & PERF_SAMPLE_TID) {
4011 /* namespace issues */
4012 data->tid_entry.pid = perf_event_pid(event, current);
4013 data->tid_entry.tid = perf_event_tid(event, current);
4016 if (sample_type & PERF_SAMPLE_TIME)
4017 data->time = perf_clock();
4019 if (sample_type & PERF_SAMPLE_ID)
4020 data->id = primary_event_id(event);
4022 if (sample_type & PERF_SAMPLE_STREAM_ID)
4023 data->stream_id = event->id;
4025 if (sample_type & PERF_SAMPLE_CPU) {
4026 data->cpu_entry.cpu = raw_smp_processor_id();
4027 data->cpu_entry.reserved = 0;
4031 static void perf_event_header__init_id(struct perf_event_header *header,
4032 struct perf_sample_data *data,
4033 struct perf_event *event)
4035 if (event->attr.sample_id_all)
4036 __perf_event_header__init_id(header, data, event);
4039 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4040 struct perf_sample_data *data)
4042 u64 sample_type = data->type;
4044 if (sample_type & PERF_SAMPLE_TID)
4045 perf_output_put(handle, data->tid_entry);
4047 if (sample_type & PERF_SAMPLE_TIME)
4048 perf_output_put(handle, data->time);
4050 if (sample_type & PERF_SAMPLE_ID)
4051 perf_output_put(handle, data->id);
4053 if (sample_type & PERF_SAMPLE_STREAM_ID)
4054 perf_output_put(handle, data->stream_id);
4056 if (sample_type & PERF_SAMPLE_CPU)
4057 perf_output_put(handle, data->cpu_entry);
4060 static void perf_event__output_id_sample(struct perf_event *event,
4061 struct perf_output_handle *handle,
4062 struct perf_sample_data *sample)
4064 if (event->attr.sample_id_all)
4065 __perf_event__output_id_sample(handle, sample);
4068 int perf_output_begin(struct perf_output_handle *handle,
4069 struct perf_event *event, unsigned int size,
4070 int nmi, int sample)
4072 struct perf_buffer *buffer;
4073 unsigned long tail, offset, head;
4075 struct perf_sample_data sample_data;
4077 struct perf_event_header header;
4084 * For inherited events we send all the output towards the parent.
4087 event = event->parent;
4089 buffer = rcu_dereference(event->buffer);
4093 handle->buffer = buffer;
4094 handle->event = event;
4096 handle->sample = sample;
4098 if (!buffer->nr_pages)
4101 have_lost = local_read(&buffer->lost);
4103 lost_event.header.size = sizeof(lost_event);
4104 perf_event_header__init_id(&lost_event.header, &sample_data,
4106 size += lost_event.header.size;
4109 perf_output_get_handle(handle);
4113 * Userspace could choose to issue a mb() before updating the
4114 * tail pointer. So that all reads will be completed before the
4117 tail = ACCESS_ONCE(buffer->user_page->data_tail);
4119 offset = head = local_read(&buffer->head);
4121 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
4123 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
4125 if (head - local_read(&buffer->wakeup) > buffer->watermark)
4126 local_add(buffer->watermark, &buffer->wakeup);
4128 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
4129 handle->page &= buffer->nr_pages - 1;
4130 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
4131 handle->addr = buffer->data_pages[handle->page];
4132 handle->addr += handle->size;
4133 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
4136 lost_event.header.type = PERF_RECORD_LOST;
4137 lost_event.header.misc = 0;
4138 lost_event.id = event->id;
4139 lost_event.lost = local_xchg(&buffer->lost, 0);
4141 perf_output_put(handle, lost_event);
4142 perf_event__output_id_sample(event, handle, &sample_data);
4148 local_inc(&buffer->lost);
4149 perf_output_put_handle(handle);
4156 void perf_output_end(struct perf_output_handle *handle)
4158 struct perf_event *event = handle->event;
4159 struct perf_buffer *buffer = handle->buffer;
4161 int wakeup_events = event->attr.wakeup_events;
4163 if (handle->sample && wakeup_events) {
4164 int events = local_inc_return(&buffer->events);
4165 if (events >= wakeup_events) {
4166 local_sub(wakeup_events, &buffer->events);
4167 local_inc(&buffer->wakeup);
4171 perf_output_put_handle(handle);
4175 static void perf_output_read_one(struct perf_output_handle *handle,
4176 struct perf_event *event,
4177 u64 enabled, u64 running)
4179 u64 read_format = event->attr.read_format;
4183 values[n++] = perf_event_count(event);
4184 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4185 values[n++] = enabled +
4186 atomic64_read(&event->child_total_time_enabled);
4188 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4189 values[n++] = running +
4190 atomic64_read(&event->child_total_time_running);
4192 if (read_format & PERF_FORMAT_ID)
4193 values[n++] = primary_event_id(event);
4195 perf_output_copy(handle, values, n * sizeof(u64));
4199 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4201 static void perf_output_read_group(struct perf_output_handle *handle,
4202 struct perf_event *event,
4203 u64 enabled, u64 running)
4205 struct perf_event *leader = event->group_leader, *sub;
4206 u64 read_format = event->attr.read_format;
4210 values[n++] = 1 + leader->nr_siblings;
4212 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4213 values[n++] = enabled;
4215 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4216 values[n++] = running;
4218 if (leader != event)
4219 leader->pmu->read(leader);
4221 values[n++] = perf_event_count(leader);
4222 if (read_format & PERF_FORMAT_ID)
4223 values[n++] = primary_event_id(leader);
4225 perf_output_copy(handle, values, n * sizeof(u64));
4227 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4231 sub->pmu->read(sub);
4233 values[n++] = perf_event_count(sub);
4234 if (read_format & PERF_FORMAT_ID)
4235 values[n++] = primary_event_id(sub);
4237 perf_output_copy(handle, values, n * sizeof(u64));
4241 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4242 PERF_FORMAT_TOTAL_TIME_RUNNING)
4244 static void perf_output_read(struct perf_output_handle *handle,
4245 struct perf_event *event)
4247 u64 enabled = 0, running = 0, now, ctx_time;
4248 u64 read_format = event->attr.read_format;
4251 * compute total_time_enabled, total_time_running
4252 * based on snapshot values taken when the event
4253 * was last scheduled in.
4255 * we cannot simply called update_context_time()
4256 * because of locking issue as we are called in
4259 if (read_format & PERF_FORMAT_TOTAL_TIMES) {
4261 ctx_time = event->shadow_ctx_time + now;
4262 enabled = ctx_time - event->tstamp_enabled;
4263 running = ctx_time - event->tstamp_running;
4266 if (event->attr.read_format & PERF_FORMAT_GROUP)
4267 perf_output_read_group(handle, event, enabled, running);
4269 perf_output_read_one(handle, event, enabled, running);
4272 void perf_output_sample(struct perf_output_handle *handle,
4273 struct perf_event_header *header,
4274 struct perf_sample_data *data,
4275 struct perf_event *event)
4277 u64 sample_type = data->type;
4279 perf_output_put(handle, *header);
4281 if (sample_type & PERF_SAMPLE_IP)
4282 perf_output_put(handle, data->ip);
4284 if (sample_type & PERF_SAMPLE_TID)
4285 perf_output_put(handle, data->tid_entry);
4287 if (sample_type & PERF_SAMPLE_TIME)
4288 perf_output_put(handle, data->time);
4290 if (sample_type & PERF_SAMPLE_ADDR)
4291 perf_output_put(handle, data->addr);
4293 if (sample_type & PERF_SAMPLE_ID)
4294 perf_output_put(handle, data->id);
4296 if (sample_type & PERF_SAMPLE_STREAM_ID)
4297 perf_output_put(handle, data->stream_id);
4299 if (sample_type & PERF_SAMPLE_CPU)
4300 perf_output_put(handle, data->cpu_entry);
4302 if (sample_type & PERF_SAMPLE_PERIOD)
4303 perf_output_put(handle, data->period);
4305 if (sample_type & PERF_SAMPLE_READ)
4306 perf_output_read(handle, event);
4308 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4309 if (data->callchain) {
4312 if (data->callchain)
4313 size += data->callchain->nr;
4315 size *= sizeof(u64);
4317 perf_output_copy(handle, data->callchain, size);
4320 perf_output_put(handle, nr);
4324 if (sample_type & PERF_SAMPLE_RAW) {
4326 perf_output_put(handle, data->raw->size);
4327 perf_output_copy(handle, data->raw->data,
4334 .size = sizeof(u32),
4337 perf_output_put(handle, raw);
4342 void perf_prepare_sample(struct perf_event_header *header,
4343 struct perf_sample_data *data,
4344 struct perf_event *event,
4345 struct pt_regs *regs)
4347 u64 sample_type = event->attr.sample_type;
4349 header->type = PERF_RECORD_SAMPLE;
4350 header->size = sizeof(*header) + event->header_size;
4353 header->misc |= perf_misc_flags(regs);
4355 __perf_event_header__init_id(header, data, event);
4357 if (sample_type & PERF_SAMPLE_IP)
4358 data->ip = perf_instruction_pointer(regs);
4360 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4363 data->callchain = perf_callchain(regs);
4365 if (data->callchain)
4366 size += data->callchain->nr;
4368 header->size += size * sizeof(u64);
4371 if (sample_type & PERF_SAMPLE_RAW) {
4372 int size = sizeof(u32);
4375 size += data->raw->size;
4377 size += sizeof(u32);
4379 WARN_ON_ONCE(size & (sizeof(u64)-1));
4380 header->size += size;
4384 static void perf_event_output(struct perf_event *event, int nmi,
4385 struct perf_sample_data *data,
4386 struct pt_regs *regs)
4388 struct perf_output_handle handle;
4389 struct perf_event_header header;
4391 /* protect the callchain buffers */
4394 perf_prepare_sample(&header, data, event, regs);
4396 if (perf_output_begin(&handle, event, header.size, nmi, 1))
4399 perf_output_sample(&handle, &header, data, event);
4401 perf_output_end(&handle);
4411 struct perf_read_event {
4412 struct perf_event_header header;
4419 perf_event_read_event(struct perf_event *event,
4420 struct task_struct *task)
4422 struct perf_output_handle handle;
4423 struct perf_sample_data sample;
4424 struct perf_read_event read_event = {
4426 .type = PERF_RECORD_READ,
4428 .size = sizeof(read_event) + event->read_size,
4430 .pid = perf_event_pid(event, task),
4431 .tid = perf_event_tid(event, task),
4435 perf_event_header__init_id(&read_event.header, &sample, event);
4436 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
4440 perf_output_put(&handle, read_event);
4441 perf_output_read(&handle, event);
4442 perf_event__output_id_sample(event, &handle, &sample);
4444 perf_output_end(&handle);
4448 * task tracking -- fork/exit
4450 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4453 struct perf_task_event {
4454 struct task_struct *task;
4455 struct perf_event_context *task_ctx;
4458 struct perf_event_header header;
4468 static void perf_event_task_output(struct perf_event *event,
4469 struct perf_task_event *task_event)
4471 struct perf_output_handle handle;
4472 struct perf_sample_data sample;
4473 struct task_struct *task = task_event->task;
4474 int ret, size = task_event->event_id.header.size;
4476 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4478 ret = perf_output_begin(&handle, event,
4479 task_event->event_id.header.size, 0, 0);
4483 task_event->event_id.pid = perf_event_pid(event, task);
4484 task_event->event_id.ppid = perf_event_pid(event, current);
4486 task_event->event_id.tid = perf_event_tid(event, task);
4487 task_event->event_id.ptid = perf_event_tid(event, current);
4489 perf_output_put(&handle, task_event->event_id);
4491 perf_event__output_id_sample(event, &handle, &sample);
4493 perf_output_end(&handle);
4495 task_event->event_id.header.size = size;
4498 static int perf_event_task_match(struct perf_event *event)
4500 if (event->state < PERF_EVENT_STATE_INACTIVE)
4503 if (!event_filter_match(event))
4506 if (event->attr.comm || event->attr.mmap ||
4507 event->attr.mmap_data || event->attr.task)
4513 static void perf_event_task_ctx(struct perf_event_context *ctx,
4514 struct perf_task_event *task_event)
4516 struct perf_event *event;
4518 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4519 if (perf_event_task_match(event))
4520 perf_event_task_output(event, task_event);
4524 static void perf_event_task_event(struct perf_task_event *task_event)
4526 struct perf_cpu_context *cpuctx;
4527 struct perf_event_context *ctx;
4532 list_for_each_entry_rcu(pmu, &pmus, entry) {
4533 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4534 if (cpuctx->active_pmu != pmu)
4536 perf_event_task_ctx(&cpuctx->ctx, task_event);
4538 ctx = task_event->task_ctx;
4540 ctxn = pmu->task_ctx_nr;
4543 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4546 perf_event_task_ctx(ctx, task_event);
4548 put_cpu_ptr(pmu->pmu_cpu_context);
4553 static void perf_event_task(struct task_struct *task,
4554 struct perf_event_context *task_ctx,
4557 struct perf_task_event task_event;
4559 if (!atomic_read(&nr_comm_events) &&
4560 !atomic_read(&nr_mmap_events) &&
4561 !atomic_read(&nr_task_events))
4564 task_event = (struct perf_task_event){
4566 .task_ctx = task_ctx,
4569 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4571 .size = sizeof(task_event.event_id),
4577 .time = perf_clock(),
4581 perf_event_task_event(&task_event);
4584 void perf_event_fork(struct task_struct *task)
4586 perf_event_task(task, NULL, 1);
4593 struct perf_comm_event {
4594 struct task_struct *task;
4599 struct perf_event_header header;
4606 static void perf_event_comm_output(struct perf_event *event,
4607 struct perf_comm_event *comm_event)
4609 struct perf_output_handle handle;
4610 struct perf_sample_data sample;
4611 int size = comm_event->event_id.header.size;
4614 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4615 ret = perf_output_begin(&handle, event,
4616 comm_event->event_id.header.size, 0, 0);
4621 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4622 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4624 perf_output_put(&handle, comm_event->event_id);
4625 perf_output_copy(&handle, comm_event->comm,
4626 comm_event->comm_size);
4628 perf_event__output_id_sample(event, &handle, &sample);
4630 perf_output_end(&handle);
4632 comm_event->event_id.header.size = size;
4635 static int perf_event_comm_match(struct perf_event *event)
4637 if (event->state < PERF_EVENT_STATE_INACTIVE)
4640 if (!event_filter_match(event))
4643 if (event->attr.comm)
4649 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4650 struct perf_comm_event *comm_event)
4652 struct perf_event *event;
4654 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4655 if (perf_event_comm_match(event))
4656 perf_event_comm_output(event, comm_event);
4660 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4662 struct perf_cpu_context *cpuctx;
4663 struct perf_event_context *ctx;
4664 char comm[TASK_COMM_LEN];
4669 memset(comm, 0, sizeof(comm));
4670 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4671 size = ALIGN(strlen(comm)+1, sizeof(u64));
4673 comm_event->comm = comm;
4674 comm_event->comm_size = size;
4676 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4678 list_for_each_entry_rcu(pmu, &pmus, entry) {
4679 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4680 if (cpuctx->active_pmu != pmu)
4682 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4684 ctxn = pmu->task_ctx_nr;
4688 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4690 perf_event_comm_ctx(ctx, comm_event);
4692 put_cpu_ptr(pmu->pmu_cpu_context);
4697 void perf_event_comm(struct task_struct *task)
4699 struct perf_comm_event comm_event;
4700 struct perf_event_context *ctx;
4703 for_each_task_context_nr(ctxn) {
4704 ctx = task->perf_event_ctxp[ctxn];
4708 perf_event_enable_on_exec(ctx);
4711 if (!atomic_read(&nr_comm_events))
4714 comm_event = (struct perf_comm_event){
4720 .type = PERF_RECORD_COMM,
4729 perf_event_comm_event(&comm_event);
4736 struct perf_mmap_event {
4737 struct vm_area_struct *vma;
4739 const char *file_name;
4743 struct perf_event_header header;
4753 static void perf_event_mmap_output(struct perf_event *event,
4754 struct perf_mmap_event *mmap_event)
4756 struct perf_output_handle handle;
4757 struct perf_sample_data sample;
4758 int size = mmap_event->event_id.header.size;
4761 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4762 ret = perf_output_begin(&handle, event,
4763 mmap_event->event_id.header.size, 0, 0);
4767 mmap_event->event_id.pid = perf_event_pid(event, current);
4768 mmap_event->event_id.tid = perf_event_tid(event, current);
4770 perf_output_put(&handle, mmap_event->event_id);
4771 perf_output_copy(&handle, mmap_event->file_name,
4772 mmap_event->file_size);
4774 perf_event__output_id_sample(event, &handle, &sample);
4776 perf_output_end(&handle);
4778 mmap_event->event_id.header.size = size;
4781 static int perf_event_mmap_match(struct perf_event *event,
4782 struct perf_mmap_event *mmap_event,
4785 if (event->state < PERF_EVENT_STATE_INACTIVE)
4788 if (!event_filter_match(event))
4791 if ((!executable && event->attr.mmap_data) ||
4792 (executable && event->attr.mmap))
4798 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4799 struct perf_mmap_event *mmap_event,
4802 struct perf_event *event;
4804 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4805 if (perf_event_mmap_match(event, mmap_event, executable))
4806 perf_event_mmap_output(event, mmap_event);
4810 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4812 struct perf_cpu_context *cpuctx;
4813 struct perf_event_context *ctx;
4814 struct vm_area_struct *vma = mmap_event->vma;
4815 struct file *file = vma->vm_file;
4823 memset(tmp, 0, sizeof(tmp));
4827 * d_path works from the end of the buffer backwards, so we
4828 * need to add enough zero bytes after the string to handle
4829 * the 64bit alignment we do later.
4831 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4833 name = strncpy(tmp, "//enomem", sizeof(tmp));
4836 name = d_path(&file->f_path, buf, PATH_MAX);
4838 name = strncpy(tmp, "//toolong", sizeof(tmp));
4842 if (arch_vma_name(mmap_event->vma)) {
4843 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4849 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4851 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4852 vma->vm_end >= vma->vm_mm->brk) {
4853 name = strncpy(tmp, "[heap]", sizeof(tmp));
4855 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4856 vma->vm_end >= vma->vm_mm->start_stack) {
4857 name = strncpy(tmp, "[stack]", sizeof(tmp));
4861 name = strncpy(tmp, "//anon", sizeof(tmp));
4866 size = ALIGN(strlen(name)+1, sizeof(u64));
4868 mmap_event->file_name = name;
4869 mmap_event->file_size = size;
4871 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4874 list_for_each_entry_rcu(pmu, &pmus, entry) {
4875 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4876 if (cpuctx->active_pmu != pmu)
4878 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4879 vma->vm_flags & VM_EXEC);
4881 ctxn = pmu->task_ctx_nr;
4885 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4887 perf_event_mmap_ctx(ctx, mmap_event,
4888 vma->vm_flags & VM_EXEC);
4891 put_cpu_ptr(pmu->pmu_cpu_context);
4898 void perf_event_mmap(struct vm_area_struct *vma)
4900 struct perf_mmap_event mmap_event;
4902 if (!atomic_read(&nr_mmap_events))
4905 mmap_event = (struct perf_mmap_event){
4911 .type = PERF_RECORD_MMAP,
4912 .misc = PERF_RECORD_MISC_USER,
4917 .start = vma->vm_start,
4918 .len = vma->vm_end - vma->vm_start,
4919 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4923 perf_event_mmap_event(&mmap_event);
4927 * IRQ throttle logging
4930 static void perf_log_throttle(struct perf_event *event, int enable)
4932 struct perf_output_handle handle;
4933 struct perf_sample_data sample;
4937 struct perf_event_header header;
4941 } throttle_event = {
4943 .type = PERF_RECORD_THROTTLE,
4945 .size = sizeof(throttle_event),
4947 .time = perf_clock(),
4948 .id = primary_event_id(event),
4949 .stream_id = event->id,
4953 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4955 perf_event_header__init_id(&throttle_event.header, &sample, event);
4957 ret = perf_output_begin(&handle, event,
4958 throttle_event.header.size, 1, 0);
4962 perf_output_put(&handle, throttle_event);
4963 perf_event__output_id_sample(event, &handle, &sample);
4964 perf_output_end(&handle);
4968 * Generic event overflow handling, sampling.
4971 static int __perf_event_overflow(struct perf_event *event, int nmi,
4972 int throttle, struct perf_sample_data *data,
4973 struct pt_regs *regs)
4975 int events = atomic_read(&event->event_limit);
4976 struct hw_perf_event *hwc = &event->hw;
4980 * Non-sampling counters might still use the PMI to fold short
4981 * hardware counters, ignore those.
4983 if (unlikely(!is_sampling_event(event)))
4986 if (unlikely(hwc->interrupts >= max_samples_per_tick)) {
4988 hwc->interrupts = MAX_INTERRUPTS;
4989 perf_log_throttle(event, 0);
4995 if (event->attr.freq) {
4996 u64 now = perf_clock();
4997 s64 delta = now - hwc->freq_time_stamp;
4999 hwc->freq_time_stamp = now;
5001 if (delta > 0 && delta < 2*TICK_NSEC)
5002 perf_adjust_period(event, delta, hwc->last_period);
5006 * XXX event_limit might not quite work as expected on inherited
5010 event->pending_kill = POLL_IN;
5011 if (events && atomic_dec_and_test(&event->event_limit)) {
5013 event->pending_kill = POLL_HUP;
5015 event->pending_disable = 1;
5016 irq_work_queue(&event->pending);
5018 perf_event_disable(event);
5021 if (event->overflow_handler)
5022 event->overflow_handler(event, nmi, data, regs);
5024 perf_event_output(event, nmi, data, regs);
5029 int perf_event_overflow(struct perf_event *event, int nmi,
5030 struct perf_sample_data *data,
5031 struct pt_regs *regs)
5033 return __perf_event_overflow(event, nmi, 1, data, regs);
5037 * Generic software event infrastructure
5040 struct swevent_htable {
5041 struct swevent_hlist *swevent_hlist;
5042 struct mutex hlist_mutex;
5045 /* Recursion avoidance in each contexts */
5046 int recursion[PERF_NR_CONTEXTS];
5049 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5052 * We directly increment event->count and keep a second value in
5053 * event->hw.period_left to count intervals. This period event
5054 * is kept in the range [-sample_period, 0] so that we can use the
5058 static u64 perf_swevent_set_period(struct perf_event *event)
5060 struct hw_perf_event *hwc = &event->hw;
5061 u64 period = hwc->last_period;
5065 hwc->last_period = hwc->sample_period;
5068 old = val = local64_read(&hwc->period_left);
5072 nr = div64_u64(period + val, period);
5073 offset = nr * period;
5075 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
5081 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
5082 int nmi, struct perf_sample_data *data,
5083 struct pt_regs *regs)
5085 struct hw_perf_event *hwc = &event->hw;
5088 data->period = event->hw.last_period;
5090 overflow = perf_swevent_set_period(event);
5092 if (hwc->interrupts == MAX_INTERRUPTS)
5095 for (; overflow; overflow--) {
5096 if (__perf_event_overflow(event, nmi, throttle,
5099 * We inhibit the overflow from happening when
5100 * hwc->interrupts == MAX_INTERRUPTS.
5108 static void perf_swevent_event(struct perf_event *event, u64 nr,
5109 int nmi, struct perf_sample_data *data,
5110 struct pt_regs *regs)
5112 struct hw_perf_event *hwc = &event->hw;
5114 local64_add(nr, &event->count);
5119 if (!is_sampling_event(event))
5122 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
5123 return perf_swevent_overflow(event, 1, nmi, data, regs);
5125 if (local64_add_negative(nr, &hwc->period_left))
5128 perf_swevent_overflow(event, 0, nmi, data, regs);
5131 static int perf_exclude_event(struct perf_event *event,
5132 struct pt_regs *regs)
5134 if (event->hw.state & PERF_HES_STOPPED)
5138 if (event->attr.exclude_user && user_mode(regs))
5141 if (event->attr.exclude_kernel && !user_mode(regs))
5148 static int perf_swevent_match(struct perf_event *event,
5149 enum perf_type_id type,
5151 struct perf_sample_data *data,
5152 struct pt_regs *regs)
5154 if (event->attr.type != type)
5157 if (event->attr.config != event_id)
5160 if (perf_exclude_event(event, regs))
5166 static inline u64 swevent_hash(u64 type, u32 event_id)
5168 u64 val = event_id | (type << 32);
5170 return hash_64(val, SWEVENT_HLIST_BITS);
5173 static inline struct hlist_head *
5174 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
5176 u64 hash = swevent_hash(type, event_id);
5178 return &hlist->heads[hash];
5181 /* For the read side: events when they trigger */
5182 static inline struct hlist_head *
5183 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
5185 struct swevent_hlist *hlist;
5187 hlist = rcu_dereference(swhash->swevent_hlist);
5191 return __find_swevent_head(hlist, type, event_id);
5194 /* For the event head insertion and removal in the hlist */
5195 static inline struct hlist_head *
5196 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
5198 struct swevent_hlist *hlist;
5199 u32 event_id = event->attr.config;
5200 u64 type = event->attr.type;
5203 * Event scheduling is always serialized against hlist allocation
5204 * and release. Which makes the protected version suitable here.
5205 * The context lock guarantees that.
5207 hlist = rcu_dereference_protected(swhash->swevent_hlist,
5208 lockdep_is_held(&event->ctx->lock));
5212 return __find_swevent_head(hlist, type, event_id);
5215 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5217 struct perf_sample_data *data,
5218 struct pt_regs *regs)
5220 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5221 struct perf_event *event;
5222 struct hlist_node *node;
5223 struct hlist_head *head;
5226 head = find_swevent_head_rcu(swhash, type, event_id);
5230 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5231 if (perf_swevent_match(event, type, event_id, data, regs))
5232 perf_swevent_event(event, nr, nmi, data, regs);
5238 int perf_swevent_get_recursion_context(void)
5240 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5242 return get_recursion_context(swhash->recursion);
5244 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
5246 inline void perf_swevent_put_recursion_context(int rctx)
5248 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5250 put_recursion_context(swhash->recursion, rctx);
5253 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
5254 struct pt_regs *regs, u64 addr)
5256 struct perf_sample_data data;
5259 preempt_disable_notrace();
5260 rctx = perf_swevent_get_recursion_context();
5264 perf_sample_data_init(&data, addr);
5266 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
5268 perf_swevent_put_recursion_context(rctx);
5269 preempt_enable_notrace();
5272 static void perf_swevent_read(struct perf_event *event)
5276 static int perf_swevent_add(struct perf_event *event, int flags)
5278 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5279 struct hw_perf_event *hwc = &event->hw;
5280 struct hlist_head *head;
5282 if (is_sampling_event(event)) {
5283 hwc->last_period = hwc->sample_period;
5284 perf_swevent_set_period(event);
5287 hwc->state = !(flags & PERF_EF_START);
5289 head = find_swevent_head(swhash, event);
5290 if (WARN_ON_ONCE(!head))
5293 hlist_add_head_rcu(&event->hlist_entry, head);
5298 static void perf_swevent_del(struct perf_event *event, int flags)
5300 hlist_del_rcu(&event->hlist_entry);
5303 static void perf_swevent_start(struct perf_event *event, int flags)
5305 event->hw.state = 0;
5308 static void perf_swevent_stop(struct perf_event *event, int flags)
5310 event->hw.state = PERF_HES_STOPPED;
5313 /* Deref the hlist from the update side */
5314 static inline struct swevent_hlist *
5315 swevent_hlist_deref(struct swevent_htable *swhash)
5317 return rcu_dereference_protected(swhash->swevent_hlist,
5318 lockdep_is_held(&swhash->hlist_mutex));
5321 static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
5323 struct swevent_hlist *hlist;
5325 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
5329 static void swevent_hlist_release(struct swevent_htable *swhash)
5331 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
5336 rcu_assign_pointer(swhash->swevent_hlist, NULL);
5337 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
5340 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5342 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5344 mutex_lock(&swhash->hlist_mutex);
5346 if (!--swhash->hlist_refcount)
5347 swevent_hlist_release(swhash);
5349 mutex_unlock(&swhash->hlist_mutex);
5352 static void swevent_hlist_put(struct perf_event *event)
5356 if (event->cpu != -1) {
5357 swevent_hlist_put_cpu(event, event->cpu);
5361 for_each_possible_cpu(cpu)
5362 swevent_hlist_put_cpu(event, cpu);
5365 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5367 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5370 mutex_lock(&swhash->hlist_mutex);
5372 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
5373 struct swevent_hlist *hlist;
5375 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5380 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5382 swhash->hlist_refcount++;
5384 mutex_unlock(&swhash->hlist_mutex);
5389 static int swevent_hlist_get(struct perf_event *event)
5392 int cpu, failed_cpu;
5394 if (event->cpu != -1)
5395 return swevent_hlist_get_cpu(event, event->cpu);
5398 for_each_possible_cpu(cpu) {
5399 err = swevent_hlist_get_cpu(event, cpu);
5409 for_each_possible_cpu(cpu) {
5410 if (cpu == failed_cpu)
5412 swevent_hlist_put_cpu(event, cpu);
5419 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
5421 static void sw_perf_event_destroy(struct perf_event *event)
5423 u64 event_id = event->attr.config;
5425 WARN_ON(event->parent);
5427 jump_label_dec(&perf_swevent_enabled[event_id]);
5428 swevent_hlist_put(event);
5431 static int perf_swevent_init(struct perf_event *event)
5433 int event_id = event->attr.config;
5435 if (event->attr.type != PERF_TYPE_SOFTWARE)
5439 case PERF_COUNT_SW_CPU_CLOCK:
5440 case PERF_COUNT_SW_TASK_CLOCK:
5447 if (event_id >= PERF_COUNT_SW_MAX)
5450 if (!event->parent) {
5453 err = swevent_hlist_get(event);
5457 jump_label_inc(&perf_swevent_enabled[event_id]);
5458 event->destroy = sw_perf_event_destroy;
5464 static struct pmu perf_swevent = {
5465 .task_ctx_nr = perf_sw_context,
5467 .event_init = perf_swevent_init,
5468 .add = perf_swevent_add,
5469 .del = perf_swevent_del,
5470 .start = perf_swevent_start,
5471 .stop = perf_swevent_stop,
5472 .read = perf_swevent_read,
5475 #ifdef CONFIG_EVENT_TRACING
5477 static int perf_tp_filter_match(struct perf_event *event,
5478 struct perf_sample_data *data)
5480 void *record = data->raw->data;
5482 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5487 static int perf_tp_event_match(struct perf_event *event,
5488 struct perf_sample_data *data,
5489 struct pt_regs *regs)
5491 if (event->hw.state & PERF_HES_STOPPED)
5494 * All tracepoints are from kernel-space.
5496 if (event->attr.exclude_kernel)
5499 if (!perf_tp_filter_match(event, data))
5505 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5506 struct pt_regs *regs, struct hlist_head *head, int rctx)
5508 struct perf_sample_data data;
5509 struct perf_event *event;
5510 struct hlist_node *node;
5512 struct perf_raw_record raw = {
5517 perf_sample_data_init(&data, addr);
5520 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5521 if (perf_tp_event_match(event, &data, regs))
5522 perf_swevent_event(event, count, 1, &data, regs);
5525 perf_swevent_put_recursion_context(rctx);
5527 EXPORT_SYMBOL_GPL(perf_tp_event);
5529 static void tp_perf_event_destroy(struct perf_event *event)
5531 perf_trace_destroy(event);
5534 static int perf_tp_event_init(struct perf_event *event)
5538 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5541 err = perf_trace_init(event);
5545 event->destroy = tp_perf_event_destroy;
5550 static struct pmu perf_tracepoint = {
5551 .task_ctx_nr = perf_sw_context,
5553 .event_init = perf_tp_event_init,
5554 .add = perf_trace_add,
5555 .del = perf_trace_del,
5556 .start = perf_swevent_start,
5557 .stop = perf_swevent_stop,
5558 .read = perf_swevent_read,
5561 static inline void perf_tp_register(void)
5563 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5566 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5571 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5574 filter_str = strndup_user(arg, PAGE_SIZE);
5575 if (IS_ERR(filter_str))
5576 return PTR_ERR(filter_str);
5578 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5584 static void perf_event_free_filter(struct perf_event *event)
5586 ftrace_profile_free_filter(event);
5591 static inline void perf_tp_register(void)
5595 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5600 static void perf_event_free_filter(struct perf_event *event)
5604 #endif /* CONFIG_EVENT_TRACING */
5606 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5607 void perf_bp_event(struct perf_event *bp, void *data)
5609 struct perf_sample_data sample;
5610 struct pt_regs *regs = data;
5612 perf_sample_data_init(&sample, bp->attr.bp_addr);
5614 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5615 perf_swevent_event(bp, 1, 1, &sample, regs);
5620 * hrtimer based swevent callback
5623 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5625 enum hrtimer_restart ret = HRTIMER_RESTART;
5626 struct perf_sample_data data;
5627 struct pt_regs *regs;
5628 struct perf_event *event;
5631 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5633 if (event->state != PERF_EVENT_STATE_ACTIVE)
5634 return HRTIMER_NORESTART;
5636 event->pmu->read(event);
5638 perf_sample_data_init(&data, 0);
5639 data.period = event->hw.last_period;
5640 regs = get_irq_regs();
5642 if (regs && !perf_exclude_event(event, regs)) {
5643 if (!(event->attr.exclude_idle && current->pid == 0))
5644 if (perf_event_overflow(event, 0, &data, regs))
5645 ret = HRTIMER_NORESTART;
5648 period = max_t(u64, 10000, event->hw.sample_period);
5649 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5654 static void perf_swevent_start_hrtimer(struct perf_event *event)
5656 struct hw_perf_event *hwc = &event->hw;
5659 if (!is_sampling_event(event))
5662 period = local64_read(&hwc->period_left);
5667 local64_set(&hwc->period_left, 0);
5669 period = max_t(u64, 10000, hwc->sample_period);
5671 __hrtimer_start_range_ns(&hwc->hrtimer,
5672 ns_to_ktime(period), 0,
5673 HRTIMER_MODE_REL_PINNED, 0);
5676 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5678 struct hw_perf_event *hwc = &event->hw;
5680 if (is_sampling_event(event)) {
5681 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5682 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5684 hrtimer_cancel(&hwc->hrtimer);
5688 static void perf_swevent_init_hrtimer(struct perf_event *event)
5690 struct hw_perf_event *hwc = &event->hw;
5692 if (!is_sampling_event(event))
5695 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5696 hwc->hrtimer.function = perf_swevent_hrtimer;
5699 * Since hrtimers have a fixed rate, we can do a static freq->period
5700 * mapping and avoid the whole period adjust feedback stuff.
5702 if (event->attr.freq) {
5703 long freq = event->attr.sample_freq;
5705 event->attr.sample_period = NSEC_PER_SEC / freq;
5706 hwc->sample_period = event->attr.sample_period;
5707 local64_set(&hwc->period_left, hwc->sample_period);
5708 event->attr.freq = 0;
5713 * Software event: cpu wall time clock
5716 static void cpu_clock_event_update(struct perf_event *event)
5721 now = local_clock();
5722 prev = local64_xchg(&event->hw.prev_count, now);
5723 local64_add(now - prev, &event->count);
5726 static void cpu_clock_event_start(struct perf_event *event, int flags)
5728 local64_set(&event->hw.prev_count, local_clock());
5729 perf_swevent_start_hrtimer(event);
5732 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5734 perf_swevent_cancel_hrtimer(event);
5735 cpu_clock_event_update(event);
5738 static int cpu_clock_event_add(struct perf_event *event, int flags)
5740 if (flags & PERF_EF_START)
5741 cpu_clock_event_start(event, flags);
5746 static void cpu_clock_event_del(struct perf_event *event, int flags)
5748 cpu_clock_event_stop(event, flags);
5751 static void cpu_clock_event_read(struct perf_event *event)
5753 cpu_clock_event_update(event);
5756 static int cpu_clock_event_init(struct perf_event *event)
5758 if (event->attr.type != PERF_TYPE_SOFTWARE)
5761 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5764 perf_swevent_init_hrtimer(event);
5769 static struct pmu perf_cpu_clock = {
5770 .task_ctx_nr = perf_sw_context,
5772 .event_init = cpu_clock_event_init,
5773 .add = cpu_clock_event_add,
5774 .del = cpu_clock_event_del,
5775 .start = cpu_clock_event_start,
5776 .stop = cpu_clock_event_stop,
5777 .read = cpu_clock_event_read,
5781 * Software event: task time clock
5784 static void task_clock_event_update(struct perf_event *event, u64 now)
5789 prev = local64_xchg(&event->hw.prev_count, now);
5791 local64_add(delta, &event->count);
5794 static void task_clock_event_start(struct perf_event *event, int flags)
5796 local64_set(&event->hw.prev_count, event->ctx->time);
5797 perf_swevent_start_hrtimer(event);
5800 static void task_clock_event_stop(struct perf_event *event, int flags)
5802 perf_swevent_cancel_hrtimer(event);
5803 task_clock_event_update(event, event->ctx->time);
5806 static int task_clock_event_add(struct perf_event *event, int flags)
5808 if (flags & PERF_EF_START)
5809 task_clock_event_start(event, flags);
5814 static void task_clock_event_del(struct perf_event *event, int flags)
5816 task_clock_event_stop(event, PERF_EF_UPDATE);
5819 static void task_clock_event_read(struct perf_event *event)
5821 u64 now = perf_clock();
5822 u64 delta = now - event->ctx->timestamp;
5823 u64 time = event->ctx->time + delta;
5825 task_clock_event_update(event, time);
5828 static int task_clock_event_init(struct perf_event *event)
5830 if (event->attr.type != PERF_TYPE_SOFTWARE)
5833 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5836 perf_swevent_init_hrtimer(event);
5841 static struct pmu perf_task_clock = {
5842 .task_ctx_nr = perf_sw_context,
5844 .event_init = task_clock_event_init,
5845 .add = task_clock_event_add,
5846 .del = task_clock_event_del,
5847 .start = task_clock_event_start,
5848 .stop = task_clock_event_stop,
5849 .read = task_clock_event_read,
5852 static void perf_pmu_nop_void(struct pmu *pmu)
5856 static int perf_pmu_nop_int(struct pmu *pmu)
5861 static void perf_pmu_start_txn(struct pmu *pmu)
5863 perf_pmu_disable(pmu);
5866 static int perf_pmu_commit_txn(struct pmu *pmu)
5868 perf_pmu_enable(pmu);
5872 static void perf_pmu_cancel_txn(struct pmu *pmu)
5874 perf_pmu_enable(pmu);
5878 * Ensures all contexts with the same task_ctx_nr have the same
5879 * pmu_cpu_context too.
5881 static void *find_pmu_context(int ctxn)
5888 list_for_each_entry(pmu, &pmus, entry) {
5889 if (pmu->task_ctx_nr == ctxn)
5890 return pmu->pmu_cpu_context;
5896 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5900 for_each_possible_cpu(cpu) {
5901 struct perf_cpu_context *cpuctx;
5903 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5905 if (cpuctx->active_pmu == old_pmu)
5906 cpuctx->active_pmu = pmu;
5910 static void free_pmu_context(struct pmu *pmu)
5914 mutex_lock(&pmus_lock);
5916 * Like a real lame refcount.
5918 list_for_each_entry(i, &pmus, entry) {
5919 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5920 update_pmu_context(i, pmu);
5925 free_percpu(pmu->pmu_cpu_context);
5927 mutex_unlock(&pmus_lock);
5929 static struct idr pmu_idr;
5932 type_show(struct device *dev, struct device_attribute *attr, char *page)
5934 struct pmu *pmu = dev_get_drvdata(dev);
5936 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5939 static struct device_attribute pmu_dev_attrs[] = {
5944 static int pmu_bus_running;
5945 static struct bus_type pmu_bus = {
5946 .name = "event_source",
5947 .dev_attrs = pmu_dev_attrs,
5950 static void pmu_dev_release(struct device *dev)
5955 static int pmu_dev_alloc(struct pmu *pmu)
5959 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5963 device_initialize(pmu->dev);
5964 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5968 dev_set_drvdata(pmu->dev, pmu);
5969 pmu->dev->bus = &pmu_bus;
5970 pmu->dev->release = pmu_dev_release;
5971 ret = device_add(pmu->dev);
5979 put_device(pmu->dev);
5983 static struct lock_class_key cpuctx_mutex;
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 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
6005 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
6013 if (pmu_bus_running) {
6014 ret = pmu_dev_alloc(pmu);
6020 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6021 if (pmu->pmu_cpu_context)
6022 goto got_cpu_context;
6024 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6025 if (!pmu->pmu_cpu_context)
6028 for_each_possible_cpu(cpu) {
6029 struct perf_cpu_context *cpuctx;
6031 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6032 __perf_event_init_context(&cpuctx->ctx);
6033 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
6034 cpuctx->ctx.type = cpu_context;
6035 cpuctx->ctx.pmu = pmu;
6036 cpuctx->jiffies_interval = 1;
6037 INIT_LIST_HEAD(&cpuctx->rotation_list);
6038 cpuctx->active_pmu = pmu;
6042 if (!pmu->start_txn) {
6043 if (pmu->pmu_enable) {
6045 * If we have pmu_enable/pmu_disable calls, install
6046 * transaction stubs that use that to try and batch
6047 * hardware accesses.
6049 pmu->start_txn = perf_pmu_start_txn;
6050 pmu->commit_txn = perf_pmu_commit_txn;
6051 pmu->cancel_txn = perf_pmu_cancel_txn;
6053 pmu->start_txn = perf_pmu_nop_void;
6054 pmu->commit_txn = perf_pmu_nop_int;
6055 pmu->cancel_txn = perf_pmu_nop_void;
6059 if (!pmu->pmu_enable) {
6060 pmu->pmu_enable = perf_pmu_nop_void;
6061 pmu->pmu_disable = perf_pmu_nop_void;
6064 list_add_rcu(&pmu->entry, &pmus);
6067 mutex_unlock(&pmus_lock);
6072 device_del(pmu->dev);
6073 put_device(pmu->dev);
6076 if (pmu->type >= PERF_TYPE_MAX)
6077 idr_remove(&pmu_idr, pmu->type);
6080 free_percpu(pmu->pmu_disable_count);
6084 void perf_pmu_unregister(struct pmu *pmu)
6086 mutex_lock(&pmus_lock);
6087 list_del_rcu(&pmu->entry);
6088 mutex_unlock(&pmus_lock);
6091 * We dereference the pmu list under both SRCU and regular RCU, so
6092 * synchronize against both of those.
6094 synchronize_srcu(&pmus_srcu);
6097 free_percpu(pmu->pmu_disable_count);
6098 if (pmu->type >= PERF_TYPE_MAX)
6099 idr_remove(&pmu_idr, pmu->type);
6100 device_del(pmu->dev);
6101 put_device(pmu->dev);
6102 free_pmu_context(pmu);
6105 struct pmu *perf_init_event(struct perf_event *event)
6107 struct pmu *pmu = NULL;
6111 idx = srcu_read_lock(&pmus_srcu);
6114 pmu = idr_find(&pmu_idr, event->attr.type);
6117 ret = pmu->event_init(event);
6123 list_for_each_entry_rcu(pmu, &pmus, entry) {
6124 ret = pmu->event_init(event);
6128 if (ret != -ENOENT) {
6133 pmu = ERR_PTR(-ENOENT);
6135 srcu_read_unlock(&pmus_srcu, idx);
6141 * Allocate and initialize a event structure
6143 static struct perf_event *
6144 perf_event_alloc(struct perf_event_attr *attr, int cpu,
6145 struct task_struct *task,
6146 struct perf_event *group_leader,
6147 struct perf_event *parent_event,
6148 perf_overflow_handler_t overflow_handler)
6151 struct perf_event *event;
6152 struct hw_perf_event *hwc;
6155 if ((unsigned)cpu >= nr_cpu_ids) {
6156 if (!task || cpu != -1)
6157 return ERR_PTR(-EINVAL);
6160 event = kzalloc(sizeof(*event), GFP_KERNEL);
6162 return ERR_PTR(-ENOMEM);
6165 * Single events are their own group leaders, with an
6166 * empty sibling list:
6169 group_leader = event;
6171 mutex_init(&event->child_mutex);
6172 INIT_LIST_HEAD(&event->child_list);
6174 INIT_LIST_HEAD(&event->group_entry);
6175 INIT_LIST_HEAD(&event->event_entry);
6176 INIT_LIST_HEAD(&event->sibling_list);
6177 init_waitqueue_head(&event->waitq);
6178 init_irq_work(&event->pending, perf_pending_event);
6180 mutex_init(&event->mmap_mutex);
6183 event->attr = *attr;
6184 event->group_leader = group_leader;
6188 event->parent = parent_event;
6190 event->ns = get_pid_ns(current->nsproxy->pid_ns);
6191 event->id = atomic64_inc_return(&perf_event_id);
6193 event->state = PERF_EVENT_STATE_INACTIVE;
6196 event->attach_state = PERF_ATTACH_TASK;
6197 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6199 * hw_breakpoint is a bit difficult here..
6201 if (attr->type == PERF_TYPE_BREAKPOINT)
6202 event->hw.bp_target = task;
6206 if (!overflow_handler && parent_event)
6207 overflow_handler = parent_event->overflow_handler;
6209 event->overflow_handler = overflow_handler;
6212 event->state = PERF_EVENT_STATE_OFF;
6217 hwc->sample_period = attr->sample_period;
6218 if (attr->freq && attr->sample_freq)
6219 hwc->sample_period = 1;
6220 hwc->last_period = hwc->sample_period;
6222 local64_set(&hwc->period_left, hwc->sample_period);
6225 * we currently do not support PERF_FORMAT_GROUP on inherited events
6227 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6230 pmu = perf_init_event(event);
6236 else if (IS_ERR(pmu))
6241 put_pid_ns(event->ns);
6243 return ERR_PTR(err);
6248 if (!event->parent) {
6249 if (event->attach_state & PERF_ATTACH_TASK)
6250 jump_label_inc(&perf_sched_events);
6251 if (event->attr.mmap || event->attr.mmap_data)
6252 atomic_inc(&nr_mmap_events);
6253 if (event->attr.comm)
6254 atomic_inc(&nr_comm_events);
6255 if (event->attr.task)
6256 atomic_inc(&nr_task_events);
6257 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6258 err = get_callchain_buffers();
6261 return ERR_PTR(err);
6269 static int perf_copy_attr(struct perf_event_attr __user *uattr,
6270 struct perf_event_attr *attr)
6275 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6279 * zero the full structure, so that a short copy will be nice.
6281 memset(attr, 0, sizeof(*attr));
6283 ret = get_user(size, &uattr->size);
6287 if (size > PAGE_SIZE) /* silly large */
6290 if (!size) /* abi compat */
6291 size = PERF_ATTR_SIZE_VER0;
6293 if (size < PERF_ATTR_SIZE_VER0)
6297 * If we're handed a bigger struct than we know of,
6298 * ensure all the unknown bits are 0 - i.e. new
6299 * user-space does not rely on any kernel feature
6300 * extensions we dont know about yet.
6302 if (size > sizeof(*attr)) {
6303 unsigned char __user *addr;
6304 unsigned char __user *end;
6307 addr = (void __user *)uattr + sizeof(*attr);
6308 end = (void __user *)uattr + size;
6310 for (; addr < end; addr++) {
6311 ret = get_user(val, addr);
6317 size = sizeof(*attr);
6320 ret = copy_from_user(attr, uattr, size);
6325 * If the type exists, the corresponding creation will verify
6328 if (attr->type >= PERF_TYPE_MAX)
6331 if (attr->__reserved_1)
6334 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6337 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6344 put_user(sizeof(*attr), &uattr->size);
6350 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6352 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
6358 /* don't allow circular references */
6359 if (event == output_event)
6363 * Don't allow cross-cpu buffers
6365 if (output_event->cpu != event->cpu)
6369 * If its not a per-cpu buffer, it must be the same task.
6371 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6375 mutex_lock(&event->mmap_mutex);
6376 /* Can't redirect output if we've got an active mmap() */
6377 if (atomic_read(&event->mmap_count))
6381 /* get the buffer we want to redirect to */
6382 buffer = perf_buffer_get(output_event);
6387 old_buffer = event->buffer;
6388 rcu_assign_pointer(event->buffer, buffer);
6391 mutex_unlock(&event->mmap_mutex);
6394 perf_buffer_put(old_buffer);
6400 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6402 * @attr_uptr: event_id type attributes for monitoring/sampling
6405 * @group_fd: group leader event fd
6407 SYSCALL_DEFINE5(perf_event_open,
6408 struct perf_event_attr __user *, attr_uptr,
6409 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6411 struct perf_event *group_leader = NULL, *output_event = NULL;
6412 struct perf_event *event, *sibling;
6413 struct perf_event_attr attr;
6414 struct perf_event_context *ctx;
6415 struct file *event_file = NULL;
6416 struct file *group_file = NULL;
6417 struct task_struct *task = NULL;
6421 int fput_needed = 0;
6424 /* for future expandability... */
6425 if (flags & ~PERF_FLAG_ALL)
6428 err = perf_copy_attr(attr_uptr, &attr);
6432 if (!attr.exclude_kernel) {
6433 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6438 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6443 * In cgroup mode, the pid argument is used to pass the fd
6444 * opened to the cgroup directory in cgroupfs. The cpu argument
6445 * designates the cpu on which to monitor threads from that
6448 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6451 event_fd = get_unused_fd_flags(O_RDWR);
6455 if (group_fd != -1) {
6456 group_leader = perf_fget_light(group_fd, &fput_needed);
6457 if (IS_ERR(group_leader)) {
6458 err = PTR_ERR(group_leader);
6461 group_file = group_leader->filp;
6462 if (flags & PERF_FLAG_FD_OUTPUT)
6463 output_event = group_leader;
6464 if (flags & PERF_FLAG_FD_NO_GROUP)
6465 group_leader = NULL;
6468 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
6469 task = find_lively_task_by_vpid(pid);
6471 err = PTR_ERR(task);
6476 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
6477 if (IS_ERR(event)) {
6478 err = PTR_ERR(event);
6482 if (flags & PERF_FLAG_PID_CGROUP) {
6483 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6488 * - that has cgroup constraint on event->cpu
6489 * - that may need work on context switch
6491 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6492 jump_label_inc(&perf_sched_events);
6496 * Special case software events and allow them to be part of
6497 * any hardware group.
6502 (is_software_event(event) != is_software_event(group_leader))) {
6503 if (is_software_event(event)) {
6505 * If event and group_leader are not both a software
6506 * event, and event is, then group leader is not.
6508 * Allow the addition of software events to !software
6509 * groups, this is safe because software events never
6512 pmu = group_leader->pmu;
6513 } else if (is_software_event(group_leader) &&
6514 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6516 * In case the group is a pure software group, and we
6517 * try to add a hardware event, move the whole group to
6518 * the hardware context.
6525 * Get the target context (task or percpu):
6527 ctx = find_get_context(pmu, task, cpu);
6534 * Look up the group leader (we will attach this event to it):
6540 * Do not allow a recursive hierarchy (this new sibling
6541 * becoming part of another group-sibling):
6543 if (group_leader->group_leader != group_leader)
6546 * Do not allow to attach to a group in a different
6547 * task or CPU context:
6550 if (group_leader->ctx->type != ctx->type)
6553 if (group_leader->ctx != ctx)
6558 * Only a group leader can be exclusive or pinned
6560 if (attr.exclusive || attr.pinned)
6565 err = perf_event_set_output(event, output_event);
6570 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6571 if (IS_ERR(event_file)) {
6572 err = PTR_ERR(event_file);
6577 struct perf_event_context *gctx = group_leader->ctx;
6579 mutex_lock(&gctx->mutex);
6580 perf_remove_from_context(group_leader);
6581 list_for_each_entry(sibling, &group_leader->sibling_list,
6583 perf_remove_from_context(sibling);
6586 mutex_unlock(&gctx->mutex);
6590 event->filp = event_file;
6591 WARN_ON_ONCE(ctx->parent_ctx);
6592 mutex_lock(&ctx->mutex);
6595 perf_install_in_context(ctx, group_leader, cpu);
6597 list_for_each_entry(sibling, &group_leader->sibling_list,
6599 perf_install_in_context(ctx, sibling, cpu);
6604 perf_install_in_context(ctx, event, cpu);
6606 perf_unpin_context(ctx);
6607 mutex_unlock(&ctx->mutex);
6609 event->owner = current;
6611 mutex_lock(¤t->perf_event_mutex);
6612 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6613 mutex_unlock(¤t->perf_event_mutex);
6616 * Precalculate sample_data sizes
6618 perf_event__header_size(event);
6619 perf_event__id_header_size(event);
6622 * Drop the reference on the group_event after placing the
6623 * new event on the sibling_list. This ensures destruction
6624 * of the group leader will find the pointer to itself in
6625 * perf_group_detach().
6627 fput_light(group_file, fput_needed);
6628 fd_install(event_fd, event_file);
6632 perf_unpin_context(ctx);
6638 put_task_struct(task);
6640 fput_light(group_file, fput_needed);
6642 put_unused_fd(event_fd);
6647 * perf_event_create_kernel_counter
6649 * @attr: attributes of the counter to create
6650 * @cpu: cpu in which the counter is bound
6651 * @task: task to profile (NULL for percpu)
6654 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6655 struct task_struct *task,
6656 perf_overflow_handler_t overflow_handler)
6658 struct perf_event_context *ctx;
6659 struct perf_event *event;
6663 * Get the target context (task or percpu):
6666 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
6667 if (IS_ERR(event)) {
6668 err = PTR_ERR(event);
6672 ctx = find_get_context(event->pmu, task, cpu);
6679 WARN_ON_ONCE(ctx->parent_ctx);
6680 mutex_lock(&ctx->mutex);
6681 perf_install_in_context(ctx, event, cpu);
6683 perf_unpin_context(ctx);
6684 mutex_unlock(&ctx->mutex);
6691 return ERR_PTR(err);
6693 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6695 static void sync_child_event(struct perf_event *child_event,
6696 struct task_struct *child)
6698 struct perf_event *parent_event = child_event->parent;
6701 if (child_event->attr.inherit_stat)
6702 perf_event_read_event(child_event, child);
6704 child_val = perf_event_count(child_event);
6707 * Add back the child's count to the parent's count:
6709 atomic64_add(child_val, &parent_event->child_count);
6710 atomic64_add(child_event->total_time_enabled,
6711 &parent_event->child_total_time_enabled);
6712 atomic64_add(child_event->total_time_running,
6713 &parent_event->child_total_time_running);
6716 * Remove this event from the parent's list
6718 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6719 mutex_lock(&parent_event->child_mutex);
6720 list_del_init(&child_event->child_list);
6721 mutex_unlock(&parent_event->child_mutex);
6724 * Release the parent event, if this was the last
6727 fput(parent_event->filp);
6731 __perf_event_exit_task(struct perf_event *child_event,
6732 struct perf_event_context *child_ctx,
6733 struct task_struct *child)
6735 if (child_event->parent) {
6736 raw_spin_lock_irq(&child_ctx->lock);
6737 perf_group_detach(child_event);
6738 raw_spin_unlock_irq(&child_ctx->lock);
6741 perf_remove_from_context(child_event);
6744 * It can happen that the parent exits first, and has events
6745 * that are still around due to the child reference. These
6746 * events need to be zapped.
6748 if (child_event->parent) {
6749 sync_child_event(child_event, child);
6750 free_event(child_event);
6754 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6756 struct perf_event *child_event, *tmp;
6757 struct perf_event_context *child_ctx;
6758 unsigned long flags;
6760 if (likely(!child->perf_event_ctxp[ctxn])) {
6761 perf_event_task(child, NULL, 0);
6765 local_irq_save(flags);
6767 * We can't reschedule here because interrupts are disabled,
6768 * and either child is current or it is a task that can't be
6769 * scheduled, so we are now safe from rescheduling changing
6772 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6773 task_ctx_sched_out(child_ctx, EVENT_ALL);
6776 * Take the context lock here so that if find_get_context is
6777 * reading child->perf_event_ctxp, we wait until it has
6778 * incremented the context's refcount before we do put_ctx below.
6780 raw_spin_lock(&child_ctx->lock);
6781 child->perf_event_ctxp[ctxn] = NULL;
6783 * If this context is a clone; unclone it so it can't get
6784 * swapped to another process while we're removing all
6785 * the events from it.
6787 unclone_ctx(child_ctx);
6788 update_context_time(child_ctx);
6789 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6792 * Report the task dead after unscheduling the events so that we
6793 * won't get any samples after PERF_RECORD_EXIT. We can however still
6794 * get a few PERF_RECORD_READ events.
6796 perf_event_task(child, child_ctx, 0);
6799 * We can recurse on the same lock type through:
6801 * __perf_event_exit_task()
6802 * sync_child_event()
6803 * fput(parent_event->filp)
6805 * mutex_lock(&ctx->mutex)
6807 * But since its the parent context it won't be the same instance.
6809 mutex_lock(&child_ctx->mutex);
6812 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6814 __perf_event_exit_task(child_event, child_ctx, child);
6816 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6818 __perf_event_exit_task(child_event, child_ctx, child);
6821 * If the last event was a group event, it will have appended all
6822 * its siblings to the list, but we obtained 'tmp' before that which
6823 * will still point to the list head terminating the iteration.
6825 if (!list_empty(&child_ctx->pinned_groups) ||
6826 !list_empty(&child_ctx->flexible_groups))
6829 mutex_unlock(&child_ctx->mutex);
6835 * When a child task exits, feed back event values to parent events.
6837 void perf_event_exit_task(struct task_struct *child)
6839 struct perf_event *event, *tmp;
6842 mutex_lock(&child->perf_event_mutex);
6843 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6845 list_del_init(&event->owner_entry);
6848 * Ensure the list deletion is visible before we clear
6849 * the owner, closes a race against perf_release() where
6850 * we need to serialize on the owner->perf_event_mutex.
6853 event->owner = NULL;
6855 mutex_unlock(&child->perf_event_mutex);
6857 for_each_task_context_nr(ctxn)
6858 perf_event_exit_task_context(child, ctxn);
6861 static void perf_free_event(struct perf_event *event,
6862 struct perf_event_context *ctx)
6864 struct perf_event *parent = event->parent;
6866 if (WARN_ON_ONCE(!parent))
6869 mutex_lock(&parent->child_mutex);
6870 list_del_init(&event->child_list);
6871 mutex_unlock(&parent->child_mutex);
6875 perf_group_detach(event);
6876 list_del_event(event, ctx);
6881 * free an unexposed, unused context as created by inheritance by
6882 * perf_event_init_task below, used by fork() in case of fail.
6884 void perf_event_free_task(struct task_struct *task)
6886 struct perf_event_context *ctx;
6887 struct perf_event *event, *tmp;
6890 for_each_task_context_nr(ctxn) {
6891 ctx = task->perf_event_ctxp[ctxn];
6895 mutex_lock(&ctx->mutex);
6897 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6899 perf_free_event(event, ctx);
6901 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6903 perf_free_event(event, ctx);
6905 if (!list_empty(&ctx->pinned_groups) ||
6906 !list_empty(&ctx->flexible_groups))
6909 mutex_unlock(&ctx->mutex);
6915 void perf_event_delayed_put(struct task_struct *task)
6919 for_each_task_context_nr(ctxn)
6920 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6924 * inherit a event from parent task to child task:
6926 static struct perf_event *
6927 inherit_event(struct perf_event *parent_event,
6928 struct task_struct *parent,
6929 struct perf_event_context *parent_ctx,
6930 struct task_struct *child,
6931 struct perf_event *group_leader,
6932 struct perf_event_context *child_ctx)
6934 struct perf_event *child_event;
6935 unsigned long flags;
6938 * Instead of creating recursive hierarchies of events,
6939 * we link inherited events back to the original parent,
6940 * which has a filp for sure, which we use as the reference
6943 if (parent_event->parent)
6944 parent_event = parent_event->parent;
6946 child_event = perf_event_alloc(&parent_event->attr,
6949 group_leader, parent_event,
6951 if (IS_ERR(child_event))
6956 * Make the child state follow the state of the parent event,
6957 * not its attr.disabled bit. We hold the parent's mutex,
6958 * so we won't race with perf_event_{en, dis}able_family.
6960 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6961 child_event->state = PERF_EVENT_STATE_INACTIVE;
6963 child_event->state = PERF_EVENT_STATE_OFF;
6965 if (parent_event->attr.freq) {
6966 u64 sample_period = parent_event->hw.sample_period;
6967 struct hw_perf_event *hwc = &child_event->hw;
6969 hwc->sample_period = sample_period;
6970 hwc->last_period = sample_period;
6972 local64_set(&hwc->period_left, sample_period);
6975 child_event->ctx = child_ctx;
6976 child_event->overflow_handler = parent_event->overflow_handler;
6979 * Precalculate sample_data sizes
6981 perf_event__header_size(child_event);
6982 perf_event__id_header_size(child_event);
6985 * Link it up in the child's context:
6987 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6988 add_event_to_ctx(child_event, child_ctx);
6989 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6992 * Get a reference to the parent filp - we will fput it
6993 * when the child event exits. This is safe to do because
6994 * we are in the parent and we know that the filp still
6995 * exists and has a nonzero count:
6997 atomic_long_inc(&parent_event->filp->f_count);
7000 * Link this into the parent event's child list
7002 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7003 mutex_lock(&parent_event->child_mutex);
7004 list_add_tail(&child_event->child_list, &parent_event->child_list);
7005 mutex_unlock(&parent_event->child_mutex);
7010 static int inherit_group(struct perf_event *parent_event,
7011 struct task_struct *parent,
7012 struct perf_event_context *parent_ctx,
7013 struct task_struct *child,
7014 struct perf_event_context *child_ctx)
7016 struct perf_event *leader;
7017 struct perf_event *sub;
7018 struct perf_event *child_ctr;
7020 leader = inherit_event(parent_event, parent, parent_ctx,
7021 child, NULL, child_ctx);
7023 return PTR_ERR(leader);
7024 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7025 child_ctr = inherit_event(sub, parent, parent_ctx,
7026 child, leader, child_ctx);
7027 if (IS_ERR(child_ctr))
7028 return PTR_ERR(child_ctr);
7034 inherit_task_group(struct perf_event *event, struct task_struct *parent,
7035 struct perf_event_context *parent_ctx,
7036 struct task_struct *child, int ctxn,
7040 struct perf_event_context *child_ctx;
7042 if (!event->attr.inherit) {
7047 child_ctx = child->perf_event_ctxp[ctxn];
7050 * This is executed from the parent task context, so
7051 * inherit events that have been marked for cloning.
7052 * First allocate and initialize a context for the
7056 child_ctx = alloc_perf_context(event->pmu, child);
7060 child->perf_event_ctxp[ctxn] = child_ctx;
7063 ret = inherit_group(event, parent, parent_ctx,
7073 * Initialize the perf_event context in task_struct
7075 int perf_event_init_context(struct task_struct *child, int ctxn)
7077 struct perf_event_context *child_ctx, *parent_ctx;
7078 struct perf_event_context *cloned_ctx;
7079 struct perf_event *event;
7080 struct task_struct *parent = current;
7081 int inherited_all = 1;
7082 unsigned long flags;
7085 if (likely(!parent->perf_event_ctxp[ctxn]))
7089 * If the parent's context is a clone, pin it so it won't get
7092 parent_ctx = perf_pin_task_context(parent, ctxn);
7095 * No need to check if parent_ctx != NULL here; since we saw
7096 * it non-NULL earlier, the only reason for it to become NULL
7097 * is if we exit, and since we're currently in the middle of
7098 * a fork we can't be exiting at the same time.
7102 * Lock the parent list. No need to lock the child - not PID
7103 * hashed yet and not running, so nobody can access it.
7105 mutex_lock(&parent_ctx->mutex);
7108 * We dont have to disable NMIs - we are only looking at
7109 * the list, not manipulating it:
7111 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
7112 ret = inherit_task_group(event, parent, parent_ctx,
7113 child, ctxn, &inherited_all);
7119 * We can't hold ctx->lock when iterating the ->flexible_group list due
7120 * to allocations, but we need to prevent rotation because
7121 * rotate_ctx() will change the list from interrupt context.
7123 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7124 parent_ctx->rotate_disable = 1;
7125 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7127 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
7128 ret = inherit_task_group(event, parent, parent_ctx,
7129 child, ctxn, &inherited_all);
7134 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7135 parent_ctx->rotate_disable = 0;
7137 child_ctx = child->perf_event_ctxp[ctxn];
7139 if (child_ctx && inherited_all) {
7141 * Mark the child context as a clone of the parent
7142 * context, or of whatever the parent is a clone of.
7144 * Note that if the parent is a clone, the holding of
7145 * parent_ctx->lock avoids it from being uncloned.
7147 cloned_ctx = parent_ctx->parent_ctx;
7149 child_ctx->parent_ctx = cloned_ctx;
7150 child_ctx->parent_gen = parent_ctx->parent_gen;
7152 child_ctx->parent_ctx = parent_ctx;
7153 child_ctx->parent_gen = parent_ctx->generation;
7155 get_ctx(child_ctx->parent_ctx);
7158 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7159 mutex_unlock(&parent_ctx->mutex);
7161 perf_unpin_context(parent_ctx);
7162 put_ctx(parent_ctx);
7168 * Initialize the perf_event context in task_struct
7170 int perf_event_init_task(struct task_struct *child)
7174 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7175 mutex_init(&child->perf_event_mutex);
7176 INIT_LIST_HEAD(&child->perf_event_list);
7178 for_each_task_context_nr(ctxn) {
7179 ret = perf_event_init_context(child, ctxn);
7187 static void __init perf_event_init_all_cpus(void)
7189 struct swevent_htable *swhash;
7192 for_each_possible_cpu(cpu) {
7193 swhash = &per_cpu(swevent_htable, cpu);
7194 mutex_init(&swhash->hlist_mutex);
7195 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
7199 static void __cpuinit perf_event_init_cpu(int cpu)
7201 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7203 mutex_lock(&swhash->hlist_mutex);
7204 if (swhash->hlist_refcount > 0) {
7205 struct swevent_hlist *hlist;
7207 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7209 rcu_assign_pointer(swhash->swevent_hlist, hlist);
7211 mutex_unlock(&swhash->hlist_mutex);
7214 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7215 static void perf_pmu_rotate_stop(struct pmu *pmu)
7217 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7219 WARN_ON(!irqs_disabled());
7221 list_del_init(&cpuctx->rotation_list);
7224 static void __perf_event_exit_context(void *__info)
7226 struct perf_event_context *ctx = __info;
7227 struct perf_event *event, *tmp;
7229 perf_pmu_rotate_stop(ctx->pmu);
7231 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
7232 __perf_remove_from_context(event);
7233 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
7234 __perf_remove_from_context(event);
7237 static void perf_event_exit_cpu_context(int cpu)
7239 struct perf_event_context *ctx;
7243 idx = srcu_read_lock(&pmus_srcu);
7244 list_for_each_entry_rcu(pmu, &pmus, entry) {
7245 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
7247 mutex_lock(&ctx->mutex);
7248 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7249 mutex_unlock(&ctx->mutex);
7251 srcu_read_unlock(&pmus_srcu, idx);
7254 static void perf_event_exit_cpu(int cpu)
7256 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7258 mutex_lock(&swhash->hlist_mutex);
7259 swevent_hlist_release(swhash);
7260 mutex_unlock(&swhash->hlist_mutex);
7262 perf_event_exit_cpu_context(cpu);
7265 static inline void perf_event_exit_cpu(int cpu) { }
7269 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7273 for_each_online_cpu(cpu)
7274 perf_event_exit_cpu(cpu);
7280 * Run the perf reboot notifier at the very last possible moment so that
7281 * the generic watchdog code runs as long as possible.
7283 static struct notifier_block perf_reboot_notifier = {
7284 .notifier_call = perf_reboot,
7285 .priority = INT_MIN,
7288 static int __cpuinit
7289 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7291 unsigned int cpu = (long)hcpu;
7293 switch (action & ~CPU_TASKS_FROZEN) {
7295 case CPU_UP_PREPARE:
7296 case CPU_DOWN_FAILED:
7297 perf_event_init_cpu(cpu);
7300 case CPU_UP_CANCELED:
7301 case CPU_DOWN_PREPARE:
7302 perf_event_exit_cpu(cpu);
7312 void __init perf_event_init(void)
7318 perf_event_init_all_cpus();
7319 init_srcu_struct(&pmus_srcu);
7320 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7321 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7322 perf_pmu_register(&perf_task_clock, NULL, -1);
7324 perf_cpu_notifier(perf_cpu_notify);
7325 register_reboot_notifier(&perf_reboot_notifier);
7327 ret = init_hw_breakpoint();
7328 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7331 static int __init perf_event_sysfs_init(void)
7336 mutex_lock(&pmus_lock);
7338 ret = bus_register(&pmu_bus);
7342 list_for_each_entry(pmu, &pmus, entry) {
7343 if (!pmu->name || pmu->type < 0)
7346 ret = pmu_dev_alloc(pmu);
7347 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7349 pmu_bus_running = 1;
7353 mutex_unlock(&pmus_lock);
7357 device_initcall(perf_event_sysfs_init);
7359 #ifdef CONFIG_CGROUP_PERF
7360 static struct cgroup_subsys_state *perf_cgroup_create(
7361 struct cgroup_subsys *ss, struct cgroup *cont)
7363 struct perf_cgroup *jc;
7365 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
7367 return ERR_PTR(-ENOMEM);
7369 jc->info = alloc_percpu(struct perf_cgroup_info);
7372 return ERR_PTR(-ENOMEM);
7378 static void perf_cgroup_destroy(struct cgroup_subsys *ss,
7379 struct cgroup *cont)
7381 struct perf_cgroup *jc;
7382 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7383 struct perf_cgroup, css);
7384 free_percpu(jc->info);
7388 static int __perf_cgroup_move(void *info)
7390 struct task_struct *task = info;
7391 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7395 static void perf_cgroup_move(struct task_struct *task)
7397 task_function_call(task, __perf_cgroup_move, task);
7400 static void perf_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
7401 struct cgroup *old_cgrp, struct task_struct *task,
7404 perf_cgroup_move(task);
7406 struct task_struct *c;
7408 list_for_each_entry_rcu(c, &task->thread_group, thread_group) {
7409 perf_cgroup_move(c);
7415 static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7416 struct cgroup *old_cgrp, struct task_struct *task)
7419 * cgroup_exit() is called in the copy_process() failure path.
7420 * Ignore this case since the task hasn't ran yet, this avoids
7421 * trying to poke a half freed task state from generic code.
7423 if (!(task->flags & PF_EXITING))
7426 perf_cgroup_move(task);
7429 struct cgroup_subsys perf_subsys = {
7430 .name = "perf_event",
7431 .subsys_id = perf_subsys_id,
7432 .create = perf_cgroup_create,
7433 .destroy = perf_cgroup_destroy,
7434 .exit = perf_cgroup_exit,
7435 .attach = perf_cgroup_attach,
7437 #endif /* CONFIG_CGROUP_PERF */