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);
410 cgrp = container_of(css, struct perf_cgroup, css);
414 * all events in a group must monitor
415 * the same cgroup because a task belongs
416 * to only one perf cgroup at a time
418 if (group_leader && group_leader->cgrp != cgrp) {
419 perf_detach_cgroup(event);
422 /* must be done before we fput() the file */
423 perf_get_cgroup(event);
425 fput_light(file, fput_needed);
430 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
432 struct perf_cgroup_info *t;
433 t = per_cpu_ptr(event->cgrp->info, event->cpu);
434 event->shadow_ctx_time = now - t->timestamp;
438 perf_cgroup_defer_enabled(struct perf_event *event)
441 * when the current task's perf cgroup does not match
442 * the event's, we need to remember to call the
443 * perf_mark_enable() function the first time a task with
444 * a matching perf cgroup is scheduled in.
446 if (is_cgroup_event(event) && !perf_cgroup_match(event))
447 event->cgrp_defer_enabled = 1;
451 perf_cgroup_mark_enabled(struct perf_event *event,
452 struct perf_event_context *ctx)
454 struct perf_event *sub;
455 u64 tstamp = perf_event_time(event);
457 if (!event->cgrp_defer_enabled)
460 event->cgrp_defer_enabled = 0;
462 event->tstamp_enabled = tstamp - event->total_time_enabled;
463 list_for_each_entry(sub, &event->sibling_list, group_entry) {
464 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
465 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
466 sub->cgrp_defer_enabled = 0;
470 #else /* !CONFIG_CGROUP_PERF */
473 perf_cgroup_match(struct perf_event *event)
478 static inline void perf_detach_cgroup(struct perf_event *event)
481 static inline int is_cgroup_event(struct perf_event *event)
486 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
491 static inline void update_cgrp_time_from_event(struct perf_event *event)
495 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
499 static inline void perf_cgroup_sched_out(struct task_struct *task)
503 static inline void perf_cgroup_sched_in(struct task_struct *task)
507 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
508 struct perf_event_attr *attr,
509 struct perf_event *group_leader)
515 perf_cgroup_set_timestamp(struct task_struct *task,
516 struct perf_event_context *ctx)
521 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
526 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
530 static inline u64 perf_cgroup_event_time(struct perf_event *event)
536 perf_cgroup_defer_enabled(struct perf_event *event)
541 perf_cgroup_mark_enabled(struct perf_event *event,
542 struct perf_event_context *ctx)
547 void perf_pmu_disable(struct pmu *pmu)
549 int *count = this_cpu_ptr(pmu->pmu_disable_count);
551 pmu->pmu_disable(pmu);
554 void perf_pmu_enable(struct pmu *pmu)
556 int *count = this_cpu_ptr(pmu->pmu_disable_count);
558 pmu->pmu_enable(pmu);
561 static DEFINE_PER_CPU(struct list_head, rotation_list);
564 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
565 * because they're strictly cpu affine and rotate_start is called with IRQs
566 * disabled, while rotate_context is called from IRQ context.
568 static void perf_pmu_rotate_start(struct pmu *pmu)
570 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
571 struct list_head *head = &__get_cpu_var(rotation_list);
573 WARN_ON(!irqs_disabled());
575 if (list_empty(&cpuctx->rotation_list))
576 list_add(&cpuctx->rotation_list, head);
579 static void get_ctx(struct perf_event_context *ctx)
581 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
584 static void free_ctx(struct rcu_head *head)
586 struct perf_event_context *ctx;
588 ctx = container_of(head, struct perf_event_context, rcu_head);
592 static void put_ctx(struct perf_event_context *ctx)
594 if (atomic_dec_and_test(&ctx->refcount)) {
596 put_ctx(ctx->parent_ctx);
598 put_task_struct(ctx->task);
599 call_rcu(&ctx->rcu_head, free_ctx);
603 static void unclone_ctx(struct perf_event_context *ctx)
605 if (ctx->parent_ctx) {
606 put_ctx(ctx->parent_ctx);
607 ctx->parent_ctx = NULL;
611 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
614 * only top level events have the pid namespace they were created in
617 event = event->parent;
619 return task_tgid_nr_ns(p, event->ns);
622 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
625 * only top level events have the pid namespace they were created in
628 event = event->parent;
630 return task_pid_nr_ns(p, event->ns);
634 * If we inherit events we want to return the parent event id
637 static u64 primary_event_id(struct perf_event *event)
642 id = event->parent->id;
648 * Get the perf_event_context for a task and lock it.
649 * This has to cope with with the fact that until it is locked,
650 * the context could get moved to another task.
652 static struct perf_event_context *
653 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
655 struct perf_event_context *ctx;
659 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
662 * If this context is a clone of another, it might
663 * get swapped for another underneath us by
664 * perf_event_task_sched_out, though the
665 * rcu_read_lock() protects us from any context
666 * getting freed. Lock the context and check if it
667 * got swapped before we could get the lock, and retry
668 * if so. If we locked the right context, then it
669 * can't get swapped on us any more.
671 raw_spin_lock_irqsave(&ctx->lock, *flags);
672 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
673 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
677 if (!atomic_inc_not_zero(&ctx->refcount)) {
678 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
687 * Get the context for a task and increment its pin_count so it
688 * can't get swapped to another task. This also increments its
689 * reference count so that the context can't get freed.
691 static struct perf_event_context *
692 perf_pin_task_context(struct task_struct *task, int ctxn)
694 struct perf_event_context *ctx;
697 ctx = perf_lock_task_context(task, ctxn, &flags);
700 raw_spin_unlock_irqrestore(&ctx->lock, flags);
705 static void perf_unpin_context(struct perf_event_context *ctx)
709 raw_spin_lock_irqsave(&ctx->lock, flags);
711 raw_spin_unlock_irqrestore(&ctx->lock, flags);
715 * Update the record of the current time in a context.
717 static void update_context_time(struct perf_event_context *ctx)
719 u64 now = perf_clock();
721 ctx->time += now - ctx->timestamp;
722 ctx->timestamp = now;
725 static u64 perf_event_time(struct perf_event *event)
727 struct perf_event_context *ctx = event->ctx;
729 if (is_cgroup_event(event))
730 return perf_cgroup_event_time(event);
732 return ctx ? ctx->time : 0;
736 * Update the total_time_enabled and total_time_running fields for a event.
738 static void update_event_times(struct perf_event *event)
740 struct perf_event_context *ctx = event->ctx;
743 if (event->state < PERF_EVENT_STATE_INACTIVE ||
744 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
747 * in cgroup mode, time_enabled represents
748 * the time the event was enabled AND active
749 * tasks were in the monitored cgroup. This is
750 * independent of the activity of the context as
751 * there may be a mix of cgroup and non-cgroup events.
753 * That is why we treat cgroup events differently
756 if (is_cgroup_event(event))
757 run_end = perf_event_time(event);
758 else if (ctx->is_active)
761 run_end = event->tstamp_stopped;
763 event->total_time_enabled = run_end - event->tstamp_enabled;
765 if (event->state == PERF_EVENT_STATE_INACTIVE)
766 run_end = event->tstamp_stopped;
768 run_end = perf_event_time(event);
770 event->total_time_running = run_end - event->tstamp_running;
775 * Update total_time_enabled and total_time_running for all events in a group.
777 static void update_group_times(struct perf_event *leader)
779 struct perf_event *event;
781 update_event_times(leader);
782 list_for_each_entry(event, &leader->sibling_list, group_entry)
783 update_event_times(event);
786 static struct list_head *
787 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
789 if (event->attr.pinned)
790 return &ctx->pinned_groups;
792 return &ctx->flexible_groups;
796 * Add a event from the lists for its context.
797 * Must be called with ctx->mutex and ctx->lock held.
800 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
802 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
803 event->attach_state |= PERF_ATTACH_CONTEXT;
806 * If we're a stand alone event or group leader, we go to the context
807 * list, group events are kept attached to the group so that
808 * perf_group_detach can, at all times, locate all siblings.
810 if (event->group_leader == event) {
811 struct list_head *list;
813 if (is_software_event(event))
814 event->group_flags |= PERF_GROUP_SOFTWARE;
816 list = ctx_group_list(event, ctx);
817 list_add_tail(&event->group_entry, list);
820 if (is_cgroup_event(event)) {
824 * - that has cgroup constraint on event->cpu
825 * - that may need work on context switch
827 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
828 jump_label_inc(&perf_sched_events);
831 list_add_rcu(&event->event_entry, &ctx->event_list);
833 perf_pmu_rotate_start(ctx->pmu);
835 if (event->attr.inherit_stat)
840 * Called at perf_event creation and when events are attached/detached from a
843 static void perf_event__read_size(struct perf_event *event)
845 int entry = sizeof(u64); /* value */
849 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
852 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
855 if (event->attr.read_format & PERF_FORMAT_ID)
856 entry += sizeof(u64);
858 if (event->attr.read_format & PERF_FORMAT_GROUP) {
859 nr += event->group_leader->nr_siblings;
864 event->read_size = size;
867 static void perf_event__header_size(struct perf_event *event)
869 struct perf_sample_data *data;
870 u64 sample_type = event->attr.sample_type;
873 perf_event__read_size(event);
875 if (sample_type & PERF_SAMPLE_IP)
876 size += sizeof(data->ip);
878 if (sample_type & PERF_SAMPLE_ADDR)
879 size += sizeof(data->addr);
881 if (sample_type & PERF_SAMPLE_PERIOD)
882 size += sizeof(data->period);
884 if (sample_type & PERF_SAMPLE_READ)
885 size += event->read_size;
887 event->header_size = size;
890 static void perf_event__id_header_size(struct perf_event *event)
892 struct perf_sample_data *data;
893 u64 sample_type = event->attr.sample_type;
896 if (sample_type & PERF_SAMPLE_TID)
897 size += sizeof(data->tid_entry);
899 if (sample_type & PERF_SAMPLE_TIME)
900 size += sizeof(data->time);
902 if (sample_type & PERF_SAMPLE_ID)
903 size += sizeof(data->id);
905 if (sample_type & PERF_SAMPLE_STREAM_ID)
906 size += sizeof(data->stream_id);
908 if (sample_type & PERF_SAMPLE_CPU)
909 size += sizeof(data->cpu_entry);
911 event->id_header_size = size;
914 static void perf_group_attach(struct perf_event *event)
916 struct perf_event *group_leader = event->group_leader, *pos;
919 * We can have double attach due to group movement in perf_event_open.
921 if (event->attach_state & PERF_ATTACH_GROUP)
924 event->attach_state |= PERF_ATTACH_GROUP;
926 if (group_leader == event)
929 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
930 !is_software_event(event))
931 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
933 list_add_tail(&event->group_entry, &group_leader->sibling_list);
934 group_leader->nr_siblings++;
936 perf_event__header_size(group_leader);
938 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
939 perf_event__header_size(pos);
943 * Remove a event from the lists for its context.
944 * Must be called with ctx->mutex and ctx->lock held.
947 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
950 * We can have double detach due to exit/hot-unplug + close.
952 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
955 event->attach_state &= ~PERF_ATTACH_CONTEXT;
957 if (is_cgroup_event(event)) {
959 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
960 jump_label_dec(&perf_sched_events);
964 if (event->attr.inherit_stat)
967 list_del_rcu(&event->event_entry);
969 if (event->group_leader == event)
970 list_del_init(&event->group_entry);
972 update_group_times(event);
975 * If event was in error state, then keep it
976 * that way, otherwise bogus counts will be
977 * returned on read(). The only way to get out
978 * of error state is by explicit re-enabling
981 if (event->state > PERF_EVENT_STATE_OFF)
982 event->state = PERF_EVENT_STATE_OFF;
985 static void perf_group_detach(struct perf_event *event)
987 struct perf_event *sibling, *tmp;
988 struct list_head *list = NULL;
991 * We can have double detach due to exit/hot-unplug + close.
993 if (!(event->attach_state & PERF_ATTACH_GROUP))
996 event->attach_state &= ~PERF_ATTACH_GROUP;
999 * If this is a sibling, remove it from its group.
1001 if (event->group_leader != event) {
1002 list_del_init(&event->group_entry);
1003 event->group_leader->nr_siblings--;
1007 if (!list_empty(&event->group_entry))
1008 list = &event->group_entry;
1011 * If this was a group event with sibling events then
1012 * upgrade the siblings to singleton events by adding them
1013 * to whatever list we are on.
1015 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
1017 list_move_tail(&sibling->group_entry, list);
1018 sibling->group_leader = sibling;
1020 /* Inherit group flags from the previous leader */
1021 sibling->group_flags = event->group_flags;
1025 perf_event__header_size(event->group_leader);
1027 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1028 perf_event__header_size(tmp);
1032 event_filter_match(struct perf_event *event)
1034 return (event->cpu == -1 || event->cpu == smp_processor_id())
1035 && perf_cgroup_match(event);
1039 event_sched_out(struct perf_event *event,
1040 struct perf_cpu_context *cpuctx,
1041 struct perf_event_context *ctx)
1043 u64 tstamp = perf_event_time(event);
1046 * An event which could not be activated because of
1047 * filter mismatch still needs to have its timings
1048 * maintained, otherwise bogus information is return
1049 * via read() for time_enabled, time_running:
1051 if (event->state == PERF_EVENT_STATE_INACTIVE
1052 && !event_filter_match(event)) {
1053 delta = tstamp - event->tstamp_stopped;
1054 event->tstamp_running += delta;
1055 event->tstamp_stopped = tstamp;
1058 if (event->state != PERF_EVENT_STATE_ACTIVE)
1061 event->state = PERF_EVENT_STATE_INACTIVE;
1062 if (event->pending_disable) {
1063 event->pending_disable = 0;
1064 event->state = PERF_EVENT_STATE_OFF;
1066 event->tstamp_stopped = tstamp;
1067 event->pmu->del(event, 0);
1070 if (!is_software_event(event))
1071 cpuctx->active_oncpu--;
1073 if (event->attr.exclusive || !cpuctx->active_oncpu)
1074 cpuctx->exclusive = 0;
1078 group_sched_out(struct perf_event *group_event,
1079 struct perf_cpu_context *cpuctx,
1080 struct perf_event_context *ctx)
1082 struct perf_event *event;
1083 int state = group_event->state;
1085 event_sched_out(group_event, cpuctx, ctx);
1088 * Schedule out siblings (if any):
1090 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1091 event_sched_out(event, cpuctx, ctx);
1093 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
1094 cpuctx->exclusive = 0;
1098 * Cross CPU call to remove a performance event
1100 * We disable the event on the hardware level first. After that we
1101 * remove it from the context list.
1103 static int __perf_remove_from_context(void *info)
1105 struct perf_event *event = info;
1106 struct perf_event_context *ctx = event->ctx;
1107 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1109 raw_spin_lock(&ctx->lock);
1110 event_sched_out(event, cpuctx, ctx);
1111 list_del_event(event, ctx);
1112 raw_spin_unlock(&ctx->lock);
1119 * Remove the event from a task's (or a CPU's) list of events.
1121 * CPU events are removed with a smp call. For task events we only
1122 * call when the task is on a CPU.
1124 * If event->ctx is a cloned context, callers must make sure that
1125 * every task struct that event->ctx->task could possibly point to
1126 * remains valid. This is OK when called from perf_release since
1127 * that only calls us on the top-level context, which can't be a clone.
1128 * When called from perf_event_exit_task, it's OK because the
1129 * context has been detached from its task.
1131 static void perf_remove_from_context(struct perf_event *event)
1133 struct perf_event_context *ctx = event->ctx;
1134 struct task_struct *task = ctx->task;
1136 lockdep_assert_held(&ctx->mutex);
1140 * Per cpu events are removed via an smp call and
1141 * the removal is always successful.
1143 cpu_function_call(event->cpu, __perf_remove_from_context, event);
1148 if (!task_function_call(task, __perf_remove_from_context, event))
1151 raw_spin_lock_irq(&ctx->lock);
1153 * If we failed to find a running task, but find the context active now
1154 * that we've acquired the ctx->lock, retry.
1156 if (ctx->is_active) {
1157 raw_spin_unlock_irq(&ctx->lock);
1162 * Since the task isn't running, its safe to remove the event, us
1163 * holding the ctx->lock ensures the task won't get scheduled in.
1165 list_del_event(event, ctx);
1166 raw_spin_unlock_irq(&ctx->lock);
1170 * Cross CPU call to disable a performance event
1172 static int __perf_event_disable(void *info)
1174 struct perf_event *event = info;
1175 struct perf_event_context *ctx = event->ctx;
1176 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1179 * If this is a per-task event, need to check whether this
1180 * event's task is the current task on this cpu.
1182 * Can trigger due to concurrent perf_event_context_sched_out()
1183 * flipping contexts around.
1185 if (ctx->task && cpuctx->task_ctx != ctx)
1188 raw_spin_lock(&ctx->lock);
1191 * If the event is on, turn it off.
1192 * If it is in error state, leave it in error state.
1194 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1195 update_context_time(ctx);
1196 update_cgrp_time_from_event(event);
1197 update_group_times(event);
1198 if (event == event->group_leader)
1199 group_sched_out(event, cpuctx, ctx);
1201 event_sched_out(event, cpuctx, ctx);
1202 event->state = PERF_EVENT_STATE_OFF;
1205 raw_spin_unlock(&ctx->lock);
1213 * If event->ctx is a cloned context, callers must make sure that
1214 * every task struct that event->ctx->task could possibly point to
1215 * remains valid. This condition is satisifed when called through
1216 * perf_event_for_each_child or perf_event_for_each because they
1217 * hold the top-level event's child_mutex, so any descendant that
1218 * goes to exit will block in sync_child_event.
1219 * When called from perf_pending_event it's OK because event->ctx
1220 * is the current context on this CPU and preemption is disabled,
1221 * hence we can't get into perf_event_task_sched_out for this context.
1223 void perf_event_disable(struct perf_event *event)
1225 struct perf_event_context *ctx = event->ctx;
1226 struct task_struct *task = ctx->task;
1230 * Disable the event on the cpu that it's on
1232 cpu_function_call(event->cpu, __perf_event_disable, event);
1237 if (!task_function_call(task, __perf_event_disable, event))
1240 raw_spin_lock_irq(&ctx->lock);
1242 * If the event is still active, we need to retry the cross-call.
1244 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1245 raw_spin_unlock_irq(&ctx->lock);
1247 * Reload the task pointer, it might have been changed by
1248 * a concurrent perf_event_context_sched_out().
1255 * Since we have the lock this context can't be scheduled
1256 * in, so we can change the state safely.
1258 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1259 update_group_times(event);
1260 event->state = PERF_EVENT_STATE_OFF;
1262 raw_spin_unlock_irq(&ctx->lock);
1265 static void perf_set_shadow_time(struct perf_event *event,
1266 struct perf_event_context *ctx,
1270 * use the correct time source for the time snapshot
1272 * We could get by without this by leveraging the
1273 * fact that to get to this function, the caller
1274 * has most likely already called update_context_time()
1275 * and update_cgrp_time_xx() and thus both timestamp
1276 * are identical (or very close). Given that tstamp is,
1277 * already adjusted for cgroup, we could say that:
1278 * tstamp - ctx->timestamp
1280 * tstamp - cgrp->timestamp.
1282 * Then, in perf_output_read(), the calculation would
1283 * work with no changes because:
1284 * - event is guaranteed scheduled in
1285 * - no scheduled out in between
1286 * - thus the timestamp would be the same
1288 * But this is a bit hairy.
1290 * So instead, we have an explicit cgroup call to remain
1291 * within the time time source all along. We believe it
1292 * is cleaner and simpler to understand.
1294 if (is_cgroup_event(event))
1295 perf_cgroup_set_shadow_time(event, tstamp);
1297 event->shadow_ctx_time = tstamp - ctx->timestamp;
1300 #define MAX_INTERRUPTS (~0ULL)
1302 static void perf_log_throttle(struct perf_event *event, int enable);
1305 event_sched_in(struct perf_event *event,
1306 struct perf_cpu_context *cpuctx,
1307 struct perf_event_context *ctx)
1309 u64 tstamp = perf_event_time(event);
1311 if (event->state <= PERF_EVENT_STATE_OFF)
1314 event->state = PERF_EVENT_STATE_ACTIVE;
1315 event->oncpu = smp_processor_id();
1318 * Unthrottle events, since we scheduled we might have missed several
1319 * ticks already, also for a heavily scheduling task there is little
1320 * guarantee it'll get a tick in a timely manner.
1322 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1323 perf_log_throttle(event, 1);
1324 event->hw.interrupts = 0;
1328 * The new state must be visible before we turn it on in the hardware:
1332 if (event->pmu->add(event, PERF_EF_START)) {
1333 event->state = PERF_EVENT_STATE_INACTIVE;
1338 event->tstamp_running += tstamp - event->tstamp_stopped;
1340 perf_set_shadow_time(event, ctx, tstamp);
1342 if (!is_software_event(event))
1343 cpuctx->active_oncpu++;
1346 if (event->attr.exclusive)
1347 cpuctx->exclusive = 1;
1353 group_sched_in(struct perf_event *group_event,
1354 struct perf_cpu_context *cpuctx,
1355 struct perf_event_context *ctx)
1357 struct perf_event *event, *partial_group = NULL;
1358 struct pmu *pmu = group_event->pmu;
1359 u64 now = ctx->time;
1360 bool simulate = false;
1362 if (group_event->state == PERF_EVENT_STATE_OFF)
1365 pmu->start_txn(pmu);
1367 if (event_sched_in(group_event, cpuctx, ctx)) {
1368 pmu->cancel_txn(pmu);
1373 * Schedule in siblings as one group (if any):
1375 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1376 if (event_sched_in(event, cpuctx, ctx)) {
1377 partial_group = event;
1382 if (!pmu->commit_txn(pmu))
1387 * Groups can be scheduled in as one unit only, so undo any
1388 * partial group before returning:
1389 * The events up to the failed event are scheduled out normally,
1390 * tstamp_stopped will be updated.
1392 * The failed events and the remaining siblings need to have
1393 * their timings updated as if they had gone thru event_sched_in()
1394 * and event_sched_out(). This is required to get consistent timings
1395 * across the group. This also takes care of the case where the group
1396 * could never be scheduled by ensuring tstamp_stopped is set to mark
1397 * the time the event was actually stopped, such that time delta
1398 * calculation in update_event_times() is correct.
1400 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1401 if (event == partial_group)
1405 event->tstamp_running += now - event->tstamp_stopped;
1406 event->tstamp_stopped = now;
1408 event_sched_out(event, cpuctx, ctx);
1411 event_sched_out(group_event, cpuctx, ctx);
1413 pmu->cancel_txn(pmu);
1419 * Work out whether we can put this event group on the CPU now.
1421 static int group_can_go_on(struct perf_event *event,
1422 struct perf_cpu_context *cpuctx,
1426 * Groups consisting entirely of software events can always go on.
1428 if (event->group_flags & PERF_GROUP_SOFTWARE)
1431 * If an exclusive group is already on, no other hardware
1434 if (cpuctx->exclusive)
1437 * If this group is exclusive and there are already
1438 * events on the CPU, it can't go on.
1440 if (event->attr.exclusive && cpuctx->active_oncpu)
1443 * Otherwise, try to add it if all previous groups were able
1449 static void add_event_to_ctx(struct perf_event *event,
1450 struct perf_event_context *ctx)
1452 u64 tstamp = perf_event_time(event);
1454 list_add_event(event, ctx);
1455 perf_group_attach(event);
1456 event->tstamp_enabled = tstamp;
1457 event->tstamp_running = tstamp;
1458 event->tstamp_stopped = tstamp;
1461 static void perf_event_context_sched_in(struct perf_event_context *ctx,
1462 struct task_struct *tsk);
1465 * Cross CPU call to install and enable a performance event
1467 * Must be called with ctx->mutex held
1469 static int __perf_install_in_context(void *info)
1471 struct perf_event *event = info;
1472 struct perf_event_context *ctx = event->ctx;
1473 struct perf_event *leader = event->group_leader;
1474 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1478 * In case we're installing a new context to an already running task,
1479 * could also happen before perf_event_task_sched_in() on architectures
1480 * which do context switches with IRQs enabled.
1482 if (ctx->task && !cpuctx->task_ctx)
1483 perf_event_context_sched_in(ctx, ctx->task);
1485 raw_spin_lock(&ctx->lock);
1487 update_context_time(ctx);
1489 * update cgrp time only if current cgrp
1490 * matches event->cgrp. Must be done before
1491 * calling add_event_to_ctx()
1493 update_cgrp_time_from_event(event);
1495 add_event_to_ctx(event, ctx);
1497 if (!event_filter_match(event))
1501 * Don't put the event on if it is disabled or if
1502 * it is in a group and the group isn't on.
1504 if (event->state != PERF_EVENT_STATE_INACTIVE ||
1505 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
1509 * An exclusive event can't go on if there are already active
1510 * hardware events, and no hardware event can go on if there
1511 * is already an exclusive event on.
1513 if (!group_can_go_on(event, cpuctx, 1))
1516 err = event_sched_in(event, cpuctx, ctx);
1520 * This event couldn't go on. If it is in a group
1521 * then we have to pull the whole group off.
1522 * If the event group is pinned then put it in error state.
1524 if (leader != event)
1525 group_sched_out(leader, cpuctx, ctx);
1526 if (leader->attr.pinned) {
1527 update_group_times(leader);
1528 leader->state = PERF_EVENT_STATE_ERROR;
1533 raw_spin_unlock(&ctx->lock);
1539 * Attach a performance event to a context
1541 * First we add the event to the list with the hardware enable bit
1542 * in event->hw_config cleared.
1544 * If the event is attached to a task which is on a CPU we use a smp
1545 * call to enable it in the task context. The task might have been
1546 * scheduled away, but we check this in the smp call again.
1549 perf_install_in_context(struct perf_event_context *ctx,
1550 struct perf_event *event,
1553 struct task_struct *task = ctx->task;
1555 lockdep_assert_held(&ctx->mutex);
1561 * Per cpu events are installed via an smp call and
1562 * the install is always successful.
1564 cpu_function_call(cpu, __perf_install_in_context, event);
1569 if (!task_function_call(task, __perf_install_in_context, event))
1572 raw_spin_lock_irq(&ctx->lock);
1574 * If we failed to find a running task, but find the context active now
1575 * that we've acquired the ctx->lock, retry.
1577 if (ctx->is_active) {
1578 raw_spin_unlock_irq(&ctx->lock);
1583 * Since the task isn't running, its safe to add the event, us holding
1584 * the ctx->lock ensures the task won't get scheduled in.
1586 add_event_to_ctx(event, ctx);
1587 raw_spin_unlock_irq(&ctx->lock);
1591 * Put a event into inactive state and update time fields.
1592 * Enabling the leader of a group effectively enables all
1593 * the group members that aren't explicitly disabled, so we
1594 * have to update their ->tstamp_enabled also.
1595 * Note: this works for group members as well as group leaders
1596 * since the non-leader members' sibling_lists will be empty.
1598 static void __perf_event_mark_enabled(struct perf_event *event,
1599 struct perf_event_context *ctx)
1601 struct perf_event *sub;
1602 u64 tstamp = perf_event_time(event);
1604 event->state = PERF_EVENT_STATE_INACTIVE;
1605 event->tstamp_enabled = tstamp - event->total_time_enabled;
1606 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1607 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1608 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1613 * Cross CPU call to enable a performance event
1615 static int __perf_event_enable(void *info)
1617 struct perf_event *event = info;
1618 struct perf_event_context *ctx = event->ctx;
1619 struct perf_event *leader = event->group_leader;
1620 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1623 if (WARN_ON_ONCE(!ctx->is_active))
1626 raw_spin_lock(&ctx->lock);
1627 update_context_time(ctx);
1629 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1633 * set current task's cgroup time reference point
1635 perf_cgroup_set_timestamp(current, ctx);
1637 __perf_event_mark_enabled(event, ctx);
1639 if (!event_filter_match(event)) {
1640 if (is_cgroup_event(event))
1641 perf_cgroup_defer_enabled(event);
1646 * If the event is in a group and isn't the group leader,
1647 * then don't put it on unless the group is on.
1649 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1652 if (!group_can_go_on(event, cpuctx, 1)) {
1655 if (event == leader)
1656 err = group_sched_in(event, cpuctx, ctx);
1658 err = event_sched_in(event, cpuctx, ctx);
1663 * If this event can't go on and it's part of a
1664 * group, then the whole group has to come off.
1666 if (leader != event)
1667 group_sched_out(leader, cpuctx, ctx);
1668 if (leader->attr.pinned) {
1669 update_group_times(leader);
1670 leader->state = PERF_EVENT_STATE_ERROR;
1675 raw_spin_unlock(&ctx->lock);
1683 * If event->ctx is a cloned context, callers must make sure that
1684 * every task struct that event->ctx->task could possibly point to
1685 * remains valid. This condition is satisfied when called through
1686 * perf_event_for_each_child or perf_event_for_each as described
1687 * for perf_event_disable.
1689 void perf_event_enable(struct perf_event *event)
1691 struct perf_event_context *ctx = event->ctx;
1692 struct task_struct *task = ctx->task;
1696 * Enable the event on the cpu that it's on
1698 cpu_function_call(event->cpu, __perf_event_enable, event);
1702 raw_spin_lock_irq(&ctx->lock);
1703 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1707 * If the event is in error state, clear that first.
1708 * That way, if we see the event in error state below, we
1709 * know that it has gone back into error state, as distinct
1710 * from the task having been scheduled away before the
1711 * cross-call arrived.
1713 if (event->state == PERF_EVENT_STATE_ERROR)
1714 event->state = PERF_EVENT_STATE_OFF;
1717 if (!ctx->is_active) {
1718 __perf_event_mark_enabled(event, ctx);
1722 raw_spin_unlock_irq(&ctx->lock);
1724 if (!task_function_call(task, __perf_event_enable, event))
1727 raw_spin_lock_irq(&ctx->lock);
1730 * If the context is active and the event is still off,
1731 * we need to retry the cross-call.
1733 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1735 * task could have been flipped by a concurrent
1736 * perf_event_context_sched_out()
1743 raw_spin_unlock_irq(&ctx->lock);
1746 static int perf_event_refresh(struct perf_event *event, int refresh)
1749 * not supported on inherited events
1751 if (event->attr.inherit || !is_sampling_event(event))
1754 atomic_add(refresh, &event->event_limit);
1755 perf_event_enable(event);
1760 static void ctx_sched_out(struct perf_event_context *ctx,
1761 struct perf_cpu_context *cpuctx,
1762 enum event_type_t event_type)
1764 struct perf_event *event;
1766 raw_spin_lock(&ctx->lock);
1767 perf_pmu_disable(ctx->pmu);
1769 if (likely(!ctx->nr_events))
1771 update_context_time(ctx);
1772 update_cgrp_time_from_cpuctx(cpuctx);
1774 if (!ctx->nr_active)
1777 if (event_type & EVENT_PINNED) {
1778 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1779 group_sched_out(event, cpuctx, ctx);
1782 if (event_type & EVENT_FLEXIBLE) {
1783 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1784 group_sched_out(event, cpuctx, ctx);
1787 perf_pmu_enable(ctx->pmu);
1788 raw_spin_unlock(&ctx->lock);
1792 * Test whether two contexts are equivalent, i.e. whether they
1793 * have both been cloned from the same version of the same context
1794 * and they both have the same number of enabled events.
1795 * If the number of enabled events is the same, then the set
1796 * of enabled events should be the same, because these are both
1797 * inherited contexts, therefore we can't access individual events
1798 * in them directly with an fd; we can only enable/disable all
1799 * events via prctl, or enable/disable all events in a family
1800 * via ioctl, which will have the same effect on both contexts.
1802 static int context_equiv(struct perf_event_context *ctx1,
1803 struct perf_event_context *ctx2)
1805 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1806 && ctx1->parent_gen == ctx2->parent_gen
1807 && !ctx1->pin_count && !ctx2->pin_count;
1810 static void __perf_event_sync_stat(struct perf_event *event,
1811 struct perf_event *next_event)
1815 if (!event->attr.inherit_stat)
1819 * Update the event value, we cannot use perf_event_read()
1820 * because we're in the middle of a context switch and have IRQs
1821 * disabled, which upsets smp_call_function_single(), however
1822 * we know the event must be on the current CPU, therefore we
1823 * don't need to use it.
1825 switch (event->state) {
1826 case PERF_EVENT_STATE_ACTIVE:
1827 event->pmu->read(event);
1830 case PERF_EVENT_STATE_INACTIVE:
1831 update_event_times(event);
1839 * In order to keep per-task stats reliable we need to flip the event
1840 * values when we flip the contexts.
1842 value = local64_read(&next_event->count);
1843 value = local64_xchg(&event->count, value);
1844 local64_set(&next_event->count, value);
1846 swap(event->total_time_enabled, next_event->total_time_enabled);
1847 swap(event->total_time_running, next_event->total_time_running);
1850 * Since we swizzled the values, update the user visible data too.
1852 perf_event_update_userpage(event);
1853 perf_event_update_userpage(next_event);
1856 #define list_next_entry(pos, member) \
1857 list_entry(pos->member.next, typeof(*pos), member)
1859 static void perf_event_sync_stat(struct perf_event_context *ctx,
1860 struct perf_event_context *next_ctx)
1862 struct perf_event *event, *next_event;
1867 update_context_time(ctx);
1869 event = list_first_entry(&ctx->event_list,
1870 struct perf_event, event_entry);
1872 next_event = list_first_entry(&next_ctx->event_list,
1873 struct perf_event, event_entry);
1875 while (&event->event_entry != &ctx->event_list &&
1876 &next_event->event_entry != &next_ctx->event_list) {
1878 __perf_event_sync_stat(event, next_event);
1880 event = list_next_entry(event, event_entry);
1881 next_event = list_next_entry(next_event, event_entry);
1885 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1886 struct task_struct *next)
1888 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1889 struct perf_event_context *next_ctx;
1890 struct perf_event_context *parent;
1891 struct perf_cpu_context *cpuctx;
1897 cpuctx = __get_cpu_context(ctx);
1898 if (!cpuctx->task_ctx)
1902 parent = rcu_dereference(ctx->parent_ctx);
1903 next_ctx = next->perf_event_ctxp[ctxn];
1904 if (parent && next_ctx &&
1905 rcu_dereference(next_ctx->parent_ctx) == parent) {
1907 * Looks like the two contexts are clones, so we might be
1908 * able to optimize the context switch. We lock both
1909 * contexts and check that they are clones under the
1910 * lock (including re-checking that neither has been
1911 * uncloned in the meantime). It doesn't matter which
1912 * order we take the locks because no other cpu could
1913 * be trying to lock both of these tasks.
1915 raw_spin_lock(&ctx->lock);
1916 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1917 if (context_equiv(ctx, next_ctx)) {
1919 * XXX do we need a memory barrier of sorts
1920 * wrt to rcu_dereference() of perf_event_ctxp
1922 task->perf_event_ctxp[ctxn] = next_ctx;
1923 next->perf_event_ctxp[ctxn] = ctx;
1925 next_ctx->task = task;
1928 perf_event_sync_stat(ctx, next_ctx);
1930 raw_spin_unlock(&next_ctx->lock);
1931 raw_spin_unlock(&ctx->lock);
1936 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1937 cpuctx->task_ctx = NULL;
1941 #define for_each_task_context_nr(ctxn) \
1942 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1945 * Called from scheduler to remove the events of the current task,
1946 * with interrupts disabled.
1948 * We stop each event and update the event value in event->count.
1950 * This does not protect us against NMI, but disable()
1951 * sets the disabled bit in the control field of event _before_
1952 * accessing the event control register. If a NMI hits, then it will
1953 * not restart the event.
1955 void __perf_event_task_sched_out(struct task_struct *task,
1956 struct task_struct *next)
1960 for_each_task_context_nr(ctxn)
1961 perf_event_context_sched_out(task, ctxn, next);
1964 * if cgroup events exist on this CPU, then we need
1965 * to check if we have to switch out PMU state.
1966 * cgroup event are system-wide mode only
1968 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
1969 perf_cgroup_sched_out(task);
1972 static void task_ctx_sched_out(struct perf_event_context *ctx,
1973 enum event_type_t event_type)
1975 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1977 if (!cpuctx->task_ctx)
1980 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1983 ctx_sched_out(ctx, cpuctx, event_type);
1984 cpuctx->task_ctx = NULL;
1988 * Called with IRQs disabled
1990 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1991 enum event_type_t event_type)
1993 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
1997 ctx_pinned_sched_in(struct perf_event_context *ctx,
1998 struct perf_cpu_context *cpuctx)
2000 struct perf_event *event;
2002 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2003 if (event->state <= PERF_EVENT_STATE_OFF)
2005 if (!event_filter_match(event))
2008 /* may need to reset tstamp_enabled */
2009 if (is_cgroup_event(event))
2010 perf_cgroup_mark_enabled(event, ctx);
2012 if (group_can_go_on(event, cpuctx, 1))
2013 group_sched_in(event, cpuctx, ctx);
2016 * If this pinned group hasn't been scheduled,
2017 * put it in error state.
2019 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2020 update_group_times(event);
2021 event->state = PERF_EVENT_STATE_ERROR;
2027 ctx_flexible_sched_in(struct perf_event_context *ctx,
2028 struct perf_cpu_context *cpuctx)
2030 struct perf_event *event;
2033 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2034 /* Ignore events in OFF or ERROR state */
2035 if (event->state <= PERF_EVENT_STATE_OFF)
2038 * Listen to the 'cpu' scheduling filter constraint
2041 if (!event_filter_match(event))
2044 /* may need to reset tstamp_enabled */
2045 if (is_cgroup_event(event))
2046 perf_cgroup_mark_enabled(event, ctx);
2048 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2049 if (group_sched_in(event, cpuctx, ctx))
2056 ctx_sched_in(struct perf_event_context *ctx,
2057 struct perf_cpu_context *cpuctx,
2058 enum event_type_t event_type,
2059 struct task_struct *task)
2063 raw_spin_lock(&ctx->lock);
2065 if (likely(!ctx->nr_events))
2069 ctx->timestamp = now;
2070 perf_cgroup_set_timestamp(task, ctx);
2072 * First go through the list and put on any pinned groups
2073 * in order to give them the best chance of going on.
2075 if (event_type & EVENT_PINNED)
2076 ctx_pinned_sched_in(ctx, cpuctx);
2078 /* Then walk through the lower prio flexible groups */
2079 if (event_type & EVENT_FLEXIBLE)
2080 ctx_flexible_sched_in(ctx, cpuctx);
2083 raw_spin_unlock(&ctx->lock);
2086 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2087 enum event_type_t event_type,
2088 struct task_struct *task)
2090 struct perf_event_context *ctx = &cpuctx->ctx;
2092 ctx_sched_in(ctx, cpuctx, event_type, task);
2095 static void task_ctx_sched_in(struct perf_event_context *ctx,
2096 enum event_type_t event_type)
2098 struct perf_cpu_context *cpuctx;
2100 cpuctx = __get_cpu_context(ctx);
2101 if (cpuctx->task_ctx == ctx)
2104 ctx_sched_in(ctx, cpuctx, event_type, NULL);
2105 cpuctx->task_ctx = ctx;
2108 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2109 struct task_struct *task)
2111 struct perf_cpu_context *cpuctx;
2113 cpuctx = __get_cpu_context(ctx);
2114 if (cpuctx->task_ctx == ctx)
2117 perf_pmu_disable(ctx->pmu);
2119 * We want to keep the following priority order:
2120 * cpu pinned (that don't need to move), task pinned,
2121 * cpu flexible, task flexible.
2123 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2125 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2126 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2127 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2129 cpuctx->task_ctx = ctx;
2132 * Since these rotations are per-cpu, we need to ensure the
2133 * cpu-context we got scheduled on is actually rotating.
2135 perf_pmu_rotate_start(ctx->pmu);
2136 perf_pmu_enable(ctx->pmu);
2140 * Called from scheduler to add the events of the current task
2141 * with interrupts disabled.
2143 * We restore the event value and then enable it.
2145 * This does not protect us against NMI, but enable()
2146 * sets the enabled bit in the control field of event _before_
2147 * accessing the event control register. If a NMI hits, then it will
2148 * keep the event running.
2150 void __perf_event_task_sched_in(struct task_struct *task)
2152 struct perf_event_context *ctx;
2155 for_each_task_context_nr(ctxn) {
2156 ctx = task->perf_event_ctxp[ctxn];
2160 perf_event_context_sched_in(ctx, task);
2163 * if cgroup events exist on this CPU, then we need
2164 * to check if we have to switch in PMU state.
2165 * cgroup event are system-wide mode only
2167 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2168 perf_cgroup_sched_in(task);
2171 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2173 u64 frequency = event->attr.sample_freq;
2174 u64 sec = NSEC_PER_SEC;
2175 u64 divisor, dividend;
2177 int count_fls, nsec_fls, frequency_fls, sec_fls;
2179 count_fls = fls64(count);
2180 nsec_fls = fls64(nsec);
2181 frequency_fls = fls64(frequency);
2185 * We got @count in @nsec, with a target of sample_freq HZ
2186 * the target period becomes:
2189 * period = -------------------
2190 * @nsec * sample_freq
2195 * Reduce accuracy by one bit such that @a and @b converge
2196 * to a similar magnitude.
2198 #define REDUCE_FLS(a, b) \
2200 if (a##_fls > b##_fls) { \
2210 * Reduce accuracy until either term fits in a u64, then proceed with
2211 * the other, so that finally we can do a u64/u64 division.
2213 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2214 REDUCE_FLS(nsec, frequency);
2215 REDUCE_FLS(sec, count);
2218 if (count_fls + sec_fls > 64) {
2219 divisor = nsec * frequency;
2221 while (count_fls + sec_fls > 64) {
2222 REDUCE_FLS(count, sec);
2226 dividend = count * sec;
2228 dividend = count * sec;
2230 while (nsec_fls + frequency_fls > 64) {
2231 REDUCE_FLS(nsec, frequency);
2235 divisor = nsec * frequency;
2241 return div64_u64(dividend, divisor);
2244 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
2246 struct hw_perf_event *hwc = &event->hw;
2247 s64 period, sample_period;
2250 period = perf_calculate_period(event, nsec, count);
2252 delta = (s64)(period - hwc->sample_period);
2253 delta = (delta + 7) / 8; /* low pass filter */
2255 sample_period = hwc->sample_period + delta;
2260 hwc->sample_period = sample_period;
2262 if (local64_read(&hwc->period_left) > 8*sample_period) {
2263 event->pmu->stop(event, PERF_EF_UPDATE);
2264 local64_set(&hwc->period_left, 0);
2265 event->pmu->start(event, PERF_EF_RELOAD);
2269 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
2271 struct perf_event *event;
2272 struct hw_perf_event *hwc;
2273 u64 interrupts, now;
2276 raw_spin_lock(&ctx->lock);
2277 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2278 if (event->state != PERF_EVENT_STATE_ACTIVE)
2281 if (!event_filter_match(event))
2286 interrupts = hwc->interrupts;
2287 hwc->interrupts = 0;
2290 * unthrottle events on the tick
2292 if (interrupts == MAX_INTERRUPTS) {
2293 perf_log_throttle(event, 1);
2294 event->pmu->start(event, 0);
2297 if (!event->attr.freq || !event->attr.sample_freq)
2300 event->pmu->read(event);
2301 now = local64_read(&event->count);
2302 delta = now - hwc->freq_count_stamp;
2303 hwc->freq_count_stamp = now;
2306 perf_adjust_period(event, period, delta);
2308 raw_spin_unlock(&ctx->lock);
2312 * Round-robin a context's events:
2314 static void rotate_ctx(struct perf_event_context *ctx)
2316 raw_spin_lock(&ctx->lock);
2319 * Rotate the first entry last of non-pinned groups. Rotation might be
2320 * disabled by the inheritance code.
2322 if (!ctx->rotate_disable)
2323 list_rotate_left(&ctx->flexible_groups);
2325 raw_spin_unlock(&ctx->lock);
2329 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2330 * because they're strictly cpu affine and rotate_start is called with IRQs
2331 * disabled, while rotate_context is called from IRQ context.
2333 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
2335 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
2336 struct perf_event_context *ctx = NULL;
2337 int rotate = 0, remove = 1;
2339 if (cpuctx->ctx.nr_events) {
2341 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2345 ctx = cpuctx->task_ctx;
2346 if (ctx && ctx->nr_events) {
2348 if (ctx->nr_events != ctx->nr_active)
2352 perf_pmu_disable(cpuctx->ctx.pmu);
2353 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
2355 perf_ctx_adjust_freq(ctx, interval);
2360 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2362 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
2364 rotate_ctx(&cpuctx->ctx);
2368 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, current);
2370 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
2374 list_del_init(&cpuctx->rotation_list);
2376 perf_pmu_enable(cpuctx->ctx.pmu);
2379 void perf_event_task_tick(void)
2381 struct list_head *head = &__get_cpu_var(rotation_list);
2382 struct perf_cpu_context *cpuctx, *tmp;
2384 WARN_ON(!irqs_disabled());
2386 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2387 if (cpuctx->jiffies_interval == 1 ||
2388 !(jiffies % cpuctx->jiffies_interval))
2389 perf_rotate_context(cpuctx);
2393 static int event_enable_on_exec(struct perf_event *event,
2394 struct perf_event_context *ctx)
2396 if (!event->attr.enable_on_exec)
2399 event->attr.enable_on_exec = 0;
2400 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2403 __perf_event_mark_enabled(event, ctx);
2409 * Enable all of a task's events that have been marked enable-on-exec.
2410 * This expects task == current.
2412 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2414 struct perf_event *event;
2415 unsigned long flags;
2419 local_irq_save(flags);
2420 if (!ctx || !ctx->nr_events)
2423 task_ctx_sched_out(ctx, EVENT_ALL);
2425 raw_spin_lock(&ctx->lock);
2427 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2428 ret = event_enable_on_exec(event, ctx);
2433 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2434 ret = event_enable_on_exec(event, ctx);
2440 * Unclone this context if we enabled any event.
2445 raw_spin_unlock(&ctx->lock);
2447 perf_event_context_sched_in(ctx, ctx->task);
2449 local_irq_restore(flags);
2453 * Cross CPU call to read the hardware event
2455 static void __perf_event_read(void *info)
2457 struct perf_event *event = info;
2458 struct perf_event_context *ctx = event->ctx;
2459 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2462 * If this is a task context, we need to check whether it is
2463 * the current task context of this cpu. If not it has been
2464 * scheduled out before the smp call arrived. In that case
2465 * event->count would have been updated to a recent sample
2466 * when the event was scheduled out.
2468 if (ctx->task && cpuctx->task_ctx != ctx)
2471 raw_spin_lock(&ctx->lock);
2472 if (ctx->is_active) {
2473 update_context_time(ctx);
2474 update_cgrp_time_from_event(event);
2476 update_event_times(event);
2477 if (event->state == PERF_EVENT_STATE_ACTIVE)
2478 event->pmu->read(event);
2479 raw_spin_unlock(&ctx->lock);
2482 static inline u64 perf_event_count(struct perf_event *event)
2484 return local64_read(&event->count) + atomic64_read(&event->child_count);
2487 static u64 perf_event_read(struct perf_event *event)
2490 * If event is enabled and currently active on a CPU, update the
2491 * value in the event structure:
2493 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2494 smp_call_function_single(event->oncpu,
2495 __perf_event_read, event, 1);
2496 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2497 struct perf_event_context *ctx = event->ctx;
2498 unsigned long flags;
2500 raw_spin_lock_irqsave(&ctx->lock, flags);
2502 * may read while context is not active
2503 * (e.g., thread is blocked), in that case
2504 * we cannot update context time
2506 if (ctx->is_active) {
2507 update_context_time(ctx);
2508 update_cgrp_time_from_event(event);
2510 update_event_times(event);
2511 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2514 return perf_event_count(event);
2521 struct callchain_cpus_entries {
2522 struct rcu_head rcu_head;
2523 struct perf_callchain_entry *cpu_entries[0];
2526 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
2527 static atomic_t nr_callchain_events;
2528 static DEFINE_MUTEX(callchain_mutex);
2529 struct callchain_cpus_entries *callchain_cpus_entries;
2532 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
2533 struct pt_regs *regs)
2537 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
2538 struct pt_regs *regs)
2542 static void release_callchain_buffers_rcu(struct rcu_head *head)
2544 struct callchain_cpus_entries *entries;
2547 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
2549 for_each_possible_cpu(cpu)
2550 kfree(entries->cpu_entries[cpu]);
2555 static void release_callchain_buffers(void)
2557 struct callchain_cpus_entries *entries;
2559 entries = callchain_cpus_entries;
2560 rcu_assign_pointer(callchain_cpus_entries, NULL);
2561 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2564 static int alloc_callchain_buffers(void)
2568 struct callchain_cpus_entries *entries;
2571 * We can't use the percpu allocation API for data that can be
2572 * accessed from NMI. Use a temporary manual per cpu allocation
2573 * until that gets sorted out.
2575 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
2577 entries = kzalloc(size, GFP_KERNEL);
2581 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
2583 for_each_possible_cpu(cpu) {
2584 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2586 if (!entries->cpu_entries[cpu])
2590 rcu_assign_pointer(callchain_cpus_entries, entries);
2595 for_each_possible_cpu(cpu)
2596 kfree(entries->cpu_entries[cpu]);
2602 static int get_callchain_buffers(void)
2607 mutex_lock(&callchain_mutex);
2609 count = atomic_inc_return(&nr_callchain_events);
2610 if (WARN_ON_ONCE(count < 1)) {
2616 /* If the allocation failed, give up */
2617 if (!callchain_cpus_entries)
2622 err = alloc_callchain_buffers();
2624 release_callchain_buffers();
2626 mutex_unlock(&callchain_mutex);
2631 static void put_callchain_buffers(void)
2633 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2634 release_callchain_buffers();
2635 mutex_unlock(&callchain_mutex);
2639 static int get_recursion_context(int *recursion)
2647 else if (in_softirq())
2652 if (recursion[rctx])
2661 static inline void put_recursion_context(int *recursion, int rctx)
2667 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2670 struct callchain_cpus_entries *entries;
2672 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2676 entries = rcu_dereference(callchain_cpus_entries);
2680 cpu = smp_processor_id();
2682 return &entries->cpu_entries[cpu][*rctx];
2686 put_callchain_entry(int rctx)
2688 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2691 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2694 struct perf_callchain_entry *entry;
2697 entry = get_callchain_entry(&rctx);
2706 if (!user_mode(regs)) {
2707 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2708 perf_callchain_kernel(entry, regs);
2710 regs = task_pt_regs(current);
2716 perf_callchain_store(entry, PERF_CONTEXT_USER);
2717 perf_callchain_user(entry, regs);
2721 put_callchain_entry(rctx);
2727 * Initialize the perf_event context in a task_struct:
2729 static void __perf_event_init_context(struct perf_event_context *ctx)
2731 raw_spin_lock_init(&ctx->lock);
2732 mutex_init(&ctx->mutex);
2733 INIT_LIST_HEAD(&ctx->pinned_groups);
2734 INIT_LIST_HEAD(&ctx->flexible_groups);
2735 INIT_LIST_HEAD(&ctx->event_list);
2736 atomic_set(&ctx->refcount, 1);
2739 static struct perf_event_context *
2740 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2742 struct perf_event_context *ctx;
2744 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2748 __perf_event_init_context(ctx);
2751 get_task_struct(task);
2758 static struct task_struct *
2759 find_lively_task_by_vpid(pid_t vpid)
2761 struct task_struct *task;
2768 task = find_task_by_vpid(vpid);
2770 get_task_struct(task);
2774 return ERR_PTR(-ESRCH);
2776 /* Reuse ptrace permission checks for now. */
2778 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2783 put_task_struct(task);
2784 return ERR_PTR(err);
2789 * Returns a matching context with refcount and pincount.
2791 static struct perf_event_context *
2792 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2794 struct perf_event_context *ctx;
2795 struct perf_cpu_context *cpuctx;
2796 unsigned long flags;
2800 /* Must be root to operate on a CPU event: */
2801 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2802 return ERR_PTR(-EACCES);
2805 * We could be clever and allow to attach a event to an
2806 * offline CPU and activate it when the CPU comes up, but
2809 if (!cpu_online(cpu))
2810 return ERR_PTR(-ENODEV);
2812 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2821 ctxn = pmu->task_ctx_nr;
2826 ctx = perf_lock_task_context(task, ctxn, &flags);
2830 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2834 ctx = alloc_perf_context(pmu, task);
2842 mutex_lock(&task->perf_event_mutex);
2844 * If it has already passed perf_event_exit_task().
2845 * we must see PF_EXITING, it takes this mutex too.
2847 if (task->flags & PF_EXITING)
2849 else if (task->perf_event_ctxp[ctxn])
2853 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2855 mutex_unlock(&task->perf_event_mutex);
2857 if (unlikely(err)) {
2858 put_task_struct(task);
2870 return ERR_PTR(err);
2873 static void perf_event_free_filter(struct perf_event *event);
2875 static void free_event_rcu(struct rcu_head *head)
2877 struct perf_event *event;
2879 event = container_of(head, struct perf_event, rcu_head);
2881 put_pid_ns(event->ns);
2882 perf_event_free_filter(event);
2886 static void perf_buffer_put(struct perf_buffer *buffer);
2888 static void free_event(struct perf_event *event)
2890 irq_work_sync(&event->pending);
2892 if (!event->parent) {
2893 if (event->attach_state & PERF_ATTACH_TASK)
2894 jump_label_dec(&perf_sched_events);
2895 if (event->attr.mmap || event->attr.mmap_data)
2896 atomic_dec(&nr_mmap_events);
2897 if (event->attr.comm)
2898 atomic_dec(&nr_comm_events);
2899 if (event->attr.task)
2900 atomic_dec(&nr_task_events);
2901 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2902 put_callchain_buffers();
2905 if (event->buffer) {
2906 perf_buffer_put(event->buffer);
2907 event->buffer = NULL;
2910 if (is_cgroup_event(event))
2911 perf_detach_cgroup(event);
2914 event->destroy(event);
2917 put_ctx(event->ctx);
2919 call_rcu(&event->rcu_head, free_event_rcu);
2922 int perf_event_release_kernel(struct perf_event *event)
2924 struct perf_event_context *ctx = event->ctx;
2927 * Remove from the PMU, can't get re-enabled since we got
2928 * here because the last ref went.
2930 perf_event_disable(event);
2932 WARN_ON_ONCE(ctx->parent_ctx);
2934 * There are two ways this annotation is useful:
2936 * 1) there is a lock recursion from perf_event_exit_task
2937 * see the comment there.
2939 * 2) there is a lock-inversion with mmap_sem through
2940 * perf_event_read_group(), which takes faults while
2941 * holding ctx->mutex, however this is called after
2942 * the last filedesc died, so there is no possibility
2943 * to trigger the AB-BA case.
2945 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2946 raw_spin_lock_irq(&ctx->lock);
2947 perf_group_detach(event);
2948 list_del_event(event, ctx);
2949 raw_spin_unlock_irq(&ctx->lock);
2950 mutex_unlock(&ctx->mutex);
2956 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2959 * Called when the last reference to the file is gone.
2961 static int perf_release(struct inode *inode, struct file *file)
2963 struct perf_event *event = file->private_data;
2964 struct task_struct *owner;
2966 file->private_data = NULL;
2969 owner = ACCESS_ONCE(event->owner);
2971 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2972 * !owner it means the list deletion is complete and we can indeed
2973 * free this event, otherwise we need to serialize on
2974 * owner->perf_event_mutex.
2976 smp_read_barrier_depends();
2979 * Since delayed_put_task_struct() also drops the last
2980 * task reference we can safely take a new reference
2981 * while holding the rcu_read_lock().
2983 get_task_struct(owner);
2988 mutex_lock(&owner->perf_event_mutex);
2990 * We have to re-check the event->owner field, if it is cleared
2991 * we raced with perf_event_exit_task(), acquiring the mutex
2992 * ensured they're done, and we can proceed with freeing the
2996 list_del_init(&event->owner_entry);
2997 mutex_unlock(&owner->perf_event_mutex);
2998 put_task_struct(owner);
3001 return perf_event_release_kernel(event);
3004 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
3006 struct perf_event *child;
3012 mutex_lock(&event->child_mutex);
3013 total += perf_event_read(event);
3014 *enabled += event->total_time_enabled +
3015 atomic64_read(&event->child_total_time_enabled);
3016 *running += event->total_time_running +
3017 atomic64_read(&event->child_total_time_running);
3019 list_for_each_entry(child, &event->child_list, child_list) {
3020 total += perf_event_read(child);
3021 *enabled += child->total_time_enabled;
3022 *running += child->total_time_running;
3024 mutex_unlock(&event->child_mutex);
3028 EXPORT_SYMBOL_GPL(perf_event_read_value);
3030 static int perf_event_read_group(struct perf_event *event,
3031 u64 read_format, char __user *buf)
3033 struct perf_event *leader = event->group_leader, *sub;
3034 int n = 0, size = 0, ret = -EFAULT;
3035 struct perf_event_context *ctx = leader->ctx;
3037 u64 count, enabled, running;
3039 mutex_lock(&ctx->mutex);
3040 count = perf_event_read_value(leader, &enabled, &running);
3042 values[n++] = 1 + leader->nr_siblings;
3043 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3044 values[n++] = enabled;
3045 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3046 values[n++] = running;
3047 values[n++] = count;
3048 if (read_format & PERF_FORMAT_ID)
3049 values[n++] = primary_event_id(leader);
3051 size = n * sizeof(u64);
3053 if (copy_to_user(buf, values, size))
3058 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3061 values[n++] = perf_event_read_value(sub, &enabled, &running);
3062 if (read_format & PERF_FORMAT_ID)
3063 values[n++] = primary_event_id(sub);
3065 size = n * sizeof(u64);
3067 if (copy_to_user(buf + ret, values, size)) {
3075 mutex_unlock(&ctx->mutex);
3080 static int perf_event_read_one(struct perf_event *event,
3081 u64 read_format, char __user *buf)
3083 u64 enabled, running;
3087 values[n++] = perf_event_read_value(event, &enabled, &running);
3088 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3089 values[n++] = enabled;
3090 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3091 values[n++] = running;
3092 if (read_format & PERF_FORMAT_ID)
3093 values[n++] = primary_event_id(event);
3095 if (copy_to_user(buf, values, n * sizeof(u64)))
3098 return n * sizeof(u64);
3102 * Read the performance event - simple non blocking version for now
3105 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3107 u64 read_format = event->attr.read_format;
3111 * Return end-of-file for a read on a event that is in
3112 * error state (i.e. because it was pinned but it couldn't be
3113 * scheduled on to the CPU at some point).
3115 if (event->state == PERF_EVENT_STATE_ERROR)
3118 if (count < event->read_size)
3121 WARN_ON_ONCE(event->ctx->parent_ctx);
3122 if (read_format & PERF_FORMAT_GROUP)
3123 ret = perf_event_read_group(event, read_format, buf);
3125 ret = perf_event_read_one(event, read_format, buf);
3131 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3133 struct perf_event *event = file->private_data;
3135 return perf_read_hw(event, buf, count);
3138 static unsigned int perf_poll(struct file *file, poll_table *wait)
3140 struct perf_event *event = file->private_data;
3141 struct perf_buffer *buffer;
3142 unsigned int events = POLL_HUP;
3145 buffer = rcu_dereference(event->buffer);
3147 events = atomic_xchg(&buffer->poll, 0);
3150 poll_wait(file, &event->waitq, wait);
3155 static void perf_event_reset(struct perf_event *event)
3157 (void)perf_event_read(event);
3158 local64_set(&event->count, 0);
3159 perf_event_update_userpage(event);
3163 * Holding the top-level event's child_mutex means that any
3164 * descendant process that has inherited this event will block
3165 * in sync_child_event if it goes to exit, thus satisfying the
3166 * task existence requirements of perf_event_enable/disable.
3168 static void perf_event_for_each_child(struct perf_event *event,
3169 void (*func)(struct perf_event *))
3171 struct perf_event *child;
3173 WARN_ON_ONCE(event->ctx->parent_ctx);
3174 mutex_lock(&event->child_mutex);
3176 list_for_each_entry(child, &event->child_list, child_list)
3178 mutex_unlock(&event->child_mutex);
3181 static void perf_event_for_each(struct perf_event *event,
3182 void (*func)(struct perf_event *))
3184 struct perf_event_context *ctx = event->ctx;
3185 struct perf_event *sibling;
3187 WARN_ON_ONCE(ctx->parent_ctx);
3188 mutex_lock(&ctx->mutex);
3189 event = event->group_leader;
3191 perf_event_for_each_child(event, func);
3193 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3194 perf_event_for_each_child(event, func);
3195 mutex_unlock(&ctx->mutex);
3198 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3200 struct perf_event_context *ctx = event->ctx;
3204 if (!is_sampling_event(event))
3207 if (copy_from_user(&value, arg, sizeof(value)))
3213 raw_spin_lock_irq(&ctx->lock);
3214 if (event->attr.freq) {
3215 if (value > sysctl_perf_event_sample_rate) {
3220 event->attr.sample_freq = value;
3222 event->attr.sample_period = value;
3223 event->hw.sample_period = value;
3226 raw_spin_unlock_irq(&ctx->lock);
3231 static const struct file_operations perf_fops;
3233 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
3237 file = fget_light(fd, fput_needed);
3239 return ERR_PTR(-EBADF);
3241 if (file->f_op != &perf_fops) {
3242 fput_light(file, *fput_needed);
3244 return ERR_PTR(-EBADF);
3247 return file->private_data;
3250 static int perf_event_set_output(struct perf_event *event,
3251 struct perf_event *output_event);
3252 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3254 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3256 struct perf_event *event = file->private_data;
3257 void (*func)(struct perf_event *);
3261 case PERF_EVENT_IOC_ENABLE:
3262 func = perf_event_enable;
3264 case PERF_EVENT_IOC_DISABLE:
3265 func = perf_event_disable;
3267 case PERF_EVENT_IOC_RESET:
3268 func = perf_event_reset;
3271 case PERF_EVENT_IOC_REFRESH:
3272 return perf_event_refresh(event, arg);
3274 case PERF_EVENT_IOC_PERIOD:
3275 return perf_event_period(event, (u64 __user *)arg);
3277 case PERF_EVENT_IOC_SET_OUTPUT:
3279 struct perf_event *output_event = NULL;
3280 int fput_needed = 0;
3284 output_event = perf_fget_light(arg, &fput_needed);
3285 if (IS_ERR(output_event))
3286 return PTR_ERR(output_event);
3289 ret = perf_event_set_output(event, output_event);
3291 fput_light(output_event->filp, fput_needed);
3296 case PERF_EVENT_IOC_SET_FILTER:
3297 return perf_event_set_filter(event, (void __user *)arg);
3303 if (flags & PERF_IOC_FLAG_GROUP)
3304 perf_event_for_each(event, func);
3306 perf_event_for_each_child(event, func);
3311 int perf_event_task_enable(void)
3313 struct perf_event *event;
3315 mutex_lock(¤t->perf_event_mutex);
3316 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3317 perf_event_for_each_child(event, perf_event_enable);
3318 mutex_unlock(¤t->perf_event_mutex);
3323 int perf_event_task_disable(void)
3325 struct perf_event *event;
3327 mutex_lock(¤t->perf_event_mutex);
3328 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3329 perf_event_for_each_child(event, perf_event_disable);
3330 mutex_unlock(¤t->perf_event_mutex);
3335 #ifndef PERF_EVENT_INDEX_OFFSET
3336 # define PERF_EVENT_INDEX_OFFSET 0
3339 static int perf_event_index(struct perf_event *event)
3341 if (event->hw.state & PERF_HES_STOPPED)
3344 if (event->state != PERF_EVENT_STATE_ACTIVE)
3347 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
3351 * Callers need to ensure there can be no nesting of this function, otherwise
3352 * the seqlock logic goes bad. We can not serialize this because the arch
3353 * code calls this from NMI context.
3355 void perf_event_update_userpage(struct perf_event *event)
3357 struct perf_event_mmap_page *userpg;
3358 struct perf_buffer *buffer;
3361 buffer = rcu_dereference(event->buffer);
3365 userpg = buffer->user_page;
3368 * Disable preemption so as to not let the corresponding user-space
3369 * spin too long if we get preempted.
3374 userpg->index = perf_event_index(event);
3375 userpg->offset = perf_event_count(event);
3376 if (event->state == PERF_EVENT_STATE_ACTIVE)
3377 userpg->offset -= local64_read(&event->hw.prev_count);
3379 userpg->time_enabled = event->total_time_enabled +
3380 atomic64_read(&event->child_total_time_enabled);
3382 userpg->time_running = event->total_time_running +
3383 atomic64_read(&event->child_total_time_running);
3392 static unsigned long perf_data_size(struct perf_buffer *buffer);
3395 perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
3397 long max_size = perf_data_size(buffer);
3400 buffer->watermark = min(max_size, watermark);
3402 if (!buffer->watermark)
3403 buffer->watermark = max_size / 2;
3405 if (flags & PERF_BUFFER_WRITABLE)
3406 buffer->writable = 1;
3408 atomic_set(&buffer->refcount, 1);
3411 #ifndef CONFIG_PERF_USE_VMALLOC
3414 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
3417 static struct page *
3418 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
3420 if (pgoff > buffer->nr_pages)
3424 return virt_to_page(buffer->user_page);
3426 return virt_to_page(buffer->data_pages[pgoff - 1]);
3429 static void *perf_mmap_alloc_page(int cpu)
3434 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
3435 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
3439 return page_address(page);
3442 static struct perf_buffer *
3443 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
3445 struct perf_buffer *buffer;
3449 size = sizeof(struct perf_buffer);
3450 size += nr_pages * sizeof(void *);
3452 buffer = kzalloc(size, GFP_KERNEL);
3456 buffer->user_page = perf_mmap_alloc_page(cpu);
3457 if (!buffer->user_page)
3458 goto fail_user_page;
3460 for (i = 0; i < nr_pages; i++) {
3461 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
3462 if (!buffer->data_pages[i])
3463 goto fail_data_pages;
3466 buffer->nr_pages = nr_pages;
3468 perf_buffer_init(buffer, watermark, flags);
3473 for (i--; i >= 0; i--)
3474 free_page((unsigned long)buffer->data_pages[i]);
3476 free_page((unsigned long)buffer->user_page);
3485 static void perf_mmap_free_page(unsigned long addr)
3487 struct page *page = virt_to_page((void *)addr);
3489 page->mapping = NULL;
3493 static void perf_buffer_free(struct perf_buffer *buffer)
3497 perf_mmap_free_page((unsigned long)buffer->user_page);
3498 for (i = 0; i < buffer->nr_pages; i++)
3499 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
3503 static inline int page_order(struct perf_buffer *buffer)
3511 * Back perf_mmap() with vmalloc memory.
3513 * Required for architectures that have d-cache aliasing issues.
3516 static inline int page_order(struct perf_buffer *buffer)
3518 return buffer->page_order;
3521 static struct page *
3522 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
3524 if (pgoff > (1UL << page_order(buffer)))
3527 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
3530 static void perf_mmap_unmark_page(void *addr)
3532 struct page *page = vmalloc_to_page(addr);
3534 page->mapping = NULL;
3537 static void perf_buffer_free_work(struct work_struct *work)
3539 struct perf_buffer *buffer;
3543 buffer = container_of(work, struct perf_buffer, work);
3544 nr = 1 << page_order(buffer);
3546 base = buffer->user_page;
3547 for (i = 0; i < nr + 1; i++)
3548 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
3554 static void perf_buffer_free(struct perf_buffer *buffer)
3556 schedule_work(&buffer->work);
3559 static struct perf_buffer *
3560 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
3562 struct perf_buffer *buffer;
3566 size = sizeof(struct perf_buffer);
3567 size += sizeof(void *);
3569 buffer = kzalloc(size, GFP_KERNEL);
3573 INIT_WORK(&buffer->work, perf_buffer_free_work);
3575 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
3579 buffer->user_page = all_buf;
3580 buffer->data_pages[0] = all_buf + PAGE_SIZE;
3581 buffer->page_order = ilog2(nr_pages);
3582 buffer->nr_pages = 1;
3584 perf_buffer_init(buffer, watermark, flags);
3597 static unsigned long perf_data_size(struct perf_buffer *buffer)
3599 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
3602 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3604 struct perf_event *event = vma->vm_file->private_data;
3605 struct perf_buffer *buffer;
3606 int ret = VM_FAULT_SIGBUS;
3608 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3609 if (vmf->pgoff == 0)
3615 buffer = rcu_dereference(event->buffer);
3619 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3622 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
3626 get_page(vmf->page);
3627 vmf->page->mapping = vma->vm_file->f_mapping;
3628 vmf->page->index = vmf->pgoff;
3637 static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
3639 struct perf_buffer *buffer;
3641 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
3642 perf_buffer_free(buffer);
3645 static struct perf_buffer *perf_buffer_get(struct perf_event *event)
3647 struct perf_buffer *buffer;
3650 buffer = rcu_dereference(event->buffer);
3652 if (!atomic_inc_not_zero(&buffer->refcount))
3660 static void perf_buffer_put(struct perf_buffer *buffer)
3662 if (!atomic_dec_and_test(&buffer->refcount))
3665 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
3668 static void perf_mmap_open(struct vm_area_struct *vma)
3670 struct perf_event *event = vma->vm_file->private_data;
3672 atomic_inc(&event->mmap_count);
3675 static void perf_mmap_close(struct vm_area_struct *vma)
3677 struct perf_event *event = vma->vm_file->private_data;
3679 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
3680 unsigned long size = perf_data_size(event->buffer);
3681 struct user_struct *user = event->mmap_user;
3682 struct perf_buffer *buffer = event->buffer;
3684 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
3685 vma->vm_mm->locked_vm -= event->mmap_locked;
3686 rcu_assign_pointer(event->buffer, NULL);
3687 mutex_unlock(&event->mmap_mutex);
3689 perf_buffer_put(buffer);
3694 static const struct vm_operations_struct perf_mmap_vmops = {
3695 .open = perf_mmap_open,
3696 .close = perf_mmap_close,
3697 .fault = perf_mmap_fault,
3698 .page_mkwrite = perf_mmap_fault,
3701 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3703 struct perf_event *event = file->private_data;
3704 unsigned long user_locked, user_lock_limit;
3705 struct user_struct *user = current_user();
3706 unsigned long locked, lock_limit;
3707 struct perf_buffer *buffer;
3708 unsigned long vma_size;
3709 unsigned long nr_pages;
3710 long user_extra, extra;
3711 int ret = 0, flags = 0;
3714 * Don't allow mmap() of inherited per-task counters. This would
3715 * create a performance issue due to all children writing to the
3718 if (event->cpu == -1 && event->attr.inherit)
3721 if (!(vma->vm_flags & VM_SHARED))
3724 vma_size = vma->vm_end - vma->vm_start;
3725 nr_pages = (vma_size / PAGE_SIZE) - 1;
3728 * If we have buffer pages ensure they're a power-of-two number, so we
3729 * can do bitmasks instead of modulo.
3731 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3734 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3737 if (vma->vm_pgoff != 0)
3740 WARN_ON_ONCE(event->ctx->parent_ctx);
3741 mutex_lock(&event->mmap_mutex);
3742 if (event->buffer) {
3743 if (event->buffer->nr_pages == nr_pages)
3744 atomic_inc(&event->buffer->refcount);
3750 user_extra = nr_pages + 1;
3751 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3754 * Increase the limit linearly with more CPUs:
3756 user_lock_limit *= num_online_cpus();
3758 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3761 if (user_locked > user_lock_limit)
3762 extra = user_locked - user_lock_limit;
3764 lock_limit = rlimit(RLIMIT_MEMLOCK);
3765 lock_limit >>= PAGE_SHIFT;
3766 locked = vma->vm_mm->locked_vm + extra;
3768 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3769 !capable(CAP_IPC_LOCK)) {
3774 WARN_ON(event->buffer);
3776 if (vma->vm_flags & VM_WRITE)
3777 flags |= PERF_BUFFER_WRITABLE;
3779 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3785 rcu_assign_pointer(event->buffer, buffer);
3787 atomic_long_add(user_extra, &user->locked_vm);
3788 event->mmap_locked = extra;
3789 event->mmap_user = get_current_user();
3790 vma->vm_mm->locked_vm += event->mmap_locked;
3794 atomic_inc(&event->mmap_count);
3795 mutex_unlock(&event->mmap_mutex);
3797 vma->vm_flags |= VM_RESERVED;
3798 vma->vm_ops = &perf_mmap_vmops;
3803 static int perf_fasync(int fd, struct file *filp, int on)
3805 struct inode *inode = filp->f_path.dentry->d_inode;
3806 struct perf_event *event = filp->private_data;
3809 mutex_lock(&inode->i_mutex);
3810 retval = fasync_helper(fd, filp, on, &event->fasync);
3811 mutex_unlock(&inode->i_mutex);
3819 static const struct file_operations perf_fops = {
3820 .llseek = no_llseek,
3821 .release = perf_release,
3824 .unlocked_ioctl = perf_ioctl,
3825 .compat_ioctl = perf_ioctl,
3827 .fasync = perf_fasync,
3833 * If there's data, ensure we set the poll() state and publish everything
3834 * to user-space before waking everybody up.
3837 void perf_event_wakeup(struct perf_event *event)
3839 wake_up_all(&event->waitq);
3841 if (event->pending_kill) {
3842 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3843 event->pending_kill = 0;
3847 static void perf_pending_event(struct irq_work *entry)
3849 struct perf_event *event = container_of(entry,
3850 struct perf_event, pending);
3852 if (event->pending_disable) {
3853 event->pending_disable = 0;
3854 __perf_event_disable(event);
3857 if (event->pending_wakeup) {
3858 event->pending_wakeup = 0;
3859 perf_event_wakeup(event);
3864 * We assume there is only KVM supporting the callbacks.
3865 * Later on, we might change it to a list if there is
3866 * another virtualization implementation supporting the callbacks.
3868 struct perf_guest_info_callbacks *perf_guest_cbs;
3870 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3872 perf_guest_cbs = cbs;
3875 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3877 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3879 perf_guest_cbs = NULL;
3882 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3887 static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
3888 unsigned long offset, unsigned long head)
3892 if (!buffer->writable)
3895 mask = perf_data_size(buffer) - 1;
3897 offset = (offset - tail) & mask;
3898 head = (head - tail) & mask;
3900 if ((int)(head - offset) < 0)
3906 static void perf_output_wakeup(struct perf_output_handle *handle)
3908 atomic_set(&handle->buffer->poll, POLL_IN);
3911 handle->event->pending_wakeup = 1;
3912 irq_work_queue(&handle->event->pending);
3914 perf_event_wakeup(handle->event);
3918 * We need to ensure a later event_id doesn't publish a head when a former
3919 * event isn't done writing. However since we need to deal with NMIs we
3920 * cannot fully serialize things.
3922 * We only publish the head (and generate a wakeup) when the outer-most
3925 static void perf_output_get_handle(struct perf_output_handle *handle)
3927 struct perf_buffer *buffer = handle->buffer;
3930 local_inc(&buffer->nest);
3931 handle->wakeup = local_read(&buffer->wakeup);
3934 static void perf_output_put_handle(struct perf_output_handle *handle)
3936 struct perf_buffer *buffer = handle->buffer;
3940 head = local_read(&buffer->head);
3943 * IRQ/NMI can happen here, which means we can miss a head update.
3946 if (!local_dec_and_test(&buffer->nest))
3950 * Publish the known good head. Rely on the full barrier implied
3951 * by atomic_dec_and_test() order the buffer->head read and this
3954 buffer->user_page->data_head = head;
3957 * Now check if we missed an update, rely on the (compiler)
3958 * barrier in atomic_dec_and_test() to re-read buffer->head.
3960 if (unlikely(head != local_read(&buffer->head))) {
3961 local_inc(&buffer->nest);
3965 if (handle->wakeup != local_read(&buffer->wakeup))
3966 perf_output_wakeup(handle);
3972 __always_inline void perf_output_copy(struct perf_output_handle *handle,
3973 const void *buf, unsigned int len)
3976 unsigned long size = min_t(unsigned long, handle->size, len);
3978 memcpy(handle->addr, buf, size);
3981 handle->addr += size;
3983 handle->size -= size;
3984 if (!handle->size) {
3985 struct perf_buffer *buffer = handle->buffer;
3988 handle->page &= buffer->nr_pages - 1;
3989 handle->addr = buffer->data_pages[handle->page];
3990 handle->size = PAGE_SIZE << page_order(buffer);
3995 static void __perf_event_header__init_id(struct perf_event_header *header,
3996 struct perf_sample_data *data,
3997 struct perf_event *event)
3999 u64 sample_type = event->attr.sample_type;
4001 data->type = sample_type;
4002 header->size += event->id_header_size;
4004 if (sample_type & PERF_SAMPLE_TID) {
4005 /* namespace issues */
4006 data->tid_entry.pid = perf_event_pid(event, current);
4007 data->tid_entry.tid = perf_event_tid(event, current);
4010 if (sample_type & PERF_SAMPLE_TIME)
4011 data->time = perf_clock();
4013 if (sample_type & PERF_SAMPLE_ID)
4014 data->id = primary_event_id(event);
4016 if (sample_type & PERF_SAMPLE_STREAM_ID)
4017 data->stream_id = event->id;
4019 if (sample_type & PERF_SAMPLE_CPU) {
4020 data->cpu_entry.cpu = raw_smp_processor_id();
4021 data->cpu_entry.reserved = 0;
4025 static void perf_event_header__init_id(struct perf_event_header *header,
4026 struct perf_sample_data *data,
4027 struct perf_event *event)
4029 if (event->attr.sample_id_all)
4030 __perf_event_header__init_id(header, data, event);
4033 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4034 struct perf_sample_data *data)
4036 u64 sample_type = data->type;
4038 if (sample_type & PERF_SAMPLE_TID)
4039 perf_output_put(handle, data->tid_entry);
4041 if (sample_type & PERF_SAMPLE_TIME)
4042 perf_output_put(handle, data->time);
4044 if (sample_type & PERF_SAMPLE_ID)
4045 perf_output_put(handle, data->id);
4047 if (sample_type & PERF_SAMPLE_STREAM_ID)
4048 perf_output_put(handle, data->stream_id);
4050 if (sample_type & PERF_SAMPLE_CPU)
4051 perf_output_put(handle, data->cpu_entry);
4054 static void perf_event__output_id_sample(struct perf_event *event,
4055 struct perf_output_handle *handle,
4056 struct perf_sample_data *sample)
4058 if (event->attr.sample_id_all)
4059 __perf_event__output_id_sample(handle, sample);
4062 int perf_output_begin(struct perf_output_handle *handle,
4063 struct perf_event *event, unsigned int size,
4064 int nmi, int sample)
4066 struct perf_buffer *buffer;
4067 unsigned long tail, offset, head;
4069 struct perf_sample_data sample_data;
4071 struct perf_event_header header;
4078 * For inherited events we send all the output towards the parent.
4081 event = event->parent;
4083 buffer = rcu_dereference(event->buffer);
4087 handle->buffer = buffer;
4088 handle->event = event;
4090 handle->sample = sample;
4092 if (!buffer->nr_pages)
4095 have_lost = local_read(&buffer->lost);
4097 lost_event.header.size = sizeof(lost_event);
4098 perf_event_header__init_id(&lost_event.header, &sample_data,
4100 size += lost_event.header.size;
4103 perf_output_get_handle(handle);
4107 * Userspace could choose to issue a mb() before updating the
4108 * tail pointer. So that all reads will be completed before the
4111 tail = ACCESS_ONCE(buffer->user_page->data_tail);
4113 offset = head = local_read(&buffer->head);
4115 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
4117 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
4119 if (head - local_read(&buffer->wakeup) > buffer->watermark)
4120 local_add(buffer->watermark, &buffer->wakeup);
4122 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
4123 handle->page &= buffer->nr_pages - 1;
4124 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
4125 handle->addr = buffer->data_pages[handle->page];
4126 handle->addr += handle->size;
4127 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
4130 lost_event.header.type = PERF_RECORD_LOST;
4131 lost_event.header.misc = 0;
4132 lost_event.id = event->id;
4133 lost_event.lost = local_xchg(&buffer->lost, 0);
4135 perf_output_put(handle, lost_event);
4136 perf_event__output_id_sample(event, handle, &sample_data);
4142 local_inc(&buffer->lost);
4143 perf_output_put_handle(handle);
4150 void perf_output_end(struct perf_output_handle *handle)
4152 struct perf_event *event = handle->event;
4153 struct perf_buffer *buffer = handle->buffer;
4155 int wakeup_events = event->attr.wakeup_events;
4157 if (handle->sample && wakeup_events) {
4158 int events = local_inc_return(&buffer->events);
4159 if (events >= wakeup_events) {
4160 local_sub(wakeup_events, &buffer->events);
4161 local_inc(&buffer->wakeup);
4165 perf_output_put_handle(handle);
4169 static void perf_output_read_one(struct perf_output_handle *handle,
4170 struct perf_event *event,
4171 u64 enabled, u64 running)
4173 u64 read_format = event->attr.read_format;
4177 values[n++] = perf_event_count(event);
4178 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4179 values[n++] = enabled +
4180 atomic64_read(&event->child_total_time_enabled);
4182 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4183 values[n++] = running +
4184 atomic64_read(&event->child_total_time_running);
4186 if (read_format & PERF_FORMAT_ID)
4187 values[n++] = primary_event_id(event);
4189 perf_output_copy(handle, values, n * sizeof(u64));
4193 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4195 static void perf_output_read_group(struct perf_output_handle *handle,
4196 struct perf_event *event,
4197 u64 enabled, u64 running)
4199 struct perf_event *leader = event->group_leader, *sub;
4200 u64 read_format = event->attr.read_format;
4204 values[n++] = 1 + leader->nr_siblings;
4206 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4207 values[n++] = enabled;
4209 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4210 values[n++] = running;
4212 if (leader != event)
4213 leader->pmu->read(leader);
4215 values[n++] = perf_event_count(leader);
4216 if (read_format & PERF_FORMAT_ID)
4217 values[n++] = primary_event_id(leader);
4219 perf_output_copy(handle, values, n * sizeof(u64));
4221 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4225 sub->pmu->read(sub);
4227 values[n++] = perf_event_count(sub);
4228 if (read_format & PERF_FORMAT_ID)
4229 values[n++] = primary_event_id(sub);
4231 perf_output_copy(handle, values, n * sizeof(u64));
4235 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4236 PERF_FORMAT_TOTAL_TIME_RUNNING)
4238 static void perf_output_read(struct perf_output_handle *handle,
4239 struct perf_event *event)
4241 u64 enabled = 0, running = 0, now, ctx_time;
4242 u64 read_format = event->attr.read_format;
4245 * compute total_time_enabled, total_time_running
4246 * based on snapshot values taken when the event
4247 * was last scheduled in.
4249 * we cannot simply called update_context_time()
4250 * because of locking issue as we are called in
4253 if (read_format & PERF_FORMAT_TOTAL_TIMES) {
4255 ctx_time = event->shadow_ctx_time + now;
4256 enabled = ctx_time - event->tstamp_enabled;
4257 running = ctx_time - event->tstamp_running;
4260 if (event->attr.read_format & PERF_FORMAT_GROUP)
4261 perf_output_read_group(handle, event, enabled, running);
4263 perf_output_read_one(handle, event, enabled, running);
4266 void perf_output_sample(struct perf_output_handle *handle,
4267 struct perf_event_header *header,
4268 struct perf_sample_data *data,
4269 struct perf_event *event)
4271 u64 sample_type = data->type;
4273 perf_output_put(handle, *header);
4275 if (sample_type & PERF_SAMPLE_IP)
4276 perf_output_put(handle, data->ip);
4278 if (sample_type & PERF_SAMPLE_TID)
4279 perf_output_put(handle, data->tid_entry);
4281 if (sample_type & PERF_SAMPLE_TIME)
4282 perf_output_put(handle, data->time);
4284 if (sample_type & PERF_SAMPLE_ADDR)
4285 perf_output_put(handle, data->addr);
4287 if (sample_type & PERF_SAMPLE_ID)
4288 perf_output_put(handle, data->id);
4290 if (sample_type & PERF_SAMPLE_STREAM_ID)
4291 perf_output_put(handle, data->stream_id);
4293 if (sample_type & PERF_SAMPLE_CPU)
4294 perf_output_put(handle, data->cpu_entry);
4296 if (sample_type & PERF_SAMPLE_PERIOD)
4297 perf_output_put(handle, data->period);
4299 if (sample_type & PERF_SAMPLE_READ)
4300 perf_output_read(handle, event);
4302 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4303 if (data->callchain) {
4306 if (data->callchain)
4307 size += data->callchain->nr;
4309 size *= sizeof(u64);
4311 perf_output_copy(handle, data->callchain, size);
4314 perf_output_put(handle, nr);
4318 if (sample_type & PERF_SAMPLE_RAW) {
4320 perf_output_put(handle, data->raw->size);
4321 perf_output_copy(handle, data->raw->data,
4328 .size = sizeof(u32),
4331 perf_output_put(handle, raw);
4336 void perf_prepare_sample(struct perf_event_header *header,
4337 struct perf_sample_data *data,
4338 struct perf_event *event,
4339 struct pt_regs *regs)
4341 u64 sample_type = event->attr.sample_type;
4343 header->type = PERF_RECORD_SAMPLE;
4344 header->size = sizeof(*header) + event->header_size;
4347 header->misc |= perf_misc_flags(regs);
4349 __perf_event_header__init_id(header, data, event);
4351 if (sample_type & PERF_SAMPLE_IP)
4352 data->ip = perf_instruction_pointer(regs);
4354 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4357 data->callchain = perf_callchain(regs);
4359 if (data->callchain)
4360 size += data->callchain->nr;
4362 header->size += size * sizeof(u64);
4365 if (sample_type & PERF_SAMPLE_RAW) {
4366 int size = sizeof(u32);
4369 size += data->raw->size;
4371 size += sizeof(u32);
4373 WARN_ON_ONCE(size & (sizeof(u64)-1));
4374 header->size += size;
4378 static void perf_event_output(struct perf_event *event, int nmi,
4379 struct perf_sample_data *data,
4380 struct pt_regs *regs)
4382 struct perf_output_handle handle;
4383 struct perf_event_header header;
4385 /* protect the callchain buffers */
4388 perf_prepare_sample(&header, data, event, regs);
4390 if (perf_output_begin(&handle, event, header.size, nmi, 1))
4393 perf_output_sample(&handle, &header, data, event);
4395 perf_output_end(&handle);
4405 struct perf_read_event {
4406 struct perf_event_header header;
4413 perf_event_read_event(struct perf_event *event,
4414 struct task_struct *task)
4416 struct perf_output_handle handle;
4417 struct perf_sample_data sample;
4418 struct perf_read_event read_event = {
4420 .type = PERF_RECORD_READ,
4422 .size = sizeof(read_event) + event->read_size,
4424 .pid = perf_event_pid(event, task),
4425 .tid = perf_event_tid(event, task),
4429 perf_event_header__init_id(&read_event.header, &sample, event);
4430 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
4434 perf_output_put(&handle, read_event);
4435 perf_output_read(&handle, event);
4436 perf_event__output_id_sample(event, &handle, &sample);
4438 perf_output_end(&handle);
4442 * task tracking -- fork/exit
4444 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4447 struct perf_task_event {
4448 struct task_struct *task;
4449 struct perf_event_context *task_ctx;
4452 struct perf_event_header header;
4462 static void perf_event_task_output(struct perf_event *event,
4463 struct perf_task_event *task_event)
4465 struct perf_output_handle handle;
4466 struct perf_sample_data sample;
4467 struct task_struct *task = task_event->task;
4468 int ret, size = task_event->event_id.header.size;
4470 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4472 ret = perf_output_begin(&handle, event,
4473 task_event->event_id.header.size, 0, 0);
4477 task_event->event_id.pid = perf_event_pid(event, task);
4478 task_event->event_id.ppid = perf_event_pid(event, current);
4480 task_event->event_id.tid = perf_event_tid(event, task);
4481 task_event->event_id.ptid = perf_event_tid(event, current);
4483 perf_output_put(&handle, task_event->event_id);
4485 perf_event__output_id_sample(event, &handle, &sample);
4487 perf_output_end(&handle);
4489 task_event->event_id.header.size = size;
4492 static int perf_event_task_match(struct perf_event *event)
4494 if (event->state < PERF_EVENT_STATE_INACTIVE)
4497 if (!event_filter_match(event))
4500 if (event->attr.comm || event->attr.mmap ||
4501 event->attr.mmap_data || event->attr.task)
4507 static void perf_event_task_ctx(struct perf_event_context *ctx,
4508 struct perf_task_event *task_event)
4510 struct perf_event *event;
4512 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4513 if (perf_event_task_match(event))
4514 perf_event_task_output(event, task_event);
4518 static void perf_event_task_event(struct perf_task_event *task_event)
4520 struct perf_cpu_context *cpuctx;
4521 struct perf_event_context *ctx;
4526 list_for_each_entry_rcu(pmu, &pmus, entry) {
4527 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4528 if (cpuctx->active_pmu != pmu)
4530 perf_event_task_ctx(&cpuctx->ctx, task_event);
4532 ctx = task_event->task_ctx;
4534 ctxn = pmu->task_ctx_nr;
4537 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4540 perf_event_task_ctx(ctx, task_event);
4542 put_cpu_ptr(pmu->pmu_cpu_context);
4547 static void perf_event_task(struct task_struct *task,
4548 struct perf_event_context *task_ctx,
4551 struct perf_task_event task_event;
4553 if (!atomic_read(&nr_comm_events) &&
4554 !atomic_read(&nr_mmap_events) &&
4555 !atomic_read(&nr_task_events))
4558 task_event = (struct perf_task_event){
4560 .task_ctx = task_ctx,
4563 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4565 .size = sizeof(task_event.event_id),
4571 .time = perf_clock(),
4575 perf_event_task_event(&task_event);
4578 void perf_event_fork(struct task_struct *task)
4580 perf_event_task(task, NULL, 1);
4587 struct perf_comm_event {
4588 struct task_struct *task;
4593 struct perf_event_header header;
4600 static void perf_event_comm_output(struct perf_event *event,
4601 struct perf_comm_event *comm_event)
4603 struct perf_output_handle handle;
4604 struct perf_sample_data sample;
4605 int size = comm_event->event_id.header.size;
4608 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4609 ret = perf_output_begin(&handle, event,
4610 comm_event->event_id.header.size, 0, 0);
4615 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4616 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4618 perf_output_put(&handle, comm_event->event_id);
4619 perf_output_copy(&handle, comm_event->comm,
4620 comm_event->comm_size);
4622 perf_event__output_id_sample(event, &handle, &sample);
4624 perf_output_end(&handle);
4626 comm_event->event_id.header.size = size;
4629 static int perf_event_comm_match(struct perf_event *event)
4631 if (event->state < PERF_EVENT_STATE_INACTIVE)
4634 if (!event_filter_match(event))
4637 if (event->attr.comm)
4643 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4644 struct perf_comm_event *comm_event)
4646 struct perf_event *event;
4648 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4649 if (perf_event_comm_match(event))
4650 perf_event_comm_output(event, comm_event);
4654 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4656 struct perf_cpu_context *cpuctx;
4657 struct perf_event_context *ctx;
4658 char comm[TASK_COMM_LEN];
4663 memset(comm, 0, sizeof(comm));
4664 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4665 size = ALIGN(strlen(comm)+1, sizeof(u64));
4667 comm_event->comm = comm;
4668 comm_event->comm_size = size;
4670 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4672 list_for_each_entry_rcu(pmu, &pmus, entry) {
4673 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4674 if (cpuctx->active_pmu != pmu)
4676 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4678 ctxn = pmu->task_ctx_nr;
4682 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4684 perf_event_comm_ctx(ctx, comm_event);
4686 put_cpu_ptr(pmu->pmu_cpu_context);
4691 void perf_event_comm(struct task_struct *task)
4693 struct perf_comm_event comm_event;
4694 struct perf_event_context *ctx;
4697 for_each_task_context_nr(ctxn) {
4698 ctx = task->perf_event_ctxp[ctxn];
4702 perf_event_enable_on_exec(ctx);
4705 if (!atomic_read(&nr_comm_events))
4708 comm_event = (struct perf_comm_event){
4714 .type = PERF_RECORD_COMM,
4723 perf_event_comm_event(&comm_event);
4730 struct perf_mmap_event {
4731 struct vm_area_struct *vma;
4733 const char *file_name;
4737 struct perf_event_header header;
4747 static void perf_event_mmap_output(struct perf_event *event,
4748 struct perf_mmap_event *mmap_event)
4750 struct perf_output_handle handle;
4751 struct perf_sample_data sample;
4752 int size = mmap_event->event_id.header.size;
4755 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4756 ret = perf_output_begin(&handle, event,
4757 mmap_event->event_id.header.size, 0, 0);
4761 mmap_event->event_id.pid = perf_event_pid(event, current);
4762 mmap_event->event_id.tid = perf_event_tid(event, current);
4764 perf_output_put(&handle, mmap_event->event_id);
4765 perf_output_copy(&handle, mmap_event->file_name,
4766 mmap_event->file_size);
4768 perf_event__output_id_sample(event, &handle, &sample);
4770 perf_output_end(&handle);
4772 mmap_event->event_id.header.size = size;
4775 static int perf_event_mmap_match(struct perf_event *event,
4776 struct perf_mmap_event *mmap_event,
4779 if (event->state < PERF_EVENT_STATE_INACTIVE)
4782 if (!event_filter_match(event))
4785 if ((!executable && event->attr.mmap_data) ||
4786 (executable && event->attr.mmap))
4792 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4793 struct perf_mmap_event *mmap_event,
4796 struct perf_event *event;
4798 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4799 if (perf_event_mmap_match(event, mmap_event, executable))
4800 perf_event_mmap_output(event, mmap_event);
4804 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4806 struct perf_cpu_context *cpuctx;
4807 struct perf_event_context *ctx;
4808 struct vm_area_struct *vma = mmap_event->vma;
4809 struct file *file = vma->vm_file;
4817 memset(tmp, 0, sizeof(tmp));
4821 * d_path works from the end of the buffer backwards, so we
4822 * need to add enough zero bytes after the string to handle
4823 * the 64bit alignment we do later.
4825 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4827 name = strncpy(tmp, "//enomem", sizeof(tmp));
4830 name = d_path(&file->f_path, buf, PATH_MAX);
4832 name = strncpy(tmp, "//toolong", sizeof(tmp));
4836 if (arch_vma_name(mmap_event->vma)) {
4837 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4843 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4845 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4846 vma->vm_end >= vma->vm_mm->brk) {
4847 name = strncpy(tmp, "[heap]", sizeof(tmp));
4849 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4850 vma->vm_end >= vma->vm_mm->start_stack) {
4851 name = strncpy(tmp, "[stack]", sizeof(tmp));
4855 name = strncpy(tmp, "//anon", sizeof(tmp));
4860 size = ALIGN(strlen(name)+1, sizeof(u64));
4862 mmap_event->file_name = name;
4863 mmap_event->file_size = size;
4865 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4868 list_for_each_entry_rcu(pmu, &pmus, entry) {
4869 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4870 if (cpuctx->active_pmu != pmu)
4872 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4873 vma->vm_flags & VM_EXEC);
4875 ctxn = pmu->task_ctx_nr;
4879 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4881 perf_event_mmap_ctx(ctx, mmap_event,
4882 vma->vm_flags & VM_EXEC);
4885 put_cpu_ptr(pmu->pmu_cpu_context);
4892 void perf_event_mmap(struct vm_area_struct *vma)
4894 struct perf_mmap_event mmap_event;
4896 if (!atomic_read(&nr_mmap_events))
4899 mmap_event = (struct perf_mmap_event){
4905 .type = PERF_RECORD_MMAP,
4906 .misc = PERF_RECORD_MISC_USER,
4911 .start = vma->vm_start,
4912 .len = vma->vm_end - vma->vm_start,
4913 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4917 perf_event_mmap_event(&mmap_event);
4921 * IRQ throttle logging
4924 static void perf_log_throttle(struct perf_event *event, int enable)
4926 struct perf_output_handle handle;
4927 struct perf_sample_data sample;
4931 struct perf_event_header header;
4935 } throttle_event = {
4937 .type = PERF_RECORD_THROTTLE,
4939 .size = sizeof(throttle_event),
4941 .time = perf_clock(),
4942 .id = primary_event_id(event),
4943 .stream_id = event->id,
4947 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4949 perf_event_header__init_id(&throttle_event.header, &sample, event);
4951 ret = perf_output_begin(&handle, event,
4952 throttle_event.header.size, 1, 0);
4956 perf_output_put(&handle, throttle_event);
4957 perf_event__output_id_sample(event, &handle, &sample);
4958 perf_output_end(&handle);
4962 * Generic event overflow handling, sampling.
4965 static int __perf_event_overflow(struct perf_event *event, int nmi,
4966 int throttle, struct perf_sample_data *data,
4967 struct pt_regs *regs)
4969 int events = atomic_read(&event->event_limit);
4970 struct hw_perf_event *hwc = &event->hw;
4974 * Non-sampling counters might still use the PMI to fold short
4975 * hardware counters, ignore those.
4977 if (unlikely(!is_sampling_event(event)))
4980 if (unlikely(hwc->interrupts >= max_samples_per_tick)) {
4982 hwc->interrupts = MAX_INTERRUPTS;
4983 perf_log_throttle(event, 0);
4989 if (event->attr.freq) {
4990 u64 now = perf_clock();
4991 s64 delta = now - hwc->freq_time_stamp;
4993 hwc->freq_time_stamp = now;
4995 if (delta > 0 && delta < 2*TICK_NSEC)
4996 perf_adjust_period(event, delta, hwc->last_period);
5000 * XXX event_limit might not quite work as expected on inherited
5004 event->pending_kill = POLL_IN;
5005 if (events && atomic_dec_and_test(&event->event_limit)) {
5007 event->pending_kill = POLL_HUP;
5009 event->pending_disable = 1;
5010 irq_work_queue(&event->pending);
5012 perf_event_disable(event);
5015 if (event->overflow_handler)
5016 event->overflow_handler(event, nmi, data, regs);
5018 perf_event_output(event, nmi, data, regs);
5023 int perf_event_overflow(struct perf_event *event, int nmi,
5024 struct perf_sample_data *data,
5025 struct pt_regs *regs)
5027 return __perf_event_overflow(event, nmi, 1, data, regs);
5031 * Generic software event infrastructure
5034 struct swevent_htable {
5035 struct swevent_hlist *swevent_hlist;
5036 struct mutex hlist_mutex;
5039 /* Recursion avoidance in each contexts */
5040 int recursion[PERF_NR_CONTEXTS];
5043 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5046 * We directly increment event->count and keep a second value in
5047 * event->hw.period_left to count intervals. This period event
5048 * is kept in the range [-sample_period, 0] so that we can use the
5052 static u64 perf_swevent_set_period(struct perf_event *event)
5054 struct hw_perf_event *hwc = &event->hw;
5055 u64 period = hwc->last_period;
5059 hwc->last_period = hwc->sample_period;
5062 old = val = local64_read(&hwc->period_left);
5066 nr = div64_u64(period + val, period);
5067 offset = nr * period;
5069 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
5075 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
5076 int nmi, struct perf_sample_data *data,
5077 struct pt_regs *regs)
5079 struct hw_perf_event *hwc = &event->hw;
5082 data->period = event->hw.last_period;
5084 overflow = perf_swevent_set_period(event);
5086 if (hwc->interrupts == MAX_INTERRUPTS)
5089 for (; overflow; overflow--) {
5090 if (__perf_event_overflow(event, nmi, throttle,
5093 * We inhibit the overflow from happening when
5094 * hwc->interrupts == MAX_INTERRUPTS.
5102 static void perf_swevent_event(struct perf_event *event, u64 nr,
5103 int nmi, struct perf_sample_data *data,
5104 struct pt_regs *regs)
5106 struct hw_perf_event *hwc = &event->hw;
5108 local64_add(nr, &event->count);
5113 if (!is_sampling_event(event))
5116 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
5117 return perf_swevent_overflow(event, 1, nmi, data, regs);
5119 if (local64_add_negative(nr, &hwc->period_left))
5122 perf_swevent_overflow(event, 0, nmi, data, regs);
5125 static int perf_exclude_event(struct perf_event *event,
5126 struct pt_regs *regs)
5128 if (event->hw.state & PERF_HES_STOPPED)
5132 if (event->attr.exclude_user && user_mode(regs))
5135 if (event->attr.exclude_kernel && !user_mode(regs))
5142 static int perf_swevent_match(struct perf_event *event,
5143 enum perf_type_id type,
5145 struct perf_sample_data *data,
5146 struct pt_regs *regs)
5148 if (event->attr.type != type)
5151 if (event->attr.config != event_id)
5154 if (perf_exclude_event(event, regs))
5160 static inline u64 swevent_hash(u64 type, u32 event_id)
5162 u64 val = event_id | (type << 32);
5164 return hash_64(val, SWEVENT_HLIST_BITS);
5167 static inline struct hlist_head *
5168 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
5170 u64 hash = swevent_hash(type, event_id);
5172 return &hlist->heads[hash];
5175 /* For the read side: events when they trigger */
5176 static inline struct hlist_head *
5177 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
5179 struct swevent_hlist *hlist;
5181 hlist = rcu_dereference(swhash->swevent_hlist);
5185 return __find_swevent_head(hlist, type, event_id);
5188 /* For the event head insertion and removal in the hlist */
5189 static inline struct hlist_head *
5190 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
5192 struct swevent_hlist *hlist;
5193 u32 event_id = event->attr.config;
5194 u64 type = event->attr.type;
5197 * Event scheduling is always serialized against hlist allocation
5198 * and release. Which makes the protected version suitable here.
5199 * The context lock guarantees that.
5201 hlist = rcu_dereference_protected(swhash->swevent_hlist,
5202 lockdep_is_held(&event->ctx->lock));
5206 return __find_swevent_head(hlist, type, event_id);
5209 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5211 struct perf_sample_data *data,
5212 struct pt_regs *regs)
5214 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5215 struct perf_event *event;
5216 struct hlist_node *node;
5217 struct hlist_head *head;
5220 head = find_swevent_head_rcu(swhash, type, event_id);
5224 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5225 if (perf_swevent_match(event, type, event_id, data, regs))
5226 perf_swevent_event(event, nr, nmi, data, regs);
5232 int perf_swevent_get_recursion_context(void)
5234 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5236 return get_recursion_context(swhash->recursion);
5238 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
5240 inline void perf_swevent_put_recursion_context(int rctx)
5242 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5244 put_recursion_context(swhash->recursion, rctx);
5247 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
5248 struct pt_regs *regs, u64 addr)
5250 struct perf_sample_data data;
5253 preempt_disable_notrace();
5254 rctx = perf_swevent_get_recursion_context();
5258 perf_sample_data_init(&data, addr);
5260 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
5262 perf_swevent_put_recursion_context(rctx);
5263 preempt_enable_notrace();
5266 static void perf_swevent_read(struct perf_event *event)
5270 static int perf_swevent_add(struct perf_event *event, int flags)
5272 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5273 struct hw_perf_event *hwc = &event->hw;
5274 struct hlist_head *head;
5276 if (is_sampling_event(event)) {
5277 hwc->last_period = hwc->sample_period;
5278 perf_swevent_set_period(event);
5281 hwc->state = !(flags & PERF_EF_START);
5283 head = find_swevent_head(swhash, event);
5284 if (WARN_ON_ONCE(!head))
5287 hlist_add_head_rcu(&event->hlist_entry, head);
5292 static void perf_swevent_del(struct perf_event *event, int flags)
5294 hlist_del_rcu(&event->hlist_entry);
5297 static void perf_swevent_start(struct perf_event *event, int flags)
5299 event->hw.state = 0;
5302 static void perf_swevent_stop(struct perf_event *event, int flags)
5304 event->hw.state = PERF_HES_STOPPED;
5307 /* Deref the hlist from the update side */
5308 static inline struct swevent_hlist *
5309 swevent_hlist_deref(struct swevent_htable *swhash)
5311 return rcu_dereference_protected(swhash->swevent_hlist,
5312 lockdep_is_held(&swhash->hlist_mutex));
5315 static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
5317 struct swevent_hlist *hlist;
5319 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
5323 static void swevent_hlist_release(struct swevent_htable *swhash)
5325 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
5330 rcu_assign_pointer(swhash->swevent_hlist, NULL);
5331 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
5334 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5336 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5338 mutex_lock(&swhash->hlist_mutex);
5340 if (!--swhash->hlist_refcount)
5341 swevent_hlist_release(swhash);
5343 mutex_unlock(&swhash->hlist_mutex);
5346 static void swevent_hlist_put(struct perf_event *event)
5350 if (event->cpu != -1) {
5351 swevent_hlist_put_cpu(event, event->cpu);
5355 for_each_possible_cpu(cpu)
5356 swevent_hlist_put_cpu(event, cpu);
5359 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5361 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5364 mutex_lock(&swhash->hlist_mutex);
5366 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
5367 struct swevent_hlist *hlist;
5369 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5374 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5376 swhash->hlist_refcount++;
5378 mutex_unlock(&swhash->hlist_mutex);
5383 static int swevent_hlist_get(struct perf_event *event)
5386 int cpu, failed_cpu;
5388 if (event->cpu != -1)
5389 return swevent_hlist_get_cpu(event, event->cpu);
5392 for_each_possible_cpu(cpu) {
5393 err = swevent_hlist_get_cpu(event, cpu);
5403 for_each_possible_cpu(cpu) {
5404 if (cpu == failed_cpu)
5406 swevent_hlist_put_cpu(event, cpu);
5413 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
5415 static void sw_perf_event_destroy(struct perf_event *event)
5417 u64 event_id = event->attr.config;
5419 WARN_ON(event->parent);
5421 jump_label_dec(&perf_swevent_enabled[event_id]);
5422 swevent_hlist_put(event);
5425 static int perf_swevent_init(struct perf_event *event)
5427 int event_id = event->attr.config;
5429 if (event->attr.type != PERF_TYPE_SOFTWARE)
5433 case PERF_COUNT_SW_CPU_CLOCK:
5434 case PERF_COUNT_SW_TASK_CLOCK:
5441 if (event_id >= PERF_COUNT_SW_MAX)
5444 if (!event->parent) {
5447 err = swevent_hlist_get(event);
5451 jump_label_inc(&perf_swevent_enabled[event_id]);
5452 event->destroy = sw_perf_event_destroy;
5458 static struct pmu perf_swevent = {
5459 .task_ctx_nr = perf_sw_context,
5461 .event_init = perf_swevent_init,
5462 .add = perf_swevent_add,
5463 .del = perf_swevent_del,
5464 .start = perf_swevent_start,
5465 .stop = perf_swevent_stop,
5466 .read = perf_swevent_read,
5469 #ifdef CONFIG_EVENT_TRACING
5471 static int perf_tp_filter_match(struct perf_event *event,
5472 struct perf_sample_data *data)
5474 void *record = data->raw->data;
5476 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5481 static int perf_tp_event_match(struct perf_event *event,
5482 struct perf_sample_data *data,
5483 struct pt_regs *regs)
5486 * All tracepoints are from kernel-space.
5488 if (event->attr.exclude_kernel)
5491 if (!perf_tp_filter_match(event, data))
5497 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5498 struct pt_regs *regs, struct hlist_head *head, int rctx)
5500 struct perf_sample_data data;
5501 struct perf_event *event;
5502 struct hlist_node *node;
5504 struct perf_raw_record raw = {
5509 perf_sample_data_init(&data, addr);
5512 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5513 if (perf_tp_event_match(event, &data, regs))
5514 perf_swevent_event(event, count, 1, &data, regs);
5517 perf_swevent_put_recursion_context(rctx);
5519 EXPORT_SYMBOL_GPL(perf_tp_event);
5521 static void tp_perf_event_destroy(struct perf_event *event)
5523 perf_trace_destroy(event);
5526 static int perf_tp_event_init(struct perf_event *event)
5530 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5533 err = perf_trace_init(event);
5537 event->destroy = tp_perf_event_destroy;
5542 static struct pmu perf_tracepoint = {
5543 .task_ctx_nr = perf_sw_context,
5545 .event_init = perf_tp_event_init,
5546 .add = perf_trace_add,
5547 .del = perf_trace_del,
5548 .start = perf_swevent_start,
5549 .stop = perf_swevent_stop,
5550 .read = perf_swevent_read,
5553 static inline void perf_tp_register(void)
5555 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5558 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5563 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5566 filter_str = strndup_user(arg, PAGE_SIZE);
5567 if (IS_ERR(filter_str))
5568 return PTR_ERR(filter_str);
5570 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5576 static void perf_event_free_filter(struct perf_event *event)
5578 ftrace_profile_free_filter(event);
5583 static inline void perf_tp_register(void)
5587 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5592 static void perf_event_free_filter(struct perf_event *event)
5596 #endif /* CONFIG_EVENT_TRACING */
5598 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5599 void perf_bp_event(struct perf_event *bp, void *data)
5601 struct perf_sample_data sample;
5602 struct pt_regs *regs = data;
5604 perf_sample_data_init(&sample, bp->attr.bp_addr);
5606 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5607 perf_swevent_event(bp, 1, 1, &sample, regs);
5612 * hrtimer based swevent callback
5615 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5617 enum hrtimer_restart ret = HRTIMER_RESTART;
5618 struct perf_sample_data data;
5619 struct pt_regs *regs;
5620 struct perf_event *event;
5623 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5625 if (event->state != PERF_EVENT_STATE_ACTIVE)
5626 return HRTIMER_NORESTART;
5628 event->pmu->read(event);
5630 perf_sample_data_init(&data, 0);
5631 data.period = event->hw.last_period;
5632 regs = get_irq_regs();
5634 if (regs && !perf_exclude_event(event, regs)) {
5635 if (!(event->attr.exclude_idle && current->pid == 0))
5636 if (perf_event_overflow(event, 0, &data, regs))
5637 ret = HRTIMER_NORESTART;
5640 period = max_t(u64, 10000, event->hw.sample_period);
5641 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5646 static void perf_swevent_start_hrtimer(struct perf_event *event)
5648 struct hw_perf_event *hwc = &event->hw;
5651 if (!is_sampling_event(event))
5654 period = local64_read(&hwc->period_left);
5659 local64_set(&hwc->period_left, 0);
5661 period = max_t(u64, 10000, hwc->sample_period);
5663 __hrtimer_start_range_ns(&hwc->hrtimer,
5664 ns_to_ktime(period), 0,
5665 HRTIMER_MODE_REL_PINNED, 0);
5668 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5670 struct hw_perf_event *hwc = &event->hw;
5672 if (is_sampling_event(event)) {
5673 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5674 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5676 hrtimer_cancel(&hwc->hrtimer);
5680 static void perf_swevent_init_hrtimer(struct perf_event *event)
5682 struct hw_perf_event *hwc = &event->hw;
5684 if (!is_sampling_event(event))
5687 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5688 hwc->hrtimer.function = perf_swevent_hrtimer;
5691 * Since hrtimers have a fixed rate, we can do a static freq->period
5692 * mapping and avoid the whole period adjust feedback stuff.
5694 if (event->attr.freq) {
5695 long freq = event->attr.sample_freq;
5697 event->attr.sample_period = NSEC_PER_SEC / freq;
5698 hwc->sample_period = event->attr.sample_period;
5699 local64_set(&hwc->period_left, hwc->sample_period);
5700 event->attr.freq = 0;
5705 * Software event: cpu wall time clock
5708 static void cpu_clock_event_update(struct perf_event *event)
5713 now = local_clock();
5714 prev = local64_xchg(&event->hw.prev_count, now);
5715 local64_add(now - prev, &event->count);
5718 static void cpu_clock_event_start(struct perf_event *event, int flags)
5720 local64_set(&event->hw.prev_count, local_clock());
5721 perf_swevent_start_hrtimer(event);
5724 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5726 perf_swevent_cancel_hrtimer(event);
5727 cpu_clock_event_update(event);
5730 static int cpu_clock_event_add(struct perf_event *event, int flags)
5732 if (flags & PERF_EF_START)
5733 cpu_clock_event_start(event, flags);
5738 static void cpu_clock_event_del(struct perf_event *event, int flags)
5740 cpu_clock_event_stop(event, flags);
5743 static void cpu_clock_event_read(struct perf_event *event)
5745 cpu_clock_event_update(event);
5748 static int cpu_clock_event_init(struct perf_event *event)
5750 if (event->attr.type != PERF_TYPE_SOFTWARE)
5753 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5756 perf_swevent_init_hrtimer(event);
5761 static struct pmu perf_cpu_clock = {
5762 .task_ctx_nr = perf_sw_context,
5764 .event_init = cpu_clock_event_init,
5765 .add = cpu_clock_event_add,
5766 .del = cpu_clock_event_del,
5767 .start = cpu_clock_event_start,
5768 .stop = cpu_clock_event_stop,
5769 .read = cpu_clock_event_read,
5773 * Software event: task time clock
5776 static void task_clock_event_update(struct perf_event *event, u64 now)
5781 prev = local64_xchg(&event->hw.prev_count, now);
5783 local64_add(delta, &event->count);
5786 static void task_clock_event_start(struct perf_event *event, int flags)
5788 local64_set(&event->hw.prev_count, event->ctx->time);
5789 perf_swevent_start_hrtimer(event);
5792 static void task_clock_event_stop(struct perf_event *event, int flags)
5794 perf_swevent_cancel_hrtimer(event);
5795 task_clock_event_update(event, event->ctx->time);
5798 static int task_clock_event_add(struct perf_event *event, int flags)
5800 if (flags & PERF_EF_START)
5801 task_clock_event_start(event, flags);
5806 static void task_clock_event_del(struct perf_event *event, int flags)
5808 task_clock_event_stop(event, PERF_EF_UPDATE);
5811 static void task_clock_event_read(struct perf_event *event)
5813 u64 now = perf_clock();
5814 u64 delta = now - event->ctx->timestamp;
5815 u64 time = event->ctx->time + delta;
5817 task_clock_event_update(event, time);
5820 static int task_clock_event_init(struct perf_event *event)
5822 if (event->attr.type != PERF_TYPE_SOFTWARE)
5825 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5828 perf_swevent_init_hrtimer(event);
5833 static struct pmu perf_task_clock = {
5834 .task_ctx_nr = perf_sw_context,
5836 .event_init = task_clock_event_init,
5837 .add = task_clock_event_add,
5838 .del = task_clock_event_del,
5839 .start = task_clock_event_start,
5840 .stop = task_clock_event_stop,
5841 .read = task_clock_event_read,
5844 static void perf_pmu_nop_void(struct pmu *pmu)
5848 static int perf_pmu_nop_int(struct pmu *pmu)
5853 static void perf_pmu_start_txn(struct pmu *pmu)
5855 perf_pmu_disable(pmu);
5858 static int perf_pmu_commit_txn(struct pmu *pmu)
5860 perf_pmu_enable(pmu);
5864 static void perf_pmu_cancel_txn(struct pmu *pmu)
5866 perf_pmu_enable(pmu);
5870 * Ensures all contexts with the same task_ctx_nr have the same
5871 * pmu_cpu_context too.
5873 static void *find_pmu_context(int ctxn)
5880 list_for_each_entry(pmu, &pmus, entry) {
5881 if (pmu->task_ctx_nr == ctxn)
5882 return pmu->pmu_cpu_context;
5888 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5892 for_each_possible_cpu(cpu) {
5893 struct perf_cpu_context *cpuctx;
5895 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5897 if (cpuctx->active_pmu == old_pmu)
5898 cpuctx->active_pmu = pmu;
5902 static void free_pmu_context(struct pmu *pmu)
5906 mutex_lock(&pmus_lock);
5908 * Like a real lame refcount.
5910 list_for_each_entry(i, &pmus, entry) {
5911 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5912 update_pmu_context(i, pmu);
5917 free_percpu(pmu->pmu_cpu_context);
5919 mutex_unlock(&pmus_lock);
5921 static struct idr pmu_idr;
5924 type_show(struct device *dev, struct device_attribute *attr, char *page)
5926 struct pmu *pmu = dev_get_drvdata(dev);
5928 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5931 static struct device_attribute pmu_dev_attrs[] = {
5936 static int pmu_bus_running;
5937 static struct bus_type pmu_bus = {
5938 .name = "event_source",
5939 .dev_attrs = pmu_dev_attrs,
5942 static void pmu_dev_release(struct device *dev)
5947 static int pmu_dev_alloc(struct pmu *pmu)
5951 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5955 device_initialize(pmu->dev);
5956 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5960 dev_set_drvdata(pmu->dev, pmu);
5961 pmu->dev->bus = &pmu_bus;
5962 pmu->dev->release = pmu_dev_release;
5963 ret = device_add(pmu->dev);
5971 put_device(pmu->dev);
5975 static struct lock_class_key cpuctx_mutex;
5977 int perf_pmu_register(struct pmu *pmu, char *name, int type)
5981 mutex_lock(&pmus_lock);
5983 pmu->pmu_disable_count = alloc_percpu(int);
5984 if (!pmu->pmu_disable_count)
5993 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5997 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
6005 if (pmu_bus_running) {
6006 ret = pmu_dev_alloc(pmu);
6012 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6013 if (pmu->pmu_cpu_context)
6014 goto got_cpu_context;
6016 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6017 if (!pmu->pmu_cpu_context)
6020 for_each_possible_cpu(cpu) {
6021 struct perf_cpu_context *cpuctx;
6023 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6024 __perf_event_init_context(&cpuctx->ctx);
6025 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
6026 cpuctx->ctx.type = cpu_context;
6027 cpuctx->ctx.pmu = pmu;
6028 cpuctx->jiffies_interval = 1;
6029 INIT_LIST_HEAD(&cpuctx->rotation_list);
6030 cpuctx->active_pmu = pmu;
6034 if (!pmu->start_txn) {
6035 if (pmu->pmu_enable) {
6037 * If we have pmu_enable/pmu_disable calls, install
6038 * transaction stubs that use that to try and batch
6039 * hardware accesses.
6041 pmu->start_txn = perf_pmu_start_txn;
6042 pmu->commit_txn = perf_pmu_commit_txn;
6043 pmu->cancel_txn = perf_pmu_cancel_txn;
6045 pmu->start_txn = perf_pmu_nop_void;
6046 pmu->commit_txn = perf_pmu_nop_int;
6047 pmu->cancel_txn = perf_pmu_nop_void;
6051 if (!pmu->pmu_enable) {
6052 pmu->pmu_enable = perf_pmu_nop_void;
6053 pmu->pmu_disable = perf_pmu_nop_void;
6056 list_add_rcu(&pmu->entry, &pmus);
6059 mutex_unlock(&pmus_lock);
6064 device_del(pmu->dev);
6065 put_device(pmu->dev);
6068 if (pmu->type >= PERF_TYPE_MAX)
6069 idr_remove(&pmu_idr, pmu->type);
6072 free_percpu(pmu->pmu_disable_count);
6076 void perf_pmu_unregister(struct pmu *pmu)
6078 mutex_lock(&pmus_lock);
6079 list_del_rcu(&pmu->entry);
6080 mutex_unlock(&pmus_lock);
6083 * We dereference the pmu list under both SRCU and regular RCU, so
6084 * synchronize against both of those.
6086 synchronize_srcu(&pmus_srcu);
6089 free_percpu(pmu->pmu_disable_count);
6090 if (pmu->type >= PERF_TYPE_MAX)
6091 idr_remove(&pmu_idr, pmu->type);
6092 device_del(pmu->dev);
6093 put_device(pmu->dev);
6094 free_pmu_context(pmu);
6097 struct pmu *perf_init_event(struct perf_event *event)
6099 struct pmu *pmu = NULL;
6102 idx = srcu_read_lock(&pmus_srcu);
6105 pmu = idr_find(&pmu_idr, event->attr.type);
6110 list_for_each_entry_rcu(pmu, &pmus, entry) {
6111 int ret = pmu->event_init(event);
6115 if (ret != -ENOENT) {
6120 pmu = ERR_PTR(-ENOENT);
6122 srcu_read_unlock(&pmus_srcu, idx);
6128 * Allocate and initialize a event structure
6130 static struct perf_event *
6131 perf_event_alloc(struct perf_event_attr *attr, int cpu,
6132 struct task_struct *task,
6133 struct perf_event *group_leader,
6134 struct perf_event *parent_event,
6135 perf_overflow_handler_t overflow_handler)
6138 struct perf_event *event;
6139 struct hw_perf_event *hwc;
6142 if ((unsigned)cpu >= nr_cpu_ids) {
6143 if (!task || cpu != -1)
6144 return ERR_PTR(-EINVAL);
6147 event = kzalloc(sizeof(*event), GFP_KERNEL);
6149 return ERR_PTR(-ENOMEM);
6152 * Single events are their own group leaders, with an
6153 * empty sibling list:
6156 group_leader = event;
6158 mutex_init(&event->child_mutex);
6159 INIT_LIST_HEAD(&event->child_list);
6161 INIT_LIST_HEAD(&event->group_entry);
6162 INIT_LIST_HEAD(&event->event_entry);
6163 INIT_LIST_HEAD(&event->sibling_list);
6164 init_waitqueue_head(&event->waitq);
6165 init_irq_work(&event->pending, perf_pending_event);
6167 mutex_init(&event->mmap_mutex);
6170 event->attr = *attr;
6171 event->group_leader = group_leader;
6175 event->parent = parent_event;
6177 event->ns = get_pid_ns(current->nsproxy->pid_ns);
6178 event->id = atomic64_inc_return(&perf_event_id);
6180 event->state = PERF_EVENT_STATE_INACTIVE;
6183 event->attach_state = PERF_ATTACH_TASK;
6184 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6186 * hw_breakpoint is a bit difficult here..
6188 if (attr->type == PERF_TYPE_BREAKPOINT)
6189 event->hw.bp_target = task;
6193 if (!overflow_handler && parent_event)
6194 overflow_handler = parent_event->overflow_handler;
6196 event->overflow_handler = overflow_handler;
6199 event->state = PERF_EVENT_STATE_OFF;
6204 hwc->sample_period = attr->sample_period;
6205 if (attr->freq && attr->sample_freq)
6206 hwc->sample_period = 1;
6207 hwc->last_period = hwc->sample_period;
6209 local64_set(&hwc->period_left, hwc->sample_period);
6212 * we currently do not support PERF_FORMAT_GROUP on inherited events
6214 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6217 pmu = perf_init_event(event);
6223 else if (IS_ERR(pmu))
6228 put_pid_ns(event->ns);
6230 return ERR_PTR(err);
6235 if (!event->parent) {
6236 if (event->attach_state & PERF_ATTACH_TASK)
6237 jump_label_inc(&perf_sched_events);
6238 if (event->attr.mmap || event->attr.mmap_data)
6239 atomic_inc(&nr_mmap_events);
6240 if (event->attr.comm)
6241 atomic_inc(&nr_comm_events);
6242 if (event->attr.task)
6243 atomic_inc(&nr_task_events);
6244 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6245 err = get_callchain_buffers();
6248 return ERR_PTR(err);
6256 static int perf_copy_attr(struct perf_event_attr __user *uattr,
6257 struct perf_event_attr *attr)
6262 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6266 * zero the full structure, so that a short copy will be nice.
6268 memset(attr, 0, sizeof(*attr));
6270 ret = get_user(size, &uattr->size);
6274 if (size > PAGE_SIZE) /* silly large */
6277 if (!size) /* abi compat */
6278 size = PERF_ATTR_SIZE_VER0;
6280 if (size < PERF_ATTR_SIZE_VER0)
6284 * If we're handed a bigger struct than we know of,
6285 * ensure all the unknown bits are 0 - i.e. new
6286 * user-space does not rely on any kernel feature
6287 * extensions we dont know about yet.
6289 if (size > sizeof(*attr)) {
6290 unsigned char __user *addr;
6291 unsigned char __user *end;
6294 addr = (void __user *)uattr + sizeof(*attr);
6295 end = (void __user *)uattr + size;
6297 for (; addr < end; addr++) {
6298 ret = get_user(val, addr);
6304 size = sizeof(*attr);
6307 ret = copy_from_user(attr, uattr, size);
6312 * If the type exists, the corresponding creation will verify
6315 if (attr->type >= PERF_TYPE_MAX)
6318 if (attr->__reserved_1)
6321 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6324 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6331 put_user(sizeof(*attr), &uattr->size);
6337 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6339 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
6345 /* don't allow circular references */
6346 if (event == output_event)
6350 * Don't allow cross-cpu buffers
6352 if (output_event->cpu != event->cpu)
6356 * If its not a per-cpu buffer, it must be the same task.
6358 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6362 mutex_lock(&event->mmap_mutex);
6363 /* Can't redirect output if we've got an active mmap() */
6364 if (atomic_read(&event->mmap_count))
6368 /* get the buffer we want to redirect to */
6369 buffer = perf_buffer_get(output_event);
6374 old_buffer = event->buffer;
6375 rcu_assign_pointer(event->buffer, buffer);
6378 mutex_unlock(&event->mmap_mutex);
6381 perf_buffer_put(old_buffer);
6387 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6389 * @attr_uptr: event_id type attributes for monitoring/sampling
6392 * @group_fd: group leader event fd
6394 SYSCALL_DEFINE5(perf_event_open,
6395 struct perf_event_attr __user *, attr_uptr,
6396 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6398 struct perf_event *group_leader = NULL, *output_event = NULL;
6399 struct perf_event *event, *sibling;
6400 struct perf_event_attr attr;
6401 struct perf_event_context *ctx;
6402 struct file *event_file = NULL;
6403 struct file *group_file = NULL;
6404 struct task_struct *task = NULL;
6408 int fput_needed = 0;
6411 /* for future expandability... */
6412 if (flags & ~PERF_FLAG_ALL)
6415 err = perf_copy_attr(attr_uptr, &attr);
6419 if (!attr.exclude_kernel) {
6420 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6425 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6430 * In cgroup mode, the pid argument is used to pass the fd
6431 * opened to the cgroup directory in cgroupfs. The cpu argument
6432 * designates the cpu on which to monitor threads from that
6435 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6438 event_fd = get_unused_fd_flags(O_RDWR);
6442 if (group_fd != -1) {
6443 group_leader = perf_fget_light(group_fd, &fput_needed);
6444 if (IS_ERR(group_leader)) {
6445 err = PTR_ERR(group_leader);
6448 group_file = group_leader->filp;
6449 if (flags & PERF_FLAG_FD_OUTPUT)
6450 output_event = group_leader;
6451 if (flags & PERF_FLAG_FD_NO_GROUP)
6452 group_leader = NULL;
6455 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
6456 task = find_lively_task_by_vpid(pid);
6458 err = PTR_ERR(task);
6463 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
6464 if (IS_ERR(event)) {
6465 err = PTR_ERR(event);
6469 if (flags & PERF_FLAG_PID_CGROUP) {
6470 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6476 * Special case software events and allow them to be part of
6477 * any hardware group.
6482 (is_software_event(event) != is_software_event(group_leader))) {
6483 if (is_software_event(event)) {
6485 * If event and group_leader are not both a software
6486 * event, and event is, then group leader is not.
6488 * Allow the addition of software events to !software
6489 * groups, this is safe because software events never
6492 pmu = group_leader->pmu;
6493 } else if (is_software_event(group_leader) &&
6494 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6496 * In case the group is a pure software group, and we
6497 * try to add a hardware event, move the whole group to
6498 * the hardware context.
6505 * Get the target context (task or percpu):
6507 ctx = find_get_context(pmu, task, cpu);
6514 * Look up the group leader (we will attach this event to it):
6520 * Do not allow a recursive hierarchy (this new sibling
6521 * becoming part of another group-sibling):
6523 if (group_leader->group_leader != group_leader)
6526 * Do not allow to attach to a group in a different
6527 * task or CPU context:
6530 if (group_leader->ctx->type != ctx->type)
6533 if (group_leader->ctx != ctx)
6538 * Only a group leader can be exclusive or pinned
6540 if (attr.exclusive || attr.pinned)
6545 err = perf_event_set_output(event, output_event);
6550 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6551 if (IS_ERR(event_file)) {
6552 err = PTR_ERR(event_file);
6557 struct perf_event_context *gctx = group_leader->ctx;
6559 mutex_lock(&gctx->mutex);
6560 perf_remove_from_context(group_leader);
6561 list_for_each_entry(sibling, &group_leader->sibling_list,
6563 perf_remove_from_context(sibling);
6566 mutex_unlock(&gctx->mutex);
6570 event->filp = event_file;
6571 WARN_ON_ONCE(ctx->parent_ctx);
6572 mutex_lock(&ctx->mutex);
6575 perf_install_in_context(ctx, group_leader, cpu);
6577 list_for_each_entry(sibling, &group_leader->sibling_list,
6579 perf_install_in_context(ctx, sibling, cpu);
6584 perf_install_in_context(ctx, event, cpu);
6586 perf_unpin_context(ctx);
6587 mutex_unlock(&ctx->mutex);
6589 event->owner = current;
6591 mutex_lock(¤t->perf_event_mutex);
6592 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6593 mutex_unlock(¤t->perf_event_mutex);
6596 * Precalculate sample_data sizes
6598 perf_event__header_size(event);
6599 perf_event__id_header_size(event);
6602 * Drop the reference on the group_event after placing the
6603 * new event on the sibling_list. This ensures destruction
6604 * of the group leader will find the pointer to itself in
6605 * perf_group_detach().
6607 fput_light(group_file, fput_needed);
6608 fd_install(event_fd, event_file);
6612 perf_unpin_context(ctx);
6618 put_task_struct(task);
6620 fput_light(group_file, fput_needed);
6622 put_unused_fd(event_fd);
6627 * perf_event_create_kernel_counter
6629 * @attr: attributes of the counter to create
6630 * @cpu: cpu in which the counter is bound
6631 * @task: task to profile (NULL for percpu)
6634 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6635 struct task_struct *task,
6636 perf_overflow_handler_t overflow_handler)
6638 struct perf_event_context *ctx;
6639 struct perf_event *event;
6643 * Get the target context (task or percpu):
6646 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
6647 if (IS_ERR(event)) {
6648 err = PTR_ERR(event);
6652 ctx = find_get_context(event->pmu, task, cpu);
6659 WARN_ON_ONCE(ctx->parent_ctx);
6660 mutex_lock(&ctx->mutex);
6661 perf_install_in_context(ctx, event, cpu);
6663 perf_unpin_context(ctx);
6664 mutex_unlock(&ctx->mutex);
6671 return ERR_PTR(err);
6673 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6675 static void sync_child_event(struct perf_event *child_event,
6676 struct task_struct *child)
6678 struct perf_event *parent_event = child_event->parent;
6681 if (child_event->attr.inherit_stat)
6682 perf_event_read_event(child_event, child);
6684 child_val = perf_event_count(child_event);
6687 * Add back the child's count to the parent's count:
6689 atomic64_add(child_val, &parent_event->child_count);
6690 atomic64_add(child_event->total_time_enabled,
6691 &parent_event->child_total_time_enabled);
6692 atomic64_add(child_event->total_time_running,
6693 &parent_event->child_total_time_running);
6696 * Remove this event from the parent's list
6698 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6699 mutex_lock(&parent_event->child_mutex);
6700 list_del_init(&child_event->child_list);
6701 mutex_unlock(&parent_event->child_mutex);
6704 * Release the parent event, if this was the last
6707 fput(parent_event->filp);
6711 __perf_event_exit_task(struct perf_event *child_event,
6712 struct perf_event_context *child_ctx,
6713 struct task_struct *child)
6715 struct perf_event *parent_event;
6717 perf_remove_from_context(child_event);
6719 parent_event = child_event->parent;
6721 * It can happen that parent exits first, and has events
6722 * that are still around due to the child reference. These
6723 * events need to be zapped - but otherwise linger.
6726 sync_child_event(child_event, child);
6727 free_event(child_event);
6731 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6733 struct perf_event *child_event, *tmp;
6734 struct perf_event_context *child_ctx;
6735 unsigned long flags;
6737 if (likely(!child->perf_event_ctxp[ctxn])) {
6738 perf_event_task(child, NULL, 0);
6742 local_irq_save(flags);
6744 * We can't reschedule here because interrupts are disabled,
6745 * and either child is current or it is a task that can't be
6746 * scheduled, so we are now safe from rescheduling changing
6749 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6750 task_ctx_sched_out(child_ctx, EVENT_ALL);
6753 * Take the context lock here so that if find_get_context is
6754 * reading child->perf_event_ctxp, we wait until it has
6755 * incremented the context's refcount before we do put_ctx below.
6757 raw_spin_lock(&child_ctx->lock);
6758 child->perf_event_ctxp[ctxn] = NULL;
6760 * If this context is a clone; unclone it so it can't get
6761 * swapped to another process while we're removing all
6762 * the events from it.
6764 unclone_ctx(child_ctx);
6765 update_context_time(child_ctx);
6766 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6769 * Report the task dead after unscheduling the events so that we
6770 * won't get any samples after PERF_RECORD_EXIT. We can however still
6771 * get a few PERF_RECORD_READ events.
6773 perf_event_task(child, child_ctx, 0);
6776 * We can recurse on the same lock type through:
6778 * __perf_event_exit_task()
6779 * sync_child_event()
6780 * fput(parent_event->filp)
6782 * mutex_lock(&ctx->mutex)
6784 * But since its the parent context it won't be the same instance.
6786 mutex_lock(&child_ctx->mutex);
6789 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6791 __perf_event_exit_task(child_event, child_ctx, child);
6793 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6795 __perf_event_exit_task(child_event, child_ctx, child);
6798 * If the last event was a group event, it will have appended all
6799 * its siblings to the list, but we obtained 'tmp' before that which
6800 * will still point to the list head terminating the iteration.
6802 if (!list_empty(&child_ctx->pinned_groups) ||
6803 !list_empty(&child_ctx->flexible_groups))
6806 mutex_unlock(&child_ctx->mutex);
6812 * When a child task exits, feed back event values to parent events.
6814 void perf_event_exit_task(struct task_struct *child)
6816 struct perf_event *event, *tmp;
6819 mutex_lock(&child->perf_event_mutex);
6820 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6822 list_del_init(&event->owner_entry);
6825 * Ensure the list deletion is visible before we clear
6826 * the owner, closes a race against perf_release() where
6827 * we need to serialize on the owner->perf_event_mutex.
6830 event->owner = NULL;
6832 mutex_unlock(&child->perf_event_mutex);
6834 for_each_task_context_nr(ctxn)
6835 perf_event_exit_task_context(child, ctxn);
6838 static void perf_free_event(struct perf_event *event,
6839 struct perf_event_context *ctx)
6841 struct perf_event *parent = event->parent;
6843 if (WARN_ON_ONCE(!parent))
6846 mutex_lock(&parent->child_mutex);
6847 list_del_init(&event->child_list);
6848 mutex_unlock(&parent->child_mutex);
6852 perf_group_detach(event);
6853 list_del_event(event, ctx);
6858 * free an unexposed, unused context as created by inheritance by
6859 * perf_event_init_task below, used by fork() in case of fail.
6861 void perf_event_free_task(struct task_struct *task)
6863 struct perf_event_context *ctx;
6864 struct perf_event *event, *tmp;
6867 for_each_task_context_nr(ctxn) {
6868 ctx = task->perf_event_ctxp[ctxn];
6872 mutex_lock(&ctx->mutex);
6874 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6876 perf_free_event(event, ctx);
6878 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6880 perf_free_event(event, ctx);
6882 if (!list_empty(&ctx->pinned_groups) ||
6883 !list_empty(&ctx->flexible_groups))
6886 mutex_unlock(&ctx->mutex);
6892 void perf_event_delayed_put(struct task_struct *task)
6896 for_each_task_context_nr(ctxn)
6897 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6901 * inherit a event from parent task to child task:
6903 static struct perf_event *
6904 inherit_event(struct perf_event *parent_event,
6905 struct task_struct *parent,
6906 struct perf_event_context *parent_ctx,
6907 struct task_struct *child,
6908 struct perf_event *group_leader,
6909 struct perf_event_context *child_ctx)
6911 struct perf_event *child_event;
6912 unsigned long flags;
6915 * Instead of creating recursive hierarchies of events,
6916 * we link inherited events back to the original parent,
6917 * which has a filp for sure, which we use as the reference
6920 if (parent_event->parent)
6921 parent_event = parent_event->parent;
6923 child_event = perf_event_alloc(&parent_event->attr,
6926 group_leader, parent_event,
6928 if (IS_ERR(child_event))
6933 * Make the child state follow the state of the parent event,
6934 * not its attr.disabled bit. We hold the parent's mutex,
6935 * so we won't race with perf_event_{en, dis}able_family.
6937 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6938 child_event->state = PERF_EVENT_STATE_INACTIVE;
6940 child_event->state = PERF_EVENT_STATE_OFF;
6942 if (parent_event->attr.freq) {
6943 u64 sample_period = parent_event->hw.sample_period;
6944 struct hw_perf_event *hwc = &child_event->hw;
6946 hwc->sample_period = sample_period;
6947 hwc->last_period = sample_period;
6949 local64_set(&hwc->period_left, sample_period);
6952 child_event->ctx = child_ctx;
6953 child_event->overflow_handler = parent_event->overflow_handler;
6956 * Precalculate sample_data sizes
6958 perf_event__header_size(child_event);
6959 perf_event__id_header_size(child_event);
6962 * Link it up in the child's context:
6964 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6965 add_event_to_ctx(child_event, child_ctx);
6966 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6969 * Get a reference to the parent filp - we will fput it
6970 * when the child event exits. This is safe to do because
6971 * we are in the parent and we know that the filp still
6972 * exists and has a nonzero count:
6974 atomic_long_inc(&parent_event->filp->f_count);
6977 * Link this into the parent event's child list
6979 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6980 mutex_lock(&parent_event->child_mutex);
6981 list_add_tail(&child_event->child_list, &parent_event->child_list);
6982 mutex_unlock(&parent_event->child_mutex);
6987 static int inherit_group(struct perf_event *parent_event,
6988 struct task_struct *parent,
6989 struct perf_event_context *parent_ctx,
6990 struct task_struct *child,
6991 struct perf_event_context *child_ctx)
6993 struct perf_event *leader;
6994 struct perf_event *sub;
6995 struct perf_event *child_ctr;
6997 leader = inherit_event(parent_event, parent, parent_ctx,
6998 child, NULL, child_ctx);
7000 return PTR_ERR(leader);
7001 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7002 child_ctr = inherit_event(sub, parent, parent_ctx,
7003 child, leader, child_ctx);
7004 if (IS_ERR(child_ctr))
7005 return PTR_ERR(child_ctr);
7011 inherit_task_group(struct perf_event *event, struct task_struct *parent,
7012 struct perf_event_context *parent_ctx,
7013 struct task_struct *child, int ctxn,
7017 struct perf_event_context *child_ctx;
7019 if (!event->attr.inherit) {
7024 child_ctx = child->perf_event_ctxp[ctxn];
7027 * This is executed from the parent task context, so
7028 * inherit events that have been marked for cloning.
7029 * First allocate and initialize a context for the
7033 child_ctx = alloc_perf_context(event->pmu, child);
7037 child->perf_event_ctxp[ctxn] = child_ctx;
7040 ret = inherit_group(event, parent, parent_ctx,
7050 * Initialize the perf_event context in task_struct
7052 int perf_event_init_context(struct task_struct *child, int ctxn)
7054 struct perf_event_context *child_ctx, *parent_ctx;
7055 struct perf_event_context *cloned_ctx;
7056 struct perf_event *event;
7057 struct task_struct *parent = current;
7058 int inherited_all = 1;
7059 unsigned long flags;
7062 if (likely(!parent->perf_event_ctxp[ctxn]))
7066 * If the parent's context is a clone, pin it so it won't get
7069 parent_ctx = perf_pin_task_context(parent, ctxn);
7072 * No need to check if parent_ctx != NULL here; since we saw
7073 * it non-NULL earlier, the only reason for it to become NULL
7074 * is if we exit, and since we're currently in the middle of
7075 * a fork we can't be exiting at the same time.
7079 * Lock the parent list. No need to lock the child - not PID
7080 * hashed yet and not running, so nobody can access it.
7082 mutex_lock(&parent_ctx->mutex);
7085 * We dont have to disable NMIs - we are only looking at
7086 * the list, not manipulating it:
7088 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
7089 ret = inherit_task_group(event, parent, parent_ctx,
7090 child, ctxn, &inherited_all);
7096 * We can't hold ctx->lock when iterating the ->flexible_group list due
7097 * to allocations, but we need to prevent rotation because
7098 * rotate_ctx() will change the list from interrupt context.
7100 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7101 parent_ctx->rotate_disable = 1;
7102 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7104 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
7105 ret = inherit_task_group(event, parent, parent_ctx,
7106 child, ctxn, &inherited_all);
7111 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7112 parent_ctx->rotate_disable = 0;
7114 child_ctx = child->perf_event_ctxp[ctxn];
7116 if (child_ctx && inherited_all) {
7118 * Mark the child context as a clone of the parent
7119 * context, or of whatever the parent is a clone of.
7121 * Note that if the parent is a clone, the holding of
7122 * parent_ctx->lock avoids it from being uncloned.
7124 cloned_ctx = parent_ctx->parent_ctx;
7126 child_ctx->parent_ctx = cloned_ctx;
7127 child_ctx->parent_gen = parent_ctx->parent_gen;
7129 child_ctx->parent_ctx = parent_ctx;
7130 child_ctx->parent_gen = parent_ctx->generation;
7132 get_ctx(child_ctx->parent_ctx);
7135 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7136 mutex_unlock(&parent_ctx->mutex);
7138 perf_unpin_context(parent_ctx);
7139 put_ctx(parent_ctx);
7145 * Initialize the perf_event context in task_struct
7147 int perf_event_init_task(struct task_struct *child)
7151 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7152 mutex_init(&child->perf_event_mutex);
7153 INIT_LIST_HEAD(&child->perf_event_list);
7155 for_each_task_context_nr(ctxn) {
7156 ret = perf_event_init_context(child, ctxn);
7164 static void __init perf_event_init_all_cpus(void)
7166 struct swevent_htable *swhash;
7169 for_each_possible_cpu(cpu) {
7170 swhash = &per_cpu(swevent_htable, cpu);
7171 mutex_init(&swhash->hlist_mutex);
7172 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
7176 static void __cpuinit perf_event_init_cpu(int cpu)
7178 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7180 mutex_lock(&swhash->hlist_mutex);
7181 if (swhash->hlist_refcount > 0) {
7182 struct swevent_hlist *hlist;
7184 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7186 rcu_assign_pointer(swhash->swevent_hlist, hlist);
7188 mutex_unlock(&swhash->hlist_mutex);
7191 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7192 static void perf_pmu_rotate_stop(struct pmu *pmu)
7194 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7196 WARN_ON(!irqs_disabled());
7198 list_del_init(&cpuctx->rotation_list);
7201 static void __perf_event_exit_context(void *__info)
7203 struct perf_event_context *ctx = __info;
7204 struct perf_event *event, *tmp;
7206 perf_pmu_rotate_stop(ctx->pmu);
7208 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
7209 __perf_remove_from_context(event);
7210 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
7211 __perf_remove_from_context(event);
7214 static void perf_event_exit_cpu_context(int cpu)
7216 struct perf_event_context *ctx;
7220 idx = srcu_read_lock(&pmus_srcu);
7221 list_for_each_entry_rcu(pmu, &pmus, entry) {
7222 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
7224 mutex_lock(&ctx->mutex);
7225 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7226 mutex_unlock(&ctx->mutex);
7228 srcu_read_unlock(&pmus_srcu, idx);
7231 static void perf_event_exit_cpu(int cpu)
7233 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7235 mutex_lock(&swhash->hlist_mutex);
7236 swevent_hlist_release(swhash);
7237 mutex_unlock(&swhash->hlist_mutex);
7239 perf_event_exit_cpu_context(cpu);
7242 static inline void perf_event_exit_cpu(int cpu) { }
7246 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7250 for_each_online_cpu(cpu)
7251 perf_event_exit_cpu(cpu);
7257 * Run the perf reboot notifier at the very last possible moment so that
7258 * the generic watchdog code runs as long as possible.
7260 static struct notifier_block perf_reboot_notifier = {
7261 .notifier_call = perf_reboot,
7262 .priority = INT_MIN,
7265 static int __cpuinit
7266 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7268 unsigned int cpu = (long)hcpu;
7270 switch (action & ~CPU_TASKS_FROZEN) {
7272 case CPU_UP_PREPARE:
7273 case CPU_DOWN_FAILED:
7274 perf_event_init_cpu(cpu);
7277 case CPU_UP_CANCELED:
7278 case CPU_DOWN_PREPARE:
7279 perf_event_exit_cpu(cpu);
7289 void __init perf_event_init(void)
7295 perf_event_init_all_cpus();
7296 init_srcu_struct(&pmus_srcu);
7297 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7298 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7299 perf_pmu_register(&perf_task_clock, NULL, -1);
7301 perf_cpu_notifier(perf_cpu_notify);
7302 register_reboot_notifier(&perf_reboot_notifier);
7304 ret = init_hw_breakpoint();
7305 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7308 static int __init perf_event_sysfs_init(void)
7313 mutex_lock(&pmus_lock);
7315 ret = bus_register(&pmu_bus);
7319 list_for_each_entry(pmu, &pmus, entry) {
7320 if (!pmu->name || pmu->type < 0)
7323 ret = pmu_dev_alloc(pmu);
7324 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7326 pmu_bus_running = 1;
7330 mutex_unlock(&pmus_lock);
7334 device_initcall(perf_event_sysfs_init);
7336 #ifdef CONFIG_CGROUP_PERF
7337 static struct cgroup_subsys_state *perf_cgroup_create(
7338 struct cgroup_subsys *ss, struct cgroup *cont)
7340 struct perf_cgroup *jc;
7341 struct perf_cgroup_info *t;
7344 jc = kmalloc(sizeof(*jc), GFP_KERNEL);
7346 return ERR_PTR(-ENOMEM);
7348 memset(jc, 0, sizeof(*jc));
7350 jc->info = alloc_percpu(struct perf_cgroup_info);
7353 return ERR_PTR(-ENOMEM);
7356 for_each_possible_cpu(c) {
7357 t = per_cpu_ptr(jc->info, c);
7364 static void perf_cgroup_destroy(struct cgroup_subsys *ss,
7365 struct cgroup *cont)
7367 struct perf_cgroup *jc;
7368 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7369 struct perf_cgroup, css);
7370 free_percpu(jc->info);
7374 static int __perf_cgroup_move(void *info)
7376 struct task_struct *task = info;
7377 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7381 static void perf_cgroup_move(struct task_struct *task)
7383 task_function_call(task, __perf_cgroup_move, task);
7386 static void perf_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
7387 struct cgroup *old_cgrp, struct task_struct *task,
7390 perf_cgroup_move(task);
7392 struct task_struct *c;
7394 list_for_each_entry_rcu(c, &task->thread_group, thread_group) {
7395 perf_cgroup_move(c);
7401 static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7402 struct cgroup *old_cgrp, struct task_struct *task)
7405 * cgroup_exit() is called in the copy_process() failure path.
7406 * Ignore this case since the task hasn't ran yet, this avoids
7407 * trying to poke a half freed task state from generic code.
7409 if (!(task->flags & PF_EXITING))
7412 perf_cgroup_move(task);
7415 struct cgroup_subsys perf_subsys = {
7416 .name = "perf_event",
7417 .subsys_id = perf_subsys_id,
7418 .create = perf_cgroup_create,
7419 .destroy = perf_cgroup_destroy,
7420 .exit = perf_cgroup_exit,
7421 .attach = perf_cgroup_attach,
7423 #endif /* CONFIG_CGROUP_PERF */