2 * Performance events core code:
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/idr.h>
17 #include <linux/file.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/hash.h>
21 #include <linux/sysfs.h>
22 #include <linux/dcache.h>
23 #include <linux/percpu.h>
24 #include <linux/ptrace.h>
25 #include <linux/reboot.h>
26 #include <linux/vmstat.h>
27 #include <linux/device.h>
28 #include <linux/vmalloc.h>
29 #include <linux/hardirq.h>
30 #include <linux/rculist.h>
31 #include <linux/uaccess.h>
32 #include <linux/syscalls.h>
33 #include <linux/anon_inodes.h>
34 #include <linux/kernel_stat.h>
35 #include <linux/perf_event.h>
36 #include <linux/ftrace_event.h>
37 #include <linux/hw_breakpoint.h>
39 #include <asm/irq_regs.h>
41 struct remote_function_call {
42 struct task_struct *p;
43 int (*func)(void *info);
48 static void remote_function(void *data)
50 struct remote_function_call *tfc = data;
51 struct task_struct *p = tfc->p;
55 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
59 tfc->ret = tfc->func(tfc->info);
63 * task_function_call - call a function on the cpu on which a task runs
64 * @p: the task to evaluate
65 * @func: the function to be called
66 * @info: the function call argument
68 * Calls the function @func when the task is currently running. This might
69 * be on the current CPU, which just calls the function directly
71 * returns: @func return value, or
72 * -ESRCH - when the process isn't running
73 * -EAGAIN - when the process moved away
76 task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
78 struct remote_function_call data = {
82 .ret = -ESRCH, /* No such (running) process */
86 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
92 * cpu_function_call - call a function on the cpu
93 * @func: the function to be called
94 * @info: the function call argument
96 * Calls the function @func on the remote cpu.
98 * returns: @func return value or -ENXIO when the cpu is offline
100 static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
102 struct remote_function_call data = {
106 .ret = -ENXIO, /* No such CPU */
109 smp_call_function_single(cpu, remote_function, &data, 1);
114 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
115 PERF_FLAG_FD_OUTPUT |\
116 PERF_FLAG_PID_CGROUP)
119 EVENT_FLEXIBLE = 0x1,
121 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
125 * perf_sched_events : >0 events exist
126 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
128 atomic_t perf_sched_events __read_mostly;
129 static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
131 static atomic_t nr_mmap_events __read_mostly;
132 static atomic_t nr_comm_events __read_mostly;
133 static atomic_t nr_task_events __read_mostly;
135 static LIST_HEAD(pmus);
136 static DEFINE_MUTEX(pmus_lock);
137 static struct srcu_struct pmus_srcu;
140 * perf event paranoia level:
141 * -1 - not paranoid at all
142 * 0 - disallow raw tracepoint access for unpriv
143 * 1 - disallow cpu events for unpriv
144 * 2 - disallow kernel profiling for unpriv
146 int sysctl_perf_event_paranoid __read_mostly = 1;
148 int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
151 * max perf event sample rate
153 int sysctl_perf_event_sample_rate __read_mostly = 100000;
155 static atomic64_t perf_event_id;
157 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
158 enum event_type_t event_type);
160 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
161 enum event_type_t event_type,
162 struct task_struct *task);
164 static void update_context_time(struct perf_event_context *ctx);
165 static u64 perf_event_time(struct perf_event *event);
167 void __weak perf_event_print_debug(void) { }
169 extern __weak const char *perf_pmu_name(void)
174 static inline u64 perf_clock(void)
176 return local_clock();
179 static inline struct perf_cpu_context *
180 __get_cpu_context(struct perf_event_context *ctx)
182 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
185 #ifdef CONFIG_CGROUP_PERF
187 static inline struct perf_cgroup *
188 perf_cgroup_from_task(struct task_struct *task)
190 return container_of(task_subsys_state(task, perf_subsys_id),
191 struct perf_cgroup, css);
195 perf_cgroup_match(struct perf_event *event)
197 struct perf_event_context *ctx = event->ctx;
198 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
200 return !event->cgrp || event->cgrp == cpuctx->cgrp;
203 static inline void perf_get_cgroup(struct perf_event *event)
205 css_get(&event->cgrp->css);
208 static inline void perf_put_cgroup(struct perf_event *event)
210 css_put(&event->cgrp->css);
213 static inline void perf_detach_cgroup(struct perf_event *event)
215 perf_put_cgroup(event);
219 static inline int is_cgroup_event(struct perf_event *event)
221 return event->cgrp != NULL;
224 static inline u64 perf_cgroup_event_time(struct perf_event *event)
226 struct perf_cgroup_info *t;
228 t = per_cpu_ptr(event->cgrp->info, event->cpu);
232 static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
234 struct perf_cgroup_info *info;
239 info = this_cpu_ptr(cgrp->info);
241 info->time += now - info->timestamp;
242 info->timestamp = now;
245 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
247 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
249 __update_cgrp_time(cgrp_out);
252 static inline void update_cgrp_time_from_event(struct perf_event *event)
254 struct perf_cgroup *cgrp = perf_cgroup_from_task(current);
256 * do not update time when cgroup is not active
258 if (!event->cgrp || cgrp != event->cgrp)
261 __update_cgrp_time(event->cgrp);
265 perf_cgroup_set_timestamp(struct task_struct *task, u64 now)
267 struct perf_cgroup *cgrp;
268 struct perf_cgroup_info *info;
273 cgrp = perf_cgroup_from_task(task);
274 info = this_cpu_ptr(cgrp->info);
275 info->timestamp = now;
278 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
279 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
282 * reschedule events based on the cgroup constraint of task.
284 * mode SWOUT : schedule out everything
285 * mode SWIN : schedule in based on cgroup for next
287 void perf_cgroup_switch(struct task_struct *task, int mode)
289 struct perf_cpu_context *cpuctx;
294 * disable interrupts to avoid geting nr_cgroup
295 * changes via __perf_event_disable(). Also
298 local_irq_save(flags);
301 * we reschedule only in the presence of cgroup
302 * constrained events.
306 list_for_each_entry_rcu(pmu, &pmus, entry) {
308 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
310 perf_pmu_disable(cpuctx->ctx.pmu);
313 * perf_cgroup_events says at least one
314 * context on this CPU has cgroup events.
316 * ctx->nr_cgroups reports the number of cgroup
317 * events for a context.
319 if (cpuctx->ctx.nr_cgroups > 0) {
321 if (mode & PERF_CGROUP_SWOUT) {
322 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
324 * must not be done before ctxswout due
325 * to event_filter_match() in event_sched_out()
330 if (mode & PERF_CGROUP_SWIN) {
331 /* set cgrp before ctxsw in to
332 * allow event_filter_match() to not
333 * have to pass task around
335 cpuctx->cgrp = perf_cgroup_from_task(task);
336 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
340 perf_pmu_enable(cpuctx->ctx.pmu);
345 local_irq_restore(flags);
348 static inline void perf_cgroup_sched_out(struct task_struct *task)
350 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
353 static inline void perf_cgroup_sched_in(struct task_struct *task)
355 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
358 static inline int perf_cgroup_connect(int fd, struct perf_event *event,
359 struct perf_event_attr *attr,
360 struct perf_event *group_leader)
362 struct perf_cgroup *cgrp;
363 struct cgroup_subsys_state *css;
365 int ret = 0, fput_needed;
367 file = fget_light(fd, &fput_needed);
371 css = cgroup_css_from_dir(file, perf_subsys_id);
375 cgrp = container_of(css, struct perf_cgroup, css);
379 * all events in a group must monitor
380 * the same cgroup because a task belongs
381 * to only one perf cgroup at a time
383 if (group_leader && group_leader->cgrp != cgrp) {
384 perf_detach_cgroup(event);
387 /* must be done before we fput() the file */
388 perf_get_cgroup(event);
390 fput_light(file, fput_needed);
395 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
397 struct perf_cgroup_info *t;
398 t = per_cpu_ptr(event->cgrp->info, event->cpu);
399 event->shadow_ctx_time = now - t->timestamp;
403 perf_cgroup_defer_enabled(struct perf_event *event)
406 * when the current task's perf cgroup does not match
407 * the event's, we need to remember to call the
408 * perf_mark_enable() function the first time a task with
409 * a matching perf cgroup is scheduled in.
411 if (is_cgroup_event(event) && !perf_cgroup_match(event))
412 event->cgrp_defer_enabled = 1;
416 perf_cgroup_mark_enabled(struct perf_event *event,
417 struct perf_event_context *ctx)
419 struct perf_event *sub;
420 u64 tstamp = perf_event_time(event);
422 if (!event->cgrp_defer_enabled)
425 event->cgrp_defer_enabled = 0;
427 event->tstamp_enabled = tstamp - event->total_time_enabled;
428 list_for_each_entry(sub, &event->sibling_list, group_entry) {
429 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
430 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
431 sub->cgrp_defer_enabled = 0;
435 #else /* !CONFIG_CGROUP_PERF */
438 perf_cgroup_match(struct perf_event *event)
443 static inline void perf_detach_cgroup(struct perf_event *event)
446 static inline int is_cgroup_event(struct perf_event *event)
451 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
456 static inline void update_cgrp_time_from_event(struct perf_event *event)
460 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
464 static inline void perf_cgroup_sched_out(struct task_struct *task)
468 static inline void perf_cgroup_sched_in(struct task_struct *task)
472 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
473 struct perf_event_attr *attr,
474 struct perf_event *group_leader)
480 perf_cgroup_set_timestamp(struct task_struct *task, u64 now)
485 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
490 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
494 static inline u64 perf_cgroup_event_time(struct perf_event *event)
500 perf_cgroup_defer_enabled(struct perf_event *event)
505 perf_cgroup_mark_enabled(struct perf_event *event,
506 struct perf_event_context *ctx)
511 void perf_pmu_disable(struct pmu *pmu)
513 int *count = this_cpu_ptr(pmu->pmu_disable_count);
515 pmu->pmu_disable(pmu);
518 void perf_pmu_enable(struct pmu *pmu)
520 int *count = this_cpu_ptr(pmu->pmu_disable_count);
522 pmu->pmu_enable(pmu);
525 static DEFINE_PER_CPU(struct list_head, rotation_list);
528 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
529 * because they're strictly cpu affine and rotate_start is called with IRQs
530 * disabled, while rotate_context is called from IRQ context.
532 static void perf_pmu_rotate_start(struct pmu *pmu)
534 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
535 struct list_head *head = &__get_cpu_var(rotation_list);
537 WARN_ON(!irqs_disabled());
539 if (list_empty(&cpuctx->rotation_list))
540 list_add(&cpuctx->rotation_list, head);
543 static void get_ctx(struct perf_event_context *ctx)
545 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
548 static void free_ctx(struct rcu_head *head)
550 struct perf_event_context *ctx;
552 ctx = container_of(head, struct perf_event_context, rcu_head);
556 static void put_ctx(struct perf_event_context *ctx)
558 if (atomic_dec_and_test(&ctx->refcount)) {
560 put_ctx(ctx->parent_ctx);
562 put_task_struct(ctx->task);
563 call_rcu(&ctx->rcu_head, free_ctx);
567 static void unclone_ctx(struct perf_event_context *ctx)
569 if (ctx->parent_ctx) {
570 put_ctx(ctx->parent_ctx);
571 ctx->parent_ctx = NULL;
575 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
578 * only top level events have the pid namespace they were created in
581 event = event->parent;
583 return task_tgid_nr_ns(p, event->ns);
586 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
589 * only top level events have the pid namespace they were created in
592 event = event->parent;
594 return task_pid_nr_ns(p, event->ns);
598 * If we inherit events we want to return the parent event id
601 static u64 primary_event_id(struct perf_event *event)
606 id = event->parent->id;
612 * Get the perf_event_context for a task and lock it.
613 * This has to cope with with the fact that until it is locked,
614 * the context could get moved to another task.
616 static struct perf_event_context *
617 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
619 struct perf_event_context *ctx;
623 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
626 * If this context is a clone of another, it might
627 * get swapped for another underneath us by
628 * perf_event_task_sched_out, though the
629 * rcu_read_lock() protects us from any context
630 * getting freed. Lock the context and check if it
631 * got swapped before we could get the lock, and retry
632 * if so. If we locked the right context, then it
633 * can't get swapped on us any more.
635 raw_spin_lock_irqsave(&ctx->lock, *flags);
636 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
637 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
641 if (!atomic_inc_not_zero(&ctx->refcount)) {
642 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
651 * Get the context for a task and increment its pin_count so it
652 * can't get swapped to another task. This also increments its
653 * reference count so that the context can't get freed.
655 static struct perf_event_context *
656 perf_pin_task_context(struct task_struct *task, int ctxn)
658 struct perf_event_context *ctx;
661 ctx = perf_lock_task_context(task, ctxn, &flags);
664 raw_spin_unlock_irqrestore(&ctx->lock, flags);
669 static void perf_unpin_context(struct perf_event_context *ctx)
673 raw_spin_lock_irqsave(&ctx->lock, flags);
675 raw_spin_unlock_irqrestore(&ctx->lock, flags);
679 * Update the record of the current time in a context.
681 static void update_context_time(struct perf_event_context *ctx)
683 u64 now = perf_clock();
685 ctx->time += now - ctx->timestamp;
686 ctx->timestamp = now;
689 static u64 perf_event_time(struct perf_event *event)
691 struct perf_event_context *ctx = event->ctx;
693 if (is_cgroup_event(event))
694 return perf_cgroup_event_time(event);
696 return ctx ? ctx->time : 0;
700 * Update the total_time_enabled and total_time_running fields for a event.
702 static void update_event_times(struct perf_event *event)
704 struct perf_event_context *ctx = event->ctx;
707 if (event->state < PERF_EVENT_STATE_INACTIVE ||
708 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
711 * in cgroup mode, time_enabled represents
712 * the time the event was enabled AND active
713 * tasks were in the monitored cgroup. This is
714 * independent of the activity of the context as
715 * there may be a mix of cgroup and non-cgroup events.
717 * That is why we treat cgroup events differently
720 if (is_cgroup_event(event))
721 run_end = perf_event_time(event);
722 else if (ctx->is_active)
725 run_end = event->tstamp_stopped;
727 event->total_time_enabled = run_end - event->tstamp_enabled;
729 if (event->state == PERF_EVENT_STATE_INACTIVE)
730 run_end = event->tstamp_stopped;
732 run_end = perf_event_time(event);
734 event->total_time_running = run_end - event->tstamp_running;
739 * Update total_time_enabled and total_time_running for all events in a group.
741 static void update_group_times(struct perf_event *leader)
743 struct perf_event *event;
745 update_event_times(leader);
746 list_for_each_entry(event, &leader->sibling_list, group_entry)
747 update_event_times(event);
750 static struct list_head *
751 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
753 if (event->attr.pinned)
754 return &ctx->pinned_groups;
756 return &ctx->flexible_groups;
760 * Add a event from the lists for its context.
761 * Must be called with ctx->mutex and ctx->lock held.
764 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
766 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
767 event->attach_state |= PERF_ATTACH_CONTEXT;
770 * If we're a stand alone event or group leader, we go to the context
771 * list, group events are kept attached to the group so that
772 * perf_group_detach can, at all times, locate all siblings.
774 if (event->group_leader == event) {
775 struct list_head *list;
777 if (is_software_event(event))
778 event->group_flags |= PERF_GROUP_SOFTWARE;
780 list = ctx_group_list(event, ctx);
781 list_add_tail(&event->group_entry, list);
784 if (is_cgroup_event(event)) {
788 * - that has cgroup constraint on event->cpu
789 * - that may need work on context switch
791 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
792 jump_label_inc(&perf_sched_events);
795 list_add_rcu(&event->event_entry, &ctx->event_list);
797 perf_pmu_rotate_start(ctx->pmu);
799 if (event->attr.inherit_stat)
804 * Called at perf_event creation and when events are attached/detached from a
807 static void perf_event__read_size(struct perf_event *event)
809 int entry = sizeof(u64); /* value */
813 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
816 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
819 if (event->attr.read_format & PERF_FORMAT_ID)
820 entry += sizeof(u64);
822 if (event->attr.read_format & PERF_FORMAT_GROUP) {
823 nr += event->group_leader->nr_siblings;
828 event->read_size = size;
831 static void perf_event__header_size(struct perf_event *event)
833 struct perf_sample_data *data;
834 u64 sample_type = event->attr.sample_type;
837 perf_event__read_size(event);
839 if (sample_type & PERF_SAMPLE_IP)
840 size += sizeof(data->ip);
842 if (sample_type & PERF_SAMPLE_ADDR)
843 size += sizeof(data->addr);
845 if (sample_type & PERF_SAMPLE_PERIOD)
846 size += sizeof(data->period);
848 if (sample_type & PERF_SAMPLE_READ)
849 size += event->read_size;
851 event->header_size = size;
854 static void perf_event__id_header_size(struct perf_event *event)
856 struct perf_sample_data *data;
857 u64 sample_type = event->attr.sample_type;
860 if (sample_type & PERF_SAMPLE_TID)
861 size += sizeof(data->tid_entry);
863 if (sample_type & PERF_SAMPLE_TIME)
864 size += sizeof(data->time);
866 if (sample_type & PERF_SAMPLE_ID)
867 size += sizeof(data->id);
869 if (sample_type & PERF_SAMPLE_STREAM_ID)
870 size += sizeof(data->stream_id);
872 if (sample_type & PERF_SAMPLE_CPU)
873 size += sizeof(data->cpu_entry);
875 event->id_header_size = size;
878 static void perf_group_attach(struct perf_event *event)
880 struct perf_event *group_leader = event->group_leader, *pos;
883 * We can have double attach due to group movement in perf_event_open.
885 if (event->attach_state & PERF_ATTACH_GROUP)
888 event->attach_state |= PERF_ATTACH_GROUP;
890 if (group_leader == event)
893 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
894 !is_software_event(event))
895 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
897 list_add_tail(&event->group_entry, &group_leader->sibling_list);
898 group_leader->nr_siblings++;
900 perf_event__header_size(group_leader);
902 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
903 perf_event__header_size(pos);
907 * Remove a event from the lists for its context.
908 * Must be called with ctx->mutex and ctx->lock held.
911 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
914 * We can have double detach due to exit/hot-unplug + close.
916 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
919 event->attach_state &= ~PERF_ATTACH_CONTEXT;
921 if (is_cgroup_event(event)) {
923 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
924 jump_label_dec(&perf_sched_events);
928 if (event->attr.inherit_stat)
931 list_del_rcu(&event->event_entry);
933 if (event->group_leader == event)
934 list_del_init(&event->group_entry);
936 update_group_times(event);
939 * If event was in error state, then keep it
940 * that way, otherwise bogus counts will be
941 * returned on read(). The only way to get out
942 * of error state is by explicit re-enabling
945 if (event->state > PERF_EVENT_STATE_OFF)
946 event->state = PERF_EVENT_STATE_OFF;
949 static void perf_group_detach(struct perf_event *event)
951 struct perf_event *sibling, *tmp;
952 struct list_head *list = NULL;
955 * We can have double detach due to exit/hot-unplug + close.
957 if (!(event->attach_state & PERF_ATTACH_GROUP))
960 event->attach_state &= ~PERF_ATTACH_GROUP;
963 * If this is a sibling, remove it from its group.
965 if (event->group_leader != event) {
966 list_del_init(&event->group_entry);
967 event->group_leader->nr_siblings--;
971 if (!list_empty(&event->group_entry))
972 list = &event->group_entry;
975 * If this was a group event with sibling events then
976 * upgrade the siblings to singleton events by adding them
977 * to whatever list we are on.
979 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
981 list_move_tail(&sibling->group_entry, list);
982 sibling->group_leader = sibling;
984 /* Inherit group flags from the previous leader */
985 sibling->group_flags = event->group_flags;
989 perf_event__header_size(event->group_leader);
991 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
992 perf_event__header_size(tmp);
996 event_filter_match(struct perf_event *event)
998 return (event->cpu == -1 || event->cpu == smp_processor_id())
999 && perf_cgroup_match(event);
1003 event_sched_out(struct perf_event *event,
1004 struct perf_cpu_context *cpuctx,
1005 struct perf_event_context *ctx)
1007 u64 tstamp = perf_event_time(event);
1010 * An event which could not be activated because of
1011 * filter mismatch still needs to have its timings
1012 * maintained, otherwise bogus information is return
1013 * via read() for time_enabled, time_running:
1015 if (event->state == PERF_EVENT_STATE_INACTIVE
1016 && !event_filter_match(event)) {
1017 delta = tstamp - event->tstamp_stopped;
1018 event->tstamp_running += delta;
1019 event->tstamp_stopped = tstamp;
1022 if (event->state != PERF_EVENT_STATE_ACTIVE)
1025 event->state = PERF_EVENT_STATE_INACTIVE;
1026 if (event->pending_disable) {
1027 event->pending_disable = 0;
1028 event->state = PERF_EVENT_STATE_OFF;
1030 event->tstamp_stopped = tstamp;
1031 event->pmu->del(event, 0);
1034 if (!is_software_event(event))
1035 cpuctx->active_oncpu--;
1037 if (event->attr.exclusive || !cpuctx->active_oncpu)
1038 cpuctx->exclusive = 0;
1042 group_sched_out(struct perf_event *group_event,
1043 struct perf_cpu_context *cpuctx,
1044 struct perf_event_context *ctx)
1046 struct perf_event *event;
1047 int state = group_event->state;
1049 event_sched_out(group_event, cpuctx, ctx);
1052 * Schedule out siblings (if any):
1054 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1055 event_sched_out(event, cpuctx, ctx);
1057 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
1058 cpuctx->exclusive = 0;
1062 * Cross CPU call to remove a performance event
1064 * We disable the event on the hardware level first. After that we
1065 * remove it from the context list.
1067 static int __perf_remove_from_context(void *info)
1069 struct perf_event *event = info;
1070 struct perf_event_context *ctx = event->ctx;
1071 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1073 raw_spin_lock(&ctx->lock);
1074 event_sched_out(event, cpuctx, ctx);
1075 list_del_event(event, ctx);
1076 raw_spin_unlock(&ctx->lock);
1083 * Remove the event from a task's (or a CPU's) list of events.
1085 * CPU events are removed with a smp call. For task events we only
1086 * call when the task is on a CPU.
1088 * If event->ctx is a cloned context, callers must make sure that
1089 * every task struct that event->ctx->task could possibly point to
1090 * remains valid. This is OK when called from perf_release since
1091 * that only calls us on the top-level context, which can't be a clone.
1092 * When called from perf_event_exit_task, it's OK because the
1093 * context has been detached from its task.
1095 static void perf_remove_from_context(struct perf_event *event)
1097 struct perf_event_context *ctx = event->ctx;
1098 struct task_struct *task = ctx->task;
1100 lockdep_assert_held(&ctx->mutex);
1104 * Per cpu events are removed via an smp call and
1105 * the removal is always successful.
1107 cpu_function_call(event->cpu, __perf_remove_from_context, event);
1112 if (!task_function_call(task, __perf_remove_from_context, event))
1115 raw_spin_lock_irq(&ctx->lock);
1117 * If we failed to find a running task, but find the context active now
1118 * that we've acquired the ctx->lock, retry.
1120 if (ctx->is_active) {
1121 raw_spin_unlock_irq(&ctx->lock);
1126 * Since the task isn't running, its safe to remove the event, us
1127 * holding the ctx->lock ensures the task won't get scheduled in.
1129 list_del_event(event, ctx);
1130 raw_spin_unlock_irq(&ctx->lock);
1134 * Cross CPU call to disable a performance event
1136 static int __perf_event_disable(void *info)
1138 struct perf_event *event = info;
1139 struct perf_event_context *ctx = event->ctx;
1140 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1143 * If this is a per-task event, need to check whether this
1144 * event's task is the current task on this cpu.
1146 * Can trigger due to concurrent perf_event_context_sched_out()
1147 * flipping contexts around.
1149 if (ctx->task && cpuctx->task_ctx != ctx)
1152 raw_spin_lock(&ctx->lock);
1155 * If the event is on, turn it off.
1156 * If it is in error state, leave it in error state.
1158 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1159 update_context_time(ctx);
1160 update_cgrp_time_from_event(event);
1161 update_group_times(event);
1162 if (event == event->group_leader)
1163 group_sched_out(event, cpuctx, ctx);
1165 event_sched_out(event, cpuctx, ctx);
1166 event->state = PERF_EVENT_STATE_OFF;
1169 raw_spin_unlock(&ctx->lock);
1177 * If event->ctx is a cloned context, callers must make sure that
1178 * every task struct that event->ctx->task could possibly point to
1179 * remains valid. This condition is satisifed when called through
1180 * perf_event_for_each_child or perf_event_for_each because they
1181 * hold the top-level event's child_mutex, so any descendant that
1182 * goes to exit will block in sync_child_event.
1183 * When called from perf_pending_event it's OK because event->ctx
1184 * is the current context on this CPU and preemption is disabled,
1185 * hence we can't get into perf_event_task_sched_out for this context.
1187 void perf_event_disable(struct perf_event *event)
1189 struct perf_event_context *ctx = event->ctx;
1190 struct task_struct *task = ctx->task;
1194 * Disable the event on the cpu that it's on
1196 cpu_function_call(event->cpu, __perf_event_disable, event);
1201 if (!task_function_call(task, __perf_event_disable, event))
1204 raw_spin_lock_irq(&ctx->lock);
1206 * If the event is still active, we need to retry the cross-call.
1208 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1209 raw_spin_unlock_irq(&ctx->lock);
1211 * Reload the task pointer, it might have been changed by
1212 * a concurrent perf_event_context_sched_out().
1219 * Since we have the lock this context can't be scheduled
1220 * in, so we can change the state safely.
1222 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1223 update_group_times(event);
1224 event->state = PERF_EVENT_STATE_OFF;
1226 raw_spin_unlock_irq(&ctx->lock);
1229 static void perf_set_shadow_time(struct perf_event *event,
1230 struct perf_event_context *ctx,
1234 * use the correct time source for the time snapshot
1236 * We could get by without this by leveraging the
1237 * fact that to get to this function, the caller
1238 * has most likely already called update_context_time()
1239 * and update_cgrp_time_xx() and thus both timestamp
1240 * are identical (or very close). Given that tstamp is,
1241 * already adjusted for cgroup, we could say that:
1242 * tstamp - ctx->timestamp
1244 * tstamp - cgrp->timestamp.
1246 * Then, in perf_output_read(), the calculation would
1247 * work with no changes because:
1248 * - event is guaranteed scheduled in
1249 * - no scheduled out in between
1250 * - thus the timestamp would be the same
1252 * But this is a bit hairy.
1254 * So instead, we have an explicit cgroup call to remain
1255 * within the time time source all along. We believe it
1256 * is cleaner and simpler to understand.
1258 if (is_cgroup_event(event))
1259 perf_cgroup_set_shadow_time(event, tstamp);
1261 event->shadow_ctx_time = tstamp - ctx->timestamp;
1264 #define MAX_INTERRUPTS (~0ULL)
1266 static void perf_log_throttle(struct perf_event *event, int enable);
1269 event_sched_in(struct perf_event *event,
1270 struct perf_cpu_context *cpuctx,
1271 struct perf_event_context *ctx)
1273 u64 tstamp = perf_event_time(event);
1275 if (event->state <= PERF_EVENT_STATE_OFF)
1278 event->state = PERF_EVENT_STATE_ACTIVE;
1279 event->oncpu = smp_processor_id();
1282 * Unthrottle events, since we scheduled we might have missed several
1283 * ticks already, also for a heavily scheduling task there is little
1284 * guarantee it'll get a tick in a timely manner.
1286 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1287 perf_log_throttle(event, 1);
1288 event->hw.interrupts = 0;
1292 * The new state must be visible before we turn it on in the hardware:
1296 if (event->pmu->add(event, PERF_EF_START)) {
1297 event->state = PERF_EVENT_STATE_INACTIVE;
1302 event->tstamp_running += tstamp - event->tstamp_stopped;
1304 perf_set_shadow_time(event, ctx, tstamp);
1306 if (!is_software_event(event))
1307 cpuctx->active_oncpu++;
1310 if (event->attr.exclusive)
1311 cpuctx->exclusive = 1;
1317 group_sched_in(struct perf_event *group_event,
1318 struct perf_cpu_context *cpuctx,
1319 struct perf_event_context *ctx)
1321 struct perf_event *event, *partial_group = NULL;
1322 struct pmu *pmu = group_event->pmu;
1323 u64 now = ctx->time;
1324 bool simulate = false;
1326 if (group_event->state == PERF_EVENT_STATE_OFF)
1329 pmu->start_txn(pmu);
1331 if (event_sched_in(group_event, cpuctx, ctx)) {
1332 pmu->cancel_txn(pmu);
1337 * Schedule in siblings as one group (if any):
1339 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1340 if (event_sched_in(event, cpuctx, ctx)) {
1341 partial_group = event;
1346 if (!pmu->commit_txn(pmu))
1351 * Groups can be scheduled in as one unit only, so undo any
1352 * partial group before returning:
1353 * The events up to the failed event are scheduled out normally,
1354 * tstamp_stopped will be updated.
1356 * The failed events and the remaining siblings need to have
1357 * their timings updated as if they had gone thru event_sched_in()
1358 * and event_sched_out(). This is required to get consistent timings
1359 * across the group. This also takes care of the case where the group
1360 * could never be scheduled by ensuring tstamp_stopped is set to mark
1361 * the time the event was actually stopped, such that time delta
1362 * calculation in update_event_times() is correct.
1364 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1365 if (event == partial_group)
1369 event->tstamp_running += now - event->tstamp_stopped;
1370 event->tstamp_stopped = now;
1372 event_sched_out(event, cpuctx, ctx);
1375 event_sched_out(group_event, cpuctx, ctx);
1377 pmu->cancel_txn(pmu);
1383 * Work out whether we can put this event group on the CPU now.
1385 static int group_can_go_on(struct perf_event *event,
1386 struct perf_cpu_context *cpuctx,
1390 * Groups consisting entirely of software events can always go on.
1392 if (event->group_flags & PERF_GROUP_SOFTWARE)
1395 * If an exclusive group is already on, no other hardware
1398 if (cpuctx->exclusive)
1401 * If this group is exclusive and there are already
1402 * events on the CPU, it can't go on.
1404 if (event->attr.exclusive && cpuctx->active_oncpu)
1407 * Otherwise, try to add it if all previous groups were able
1413 static void add_event_to_ctx(struct perf_event *event,
1414 struct perf_event_context *ctx)
1416 u64 tstamp = perf_event_time(event);
1418 list_add_event(event, ctx);
1419 perf_group_attach(event);
1420 event->tstamp_enabled = tstamp;
1421 event->tstamp_running = tstamp;
1422 event->tstamp_stopped = tstamp;
1425 static void perf_event_context_sched_in(struct perf_event_context *ctx,
1426 struct task_struct *tsk);
1429 * Cross CPU call to install and enable a performance event
1431 * Must be called with ctx->mutex held
1433 static int __perf_install_in_context(void *info)
1435 struct perf_event *event = info;
1436 struct perf_event_context *ctx = event->ctx;
1437 struct perf_event *leader = event->group_leader;
1438 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1442 * In case we're installing a new context to an already running task,
1443 * could also happen before perf_event_task_sched_in() on architectures
1444 * which do context switches with IRQs enabled.
1446 if (ctx->task && !cpuctx->task_ctx)
1447 perf_event_context_sched_in(ctx, ctx->task);
1449 raw_spin_lock(&ctx->lock);
1451 update_context_time(ctx);
1453 * update cgrp time only if current cgrp
1454 * matches event->cgrp. Must be done before
1455 * calling add_event_to_ctx()
1457 update_cgrp_time_from_event(event);
1459 add_event_to_ctx(event, ctx);
1461 if (!event_filter_match(event))
1465 * Don't put the event on if it is disabled or if
1466 * it is in a group and the group isn't on.
1468 if (event->state != PERF_EVENT_STATE_INACTIVE ||
1469 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
1473 * An exclusive event can't go on if there are already active
1474 * hardware events, and no hardware event can go on if there
1475 * is already an exclusive event on.
1477 if (!group_can_go_on(event, cpuctx, 1))
1480 err = event_sched_in(event, cpuctx, ctx);
1484 * This event couldn't go on. If it is in a group
1485 * then we have to pull the whole group off.
1486 * If the event group is pinned then put it in error state.
1488 if (leader != event)
1489 group_sched_out(leader, cpuctx, ctx);
1490 if (leader->attr.pinned) {
1491 update_group_times(leader);
1492 leader->state = PERF_EVENT_STATE_ERROR;
1497 raw_spin_unlock(&ctx->lock);
1503 * Attach a performance event to a context
1505 * First we add the event to the list with the hardware enable bit
1506 * in event->hw_config cleared.
1508 * If the event is attached to a task which is on a CPU we use a smp
1509 * call to enable it in the task context. The task might have been
1510 * scheduled away, but we check this in the smp call again.
1513 perf_install_in_context(struct perf_event_context *ctx,
1514 struct perf_event *event,
1517 struct task_struct *task = ctx->task;
1519 lockdep_assert_held(&ctx->mutex);
1525 * Per cpu events are installed via an smp call and
1526 * the install is always successful.
1528 cpu_function_call(cpu, __perf_install_in_context, event);
1533 if (!task_function_call(task, __perf_install_in_context, event))
1536 raw_spin_lock_irq(&ctx->lock);
1538 * If we failed to find a running task, but find the context active now
1539 * that we've acquired the ctx->lock, retry.
1541 if (ctx->is_active) {
1542 raw_spin_unlock_irq(&ctx->lock);
1547 * Since the task isn't running, its safe to add the event, us holding
1548 * the ctx->lock ensures the task won't get scheduled in.
1550 add_event_to_ctx(event, ctx);
1551 raw_spin_unlock_irq(&ctx->lock);
1555 * Put a event into inactive state and update time fields.
1556 * Enabling the leader of a group effectively enables all
1557 * the group members that aren't explicitly disabled, so we
1558 * have to update their ->tstamp_enabled also.
1559 * Note: this works for group members as well as group leaders
1560 * since the non-leader members' sibling_lists will be empty.
1562 static void __perf_event_mark_enabled(struct perf_event *event,
1563 struct perf_event_context *ctx)
1565 struct perf_event *sub;
1566 u64 tstamp = perf_event_time(event);
1568 event->state = PERF_EVENT_STATE_INACTIVE;
1569 event->tstamp_enabled = tstamp - event->total_time_enabled;
1570 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1571 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1572 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1577 * Cross CPU call to enable a performance event
1579 static int __perf_event_enable(void *info)
1581 struct perf_event *event = info;
1582 struct perf_event_context *ctx = event->ctx;
1583 struct perf_event *leader = event->group_leader;
1584 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1587 if (WARN_ON_ONCE(!ctx->is_active))
1590 raw_spin_lock(&ctx->lock);
1591 update_context_time(ctx);
1593 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1597 * set current task's cgroup time reference point
1599 perf_cgroup_set_timestamp(current, perf_clock());
1601 __perf_event_mark_enabled(event, ctx);
1603 if (!event_filter_match(event)) {
1604 if (is_cgroup_event(event))
1605 perf_cgroup_defer_enabled(event);
1610 * If the event is in a group and isn't the group leader,
1611 * then don't put it on unless the group is on.
1613 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1616 if (!group_can_go_on(event, cpuctx, 1)) {
1619 if (event == leader)
1620 err = group_sched_in(event, cpuctx, ctx);
1622 err = event_sched_in(event, cpuctx, ctx);
1627 * If this event can't go on and it's part of a
1628 * group, then the whole group has to come off.
1630 if (leader != event)
1631 group_sched_out(leader, cpuctx, ctx);
1632 if (leader->attr.pinned) {
1633 update_group_times(leader);
1634 leader->state = PERF_EVENT_STATE_ERROR;
1639 raw_spin_unlock(&ctx->lock);
1647 * If event->ctx is a cloned context, callers must make sure that
1648 * every task struct that event->ctx->task could possibly point to
1649 * remains valid. This condition is satisfied when called through
1650 * perf_event_for_each_child or perf_event_for_each as described
1651 * for perf_event_disable.
1653 void perf_event_enable(struct perf_event *event)
1655 struct perf_event_context *ctx = event->ctx;
1656 struct task_struct *task = ctx->task;
1660 * Enable the event on the cpu that it's on
1662 cpu_function_call(event->cpu, __perf_event_enable, event);
1666 raw_spin_lock_irq(&ctx->lock);
1667 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1671 * If the event is in error state, clear that first.
1672 * That way, if we see the event in error state below, we
1673 * know that it has gone back into error state, as distinct
1674 * from the task having been scheduled away before the
1675 * cross-call arrived.
1677 if (event->state == PERF_EVENT_STATE_ERROR)
1678 event->state = PERF_EVENT_STATE_OFF;
1681 if (!ctx->is_active) {
1682 __perf_event_mark_enabled(event, ctx);
1686 raw_spin_unlock_irq(&ctx->lock);
1688 if (!task_function_call(task, __perf_event_enable, event))
1691 raw_spin_lock_irq(&ctx->lock);
1694 * If the context is active and the event is still off,
1695 * we need to retry the cross-call.
1697 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1699 * task could have been flipped by a concurrent
1700 * perf_event_context_sched_out()
1707 raw_spin_unlock_irq(&ctx->lock);
1710 static int perf_event_refresh(struct perf_event *event, int refresh)
1713 * not supported on inherited events
1715 if (event->attr.inherit || !is_sampling_event(event))
1718 atomic_add(refresh, &event->event_limit);
1719 perf_event_enable(event);
1724 static void ctx_sched_out(struct perf_event_context *ctx,
1725 struct perf_cpu_context *cpuctx,
1726 enum event_type_t event_type)
1728 struct perf_event *event;
1730 raw_spin_lock(&ctx->lock);
1731 perf_pmu_disable(ctx->pmu);
1733 if (likely(!ctx->nr_events))
1735 update_context_time(ctx);
1736 update_cgrp_time_from_cpuctx(cpuctx);
1738 if (!ctx->nr_active)
1741 if (event_type & EVENT_PINNED) {
1742 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1743 group_sched_out(event, cpuctx, ctx);
1746 if (event_type & EVENT_FLEXIBLE) {
1747 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1748 group_sched_out(event, cpuctx, ctx);
1751 perf_pmu_enable(ctx->pmu);
1752 raw_spin_unlock(&ctx->lock);
1756 * Test whether two contexts are equivalent, i.e. whether they
1757 * have both been cloned from the same version of the same context
1758 * and they both have the same number of enabled events.
1759 * If the number of enabled events is the same, then the set
1760 * of enabled events should be the same, because these are both
1761 * inherited contexts, therefore we can't access individual events
1762 * in them directly with an fd; we can only enable/disable all
1763 * events via prctl, or enable/disable all events in a family
1764 * via ioctl, which will have the same effect on both contexts.
1766 static int context_equiv(struct perf_event_context *ctx1,
1767 struct perf_event_context *ctx2)
1769 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1770 && ctx1->parent_gen == ctx2->parent_gen
1771 && !ctx1->pin_count && !ctx2->pin_count;
1774 static void __perf_event_sync_stat(struct perf_event *event,
1775 struct perf_event *next_event)
1779 if (!event->attr.inherit_stat)
1783 * Update the event value, we cannot use perf_event_read()
1784 * because we're in the middle of a context switch and have IRQs
1785 * disabled, which upsets smp_call_function_single(), however
1786 * we know the event must be on the current CPU, therefore we
1787 * don't need to use it.
1789 switch (event->state) {
1790 case PERF_EVENT_STATE_ACTIVE:
1791 event->pmu->read(event);
1794 case PERF_EVENT_STATE_INACTIVE:
1795 update_event_times(event);
1803 * In order to keep per-task stats reliable we need to flip the event
1804 * values when we flip the contexts.
1806 value = local64_read(&next_event->count);
1807 value = local64_xchg(&event->count, value);
1808 local64_set(&next_event->count, value);
1810 swap(event->total_time_enabled, next_event->total_time_enabled);
1811 swap(event->total_time_running, next_event->total_time_running);
1814 * Since we swizzled the values, update the user visible data too.
1816 perf_event_update_userpage(event);
1817 perf_event_update_userpage(next_event);
1820 #define list_next_entry(pos, member) \
1821 list_entry(pos->member.next, typeof(*pos), member)
1823 static void perf_event_sync_stat(struct perf_event_context *ctx,
1824 struct perf_event_context *next_ctx)
1826 struct perf_event *event, *next_event;
1831 update_context_time(ctx);
1833 event = list_first_entry(&ctx->event_list,
1834 struct perf_event, event_entry);
1836 next_event = list_first_entry(&next_ctx->event_list,
1837 struct perf_event, event_entry);
1839 while (&event->event_entry != &ctx->event_list &&
1840 &next_event->event_entry != &next_ctx->event_list) {
1842 __perf_event_sync_stat(event, next_event);
1844 event = list_next_entry(event, event_entry);
1845 next_event = list_next_entry(next_event, event_entry);
1849 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1850 struct task_struct *next)
1852 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1853 struct perf_event_context *next_ctx;
1854 struct perf_event_context *parent;
1855 struct perf_cpu_context *cpuctx;
1861 cpuctx = __get_cpu_context(ctx);
1862 if (!cpuctx->task_ctx)
1866 parent = rcu_dereference(ctx->parent_ctx);
1867 next_ctx = next->perf_event_ctxp[ctxn];
1868 if (parent && next_ctx &&
1869 rcu_dereference(next_ctx->parent_ctx) == parent) {
1871 * Looks like the two contexts are clones, so we might be
1872 * able to optimize the context switch. We lock both
1873 * contexts and check that they are clones under the
1874 * lock (including re-checking that neither has been
1875 * uncloned in the meantime). It doesn't matter which
1876 * order we take the locks because no other cpu could
1877 * be trying to lock both of these tasks.
1879 raw_spin_lock(&ctx->lock);
1880 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1881 if (context_equiv(ctx, next_ctx)) {
1883 * XXX do we need a memory barrier of sorts
1884 * wrt to rcu_dereference() of perf_event_ctxp
1886 task->perf_event_ctxp[ctxn] = next_ctx;
1887 next->perf_event_ctxp[ctxn] = ctx;
1889 next_ctx->task = task;
1892 perf_event_sync_stat(ctx, next_ctx);
1894 raw_spin_unlock(&next_ctx->lock);
1895 raw_spin_unlock(&ctx->lock);
1900 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1901 cpuctx->task_ctx = NULL;
1905 #define for_each_task_context_nr(ctxn) \
1906 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1909 * Called from scheduler to remove the events of the current task,
1910 * with interrupts disabled.
1912 * We stop each event and update the event value in event->count.
1914 * This does not protect us against NMI, but disable()
1915 * sets the disabled bit in the control field of event _before_
1916 * accessing the event control register. If a NMI hits, then it will
1917 * not restart the event.
1919 void __perf_event_task_sched_out(struct task_struct *task,
1920 struct task_struct *next)
1924 for_each_task_context_nr(ctxn)
1925 perf_event_context_sched_out(task, ctxn, next);
1928 * if cgroup events exist on this CPU, then we need
1929 * to check if we have to switch out PMU state.
1930 * cgroup event are system-wide mode only
1932 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
1933 perf_cgroup_sched_out(task);
1936 static void task_ctx_sched_out(struct perf_event_context *ctx,
1937 enum event_type_t event_type)
1939 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1941 if (!cpuctx->task_ctx)
1944 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1947 ctx_sched_out(ctx, cpuctx, event_type);
1948 cpuctx->task_ctx = NULL;
1952 * Called with IRQs disabled
1954 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1955 enum event_type_t event_type)
1957 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
1961 ctx_pinned_sched_in(struct perf_event_context *ctx,
1962 struct perf_cpu_context *cpuctx)
1964 struct perf_event *event;
1966 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1967 if (event->state <= PERF_EVENT_STATE_OFF)
1969 if (!event_filter_match(event))
1972 /* may need to reset tstamp_enabled */
1973 if (is_cgroup_event(event))
1974 perf_cgroup_mark_enabled(event, ctx);
1976 if (group_can_go_on(event, cpuctx, 1))
1977 group_sched_in(event, cpuctx, ctx);
1980 * If this pinned group hasn't been scheduled,
1981 * put it in error state.
1983 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1984 update_group_times(event);
1985 event->state = PERF_EVENT_STATE_ERROR;
1991 ctx_flexible_sched_in(struct perf_event_context *ctx,
1992 struct perf_cpu_context *cpuctx)
1994 struct perf_event *event;
1997 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1998 /* Ignore events in OFF or ERROR state */
1999 if (event->state <= PERF_EVENT_STATE_OFF)
2002 * Listen to the 'cpu' scheduling filter constraint
2005 if (!event_filter_match(event))
2008 /* may need to reset tstamp_enabled */
2009 if (is_cgroup_event(event))
2010 perf_cgroup_mark_enabled(event, ctx);
2012 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2013 if (group_sched_in(event, cpuctx, ctx))
2020 ctx_sched_in(struct perf_event_context *ctx,
2021 struct perf_cpu_context *cpuctx,
2022 enum event_type_t event_type,
2023 struct task_struct *task)
2027 raw_spin_lock(&ctx->lock);
2029 if (likely(!ctx->nr_events))
2033 ctx->timestamp = now;
2034 perf_cgroup_set_timestamp(task, now);
2036 * First go through the list and put on any pinned groups
2037 * in order to give them the best chance of going on.
2039 if (event_type & EVENT_PINNED)
2040 ctx_pinned_sched_in(ctx, cpuctx);
2042 /* Then walk through the lower prio flexible groups */
2043 if (event_type & EVENT_FLEXIBLE)
2044 ctx_flexible_sched_in(ctx, cpuctx);
2047 raw_spin_unlock(&ctx->lock);
2050 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2051 enum event_type_t event_type,
2052 struct task_struct *task)
2054 struct perf_event_context *ctx = &cpuctx->ctx;
2056 ctx_sched_in(ctx, cpuctx, event_type, task);
2059 static void task_ctx_sched_in(struct perf_event_context *ctx,
2060 enum event_type_t event_type)
2062 struct perf_cpu_context *cpuctx;
2064 cpuctx = __get_cpu_context(ctx);
2065 if (cpuctx->task_ctx == ctx)
2068 ctx_sched_in(ctx, cpuctx, event_type, NULL);
2069 cpuctx->task_ctx = ctx;
2072 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2073 struct task_struct *task)
2075 struct perf_cpu_context *cpuctx;
2077 cpuctx = __get_cpu_context(ctx);
2078 if (cpuctx->task_ctx == ctx)
2081 perf_pmu_disable(ctx->pmu);
2083 * We want to keep the following priority order:
2084 * cpu pinned (that don't need to move), task pinned,
2085 * cpu flexible, task flexible.
2087 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2089 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2090 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2091 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2093 cpuctx->task_ctx = ctx;
2096 * Since these rotations are per-cpu, we need to ensure the
2097 * cpu-context we got scheduled on is actually rotating.
2099 perf_pmu_rotate_start(ctx->pmu);
2100 perf_pmu_enable(ctx->pmu);
2104 * Called from scheduler to add the events of the current task
2105 * with interrupts disabled.
2107 * We restore the event value and then enable it.
2109 * This does not protect us against NMI, but enable()
2110 * sets the enabled bit in the control field of event _before_
2111 * accessing the event control register. If a NMI hits, then it will
2112 * keep the event running.
2114 void __perf_event_task_sched_in(struct task_struct *task)
2116 struct perf_event_context *ctx;
2119 for_each_task_context_nr(ctxn) {
2120 ctx = task->perf_event_ctxp[ctxn];
2124 perf_event_context_sched_in(ctx, task);
2127 * if cgroup events exist on this CPU, then we need
2128 * to check if we have to switch in PMU state.
2129 * cgroup event are system-wide mode only
2131 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2132 perf_cgroup_sched_in(task);
2135 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2137 u64 frequency = event->attr.sample_freq;
2138 u64 sec = NSEC_PER_SEC;
2139 u64 divisor, dividend;
2141 int count_fls, nsec_fls, frequency_fls, sec_fls;
2143 count_fls = fls64(count);
2144 nsec_fls = fls64(nsec);
2145 frequency_fls = fls64(frequency);
2149 * We got @count in @nsec, with a target of sample_freq HZ
2150 * the target period becomes:
2153 * period = -------------------
2154 * @nsec * sample_freq
2159 * Reduce accuracy by one bit such that @a and @b converge
2160 * to a similar magnitude.
2162 #define REDUCE_FLS(a, b) \
2164 if (a##_fls > b##_fls) { \
2174 * Reduce accuracy until either term fits in a u64, then proceed with
2175 * the other, so that finally we can do a u64/u64 division.
2177 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2178 REDUCE_FLS(nsec, frequency);
2179 REDUCE_FLS(sec, count);
2182 if (count_fls + sec_fls > 64) {
2183 divisor = nsec * frequency;
2185 while (count_fls + sec_fls > 64) {
2186 REDUCE_FLS(count, sec);
2190 dividend = count * sec;
2192 dividend = count * sec;
2194 while (nsec_fls + frequency_fls > 64) {
2195 REDUCE_FLS(nsec, frequency);
2199 divisor = nsec * frequency;
2205 return div64_u64(dividend, divisor);
2208 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
2210 struct hw_perf_event *hwc = &event->hw;
2211 s64 period, sample_period;
2214 period = perf_calculate_period(event, nsec, count);
2216 delta = (s64)(period - hwc->sample_period);
2217 delta = (delta + 7) / 8; /* low pass filter */
2219 sample_period = hwc->sample_period + delta;
2224 hwc->sample_period = sample_period;
2226 if (local64_read(&hwc->period_left) > 8*sample_period) {
2227 event->pmu->stop(event, PERF_EF_UPDATE);
2228 local64_set(&hwc->period_left, 0);
2229 event->pmu->start(event, PERF_EF_RELOAD);
2233 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
2235 struct perf_event *event;
2236 struct hw_perf_event *hwc;
2237 u64 interrupts, now;
2240 raw_spin_lock(&ctx->lock);
2241 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2242 if (event->state != PERF_EVENT_STATE_ACTIVE)
2245 if (!event_filter_match(event))
2250 interrupts = hwc->interrupts;
2251 hwc->interrupts = 0;
2254 * unthrottle events on the tick
2256 if (interrupts == MAX_INTERRUPTS) {
2257 perf_log_throttle(event, 1);
2258 event->pmu->start(event, 0);
2261 if (!event->attr.freq || !event->attr.sample_freq)
2264 event->pmu->read(event);
2265 now = local64_read(&event->count);
2266 delta = now - hwc->freq_count_stamp;
2267 hwc->freq_count_stamp = now;
2270 perf_adjust_period(event, period, delta);
2272 raw_spin_unlock(&ctx->lock);
2276 * Round-robin a context's events:
2278 static void rotate_ctx(struct perf_event_context *ctx)
2280 raw_spin_lock(&ctx->lock);
2283 * Rotate the first entry last of non-pinned groups. Rotation might be
2284 * disabled by the inheritance code.
2286 if (!ctx->rotate_disable)
2287 list_rotate_left(&ctx->flexible_groups);
2289 raw_spin_unlock(&ctx->lock);
2293 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2294 * because they're strictly cpu affine and rotate_start is called with IRQs
2295 * disabled, while rotate_context is called from IRQ context.
2297 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
2299 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
2300 struct perf_event_context *ctx = NULL;
2301 int rotate = 0, remove = 1;
2303 if (cpuctx->ctx.nr_events) {
2305 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2309 ctx = cpuctx->task_ctx;
2310 if (ctx && ctx->nr_events) {
2312 if (ctx->nr_events != ctx->nr_active)
2316 perf_pmu_disable(cpuctx->ctx.pmu);
2317 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
2319 perf_ctx_adjust_freq(ctx, interval);
2324 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2326 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
2328 rotate_ctx(&cpuctx->ctx);
2332 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, current);
2334 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
2338 list_del_init(&cpuctx->rotation_list);
2340 perf_pmu_enable(cpuctx->ctx.pmu);
2343 void perf_event_task_tick(void)
2345 struct list_head *head = &__get_cpu_var(rotation_list);
2346 struct perf_cpu_context *cpuctx, *tmp;
2348 WARN_ON(!irqs_disabled());
2350 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2351 if (cpuctx->jiffies_interval == 1 ||
2352 !(jiffies % cpuctx->jiffies_interval))
2353 perf_rotate_context(cpuctx);
2357 static int event_enable_on_exec(struct perf_event *event,
2358 struct perf_event_context *ctx)
2360 if (!event->attr.enable_on_exec)
2363 event->attr.enable_on_exec = 0;
2364 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2367 __perf_event_mark_enabled(event, ctx);
2373 * Enable all of a task's events that have been marked enable-on-exec.
2374 * This expects task == current.
2376 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2378 struct perf_event *event;
2379 unsigned long flags;
2383 local_irq_save(flags);
2384 if (!ctx || !ctx->nr_events)
2387 task_ctx_sched_out(ctx, EVENT_ALL);
2389 raw_spin_lock(&ctx->lock);
2391 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2392 ret = event_enable_on_exec(event, ctx);
2397 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2398 ret = event_enable_on_exec(event, ctx);
2404 * Unclone this context if we enabled any event.
2409 raw_spin_unlock(&ctx->lock);
2411 perf_event_context_sched_in(ctx, ctx->task);
2413 local_irq_restore(flags);
2417 * Cross CPU call to read the hardware event
2419 static void __perf_event_read(void *info)
2421 struct perf_event *event = info;
2422 struct perf_event_context *ctx = event->ctx;
2423 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2426 * If this is a task context, we need to check whether it is
2427 * the current task context of this cpu. If not it has been
2428 * scheduled out before the smp call arrived. In that case
2429 * event->count would have been updated to a recent sample
2430 * when the event was scheduled out.
2432 if (ctx->task && cpuctx->task_ctx != ctx)
2435 raw_spin_lock(&ctx->lock);
2436 if (ctx->is_active) {
2437 update_context_time(ctx);
2438 update_cgrp_time_from_event(event);
2440 update_event_times(event);
2441 if (event->state == PERF_EVENT_STATE_ACTIVE)
2442 event->pmu->read(event);
2443 raw_spin_unlock(&ctx->lock);
2446 static inline u64 perf_event_count(struct perf_event *event)
2448 return local64_read(&event->count) + atomic64_read(&event->child_count);
2451 static u64 perf_event_read(struct perf_event *event)
2454 * If event is enabled and currently active on a CPU, update the
2455 * value in the event structure:
2457 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2458 smp_call_function_single(event->oncpu,
2459 __perf_event_read, event, 1);
2460 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2461 struct perf_event_context *ctx = event->ctx;
2462 unsigned long flags;
2464 raw_spin_lock_irqsave(&ctx->lock, flags);
2466 * may read while context is not active
2467 * (e.g., thread is blocked), in that case
2468 * we cannot update context time
2470 if (ctx->is_active) {
2471 update_context_time(ctx);
2472 update_cgrp_time_from_event(event);
2474 update_event_times(event);
2475 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2478 return perf_event_count(event);
2485 struct callchain_cpus_entries {
2486 struct rcu_head rcu_head;
2487 struct perf_callchain_entry *cpu_entries[0];
2490 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
2491 static atomic_t nr_callchain_events;
2492 static DEFINE_MUTEX(callchain_mutex);
2493 struct callchain_cpus_entries *callchain_cpus_entries;
2496 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
2497 struct pt_regs *regs)
2501 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
2502 struct pt_regs *regs)
2506 static void release_callchain_buffers_rcu(struct rcu_head *head)
2508 struct callchain_cpus_entries *entries;
2511 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
2513 for_each_possible_cpu(cpu)
2514 kfree(entries->cpu_entries[cpu]);
2519 static void release_callchain_buffers(void)
2521 struct callchain_cpus_entries *entries;
2523 entries = callchain_cpus_entries;
2524 rcu_assign_pointer(callchain_cpus_entries, NULL);
2525 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2528 static int alloc_callchain_buffers(void)
2532 struct callchain_cpus_entries *entries;
2535 * We can't use the percpu allocation API for data that can be
2536 * accessed from NMI. Use a temporary manual per cpu allocation
2537 * until that gets sorted out.
2539 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
2541 entries = kzalloc(size, GFP_KERNEL);
2545 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
2547 for_each_possible_cpu(cpu) {
2548 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2550 if (!entries->cpu_entries[cpu])
2554 rcu_assign_pointer(callchain_cpus_entries, entries);
2559 for_each_possible_cpu(cpu)
2560 kfree(entries->cpu_entries[cpu]);
2566 static int get_callchain_buffers(void)
2571 mutex_lock(&callchain_mutex);
2573 count = atomic_inc_return(&nr_callchain_events);
2574 if (WARN_ON_ONCE(count < 1)) {
2580 /* If the allocation failed, give up */
2581 if (!callchain_cpus_entries)
2586 err = alloc_callchain_buffers();
2588 release_callchain_buffers();
2590 mutex_unlock(&callchain_mutex);
2595 static void put_callchain_buffers(void)
2597 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2598 release_callchain_buffers();
2599 mutex_unlock(&callchain_mutex);
2603 static int get_recursion_context(int *recursion)
2611 else if (in_softirq())
2616 if (recursion[rctx])
2625 static inline void put_recursion_context(int *recursion, int rctx)
2631 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2634 struct callchain_cpus_entries *entries;
2636 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2640 entries = rcu_dereference(callchain_cpus_entries);
2644 cpu = smp_processor_id();
2646 return &entries->cpu_entries[cpu][*rctx];
2650 put_callchain_entry(int rctx)
2652 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2655 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2658 struct perf_callchain_entry *entry;
2661 entry = get_callchain_entry(&rctx);
2670 if (!user_mode(regs)) {
2671 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2672 perf_callchain_kernel(entry, regs);
2674 regs = task_pt_regs(current);
2680 perf_callchain_store(entry, PERF_CONTEXT_USER);
2681 perf_callchain_user(entry, regs);
2685 put_callchain_entry(rctx);
2691 * Initialize the perf_event context in a task_struct:
2693 static void __perf_event_init_context(struct perf_event_context *ctx)
2695 raw_spin_lock_init(&ctx->lock);
2696 mutex_init(&ctx->mutex);
2697 INIT_LIST_HEAD(&ctx->pinned_groups);
2698 INIT_LIST_HEAD(&ctx->flexible_groups);
2699 INIT_LIST_HEAD(&ctx->event_list);
2700 atomic_set(&ctx->refcount, 1);
2703 static struct perf_event_context *
2704 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2706 struct perf_event_context *ctx;
2708 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2712 __perf_event_init_context(ctx);
2715 get_task_struct(task);
2722 static struct task_struct *
2723 find_lively_task_by_vpid(pid_t vpid)
2725 struct task_struct *task;
2732 task = find_task_by_vpid(vpid);
2734 get_task_struct(task);
2738 return ERR_PTR(-ESRCH);
2740 /* Reuse ptrace permission checks for now. */
2742 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2747 put_task_struct(task);
2748 return ERR_PTR(err);
2753 * Returns a matching context with refcount and pincount.
2755 static struct perf_event_context *
2756 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2758 struct perf_event_context *ctx;
2759 struct perf_cpu_context *cpuctx;
2760 unsigned long flags;
2764 /* Must be root to operate on a CPU event: */
2765 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2766 return ERR_PTR(-EACCES);
2769 * We could be clever and allow to attach a event to an
2770 * offline CPU and activate it when the CPU comes up, but
2773 if (!cpu_online(cpu))
2774 return ERR_PTR(-ENODEV);
2776 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2785 ctxn = pmu->task_ctx_nr;
2790 ctx = perf_lock_task_context(task, ctxn, &flags);
2794 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2798 ctx = alloc_perf_context(pmu, task);
2806 mutex_lock(&task->perf_event_mutex);
2808 * If it has already passed perf_event_exit_task().
2809 * we must see PF_EXITING, it takes this mutex too.
2811 if (task->flags & PF_EXITING)
2813 else if (task->perf_event_ctxp[ctxn])
2817 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2819 mutex_unlock(&task->perf_event_mutex);
2821 if (unlikely(err)) {
2822 put_task_struct(task);
2834 return ERR_PTR(err);
2837 static void perf_event_free_filter(struct perf_event *event);
2839 static void free_event_rcu(struct rcu_head *head)
2841 struct perf_event *event;
2843 event = container_of(head, struct perf_event, rcu_head);
2845 put_pid_ns(event->ns);
2846 perf_event_free_filter(event);
2850 static void perf_buffer_put(struct perf_buffer *buffer);
2852 static void free_event(struct perf_event *event)
2854 irq_work_sync(&event->pending);
2856 if (!event->parent) {
2857 if (event->attach_state & PERF_ATTACH_TASK)
2858 jump_label_dec(&perf_sched_events);
2859 if (event->attr.mmap || event->attr.mmap_data)
2860 atomic_dec(&nr_mmap_events);
2861 if (event->attr.comm)
2862 atomic_dec(&nr_comm_events);
2863 if (event->attr.task)
2864 atomic_dec(&nr_task_events);
2865 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2866 put_callchain_buffers();
2869 if (event->buffer) {
2870 perf_buffer_put(event->buffer);
2871 event->buffer = NULL;
2874 if (is_cgroup_event(event))
2875 perf_detach_cgroup(event);
2878 event->destroy(event);
2881 put_ctx(event->ctx);
2883 call_rcu(&event->rcu_head, free_event_rcu);
2886 int perf_event_release_kernel(struct perf_event *event)
2888 struct perf_event_context *ctx = event->ctx;
2891 * Remove from the PMU, can't get re-enabled since we got
2892 * here because the last ref went.
2894 perf_event_disable(event);
2896 WARN_ON_ONCE(ctx->parent_ctx);
2898 * There are two ways this annotation is useful:
2900 * 1) there is a lock recursion from perf_event_exit_task
2901 * see the comment there.
2903 * 2) there is a lock-inversion with mmap_sem through
2904 * perf_event_read_group(), which takes faults while
2905 * holding ctx->mutex, however this is called after
2906 * the last filedesc died, so there is no possibility
2907 * to trigger the AB-BA case.
2909 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2910 raw_spin_lock_irq(&ctx->lock);
2911 perf_group_detach(event);
2912 list_del_event(event, ctx);
2913 raw_spin_unlock_irq(&ctx->lock);
2914 mutex_unlock(&ctx->mutex);
2920 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2923 * Called when the last reference to the file is gone.
2925 static int perf_release(struct inode *inode, struct file *file)
2927 struct perf_event *event = file->private_data;
2928 struct task_struct *owner;
2930 file->private_data = NULL;
2933 owner = ACCESS_ONCE(event->owner);
2935 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2936 * !owner it means the list deletion is complete and we can indeed
2937 * free this event, otherwise we need to serialize on
2938 * owner->perf_event_mutex.
2940 smp_read_barrier_depends();
2943 * Since delayed_put_task_struct() also drops the last
2944 * task reference we can safely take a new reference
2945 * while holding the rcu_read_lock().
2947 get_task_struct(owner);
2952 mutex_lock(&owner->perf_event_mutex);
2954 * We have to re-check the event->owner field, if it is cleared
2955 * we raced with perf_event_exit_task(), acquiring the mutex
2956 * ensured they're done, and we can proceed with freeing the
2960 list_del_init(&event->owner_entry);
2961 mutex_unlock(&owner->perf_event_mutex);
2962 put_task_struct(owner);
2965 return perf_event_release_kernel(event);
2968 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
2970 struct perf_event *child;
2976 mutex_lock(&event->child_mutex);
2977 total += perf_event_read(event);
2978 *enabled += event->total_time_enabled +
2979 atomic64_read(&event->child_total_time_enabled);
2980 *running += event->total_time_running +
2981 atomic64_read(&event->child_total_time_running);
2983 list_for_each_entry(child, &event->child_list, child_list) {
2984 total += perf_event_read(child);
2985 *enabled += child->total_time_enabled;
2986 *running += child->total_time_running;
2988 mutex_unlock(&event->child_mutex);
2992 EXPORT_SYMBOL_GPL(perf_event_read_value);
2994 static int perf_event_read_group(struct perf_event *event,
2995 u64 read_format, char __user *buf)
2997 struct perf_event *leader = event->group_leader, *sub;
2998 int n = 0, size = 0, ret = -EFAULT;
2999 struct perf_event_context *ctx = leader->ctx;
3001 u64 count, enabled, running;
3003 mutex_lock(&ctx->mutex);
3004 count = perf_event_read_value(leader, &enabled, &running);
3006 values[n++] = 1 + leader->nr_siblings;
3007 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3008 values[n++] = enabled;
3009 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3010 values[n++] = running;
3011 values[n++] = count;
3012 if (read_format & PERF_FORMAT_ID)
3013 values[n++] = primary_event_id(leader);
3015 size = n * sizeof(u64);
3017 if (copy_to_user(buf, values, size))
3022 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3025 values[n++] = perf_event_read_value(sub, &enabled, &running);
3026 if (read_format & PERF_FORMAT_ID)
3027 values[n++] = primary_event_id(sub);
3029 size = n * sizeof(u64);
3031 if (copy_to_user(buf + ret, values, size)) {
3039 mutex_unlock(&ctx->mutex);
3044 static int perf_event_read_one(struct perf_event *event,
3045 u64 read_format, char __user *buf)
3047 u64 enabled, running;
3051 values[n++] = perf_event_read_value(event, &enabled, &running);
3052 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3053 values[n++] = enabled;
3054 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3055 values[n++] = running;
3056 if (read_format & PERF_FORMAT_ID)
3057 values[n++] = primary_event_id(event);
3059 if (copy_to_user(buf, values, n * sizeof(u64)))
3062 return n * sizeof(u64);
3066 * Read the performance event - simple non blocking version for now
3069 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3071 u64 read_format = event->attr.read_format;
3075 * Return end-of-file for a read on a event that is in
3076 * error state (i.e. because it was pinned but it couldn't be
3077 * scheduled on to the CPU at some point).
3079 if (event->state == PERF_EVENT_STATE_ERROR)
3082 if (count < event->read_size)
3085 WARN_ON_ONCE(event->ctx->parent_ctx);
3086 if (read_format & PERF_FORMAT_GROUP)
3087 ret = perf_event_read_group(event, read_format, buf);
3089 ret = perf_event_read_one(event, read_format, buf);
3095 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3097 struct perf_event *event = file->private_data;
3099 return perf_read_hw(event, buf, count);
3102 static unsigned int perf_poll(struct file *file, poll_table *wait)
3104 struct perf_event *event = file->private_data;
3105 struct perf_buffer *buffer;
3106 unsigned int events = POLL_HUP;
3109 buffer = rcu_dereference(event->buffer);
3111 events = atomic_xchg(&buffer->poll, 0);
3114 poll_wait(file, &event->waitq, wait);
3119 static void perf_event_reset(struct perf_event *event)
3121 (void)perf_event_read(event);
3122 local64_set(&event->count, 0);
3123 perf_event_update_userpage(event);
3127 * Holding the top-level event's child_mutex means that any
3128 * descendant process that has inherited this event will block
3129 * in sync_child_event if it goes to exit, thus satisfying the
3130 * task existence requirements of perf_event_enable/disable.
3132 static void perf_event_for_each_child(struct perf_event *event,
3133 void (*func)(struct perf_event *))
3135 struct perf_event *child;
3137 WARN_ON_ONCE(event->ctx->parent_ctx);
3138 mutex_lock(&event->child_mutex);
3140 list_for_each_entry(child, &event->child_list, child_list)
3142 mutex_unlock(&event->child_mutex);
3145 static void perf_event_for_each(struct perf_event *event,
3146 void (*func)(struct perf_event *))
3148 struct perf_event_context *ctx = event->ctx;
3149 struct perf_event *sibling;
3151 WARN_ON_ONCE(ctx->parent_ctx);
3152 mutex_lock(&ctx->mutex);
3153 event = event->group_leader;
3155 perf_event_for_each_child(event, func);
3157 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3158 perf_event_for_each_child(event, func);
3159 mutex_unlock(&ctx->mutex);
3162 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3164 struct perf_event_context *ctx = event->ctx;
3168 if (!is_sampling_event(event))
3171 if (copy_from_user(&value, arg, sizeof(value)))
3177 raw_spin_lock_irq(&ctx->lock);
3178 if (event->attr.freq) {
3179 if (value > sysctl_perf_event_sample_rate) {
3184 event->attr.sample_freq = value;
3186 event->attr.sample_period = value;
3187 event->hw.sample_period = value;
3190 raw_spin_unlock_irq(&ctx->lock);
3195 static const struct file_operations perf_fops;
3197 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
3201 file = fget_light(fd, fput_needed);
3203 return ERR_PTR(-EBADF);
3205 if (file->f_op != &perf_fops) {
3206 fput_light(file, *fput_needed);
3208 return ERR_PTR(-EBADF);
3211 return file->private_data;
3214 static int perf_event_set_output(struct perf_event *event,
3215 struct perf_event *output_event);
3216 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3218 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3220 struct perf_event *event = file->private_data;
3221 void (*func)(struct perf_event *);
3225 case PERF_EVENT_IOC_ENABLE:
3226 func = perf_event_enable;
3228 case PERF_EVENT_IOC_DISABLE:
3229 func = perf_event_disable;
3231 case PERF_EVENT_IOC_RESET:
3232 func = perf_event_reset;
3235 case PERF_EVENT_IOC_REFRESH:
3236 return perf_event_refresh(event, arg);
3238 case PERF_EVENT_IOC_PERIOD:
3239 return perf_event_period(event, (u64 __user *)arg);
3241 case PERF_EVENT_IOC_SET_OUTPUT:
3243 struct perf_event *output_event = NULL;
3244 int fput_needed = 0;
3248 output_event = perf_fget_light(arg, &fput_needed);
3249 if (IS_ERR(output_event))
3250 return PTR_ERR(output_event);
3253 ret = perf_event_set_output(event, output_event);
3255 fput_light(output_event->filp, fput_needed);
3260 case PERF_EVENT_IOC_SET_FILTER:
3261 return perf_event_set_filter(event, (void __user *)arg);
3267 if (flags & PERF_IOC_FLAG_GROUP)
3268 perf_event_for_each(event, func);
3270 perf_event_for_each_child(event, func);
3275 int perf_event_task_enable(void)
3277 struct perf_event *event;
3279 mutex_lock(¤t->perf_event_mutex);
3280 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3281 perf_event_for_each_child(event, perf_event_enable);
3282 mutex_unlock(¤t->perf_event_mutex);
3287 int perf_event_task_disable(void)
3289 struct perf_event *event;
3291 mutex_lock(¤t->perf_event_mutex);
3292 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3293 perf_event_for_each_child(event, perf_event_disable);
3294 mutex_unlock(¤t->perf_event_mutex);
3299 #ifndef PERF_EVENT_INDEX_OFFSET
3300 # define PERF_EVENT_INDEX_OFFSET 0
3303 static int perf_event_index(struct perf_event *event)
3305 if (event->hw.state & PERF_HES_STOPPED)
3308 if (event->state != PERF_EVENT_STATE_ACTIVE)
3311 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
3315 * Callers need to ensure there can be no nesting of this function, otherwise
3316 * the seqlock logic goes bad. We can not serialize this because the arch
3317 * code calls this from NMI context.
3319 void perf_event_update_userpage(struct perf_event *event)
3321 struct perf_event_mmap_page *userpg;
3322 struct perf_buffer *buffer;
3325 buffer = rcu_dereference(event->buffer);
3329 userpg = buffer->user_page;
3332 * Disable preemption so as to not let the corresponding user-space
3333 * spin too long if we get preempted.
3338 userpg->index = perf_event_index(event);
3339 userpg->offset = perf_event_count(event);
3340 if (event->state == PERF_EVENT_STATE_ACTIVE)
3341 userpg->offset -= local64_read(&event->hw.prev_count);
3343 userpg->time_enabled = event->total_time_enabled +
3344 atomic64_read(&event->child_total_time_enabled);
3346 userpg->time_running = event->total_time_running +
3347 atomic64_read(&event->child_total_time_running);
3356 static unsigned long perf_data_size(struct perf_buffer *buffer);
3359 perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
3361 long max_size = perf_data_size(buffer);
3364 buffer->watermark = min(max_size, watermark);
3366 if (!buffer->watermark)
3367 buffer->watermark = max_size / 2;
3369 if (flags & PERF_BUFFER_WRITABLE)
3370 buffer->writable = 1;
3372 atomic_set(&buffer->refcount, 1);
3375 #ifndef CONFIG_PERF_USE_VMALLOC
3378 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
3381 static struct page *
3382 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
3384 if (pgoff > buffer->nr_pages)
3388 return virt_to_page(buffer->user_page);
3390 return virt_to_page(buffer->data_pages[pgoff - 1]);
3393 static void *perf_mmap_alloc_page(int cpu)
3398 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
3399 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
3403 return page_address(page);
3406 static struct perf_buffer *
3407 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
3409 struct perf_buffer *buffer;
3413 size = sizeof(struct perf_buffer);
3414 size += nr_pages * sizeof(void *);
3416 buffer = kzalloc(size, GFP_KERNEL);
3420 buffer->user_page = perf_mmap_alloc_page(cpu);
3421 if (!buffer->user_page)
3422 goto fail_user_page;
3424 for (i = 0; i < nr_pages; i++) {
3425 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
3426 if (!buffer->data_pages[i])
3427 goto fail_data_pages;
3430 buffer->nr_pages = nr_pages;
3432 perf_buffer_init(buffer, watermark, flags);
3437 for (i--; i >= 0; i--)
3438 free_page((unsigned long)buffer->data_pages[i]);
3440 free_page((unsigned long)buffer->user_page);
3449 static void perf_mmap_free_page(unsigned long addr)
3451 struct page *page = virt_to_page((void *)addr);
3453 page->mapping = NULL;
3457 static void perf_buffer_free(struct perf_buffer *buffer)
3461 perf_mmap_free_page((unsigned long)buffer->user_page);
3462 for (i = 0; i < buffer->nr_pages; i++)
3463 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
3467 static inline int page_order(struct perf_buffer *buffer)
3475 * Back perf_mmap() with vmalloc memory.
3477 * Required for architectures that have d-cache aliasing issues.
3480 static inline int page_order(struct perf_buffer *buffer)
3482 return buffer->page_order;
3485 static struct page *
3486 perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
3488 if (pgoff > (1UL << page_order(buffer)))
3491 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
3494 static void perf_mmap_unmark_page(void *addr)
3496 struct page *page = vmalloc_to_page(addr);
3498 page->mapping = NULL;
3501 static void perf_buffer_free_work(struct work_struct *work)
3503 struct perf_buffer *buffer;
3507 buffer = container_of(work, struct perf_buffer, work);
3508 nr = 1 << page_order(buffer);
3510 base = buffer->user_page;
3511 for (i = 0; i < nr + 1; i++)
3512 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
3518 static void perf_buffer_free(struct perf_buffer *buffer)
3520 schedule_work(&buffer->work);
3523 static struct perf_buffer *
3524 perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
3526 struct perf_buffer *buffer;
3530 size = sizeof(struct perf_buffer);
3531 size += sizeof(void *);
3533 buffer = kzalloc(size, GFP_KERNEL);
3537 INIT_WORK(&buffer->work, perf_buffer_free_work);
3539 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
3543 buffer->user_page = all_buf;
3544 buffer->data_pages[0] = all_buf + PAGE_SIZE;
3545 buffer->page_order = ilog2(nr_pages);
3546 buffer->nr_pages = 1;
3548 perf_buffer_init(buffer, watermark, flags);
3561 static unsigned long perf_data_size(struct perf_buffer *buffer)
3563 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
3566 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3568 struct perf_event *event = vma->vm_file->private_data;
3569 struct perf_buffer *buffer;
3570 int ret = VM_FAULT_SIGBUS;
3572 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3573 if (vmf->pgoff == 0)
3579 buffer = rcu_dereference(event->buffer);
3583 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3586 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
3590 get_page(vmf->page);
3591 vmf->page->mapping = vma->vm_file->f_mapping;
3592 vmf->page->index = vmf->pgoff;
3601 static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
3603 struct perf_buffer *buffer;
3605 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
3606 perf_buffer_free(buffer);
3609 static struct perf_buffer *perf_buffer_get(struct perf_event *event)
3611 struct perf_buffer *buffer;
3614 buffer = rcu_dereference(event->buffer);
3616 if (!atomic_inc_not_zero(&buffer->refcount))
3624 static void perf_buffer_put(struct perf_buffer *buffer)
3626 if (!atomic_dec_and_test(&buffer->refcount))
3629 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
3632 static void perf_mmap_open(struct vm_area_struct *vma)
3634 struct perf_event *event = vma->vm_file->private_data;
3636 atomic_inc(&event->mmap_count);
3639 static void perf_mmap_close(struct vm_area_struct *vma)
3641 struct perf_event *event = vma->vm_file->private_data;
3643 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
3644 unsigned long size = perf_data_size(event->buffer);
3645 struct user_struct *user = event->mmap_user;
3646 struct perf_buffer *buffer = event->buffer;
3648 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
3649 vma->vm_mm->locked_vm -= event->mmap_locked;
3650 rcu_assign_pointer(event->buffer, NULL);
3651 mutex_unlock(&event->mmap_mutex);
3653 perf_buffer_put(buffer);
3658 static const struct vm_operations_struct perf_mmap_vmops = {
3659 .open = perf_mmap_open,
3660 .close = perf_mmap_close,
3661 .fault = perf_mmap_fault,
3662 .page_mkwrite = perf_mmap_fault,
3665 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3667 struct perf_event *event = file->private_data;
3668 unsigned long user_locked, user_lock_limit;
3669 struct user_struct *user = current_user();
3670 unsigned long locked, lock_limit;
3671 struct perf_buffer *buffer;
3672 unsigned long vma_size;
3673 unsigned long nr_pages;
3674 long user_extra, extra;
3675 int ret = 0, flags = 0;
3678 * Don't allow mmap() of inherited per-task counters. This would
3679 * create a performance issue due to all children writing to the
3682 if (event->cpu == -1 && event->attr.inherit)
3685 if (!(vma->vm_flags & VM_SHARED))
3688 vma_size = vma->vm_end - vma->vm_start;
3689 nr_pages = (vma_size / PAGE_SIZE) - 1;
3692 * If we have buffer pages ensure they're a power-of-two number, so we
3693 * can do bitmasks instead of modulo.
3695 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3698 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3701 if (vma->vm_pgoff != 0)
3704 WARN_ON_ONCE(event->ctx->parent_ctx);
3705 mutex_lock(&event->mmap_mutex);
3706 if (event->buffer) {
3707 if (event->buffer->nr_pages == nr_pages)
3708 atomic_inc(&event->buffer->refcount);
3714 user_extra = nr_pages + 1;
3715 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3718 * Increase the limit linearly with more CPUs:
3720 user_lock_limit *= num_online_cpus();
3722 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3725 if (user_locked > user_lock_limit)
3726 extra = user_locked - user_lock_limit;
3728 lock_limit = rlimit(RLIMIT_MEMLOCK);
3729 lock_limit >>= PAGE_SHIFT;
3730 locked = vma->vm_mm->locked_vm + extra;
3732 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3733 !capable(CAP_IPC_LOCK)) {
3738 WARN_ON(event->buffer);
3740 if (vma->vm_flags & VM_WRITE)
3741 flags |= PERF_BUFFER_WRITABLE;
3743 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3749 rcu_assign_pointer(event->buffer, buffer);
3751 atomic_long_add(user_extra, &user->locked_vm);
3752 event->mmap_locked = extra;
3753 event->mmap_user = get_current_user();
3754 vma->vm_mm->locked_vm += event->mmap_locked;
3758 atomic_inc(&event->mmap_count);
3759 mutex_unlock(&event->mmap_mutex);
3761 vma->vm_flags |= VM_RESERVED;
3762 vma->vm_ops = &perf_mmap_vmops;
3767 static int perf_fasync(int fd, struct file *filp, int on)
3769 struct inode *inode = filp->f_path.dentry->d_inode;
3770 struct perf_event *event = filp->private_data;
3773 mutex_lock(&inode->i_mutex);
3774 retval = fasync_helper(fd, filp, on, &event->fasync);
3775 mutex_unlock(&inode->i_mutex);
3783 static const struct file_operations perf_fops = {
3784 .llseek = no_llseek,
3785 .release = perf_release,
3788 .unlocked_ioctl = perf_ioctl,
3789 .compat_ioctl = perf_ioctl,
3791 .fasync = perf_fasync,
3797 * If there's data, ensure we set the poll() state and publish everything
3798 * to user-space before waking everybody up.
3801 void perf_event_wakeup(struct perf_event *event)
3803 wake_up_all(&event->waitq);
3805 if (event->pending_kill) {
3806 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3807 event->pending_kill = 0;
3811 static void perf_pending_event(struct irq_work *entry)
3813 struct perf_event *event = container_of(entry,
3814 struct perf_event, pending);
3816 if (event->pending_disable) {
3817 event->pending_disable = 0;
3818 __perf_event_disable(event);
3821 if (event->pending_wakeup) {
3822 event->pending_wakeup = 0;
3823 perf_event_wakeup(event);
3828 * We assume there is only KVM supporting the callbacks.
3829 * Later on, we might change it to a list if there is
3830 * another virtualization implementation supporting the callbacks.
3832 struct perf_guest_info_callbacks *perf_guest_cbs;
3834 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3836 perf_guest_cbs = cbs;
3839 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3841 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3843 perf_guest_cbs = NULL;
3846 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3851 static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
3852 unsigned long offset, unsigned long head)
3856 if (!buffer->writable)
3859 mask = perf_data_size(buffer) - 1;
3861 offset = (offset - tail) & mask;
3862 head = (head - tail) & mask;
3864 if ((int)(head - offset) < 0)
3870 static void perf_output_wakeup(struct perf_output_handle *handle)
3872 atomic_set(&handle->buffer->poll, POLL_IN);
3875 handle->event->pending_wakeup = 1;
3876 irq_work_queue(&handle->event->pending);
3878 perf_event_wakeup(handle->event);
3882 * We need to ensure a later event_id doesn't publish a head when a former
3883 * event isn't done writing. However since we need to deal with NMIs we
3884 * cannot fully serialize things.
3886 * We only publish the head (and generate a wakeup) when the outer-most
3889 static void perf_output_get_handle(struct perf_output_handle *handle)
3891 struct perf_buffer *buffer = handle->buffer;
3894 local_inc(&buffer->nest);
3895 handle->wakeup = local_read(&buffer->wakeup);
3898 static void perf_output_put_handle(struct perf_output_handle *handle)
3900 struct perf_buffer *buffer = handle->buffer;
3904 head = local_read(&buffer->head);
3907 * IRQ/NMI can happen here, which means we can miss a head update.
3910 if (!local_dec_and_test(&buffer->nest))
3914 * Publish the known good head. Rely on the full barrier implied
3915 * by atomic_dec_and_test() order the buffer->head read and this
3918 buffer->user_page->data_head = head;
3921 * Now check if we missed an update, rely on the (compiler)
3922 * barrier in atomic_dec_and_test() to re-read buffer->head.
3924 if (unlikely(head != local_read(&buffer->head))) {
3925 local_inc(&buffer->nest);
3929 if (handle->wakeup != local_read(&buffer->wakeup))
3930 perf_output_wakeup(handle);
3936 __always_inline void perf_output_copy(struct perf_output_handle *handle,
3937 const void *buf, unsigned int len)
3940 unsigned long size = min_t(unsigned long, handle->size, len);
3942 memcpy(handle->addr, buf, size);
3945 handle->addr += size;
3947 handle->size -= size;
3948 if (!handle->size) {
3949 struct perf_buffer *buffer = handle->buffer;
3952 handle->page &= buffer->nr_pages - 1;
3953 handle->addr = buffer->data_pages[handle->page];
3954 handle->size = PAGE_SIZE << page_order(buffer);
3959 static void __perf_event_header__init_id(struct perf_event_header *header,
3960 struct perf_sample_data *data,
3961 struct perf_event *event)
3963 u64 sample_type = event->attr.sample_type;
3965 data->type = sample_type;
3966 header->size += event->id_header_size;
3968 if (sample_type & PERF_SAMPLE_TID) {
3969 /* namespace issues */
3970 data->tid_entry.pid = perf_event_pid(event, current);
3971 data->tid_entry.tid = perf_event_tid(event, current);
3974 if (sample_type & PERF_SAMPLE_TIME)
3975 data->time = perf_clock();
3977 if (sample_type & PERF_SAMPLE_ID)
3978 data->id = primary_event_id(event);
3980 if (sample_type & PERF_SAMPLE_STREAM_ID)
3981 data->stream_id = event->id;
3983 if (sample_type & PERF_SAMPLE_CPU) {
3984 data->cpu_entry.cpu = raw_smp_processor_id();
3985 data->cpu_entry.reserved = 0;
3989 static void perf_event_header__init_id(struct perf_event_header *header,
3990 struct perf_sample_data *data,
3991 struct perf_event *event)
3993 if (event->attr.sample_id_all)
3994 __perf_event_header__init_id(header, data, event);
3997 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3998 struct perf_sample_data *data)
4000 u64 sample_type = data->type;
4002 if (sample_type & PERF_SAMPLE_TID)
4003 perf_output_put(handle, data->tid_entry);
4005 if (sample_type & PERF_SAMPLE_TIME)
4006 perf_output_put(handle, data->time);
4008 if (sample_type & PERF_SAMPLE_ID)
4009 perf_output_put(handle, data->id);
4011 if (sample_type & PERF_SAMPLE_STREAM_ID)
4012 perf_output_put(handle, data->stream_id);
4014 if (sample_type & PERF_SAMPLE_CPU)
4015 perf_output_put(handle, data->cpu_entry);
4018 static void perf_event__output_id_sample(struct perf_event *event,
4019 struct perf_output_handle *handle,
4020 struct perf_sample_data *sample)
4022 if (event->attr.sample_id_all)
4023 __perf_event__output_id_sample(handle, sample);
4026 int perf_output_begin(struct perf_output_handle *handle,
4027 struct perf_event *event, unsigned int size,
4028 int nmi, int sample)
4030 struct perf_buffer *buffer;
4031 unsigned long tail, offset, head;
4033 struct perf_sample_data sample_data;
4035 struct perf_event_header header;
4042 * For inherited events we send all the output towards the parent.
4045 event = event->parent;
4047 buffer = rcu_dereference(event->buffer);
4051 handle->buffer = buffer;
4052 handle->event = event;
4054 handle->sample = sample;
4056 if (!buffer->nr_pages)
4059 have_lost = local_read(&buffer->lost);
4061 lost_event.header.size = sizeof(lost_event);
4062 perf_event_header__init_id(&lost_event.header, &sample_data,
4064 size += lost_event.header.size;
4067 perf_output_get_handle(handle);
4071 * Userspace could choose to issue a mb() before updating the
4072 * tail pointer. So that all reads will be completed before the
4075 tail = ACCESS_ONCE(buffer->user_page->data_tail);
4077 offset = head = local_read(&buffer->head);
4079 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
4081 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
4083 if (head - local_read(&buffer->wakeup) > buffer->watermark)
4084 local_add(buffer->watermark, &buffer->wakeup);
4086 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
4087 handle->page &= buffer->nr_pages - 1;
4088 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
4089 handle->addr = buffer->data_pages[handle->page];
4090 handle->addr += handle->size;
4091 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
4094 lost_event.header.type = PERF_RECORD_LOST;
4095 lost_event.header.misc = 0;
4096 lost_event.id = event->id;
4097 lost_event.lost = local_xchg(&buffer->lost, 0);
4099 perf_output_put(handle, lost_event);
4100 perf_event__output_id_sample(event, handle, &sample_data);
4106 local_inc(&buffer->lost);
4107 perf_output_put_handle(handle);
4114 void perf_output_end(struct perf_output_handle *handle)
4116 struct perf_event *event = handle->event;
4117 struct perf_buffer *buffer = handle->buffer;
4119 int wakeup_events = event->attr.wakeup_events;
4121 if (handle->sample && wakeup_events) {
4122 int events = local_inc_return(&buffer->events);
4123 if (events >= wakeup_events) {
4124 local_sub(wakeup_events, &buffer->events);
4125 local_inc(&buffer->wakeup);
4129 perf_output_put_handle(handle);
4133 static void perf_output_read_one(struct perf_output_handle *handle,
4134 struct perf_event *event,
4135 u64 enabled, u64 running)
4137 u64 read_format = event->attr.read_format;
4141 values[n++] = perf_event_count(event);
4142 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4143 values[n++] = enabled +
4144 atomic64_read(&event->child_total_time_enabled);
4146 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4147 values[n++] = running +
4148 atomic64_read(&event->child_total_time_running);
4150 if (read_format & PERF_FORMAT_ID)
4151 values[n++] = primary_event_id(event);
4153 perf_output_copy(handle, values, n * sizeof(u64));
4157 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4159 static void perf_output_read_group(struct perf_output_handle *handle,
4160 struct perf_event *event,
4161 u64 enabled, u64 running)
4163 struct perf_event *leader = event->group_leader, *sub;
4164 u64 read_format = event->attr.read_format;
4168 values[n++] = 1 + leader->nr_siblings;
4170 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4171 values[n++] = enabled;
4173 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4174 values[n++] = running;
4176 if (leader != event)
4177 leader->pmu->read(leader);
4179 values[n++] = perf_event_count(leader);
4180 if (read_format & PERF_FORMAT_ID)
4181 values[n++] = primary_event_id(leader);
4183 perf_output_copy(handle, values, n * sizeof(u64));
4185 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4189 sub->pmu->read(sub);
4191 values[n++] = perf_event_count(sub);
4192 if (read_format & PERF_FORMAT_ID)
4193 values[n++] = primary_event_id(sub);
4195 perf_output_copy(handle, values, n * sizeof(u64));
4199 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4200 PERF_FORMAT_TOTAL_TIME_RUNNING)
4202 static void perf_output_read(struct perf_output_handle *handle,
4203 struct perf_event *event)
4205 u64 enabled = 0, running = 0, now, ctx_time;
4206 u64 read_format = event->attr.read_format;
4209 * compute total_time_enabled, total_time_running
4210 * based on snapshot values taken when the event
4211 * was last scheduled in.
4213 * we cannot simply called update_context_time()
4214 * because of locking issue as we are called in
4217 if (read_format & PERF_FORMAT_TOTAL_TIMES) {
4219 ctx_time = event->shadow_ctx_time + now;
4220 enabled = ctx_time - event->tstamp_enabled;
4221 running = ctx_time - event->tstamp_running;
4224 if (event->attr.read_format & PERF_FORMAT_GROUP)
4225 perf_output_read_group(handle, event, enabled, running);
4227 perf_output_read_one(handle, event, enabled, running);
4230 void perf_output_sample(struct perf_output_handle *handle,
4231 struct perf_event_header *header,
4232 struct perf_sample_data *data,
4233 struct perf_event *event)
4235 u64 sample_type = data->type;
4237 perf_output_put(handle, *header);
4239 if (sample_type & PERF_SAMPLE_IP)
4240 perf_output_put(handle, data->ip);
4242 if (sample_type & PERF_SAMPLE_TID)
4243 perf_output_put(handle, data->tid_entry);
4245 if (sample_type & PERF_SAMPLE_TIME)
4246 perf_output_put(handle, data->time);
4248 if (sample_type & PERF_SAMPLE_ADDR)
4249 perf_output_put(handle, data->addr);
4251 if (sample_type & PERF_SAMPLE_ID)
4252 perf_output_put(handle, data->id);
4254 if (sample_type & PERF_SAMPLE_STREAM_ID)
4255 perf_output_put(handle, data->stream_id);
4257 if (sample_type & PERF_SAMPLE_CPU)
4258 perf_output_put(handle, data->cpu_entry);
4260 if (sample_type & PERF_SAMPLE_PERIOD)
4261 perf_output_put(handle, data->period);
4263 if (sample_type & PERF_SAMPLE_READ)
4264 perf_output_read(handle, event);
4266 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4267 if (data->callchain) {
4270 if (data->callchain)
4271 size += data->callchain->nr;
4273 size *= sizeof(u64);
4275 perf_output_copy(handle, data->callchain, size);
4278 perf_output_put(handle, nr);
4282 if (sample_type & PERF_SAMPLE_RAW) {
4284 perf_output_put(handle, data->raw->size);
4285 perf_output_copy(handle, data->raw->data,
4292 .size = sizeof(u32),
4295 perf_output_put(handle, raw);
4300 void perf_prepare_sample(struct perf_event_header *header,
4301 struct perf_sample_data *data,
4302 struct perf_event *event,
4303 struct pt_regs *regs)
4305 u64 sample_type = event->attr.sample_type;
4307 header->type = PERF_RECORD_SAMPLE;
4308 header->size = sizeof(*header) + event->header_size;
4311 header->misc |= perf_misc_flags(regs);
4313 __perf_event_header__init_id(header, data, event);
4315 if (sample_type & PERF_SAMPLE_IP)
4316 data->ip = perf_instruction_pointer(regs);
4318 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4321 data->callchain = perf_callchain(regs);
4323 if (data->callchain)
4324 size += data->callchain->nr;
4326 header->size += size * sizeof(u64);
4329 if (sample_type & PERF_SAMPLE_RAW) {
4330 int size = sizeof(u32);
4333 size += data->raw->size;
4335 size += sizeof(u32);
4337 WARN_ON_ONCE(size & (sizeof(u64)-1));
4338 header->size += size;
4342 static void perf_event_output(struct perf_event *event, int nmi,
4343 struct perf_sample_data *data,
4344 struct pt_regs *regs)
4346 struct perf_output_handle handle;
4347 struct perf_event_header header;
4349 /* protect the callchain buffers */
4352 perf_prepare_sample(&header, data, event, regs);
4354 if (perf_output_begin(&handle, event, header.size, nmi, 1))
4357 perf_output_sample(&handle, &header, data, event);
4359 perf_output_end(&handle);
4369 struct perf_read_event {
4370 struct perf_event_header header;
4377 perf_event_read_event(struct perf_event *event,
4378 struct task_struct *task)
4380 struct perf_output_handle handle;
4381 struct perf_sample_data sample;
4382 struct perf_read_event read_event = {
4384 .type = PERF_RECORD_READ,
4386 .size = sizeof(read_event) + event->read_size,
4388 .pid = perf_event_pid(event, task),
4389 .tid = perf_event_tid(event, task),
4393 perf_event_header__init_id(&read_event.header, &sample, event);
4394 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
4398 perf_output_put(&handle, read_event);
4399 perf_output_read(&handle, event);
4400 perf_event__output_id_sample(event, &handle, &sample);
4402 perf_output_end(&handle);
4406 * task tracking -- fork/exit
4408 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4411 struct perf_task_event {
4412 struct task_struct *task;
4413 struct perf_event_context *task_ctx;
4416 struct perf_event_header header;
4426 static void perf_event_task_output(struct perf_event *event,
4427 struct perf_task_event *task_event)
4429 struct perf_output_handle handle;
4430 struct perf_sample_data sample;
4431 struct task_struct *task = task_event->task;
4432 int ret, size = task_event->event_id.header.size;
4434 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4436 ret = perf_output_begin(&handle, event,
4437 task_event->event_id.header.size, 0, 0);
4441 task_event->event_id.pid = perf_event_pid(event, task);
4442 task_event->event_id.ppid = perf_event_pid(event, current);
4444 task_event->event_id.tid = perf_event_tid(event, task);
4445 task_event->event_id.ptid = perf_event_tid(event, current);
4447 perf_output_put(&handle, task_event->event_id);
4449 perf_event__output_id_sample(event, &handle, &sample);
4451 perf_output_end(&handle);
4453 task_event->event_id.header.size = size;
4456 static int perf_event_task_match(struct perf_event *event)
4458 if (event->state < PERF_EVENT_STATE_INACTIVE)
4461 if (!event_filter_match(event))
4464 if (event->attr.comm || event->attr.mmap ||
4465 event->attr.mmap_data || event->attr.task)
4471 static void perf_event_task_ctx(struct perf_event_context *ctx,
4472 struct perf_task_event *task_event)
4474 struct perf_event *event;
4476 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4477 if (perf_event_task_match(event))
4478 perf_event_task_output(event, task_event);
4482 static void perf_event_task_event(struct perf_task_event *task_event)
4484 struct perf_cpu_context *cpuctx;
4485 struct perf_event_context *ctx;
4490 list_for_each_entry_rcu(pmu, &pmus, entry) {
4491 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4492 if (cpuctx->active_pmu != pmu)
4494 perf_event_task_ctx(&cpuctx->ctx, task_event);
4496 ctx = task_event->task_ctx;
4498 ctxn = pmu->task_ctx_nr;
4501 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4504 perf_event_task_ctx(ctx, task_event);
4506 put_cpu_ptr(pmu->pmu_cpu_context);
4511 static void perf_event_task(struct task_struct *task,
4512 struct perf_event_context *task_ctx,
4515 struct perf_task_event task_event;
4517 if (!atomic_read(&nr_comm_events) &&
4518 !atomic_read(&nr_mmap_events) &&
4519 !atomic_read(&nr_task_events))
4522 task_event = (struct perf_task_event){
4524 .task_ctx = task_ctx,
4527 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4529 .size = sizeof(task_event.event_id),
4535 .time = perf_clock(),
4539 perf_event_task_event(&task_event);
4542 void perf_event_fork(struct task_struct *task)
4544 perf_event_task(task, NULL, 1);
4551 struct perf_comm_event {
4552 struct task_struct *task;
4557 struct perf_event_header header;
4564 static void perf_event_comm_output(struct perf_event *event,
4565 struct perf_comm_event *comm_event)
4567 struct perf_output_handle handle;
4568 struct perf_sample_data sample;
4569 int size = comm_event->event_id.header.size;
4572 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4573 ret = perf_output_begin(&handle, event,
4574 comm_event->event_id.header.size, 0, 0);
4579 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4580 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4582 perf_output_put(&handle, comm_event->event_id);
4583 perf_output_copy(&handle, comm_event->comm,
4584 comm_event->comm_size);
4586 perf_event__output_id_sample(event, &handle, &sample);
4588 perf_output_end(&handle);
4590 comm_event->event_id.header.size = size;
4593 static int perf_event_comm_match(struct perf_event *event)
4595 if (event->state < PERF_EVENT_STATE_INACTIVE)
4598 if (!event_filter_match(event))
4601 if (event->attr.comm)
4607 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4608 struct perf_comm_event *comm_event)
4610 struct perf_event *event;
4612 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4613 if (perf_event_comm_match(event))
4614 perf_event_comm_output(event, comm_event);
4618 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4620 struct perf_cpu_context *cpuctx;
4621 struct perf_event_context *ctx;
4622 char comm[TASK_COMM_LEN];
4627 memset(comm, 0, sizeof(comm));
4628 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4629 size = ALIGN(strlen(comm)+1, sizeof(u64));
4631 comm_event->comm = comm;
4632 comm_event->comm_size = size;
4634 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4636 list_for_each_entry_rcu(pmu, &pmus, entry) {
4637 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4638 if (cpuctx->active_pmu != pmu)
4640 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4642 ctxn = pmu->task_ctx_nr;
4646 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4648 perf_event_comm_ctx(ctx, comm_event);
4650 put_cpu_ptr(pmu->pmu_cpu_context);
4655 void perf_event_comm(struct task_struct *task)
4657 struct perf_comm_event comm_event;
4658 struct perf_event_context *ctx;
4661 for_each_task_context_nr(ctxn) {
4662 ctx = task->perf_event_ctxp[ctxn];
4666 perf_event_enable_on_exec(ctx);
4669 if (!atomic_read(&nr_comm_events))
4672 comm_event = (struct perf_comm_event){
4678 .type = PERF_RECORD_COMM,
4687 perf_event_comm_event(&comm_event);
4694 struct perf_mmap_event {
4695 struct vm_area_struct *vma;
4697 const char *file_name;
4701 struct perf_event_header header;
4711 static void perf_event_mmap_output(struct perf_event *event,
4712 struct perf_mmap_event *mmap_event)
4714 struct perf_output_handle handle;
4715 struct perf_sample_data sample;
4716 int size = mmap_event->event_id.header.size;
4719 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4720 ret = perf_output_begin(&handle, event,
4721 mmap_event->event_id.header.size, 0, 0);
4725 mmap_event->event_id.pid = perf_event_pid(event, current);
4726 mmap_event->event_id.tid = perf_event_tid(event, current);
4728 perf_output_put(&handle, mmap_event->event_id);
4729 perf_output_copy(&handle, mmap_event->file_name,
4730 mmap_event->file_size);
4732 perf_event__output_id_sample(event, &handle, &sample);
4734 perf_output_end(&handle);
4736 mmap_event->event_id.header.size = size;
4739 static int perf_event_mmap_match(struct perf_event *event,
4740 struct perf_mmap_event *mmap_event,
4743 if (event->state < PERF_EVENT_STATE_INACTIVE)
4746 if (!event_filter_match(event))
4749 if ((!executable && event->attr.mmap_data) ||
4750 (executable && event->attr.mmap))
4756 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4757 struct perf_mmap_event *mmap_event,
4760 struct perf_event *event;
4762 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4763 if (perf_event_mmap_match(event, mmap_event, executable))
4764 perf_event_mmap_output(event, mmap_event);
4768 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4770 struct perf_cpu_context *cpuctx;
4771 struct perf_event_context *ctx;
4772 struct vm_area_struct *vma = mmap_event->vma;
4773 struct file *file = vma->vm_file;
4781 memset(tmp, 0, sizeof(tmp));
4785 * d_path works from the end of the buffer backwards, so we
4786 * need to add enough zero bytes after the string to handle
4787 * the 64bit alignment we do later.
4789 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4791 name = strncpy(tmp, "//enomem", sizeof(tmp));
4794 name = d_path(&file->f_path, buf, PATH_MAX);
4796 name = strncpy(tmp, "//toolong", sizeof(tmp));
4800 if (arch_vma_name(mmap_event->vma)) {
4801 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4807 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4809 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4810 vma->vm_end >= vma->vm_mm->brk) {
4811 name = strncpy(tmp, "[heap]", sizeof(tmp));
4813 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4814 vma->vm_end >= vma->vm_mm->start_stack) {
4815 name = strncpy(tmp, "[stack]", sizeof(tmp));
4819 name = strncpy(tmp, "//anon", sizeof(tmp));
4824 size = ALIGN(strlen(name)+1, sizeof(u64));
4826 mmap_event->file_name = name;
4827 mmap_event->file_size = size;
4829 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4832 list_for_each_entry_rcu(pmu, &pmus, entry) {
4833 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4834 if (cpuctx->active_pmu != pmu)
4836 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4837 vma->vm_flags & VM_EXEC);
4839 ctxn = pmu->task_ctx_nr;
4843 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4845 perf_event_mmap_ctx(ctx, mmap_event,
4846 vma->vm_flags & VM_EXEC);
4849 put_cpu_ptr(pmu->pmu_cpu_context);
4856 void perf_event_mmap(struct vm_area_struct *vma)
4858 struct perf_mmap_event mmap_event;
4860 if (!atomic_read(&nr_mmap_events))
4863 mmap_event = (struct perf_mmap_event){
4869 .type = PERF_RECORD_MMAP,
4870 .misc = PERF_RECORD_MISC_USER,
4875 .start = vma->vm_start,
4876 .len = vma->vm_end - vma->vm_start,
4877 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4881 perf_event_mmap_event(&mmap_event);
4885 * IRQ throttle logging
4888 static void perf_log_throttle(struct perf_event *event, int enable)
4890 struct perf_output_handle handle;
4891 struct perf_sample_data sample;
4895 struct perf_event_header header;
4899 } throttle_event = {
4901 .type = PERF_RECORD_THROTTLE,
4903 .size = sizeof(throttle_event),
4905 .time = perf_clock(),
4906 .id = primary_event_id(event),
4907 .stream_id = event->id,
4911 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4913 perf_event_header__init_id(&throttle_event.header, &sample, event);
4915 ret = perf_output_begin(&handle, event,
4916 throttle_event.header.size, 1, 0);
4920 perf_output_put(&handle, throttle_event);
4921 perf_event__output_id_sample(event, &handle, &sample);
4922 perf_output_end(&handle);
4926 * Generic event overflow handling, sampling.
4929 static int __perf_event_overflow(struct perf_event *event, int nmi,
4930 int throttle, struct perf_sample_data *data,
4931 struct pt_regs *regs)
4933 int events = atomic_read(&event->event_limit);
4934 struct hw_perf_event *hwc = &event->hw;
4938 * Non-sampling counters might still use the PMI to fold short
4939 * hardware counters, ignore those.
4941 if (unlikely(!is_sampling_event(event)))
4947 if (hwc->interrupts != MAX_INTERRUPTS) {
4949 if (HZ * hwc->interrupts >
4950 (u64)sysctl_perf_event_sample_rate) {
4951 hwc->interrupts = MAX_INTERRUPTS;
4952 perf_log_throttle(event, 0);
4957 * Keep re-disabling events even though on the previous
4958 * pass we disabled it - just in case we raced with a
4959 * sched-in and the event got enabled again:
4965 if (event->attr.freq) {
4966 u64 now = perf_clock();
4967 s64 delta = now - hwc->freq_time_stamp;
4969 hwc->freq_time_stamp = now;
4971 if (delta > 0 && delta < 2*TICK_NSEC)
4972 perf_adjust_period(event, delta, hwc->last_period);
4976 * XXX event_limit might not quite work as expected on inherited
4980 event->pending_kill = POLL_IN;
4981 if (events && atomic_dec_and_test(&event->event_limit)) {
4983 event->pending_kill = POLL_HUP;
4985 event->pending_disable = 1;
4986 irq_work_queue(&event->pending);
4988 perf_event_disable(event);
4991 if (event->overflow_handler)
4992 event->overflow_handler(event, nmi, data, regs);
4994 perf_event_output(event, nmi, data, regs);
4999 int perf_event_overflow(struct perf_event *event, int nmi,
5000 struct perf_sample_data *data,
5001 struct pt_regs *regs)
5003 return __perf_event_overflow(event, nmi, 1, data, regs);
5007 * Generic software event infrastructure
5010 struct swevent_htable {
5011 struct swevent_hlist *swevent_hlist;
5012 struct mutex hlist_mutex;
5015 /* Recursion avoidance in each contexts */
5016 int recursion[PERF_NR_CONTEXTS];
5019 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5022 * We directly increment event->count and keep a second value in
5023 * event->hw.period_left to count intervals. This period event
5024 * is kept in the range [-sample_period, 0] so that we can use the
5028 static u64 perf_swevent_set_period(struct perf_event *event)
5030 struct hw_perf_event *hwc = &event->hw;
5031 u64 period = hwc->last_period;
5035 hwc->last_period = hwc->sample_period;
5038 old = val = local64_read(&hwc->period_left);
5042 nr = div64_u64(period + val, period);
5043 offset = nr * period;
5045 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
5051 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
5052 int nmi, struct perf_sample_data *data,
5053 struct pt_regs *regs)
5055 struct hw_perf_event *hwc = &event->hw;
5058 data->period = event->hw.last_period;
5060 overflow = perf_swevent_set_period(event);
5062 if (hwc->interrupts == MAX_INTERRUPTS)
5065 for (; overflow; overflow--) {
5066 if (__perf_event_overflow(event, nmi, throttle,
5069 * We inhibit the overflow from happening when
5070 * hwc->interrupts == MAX_INTERRUPTS.
5078 static void perf_swevent_event(struct perf_event *event, u64 nr,
5079 int nmi, struct perf_sample_data *data,
5080 struct pt_regs *regs)
5082 struct hw_perf_event *hwc = &event->hw;
5084 local64_add(nr, &event->count);
5089 if (!is_sampling_event(event))
5092 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
5093 return perf_swevent_overflow(event, 1, nmi, data, regs);
5095 if (local64_add_negative(nr, &hwc->period_left))
5098 perf_swevent_overflow(event, 0, nmi, data, regs);
5101 static int perf_exclude_event(struct perf_event *event,
5102 struct pt_regs *regs)
5104 if (event->hw.state & PERF_HES_STOPPED)
5108 if (event->attr.exclude_user && user_mode(regs))
5111 if (event->attr.exclude_kernel && !user_mode(regs))
5118 static int perf_swevent_match(struct perf_event *event,
5119 enum perf_type_id type,
5121 struct perf_sample_data *data,
5122 struct pt_regs *regs)
5124 if (event->attr.type != type)
5127 if (event->attr.config != event_id)
5130 if (perf_exclude_event(event, regs))
5136 static inline u64 swevent_hash(u64 type, u32 event_id)
5138 u64 val = event_id | (type << 32);
5140 return hash_64(val, SWEVENT_HLIST_BITS);
5143 static inline struct hlist_head *
5144 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
5146 u64 hash = swevent_hash(type, event_id);
5148 return &hlist->heads[hash];
5151 /* For the read side: events when they trigger */
5152 static inline struct hlist_head *
5153 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
5155 struct swevent_hlist *hlist;
5157 hlist = rcu_dereference(swhash->swevent_hlist);
5161 return __find_swevent_head(hlist, type, event_id);
5164 /* For the event head insertion and removal in the hlist */
5165 static inline struct hlist_head *
5166 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
5168 struct swevent_hlist *hlist;
5169 u32 event_id = event->attr.config;
5170 u64 type = event->attr.type;
5173 * Event scheduling is always serialized against hlist allocation
5174 * and release. Which makes the protected version suitable here.
5175 * The context lock guarantees that.
5177 hlist = rcu_dereference_protected(swhash->swevent_hlist,
5178 lockdep_is_held(&event->ctx->lock));
5182 return __find_swevent_head(hlist, type, event_id);
5185 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5187 struct perf_sample_data *data,
5188 struct pt_regs *regs)
5190 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5191 struct perf_event *event;
5192 struct hlist_node *node;
5193 struct hlist_head *head;
5196 head = find_swevent_head_rcu(swhash, type, event_id);
5200 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5201 if (perf_swevent_match(event, type, event_id, data, regs))
5202 perf_swevent_event(event, nr, nmi, data, regs);
5208 int perf_swevent_get_recursion_context(void)
5210 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5212 return get_recursion_context(swhash->recursion);
5214 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
5216 inline void perf_swevent_put_recursion_context(int rctx)
5218 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5220 put_recursion_context(swhash->recursion, rctx);
5223 void __perf_sw_event(u32 event_id, u64 nr, int nmi,
5224 struct pt_regs *regs, u64 addr)
5226 struct perf_sample_data data;
5229 preempt_disable_notrace();
5230 rctx = perf_swevent_get_recursion_context();
5234 perf_sample_data_init(&data, addr);
5236 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
5238 perf_swevent_put_recursion_context(rctx);
5239 preempt_enable_notrace();
5242 static void perf_swevent_read(struct perf_event *event)
5246 static int perf_swevent_add(struct perf_event *event, int flags)
5248 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5249 struct hw_perf_event *hwc = &event->hw;
5250 struct hlist_head *head;
5252 if (is_sampling_event(event)) {
5253 hwc->last_period = hwc->sample_period;
5254 perf_swevent_set_period(event);
5257 hwc->state = !(flags & PERF_EF_START);
5259 head = find_swevent_head(swhash, event);
5260 if (WARN_ON_ONCE(!head))
5263 hlist_add_head_rcu(&event->hlist_entry, head);
5268 static void perf_swevent_del(struct perf_event *event, int flags)
5270 hlist_del_rcu(&event->hlist_entry);
5273 static void perf_swevent_start(struct perf_event *event, int flags)
5275 event->hw.state = 0;
5278 static void perf_swevent_stop(struct perf_event *event, int flags)
5280 event->hw.state = PERF_HES_STOPPED;
5283 /* Deref the hlist from the update side */
5284 static inline struct swevent_hlist *
5285 swevent_hlist_deref(struct swevent_htable *swhash)
5287 return rcu_dereference_protected(swhash->swevent_hlist,
5288 lockdep_is_held(&swhash->hlist_mutex));
5291 static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
5293 struct swevent_hlist *hlist;
5295 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
5299 static void swevent_hlist_release(struct swevent_htable *swhash)
5301 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
5306 rcu_assign_pointer(swhash->swevent_hlist, NULL);
5307 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
5310 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5312 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5314 mutex_lock(&swhash->hlist_mutex);
5316 if (!--swhash->hlist_refcount)
5317 swevent_hlist_release(swhash);
5319 mutex_unlock(&swhash->hlist_mutex);
5322 static void swevent_hlist_put(struct perf_event *event)
5326 if (event->cpu != -1) {
5327 swevent_hlist_put_cpu(event, event->cpu);
5331 for_each_possible_cpu(cpu)
5332 swevent_hlist_put_cpu(event, cpu);
5335 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5337 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5340 mutex_lock(&swhash->hlist_mutex);
5342 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
5343 struct swevent_hlist *hlist;
5345 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5350 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5352 swhash->hlist_refcount++;
5354 mutex_unlock(&swhash->hlist_mutex);
5359 static int swevent_hlist_get(struct perf_event *event)
5362 int cpu, failed_cpu;
5364 if (event->cpu != -1)
5365 return swevent_hlist_get_cpu(event, event->cpu);
5368 for_each_possible_cpu(cpu) {
5369 err = swevent_hlist_get_cpu(event, cpu);
5379 for_each_possible_cpu(cpu) {
5380 if (cpu == failed_cpu)
5382 swevent_hlist_put_cpu(event, cpu);
5389 atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
5391 static void sw_perf_event_destroy(struct perf_event *event)
5393 u64 event_id = event->attr.config;
5395 WARN_ON(event->parent);
5397 jump_label_dec(&perf_swevent_enabled[event_id]);
5398 swevent_hlist_put(event);
5401 static int perf_swevent_init(struct perf_event *event)
5403 int event_id = event->attr.config;
5405 if (event->attr.type != PERF_TYPE_SOFTWARE)
5409 case PERF_COUNT_SW_CPU_CLOCK:
5410 case PERF_COUNT_SW_TASK_CLOCK:
5417 if (event_id >= PERF_COUNT_SW_MAX)
5420 if (!event->parent) {
5423 err = swevent_hlist_get(event);
5427 jump_label_inc(&perf_swevent_enabled[event_id]);
5428 event->destroy = sw_perf_event_destroy;
5434 static struct pmu perf_swevent = {
5435 .task_ctx_nr = perf_sw_context,
5437 .event_init = perf_swevent_init,
5438 .add = perf_swevent_add,
5439 .del = perf_swevent_del,
5440 .start = perf_swevent_start,
5441 .stop = perf_swevent_stop,
5442 .read = perf_swevent_read,
5445 #ifdef CONFIG_EVENT_TRACING
5447 static int perf_tp_filter_match(struct perf_event *event,
5448 struct perf_sample_data *data)
5450 void *record = data->raw->data;
5452 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5457 static int perf_tp_event_match(struct perf_event *event,
5458 struct perf_sample_data *data,
5459 struct pt_regs *regs)
5462 * All tracepoints are from kernel-space.
5464 if (event->attr.exclude_kernel)
5467 if (!perf_tp_filter_match(event, data))
5473 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5474 struct pt_regs *regs, struct hlist_head *head, int rctx)
5476 struct perf_sample_data data;
5477 struct perf_event *event;
5478 struct hlist_node *node;
5480 struct perf_raw_record raw = {
5485 perf_sample_data_init(&data, addr);
5488 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5489 if (perf_tp_event_match(event, &data, regs))
5490 perf_swevent_event(event, count, 1, &data, regs);
5493 perf_swevent_put_recursion_context(rctx);
5495 EXPORT_SYMBOL_GPL(perf_tp_event);
5497 static void tp_perf_event_destroy(struct perf_event *event)
5499 perf_trace_destroy(event);
5502 static int perf_tp_event_init(struct perf_event *event)
5506 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5509 err = perf_trace_init(event);
5513 event->destroy = tp_perf_event_destroy;
5518 static struct pmu perf_tracepoint = {
5519 .task_ctx_nr = perf_sw_context,
5521 .event_init = perf_tp_event_init,
5522 .add = perf_trace_add,
5523 .del = perf_trace_del,
5524 .start = perf_swevent_start,
5525 .stop = perf_swevent_stop,
5526 .read = perf_swevent_read,
5529 static inline void perf_tp_register(void)
5531 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5534 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5539 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5542 filter_str = strndup_user(arg, PAGE_SIZE);
5543 if (IS_ERR(filter_str))
5544 return PTR_ERR(filter_str);
5546 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5552 static void perf_event_free_filter(struct perf_event *event)
5554 ftrace_profile_free_filter(event);
5559 static inline void perf_tp_register(void)
5563 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5568 static void perf_event_free_filter(struct perf_event *event)
5572 #endif /* CONFIG_EVENT_TRACING */
5574 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5575 void perf_bp_event(struct perf_event *bp, void *data)
5577 struct perf_sample_data sample;
5578 struct pt_regs *regs = data;
5580 perf_sample_data_init(&sample, bp->attr.bp_addr);
5582 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5583 perf_swevent_event(bp, 1, 1, &sample, regs);
5588 * hrtimer based swevent callback
5591 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5593 enum hrtimer_restart ret = HRTIMER_RESTART;
5594 struct perf_sample_data data;
5595 struct pt_regs *regs;
5596 struct perf_event *event;
5599 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5600 event->pmu->read(event);
5602 perf_sample_data_init(&data, 0);
5603 data.period = event->hw.last_period;
5604 regs = get_irq_regs();
5606 if (regs && !perf_exclude_event(event, regs)) {
5607 if (!(event->attr.exclude_idle && current->pid == 0))
5608 if (perf_event_overflow(event, 0, &data, regs))
5609 ret = HRTIMER_NORESTART;
5612 period = max_t(u64, 10000, event->hw.sample_period);
5613 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5618 static void perf_swevent_start_hrtimer(struct perf_event *event)
5620 struct hw_perf_event *hwc = &event->hw;
5623 if (!is_sampling_event(event))
5626 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5627 hwc->hrtimer.function = perf_swevent_hrtimer;
5629 period = local64_read(&hwc->period_left);
5634 local64_set(&hwc->period_left, 0);
5636 period = max_t(u64, 10000, hwc->sample_period);
5638 __hrtimer_start_range_ns(&hwc->hrtimer,
5639 ns_to_ktime(period), 0,
5640 HRTIMER_MODE_REL_PINNED, 0);
5643 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5645 struct hw_perf_event *hwc = &event->hw;
5647 if (is_sampling_event(event)) {
5648 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5649 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5651 hrtimer_cancel(&hwc->hrtimer);
5656 * Software event: cpu wall time clock
5659 static void cpu_clock_event_update(struct perf_event *event)
5664 now = local_clock();
5665 prev = local64_xchg(&event->hw.prev_count, now);
5666 local64_add(now - prev, &event->count);
5669 static void cpu_clock_event_start(struct perf_event *event, int flags)
5671 local64_set(&event->hw.prev_count, local_clock());
5672 perf_swevent_start_hrtimer(event);
5675 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5677 perf_swevent_cancel_hrtimer(event);
5678 cpu_clock_event_update(event);
5681 static int cpu_clock_event_add(struct perf_event *event, int flags)
5683 if (flags & PERF_EF_START)
5684 cpu_clock_event_start(event, flags);
5689 static void cpu_clock_event_del(struct perf_event *event, int flags)
5691 cpu_clock_event_stop(event, flags);
5694 static void cpu_clock_event_read(struct perf_event *event)
5696 cpu_clock_event_update(event);
5699 static int cpu_clock_event_init(struct perf_event *event)
5701 if (event->attr.type != PERF_TYPE_SOFTWARE)
5704 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5710 static struct pmu perf_cpu_clock = {
5711 .task_ctx_nr = perf_sw_context,
5713 .event_init = cpu_clock_event_init,
5714 .add = cpu_clock_event_add,
5715 .del = cpu_clock_event_del,
5716 .start = cpu_clock_event_start,
5717 .stop = cpu_clock_event_stop,
5718 .read = cpu_clock_event_read,
5722 * Software event: task time clock
5725 static void task_clock_event_update(struct perf_event *event, u64 now)
5730 prev = local64_xchg(&event->hw.prev_count, now);
5732 local64_add(delta, &event->count);
5735 static void task_clock_event_start(struct perf_event *event, int flags)
5737 local64_set(&event->hw.prev_count, event->ctx->time);
5738 perf_swevent_start_hrtimer(event);
5741 static void task_clock_event_stop(struct perf_event *event, int flags)
5743 perf_swevent_cancel_hrtimer(event);
5744 task_clock_event_update(event, event->ctx->time);
5747 static int task_clock_event_add(struct perf_event *event, int flags)
5749 if (flags & PERF_EF_START)
5750 task_clock_event_start(event, flags);
5755 static void task_clock_event_del(struct perf_event *event, int flags)
5757 task_clock_event_stop(event, PERF_EF_UPDATE);
5760 static void task_clock_event_read(struct perf_event *event)
5765 update_context_time(event->ctx);
5766 update_cgrp_time_from_event(event);
5767 time = event->ctx->time;
5769 u64 now = perf_clock();
5770 u64 delta = now - event->ctx->timestamp;
5771 time = event->ctx->time + delta;
5774 task_clock_event_update(event, time);
5777 static int task_clock_event_init(struct perf_event *event)
5779 if (event->attr.type != PERF_TYPE_SOFTWARE)
5782 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5788 static struct pmu perf_task_clock = {
5789 .task_ctx_nr = perf_sw_context,
5791 .event_init = task_clock_event_init,
5792 .add = task_clock_event_add,
5793 .del = task_clock_event_del,
5794 .start = task_clock_event_start,
5795 .stop = task_clock_event_stop,
5796 .read = task_clock_event_read,
5799 static void perf_pmu_nop_void(struct pmu *pmu)
5803 static int perf_pmu_nop_int(struct pmu *pmu)
5808 static void perf_pmu_start_txn(struct pmu *pmu)
5810 perf_pmu_disable(pmu);
5813 static int perf_pmu_commit_txn(struct pmu *pmu)
5815 perf_pmu_enable(pmu);
5819 static void perf_pmu_cancel_txn(struct pmu *pmu)
5821 perf_pmu_enable(pmu);
5825 * Ensures all contexts with the same task_ctx_nr have the same
5826 * pmu_cpu_context too.
5828 static void *find_pmu_context(int ctxn)
5835 list_for_each_entry(pmu, &pmus, entry) {
5836 if (pmu->task_ctx_nr == ctxn)
5837 return pmu->pmu_cpu_context;
5843 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5847 for_each_possible_cpu(cpu) {
5848 struct perf_cpu_context *cpuctx;
5850 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5852 if (cpuctx->active_pmu == old_pmu)
5853 cpuctx->active_pmu = pmu;
5857 static void free_pmu_context(struct pmu *pmu)
5861 mutex_lock(&pmus_lock);
5863 * Like a real lame refcount.
5865 list_for_each_entry(i, &pmus, entry) {
5866 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5867 update_pmu_context(i, pmu);
5872 free_percpu(pmu->pmu_cpu_context);
5874 mutex_unlock(&pmus_lock);
5876 static struct idr pmu_idr;
5879 type_show(struct device *dev, struct device_attribute *attr, char *page)
5881 struct pmu *pmu = dev_get_drvdata(dev);
5883 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5886 static struct device_attribute pmu_dev_attrs[] = {
5891 static int pmu_bus_running;
5892 static struct bus_type pmu_bus = {
5893 .name = "event_source",
5894 .dev_attrs = pmu_dev_attrs,
5897 static void pmu_dev_release(struct device *dev)
5902 static int pmu_dev_alloc(struct pmu *pmu)
5906 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5910 device_initialize(pmu->dev);
5911 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5915 dev_set_drvdata(pmu->dev, pmu);
5916 pmu->dev->bus = &pmu_bus;
5917 pmu->dev->release = pmu_dev_release;
5918 ret = device_add(pmu->dev);
5926 put_device(pmu->dev);
5930 static struct lock_class_key cpuctx_mutex;
5932 int perf_pmu_register(struct pmu *pmu, char *name, int type)
5936 mutex_lock(&pmus_lock);
5938 pmu->pmu_disable_count = alloc_percpu(int);
5939 if (!pmu->pmu_disable_count)
5948 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5952 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5960 if (pmu_bus_running) {
5961 ret = pmu_dev_alloc(pmu);
5967 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5968 if (pmu->pmu_cpu_context)
5969 goto got_cpu_context;
5971 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5972 if (!pmu->pmu_cpu_context)
5975 for_each_possible_cpu(cpu) {
5976 struct perf_cpu_context *cpuctx;
5978 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5979 __perf_event_init_context(&cpuctx->ctx);
5980 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
5981 cpuctx->ctx.type = cpu_context;
5982 cpuctx->ctx.pmu = pmu;
5983 cpuctx->jiffies_interval = 1;
5984 INIT_LIST_HEAD(&cpuctx->rotation_list);
5985 cpuctx->active_pmu = pmu;
5989 if (!pmu->start_txn) {
5990 if (pmu->pmu_enable) {
5992 * If we have pmu_enable/pmu_disable calls, install
5993 * transaction stubs that use that to try and batch
5994 * hardware accesses.
5996 pmu->start_txn = perf_pmu_start_txn;
5997 pmu->commit_txn = perf_pmu_commit_txn;
5998 pmu->cancel_txn = perf_pmu_cancel_txn;
6000 pmu->start_txn = perf_pmu_nop_void;
6001 pmu->commit_txn = perf_pmu_nop_int;
6002 pmu->cancel_txn = perf_pmu_nop_void;
6006 if (!pmu->pmu_enable) {
6007 pmu->pmu_enable = perf_pmu_nop_void;
6008 pmu->pmu_disable = perf_pmu_nop_void;
6011 list_add_rcu(&pmu->entry, &pmus);
6014 mutex_unlock(&pmus_lock);
6019 device_del(pmu->dev);
6020 put_device(pmu->dev);
6023 if (pmu->type >= PERF_TYPE_MAX)
6024 idr_remove(&pmu_idr, pmu->type);
6027 free_percpu(pmu->pmu_disable_count);
6031 void perf_pmu_unregister(struct pmu *pmu)
6033 mutex_lock(&pmus_lock);
6034 list_del_rcu(&pmu->entry);
6035 mutex_unlock(&pmus_lock);
6038 * We dereference the pmu list under both SRCU and regular RCU, so
6039 * synchronize against both of those.
6041 synchronize_srcu(&pmus_srcu);
6044 free_percpu(pmu->pmu_disable_count);
6045 if (pmu->type >= PERF_TYPE_MAX)
6046 idr_remove(&pmu_idr, pmu->type);
6047 device_del(pmu->dev);
6048 put_device(pmu->dev);
6049 free_pmu_context(pmu);
6052 struct pmu *perf_init_event(struct perf_event *event)
6054 struct pmu *pmu = NULL;
6057 idx = srcu_read_lock(&pmus_srcu);
6060 pmu = idr_find(&pmu_idr, event->attr.type);
6065 list_for_each_entry_rcu(pmu, &pmus, entry) {
6066 int ret = pmu->event_init(event);
6070 if (ret != -ENOENT) {
6075 pmu = ERR_PTR(-ENOENT);
6077 srcu_read_unlock(&pmus_srcu, idx);
6083 * Allocate and initialize a event structure
6085 static struct perf_event *
6086 perf_event_alloc(struct perf_event_attr *attr, int cpu,
6087 struct task_struct *task,
6088 struct perf_event *group_leader,
6089 struct perf_event *parent_event,
6090 perf_overflow_handler_t overflow_handler)
6093 struct perf_event *event;
6094 struct hw_perf_event *hwc;
6097 if ((unsigned)cpu >= nr_cpu_ids) {
6098 if (!task || cpu != -1)
6099 return ERR_PTR(-EINVAL);
6102 event = kzalloc(sizeof(*event), GFP_KERNEL);
6104 return ERR_PTR(-ENOMEM);
6107 * Single events are their own group leaders, with an
6108 * empty sibling list:
6111 group_leader = event;
6113 mutex_init(&event->child_mutex);
6114 INIT_LIST_HEAD(&event->child_list);
6116 INIT_LIST_HEAD(&event->group_entry);
6117 INIT_LIST_HEAD(&event->event_entry);
6118 INIT_LIST_HEAD(&event->sibling_list);
6119 init_waitqueue_head(&event->waitq);
6120 init_irq_work(&event->pending, perf_pending_event);
6122 mutex_init(&event->mmap_mutex);
6125 event->attr = *attr;
6126 event->group_leader = group_leader;
6130 event->parent = parent_event;
6132 event->ns = get_pid_ns(current->nsproxy->pid_ns);
6133 event->id = atomic64_inc_return(&perf_event_id);
6135 event->state = PERF_EVENT_STATE_INACTIVE;
6138 event->attach_state = PERF_ATTACH_TASK;
6139 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6141 * hw_breakpoint is a bit difficult here..
6143 if (attr->type == PERF_TYPE_BREAKPOINT)
6144 event->hw.bp_target = task;
6148 if (!overflow_handler && parent_event)
6149 overflow_handler = parent_event->overflow_handler;
6151 event->overflow_handler = overflow_handler;
6154 event->state = PERF_EVENT_STATE_OFF;
6159 hwc->sample_period = attr->sample_period;
6160 if (attr->freq && attr->sample_freq)
6161 hwc->sample_period = 1;
6162 hwc->last_period = hwc->sample_period;
6164 local64_set(&hwc->period_left, hwc->sample_period);
6167 * we currently do not support PERF_FORMAT_GROUP on inherited events
6169 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6172 pmu = perf_init_event(event);
6178 else if (IS_ERR(pmu))
6183 put_pid_ns(event->ns);
6185 return ERR_PTR(err);
6190 if (!event->parent) {
6191 if (event->attach_state & PERF_ATTACH_TASK)
6192 jump_label_inc(&perf_sched_events);
6193 if (event->attr.mmap || event->attr.mmap_data)
6194 atomic_inc(&nr_mmap_events);
6195 if (event->attr.comm)
6196 atomic_inc(&nr_comm_events);
6197 if (event->attr.task)
6198 atomic_inc(&nr_task_events);
6199 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6200 err = get_callchain_buffers();
6203 return ERR_PTR(err);
6211 static int perf_copy_attr(struct perf_event_attr __user *uattr,
6212 struct perf_event_attr *attr)
6217 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6221 * zero the full structure, so that a short copy will be nice.
6223 memset(attr, 0, sizeof(*attr));
6225 ret = get_user(size, &uattr->size);
6229 if (size > PAGE_SIZE) /* silly large */
6232 if (!size) /* abi compat */
6233 size = PERF_ATTR_SIZE_VER0;
6235 if (size < PERF_ATTR_SIZE_VER0)
6239 * If we're handed a bigger struct than we know of,
6240 * ensure all the unknown bits are 0 - i.e. new
6241 * user-space does not rely on any kernel feature
6242 * extensions we dont know about yet.
6244 if (size > sizeof(*attr)) {
6245 unsigned char __user *addr;
6246 unsigned char __user *end;
6249 addr = (void __user *)uattr + sizeof(*attr);
6250 end = (void __user *)uattr + size;
6252 for (; addr < end; addr++) {
6253 ret = get_user(val, addr);
6259 size = sizeof(*attr);
6262 ret = copy_from_user(attr, uattr, size);
6267 * If the type exists, the corresponding creation will verify
6270 if (attr->type >= PERF_TYPE_MAX)
6273 if (attr->__reserved_1)
6276 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6279 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6286 put_user(sizeof(*attr), &uattr->size);
6292 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6294 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
6300 /* don't allow circular references */
6301 if (event == output_event)
6305 * Don't allow cross-cpu buffers
6307 if (output_event->cpu != event->cpu)
6311 * If its not a per-cpu buffer, it must be the same task.
6313 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6317 mutex_lock(&event->mmap_mutex);
6318 /* Can't redirect output if we've got an active mmap() */
6319 if (atomic_read(&event->mmap_count))
6323 /* get the buffer we want to redirect to */
6324 buffer = perf_buffer_get(output_event);
6329 old_buffer = event->buffer;
6330 rcu_assign_pointer(event->buffer, buffer);
6333 mutex_unlock(&event->mmap_mutex);
6336 perf_buffer_put(old_buffer);
6342 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6344 * @attr_uptr: event_id type attributes for monitoring/sampling
6347 * @group_fd: group leader event fd
6349 SYSCALL_DEFINE5(perf_event_open,
6350 struct perf_event_attr __user *, attr_uptr,
6351 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6353 struct perf_event *group_leader = NULL, *output_event = NULL;
6354 struct perf_event *event, *sibling;
6355 struct perf_event_attr attr;
6356 struct perf_event_context *ctx;
6357 struct file *event_file = NULL;
6358 struct file *group_file = NULL;
6359 struct task_struct *task = NULL;
6363 int fput_needed = 0;
6366 /* for future expandability... */
6367 if (flags & ~PERF_FLAG_ALL)
6370 err = perf_copy_attr(attr_uptr, &attr);
6374 if (!attr.exclude_kernel) {
6375 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6380 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6385 * In cgroup mode, the pid argument is used to pass the fd
6386 * opened to the cgroup directory in cgroupfs. The cpu argument
6387 * designates the cpu on which to monitor threads from that
6390 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6393 event_fd = get_unused_fd_flags(O_RDWR);
6397 if (group_fd != -1) {
6398 group_leader = perf_fget_light(group_fd, &fput_needed);
6399 if (IS_ERR(group_leader)) {
6400 err = PTR_ERR(group_leader);
6403 group_file = group_leader->filp;
6404 if (flags & PERF_FLAG_FD_OUTPUT)
6405 output_event = group_leader;
6406 if (flags & PERF_FLAG_FD_NO_GROUP)
6407 group_leader = NULL;
6410 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
6411 task = find_lively_task_by_vpid(pid);
6413 err = PTR_ERR(task);
6418 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
6419 if (IS_ERR(event)) {
6420 err = PTR_ERR(event);
6424 if (flags & PERF_FLAG_PID_CGROUP) {
6425 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6431 * Special case software events and allow them to be part of
6432 * any hardware group.
6437 (is_software_event(event) != is_software_event(group_leader))) {
6438 if (is_software_event(event)) {
6440 * If event and group_leader are not both a software
6441 * event, and event is, then group leader is not.
6443 * Allow the addition of software events to !software
6444 * groups, this is safe because software events never
6447 pmu = group_leader->pmu;
6448 } else if (is_software_event(group_leader) &&
6449 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6451 * In case the group is a pure software group, and we
6452 * try to add a hardware event, move the whole group to
6453 * the hardware context.
6460 * Get the target context (task or percpu):
6462 ctx = find_get_context(pmu, task, cpu);
6469 * Look up the group leader (we will attach this event to it):
6475 * Do not allow a recursive hierarchy (this new sibling
6476 * becoming part of another group-sibling):
6478 if (group_leader->group_leader != group_leader)
6481 * Do not allow to attach to a group in a different
6482 * task or CPU context:
6485 if (group_leader->ctx->type != ctx->type)
6488 if (group_leader->ctx != ctx)
6493 * Only a group leader can be exclusive or pinned
6495 if (attr.exclusive || attr.pinned)
6500 err = perf_event_set_output(event, output_event);
6505 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6506 if (IS_ERR(event_file)) {
6507 err = PTR_ERR(event_file);
6512 struct perf_event_context *gctx = group_leader->ctx;
6514 mutex_lock(&gctx->mutex);
6515 perf_remove_from_context(group_leader);
6516 list_for_each_entry(sibling, &group_leader->sibling_list,
6518 perf_remove_from_context(sibling);
6521 mutex_unlock(&gctx->mutex);
6525 event->filp = event_file;
6526 WARN_ON_ONCE(ctx->parent_ctx);
6527 mutex_lock(&ctx->mutex);
6530 perf_install_in_context(ctx, group_leader, cpu);
6532 list_for_each_entry(sibling, &group_leader->sibling_list,
6534 perf_install_in_context(ctx, sibling, cpu);
6539 perf_install_in_context(ctx, event, cpu);
6541 perf_unpin_context(ctx);
6542 mutex_unlock(&ctx->mutex);
6544 event->owner = current;
6546 mutex_lock(¤t->perf_event_mutex);
6547 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6548 mutex_unlock(¤t->perf_event_mutex);
6551 * Precalculate sample_data sizes
6553 perf_event__header_size(event);
6554 perf_event__id_header_size(event);
6557 * Drop the reference on the group_event after placing the
6558 * new event on the sibling_list. This ensures destruction
6559 * of the group leader will find the pointer to itself in
6560 * perf_group_detach().
6562 fput_light(group_file, fput_needed);
6563 fd_install(event_fd, event_file);
6567 perf_unpin_context(ctx);
6573 put_task_struct(task);
6575 fput_light(group_file, fput_needed);
6577 put_unused_fd(event_fd);
6582 * perf_event_create_kernel_counter
6584 * @attr: attributes of the counter to create
6585 * @cpu: cpu in which the counter is bound
6586 * @task: task to profile (NULL for percpu)
6589 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6590 struct task_struct *task,
6591 perf_overflow_handler_t overflow_handler)
6593 struct perf_event_context *ctx;
6594 struct perf_event *event;
6598 * Get the target context (task or percpu):
6601 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
6602 if (IS_ERR(event)) {
6603 err = PTR_ERR(event);
6607 ctx = find_get_context(event->pmu, task, cpu);
6614 WARN_ON_ONCE(ctx->parent_ctx);
6615 mutex_lock(&ctx->mutex);
6616 perf_install_in_context(ctx, event, cpu);
6618 perf_unpin_context(ctx);
6619 mutex_unlock(&ctx->mutex);
6626 return ERR_PTR(err);
6628 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6630 static void sync_child_event(struct perf_event *child_event,
6631 struct task_struct *child)
6633 struct perf_event *parent_event = child_event->parent;
6636 if (child_event->attr.inherit_stat)
6637 perf_event_read_event(child_event, child);
6639 child_val = perf_event_count(child_event);
6642 * Add back the child's count to the parent's count:
6644 atomic64_add(child_val, &parent_event->child_count);
6645 atomic64_add(child_event->total_time_enabled,
6646 &parent_event->child_total_time_enabled);
6647 atomic64_add(child_event->total_time_running,
6648 &parent_event->child_total_time_running);
6651 * Remove this event from the parent's list
6653 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6654 mutex_lock(&parent_event->child_mutex);
6655 list_del_init(&child_event->child_list);
6656 mutex_unlock(&parent_event->child_mutex);
6659 * Release the parent event, if this was the last
6662 fput(parent_event->filp);
6666 __perf_event_exit_task(struct perf_event *child_event,
6667 struct perf_event_context *child_ctx,
6668 struct task_struct *child)
6670 struct perf_event *parent_event;
6672 perf_remove_from_context(child_event);
6674 parent_event = child_event->parent;
6676 * It can happen that parent exits first, and has events
6677 * that are still around due to the child reference. These
6678 * events need to be zapped - but otherwise linger.
6681 sync_child_event(child_event, child);
6682 free_event(child_event);
6686 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6688 struct perf_event *child_event, *tmp;
6689 struct perf_event_context *child_ctx;
6690 unsigned long flags;
6692 if (likely(!child->perf_event_ctxp[ctxn])) {
6693 perf_event_task(child, NULL, 0);
6697 local_irq_save(flags);
6699 * We can't reschedule here because interrupts are disabled,
6700 * and either child is current or it is a task that can't be
6701 * scheduled, so we are now safe from rescheduling changing
6704 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6705 task_ctx_sched_out(child_ctx, EVENT_ALL);
6708 * Take the context lock here so that if find_get_context is
6709 * reading child->perf_event_ctxp, we wait until it has
6710 * incremented the context's refcount before we do put_ctx below.
6712 raw_spin_lock(&child_ctx->lock);
6713 child->perf_event_ctxp[ctxn] = NULL;
6715 * If this context is a clone; unclone it so it can't get
6716 * swapped to another process while we're removing all
6717 * the events from it.
6719 unclone_ctx(child_ctx);
6720 update_context_time(child_ctx);
6721 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6724 * Report the task dead after unscheduling the events so that we
6725 * won't get any samples after PERF_RECORD_EXIT. We can however still
6726 * get a few PERF_RECORD_READ events.
6728 perf_event_task(child, child_ctx, 0);
6731 * We can recurse on the same lock type through:
6733 * __perf_event_exit_task()
6734 * sync_child_event()
6735 * fput(parent_event->filp)
6737 * mutex_lock(&ctx->mutex)
6739 * But since its the parent context it won't be the same instance.
6741 mutex_lock(&child_ctx->mutex);
6744 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6746 __perf_event_exit_task(child_event, child_ctx, child);
6748 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6750 __perf_event_exit_task(child_event, child_ctx, child);
6753 * If the last event was a group event, it will have appended all
6754 * its siblings to the list, but we obtained 'tmp' before that which
6755 * will still point to the list head terminating the iteration.
6757 if (!list_empty(&child_ctx->pinned_groups) ||
6758 !list_empty(&child_ctx->flexible_groups))
6761 mutex_unlock(&child_ctx->mutex);
6767 * When a child task exits, feed back event values to parent events.
6769 void perf_event_exit_task(struct task_struct *child)
6771 struct perf_event *event, *tmp;
6774 mutex_lock(&child->perf_event_mutex);
6775 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6777 list_del_init(&event->owner_entry);
6780 * Ensure the list deletion is visible before we clear
6781 * the owner, closes a race against perf_release() where
6782 * we need to serialize on the owner->perf_event_mutex.
6785 event->owner = NULL;
6787 mutex_unlock(&child->perf_event_mutex);
6789 for_each_task_context_nr(ctxn)
6790 perf_event_exit_task_context(child, ctxn);
6793 static void perf_free_event(struct perf_event *event,
6794 struct perf_event_context *ctx)
6796 struct perf_event *parent = event->parent;
6798 if (WARN_ON_ONCE(!parent))
6801 mutex_lock(&parent->child_mutex);
6802 list_del_init(&event->child_list);
6803 mutex_unlock(&parent->child_mutex);
6807 perf_group_detach(event);
6808 list_del_event(event, ctx);
6813 * free an unexposed, unused context as created by inheritance by
6814 * perf_event_init_task below, used by fork() in case of fail.
6816 void perf_event_free_task(struct task_struct *task)
6818 struct perf_event_context *ctx;
6819 struct perf_event *event, *tmp;
6822 for_each_task_context_nr(ctxn) {
6823 ctx = task->perf_event_ctxp[ctxn];
6827 mutex_lock(&ctx->mutex);
6829 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6831 perf_free_event(event, ctx);
6833 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6835 perf_free_event(event, ctx);
6837 if (!list_empty(&ctx->pinned_groups) ||
6838 !list_empty(&ctx->flexible_groups))
6841 mutex_unlock(&ctx->mutex);
6847 void perf_event_delayed_put(struct task_struct *task)
6851 for_each_task_context_nr(ctxn)
6852 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6856 * inherit a event from parent task to child task:
6858 static struct perf_event *
6859 inherit_event(struct perf_event *parent_event,
6860 struct task_struct *parent,
6861 struct perf_event_context *parent_ctx,
6862 struct task_struct *child,
6863 struct perf_event *group_leader,
6864 struct perf_event_context *child_ctx)
6866 struct perf_event *child_event;
6867 unsigned long flags;
6870 * Instead of creating recursive hierarchies of events,
6871 * we link inherited events back to the original parent,
6872 * which has a filp for sure, which we use as the reference
6875 if (parent_event->parent)
6876 parent_event = parent_event->parent;
6878 child_event = perf_event_alloc(&parent_event->attr,
6881 group_leader, parent_event,
6883 if (IS_ERR(child_event))
6888 * Make the child state follow the state of the parent event,
6889 * not its attr.disabled bit. We hold the parent's mutex,
6890 * so we won't race with perf_event_{en, dis}able_family.
6892 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6893 child_event->state = PERF_EVENT_STATE_INACTIVE;
6895 child_event->state = PERF_EVENT_STATE_OFF;
6897 if (parent_event->attr.freq) {
6898 u64 sample_period = parent_event->hw.sample_period;
6899 struct hw_perf_event *hwc = &child_event->hw;
6901 hwc->sample_period = sample_period;
6902 hwc->last_period = sample_period;
6904 local64_set(&hwc->period_left, sample_period);
6907 child_event->ctx = child_ctx;
6908 child_event->overflow_handler = parent_event->overflow_handler;
6911 * Precalculate sample_data sizes
6913 perf_event__header_size(child_event);
6914 perf_event__id_header_size(child_event);
6917 * Link it up in the child's context:
6919 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6920 add_event_to_ctx(child_event, child_ctx);
6921 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6924 * Get a reference to the parent filp - we will fput it
6925 * when the child event exits. This is safe to do because
6926 * we are in the parent and we know that the filp still
6927 * exists and has a nonzero count:
6929 atomic_long_inc(&parent_event->filp->f_count);
6932 * Link this into the parent event's child list
6934 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6935 mutex_lock(&parent_event->child_mutex);
6936 list_add_tail(&child_event->child_list, &parent_event->child_list);
6937 mutex_unlock(&parent_event->child_mutex);
6942 static int inherit_group(struct perf_event *parent_event,
6943 struct task_struct *parent,
6944 struct perf_event_context *parent_ctx,
6945 struct task_struct *child,
6946 struct perf_event_context *child_ctx)
6948 struct perf_event *leader;
6949 struct perf_event *sub;
6950 struct perf_event *child_ctr;
6952 leader = inherit_event(parent_event, parent, parent_ctx,
6953 child, NULL, child_ctx);
6955 return PTR_ERR(leader);
6956 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6957 child_ctr = inherit_event(sub, parent, parent_ctx,
6958 child, leader, child_ctx);
6959 if (IS_ERR(child_ctr))
6960 return PTR_ERR(child_ctr);
6966 inherit_task_group(struct perf_event *event, struct task_struct *parent,
6967 struct perf_event_context *parent_ctx,
6968 struct task_struct *child, int ctxn,
6972 struct perf_event_context *child_ctx;
6974 if (!event->attr.inherit) {
6979 child_ctx = child->perf_event_ctxp[ctxn];
6982 * This is executed from the parent task context, so
6983 * inherit events that have been marked for cloning.
6984 * First allocate and initialize a context for the
6988 child_ctx = alloc_perf_context(event->pmu, child);
6992 child->perf_event_ctxp[ctxn] = child_ctx;
6995 ret = inherit_group(event, parent, parent_ctx,
7005 * Initialize the perf_event context in task_struct
7007 int perf_event_init_context(struct task_struct *child, int ctxn)
7009 struct perf_event_context *child_ctx, *parent_ctx;
7010 struct perf_event_context *cloned_ctx;
7011 struct perf_event *event;
7012 struct task_struct *parent = current;
7013 int inherited_all = 1;
7014 unsigned long flags;
7017 if (likely(!parent->perf_event_ctxp[ctxn]))
7021 * If the parent's context is a clone, pin it so it won't get
7024 parent_ctx = perf_pin_task_context(parent, ctxn);
7027 * No need to check if parent_ctx != NULL here; since we saw
7028 * it non-NULL earlier, the only reason for it to become NULL
7029 * is if we exit, and since we're currently in the middle of
7030 * a fork we can't be exiting at the same time.
7034 * Lock the parent list. No need to lock the child - not PID
7035 * hashed yet and not running, so nobody can access it.
7037 mutex_lock(&parent_ctx->mutex);
7040 * We dont have to disable NMIs - we are only looking at
7041 * the list, not manipulating it:
7043 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
7044 ret = inherit_task_group(event, parent, parent_ctx,
7045 child, ctxn, &inherited_all);
7051 * We can't hold ctx->lock when iterating the ->flexible_group list due
7052 * to allocations, but we need to prevent rotation because
7053 * rotate_ctx() will change the list from interrupt context.
7055 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7056 parent_ctx->rotate_disable = 1;
7057 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7059 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
7060 ret = inherit_task_group(event, parent, parent_ctx,
7061 child, ctxn, &inherited_all);
7066 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7067 parent_ctx->rotate_disable = 0;
7069 child_ctx = child->perf_event_ctxp[ctxn];
7071 if (child_ctx && inherited_all) {
7073 * Mark the child context as a clone of the parent
7074 * context, or of whatever the parent is a clone of.
7076 * Note that if the parent is a clone, the holding of
7077 * parent_ctx->lock avoids it from being uncloned.
7079 cloned_ctx = parent_ctx->parent_ctx;
7081 child_ctx->parent_ctx = cloned_ctx;
7082 child_ctx->parent_gen = parent_ctx->parent_gen;
7084 child_ctx->parent_ctx = parent_ctx;
7085 child_ctx->parent_gen = parent_ctx->generation;
7087 get_ctx(child_ctx->parent_ctx);
7090 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7091 mutex_unlock(&parent_ctx->mutex);
7093 perf_unpin_context(parent_ctx);
7094 put_ctx(parent_ctx);
7100 * Initialize the perf_event context in task_struct
7102 int perf_event_init_task(struct task_struct *child)
7106 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7107 mutex_init(&child->perf_event_mutex);
7108 INIT_LIST_HEAD(&child->perf_event_list);
7110 for_each_task_context_nr(ctxn) {
7111 ret = perf_event_init_context(child, ctxn);
7119 static void __init perf_event_init_all_cpus(void)
7121 struct swevent_htable *swhash;
7124 for_each_possible_cpu(cpu) {
7125 swhash = &per_cpu(swevent_htable, cpu);
7126 mutex_init(&swhash->hlist_mutex);
7127 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
7131 static void __cpuinit perf_event_init_cpu(int cpu)
7133 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7135 mutex_lock(&swhash->hlist_mutex);
7136 if (swhash->hlist_refcount > 0) {
7137 struct swevent_hlist *hlist;
7139 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7141 rcu_assign_pointer(swhash->swevent_hlist, hlist);
7143 mutex_unlock(&swhash->hlist_mutex);
7146 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7147 static void perf_pmu_rotate_stop(struct pmu *pmu)
7149 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7151 WARN_ON(!irqs_disabled());
7153 list_del_init(&cpuctx->rotation_list);
7156 static void __perf_event_exit_context(void *__info)
7158 struct perf_event_context *ctx = __info;
7159 struct perf_event *event, *tmp;
7161 perf_pmu_rotate_stop(ctx->pmu);
7163 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
7164 __perf_remove_from_context(event);
7165 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
7166 __perf_remove_from_context(event);
7169 static void perf_event_exit_cpu_context(int cpu)
7171 struct perf_event_context *ctx;
7175 idx = srcu_read_lock(&pmus_srcu);
7176 list_for_each_entry_rcu(pmu, &pmus, entry) {
7177 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
7179 mutex_lock(&ctx->mutex);
7180 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7181 mutex_unlock(&ctx->mutex);
7183 srcu_read_unlock(&pmus_srcu, idx);
7186 static void perf_event_exit_cpu(int cpu)
7188 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7190 mutex_lock(&swhash->hlist_mutex);
7191 swevent_hlist_release(swhash);
7192 mutex_unlock(&swhash->hlist_mutex);
7194 perf_event_exit_cpu_context(cpu);
7197 static inline void perf_event_exit_cpu(int cpu) { }
7201 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7205 for_each_online_cpu(cpu)
7206 perf_event_exit_cpu(cpu);
7212 * Run the perf reboot notifier at the very last possible moment so that
7213 * the generic watchdog code runs as long as possible.
7215 static struct notifier_block perf_reboot_notifier = {
7216 .notifier_call = perf_reboot,
7217 .priority = INT_MIN,
7220 static int __cpuinit
7221 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7223 unsigned int cpu = (long)hcpu;
7225 switch (action & ~CPU_TASKS_FROZEN) {
7227 case CPU_UP_PREPARE:
7228 case CPU_DOWN_FAILED:
7229 perf_event_init_cpu(cpu);
7232 case CPU_UP_CANCELED:
7233 case CPU_DOWN_PREPARE:
7234 perf_event_exit_cpu(cpu);
7244 void __init perf_event_init(void)
7250 perf_event_init_all_cpus();
7251 init_srcu_struct(&pmus_srcu);
7252 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7253 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7254 perf_pmu_register(&perf_task_clock, NULL, -1);
7256 perf_cpu_notifier(perf_cpu_notify);
7257 register_reboot_notifier(&perf_reboot_notifier);
7259 ret = init_hw_breakpoint();
7260 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7263 static int __init perf_event_sysfs_init(void)
7268 mutex_lock(&pmus_lock);
7270 ret = bus_register(&pmu_bus);
7274 list_for_each_entry(pmu, &pmus, entry) {
7275 if (!pmu->name || pmu->type < 0)
7278 ret = pmu_dev_alloc(pmu);
7279 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7281 pmu_bus_running = 1;
7285 mutex_unlock(&pmus_lock);
7289 device_initcall(perf_event_sysfs_init);
7291 #ifdef CONFIG_CGROUP_PERF
7292 static struct cgroup_subsys_state *perf_cgroup_create(
7293 struct cgroup_subsys *ss, struct cgroup *cont)
7295 struct perf_cgroup *jc;
7296 struct perf_cgroup_info *t;
7299 jc = kmalloc(sizeof(*jc), GFP_KERNEL);
7301 return ERR_PTR(-ENOMEM);
7303 memset(jc, 0, sizeof(*jc));
7305 jc->info = alloc_percpu(struct perf_cgroup_info);
7308 return ERR_PTR(-ENOMEM);
7311 for_each_possible_cpu(c) {
7312 t = per_cpu_ptr(jc->info, c);
7319 static void perf_cgroup_destroy(struct cgroup_subsys *ss,
7320 struct cgroup *cont)
7322 struct perf_cgroup *jc;
7323 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7324 struct perf_cgroup, css);
7325 free_percpu(jc->info);
7329 static int __perf_cgroup_move(void *info)
7331 struct task_struct *task = info;
7332 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7336 static void perf_cgroup_move(struct task_struct *task)
7338 task_function_call(task, __perf_cgroup_move, task);
7341 static void perf_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
7342 struct cgroup *old_cgrp, struct task_struct *task,
7345 perf_cgroup_move(task);
7347 struct task_struct *c;
7349 list_for_each_entry_rcu(c, &task->thread_group, thread_group) {
7350 perf_cgroup_move(c);
7356 static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7357 struct cgroup *old_cgrp, struct task_struct *task)
7360 * cgroup_exit() is called in the copy_process() failure path.
7361 * Ignore this case since the task hasn't ran yet, this avoids
7362 * trying to poke a half freed task state from generic code.
7364 if (!(task->flags & PF_EXITING))
7367 perf_cgroup_move(task);
7370 struct cgroup_subsys perf_subsys = {
7371 .name = "perf_event",
7372 .subsys_id = perf_subsys_id,
7373 .create = perf_cgroup_create,
7374 .destroy = perf_cgroup_destroy,
7375 .exit = perf_cgroup_exit,
7376 .attach = perf_cgroup_attach,
7378 #endif /* CONFIG_CGROUP_PERF */