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/file.h>
17 #include <linux/poll.h>
18 #include <linux/slab.h>
19 #include <linux/hash.h>
20 #include <linux/sysfs.h>
21 #include <linux/dcache.h>
22 #include <linux/percpu.h>
23 #include <linux/ptrace.h>
24 #include <linux/vmstat.h>
25 #include <linux/vmalloc.h>
26 #include <linux/hardirq.h>
27 #include <linux/rculist.h>
28 #include <linux/uaccess.h>
29 #include <linux/syscalls.h>
30 #include <linux/anon_inodes.h>
31 #include <linux/kernel_stat.h>
32 #include <linux/perf_event.h>
33 #include <linux/ftrace_event.h>
35 #include <asm/irq_regs.h>
38 * Each CPU has a list of per CPU events:
40 static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
42 int perf_max_events __read_mostly = 1;
43 static int perf_reserved_percpu __read_mostly;
44 static int perf_overcommit __read_mostly = 1;
46 static atomic_t nr_events __read_mostly;
47 static atomic_t nr_mmap_events __read_mostly;
48 static atomic_t nr_comm_events __read_mostly;
49 static atomic_t nr_task_events __read_mostly;
52 * perf event paranoia level:
53 * -1 - not paranoid at all
54 * 0 - disallow raw tracepoint access for unpriv
55 * 1 - disallow cpu events for unpriv
56 * 2 - disallow kernel profiling for unpriv
58 int sysctl_perf_event_paranoid __read_mostly = 1;
60 int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
63 * max perf event sample rate
65 int sysctl_perf_event_sample_rate __read_mostly = 100000;
67 static atomic64_t perf_event_id;
70 * Lock for (sysadmin-configurable) event reservations:
72 static DEFINE_SPINLOCK(perf_resource_lock);
74 void __weak hw_perf_disable(void) { barrier(); }
75 void __weak hw_perf_enable(void) { barrier(); }
77 void __weak perf_event_print_debug(void) { }
79 static DEFINE_PER_CPU(int, perf_disable_count);
81 void perf_disable(void)
83 if (!__get_cpu_var(perf_disable_count)++)
87 void perf_enable(void)
89 if (!--__get_cpu_var(perf_disable_count))
93 static void get_ctx(struct perf_event_context *ctx)
95 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
98 static void free_ctx(struct rcu_head *head)
100 struct perf_event_context *ctx;
102 ctx = container_of(head, struct perf_event_context, rcu_head);
106 static void put_ctx(struct perf_event_context *ctx)
108 if (atomic_dec_and_test(&ctx->refcount)) {
110 put_ctx(ctx->parent_ctx);
112 put_task_struct(ctx->task);
113 call_rcu(&ctx->rcu_head, free_ctx);
117 static void unclone_ctx(struct perf_event_context *ctx)
119 if (ctx->parent_ctx) {
120 put_ctx(ctx->parent_ctx);
121 ctx->parent_ctx = NULL;
126 * If we inherit events we want to return the parent event id
129 static u64 primary_event_id(struct perf_event *event)
134 id = event->parent->id;
140 * Get the perf_event_context for a task and lock it.
141 * This has to cope with with the fact that until it is locked,
142 * the context could get moved to another task.
144 static struct perf_event_context *
145 perf_lock_task_context(struct task_struct *task, unsigned long *flags)
147 struct perf_event_context *ctx;
151 ctx = rcu_dereference(task->perf_event_ctxp);
154 * If this context is a clone of another, it might
155 * get swapped for another underneath us by
156 * perf_event_task_sched_out, though the
157 * rcu_read_lock() protects us from any context
158 * getting freed. Lock the context and check if it
159 * got swapped before we could get the lock, and retry
160 * if so. If we locked the right context, then it
161 * can't get swapped on us any more.
163 raw_spin_lock_irqsave(&ctx->lock, *flags);
164 if (ctx != rcu_dereference(task->perf_event_ctxp)) {
165 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
169 if (!atomic_inc_not_zero(&ctx->refcount)) {
170 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
179 * Get the context for a task and increment its pin_count so it
180 * can't get swapped to another task. This also increments its
181 * reference count so that the context can't get freed.
183 static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
185 struct perf_event_context *ctx;
188 ctx = perf_lock_task_context(task, &flags);
191 raw_spin_unlock_irqrestore(&ctx->lock, flags);
196 static void perf_unpin_context(struct perf_event_context *ctx)
200 raw_spin_lock_irqsave(&ctx->lock, flags);
202 raw_spin_unlock_irqrestore(&ctx->lock, flags);
206 static inline u64 perf_clock(void)
208 return local_clock();
212 * Update the record of the current time in a context.
214 static void update_context_time(struct perf_event_context *ctx)
216 u64 now = perf_clock();
218 ctx->time += now - ctx->timestamp;
219 ctx->timestamp = now;
223 * Update the total_time_enabled and total_time_running fields for a event.
225 static void update_event_times(struct perf_event *event)
227 struct perf_event_context *ctx = event->ctx;
230 if (event->state < PERF_EVENT_STATE_INACTIVE ||
231 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
237 run_end = event->tstamp_stopped;
239 event->total_time_enabled = run_end - event->tstamp_enabled;
241 if (event->state == PERF_EVENT_STATE_INACTIVE)
242 run_end = event->tstamp_stopped;
246 event->total_time_running = run_end - event->tstamp_running;
250 * Update total_time_enabled and total_time_running for all events in a group.
252 static void update_group_times(struct perf_event *leader)
254 struct perf_event *event;
256 update_event_times(leader);
257 list_for_each_entry(event, &leader->sibling_list, group_entry)
258 update_event_times(event);
261 static struct list_head *
262 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
264 if (event->attr.pinned)
265 return &ctx->pinned_groups;
267 return &ctx->flexible_groups;
271 * Add a event from the lists for its context.
272 * Must be called with ctx->mutex and ctx->lock held.
275 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
277 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
278 event->attach_state |= PERF_ATTACH_CONTEXT;
281 * If we're a stand alone event or group leader, we go to the context
282 * list, group events are kept attached to the group so that
283 * perf_group_detach can, at all times, locate all siblings.
285 if (event->group_leader == event) {
286 struct list_head *list;
288 if (is_software_event(event))
289 event->group_flags |= PERF_GROUP_SOFTWARE;
291 list = ctx_group_list(event, ctx);
292 list_add_tail(&event->group_entry, list);
295 list_add_rcu(&event->event_entry, &ctx->event_list);
297 if (event->attr.inherit_stat)
301 static void perf_group_attach(struct perf_event *event)
303 struct perf_event *group_leader = event->group_leader;
305 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_GROUP);
306 event->attach_state |= PERF_ATTACH_GROUP;
308 if (group_leader == event)
311 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
312 !is_software_event(event))
313 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
315 list_add_tail(&event->group_entry, &group_leader->sibling_list);
316 group_leader->nr_siblings++;
320 * Remove a event from the lists for its context.
321 * Must be called with ctx->mutex and ctx->lock held.
324 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
327 * We can have double detach due to exit/hot-unplug + close.
329 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
332 event->attach_state &= ~PERF_ATTACH_CONTEXT;
335 if (event->attr.inherit_stat)
338 list_del_rcu(&event->event_entry);
340 if (event->group_leader == event)
341 list_del_init(&event->group_entry);
343 update_group_times(event);
346 * If event was in error state, then keep it
347 * that way, otherwise bogus counts will be
348 * returned on read(). The only way to get out
349 * of error state is by explicit re-enabling
352 if (event->state > PERF_EVENT_STATE_OFF)
353 event->state = PERF_EVENT_STATE_OFF;
356 static void perf_group_detach(struct perf_event *event)
358 struct perf_event *sibling, *tmp;
359 struct list_head *list = NULL;
362 * We can have double detach due to exit/hot-unplug + close.
364 if (!(event->attach_state & PERF_ATTACH_GROUP))
367 event->attach_state &= ~PERF_ATTACH_GROUP;
370 * If this is a sibling, remove it from its group.
372 if (event->group_leader != event) {
373 list_del_init(&event->group_entry);
374 event->group_leader->nr_siblings--;
378 if (!list_empty(&event->group_entry))
379 list = &event->group_entry;
382 * If this was a group event with sibling events then
383 * upgrade the siblings to singleton events by adding them
384 * to whatever list we are on.
386 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
388 list_move_tail(&sibling->group_entry, list);
389 sibling->group_leader = sibling;
391 /* Inherit group flags from the previous leader */
392 sibling->group_flags = event->group_flags;
397 event_filter_match(struct perf_event *event)
399 return event->cpu == -1 || event->cpu == smp_processor_id();
403 event_sched_out(struct perf_event *event,
404 struct perf_cpu_context *cpuctx,
405 struct perf_event_context *ctx)
409 * An event which could not be activated because of
410 * filter mismatch still needs to have its timings
411 * maintained, otherwise bogus information is return
412 * via read() for time_enabled, time_running:
414 if (event->state == PERF_EVENT_STATE_INACTIVE
415 && !event_filter_match(event)) {
416 delta = ctx->time - event->tstamp_stopped;
417 event->tstamp_running += delta;
418 event->tstamp_stopped = ctx->time;
421 if (event->state != PERF_EVENT_STATE_ACTIVE)
424 event->state = PERF_EVENT_STATE_INACTIVE;
425 if (event->pending_disable) {
426 event->pending_disable = 0;
427 event->state = PERF_EVENT_STATE_OFF;
429 event->tstamp_stopped = ctx->time;
430 event->pmu->disable(event);
433 if (!is_software_event(event))
434 cpuctx->active_oncpu--;
436 if (event->attr.exclusive || !cpuctx->active_oncpu)
437 cpuctx->exclusive = 0;
441 group_sched_out(struct perf_event *group_event,
442 struct perf_cpu_context *cpuctx,
443 struct perf_event_context *ctx)
445 struct perf_event *event;
446 int state = group_event->state;
448 event_sched_out(group_event, cpuctx, ctx);
451 * Schedule out siblings (if any):
453 list_for_each_entry(event, &group_event->sibling_list, group_entry)
454 event_sched_out(event, cpuctx, ctx);
456 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
457 cpuctx->exclusive = 0;
461 * Cross CPU call to remove a performance event
463 * We disable the event on the hardware level first. After that we
464 * remove it from the context list.
466 static void __perf_event_remove_from_context(void *info)
468 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
469 struct perf_event *event = info;
470 struct perf_event_context *ctx = event->ctx;
473 * If this is a task context, we need to check whether it is
474 * the current task context of this cpu. If not it has been
475 * scheduled out before the smp call arrived.
477 if (ctx->task && cpuctx->task_ctx != ctx)
480 raw_spin_lock(&ctx->lock);
482 * Protect the list operation against NMI by disabling the
483 * events on a global level.
487 event_sched_out(event, cpuctx, ctx);
489 list_del_event(event, ctx);
493 * Allow more per task events with respect to the
496 cpuctx->max_pertask =
497 min(perf_max_events - ctx->nr_events,
498 perf_max_events - perf_reserved_percpu);
502 raw_spin_unlock(&ctx->lock);
507 * Remove the event from a task's (or a CPU's) list of events.
509 * Must be called with ctx->mutex held.
511 * CPU events are removed with a smp call. For task events we only
512 * call when the task is on a CPU.
514 * If event->ctx is a cloned context, callers must make sure that
515 * every task struct that event->ctx->task could possibly point to
516 * remains valid. This is OK when called from perf_release since
517 * that only calls us on the top-level context, which can't be a clone.
518 * When called from perf_event_exit_task, it's OK because the
519 * context has been detached from its task.
521 static void perf_event_remove_from_context(struct perf_event *event)
523 struct perf_event_context *ctx = event->ctx;
524 struct task_struct *task = ctx->task;
528 * Per cpu events are removed via an smp call and
529 * the removal is always successful.
531 smp_call_function_single(event->cpu,
532 __perf_event_remove_from_context,
538 task_oncpu_function_call(task, __perf_event_remove_from_context,
541 raw_spin_lock_irq(&ctx->lock);
543 * If the context is active we need to retry the smp call.
545 if (ctx->nr_active && !list_empty(&event->group_entry)) {
546 raw_spin_unlock_irq(&ctx->lock);
551 * The lock prevents that this context is scheduled in so we
552 * can remove the event safely, if the call above did not
555 if (!list_empty(&event->group_entry))
556 list_del_event(event, ctx);
557 raw_spin_unlock_irq(&ctx->lock);
561 * Cross CPU call to disable a performance event
563 static void __perf_event_disable(void *info)
565 struct perf_event *event = info;
566 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
567 struct perf_event_context *ctx = event->ctx;
570 * If this is a per-task event, need to check whether this
571 * event's task is the current task on this cpu.
573 if (ctx->task && cpuctx->task_ctx != ctx)
576 raw_spin_lock(&ctx->lock);
579 * If the event is on, turn it off.
580 * If it is in error state, leave it in error state.
582 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
583 update_context_time(ctx);
584 update_group_times(event);
585 if (event == event->group_leader)
586 group_sched_out(event, cpuctx, ctx);
588 event_sched_out(event, cpuctx, ctx);
589 event->state = PERF_EVENT_STATE_OFF;
592 raw_spin_unlock(&ctx->lock);
598 * If event->ctx is a cloned context, callers must make sure that
599 * every task struct that event->ctx->task could possibly point to
600 * remains valid. This condition is satisifed when called through
601 * perf_event_for_each_child or perf_event_for_each because they
602 * hold the top-level event's child_mutex, so any descendant that
603 * goes to exit will block in sync_child_event.
604 * When called from perf_pending_event it's OK because event->ctx
605 * is the current context on this CPU and preemption is disabled,
606 * hence we can't get into perf_event_task_sched_out for this context.
608 void perf_event_disable(struct perf_event *event)
610 struct perf_event_context *ctx = event->ctx;
611 struct task_struct *task = ctx->task;
615 * Disable the event on the cpu that it's on
617 smp_call_function_single(event->cpu, __perf_event_disable,
623 task_oncpu_function_call(task, __perf_event_disable, event);
625 raw_spin_lock_irq(&ctx->lock);
627 * If the event is still active, we need to retry the cross-call.
629 if (event->state == PERF_EVENT_STATE_ACTIVE) {
630 raw_spin_unlock_irq(&ctx->lock);
635 * Since we have the lock this context can't be scheduled
636 * in, so we can change the state safely.
638 if (event->state == PERF_EVENT_STATE_INACTIVE) {
639 update_group_times(event);
640 event->state = PERF_EVENT_STATE_OFF;
643 raw_spin_unlock_irq(&ctx->lock);
647 event_sched_in(struct perf_event *event,
648 struct perf_cpu_context *cpuctx,
649 struct perf_event_context *ctx)
651 if (event->state <= PERF_EVENT_STATE_OFF)
654 event->state = PERF_EVENT_STATE_ACTIVE;
655 event->oncpu = smp_processor_id();
657 * The new state must be visible before we turn it on in the hardware:
661 if (event->pmu->enable(event)) {
662 event->state = PERF_EVENT_STATE_INACTIVE;
667 event->tstamp_running += ctx->time - event->tstamp_stopped;
669 if (!is_software_event(event))
670 cpuctx->active_oncpu++;
673 if (event->attr.exclusive)
674 cpuctx->exclusive = 1;
680 group_sched_in(struct perf_event *group_event,
681 struct perf_cpu_context *cpuctx,
682 struct perf_event_context *ctx)
684 struct perf_event *event, *partial_group = NULL;
685 struct pmu *pmu = group_event->pmu;
688 if (group_event->state == PERF_EVENT_STATE_OFF)
691 /* Check if group transaction availabe */
698 if (event_sched_in(group_event, cpuctx, ctx)) {
700 pmu->cancel_txn(pmu);
705 * Schedule in siblings as one group (if any):
707 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
708 if (event_sched_in(event, cpuctx, ctx)) {
709 partial_group = event;
714 if (!txn || !pmu->commit_txn(pmu))
719 * Groups can be scheduled in as one unit only, so undo any
720 * partial group before returning:
722 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
723 if (event == partial_group)
725 event_sched_out(event, cpuctx, ctx);
727 event_sched_out(group_event, cpuctx, ctx);
730 pmu->cancel_txn(pmu);
736 * Work out whether we can put this event group on the CPU now.
738 static int group_can_go_on(struct perf_event *event,
739 struct perf_cpu_context *cpuctx,
743 * Groups consisting entirely of software events can always go on.
745 if (event->group_flags & PERF_GROUP_SOFTWARE)
748 * If an exclusive group is already on, no other hardware
751 if (cpuctx->exclusive)
754 * If this group is exclusive and there are already
755 * events on the CPU, it can't go on.
757 if (event->attr.exclusive && cpuctx->active_oncpu)
760 * Otherwise, try to add it if all previous groups were able
766 static void add_event_to_ctx(struct perf_event *event,
767 struct perf_event_context *ctx)
769 list_add_event(event, ctx);
770 perf_group_attach(event);
771 event->tstamp_enabled = ctx->time;
772 event->tstamp_running = ctx->time;
773 event->tstamp_stopped = ctx->time;
777 * Cross CPU call to install and enable a performance event
779 * Must be called with ctx->mutex held
781 static void __perf_install_in_context(void *info)
783 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
784 struct perf_event *event = info;
785 struct perf_event_context *ctx = event->ctx;
786 struct perf_event *leader = event->group_leader;
790 * If this is a task context, we need to check whether it is
791 * the current task context of this cpu. If not it has been
792 * scheduled out before the smp call arrived.
793 * Or possibly this is the right context but it isn't
794 * on this cpu because it had no events.
796 if (ctx->task && cpuctx->task_ctx != ctx) {
797 if (cpuctx->task_ctx || ctx->task != current)
799 cpuctx->task_ctx = ctx;
802 raw_spin_lock(&ctx->lock);
804 update_context_time(ctx);
807 * Protect the list operation against NMI by disabling the
808 * events on a global level. NOP for non NMI based events.
812 add_event_to_ctx(event, ctx);
814 if (event->cpu != -1 && event->cpu != smp_processor_id())
818 * Don't put the event on if it is disabled or if
819 * it is in a group and the group isn't on.
821 if (event->state != PERF_EVENT_STATE_INACTIVE ||
822 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
826 * An exclusive event can't go on if there are already active
827 * hardware events, and no hardware event can go on if there
828 * is already an exclusive event on.
830 if (!group_can_go_on(event, cpuctx, 1))
833 err = event_sched_in(event, cpuctx, ctx);
837 * This event couldn't go on. If it is in a group
838 * then we have to pull the whole group off.
839 * If the event group is pinned then put it in error state.
842 group_sched_out(leader, cpuctx, ctx);
843 if (leader->attr.pinned) {
844 update_group_times(leader);
845 leader->state = PERF_EVENT_STATE_ERROR;
849 if (!err && !ctx->task && cpuctx->max_pertask)
850 cpuctx->max_pertask--;
855 raw_spin_unlock(&ctx->lock);
859 * Attach a performance event to a context
861 * First we add the event to the list with the hardware enable bit
862 * in event->hw_config cleared.
864 * If the event is attached to a task which is on a CPU we use a smp
865 * call to enable it in the task context. The task might have been
866 * scheduled away, but we check this in the smp call again.
868 * Must be called with ctx->mutex held.
871 perf_install_in_context(struct perf_event_context *ctx,
872 struct perf_event *event,
875 struct task_struct *task = ctx->task;
879 * Per cpu events are installed via an smp call and
880 * the install is always successful.
882 smp_call_function_single(cpu, __perf_install_in_context,
888 task_oncpu_function_call(task, __perf_install_in_context,
891 raw_spin_lock_irq(&ctx->lock);
893 * we need to retry the smp call.
895 if (ctx->is_active && list_empty(&event->group_entry)) {
896 raw_spin_unlock_irq(&ctx->lock);
901 * The lock prevents that this context is scheduled in so we
902 * can add the event safely, if it the call above did not
905 if (list_empty(&event->group_entry))
906 add_event_to_ctx(event, ctx);
907 raw_spin_unlock_irq(&ctx->lock);
911 * Put a event into inactive state and update time fields.
912 * Enabling the leader of a group effectively enables all
913 * the group members that aren't explicitly disabled, so we
914 * have to update their ->tstamp_enabled also.
915 * Note: this works for group members as well as group leaders
916 * since the non-leader members' sibling_lists will be empty.
918 static void __perf_event_mark_enabled(struct perf_event *event,
919 struct perf_event_context *ctx)
921 struct perf_event *sub;
923 event->state = PERF_EVENT_STATE_INACTIVE;
924 event->tstamp_enabled = ctx->time - event->total_time_enabled;
925 list_for_each_entry(sub, &event->sibling_list, group_entry)
926 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
927 sub->tstamp_enabled =
928 ctx->time - sub->total_time_enabled;
932 * Cross CPU call to enable a performance event
934 static void __perf_event_enable(void *info)
936 struct perf_event *event = info;
937 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
938 struct perf_event_context *ctx = event->ctx;
939 struct perf_event *leader = event->group_leader;
943 * If this is a per-task event, need to check whether this
944 * event's task is the current task on this cpu.
946 if (ctx->task && cpuctx->task_ctx != ctx) {
947 if (cpuctx->task_ctx || ctx->task != current)
949 cpuctx->task_ctx = ctx;
952 raw_spin_lock(&ctx->lock);
954 update_context_time(ctx);
956 if (event->state >= PERF_EVENT_STATE_INACTIVE)
958 __perf_event_mark_enabled(event, ctx);
960 if (event->cpu != -1 && event->cpu != smp_processor_id())
964 * If the event is in a group and isn't the group leader,
965 * then don't put it on unless the group is on.
967 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
970 if (!group_can_go_on(event, cpuctx, 1)) {
975 err = group_sched_in(event, cpuctx, ctx);
977 err = event_sched_in(event, cpuctx, ctx);
983 * If this event can't go on and it's part of a
984 * group, then the whole group has to come off.
987 group_sched_out(leader, cpuctx, ctx);
988 if (leader->attr.pinned) {
989 update_group_times(leader);
990 leader->state = PERF_EVENT_STATE_ERROR;
995 raw_spin_unlock(&ctx->lock);
1001 * If event->ctx is a cloned context, callers must make sure that
1002 * every task struct that event->ctx->task could possibly point to
1003 * remains valid. This condition is satisfied when called through
1004 * perf_event_for_each_child or perf_event_for_each as described
1005 * for perf_event_disable.
1007 void perf_event_enable(struct perf_event *event)
1009 struct perf_event_context *ctx = event->ctx;
1010 struct task_struct *task = ctx->task;
1014 * Enable the event on the cpu that it's on
1016 smp_call_function_single(event->cpu, __perf_event_enable,
1021 raw_spin_lock_irq(&ctx->lock);
1022 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1026 * If the event is in error state, clear that first.
1027 * That way, if we see the event in error state below, we
1028 * know that it has gone back into error state, as distinct
1029 * from the task having been scheduled away before the
1030 * cross-call arrived.
1032 if (event->state == PERF_EVENT_STATE_ERROR)
1033 event->state = PERF_EVENT_STATE_OFF;
1036 raw_spin_unlock_irq(&ctx->lock);
1037 task_oncpu_function_call(task, __perf_event_enable, event);
1039 raw_spin_lock_irq(&ctx->lock);
1042 * If the context is active and the event is still off,
1043 * we need to retry the cross-call.
1045 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1049 * Since we have the lock this context can't be scheduled
1050 * in, so we can change the state safely.
1052 if (event->state == PERF_EVENT_STATE_OFF)
1053 __perf_event_mark_enabled(event, ctx);
1056 raw_spin_unlock_irq(&ctx->lock);
1059 static int perf_event_refresh(struct perf_event *event, int refresh)
1062 * not supported on inherited events
1064 if (event->attr.inherit)
1067 atomic_add(refresh, &event->event_limit);
1068 perf_event_enable(event);
1074 EVENT_FLEXIBLE = 0x1,
1076 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1079 static void ctx_sched_out(struct perf_event_context *ctx,
1080 struct perf_cpu_context *cpuctx,
1081 enum event_type_t event_type)
1083 struct perf_event *event;
1085 raw_spin_lock(&ctx->lock);
1087 if (likely(!ctx->nr_events))
1089 update_context_time(ctx);
1092 if (!ctx->nr_active)
1095 if (event_type & EVENT_PINNED)
1096 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1097 group_sched_out(event, cpuctx, ctx);
1099 if (event_type & EVENT_FLEXIBLE)
1100 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1101 group_sched_out(event, cpuctx, ctx);
1106 raw_spin_unlock(&ctx->lock);
1110 * Test whether two contexts are equivalent, i.e. whether they
1111 * have both been cloned from the same version of the same context
1112 * and they both have the same number of enabled events.
1113 * If the number of enabled events is the same, then the set
1114 * of enabled events should be the same, because these are both
1115 * inherited contexts, therefore we can't access individual events
1116 * in them directly with an fd; we can only enable/disable all
1117 * events via prctl, or enable/disable all events in a family
1118 * via ioctl, which will have the same effect on both contexts.
1120 static int context_equiv(struct perf_event_context *ctx1,
1121 struct perf_event_context *ctx2)
1123 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1124 && ctx1->parent_gen == ctx2->parent_gen
1125 && !ctx1->pin_count && !ctx2->pin_count;
1128 static void __perf_event_sync_stat(struct perf_event *event,
1129 struct perf_event *next_event)
1133 if (!event->attr.inherit_stat)
1137 * Update the event value, we cannot use perf_event_read()
1138 * because we're in the middle of a context switch and have IRQs
1139 * disabled, which upsets smp_call_function_single(), however
1140 * we know the event must be on the current CPU, therefore we
1141 * don't need to use it.
1143 switch (event->state) {
1144 case PERF_EVENT_STATE_ACTIVE:
1145 event->pmu->read(event);
1148 case PERF_EVENT_STATE_INACTIVE:
1149 update_event_times(event);
1157 * In order to keep per-task stats reliable we need to flip the event
1158 * values when we flip the contexts.
1160 value = local64_read(&next_event->count);
1161 value = local64_xchg(&event->count, value);
1162 local64_set(&next_event->count, value);
1164 swap(event->total_time_enabled, next_event->total_time_enabled);
1165 swap(event->total_time_running, next_event->total_time_running);
1168 * Since we swizzled the values, update the user visible data too.
1170 perf_event_update_userpage(event);
1171 perf_event_update_userpage(next_event);
1174 #define list_next_entry(pos, member) \
1175 list_entry(pos->member.next, typeof(*pos), member)
1177 static void perf_event_sync_stat(struct perf_event_context *ctx,
1178 struct perf_event_context *next_ctx)
1180 struct perf_event *event, *next_event;
1185 update_context_time(ctx);
1187 event = list_first_entry(&ctx->event_list,
1188 struct perf_event, event_entry);
1190 next_event = list_first_entry(&next_ctx->event_list,
1191 struct perf_event, event_entry);
1193 while (&event->event_entry != &ctx->event_list &&
1194 &next_event->event_entry != &next_ctx->event_list) {
1196 __perf_event_sync_stat(event, next_event);
1198 event = list_next_entry(event, event_entry);
1199 next_event = list_next_entry(next_event, event_entry);
1204 * Called from scheduler to remove the events of the current task,
1205 * with interrupts disabled.
1207 * We stop each event and update the event value in event->count.
1209 * This does not protect us against NMI, but disable()
1210 * sets the disabled bit in the control field of event _before_
1211 * accessing the event control register. If a NMI hits, then it will
1212 * not restart the event.
1214 void perf_event_task_sched_out(struct task_struct *task,
1215 struct task_struct *next)
1217 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1218 struct perf_event_context *ctx = task->perf_event_ctxp;
1219 struct perf_event_context *next_ctx;
1220 struct perf_event_context *parent;
1223 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
1225 if (likely(!ctx || !cpuctx->task_ctx))
1229 parent = rcu_dereference(ctx->parent_ctx);
1230 next_ctx = next->perf_event_ctxp;
1231 if (parent && next_ctx &&
1232 rcu_dereference(next_ctx->parent_ctx) == parent) {
1234 * Looks like the two contexts are clones, so we might be
1235 * able to optimize the context switch. We lock both
1236 * contexts and check that they are clones under the
1237 * lock (including re-checking that neither has been
1238 * uncloned in the meantime). It doesn't matter which
1239 * order we take the locks because no other cpu could
1240 * be trying to lock both of these tasks.
1242 raw_spin_lock(&ctx->lock);
1243 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1244 if (context_equiv(ctx, next_ctx)) {
1246 * XXX do we need a memory barrier of sorts
1247 * wrt to rcu_dereference() of perf_event_ctxp
1249 task->perf_event_ctxp = next_ctx;
1250 next->perf_event_ctxp = ctx;
1252 next_ctx->task = task;
1255 perf_event_sync_stat(ctx, next_ctx);
1257 raw_spin_unlock(&next_ctx->lock);
1258 raw_spin_unlock(&ctx->lock);
1263 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1264 cpuctx->task_ctx = NULL;
1268 static void task_ctx_sched_out(struct perf_event_context *ctx,
1269 enum event_type_t event_type)
1271 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1273 if (!cpuctx->task_ctx)
1276 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1279 ctx_sched_out(ctx, cpuctx, event_type);
1280 cpuctx->task_ctx = NULL;
1284 * Called with IRQs disabled
1286 static void __perf_event_task_sched_out(struct perf_event_context *ctx)
1288 task_ctx_sched_out(ctx, EVENT_ALL);
1292 * Called with IRQs disabled
1294 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1295 enum event_type_t event_type)
1297 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
1301 ctx_pinned_sched_in(struct perf_event_context *ctx,
1302 struct perf_cpu_context *cpuctx)
1304 struct perf_event *event;
1306 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1307 if (event->state <= PERF_EVENT_STATE_OFF)
1309 if (event->cpu != -1 && event->cpu != smp_processor_id())
1312 if (group_can_go_on(event, cpuctx, 1))
1313 group_sched_in(event, cpuctx, ctx);
1316 * If this pinned group hasn't been scheduled,
1317 * put it in error state.
1319 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1320 update_group_times(event);
1321 event->state = PERF_EVENT_STATE_ERROR;
1327 ctx_flexible_sched_in(struct perf_event_context *ctx,
1328 struct perf_cpu_context *cpuctx)
1330 struct perf_event *event;
1333 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1334 /* Ignore events in OFF or ERROR state */
1335 if (event->state <= PERF_EVENT_STATE_OFF)
1338 * Listen to the 'cpu' scheduling filter constraint
1341 if (event->cpu != -1 && event->cpu != smp_processor_id())
1344 if (group_can_go_on(event, cpuctx, can_add_hw))
1345 if (group_sched_in(event, cpuctx, ctx))
1351 ctx_sched_in(struct perf_event_context *ctx,
1352 struct perf_cpu_context *cpuctx,
1353 enum event_type_t event_type)
1355 raw_spin_lock(&ctx->lock);
1357 if (likely(!ctx->nr_events))
1360 ctx->timestamp = perf_clock();
1365 * First go through the list and put on any pinned groups
1366 * in order to give them the best chance of going on.
1368 if (event_type & EVENT_PINNED)
1369 ctx_pinned_sched_in(ctx, cpuctx);
1371 /* Then walk through the lower prio flexible groups */
1372 if (event_type & EVENT_FLEXIBLE)
1373 ctx_flexible_sched_in(ctx, cpuctx);
1377 raw_spin_unlock(&ctx->lock);
1380 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1381 enum event_type_t event_type)
1383 struct perf_event_context *ctx = &cpuctx->ctx;
1385 ctx_sched_in(ctx, cpuctx, event_type);
1388 static void task_ctx_sched_in(struct task_struct *task,
1389 enum event_type_t event_type)
1391 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1392 struct perf_event_context *ctx = task->perf_event_ctxp;
1396 if (cpuctx->task_ctx == ctx)
1398 ctx_sched_in(ctx, cpuctx, event_type);
1399 cpuctx->task_ctx = ctx;
1402 * Called from scheduler to add the events of the current task
1403 * with interrupts disabled.
1405 * We restore the event value and then enable it.
1407 * This does not protect us against NMI, but enable()
1408 * sets the enabled bit in the control field of event _before_
1409 * accessing the event control register. If a NMI hits, then it will
1410 * keep the event running.
1412 void perf_event_task_sched_in(struct task_struct *task)
1414 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1415 struct perf_event_context *ctx = task->perf_event_ctxp;
1420 if (cpuctx->task_ctx == ctx)
1426 * We want to keep the following priority order:
1427 * cpu pinned (that don't need to move), task pinned,
1428 * cpu flexible, task flexible.
1430 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1432 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1433 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1434 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1436 cpuctx->task_ctx = ctx;
1441 #define MAX_INTERRUPTS (~0ULL)
1443 static void perf_log_throttle(struct perf_event *event, int enable);
1445 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1447 u64 frequency = event->attr.sample_freq;
1448 u64 sec = NSEC_PER_SEC;
1449 u64 divisor, dividend;
1451 int count_fls, nsec_fls, frequency_fls, sec_fls;
1453 count_fls = fls64(count);
1454 nsec_fls = fls64(nsec);
1455 frequency_fls = fls64(frequency);
1459 * We got @count in @nsec, with a target of sample_freq HZ
1460 * the target period becomes:
1463 * period = -------------------
1464 * @nsec * sample_freq
1469 * Reduce accuracy by one bit such that @a and @b converge
1470 * to a similar magnitude.
1472 #define REDUCE_FLS(a, b) \
1474 if (a##_fls > b##_fls) { \
1484 * Reduce accuracy until either term fits in a u64, then proceed with
1485 * the other, so that finally we can do a u64/u64 division.
1487 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1488 REDUCE_FLS(nsec, frequency);
1489 REDUCE_FLS(sec, count);
1492 if (count_fls + sec_fls > 64) {
1493 divisor = nsec * frequency;
1495 while (count_fls + sec_fls > 64) {
1496 REDUCE_FLS(count, sec);
1500 dividend = count * sec;
1502 dividend = count * sec;
1504 while (nsec_fls + frequency_fls > 64) {
1505 REDUCE_FLS(nsec, frequency);
1509 divisor = nsec * frequency;
1515 return div64_u64(dividend, divisor);
1518 static void perf_event_stop(struct perf_event *event)
1520 if (!event->pmu->stop)
1521 return event->pmu->disable(event);
1523 return event->pmu->stop(event);
1526 static int perf_event_start(struct perf_event *event)
1528 if (!event->pmu->start)
1529 return event->pmu->enable(event);
1531 return event->pmu->start(event);
1534 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
1536 struct hw_perf_event *hwc = &event->hw;
1537 s64 period, sample_period;
1540 period = perf_calculate_period(event, nsec, count);
1542 delta = (s64)(period - hwc->sample_period);
1543 delta = (delta + 7) / 8; /* low pass filter */
1545 sample_period = hwc->sample_period + delta;
1550 hwc->sample_period = sample_period;
1552 if (local64_read(&hwc->period_left) > 8*sample_period) {
1554 perf_event_stop(event);
1555 local64_set(&hwc->period_left, 0);
1556 perf_event_start(event);
1561 static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
1563 struct perf_event *event;
1564 struct hw_perf_event *hwc;
1565 u64 interrupts, now;
1568 raw_spin_lock(&ctx->lock);
1569 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
1570 if (event->state != PERF_EVENT_STATE_ACTIVE)
1573 if (event->cpu != -1 && event->cpu != smp_processor_id())
1578 interrupts = hwc->interrupts;
1579 hwc->interrupts = 0;
1582 * unthrottle events on the tick
1584 if (interrupts == MAX_INTERRUPTS) {
1585 perf_log_throttle(event, 1);
1587 event->pmu->unthrottle(event);
1591 if (!event->attr.freq || !event->attr.sample_freq)
1595 event->pmu->read(event);
1596 now = local64_read(&event->count);
1597 delta = now - hwc->freq_count_stamp;
1598 hwc->freq_count_stamp = now;
1601 perf_adjust_period(event, TICK_NSEC, delta);
1604 raw_spin_unlock(&ctx->lock);
1608 * Round-robin a context's events:
1610 static void rotate_ctx(struct perf_event_context *ctx)
1612 raw_spin_lock(&ctx->lock);
1614 /* Rotate the first entry last of non-pinned groups */
1615 list_rotate_left(&ctx->flexible_groups);
1617 raw_spin_unlock(&ctx->lock);
1620 void perf_event_task_tick(struct task_struct *curr)
1622 struct perf_cpu_context *cpuctx;
1623 struct perf_event_context *ctx;
1626 if (!atomic_read(&nr_events))
1629 cpuctx = &__get_cpu_var(perf_cpu_context);
1630 if (cpuctx->ctx.nr_events &&
1631 cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1634 ctx = curr->perf_event_ctxp;
1635 if (ctx && ctx->nr_events && ctx->nr_events != ctx->nr_active)
1638 perf_ctx_adjust_freq(&cpuctx->ctx);
1640 perf_ctx_adjust_freq(ctx);
1646 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1648 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
1650 rotate_ctx(&cpuctx->ctx);
1654 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1656 task_ctx_sched_in(curr, EVENT_FLEXIBLE);
1660 static int event_enable_on_exec(struct perf_event *event,
1661 struct perf_event_context *ctx)
1663 if (!event->attr.enable_on_exec)
1666 event->attr.enable_on_exec = 0;
1667 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1670 __perf_event_mark_enabled(event, ctx);
1676 * Enable all of a task's events that have been marked enable-on-exec.
1677 * This expects task == current.
1679 static void perf_event_enable_on_exec(struct task_struct *task)
1681 struct perf_event_context *ctx;
1682 struct perf_event *event;
1683 unsigned long flags;
1687 local_irq_save(flags);
1688 ctx = task->perf_event_ctxp;
1689 if (!ctx || !ctx->nr_events)
1692 __perf_event_task_sched_out(ctx);
1694 raw_spin_lock(&ctx->lock);
1696 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1697 ret = event_enable_on_exec(event, ctx);
1702 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1703 ret = event_enable_on_exec(event, ctx);
1709 * Unclone this context if we enabled any event.
1714 raw_spin_unlock(&ctx->lock);
1716 perf_event_task_sched_in(task);
1718 local_irq_restore(flags);
1722 * Cross CPU call to read the hardware event
1724 static void __perf_event_read(void *info)
1726 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1727 struct perf_event *event = info;
1728 struct perf_event_context *ctx = event->ctx;
1731 * If this is a task context, we need to check whether it is
1732 * the current task context of this cpu. If not it has been
1733 * scheduled out before the smp call arrived. In that case
1734 * event->count would have been updated to a recent sample
1735 * when the event was scheduled out.
1737 if (ctx->task && cpuctx->task_ctx != ctx)
1740 raw_spin_lock(&ctx->lock);
1741 update_context_time(ctx);
1742 update_event_times(event);
1743 raw_spin_unlock(&ctx->lock);
1745 event->pmu->read(event);
1748 static inline u64 perf_event_count(struct perf_event *event)
1750 return local64_read(&event->count) + atomic64_read(&event->child_count);
1753 static u64 perf_event_read(struct perf_event *event)
1756 * If event is enabled and currently active on a CPU, update the
1757 * value in the event structure:
1759 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1760 smp_call_function_single(event->oncpu,
1761 __perf_event_read, event, 1);
1762 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
1763 struct perf_event_context *ctx = event->ctx;
1764 unsigned long flags;
1766 raw_spin_lock_irqsave(&ctx->lock, flags);
1767 update_context_time(ctx);
1768 update_event_times(event);
1769 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1772 return perf_event_count(event);
1779 struct callchain_cpus_entries {
1780 struct rcu_head rcu_head;
1781 struct perf_callchain_entry *cpu_entries[0];
1784 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
1785 static atomic_t nr_callchain_events;
1786 static DEFINE_MUTEX(callchain_mutex);
1787 struct callchain_cpus_entries *callchain_cpus_entries;
1790 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1791 struct pt_regs *regs)
1795 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
1796 struct pt_regs *regs)
1800 static void release_callchain_buffers_rcu(struct rcu_head *head)
1802 struct callchain_cpus_entries *entries;
1805 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1807 for_each_possible_cpu(cpu)
1808 kfree(entries->cpu_entries[cpu]);
1813 static void release_callchain_buffers(void)
1815 struct callchain_cpus_entries *entries;
1817 entries = callchain_cpus_entries;
1818 rcu_assign_pointer(callchain_cpus_entries, NULL);
1819 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1822 static int alloc_callchain_buffers(void)
1826 struct callchain_cpus_entries *entries;
1829 * We can't use the percpu allocation API for data that can be
1830 * accessed from NMI. Use a temporary manual per cpu allocation
1831 * until that gets sorted out.
1833 size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
1834 num_possible_cpus();
1836 entries = kzalloc(size, GFP_KERNEL);
1840 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
1842 for_each_possible_cpu(cpu) {
1843 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
1845 if (!entries->cpu_entries[cpu])
1849 rcu_assign_pointer(callchain_cpus_entries, entries);
1854 for_each_possible_cpu(cpu)
1855 kfree(entries->cpu_entries[cpu]);
1861 static int get_callchain_buffers(void)
1866 mutex_lock(&callchain_mutex);
1868 count = atomic_inc_return(&nr_callchain_events);
1869 if (WARN_ON_ONCE(count < 1)) {
1875 /* If the allocation failed, give up */
1876 if (!callchain_cpus_entries)
1881 err = alloc_callchain_buffers();
1883 release_callchain_buffers();
1885 mutex_unlock(&callchain_mutex);
1890 static void put_callchain_buffers(void)
1892 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
1893 release_callchain_buffers();
1894 mutex_unlock(&callchain_mutex);
1898 static int get_recursion_context(int *recursion)
1906 else if (in_softirq())
1911 if (recursion[rctx])
1920 static inline void put_recursion_context(int *recursion, int rctx)
1926 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
1929 struct callchain_cpus_entries *entries;
1931 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
1935 entries = rcu_dereference(callchain_cpus_entries);
1939 cpu = smp_processor_id();
1941 return &entries->cpu_entries[cpu][*rctx];
1945 put_callchain_entry(int rctx)
1947 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
1950 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1953 struct perf_callchain_entry *entry;
1956 entry = get_callchain_entry(&rctx);
1965 if (!user_mode(regs)) {
1966 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
1967 perf_callchain_kernel(entry, regs);
1969 regs = task_pt_regs(current);
1975 perf_callchain_store(entry, PERF_CONTEXT_USER);
1976 perf_callchain_user(entry, regs);
1980 put_callchain_entry(rctx);
1986 * Initialize the perf_event context in a task_struct:
1989 __perf_event_init_context(struct perf_event_context *ctx,
1990 struct task_struct *task)
1992 raw_spin_lock_init(&ctx->lock);
1993 mutex_init(&ctx->mutex);
1994 INIT_LIST_HEAD(&ctx->pinned_groups);
1995 INIT_LIST_HEAD(&ctx->flexible_groups);
1996 INIT_LIST_HEAD(&ctx->event_list);
1997 atomic_set(&ctx->refcount, 1);
2001 static struct perf_event_context *find_get_context(pid_t pid, int cpu)
2003 struct perf_event_context *ctx;
2004 struct perf_cpu_context *cpuctx;
2005 struct task_struct *task;
2006 unsigned long flags;
2009 if (pid == -1 && cpu != -1) {
2010 /* Must be root to operate on a CPU event: */
2011 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2012 return ERR_PTR(-EACCES);
2014 if (cpu < 0 || cpu >= nr_cpumask_bits)
2015 return ERR_PTR(-EINVAL);
2018 * We could be clever and allow to attach a event to an
2019 * offline CPU and activate it when the CPU comes up, but
2022 if (!cpu_online(cpu))
2023 return ERR_PTR(-ENODEV);
2025 cpuctx = &per_cpu(perf_cpu_context, cpu);
2036 task = find_task_by_vpid(pid);
2038 get_task_struct(task);
2042 return ERR_PTR(-ESRCH);
2045 * Can't attach events to a dying task.
2048 if (task->flags & PF_EXITING)
2051 /* Reuse ptrace permission checks for now. */
2053 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2057 ctx = perf_lock_task_context(task, &flags);
2060 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2064 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2068 __perf_event_init_context(ctx, task);
2070 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
2072 * We raced with some other task; use
2073 * the context they set.
2078 get_task_struct(task);
2081 put_task_struct(task);
2085 put_task_struct(task);
2086 return ERR_PTR(err);
2089 static void perf_event_free_filter(struct perf_event *event);
2091 static void free_event_rcu(struct rcu_head *head)
2093 struct perf_event *event;
2095 event = container_of(head, struct perf_event, rcu_head);
2097 put_pid_ns(event->ns);
2098 perf_event_free_filter(event);
2102 static void perf_pending_sync(struct perf_event *event);
2103 static void perf_buffer_put(struct perf_buffer *buffer);
2105 static void free_event(struct perf_event *event)
2107 perf_pending_sync(event);
2109 if (!event->parent) {
2110 atomic_dec(&nr_events);
2111 if (event->attr.mmap || event->attr.mmap_data)
2112 atomic_dec(&nr_mmap_events);
2113 if (event->attr.comm)
2114 atomic_dec(&nr_comm_events);
2115 if (event->attr.task)
2116 atomic_dec(&nr_task_events);
2117 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2118 put_callchain_buffers();
2121 if (event->buffer) {
2122 perf_buffer_put(event->buffer);
2123 event->buffer = NULL;
2127 event->destroy(event);
2129 put_ctx(event->ctx);
2130 call_rcu(&event->rcu_head, free_event_rcu);
2133 int perf_event_release_kernel(struct perf_event *event)
2135 struct perf_event_context *ctx = event->ctx;
2138 * Remove from the PMU, can't get re-enabled since we got
2139 * here because the last ref went.
2141 perf_event_disable(event);
2143 WARN_ON_ONCE(ctx->parent_ctx);
2145 * There are two ways this annotation is useful:
2147 * 1) there is a lock recursion from perf_event_exit_task
2148 * see the comment there.
2150 * 2) there is a lock-inversion with mmap_sem through
2151 * perf_event_read_group(), which takes faults while
2152 * holding ctx->mutex, however this is called after
2153 * the last filedesc died, so there is no possibility
2154 * to trigger the AB-BA case.
2156 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2157 raw_spin_lock_irq(&ctx->lock);
2158 perf_group_detach(event);
2159 list_del_event(event, ctx);
2160 raw_spin_unlock_irq(&ctx->lock);
2161 mutex_unlock(&ctx->mutex);
2163 mutex_lock(&event->owner->perf_event_mutex);
2164 list_del_init(&event->owner_entry);
2165 mutex_unlock(&event->owner->perf_event_mutex);
2166 put_task_struct(event->owner);
2172 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2175 * Called when the last reference to the file is gone.
2177 static int perf_release(struct inode *inode, struct file *file)
2179 struct perf_event *event = file->private_data;
2181 file->private_data = NULL;
2183 return perf_event_release_kernel(event);
2186 static int perf_event_read_size(struct perf_event *event)
2188 int entry = sizeof(u64); /* value */
2192 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2193 size += sizeof(u64);
2195 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2196 size += sizeof(u64);
2198 if (event->attr.read_format & PERF_FORMAT_ID)
2199 entry += sizeof(u64);
2201 if (event->attr.read_format & PERF_FORMAT_GROUP) {
2202 nr += event->group_leader->nr_siblings;
2203 size += sizeof(u64);
2211 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
2213 struct perf_event *child;
2219 mutex_lock(&event->child_mutex);
2220 total += perf_event_read(event);
2221 *enabled += event->total_time_enabled +
2222 atomic64_read(&event->child_total_time_enabled);
2223 *running += event->total_time_running +
2224 atomic64_read(&event->child_total_time_running);
2226 list_for_each_entry(child, &event->child_list, child_list) {
2227 total += perf_event_read(child);
2228 *enabled += child->total_time_enabled;
2229 *running += child->total_time_running;
2231 mutex_unlock(&event->child_mutex);
2235 EXPORT_SYMBOL_GPL(perf_event_read_value);
2237 static int perf_event_read_group(struct perf_event *event,
2238 u64 read_format, char __user *buf)
2240 struct perf_event *leader = event->group_leader, *sub;
2241 int n = 0, size = 0, ret = -EFAULT;
2242 struct perf_event_context *ctx = leader->ctx;
2244 u64 count, enabled, running;
2246 mutex_lock(&ctx->mutex);
2247 count = perf_event_read_value(leader, &enabled, &running);
2249 values[n++] = 1 + leader->nr_siblings;
2250 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2251 values[n++] = enabled;
2252 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2253 values[n++] = running;
2254 values[n++] = count;
2255 if (read_format & PERF_FORMAT_ID)
2256 values[n++] = primary_event_id(leader);
2258 size = n * sizeof(u64);
2260 if (copy_to_user(buf, values, size))
2265 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2268 values[n++] = perf_event_read_value(sub, &enabled, &running);
2269 if (read_format & PERF_FORMAT_ID)
2270 values[n++] = primary_event_id(sub);
2272 size = n * sizeof(u64);
2274 if (copy_to_user(buf + ret, values, size)) {
2282 mutex_unlock(&ctx->mutex);
2287 static int perf_event_read_one(struct perf_event *event,
2288 u64 read_format, char __user *buf)
2290 u64 enabled, running;
2294 values[n++] = perf_event_read_value(event, &enabled, &running);
2295 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2296 values[n++] = enabled;
2297 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2298 values[n++] = running;
2299 if (read_format & PERF_FORMAT_ID)
2300 values[n++] = primary_event_id(event);
2302 if (copy_to_user(buf, values, n * sizeof(u64)))
2305 return n * sizeof(u64);
2309 * Read the performance event - simple non blocking version for now
2312 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2314 u64 read_format = event->attr.read_format;
2318 * Return end-of-file for a read on a event that is in
2319 * error state (i.e. because it was pinned but it couldn't be
2320 * scheduled on to the CPU at some point).
2322 if (event->state == PERF_EVENT_STATE_ERROR)
2325 if (count < perf_event_read_size(event))
2328 WARN_ON_ONCE(event->ctx->parent_ctx);
2329 if (read_format & PERF_FORMAT_GROUP)
2330 ret = perf_event_read_group(event, read_format, buf);
2332 ret = perf_event_read_one(event, read_format, buf);
2338 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2340 struct perf_event *event = file->private_data;
2342 return perf_read_hw(event, buf, count);
2345 static unsigned int perf_poll(struct file *file, poll_table *wait)
2347 struct perf_event *event = file->private_data;
2348 struct perf_buffer *buffer;
2349 unsigned int events = POLL_HUP;
2352 buffer = rcu_dereference(event->buffer);
2354 events = atomic_xchg(&buffer->poll, 0);
2357 poll_wait(file, &event->waitq, wait);
2362 static void perf_event_reset(struct perf_event *event)
2364 (void)perf_event_read(event);
2365 local64_set(&event->count, 0);
2366 perf_event_update_userpage(event);
2370 * Holding the top-level event's child_mutex means that any
2371 * descendant process that has inherited this event will block
2372 * in sync_child_event if it goes to exit, thus satisfying the
2373 * task existence requirements of perf_event_enable/disable.
2375 static void perf_event_for_each_child(struct perf_event *event,
2376 void (*func)(struct perf_event *))
2378 struct perf_event *child;
2380 WARN_ON_ONCE(event->ctx->parent_ctx);
2381 mutex_lock(&event->child_mutex);
2383 list_for_each_entry(child, &event->child_list, child_list)
2385 mutex_unlock(&event->child_mutex);
2388 static void perf_event_for_each(struct perf_event *event,
2389 void (*func)(struct perf_event *))
2391 struct perf_event_context *ctx = event->ctx;
2392 struct perf_event *sibling;
2394 WARN_ON_ONCE(ctx->parent_ctx);
2395 mutex_lock(&ctx->mutex);
2396 event = event->group_leader;
2398 perf_event_for_each_child(event, func);
2400 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2401 perf_event_for_each_child(event, func);
2402 mutex_unlock(&ctx->mutex);
2405 static int perf_event_period(struct perf_event *event, u64 __user *arg)
2407 struct perf_event_context *ctx = event->ctx;
2412 if (!event->attr.sample_period)
2415 size = copy_from_user(&value, arg, sizeof(value));
2416 if (size != sizeof(value))
2422 raw_spin_lock_irq(&ctx->lock);
2423 if (event->attr.freq) {
2424 if (value > sysctl_perf_event_sample_rate) {
2429 event->attr.sample_freq = value;
2431 event->attr.sample_period = value;
2432 event->hw.sample_period = value;
2435 raw_spin_unlock_irq(&ctx->lock);
2440 static const struct file_operations perf_fops;
2442 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2446 file = fget_light(fd, fput_needed);
2448 return ERR_PTR(-EBADF);
2450 if (file->f_op != &perf_fops) {
2451 fput_light(file, *fput_needed);
2453 return ERR_PTR(-EBADF);
2456 return file->private_data;
2459 static int perf_event_set_output(struct perf_event *event,
2460 struct perf_event *output_event);
2461 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
2463 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2465 struct perf_event *event = file->private_data;
2466 void (*func)(struct perf_event *);
2470 case PERF_EVENT_IOC_ENABLE:
2471 func = perf_event_enable;
2473 case PERF_EVENT_IOC_DISABLE:
2474 func = perf_event_disable;
2476 case PERF_EVENT_IOC_RESET:
2477 func = perf_event_reset;
2480 case PERF_EVENT_IOC_REFRESH:
2481 return perf_event_refresh(event, arg);
2483 case PERF_EVENT_IOC_PERIOD:
2484 return perf_event_period(event, (u64 __user *)arg);
2486 case PERF_EVENT_IOC_SET_OUTPUT:
2488 struct perf_event *output_event = NULL;
2489 int fput_needed = 0;
2493 output_event = perf_fget_light(arg, &fput_needed);
2494 if (IS_ERR(output_event))
2495 return PTR_ERR(output_event);
2498 ret = perf_event_set_output(event, output_event);
2500 fput_light(output_event->filp, fput_needed);
2505 case PERF_EVENT_IOC_SET_FILTER:
2506 return perf_event_set_filter(event, (void __user *)arg);
2512 if (flags & PERF_IOC_FLAG_GROUP)
2513 perf_event_for_each(event, func);
2515 perf_event_for_each_child(event, func);
2520 int perf_event_task_enable(void)
2522 struct perf_event *event;
2524 mutex_lock(¤t->perf_event_mutex);
2525 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2526 perf_event_for_each_child(event, perf_event_enable);
2527 mutex_unlock(¤t->perf_event_mutex);
2532 int perf_event_task_disable(void)
2534 struct perf_event *event;
2536 mutex_lock(¤t->perf_event_mutex);
2537 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
2538 perf_event_for_each_child(event, perf_event_disable);
2539 mutex_unlock(¤t->perf_event_mutex);
2544 #ifndef PERF_EVENT_INDEX_OFFSET
2545 # define PERF_EVENT_INDEX_OFFSET 0
2548 static int perf_event_index(struct perf_event *event)
2550 if (event->state != PERF_EVENT_STATE_ACTIVE)
2553 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2557 * Callers need to ensure there can be no nesting of this function, otherwise
2558 * the seqlock logic goes bad. We can not serialize this because the arch
2559 * code calls this from NMI context.
2561 void perf_event_update_userpage(struct perf_event *event)
2563 struct perf_event_mmap_page *userpg;
2564 struct perf_buffer *buffer;
2567 buffer = rcu_dereference(event->buffer);
2571 userpg = buffer->user_page;
2574 * Disable preemption so as to not let the corresponding user-space
2575 * spin too long if we get preempted.
2580 userpg->index = perf_event_index(event);
2581 userpg->offset = perf_event_count(event);
2582 if (event->state == PERF_EVENT_STATE_ACTIVE)
2583 userpg->offset -= local64_read(&event->hw.prev_count);
2585 userpg->time_enabled = event->total_time_enabled +
2586 atomic64_read(&event->child_total_time_enabled);
2588 userpg->time_running = event->total_time_running +
2589 atomic64_read(&event->child_total_time_running);
2598 static unsigned long perf_data_size(struct perf_buffer *buffer);
2601 perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2603 long max_size = perf_data_size(buffer);
2606 buffer->watermark = min(max_size, watermark);
2608 if (!buffer->watermark)
2609 buffer->watermark = max_size / 2;
2611 if (flags & PERF_BUFFER_WRITABLE)
2612 buffer->writable = 1;
2614 atomic_set(&buffer->refcount, 1);
2617 #ifndef CONFIG_PERF_USE_VMALLOC
2620 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2623 static struct page *
2624 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2626 if (pgoff > buffer->nr_pages)
2630 return virt_to_page(buffer->user_page);
2632 return virt_to_page(buffer->data_pages[pgoff - 1]);
2635 static void *perf_mmap_alloc_page(int cpu)
2640 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2641 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2645 return page_address(page);
2648 static struct perf_buffer *
2649 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2651 struct perf_buffer *buffer;
2655 size = sizeof(struct perf_buffer);
2656 size += nr_pages * sizeof(void *);
2658 buffer = kzalloc(size, GFP_KERNEL);
2662 buffer->user_page = perf_mmap_alloc_page(cpu);
2663 if (!buffer->user_page)
2664 goto fail_user_page;
2666 for (i = 0; i < nr_pages; i++) {
2667 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
2668 if (!buffer->data_pages[i])
2669 goto fail_data_pages;
2672 buffer->nr_pages = nr_pages;
2674 perf_buffer_init(buffer, watermark, flags);
2679 for (i--; i >= 0; i--)
2680 free_page((unsigned long)buffer->data_pages[i]);
2682 free_page((unsigned long)buffer->user_page);
2691 static void perf_mmap_free_page(unsigned long addr)
2693 struct page *page = virt_to_page((void *)addr);
2695 page->mapping = NULL;
2699 static void perf_buffer_free(struct perf_buffer *buffer)
2703 perf_mmap_free_page((unsigned long)buffer->user_page);
2704 for (i = 0; i < buffer->nr_pages; i++)
2705 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2709 static inline int page_order(struct perf_buffer *buffer)
2717 * Back perf_mmap() with vmalloc memory.
2719 * Required for architectures that have d-cache aliasing issues.
2722 static inline int page_order(struct perf_buffer *buffer)
2724 return buffer->page_order;
2727 static struct page *
2728 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
2730 if (pgoff > (1UL << page_order(buffer)))
2733 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
2736 static void perf_mmap_unmark_page(void *addr)
2738 struct page *page = vmalloc_to_page(addr);
2740 page->mapping = NULL;
2743 static void perf_buffer_free_work(struct work_struct *work)
2745 struct perf_buffer *buffer;
2749 buffer = container_of(work, struct perf_buffer, work);
2750 nr = 1 << page_order(buffer);
2752 base = buffer->user_page;
2753 for (i = 0; i < nr + 1; i++)
2754 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2760 static void perf_buffer_free(struct perf_buffer *buffer)
2762 schedule_work(&buffer->work);
2765 static struct perf_buffer *
2766 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
2768 struct perf_buffer *buffer;
2772 size = sizeof(struct perf_buffer);
2773 size += sizeof(void *);
2775 buffer = kzalloc(size, GFP_KERNEL);
2779 INIT_WORK(&buffer->work, perf_buffer_free_work);
2781 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2785 buffer->user_page = all_buf;
2786 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2787 buffer->page_order = ilog2(nr_pages);
2788 buffer->nr_pages = 1;
2790 perf_buffer_init(buffer, watermark, flags);
2803 static unsigned long perf_data_size(struct perf_buffer *buffer)
2805 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
2808 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2810 struct perf_event *event = vma->vm_file->private_data;
2811 struct perf_buffer *buffer;
2812 int ret = VM_FAULT_SIGBUS;
2814 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2815 if (vmf->pgoff == 0)
2821 buffer = rcu_dereference(event->buffer);
2825 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2828 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
2832 get_page(vmf->page);
2833 vmf->page->mapping = vma->vm_file->f_mapping;
2834 vmf->page->index = vmf->pgoff;
2843 static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
2845 struct perf_buffer *buffer;
2847 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
2848 perf_buffer_free(buffer);
2851 static struct perf_buffer *perf_buffer_get(struct perf_event *event)
2853 struct perf_buffer *buffer;
2856 buffer = rcu_dereference(event->buffer);
2858 if (!atomic_inc_not_zero(&buffer->refcount))
2866 static void perf_buffer_put(struct perf_buffer *buffer)
2868 if (!atomic_dec_and_test(&buffer->refcount))
2871 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
2874 static void perf_mmap_open(struct vm_area_struct *vma)
2876 struct perf_event *event = vma->vm_file->private_data;
2878 atomic_inc(&event->mmap_count);
2881 static void perf_mmap_close(struct vm_area_struct *vma)
2883 struct perf_event *event = vma->vm_file->private_data;
2885 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
2886 unsigned long size = perf_data_size(event->buffer);
2887 struct user_struct *user = event->mmap_user;
2888 struct perf_buffer *buffer = event->buffer;
2890 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
2891 vma->vm_mm->locked_vm -= event->mmap_locked;
2892 rcu_assign_pointer(event->buffer, NULL);
2893 mutex_unlock(&event->mmap_mutex);
2895 perf_buffer_put(buffer);
2900 static const struct vm_operations_struct perf_mmap_vmops = {
2901 .open = perf_mmap_open,
2902 .close = perf_mmap_close,
2903 .fault = perf_mmap_fault,
2904 .page_mkwrite = perf_mmap_fault,
2907 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2909 struct perf_event *event = file->private_data;
2910 unsigned long user_locked, user_lock_limit;
2911 struct user_struct *user = current_user();
2912 unsigned long locked, lock_limit;
2913 struct perf_buffer *buffer;
2914 unsigned long vma_size;
2915 unsigned long nr_pages;
2916 long user_extra, extra;
2917 int ret = 0, flags = 0;
2920 * Don't allow mmap() of inherited per-task counters. This would
2921 * create a performance issue due to all children writing to the
2924 if (event->cpu == -1 && event->attr.inherit)
2927 if (!(vma->vm_flags & VM_SHARED))
2930 vma_size = vma->vm_end - vma->vm_start;
2931 nr_pages = (vma_size / PAGE_SIZE) - 1;
2934 * If we have buffer pages ensure they're a power-of-two number, so we
2935 * can do bitmasks instead of modulo.
2937 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2940 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2943 if (vma->vm_pgoff != 0)
2946 WARN_ON_ONCE(event->ctx->parent_ctx);
2947 mutex_lock(&event->mmap_mutex);
2948 if (event->buffer) {
2949 if (event->buffer->nr_pages == nr_pages)
2950 atomic_inc(&event->buffer->refcount);
2956 user_extra = nr_pages + 1;
2957 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2960 * Increase the limit linearly with more CPUs:
2962 user_lock_limit *= num_online_cpus();
2964 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2967 if (user_locked > user_lock_limit)
2968 extra = user_locked - user_lock_limit;
2970 lock_limit = rlimit(RLIMIT_MEMLOCK);
2971 lock_limit >>= PAGE_SHIFT;
2972 locked = vma->vm_mm->locked_vm + extra;
2974 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2975 !capable(CAP_IPC_LOCK)) {
2980 WARN_ON(event->buffer);
2982 if (vma->vm_flags & VM_WRITE)
2983 flags |= PERF_BUFFER_WRITABLE;
2985 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
2991 rcu_assign_pointer(event->buffer, buffer);
2993 atomic_long_add(user_extra, &user->locked_vm);
2994 event->mmap_locked = extra;
2995 event->mmap_user = get_current_user();
2996 vma->vm_mm->locked_vm += event->mmap_locked;
3000 atomic_inc(&event->mmap_count);
3001 mutex_unlock(&event->mmap_mutex);
3003 vma->vm_flags |= VM_RESERVED;
3004 vma->vm_ops = &perf_mmap_vmops;
3009 static int perf_fasync(int fd, struct file *filp, int on)
3011 struct inode *inode = filp->f_path.dentry->d_inode;
3012 struct perf_event *event = filp->private_data;
3015 mutex_lock(&inode->i_mutex);
3016 retval = fasync_helper(fd, filp, on, &event->fasync);
3017 mutex_unlock(&inode->i_mutex);
3025 static const struct file_operations perf_fops = {
3026 .llseek = no_llseek,
3027 .release = perf_release,
3030 .unlocked_ioctl = perf_ioctl,
3031 .compat_ioctl = perf_ioctl,
3033 .fasync = perf_fasync,
3039 * If there's data, ensure we set the poll() state and publish everything
3040 * to user-space before waking everybody up.
3043 void perf_event_wakeup(struct perf_event *event)
3045 wake_up_all(&event->waitq);
3047 if (event->pending_kill) {
3048 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3049 event->pending_kill = 0;
3056 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
3058 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
3059 * single linked list and use cmpxchg() to add entries lockless.
3062 static void perf_pending_event(struct perf_pending_entry *entry)
3064 struct perf_event *event = container_of(entry,
3065 struct perf_event, pending);
3067 if (event->pending_disable) {
3068 event->pending_disable = 0;
3069 __perf_event_disable(event);
3072 if (event->pending_wakeup) {
3073 event->pending_wakeup = 0;
3074 perf_event_wakeup(event);
3078 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
3080 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
3084 static void perf_pending_queue(struct perf_pending_entry *entry,
3085 void (*func)(struct perf_pending_entry *))
3087 struct perf_pending_entry **head;
3089 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
3094 head = &get_cpu_var(perf_pending_head);
3097 entry->next = *head;
3098 } while (cmpxchg(head, entry->next, entry) != entry->next);
3100 set_perf_event_pending();
3102 put_cpu_var(perf_pending_head);
3105 static int __perf_pending_run(void)
3107 struct perf_pending_entry *list;
3110 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
3111 while (list != PENDING_TAIL) {
3112 void (*func)(struct perf_pending_entry *);
3113 struct perf_pending_entry *entry = list;
3120 * Ensure we observe the unqueue before we issue the wakeup,
3121 * so that we won't be waiting forever.
3122 * -- see perf_not_pending().
3133 static inline int perf_not_pending(struct perf_event *event)
3136 * If we flush on whatever cpu we run, there is a chance we don't
3140 __perf_pending_run();
3144 * Ensure we see the proper queue state before going to sleep
3145 * so that we do not miss the wakeup. -- see perf_pending_handle()
3148 return event->pending.next == NULL;
3151 static void perf_pending_sync(struct perf_event *event)
3153 wait_event(event->waitq, perf_not_pending(event));
3156 void perf_event_do_pending(void)
3158 __perf_pending_run();
3162 * We assume there is only KVM supporting the callbacks.
3163 * Later on, we might change it to a list if there is
3164 * another virtualization implementation supporting the callbacks.
3166 struct perf_guest_info_callbacks *perf_guest_cbs;
3168 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3170 perf_guest_cbs = cbs;
3173 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3175 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3177 perf_guest_cbs = NULL;
3180 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3185 static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
3186 unsigned long offset, unsigned long head)
3190 if (!buffer->writable)
3193 mask = perf_data_size(buffer) - 1;
3195 offset = (offset - tail) & mask;
3196 head = (head - tail) & mask;
3198 if ((int)(head - offset) < 0)
3204 static void perf_output_wakeup(struct perf_output_handle *handle)
3206 atomic_set(&handle->buffer->poll, POLL_IN);
3209 handle->event->pending_wakeup = 1;
3210 perf_pending_queue(&handle->event->pending,
3211 perf_pending_event);
3213 perf_event_wakeup(handle->event);
3217 * We need to ensure a later event_id doesn't publish a head when a former
3218 * event isn't done writing. However since we need to deal with NMIs we
3219 * cannot fully serialize things.
3221 * We only publish the head (and generate a wakeup) when the outer-most
3224 static void perf_output_get_handle(struct perf_output_handle *handle)
3226 struct perf_buffer *buffer = handle->buffer;
3229 local_inc(&buffer->nest);
3230 handle->wakeup = local_read(&buffer->wakeup);
3233 static void perf_output_put_handle(struct perf_output_handle *handle)
3235 struct perf_buffer *buffer = handle->buffer;
3239 head = local_read(&buffer->head);
3242 * IRQ/NMI can happen here, which means we can miss a head update.
3245 if (!local_dec_and_test(&buffer->nest))
3249 * Publish the known good head. Rely on the full barrier implied
3250 * by atomic_dec_and_test() order the buffer->head read and this
3253 buffer->user_page->data_head = head;
3256 * Now check if we missed an update, rely on the (compiler)
3257 * barrier in atomic_dec_and_test() to re-read buffer->head.
3259 if (unlikely(head != local_read(&buffer->head))) {
3260 local_inc(&buffer->nest);
3264 if (handle->wakeup != local_read(&buffer->wakeup))
3265 perf_output_wakeup(handle);
3271 __always_inline void perf_output_copy(struct perf_output_handle *handle,
3272 const void *buf, unsigned int len)
3275 unsigned long size = min_t(unsigned long, handle->size, len);
3277 memcpy(handle->addr, buf, size);
3280 handle->addr += size;
3282 handle->size -= size;
3283 if (!handle->size) {
3284 struct perf_buffer *buffer = handle->buffer;
3287 handle->page &= buffer->nr_pages - 1;
3288 handle->addr = buffer->data_pages[handle->page];
3289 handle->size = PAGE_SIZE << page_order(buffer);
3294 int perf_output_begin(struct perf_output_handle *handle,
3295 struct perf_event *event, unsigned int size,
3296 int nmi, int sample)
3298 struct perf_buffer *buffer;
3299 unsigned long tail, offset, head;
3302 struct perf_event_header header;
3309 * For inherited events we send all the output towards the parent.
3312 event = event->parent;
3314 buffer = rcu_dereference(event->buffer);
3318 handle->buffer = buffer;
3319 handle->event = event;
3321 handle->sample = sample;
3323 if (!buffer->nr_pages)
3326 have_lost = local_read(&buffer->lost);
3328 size += sizeof(lost_event);
3330 perf_output_get_handle(handle);
3334 * Userspace could choose to issue a mb() before updating the
3335 * tail pointer. So that all reads will be completed before the
3338 tail = ACCESS_ONCE(buffer->user_page->data_tail);
3340 offset = head = local_read(&buffer->head);
3342 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
3344 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
3346 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3347 local_add(buffer->watermark, &buffer->wakeup);
3349 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3350 handle->page &= buffer->nr_pages - 1;
3351 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3352 handle->addr = buffer->data_pages[handle->page];
3353 handle->addr += handle->size;
3354 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
3357 lost_event.header.type = PERF_RECORD_LOST;
3358 lost_event.header.misc = 0;
3359 lost_event.header.size = sizeof(lost_event);
3360 lost_event.id = event->id;
3361 lost_event.lost = local_xchg(&buffer->lost, 0);
3363 perf_output_put(handle, lost_event);
3369 local_inc(&buffer->lost);
3370 perf_output_put_handle(handle);
3377 void perf_output_end(struct perf_output_handle *handle)
3379 struct perf_event *event = handle->event;
3380 struct perf_buffer *buffer = handle->buffer;
3382 int wakeup_events = event->attr.wakeup_events;
3384 if (handle->sample && wakeup_events) {
3385 int events = local_inc_return(&buffer->events);
3386 if (events >= wakeup_events) {
3387 local_sub(wakeup_events, &buffer->events);
3388 local_inc(&buffer->wakeup);
3392 perf_output_put_handle(handle);
3396 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3399 * only top level events have the pid namespace they were created in
3402 event = event->parent;
3404 return task_tgid_nr_ns(p, event->ns);
3407 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3410 * only top level events have the pid namespace they were created in
3413 event = event->parent;
3415 return task_pid_nr_ns(p, event->ns);
3418 static void perf_output_read_one(struct perf_output_handle *handle,
3419 struct perf_event *event)
3421 u64 read_format = event->attr.read_format;
3425 values[n++] = perf_event_count(event);
3426 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3427 values[n++] = event->total_time_enabled +
3428 atomic64_read(&event->child_total_time_enabled);
3430 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3431 values[n++] = event->total_time_running +
3432 atomic64_read(&event->child_total_time_running);
3434 if (read_format & PERF_FORMAT_ID)
3435 values[n++] = primary_event_id(event);
3437 perf_output_copy(handle, values, n * sizeof(u64));
3441 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3443 static void perf_output_read_group(struct perf_output_handle *handle,
3444 struct perf_event *event)
3446 struct perf_event *leader = event->group_leader, *sub;
3447 u64 read_format = event->attr.read_format;
3451 values[n++] = 1 + leader->nr_siblings;
3453 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3454 values[n++] = leader->total_time_enabled;
3456 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3457 values[n++] = leader->total_time_running;
3459 if (leader != event)
3460 leader->pmu->read(leader);
3462 values[n++] = perf_event_count(leader);
3463 if (read_format & PERF_FORMAT_ID)
3464 values[n++] = primary_event_id(leader);
3466 perf_output_copy(handle, values, n * sizeof(u64));
3468 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3472 sub->pmu->read(sub);
3474 values[n++] = perf_event_count(sub);
3475 if (read_format & PERF_FORMAT_ID)
3476 values[n++] = primary_event_id(sub);
3478 perf_output_copy(handle, values, n * sizeof(u64));
3482 static void perf_output_read(struct perf_output_handle *handle,
3483 struct perf_event *event)
3485 if (event->attr.read_format & PERF_FORMAT_GROUP)
3486 perf_output_read_group(handle, event);
3488 perf_output_read_one(handle, event);
3491 void perf_output_sample(struct perf_output_handle *handle,
3492 struct perf_event_header *header,
3493 struct perf_sample_data *data,
3494 struct perf_event *event)
3496 u64 sample_type = data->type;
3498 perf_output_put(handle, *header);
3500 if (sample_type & PERF_SAMPLE_IP)
3501 perf_output_put(handle, data->ip);
3503 if (sample_type & PERF_SAMPLE_TID)
3504 perf_output_put(handle, data->tid_entry);
3506 if (sample_type & PERF_SAMPLE_TIME)
3507 perf_output_put(handle, data->time);
3509 if (sample_type & PERF_SAMPLE_ADDR)
3510 perf_output_put(handle, data->addr);
3512 if (sample_type & PERF_SAMPLE_ID)
3513 perf_output_put(handle, data->id);
3515 if (sample_type & PERF_SAMPLE_STREAM_ID)
3516 perf_output_put(handle, data->stream_id);
3518 if (sample_type & PERF_SAMPLE_CPU)
3519 perf_output_put(handle, data->cpu_entry);
3521 if (sample_type & PERF_SAMPLE_PERIOD)
3522 perf_output_put(handle, data->period);
3524 if (sample_type & PERF_SAMPLE_READ)
3525 perf_output_read(handle, event);
3527 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3528 if (data->callchain) {
3531 if (data->callchain)
3532 size += data->callchain->nr;
3534 size *= sizeof(u64);
3536 perf_output_copy(handle, data->callchain, size);
3539 perf_output_put(handle, nr);
3543 if (sample_type & PERF_SAMPLE_RAW) {
3545 perf_output_put(handle, data->raw->size);
3546 perf_output_copy(handle, data->raw->data,
3553 .size = sizeof(u32),
3556 perf_output_put(handle, raw);
3561 void perf_prepare_sample(struct perf_event_header *header,
3562 struct perf_sample_data *data,
3563 struct perf_event *event,
3564 struct pt_regs *regs)
3566 u64 sample_type = event->attr.sample_type;
3568 data->type = sample_type;
3570 header->type = PERF_RECORD_SAMPLE;
3571 header->size = sizeof(*header);
3574 header->misc |= perf_misc_flags(regs);
3576 if (sample_type & PERF_SAMPLE_IP) {
3577 data->ip = perf_instruction_pointer(regs);
3579 header->size += sizeof(data->ip);
3582 if (sample_type & PERF_SAMPLE_TID) {
3583 /* namespace issues */
3584 data->tid_entry.pid = perf_event_pid(event, current);
3585 data->tid_entry.tid = perf_event_tid(event, current);
3587 header->size += sizeof(data->tid_entry);
3590 if (sample_type & PERF_SAMPLE_TIME) {
3591 data->time = perf_clock();
3593 header->size += sizeof(data->time);
3596 if (sample_type & PERF_SAMPLE_ADDR)
3597 header->size += sizeof(data->addr);
3599 if (sample_type & PERF_SAMPLE_ID) {
3600 data->id = primary_event_id(event);
3602 header->size += sizeof(data->id);
3605 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3606 data->stream_id = event->id;
3608 header->size += sizeof(data->stream_id);
3611 if (sample_type & PERF_SAMPLE_CPU) {
3612 data->cpu_entry.cpu = raw_smp_processor_id();
3613 data->cpu_entry.reserved = 0;
3615 header->size += sizeof(data->cpu_entry);
3618 if (sample_type & PERF_SAMPLE_PERIOD)
3619 header->size += sizeof(data->period);
3621 if (sample_type & PERF_SAMPLE_READ)
3622 header->size += perf_event_read_size(event);
3624 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3627 data->callchain = perf_callchain(regs);
3629 if (data->callchain)
3630 size += data->callchain->nr;
3632 header->size += size * sizeof(u64);
3635 if (sample_type & PERF_SAMPLE_RAW) {
3636 int size = sizeof(u32);
3639 size += data->raw->size;
3641 size += sizeof(u32);
3643 WARN_ON_ONCE(size & (sizeof(u64)-1));
3644 header->size += size;
3648 static void perf_event_output(struct perf_event *event, int nmi,
3649 struct perf_sample_data *data,
3650 struct pt_regs *regs)
3652 struct perf_output_handle handle;
3653 struct perf_event_header header;
3655 /* protect the callchain buffers */
3658 perf_prepare_sample(&header, data, event, regs);
3660 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3663 perf_output_sample(&handle, &header, data, event);
3665 perf_output_end(&handle);
3675 struct perf_read_event {
3676 struct perf_event_header header;
3683 perf_event_read_event(struct perf_event *event,
3684 struct task_struct *task)
3686 struct perf_output_handle handle;
3687 struct perf_read_event read_event = {
3689 .type = PERF_RECORD_READ,
3691 .size = sizeof(read_event) + perf_event_read_size(event),
3693 .pid = perf_event_pid(event, task),
3694 .tid = perf_event_tid(event, task),
3698 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3702 perf_output_put(&handle, read_event);
3703 perf_output_read(&handle, event);
3705 perf_output_end(&handle);
3709 * task tracking -- fork/exit
3711 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
3714 struct perf_task_event {
3715 struct task_struct *task;
3716 struct perf_event_context *task_ctx;
3719 struct perf_event_header header;
3729 static void perf_event_task_output(struct perf_event *event,
3730 struct perf_task_event *task_event)
3732 struct perf_output_handle handle;
3733 struct task_struct *task = task_event->task;
3736 size = task_event->event_id.header.size;
3737 ret = perf_output_begin(&handle, event, size, 0, 0);
3742 task_event->event_id.pid = perf_event_pid(event, task);
3743 task_event->event_id.ppid = perf_event_pid(event, current);
3745 task_event->event_id.tid = perf_event_tid(event, task);
3746 task_event->event_id.ptid = perf_event_tid(event, current);
3748 perf_output_put(&handle, task_event->event_id);
3750 perf_output_end(&handle);
3753 static int perf_event_task_match(struct perf_event *event)
3755 if (event->state < PERF_EVENT_STATE_INACTIVE)
3758 if (event->cpu != -1 && event->cpu != smp_processor_id())
3761 if (event->attr.comm || event->attr.mmap ||
3762 event->attr.mmap_data || event->attr.task)
3768 static void perf_event_task_ctx(struct perf_event_context *ctx,
3769 struct perf_task_event *task_event)
3771 struct perf_event *event;
3773 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3774 if (perf_event_task_match(event))
3775 perf_event_task_output(event, task_event);
3779 static void perf_event_task_event(struct perf_task_event *task_event)
3781 struct perf_cpu_context *cpuctx;
3782 struct perf_event_context *ctx = task_event->task_ctx;
3785 cpuctx = &get_cpu_var(perf_cpu_context);
3786 perf_event_task_ctx(&cpuctx->ctx, task_event);
3788 ctx = rcu_dereference(current->perf_event_ctxp);
3790 perf_event_task_ctx(ctx, task_event);
3791 put_cpu_var(perf_cpu_context);
3795 static void perf_event_task(struct task_struct *task,
3796 struct perf_event_context *task_ctx,
3799 struct perf_task_event task_event;
3801 if (!atomic_read(&nr_comm_events) &&
3802 !atomic_read(&nr_mmap_events) &&
3803 !atomic_read(&nr_task_events))
3806 task_event = (struct perf_task_event){
3808 .task_ctx = task_ctx,
3811 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3813 .size = sizeof(task_event.event_id),
3819 .time = perf_clock(),
3823 perf_event_task_event(&task_event);
3826 void perf_event_fork(struct task_struct *task)
3828 perf_event_task(task, NULL, 1);
3835 struct perf_comm_event {
3836 struct task_struct *task;
3841 struct perf_event_header header;
3848 static void perf_event_comm_output(struct perf_event *event,
3849 struct perf_comm_event *comm_event)
3851 struct perf_output_handle handle;
3852 int size = comm_event->event_id.header.size;
3853 int ret = perf_output_begin(&handle, event, size, 0, 0);
3858 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3859 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3861 perf_output_put(&handle, comm_event->event_id);
3862 perf_output_copy(&handle, comm_event->comm,
3863 comm_event->comm_size);
3864 perf_output_end(&handle);
3867 static int perf_event_comm_match(struct perf_event *event)
3869 if (event->state < PERF_EVENT_STATE_INACTIVE)
3872 if (event->cpu != -1 && event->cpu != smp_processor_id())
3875 if (event->attr.comm)
3881 static void perf_event_comm_ctx(struct perf_event_context *ctx,
3882 struct perf_comm_event *comm_event)
3884 struct perf_event *event;
3886 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3887 if (perf_event_comm_match(event))
3888 perf_event_comm_output(event, comm_event);
3892 static void perf_event_comm_event(struct perf_comm_event *comm_event)
3894 struct perf_cpu_context *cpuctx;
3895 struct perf_event_context *ctx;
3897 char comm[TASK_COMM_LEN];
3899 memset(comm, 0, sizeof(comm));
3900 strlcpy(comm, comm_event->task->comm, sizeof(comm));
3901 size = ALIGN(strlen(comm)+1, sizeof(u64));
3903 comm_event->comm = comm;
3904 comm_event->comm_size = size;
3906 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3909 cpuctx = &get_cpu_var(perf_cpu_context);
3910 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
3911 ctx = rcu_dereference(current->perf_event_ctxp);
3913 perf_event_comm_ctx(ctx, comm_event);
3914 put_cpu_var(perf_cpu_context);
3918 void perf_event_comm(struct task_struct *task)
3920 struct perf_comm_event comm_event;
3922 if (task->perf_event_ctxp)
3923 perf_event_enable_on_exec(task);
3925 if (!atomic_read(&nr_comm_events))
3928 comm_event = (struct perf_comm_event){
3934 .type = PERF_RECORD_COMM,
3943 perf_event_comm_event(&comm_event);
3950 struct perf_mmap_event {
3951 struct vm_area_struct *vma;
3953 const char *file_name;
3957 struct perf_event_header header;
3967 static void perf_event_mmap_output(struct perf_event *event,
3968 struct perf_mmap_event *mmap_event)
3970 struct perf_output_handle handle;
3971 int size = mmap_event->event_id.header.size;
3972 int ret = perf_output_begin(&handle, event, size, 0, 0);
3977 mmap_event->event_id.pid = perf_event_pid(event, current);
3978 mmap_event->event_id.tid = perf_event_tid(event, current);
3980 perf_output_put(&handle, mmap_event->event_id);
3981 perf_output_copy(&handle, mmap_event->file_name,
3982 mmap_event->file_size);
3983 perf_output_end(&handle);
3986 static int perf_event_mmap_match(struct perf_event *event,
3987 struct perf_mmap_event *mmap_event,
3990 if (event->state < PERF_EVENT_STATE_INACTIVE)
3993 if (event->cpu != -1 && event->cpu != smp_processor_id())
3996 if ((!executable && event->attr.mmap_data) ||
3997 (executable && event->attr.mmap))
4003 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4004 struct perf_mmap_event *mmap_event,
4007 struct perf_event *event;
4009 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4010 if (perf_event_mmap_match(event, mmap_event, executable))
4011 perf_event_mmap_output(event, mmap_event);
4015 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4017 struct perf_cpu_context *cpuctx;
4018 struct perf_event_context *ctx;
4019 struct vm_area_struct *vma = mmap_event->vma;
4020 struct file *file = vma->vm_file;
4026 memset(tmp, 0, sizeof(tmp));
4030 * d_path works from the end of the buffer backwards, so we
4031 * need to add enough zero bytes after the string to handle
4032 * the 64bit alignment we do later.
4034 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4036 name = strncpy(tmp, "//enomem", sizeof(tmp));
4039 name = d_path(&file->f_path, buf, PATH_MAX);
4041 name = strncpy(tmp, "//toolong", sizeof(tmp));
4045 if (arch_vma_name(mmap_event->vma)) {
4046 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4052 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4054 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4055 vma->vm_end >= vma->vm_mm->brk) {
4056 name = strncpy(tmp, "[heap]", sizeof(tmp));
4058 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4059 vma->vm_end >= vma->vm_mm->start_stack) {
4060 name = strncpy(tmp, "[stack]", sizeof(tmp));
4064 name = strncpy(tmp, "//anon", sizeof(tmp));
4069 size = ALIGN(strlen(name)+1, sizeof(u64));
4071 mmap_event->file_name = name;
4072 mmap_event->file_size = size;
4074 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4077 cpuctx = &get_cpu_var(perf_cpu_context);
4078 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event, vma->vm_flags & VM_EXEC);
4079 ctx = rcu_dereference(current->perf_event_ctxp);
4081 perf_event_mmap_ctx(ctx, mmap_event, vma->vm_flags & VM_EXEC);
4082 put_cpu_var(perf_cpu_context);
4088 void perf_event_mmap(struct vm_area_struct *vma)
4090 struct perf_mmap_event mmap_event;
4092 if (!atomic_read(&nr_mmap_events))
4095 mmap_event = (struct perf_mmap_event){
4101 .type = PERF_RECORD_MMAP,
4102 .misc = PERF_RECORD_MISC_USER,
4107 .start = vma->vm_start,
4108 .len = vma->vm_end - vma->vm_start,
4109 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4113 perf_event_mmap_event(&mmap_event);
4117 * IRQ throttle logging
4120 static void perf_log_throttle(struct perf_event *event, int enable)
4122 struct perf_output_handle handle;
4126 struct perf_event_header header;
4130 } throttle_event = {
4132 .type = PERF_RECORD_THROTTLE,
4134 .size = sizeof(throttle_event),
4136 .time = perf_clock(),
4137 .id = primary_event_id(event),
4138 .stream_id = event->id,
4142 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4144 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
4148 perf_output_put(&handle, throttle_event);
4149 perf_output_end(&handle);
4153 * Generic event overflow handling, sampling.
4156 static int __perf_event_overflow(struct perf_event *event, int nmi,
4157 int throttle, struct perf_sample_data *data,
4158 struct pt_regs *regs)
4160 int events = atomic_read(&event->event_limit);
4161 struct hw_perf_event *hwc = &event->hw;
4164 throttle = (throttle && event->pmu->unthrottle != NULL);
4169 if (hwc->interrupts != MAX_INTERRUPTS) {
4171 if (HZ * hwc->interrupts >
4172 (u64)sysctl_perf_event_sample_rate) {
4173 hwc->interrupts = MAX_INTERRUPTS;
4174 perf_log_throttle(event, 0);
4179 * Keep re-disabling events even though on the previous
4180 * pass we disabled it - just in case we raced with a
4181 * sched-in and the event got enabled again:
4187 if (event->attr.freq) {
4188 u64 now = perf_clock();
4189 s64 delta = now - hwc->freq_time_stamp;
4191 hwc->freq_time_stamp = now;
4193 if (delta > 0 && delta < 2*TICK_NSEC)
4194 perf_adjust_period(event, delta, hwc->last_period);
4198 * XXX event_limit might not quite work as expected on inherited
4202 event->pending_kill = POLL_IN;
4203 if (events && atomic_dec_and_test(&event->event_limit)) {
4205 event->pending_kill = POLL_HUP;
4207 event->pending_disable = 1;
4208 perf_pending_queue(&event->pending,
4209 perf_pending_event);
4211 perf_event_disable(event);
4214 if (event->overflow_handler)
4215 event->overflow_handler(event, nmi, data, regs);
4217 perf_event_output(event, nmi, data, regs);
4222 int perf_event_overflow(struct perf_event *event, int nmi,
4223 struct perf_sample_data *data,
4224 struct pt_regs *regs)
4226 return __perf_event_overflow(event, nmi, 1, data, regs);
4230 * Generic software event infrastructure
4234 * We directly increment event->count and keep a second value in
4235 * event->hw.period_left to count intervals. This period event
4236 * is kept in the range [-sample_period, 0] so that we can use the
4240 static u64 perf_swevent_set_period(struct perf_event *event)
4242 struct hw_perf_event *hwc = &event->hw;
4243 u64 period = hwc->last_period;
4247 hwc->last_period = hwc->sample_period;
4250 old = val = local64_read(&hwc->period_left);
4254 nr = div64_u64(period + val, period);
4255 offset = nr * period;
4257 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4263 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4264 int nmi, struct perf_sample_data *data,
4265 struct pt_regs *regs)
4267 struct hw_perf_event *hwc = &event->hw;
4270 data->period = event->hw.last_period;
4272 overflow = perf_swevent_set_period(event);
4274 if (hwc->interrupts == MAX_INTERRUPTS)
4277 for (; overflow; overflow--) {
4278 if (__perf_event_overflow(event, nmi, throttle,
4281 * We inhibit the overflow from happening when
4282 * hwc->interrupts == MAX_INTERRUPTS.
4290 static void perf_swevent_add(struct perf_event *event, u64 nr,
4291 int nmi, struct perf_sample_data *data,
4292 struct pt_regs *regs)
4294 struct hw_perf_event *hwc = &event->hw;
4296 local64_add(nr, &event->count);
4301 if (!hwc->sample_period)
4304 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4305 return perf_swevent_overflow(event, 1, nmi, data, regs);
4307 if (local64_add_negative(nr, &hwc->period_left))
4310 perf_swevent_overflow(event, 0, nmi, data, regs);
4313 static int perf_exclude_event(struct perf_event *event,
4314 struct pt_regs *regs)
4317 if (event->attr.exclude_user && user_mode(regs))
4320 if (event->attr.exclude_kernel && !user_mode(regs))
4327 static int perf_swevent_match(struct perf_event *event,
4328 enum perf_type_id type,
4330 struct perf_sample_data *data,
4331 struct pt_regs *regs)
4333 if (event->attr.type != type)
4336 if (event->attr.config != event_id)
4339 if (perf_exclude_event(event, regs))
4345 static inline u64 swevent_hash(u64 type, u32 event_id)
4347 u64 val = event_id | (type << 32);
4349 return hash_64(val, SWEVENT_HLIST_BITS);
4352 static inline struct hlist_head *
4353 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
4355 u64 hash = swevent_hash(type, event_id);
4357 return &hlist->heads[hash];
4360 /* For the read side: events when they trigger */
4361 static inline struct hlist_head *
4362 find_swevent_head_rcu(struct perf_cpu_context *ctx, u64 type, u32 event_id)
4364 struct swevent_hlist *hlist;
4366 hlist = rcu_dereference(ctx->swevent_hlist);
4370 return __find_swevent_head(hlist, type, event_id);
4373 /* For the event head insertion and removal in the hlist */
4374 static inline struct hlist_head *
4375 find_swevent_head(struct perf_cpu_context *ctx, struct perf_event *event)
4377 struct swevent_hlist *hlist;
4378 u32 event_id = event->attr.config;
4379 u64 type = event->attr.type;
4382 * Event scheduling is always serialized against hlist allocation
4383 * and release. Which makes the protected version suitable here.
4384 * The context lock guarantees that.
4386 hlist = rcu_dereference_protected(ctx->swevent_hlist,
4387 lockdep_is_held(&event->ctx->lock));
4391 return __find_swevent_head(hlist, type, event_id);
4394 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4396 struct perf_sample_data *data,
4397 struct pt_regs *regs)
4399 struct perf_cpu_context *cpuctx;
4400 struct perf_event *event;
4401 struct hlist_node *node;
4402 struct hlist_head *head;
4404 cpuctx = &__get_cpu_var(perf_cpu_context);
4408 head = find_swevent_head_rcu(cpuctx, type, event_id);
4413 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4414 if (perf_swevent_match(event, type, event_id, data, regs))
4415 perf_swevent_add(event, nr, nmi, data, regs);
4421 int perf_swevent_get_recursion_context(void)
4423 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4425 return get_recursion_context(cpuctx->recursion);
4427 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
4429 void inline perf_swevent_put_recursion_context(int rctx)
4431 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
4433 put_recursion_context(cpuctx->recursion, rctx);
4436 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4437 struct pt_regs *regs, u64 addr)
4439 struct perf_sample_data data;
4442 preempt_disable_notrace();
4443 rctx = perf_swevent_get_recursion_context();
4447 perf_sample_data_init(&data, addr);
4449 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
4451 perf_swevent_put_recursion_context(rctx);
4452 preempt_enable_notrace();
4455 static void perf_swevent_read(struct perf_event *event)
4459 static int perf_swevent_enable(struct perf_event *event)
4461 struct hw_perf_event *hwc = &event->hw;
4462 struct perf_cpu_context *cpuctx;
4463 struct hlist_head *head;
4465 cpuctx = &__get_cpu_var(perf_cpu_context);
4467 if (hwc->sample_period) {
4468 hwc->last_period = hwc->sample_period;
4469 perf_swevent_set_period(event);
4472 head = find_swevent_head(cpuctx, event);
4473 if (WARN_ON_ONCE(!head))
4476 hlist_add_head_rcu(&event->hlist_entry, head);
4481 static void perf_swevent_disable(struct perf_event *event)
4483 hlist_del_rcu(&event->hlist_entry);
4486 static void perf_swevent_void(struct perf_event *event)
4490 static int perf_swevent_int(struct perf_event *event)
4495 /* Deref the hlist from the update side */
4496 static inline struct swevent_hlist *
4497 swevent_hlist_deref(struct perf_cpu_context *cpuctx)
4499 return rcu_dereference_protected(cpuctx->swevent_hlist,
4500 lockdep_is_held(&cpuctx->hlist_mutex));
4503 static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4505 struct swevent_hlist *hlist;
4507 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4511 static void swevent_hlist_release(struct perf_cpu_context *cpuctx)
4513 struct swevent_hlist *hlist = swevent_hlist_deref(cpuctx);
4518 rcu_assign_pointer(cpuctx->swevent_hlist, NULL);
4519 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4522 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4524 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4526 mutex_lock(&cpuctx->hlist_mutex);
4528 if (!--cpuctx->hlist_refcount)
4529 swevent_hlist_release(cpuctx);
4531 mutex_unlock(&cpuctx->hlist_mutex);
4534 static void swevent_hlist_put(struct perf_event *event)
4538 if (event->cpu != -1) {
4539 swevent_hlist_put_cpu(event, event->cpu);
4543 for_each_possible_cpu(cpu)
4544 swevent_hlist_put_cpu(event, cpu);
4547 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4549 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4552 mutex_lock(&cpuctx->hlist_mutex);
4554 if (!swevent_hlist_deref(cpuctx) && cpu_online(cpu)) {
4555 struct swevent_hlist *hlist;
4557 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4562 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
4564 cpuctx->hlist_refcount++;
4566 mutex_unlock(&cpuctx->hlist_mutex);
4571 static int swevent_hlist_get(struct perf_event *event)
4574 int cpu, failed_cpu;
4576 if (event->cpu != -1)
4577 return swevent_hlist_get_cpu(event, event->cpu);
4580 for_each_possible_cpu(cpu) {
4581 err = swevent_hlist_get_cpu(event, cpu);
4591 for_each_possible_cpu(cpu) {
4592 if (cpu == failed_cpu)
4594 swevent_hlist_put_cpu(event, cpu);
4601 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4603 static void sw_perf_event_destroy(struct perf_event *event)
4605 u64 event_id = event->attr.config;
4607 WARN_ON(event->parent);
4609 atomic_dec(&perf_swevent_enabled[event_id]);
4610 swevent_hlist_put(event);
4613 static int perf_swevent_init(struct perf_event *event)
4615 int event_id = event->attr.config;
4617 if (event->attr.type != PERF_TYPE_SOFTWARE)
4621 case PERF_COUNT_SW_CPU_CLOCK:
4622 case PERF_COUNT_SW_TASK_CLOCK:
4629 if (event_id > PERF_COUNT_SW_MAX)
4632 if (!event->parent) {
4635 err = swevent_hlist_get(event);
4639 atomic_inc(&perf_swevent_enabled[event_id]);
4640 event->destroy = sw_perf_event_destroy;
4646 static struct pmu perf_swevent = {
4647 .event_init = perf_swevent_init,
4648 .enable = perf_swevent_enable,
4649 .disable = perf_swevent_disable,
4650 .start = perf_swevent_int,
4651 .stop = perf_swevent_void,
4652 .read = perf_swevent_read,
4653 .unthrottle = perf_swevent_void, /* hwc->interrupts already reset */
4656 #ifdef CONFIG_EVENT_TRACING
4658 static int perf_tp_filter_match(struct perf_event *event,
4659 struct perf_sample_data *data)
4661 void *record = data->raw->data;
4663 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4668 static int perf_tp_event_match(struct perf_event *event,
4669 struct perf_sample_data *data,
4670 struct pt_regs *regs)
4673 * All tracepoints are from kernel-space.
4675 if (event->attr.exclude_kernel)
4678 if (!perf_tp_filter_match(event, data))
4684 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
4685 struct pt_regs *regs, struct hlist_head *head, int rctx)
4687 struct perf_sample_data data;
4688 struct perf_event *event;
4689 struct hlist_node *node;
4691 struct perf_raw_record raw = {
4696 perf_sample_data_init(&data, addr);
4699 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4700 if (perf_tp_event_match(event, &data, regs))
4701 perf_swevent_add(event, count, 1, &data, regs);
4704 perf_swevent_put_recursion_context(rctx);
4706 EXPORT_SYMBOL_GPL(perf_tp_event);
4708 static void tp_perf_event_destroy(struct perf_event *event)
4710 perf_trace_destroy(event);
4713 static int perf_tp_event_init(struct perf_event *event)
4717 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4721 * Raw tracepoint data is a severe data leak, only allow root to
4724 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4725 perf_paranoid_tracepoint_raw() &&
4726 !capable(CAP_SYS_ADMIN))
4729 err = perf_trace_init(event);
4733 event->destroy = tp_perf_event_destroy;
4738 static struct pmu perf_tracepoint = {
4739 .event_init = perf_tp_event_init,
4740 .enable = perf_trace_enable,
4741 .disable = perf_trace_disable,
4742 .start = perf_swevent_int,
4743 .stop = perf_swevent_void,
4744 .read = perf_swevent_read,
4745 .unthrottle = perf_swevent_void,
4748 static inline void perf_tp_register(void)
4750 perf_pmu_register(&perf_tracepoint);
4753 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4758 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4761 filter_str = strndup_user(arg, PAGE_SIZE);
4762 if (IS_ERR(filter_str))
4763 return PTR_ERR(filter_str);
4765 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4771 static void perf_event_free_filter(struct perf_event *event)
4773 ftrace_profile_free_filter(event);
4778 static inline void perf_tp_register(void)
4782 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4787 static void perf_event_free_filter(struct perf_event *event)
4791 #endif /* CONFIG_EVENT_TRACING */
4793 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4794 void perf_bp_event(struct perf_event *bp, void *data)
4796 struct perf_sample_data sample;
4797 struct pt_regs *regs = data;
4799 perf_sample_data_init(&sample, bp->attr.bp_addr);
4801 if (!perf_exclude_event(bp, regs))
4802 perf_swevent_add(bp, 1, 1, &sample, regs);
4807 * hrtimer based swevent callback
4810 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
4812 enum hrtimer_restart ret = HRTIMER_RESTART;
4813 struct perf_sample_data data;
4814 struct pt_regs *regs;
4815 struct perf_event *event;
4818 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4819 event->pmu->read(event);
4821 perf_sample_data_init(&data, 0);
4822 data.period = event->hw.last_period;
4823 regs = get_irq_regs();
4825 if (regs && !perf_exclude_event(event, regs)) {
4826 if (!(event->attr.exclude_idle && current->pid == 0))
4827 if (perf_event_overflow(event, 0, &data, regs))
4828 ret = HRTIMER_NORESTART;
4831 period = max_t(u64, 10000, event->hw.sample_period);
4832 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4837 static void perf_swevent_start_hrtimer(struct perf_event *event)
4839 struct hw_perf_event *hwc = &event->hw;
4841 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4842 hwc->hrtimer.function = perf_swevent_hrtimer;
4843 if (hwc->sample_period) {
4846 if (hwc->remaining) {
4847 if (hwc->remaining < 0)
4850 period = hwc->remaining;
4853 period = max_t(u64, 10000, hwc->sample_period);
4855 __hrtimer_start_range_ns(&hwc->hrtimer,
4856 ns_to_ktime(period), 0,
4857 HRTIMER_MODE_REL, 0);
4861 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4863 struct hw_perf_event *hwc = &event->hw;
4865 if (hwc->sample_period) {
4866 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4867 hwc->remaining = ktime_to_ns(remaining);
4869 hrtimer_cancel(&hwc->hrtimer);
4874 * Software event: cpu wall time clock
4877 static void cpu_clock_event_update(struct perf_event *event)
4879 int cpu = raw_smp_processor_id();
4883 now = cpu_clock(cpu);
4884 prev = local64_xchg(&event->hw.prev_count, now);
4885 local64_add(now - prev, &event->count);
4888 static int cpu_clock_event_enable(struct perf_event *event)
4890 struct hw_perf_event *hwc = &event->hw;
4891 int cpu = raw_smp_processor_id();
4893 local64_set(&hwc->prev_count, cpu_clock(cpu));
4894 perf_swevent_start_hrtimer(event);
4899 static void cpu_clock_event_disable(struct perf_event *event)
4901 perf_swevent_cancel_hrtimer(event);
4902 cpu_clock_event_update(event);
4905 static void cpu_clock_event_read(struct perf_event *event)
4907 cpu_clock_event_update(event);
4910 static int cpu_clock_event_init(struct perf_event *event)
4912 if (event->attr.type != PERF_TYPE_SOFTWARE)
4915 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
4921 static struct pmu perf_cpu_clock = {
4922 .event_init = cpu_clock_event_init,
4923 .enable = cpu_clock_event_enable,
4924 .disable = cpu_clock_event_disable,
4925 .read = cpu_clock_event_read,
4929 * Software event: task time clock
4932 static void task_clock_event_update(struct perf_event *event, u64 now)
4937 prev = local64_xchg(&event->hw.prev_count, now);
4939 local64_add(delta, &event->count);
4942 static int task_clock_event_enable(struct perf_event *event)
4944 struct hw_perf_event *hwc = &event->hw;
4947 now = event->ctx->time;
4949 local64_set(&hwc->prev_count, now);
4951 perf_swevent_start_hrtimer(event);
4956 static void task_clock_event_disable(struct perf_event *event)
4958 perf_swevent_cancel_hrtimer(event);
4959 task_clock_event_update(event, event->ctx->time);
4963 static void task_clock_event_read(struct perf_event *event)
4968 update_context_time(event->ctx);
4969 time = event->ctx->time;
4971 u64 now = perf_clock();
4972 u64 delta = now - event->ctx->timestamp;
4973 time = event->ctx->time + delta;
4976 task_clock_event_update(event, time);
4979 static int task_clock_event_init(struct perf_event *event)
4981 if (event->attr.type != PERF_TYPE_SOFTWARE)
4984 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
4990 static struct pmu perf_task_clock = {
4991 .event_init = task_clock_event_init,
4992 .enable = task_clock_event_enable,
4993 .disable = task_clock_event_disable,
4994 .read = task_clock_event_read,
4997 static LIST_HEAD(pmus);
4998 static DEFINE_MUTEX(pmus_lock);
4999 static struct srcu_struct pmus_srcu;
5001 int perf_pmu_register(struct pmu *pmu)
5003 mutex_lock(&pmus_lock);
5004 list_add_rcu(&pmu->entry, &pmus);
5005 mutex_unlock(&pmus_lock);
5010 void perf_pmu_unregister(struct pmu *pmu)
5012 mutex_lock(&pmus_lock);
5013 list_del_rcu(&pmu->entry);
5014 mutex_unlock(&pmus_lock);
5016 synchronize_srcu(&pmus_srcu);
5019 struct pmu *perf_init_event(struct perf_event *event)
5021 struct pmu *pmu = NULL;
5024 idx = srcu_read_lock(&pmus_srcu);
5025 list_for_each_entry_rcu(pmu, &pmus, entry) {
5026 int ret = pmu->event_init(event);
5029 if (ret != -ENOENT) {
5034 srcu_read_unlock(&pmus_srcu, idx);
5040 * Allocate and initialize a event structure
5042 static struct perf_event *
5043 perf_event_alloc(struct perf_event_attr *attr,
5045 struct perf_event_context *ctx,
5046 struct perf_event *group_leader,
5047 struct perf_event *parent_event,
5048 perf_overflow_handler_t overflow_handler,
5052 struct perf_event *event;
5053 struct hw_perf_event *hwc;
5056 event = kzalloc(sizeof(*event), gfpflags);
5058 return ERR_PTR(-ENOMEM);
5061 * Single events are their own group leaders, with an
5062 * empty sibling list:
5065 group_leader = event;
5067 mutex_init(&event->child_mutex);
5068 INIT_LIST_HEAD(&event->child_list);
5070 INIT_LIST_HEAD(&event->group_entry);
5071 INIT_LIST_HEAD(&event->event_entry);
5072 INIT_LIST_HEAD(&event->sibling_list);
5073 init_waitqueue_head(&event->waitq);
5075 mutex_init(&event->mmap_mutex);
5078 event->attr = *attr;
5079 event->group_leader = group_leader;
5084 event->parent = parent_event;
5086 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5087 event->id = atomic64_inc_return(&perf_event_id);
5089 event->state = PERF_EVENT_STATE_INACTIVE;
5091 if (!overflow_handler && parent_event)
5092 overflow_handler = parent_event->overflow_handler;
5094 event->overflow_handler = overflow_handler;
5097 event->state = PERF_EVENT_STATE_OFF;
5102 hwc->sample_period = attr->sample_period;
5103 if (attr->freq && attr->sample_freq)
5104 hwc->sample_period = 1;
5105 hwc->last_period = hwc->sample_period;
5107 local64_set(&hwc->period_left, hwc->sample_period);
5110 * we currently do not support PERF_FORMAT_GROUP on inherited events
5112 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5115 pmu = perf_init_event(event);
5121 else if (IS_ERR(pmu))
5126 put_pid_ns(event->ns);
5128 return ERR_PTR(err);
5133 if (!event->parent) {
5134 atomic_inc(&nr_events);
5135 if (event->attr.mmap || event->attr.mmap_data)
5136 atomic_inc(&nr_mmap_events);
5137 if (event->attr.comm)
5138 atomic_inc(&nr_comm_events);
5139 if (event->attr.task)
5140 atomic_inc(&nr_task_events);
5141 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5142 err = get_callchain_buffers();
5145 return ERR_PTR(err);
5153 static int perf_copy_attr(struct perf_event_attr __user *uattr,
5154 struct perf_event_attr *attr)
5159 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5163 * zero the full structure, so that a short copy will be nice.
5165 memset(attr, 0, sizeof(*attr));
5167 ret = get_user(size, &uattr->size);
5171 if (size > PAGE_SIZE) /* silly large */
5174 if (!size) /* abi compat */
5175 size = PERF_ATTR_SIZE_VER0;
5177 if (size < PERF_ATTR_SIZE_VER0)
5181 * If we're handed a bigger struct than we know of,
5182 * ensure all the unknown bits are 0 - i.e. new
5183 * user-space does not rely on any kernel feature
5184 * extensions we dont know about yet.
5186 if (size > sizeof(*attr)) {
5187 unsigned char __user *addr;
5188 unsigned char __user *end;
5191 addr = (void __user *)uattr + sizeof(*attr);
5192 end = (void __user *)uattr + size;
5194 for (; addr < end; addr++) {
5195 ret = get_user(val, addr);
5201 size = sizeof(*attr);
5204 ret = copy_from_user(attr, uattr, size);
5209 * If the type exists, the corresponding creation will verify
5212 if (attr->type >= PERF_TYPE_MAX)
5215 if (attr->__reserved_1)
5218 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5221 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5228 put_user(sizeof(*attr), &uattr->size);
5234 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
5236 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
5242 /* don't allow circular references */
5243 if (event == output_event)
5247 * Don't allow cross-cpu buffers
5249 if (output_event->cpu != event->cpu)
5253 * If its not a per-cpu buffer, it must be the same task.
5255 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5259 mutex_lock(&event->mmap_mutex);
5260 /* Can't redirect output if we've got an active mmap() */
5261 if (atomic_read(&event->mmap_count))
5265 /* get the buffer we want to redirect to */
5266 buffer = perf_buffer_get(output_event);
5271 old_buffer = event->buffer;
5272 rcu_assign_pointer(event->buffer, buffer);
5275 mutex_unlock(&event->mmap_mutex);
5278 perf_buffer_put(old_buffer);
5284 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5286 * @attr_uptr: event_id type attributes for monitoring/sampling
5289 * @group_fd: group leader event fd
5291 SYSCALL_DEFINE5(perf_event_open,
5292 struct perf_event_attr __user *, attr_uptr,
5293 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5295 struct perf_event *event, *group_leader = NULL, *output_event = NULL;
5296 struct perf_event_attr attr;
5297 struct perf_event_context *ctx;
5298 struct file *event_file = NULL;
5299 struct file *group_file = NULL;
5301 int fput_needed = 0;
5304 /* for future expandability... */
5305 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5308 err = perf_copy_attr(attr_uptr, &attr);
5312 if (!attr.exclude_kernel) {
5313 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5318 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5322 event_fd = get_unused_fd_flags(O_RDWR);
5327 * Get the target context (task or percpu):
5329 ctx = find_get_context(pid, cpu);
5335 if (group_fd != -1) {
5336 group_leader = perf_fget_light(group_fd, &fput_needed);
5337 if (IS_ERR(group_leader)) {
5338 err = PTR_ERR(group_leader);
5339 goto err_put_context;
5341 group_file = group_leader->filp;
5342 if (flags & PERF_FLAG_FD_OUTPUT)
5343 output_event = group_leader;
5344 if (flags & PERF_FLAG_FD_NO_GROUP)
5345 group_leader = NULL;
5349 * Look up the group leader (we will attach this event to it):
5355 * Do not allow a recursive hierarchy (this new sibling
5356 * becoming part of another group-sibling):
5358 if (group_leader->group_leader != group_leader)
5359 goto err_put_context;
5361 * Do not allow to attach to a group in a different
5362 * task or CPU context:
5364 if (group_leader->ctx != ctx)
5365 goto err_put_context;
5367 * Only a group leader can be exclusive or pinned
5369 if (attr.exclusive || attr.pinned)
5370 goto err_put_context;
5373 event = perf_event_alloc(&attr, cpu, ctx, group_leader,
5374 NULL, NULL, GFP_KERNEL);
5375 if (IS_ERR(event)) {
5376 err = PTR_ERR(event);
5377 goto err_put_context;
5381 err = perf_event_set_output(event, output_event);
5383 goto err_free_put_context;
5386 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5387 if (IS_ERR(event_file)) {
5388 err = PTR_ERR(event_file);
5389 goto err_free_put_context;
5392 event->filp = event_file;
5393 WARN_ON_ONCE(ctx->parent_ctx);
5394 mutex_lock(&ctx->mutex);
5395 perf_install_in_context(ctx, event, cpu);
5397 mutex_unlock(&ctx->mutex);
5399 event->owner = current;
5400 get_task_struct(current);
5401 mutex_lock(¤t->perf_event_mutex);
5402 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
5403 mutex_unlock(¤t->perf_event_mutex);
5406 * Drop the reference on the group_event after placing the
5407 * new event on the sibling_list. This ensures destruction
5408 * of the group leader will find the pointer to itself in
5409 * perf_group_detach().
5411 fput_light(group_file, fput_needed);
5412 fd_install(event_fd, event_file);
5415 err_free_put_context:
5418 fput_light(group_file, fput_needed);
5421 put_unused_fd(event_fd);
5426 * perf_event_create_kernel_counter
5428 * @attr: attributes of the counter to create
5429 * @cpu: cpu in which the counter is bound
5430 * @pid: task to profile
5433 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
5435 perf_overflow_handler_t overflow_handler)
5437 struct perf_event *event;
5438 struct perf_event_context *ctx;
5442 * Get the target context (task or percpu):
5445 ctx = find_get_context(pid, cpu);
5451 event = perf_event_alloc(attr, cpu, ctx, NULL,
5452 NULL, overflow_handler, GFP_KERNEL);
5453 if (IS_ERR(event)) {
5454 err = PTR_ERR(event);
5455 goto err_put_context;
5459 WARN_ON_ONCE(ctx->parent_ctx);
5460 mutex_lock(&ctx->mutex);
5461 perf_install_in_context(ctx, event, cpu);
5463 mutex_unlock(&ctx->mutex);
5465 event->owner = current;
5466 get_task_struct(current);
5467 mutex_lock(¤t->perf_event_mutex);
5468 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
5469 mutex_unlock(¤t->perf_event_mutex);
5476 return ERR_PTR(err);
5478 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5481 * inherit a event from parent task to child task:
5483 static struct perf_event *
5484 inherit_event(struct perf_event *parent_event,
5485 struct task_struct *parent,
5486 struct perf_event_context *parent_ctx,
5487 struct task_struct *child,
5488 struct perf_event *group_leader,
5489 struct perf_event_context *child_ctx)
5491 struct perf_event *child_event;
5494 * Instead of creating recursive hierarchies of events,
5495 * we link inherited events back to the original parent,
5496 * which has a filp for sure, which we use as the reference
5499 if (parent_event->parent)
5500 parent_event = parent_event->parent;
5502 child_event = perf_event_alloc(&parent_event->attr,
5503 parent_event->cpu, child_ctx,
5504 group_leader, parent_event,
5506 if (IS_ERR(child_event))
5511 * Make the child state follow the state of the parent event,
5512 * not its attr.disabled bit. We hold the parent's mutex,
5513 * so we won't race with perf_event_{en, dis}able_family.
5515 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
5516 child_event->state = PERF_EVENT_STATE_INACTIVE;
5518 child_event->state = PERF_EVENT_STATE_OFF;
5520 if (parent_event->attr.freq) {
5521 u64 sample_period = parent_event->hw.sample_period;
5522 struct hw_perf_event *hwc = &child_event->hw;
5524 hwc->sample_period = sample_period;
5525 hwc->last_period = sample_period;
5527 local64_set(&hwc->period_left, sample_period);
5530 child_event->overflow_handler = parent_event->overflow_handler;
5533 * Link it up in the child's context:
5535 add_event_to_ctx(child_event, child_ctx);
5538 * Get a reference to the parent filp - we will fput it
5539 * when the child event exits. This is safe to do because
5540 * we are in the parent and we know that the filp still
5541 * exists and has a nonzero count:
5543 atomic_long_inc(&parent_event->filp->f_count);
5546 * Link this into the parent event's child list
5548 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5549 mutex_lock(&parent_event->child_mutex);
5550 list_add_tail(&child_event->child_list, &parent_event->child_list);
5551 mutex_unlock(&parent_event->child_mutex);
5556 static int inherit_group(struct perf_event *parent_event,
5557 struct task_struct *parent,
5558 struct perf_event_context *parent_ctx,
5559 struct task_struct *child,
5560 struct perf_event_context *child_ctx)
5562 struct perf_event *leader;
5563 struct perf_event *sub;
5564 struct perf_event *child_ctr;
5566 leader = inherit_event(parent_event, parent, parent_ctx,
5567 child, NULL, child_ctx);
5569 return PTR_ERR(leader);
5570 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
5571 child_ctr = inherit_event(sub, parent, parent_ctx,
5572 child, leader, child_ctx);
5573 if (IS_ERR(child_ctr))
5574 return PTR_ERR(child_ctr);
5579 static void sync_child_event(struct perf_event *child_event,
5580 struct task_struct *child)
5582 struct perf_event *parent_event = child_event->parent;
5585 if (child_event->attr.inherit_stat)
5586 perf_event_read_event(child_event, child);
5588 child_val = perf_event_count(child_event);
5591 * Add back the child's count to the parent's count:
5593 atomic64_add(child_val, &parent_event->child_count);
5594 atomic64_add(child_event->total_time_enabled,
5595 &parent_event->child_total_time_enabled);
5596 atomic64_add(child_event->total_time_running,
5597 &parent_event->child_total_time_running);
5600 * Remove this event from the parent's list
5602 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5603 mutex_lock(&parent_event->child_mutex);
5604 list_del_init(&child_event->child_list);
5605 mutex_unlock(&parent_event->child_mutex);
5608 * Release the parent event, if this was the last
5611 fput(parent_event->filp);
5615 __perf_event_exit_task(struct perf_event *child_event,
5616 struct perf_event_context *child_ctx,
5617 struct task_struct *child)
5619 struct perf_event *parent_event;
5621 perf_event_remove_from_context(child_event);
5623 parent_event = child_event->parent;
5625 * It can happen that parent exits first, and has events
5626 * that are still around due to the child reference. These
5627 * events need to be zapped - but otherwise linger.
5630 sync_child_event(child_event, child);
5631 free_event(child_event);
5636 * When a child task exits, feed back event values to parent events.
5638 void perf_event_exit_task(struct task_struct *child)
5640 struct perf_event *child_event, *tmp;
5641 struct perf_event_context *child_ctx;
5642 unsigned long flags;
5644 if (likely(!child->perf_event_ctxp)) {
5645 perf_event_task(child, NULL, 0);
5649 local_irq_save(flags);
5651 * We can't reschedule here because interrupts are disabled,
5652 * and either child is current or it is a task that can't be
5653 * scheduled, so we are now safe from rescheduling changing
5656 child_ctx = child->perf_event_ctxp;
5657 __perf_event_task_sched_out(child_ctx);
5660 * Take the context lock here so that if find_get_context is
5661 * reading child->perf_event_ctxp, we wait until it has
5662 * incremented the context's refcount before we do put_ctx below.
5664 raw_spin_lock(&child_ctx->lock);
5665 child->perf_event_ctxp = NULL;
5667 * If this context is a clone; unclone it so it can't get
5668 * swapped to another process while we're removing all
5669 * the events from it.
5671 unclone_ctx(child_ctx);
5672 update_context_time(child_ctx);
5673 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
5676 * Report the task dead after unscheduling the events so that we
5677 * won't get any samples after PERF_RECORD_EXIT. We can however still
5678 * get a few PERF_RECORD_READ events.
5680 perf_event_task(child, child_ctx, 0);
5683 * We can recurse on the same lock type through:
5685 * __perf_event_exit_task()
5686 * sync_child_event()
5687 * fput(parent_event->filp)
5689 * mutex_lock(&ctx->mutex)
5691 * But since its the parent context it won't be the same instance.
5693 mutex_lock(&child_ctx->mutex);
5696 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5698 __perf_event_exit_task(child_event, child_ctx, child);
5700 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
5702 __perf_event_exit_task(child_event, child_ctx, child);
5705 * If the last event was a group event, it will have appended all
5706 * its siblings to the list, but we obtained 'tmp' before that which
5707 * will still point to the list head terminating the iteration.
5709 if (!list_empty(&child_ctx->pinned_groups) ||
5710 !list_empty(&child_ctx->flexible_groups))
5713 mutex_unlock(&child_ctx->mutex);
5718 static void perf_free_event(struct perf_event *event,
5719 struct perf_event_context *ctx)
5721 struct perf_event *parent = event->parent;
5723 if (WARN_ON_ONCE(!parent))
5726 mutex_lock(&parent->child_mutex);
5727 list_del_init(&event->child_list);
5728 mutex_unlock(&parent->child_mutex);
5732 perf_group_detach(event);
5733 list_del_event(event, ctx);
5738 * free an unexposed, unused context as created by inheritance by
5739 * init_task below, used by fork() in case of fail.
5741 void perf_event_free_task(struct task_struct *task)
5743 struct perf_event_context *ctx = task->perf_event_ctxp;
5744 struct perf_event *event, *tmp;
5749 mutex_lock(&ctx->mutex);
5751 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5752 perf_free_event(event, ctx);
5754 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5756 perf_free_event(event, ctx);
5758 if (!list_empty(&ctx->pinned_groups) ||
5759 !list_empty(&ctx->flexible_groups))
5762 mutex_unlock(&ctx->mutex);
5768 inherit_task_group(struct perf_event *event, struct task_struct *parent,
5769 struct perf_event_context *parent_ctx,
5770 struct task_struct *child,
5774 struct perf_event_context *child_ctx = child->perf_event_ctxp;
5776 if (!event->attr.inherit) {
5783 * This is executed from the parent task context, so
5784 * inherit events that have been marked for cloning.
5785 * First allocate and initialize a context for the
5789 child_ctx = kzalloc(sizeof(struct perf_event_context),
5794 __perf_event_init_context(child_ctx, child);
5795 child->perf_event_ctxp = child_ctx;
5796 get_task_struct(child);
5799 ret = inherit_group(event, parent, parent_ctx,
5810 * Initialize the perf_event context in task_struct
5812 int perf_event_init_task(struct task_struct *child)
5814 struct perf_event_context *child_ctx, *parent_ctx;
5815 struct perf_event_context *cloned_ctx;
5816 struct perf_event *event;
5817 struct task_struct *parent = current;
5818 int inherited_all = 1;
5821 child->perf_event_ctxp = NULL;
5823 mutex_init(&child->perf_event_mutex);
5824 INIT_LIST_HEAD(&child->perf_event_list);
5826 if (likely(!parent->perf_event_ctxp))
5830 * If the parent's context is a clone, pin it so it won't get
5833 parent_ctx = perf_pin_task_context(parent);
5836 * No need to check if parent_ctx != NULL here; since we saw
5837 * it non-NULL earlier, the only reason for it to become NULL
5838 * is if we exit, and since we're currently in the middle of
5839 * a fork we can't be exiting at the same time.
5843 * Lock the parent list. No need to lock the child - not PID
5844 * hashed yet and not running, so nobody can access it.
5846 mutex_lock(&parent_ctx->mutex);
5849 * We dont have to disable NMIs - we are only looking at
5850 * the list, not manipulating it:
5852 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
5853 ret = inherit_task_group(event, parent, parent_ctx, child,
5859 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
5860 ret = inherit_task_group(event, parent, parent_ctx, child,
5866 child_ctx = child->perf_event_ctxp;
5868 if (child_ctx && inherited_all) {
5870 * Mark the child context as a clone of the parent
5871 * context, or of whatever the parent is a clone of.
5872 * Note that if the parent is a clone, it could get
5873 * uncloned at any point, but that doesn't matter
5874 * because the list of events and the generation
5875 * count can't have changed since we took the mutex.
5877 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
5879 child_ctx->parent_ctx = cloned_ctx;
5880 child_ctx->parent_gen = parent_ctx->parent_gen;
5882 child_ctx->parent_ctx = parent_ctx;
5883 child_ctx->parent_gen = parent_ctx->generation;
5885 get_ctx(child_ctx->parent_ctx);
5888 mutex_unlock(&parent_ctx->mutex);
5890 perf_unpin_context(parent_ctx);
5895 static void __init perf_event_init_all_cpus(void)
5898 struct perf_cpu_context *cpuctx;
5900 for_each_possible_cpu(cpu) {
5901 cpuctx = &per_cpu(perf_cpu_context, cpu);
5902 mutex_init(&cpuctx->hlist_mutex);
5903 __perf_event_init_context(&cpuctx->ctx, NULL);
5907 static void __cpuinit perf_event_init_cpu(int cpu)
5909 struct perf_cpu_context *cpuctx;
5911 cpuctx = &per_cpu(perf_cpu_context, cpu);
5913 spin_lock(&perf_resource_lock);
5914 cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
5915 spin_unlock(&perf_resource_lock);
5917 mutex_lock(&cpuctx->hlist_mutex);
5918 if (cpuctx->hlist_refcount > 0) {
5919 struct swevent_hlist *hlist;
5921 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5922 WARN_ON_ONCE(!hlist);
5923 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
5925 mutex_unlock(&cpuctx->hlist_mutex);
5928 #ifdef CONFIG_HOTPLUG_CPU
5929 static void __perf_event_exit_cpu(void *info)
5931 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
5932 struct perf_event_context *ctx = &cpuctx->ctx;
5933 struct perf_event *event, *tmp;
5935 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5936 __perf_event_remove_from_context(event);
5937 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
5938 __perf_event_remove_from_context(event);
5940 static void perf_event_exit_cpu(int cpu)
5942 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
5943 struct perf_event_context *ctx = &cpuctx->ctx;
5945 mutex_lock(&cpuctx->hlist_mutex);
5946 swevent_hlist_release(cpuctx);
5947 mutex_unlock(&cpuctx->hlist_mutex);
5949 mutex_lock(&ctx->mutex);
5950 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
5951 mutex_unlock(&ctx->mutex);
5954 static inline void perf_event_exit_cpu(int cpu) { }
5957 static int __cpuinit
5958 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
5960 unsigned int cpu = (long)hcpu;
5962 switch (action & ~CPU_TASKS_FROZEN) {
5964 case CPU_UP_PREPARE:
5965 case CPU_DOWN_FAILED:
5966 perf_event_init_cpu(cpu);
5969 case CPU_UP_CANCELED:
5970 case CPU_DOWN_PREPARE:
5971 perf_event_exit_cpu(cpu);
5981 void __init perf_event_init(void)
5983 perf_event_init_all_cpus();
5984 init_srcu_struct(&pmus_srcu);
5985 perf_pmu_register(&perf_swevent);
5986 perf_pmu_register(&perf_cpu_clock);
5987 perf_pmu_register(&perf_task_clock);
5989 perf_cpu_notifier(perf_cpu_notify);
5992 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class,
5993 struct sysdev_class_attribute *attr,
5996 return sprintf(buf, "%d\n", perf_reserved_percpu);
6000 perf_set_reserve_percpu(struct sysdev_class *class,
6001 struct sysdev_class_attribute *attr,
6005 struct perf_cpu_context *cpuctx;
6009 err = strict_strtoul(buf, 10, &val);
6012 if (val > perf_max_events)
6015 spin_lock(&perf_resource_lock);
6016 perf_reserved_percpu = val;
6017 for_each_online_cpu(cpu) {
6018 cpuctx = &per_cpu(perf_cpu_context, cpu);
6019 raw_spin_lock_irq(&cpuctx->ctx.lock);
6020 mpt = min(perf_max_events - cpuctx->ctx.nr_events,
6021 perf_max_events - perf_reserved_percpu);
6022 cpuctx->max_pertask = mpt;
6023 raw_spin_unlock_irq(&cpuctx->ctx.lock);
6025 spin_unlock(&perf_resource_lock);
6030 static ssize_t perf_show_overcommit(struct sysdev_class *class,
6031 struct sysdev_class_attribute *attr,
6034 return sprintf(buf, "%d\n", perf_overcommit);
6038 perf_set_overcommit(struct sysdev_class *class,
6039 struct sysdev_class_attribute *attr,
6040 const char *buf, size_t count)
6045 err = strict_strtoul(buf, 10, &val);
6051 spin_lock(&perf_resource_lock);
6052 perf_overcommit = val;
6053 spin_unlock(&perf_resource_lock);
6058 static SYSDEV_CLASS_ATTR(
6061 perf_show_reserve_percpu,
6062 perf_set_reserve_percpu
6065 static SYSDEV_CLASS_ATTR(
6068 perf_show_overcommit,
6072 static struct attribute *perfclass_attrs[] = {
6073 &attr_reserve_percpu.attr,
6074 &attr_overcommit.attr,
6078 static struct attribute_group perfclass_attr_group = {
6079 .attrs = perfclass_attrs,
6080 .name = "perf_events",
6083 static int __init perf_event_sysfs_init(void)
6085 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
6086 &perfclass_attr_group);
6088 device_initcall(perf_event_sysfs_init);