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
204 static inline struct perf_cgroup *
205 perf_cgroup_from_task(struct task_struct *task)
207 return container_of(task_subsys_state(task, perf_subsys_id),
208 struct perf_cgroup, css);
212 perf_cgroup_match(struct perf_event *event)
214 struct perf_event_context *ctx = event->ctx;
215 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
217 return !event->cgrp || event->cgrp == cpuctx->cgrp;
220 static inline void perf_get_cgroup(struct perf_event *event)
222 css_get(&event->cgrp->css);
225 static inline void perf_put_cgroup(struct perf_event *event)
227 css_put(&event->cgrp->css);
230 static inline void perf_detach_cgroup(struct perf_event *event)
232 perf_put_cgroup(event);
236 static inline int is_cgroup_event(struct perf_event *event)
238 return event->cgrp != NULL;
241 static inline u64 perf_cgroup_event_time(struct perf_event *event)
243 struct perf_cgroup_info *t;
245 t = per_cpu_ptr(event->cgrp->info, event->cpu);
249 static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
251 struct perf_cgroup_info *info;
256 info = this_cpu_ptr(cgrp->info);
258 info->time += now - info->timestamp;
259 info->timestamp = now;
262 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
264 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
266 __update_cgrp_time(cgrp_out);
269 static inline void update_cgrp_time_from_event(struct perf_event *event)
271 struct perf_cgroup *cgrp = perf_cgroup_from_task(current);
273 * do not update time when cgroup is not active
275 if (!event->cgrp || cgrp != event->cgrp)
278 __update_cgrp_time(event->cgrp);
282 perf_cgroup_set_timestamp(struct task_struct *task, u64 now)
284 struct perf_cgroup *cgrp;
285 struct perf_cgroup_info *info;
290 cgrp = perf_cgroup_from_task(task);
291 info = this_cpu_ptr(cgrp->info);
292 info->timestamp = now;
295 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
296 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
299 * reschedule events based on the cgroup constraint of task.
301 * mode SWOUT : schedule out everything
302 * mode SWIN : schedule in based on cgroup for next
304 void perf_cgroup_switch(struct task_struct *task, int mode)
306 struct perf_cpu_context *cpuctx;
311 * disable interrupts to avoid geting nr_cgroup
312 * changes via __perf_event_disable(). Also
315 local_irq_save(flags);
318 * we reschedule only in the presence of cgroup
319 * constrained events.
323 list_for_each_entry_rcu(pmu, &pmus, entry) {
325 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
327 perf_pmu_disable(cpuctx->ctx.pmu);
330 * perf_cgroup_events says at least one
331 * context on this CPU has cgroup events.
333 * ctx->nr_cgroups reports the number of cgroup
334 * events for a context.
336 if (cpuctx->ctx.nr_cgroups > 0) {
338 if (mode & PERF_CGROUP_SWOUT) {
339 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
341 * must not be done before ctxswout due
342 * to event_filter_match() in event_sched_out()
347 if (mode & PERF_CGROUP_SWIN) {
348 /* set cgrp before ctxsw in to
349 * allow event_filter_match() to not
350 * have to pass task around
352 cpuctx->cgrp = perf_cgroup_from_task(task);
353 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
357 perf_pmu_enable(cpuctx->ctx.pmu);
362 local_irq_restore(flags);
365 static inline void perf_cgroup_sched_out(struct task_struct *task)
367 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
370 static inline void perf_cgroup_sched_in(struct task_struct *task)
372 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
375 static inline int perf_cgroup_connect(int fd, struct perf_event *event,
376 struct perf_event_attr *attr,
377 struct perf_event *group_leader)
379 struct perf_cgroup *cgrp;
380 struct cgroup_subsys_state *css;
382 int ret = 0, fput_needed;
384 file = fget_light(fd, &fput_needed);
388 css = cgroup_css_from_dir(file, perf_subsys_id);
392 cgrp = container_of(css, struct perf_cgroup, css);
396 * all events in a group must monitor
397 * the same cgroup because a task belongs
398 * to only one perf cgroup at a time
400 if (group_leader && group_leader->cgrp != cgrp) {
401 perf_detach_cgroup(event);
404 /* must be done before we fput() the file */
405 perf_get_cgroup(event);
407 fput_light(file, fput_needed);
412 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
414 struct perf_cgroup_info *t;
415 t = per_cpu_ptr(event->cgrp->info, event->cpu);
416 event->shadow_ctx_time = now - t->timestamp;
420 perf_cgroup_defer_enabled(struct perf_event *event)
423 * when the current task's perf cgroup does not match
424 * the event's, we need to remember to call the
425 * perf_mark_enable() function the first time a task with
426 * a matching perf cgroup is scheduled in.
428 if (is_cgroup_event(event) && !perf_cgroup_match(event))
429 event->cgrp_defer_enabled = 1;
433 perf_cgroup_mark_enabled(struct perf_event *event,
434 struct perf_event_context *ctx)
436 struct perf_event *sub;
437 u64 tstamp = perf_event_time(event);
439 if (!event->cgrp_defer_enabled)
442 event->cgrp_defer_enabled = 0;
444 event->tstamp_enabled = tstamp - event->total_time_enabled;
445 list_for_each_entry(sub, &event->sibling_list, group_entry) {
446 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
447 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
448 sub->cgrp_defer_enabled = 0;
452 #else /* !CONFIG_CGROUP_PERF */
455 perf_cgroup_match(struct perf_event *event)
460 static inline void perf_detach_cgroup(struct perf_event *event)
463 static inline int is_cgroup_event(struct perf_event *event)
468 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
473 static inline void update_cgrp_time_from_event(struct perf_event *event)
477 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
481 static inline void perf_cgroup_sched_out(struct task_struct *task)
485 static inline void perf_cgroup_sched_in(struct task_struct *task)
489 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
490 struct perf_event_attr *attr,
491 struct perf_event *group_leader)
497 perf_cgroup_set_timestamp(struct task_struct *task, u64 now)
502 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
507 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
511 static inline u64 perf_cgroup_event_time(struct perf_event *event)
517 perf_cgroup_defer_enabled(struct perf_event *event)
522 perf_cgroup_mark_enabled(struct perf_event *event,
523 struct perf_event_context *ctx)
528 void perf_pmu_disable(struct pmu *pmu)
530 int *count = this_cpu_ptr(pmu->pmu_disable_count);
532 pmu->pmu_disable(pmu);
535 void perf_pmu_enable(struct pmu *pmu)
537 int *count = this_cpu_ptr(pmu->pmu_disable_count);
539 pmu->pmu_enable(pmu);
542 static DEFINE_PER_CPU(struct list_head, rotation_list);
545 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
546 * because they're strictly cpu affine and rotate_start is called with IRQs
547 * disabled, while rotate_context is called from IRQ context.
549 static void perf_pmu_rotate_start(struct pmu *pmu)
551 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
552 struct list_head *head = &__get_cpu_var(rotation_list);
554 WARN_ON(!irqs_disabled());
556 if (list_empty(&cpuctx->rotation_list))
557 list_add(&cpuctx->rotation_list, head);
560 static void get_ctx(struct perf_event_context *ctx)
562 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
565 static void free_ctx(struct rcu_head *head)
567 struct perf_event_context *ctx;
569 ctx = container_of(head, struct perf_event_context, rcu_head);
573 static void put_ctx(struct perf_event_context *ctx)
575 if (atomic_dec_and_test(&ctx->refcount)) {
577 put_ctx(ctx->parent_ctx);
579 put_task_struct(ctx->task);
580 call_rcu(&ctx->rcu_head, free_ctx);
584 static void unclone_ctx(struct perf_event_context *ctx)
586 if (ctx->parent_ctx) {
587 put_ctx(ctx->parent_ctx);
588 ctx->parent_ctx = NULL;
592 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
595 * only top level events have the pid namespace they were created in
598 event = event->parent;
600 return task_tgid_nr_ns(p, event->ns);
603 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
606 * only top level events have the pid namespace they were created in
609 event = event->parent;
611 return task_pid_nr_ns(p, event->ns);
615 * If we inherit events we want to return the parent event id
618 static u64 primary_event_id(struct perf_event *event)
623 id = event->parent->id;
629 * Get the perf_event_context for a task and lock it.
630 * This has to cope with with the fact that until it is locked,
631 * the context could get moved to another task.
633 static struct perf_event_context *
634 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
636 struct perf_event_context *ctx;
640 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
643 * If this context is a clone of another, it might
644 * get swapped for another underneath us by
645 * perf_event_task_sched_out, though the
646 * rcu_read_lock() protects us from any context
647 * getting freed. Lock the context and check if it
648 * got swapped before we could get the lock, and retry
649 * if so. If we locked the right context, then it
650 * can't get swapped on us any more.
652 raw_spin_lock_irqsave(&ctx->lock, *flags);
653 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
654 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
658 if (!atomic_inc_not_zero(&ctx->refcount)) {
659 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
668 * Get the context for a task and increment its pin_count so it
669 * can't get swapped to another task. This also increments its
670 * reference count so that the context can't get freed.
672 static struct perf_event_context *
673 perf_pin_task_context(struct task_struct *task, int ctxn)
675 struct perf_event_context *ctx;
678 ctx = perf_lock_task_context(task, ctxn, &flags);
681 raw_spin_unlock_irqrestore(&ctx->lock, flags);
686 static void perf_unpin_context(struct perf_event_context *ctx)
690 raw_spin_lock_irqsave(&ctx->lock, flags);
692 raw_spin_unlock_irqrestore(&ctx->lock, flags);
696 * Update the record of the current time in a context.
698 static void update_context_time(struct perf_event_context *ctx)
700 u64 now = perf_clock();
702 ctx->time += now - ctx->timestamp;
703 ctx->timestamp = now;
706 static u64 perf_event_time(struct perf_event *event)
708 struct perf_event_context *ctx = event->ctx;
710 if (is_cgroup_event(event))
711 return perf_cgroup_event_time(event);
713 return ctx ? ctx->time : 0;
717 * Update the total_time_enabled and total_time_running fields for a event.
719 static void update_event_times(struct perf_event *event)
721 struct perf_event_context *ctx = event->ctx;
724 if (event->state < PERF_EVENT_STATE_INACTIVE ||
725 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
728 * in cgroup mode, time_enabled represents
729 * the time the event was enabled AND active
730 * tasks were in the monitored cgroup. This is
731 * independent of the activity of the context as
732 * there may be a mix of cgroup and non-cgroup events.
734 * That is why we treat cgroup events differently
737 if (is_cgroup_event(event))
738 run_end = perf_event_time(event);
739 else if (ctx->is_active)
742 run_end = event->tstamp_stopped;
744 event->total_time_enabled = run_end - event->tstamp_enabled;
746 if (event->state == PERF_EVENT_STATE_INACTIVE)
747 run_end = event->tstamp_stopped;
749 run_end = perf_event_time(event);
751 event->total_time_running = run_end - event->tstamp_running;
756 * Update total_time_enabled and total_time_running for all events in a group.
758 static void update_group_times(struct perf_event *leader)
760 struct perf_event *event;
762 update_event_times(leader);
763 list_for_each_entry(event, &leader->sibling_list, group_entry)
764 update_event_times(event);
767 static struct list_head *
768 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
770 if (event->attr.pinned)
771 return &ctx->pinned_groups;
773 return &ctx->flexible_groups;
777 * Add a event from the lists for its context.
778 * Must be called with ctx->mutex and ctx->lock held.
781 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
783 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
784 event->attach_state |= PERF_ATTACH_CONTEXT;
787 * If we're a stand alone event or group leader, we go to the context
788 * list, group events are kept attached to the group so that
789 * perf_group_detach can, at all times, locate all siblings.
791 if (event->group_leader == event) {
792 struct list_head *list;
794 if (is_software_event(event))
795 event->group_flags |= PERF_GROUP_SOFTWARE;
797 list = ctx_group_list(event, ctx);
798 list_add_tail(&event->group_entry, list);
801 if (is_cgroup_event(event)) {
805 * - that has cgroup constraint on event->cpu
806 * - that may need work on context switch
808 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
809 jump_label_inc(&perf_sched_events);
812 list_add_rcu(&event->event_entry, &ctx->event_list);
814 perf_pmu_rotate_start(ctx->pmu);
816 if (event->attr.inherit_stat)
821 * Called at perf_event creation and when events are attached/detached from a
824 static void perf_event__read_size(struct perf_event *event)
826 int entry = sizeof(u64); /* value */
830 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
833 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
836 if (event->attr.read_format & PERF_FORMAT_ID)
837 entry += sizeof(u64);
839 if (event->attr.read_format & PERF_FORMAT_GROUP) {
840 nr += event->group_leader->nr_siblings;
845 event->read_size = size;
848 static void perf_event__header_size(struct perf_event *event)
850 struct perf_sample_data *data;
851 u64 sample_type = event->attr.sample_type;
854 perf_event__read_size(event);
856 if (sample_type & PERF_SAMPLE_IP)
857 size += sizeof(data->ip);
859 if (sample_type & PERF_SAMPLE_ADDR)
860 size += sizeof(data->addr);
862 if (sample_type & PERF_SAMPLE_PERIOD)
863 size += sizeof(data->period);
865 if (sample_type & PERF_SAMPLE_READ)
866 size += event->read_size;
868 event->header_size = size;
871 static void perf_event__id_header_size(struct perf_event *event)
873 struct perf_sample_data *data;
874 u64 sample_type = event->attr.sample_type;
877 if (sample_type & PERF_SAMPLE_TID)
878 size += sizeof(data->tid_entry);
880 if (sample_type & PERF_SAMPLE_TIME)
881 size += sizeof(data->time);
883 if (sample_type & PERF_SAMPLE_ID)
884 size += sizeof(data->id);
886 if (sample_type & PERF_SAMPLE_STREAM_ID)
887 size += sizeof(data->stream_id);
889 if (sample_type & PERF_SAMPLE_CPU)
890 size += sizeof(data->cpu_entry);
892 event->id_header_size = size;
895 static void perf_group_attach(struct perf_event *event)
897 struct perf_event *group_leader = event->group_leader, *pos;
900 * We can have double attach due to group movement in perf_event_open.
902 if (event->attach_state & PERF_ATTACH_GROUP)
905 event->attach_state |= PERF_ATTACH_GROUP;
907 if (group_leader == event)
910 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
911 !is_software_event(event))
912 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
914 list_add_tail(&event->group_entry, &group_leader->sibling_list);
915 group_leader->nr_siblings++;
917 perf_event__header_size(group_leader);
919 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
920 perf_event__header_size(pos);
924 * Remove a event from the lists for its context.
925 * Must be called with ctx->mutex and ctx->lock held.
928 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
931 * We can have double detach due to exit/hot-unplug + close.
933 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
936 event->attach_state &= ~PERF_ATTACH_CONTEXT;
938 if (is_cgroup_event(event)) {
940 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
941 jump_label_dec(&perf_sched_events);
945 if (event->attr.inherit_stat)
948 list_del_rcu(&event->event_entry);
950 if (event->group_leader == event)
951 list_del_init(&event->group_entry);
953 update_group_times(event);
956 * If event was in error state, then keep it
957 * that way, otherwise bogus counts will be
958 * returned on read(). The only way to get out
959 * of error state is by explicit re-enabling
962 if (event->state > PERF_EVENT_STATE_OFF)
963 event->state = PERF_EVENT_STATE_OFF;
966 static void perf_group_detach(struct perf_event *event)
968 struct perf_event *sibling, *tmp;
969 struct list_head *list = NULL;
972 * We can have double detach due to exit/hot-unplug + close.
974 if (!(event->attach_state & PERF_ATTACH_GROUP))
977 event->attach_state &= ~PERF_ATTACH_GROUP;
980 * If this is a sibling, remove it from its group.
982 if (event->group_leader != event) {
983 list_del_init(&event->group_entry);
984 event->group_leader->nr_siblings--;
988 if (!list_empty(&event->group_entry))
989 list = &event->group_entry;
992 * If this was a group event with sibling events then
993 * upgrade the siblings to singleton events by adding them
994 * to whatever list we are on.
996 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
998 list_move_tail(&sibling->group_entry, list);
999 sibling->group_leader = sibling;
1001 /* Inherit group flags from the previous leader */
1002 sibling->group_flags = event->group_flags;
1006 perf_event__header_size(event->group_leader);
1008 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1009 perf_event__header_size(tmp);
1013 event_filter_match(struct perf_event *event)
1015 return (event->cpu == -1 || event->cpu == smp_processor_id())
1016 && perf_cgroup_match(event);
1020 event_sched_out(struct perf_event *event,
1021 struct perf_cpu_context *cpuctx,
1022 struct perf_event_context *ctx)
1024 u64 tstamp = perf_event_time(event);
1027 * An event which could not be activated because of
1028 * filter mismatch still needs to have its timings
1029 * maintained, otherwise bogus information is return
1030 * via read() for time_enabled, time_running:
1032 if (event->state == PERF_EVENT_STATE_INACTIVE
1033 && !event_filter_match(event)) {
1034 delta = tstamp - event->tstamp_stopped;
1035 event->tstamp_running += delta;
1036 event->tstamp_stopped = tstamp;
1039 if (event->state != PERF_EVENT_STATE_ACTIVE)
1042 event->state = PERF_EVENT_STATE_INACTIVE;
1043 if (event->pending_disable) {
1044 event->pending_disable = 0;
1045 event->state = PERF_EVENT_STATE_OFF;
1047 event->tstamp_stopped = tstamp;
1048 event->pmu->del(event, 0);
1051 if (!is_software_event(event))
1052 cpuctx->active_oncpu--;
1054 if (event->attr.exclusive || !cpuctx->active_oncpu)
1055 cpuctx->exclusive = 0;
1059 group_sched_out(struct perf_event *group_event,
1060 struct perf_cpu_context *cpuctx,
1061 struct perf_event_context *ctx)
1063 struct perf_event *event;
1064 int state = group_event->state;
1066 event_sched_out(group_event, cpuctx, ctx);
1069 * Schedule out siblings (if any):
1071 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1072 event_sched_out(event, cpuctx, ctx);
1074 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
1075 cpuctx->exclusive = 0;
1079 * Cross CPU call to remove a performance event
1081 * We disable the event on the hardware level first. After that we
1082 * remove it from the context list.
1084 static int __perf_remove_from_context(void *info)
1086 struct perf_event *event = info;
1087 struct perf_event_context *ctx = event->ctx;
1088 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1090 raw_spin_lock(&ctx->lock);
1091 event_sched_out(event, cpuctx, ctx);
1092 list_del_event(event, ctx);
1093 raw_spin_unlock(&ctx->lock);
1100 * Remove the event from a task's (or a CPU's) list of events.
1102 * CPU events are removed with a smp call. For task events we only
1103 * call when the task is on a CPU.
1105 * If event->ctx is a cloned context, callers must make sure that
1106 * every task struct that event->ctx->task could possibly point to
1107 * remains valid. This is OK when called from perf_release since
1108 * that only calls us on the top-level context, which can't be a clone.
1109 * When called from perf_event_exit_task, it's OK because the
1110 * context has been detached from its task.
1112 static void perf_remove_from_context(struct perf_event *event)
1114 struct perf_event_context *ctx = event->ctx;
1115 struct task_struct *task = ctx->task;
1117 lockdep_assert_held(&ctx->mutex);
1121 * Per cpu events are removed via an smp call and
1122 * the removal is always successful.
1124 cpu_function_call(event->cpu, __perf_remove_from_context, event);
1129 if (!task_function_call(task, __perf_remove_from_context, event))
1132 raw_spin_lock_irq(&ctx->lock);
1134 * If we failed to find a running task, but find the context active now
1135 * that we've acquired the ctx->lock, retry.
1137 if (ctx->is_active) {
1138 raw_spin_unlock_irq(&ctx->lock);
1143 * Since the task isn't running, its safe to remove the event, us
1144 * holding the ctx->lock ensures the task won't get scheduled in.
1146 list_del_event(event, ctx);
1147 raw_spin_unlock_irq(&ctx->lock);
1151 * Cross CPU call to disable a performance event
1153 static int __perf_event_disable(void *info)
1155 struct perf_event *event = info;
1156 struct perf_event_context *ctx = event->ctx;
1157 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1160 * If this is a per-task event, need to check whether this
1161 * event's task is the current task on this cpu.
1163 * Can trigger due to concurrent perf_event_context_sched_out()
1164 * flipping contexts around.
1166 if (ctx->task && cpuctx->task_ctx != ctx)
1169 raw_spin_lock(&ctx->lock);
1172 * If the event is on, turn it off.
1173 * If it is in error state, leave it in error state.
1175 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1176 update_context_time(ctx);
1177 update_cgrp_time_from_event(event);
1178 update_group_times(event);
1179 if (event == event->group_leader)
1180 group_sched_out(event, cpuctx, ctx);
1182 event_sched_out(event, cpuctx, ctx);
1183 event->state = PERF_EVENT_STATE_OFF;
1186 raw_spin_unlock(&ctx->lock);
1194 * If event->ctx is a cloned context, callers must make sure that
1195 * every task struct that event->ctx->task could possibly point to
1196 * remains valid. This condition is satisifed when called through
1197 * perf_event_for_each_child or perf_event_for_each because they
1198 * hold the top-level event's child_mutex, so any descendant that
1199 * goes to exit will block in sync_child_event.
1200 * When called from perf_pending_event it's OK because event->ctx
1201 * is the current context on this CPU and preemption is disabled,
1202 * hence we can't get into perf_event_task_sched_out for this context.
1204 void perf_event_disable(struct perf_event *event)
1206 struct perf_event_context *ctx = event->ctx;
1207 struct task_struct *task = ctx->task;
1211 * Disable the event on the cpu that it's on
1213 cpu_function_call(event->cpu, __perf_event_disable, event);
1218 if (!task_function_call(task, __perf_event_disable, event))
1221 raw_spin_lock_irq(&ctx->lock);
1223 * If the event is still active, we need to retry the cross-call.
1225 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1226 raw_spin_unlock_irq(&ctx->lock);
1228 * Reload the task pointer, it might have been changed by
1229 * a concurrent perf_event_context_sched_out().
1236 * Since we have the lock this context can't be scheduled
1237 * in, so we can change the state safely.
1239 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1240 update_group_times(event);
1241 event->state = PERF_EVENT_STATE_OFF;
1243 raw_spin_unlock_irq(&ctx->lock);
1246 static void perf_set_shadow_time(struct perf_event *event,
1247 struct perf_event_context *ctx,
1251 * use the correct time source for the time snapshot
1253 * We could get by without this by leveraging the
1254 * fact that to get to this function, the caller
1255 * has most likely already called update_context_time()
1256 * and update_cgrp_time_xx() and thus both timestamp
1257 * are identical (or very close). Given that tstamp is,
1258 * already adjusted for cgroup, we could say that:
1259 * tstamp - ctx->timestamp
1261 * tstamp - cgrp->timestamp.
1263 * Then, in perf_output_read(), the calculation would
1264 * work with no changes because:
1265 * - event is guaranteed scheduled in
1266 * - no scheduled out in between
1267 * - thus the timestamp would be the same
1269 * But this is a bit hairy.
1271 * So instead, we have an explicit cgroup call to remain
1272 * within the time time source all along. We believe it
1273 * is cleaner and simpler to understand.
1275 if (is_cgroup_event(event))
1276 perf_cgroup_set_shadow_time(event, tstamp);
1278 event->shadow_ctx_time = tstamp - ctx->timestamp;
1281 #define MAX_INTERRUPTS (~0ULL)
1283 static void perf_log_throttle(struct perf_event *event, int enable);
1286 event_sched_in(struct perf_event *event,
1287 struct perf_cpu_context *cpuctx,
1288 struct perf_event_context *ctx)
1290 u64 tstamp = perf_event_time(event);
1292 if (event->state <= PERF_EVENT_STATE_OFF)
1295 event->state = PERF_EVENT_STATE_ACTIVE;
1296 event->oncpu = smp_processor_id();
1299 * Unthrottle events, since we scheduled we might have missed several
1300 * ticks already, also for a heavily scheduling task there is little
1301 * guarantee it'll get a tick in a timely manner.
1303 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1304 perf_log_throttle(event, 1);
1305 event->hw.interrupts = 0;
1309 * The new state must be visible before we turn it on in the hardware:
1313 if (event->pmu->add(event, PERF_EF_START)) {
1314 event->state = PERF_EVENT_STATE_INACTIVE;
1319 event->tstamp_running += tstamp - event->tstamp_stopped;
1321 perf_set_shadow_time(event, ctx, tstamp);
1323 if (!is_software_event(event))
1324 cpuctx->active_oncpu++;
1327 if (event->attr.exclusive)
1328 cpuctx->exclusive = 1;
1334 group_sched_in(struct perf_event *group_event,
1335 struct perf_cpu_context *cpuctx,
1336 struct perf_event_context *ctx)
1338 struct perf_event *event, *partial_group = NULL;
1339 struct pmu *pmu = group_event->pmu;
1340 u64 now = ctx->time;
1341 bool simulate = false;
1343 if (group_event->state == PERF_EVENT_STATE_OFF)
1346 pmu->start_txn(pmu);
1348 if (event_sched_in(group_event, cpuctx, ctx)) {
1349 pmu->cancel_txn(pmu);
1354 * Schedule in siblings as one group (if any):
1356 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1357 if (event_sched_in(event, cpuctx, ctx)) {
1358 partial_group = event;
1363 if (!pmu->commit_txn(pmu))
1368 * Groups can be scheduled in as one unit only, so undo any
1369 * partial group before returning:
1370 * The events up to the failed event are scheduled out normally,
1371 * tstamp_stopped will be updated.
1373 * The failed events and the remaining siblings need to have
1374 * their timings updated as if they had gone thru event_sched_in()
1375 * and event_sched_out(). This is required to get consistent timings
1376 * across the group. This also takes care of the case where the group
1377 * could never be scheduled by ensuring tstamp_stopped is set to mark
1378 * the time the event was actually stopped, such that time delta
1379 * calculation in update_event_times() is correct.
1381 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1382 if (event == partial_group)
1386 event->tstamp_running += now - event->tstamp_stopped;
1387 event->tstamp_stopped = now;
1389 event_sched_out(event, cpuctx, ctx);
1392 event_sched_out(group_event, cpuctx, ctx);
1394 pmu->cancel_txn(pmu);
1400 * Work out whether we can put this event group on the CPU now.
1402 static int group_can_go_on(struct perf_event *event,
1403 struct perf_cpu_context *cpuctx,
1407 * Groups consisting entirely of software events can always go on.
1409 if (event->group_flags & PERF_GROUP_SOFTWARE)
1412 * If an exclusive group is already on, no other hardware
1415 if (cpuctx->exclusive)
1418 * If this group is exclusive and there are already
1419 * events on the CPU, it can't go on.
1421 if (event->attr.exclusive && cpuctx->active_oncpu)
1424 * Otherwise, try to add it if all previous groups were able
1430 static void add_event_to_ctx(struct perf_event *event,
1431 struct perf_event_context *ctx)
1433 u64 tstamp = perf_event_time(event);
1435 list_add_event(event, ctx);
1436 perf_group_attach(event);
1437 event->tstamp_enabled = tstamp;
1438 event->tstamp_running = tstamp;
1439 event->tstamp_stopped = tstamp;
1442 static void perf_event_context_sched_in(struct perf_event_context *ctx,
1443 struct task_struct *tsk);
1446 * Cross CPU call to install and enable a performance event
1448 * Must be called with ctx->mutex held
1450 static int __perf_install_in_context(void *info)
1452 struct perf_event *event = info;
1453 struct perf_event_context *ctx = event->ctx;
1454 struct perf_event *leader = event->group_leader;
1455 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1459 * In case we're installing a new context to an already running task,
1460 * could also happen before perf_event_task_sched_in() on architectures
1461 * which do context switches with IRQs enabled.
1463 if (ctx->task && !cpuctx->task_ctx)
1464 perf_event_context_sched_in(ctx, ctx->task);
1466 raw_spin_lock(&ctx->lock);
1468 update_context_time(ctx);
1470 * update cgrp time only if current cgrp
1471 * matches event->cgrp. Must be done before
1472 * calling add_event_to_ctx()
1474 update_cgrp_time_from_event(event);
1476 add_event_to_ctx(event, ctx);
1478 if (!event_filter_match(event))
1482 * Don't put the event on if it is disabled or if
1483 * it is in a group and the group isn't on.
1485 if (event->state != PERF_EVENT_STATE_INACTIVE ||
1486 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
1490 * An exclusive event can't go on if there are already active
1491 * hardware events, and no hardware event can go on if there
1492 * is already an exclusive event on.
1494 if (!group_can_go_on(event, cpuctx, 1))
1497 err = event_sched_in(event, cpuctx, ctx);
1501 * This event couldn't go on. If it is in a group
1502 * then we have to pull the whole group off.
1503 * If the event group is pinned then put it in error state.
1505 if (leader != event)
1506 group_sched_out(leader, cpuctx, ctx);
1507 if (leader->attr.pinned) {
1508 update_group_times(leader);
1509 leader->state = PERF_EVENT_STATE_ERROR;
1514 raw_spin_unlock(&ctx->lock);
1520 * Attach a performance event to a context
1522 * First we add the event to the list with the hardware enable bit
1523 * in event->hw_config cleared.
1525 * If the event is attached to a task which is on a CPU we use a smp
1526 * call to enable it in the task context. The task might have been
1527 * scheduled away, but we check this in the smp call again.
1530 perf_install_in_context(struct perf_event_context *ctx,
1531 struct perf_event *event,
1534 struct task_struct *task = ctx->task;
1536 lockdep_assert_held(&ctx->mutex);
1542 * Per cpu events are installed via an smp call and
1543 * the install is always successful.
1545 cpu_function_call(cpu, __perf_install_in_context, event);
1550 if (!task_function_call(task, __perf_install_in_context, event))
1553 raw_spin_lock_irq(&ctx->lock);
1555 * If we failed to find a running task, but find the context active now
1556 * that we've acquired the ctx->lock, retry.
1558 if (ctx->is_active) {
1559 raw_spin_unlock_irq(&ctx->lock);
1564 * Since the task isn't running, its safe to add the event, us holding
1565 * the ctx->lock ensures the task won't get scheduled in.
1567 add_event_to_ctx(event, ctx);
1568 raw_spin_unlock_irq(&ctx->lock);
1572 * Put a event into inactive state and update time fields.
1573 * Enabling the leader of a group effectively enables all
1574 * the group members that aren't explicitly disabled, so we
1575 * have to update their ->tstamp_enabled also.
1576 * Note: this works for group members as well as group leaders
1577 * since the non-leader members' sibling_lists will be empty.
1579 static void __perf_event_mark_enabled(struct perf_event *event,
1580 struct perf_event_context *ctx)
1582 struct perf_event *sub;
1583 u64 tstamp = perf_event_time(event);
1585 event->state = PERF_EVENT_STATE_INACTIVE;
1586 event->tstamp_enabled = tstamp - event->total_time_enabled;
1587 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1588 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1589 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1594 * Cross CPU call to enable a performance event
1596 static int __perf_event_enable(void *info)
1598 struct perf_event *event = info;
1599 struct perf_event_context *ctx = event->ctx;
1600 struct perf_event *leader = event->group_leader;
1601 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1604 if (WARN_ON_ONCE(!ctx->is_active))
1607 raw_spin_lock(&ctx->lock);
1608 update_context_time(ctx);
1610 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1614 * set current task's cgroup time reference point
1616 perf_cgroup_set_timestamp(current, perf_clock());
1618 __perf_event_mark_enabled(event, ctx);
1620 if (!event_filter_match(event)) {
1621 if (is_cgroup_event(event))
1622 perf_cgroup_defer_enabled(event);
1627 * If the event is in a group and isn't the group leader,
1628 * then don't put it on unless the group is on.
1630 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1633 if (!group_can_go_on(event, cpuctx, 1)) {
1636 if (event == leader)
1637 err = group_sched_in(event, cpuctx, ctx);
1639 err = event_sched_in(event, cpuctx, ctx);
1644 * If this event can't go on and it's part of a
1645 * group, then the whole group has to come off.
1647 if (leader != event)
1648 group_sched_out(leader, cpuctx, ctx);
1649 if (leader->attr.pinned) {
1650 update_group_times(leader);
1651 leader->state = PERF_EVENT_STATE_ERROR;
1656 raw_spin_unlock(&ctx->lock);
1664 * If event->ctx is a cloned context, callers must make sure that
1665 * every task struct that event->ctx->task could possibly point to
1666 * remains valid. This condition is satisfied when called through
1667 * perf_event_for_each_child or perf_event_for_each as described
1668 * for perf_event_disable.
1670 void perf_event_enable(struct perf_event *event)
1672 struct perf_event_context *ctx = event->ctx;
1673 struct task_struct *task = ctx->task;
1677 * Enable the event on the cpu that it's on
1679 cpu_function_call(event->cpu, __perf_event_enable, event);
1683 raw_spin_lock_irq(&ctx->lock);
1684 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1688 * If the event is in error state, clear that first.
1689 * That way, if we see the event in error state below, we
1690 * know that it has gone back into error state, as distinct
1691 * from the task having been scheduled away before the
1692 * cross-call arrived.
1694 if (event->state == PERF_EVENT_STATE_ERROR)
1695 event->state = PERF_EVENT_STATE_OFF;
1698 if (!ctx->is_active) {
1699 __perf_event_mark_enabled(event, ctx);
1703 raw_spin_unlock_irq(&ctx->lock);
1705 if (!task_function_call(task, __perf_event_enable, event))
1708 raw_spin_lock_irq(&ctx->lock);
1711 * If the context is active and the event is still off,
1712 * we need to retry the cross-call.
1714 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1716 * task could have been flipped by a concurrent
1717 * perf_event_context_sched_out()
1724 raw_spin_unlock_irq(&ctx->lock);
1727 static int perf_event_refresh(struct perf_event *event, int refresh)
1730 * not supported on inherited events
1732 if (event->attr.inherit || !is_sampling_event(event))
1735 atomic_add(refresh, &event->event_limit);
1736 perf_event_enable(event);
1741 static void ctx_sched_out(struct perf_event_context *ctx,
1742 struct perf_cpu_context *cpuctx,
1743 enum event_type_t event_type)
1745 struct perf_event *event;
1747 raw_spin_lock(&ctx->lock);
1748 perf_pmu_disable(ctx->pmu);
1750 if (likely(!ctx->nr_events))
1752 update_context_time(ctx);
1753 update_cgrp_time_from_cpuctx(cpuctx);
1755 if (!ctx->nr_active)
1758 if (event_type & EVENT_PINNED) {
1759 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1760 group_sched_out(event, cpuctx, ctx);
1763 if (event_type & EVENT_FLEXIBLE) {
1764 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1765 group_sched_out(event, cpuctx, ctx);
1768 perf_pmu_enable(ctx->pmu);
1769 raw_spin_unlock(&ctx->lock);
1773 * Test whether two contexts are equivalent, i.e. whether they
1774 * have both been cloned from the same version of the same context
1775 * and they both have the same number of enabled events.
1776 * If the number of enabled events is the same, then the set
1777 * of enabled events should be the same, because these are both
1778 * inherited contexts, therefore we can't access individual events
1779 * in them directly with an fd; we can only enable/disable all
1780 * events via prctl, or enable/disable all events in a family
1781 * via ioctl, which will have the same effect on both contexts.
1783 static int context_equiv(struct perf_event_context *ctx1,
1784 struct perf_event_context *ctx2)
1786 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1787 && ctx1->parent_gen == ctx2->parent_gen
1788 && !ctx1->pin_count && !ctx2->pin_count;
1791 static void __perf_event_sync_stat(struct perf_event *event,
1792 struct perf_event *next_event)
1796 if (!event->attr.inherit_stat)
1800 * Update the event value, we cannot use perf_event_read()
1801 * because we're in the middle of a context switch and have IRQs
1802 * disabled, which upsets smp_call_function_single(), however
1803 * we know the event must be on the current CPU, therefore we
1804 * don't need to use it.
1806 switch (event->state) {
1807 case PERF_EVENT_STATE_ACTIVE:
1808 event->pmu->read(event);
1811 case PERF_EVENT_STATE_INACTIVE:
1812 update_event_times(event);
1820 * In order to keep per-task stats reliable we need to flip the event
1821 * values when we flip the contexts.
1823 value = local64_read(&next_event->count);
1824 value = local64_xchg(&event->count, value);
1825 local64_set(&next_event->count, value);
1827 swap(event->total_time_enabled, next_event->total_time_enabled);
1828 swap(event->total_time_running, next_event->total_time_running);
1831 * Since we swizzled the values, update the user visible data too.
1833 perf_event_update_userpage(event);
1834 perf_event_update_userpage(next_event);
1837 #define list_next_entry(pos, member) \
1838 list_entry(pos->member.next, typeof(*pos), member)
1840 static void perf_event_sync_stat(struct perf_event_context *ctx,
1841 struct perf_event_context *next_ctx)
1843 struct perf_event *event, *next_event;
1848 update_context_time(ctx);
1850 event = list_first_entry(&ctx->event_list,
1851 struct perf_event, event_entry);
1853 next_event = list_first_entry(&next_ctx->event_list,
1854 struct perf_event, event_entry);
1856 while (&event->event_entry != &ctx->event_list &&
1857 &next_event->event_entry != &next_ctx->event_list) {
1859 __perf_event_sync_stat(event, next_event);
1861 event = list_next_entry(event, event_entry);
1862 next_event = list_next_entry(next_event, event_entry);
1866 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1867 struct task_struct *next)
1869 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1870 struct perf_event_context *next_ctx;
1871 struct perf_event_context *parent;
1872 struct perf_cpu_context *cpuctx;
1878 cpuctx = __get_cpu_context(ctx);
1879 if (!cpuctx->task_ctx)
1883 parent = rcu_dereference(ctx->parent_ctx);
1884 next_ctx = next->perf_event_ctxp[ctxn];
1885 if (parent && next_ctx &&
1886 rcu_dereference(next_ctx->parent_ctx) == parent) {
1888 * Looks like the two contexts are clones, so we might be
1889 * able to optimize the context switch. We lock both
1890 * contexts and check that they are clones under the
1891 * lock (including re-checking that neither has been
1892 * uncloned in the meantime). It doesn't matter which
1893 * order we take the locks because no other cpu could
1894 * be trying to lock both of these tasks.
1896 raw_spin_lock(&ctx->lock);
1897 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1898 if (context_equiv(ctx, next_ctx)) {
1900 * XXX do we need a memory barrier of sorts
1901 * wrt to rcu_dereference() of perf_event_ctxp
1903 task->perf_event_ctxp[ctxn] = next_ctx;
1904 next->perf_event_ctxp[ctxn] = ctx;
1906 next_ctx->task = task;
1909 perf_event_sync_stat(ctx, next_ctx);
1911 raw_spin_unlock(&next_ctx->lock);
1912 raw_spin_unlock(&ctx->lock);
1917 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1918 cpuctx->task_ctx = NULL;
1922 #define for_each_task_context_nr(ctxn) \
1923 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1926 * Called from scheduler to remove the events of the current task,
1927 * with interrupts disabled.
1929 * We stop each event and update the event value in event->count.
1931 * This does not protect us against NMI, but disable()
1932 * sets the disabled bit in the control field of event _before_
1933 * accessing the event control register. If a NMI hits, then it will
1934 * not restart the event.
1936 void __perf_event_task_sched_out(struct task_struct *task,
1937 struct task_struct *next)
1941 for_each_task_context_nr(ctxn)
1942 perf_event_context_sched_out(task, ctxn, next);
1945 * if cgroup events exist on this CPU, then we need
1946 * to check if we have to switch out PMU state.
1947 * cgroup event are system-wide mode only
1949 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
1950 perf_cgroup_sched_out(task);
1953 static void task_ctx_sched_out(struct perf_event_context *ctx,
1954 enum event_type_t event_type)
1956 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1958 if (!cpuctx->task_ctx)
1961 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1964 ctx_sched_out(ctx, cpuctx, event_type);
1965 cpuctx->task_ctx = NULL;
1969 * Called with IRQs disabled
1971 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1972 enum event_type_t event_type)
1974 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
1978 ctx_pinned_sched_in(struct perf_event_context *ctx,
1979 struct perf_cpu_context *cpuctx)
1981 struct perf_event *event;
1983 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1984 if (event->state <= PERF_EVENT_STATE_OFF)
1986 if (!event_filter_match(event))
1989 /* may need to reset tstamp_enabled */
1990 if (is_cgroup_event(event))
1991 perf_cgroup_mark_enabled(event, ctx);
1993 if (group_can_go_on(event, cpuctx, 1))
1994 group_sched_in(event, cpuctx, ctx);
1997 * If this pinned group hasn't been scheduled,
1998 * put it in error state.
2000 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2001 update_group_times(event);
2002 event->state = PERF_EVENT_STATE_ERROR;
2008 ctx_flexible_sched_in(struct perf_event_context *ctx,
2009 struct perf_cpu_context *cpuctx)
2011 struct perf_event *event;
2014 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2015 /* Ignore events in OFF or ERROR state */
2016 if (event->state <= PERF_EVENT_STATE_OFF)
2019 * Listen to the 'cpu' scheduling filter constraint
2022 if (!event_filter_match(event))
2025 /* may need to reset tstamp_enabled */
2026 if (is_cgroup_event(event))
2027 perf_cgroup_mark_enabled(event, ctx);
2029 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2030 if (group_sched_in(event, cpuctx, ctx))
2037 ctx_sched_in(struct perf_event_context *ctx,
2038 struct perf_cpu_context *cpuctx,
2039 enum event_type_t event_type,
2040 struct task_struct *task)
2044 raw_spin_lock(&ctx->lock);
2046 if (likely(!ctx->nr_events))
2050 ctx->timestamp = now;
2051 perf_cgroup_set_timestamp(task, now);
2053 * First go through the list and put on any pinned groups
2054 * in order to give them the best chance of going on.
2056 if (event_type & EVENT_PINNED)
2057 ctx_pinned_sched_in(ctx, cpuctx);
2059 /* Then walk through the lower prio flexible groups */
2060 if (event_type & EVENT_FLEXIBLE)
2061 ctx_flexible_sched_in(ctx, cpuctx);
2064 raw_spin_unlock(&ctx->lock);
2067 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2068 enum event_type_t event_type,
2069 struct task_struct *task)
2071 struct perf_event_context *ctx = &cpuctx->ctx;
2073 ctx_sched_in(ctx, cpuctx, event_type, task);
2076 static void task_ctx_sched_in(struct perf_event_context *ctx,
2077 enum event_type_t event_type)
2079 struct perf_cpu_context *cpuctx;
2081 cpuctx = __get_cpu_context(ctx);
2082 if (cpuctx->task_ctx == ctx)
2085 ctx_sched_in(ctx, cpuctx, event_type, NULL);
2086 cpuctx->task_ctx = ctx;
2089 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2090 struct task_struct *task)
2092 struct perf_cpu_context *cpuctx;
2094 cpuctx = __get_cpu_context(ctx);
2095 if (cpuctx->task_ctx == ctx)
2098 perf_pmu_disable(ctx->pmu);
2100 * We want to keep the following priority order:
2101 * cpu pinned (that don't need to move), task pinned,
2102 * cpu flexible, task flexible.
2104 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2106 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2107 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2108 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2110 cpuctx->task_ctx = ctx;
2113 * Since these rotations are per-cpu, we need to ensure the
2114 * cpu-context we got scheduled on is actually rotating.
2116 perf_pmu_rotate_start(ctx->pmu);
2117 perf_pmu_enable(ctx->pmu);
2121 * Called from scheduler to add the events of the current task
2122 * with interrupts disabled.
2124 * We restore the event value and then enable it.
2126 * This does not protect us against NMI, but enable()
2127 * sets the enabled bit in the control field of event _before_
2128 * accessing the event control register. If a NMI hits, then it will
2129 * keep the event running.
2131 void __perf_event_task_sched_in(struct task_struct *task)
2133 struct perf_event_context *ctx;
2136 for_each_task_context_nr(ctxn) {
2137 ctx = task->perf_event_ctxp[ctxn];
2141 perf_event_context_sched_in(ctx, task);
2144 * if cgroup events exist on this CPU, then we need
2145 * to check if we have to switch in PMU state.
2146 * cgroup event are system-wide mode only
2148 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2149 perf_cgroup_sched_in(task);
2152 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2154 u64 frequency = event->attr.sample_freq;
2155 u64 sec = NSEC_PER_SEC;
2156 u64 divisor, dividend;
2158 int count_fls, nsec_fls, frequency_fls, sec_fls;
2160 count_fls = fls64(count);
2161 nsec_fls = fls64(nsec);
2162 frequency_fls = fls64(frequency);
2166 * We got @count in @nsec, with a target of sample_freq HZ
2167 * the target period becomes:
2170 * period = -------------------
2171 * @nsec * sample_freq
2176 * Reduce accuracy by one bit such that @a and @b converge
2177 * to a similar magnitude.
2179 #define REDUCE_FLS(a, b) \
2181 if (a##_fls > b##_fls) { \
2191 * Reduce accuracy until either term fits in a u64, then proceed with
2192 * the other, so that finally we can do a u64/u64 division.
2194 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2195 REDUCE_FLS(nsec, frequency);
2196 REDUCE_FLS(sec, count);
2199 if (count_fls + sec_fls > 64) {
2200 divisor = nsec * frequency;
2202 while (count_fls + sec_fls > 64) {
2203 REDUCE_FLS(count, sec);
2207 dividend = count * sec;
2209 dividend = count * sec;
2211 while (nsec_fls + frequency_fls > 64) {
2212 REDUCE_FLS(nsec, frequency);
2216 divisor = nsec * frequency;
2222 return div64_u64(dividend, divisor);
2225 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
2227 struct hw_perf_event *hwc = &event->hw;
2228 s64 period, sample_period;
2231 period = perf_calculate_period(event, nsec, count);
2233 delta = (s64)(period - hwc->sample_period);
2234 delta = (delta + 7) / 8; /* low pass filter */
2236 sample_period = hwc->sample_period + delta;
2241 hwc->sample_period = sample_period;
2243 if (local64_read(&hwc->period_left) > 8*sample_period) {
2244 event->pmu->stop(event, PERF_EF_UPDATE);
2245 local64_set(&hwc->period_left, 0);
2246 event->pmu->start(event, PERF_EF_RELOAD);
2250 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
2252 struct perf_event *event;
2253 struct hw_perf_event *hwc;
2254 u64 interrupts, now;
2257 raw_spin_lock(&ctx->lock);
2258 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2259 if (event->state != PERF_EVENT_STATE_ACTIVE)
2262 if (!event_filter_match(event))
2267 interrupts = hwc->interrupts;
2268 hwc->interrupts = 0;
2271 * unthrottle events on the tick
2273 if (interrupts == MAX_INTERRUPTS) {
2274 perf_log_throttle(event, 1);
2275 event->pmu->start(event, 0);
2278 if (!event->attr.freq || !event->attr.sample_freq)
2281 event->pmu->read(event);
2282 now = local64_read(&event->count);
2283 delta = now - hwc->freq_count_stamp;
2284 hwc->freq_count_stamp = now;
2287 perf_adjust_period(event, period, delta);
2289 raw_spin_unlock(&ctx->lock);
2293 * Round-robin a context's events:
2295 static void rotate_ctx(struct perf_event_context *ctx)
2297 raw_spin_lock(&ctx->lock);
2300 * Rotate the first entry last of non-pinned groups. Rotation might be
2301 * disabled by the inheritance code.
2303 if (!ctx->rotate_disable)
2304 list_rotate_left(&ctx->flexible_groups);
2306 raw_spin_unlock(&ctx->lock);
2310 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2311 * because they're strictly cpu affine and rotate_start is called with IRQs
2312 * disabled, while rotate_context is called from IRQ context.
2314 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
2316 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
2317 struct perf_event_context *ctx = NULL;
2318 int rotate = 0, remove = 1;
2320 if (cpuctx->ctx.nr_events) {
2322 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2326 ctx = cpuctx->task_ctx;
2327 if (ctx && ctx->nr_events) {
2329 if (ctx->nr_events != ctx->nr_active)
2333 perf_pmu_disable(cpuctx->ctx.pmu);
2334 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
2336 perf_ctx_adjust_freq(ctx, interval);
2341 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2343 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
2345 rotate_ctx(&cpuctx->ctx);
2349 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, current);
2351 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
2355 list_del_init(&cpuctx->rotation_list);
2357 perf_pmu_enable(cpuctx->ctx.pmu);
2360 void perf_event_task_tick(void)
2362 struct list_head *head = &__get_cpu_var(rotation_list);
2363 struct perf_cpu_context *cpuctx, *tmp;
2365 WARN_ON(!irqs_disabled());
2367 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2368 if (cpuctx->jiffies_interval == 1 ||
2369 !(jiffies % cpuctx->jiffies_interval))
2370 perf_rotate_context(cpuctx);
2374 static int event_enable_on_exec(struct perf_event *event,
2375 struct perf_event_context *ctx)
2377 if (!event->attr.enable_on_exec)
2380 event->attr.enable_on_exec = 0;
2381 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2384 __perf_event_mark_enabled(event, ctx);
2390 * Enable all of a task's events that have been marked enable-on-exec.
2391 * This expects task == current.
2393 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2395 struct perf_event *event;
2396 unsigned long flags;
2400 local_irq_save(flags);
2401 if (!ctx || !ctx->nr_events)
2404 task_ctx_sched_out(ctx, EVENT_ALL);
2406 raw_spin_lock(&ctx->lock);
2408 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2409 ret = event_enable_on_exec(event, ctx);
2414 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2415 ret = event_enable_on_exec(event, ctx);
2421 * Unclone this context if we enabled any event.
2426 raw_spin_unlock(&ctx->lock);
2428 perf_event_context_sched_in(ctx, ctx->task);
2430 local_irq_restore(flags);
2434 * Cross CPU call to read the hardware event
2436 static void __perf_event_read(void *info)
2438 struct perf_event *event = info;
2439 struct perf_event_context *ctx = event->ctx;
2440 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2443 * If this is a task context, we need to check whether it is
2444 * the current task context of this cpu. If not it has been
2445 * scheduled out before the smp call arrived. In that case
2446 * event->count would have been updated to a recent sample
2447 * when the event was scheduled out.
2449 if (ctx->task && cpuctx->task_ctx != ctx)
2452 raw_spin_lock(&ctx->lock);
2453 if (ctx->is_active) {
2454 update_context_time(ctx);
2455 update_cgrp_time_from_event(event);
2457 update_event_times(event);
2458 if (event->state == PERF_EVENT_STATE_ACTIVE)
2459 event->pmu->read(event);
2460 raw_spin_unlock(&ctx->lock);
2463 static inline u64 perf_event_count(struct perf_event *event)
2465 return local64_read(&event->count) + atomic64_read(&event->child_count);
2468 static u64 perf_event_read(struct perf_event *event)
2471 * If event is enabled and currently active on a CPU, update the
2472 * value in the event structure:
2474 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2475 smp_call_function_single(event->oncpu,
2476 __perf_event_read, event, 1);
2477 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2478 struct perf_event_context *ctx = event->ctx;
2479 unsigned long flags;
2481 raw_spin_lock_irqsave(&ctx->lock, flags);
2483 * may read while context is not active
2484 * (e.g., thread is blocked), in that case
2485 * we cannot update context time
2487 if (ctx->is_active) {
2488 update_context_time(ctx);
2489 update_cgrp_time_from_event(event);
2491 update_event_times(event);
2492 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2495 return perf_event_count(event);
2502 struct callchain_cpus_entries {
2503 struct rcu_head rcu_head;
2504 struct perf_callchain_entry *cpu_entries[0];
2507 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
2508 static atomic_t nr_callchain_events;
2509 static DEFINE_MUTEX(callchain_mutex);
2510 struct callchain_cpus_entries *callchain_cpus_entries;
2513 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
2514 struct pt_regs *regs)
2518 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
2519 struct pt_regs *regs)
2523 static void release_callchain_buffers_rcu(struct rcu_head *head)
2525 struct callchain_cpus_entries *entries;
2528 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
2530 for_each_possible_cpu(cpu)
2531 kfree(entries->cpu_entries[cpu]);
2536 static void release_callchain_buffers(void)
2538 struct callchain_cpus_entries *entries;
2540 entries = callchain_cpus_entries;
2541 rcu_assign_pointer(callchain_cpus_entries, NULL);
2542 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2545 static int alloc_callchain_buffers(void)
2549 struct callchain_cpus_entries *entries;
2552 * We can't use the percpu allocation API for data that can be
2553 * accessed from NMI. Use a temporary manual per cpu allocation
2554 * until that gets sorted out.
2556 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
2558 entries = kzalloc(size, GFP_KERNEL);
2562 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
2564 for_each_possible_cpu(cpu) {
2565 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2567 if (!entries->cpu_entries[cpu])
2571 rcu_assign_pointer(callchain_cpus_entries, entries);
2576 for_each_possible_cpu(cpu)
2577 kfree(entries->cpu_entries[cpu]);
2583 static int get_callchain_buffers(void)
2588 mutex_lock(&callchain_mutex);
2590 count = atomic_inc_return(&nr_callchain_events);
2591 if (WARN_ON_ONCE(count < 1)) {
2597 /* If the allocation failed, give up */
2598 if (!callchain_cpus_entries)
2603 err = alloc_callchain_buffers();
2605 release_callchain_buffers();
2607 mutex_unlock(&callchain_mutex);
2612 static void put_callchain_buffers(void)
2614 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2615 release_callchain_buffers();
2616 mutex_unlock(&callchain_mutex);
2620 static int get_recursion_context(int *recursion)
2628 else if (in_softirq())
2633 if (recursion[rctx])
2642 static inline void put_recursion_context(int *recursion, int rctx)
2648 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2651 struct callchain_cpus_entries *entries;
2653 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2657 entries = rcu_dereference(callchain_cpus_entries);
2661 cpu = smp_processor_id();
2663 return &entries->cpu_entries[cpu][*rctx];
2667 put_callchain_entry(int rctx)
2669 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2672 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2675 struct perf_callchain_entry *entry;
2678 entry = get_callchain_entry(&rctx);
2687 if (!user_mode(regs)) {
2688 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2689 perf_callchain_kernel(entry, regs);
2691 regs = task_pt_regs(current);
2697 perf_callchain_store(entry, PERF_CONTEXT_USER);
2698 perf_callchain_user(entry, regs);
2702 put_callchain_entry(rctx);
2708 * Initialize the perf_event context in a task_struct:
2710 static void __perf_event_init_context(struct perf_event_context *ctx)
2712 raw_spin_lock_init(&ctx->lock);
2713 mutex_init(&ctx->mutex);
2714 INIT_LIST_HEAD(&ctx->pinned_groups);
2715 INIT_LIST_HEAD(&ctx->flexible_groups);
2716 INIT_LIST_HEAD(&ctx->event_list);
2717 atomic_set(&ctx->refcount, 1);
2720 static struct perf_event_context *
2721 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2723 struct perf_event_context *ctx;
2725 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2729 __perf_event_init_context(ctx);
2732 get_task_struct(task);
2739 static struct task_struct *
2740 find_lively_task_by_vpid(pid_t vpid)
2742 struct task_struct *task;
2749 task = find_task_by_vpid(vpid);
2751 get_task_struct(task);
2755 return ERR_PTR(-ESRCH);
2757 /* Reuse ptrace permission checks for now. */
2759 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2764 put_task_struct(task);
2765 return ERR_PTR(err);
2770 * Returns a matching context with refcount and pincount.
2772 static struct perf_event_context *
2773 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2775 struct perf_event_context *ctx;
2776 struct perf_cpu_context *cpuctx;
2777 unsigned long flags;
2781 /* Must be root to operate on a CPU event: */
2782 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2783 return ERR_PTR(-EACCES);
2786 * We could be clever and allow to attach a event to an
2787 * offline CPU and activate it when the CPU comes up, but
2790 if (!cpu_online(cpu))
2791 return ERR_PTR(-ENODEV);
2793 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2802 ctxn = pmu->task_ctx_nr;
2807 ctx = perf_lock_task_context(task, ctxn, &flags);
2811 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2815 ctx = alloc_perf_context(pmu, task);
2823 mutex_lock(&task->perf_event_mutex);
2825 * If it has already passed perf_event_exit_task().
2826 * we must see PF_EXITING, it takes this mutex too.
2828 if (task->flags & PF_EXITING)
2830 else if (task->perf_event_ctxp[ctxn])
2834 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2836 mutex_unlock(&task->perf_event_mutex);
2838 if (unlikely(err)) {
2839 put_task_struct(task);
2851 return ERR_PTR(err);
2854 static void perf_event_free_filter(struct perf_event *event);
2856 static void free_event_rcu(struct rcu_head *head)
2858 struct perf_event *event;
2860 event = container_of(head, struct perf_event, rcu_head);
2862 put_pid_ns(event->ns);
2863 perf_event_free_filter(event);
2867 static void perf_buffer_put(struct perf_buffer *buffer);
2869 static void free_event(struct perf_event *event)
2871 irq_work_sync(&event->pending);
2873 if (!event->parent) {
2874 if (event->attach_state & PERF_ATTACH_TASK)
2875 jump_label_dec(&perf_sched_events);
2876 if (event->attr.mmap || event->attr.mmap_data)
2877 atomic_dec(&nr_mmap_events);
2878 if (event->attr.comm)
2879 atomic_dec(&nr_comm_events);
2880 if (event->attr.task)
2881 atomic_dec(&nr_task_events);
2882 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2883 put_callchain_buffers();
2886 if (event->buffer) {
2887 perf_buffer_put(event->buffer);
2888 event->buffer = NULL;
2891 if (is_cgroup_event(event))
2892 perf_detach_cgroup(event);
2895 event->destroy(event);
2898 put_ctx(event->ctx);
2900 call_rcu(&event->rcu_head, free_event_rcu);
2903 int perf_event_release_kernel(struct perf_event *event)
2905 struct perf_event_context *ctx = event->ctx;
2908 * Remove from the PMU, can't get re-enabled since we got
2909 * here because the last ref went.
2911 perf_event_disable(event);
2913 WARN_ON_ONCE(ctx->parent_ctx);
2915 * There are two ways this annotation is useful:
2917 * 1) there is a lock recursion from perf_event_exit_task
2918 * see the comment there.
2920 * 2) there is a lock-inversion with mmap_sem through
2921 * perf_event_read_group(), which takes faults while
2922 * holding ctx->mutex, however this is called after
2923 * the last filedesc died, so there is no possibility
2924 * to trigger the AB-BA case.
2926 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2927 raw_spin_lock_irq(&ctx->lock);
2928 perf_group_detach(event);
2929 list_del_event(event, ctx);
2930 raw_spin_unlock_irq(&ctx->lock);
2931 mutex_unlock(&ctx->mutex);
2937 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2940 * Called when the last reference to the file is gone.
2942 static int perf_release(struct inode *inode, struct file *file)
2944 struct perf_event *event = file->private_data;
2945 struct task_struct *owner;
2947 file->private_data = NULL;
2950 owner = ACCESS_ONCE(event->owner);
2952 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2953 * !owner it means the list deletion is complete and we can indeed
2954 * free this event, otherwise we need to serialize on
2955 * owner->perf_event_mutex.
2957 smp_read_barrier_depends();
2960 * Since delayed_put_task_struct() also drops the last
2961 * task reference we can safely take a new reference
2962 * while holding the rcu_read_lock().
2964 get_task_struct(owner);
2969 mutex_lock(&owner->perf_event_mutex);
2971 * We have to re-check the event->owner field, if it is cleared
2972 * we raced with perf_event_exit_task(), acquiring the mutex
2973 * ensured they're done, and we can proceed with freeing the
2977 list_del_init(&event->owner_entry);
2978 mutex_unlock(&owner->perf_event_mutex);
2979 put_task_struct(owner);
2982 return perf_event_release_kernel(event);
2985 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
2987 struct perf_event *child;
2993 mutex_lock(&event->child_mutex);
2994 total += perf_event_read(event);
2995 *enabled += event->total_time_enabled +
2996 atomic64_read(&event->child_total_time_enabled);
2997 *running += event->total_time_running +
2998 atomic64_read(&event->child_total_time_running);
3000 list_for_each_entry(child, &event->child_list, child_list) {
3001 total += perf_event_read(child);
3002 *enabled += child->total_time_enabled;
3003 *running += child->total_time_running;
3005 mutex_unlock(&event->child_mutex);
3009 EXPORT_SYMBOL_GPL(perf_event_read_value);
3011 static int perf_event_read_group(struct perf_event *event,
3012 u64 read_format, char __user *buf)
3014 struct perf_event *leader = event->group_leader, *sub;
3015 int n = 0, size = 0, ret = -EFAULT;
3016 struct perf_event_context *ctx = leader->ctx;
3018 u64 count, enabled, running;
3020 mutex_lock(&ctx->mutex);
3021 count = perf_event_read_value(leader, &enabled, &running);
3023 values[n++] = 1 + leader->nr_siblings;
3024 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3025 values[n++] = enabled;
3026 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3027 values[n++] = running;
3028 values[n++] = count;
3029 if (read_format & PERF_FORMAT_ID)
3030 values[n++] = primary_event_id(leader);
3032 size = n * sizeof(u64);
3034 if (copy_to_user(buf, values, size))
3039 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3042 values[n++] = perf_event_read_value(sub, &enabled, &running);
3043 if (read_format & PERF_FORMAT_ID)
3044 values[n++] = primary_event_id(sub);
3046 size = n * sizeof(u64);
3048 if (copy_to_user(buf + ret, values, size)) {
3056 mutex_unlock(&ctx->mutex);
3061 static int perf_event_read_one(struct perf_event *event,
3062 u64 read_format, char __user *buf)
3064 u64 enabled, running;
3068 values[n++] = perf_event_read_value(event, &enabled, &running);
3069 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3070 values[n++] = enabled;
3071 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3072 values[n++] = running;
3073 if (read_format & PERF_FORMAT_ID)
3074 values[n++] = primary_event_id(event);
3076 if (copy_to_user(buf, values, n * sizeof(u64)))
3079 return n * sizeof(u64);
3083 * Read the performance event - simple non blocking version for now
3086 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3088 u64 read_format = event->attr.read_format;
3092 * Return end-of-file for a read on a event that is in
3093 * error state (i.e. because it was pinned but it couldn't be
3094 * scheduled on to the CPU at some point).
3096 if (event->state == PERF_EVENT_STATE_ERROR)
3099 if (count < event->read_size)
3102 WARN_ON_ONCE(event->ctx->parent_ctx);
3103 if (read_format & PERF_FORMAT_GROUP)
3104 ret = perf_event_read_group(event, read_format, buf);
3106 ret = perf_event_read_one(event, read_format, buf);
3112 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3114 struct perf_event *event = file->private_data;
3116 return perf_read_hw(event, buf, count);
3119 static unsigned int perf_poll(struct file *file, poll_table *wait)
3121 struct perf_event *event = file->private_data;
3122 struct perf_buffer *buffer;
3123 unsigned int events = POLL_HUP;
3126 buffer = rcu_dereference(event->buffer);
3128 events = atomic_xchg(&buffer->poll, 0);
3131 poll_wait(file, &event->waitq, wait);
3136 static void perf_event_reset(struct perf_event *event)
3138 (void)perf_event_read(event);
3139 local64_set(&event->count, 0);
3140 perf_event_update_userpage(event);
3144 * Holding the top-level event's child_mutex means that any
3145 * descendant process that has inherited this event will block
3146 * in sync_child_event if it goes to exit, thus satisfying the
3147 * task existence requirements of perf_event_enable/disable.
3149 static void perf_event_for_each_child(struct perf_event *event,
3150 void (*func)(struct perf_event *))
3152 struct perf_event *child;
3154 WARN_ON_ONCE(event->ctx->parent_ctx);
3155 mutex_lock(&event->child_mutex);
3157 list_for_each_entry(child, &event->child_list, child_list)
3159 mutex_unlock(&event->child_mutex);
3162 static void perf_event_for_each(struct perf_event *event,
3163 void (*func)(struct perf_event *))
3165 struct perf_event_context *ctx = event->ctx;
3166 struct perf_event *sibling;
3168 WARN_ON_ONCE(ctx->parent_ctx);
3169 mutex_lock(&ctx->mutex);
3170 event = event->group_leader;
3172 perf_event_for_each_child(event, func);
3174 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3175 perf_event_for_each_child(event, func);
3176 mutex_unlock(&ctx->mutex);
3179 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3181 struct perf_event_context *ctx = event->ctx;
3185 if (!is_sampling_event(event))
3188 if (copy_from_user(&value, arg, sizeof(value)))
3194 raw_spin_lock_irq(&ctx->lock);
3195 if (event->attr.freq) {
3196 if (value > sysctl_perf_event_sample_rate) {
3201 event->attr.sample_freq = value;
3203 event->attr.sample_period = value;
3204 event->hw.sample_period = value;
3207 raw_spin_unlock_irq(&ctx->lock);
3212 static const struct file_operations perf_fops;
3214 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
3218 file = fget_light(fd, fput_needed);
3220 return ERR_PTR(-EBADF);
3222 if (file->f_op != &perf_fops) {
3223 fput_light(file, *fput_needed);
3225 return ERR_PTR(-EBADF);
3228 return file->private_data;
3231 static int perf_event_set_output(struct perf_event *event,
3232 struct perf_event *output_event);
3233 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3235 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3237 struct perf_event *event = file->private_data;
3238 void (*func)(struct perf_event *);
3242 case PERF_EVENT_IOC_ENABLE:
3243 func = perf_event_enable;
3245 case PERF_EVENT_IOC_DISABLE:
3246 func = perf_event_disable;
3248 case PERF_EVENT_IOC_RESET:
3249 func = perf_event_reset;
3252 case PERF_EVENT_IOC_REFRESH:
3253 return perf_event_refresh(event, arg);
3255 case PERF_EVENT_IOC_PERIOD:
3256 return perf_event_period(event, (u64 __user *)arg);
3258 case PERF_EVENT_IOC_SET_OUTPUT:
3260 struct perf_event *output_event = NULL;
3261 int fput_needed = 0;
3265 output_event = perf_fget_light(arg, &fput_needed);
3266 if (IS_ERR(output_event))
3267 return PTR_ERR(output_event);
3270 ret = perf_event_set_output(event, output_event);
3272 fput_light(output_event->filp, fput_needed);
3277 case PERF_EVENT_IOC_SET_FILTER:
3278 return perf_event_set_filter(event, (void __user *)arg);
3284 if (flags & PERF_IOC_FLAG_GROUP)
3285 perf_event_for_each(event, func);
3287 perf_event_for_each_child(event, func);
3292 int perf_event_task_enable(void)
3294 struct perf_event *event;
3296 mutex_lock(¤t->perf_event_mutex);
3297 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3298 perf_event_for_each_child(event, perf_event_enable);
3299 mutex_unlock(¤t->perf_event_mutex);
3304 int perf_event_task_disable(void)
3306 struct perf_event *event;
3308 mutex_lock(¤t->perf_event_mutex);
3309 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3310 perf_event_for_each_child(event, perf_event_disable);
3311 mutex_unlock(¤t->perf_event_mutex);
3316 #ifndef PERF_EVENT_INDEX_OFFSET
3317 # define PERF_EVENT_INDEX_OFFSET 0
3320 static int perf_event_index(struct perf_event *event)
3322 if (event->hw.state & PERF_HES_STOPPED)
3325 if (event->state != PERF_EVENT_STATE_ACTIVE)
3328 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
3332 * Callers need to ensure there can be no nesting of this function, otherwise
3333 * the seqlock logic goes bad. We can not serialize this because the arch
3334 * code calls this from NMI context.
3336 void perf_event_update_userpage(struct perf_event *event)
3338 struct perf_event_mmap_page *userpg;
3339 struct perf_buffer *buffer;
3342 buffer = rcu_dereference(event->buffer);
3346 userpg = buffer->user_page;
3349 * Disable preemption so as to not let the corresponding user-space
3350 * spin too long if we get preempted.
3355 userpg->index = perf_event_index(event);
3356 userpg->offset = perf_event_count(event);
3357 if (event->state == PERF_EVENT_STATE_ACTIVE)
3358 userpg->offset -= local64_read(&event->hw.prev_count);
3360 userpg->time_enabled = event->total_time_enabled +
3361 atomic64_read(&event->child_total_time_enabled);
3363 userpg->time_running = event->total_time_running +
3364 atomic64_read(&event->child_total_time_running);
3373 static unsigned long perf_data_size(struct perf_buffer *buffer);
3376 perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
3378 long max_size = perf_data_size(buffer);
3381 buffer->watermark = min(max_size, watermark);
3383 if (!buffer->watermark)
3384 buffer->watermark = max_size / 2;
3386 if (flags & PERF_BUFFER_WRITABLE)
3387 buffer->writable = 1;
3389 atomic_set(&buffer->refcount, 1);
3392 #ifndef CONFIG_PERF_USE_VMALLOC
3395 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
3398 static struct page *
3399 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
3401 if (pgoff > buffer->nr_pages)
3405 return virt_to_page(buffer->user_page);
3407 return virt_to_page(buffer->data_pages[pgoff - 1]);
3410 static void *perf_mmap_alloc_page(int cpu)
3415 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
3416 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
3420 return page_address(page);
3423 static struct perf_buffer *
3424 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
3426 struct perf_buffer *buffer;
3430 size = sizeof(struct perf_buffer);
3431 size += nr_pages * sizeof(void *);
3433 buffer = kzalloc(size, GFP_KERNEL);
3437 buffer->user_page = perf_mmap_alloc_page(cpu);
3438 if (!buffer->user_page)
3439 goto fail_user_page;
3441 for (i = 0; i < nr_pages; i++) {
3442 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
3443 if (!buffer->data_pages[i])
3444 goto fail_data_pages;
3447 buffer->nr_pages = nr_pages;
3449 perf_buffer_init(buffer, watermark, flags);
3454 for (i--; i >= 0; i--)
3455 free_page((unsigned long)buffer->data_pages[i]);
3457 free_page((unsigned long)buffer->user_page);
3466 static void perf_mmap_free_page(unsigned long addr)
3468 struct page *page = virt_to_page((void *)addr);
3470 page->mapping = NULL;
3474 static void perf_buffer_free(struct perf_buffer *buffer)
3478 perf_mmap_free_page((unsigned long)buffer->user_page);
3479 for (i = 0; i < buffer->nr_pages; i++)
3480 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
3484 static inline int page_order(struct perf_buffer *buffer)
3492 * Back perf_mmap() with vmalloc memory.
3494 * Required for architectures that have d-cache aliasing issues.
3497 static inline int page_order(struct perf_buffer *buffer)
3499 return buffer->page_order;
3502 static struct page *
3503 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
3505 if (pgoff > (1UL << page_order(buffer)))
3508 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
3511 static void perf_mmap_unmark_page(void *addr)
3513 struct page *page = vmalloc_to_page(addr);
3515 page->mapping = NULL;
3518 static void perf_buffer_free_work(struct work_struct *work)
3520 struct perf_buffer *buffer;
3524 buffer = container_of(work, struct perf_buffer, work);
3525 nr = 1 << page_order(buffer);
3527 base = buffer->user_page;
3528 for (i = 0; i < nr + 1; i++)
3529 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
3535 static void perf_buffer_free(struct perf_buffer *buffer)
3537 schedule_work(&buffer->work);
3540 static struct perf_buffer *
3541 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
3543 struct perf_buffer *buffer;
3547 size = sizeof(struct perf_buffer);
3548 size += sizeof(void *);
3550 buffer = kzalloc(size, GFP_KERNEL);
3554 INIT_WORK(&buffer->work, perf_buffer_free_work);
3556 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
3560 buffer->user_page = all_buf;
3561 buffer->data_pages[0] = all_buf + PAGE_SIZE;
3562 buffer->page_order = ilog2(nr_pages);
3563 buffer->nr_pages = 1;
3565 perf_buffer_init(buffer, watermark, flags);
3578 static unsigned long perf_data_size(struct perf_buffer *buffer)
3580 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
3583 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3585 struct perf_event *event = vma->vm_file->private_data;
3586 struct perf_buffer *buffer;
3587 int ret = VM_FAULT_SIGBUS;
3589 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3590 if (vmf->pgoff == 0)
3596 buffer = rcu_dereference(event->buffer);
3600 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3603 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
3607 get_page(vmf->page);
3608 vmf->page->mapping = vma->vm_file->f_mapping;
3609 vmf->page->index = vmf->pgoff;
3618 static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
3620 struct perf_buffer *buffer;
3622 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
3623 perf_buffer_free(buffer);
3626 static struct perf_buffer *perf_buffer_get(struct perf_event *event)
3628 struct perf_buffer *buffer;
3631 buffer = rcu_dereference(event->buffer);
3633 if (!atomic_inc_not_zero(&buffer->refcount))
3641 static void perf_buffer_put(struct perf_buffer *buffer)
3643 if (!atomic_dec_and_test(&buffer->refcount))
3646 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
3649 static void perf_mmap_open(struct vm_area_struct *vma)
3651 struct perf_event *event = vma->vm_file->private_data;
3653 atomic_inc(&event->mmap_count);
3656 static void perf_mmap_close(struct vm_area_struct *vma)
3658 struct perf_event *event = vma->vm_file->private_data;
3660 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
3661 unsigned long size = perf_data_size(event->buffer);
3662 struct user_struct *user = event->mmap_user;
3663 struct perf_buffer *buffer = event->buffer;
3665 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
3666 vma->vm_mm->locked_vm -= event->mmap_locked;
3667 rcu_assign_pointer(event->buffer, NULL);
3668 mutex_unlock(&event->mmap_mutex);
3670 perf_buffer_put(buffer);
3675 static const struct vm_operations_struct perf_mmap_vmops = {
3676 .open = perf_mmap_open,
3677 .close = perf_mmap_close,
3678 .fault = perf_mmap_fault,
3679 .page_mkwrite = perf_mmap_fault,
3682 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3684 struct perf_event *event = file->private_data;
3685 unsigned long user_locked, user_lock_limit;
3686 struct user_struct *user = current_user();
3687 unsigned long locked, lock_limit;
3688 struct perf_buffer *buffer;
3689 unsigned long vma_size;
3690 unsigned long nr_pages;
3691 long user_extra, extra;
3692 int ret = 0, flags = 0;
3695 * Don't allow mmap() of inherited per-task counters. This would
3696 * create a performance issue due to all children writing to the
3699 if (event->cpu == -1 && event->attr.inherit)
3702 if (!(vma->vm_flags & VM_SHARED))
3705 vma_size = vma->vm_end - vma->vm_start;
3706 nr_pages = (vma_size / PAGE_SIZE) - 1;
3709 * If we have buffer pages ensure they're a power-of-two number, so we
3710 * can do bitmasks instead of modulo.
3712 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3715 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3718 if (vma->vm_pgoff != 0)
3721 WARN_ON_ONCE(event->ctx->parent_ctx);
3722 mutex_lock(&event->mmap_mutex);
3723 if (event->buffer) {
3724 if (event->buffer->nr_pages == nr_pages)
3725 atomic_inc(&event->buffer->refcount);
3731 user_extra = nr_pages + 1;
3732 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3735 * Increase the limit linearly with more CPUs:
3737 user_lock_limit *= num_online_cpus();
3739 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3742 if (user_locked > user_lock_limit)
3743 extra = user_locked - user_lock_limit;
3745 lock_limit = rlimit(RLIMIT_MEMLOCK);
3746 lock_limit >>= PAGE_SHIFT;
3747 locked = vma->vm_mm->locked_vm + extra;
3749 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3750 !capable(CAP_IPC_LOCK)) {
3755 WARN_ON(event->buffer);
3757 if (vma->vm_flags & VM_WRITE)
3758 flags |= PERF_BUFFER_WRITABLE;
3760 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3766 rcu_assign_pointer(event->buffer, buffer);
3768 atomic_long_add(user_extra, &user->locked_vm);
3769 event->mmap_locked = extra;
3770 event->mmap_user = get_current_user();
3771 vma->vm_mm->locked_vm += event->mmap_locked;
3775 atomic_inc(&event->mmap_count);
3776 mutex_unlock(&event->mmap_mutex);
3778 vma->vm_flags |= VM_RESERVED;
3779 vma->vm_ops = &perf_mmap_vmops;
3784 static int perf_fasync(int fd, struct file *filp, int on)
3786 struct inode *inode = filp->f_path.dentry->d_inode;
3787 struct perf_event *event = filp->private_data;
3790 mutex_lock(&inode->i_mutex);
3791 retval = fasync_helper(fd, filp, on, &event->fasync);
3792 mutex_unlock(&inode->i_mutex);
3800 static const struct file_operations perf_fops = {
3801 .llseek = no_llseek,
3802 .release = perf_release,
3805 .unlocked_ioctl = perf_ioctl,
3806 .compat_ioctl = perf_ioctl,
3808 .fasync = perf_fasync,
3814 * If there's data, ensure we set the poll() state and publish everything
3815 * to user-space before waking everybody up.
3818 void perf_event_wakeup(struct perf_event *event)
3820 wake_up_all(&event->waitq);
3822 if (event->pending_kill) {
3823 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3824 event->pending_kill = 0;
3828 static void perf_pending_event(struct irq_work *entry)
3830 struct perf_event *event = container_of(entry,
3831 struct perf_event, pending);
3833 if (event->pending_disable) {
3834 event->pending_disable = 0;
3835 __perf_event_disable(event);
3838 if (event->pending_wakeup) {
3839 event->pending_wakeup = 0;
3840 perf_event_wakeup(event);
3845 * We assume there is only KVM supporting the callbacks.
3846 * Later on, we might change it to a list if there is
3847 * another virtualization implementation supporting the callbacks.
3849 struct perf_guest_info_callbacks *perf_guest_cbs;
3851 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3853 perf_guest_cbs = cbs;
3856 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3858 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3860 perf_guest_cbs = NULL;
3863 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3868 static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
3869 unsigned long offset, unsigned long head)
3873 if (!buffer->writable)
3876 mask = perf_data_size(buffer) - 1;
3878 offset = (offset - tail) & mask;
3879 head = (head - tail) & mask;
3881 if ((int)(head - offset) < 0)
3887 static void perf_output_wakeup(struct perf_output_handle *handle)
3889 atomic_set(&handle->buffer->poll, POLL_IN);
3892 handle->event->pending_wakeup = 1;
3893 irq_work_queue(&handle->event->pending);
3895 perf_event_wakeup(handle->event);
3899 * We need to ensure a later event_id doesn't publish a head when a former
3900 * event isn't done writing. However since we need to deal with NMIs we
3901 * cannot fully serialize things.
3903 * We only publish the head (and generate a wakeup) when the outer-most
3906 static void perf_output_get_handle(struct perf_output_handle *handle)
3908 struct perf_buffer *buffer = handle->buffer;
3911 local_inc(&buffer->nest);
3912 handle->wakeup = local_read(&buffer->wakeup);
3915 static void perf_output_put_handle(struct perf_output_handle *handle)
3917 struct perf_buffer *buffer = handle->buffer;
3921 head = local_read(&buffer->head);
3924 * IRQ/NMI can happen here, which means we can miss a head update.
3927 if (!local_dec_and_test(&buffer->nest))
3931 * Publish the known good head. Rely on the full barrier implied
3932 * by atomic_dec_and_test() order the buffer->head read and this
3935 buffer->user_page->data_head = head;
3938 * Now check if we missed an update, rely on the (compiler)
3939 * barrier in atomic_dec_and_test() to re-read buffer->head.
3941 if (unlikely(head != local_read(&buffer->head))) {
3942 local_inc(&buffer->nest);
3946 if (handle->wakeup != local_read(&buffer->wakeup))
3947 perf_output_wakeup(handle);
3953 __always_inline void perf_output_copy(struct perf_output_handle *handle,
3954 const void *buf, unsigned int len)
3957 unsigned long size = min_t(unsigned long, handle->size, len);
3959 memcpy(handle->addr, buf, size);
3962 handle->addr += size;
3964 handle->size -= size;
3965 if (!handle->size) {
3966 struct perf_buffer *buffer = handle->buffer;
3969 handle->page &= buffer->nr_pages - 1;
3970 handle->addr = buffer->data_pages[handle->page];
3971 handle->size = PAGE_SIZE << page_order(buffer);
3976 static void __perf_event_header__init_id(struct perf_event_header *header,
3977 struct perf_sample_data *data,
3978 struct perf_event *event)
3980 u64 sample_type = event->attr.sample_type;
3982 data->type = sample_type;
3983 header->size += event->id_header_size;
3985 if (sample_type & PERF_SAMPLE_TID) {
3986 /* namespace issues */
3987 data->tid_entry.pid = perf_event_pid(event, current);
3988 data->tid_entry.tid = perf_event_tid(event, current);
3991 if (sample_type & PERF_SAMPLE_TIME)
3992 data->time = perf_clock();
3994 if (sample_type & PERF_SAMPLE_ID)
3995 data->id = primary_event_id(event);
3997 if (sample_type & PERF_SAMPLE_STREAM_ID)
3998 data->stream_id = event->id;
4000 if (sample_type & PERF_SAMPLE_CPU) {
4001 data->cpu_entry.cpu = raw_smp_processor_id();
4002 data->cpu_entry.reserved = 0;
4006 static void perf_event_header__init_id(struct perf_event_header *header,
4007 struct perf_sample_data *data,
4008 struct perf_event *event)
4010 if (event->attr.sample_id_all)
4011 __perf_event_header__init_id(header, data, event);
4014 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4015 struct perf_sample_data *data)
4017 u64 sample_type = data->type;
4019 if (sample_type & PERF_SAMPLE_TID)
4020 perf_output_put(handle, data->tid_entry);
4022 if (sample_type & PERF_SAMPLE_TIME)
4023 perf_output_put(handle, data->time);
4025 if (sample_type & PERF_SAMPLE_ID)
4026 perf_output_put(handle, data->id);
4028 if (sample_type & PERF_SAMPLE_STREAM_ID)
4029 perf_output_put(handle, data->stream_id);
4031 if (sample_type & PERF_SAMPLE_CPU)
4032 perf_output_put(handle, data->cpu_entry);
4035 static void perf_event__output_id_sample(struct perf_event *event,
4036 struct perf_output_handle *handle,
4037 struct perf_sample_data *sample)
4039 if (event->attr.sample_id_all)
4040 __perf_event__output_id_sample(handle, sample);
4043 int perf_output_begin(struct perf_output_handle *handle,
4044 struct perf_event *event, unsigned int size,
4045 int nmi, int sample)
4047 struct perf_buffer *buffer;
4048 unsigned long tail, offset, head;
4050 struct perf_sample_data sample_data;
4052 struct perf_event_header header;
4059 * For inherited events we send all the output towards the parent.
4062 event = event->parent;
4064 buffer = rcu_dereference(event->buffer);
4068 handle->buffer = buffer;
4069 handle->event = event;
4071 handle->sample = sample;
4073 if (!buffer->nr_pages)
4076 have_lost = local_read(&buffer->lost);
4078 lost_event.header.size = sizeof(lost_event);
4079 perf_event_header__init_id(&lost_event.header, &sample_data,
4081 size += lost_event.header.size;
4084 perf_output_get_handle(handle);
4088 * Userspace could choose to issue a mb() before updating the
4089 * tail pointer. So that all reads will be completed before the
4092 tail = ACCESS_ONCE(buffer->user_page->data_tail);
4094 offset = head = local_read(&buffer->head);
4096 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
4098 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
4100 if (head - local_read(&buffer->wakeup) > buffer->watermark)
4101 local_add(buffer->watermark, &buffer->wakeup);
4103 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
4104 handle->page &= buffer->nr_pages - 1;
4105 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
4106 handle->addr = buffer->data_pages[handle->page];
4107 handle->addr += handle->size;
4108 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
4111 lost_event.header.type = PERF_RECORD_LOST;
4112 lost_event.header.misc = 0;
4113 lost_event.id = event->id;
4114 lost_event.lost = local_xchg(&buffer->lost, 0);
4116 perf_output_put(handle, lost_event);
4117 perf_event__output_id_sample(event, handle, &sample_data);
4123 local_inc(&buffer->lost);
4124 perf_output_put_handle(handle);
4131 void perf_output_end(struct perf_output_handle *handle)
4133 struct perf_event *event = handle->event;
4134 struct perf_buffer *buffer = handle->buffer;
4136 int wakeup_events = event->attr.wakeup_events;
4138 if (handle->sample && wakeup_events) {
4139 int events = local_inc_return(&buffer->events);
4140 if (events >= wakeup_events) {
4141 local_sub(wakeup_events, &buffer->events);
4142 local_inc(&buffer->wakeup);
4146 perf_output_put_handle(handle);
4150 static void perf_output_read_one(struct perf_output_handle *handle,
4151 struct perf_event *event,
4152 u64 enabled, u64 running)
4154 u64 read_format = event->attr.read_format;
4158 values[n++] = perf_event_count(event);
4159 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4160 values[n++] = enabled +
4161 atomic64_read(&event->child_total_time_enabled);
4163 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4164 values[n++] = running +
4165 atomic64_read(&event->child_total_time_running);
4167 if (read_format & PERF_FORMAT_ID)
4168 values[n++] = primary_event_id(event);
4170 perf_output_copy(handle, values, n * sizeof(u64));
4174 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4176 static void perf_output_read_group(struct perf_output_handle *handle,
4177 struct perf_event *event,
4178 u64 enabled, u64 running)
4180 struct perf_event *leader = event->group_leader, *sub;
4181 u64 read_format = event->attr.read_format;
4185 values[n++] = 1 + leader->nr_siblings;
4187 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4188 values[n++] = enabled;
4190 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4191 values[n++] = running;
4193 if (leader != event)
4194 leader->pmu->read(leader);
4196 values[n++] = perf_event_count(leader);
4197 if (read_format & PERF_FORMAT_ID)
4198 values[n++] = primary_event_id(leader);
4200 perf_output_copy(handle, values, n * sizeof(u64));
4202 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4206 sub->pmu->read(sub);
4208 values[n++] = perf_event_count(sub);
4209 if (read_format & PERF_FORMAT_ID)
4210 values[n++] = primary_event_id(sub);
4212 perf_output_copy(handle, values, n * sizeof(u64));
4216 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4217 PERF_FORMAT_TOTAL_TIME_RUNNING)
4219 static void perf_output_read(struct perf_output_handle *handle,
4220 struct perf_event *event)
4222 u64 enabled = 0, running = 0, now, ctx_time;
4223 u64 read_format = event->attr.read_format;
4226 * compute total_time_enabled, total_time_running
4227 * based on snapshot values taken when the event
4228 * was last scheduled in.
4230 * we cannot simply called update_context_time()
4231 * because of locking issue as we are called in
4234 if (read_format & PERF_FORMAT_TOTAL_TIMES) {
4236 ctx_time = event->shadow_ctx_time + now;
4237 enabled = ctx_time - event->tstamp_enabled;
4238 running = ctx_time - event->tstamp_running;
4241 if (event->attr.read_format & PERF_FORMAT_GROUP)
4242 perf_output_read_group(handle, event, enabled, running);
4244 perf_output_read_one(handle, event, enabled, running);
4247 void perf_output_sample(struct perf_output_handle *handle,
4248 struct perf_event_header *header,
4249 struct perf_sample_data *data,
4250 struct perf_event *event)
4252 u64 sample_type = data->type;
4254 perf_output_put(handle, *header);
4256 if (sample_type & PERF_SAMPLE_IP)
4257 perf_output_put(handle, data->ip);
4259 if (sample_type & PERF_SAMPLE_TID)
4260 perf_output_put(handle, data->tid_entry);
4262 if (sample_type & PERF_SAMPLE_TIME)
4263 perf_output_put(handle, data->time);
4265 if (sample_type & PERF_SAMPLE_ADDR)
4266 perf_output_put(handle, data->addr);
4268 if (sample_type & PERF_SAMPLE_ID)
4269 perf_output_put(handle, data->id);
4271 if (sample_type & PERF_SAMPLE_STREAM_ID)
4272 perf_output_put(handle, data->stream_id);
4274 if (sample_type & PERF_SAMPLE_CPU)
4275 perf_output_put(handle, data->cpu_entry);
4277 if (sample_type & PERF_SAMPLE_PERIOD)
4278 perf_output_put(handle, data->period);
4280 if (sample_type & PERF_SAMPLE_READ)
4281 perf_output_read(handle, event);
4283 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4284 if (data->callchain) {
4287 if (data->callchain)
4288 size += data->callchain->nr;
4290 size *= sizeof(u64);
4292 perf_output_copy(handle, data->callchain, size);
4295 perf_output_put(handle, nr);
4299 if (sample_type & PERF_SAMPLE_RAW) {
4301 perf_output_put(handle, data->raw->size);
4302 perf_output_copy(handle, data->raw->data,
4309 .size = sizeof(u32),
4312 perf_output_put(handle, raw);
4317 void perf_prepare_sample(struct perf_event_header *header,
4318 struct perf_sample_data *data,
4319 struct perf_event *event,
4320 struct pt_regs *regs)
4322 u64 sample_type = event->attr.sample_type;
4324 header->type = PERF_RECORD_SAMPLE;
4325 header->size = sizeof(*header) + event->header_size;
4328 header->misc |= perf_misc_flags(regs);
4330 __perf_event_header__init_id(header, data, event);
4332 if (sample_type & PERF_SAMPLE_IP)
4333 data->ip = perf_instruction_pointer(regs);
4335 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4338 data->callchain = perf_callchain(regs);
4340 if (data->callchain)
4341 size += data->callchain->nr;
4343 header->size += size * sizeof(u64);
4346 if (sample_type & PERF_SAMPLE_RAW) {
4347 int size = sizeof(u32);
4350 size += data->raw->size;
4352 size += sizeof(u32);
4354 WARN_ON_ONCE(size & (sizeof(u64)-1));
4355 header->size += size;
4359 static void perf_event_output(struct perf_event *event, int nmi,
4360 struct perf_sample_data *data,
4361 struct pt_regs *regs)
4363 struct perf_output_handle handle;
4364 struct perf_event_header header;
4366 /* protect the callchain buffers */
4369 perf_prepare_sample(&header, data, event, regs);
4371 if (perf_output_begin(&handle, event, header.size, nmi, 1))
4374 perf_output_sample(&handle, &header, data, event);
4376 perf_output_end(&handle);
4386 struct perf_read_event {
4387 struct perf_event_header header;
4394 perf_event_read_event(struct perf_event *event,
4395 struct task_struct *task)
4397 struct perf_output_handle handle;
4398 struct perf_sample_data sample;
4399 struct perf_read_event read_event = {
4401 .type = PERF_RECORD_READ,
4403 .size = sizeof(read_event) + event->read_size,
4405 .pid = perf_event_pid(event, task),
4406 .tid = perf_event_tid(event, task),
4410 perf_event_header__init_id(&read_event.header, &sample, event);
4411 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
4415 perf_output_put(&handle, read_event);
4416 perf_output_read(&handle, event);
4417 perf_event__output_id_sample(event, &handle, &sample);
4419 perf_output_end(&handle);
4423 * task tracking -- fork/exit
4425 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4428 struct perf_task_event {
4429 struct task_struct *task;
4430 struct perf_event_context *task_ctx;
4433 struct perf_event_header header;
4443 static void perf_event_task_output(struct perf_event *event,
4444 struct perf_task_event *task_event)
4446 struct perf_output_handle handle;
4447 struct perf_sample_data sample;
4448 struct task_struct *task = task_event->task;
4449 int ret, size = task_event->event_id.header.size;
4451 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4453 ret = perf_output_begin(&handle, event,
4454 task_event->event_id.header.size, 0, 0);
4458 task_event->event_id.pid = perf_event_pid(event, task);
4459 task_event->event_id.ppid = perf_event_pid(event, current);
4461 task_event->event_id.tid = perf_event_tid(event, task);
4462 task_event->event_id.ptid = perf_event_tid(event, current);
4464 perf_output_put(&handle, task_event->event_id);
4466 perf_event__output_id_sample(event, &handle, &sample);
4468 perf_output_end(&handle);
4470 task_event->event_id.header.size = size;
4473 static int perf_event_task_match(struct perf_event *event)
4475 if (event->state < PERF_EVENT_STATE_INACTIVE)
4478 if (!event_filter_match(event))
4481 if (event->attr.comm || event->attr.mmap ||
4482 event->attr.mmap_data || event->attr.task)
4488 static void perf_event_task_ctx(struct perf_event_context *ctx,
4489 struct perf_task_event *task_event)
4491 struct perf_event *event;
4493 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4494 if (perf_event_task_match(event))
4495 perf_event_task_output(event, task_event);
4499 static void perf_event_task_event(struct perf_task_event *task_event)
4501 struct perf_cpu_context *cpuctx;
4502 struct perf_event_context *ctx;
4507 list_for_each_entry_rcu(pmu, &pmus, entry) {
4508 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4509 if (cpuctx->active_pmu != pmu)
4511 perf_event_task_ctx(&cpuctx->ctx, task_event);
4513 ctx = task_event->task_ctx;
4515 ctxn = pmu->task_ctx_nr;
4518 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4521 perf_event_task_ctx(ctx, task_event);
4523 put_cpu_ptr(pmu->pmu_cpu_context);
4528 static void perf_event_task(struct task_struct *task,
4529 struct perf_event_context *task_ctx,
4532 struct perf_task_event task_event;
4534 if (!atomic_read(&nr_comm_events) &&
4535 !atomic_read(&nr_mmap_events) &&
4536 !atomic_read(&nr_task_events))
4539 task_event = (struct perf_task_event){
4541 .task_ctx = task_ctx,
4544 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4546 .size = sizeof(task_event.event_id),
4552 .time = perf_clock(),
4556 perf_event_task_event(&task_event);
4559 void perf_event_fork(struct task_struct *task)
4561 perf_event_task(task, NULL, 1);
4568 struct perf_comm_event {
4569 struct task_struct *task;
4574 struct perf_event_header header;
4581 static void perf_event_comm_output(struct perf_event *event,
4582 struct perf_comm_event *comm_event)
4584 struct perf_output_handle handle;
4585 struct perf_sample_data sample;
4586 int size = comm_event->event_id.header.size;
4589 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4590 ret = perf_output_begin(&handle, event,
4591 comm_event->event_id.header.size, 0, 0);
4596 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4597 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4599 perf_output_put(&handle, comm_event->event_id);
4600 perf_output_copy(&handle, comm_event->comm,
4601 comm_event->comm_size);
4603 perf_event__output_id_sample(event, &handle, &sample);
4605 perf_output_end(&handle);
4607 comm_event->event_id.header.size = size;
4610 static int perf_event_comm_match(struct perf_event *event)
4612 if (event->state < PERF_EVENT_STATE_INACTIVE)
4615 if (!event_filter_match(event))
4618 if (event->attr.comm)
4624 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4625 struct perf_comm_event *comm_event)
4627 struct perf_event *event;
4629 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4630 if (perf_event_comm_match(event))
4631 perf_event_comm_output(event, comm_event);
4635 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4637 struct perf_cpu_context *cpuctx;
4638 struct perf_event_context *ctx;
4639 char comm[TASK_COMM_LEN];
4644 memset(comm, 0, sizeof(comm));
4645 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4646 size = ALIGN(strlen(comm)+1, sizeof(u64));
4648 comm_event->comm = comm;
4649 comm_event->comm_size = size;
4651 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4653 list_for_each_entry_rcu(pmu, &pmus, entry) {
4654 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4655 if (cpuctx->active_pmu != pmu)
4657 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4659 ctxn = pmu->task_ctx_nr;
4663 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4665 perf_event_comm_ctx(ctx, comm_event);
4667 put_cpu_ptr(pmu->pmu_cpu_context);
4672 void perf_event_comm(struct task_struct *task)
4674 struct perf_comm_event comm_event;
4675 struct perf_event_context *ctx;
4678 for_each_task_context_nr(ctxn) {
4679 ctx = task->perf_event_ctxp[ctxn];
4683 perf_event_enable_on_exec(ctx);
4686 if (!atomic_read(&nr_comm_events))
4689 comm_event = (struct perf_comm_event){
4695 .type = PERF_RECORD_COMM,
4704 perf_event_comm_event(&comm_event);
4711 struct perf_mmap_event {
4712 struct vm_area_struct *vma;
4714 const char *file_name;
4718 struct perf_event_header header;
4728 static void perf_event_mmap_output(struct perf_event *event,
4729 struct perf_mmap_event *mmap_event)
4731 struct perf_output_handle handle;
4732 struct perf_sample_data sample;
4733 int size = mmap_event->event_id.header.size;
4736 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4737 ret = perf_output_begin(&handle, event,
4738 mmap_event->event_id.header.size, 0, 0);
4742 mmap_event->event_id.pid = perf_event_pid(event, current);
4743 mmap_event->event_id.tid = perf_event_tid(event, current);
4745 perf_output_put(&handle, mmap_event->event_id);
4746 perf_output_copy(&handle, mmap_event->file_name,
4747 mmap_event->file_size);
4749 perf_event__output_id_sample(event, &handle, &sample);
4751 perf_output_end(&handle);
4753 mmap_event->event_id.header.size = size;
4756 static int perf_event_mmap_match(struct perf_event *event,
4757 struct perf_mmap_event *mmap_event,
4760 if (event->state < PERF_EVENT_STATE_INACTIVE)
4763 if (!event_filter_match(event))
4766 if ((!executable && event->attr.mmap_data) ||
4767 (executable && event->attr.mmap))
4773 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4774 struct perf_mmap_event *mmap_event,
4777 struct perf_event *event;
4779 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4780 if (perf_event_mmap_match(event, mmap_event, executable))
4781 perf_event_mmap_output(event, mmap_event);
4785 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4787 struct perf_cpu_context *cpuctx;
4788 struct perf_event_context *ctx;
4789 struct vm_area_struct *vma = mmap_event->vma;
4790 struct file *file = vma->vm_file;
4798 memset(tmp, 0, sizeof(tmp));
4802 * d_path works from the end of the buffer backwards, so we
4803 * need to add enough zero bytes after the string to handle
4804 * the 64bit alignment we do later.
4806 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4808 name = strncpy(tmp, "//enomem", sizeof(tmp));
4811 name = d_path(&file->f_path, buf, PATH_MAX);
4813 name = strncpy(tmp, "//toolong", sizeof(tmp));
4817 if (arch_vma_name(mmap_event->vma)) {
4818 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4824 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4826 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4827 vma->vm_end >= vma->vm_mm->brk) {
4828 name = strncpy(tmp, "[heap]", sizeof(tmp));
4830 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4831 vma->vm_end >= vma->vm_mm->start_stack) {
4832 name = strncpy(tmp, "[stack]", sizeof(tmp));
4836 name = strncpy(tmp, "//anon", sizeof(tmp));
4841 size = ALIGN(strlen(name)+1, sizeof(u64));
4843 mmap_event->file_name = name;
4844 mmap_event->file_size = size;
4846 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4849 list_for_each_entry_rcu(pmu, &pmus, entry) {
4850 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4851 if (cpuctx->active_pmu != pmu)
4853 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4854 vma->vm_flags & VM_EXEC);
4856 ctxn = pmu->task_ctx_nr;
4860 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4862 perf_event_mmap_ctx(ctx, mmap_event,
4863 vma->vm_flags & VM_EXEC);
4866 put_cpu_ptr(pmu->pmu_cpu_context);
4873 void perf_event_mmap(struct vm_area_struct *vma)
4875 struct perf_mmap_event mmap_event;
4877 if (!atomic_read(&nr_mmap_events))
4880 mmap_event = (struct perf_mmap_event){
4886 .type = PERF_RECORD_MMAP,
4887 .misc = PERF_RECORD_MISC_USER,
4892 .start = vma->vm_start,
4893 .len = vma->vm_end - vma->vm_start,
4894 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4898 perf_event_mmap_event(&mmap_event);
4902 * IRQ throttle logging
4905 static void perf_log_throttle(struct perf_event *event, int enable)
4907 struct perf_output_handle handle;
4908 struct perf_sample_data sample;
4912 struct perf_event_header header;
4916 } throttle_event = {
4918 .type = PERF_RECORD_THROTTLE,
4920 .size = sizeof(throttle_event),
4922 .time = perf_clock(),
4923 .id = primary_event_id(event),
4924 .stream_id = event->id,
4928 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4930 perf_event_header__init_id(&throttle_event.header, &sample, event);
4932 ret = perf_output_begin(&handle, event,
4933 throttle_event.header.size, 1, 0);
4937 perf_output_put(&handle, throttle_event);
4938 perf_event__output_id_sample(event, &handle, &sample);
4939 perf_output_end(&handle);
4943 * Generic event overflow handling, sampling.
4946 static int __perf_event_overflow(struct perf_event *event, int nmi,
4947 int throttle, struct perf_sample_data *data,
4948 struct pt_regs *regs)
4950 int events = atomic_read(&event->event_limit);
4951 struct hw_perf_event *hwc = &event->hw;
4955 * Non-sampling counters might still use the PMI to fold short
4956 * hardware counters, ignore those.
4958 if (unlikely(!is_sampling_event(event)))
4961 if (unlikely(hwc->interrupts >= max_samples_per_tick)) {
4963 hwc->interrupts = MAX_INTERRUPTS;
4964 perf_log_throttle(event, 0);
4970 if (event->attr.freq) {
4971 u64 now = perf_clock();
4972 s64 delta = now - hwc->freq_time_stamp;
4974 hwc->freq_time_stamp = now;
4976 if (delta > 0 && delta < 2*TICK_NSEC)
4977 perf_adjust_period(event, delta, hwc->last_period);
4981 * XXX event_limit might not quite work as expected on inherited
4985 event->pending_kill = POLL_IN;
4986 if (events && atomic_dec_and_test(&event->event_limit)) {
4988 event->pending_kill = POLL_HUP;
4990 event->pending_disable = 1;
4991 irq_work_queue(&event->pending);
4993 perf_event_disable(event);
4996 if (event->overflow_handler)
4997 event->overflow_handler(event, nmi, data, regs);
4999 perf_event_output(event, nmi, data, regs);
5004 int perf_event_overflow(struct perf_event *event, int nmi,
5005 struct perf_sample_data *data,
5006 struct pt_regs *regs)
5008 return __perf_event_overflow(event, nmi, 1, data, regs);
5012 * Generic software event infrastructure
5015 struct swevent_htable {
5016 struct swevent_hlist *swevent_hlist;
5017 struct mutex hlist_mutex;
5020 /* Recursion avoidance in each contexts */
5021 int recursion[PERF_NR_CONTEXTS];
5024 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5027 * We directly increment event->count and keep a second value in
5028 * event->hw.period_left to count intervals. This period event
5029 * is kept in the range [-sample_period, 0] so that we can use the
5033 static u64 perf_swevent_set_period(struct perf_event *event)
5035 struct hw_perf_event *hwc = &event->hw;
5036 u64 period = hwc->last_period;
5040 hwc->last_period = hwc->sample_period;
5043 old = val = local64_read(&hwc->period_left);
5047 nr = div64_u64(period + val, period);
5048 offset = nr * period;
5050 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
5056 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
5057 int nmi, struct perf_sample_data *data,
5058 struct pt_regs *regs)
5060 struct hw_perf_event *hwc = &event->hw;
5063 data->period = event->hw.last_period;
5065 overflow = perf_swevent_set_period(event);
5067 if (hwc->interrupts == MAX_INTERRUPTS)
5070 for (; overflow; overflow--) {
5071 if (__perf_event_overflow(event, nmi, throttle,
5074 * We inhibit the overflow from happening when
5075 * hwc->interrupts == MAX_INTERRUPTS.
5083 static void perf_swevent_event(struct perf_event *event, u64 nr,
5084 int nmi, struct perf_sample_data *data,
5085 struct pt_regs *regs)
5087 struct hw_perf_event *hwc = &event->hw;
5089 local64_add(nr, &event->count);
5094 if (!is_sampling_event(event))
5097 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
5098 return perf_swevent_overflow(event, 1, nmi, data, regs);
5100 if (local64_add_negative(nr, &hwc->period_left))
5103 perf_swevent_overflow(event, 0, nmi, data, regs);
5106 static int perf_exclude_event(struct perf_event *event,
5107 struct pt_regs *regs)
5109 if (event->hw.state & PERF_HES_STOPPED)
5113 if (event->attr.exclude_user && user_mode(regs))
5116 if (event->attr.exclude_kernel && !user_mode(regs))
5123 static int perf_swevent_match(struct perf_event *event,
5124 enum perf_type_id type,
5126 struct perf_sample_data *data,
5127 struct pt_regs *regs)
5129 if (event->attr.type != type)
5132 if (event->attr.config != event_id)
5135 if (perf_exclude_event(event, regs))
5141 static inline u64 swevent_hash(u64 type, u32 event_id)
5143 u64 val = event_id | (type << 32);
5145 return hash_64(val, SWEVENT_HLIST_BITS);
5148 static inline struct hlist_head *
5149 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
5151 u64 hash = swevent_hash(type, event_id);
5153 return &hlist->heads[hash];
5156 /* For the read side: events when they trigger */
5157 static inline struct hlist_head *
5158 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
5160 struct swevent_hlist *hlist;
5162 hlist = rcu_dereference(swhash->swevent_hlist);
5166 return __find_swevent_head(hlist, type, event_id);
5169 /* For the event head insertion and removal in the hlist */
5170 static inline struct hlist_head *
5171 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
5173 struct swevent_hlist *hlist;
5174 u32 event_id = event->attr.config;
5175 u64 type = event->attr.type;
5178 * Event scheduling is always serialized against hlist allocation
5179 * and release. Which makes the protected version suitable here.
5180 * The context lock guarantees that.
5182 hlist = rcu_dereference_protected(swhash->swevent_hlist,
5183 lockdep_is_held(&event->ctx->lock));
5187 return __find_swevent_head(hlist, type, event_id);
5190 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5192 struct perf_sample_data *data,
5193 struct pt_regs *regs)
5195 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5196 struct perf_event *event;
5197 struct hlist_node *node;
5198 struct hlist_head *head;
5201 head = find_swevent_head_rcu(swhash, type, event_id);
5205 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5206 if (perf_swevent_match(event, type, event_id, data, regs))
5207 perf_swevent_event(event, nr, nmi, data, regs);
5213 int perf_swevent_get_recursion_context(void)
5215 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5217 return get_recursion_context(swhash->recursion);
5219 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
5221 inline void perf_swevent_put_recursion_context(int rctx)
5223 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5225 put_recursion_context(swhash->recursion, rctx);
5228 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
5229 struct pt_regs *regs, u64 addr)
5231 struct perf_sample_data data;
5234 preempt_disable_notrace();
5235 rctx = perf_swevent_get_recursion_context();
5239 perf_sample_data_init(&data, addr);
5241 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
5243 perf_swevent_put_recursion_context(rctx);
5244 preempt_enable_notrace();
5247 static void perf_swevent_read(struct perf_event *event)
5251 static int perf_swevent_add(struct perf_event *event, int flags)
5253 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5254 struct hw_perf_event *hwc = &event->hw;
5255 struct hlist_head *head;
5257 if (is_sampling_event(event)) {
5258 hwc->last_period = hwc->sample_period;
5259 perf_swevent_set_period(event);
5262 hwc->state = !(flags & PERF_EF_START);
5264 head = find_swevent_head(swhash, event);
5265 if (WARN_ON_ONCE(!head))
5268 hlist_add_head_rcu(&event->hlist_entry, head);
5273 static void perf_swevent_del(struct perf_event *event, int flags)
5275 hlist_del_rcu(&event->hlist_entry);
5278 static void perf_swevent_start(struct perf_event *event, int flags)
5280 event->hw.state = 0;
5283 static void perf_swevent_stop(struct perf_event *event, int flags)
5285 event->hw.state = PERF_HES_STOPPED;
5288 /* Deref the hlist from the update side */
5289 static inline struct swevent_hlist *
5290 swevent_hlist_deref(struct swevent_htable *swhash)
5292 return rcu_dereference_protected(swhash->swevent_hlist,
5293 lockdep_is_held(&swhash->hlist_mutex));
5296 static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
5298 struct swevent_hlist *hlist;
5300 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
5304 static void swevent_hlist_release(struct swevent_htable *swhash)
5306 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
5311 rcu_assign_pointer(swhash->swevent_hlist, NULL);
5312 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
5315 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5317 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5319 mutex_lock(&swhash->hlist_mutex);
5321 if (!--swhash->hlist_refcount)
5322 swevent_hlist_release(swhash);
5324 mutex_unlock(&swhash->hlist_mutex);
5327 static void swevent_hlist_put(struct perf_event *event)
5331 if (event->cpu != -1) {
5332 swevent_hlist_put_cpu(event, event->cpu);
5336 for_each_possible_cpu(cpu)
5337 swevent_hlist_put_cpu(event, cpu);
5340 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5342 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5345 mutex_lock(&swhash->hlist_mutex);
5347 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
5348 struct swevent_hlist *hlist;
5350 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5355 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5357 swhash->hlist_refcount++;
5359 mutex_unlock(&swhash->hlist_mutex);
5364 static int swevent_hlist_get(struct perf_event *event)
5367 int cpu, failed_cpu;
5369 if (event->cpu != -1)
5370 return swevent_hlist_get_cpu(event, event->cpu);
5373 for_each_possible_cpu(cpu) {
5374 err = swevent_hlist_get_cpu(event, cpu);
5384 for_each_possible_cpu(cpu) {
5385 if (cpu == failed_cpu)
5387 swevent_hlist_put_cpu(event, cpu);
5394 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
5396 static void sw_perf_event_destroy(struct perf_event *event)
5398 u64 event_id = event->attr.config;
5400 WARN_ON(event->parent);
5402 jump_label_dec(&perf_swevent_enabled[event_id]);
5403 swevent_hlist_put(event);
5406 static int perf_swevent_init(struct perf_event *event)
5408 int event_id = event->attr.config;
5410 if (event->attr.type != PERF_TYPE_SOFTWARE)
5414 case PERF_COUNT_SW_CPU_CLOCK:
5415 case PERF_COUNT_SW_TASK_CLOCK:
5422 if (event_id >= PERF_COUNT_SW_MAX)
5425 if (!event->parent) {
5428 err = swevent_hlist_get(event);
5432 jump_label_inc(&perf_swevent_enabled[event_id]);
5433 event->destroy = sw_perf_event_destroy;
5439 static struct pmu perf_swevent = {
5440 .task_ctx_nr = perf_sw_context,
5442 .event_init = perf_swevent_init,
5443 .add = perf_swevent_add,
5444 .del = perf_swevent_del,
5445 .start = perf_swevent_start,
5446 .stop = perf_swevent_stop,
5447 .read = perf_swevent_read,
5450 #ifdef CONFIG_EVENT_TRACING
5452 static int perf_tp_filter_match(struct perf_event *event,
5453 struct perf_sample_data *data)
5455 void *record = data->raw->data;
5457 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5462 static int perf_tp_event_match(struct perf_event *event,
5463 struct perf_sample_data *data,
5464 struct pt_regs *regs)
5467 * All tracepoints are from kernel-space.
5469 if (event->attr.exclude_kernel)
5472 if (!perf_tp_filter_match(event, data))
5478 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5479 struct pt_regs *regs, struct hlist_head *head, int rctx)
5481 struct perf_sample_data data;
5482 struct perf_event *event;
5483 struct hlist_node *node;
5485 struct perf_raw_record raw = {
5490 perf_sample_data_init(&data, addr);
5493 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5494 if (perf_tp_event_match(event, &data, regs))
5495 perf_swevent_event(event, count, 1, &data, regs);
5498 perf_swevent_put_recursion_context(rctx);
5500 EXPORT_SYMBOL_GPL(perf_tp_event);
5502 static void tp_perf_event_destroy(struct perf_event *event)
5504 perf_trace_destroy(event);
5507 static int perf_tp_event_init(struct perf_event *event)
5511 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5514 err = perf_trace_init(event);
5518 event->destroy = tp_perf_event_destroy;
5523 static struct pmu perf_tracepoint = {
5524 .task_ctx_nr = perf_sw_context,
5526 .event_init = perf_tp_event_init,
5527 .add = perf_trace_add,
5528 .del = perf_trace_del,
5529 .start = perf_swevent_start,
5530 .stop = perf_swevent_stop,
5531 .read = perf_swevent_read,
5534 static inline void perf_tp_register(void)
5536 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5539 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5544 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5547 filter_str = strndup_user(arg, PAGE_SIZE);
5548 if (IS_ERR(filter_str))
5549 return PTR_ERR(filter_str);
5551 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5557 static void perf_event_free_filter(struct perf_event *event)
5559 ftrace_profile_free_filter(event);
5564 static inline void perf_tp_register(void)
5568 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5573 static void perf_event_free_filter(struct perf_event *event)
5577 #endif /* CONFIG_EVENT_TRACING */
5579 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5580 void perf_bp_event(struct perf_event *bp, void *data)
5582 struct perf_sample_data sample;
5583 struct pt_regs *regs = data;
5585 perf_sample_data_init(&sample, bp->attr.bp_addr);
5587 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5588 perf_swevent_event(bp, 1, 1, &sample, regs);
5593 * hrtimer based swevent callback
5596 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5598 enum hrtimer_restart ret = HRTIMER_RESTART;
5599 struct perf_sample_data data;
5600 struct pt_regs *regs;
5601 struct perf_event *event;
5604 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5606 if (event->state != PERF_EVENT_STATE_ACTIVE)
5607 return HRTIMER_NORESTART;
5609 event->pmu->read(event);
5611 perf_sample_data_init(&data, 0);
5612 data.period = event->hw.last_period;
5613 regs = get_irq_regs();
5615 if (regs && !perf_exclude_event(event, regs)) {
5616 if (!(event->attr.exclude_idle && current->pid == 0))
5617 if (perf_event_overflow(event, 0, &data, regs))
5618 ret = HRTIMER_NORESTART;
5621 period = max_t(u64, 10000, event->hw.sample_period);
5622 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5627 static void perf_swevent_start_hrtimer(struct perf_event *event)
5629 struct hw_perf_event *hwc = &event->hw;
5632 if (!is_sampling_event(event))
5635 period = local64_read(&hwc->period_left);
5640 local64_set(&hwc->period_left, 0);
5642 period = max_t(u64, 10000, hwc->sample_period);
5644 __hrtimer_start_range_ns(&hwc->hrtimer,
5645 ns_to_ktime(period), 0,
5646 HRTIMER_MODE_REL_PINNED, 0);
5649 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5651 struct hw_perf_event *hwc = &event->hw;
5653 if (is_sampling_event(event)) {
5654 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5655 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5657 hrtimer_cancel(&hwc->hrtimer);
5661 static void perf_swevent_init_hrtimer(struct perf_event *event)
5663 struct hw_perf_event *hwc = &event->hw;
5665 if (!is_sampling_event(event))
5668 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5669 hwc->hrtimer.function = perf_swevent_hrtimer;
5672 * Since hrtimers have a fixed rate, we can do a static freq->period
5673 * mapping and avoid the whole period adjust feedback stuff.
5675 if (event->attr.freq) {
5676 long freq = event->attr.sample_freq;
5678 event->attr.sample_period = NSEC_PER_SEC / freq;
5679 hwc->sample_period = event->attr.sample_period;
5680 local64_set(&hwc->period_left, hwc->sample_period);
5681 event->attr.freq = 0;
5686 * Software event: cpu wall time clock
5689 static void cpu_clock_event_update(struct perf_event *event)
5694 now = local_clock();
5695 prev = local64_xchg(&event->hw.prev_count, now);
5696 local64_add(now - prev, &event->count);
5699 static void cpu_clock_event_start(struct perf_event *event, int flags)
5701 local64_set(&event->hw.prev_count, local_clock());
5702 perf_swevent_start_hrtimer(event);
5705 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5707 perf_swevent_cancel_hrtimer(event);
5708 cpu_clock_event_update(event);
5711 static int cpu_clock_event_add(struct perf_event *event, int flags)
5713 if (flags & PERF_EF_START)
5714 cpu_clock_event_start(event, flags);
5719 static void cpu_clock_event_del(struct perf_event *event, int flags)
5721 cpu_clock_event_stop(event, flags);
5724 static void cpu_clock_event_read(struct perf_event *event)
5726 cpu_clock_event_update(event);
5729 static int cpu_clock_event_init(struct perf_event *event)
5731 if (event->attr.type != PERF_TYPE_SOFTWARE)
5734 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5737 perf_swevent_init_hrtimer(event);
5742 static struct pmu perf_cpu_clock = {
5743 .task_ctx_nr = perf_sw_context,
5745 .event_init = cpu_clock_event_init,
5746 .add = cpu_clock_event_add,
5747 .del = cpu_clock_event_del,
5748 .start = cpu_clock_event_start,
5749 .stop = cpu_clock_event_stop,
5750 .read = cpu_clock_event_read,
5754 * Software event: task time clock
5757 static void task_clock_event_update(struct perf_event *event, u64 now)
5762 prev = local64_xchg(&event->hw.prev_count, now);
5764 local64_add(delta, &event->count);
5767 static void task_clock_event_start(struct perf_event *event, int flags)
5769 local64_set(&event->hw.prev_count, event->ctx->time);
5770 perf_swevent_start_hrtimer(event);
5773 static void task_clock_event_stop(struct perf_event *event, int flags)
5775 perf_swevent_cancel_hrtimer(event);
5776 task_clock_event_update(event, event->ctx->time);
5779 static int task_clock_event_add(struct perf_event *event, int flags)
5781 if (flags & PERF_EF_START)
5782 task_clock_event_start(event, flags);
5787 static void task_clock_event_del(struct perf_event *event, int flags)
5789 task_clock_event_stop(event, PERF_EF_UPDATE);
5792 static void task_clock_event_read(struct perf_event *event)
5797 update_context_time(event->ctx);
5798 update_cgrp_time_from_event(event);
5799 time = event->ctx->time;
5801 u64 now = perf_clock();
5802 u64 delta = now - event->ctx->timestamp;
5803 time = event->ctx->time + delta;
5806 task_clock_event_update(event, time);
5809 static int task_clock_event_init(struct perf_event *event)
5811 if (event->attr.type != PERF_TYPE_SOFTWARE)
5814 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5817 perf_swevent_init_hrtimer(event);
5822 static struct pmu perf_task_clock = {
5823 .task_ctx_nr = perf_sw_context,
5825 .event_init = task_clock_event_init,
5826 .add = task_clock_event_add,
5827 .del = task_clock_event_del,
5828 .start = task_clock_event_start,
5829 .stop = task_clock_event_stop,
5830 .read = task_clock_event_read,
5833 static void perf_pmu_nop_void(struct pmu *pmu)
5837 static int perf_pmu_nop_int(struct pmu *pmu)
5842 static void perf_pmu_start_txn(struct pmu *pmu)
5844 perf_pmu_disable(pmu);
5847 static int perf_pmu_commit_txn(struct pmu *pmu)
5849 perf_pmu_enable(pmu);
5853 static void perf_pmu_cancel_txn(struct pmu *pmu)
5855 perf_pmu_enable(pmu);
5859 * Ensures all contexts with the same task_ctx_nr have the same
5860 * pmu_cpu_context too.
5862 static void *find_pmu_context(int ctxn)
5869 list_for_each_entry(pmu, &pmus, entry) {
5870 if (pmu->task_ctx_nr == ctxn)
5871 return pmu->pmu_cpu_context;
5877 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5881 for_each_possible_cpu(cpu) {
5882 struct perf_cpu_context *cpuctx;
5884 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5886 if (cpuctx->active_pmu == old_pmu)
5887 cpuctx->active_pmu = pmu;
5891 static void free_pmu_context(struct pmu *pmu)
5895 mutex_lock(&pmus_lock);
5897 * Like a real lame refcount.
5899 list_for_each_entry(i, &pmus, entry) {
5900 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5901 update_pmu_context(i, pmu);
5906 free_percpu(pmu->pmu_cpu_context);
5908 mutex_unlock(&pmus_lock);
5910 static struct idr pmu_idr;
5913 type_show(struct device *dev, struct device_attribute *attr, char *page)
5915 struct pmu *pmu = dev_get_drvdata(dev);
5917 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5920 static struct device_attribute pmu_dev_attrs[] = {
5925 static int pmu_bus_running;
5926 static struct bus_type pmu_bus = {
5927 .name = "event_source",
5928 .dev_attrs = pmu_dev_attrs,
5931 static void pmu_dev_release(struct device *dev)
5936 static int pmu_dev_alloc(struct pmu *pmu)
5940 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5944 device_initialize(pmu->dev);
5945 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5949 dev_set_drvdata(pmu->dev, pmu);
5950 pmu->dev->bus = &pmu_bus;
5951 pmu->dev->release = pmu_dev_release;
5952 ret = device_add(pmu->dev);
5960 put_device(pmu->dev);
5964 static struct lock_class_key cpuctx_mutex;
5966 int perf_pmu_register(struct pmu *pmu, char *name, int type)
5970 mutex_lock(&pmus_lock);
5972 pmu->pmu_disable_count = alloc_percpu(int);
5973 if (!pmu->pmu_disable_count)
5982 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5986 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5994 if (pmu_bus_running) {
5995 ret = pmu_dev_alloc(pmu);
6001 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6002 if (pmu->pmu_cpu_context)
6003 goto got_cpu_context;
6005 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6006 if (!pmu->pmu_cpu_context)
6009 for_each_possible_cpu(cpu) {
6010 struct perf_cpu_context *cpuctx;
6012 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6013 __perf_event_init_context(&cpuctx->ctx);
6014 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
6015 cpuctx->ctx.type = cpu_context;
6016 cpuctx->ctx.pmu = pmu;
6017 cpuctx->jiffies_interval = 1;
6018 INIT_LIST_HEAD(&cpuctx->rotation_list);
6019 cpuctx->active_pmu = pmu;
6023 if (!pmu->start_txn) {
6024 if (pmu->pmu_enable) {
6026 * If we have pmu_enable/pmu_disable calls, install
6027 * transaction stubs that use that to try and batch
6028 * hardware accesses.
6030 pmu->start_txn = perf_pmu_start_txn;
6031 pmu->commit_txn = perf_pmu_commit_txn;
6032 pmu->cancel_txn = perf_pmu_cancel_txn;
6034 pmu->start_txn = perf_pmu_nop_void;
6035 pmu->commit_txn = perf_pmu_nop_int;
6036 pmu->cancel_txn = perf_pmu_nop_void;
6040 if (!pmu->pmu_enable) {
6041 pmu->pmu_enable = perf_pmu_nop_void;
6042 pmu->pmu_disable = perf_pmu_nop_void;
6045 list_add_rcu(&pmu->entry, &pmus);
6048 mutex_unlock(&pmus_lock);
6053 device_del(pmu->dev);
6054 put_device(pmu->dev);
6057 if (pmu->type >= PERF_TYPE_MAX)
6058 idr_remove(&pmu_idr, pmu->type);
6061 free_percpu(pmu->pmu_disable_count);
6065 void perf_pmu_unregister(struct pmu *pmu)
6067 mutex_lock(&pmus_lock);
6068 list_del_rcu(&pmu->entry);
6069 mutex_unlock(&pmus_lock);
6072 * We dereference the pmu list under both SRCU and regular RCU, so
6073 * synchronize against both of those.
6075 synchronize_srcu(&pmus_srcu);
6078 free_percpu(pmu->pmu_disable_count);
6079 if (pmu->type >= PERF_TYPE_MAX)
6080 idr_remove(&pmu_idr, pmu->type);
6081 device_del(pmu->dev);
6082 put_device(pmu->dev);
6083 free_pmu_context(pmu);
6086 struct pmu *perf_init_event(struct perf_event *event)
6088 struct pmu *pmu = NULL;
6091 idx = srcu_read_lock(&pmus_srcu);
6094 pmu = idr_find(&pmu_idr, event->attr.type);
6099 list_for_each_entry_rcu(pmu, &pmus, entry) {
6100 int ret = pmu->event_init(event);
6104 if (ret != -ENOENT) {
6109 pmu = ERR_PTR(-ENOENT);
6111 srcu_read_unlock(&pmus_srcu, idx);
6117 * Allocate and initialize a event structure
6119 static struct perf_event *
6120 perf_event_alloc(struct perf_event_attr *attr, int cpu,
6121 struct task_struct *task,
6122 struct perf_event *group_leader,
6123 struct perf_event *parent_event,
6124 perf_overflow_handler_t overflow_handler)
6127 struct perf_event *event;
6128 struct hw_perf_event *hwc;
6131 if ((unsigned)cpu >= nr_cpu_ids) {
6132 if (!task || cpu != -1)
6133 return ERR_PTR(-EINVAL);
6136 event = kzalloc(sizeof(*event), GFP_KERNEL);
6138 return ERR_PTR(-ENOMEM);
6141 * Single events are their own group leaders, with an
6142 * empty sibling list:
6145 group_leader = event;
6147 mutex_init(&event->child_mutex);
6148 INIT_LIST_HEAD(&event->child_list);
6150 INIT_LIST_HEAD(&event->group_entry);
6151 INIT_LIST_HEAD(&event->event_entry);
6152 INIT_LIST_HEAD(&event->sibling_list);
6153 init_waitqueue_head(&event->waitq);
6154 init_irq_work(&event->pending, perf_pending_event);
6156 mutex_init(&event->mmap_mutex);
6159 event->attr = *attr;
6160 event->group_leader = group_leader;
6164 event->parent = parent_event;
6166 event->ns = get_pid_ns(current->nsproxy->pid_ns);
6167 event->id = atomic64_inc_return(&perf_event_id);
6169 event->state = PERF_EVENT_STATE_INACTIVE;
6172 event->attach_state = PERF_ATTACH_TASK;
6173 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6175 * hw_breakpoint is a bit difficult here..
6177 if (attr->type == PERF_TYPE_BREAKPOINT)
6178 event->hw.bp_target = task;
6182 if (!overflow_handler && parent_event)
6183 overflow_handler = parent_event->overflow_handler;
6185 event->overflow_handler = overflow_handler;
6188 event->state = PERF_EVENT_STATE_OFF;
6193 hwc->sample_period = attr->sample_period;
6194 if (attr->freq && attr->sample_freq)
6195 hwc->sample_period = 1;
6196 hwc->last_period = hwc->sample_period;
6198 local64_set(&hwc->period_left, hwc->sample_period);
6201 * we currently do not support PERF_FORMAT_GROUP on inherited events
6203 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6206 pmu = perf_init_event(event);
6212 else if (IS_ERR(pmu))
6217 put_pid_ns(event->ns);
6219 return ERR_PTR(err);
6224 if (!event->parent) {
6225 if (event->attach_state & PERF_ATTACH_TASK)
6226 jump_label_inc(&perf_sched_events);
6227 if (event->attr.mmap || event->attr.mmap_data)
6228 atomic_inc(&nr_mmap_events);
6229 if (event->attr.comm)
6230 atomic_inc(&nr_comm_events);
6231 if (event->attr.task)
6232 atomic_inc(&nr_task_events);
6233 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6234 err = get_callchain_buffers();
6237 return ERR_PTR(err);
6245 static int perf_copy_attr(struct perf_event_attr __user *uattr,
6246 struct perf_event_attr *attr)
6251 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6255 * zero the full structure, so that a short copy will be nice.
6257 memset(attr, 0, sizeof(*attr));
6259 ret = get_user(size, &uattr->size);
6263 if (size > PAGE_SIZE) /* silly large */
6266 if (!size) /* abi compat */
6267 size = PERF_ATTR_SIZE_VER0;
6269 if (size < PERF_ATTR_SIZE_VER0)
6273 * If we're handed a bigger struct than we know of,
6274 * ensure all the unknown bits are 0 - i.e. new
6275 * user-space does not rely on any kernel feature
6276 * extensions we dont know about yet.
6278 if (size > sizeof(*attr)) {
6279 unsigned char __user *addr;
6280 unsigned char __user *end;
6283 addr = (void __user *)uattr + sizeof(*attr);
6284 end = (void __user *)uattr + size;
6286 for (; addr < end; addr++) {
6287 ret = get_user(val, addr);
6293 size = sizeof(*attr);
6296 ret = copy_from_user(attr, uattr, size);
6301 * If the type exists, the corresponding creation will verify
6304 if (attr->type >= PERF_TYPE_MAX)
6307 if (attr->__reserved_1)
6310 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6313 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6320 put_user(sizeof(*attr), &uattr->size);
6326 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6328 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
6334 /* don't allow circular references */
6335 if (event == output_event)
6339 * Don't allow cross-cpu buffers
6341 if (output_event->cpu != event->cpu)
6345 * If its not a per-cpu buffer, it must be the same task.
6347 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6351 mutex_lock(&event->mmap_mutex);
6352 /* Can't redirect output if we've got an active mmap() */
6353 if (atomic_read(&event->mmap_count))
6357 /* get the buffer we want to redirect to */
6358 buffer = perf_buffer_get(output_event);
6363 old_buffer = event->buffer;
6364 rcu_assign_pointer(event->buffer, buffer);
6367 mutex_unlock(&event->mmap_mutex);
6370 perf_buffer_put(old_buffer);
6376 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6378 * @attr_uptr: event_id type attributes for monitoring/sampling
6381 * @group_fd: group leader event fd
6383 SYSCALL_DEFINE5(perf_event_open,
6384 struct perf_event_attr __user *, attr_uptr,
6385 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6387 struct perf_event *group_leader = NULL, *output_event = NULL;
6388 struct perf_event *event, *sibling;
6389 struct perf_event_attr attr;
6390 struct perf_event_context *ctx;
6391 struct file *event_file = NULL;
6392 struct file *group_file = NULL;
6393 struct task_struct *task = NULL;
6397 int fput_needed = 0;
6400 /* for future expandability... */
6401 if (flags & ~PERF_FLAG_ALL)
6404 err = perf_copy_attr(attr_uptr, &attr);
6408 if (!attr.exclude_kernel) {
6409 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6414 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6419 * In cgroup mode, the pid argument is used to pass the fd
6420 * opened to the cgroup directory in cgroupfs. The cpu argument
6421 * designates the cpu on which to monitor threads from that
6424 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6427 event_fd = get_unused_fd_flags(O_RDWR);
6431 if (group_fd != -1) {
6432 group_leader = perf_fget_light(group_fd, &fput_needed);
6433 if (IS_ERR(group_leader)) {
6434 err = PTR_ERR(group_leader);
6437 group_file = group_leader->filp;
6438 if (flags & PERF_FLAG_FD_OUTPUT)
6439 output_event = group_leader;
6440 if (flags & PERF_FLAG_FD_NO_GROUP)
6441 group_leader = NULL;
6444 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
6445 task = find_lively_task_by_vpid(pid);
6447 err = PTR_ERR(task);
6452 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
6453 if (IS_ERR(event)) {
6454 err = PTR_ERR(event);
6458 if (flags & PERF_FLAG_PID_CGROUP) {
6459 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6465 * Special case software events and allow them to be part of
6466 * any hardware group.
6471 (is_software_event(event) != is_software_event(group_leader))) {
6472 if (is_software_event(event)) {
6474 * If event and group_leader are not both a software
6475 * event, and event is, then group leader is not.
6477 * Allow the addition of software events to !software
6478 * groups, this is safe because software events never
6481 pmu = group_leader->pmu;
6482 } else if (is_software_event(group_leader) &&
6483 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6485 * In case the group is a pure software group, and we
6486 * try to add a hardware event, move the whole group to
6487 * the hardware context.
6494 * Get the target context (task or percpu):
6496 ctx = find_get_context(pmu, task, cpu);
6503 * Look up the group leader (we will attach this event to it):
6509 * Do not allow a recursive hierarchy (this new sibling
6510 * becoming part of another group-sibling):
6512 if (group_leader->group_leader != group_leader)
6515 * Do not allow to attach to a group in a different
6516 * task or CPU context:
6519 if (group_leader->ctx->type != ctx->type)
6522 if (group_leader->ctx != ctx)
6527 * Only a group leader can be exclusive or pinned
6529 if (attr.exclusive || attr.pinned)
6534 err = perf_event_set_output(event, output_event);
6539 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6540 if (IS_ERR(event_file)) {
6541 err = PTR_ERR(event_file);
6546 struct perf_event_context *gctx = group_leader->ctx;
6548 mutex_lock(&gctx->mutex);
6549 perf_remove_from_context(group_leader);
6550 list_for_each_entry(sibling, &group_leader->sibling_list,
6552 perf_remove_from_context(sibling);
6555 mutex_unlock(&gctx->mutex);
6559 event->filp = event_file;
6560 WARN_ON_ONCE(ctx->parent_ctx);
6561 mutex_lock(&ctx->mutex);
6564 perf_install_in_context(ctx, group_leader, cpu);
6566 list_for_each_entry(sibling, &group_leader->sibling_list,
6568 perf_install_in_context(ctx, sibling, cpu);
6573 perf_install_in_context(ctx, event, cpu);
6575 perf_unpin_context(ctx);
6576 mutex_unlock(&ctx->mutex);
6578 event->owner = current;
6580 mutex_lock(¤t->perf_event_mutex);
6581 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6582 mutex_unlock(¤t->perf_event_mutex);
6585 * Precalculate sample_data sizes
6587 perf_event__header_size(event);
6588 perf_event__id_header_size(event);
6591 * Drop the reference on the group_event after placing the
6592 * new event on the sibling_list. This ensures destruction
6593 * of the group leader will find the pointer to itself in
6594 * perf_group_detach().
6596 fput_light(group_file, fput_needed);
6597 fd_install(event_fd, event_file);
6601 perf_unpin_context(ctx);
6607 put_task_struct(task);
6609 fput_light(group_file, fput_needed);
6611 put_unused_fd(event_fd);
6616 * perf_event_create_kernel_counter
6618 * @attr: attributes of the counter to create
6619 * @cpu: cpu in which the counter is bound
6620 * @task: task to profile (NULL for percpu)
6623 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6624 struct task_struct *task,
6625 perf_overflow_handler_t overflow_handler)
6627 struct perf_event_context *ctx;
6628 struct perf_event *event;
6632 * Get the target context (task or percpu):
6635 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
6636 if (IS_ERR(event)) {
6637 err = PTR_ERR(event);
6641 ctx = find_get_context(event->pmu, task, cpu);
6648 WARN_ON_ONCE(ctx->parent_ctx);
6649 mutex_lock(&ctx->mutex);
6650 perf_install_in_context(ctx, event, cpu);
6652 perf_unpin_context(ctx);
6653 mutex_unlock(&ctx->mutex);
6660 return ERR_PTR(err);
6662 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6664 static void sync_child_event(struct perf_event *child_event,
6665 struct task_struct *child)
6667 struct perf_event *parent_event = child_event->parent;
6670 if (child_event->attr.inherit_stat)
6671 perf_event_read_event(child_event, child);
6673 child_val = perf_event_count(child_event);
6676 * Add back the child's count to the parent's count:
6678 atomic64_add(child_val, &parent_event->child_count);
6679 atomic64_add(child_event->total_time_enabled,
6680 &parent_event->child_total_time_enabled);
6681 atomic64_add(child_event->total_time_running,
6682 &parent_event->child_total_time_running);
6685 * Remove this event from the parent's list
6687 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6688 mutex_lock(&parent_event->child_mutex);
6689 list_del_init(&child_event->child_list);
6690 mutex_unlock(&parent_event->child_mutex);
6693 * Release the parent event, if this was the last
6696 fput(parent_event->filp);
6700 __perf_event_exit_task(struct perf_event *child_event,
6701 struct perf_event_context *child_ctx,
6702 struct task_struct *child)
6704 struct perf_event *parent_event;
6706 perf_remove_from_context(child_event);
6708 parent_event = child_event->parent;
6710 * It can happen that parent exits first, and has events
6711 * that are still around due to the child reference. These
6712 * events need to be zapped - but otherwise linger.
6715 sync_child_event(child_event, child);
6716 free_event(child_event);
6720 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6722 struct perf_event *child_event, *tmp;
6723 struct perf_event_context *child_ctx;
6724 unsigned long flags;
6726 if (likely(!child->perf_event_ctxp[ctxn])) {
6727 perf_event_task(child, NULL, 0);
6731 local_irq_save(flags);
6733 * We can't reschedule here because interrupts are disabled,
6734 * and either child is current or it is a task that can't be
6735 * scheduled, so we are now safe from rescheduling changing
6738 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6739 task_ctx_sched_out(child_ctx, EVENT_ALL);
6742 * Take the context lock here so that if find_get_context is
6743 * reading child->perf_event_ctxp, we wait until it has
6744 * incremented the context's refcount before we do put_ctx below.
6746 raw_spin_lock(&child_ctx->lock);
6747 child->perf_event_ctxp[ctxn] = NULL;
6749 * If this context is a clone; unclone it so it can't get
6750 * swapped to another process while we're removing all
6751 * the events from it.
6753 unclone_ctx(child_ctx);
6754 update_context_time(child_ctx);
6755 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6758 * Report the task dead after unscheduling the events so that we
6759 * won't get any samples after PERF_RECORD_EXIT. We can however still
6760 * get a few PERF_RECORD_READ events.
6762 perf_event_task(child, child_ctx, 0);
6765 * We can recurse on the same lock type through:
6767 * __perf_event_exit_task()
6768 * sync_child_event()
6769 * fput(parent_event->filp)
6771 * mutex_lock(&ctx->mutex)
6773 * But since its the parent context it won't be the same instance.
6775 mutex_lock(&child_ctx->mutex);
6778 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6780 __perf_event_exit_task(child_event, child_ctx, child);
6782 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6784 __perf_event_exit_task(child_event, child_ctx, child);
6787 * If the last event was a group event, it will have appended all
6788 * its siblings to the list, but we obtained 'tmp' before that which
6789 * will still point to the list head terminating the iteration.
6791 if (!list_empty(&child_ctx->pinned_groups) ||
6792 !list_empty(&child_ctx->flexible_groups))
6795 mutex_unlock(&child_ctx->mutex);
6801 * When a child task exits, feed back event values to parent events.
6803 void perf_event_exit_task(struct task_struct *child)
6805 struct perf_event *event, *tmp;
6808 mutex_lock(&child->perf_event_mutex);
6809 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6811 list_del_init(&event->owner_entry);
6814 * Ensure the list deletion is visible before we clear
6815 * the owner, closes a race against perf_release() where
6816 * we need to serialize on the owner->perf_event_mutex.
6819 event->owner = NULL;
6821 mutex_unlock(&child->perf_event_mutex);
6823 for_each_task_context_nr(ctxn)
6824 perf_event_exit_task_context(child, ctxn);
6827 static void perf_free_event(struct perf_event *event,
6828 struct perf_event_context *ctx)
6830 struct perf_event *parent = event->parent;
6832 if (WARN_ON_ONCE(!parent))
6835 mutex_lock(&parent->child_mutex);
6836 list_del_init(&event->child_list);
6837 mutex_unlock(&parent->child_mutex);
6841 perf_group_detach(event);
6842 list_del_event(event, ctx);
6847 * free an unexposed, unused context as created by inheritance by
6848 * perf_event_init_task below, used by fork() in case of fail.
6850 void perf_event_free_task(struct task_struct *task)
6852 struct perf_event_context *ctx;
6853 struct perf_event *event, *tmp;
6856 for_each_task_context_nr(ctxn) {
6857 ctx = task->perf_event_ctxp[ctxn];
6861 mutex_lock(&ctx->mutex);
6863 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6865 perf_free_event(event, ctx);
6867 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6869 perf_free_event(event, ctx);
6871 if (!list_empty(&ctx->pinned_groups) ||
6872 !list_empty(&ctx->flexible_groups))
6875 mutex_unlock(&ctx->mutex);
6881 void perf_event_delayed_put(struct task_struct *task)
6885 for_each_task_context_nr(ctxn)
6886 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6890 * inherit a event from parent task to child task:
6892 static struct perf_event *
6893 inherit_event(struct perf_event *parent_event,
6894 struct task_struct *parent,
6895 struct perf_event_context *parent_ctx,
6896 struct task_struct *child,
6897 struct perf_event *group_leader,
6898 struct perf_event_context *child_ctx)
6900 struct perf_event *child_event;
6901 unsigned long flags;
6904 * Instead of creating recursive hierarchies of events,
6905 * we link inherited events back to the original parent,
6906 * which has a filp for sure, which we use as the reference
6909 if (parent_event->parent)
6910 parent_event = parent_event->parent;
6912 child_event = perf_event_alloc(&parent_event->attr,
6915 group_leader, parent_event,
6917 if (IS_ERR(child_event))
6922 * Make the child state follow the state of the parent event,
6923 * not its attr.disabled bit. We hold the parent's mutex,
6924 * so we won't race with perf_event_{en, dis}able_family.
6926 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6927 child_event->state = PERF_EVENT_STATE_INACTIVE;
6929 child_event->state = PERF_EVENT_STATE_OFF;
6931 if (parent_event->attr.freq) {
6932 u64 sample_period = parent_event->hw.sample_period;
6933 struct hw_perf_event *hwc = &child_event->hw;
6935 hwc->sample_period = sample_period;
6936 hwc->last_period = sample_period;
6938 local64_set(&hwc->period_left, sample_period);
6941 child_event->ctx = child_ctx;
6942 child_event->overflow_handler = parent_event->overflow_handler;
6945 * Precalculate sample_data sizes
6947 perf_event__header_size(child_event);
6948 perf_event__id_header_size(child_event);
6951 * Link it up in the child's context:
6953 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6954 add_event_to_ctx(child_event, child_ctx);
6955 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6958 * Get a reference to the parent filp - we will fput it
6959 * when the child event exits. This is safe to do because
6960 * we are in the parent and we know that the filp still
6961 * exists and has a nonzero count:
6963 atomic_long_inc(&parent_event->filp->f_count);
6966 * Link this into the parent event's child list
6968 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6969 mutex_lock(&parent_event->child_mutex);
6970 list_add_tail(&child_event->child_list, &parent_event->child_list);
6971 mutex_unlock(&parent_event->child_mutex);
6976 static int inherit_group(struct perf_event *parent_event,
6977 struct task_struct *parent,
6978 struct perf_event_context *parent_ctx,
6979 struct task_struct *child,
6980 struct perf_event_context *child_ctx)
6982 struct perf_event *leader;
6983 struct perf_event *sub;
6984 struct perf_event *child_ctr;
6986 leader = inherit_event(parent_event, parent, parent_ctx,
6987 child, NULL, child_ctx);
6989 return PTR_ERR(leader);
6990 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6991 child_ctr = inherit_event(sub, parent, parent_ctx,
6992 child, leader, child_ctx);
6993 if (IS_ERR(child_ctr))
6994 return PTR_ERR(child_ctr);
7000 inherit_task_group(struct perf_event *event, struct task_struct *parent,
7001 struct perf_event_context *parent_ctx,
7002 struct task_struct *child, int ctxn,
7006 struct perf_event_context *child_ctx;
7008 if (!event->attr.inherit) {
7013 child_ctx = child->perf_event_ctxp[ctxn];
7016 * This is executed from the parent task context, so
7017 * inherit events that have been marked for cloning.
7018 * First allocate and initialize a context for the
7022 child_ctx = alloc_perf_context(event->pmu, child);
7026 child->perf_event_ctxp[ctxn] = child_ctx;
7029 ret = inherit_group(event, parent, parent_ctx,
7039 * Initialize the perf_event context in task_struct
7041 int perf_event_init_context(struct task_struct *child, int ctxn)
7043 struct perf_event_context *child_ctx, *parent_ctx;
7044 struct perf_event_context *cloned_ctx;
7045 struct perf_event *event;
7046 struct task_struct *parent = current;
7047 int inherited_all = 1;
7048 unsigned long flags;
7051 if (likely(!parent->perf_event_ctxp[ctxn]))
7055 * If the parent's context is a clone, pin it so it won't get
7058 parent_ctx = perf_pin_task_context(parent, ctxn);
7061 * No need to check if parent_ctx != NULL here; since we saw
7062 * it non-NULL earlier, the only reason for it to become NULL
7063 * is if we exit, and since we're currently in the middle of
7064 * a fork we can't be exiting at the same time.
7068 * Lock the parent list. No need to lock the child - not PID
7069 * hashed yet and not running, so nobody can access it.
7071 mutex_lock(&parent_ctx->mutex);
7074 * We dont have to disable NMIs - we are only looking at
7075 * the list, not manipulating it:
7077 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
7078 ret = inherit_task_group(event, parent, parent_ctx,
7079 child, ctxn, &inherited_all);
7085 * We can't hold ctx->lock when iterating the ->flexible_group list due
7086 * to allocations, but we need to prevent rotation because
7087 * rotate_ctx() will change the list from interrupt context.
7089 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7090 parent_ctx->rotate_disable = 1;
7091 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7093 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
7094 ret = inherit_task_group(event, parent, parent_ctx,
7095 child, ctxn, &inherited_all);
7100 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7101 parent_ctx->rotate_disable = 0;
7103 child_ctx = child->perf_event_ctxp[ctxn];
7105 if (child_ctx && inherited_all) {
7107 * Mark the child context as a clone of the parent
7108 * context, or of whatever the parent is a clone of.
7110 * Note that if the parent is a clone, the holding of
7111 * parent_ctx->lock avoids it from being uncloned.
7113 cloned_ctx = parent_ctx->parent_ctx;
7115 child_ctx->parent_ctx = cloned_ctx;
7116 child_ctx->parent_gen = parent_ctx->parent_gen;
7118 child_ctx->parent_ctx = parent_ctx;
7119 child_ctx->parent_gen = parent_ctx->generation;
7121 get_ctx(child_ctx->parent_ctx);
7124 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7125 mutex_unlock(&parent_ctx->mutex);
7127 perf_unpin_context(parent_ctx);
7128 put_ctx(parent_ctx);
7134 * Initialize the perf_event context in task_struct
7136 int perf_event_init_task(struct task_struct *child)
7140 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7141 mutex_init(&child->perf_event_mutex);
7142 INIT_LIST_HEAD(&child->perf_event_list);
7144 for_each_task_context_nr(ctxn) {
7145 ret = perf_event_init_context(child, ctxn);
7153 static void __init perf_event_init_all_cpus(void)
7155 struct swevent_htable *swhash;
7158 for_each_possible_cpu(cpu) {
7159 swhash = &per_cpu(swevent_htable, cpu);
7160 mutex_init(&swhash->hlist_mutex);
7161 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
7165 static void __cpuinit perf_event_init_cpu(int cpu)
7167 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7169 mutex_lock(&swhash->hlist_mutex);
7170 if (swhash->hlist_refcount > 0) {
7171 struct swevent_hlist *hlist;
7173 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7175 rcu_assign_pointer(swhash->swevent_hlist, hlist);
7177 mutex_unlock(&swhash->hlist_mutex);
7180 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7181 static void perf_pmu_rotate_stop(struct pmu *pmu)
7183 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7185 WARN_ON(!irqs_disabled());
7187 list_del_init(&cpuctx->rotation_list);
7190 static void __perf_event_exit_context(void *__info)
7192 struct perf_event_context *ctx = __info;
7193 struct perf_event *event, *tmp;
7195 perf_pmu_rotate_stop(ctx->pmu);
7197 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
7198 __perf_remove_from_context(event);
7199 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
7200 __perf_remove_from_context(event);
7203 static void perf_event_exit_cpu_context(int cpu)
7205 struct perf_event_context *ctx;
7209 idx = srcu_read_lock(&pmus_srcu);
7210 list_for_each_entry_rcu(pmu, &pmus, entry) {
7211 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
7213 mutex_lock(&ctx->mutex);
7214 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7215 mutex_unlock(&ctx->mutex);
7217 srcu_read_unlock(&pmus_srcu, idx);
7220 static void perf_event_exit_cpu(int cpu)
7222 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7224 mutex_lock(&swhash->hlist_mutex);
7225 swevent_hlist_release(swhash);
7226 mutex_unlock(&swhash->hlist_mutex);
7228 perf_event_exit_cpu_context(cpu);
7231 static inline void perf_event_exit_cpu(int cpu) { }
7235 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7239 for_each_online_cpu(cpu)
7240 perf_event_exit_cpu(cpu);
7246 * Run the perf reboot notifier at the very last possible moment so that
7247 * the generic watchdog code runs as long as possible.
7249 static struct notifier_block perf_reboot_notifier = {
7250 .notifier_call = perf_reboot,
7251 .priority = INT_MIN,
7254 static int __cpuinit
7255 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7257 unsigned int cpu = (long)hcpu;
7259 switch (action & ~CPU_TASKS_FROZEN) {
7261 case CPU_UP_PREPARE:
7262 case CPU_DOWN_FAILED:
7263 perf_event_init_cpu(cpu);
7266 case CPU_UP_CANCELED:
7267 case CPU_DOWN_PREPARE:
7268 perf_event_exit_cpu(cpu);
7278 void __init perf_event_init(void)
7284 perf_event_init_all_cpus();
7285 init_srcu_struct(&pmus_srcu);
7286 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7287 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7288 perf_pmu_register(&perf_task_clock, NULL, -1);
7290 perf_cpu_notifier(perf_cpu_notify);
7291 register_reboot_notifier(&perf_reboot_notifier);
7293 ret = init_hw_breakpoint();
7294 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7297 static int __init perf_event_sysfs_init(void)
7302 mutex_lock(&pmus_lock);
7304 ret = bus_register(&pmu_bus);
7308 list_for_each_entry(pmu, &pmus, entry) {
7309 if (!pmu->name || pmu->type < 0)
7312 ret = pmu_dev_alloc(pmu);
7313 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7315 pmu_bus_running = 1;
7319 mutex_unlock(&pmus_lock);
7323 device_initcall(perf_event_sysfs_init);
7325 #ifdef CONFIG_CGROUP_PERF
7326 static struct cgroup_subsys_state *perf_cgroup_create(
7327 struct cgroup_subsys *ss, struct cgroup *cont)
7329 struct perf_cgroup *jc;
7330 struct perf_cgroup_info *t;
7333 jc = kmalloc(sizeof(*jc), GFP_KERNEL);
7335 return ERR_PTR(-ENOMEM);
7337 memset(jc, 0, sizeof(*jc));
7339 jc->info = alloc_percpu(struct perf_cgroup_info);
7342 return ERR_PTR(-ENOMEM);
7345 for_each_possible_cpu(c) {
7346 t = per_cpu_ptr(jc->info, c);
7353 static void perf_cgroup_destroy(struct cgroup_subsys *ss,
7354 struct cgroup *cont)
7356 struct perf_cgroup *jc;
7357 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7358 struct perf_cgroup, css);
7359 free_percpu(jc->info);
7363 static int __perf_cgroup_move(void *info)
7365 struct task_struct *task = info;
7366 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7370 static void perf_cgroup_move(struct task_struct *task)
7372 task_function_call(task, __perf_cgroup_move, task);
7375 static void perf_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
7376 struct cgroup *old_cgrp, struct task_struct *task,
7379 perf_cgroup_move(task);
7381 struct task_struct *c;
7383 list_for_each_entry_rcu(c, &task->thread_group, thread_group) {
7384 perf_cgroup_move(c);
7390 static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7391 struct cgroup *old_cgrp, struct task_struct *task)
7394 * cgroup_exit() is called in the copy_process() failure path.
7395 * Ignore this case since the task hasn't ran yet, this avoids
7396 * trying to poke a half freed task state from generic code.
7398 if (!(task->flags & PF_EXITING))
7401 perf_cgroup_move(task);
7404 struct cgroup_subsys perf_subsys = {
7405 .name = "perf_event",
7406 .subsys_id = perf_subsys_id,
7407 .create = perf_cgroup_create,
7408 .destroy = perf_cgroup_destroy,
7409 .exit = perf_cgroup_exit,
7410 .attach = perf_cgroup_attach,
7412 #endif /* CONFIG_CGROUP_PERF */