2 * Performance events core code:
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011 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>
41 #include <asm/irq_regs.h>
43 struct remote_function_call {
44 struct task_struct *p;
45 int (*func)(void *info);
50 static void remote_function(void *data)
52 struct remote_function_call *tfc = data;
53 struct task_struct *p = tfc->p;
57 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
61 tfc->ret = tfc->func(tfc->info);
65 * task_function_call - call a function on the cpu on which a task runs
66 * @p: the task to evaluate
67 * @func: the function to be called
68 * @info: the function call argument
70 * Calls the function @func when the task is currently running. This might
71 * be on the current CPU, which just calls the function directly
73 * returns: @func return value, or
74 * -ESRCH - when the process isn't running
75 * -EAGAIN - when the process moved away
78 task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
80 struct remote_function_call data = {
84 .ret = -ESRCH, /* No such (running) process */
88 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
94 * cpu_function_call - call a function on the cpu
95 * @func: the function to be called
96 * @info: the function call argument
98 * Calls the function @func on the remote cpu.
100 * returns: @func return value or -ENXIO when the cpu is offline
102 static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
104 struct remote_function_call data = {
108 .ret = -ENXIO, /* No such CPU */
111 smp_call_function_single(cpu, remote_function, &data, 1);
116 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
117 PERF_FLAG_FD_OUTPUT |\
118 PERF_FLAG_PID_CGROUP)
121 EVENT_FLEXIBLE = 0x1,
123 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
127 * perf_sched_events : >0 events exist
128 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
130 struct jump_label_key perf_sched_events __read_mostly;
131 static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
133 static atomic_t nr_mmap_events __read_mostly;
134 static atomic_t nr_comm_events __read_mostly;
135 static atomic_t nr_task_events __read_mostly;
137 static LIST_HEAD(pmus);
138 static DEFINE_MUTEX(pmus_lock);
139 static struct srcu_struct pmus_srcu;
142 * perf event paranoia level:
143 * -1 - not paranoid at all
144 * 0 - disallow raw tracepoint access for unpriv
145 * 1 - disallow cpu events for unpriv
146 * 2 - disallow kernel profiling for unpriv
148 int sysctl_perf_event_paranoid __read_mostly = 1;
150 /* Minimum for 512 kiB + 1 user control page */
151 int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
154 * max perf event sample rate
156 #define DEFAULT_MAX_SAMPLE_RATE 100000
157 int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
158 static int max_samples_per_tick __read_mostly =
159 DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
161 int perf_proc_update_handler(struct ctl_table *table, int write,
162 void __user *buffer, size_t *lenp,
165 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
170 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
175 static atomic64_t perf_event_id;
177 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
178 enum event_type_t event_type);
180 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
181 enum event_type_t event_type,
182 struct task_struct *task);
184 static void update_context_time(struct perf_event_context *ctx);
185 static u64 perf_event_time(struct perf_event *event);
187 void __weak perf_event_print_debug(void) { }
189 extern __weak const char *perf_pmu_name(void)
194 static inline u64 perf_clock(void)
196 return local_clock();
199 static inline struct perf_cpu_context *
200 __get_cpu_context(struct perf_event_context *ctx)
202 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
205 static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
206 struct perf_event_context *ctx)
208 raw_spin_lock(&cpuctx->ctx.lock);
210 raw_spin_lock(&ctx->lock);
213 static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
214 struct perf_event_context *ctx)
217 raw_spin_unlock(&ctx->lock);
218 raw_spin_unlock(&cpuctx->ctx.lock);
221 #ifdef CONFIG_CGROUP_PERF
224 * Must ensure cgroup is pinned (css_get) before calling
225 * this function. In other words, we cannot call this function
226 * if there is no cgroup event for the current CPU context.
228 static inline struct perf_cgroup *
229 perf_cgroup_from_task(struct task_struct *task)
231 return container_of(task_subsys_state(task, perf_subsys_id),
232 struct perf_cgroup, css);
236 perf_cgroup_match(struct perf_event *event)
238 struct perf_event_context *ctx = event->ctx;
239 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
241 return !event->cgrp || event->cgrp == cpuctx->cgrp;
244 static inline void perf_get_cgroup(struct perf_event *event)
246 css_get(&event->cgrp->css);
249 static inline void perf_put_cgroup(struct perf_event *event)
251 css_put(&event->cgrp->css);
254 static inline void perf_detach_cgroup(struct perf_event *event)
256 perf_put_cgroup(event);
260 static inline int is_cgroup_event(struct perf_event *event)
262 return event->cgrp != NULL;
265 static inline u64 perf_cgroup_event_time(struct perf_event *event)
267 struct perf_cgroup_info *t;
269 t = per_cpu_ptr(event->cgrp->info, event->cpu);
273 static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
275 struct perf_cgroup_info *info;
280 info = this_cpu_ptr(cgrp->info);
282 info->time += now - info->timestamp;
283 info->timestamp = now;
286 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
288 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
290 __update_cgrp_time(cgrp_out);
293 static inline void update_cgrp_time_from_event(struct perf_event *event)
295 struct perf_cgroup *cgrp;
298 * ensure we access cgroup data only when needed and
299 * when we know the cgroup is pinned (css_get)
301 if (!is_cgroup_event(event))
304 cgrp = perf_cgroup_from_task(current);
306 * Do not update time when cgroup is not active
308 if (cgrp == event->cgrp)
309 __update_cgrp_time(event->cgrp);
313 perf_cgroup_set_timestamp(struct task_struct *task,
314 struct perf_event_context *ctx)
316 struct perf_cgroup *cgrp;
317 struct perf_cgroup_info *info;
320 * ctx->lock held by caller
321 * ensure we do not access cgroup data
322 * unless we have the cgroup pinned (css_get)
324 if (!task || !ctx->nr_cgroups)
327 cgrp = perf_cgroup_from_task(task);
328 info = this_cpu_ptr(cgrp->info);
329 info->timestamp = ctx->timestamp;
332 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
333 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
336 * reschedule events based on the cgroup constraint of task.
338 * mode SWOUT : schedule out everything
339 * mode SWIN : schedule in based on cgroup for next
341 void perf_cgroup_switch(struct task_struct *task, int mode)
343 struct perf_cpu_context *cpuctx;
348 * disable interrupts to avoid geting nr_cgroup
349 * changes via __perf_event_disable(). Also
352 local_irq_save(flags);
355 * we reschedule only in the presence of cgroup
356 * constrained events.
360 list_for_each_entry_rcu(pmu, &pmus, entry) {
361 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
364 * perf_cgroup_events says at least one
365 * context on this CPU has cgroup events.
367 * ctx->nr_cgroups reports the number of cgroup
368 * events for a context.
370 if (cpuctx->ctx.nr_cgroups > 0) {
371 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
372 perf_pmu_disable(cpuctx->ctx.pmu);
374 if (mode & PERF_CGROUP_SWOUT) {
375 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
377 * must not be done before ctxswout due
378 * to event_filter_match() in event_sched_out()
383 if (mode & PERF_CGROUP_SWIN) {
384 WARN_ON_ONCE(cpuctx->cgrp);
385 /* set cgrp before ctxsw in to
386 * allow event_filter_match() to not
387 * have to pass task around
389 cpuctx->cgrp = perf_cgroup_from_task(task);
390 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
392 perf_pmu_enable(cpuctx->ctx.pmu);
393 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
399 local_irq_restore(flags);
402 static inline void perf_cgroup_sched_out(struct task_struct *task)
404 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
407 static inline void perf_cgroup_sched_in(struct task_struct *task)
409 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
412 static inline int perf_cgroup_connect(int fd, struct perf_event *event,
413 struct perf_event_attr *attr,
414 struct perf_event *group_leader)
416 struct perf_cgroup *cgrp;
417 struct cgroup_subsys_state *css;
419 int ret = 0, fput_needed;
421 file = fget_light(fd, &fput_needed);
425 css = cgroup_css_from_dir(file, perf_subsys_id);
431 cgrp = container_of(css, struct perf_cgroup, css);
434 /* must be done before we fput() the file */
435 perf_get_cgroup(event);
438 * all events in a group must monitor
439 * the same cgroup because a task belongs
440 * to only one perf cgroup at a time
442 if (group_leader && group_leader->cgrp != cgrp) {
443 perf_detach_cgroup(event);
447 fput_light(file, fput_needed);
452 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
454 struct perf_cgroup_info *t;
455 t = per_cpu_ptr(event->cgrp->info, event->cpu);
456 event->shadow_ctx_time = now - t->timestamp;
460 perf_cgroup_defer_enabled(struct perf_event *event)
463 * when the current task's perf cgroup does not match
464 * the event's, we need to remember to call the
465 * perf_mark_enable() function the first time a task with
466 * a matching perf cgroup is scheduled in.
468 if (is_cgroup_event(event) && !perf_cgroup_match(event))
469 event->cgrp_defer_enabled = 1;
473 perf_cgroup_mark_enabled(struct perf_event *event,
474 struct perf_event_context *ctx)
476 struct perf_event *sub;
477 u64 tstamp = perf_event_time(event);
479 if (!event->cgrp_defer_enabled)
482 event->cgrp_defer_enabled = 0;
484 event->tstamp_enabled = tstamp - event->total_time_enabled;
485 list_for_each_entry(sub, &event->sibling_list, group_entry) {
486 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
487 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
488 sub->cgrp_defer_enabled = 0;
492 #else /* !CONFIG_CGROUP_PERF */
495 perf_cgroup_match(struct perf_event *event)
500 static inline void perf_detach_cgroup(struct perf_event *event)
503 static inline int is_cgroup_event(struct perf_event *event)
508 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
513 static inline void update_cgrp_time_from_event(struct perf_event *event)
517 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
521 static inline void perf_cgroup_sched_out(struct task_struct *task)
525 static inline void perf_cgroup_sched_in(struct task_struct *task)
529 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
530 struct perf_event_attr *attr,
531 struct perf_event *group_leader)
537 perf_cgroup_set_timestamp(struct task_struct *task,
538 struct perf_event_context *ctx)
543 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
548 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
552 static inline u64 perf_cgroup_event_time(struct perf_event *event)
558 perf_cgroup_defer_enabled(struct perf_event *event)
563 perf_cgroup_mark_enabled(struct perf_event *event,
564 struct perf_event_context *ctx)
569 void perf_pmu_disable(struct pmu *pmu)
571 int *count = this_cpu_ptr(pmu->pmu_disable_count);
573 pmu->pmu_disable(pmu);
576 void perf_pmu_enable(struct pmu *pmu)
578 int *count = this_cpu_ptr(pmu->pmu_disable_count);
580 pmu->pmu_enable(pmu);
583 static DEFINE_PER_CPU(struct list_head, rotation_list);
586 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
587 * because they're strictly cpu affine and rotate_start is called with IRQs
588 * disabled, while rotate_context is called from IRQ context.
590 static void perf_pmu_rotate_start(struct pmu *pmu)
592 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
593 struct list_head *head = &__get_cpu_var(rotation_list);
595 WARN_ON(!irqs_disabled());
597 if (list_empty(&cpuctx->rotation_list))
598 list_add(&cpuctx->rotation_list, head);
601 static void get_ctx(struct perf_event_context *ctx)
603 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
606 static void put_ctx(struct perf_event_context *ctx)
608 if (atomic_dec_and_test(&ctx->refcount)) {
610 put_ctx(ctx->parent_ctx);
612 put_task_struct(ctx->task);
613 kfree_rcu(ctx, rcu_head);
617 static void unclone_ctx(struct perf_event_context *ctx)
619 if (ctx->parent_ctx) {
620 put_ctx(ctx->parent_ctx);
621 ctx->parent_ctx = NULL;
625 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
628 * only top level events have the pid namespace they were created in
631 event = event->parent;
633 return task_tgid_nr_ns(p, event->ns);
636 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
639 * only top level events have the pid namespace they were created in
642 event = event->parent;
644 return task_pid_nr_ns(p, event->ns);
648 * If we inherit events we want to return the parent event id
651 static u64 primary_event_id(struct perf_event *event)
656 id = event->parent->id;
662 * Get the perf_event_context for a task and lock it.
663 * This has to cope with with the fact that until it is locked,
664 * the context could get moved to another task.
666 static struct perf_event_context *
667 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
669 struct perf_event_context *ctx;
673 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
676 * If this context is a clone of another, it might
677 * get swapped for another underneath us by
678 * perf_event_task_sched_out, though the
679 * rcu_read_lock() protects us from any context
680 * getting freed. Lock the context and check if it
681 * got swapped before we could get the lock, and retry
682 * if so. If we locked the right context, then it
683 * can't get swapped on us any more.
685 raw_spin_lock_irqsave(&ctx->lock, *flags);
686 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
687 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
691 if (!atomic_inc_not_zero(&ctx->refcount)) {
692 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
701 * Get the context for a task and increment its pin_count so it
702 * can't get swapped to another task. This also increments its
703 * reference count so that the context can't get freed.
705 static struct perf_event_context *
706 perf_pin_task_context(struct task_struct *task, int ctxn)
708 struct perf_event_context *ctx;
711 ctx = perf_lock_task_context(task, ctxn, &flags);
714 raw_spin_unlock_irqrestore(&ctx->lock, flags);
719 static void perf_unpin_context(struct perf_event_context *ctx)
723 raw_spin_lock_irqsave(&ctx->lock, flags);
725 raw_spin_unlock_irqrestore(&ctx->lock, flags);
729 * Update the record of the current time in a context.
731 static void update_context_time(struct perf_event_context *ctx)
733 u64 now = perf_clock();
735 ctx->time += now - ctx->timestamp;
736 ctx->timestamp = now;
739 static u64 perf_event_time(struct perf_event *event)
741 struct perf_event_context *ctx = event->ctx;
743 if (is_cgroup_event(event))
744 return perf_cgroup_event_time(event);
746 return ctx ? ctx->time : 0;
750 * Update the total_time_enabled and total_time_running fields for a event.
751 * The caller of this function needs to hold the ctx->lock.
753 static void update_event_times(struct perf_event *event)
755 struct perf_event_context *ctx = event->ctx;
758 if (event->state < PERF_EVENT_STATE_INACTIVE ||
759 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
762 * in cgroup mode, time_enabled represents
763 * the time the event was enabled AND active
764 * tasks were in the monitored cgroup. This is
765 * independent of the activity of the context as
766 * there may be a mix of cgroup and non-cgroup events.
768 * That is why we treat cgroup events differently
771 if (is_cgroup_event(event))
772 run_end = perf_event_time(event);
773 else if (ctx->is_active)
776 run_end = event->tstamp_stopped;
778 event->total_time_enabled = run_end - event->tstamp_enabled;
780 if (event->state == PERF_EVENT_STATE_INACTIVE)
781 run_end = event->tstamp_stopped;
783 run_end = perf_event_time(event);
785 event->total_time_running = run_end - event->tstamp_running;
790 * Update total_time_enabled and total_time_running for all events in a group.
792 static void update_group_times(struct perf_event *leader)
794 struct perf_event *event;
796 update_event_times(leader);
797 list_for_each_entry(event, &leader->sibling_list, group_entry)
798 update_event_times(event);
801 static struct list_head *
802 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
804 if (event->attr.pinned)
805 return &ctx->pinned_groups;
807 return &ctx->flexible_groups;
811 * Add a event from the lists for its context.
812 * Must be called with ctx->mutex and ctx->lock held.
815 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
817 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
818 event->attach_state |= PERF_ATTACH_CONTEXT;
821 * If we're a stand alone event or group leader, we go to the context
822 * list, group events are kept attached to the group so that
823 * perf_group_detach can, at all times, locate all siblings.
825 if (event->group_leader == event) {
826 struct list_head *list;
828 if (is_software_event(event))
829 event->group_flags |= PERF_GROUP_SOFTWARE;
831 list = ctx_group_list(event, ctx);
832 list_add_tail(&event->group_entry, list);
835 if (is_cgroup_event(event))
838 list_add_rcu(&event->event_entry, &ctx->event_list);
840 perf_pmu_rotate_start(ctx->pmu);
842 if (event->attr.inherit_stat)
847 * Called at perf_event creation and when events are attached/detached from a
850 static void perf_event__read_size(struct perf_event *event)
852 int entry = sizeof(u64); /* value */
856 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
859 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
862 if (event->attr.read_format & PERF_FORMAT_ID)
863 entry += sizeof(u64);
865 if (event->attr.read_format & PERF_FORMAT_GROUP) {
866 nr += event->group_leader->nr_siblings;
871 event->read_size = size;
874 static void perf_event__header_size(struct perf_event *event)
876 struct perf_sample_data *data;
877 u64 sample_type = event->attr.sample_type;
880 perf_event__read_size(event);
882 if (sample_type & PERF_SAMPLE_IP)
883 size += sizeof(data->ip);
885 if (sample_type & PERF_SAMPLE_ADDR)
886 size += sizeof(data->addr);
888 if (sample_type & PERF_SAMPLE_PERIOD)
889 size += sizeof(data->period);
891 if (sample_type & PERF_SAMPLE_READ)
892 size += event->read_size;
894 event->header_size = size;
897 static void perf_event__id_header_size(struct perf_event *event)
899 struct perf_sample_data *data;
900 u64 sample_type = event->attr.sample_type;
903 if (sample_type & PERF_SAMPLE_TID)
904 size += sizeof(data->tid_entry);
906 if (sample_type & PERF_SAMPLE_TIME)
907 size += sizeof(data->time);
909 if (sample_type & PERF_SAMPLE_ID)
910 size += sizeof(data->id);
912 if (sample_type & PERF_SAMPLE_STREAM_ID)
913 size += sizeof(data->stream_id);
915 if (sample_type & PERF_SAMPLE_CPU)
916 size += sizeof(data->cpu_entry);
918 event->id_header_size = size;
921 static void perf_group_attach(struct perf_event *event)
923 struct perf_event *group_leader = event->group_leader, *pos;
926 * We can have double attach due to group movement in perf_event_open.
928 if (event->attach_state & PERF_ATTACH_GROUP)
931 event->attach_state |= PERF_ATTACH_GROUP;
933 if (group_leader == event)
936 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
937 !is_software_event(event))
938 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
940 list_add_tail(&event->group_entry, &group_leader->sibling_list);
941 group_leader->nr_siblings++;
943 perf_event__header_size(group_leader);
945 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
946 perf_event__header_size(pos);
950 * Remove a event from the lists for its context.
951 * Must be called with ctx->mutex and ctx->lock held.
954 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
956 struct perf_cpu_context *cpuctx;
958 * We can have double detach due to exit/hot-unplug + close.
960 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
963 event->attach_state &= ~PERF_ATTACH_CONTEXT;
965 if (is_cgroup_event(event)) {
967 cpuctx = __get_cpu_context(ctx);
969 * if there are no more cgroup events
970 * then cler cgrp to avoid stale pointer
971 * in update_cgrp_time_from_cpuctx()
973 if (!ctx->nr_cgroups)
978 if (event->attr.inherit_stat)
981 list_del_rcu(&event->event_entry);
983 if (event->group_leader == event)
984 list_del_init(&event->group_entry);
986 update_group_times(event);
989 * If event was in error state, then keep it
990 * that way, otherwise bogus counts will be
991 * returned on read(). The only way to get out
992 * of error state is by explicit re-enabling
995 if (event->state > PERF_EVENT_STATE_OFF)
996 event->state = PERF_EVENT_STATE_OFF;
999 static void perf_group_detach(struct perf_event *event)
1001 struct perf_event *sibling, *tmp;
1002 struct list_head *list = NULL;
1005 * We can have double detach due to exit/hot-unplug + close.
1007 if (!(event->attach_state & PERF_ATTACH_GROUP))
1010 event->attach_state &= ~PERF_ATTACH_GROUP;
1013 * If this is a sibling, remove it from its group.
1015 if (event->group_leader != event) {
1016 list_del_init(&event->group_entry);
1017 event->group_leader->nr_siblings--;
1021 if (!list_empty(&event->group_entry))
1022 list = &event->group_entry;
1025 * If this was a group event with sibling events then
1026 * upgrade the siblings to singleton events by adding them
1027 * to whatever list we are on.
1029 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
1031 list_move_tail(&sibling->group_entry, list);
1032 sibling->group_leader = sibling;
1034 /* Inherit group flags from the previous leader */
1035 sibling->group_flags = event->group_flags;
1039 perf_event__header_size(event->group_leader);
1041 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1042 perf_event__header_size(tmp);
1046 event_filter_match(struct perf_event *event)
1048 return (event->cpu == -1 || event->cpu == smp_processor_id())
1049 && perf_cgroup_match(event);
1053 event_sched_out(struct perf_event *event,
1054 struct perf_cpu_context *cpuctx,
1055 struct perf_event_context *ctx)
1057 u64 tstamp = perf_event_time(event);
1060 * An event which could not be activated because of
1061 * filter mismatch still needs to have its timings
1062 * maintained, otherwise bogus information is return
1063 * via read() for time_enabled, time_running:
1065 if (event->state == PERF_EVENT_STATE_INACTIVE
1066 && !event_filter_match(event)) {
1067 delta = tstamp - event->tstamp_stopped;
1068 event->tstamp_running += delta;
1069 event->tstamp_stopped = tstamp;
1072 if (event->state != PERF_EVENT_STATE_ACTIVE)
1075 event->state = PERF_EVENT_STATE_INACTIVE;
1076 if (event->pending_disable) {
1077 event->pending_disable = 0;
1078 event->state = PERF_EVENT_STATE_OFF;
1080 event->tstamp_stopped = tstamp;
1081 event->pmu->del(event, 0);
1084 if (!is_software_event(event))
1085 cpuctx->active_oncpu--;
1087 if (event->attr.exclusive || !cpuctx->active_oncpu)
1088 cpuctx->exclusive = 0;
1092 group_sched_out(struct perf_event *group_event,
1093 struct perf_cpu_context *cpuctx,
1094 struct perf_event_context *ctx)
1096 struct perf_event *event;
1097 int state = group_event->state;
1099 event_sched_out(group_event, cpuctx, ctx);
1102 * Schedule out siblings (if any):
1104 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1105 event_sched_out(event, cpuctx, ctx);
1107 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
1108 cpuctx->exclusive = 0;
1112 * Cross CPU call to remove a performance event
1114 * We disable the event on the hardware level first. After that we
1115 * remove it from the context list.
1117 static int __perf_remove_from_context(void *info)
1119 struct perf_event *event = info;
1120 struct perf_event_context *ctx = event->ctx;
1121 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1123 raw_spin_lock(&ctx->lock);
1124 event_sched_out(event, cpuctx, ctx);
1125 list_del_event(event, ctx);
1126 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1128 cpuctx->task_ctx = NULL;
1130 raw_spin_unlock(&ctx->lock);
1137 * Remove the event from a task's (or a CPU's) list of events.
1139 * CPU events are removed with a smp call. For task events we only
1140 * call when the task is on a CPU.
1142 * If event->ctx is a cloned context, callers must make sure that
1143 * every task struct that event->ctx->task could possibly point to
1144 * remains valid. This is OK when called from perf_release since
1145 * that only calls us on the top-level context, which can't be a clone.
1146 * When called from perf_event_exit_task, it's OK because the
1147 * context has been detached from its task.
1149 static void perf_remove_from_context(struct perf_event *event)
1151 struct perf_event_context *ctx = event->ctx;
1152 struct task_struct *task = ctx->task;
1154 lockdep_assert_held(&ctx->mutex);
1158 * Per cpu events are removed via an smp call and
1159 * the removal is always successful.
1161 cpu_function_call(event->cpu, __perf_remove_from_context, event);
1166 if (!task_function_call(task, __perf_remove_from_context, event))
1169 raw_spin_lock_irq(&ctx->lock);
1171 * If we failed to find a running task, but find the context active now
1172 * that we've acquired the ctx->lock, retry.
1174 if (ctx->is_active) {
1175 raw_spin_unlock_irq(&ctx->lock);
1180 * Since the task isn't running, its safe to remove the event, us
1181 * holding the ctx->lock ensures the task won't get scheduled in.
1183 list_del_event(event, ctx);
1184 raw_spin_unlock_irq(&ctx->lock);
1188 * Cross CPU call to disable a performance event
1190 static int __perf_event_disable(void *info)
1192 struct perf_event *event = info;
1193 struct perf_event_context *ctx = event->ctx;
1194 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1197 * If this is a per-task event, need to check whether this
1198 * event's task is the current task on this cpu.
1200 * Can trigger due to concurrent perf_event_context_sched_out()
1201 * flipping contexts around.
1203 if (ctx->task && cpuctx->task_ctx != ctx)
1206 raw_spin_lock(&ctx->lock);
1209 * If the event is on, turn it off.
1210 * If it is in error state, leave it in error state.
1212 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1213 update_context_time(ctx);
1214 update_cgrp_time_from_event(event);
1215 update_group_times(event);
1216 if (event == event->group_leader)
1217 group_sched_out(event, cpuctx, ctx);
1219 event_sched_out(event, cpuctx, ctx);
1220 event->state = PERF_EVENT_STATE_OFF;
1223 raw_spin_unlock(&ctx->lock);
1231 * If event->ctx is a cloned context, callers must make sure that
1232 * every task struct that event->ctx->task could possibly point to
1233 * remains valid. This condition is satisifed when called through
1234 * perf_event_for_each_child or perf_event_for_each because they
1235 * hold the top-level event's child_mutex, so any descendant that
1236 * goes to exit will block in sync_child_event.
1237 * When called from perf_pending_event it's OK because event->ctx
1238 * is the current context on this CPU and preemption is disabled,
1239 * hence we can't get into perf_event_task_sched_out for this context.
1241 void perf_event_disable(struct perf_event *event)
1243 struct perf_event_context *ctx = event->ctx;
1244 struct task_struct *task = ctx->task;
1248 * Disable the event on the cpu that it's on
1250 cpu_function_call(event->cpu, __perf_event_disable, event);
1255 if (!task_function_call(task, __perf_event_disable, event))
1258 raw_spin_lock_irq(&ctx->lock);
1260 * If the event is still active, we need to retry the cross-call.
1262 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1263 raw_spin_unlock_irq(&ctx->lock);
1265 * Reload the task pointer, it might have been changed by
1266 * a concurrent perf_event_context_sched_out().
1273 * Since we have the lock this context can't be scheduled
1274 * in, so we can change the state safely.
1276 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1277 update_group_times(event);
1278 event->state = PERF_EVENT_STATE_OFF;
1280 raw_spin_unlock_irq(&ctx->lock);
1283 static void perf_set_shadow_time(struct perf_event *event,
1284 struct perf_event_context *ctx,
1288 * use the correct time source for the time snapshot
1290 * We could get by without this by leveraging the
1291 * fact that to get to this function, the caller
1292 * has most likely already called update_context_time()
1293 * and update_cgrp_time_xx() and thus both timestamp
1294 * are identical (or very close). Given that tstamp is,
1295 * already adjusted for cgroup, we could say that:
1296 * tstamp - ctx->timestamp
1298 * tstamp - cgrp->timestamp.
1300 * Then, in perf_output_read(), the calculation would
1301 * work with no changes because:
1302 * - event is guaranteed scheduled in
1303 * - no scheduled out in between
1304 * - thus the timestamp would be the same
1306 * But this is a bit hairy.
1308 * So instead, we have an explicit cgroup call to remain
1309 * within the time time source all along. We believe it
1310 * is cleaner and simpler to understand.
1312 if (is_cgroup_event(event))
1313 perf_cgroup_set_shadow_time(event, tstamp);
1315 event->shadow_ctx_time = tstamp - ctx->timestamp;
1318 #define MAX_INTERRUPTS (~0ULL)
1320 static void perf_log_throttle(struct perf_event *event, int enable);
1323 event_sched_in(struct perf_event *event,
1324 struct perf_cpu_context *cpuctx,
1325 struct perf_event_context *ctx)
1327 u64 tstamp = perf_event_time(event);
1329 if (event->state <= PERF_EVENT_STATE_OFF)
1332 event->state = PERF_EVENT_STATE_ACTIVE;
1333 event->oncpu = smp_processor_id();
1336 * Unthrottle events, since we scheduled we might have missed several
1337 * ticks already, also for a heavily scheduling task there is little
1338 * guarantee it'll get a tick in a timely manner.
1340 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1341 perf_log_throttle(event, 1);
1342 event->hw.interrupts = 0;
1346 * The new state must be visible before we turn it on in the hardware:
1350 if (event->pmu->add(event, PERF_EF_START)) {
1351 event->state = PERF_EVENT_STATE_INACTIVE;
1356 event->tstamp_running += tstamp - event->tstamp_stopped;
1358 perf_set_shadow_time(event, ctx, tstamp);
1360 if (!is_software_event(event))
1361 cpuctx->active_oncpu++;
1364 if (event->attr.exclusive)
1365 cpuctx->exclusive = 1;
1371 group_sched_in(struct perf_event *group_event,
1372 struct perf_cpu_context *cpuctx,
1373 struct perf_event_context *ctx)
1375 struct perf_event *event, *partial_group = NULL;
1376 struct pmu *pmu = group_event->pmu;
1377 u64 now = ctx->time;
1378 bool simulate = false;
1380 if (group_event->state == PERF_EVENT_STATE_OFF)
1383 pmu->start_txn(pmu);
1385 if (event_sched_in(group_event, cpuctx, ctx)) {
1386 pmu->cancel_txn(pmu);
1391 * Schedule in siblings as one group (if any):
1393 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1394 if (event_sched_in(event, cpuctx, ctx)) {
1395 partial_group = event;
1400 if (!pmu->commit_txn(pmu))
1405 * Groups can be scheduled in as one unit only, so undo any
1406 * partial group before returning:
1407 * The events up to the failed event are scheduled out normally,
1408 * tstamp_stopped will be updated.
1410 * The failed events and the remaining siblings need to have
1411 * their timings updated as if they had gone thru event_sched_in()
1412 * and event_sched_out(). This is required to get consistent timings
1413 * across the group. This also takes care of the case where the group
1414 * could never be scheduled by ensuring tstamp_stopped is set to mark
1415 * the time the event was actually stopped, such that time delta
1416 * calculation in update_event_times() is correct.
1418 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1419 if (event == partial_group)
1423 event->tstamp_running += now - event->tstamp_stopped;
1424 event->tstamp_stopped = now;
1426 event_sched_out(event, cpuctx, ctx);
1429 event_sched_out(group_event, cpuctx, ctx);
1431 pmu->cancel_txn(pmu);
1437 * Work out whether we can put this event group on the CPU now.
1439 static int group_can_go_on(struct perf_event *event,
1440 struct perf_cpu_context *cpuctx,
1444 * Groups consisting entirely of software events can always go on.
1446 if (event->group_flags & PERF_GROUP_SOFTWARE)
1449 * If an exclusive group is already on, no other hardware
1452 if (cpuctx->exclusive)
1455 * If this group is exclusive and there are already
1456 * events on the CPU, it can't go on.
1458 if (event->attr.exclusive && cpuctx->active_oncpu)
1461 * Otherwise, try to add it if all previous groups were able
1467 static void add_event_to_ctx(struct perf_event *event,
1468 struct perf_event_context *ctx)
1470 u64 tstamp = perf_event_time(event);
1472 list_add_event(event, ctx);
1473 perf_group_attach(event);
1474 event->tstamp_enabled = tstamp;
1475 event->tstamp_running = tstamp;
1476 event->tstamp_stopped = tstamp;
1479 static void task_ctx_sched_out(struct perf_event_context *ctx);
1481 ctx_sched_in(struct perf_event_context *ctx,
1482 struct perf_cpu_context *cpuctx,
1483 enum event_type_t event_type,
1484 struct task_struct *task);
1486 static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1487 struct perf_event_context *ctx,
1488 struct task_struct *task)
1490 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1492 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1493 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1495 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1499 * Cross CPU call to install and enable a performance event
1501 * Must be called with ctx->mutex held
1503 static int __perf_install_in_context(void *info)
1505 struct perf_event *event = info;
1506 struct perf_event_context *ctx = event->ctx;
1507 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1508 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1509 struct task_struct *task = current;
1511 perf_ctx_lock(cpuctx, task_ctx);
1512 perf_pmu_disable(cpuctx->ctx.pmu);
1515 * If there was an active task_ctx schedule it out.
1518 task_ctx_sched_out(task_ctx);
1521 * If the context we're installing events in is not the
1522 * active task_ctx, flip them.
1524 if (ctx->task && task_ctx != ctx) {
1526 raw_spin_unlock(&task_ctx->lock);
1527 raw_spin_lock(&ctx->lock);
1532 cpuctx->task_ctx = task_ctx;
1533 task = task_ctx->task;
1536 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
1538 update_context_time(ctx);
1540 * update cgrp time only if current cgrp
1541 * matches event->cgrp. Must be done before
1542 * calling add_event_to_ctx()
1544 update_cgrp_time_from_event(event);
1546 add_event_to_ctx(event, ctx);
1549 * Schedule everything back in
1551 perf_event_sched_in(cpuctx, task_ctx, task);
1553 perf_pmu_enable(cpuctx->ctx.pmu);
1554 perf_ctx_unlock(cpuctx, task_ctx);
1560 * Attach a performance event to a context
1562 * First we add the event to the list with the hardware enable bit
1563 * in event->hw_config cleared.
1565 * If the event is attached to a task which is on a CPU we use a smp
1566 * call to enable it in the task context. The task might have been
1567 * scheduled away, but we check this in the smp call again.
1570 perf_install_in_context(struct perf_event_context *ctx,
1571 struct perf_event *event,
1574 struct task_struct *task = ctx->task;
1576 lockdep_assert_held(&ctx->mutex);
1582 * Per cpu events are installed via an smp call and
1583 * the install is always successful.
1585 cpu_function_call(cpu, __perf_install_in_context, event);
1590 if (!task_function_call(task, __perf_install_in_context, event))
1593 raw_spin_lock_irq(&ctx->lock);
1595 * If we failed to find a running task, but find the context active now
1596 * that we've acquired the ctx->lock, retry.
1598 if (ctx->is_active) {
1599 raw_spin_unlock_irq(&ctx->lock);
1604 * Since the task isn't running, its safe to add the event, us holding
1605 * the ctx->lock ensures the task won't get scheduled in.
1607 add_event_to_ctx(event, ctx);
1608 raw_spin_unlock_irq(&ctx->lock);
1612 * Put a event into inactive state and update time fields.
1613 * Enabling the leader of a group effectively enables all
1614 * the group members that aren't explicitly disabled, so we
1615 * have to update their ->tstamp_enabled also.
1616 * Note: this works for group members as well as group leaders
1617 * since the non-leader members' sibling_lists will be empty.
1619 static void __perf_event_mark_enabled(struct perf_event *event,
1620 struct perf_event_context *ctx)
1622 struct perf_event *sub;
1623 u64 tstamp = perf_event_time(event);
1625 event->state = PERF_EVENT_STATE_INACTIVE;
1626 event->tstamp_enabled = tstamp - event->total_time_enabled;
1627 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1628 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1629 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1634 * Cross CPU call to enable a performance event
1636 static int __perf_event_enable(void *info)
1638 struct perf_event *event = info;
1639 struct perf_event_context *ctx = event->ctx;
1640 struct perf_event *leader = event->group_leader;
1641 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1644 if (WARN_ON_ONCE(!ctx->is_active))
1647 raw_spin_lock(&ctx->lock);
1648 update_context_time(ctx);
1650 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1654 * set current task's cgroup time reference point
1656 perf_cgroup_set_timestamp(current, ctx);
1658 __perf_event_mark_enabled(event, ctx);
1660 if (!event_filter_match(event)) {
1661 if (is_cgroup_event(event))
1662 perf_cgroup_defer_enabled(event);
1667 * If the event is in a group and isn't the group leader,
1668 * then don't put it on unless the group is on.
1670 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1673 if (!group_can_go_on(event, cpuctx, 1)) {
1676 if (event == leader)
1677 err = group_sched_in(event, cpuctx, ctx);
1679 err = event_sched_in(event, cpuctx, ctx);
1684 * If this event can't go on and it's part of a
1685 * group, then the whole group has to come off.
1687 if (leader != event)
1688 group_sched_out(leader, cpuctx, ctx);
1689 if (leader->attr.pinned) {
1690 update_group_times(leader);
1691 leader->state = PERF_EVENT_STATE_ERROR;
1696 raw_spin_unlock(&ctx->lock);
1704 * If event->ctx is a cloned context, callers must make sure that
1705 * every task struct that event->ctx->task could possibly point to
1706 * remains valid. This condition is satisfied when called through
1707 * perf_event_for_each_child or perf_event_for_each as described
1708 * for perf_event_disable.
1710 void perf_event_enable(struct perf_event *event)
1712 struct perf_event_context *ctx = event->ctx;
1713 struct task_struct *task = ctx->task;
1717 * Enable the event on the cpu that it's on
1719 cpu_function_call(event->cpu, __perf_event_enable, event);
1723 raw_spin_lock_irq(&ctx->lock);
1724 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1728 * If the event is in error state, clear that first.
1729 * That way, if we see the event in error state below, we
1730 * know that it has gone back into error state, as distinct
1731 * from the task having been scheduled away before the
1732 * cross-call arrived.
1734 if (event->state == PERF_EVENT_STATE_ERROR)
1735 event->state = PERF_EVENT_STATE_OFF;
1738 if (!ctx->is_active) {
1739 __perf_event_mark_enabled(event, ctx);
1743 raw_spin_unlock_irq(&ctx->lock);
1745 if (!task_function_call(task, __perf_event_enable, event))
1748 raw_spin_lock_irq(&ctx->lock);
1751 * If the context is active and the event is still off,
1752 * we need to retry the cross-call.
1754 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1756 * task could have been flipped by a concurrent
1757 * perf_event_context_sched_out()
1764 raw_spin_unlock_irq(&ctx->lock);
1767 static int perf_event_refresh(struct perf_event *event, int refresh)
1770 * not supported on inherited events
1772 if (event->attr.inherit || !is_sampling_event(event))
1775 atomic_add(refresh, &event->event_limit);
1776 perf_event_enable(event);
1781 static void ctx_sched_out(struct perf_event_context *ctx,
1782 struct perf_cpu_context *cpuctx,
1783 enum event_type_t event_type)
1785 struct perf_event *event;
1786 int is_active = ctx->is_active;
1788 ctx->is_active &= ~event_type;
1789 if (likely(!ctx->nr_events))
1792 update_context_time(ctx);
1793 update_cgrp_time_from_cpuctx(cpuctx);
1794 if (!ctx->nr_active)
1797 perf_pmu_disable(ctx->pmu);
1798 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
1799 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1800 group_sched_out(event, cpuctx, ctx);
1803 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
1804 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1805 group_sched_out(event, cpuctx, ctx);
1807 perf_pmu_enable(ctx->pmu);
1811 * Test whether two contexts are equivalent, i.e. whether they
1812 * have both been cloned from the same version of the same context
1813 * and they both have the same number of enabled events.
1814 * If the number of enabled events is the same, then the set
1815 * of enabled events should be the same, because these are both
1816 * inherited contexts, therefore we can't access individual events
1817 * in them directly with an fd; we can only enable/disable all
1818 * events via prctl, or enable/disable all events in a family
1819 * via ioctl, which will have the same effect on both contexts.
1821 static int context_equiv(struct perf_event_context *ctx1,
1822 struct perf_event_context *ctx2)
1824 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1825 && ctx1->parent_gen == ctx2->parent_gen
1826 && !ctx1->pin_count && !ctx2->pin_count;
1829 static void __perf_event_sync_stat(struct perf_event *event,
1830 struct perf_event *next_event)
1834 if (!event->attr.inherit_stat)
1838 * Update the event value, we cannot use perf_event_read()
1839 * because we're in the middle of a context switch and have IRQs
1840 * disabled, which upsets smp_call_function_single(), however
1841 * we know the event must be on the current CPU, therefore we
1842 * don't need to use it.
1844 switch (event->state) {
1845 case PERF_EVENT_STATE_ACTIVE:
1846 event->pmu->read(event);
1849 case PERF_EVENT_STATE_INACTIVE:
1850 update_event_times(event);
1858 * In order to keep per-task stats reliable we need to flip the event
1859 * values when we flip the contexts.
1861 value = local64_read(&next_event->count);
1862 value = local64_xchg(&event->count, value);
1863 local64_set(&next_event->count, value);
1865 swap(event->total_time_enabled, next_event->total_time_enabled);
1866 swap(event->total_time_running, next_event->total_time_running);
1869 * Since we swizzled the values, update the user visible data too.
1871 perf_event_update_userpage(event);
1872 perf_event_update_userpage(next_event);
1875 #define list_next_entry(pos, member) \
1876 list_entry(pos->member.next, typeof(*pos), member)
1878 static void perf_event_sync_stat(struct perf_event_context *ctx,
1879 struct perf_event_context *next_ctx)
1881 struct perf_event *event, *next_event;
1886 update_context_time(ctx);
1888 event = list_first_entry(&ctx->event_list,
1889 struct perf_event, event_entry);
1891 next_event = list_first_entry(&next_ctx->event_list,
1892 struct perf_event, event_entry);
1894 while (&event->event_entry != &ctx->event_list &&
1895 &next_event->event_entry != &next_ctx->event_list) {
1897 __perf_event_sync_stat(event, next_event);
1899 event = list_next_entry(event, event_entry);
1900 next_event = list_next_entry(next_event, event_entry);
1904 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1905 struct task_struct *next)
1907 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1908 struct perf_event_context *next_ctx;
1909 struct perf_event_context *parent;
1910 struct perf_cpu_context *cpuctx;
1916 cpuctx = __get_cpu_context(ctx);
1917 if (!cpuctx->task_ctx)
1921 parent = rcu_dereference(ctx->parent_ctx);
1922 next_ctx = next->perf_event_ctxp[ctxn];
1923 if (parent && next_ctx &&
1924 rcu_dereference(next_ctx->parent_ctx) == parent) {
1926 * Looks like the two contexts are clones, so we might be
1927 * able to optimize the context switch. We lock both
1928 * contexts and check that they are clones under the
1929 * lock (including re-checking that neither has been
1930 * uncloned in the meantime). It doesn't matter which
1931 * order we take the locks because no other cpu could
1932 * be trying to lock both of these tasks.
1934 raw_spin_lock(&ctx->lock);
1935 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1936 if (context_equiv(ctx, next_ctx)) {
1938 * XXX do we need a memory barrier of sorts
1939 * wrt to rcu_dereference() of perf_event_ctxp
1941 task->perf_event_ctxp[ctxn] = next_ctx;
1942 next->perf_event_ctxp[ctxn] = ctx;
1944 next_ctx->task = task;
1947 perf_event_sync_stat(ctx, next_ctx);
1949 raw_spin_unlock(&next_ctx->lock);
1950 raw_spin_unlock(&ctx->lock);
1955 raw_spin_lock(&ctx->lock);
1956 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
1957 cpuctx->task_ctx = NULL;
1958 raw_spin_unlock(&ctx->lock);
1962 #define for_each_task_context_nr(ctxn) \
1963 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1966 * Called from scheduler to remove the events of the current task,
1967 * with interrupts disabled.
1969 * We stop each event and update the event value in event->count.
1971 * This does not protect us against NMI, but disable()
1972 * sets the disabled bit in the control field of event _before_
1973 * accessing the event control register. If a NMI hits, then it will
1974 * not restart the event.
1976 void __perf_event_task_sched_out(struct task_struct *task,
1977 struct task_struct *next)
1981 for_each_task_context_nr(ctxn)
1982 perf_event_context_sched_out(task, ctxn, next);
1985 * if cgroup events exist on this CPU, then we need
1986 * to check if we have to switch out PMU state.
1987 * cgroup event are system-wide mode only
1989 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
1990 perf_cgroup_sched_out(task);
1993 static void task_ctx_sched_out(struct perf_event_context *ctx)
1995 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1997 if (!cpuctx->task_ctx)
2000 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2003 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2004 cpuctx->task_ctx = NULL;
2008 * Called with IRQs disabled
2010 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2011 enum event_type_t event_type)
2013 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
2017 ctx_pinned_sched_in(struct perf_event_context *ctx,
2018 struct perf_cpu_context *cpuctx)
2020 struct perf_event *event;
2022 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2023 if (event->state <= PERF_EVENT_STATE_OFF)
2025 if (!event_filter_match(event))
2028 /* may need to reset tstamp_enabled */
2029 if (is_cgroup_event(event))
2030 perf_cgroup_mark_enabled(event, ctx);
2032 if (group_can_go_on(event, cpuctx, 1))
2033 group_sched_in(event, cpuctx, ctx);
2036 * If this pinned group hasn't been scheduled,
2037 * put it in error state.
2039 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2040 update_group_times(event);
2041 event->state = PERF_EVENT_STATE_ERROR;
2047 ctx_flexible_sched_in(struct perf_event_context *ctx,
2048 struct perf_cpu_context *cpuctx)
2050 struct perf_event *event;
2053 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2054 /* Ignore events in OFF or ERROR state */
2055 if (event->state <= PERF_EVENT_STATE_OFF)
2058 * Listen to the 'cpu' scheduling filter constraint
2061 if (!event_filter_match(event))
2064 /* may need to reset tstamp_enabled */
2065 if (is_cgroup_event(event))
2066 perf_cgroup_mark_enabled(event, ctx);
2068 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2069 if (group_sched_in(event, cpuctx, ctx))
2076 ctx_sched_in(struct perf_event_context *ctx,
2077 struct perf_cpu_context *cpuctx,
2078 enum event_type_t event_type,
2079 struct task_struct *task)
2082 int is_active = ctx->is_active;
2084 ctx->is_active |= event_type;
2085 if (likely(!ctx->nr_events))
2089 ctx->timestamp = now;
2090 perf_cgroup_set_timestamp(task, ctx);
2092 * First go through the list and put on any pinned groups
2093 * in order to give them the best chance of going on.
2095 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
2096 ctx_pinned_sched_in(ctx, cpuctx);
2098 /* Then walk through the lower prio flexible groups */
2099 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
2100 ctx_flexible_sched_in(ctx, cpuctx);
2103 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2104 enum event_type_t event_type,
2105 struct task_struct *task)
2107 struct perf_event_context *ctx = &cpuctx->ctx;
2109 ctx_sched_in(ctx, cpuctx, event_type, task);
2112 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2113 struct task_struct *task)
2115 struct perf_cpu_context *cpuctx;
2117 cpuctx = __get_cpu_context(ctx);
2118 if (cpuctx->task_ctx == ctx)
2121 perf_ctx_lock(cpuctx, ctx);
2122 perf_pmu_disable(ctx->pmu);
2124 * We want to keep the following priority order:
2125 * cpu pinned (that don't need to move), task pinned,
2126 * cpu flexible, task flexible.
2128 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2130 perf_event_sched_in(cpuctx, ctx, task);
2132 cpuctx->task_ctx = ctx;
2134 perf_pmu_enable(ctx->pmu);
2135 perf_ctx_unlock(cpuctx, ctx);
2138 * Since these rotations are per-cpu, we need to ensure the
2139 * cpu-context we got scheduled on is actually rotating.
2141 perf_pmu_rotate_start(ctx->pmu);
2145 * Called from scheduler to add the events of the current task
2146 * with interrupts disabled.
2148 * We restore the event value and then enable it.
2150 * This does not protect us against NMI, but enable()
2151 * sets the enabled bit in the control field of event _before_
2152 * accessing the event control register. If a NMI hits, then it will
2153 * keep the event running.
2155 void __perf_event_task_sched_in(struct task_struct *task)
2157 struct perf_event_context *ctx;
2160 for_each_task_context_nr(ctxn) {
2161 ctx = task->perf_event_ctxp[ctxn];
2165 perf_event_context_sched_in(ctx, task);
2168 * if cgroup events exist on this CPU, then we need
2169 * to check if we have to switch in PMU state.
2170 * cgroup event are system-wide mode only
2172 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2173 perf_cgroup_sched_in(task);
2176 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2178 u64 frequency = event->attr.sample_freq;
2179 u64 sec = NSEC_PER_SEC;
2180 u64 divisor, dividend;
2182 int count_fls, nsec_fls, frequency_fls, sec_fls;
2184 count_fls = fls64(count);
2185 nsec_fls = fls64(nsec);
2186 frequency_fls = fls64(frequency);
2190 * We got @count in @nsec, with a target of sample_freq HZ
2191 * the target period becomes:
2194 * period = -------------------
2195 * @nsec * sample_freq
2200 * Reduce accuracy by one bit such that @a and @b converge
2201 * to a similar magnitude.
2203 #define REDUCE_FLS(a, b) \
2205 if (a##_fls > b##_fls) { \
2215 * Reduce accuracy until either term fits in a u64, then proceed with
2216 * the other, so that finally we can do a u64/u64 division.
2218 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2219 REDUCE_FLS(nsec, frequency);
2220 REDUCE_FLS(sec, count);
2223 if (count_fls + sec_fls > 64) {
2224 divisor = nsec * frequency;
2226 while (count_fls + sec_fls > 64) {
2227 REDUCE_FLS(count, sec);
2231 dividend = count * sec;
2233 dividend = count * sec;
2235 while (nsec_fls + frequency_fls > 64) {
2236 REDUCE_FLS(nsec, frequency);
2240 divisor = nsec * frequency;
2246 return div64_u64(dividend, divisor);
2249 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
2251 struct hw_perf_event *hwc = &event->hw;
2252 s64 period, sample_period;
2255 period = perf_calculate_period(event, nsec, count);
2257 delta = (s64)(period - hwc->sample_period);
2258 delta = (delta + 7) / 8; /* low pass filter */
2260 sample_period = hwc->sample_period + delta;
2265 hwc->sample_period = sample_period;
2267 if (local64_read(&hwc->period_left) > 8*sample_period) {
2268 event->pmu->stop(event, PERF_EF_UPDATE);
2269 local64_set(&hwc->period_left, 0);
2270 event->pmu->start(event, PERF_EF_RELOAD);
2274 static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
2276 struct perf_event *event;
2277 struct hw_perf_event *hwc;
2278 u64 interrupts, now;
2281 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2282 if (event->state != PERF_EVENT_STATE_ACTIVE)
2285 if (!event_filter_match(event))
2290 interrupts = hwc->interrupts;
2291 hwc->interrupts = 0;
2294 * unthrottle events on the tick
2296 if (interrupts == MAX_INTERRUPTS) {
2297 perf_log_throttle(event, 1);
2298 event->pmu->start(event, 0);
2301 if (!event->attr.freq || !event->attr.sample_freq)
2304 event->pmu->read(event);
2305 now = local64_read(&event->count);
2306 delta = now - hwc->freq_count_stamp;
2307 hwc->freq_count_stamp = now;
2310 perf_adjust_period(event, period, delta);
2315 * Round-robin a context's events:
2317 static void rotate_ctx(struct perf_event_context *ctx)
2320 * Rotate the first entry last of non-pinned groups. Rotation might be
2321 * disabled by the inheritance code.
2323 if (!ctx->rotate_disable)
2324 list_rotate_left(&ctx->flexible_groups);
2328 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2329 * because they're strictly cpu affine and rotate_start is called with IRQs
2330 * disabled, while rotate_context is called from IRQ context.
2332 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
2334 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
2335 struct perf_event_context *ctx = NULL;
2336 int rotate = 0, remove = 1;
2338 if (cpuctx->ctx.nr_events) {
2340 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2344 ctx = cpuctx->task_ctx;
2345 if (ctx && ctx->nr_events) {
2347 if (ctx->nr_events != ctx->nr_active)
2351 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2352 perf_pmu_disable(cpuctx->ctx.pmu);
2353 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
2355 perf_ctx_adjust_freq(ctx, interval);
2360 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2362 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
2364 rotate_ctx(&cpuctx->ctx);
2368 perf_event_sched_in(cpuctx, ctx, current);
2372 list_del_init(&cpuctx->rotation_list);
2374 perf_pmu_enable(cpuctx->ctx.pmu);
2375 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2378 void perf_event_task_tick(void)
2380 struct list_head *head = &__get_cpu_var(rotation_list);
2381 struct perf_cpu_context *cpuctx, *tmp;
2383 WARN_ON(!irqs_disabled());
2385 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2386 if (cpuctx->jiffies_interval == 1 ||
2387 !(jiffies % cpuctx->jiffies_interval))
2388 perf_rotate_context(cpuctx);
2392 static int event_enable_on_exec(struct perf_event *event,
2393 struct perf_event_context *ctx)
2395 if (!event->attr.enable_on_exec)
2398 event->attr.enable_on_exec = 0;
2399 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2402 __perf_event_mark_enabled(event, ctx);
2408 * Enable all of a task's events that have been marked enable-on-exec.
2409 * This expects task == current.
2411 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2413 struct perf_event *event;
2414 unsigned long flags;
2418 local_irq_save(flags);
2419 if (!ctx || !ctx->nr_events)
2423 * We must ctxsw out cgroup events to avoid conflict
2424 * when invoking perf_task_event_sched_in() later on
2425 * in this function. Otherwise we end up trying to
2426 * ctxswin cgroup events which are already scheduled
2429 perf_cgroup_sched_out(current);
2431 raw_spin_lock(&ctx->lock);
2432 task_ctx_sched_out(ctx);
2434 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2435 ret = event_enable_on_exec(event, ctx);
2440 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2441 ret = event_enable_on_exec(event, ctx);
2447 * Unclone this context if we enabled any event.
2452 raw_spin_unlock(&ctx->lock);
2455 * Also calls ctxswin for cgroup events, if any:
2457 perf_event_context_sched_in(ctx, ctx->task);
2459 local_irq_restore(flags);
2463 * Cross CPU call to read the hardware event
2465 static void __perf_event_read(void *info)
2467 struct perf_event *event = info;
2468 struct perf_event_context *ctx = event->ctx;
2469 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2472 * If this is a task context, we need to check whether it is
2473 * the current task context of this cpu. If not it has been
2474 * scheduled out before the smp call arrived. In that case
2475 * event->count would have been updated to a recent sample
2476 * when the event was scheduled out.
2478 if (ctx->task && cpuctx->task_ctx != ctx)
2481 raw_spin_lock(&ctx->lock);
2482 if (ctx->is_active) {
2483 update_context_time(ctx);
2484 update_cgrp_time_from_event(event);
2486 update_event_times(event);
2487 if (event->state == PERF_EVENT_STATE_ACTIVE)
2488 event->pmu->read(event);
2489 raw_spin_unlock(&ctx->lock);
2492 static inline u64 perf_event_count(struct perf_event *event)
2494 return local64_read(&event->count) + atomic64_read(&event->child_count);
2497 static u64 perf_event_read(struct perf_event *event)
2500 * If event is enabled and currently active on a CPU, update the
2501 * value in the event structure:
2503 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2504 smp_call_function_single(event->oncpu,
2505 __perf_event_read, event, 1);
2506 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2507 struct perf_event_context *ctx = event->ctx;
2508 unsigned long flags;
2510 raw_spin_lock_irqsave(&ctx->lock, flags);
2512 * may read while context is not active
2513 * (e.g., thread is blocked), in that case
2514 * we cannot update context time
2516 if (ctx->is_active) {
2517 update_context_time(ctx);
2518 update_cgrp_time_from_event(event);
2520 update_event_times(event);
2521 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2524 return perf_event_count(event);
2531 struct callchain_cpus_entries {
2532 struct rcu_head rcu_head;
2533 struct perf_callchain_entry *cpu_entries[0];
2536 static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
2537 static atomic_t nr_callchain_events;
2538 static DEFINE_MUTEX(callchain_mutex);
2539 struct callchain_cpus_entries *callchain_cpus_entries;
2542 __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
2543 struct pt_regs *regs)
2547 __weak void perf_callchain_user(struct perf_callchain_entry *entry,
2548 struct pt_regs *regs)
2552 static void release_callchain_buffers_rcu(struct rcu_head *head)
2554 struct callchain_cpus_entries *entries;
2557 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
2559 for_each_possible_cpu(cpu)
2560 kfree(entries->cpu_entries[cpu]);
2565 static void release_callchain_buffers(void)
2567 struct callchain_cpus_entries *entries;
2569 entries = callchain_cpus_entries;
2570 rcu_assign_pointer(callchain_cpus_entries, NULL);
2571 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2574 static int alloc_callchain_buffers(void)
2578 struct callchain_cpus_entries *entries;
2581 * We can't use the percpu allocation API for data that can be
2582 * accessed from NMI. Use a temporary manual per cpu allocation
2583 * until that gets sorted out.
2585 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
2587 entries = kzalloc(size, GFP_KERNEL);
2591 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
2593 for_each_possible_cpu(cpu) {
2594 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2596 if (!entries->cpu_entries[cpu])
2600 rcu_assign_pointer(callchain_cpus_entries, entries);
2605 for_each_possible_cpu(cpu)
2606 kfree(entries->cpu_entries[cpu]);
2612 static int get_callchain_buffers(void)
2617 mutex_lock(&callchain_mutex);
2619 count = atomic_inc_return(&nr_callchain_events);
2620 if (WARN_ON_ONCE(count < 1)) {
2626 /* If the allocation failed, give up */
2627 if (!callchain_cpus_entries)
2632 err = alloc_callchain_buffers();
2634 release_callchain_buffers();
2636 mutex_unlock(&callchain_mutex);
2641 static void put_callchain_buffers(void)
2643 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2644 release_callchain_buffers();
2645 mutex_unlock(&callchain_mutex);
2649 static int get_recursion_context(int *recursion)
2657 else if (in_softirq())
2662 if (recursion[rctx])
2671 static inline void put_recursion_context(int *recursion, int rctx)
2677 static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2680 struct callchain_cpus_entries *entries;
2682 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2686 entries = rcu_dereference(callchain_cpus_entries);
2690 cpu = smp_processor_id();
2692 return &entries->cpu_entries[cpu][*rctx];
2696 put_callchain_entry(int rctx)
2698 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2701 static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2704 struct perf_callchain_entry *entry;
2707 entry = get_callchain_entry(&rctx);
2716 if (!user_mode(regs)) {
2717 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2718 perf_callchain_kernel(entry, regs);
2720 regs = task_pt_regs(current);
2726 perf_callchain_store(entry, PERF_CONTEXT_USER);
2727 perf_callchain_user(entry, regs);
2731 put_callchain_entry(rctx);
2737 * Initialize the perf_event context in a task_struct:
2739 static void __perf_event_init_context(struct perf_event_context *ctx)
2741 raw_spin_lock_init(&ctx->lock);
2742 mutex_init(&ctx->mutex);
2743 INIT_LIST_HEAD(&ctx->pinned_groups);
2744 INIT_LIST_HEAD(&ctx->flexible_groups);
2745 INIT_LIST_HEAD(&ctx->event_list);
2746 atomic_set(&ctx->refcount, 1);
2749 static struct perf_event_context *
2750 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2752 struct perf_event_context *ctx;
2754 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2758 __perf_event_init_context(ctx);
2761 get_task_struct(task);
2768 static struct task_struct *
2769 find_lively_task_by_vpid(pid_t vpid)
2771 struct task_struct *task;
2778 task = find_task_by_vpid(vpid);
2780 get_task_struct(task);
2784 return ERR_PTR(-ESRCH);
2786 /* Reuse ptrace permission checks for now. */
2788 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2793 put_task_struct(task);
2794 return ERR_PTR(err);
2799 * Returns a matching context with refcount and pincount.
2801 static struct perf_event_context *
2802 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2804 struct perf_event_context *ctx;
2805 struct perf_cpu_context *cpuctx;
2806 unsigned long flags;
2810 /* Must be root to operate on a CPU event: */
2811 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2812 return ERR_PTR(-EACCES);
2815 * We could be clever and allow to attach a event to an
2816 * offline CPU and activate it when the CPU comes up, but
2819 if (!cpu_online(cpu))
2820 return ERR_PTR(-ENODEV);
2822 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2831 ctxn = pmu->task_ctx_nr;
2836 ctx = perf_lock_task_context(task, ctxn, &flags);
2840 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2842 ctx = alloc_perf_context(pmu, task);
2848 mutex_lock(&task->perf_event_mutex);
2850 * If it has already passed perf_event_exit_task().
2851 * we must see PF_EXITING, it takes this mutex too.
2853 if (task->flags & PF_EXITING)
2855 else if (task->perf_event_ctxp[ctxn])
2860 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2862 mutex_unlock(&task->perf_event_mutex);
2864 if (unlikely(err)) {
2876 return ERR_PTR(err);
2879 static void perf_event_free_filter(struct perf_event *event);
2881 static void free_event_rcu(struct rcu_head *head)
2883 struct perf_event *event;
2885 event = container_of(head, struct perf_event, rcu_head);
2887 put_pid_ns(event->ns);
2888 perf_event_free_filter(event);
2892 static void ring_buffer_put(struct ring_buffer *rb);
2894 static void free_event(struct perf_event *event)
2896 irq_work_sync(&event->pending);
2898 if (!event->parent) {
2899 if (event->attach_state & PERF_ATTACH_TASK)
2900 jump_label_dec(&perf_sched_events);
2901 if (event->attr.mmap || event->attr.mmap_data)
2902 atomic_dec(&nr_mmap_events);
2903 if (event->attr.comm)
2904 atomic_dec(&nr_comm_events);
2905 if (event->attr.task)
2906 atomic_dec(&nr_task_events);
2907 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2908 put_callchain_buffers();
2909 if (is_cgroup_event(event)) {
2910 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
2911 jump_label_dec(&perf_sched_events);
2916 ring_buffer_put(event->rb);
2920 if (is_cgroup_event(event))
2921 perf_detach_cgroup(event);
2924 event->destroy(event);
2927 put_ctx(event->ctx);
2929 call_rcu(&event->rcu_head, free_event_rcu);
2932 int perf_event_release_kernel(struct perf_event *event)
2934 struct perf_event_context *ctx = event->ctx;
2936 WARN_ON_ONCE(ctx->parent_ctx);
2938 * There are two ways this annotation is useful:
2940 * 1) there is a lock recursion from perf_event_exit_task
2941 * see the comment there.
2943 * 2) there is a lock-inversion with mmap_sem through
2944 * perf_event_read_group(), which takes faults while
2945 * holding ctx->mutex, however this is called after
2946 * the last filedesc died, so there is no possibility
2947 * to trigger the AB-BA case.
2949 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2950 raw_spin_lock_irq(&ctx->lock);
2951 perf_group_detach(event);
2952 raw_spin_unlock_irq(&ctx->lock);
2953 perf_remove_from_context(event);
2954 mutex_unlock(&ctx->mutex);
2960 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2963 * Called when the last reference to the file is gone.
2965 static int perf_release(struct inode *inode, struct file *file)
2967 struct perf_event *event = file->private_data;
2968 struct task_struct *owner;
2970 file->private_data = NULL;
2973 owner = ACCESS_ONCE(event->owner);
2975 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2976 * !owner it means the list deletion is complete and we can indeed
2977 * free this event, otherwise we need to serialize on
2978 * owner->perf_event_mutex.
2980 smp_read_barrier_depends();
2983 * Since delayed_put_task_struct() also drops the last
2984 * task reference we can safely take a new reference
2985 * while holding the rcu_read_lock().
2987 get_task_struct(owner);
2992 mutex_lock(&owner->perf_event_mutex);
2994 * We have to re-check the event->owner field, if it is cleared
2995 * we raced with perf_event_exit_task(), acquiring the mutex
2996 * ensured they're done, and we can proceed with freeing the
3000 list_del_init(&event->owner_entry);
3001 mutex_unlock(&owner->perf_event_mutex);
3002 put_task_struct(owner);
3005 return perf_event_release_kernel(event);
3008 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
3010 struct perf_event *child;
3016 mutex_lock(&event->child_mutex);
3017 total += perf_event_read(event);
3018 *enabled += event->total_time_enabled +
3019 atomic64_read(&event->child_total_time_enabled);
3020 *running += event->total_time_running +
3021 atomic64_read(&event->child_total_time_running);
3023 list_for_each_entry(child, &event->child_list, child_list) {
3024 total += perf_event_read(child);
3025 *enabled += child->total_time_enabled;
3026 *running += child->total_time_running;
3028 mutex_unlock(&event->child_mutex);
3032 EXPORT_SYMBOL_GPL(perf_event_read_value);
3034 static int perf_event_read_group(struct perf_event *event,
3035 u64 read_format, char __user *buf)
3037 struct perf_event *leader = event->group_leader, *sub;
3038 int n = 0, size = 0, ret = -EFAULT;
3039 struct perf_event_context *ctx = leader->ctx;
3041 u64 count, enabled, running;
3043 mutex_lock(&ctx->mutex);
3044 count = perf_event_read_value(leader, &enabled, &running);
3046 values[n++] = 1 + leader->nr_siblings;
3047 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3048 values[n++] = enabled;
3049 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3050 values[n++] = running;
3051 values[n++] = count;
3052 if (read_format & PERF_FORMAT_ID)
3053 values[n++] = primary_event_id(leader);
3055 size = n * sizeof(u64);
3057 if (copy_to_user(buf, values, size))
3062 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3065 values[n++] = perf_event_read_value(sub, &enabled, &running);
3066 if (read_format & PERF_FORMAT_ID)
3067 values[n++] = primary_event_id(sub);
3069 size = n * sizeof(u64);
3071 if (copy_to_user(buf + ret, values, size)) {
3079 mutex_unlock(&ctx->mutex);
3084 static int perf_event_read_one(struct perf_event *event,
3085 u64 read_format, char __user *buf)
3087 u64 enabled, running;
3091 values[n++] = perf_event_read_value(event, &enabled, &running);
3092 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3093 values[n++] = enabled;
3094 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3095 values[n++] = running;
3096 if (read_format & PERF_FORMAT_ID)
3097 values[n++] = primary_event_id(event);
3099 if (copy_to_user(buf, values, n * sizeof(u64)))
3102 return n * sizeof(u64);
3106 * Read the performance event - simple non blocking version for now
3109 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3111 u64 read_format = event->attr.read_format;
3115 * Return end-of-file for a read on a event that is in
3116 * error state (i.e. because it was pinned but it couldn't be
3117 * scheduled on to the CPU at some point).
3119 if (event->state == PERF_EVENT_STATE_ERROR)
3122 if (count < event->read_size)
3125 WARN_ON_ONCE(event->ctx->parent_ctx);
3126 if (read_format & PERF_FORMAT_GROUP)
3127 ret = perf_event_read_group(event, read_format, buf);
3129 ret = perf_event_read_one(event, read_format, buf);
3135 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3137 struct perf_event *event = file->private_data;
3139 return perf_read_hw(event, buf, count);
3142 static unsigned int perf_poll(struct file *file, poll_table *wait)
3144 struct perf_event *event = file->private_data;
3145 struct ring_buffer *rb;
3146 unsigned int events = POLL_HUP;
3149 rb = rcu_dereference(event->rb);
3151 events = atomic_xchg(&rb->poll, 0);
3154 poll_wait(file, &event->waitq, wait);
3159 static void perf_event_reset(struct perf_event *event)
3161 (void)perf_event_read(event);
3162 local64_set(&event->count, 0);
3163 perf_event_update_userpage(event);
3167 * Holding the top-level event's child_mutex means that any
3168 * descendant process that has inherited this event will block
3169 * in sync_child_event if it goes to exit, thus satisfying the
3170 * task existence requirements of perf_event_enable/disable.
3172 static void perf_event_for_each_child(struct perf_event *event,
3173 void (*func)(struct perf_event *))
3175 struct perf_event *child;
3177 WARN_ON_ONCE(event->ctx->parent_ctx);
3178 mutex_lock(&event->child_mutex);
3180 list_for_each_entry(child, &event->child_list, child_list)
3182 mutex_unlock(&event->child_mutex);
3185 static void perf_event_for_each(struct perf_event *event,
3186 void (*func)(struct perf_event *))
3188 struct perf_event_context *ctx = event->ctx;
3189 struct perf_event *sibling;
3191 WARN_ON_ONCE(ctx->parent_ctx);
3192 mutex_lock(&ctx->mutex);
3193 event = event->group_leader;
3195 perf_event_for_each_child(event, func);
3197 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3198 perf_event_for_each_child(event, func);
3199 mutex_unlock(&ctx->mutex);
3202 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3204 struct perf_event_context *ctx = event->ctx;
3208 if (!is_sampling_event(event))
3211 if (copy_from_user(&value, arg, sizeof(value)))
3217 raw_spin_lock_irq(&ctx->lock);
3218 if (event->attr.freq) {
3219 if (value > sysctl_perf_event_sample_rate) {
3224 event->attr.sample_freq = value;
3226 event->attr.sample_period = value;
3227 event->hw.sample_period = value;
3230 raw_spin_unlock_irq(&ctx->lock);
3235 static const struct file_operations perf_fops;
3237 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
3241 file = fget_light(fd, fput_needed);
3243 return ERR_PTR(-EBADF);
3245 if (file->f_op != &perf_fops) {
3246 fput_light(file, *fput_needed);
3248 return ERR_PTR(-EBADF);
3251 return file->private_data;
3254 static int perf_event_set_output(struct perf_event *event,
3255 struct perf_event *output_event);
3256 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3258 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3260 struct perf_event *event = file->private_data;
3261 void (*func)(struct perf_event *);
3265 case PERF_EVENT_IOC_ENABLE:
3266 func = perf_event_enable;
3268 case PERF_EVENT_IOC_DISABLE:
3269 func = perf_event_disable;
3271 case PERF_EVENT_IOC_RESET:
3272 func = perf_event_reset;
3275 case PERF_EVENT_IOC_REFRESH:
3276 return perf_event_refresh(event, arg);
3278 case PERF_EVENT_IOC_PERIOD:
3279 return perf_event_period(event, (u64 __user *)arg);
3281 case PERF_EVENT_IOC_SET_OUTPUT:
3283 struct perf_event *output_event = NULL;
3284 int fput_needed = 0;
3288 output_event = perf_fget_light(arg, &fput_needed);
3289 if (IS_ERR(output_event))
3290 return PTR_ERR(output_event);
3293 ret = perf_event_set_output(event, output_event);
3295 fput_light(output_event->filp, fput_needed);
3300 case PERF_EVENT_IOC_SET_FILTER:
3301 return perf_event_set_filter(event, (void __user *)arg);
3307 if (flags & PERF_IOC_FLAG_GROUP)
3308 perf_event_for_each(event, func);
3310 perf_event_for_each_child(event, func);
3315 int perf_event_task_enable(void)
3317 struct perf_event *event;
3319 mutex_lock(¤t->perf_event_mutex);
3320 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3321 perf_event_for_each_child(event, perf_event_enable);
3322 mutex_unlock(¤t->perf_event_mutex);
3327 int perf_event_task_disable(void)
3329 struct perf_event *event;
3331 mutex_lock(¤t->perf_event_mutex);
3332 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3333 perf_event_for_each_child(event, perf_event_disable);
3334 mutex_unlock(¤t->perf_event_mutex);
3339 #ifndef PERF_EVENT_INDEX_OFFSET
3340 # define PERF_EVENT_INDEX_OFFSET 0
3343 static int perf_event_index(struct perf_event *event)
3345 if (event->hw.state & PERF_HES_STOPPED)
3348 if (event->state != PERF_EVENT_STATE_ACTIVE)
3351 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
3354 static void calc_timer_values(struct perf_event *event,
3361 ctx_time = event->shadow_ctx_time + now;
3362 *enabled = ctx_time - event->tstamp_enabled;
3363 *running = ctx_time - event->tstamp_running;
3367 * Callers need to ensure there can be no nesting of this function, otherwise
3368 * the seqlock logic goes bad. We can not serialize this because the arch
3369 * code calls this from NMI context.
3371 void perf_event_update_userpage(struct perf_event *event)
3373 struct perf_event_mmap_page *userpg;
3374 struct ring_buffer *rb;
3375 u64 enabled, running;
3379 * compute total_time_enabled, total_time_running
3380 * based on snapshot values taken when the event
3381 * was last scheduled in.
3383 * we cannot simply called update_context_time()
3384 * because of locking issue as we can be called in
3387 calc_timer_values(event, &enabled, &running);
3388 rb = rcu_dereference(event->rb);
3392 userpg = rb->user_page;
3395 * Disable preemption so as to not let the corresponding user-space
3396 * spin too long if we get preempted.
3401 userpg->index = perf_event_index(event);
3402 userpg->offset = perf_event_count(event);
3403 if (event->state == PERF_EVENT_STATE_ACTIVE)
3404 userpg->offset -= local64_read(&event->hw.prev_count);
3406 userpg->time_enabled = enabled +
3407 atomic64_read(&event->child_total_time_enabled);
3409 userpg->time_running = running +
3410 atomic64_read(&event->child_total_time_running);
3419 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3421 struct perf_event *event = vma->vm_file->private_data;
3422 struct ring_buffer *rb;
3423 int ret = VM_FAULT_SIGBUS;
3425 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3426 if (vmf->pgoff == 0)
3432 rb = rcu_dereference(event->rb);
3436 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3439 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
3443 get_page(vmf->page);
3444 vmf->page->mapping = vma->vm_file->f_mapping;
3445 vmf->page->index = vmf->pgoff;
3454 static void rb_free_rcu(struct rcu_head *rcu_head)
3456 struct ring_buffer *rb;
3458 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3462 static struct ring_buffer *ring_buffer_get(struct perf_event *event)
3464 struct ring_buffer *rb;
3467 rb = rcu_dereference(event->rb);
3469 if (!atomic_inc_not_zero(&rb->refcount))
3477 static void ring_buffer_put(struct ring_buffer *rb)
3479 if (!atomic_dec_and_test(&rb->refcount))
3482 call_rcu(&rb->rcu_head, rb_free_rcu);
3485 static void perf_mmap_open(struct vm_area_struct *vma)
3487 struct perf_event *event = vma->vm_file->private_data;
3489 atomic_inc(&event->mmap_count);
3492 static void perf_mmap_close(struct vm_area_struct *vma)
3494 struct perf_event *event = vma->vm_file->private_data;
3496 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
3497 unsigned long size = perf_data_size(event->rb);
3498 struct user_struct *user = event->mmap_user;
3499 struct ring_buffer *rb = event->rb;
3501 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
3502 vma->vm_mm->locked_vm -= event->mmap_locked;
3503 rcu_assign_pointer(event->rb, NULL);
3504 mutex_unlock(&event->mmap_mutex);
3506 ring_buffer_put(rb);
3511 static const struct vm_operations_struct perf_mmap_vmops = {
3512 .open = perf_mmap_open,
3513 .close = perf_mmap_close,
3514 .fault = perf_mmap_fault,
3515 .page_mkwrite = perf_mmap_fault,
3518 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3520 struct perf_event *event = file->private_data;
3521 unsigned long user_locked, user_lock_limit;
3522 struct user_struct *user = current_user();
3523 unsigned long locked, lock_limit;
3524 struct ring_buffer *rb;
3525 unsigned long vma_size;
3526 unsigned long nr_pages;
3527 long user_extra, extra;
3528 int ret = 0, flags = 0;
3531 * Don't allow mmap() of inherited per-task counters. This would
3532 * create a performance issue due to all children writing to the
3535 if (event->cpu == -1 && event->attr.inherit)
3538 if (!(vma->vm_flags & VM_SHARED))
3541 vma_size = vma->vm_end - vma->vm_start;
3542 nr_pages = (vma_size / PAGE_SIZE) - 1;
3545 * If we have rb pages ensure they're a power-of-two number, so we
3546 * can do bitmasks instead of modulo.
3548 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3551 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3554 if (vma->vm_pgoff != 0)
3557 WARN_ON_ONCE(event->ctx->parent_ctx);
3558 mutex_lock(&event->mmap_mutex);
3560 if (event->rb->nr_pages == nr_pages)
3561 atomic_inc(&event->rb->refcount);
3567 user_extra = nr_pages + 1;
3568 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3571 * Increase the limit linearly with more CPUs:
3573 user_lock_limit *= num_online_cpus();
3575 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3578 if (user_locked > user_lock_limit)
3579 extra = user_locked - user_lock_limit;
3581 lock_limit = rlimit(RLIMIT_MEMLOCK);
3582 lock_limit >>= PAGE_SHIFT;
3583 locked = vma->vm_mm->locked_vm + extra;
3585 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3586 !capable(CAP_IPC_LOCK)) {
3593 if (vma->vm_flags & VM_WRITE)
3594 flags |= RING_BUFFER_WRITABLE;
3596 rb = rb_alloc(nr_pages,
3597 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3604 rcu_assign_pointer(event->rb, rb);
3606 atomic_long_add(user_extra, &user->locked_vm);
3607 event->mmap_locked = extra;
3608 event->mmap_user = get_current_user();
3609 vma->vm_mm->locked_vm += event->mmap_locked;
3613 atomic_inc(&event->mmap_count);
3614 mutex_unlock(&event->mmap_mutex);
3616 vma->vm_flags |= VM_RESERVED;
3617 vma->vm_ops = &perf_mmap_vmops;
3622 static int perf_fasync(int fd, struct file *filp, int on)
3624 struct inode *inode = filp->f_path.dentry->d_inode;
3625 struct perf_event *event = filp->private_data;
3628 mutex_lock(&inode->i_mutex);
3629 retval = fasync_helper(fd, filp, on, &event->fasync);
3630 mutex_unlock(&inode->i_mutex);
3638 static const struct file_operations perf_fops = {
3639 .llseek = no_llseek,
3640 .release = perf_release,
3643 .unlocked_ioctl = perf_ioctl,
3644 .compat_ioctl = perf_ioctl,
3646 .fasync = perf_fasync,
3652 * If there's data, ensure we set the poll() state and publish everything
3653 * to user-space before waking everybody up.
3656 void perf_event_wakeup(struct perf_event *event)
3658 wake_up_all(&event->waitq);
3660 if (event->pending_kill) {
3661 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3662 event->pending_kill = 0;
3666 static void perf_pending_event(struct irq_work *entry)
3668 struct perf_event *event = container_of(entry,
3669 struct perf_event, pending);
3671 if (event->pending_disable) {
3672 event->pending_disable = 0;
3673 __perf_event_disable(event);
3676 if (event->pending_wakeup) {
3677 event->pending_wakeup = 0;
3678 perf_event_wakeup(event);
3683 * We assume there is only KVM supporting the callbacks.
3684 * Later on, we might change it to a list if there is
3685 * another virtualization implementation supporting the callbacks.
3687 struct perf_guest_info_callbacks *perf_guest_cbs;
3689 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3691 perf_guest_cbs = cbs;
3694 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3696 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3698 perf_guest_cbs = NULL;
3701 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3703 static void __perf_event_header__init_id(struct perf_event_header *header,
3704 struct perf_sample_data *data,
3705 struct perf_event *event)
3707 u64 sample_type = event->attr.sample_type;
3709 data->type = sample_type;
3710 header->size += event->id_header_size;
3712 if (sample_type & PERF_SAMPLE_TID) {
3713 /* namespace issues */
3714 data->tid_entry.pid = perf_event_pid(event, current);
3715 data->tid_entry.tid = perf_event_tid(event, current);
3718 if (sample_type & PERF_SAMPLE_TIME)
3719 data->time = perf_clock();
3721 if (sample_type & PERF_SAMPLE_ID)
3722 data->id = primary_event_id(event);
3724 if (sample_type & PERF_SAMPLE_STREAM_ID)
3725 data->stream_id = event->id;
3727 if (sample_type & PERF_SAMPLE_CPU) {
3728 data->cpu_entry.cpu = raw_smp_processor_id();
3729 data->cpu_entry.reserved = 0;
3733 void perf_event_header__init_id(struct perf_event_header *header,
3734 struct perf_sample_data *data,
3735 struct perf_event *event)
3737 if (event->attr.sample_id_all)
3738 __perf_event_header__init_id(header, data, event);
3741 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3742 struct perf_sample_data *data)
3744 u64 sample_type = data->type;
3746 if (sample_type & PERF_SAMPLE_TID)
3747 perf_output_put(handle, data->tid_entry);
3749 if (sample_type & PERF_SAMPLE_TIME)
3750 perf_output_put(handle, data->time);
3752 if (sample_type & PERF_SAMPLE_ID)
3753 perf_output_put(handle, data->id);
3755 if (sample_type & PERF_SAMPLE_STREAM_ID)
3756 perf_output_put(handle, data->stream_id);
3758 if (sample_type & PERF_SAMPLE_CPU)
3759 perf_output_put(handle, data->cpu_entry);
3762 void perf_event__output_id_sample(struct perf_event *event,
3763 struct perf_output_handle *handle,
3764 struct perf_sample_data *sample)
3766 if (event->attr.sample_id_all)
3767 __perf_event__output_id_sample(handle, sample);
3770 static void perf_output_read_one(struct perf_output_handle *handle,
3771 struct perf_event *event,
3772 u64 enabled, u64 running)
3774 u64 read_format = event->attr.read_format;
3778 values[n++] = perf_event_count(event);
3779 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3780 values[n++] = enabled +
3781 atomic64_read(&event->child_total_time_enabled);
3783 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3784 values[n++] = running +
3785 atomic64_read(&event->child_total_time_running);
3787 if (read_format & PERF_FORMAT_ID)
3788 values[n++] = primary_event_id(event);
3790 __output_copy(handle, values, n * sizeof(u64));
3794 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3796 static void perf_output_read_group(struct perf_output_handle *handle,
3797 struct perf_event *event,
3798 u64 enabled, u64 running)
3800 struct perf_event *leader = event->group_leader, *sub;
3801 u64 read_format = event->attr.read_format;
3805 values[n++] = 1 + leader->nr_siblings;
3807 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3808 values[n++] = enabled;
3810 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3811 values[n++] = running;
3813 if (leader != event)
3814 leader->pmu->read(leader);
3816 values[n++] = perf_event_count(leader);
3817 if (read_format & PERF_FORMAT_ID)
3818 values[n++] = primary_event_id(leader);
3820 __output_copy(handle, values, n * sizeof(u64));
3822 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3826 sub->pmu->read(sub);
3828 values[n++] = perf_event_count(sub);
3829 if (read_format & PERF_FORMAT_ID)
3830 values[n++] = primary_event_id(sub);
3832 __output_copy(handle, values, n * sizeof(u64));
3836 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
3837 PERF_FORMAT_TOTAL_TIME_RUNNING)
3839 static void perf_output_read(struct perf_output_handle *handle,
3840 struct perf_event *event)
3842 u64 enabled = 0, running = 0;
3843 u64 read_format = event->attr.read_format;
3846 * compute total_time_enabled, total_time_running
3847 * based on snapshot values taken when the event
3848 * was last scheduled in.
3850 * we cannot simply called update_context_time()
3851 * because of locking issue as we are called in
3854 if (read_format & PERF_FORMAT_TOTAL_TIMES)
3855 calc_timer_values(event, &enabled, &running);
3857 if (event->attr.read_format & PERF_FORMAT_GROUP)
3858 perf_output_read_group(handle, event, enabled, running);
3860 perf_output_read_one(handle, event, enabled, running);
3863 void perf_output_sample(struct perf_output_handle *handle,
3864 struct perf_event_header *header,
3865 struct perf_sample_data *data,
3866 struct perf_event *event)
3868 u64 sample_type = data->type;
3870 perf_output_put(handle, *header);
3872 if (sample_type & PERF_SAMPLE_IP)
3873 perf_output_put(handle, data->ip);
3875 if (sample_type & PERF_SAMPLE_TID)
3876 perf_output_put(handle, data->tid_entry);
3878 if (sample_type & PERF_SAMPLE_TIME)
3879 perf_output_put(handle, data->time);
3881 if (sample_type & PERF_SAMPLE_ADDR)
3882 perf_output_put(handle, data->addr);
3884 if (sample_type & PERF_SAMPLE_ID)
3885 perf_output_put(handle, data->id);
3887 if (sample_type & PERF_SAMPLE_STREAM_ID)
3888 perf_output_put(handle, data->stream_id);
3890 if (sample_type & PERF_SAMPLE_CPU)
3891 perf_output_put(handle, data->cpu_entry);
3893 if (sample_type & PERF_SAMPLE_PERIOD)
3894 perf_output_put(handle, data->period);
3896 if (sample_type & PERF_SAMPLE_READ)
3897 perf_output_read(handle, event);
3899 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3900 if (data->callchain) {
3903 if (data->callchain)
3904 size += data->callchain->nr;
3906 size *= sizeof(u64);
3908 __output_copy(handle, data->callchain, size);
3911 perf_output_put(handle, nr);
3915 if (sample_type & PERF_SAMPLE_RAW) {
3917 perf_output_put(handle, data->raw->size);
3918 __output_copy(handle, data->raw->data,
3925 .size = sizeof(u32),
3928 perf_output_put(handle, raw);
3932 if (!event->attr.watermark) {
3933 int wakeup_events = event->attr.wakeup_events;
3935 if (wakeup_events) {
3936 struct ring_buffer *rb = handle->rb;
3937 int events = local_inc_return(&rb->events);
3939 if (events >= wakeup_events) {
3940 local_sub(wakeup_events, &rb->events);
3941 local_inc(&rb->wakeup);
3947 void perf_prepare_sample(struct perf_event_header *header,
3948 struct perf_sample_data *data,
3949 struct perf_event *event,
3950 struct pt_regs *regs)
3952 u64 sample_type = event->attr.sample_type;
3954 header->type = PERF_RECORD_SAMPLE;
3955 header->size = sizeof(*header) + event->header_size;
3958 header->misc |= perf_misc_flags(regs);
3960 __perf_event_header__init_id(header, data, event);
3962 if (sample_type & PERF_SAMPLE_IP)
3963 data->ip = perf_instruction_pointer(regs);
3965 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3968 data->callchain = perf_callchain(regs);
3970 if (data->callchain)
3971 size += data->callchain->nr;
3973 header->size += size * sizeof(u64);
3976 if (sample_type & PERF_SAMPLE_RAW) {
3977 int size = sizeof(u32);
3980 size += data->raw->size;
3982 size += sizeof(u32);
3984 WARN_ON_ONCE(size & (sizeof(u64)-1));
3985 header->size += size;
3989 static void perf_event_output(struct perf_event *event,
3990 struct perf_sample_data *data,
3991 struct pt_regs *regs)
3993 struct perf_output_handle handle;
3994 struct perf_event_header header;
3996 /* protect the callchain buffers */
3999 perf_prepare_sample(&header, data, event, regs);
4001 if (perf_output_begin(&handle, event, header.size))
4004 perf_output_sample(&handle, &header, data, event);
4006 perf_output_end(&handle);
4016 struct perf_read_event {
4017 struct perf_event_header header;
4024 perf_event_read_event(struct perf_event *event,
4025 struct task_struct *task)
4027 struct perf_output_handle handle;
4028 struct perf_sample_data sample;
4029 struct perf_read_event read_event = {
4031 .type = PERF_RECORD_READ,
4033 .size = sizeof(read_event) + event->read_size,
4035 .pid = perf_event_pid(event, task),
4036 .tid = perf_event_tid(event, task),
4040 perf_event_header__init_id(&read_event.header, &sample, event);
4041 ret = perf_output_begin(&handle, event, read_event.header.size);
4045 perf_output_put(&handle, read_event);
4046 perf_output_read(&handle, event);
4047 perf_event__output_id_sample(event, &handle, &sample);
4049 perf_output_end(&handle);
4053 * task tracking -- fork/exit
4055 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4058 struct perf_task_event {
4059 struct task_struct *task;
4060 struct perf_event_context *task_ctx;
4063 struct perf_event_header header;
4073 static void perf_event_task_output(struct perf_event *event,
4074 struct perf_task_event *task_event)
4076 struct perf_output_handle handle;
4077 struct perf_sample_data sample;
4078 struct task_struct *task = task_event->task;
4079 int ret, size = task_event->event_id.header.size;
4081 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4083 ret = perf_output_begin(&handle, event,
4084 task_event->event_id.header.size);
4088 task_event->event_id.pid = perf_event_pid(event, task);
4089 task_event->event_id.ppid = perf_event_pid(event, current);
4091 task_event->event_id.tid = perf_event_tid(event, task);
4092 task_event->event_id.ptid = perf_event_tid(event, current);
4094 perf_output_put(&handle, task_event->event_id);
4096 perf_event__output_id_sample(event, &handle, &sample);
4098 perf_output_end(&handle);
4100 task_event->event_id.header.size = size;
4103 static int perf_event_task_match(struct perf_event *event)
4105 if (event->state < PERF_EVENT_STATE_INACTIVE)
4108 if (!event_filter_match(event))
4111 if (event->attr.comm || event->attr.mmap ||
4112 event->attr.mmap_data || event->attr.task)
4118 static void perf_event_task_ctx(struct perf_event_context *ctx,
4119 struct perf_task_event *task_event)
4121 struct perf_event *event;
4123 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4124 if (perf_event_task_match(event))
4125 perf_event_task_output(event, task_event);
4129 static void perf_event_task_event(struct perf_task_event *task_event)
4131 struct perf_cpu_context *cpuctx;
4132 struct perf_event_context *ctx;
4137 list_for_each_entry_rcu(pmu, &pmus, entry) {
4138 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4139 if (cpuctx->active_pmu != pmu)
4141 perf_event_task_ctx(&cpuctx->ctx, task_event);
4143 ctx = task_event->task_ctx;
4145 ctxn = pmu->task_ctx_nr;
4148 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4151 perf_event_task_ctx(ctx, task_event);
4153 put_cpu_ptr(pmu->pmu_cpu_context);
4158 static void perf_event_task(struct task_struct *task,
4159 struct perf_event_context *task_ctx,
4162 struct perf_task_event task_event;
4164 if (!atomic_read(&nr_comm_events) &&
4165 !atomic_read(&nr_mmap_events) &&
4166 !atomic_read(&nr_task_events))
4169 task_event = (struct perf_task_event){
4171 .task_ctx = task_ctx,
4174 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4176 .size = sizeof(task_event.event_id),
4182 .time = perf_clock(),
4186 perf_event_task_event(&task_event);
4189 void perf_event_fork(struct task_struct *task)
4191 perf_event_task(task, NULL, 1);
4198 struct perf_comm_event {
4199 struct task_struct *task;
4204 struct perf_event_header header;
4211 static void perf_event_comm_output(struct perf_event *event,
4212 struct perf_comm_event *comm_event)
4214 struct perf_output_handle handle;
4215 struct perf_sample_data sample;
4216 int size = comm_event->event_id.header.size;
4219 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4220 ret = perf_output_begin(&handle, event,
4221 comm_event->event_id.header.size);
4226 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4227 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4229 perf_output_put(&handle, comm_event->event_id);
4230 __output_copy(&handle, comm_event->comm,
4231 comm_event->comm_size);
4233 perf_event__output_id_sample(event, &handle, &sample);
4235 perf_output_end(&handle);
4237 comm_event->event_id.header.size = size;
4240 static int perf_event_comm_match(struct perf_event *event)
4242 if (event->state < PERF_EVENT_STATE_INACTIVE)
4245 if (!event_filter_match(event))
4248 if (event->attr.comm)
4254 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4255 struct perf_comm_event *comm_event)
4257 struct perf_event *event;
4259 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4260 if (perf_event_comm_match(event))
4261 perf_event_comm_output(event, comm_event);
4265 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4267 struct perf_cpu_context *cpuctx;
4268 struct perf_event_context *ctx;
4269 char comm[TASK_COMM_LEN];
4274 memset(comm, 0, sizeof(comm));
4275 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4276 size = ALIGN(strlen(comm)+1, sizeof(u64));
4278 comm_event->comm = comm;
4279 comm_event->comm_size = size;
4281 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4283 list_for_each_entry_rcu(pmu, &pmus, entry) {
4284 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4285 if (cpuctx->active_pmu != pmu)
4287 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4289 ctxn = pmu->task_ctx_nr;
4293 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4295 perf_event_comm_ctx(ctx, comm_event);
4297 put_cpu_ptr(pmu->pmu_cpu_context);
4302 void perf_event_comm(struct task_struct *task)
4304 struct perf_comm_event comm_event;
4305 struct perf_event_context *ctx;
4308 for_each_task_context_nr(ctxn) {
4309 ctx = task->perf_event_ctxp[ctxn];
4313 perf_event_enable_on_exec(ctx);
4316 if (!atomic_read(&nr_comm_events))
4319 comm_event = (struct perf_comm_event){
4325 .type = PERF_RECORD_COMM,
4334 perf_event_comm_event(&comm_event);
4341 struct perf_mmap_event {
4342 struct vm_area_struct *vma;
4344 const char *file_name;
4348 struct perf_event_header header;
4358 static void perf_event_mmap_output(struct perf_event *event,
4359 struct perf_mmap_event *mmap_event)
4361 struct perf_output_handle handle;
4362 struct perf_sample_data sample;
4363 int size = mmap_event->event_id.header.size;
4366 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4367 ret = perf_output_begin(&handle, event,
4368 mmap_event->event_id.header.size);
4372 mmap_event->event_id.pid = perf_event_pid(event, current);
4373 mmap_event->event_id.tid = perf_event_tid(event, current);
4375 perf_output_put(&handle, mmap_event->event_id);
4376 __output_copy(&handle, mmap_event->file_name,
4377 mmap_event->file_size);
4379 perf_event__output_id_sample(event, &handle, &sample);
4381 perf_output_end(&handle);
4383 mmap_event->event_id.header.size = size;
4386 static int perf_event_mmap_match(struct perf_event *event,
4387 struct perf_mmap_event *mmap_event,
4390 if (event->state < PERF_EVENT_STATE_INACTIVE)
4393 if (!event_filter_match(event))
4396 if ((!executable && event->attr.mmap_data) ||
4397 (executable && event->attr.mmap))
4403 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4404 struct perf_mmap_event *mmap_event,
4407 struct perf_event *event;
4409 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4410 if (perf_event_mmap_match(event, mmap_event, executable))
4411 perf_event_mmap_output(event, mmap_event);
4415 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4417 struct perf_cpu_context *cpuctx;
4418 struct perf_event_context *ctx;
4419 struct vm_area_struct *vma = mmap_event->vma;
4420 struct file *file = vma->vm_file;
4428 memset(tmp, 0, sizeof(tmp));
4432 * d_path works from the end of the rb backwards, so we
4433 * need to add enough zero bytes after the string to handle
4434 * the 64bit alignment we do later.
4436 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4438 name = strncpy(tmp, "//enomem", sizeof(tmp));
4441 name = d_path(&file->f_path, buf, PATH_MAX);
4443 name = strncpy(tmp, "//toolong", sizeof(tmp));
4447 if (arch_vma_name(mmap_event->vma)) {
4448 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4454 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4456 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4457 vma->vm_end >= vma->vm_mm->brk) {
4458 name = strncpy(tmp, "[heap]", sizeof(tmp));
4460 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4461 vma->vm_end >= vma->vm_mm->start_stack) {
4462 name = strncpy(tmp, "[stack]", sizeof(tmp));
4466 name = strncpy(tmp, "//anon", sizeof(tmp));
4471 size = ALIGN(strlen(name)+1, sizeof(u64));
4473 mmap_event->file_name = name;
4474 mmap_event->file_size = size;
4476 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4479 list_for_each_entry_rcu(pmu, &pmus, entry) {
4480 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4481 if (cpuctx->active_pmu != pmu)
4483 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4484 vma->vm_flags & VM_EXEC);
4486 ctxn = pmu->task_ctx_nr;
4490 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4492 perf_event_mmap_ctx(ctx, mmap_event,
4493 vma->vm_flags & VM_EXEC);
4496 put_cpu_ptr(pmu->pmu_cpu_context);
4503 void perf_event_mmap(struct vm_area_struct *vma)
4505 struct perf_mmap_event mmap_event;
4507 if (!atomic_read(&nr_mmap_events))
4510 mmap_event = (struct perf_mmap_event){
4516 .type = PERF_RECORD_MMAP,
4517 .misc = PERF_RECORD_MISC_USER,
4522 .start = vma->vm_start,
4523 .len = vma->vm_end - vma->vm_start,
4524 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4528 perf_event_mmap_event(&mmap_event);
4532 * IRQ throttle logging
4535 static void perf_log_throttle(struct perf_event *event, int enable)
4537 struct perf_output_handle handle;
4538 struct perf_sample_data sample;
4542 struct perf_event_header header;
4546 } throttle_event = {
4548 .type = PERF_RECORD_THROTTLE,
4550 .size = sizeof(throttle_event),
4552 .time = perf_clock(),
4553 .id = primary_event_id(event),
4554 .stream_id = event->id,
4558 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4560 perf_event_header__init_id(&throttle_event.header, &sample, event);
4562 ret = perf_output_begin(&handle, event,
4563 throttle_event.header.size);
4567 perf_output_put(&handle, throttle_event);
4568 perf_event__output_id_sample(event, &handle, &sample);
4569 perf_output_end(&handle);
4573 * Generic event overflow handling, sampling.
4576 static int __perf_event_overflow(struct perf_event *event,
4577 int throttle, struct perf_sample_data *data,
4578 struct pt_regs *regs)
4580 int events = atomic_read(&event->event_limit);
4581 struct hw_perf_event *hwc = &event->hw;
4585 * Non-sampling counters might still use the PMI to fold short
4586 * hardware counters, ignore those.
4588 if (unlikely(!is_sampling_event(event)))
4591 if (unlikely(hwc->interrupts >= max_samples_per_tick)) {
4593 hwc->interrupts = MAX_INTERRUPTS;
4594 perf_log_throttle(event, 0);
4600 if (event->attr.freq) {
4601 u64 now = perf_clock();
4602 s64 delta = now - hwc->freq_time_stamp;
4604 hwc->freq_time_stamp = now;
4606 if (delta > 0 && delta < 2*TICK_NSEC)
4607 perf_adjust_period(event, delta, hwc->last_period);
4611 * XXX event_limit might not quite work as expected on inherited
4615 event->pending_kill = POLL_IN;
4616 if (events && atomic_dec_and_test(&event->event_limit)) {
4618 event->pending_kill = POLL_HUP;
4619 event->pending_disable = 1;
4620 irq_work_queue(&event->pending);
4623 if (event->overflow_handler)
4624 event->overflow_handler(event, data, regs);
4626 perf_event_output(event, data, regs);
4628 if (event->fasync && event->pending_kill) {
4629 event->pending_wakeup = 1;
4630 irq_work_queue(&event->pending);
4636 int perf_event_overflow(struct perf_event *event,
4637 struct perf_sample_data *data,
4638 struct pt_regs *regs)
4640 return __perf_event_overflow(event, 1, data, regs);
4644 * Generic software event infrastructure
4647 struct swevent_htable {
4648 struct swevent_hlist *swevent_hlist;
4649 struct mutex hlist_mutex;
4652 /* Recursion avoidance in each contexts */
4653 int recursion[PERF_NR_CONTEXTS];
4656 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4659 * We directly increment event->count and keep a second value in
4660 * event->hw.period_left to count intervals. This period event
4661 * is kept in the range [-sample_period, 0] so that we can use the
4665 static u64 perf_swevent_set_period(struct perf_event *event)
4667 struct hw_perf_event *hwc = &event->hw;
4668 u64 period = hwc->last_period;
4672 hwc->last_period = hwc->sample_period;
4675 old = val = local64_read(&hwc->period_left);
4679 nr = div64_u64(period + val, period);
4680 offset = nr * period;
4682 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4688 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4689 struct perf_sample_data *data,
4690 struct pt_regs *regs)
4692 struct hw_perf_event *hwc = &event->hw;
4695 data->period = event->hw.last_period;
4697 overflow = perf_swevent_set_period(event);
4699 if (hwc->interrupts == MAX_INTERRUPTS)
4702 for (; overflow; overflow--) {
4703 if (__perf_event_overflow(event, throttle,
4706 * We inhibit the overflow from happening when
4707 * hwc->interrupts == MAX_INTERRUPTS.
4715 static void perf_swevent_event(struct perf_event *event, u64 nr,
4716 struct perf_sample_data *data,
4717 struct pt_regs *regs)
4719 struct hw_perf_event *hwc = &event->hw;
4721 local64_add(nr, &event->count);
4726 if (!is_sampling_event(event))
4729 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4730 return perf_swevent_overflow(event, 1, data, regs);
4732 if (local64_add_negative(nr, &hwc->period_left))
4735 perf_swevent_overflow(event, 0, data, regs);
4738 static int perf_exclude_event(struct perf_event *event,
4739 struct pt_regs *regs)
4741 if (event->hw.state & PERF_HES_STOPPED)
4745 if (event->attr.exclude_user && user_mode(regs))
4748 if (event->attr.exclude_kernel && !user_mode(regs))
4755 static int perf_swevent_match(struct perf_event *event,
4756 enum perf_type_id type,
4758 struct perf_sample_data *data,
4759 struct pt_regs *regs)
4761 if (event->attr.type != type)
4764 if (event->attr.config != event_id)
4767 if (perf_exclude_event(event, regs))
4773 static inline u64 swevent_hash(u64 type, u32 event_id)
4775 u64 val = event_id | (type << 32);
4777 return hash_64(val, SWEVENT_HLIST_BITS);
4780 static inline struct hlist_head *
4781 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
4783 u64 hash = swevent_hash(type, event_id);
4785 return &hlist->heads[hash];
4788 /* For the read side: events when they trigger */
4789 static inline struct hlist_head *
4790 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
4792 struct swevent_hlist *hlist;
4794 hlist = rcu_dereference(swhash->swevent_hlist);
4798 return __find_swevent_head(hlist, type, event_id);
4801 /* For the event head insertion and removal in the hlist */
4802 static inline struct hlist_head *
4803 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
4805 struct swevent_hlist *hlist;
4806 u32 event_id = event->attr.config;
4807 u64 type = event->attr.type;
4810 * Event scheduling is always serialized against hlist allocation
4811 * and release. Which makes the protected version suitable here.
4812 * The context lock guarantees that.
4814 hlist = rcu_dereference_protected(swhash->swevent_hlist,
4815 lockdep_is_held(&event->ctx->lock));
4819 return __find_swevent_head(hlist, type, event_id);
4822 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4824 struct perf_sample_data *data,
4825 struct pt_regs *regs)
4827 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4828 struct perf_event *event;
4829 struct hlist_node *node;
4830 struct hlist_head *head;
4833 head = find_swevent_head_rcu(swhash, type, event_id);
4837 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4838 if (perf_swevent_match(event, type, event_id, data, regs))
4839 perf_swevent_event(event, nr, data, regs);
4845 int perf_swevent_get_recursion_context(void)
4847 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4849 return get_recursion_context(swhash->recursion);
4851 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
4853 inline void perf_swevent_put_recursion_context(int rctx)
4855 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4857 put_recursion_context(swhash->recursion, rctx);
4860 void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
4862 struct perf_sample_data data;
4865 preempt_disable_notrace();
4866 rctx = perf_swevent_get_recursion_context();
4870 perf_sample_data_init(&data, addr);
4872 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
4874 perf_swevent_put_recursion_context(rctx);
4875 preempt_enable_notrace();
4878 static void perf_swevent_read(struct perf_event *event)
4882 static int perf_swevent_add(struct perf_event *event, int flags)
4884 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4885 struct hw_perf_event *hwc = &event->hw;
4886 struct hlist_head *head;
4888 if (is_sampling_event(event)) {
4889 hwc->last_period = hwc->sample_period;
4890 perf_swevent_set_period(event);
4893 hwc->state = !(flags & PERF_EF_START);
4895 head = find_swevent_head(swhash, event);
4896 if (WARN_ON_ONCE(!head))
4899 hlist_add_head_rcu(&event->hlist_entry, head);
4904 static void perf_swevent_del(struct perf_event *event, int flags)
4906 hlist_del_rcu(&event->hlist_entry);
4909 static void perf_swevent_start(struct perf_event *event, int flags)
4911 event->hw.state = 0;
4914 static void perf_swevent_stop(struct perf_event *event, int flags)
4916 event->hw.state = PERF_HES_STOPPED;
4919 /* Deref the hlist from the update side */
4920 static inline struct swevent_hlist *
4921 swevent_hlist_deref(struct swevent_htable *swhash)
4923 return rcu_dereference_protected(swhash->swevent_hlist,
4924 lockdep_is_held(&swhash->hlist_mutex));
4927 static void swevent_hlist_release(struct swevent_htable *swhash)
4929 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
4934 rcu_assign_pointer(swhash->swevent_hlist, NULL);
4935 kfree_rcu(hlist, rcu_head);
4938 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4940 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4942 mutex_lock(&swhash->hlist_mutex);
4944 if (!--swhash->hlist_refcount)
4945 swevent_hlist_release(swhash);
4947 mutex_unlock(&swhash->hlist_mutex);
4950 static void swevent_hlist_put(struct perf_event *event)
4954 if (event->cpu != -1) {
4955 swevent_hlist_put_cpu(event, event->cpu);
4959 for_each_possible_cpu(cpu)
4960 swevent_hlist_put_cpu(event, cpu);
4963 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4965 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4968 mutex_lock(&swhash->hlist_mutex);
4970 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
4971 struct swevent_hlist *hlist;
4973 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4978 rcu_assign_pointer(swhash->swevent_hlist, hlist);
4980 swhash->hlist_refcount++;
4982 mutex_unlock(&swhash->hlist_mutex);
4987 static int swevent_hlist_get(struct perf_event *event)
4990 int cpu, failed_cpu;
4992 if (event->cpu != -1)
4993 return swevent_hlist_get_cpu(event, event->cpu);
4996 for_each_possible_cpu(cpu) {
4997 err = swevent_hlist_get_cpu(event, cpu);
5007 for_each_possible_cpu(cpu) {
5008 if (cpu == failed_cpu)
5010 swevent_hlist_put_cpu(event, cpu);
5017 struct jump_label_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
5019 static void sw_perf_event_destroy(struct perf_event *event)
5021 u64 event_id = event->attr.config;
5023 WARN_ON(event->parent);
5025 jump_label_dec(&perf_swevent_enabled[event_id]);
5026 swevent_hlist_put(event);
5029 static int perf_swevent_init(struct perf_event *event)
5031 int event_id = event->attr.config;
5033 if (event->attr.type != PERF_TYPE_SOFTWARE)
5037 case PERF_COUNT_SW_CPU_CLOCK:
5038 case PERF_COUNT_SW_TASK_CLOCK:
5045 if (event_id >= PERF_COUNT_SW_MAX)
5048 if (!event->parent) {
5051 err = swevent_hlist_get(event);
5055 jump_label_inc(&perf_swevent_enabled[event_id]);
5056 event->destroy = sw_perf_event_destroy;
5062 static struct pmu perf_swevent = {
5063 .task_ctx_nr = perf_sw_context,
5065 .event_init = perf_swevent_init,
5066 .add = perf_swevent_add,
5067 .del = perf_swevent_del,
5068 .start = perf_swevent_start,
5069 .stop = perf_swevent_stop,
5070 .read = perf_swevent_read,
5073 #ifdef CONFIG_EVENT_TRACING
5075 static int perf_tp_filter_match(struct perf_event *event,
5076 struct perf_sample_data *data)
5078 void *record = data->raw->data;
5080 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5085 static int perf_tp_event_match(struct perf_event *event,
5086 struct perf_sample_data *data,
5087 struct pt_regs *regs)
5089 if (event->hw.state & PERF_HES_STOPPED)
5092 * All tracepoints are from kernel-space.
5094 if (event->attr.exclude_kernel)
5097 if (!perf_tp_filter_match(event, data))
5103 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5104 struct pt_regs *regs, struct hlist_head *head, int rctx)
5106 struct perf_sample_data data;
5107 struct perf_event *event;
5108 struct hlist_node *node;
5110 struct perf_raw_record raw = {
5115 perf_sample_data_init(&data, addr);
5118 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5119 if (perf_tp_event_match(event, &data, regs))
5120 perf_swevent_event(event, count, &data, regs);
5123 perf_swevent_put_recursion_context(rctx);
5125 EXPORT_SYMBOL_GPL(perf_tp_event);
5127 static void tp_perf_event_destroy(struct perf_event *event)
5129 perf_trace_destroy(event);
5132 static int perf_tp_event_init(struct perf_event *event)
5136 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5139 err = perf_trace_init(event);
5143 event->destroy = tp_perf_event_destroy;
5148 static struct pmu perf_tracepoint = {
5149 .task_ctx_nr = perf_sw_context,
5151 .event_init = perf_tp_event_init,
5152 .add = perf_trace_add,
5153 .del = perf_trace_del,
5154 .start = perf_swevent_start,
5155 .stop = perf_swevent_stop,
5156 .read = perf_swevent_read,
5159 static inline void perf_tp_register(void)
5161 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5164 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5169 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5172 filter_str = strndup_user(arg, PAGE_SIZE);
5173 if (IS_ERR(filter_str))
5174 return PTR_ERR(filter_str);
5176 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5182 static void perf_event_free_filter(struct perf_event *event)
5184 ftrace_profile_free_filter(event);
5189 static inline void perf_tp_register(void)
5193 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5198 static void perf_event_free_filter(struct perf_event *event)
5202 #endif /* CONFIG_EVENT_TRACING */
5204 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5205 void perf_bp_event(struct perf_event *bp, void *data)
5207 struct perf_sample_data sample;
5208 struct pt_regs *regs = data;
5210 perf_sample_data_init(&sample, bp->attr.bp_addr);
5212 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5213 perf_swevent_event(bp, 1, &sample, regs);
5218 * hrtimer based swevent callback
5221 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5223 enum hrtimer_restart ret = HRTIMER_RESTART;
5224 struct perf_sample_data data;
5225 struct pt_regs *regs;
5226 struct perf_event *event;
5229 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5231 if (event->state != PERF_EVENT_STATE_ACTIVE)
5232 return HRTIMER_NORESTART;
5234 event->pmu->read(event);
5236 perf_sample_data_init(&data, 0);
5237 data.period = event->hw.last_period;
5238 regs = get_irq_regs();
5240 if (regs && !perf_exclude_event(event, regs)) {
5241 if (!(event->attr.exclude_idle && current->pid == 0))
5242 if (perf_event_overflow(event, &data, regs))
5243 ret = HRTIMER_NORESTART;
5246 period = max_t(u64, 10000, event->hw.sample_period);
5247 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5252 static void perf_swevent_start_hrtimer(struct perf_event *event)
5254 struct hw_perf_event *hwc = &event->hw;
5257 if (!is_sampling_event(event))
5260 period = local64_read(&hwc->period_left);
5265 local64_set(&hwc->period_left, 0);
5267 period = max_t(u64, 10000, hwc->sample_period);
5269 __hrtimer_start_range_ns(&hwc->hrtimer,
5270 ns_to_ktime(period), 0,
5271 HRTIMER_MODE_REL_PINNED, 0);
5274 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5276 struct hw_perf_event *hwc = &event->hw;
5278 if (is_sampling_event(event)) {
5279 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5280 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5282 hrtimer_cancel(&hwc->hrtimer);
5286 static void perf_swevent_init_hrtimer(struct perf_event *event)
5288 struct hw_perf_event *hwc = &event->hw;
5290 if (!is_sampling_event(event))
5293 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5294 hwc->hrtimer.function = perf_swevent_hrtimer;
5297 * Since hrtimers have a fixed rate, we can do a static freq->period
5298 * mapping and avoid the whole period adjust feedback stuff.
5300 if (event->attr.freq) {
5301 long freq = event->attr.sample_freq;
5303 event->attr.sample_period = NSEC_PER_SEC / freq;
5304 hwc->sample_period = event->attr.sample_period;
5305 local64_set(&hwc->period_left, hwc->sample_period);
5306 event->attr.freq = 0;
5311 * Software event: cpu wall time clock
5314 static void cpu_clock_event_update(struct perf_event *event)
5319 now = local_clock();
5320 prev = local64_xchg(&event->hw.prev_count, now);
5321 local64_add(now - prev, &event->count);
5324 static void cpu_clock_event_start(struct perf_event *event, int flags)
5326 local64_set(&event->hw.prev_count, local_clock());
5327 perf_swevent_start_hrtimer(event);
5330 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5332 perf_swevent_cancel_hrtimer(event);
5333 cpu_clock_event_update(event);
5336 static int cpu_clock_event_add(struct perf_event *event, int flags)
5338 if (flags & PERF_EF_START)
5339 cpu_clock_event_start(event, flags);
5344 static void cpu_clock_event_del(struct perf_event *event, int flags)
5346 cpu_clock_event_stop(event, flags);
5349 static void cpu_clock_event_read(struct perf_event *event)
5351 cpu_clock_event_update(event);
5354 static int cpu_clock_event_init(struct perf_event *event)
5356 if (event->attr.type != PERF_TYPE_SOFTWARE)
5359 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5362 perf_swevent_init_hrtimer(event);
5367 static struct pmu perf_cpu_clock = {
5368 .task_ctx_nr = perf_sw_context,
5370 .event_init = cpu_clock_event_init,
5371 .add = cpu_clock_event_add,
5372 .del = cpu_clock_event_del,
5373 .start = cpu_clock_event_start,
5374 .stop = cpu_clock_event_stop,
5375 .read = cpu_clock_event_read,
5379 * Software event: task time clock
5382 static void task_clock_event_update(struct perf_event *event, u64 now)
5387 prev = local64_xchg(&event->hw.prev_count, now);
5389 local64_add(delta, &event->count);
5392 static void task_clock_event_start(struct perf_event *event, int flags)
5394 local64_set(&event->hw.prev_count, event->ctx->time);
5395 perf_swevent_start_hrtimer(event);
5398 static void task_clock_event_stop(struct perf_event *event, int flags)
5400 perf_swevent_cancel_hrtimer(event);
5401 task_clock_event_update(event, event->ctx->time);
5404 static int task_clock_event_add(struct perf_event *event, int flags)
5406 if (flags & PERF_EF_START)
5407 task_clock_event_start(event, flags);
5412 static void task_clock_event_del(struct perf_event *event, int flags)
5414 task_clock_event_stop(event, PERF_EF_UPDATE);
5417 static void task_clock_event_read(struct perf_event *event)
5419 u64 now = perf_clock();
5420 u64 delta = now - event->ctx->timestamp;
5421 u64 time = event->ctx->time + delta;
5423 task_clock_event_update(event, time);
5426 static int task_clock_event_init(struct perf_event *event)
5428 if (event->attr.type != PERF_TYPE_SOFTWARE)
5431 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5434 perf_swevent_init_hrtimer(event);
5439 static struct pmu perf_task_clock = {
5440 .task_ctx_nr = perf_sw_context,
5442 .event_init = task_clock_event_init,
5443 .add = task_clock_event_add,
5444 .del = task_clock_event_del,
5445 .start = task_clock_event_start,
5446 .stop = task_clock_event_stop,
5447 .read = task_clock_event_read,
5450 static void perf_pmu_nop_void(struct pmu *pmu)
5454 static int perf_pmu_nop_int(struct pmu *pmu)
5459 static void perf_pmu_start_txn(struct pmu *pmu)
5461 perf_pmu_disable(pmu);
5464 static int perf_pmu_commit_txn(struct pmu *pmu)
5466 perf_pmu_enable(pmu);
5470 static void perf_pmu_cancel_txn(struct pmu *pmu)
5472 perf_pmu_enable(pmu);
5476 * Ensures all contexts with the same task_ctx_nr have the same
5477 * pmu_cpu_context too.
5479 static void *find_pmu_context(int ctxn)
5486 list_for_each_entry(pmu, &pmus, entry) {
5487 if (pmu->task_ctx_nr == ctxn)
5488 return pmu->pmu_cpu_context;
5494 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5498 for_each_possible_cpu(cpu) {
5499 struct perf_cpu_context *cpuctx;
5501 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5503 if (cpuctx->active_pmu == old_pmu)
5504 cpuctx->active_pmu = pmu;
5508 static void free_pmu_context(struct pmu *pmu)
5512 mutex_lock(&pmus_lock);
5514 * Like a real lame refcount.
5516 list_for_each_entry(i, &pmus, entry) {
5517 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5518 update_pmu_context(i, pmu);
5523 free_percpu(pmu->pmu_cpu_context);
5525 mutex_unlock(&pmus_lock);
5527 static struct idr pmu_idr;
5530 type_show(struct device *dev, struct device_attribute *attr, char *page)
5532 struct pmu *pmu = dev_get_drvdata(dev);
5534 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5537 static struct device_attribute pmu_dev_attrs[] = {
5542 static int pmu_bus_running;
5543 static struct bus_type pmu_bus = {
5544 .name = "event_source",
5545 .dev_attrs = pmu_dev_attrs,
5548 static void pmu_dev_release(struct device *dev)
5553 static int pmu_dev_alloc(struct pmu *pmu)
5557 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5561 device_initialize(pmu->dev);
5562 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5566 dev_set_drvdata(pmu->dev, pmu);
5567 pmu->dev->bus = &pmu_bus;
5568 pmu->dev->release = pmu_dev_release;
5569 ret = device_add(pmu->dev);
5577 put_device(pmu->dev);
5581 static struct lock_class_key cpuctx_mutex;
5582 static struct lock_class_key cpuctx_lock;
5584 int perf_pmu_register(struct pmu *pmu, char *name, int type)
5588 mutex_lock(&pmus_lock);
5590 pmu->pmu_disable_count = alloc_percpu(int);
5591 if (!pmu->pmu_disable_count)
5600 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5604 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5612 if (pmu_bus_running) {
5613 ret = pmu_dev_alloc(pmu);
5619 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5620 if (pmu->pmu_cpu_context)
5621 goto got_cpu_context;
5623 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5624 if (!pmu->pmu_cpu_context)
5627 for_each_possible_cpu(cpu) {
5628 struct perf_cpu_context *cpuctx;
5630 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5631 __perf_event_init_context(&cpuctx->ctx);
5632 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
5633 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
5634 cpuctx->ctx.type = cpu_context;
5635 cpuctx->ctx.pmu = pmu;
5636 cpuctx->jiffies_interval = 1;
5637 INIT_LIST_HEAD(&cpuctx->rotation_list);
5638 cpuctx->active_pmu = pmu;
5642 if (!pmu->start_txn) {
5643 if (pmu->pmu_enable) {
5645 * If we have pmu_enable/pmu_disable calls, install
5646 * transaction stubs that use that to try and batch
5647 * hardware accesses.
5649 pmu->start_txn = perf_pmu_start_txn;
5650 pmu->commit_txn = perf_pmu_commit_txn;
5651 pmu->cancel_txn = perf_pmu_cancel_txn;
5653 pmu->start_txn = perf_pmu_nop_void;
5654 pmu->commit_txn = perf_pmu_nop_int;
5655 pmu->cancel_txn = perf_pmu_nop_void;
5659 if (!pmu->pmu_enable) {
5660 pmu->pmu_enable = perf_pmu_nop_void;
5661 pmu->pmu_disable = perf_pmu_nop_void;
5664 list_add_rcu(&pmu->entry, &pmus);
5667 mutex_unlock(&pmus_lock);
5672 device_del(pmu->dev);
5673 put_device(pmu->dev);
5676 if (pmu->type >= PERF_TYPE_MAX)
5677 idr_remove(&pmu_idr, pmu->type);
5680 free_percpu(pmu->pmu_disable_count);
5684 void perf_pmu_unregister(struct pmu *pmu)
5686 mutex_lock(&pmus_lock);
5687 list_del_rcu(&pmu->entry);
5688 mutex_unlock(&pmus_lock);
5691 * We dereference the pmu list under both SRCU and regular RCU, so
5692 * synchronize against both of those.
5694 synchronize_srcu(&pmus_srcu);
5697 free_percpu(pmu->pmu_disable_count);
5698 if (pmu->type >= PERF_TYPE_MAX)
5699 idr_remove(&pmu_idr, pmu->type);
5700 device_del(pmu->dev);
5701 put_device(pmu->dev);
5702 free_pmu_context(pmu);
5705 struct pmu *perf_init_event(struct perf_event *event)
5707 struct pmu *pmu = NULL;
5711 idx = srcu_read_lock(&pmus_srcu);
5714 pmu = idr_find(&pmu_idr, event->attr.type);
5717 ret = pmu->event_init(event);
5723 list_for_each_entry_rcu(pmu, &pmus, entry) {
5724 ret = pmu->event_init(event);
5728 if (ret != -ENOENT) {
5733 pmu = ERR_PTR(-ENOENT);
5735 srcu_read_unlock(&pmus_srcu, idx);
5741 * Allocate and initialize a event structure
5743 static struct perf_event *
5744 perf_event_alloc(struct perf_event_attr *attr, int cpu,
5745 struct task_struct *task,
5746 struct perf_event *group_leader,
5747 struct perf_event *parent_event,
5748 perf_overflow_handler_t overflow_handler)
5751 struct perf_event *event;
5752 struct hw_perf_event *hwc;
5755 if ((unsigned)cpu >= nr_cpu_ids) {
5756 if (!task || cpu != -1)
5757 return ERR_PTR(-EINVAL);
5760 event = kzalloc(sizeof(*event), GFP_KERNEL);
5762 return ERR_PTR(-ENOMEM);
5765 * Single events are their own group leaders, with an
5766 * empty sibling list:
5769 group_leader = event;
5771 mutex_init(&event->child_mutex);
5772 INIT_LIST_HEAD(&event->child_list);
5774 INIT_LIST_HEAD(&event->group_entry);
5775 INIT_LIST_HEAD(&event->event_entry);
5776 INIT_LIST_HEAD(&event->sibling_list);
5777 init_waitqueue_head(&event->waitq);
5778 init_irq_work(&event->pending, perf_pending_event);
5780 mutex_init(&event->mmap_mutex);
5783 event->attr = *attr;
5784 event->group_leader = group_leader;
5788 event->parent = parent_event;
5790 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5791 event->id = atomic64_inc_return(&perf_event_id);
5793 event->state = PERF_EVENT_STATE_INACTIVE;
5796 event->attach_state = PERF_ATTACH_TASK;
5797 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5799 * hw_breakpoint is a bit difficult here..
5801 if (attr->type == PERF_TYPE_BREAKPOINT)
5802 event->hw.bp_target = task;
5806 if (!overflow_handler && parent_event)
5807 overflow_handler = parent_event->overflow_handler;
5809 event->overflow_handler = overflow_handler;
5812 event->state = PERF_EVENT_STATE_OFF;
5817 hwc->sample_period = attr->sample_period;
5818 if (attr->freq && attr->sample_freq)
5819 hwc->sample_period = 1;
5820 hwc->last_period = hwc->sample_period;
5822 local64_set(&hwc->period_left, hwc->sample_period);
5825 * we currently do not support PERF_FORMAT_GROUP on inherited events
5827 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5830 pmu = perf_init_event(event);
5836 else if (IS_ERR(pmu))
5841 put_pid_ns(event->ns);
5843 return ERR_PTR(err);
5848 if (!event->parent) {
5849 if (event->attach_state & PERF_ATTACH_TASK)
5850 jump_label_inc(&perf_sched_events);
5851 if (event->attr.mmap || event->attr.mmap_data)
5852 atomic_inc(&nr_mmap_events);
5853 if (event->attr.comm)
5854 atomic_inc(&nr_comm_events);
5855 if (event->attr.task)
5856 atomic_inc(&nr_task_events);
5857 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5858 err = get_callchain_buffers();
5861 return ERR_PTR(err);
5869 static int perf_copy_attr(struct perf_event_attr __user *uattr,
5870 struct perf_event_attr *attr)
5875 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5879 * zero the full structure, so that a short copy will be nice.
5881 memset(attr, 0, sizeof(*attr));
5883 ret = get_user(size, &uattr->size);
5887 if (size > PAGE_SIZE) /* silly large */
5890 if (!size) /* abi compat */
5891 size = PERF_ATTR_SIZE_VER0;
5893 if (size < PERF_ATTR_SIZE_VER0)
5897 * If we're handed a bigger struct than we know of,
5898 * ensure all the unknown bits are 0 - i.e. new
5899 * user-space does not rely on any kernel feature
5900 * extensions we dont know about yet.
5902 if (size > sizeof(*attr)) {
5903 unsigned char __user *addr;
5904 unsigned char __user *end;
5907 addr = (void __user *)uattr + sizeof(*attr);
5908 end = (void __user *)uattr + size;
5910 for (; addr < end; addr++) {
5911 ret = get_user(val, addr);
5917 size = sizeof(*attr);
5920 ret = copy_from_user(attr, uattr, size);
5925 * If the type exists, the corresponding creation will verify
5928 if (attr->type >= PERF_TYPE_MAX)
5931 if (attr->__reserved_1)
5934 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5937 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5944 put_user(sizeof(*attr), &uattr->size);
5950 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
5952 struct ring_buffer *rb = NULL, *old_rb = NULL;
5958 /* don't allow circular references */
5959 if (event == output_event)
5963 * Don't allow cross-cpu buffers
5965 if (output_event->cpu != event->cpu)
5969 * If its not a per-cpu rb, it must be the same task.
5971 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5975 mutex_lock(&event->mmap_mutex);
5976 /* Can't redirect output if we've got an active mmap() */
5977 if (atomic_read(&event->mmap_count))
5981 /* get the rb we want to redirect to */
5982 rb = ring_buffer_get(output_event);
5988 rcu_assign_pointer(event->rb, rb);
5991 mutex_unlock(&event->mmap_mutex);
5994 ring_buffer_put(old_rb);
6000 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6002 * @attr_uptr: event_id type attributes for monitoring/sampling
6005 * @group_fd: group leader event fd
6007 SYSCALL_DEFINE5(perf_event_open,
6008 struct perf_event_attr __user *, attr_uptr,
6009 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6011 struct perf_event *group_leader = NULL, *output_event = NULL;
6012 struct perf_event *event, *sibling;
6013 struct perf_event_attr attr;
6014 struct perf_event_context *ctx;
6015 struct file *event_file = NULL;
6016 struct file *group_file = NULL;
6017 struct task_struct *task = NULL;
6021 int fput_needed = 0;
6024 /* for future expandability... */
6025 if (flags & ~PERF_FLAG_ALL)
6028 err = perf_copy_attr(attr_uptr, &attr);
6032 if (!attr.exclude_kernel) {
6033 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6038 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6043 * In cgroup mode, the pid argument is used to pass the fd
6044 * opened to the cgroup directory in cgroupfs. The cpu argument
6045 * designates the cpu on which to monitor threads from that
6048 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6051 event_fd = get_unused_fd_flags(O_RDWR);
6055 if (group_fd != -1) {
6056 group_leader = perf_fget_light(group_fd, &fput_needed);
6057 if (IS_ERR(group_leader)) {
6058 err = PTR_ERR(group_leader);
6061 group_file = group_leader->filp;
6062 if (flags & PERF_FLAG_FD_OUTPUT)
6063 output_event = group_leader;
6064 if (flags & PERF_FLAG_FD_NO_GROUP)
6065 group_leader = NULL;
6068 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
6069 task = find_lively_task_by_vpid(pid);
6071 err = PTR_ERR(task);
6076 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
6077 if (IS_ERR(event)) {
6078 err = PTR_ERR(event);
6082 if (flags & PERF_FLAG_PID_CGROUP) {
6083 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6088 * - that has cgroup constraint on event->cpu
6089 * - that may need work on context switch
6091 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6092 jump_label_inc(&perf_sched_events);
6096 * Special case software events and allow them to be part of
6097 * any hardware group.
6102 (is_software_event(event) != is_software_event(group_leader))) {
6103 if (is_software_event(event)) {
6105 * If event and group_leader are not both a software
6106 * event, and event is, then group leader is not.
6108 * Allow the addition of software events to !software
6109 * groups, this is safe because software events never
6112 pmu = group_leader->pmu;
6113 } else if (is_software_event(group_leader) &&
6114 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6116 * In case the group is a pure software group, and we
6117 * try to add a hardware event, move the whole group to
6118 * the hardware context.
6125 * Get the target context (task or percpu):
6127 ctx = find_get_context(pmu, task, cpu);
6134 put_task_struct(task);
6139 * Look up the group leader (we will attach this event to it):
6145 * Do not allow a recursive hierarchy (this new sibling
6146 * becoming part of another group-sibling):
6148 if (group_leader->group_leader != group_leader)
6151 * Do not allow to attach to a group in a different
6152 * task or CPU context:
6155 if (group_leader->ctx->type != ctx->type)
6158 if (group_leader->ctx != ctx)
6163 * Only a group leader can be exclusive or pinned
6165 if (attr.exclusive || attr.pinned)
6170 err = perf_event_set_output(event, output_event);
6175 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6176 if (IS_ERR(event_file)) {
6177 err = PTR_ERR(event_file);
6182 struct perf_event_context *gctx = group_leader->ctx;
6184 mutex_lock(&gctx->mutex);
6185 perf_remove_from_context(group_leader);
6186 list_for_each_entry(sibling, &group_leader->sibling_list,
6188 perf_remove_from_context(sibling);
6191 mutex_unlock(&gctx->mutex);
6195 event->filp = event_file;
6196 WARN_ON_ONCE(ctx->parent_ctx);
6197 mutex_lock(&ctx->mutex);
6200 perf_install_in_context(ctx, group_leader, cpu);
6202 list_for_each_entry(sibling, &group_leader->sibling_list,
6204 perf_install_in_context(ctx, sibling, cpu);
6209 perf_install_in_context(ctx, event, cpu);
6211 perf_unpin_context(ctx);
6212 mutex_unlock(&ctx->mutex);
6214 event->owner = current;
6216 mutex_lock(¤t->perf_event_mutex);
6217 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6218 mutex_unlock(¤t->perf_event_mutex);
6221 * Precalculate sample_data sizes
6223 perf_event__header_size(event);
6224 perf_event__id_header_size(event);
6227 * Drop the reference on the group_event after placing the
6228 * new event on the sibling_list. This ensures destruction
6229 * of the group leader will find the pointer to itself in
6230 * perf_group_detach().
6232 fput_light(group_file, fput_needed);
6233 fd_install(event_fd, event_file);
6237 perf_unpin_context(ctx);
6243 put_task_struct(task);
6245 fput_light(group_file, fput_needed);
6247 put_unused_fd(event_fd);
6252 * perf_event_create_kernel_counter
6254 * @attr: attributes of the counter to create
6255 * @cpu: cpu in which the counter is bound
6256 * @task: task to profile (NULL for percpu)
6259 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6260 struct task_struct *task,
6261 perf_overflow_handler_t overflow_handler)
6263 struct perf_event_context *ctx;
6264 struct perf_event *event;
6268 * Get the target context (task or percpu):
6271 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
6272 if (IS_ERR(event)) {
6273 err = PTR_ERR(event);
6277 ctx = find_get_context(event->pmu, task, cpu);
6284 WARN_ON_ONCE(ctx->parent_ctx);
6285 mutex_lock(&ctx->mutex);
6286 perf_install_in_context(ctx, event, cpu);
6288 perf_unpin_context(ctx);
6289 mutex_unlock(&ctx->mutex);
6296 return ERR_PTR(err);
6298 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6300 static void sync_child_event(struct perf_event *child_event,
6301 struct task_struct *child)
6303 struct perf_event *parent_event = child_event->parent;
6306 if (child_event->attr.inherit_stat)
6307 perf_event_read_event(child_event, child);
6309 child_val = perf_event_count(child_event);
6312 * Add back the child's count to the parent's count:
6314 atomic64_add(child_val, &parent_event->child_count);
6315 atomic64_add(child_event->total_time_enabled,
6316 &parent_event->child_total_time_enabled);
6317 atomic64_add(child_event->total_time_running,
6318 &parent_event->child_total_time_running);
6321 * Remove this event from the parent's list
6323 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6324 mutex_lock(&parent_event->child_mutex);
6325 list_del_init(&child_event->child_list);
6326 mutex_unlock(&parent_event->child_mutex);
6329 * Release the parent event, if this was the last
6332 fput(parent_event->filp);
6336 __perf_event_exit_task(struct perf_event *child_event,
6337 struct perf_event_context *child_ctx,
6338 struct task_struct *child)
6340 if (child_event->parent) {
6341 raw_spin_lock_irq(&child_ctx->lock);
6342 perf_group_detach(child_event);
6343 raw_spin_unlock_irq(&child_ctx->lock);
6346 perf_remove_from_context(child_event);
6349 * It can happen that the parent exits first, and has events
6350 * that are still around due to the child reference. These
6351 * events need to be zapped.
6353 if (child_event->parent) {
6354 sync_child_event(child_event, child);
6355 free_event(child_event);
6359 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6361 struct perf_event *child_event, *tmp;
6362 struct perf_event_context *child_ctx;
6363 unsigned long flags;
6365 if (likely(!child->perf_event_ctxp[ctxn])) {
6366 perf_event_task(child, NULL, 0);
6370 local_irq_save(flags);
6372 * We can't reschedule here because interrupts are disabled,
6373 * and either child is current or it is a task that can't be
6374 * scheduled, so we are now safe from rescheduling changing
6377 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6380 * Take the context lock here so that if find_get_context is
6381 * reading child->perf_event_ctxp, we wait until it has
6382 * incremented the context's refcount before we do put_ctx below.
6384 raw_spin_lock(&child_ctx->lock);
6385 task_ctx_sched_out(child_ctx);
6386 child->perf_event_ctxp[ctxn] = NULL;
6388 * If this context is a clone; unclone it so it can't get
6389 * swapped to another process while we're removing all
6390 * the events from it.
6392 unclone_ctx(child_ctx);
6393 update_context_time(child_ctx);
6394 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6397 * Report the task dead after unscheduling the events so that we
6398 * won't get any samples after PERF_RECORD_EXIT. We can however still
6399 * get a few PERF_RECORD_READ events.
6401 perf_event_task(child, child_ctx, 0);
6404 * We can recurse on the same lock type through:
6406 * __perf_event_exit_task()
6407 * sync_child_event()
6408 * fput(parent_event->filp)
6410 * mutex_lock(&ctx->mutex)
6412 * But since its the parent context it won't be the same instance.
6414 mutex_lock(&child_ctx->mutex);
6417 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6419 __perf_event_exit_task(child_event, child_ctx, child);
6421 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6423 __perf_event_exit_task(child_event, child_ctx, child);
6426 * If the last event was a group event, it will have appended all
6427 * its siblings to the list, but we obtained 'tmp' before that which
6428 * will still point to the list head terminating the iteration.
6430 if (!list_empty(&child_ctx->pinned_groups) ||
6431 !list_empty(&child_ctx->flexible_groups))
6434 mutex_unlock(&child_ctx->mutex);
6440 * When a child task exits, feed back event values to parent events.
6442 void perf_event_exit_task(struct task_struct *child)
6444 struct perf_event *event, *tmp;
6447 mutex_lock(&child->perf_event_mutex);
6448 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6450 list_del_init(&event->owner_entry);
6453 * Ensure the list deletion is visible before we clear
6454 * the owner, closes a race against perf_release() where
6455 * we need to serialize on the owner->perf_event_mutex.
6458 event->owner = NULL;
6460 mutex_unlock(&child->perf_event_mutex);
6462 for_each_task_context_nr(ctxn)
6463 perf_event_exit_task_context(child, ctxn);
6466 static void perf_free_event(struct perf_event *event,
6467 struct perf_event_context *ctx)
6469 struct perf_event *parent = event->parent;
6471 if (WARN_ON_ONCE(!parent))
6474 mutex_lock(&parent->child_mutex);
6475 list_del_init(&event->child_list);
6476 mutex_unlock(&parent->child_mutex);
6480 perf_group_detach(event);
6481 list_del_event(event, ctx);
6486 * free an unexposed, unused context as created by inheritance by
6487 * perf_event_init_task below, used by fork() in case of fail.
6489 void perf_event_free_task(struct task_struct *task)
6491 struct perf_event_context *ctx;
6492 struct perf_event *event, *tmp;
6495 for_each_task_context_nr(ctxn) {
6496 ctx = task->perf_event_ctxp[ctxn];
6500 mutex_lock(&ctx->mutex);
6502 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6504 perf_free_event(event, ctx);
6506 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6508 perf_free_event(event, ctx);
6510 if (!list_empty(&ctx->pinned_groups) ||
6511 !list_empty(&ctx->flexible_groups))
6514 mutex_unlock(&ctx->mutex);
6520 void perf_event_delayed_put(struct task_struct *task)
6524 for_each_task_context_nr(ctxn)
6525 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6529 * inherit a event from parent task to child task:
6531 static struct perf_event *
6532 inherit_event(struct perf_event *parent_event,
6533 struct task_struct *parent,
6534 struct perf_event_context *parent_ctx,
6535 struct task_struct *child,
6536 struct perf_event *group_leader,
6537 struct perf_event_context *child_ctx)
6539 struct perf_event *child_event;
6540 unsigned long flags;
6543 * Instead of creating recursive hierarchies of events,
6544 * we link inherited events back to the original parent,
6545 * which has a filp for sure, which we use as the reference
6548 if (parent_event->parent)
6549 parent_event = parent_event->parent;
6551 child_event = perf_event_alloc(&parent_event->attr,
6554 group_leader, parent_event,
6556 if (IS_ERR(child_event))
6561 * Make the child state follow the state of the parent event,
6562 * not its attr.disabled bit. We hold the parent's mutex,
6563 * so we won't race with perf_event_{en, dis}able_family.
6565 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6566 child_event->state = PERF_EVENT_STATE_INACTIVE;
6568 child_event->state = PERF_EVENT_STATE_OFF;
6570 if (parent_event->attr.freq) {
6571 u64 sample_period = parent_event->hw.sample_period;
6572 struct hw_perf_event *hwc = &child_event->hw;
6574 hwc->sample_period = sample_period;
6575 hwc->last_period = sample_period;
6577 local64_set(&hwc->period_left, sample_period);
6580 child_event->ctx = child_ctx;
6581 child_event->overflow_handler = parent_event->overflow_handler;
6584 * Precalculate sample_data sizes
6586 perf_event__header_size(child_event);
6587 perf_event__id_header_size(child_event);
6590 * Link it up in the child's context:
6592 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6593 add_event_to_ctx(child_event, child_ctx);
6594 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6597 * Get a reference to the parent filp - we will fput it
6598 * when the child event exits. This is safe to do because
6599 * we are in the parent and we know that the filp still
6600 * exists and has a nonzero count:
6602 atomic_long_inc(&parent_event->filp->f_count);
6605 * Link this into the parent event's child list
6607 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6608 mutex_lock(&parent_event->child_mutex);
6609 list_add_tail(&child_event->child_list, &parent_event->child_list);
6610 mutex_unlock(&parent_event->child_mutex);
6615 static int inherit_group(struct perf_event *parent_event,
6616 struct task_struct *parent,
6617 struct perf_event_context *parent_ctx,
6618 struct task_struct *child,
6619 struct perf_event_context *child_ctx)
6621 struct perf_event *leader;
6622 struct perf_event *sub;
6623 struct perf_event *child_ctr;
6625 leader = inherit_event(parent_event, parent, parent_ctx,
6626 child, NULL, child_ctx);
6628 return PTR_ERR(leader);
6629 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6630 child_ctr = inherit_event(sub, parent, parent_ctx,
6631 child, leader, child_ctx);
6632 if (IS_ERR(child_ctr))
6633 return PTR_ERR(child_ctr);
6639 inherit_task_group(struct perf_event *event, struct task_struct *parent,
6640 struct perf_event_context *parent_ctx,
6641 struct task_struct *child, int ctxn,
6645 struct perf_event_context *child_ctx;
6647 if (!event->attr.inherit) {
6652 child_ctx = child->perf_event_ctxp[ctxn];
6655 * This is executed from the parent task context, so
6656 * inherit events that have been marked for cloning.
6657 * First allocate and initialize a context for the
6661 child_ctx = alloc_perf_context(event->pmu, child);
6665 child->perf_event_ctxp[ctxn] = child_ctx;
6668 ret = inherit_group(event, parent, parent_ctx,
6678 * Initialize the perf_event context in task_struct
6680 int perf_event_init_context(struct task_struct *child, int ctxn)
6682 struct perf_event_context *child_ctx, *parent_ctx;
6683 struct perf_event_context *cloned_ctx;
6684 struct perf_event *event;
6685 struct task_struct *parent = current;
6686 int inherited_all = 1;
6687 unsigned long flags;
6690 if (likely(!parent->perf_event_ctxp[ctxn]))
6694 * If the parent's context is a clone, pin it so it won't get
6697 parent_ctx = perf_pin_task_context(parent, ctxn);
6700 * No need to check if parent_ctx != NULL here; since we saw
6701 * it non-NULL earlier, the only reason for it to become NULL
6702 * is if we exit, and since we're currently in the middle of
6703 * a fork we can't be exiting at the same time.
6707 * Lock the parent list. No need to lock the child - not PID
6708 * hashed yet and not running, so nobody can access it.
6710 mutex_lock(&parent_ctx->mutex);
6713 * We dont have to disable NMIs - we are only looking at
6714 * the list, not manipulating it:
6716 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
6717 ret = inherit_task_group(event, parent, parent_ctx,
6718 child, ctxn, &inherited_all);
6724 * We can't hold ctx->lock when iterating the ->flexible_group list due
6725 * to allocations, but we need to prevent rotation because
6726 * rotate_ctx() will change the list from interrupt context.
6728 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6729 parent_ctx->rotate_disable = 1;
6730 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6732 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
6733 ret = inherit_task_group(event, parent, parent_ctx,
6734 child, ctxn, &inherited_all);
6739 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6740 parent_ctx->rotate_disable = 0;
6742 child_ctx = child->perf_event_ctxp[ctxn];
6744 if (child_ctx && inherited_all) {
6746 * Mark the child context as a clone of the parent
6747 * context, or of whatever the parent is a clone of.
6749 * Note that if the parent is a clone, the holding of
6750 * parent_ctx->lock avoids it from being uncloned.
6752 cloned_ctx = parent_ctx->parent_ctx;
6754 child_ctx->parent_ctx = cloned_ctx;
6755 child_ctx->parent_gen = parent_ctx->parent_gen;
6757 child_ctx->parent_ctx = parent_ctx;
6758 child_ctx->parent_gen = parent_ctx->generation;
6760 get_ctx(child_ctx->parent_ctx);
6763 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6764 mutex_unlock(&parent_ctx->mutex);
6766 perf_unpin_context(parent_ctx);
6767 put_ctx(parent_ctx);
6773 * Initialize the perf_event context in task_struct
6775 int perf_event_init_task(struct task_struct *child)
6779 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
6780 mutex_init(&child->perf_event_mutex);
6781 INIT_LIST_HEAD(&child->perf_event_list);
6783 for_each_task_context_nr(ctxn) {
6784 ret = perf_event_init_context(child, ctxn);
6792 static void __init perf_event_init_all_cpus(void)
6794 struct swevent_htable *swhash;
6797 for_each_possible_cpu(cpu) {
6798 swhash = &per_cpu(swevent_htable, cpu);
6799 mutex_init(&swhash->hlist_mutex);
6800 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
6804 static void __cpuinit perf_event_init_cpu(int cpu)
6806 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6808 mutex_lock(&swhash->hlist_mutex);
6809 if (swhash->hlist_refcount > 0) {
6810 struct swevent_hlist *hlist;
6812 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6814 rcu_assign_pointer(swhash->swevent_hlist, hlist);
6816 mutex_unlock(&swhash->hlist_mutex);
6819 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
6820 static void perf_pmu_rotate_stop(struct pmu *pmu)
6822 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6824 WARN_ON(!irqs_disabled());
6826 list_del_init(&cpuctx->rotation_list);
6829 static void __perf_event_exit_context(void *__info)
6831 struct perf_event_context *ctx = __info;
6832 struct perf_event *event, *tmp;
6834 perf_pmu_rotate_stop(ctx->pmu);
6836 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
6837 __perf_remove_from_context(event);
6838 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
6839 __perf_remove_from_context(event);
6842 static void perf_event_exit_cpu_context(int cpu)
6844 struct perf_event_context *ctx;
6848 idx = srcu_read_lock(&pmus_srcu);
6849 list_for_each_entry_rcu(pmu, &pmus, entry) {
6850 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
6852 mutex_lock(&ctx->mutex);
6853 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6854 mutex_unlock(&ctx->mutex);
6856 srcu_read_unlock(&pmus_srcu, idx);
6859 static void perf_event_exit_cpu(int cpu)
6861 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6863 mutex_lock(&swhash->hlist_mutex);
6864 swevent_hlist_release(swhash);
6865 mutex_unlock(&swhash->hlist_mutex);
6867 perf_event_exit_cpu_context(cpu);
6870 static inline void perf_event_exit_cpu(int cpu) { }
6874 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
6878 for_each_online_cpu(cpu)
6879 perf_event_exit_cpu(cpu);
6885 * Run the perf reboot notifier at the very last possible moment so that
6886 * the generic watchdog code runs as long as possible.
6888 static struct notifier_block perf_reboot_notifier = {
6889 .notifier_call = perf_reboot,
6890 .priority = INT_MIN,
6893 static int __cpuinit
6894 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6896 unsigned int cpu = (long)hcpu;
6898 switch (action & ~CPU_TASKS_FROZEN) {
6900 case CPU_UP_PREPARE:
6901 case CPU_DOWN_FAILED:
6902 perf_event_init_cpu(cpu);
6905 case CPU_UP_CANCELED:
6906 case CPU_DOWN_PREPARE:
6907 perf_event_exit_cpu(cpu);
6917 void __init perf_event_init(void)
6923 perf_event_init_all_cpus();
6924 init_srcu_struct(&pmus_srcu);
6925 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
6926 perf_pmu_register(&perf_cpu_clock, NULL, -1);
6927 perf_pmu_register(&perf_task_clock, NULL, -1);
6929 perf_cpu_notifier(perf_cpu_notify);
6930 register_reboot_notifier(&perf_reboot_notifier);
6932 ret = init_hw_breakpoint();
6933 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
6936 static int __init perf_event_sysfs_init(void)
6941 mutex_lock(&pmus_lock);
6943 ret = bus_register(&pmu_bus);
6947 list_for_each_entry(pmu, &pmus, entry) {
6948 if (!pmu->name || pmu->type < 0)
6951 ret = pmu_dev_alloc(pmu);
6952 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
6954 pmu_bus_running = 1;
6958 mutex_unlock(&pmus_lock);
6962 device_initcall(perf_event_sysfs_init);
6964 #ifdef CONFIG_CGROUP_PERF
6965 static struct cgroup_subsys_state *perf_cgroup_create(
6966 struct cgroup_subsys *ss, struct cgroup *cont)
6968 struct perf_cgroup *jc;
6970 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
6972 return ERR_PTR(-ENOMEM);
6974 jc->info = alloc_percpu(struct perf_cgroup_info);
6977 return ERR_PTR(-ENOMEM);
6983 static void perf_cgroup_destroy(struct cgroup_subsys *ss,
6984 struct cgroup *cont)
6986 struct perf_cgroup *jc;
6987 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
6988 struct perf_cgroup, css);
6989 free_percpu(jc->info);
6993 static int __perf_cgroup_move(void *info)
6995 struct task_struct *task = info;
6996 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7001 perf_cgroup_attach_task(struct cgroup *cgrp, struct task_struct *task)
7003 task_function_call(task, __perf_cgroup_move, task);
7006 static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7007 struct cgroup *old_cgrp, struct task_struct *task)
7010 * cgroup_exit() is called in the copy_process() failure path.
7011 * Ignore this case since the task hasn't ran yet, this avoids
7012 * trying to poke a half freed task state from generic code.
7014 if (!(task->flags & PF_EXITING))
7017 perf_cgroup_attach_task(cgrp, task);
7020 struct cgroup_subsys perf_subsys = {
7021 .name = "perf_event",
7022 .subsys_id = perf_subsys_id,
7023 .create = perf_cgroup_create,
7024 .destroy = perf_cgroup_destroy,
7025 .exit = perf_cgroup_exit,
7026 .attach_task = perf_cgroup_attach_task,
7028 #endif /* CONFIG_CGROUP_PERF */