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/export.h>
29 #include <linux/vmalloc.h>
30 #include <linux/hardirq.h>
31 #include <linux/rculist.h>
32 #include <linux/uaccess.h>
33 #include <linux/syscalls.h>
34 #include <linux/anon_inodes.h>
35 #include <linux/kernel_stat.h>
36 #include <linux/perf_event.h>
37 #include <linux/ftrace_event.h>
38 #include <linux/hw_breakpoint.h>
42 #include <asm/irq_regs.h>
44 struct remote_function_call {
45 struct task_struct *p;
46 int (*func)(void *info);
51 static void remote_function(void *data)
53 struct remote_function_call *tfc = data;
54 struct task_struct *p = tfc->p;
58 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
62 tfc->ret = tfc->func(tfc->info);
66 * task_function_call - call a function on the cpu on which a task runs
67 * @p: the task to evaluate
68 * @func: the function to be called
69 * @info: the function call argument
71 * Calls the function @func when the task is currently running. This might
72 * be on the current CPU, which just calls the function directly
74 * returns: @func return value, or
75 * -ESRCH - when the process isn't running
76 * -EAGAIN - when the process moved away
79 task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
81 struct remote_function_call data = {
85 .ret = -ESRCH, /* No such (running) process */
89 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
95 * cpu_function_call - call a function on the cpu
96 * @func: the function to be called
97 * @info: the function call argument
99 * Calls the function @func on the remote cpu.
101 * returns: @func return value or -ENXIO when the cpu is offline
103 static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
105 struct remote_function_call data = {
109 .ret = -ENXIO, /* No such CPU */
112 smp_call_function_single(cpu, remote_function, &data, 1);
117 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
118 PERF_FLAG_FD_OUTPUT |\
119 PERF_FLAG_PID_CGROUP)
122 * branch priv levels that need permission checks
124 #define PERF_SAMPLE_BRANCH_PERM_PLM \
125 (PERF_SAMPLE_BRANCH_KERNEL |\
126 PERF_SAMPLE_BRANCH_HV)
129 EVENT_FLEXIBLE = 0x1,
131 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
135 * perf_sched_events : >0 events exist
136 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
138 struct static_key_deferred perf_sched_events __read_mostly;
139 static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
141 static atomic_t nr_mmap_events __read_mostly;
142 static atomic_t nr_comm_events __read_mostly;
143 static atomic_t nr_task_events __read_mostly;
145 static LIST_HEAD(pmus);
146 static DEFINE_MUTEX(pmus_lock);
147 static struct srcu_struct pmus_srcu;
150 * perf event paranoia level:
151 * -1 - not paranoid at all
152 * 0 - disallow raw tracepoint access for unpriv
153 * 1 - disallow cpu events for unpriv
154 * 2 - disallow kernel profiling for unpriv
156 int sysctl_perf_event_paranoid __read_mostly = 1;
158 /* Minimum for 512 kiB + 1 user control page */
159 int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
162 * max perf event sample rate
164 #define DEFAULT_MAX_SAMPLE_RATE 100000
165 int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
166 static int max_samples_per_tick __read_mostly =
167 DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
169 int perf_proc_update_handler(struct ctl_table *table, int write,
170 void __user *buffer, size_t *lenp,
173 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
178 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
183 static atomic64_t perf_event_id;
185 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
186 enum event_type_t event_type);
188 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
189 enum event_type_t event_type,
190 struct task_struct *task);
192 static void update_context_time(struct perf_event_context *ctx);
193 static u64 perf_event_time(struct perf_event *event);
195 static void ring_buffer_attach(struct perf_event *event,
196 struct ring_buffer *rb);
198 void __weak perf_event_print_debug(void) { }
200 extern __weak const char *perf_pmu_name(void)
205 static inline u64 perf_clock(void)
207 return local_clock();
210 static inline struct perf_cpu_context *
211 __get_cpu_context(struct perf_event_context *ctx)
213 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
216 static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
217 struct perf_event_context *ctx)
219 raw_spin_lock(&cpuctx->ctx.lock);
221 raw_spin_lock(&ctx->lock);
224 static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
225 struct perf_event_context *ctx)
228 raw_spin_unlock(&ctx->lock);
229 raw_spin_unlock(&cpuctx->ctx.lock);
232 #ifdef CONFIG_CGROUP_PERF
235 * Must ensure cgroup is pinned (css_get) before calling
236 * this function. In other words, we cannot call this function
237 * if there is no cgroup event for the current CPU context.
239 static inline struct perf_cgroup *
240 perf_cgroup_from_task(struct task_struct *task)
242 return container_of(task_subsys_state(task, perf_subsys_id),
243 struct perf_cgroup, css);
247 perf_cgroup_match(struct perf_event *event)
249 struct perf_event_context *ctx = event->ctx;
250 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
252 return !event->cgrp || event->cgrp == cpuctx->cgrp;
255 static inline void perf_get_cgroup(struct perf_event *event)
257 css_get(&event->cgrp->css);
260 static inline void perf_put_cgroup(struct perf_event *event)
262 css_put(&event->cgrp->css);
265 static inline void perf_detach_cgroup(struct perf_event *event)
267 perf_put_cgroup(event);
271 static inline int is_cgroup_event(struct perf_event *event)
273 return event->cgrp != NULL;
276 static inline u64 perf_cgroup_event_time(struct perf_event *event)
278 struct perf_cgroup_info *t;
280 t = per_cpu_ptr(event->cgrp->info, event->cpu);
284 static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
286 struct perf_cgroup_info *info;
291 info = this_cpu_ptr(cgrp->info);
293 info->time += now - info->timestamp;
294 info->timestamp = now;
297 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
299 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
301 __update_cgrp_time(cgrp_out);
304 static inline void update_cgrp_time_from_event(struct perf_event *event)
306 struct perf_cgroup *cgrp;
309 * ensure we access cgroup data only when needed and
310 * when we know the cgroup is pinned (css_get)
312 if (!is_cgroup_event(event))
315 cgrp = perf_cgroup_from_task(current);
317 * Do not update time when cgroup is not active
319 if (cgrp == event->cgrp)
320 __update_cgrp_time(event->cgrp);
324 perf_cgroup_set_timestamp(struct task_struct *task,
325 struct perf_event_context *ctx)
327 struct perf_cgroup *cgrp;
328 struct perf_cgroup_info *info;
331 * ctx->lock held by caller
332 * ensure we do not access cgroup data
333 * unless we have the cgroup pinned (css_get)
335 if (!task || !ctx->nr_cgroups)
338 cgrp = perf_cgroup_from_task(task);
339 info = this_cpu_ptr(cgrp->info);
340 info->timestamp = ctx->timestamp;
343 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
344 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
347 * reschedule events based on the cgroup constraint of task.
349 * mode SWOUT : schedule out everything
350 * mode SWIN : schedule in based on cgroup for next
352 void perf_cgroup_switch(struct task_struct *task, int mode)
354 struct perf_cpu_context *cpuctx;
359 * disable interrupts to avoid geting nr_cgroup
360 * changes via __perf_event_disable(). Also
363 local_irq_save(flags);
366 * we reschedule only in the presence of cgroup
367 * constrained events.
371 list_for_each_entry_rcu(pmu, &pmus, entry) {
372 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
375 * perf_cgroup_events says at least one
376 * context on this CPU has cgroup events.
378 * ctx->nr_cgroups reports the number of cgroup
379 * events for a context.
381 if (cpuctx->ctx.nr_cgroups > 0) {
382 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
383 perf_pmu_disable(cpuctx->ctx.pmu);
385 if (mode & PERF_CGROUP_SWOUT) {
386 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
388 * must not be done before ctxswout due
389 * to event_filter_match() in event_sched_out()
394 if (mode & PERF_CGROUP_SWIN) {
395 WARN_ON_ONCE(cpuctx->cgrp);
396 /* set cgrp before ctxsw in to
397 * allow event_filter_match() to not
398 * have to pass task around
400 cpuctx->cgrp = perf_cgroup_from_task(task);
401 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
403 perf_pmu_enable(cpuctx->ctx.pmu);
404 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
410 local_irq_restore(flags);
413 static inline void perf_cgroup_sched_out(struct task_struct *task,
414 struct task_struct *next)
416 struct perf_cgroup *cgrp1;
417 struct perf_cgroup *cgrp2 = NULL;
420 * we come here when we know perf_cgroup_events > 0
422 cgrp1 = perf_cgroup_from_task(task);
425 * next is NULL when called from perf_event_enable_on_exec()
426 * that will systematically cause a cgroup_switch()
429 cgrp2 = perf_cgroup_from_task(next);
432 * only schedule out current cgroup events if we know
433 * that we are switching to a different cgroup. Otherwise,
434 * do no touch the cgroup events.
437 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
440 static inline void perf_cgroup_sched_in(struct task_struct *prev,
441 struct task_struct *task)
443 struct perf_cgroup *cgrp1;
444 struct perf_cgroup *cgrp2 = NULL;
447 * we come here when we know perf_cgroup_events > 0
449 cgrp1 = perf_cgroup_from_task(task);
451 /* prev can never be NULL */
452 cgrp2 = perf_cgroup_from_task(prev);
455 * only need to schedule in cgroup events if we are changing
456 * cgroup during ctxsw. Cgroup events were not scheduled
457 * out of ctxsw out if that was not the case.
460 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
463 static inline int perf_cgroup_connect(int fd, struct perf_event *event,
464 struct perf_event_attr *attr,
465 struct perf_event *group_leader)
467 struct perf_cgroup *cgrp;
468 struct cgroup_subsys_state *css;
470 int ret = 0, fput_needed;
472 file = fget_light(fd, &fput_needed);
476 css = cgroup_css_from_dir(file, perf_subsys_id);
482 cgrp = container_of(css, struct perf_cgroup, css);
485 /* must be done before we fput() the file */
486 perf_get_cgroup(event);
489 * all events in a group must monitor
490 * the same cgroup because a task belongs
491 * to only one perf cgroup at a time
493 if (group_leader && group_leader->cgrp != cgrp) {
494 perf_detach_cgroup(event);
498 fput_light(file, fput_needed);
503 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
505 struct perf_cgroup_info *t;
506 t = per_cpu_ptr(event->cgrp->info, event->cpu);
507 event->shadow_ctx_time = now - t->timestamp;
511 perf_cgroup_defer_enabled(struct perf_event *event)
514 * when the current task's perf cgroup does not match
515 * the event's, we need to remember to call the
516 * perf_mark_enable() function the first time a task with
517 * a matching perf cgroup is scheduled in.
519 if (is_cgroup_event(event) && !perf_cgroup_match(event))
520 event->cgrp_defer_enabled = 1;
524 perf_cgroup_mark_enabled(struct perf_event *event,
525 struct perf_event_context *ctx)
527 struct perf_event *sub;
528 u64 tstamp = perf_event_time(event);
530 if (!event->cgrp_defer_enabled)
533 event->cgrp_defer_enabled = 0;
535 event->tstamp_enabled = tstamp - event->total_time_enabled;
536 list_for_each_entry(sub, &event->sibling_list, group_entry) {
537 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
538 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
539 sub->cgrp_defer_enabled = 0;
543 #else /* !CONFIG_CGROUP_PERF */
546 perf_cgroup_match(struct perf_event *event)
551 static inline void perf_detach_cgroup(struct perf_event *event)
554 static inline int is_cgroup_event(struct perf_event *event)
559 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
564 static inline void update_cgrp_time_from_event(struct perf_event *event)
568 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
572 static inline void perf_cgroup_sched_out(struct task_struct *task,
573 struct task_struct *next)
577 static inline void perf_cgroup_sched_in(struct task_struct *prev,
578 struct task_struct *task)
582 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
583 struct perf_event_attr *attr,
584 struct perf_event *group_leader)
590 perf_cgroup_set_timestamp(struct task_struct *task,
591 struct perf_event_context *ctx)
596 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
601 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
605 static inline u64 perf_cgroup_event_time(struct perf_event *event)
611 perf_cgroup_defer_enabled(struct perf_event *event)
616 perf_cgroup_mark_enabled(struct perf_event *event,
617 struct perf_event_context *ctx)
622 void perf_pmu_disable(struct pmu *pmu)
624 int *count = this_cpu_ptr(pmu->pmu_disable_count);
626 pmu->pmu_disable(pmu);
629 void perf_pmu_enable(struct pmu *pmu)
631 int *count = this_cpu_ptr(pmu->pmu_disable_count);
633 pmu->pmu_enable(pmu);
636 static DEFINE_PER_CPU(struct list_head, rotation_list);
639 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
640 * because they're strictly cpu affine and rotate_start is called with IRQs
641 * disabled, while rotate_context is called from IRQ context.
643 static void perf_pmu_rotate_start(struct pmu *pmu)
645 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
646 struct list_head *head = &__get_cpu_var(rotation_list);
648 WARN_ON(!irqs_disabled());
650 if (list_empty(&cpuctx->rotation_list))
651 list_add(&cpuctx->rotation_list, head);
654 static void get_ctx(struct perf_event_context *ctx)
656 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
659 static void put_ctx(struct perf_event_context *ctx)
661 if (atomic_dec_and_test(&ctx->refcount)) {
663 put_ctx(ctx->parent_ctx);
665 put_task_struct(ctx->task);
666 kfree_rcu(ctx, rcu_head);
670 static void unclone_ctx(struct perf_event_context *ctx)
672 if (ctx->parent_ctx) {
673 put_ctx(ctx->parent_ctx);
674 ctx->parent_ctx = NULL;
678 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
681 * only top level events have the pid namespace they were created in
684 event = event->parent;
686 return task_tgid_nr_ns(p, event->ns);
689 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
692 * only top level events have the pid namespace they were created in
695 event = event->parent;
697 return task_pid_nr_ns(p, event->ns);
701 * If we inherit events we want to return the parent event id
704 static u64 primary_event_id(struct perf_event *event)
709 id = event->parent->id;
715 * Get the perf_event_context for a task and lock it.
716 * This has to cope with with the fact that until it is locked,
717 * the context could get moved to another task.
719 static struct perf_event_context *
720 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
722 struct perf_event_context *ctx;
726 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
729 * If this context is a clone of another, it might
730 * get swapped for another underneath us by
731 * perf_event_task_sched_out, though the
732 * rcu_read_lock() protects us from any context
733 * getting freed. Lock the context and check if it
734 * got swapped before we could get the lock, and retry
735 * if so. If we locked the right context, then it
736 * can't get swapped on us any more.
738 raw_spin_lock_irqsave(&ctx->lock, *flags);
739 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
740 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
744 if (!atomic_inc_not_zero(&ctx->refcount)) {
745 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
754 * Get the context for a task and increment its pin_count so it
755 * can't get swapped to another task. This also increments its
756 * reference count so that the context can't get freed.
758 static struct perf_event_context *
759 perf_pin_task_context(struct task_struct *task, int ctxn)
761 struct perf_event_context *ctx;
764 ctx = perf_lock_task_context(task, ctxn, &flags);
767 raw_spin_unlock_irqrestore(&ctx->lock, flags);
772 static void perf_unpin_context(struct perf_event_context *ctx)
776 raw_spin_lock_irqsave(&ctx->lock, flags);
778 raw_spin_unlock_irqrestore(&ctx->lock, flags);
782 * Update the record of the current time in a context.
784 static void update_context_time(struct perf_event_context *ctx)
786 u64 now = perf_clock();
788 ctx->time += now - ctx->timestamp;
789 ctx->timestamp = now;
792 static u64 perf_event_time(struct perf_event *event)
794 struct perf_event_context *ctx = event->ctx;
796 if (is_cgroup_event(event))
797 return perf_cgroup_event_time(event);
799 return ctx ? ctx->time : 0;
803 * Update the total_time_enabled and total_time_running fields for a event.
804 * The caller of this function needs to hold the ctx->lock.
806 static void update_event_times(struct perf_event *event)
808 struct perf_event_context *ctx = event->ctx;
811 if (event->state < PERF_EVENT_STATE_INACTIVE ||
812 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
815 * in cgroup mode, time_enabled represents
816 * the time the event was enabled AND active
817 * tasks were in the monitored cgroup. This is
818 * independent of the activity of the context as
819 * there may be a mix of cgroup and non-cgroup events.
821 * That is why we treat cgroup events differently
824 if (is_cgroup_event(event))
825 run_end = perf_cgroup_event_time(event);
826 else if (ctx->is_active)
829 run_end = event->tstamp_stopped;
831 event->total_time_enabled = run_end - event->tstamp_enabled;
833 if (event->state == PERF_EVENT_STATE_INACTIVE)
834 run_end = event->tstamp_stopped;
836 run_end = perf_event_time(event);
838 event->total_time_running = run_end - event->tstamp_running;
843 * Update total_time_enabled and total_time_running for all events in a group.
845 static void update_group_times(struct perf_event *leader)
847 struct perf_event *event;
849 update_event_times(leader);
850 list_for_each_entry(event, &leader->sibling_list, group_entry)
851 update_event_times(event);
854 static struct list_head *
855 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
857 if (event->attr.pinned)
858 return &ctx->pinned_groups;
860 return &ctx->flexible_groups;
864 * Add a event from the lists for its context.
865 * Must be called with ctx->mutex and ctx->lock held.
868 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
870 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
871 event->attach_state |= PERF_ATTACH_CONTEXT;
874 * If we're a stand alone event or group leader, we go to the context
875 * list, group events are kept attached to the group so that
876 * perf_group_detach can, at all times, locate all siblings.
878 if (event->group_leader == event) {
879 struct list_head *list;
881 if (is_software_event(event))
882 event->group_flags |= PERF_GROUP_SOFTWARE;
884 list = ctx_group_list(event, ctx);
885 list_add_tail(&event->group_entry, list);
888 if (is_cgroup_event(event))
891 list_add_rcu(&event->event_entry, &ctx->event_list);
893 perf_pmu_rotate_start(ctx->pmu);
895 if (event->attr.inherit_stat)
900 * Called at perf_event creation and when events are attached/detached from a
903 static void perf_event__read_size(struct perf_event *event)
905 int entry = sizeof(u64); /* value */
909 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
912 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
915 if (event->attr.read_format & PERF_FORMAT_ID)
916 entry += sizeof(u64);
918 if (event->attr.read_format & PERF_FORMAT_GROUP) {
919 nr += event->group_leader->nr_siblings;
924 event->read_size = size;
927 static void perf_event__header_size(struct perf_event *event)
929 struct perf_sample_data *data;
930 u64 sample_type = event->attr.sample_type;
933 perf_event__read_size(event);
935 if (sample_type & PERF_SAMPLE_IP)
936 size += sizeof(data->ip);
938 if (sample_type & PERF_SAMPLE_ADDR)
939 size += sizeof(data->addr);
941 if (sample_type & PERF_SAMPLE_PERIOD)
942 size += sizeof(data->period);
944 if (sample_type & PERF_SAMPLE_READ)
945 size += event->read_size;
947 event->header_size = size;
950 static void perf_event__id_header_size(struct perf_event *event)
952 struct perf_sample_data *data;
953 u64 sample_type = event->attr.sample_type;
956 if (sample_type & PERF_SAMPLE_TID)
957 size += sizeof(data->tid_entry);
959 if (sample_type & PERF_SAMPLE_TIME)
960 size += sizeof(data->time);
962 if (sample_type & PERF_SAMPLE_ID)
963 size += sizeof(data->id);
965 if (sample_type & PERF_SAMPLE_STREAM_ID)
966 size += sizeof(data->stream_id);
968 if (sample_type & PERF_SAMPLE_CPU)
969 size += sizeof(data->cpu_entry);
971 event->id_header_size = size;
974 static void perf_group_attach(struct perf_event *event)
976 struct perf_event *group_leader = event->group_leader, *pos;
979 * We can have double attach due to group movement in perf_event_open.
981 if (event->attach_state & PERF_ATTACH_GROUP)
984 event->attach_state |= PERF_ATTACH_GROUP;
986 if (group_leader == event)
989 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
990 !is_software_event(event))
991 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
993 list_add_tail(&event->group_entry, &group_leader->sibling_list);
994 group_leader->nr_siblings++;
996 perf_event__header_size(group_leader);
998 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
999 perf_event__header_size(pos);
1003 * Remove a event from the lists for its context.
1004 * Must be called with ctx->mutex and ctx->lock held.
1007 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1009 struct perf_cpu_context *cpuctx;
1011 * We can have double detach due to exit/hot-unplug + close.
1013 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
1016 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1018 if (is_cgroup_event(event)) {
1020 cpuctx = __get_cpu_context(ctx);
1022 * if there are no more cgroup events
1023 * then cler cgrp to avoid stale pointer
1024 * in update_cgrp_time_from_cpuctx()
1026 if (!ctx->nr_cgroups)
1027 cpuctx->cgrp = NULL;
1031 if (event->attr.inherit_stat)
1034 list_del_rcu(&event->event_entry);
1036 if (event->group_leader == event)
1037 list_del_init(&event->group_entry);
1039 update_group_times(event);
1042 * If event was in error state, then keep it
1043 * that way, otherwise bogus counts will be
1044 * returned on read(). The only way to get out
1045 * of error state is by explicit re-enabling
1048 if (event->state > PERF_EVENT_STATE_OFF)
1049 event->state = PERF_EVENT_STATE_OFF;
1052 static void perf_group_detach(struct perf_event *event)
1054 struct perf_event *sibling, *tmp;
1055 struct list_head *list = NULL;
1058 * We can have double detach due to exit/hot-unplug + close.
1060 if (!(event->attach_state & PERF_ATTACH_GROUP))
1063 event->attach_state &= ~PERF_ATTACH_GROUP;
1066 * If this is a sibling, remove it from its group.
1068 if (event->group_leader != event) {
1069 list_del_init(&event->group_entry);
1070 event->group_leader->nr_siblings--;
1074 if (!list_empty(&event->group_entry))
1075 list = &event->group_entry;
1078 * If this was a group event with sibling events then
1079 * upgrade the siblings to singleton events by adding them
1080 * to whatever list we are on.
1082 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
1084 list_move_tail(&sibling->group_entry, list);
1085 sibling->group_leader = sibling;
1087 /* Inherit group flags from the previous leader */
1088 sibling->group_flags = event->group_flags;
1092 perf_event__header_size(event->group_leader);
1094 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1095 perf_event__header_size(tmp);
1099 event_filter_match(struct perf_event *event)
1101 return (event->cpu == -1 || event->cpu == smp_processor_id())
1102 && perf_cgroup_match(event);
1106 event_sched_out(struct perf_event *event,
1107 struct perf_cpu_context *cpuctx,
1108 struct perf_event_context *ctx)
1110 u64 tstamp = perf_event_time(event);
1113 * An event which could not be activated because of
1114 * filter mismatch still needs to have its timings
1115 * maintained, otherwise bogus information is return
1116 * via read() for time_enabled, time_running:
1118 if (event->state == PERF_EVENT_STATE_INACTIVE
1119 && !event_filter_match(event)) {
1120 delta = tstamp - event->tstamp_stopped;
1121 event->tstamp_running += delta;
1122 event->tstamp_stopped = tstamp;
1125 if (event->state != PERF_EVENT_STATE_ACTIVE)
1128 event->state = PERF_EVENT_STATE_INACTIVE;
1129 if (event->pending_disable) {
1130 event->pending_disable = 0;
1131 event->state = PERF_EVENT_STATE_OFF;
1133 event->tstamp_stopped = tstamp;
1134 event->pmu->del(event, 0);
1137 if (!is_software_event(event))
1138 cpuctx->active_oncpu--;
1140 if (event->attr.freq && event->attr.sample_freq)
1142 if (event->attr.exclusive || !cpuctx->active_oncpu)
1143 cpuctx->exclusive = 0;
1147 group_sched_out(struct perf_event *group_event,
1148 struct perf_cpu_context *cpuctx,
1149 struct perf_event_context *ctx)
1151 struct perf_event *event;
1152 int state = group_event->state;
1154 event_sched_out(group_event, cpuctx, ctx);
1157 * Schedule out siblings (if any):
1159 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1160 event_sched_out(event, cpuctx, ctx);
1162 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
1163 cpuctx->exclusive = 0;
1167 * Cross CPU call to remove a performance event
1169 * We disable the event on the hardware level first. After that we
1170 * remove it from the context list.
1172 static int __perf_remove_from_context(void *info)
1174 struct perf_event *event = info;
1175 struct perf_event_context *ctx = event->ctx;
1176 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1178 raw_spin_lock(&ctx->lock);
1179 event_sched_out(event, cpuctx, ctx);
1180 list_del_event(event, ctx);
1181 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1183 cpuctx->task_ctx = NULL;
1185 raw_spin_unlock(&ctx->lock);
1192 * Remove the event from a task's (or a CPU's) list of events.
1194 * CPU events are removed with a smp call. For task events we only
1195 * call when the task is on a CPU.
1197 * If event->ctx is a cloned context, callers must make sure that
1198 * every task struct that event->ctx->task could possibly point to
1199 * remains valid. This is OK when called from perf_release since
1200 * that only calls us on the top-level context, which can't be a clone.
1201 * When called from perf_event_exit_task, it's OK because the
1202 * context has been detached from its task.
1204 static void perf_remove_from_context(struct perf_event *event)
1206 struct perf_event_context *ctx = event->ctx;
1207 struct task_struct *task = ctx->task;
1209 lockdep_assert_held(&ctx->mutex);
1213 * Per cpu events are removed via an smp call and
1214 * the removal is always successful.
1216 cpu_function_call(event->cpu, __perf_remove_from_context, event);
1221 if (!task_function_call(task, __perf_remove_from_context, event))
1224 raw_spin_lock_irq(&ctx->lock);
1226 * If we failed to find a running task, but find the context active now
1227 * that we've acquired the ctx->lock, retry.
1229 if (ctx->is_active) {
1230 raw_spin_unlock_irq(&ctx->lock);
1235 * Since the task isn't running, its safe to remove the event, us
1236 * holding the ctx->lock ensures the task won't get scheduled in.
1238 list_del_event(event, ctx);
1239 raw_spin_unlock_irq(&ctx->lock);
1243 * Cross CPU call to disable a performance event
1245 static int __perf_event_disable(void *info)
1247 struct perf_event *event = info;
1248 struct perf_event_context *ctx = event->ctx;
1249 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1252 * If this is a per-task event, need to check whether this
1253 * event's task is the current task on this cpu.
1255 * Can trigger due to concurrent perf_event_context_sched_out()
1256 * flipping contexts around.
1258 if (ctx->task && cpuctx->task_ctx != ctx)
1261 raw_spin_lock(&ctx->lock);
1264 * If the event is on, turn it off.
1265 * If it is in error state, leave it in error state.
1267 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1268 update_context_time(ctx);
1269 update_cgrp_time_from_event(event);
1270 update_group_times(event);
1271 if (event == event->group_leader)
1272 group_sched_out(event, cpuctx, ctx);
1274 event_sched_out(event, cpuctx, ctx);
1275 event->state = PERF_EVENT_STATE_OFF;
1278 raw_spin_unlock(&ctx->lock);
1286 * If event->ctx is a cloned context, callers must make sure that
1287 * every task struct that event->ctx->task could possibly point to
1288 * remains valid. This condition is satisifed when called through
1289 * perf_event_for_each_child or perf_event_for_each because they
1290 * hold the top-level event's child_mutex, so any descendant that
1291 * goes to exit will block in sync_child_event.
1292 * When called from perf_pending_event it's OK because event->ctx
1293 * is the current context on this CPU and preemption is disabled,
1294 * hence we can't get into perf_event_task_sched_out for this context.
1296 void perf_event_disable(struct perf_event *event)
1298 struct perf_event_context *ctx = event->ctx;
1299 struct task_struct *task = ctx->task;
1303 * Disable the event on the cpu that it's on
1305 cpu_function_call(event->cpu, __perf_event_disable, event);
1310 if (!task_function_call(task, __perf_event_disable, event))
1313 raw_spin_lock_irq(&ctx->lock);
1315 * If the event is still active, we need to retry the cross-call.
1317 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1318 raw_spin_unlock_irq(&ctx->lock);
1320 * Reload the task pointer, it might have been changed by
1321 * a concurrent perf_event_context_sched_out().
1328 * Since we have the lock this context can't be scheduled
1329 * in, so we can change the state safely.
1331 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1332 update_group_times(event);
1333 event->state = PERF_EVENT_STATE_OFF;
1335 raw_spin_unlock_irq(&ctx->lock);
1337 EXPORT_SYMBOL_GPL(perf_event_disable);
1339 static void perf_set_shadow_time(struct perf_event *event,
1340 struct perf_event_context *ctx,
1344 * use the correct time source for the time snapshot
1346 * We could get by without this by leveraging the
1347 * fact that to get to this function, the caller
1348 * has most likely already called update_context_time()
1349 * and update_cgrp_time_xx() and thus both timestamp
1350 * are identical (or very close). Given that tstamp is,
1351 * already adjusted for cgroup, we could say that:
1352 * tstamp - ctx->timestamp
1354 * tstamp - cgrp->timestamp.
1356 * Then, in perf_output_read(), the calculation would
1357 * work with no changes because:
1358 * - event is guaranteed scheduled in
1359 * - no scheduled out in between
1360 * - thus the timestamp would be the same
1362 * But this is a bit hairy.
1364 * So instead, we have an explicit cgroup call to remain
1365 * within the time time source all along. We believe it
1366 * is cleaner and simpler to understand.
1368 if (is_cgroup_event(event))
1369 perf_cgroup_set_shadow_time(event, tstamp);
1371 event->shadow_ctx_time = tstamp - ctx->timestamp;
1374 #define MAX_INTERRUPTS (~0ULL)
1376 static void perf_log_throttle(struct perf_event *event, int enable);
1379 event_sched_in(struct perf_event *event,
1380 struct perf_cpu_context *cpuctx,
1381 struct perf_event_context *ctx)
1383 u64 tstamp = perf_event_time(event);
1385 if (event->state <= PERF_EVENT_STATE_OFF)
1388 event->state = PERF_EVENT_STATE_ACTIVE;
1389 event->oncpu = smp_processor_id();
1392 * Unthrottle events, since we scheduled we might have missed several
1393 * ticks already, also for a heavily scheduling task there is little
1394 * guarantee it'll get a tick in a timely manner.
1396 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1397 perf_log_throttle(event, 1);
1398 event->hw.interrupts = 0;
1402 * The new state must be visible before we turn it on in the hardware:
1406 if (event->pmu->add(event, PERF_EF_START)) {
1407 event->state = PERF_EVENT_STATE_INACTIVE;
1412 event->tstamp_running += tstamp - event->tstamp_stopped;
1414 perf_set_shadow_time(event, ctx, tstamp);
1416 if (!is_software_event(event))
1417 cpuctx->active_oncpu++;
1419 if (event->attr.freq && event->attr.sample_freq)
1422 if (event->attr.exclusive)
1423 cpuctx->exclusive = 1;
1429 group_sched_in(struct perf_event *group_event,
1430 struct perf_cpu_context *cpuctx,
1431 struct perf_event_context *ctx)
1433 struct perf_event *event, *partial_group = NULL;
1434 struct pmu *pmu = group_event->pmu;
1435 u64 now = ctx->time;
1436 bool simulate = false;
1438 if (group_event->state == PERF_EVENT_STATE_OFF)
1441 pmu->start_txn(pmu);
1443 if (event_sched_in(group_event, cpuctx, ctx)) {
1444 pmu->cancel_txn(pmu);
1449 * Schedule in siblings as one group (if any):
1451 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1452 if (event_sched_in(event, cpuctx, ctx)) {
1453 partial_group = event;
1458 if (!pmu->commit_txn(pmu))
1463 * Groups can be scheduled in as one unit only, so undo any
1464 * partial group before returning:
1465 * The events up to the failed event are scheduled out normally,
1466 * tstamp_stopped will be updated.
1468 * The failed events and the remaining siblings need to have
1469 * their timings updated as if they had gone thru event_sched_in()
1470 * and event_sched_out(). This is required to get consistent timings
1471 * across the group. This also takes care of the case where the group
1472 * could never be scheduled by ensuring tstamp_stopped is set to mark
1473 * the time the event was actually stopped, such that time delta
1474 * calculation in update_event_times() is correct.
1476 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1477 if (event == partial_group)
1481 event->tstamp_running += now - event->tstamp_stopped;
1482 event->tstamp_stopped = now;
1484 event_sched_out(event, cpuctx, ctx);
1487 event_sched_out(group_event, cpuctx, ctx);
1489 pmu->cancel_txn(pmu);
1495 * Work out whether we can put this event group on the CPU now.
1497 static int group_can_go_on(struct perf_event *event,
1498 struct perf_cpu_context *cpuctx,
1502 * Groups consisting entirely of software events can always go on.
1504 if (event->group_flags & PERF_GROUP_SOFTWARE)
1507 * If an exclusive group is already on, no other hardware
1510 if (cpuctx->exclusive)
1513 * If this group is exclusive and there are already
1514 * events on the CPU, it can't go on.
1516 if (event->attr.exclusive && cpuctx->active_oncpu)
1519 * Otherwise, try to add it if all previous groups were able
1525 static void add_event_to_ctx(struct perf_event *event,
1526 struct perf_event_context *ctx)
1528 u64 tstamp = perf_event_time(event);
1530 list_add_event(event, ctx);
1531 perf_group_attach(event);
1532 event->tstamp_enabled = tstamp;
1533 event->tstamp_running = tstamp;
1534 event->tstamp_stopped = tstamp;
1537 static void task_ctx_sched_out(struct perf_event_context *ctx);
1539 ctx_sched_in(struct perf_event_context *ctx,
1540 struct perf_cpu_context *cpuctx,
1541 enum event_type_t event_type,
1542 struct task_struct *task);
1544 static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1545 struct perf_event_context *ctx,
1546 struct task_struct *task)
1548 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1550 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1551 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1553 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1557 * Cross CPU call to install and enable a performance event
1559 * Must be called with ctx->mutex held
1561 static int __perf_install_in_context(void *info)
1563 struct perf_event *event = info;
1564 struct perf_event_context *ctx = event->ctx;
1565 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1566 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1567 struct task_struct *task = current;
1569 perf_ctx_lock(cpuctx, task_ctx);
1570 perf_pmu_disable(cpuctx->ctx.pmu);
1573 * If there was an active task_ctx schedule it out.
1576 task_ctx_sched_out(task_ctx);
1579 * If the context we're installing events in is not the
1580 * active task_ctx, flip them.
1582 if (ctx->task && task_ctx != ctx) {
1584 raw_spin_unlock(&task_ctx->lock);
1585 raw_spin_lock(&ctx->lock);
1590 cpuctx->task_ctx = task_ctx;
1591 task = task_ctx->task;
1594 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
1596 update_context_time(ctx);
1598 * update cgrp time only if current cgrp
1599 * matches event->cgrp. Must be done before
1600 * calling add_event_to_ctx()
1602 update_cgrp_time_from_event(event);
1604 add_event_to_ctx(event, ctx);
1607 * Schedule everything back in
1609 perf_event_sched_in(cpuctx, task_ctx, task);
1611 perf_pmu_enable(cpuctx->ctx.pmu);
1612 perf_ctx_unlock(cpuctx, task_ctx);
1618 * Attach a performance event to a context
1620 * First we add the event to the list with the hardware enable bit
1621 * in event->hw_config cleared.
1623 * If the event is attached to a task which is on a CPU we use a smp
1624 * call to enable it in the task context. The task might have been
1625 * scheduled away, but we check this in the smp call again.
1628 perf_install_in_context(struct perf_event_context *ctx,
1629 struct perf_event *event,
1632 struct task_struct *task = ctx->task;
1634 lockdep_assert_held(&ctx->mutex);
1640 * Per cpu events are installed via an smp call and
1641 * the install is always successful.
1643 cpu_function_call(cpu, __perf_install_in_context, event);
1648 if (!task_function_call(task, __perf_install_in_context, event))
1651 raw_spin_lock_irq(&ctx->lock);
1653 * If we failed to find a running task, but find the context active now
1654 * that we've acquired the ctx->lock, retry.
1656 if (ctx->is_active) {
1657 raw_spin_unlock_irq(&ctx->lock);
1662 * Since the task isn't running, its safe to add the event, us holding
1663 * the ctx->lock ensures the task won't get scheduled in.
1665 add_event_to_ctx(event, ctx);
1666 raw_spin_unlock_irq(&ctx->lock);
1670 * Put a event into inactive state and update time fields.
1671 * Enabling the leader of a group effectively enables all
1672 * the group members that aren't explicitly disabled, so we
1673 * have to update their ->tstamp_enabled also.
1674 * Note: this works for group members as well as group leaders
1675 * since the non-leader members' sibling_lists will be empty.
1677 static void __perf_event_mark_enabled(struct perf_event *event)
1679 struct perf_event *sub;
1680 u64 tstamp = perf_event_time(event);
1682 event->state = PERF_EVENT_STATE_INACTIVE;
1683 event->tstamp_enabled = tstamp - event->total_time_enabled;
1684 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1685 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1686 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1691 * Cross CPU call to enable a performance event
1693 static int __perf_event_enable(void *info)
1695 struct perf_event *event = info;
1696 struct perf_event_context *ctx = event->ctx;
1697 struct perf_event *leader = event->group_leader;
1698 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1701 if (WARN_ON_ONCE(!ctx->is_active))
1704 raw_spin_lock(&ctx->lock);
1705 update_context_time(ctx);
1707 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1711 * set current task's cgroup time reference point
1713 perf_cgroup_set_timestamp(current, ctx);
1715 __perf_event_mark_enabled(event);
1717 if (!event_filter_match(event)) {
1718 if (is_cgroup_event(event))
1719 perf_cgroup_defer_enabled(event);
1724 * If the event is in a group and isn't the group leader,
1725 * then don't put it on unless the group is on.
1727 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1730 if (!group_can_go_on(event, cpuctx, 1)) {
1733 if (event == leader)
1734 err = group_sched_in(event, cpuctx, ctx);
1736 err = event_sched_in(event, cpuctx, ctx);
1741 * If this event can't go on and it's part of a
1742 * group, then the whole group has to come off.
1744 if (leader != event)
1745 group_sched_out(leader, cpuctx, ctx);
1746 if (leader->attr.pinned) {
1747 update_group_times(leader);
1748 leader->state = PERF_EVENT_STATE_ERROR;
1753 raw_spin_unlock(&ctx->lock);
1761 * If event->ctx is a cloned context, callers must make sure that
1762 * every task struct that event->ctx->task could possibly point to
1763 * remains valid. This condition is satisfied when called through
1764 * perf_event_for_each_child or perf_event_for_each as described
1765 * for perf_event_disable.
1767 void perf_event_enable(struct perf_event *event)
1769 struct perf_event_context *ctx = event->ctx;
1770 struct task_struct *task = ctx->task;
1774 * Enable the event on the cpu that it's on
1776 cpu_function_call(event->cpu, __perf_event_enable, event);
1780 raw_spin_lock_irq(&ctx->lock);
1781 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1785 * If the event is in error state, clear that first.
1786 * That way, if we see the event in error state below, we
1787 * know that it has gone back into error state, as distinct
1788 * from the task having been scheduled away before the
1789 * cross-call arrived.
1791 if (event->state == PERF_EVENT_STATE_ERROR)
1792 event->state = PERF_EVENT_STATE_OFF;
1795 if (!ctx->is_active) {
1796 __perf_event_mark_enabled(event);
1800 raw_spin_unlock_irq(&ctx->lock);
1802 if (!task_function_call(task, __perf_event_enable, event))
1805 raw_spin_lock_irq(&ctx->lock);
1808 * If the context is active and the event is still off,
1809 * we need to retry the cross-call.
1811 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1813 * task could have been flipped by a concurrent
1814 * perf_event_context_sched_out()
1821 raw_spin_unlock_irq(&ctx->lock);
1823 EXPORT_SYMBOL_GPL(perf_event_enable);
1825 int perf_event_refresh(struct perf_event *event, int refresh)
1828 * not supported on inherited events
1830 if (event->attr.inherit || !is_sampling_event(event))
1833 atomic_add(refresh, &event->event_limit);
1834 perf_event_enable(event);
1838 EXPORT_SYMBOL_GPL(perf_event_refresh);
1840 static void ctx_sched_out(struct perf_event_context *ctx,
1841 struct perf_cpu_context *cpuctx,
1842 enum event_type_t event_type)
1844 struct perf_event *event;
1845 int is_active = ctx->is_active;
1847 ctx->is_active &= ~event_type;
1848 if (likely(!ctx->nr_events))
1851 update_context_time(ctx);
1852 update_cgrp_time_from_cpuctx(cpuctx);
1853 if (!ctx->nr_active)
1856 perf_pmu_disable(ctx->pmu);
1857 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
1858 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1859 group_sched_out(event, cpuctx, ctx);
1862 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
1863 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
1864 group_sched_out(event, cpuctx, ctx);
1866 perf_pmu_enable(ctx->pmu);
1870 * Test whether two contexts are equivalent, i.e. whether they
1871 * have both been cloned from the same version of the same context
1872 * and they both have the same number of enabled events.
1873 * If the number of enabled events is the same, then the set
1874 * of enabled events should be the same, because these are both
1875 * inherited contexts, therefore we can't access individual events
1876 * in them directly with an fd; we can only enable/disable all
1877 * events via prctl, or enable/disable all events in a family
1878 * via ioctl, which will have the same effect on both contexts.
1880 static int context_equiv(struct perf_event_context *ctx1,
1881 struct perf_event_context *ctx2)
1883 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1884 && ctx1->parent_gen == ctx2->parent_gen
1885 && !ctx1->pin_count && !ctx2->pin_count;
1888 static void __perf_event_sync_stat(struct perf_event *event,
1889 struct perf_event *next_event)
1893 if (!event->attr.inherit_stat)
1897 * Update the event value, we cannot use perf_event_read()
1898 * because we're in the middle of a context switch and have IRQs
1899 * disabled, which upsets smp_call_function_single(), however
1900 * we know the event must be on the current CPU, therefore we
1901 * don't need to use it.
1903 switch (event->state) {
1904 case PERF_EVENT_STATE_ACTIVE:
1905 event->pmu->read(event);
1908 case PERF_EVENT_STATE_INACTIVE:
1909 update_event_times(event);
1917 * In order to keep per-task stats reliable we need to flip the event
1918 * values when we flip the contexts.
1920 value = local64_read(&next_event->count);
1921 value = local64_xchg(&event->count, value);
1922 local64_set(&next_event->count, value);
1924 swap(event->total_time_enabled, next_event->total_time_enabled);
1925 swap(event->total_time_running, next_event->total_time_running);
1928 * Since we swizzled the values, update the user visible data too.
1930 perf_event_update_userpage(event);
1931 perf_event_update_userpage(next_event);
1934 #define list_next_entry(pos, member) \
1935 list_entry(pos->member.next, typeof(*pos), member)
1937 static void perf_event_sync_stat(struct perf_event_context *ctx,
1938 struct perf_event_context *next_ctx)
1940 struct perf_event *event, *next_event;
1945 update_context_time(ctx);
1947 event = list_first_entry(&ctx->event_list,
1948 struct perf_event, event_entry);
1950 next_event = list_first_entry(&next_ctx->event_list,
1951 struct perf_event, event_entry);
1953 while (&event->event_entry != &ctx->event_list &&
1954 &next_event->event_entry != &next_ctx->event_list) {
1956 __perf_event_sync_stat(event, next_event);
1958 event = list_next_entry(event, event_entry);
1959 next_event = list_next_entry(next_event, event_entry);
1963 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1964 struct task_struct *next)
1966 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
1967 struct perf_event_context *next_ctx;
1968 struct perf_event_context *parent;
1969 struct perf_cpu_context *cpuctx;
1975 cpuctx = __get_cpu_context(ctx);
1976 if (!cpuctx->task_ctx)
1980 parent = rcu_dereference(ctx->parent_ctx);
1981 next_ctx = next->perf_event_ctxp[ctxn];
1982 if (parent && next_ctx &&
1983 rcu_dereference(next_ctx->parent_ctx) == parent) {
1985 * Looks like the two contexts are clones, so we might be
1986 * able to optimize the context switch. We lock both
1987 * contexts and check that they are clones under the
1988 * lock (including re-checking that neither has been
1989 * uncloned in the meantime). It doesn't matter which
1990 * order we take the locks because no other cpu could
1991 * be trying to lock both of these tasks.
1993 raw_spin_lock(&ctx->lock);
1994 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
1995 if (context_equiv(ctx, next_ctx)) {
1997 * XXX do we need a memory barrier of sorts
1998 * wrt to rcu_dereference() of perf_event_ctxp
2000 task->perf_event_ctxp[ctxn] = next_ctx;
2001 next->perf_event_ctxp[ctxn] = ctx;
2003 next_ctx->task = task;
2006 perf_event_sync_stat(ctx, next_ctx);
2008 raw_spin_unlock(&next_ctx->lock);
2009 raw_spin_unlock(&ctx->lock);
2014 raw_spin_lock(&ctx->lock);
2015 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2016 cpuctx->task_ctx = NULL;
2017 raw_spin_unlock(&ctx->lock);
2021 #define for_each_task_context_nr(ctxn) \
2022 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2025 * Called from scheduler to remove the events of the current task,
2026 * with interrupts disabled.
2028 * We stop each event and update the event value in event->count.
2030 * This does not protect us against NMI, but disable()
2031 * sets the disabled bit in the control field of event _before_
2032 * accessing the event control register. If a NMI hits, then it will
2033 * not restart the event.
2035 void __perf_event_task_sched_out(struct task_struct *task,
2036 struct task_struct *next)
2040 for_each_task_context_nr(ctxn)
2041 perf_event_context_sched_out(task, ctxn, next);
2044 * if cgroup events exist on this CPU, then we need
2045 * to check if we have to switch out PMU state.
2046 * cgroup event are system-wide mode only
2048 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2049 perf_cgroup_sched_out(task, next);
2052 static void task_ctx_sched_out(struct perf_event_context *ctx)
2054 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2056 if (!cpuctx->task_ctx)
2059 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2062 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2063 cpuctx->task_ctx = NULL;
2067 * Called with IRQs disabled
2069 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2070 enum event_type_t event_type)
2072 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
2076 ctx_pinned_sched_in(struct perf_event_context *ctx,
2077 struct perf_cpu_context *cpuctx)
2079 struct perf_event *event;
2081 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2082 if (event->state <= PERF_EVENT_STATE_OFF)
2084 if (!event_filter_match(event))
2087 /* may need to reset tstamp_enabled */
2088 if (is_cgroup_event(event))
2089 perf_cgroup_mark_enabled(event, ctx);
2091 if (group_can_go_on(event, cpuctx, 1))
2092 group_sched_in(event, cpuctx, ctx);
2095 * If this pinned group hasn't been scheduled,
2096 * put it in error state.
2098 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2099 update_group_times(event);
2100 event->state = PERF_EVENT_STATE_ERROR;
2106 ctx_flexible_sched_in(struct perf_event_context *ctx,
2107 struct perf_cpu_context *cpuctx)
2109 struct perf_event *event;
2112 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2113 /* Ignore events in OFF or ERROR state */
2114 if (event->state <= PERF_EVENT_STATE_OFF)
2117 * Listen to the 'cpu' scheduling filter constraint
2120 if (!event_filter_match(event))
2123 /* may need to reset tstamp_enabled */
2124 if (is_cgroup_event(event))
2125 perf_cgroup_mark_enabled(event, ctx);
2127 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2128 if (group_sched_in(event, cpuctx, ctx))
2135 ctx_sched_in(struct perf_event_context *ctx,
2136 struct perf_cpu_context *cpuctx,
2137 enum event_type_t event_type,
2138 struct task_struct *task)
2141 int is_active = ctx->is_active;
2143 ctx->is_active |= event_type;
2144 if (likely(!ctx->nr_events))
2148 ctx->timestamp = now;
2149 perf_cgroup_set_timestamp(task, ctx);
2151 * First go through the list and put on any pinned groups
2152 * in order to give them the best chance of going on.
2154 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
2155 ctx_pinned_sched_in(ctx, cpuctx);
2157 /* Then walk through the lower prio flexible groups */
2158 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
2159 ctx_flexible_sched_in(ctx, cpuctx);
2162 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2163 enum event_type_t event_type,
2164 struct task_struct *task)
2166 struct perf_event_context *ctx = &cpuctx->ctx;
2168 ctx_sched_in(ctx, cpuctx, event_type, task);
2171 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2172 struct task_struct *task)
2174 struct perf_cpu_context *cpuctx;
2176 cpuctx = __get_cpu_context(ctx);
2177 if (cpuctx->task_ctx == ctx)
2180 perf_ctx_lock(cpuctx, ctx);
2181 perf_pmu_disable(ctx->pmu);
2183 * We want to keep the following priority order:
2184 * cpu pinned (that don't need to move), task pinned,
2185 * cpu flexible, task flexible.
2187 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2190 cpuctx->task_ctx = ctx;
2192 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2194 perf_pmu_enable(ctx->pmu);
2195 perf_ctx_unlock(cpuctx, ctx);
2198 * Since these rotations are per-cpu, we need to ensure the
2199 * cpu-context we got scheduled on is actually rotating.
2201 perf_pmu_rotate_start(ctx->pmu);
2205 * Called from scheduler to add the events of the current task
2206 * with interrupts disabled.
2208 * We restore the event value and then enable it.
2210 * This does not protect us against NMI, but enable()
2211 * sets the enabled bit in the control field of event _before_
2212 * accessing the event control register. If a NMI hits, then it will
2213 * keep the event running.
2215 void __perf_event_task_sched_in(struct task_struct *prev,
2216 struct task_struct *task)
2218 struct perf_event_context *ctx;
2221 for_each_task_context_nr(ctxn) {
2222 ctx = task->perf_event_ctxp[ctxn];
2226 perf_event_context_sched_in(ctx, task);
2229 * if cgroup events exist on this CPU, then we need
2230 * to check if we have to switch in PMU state.
2231 * cgroup event are system-wide mode only
2233 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2234 perf_cgroup_sched_in(prev, task);
2237 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2239 u64 frequency = event->attr.sample_freq;
2240 u64 sec = NSEC_PER_SEC;
2241 u64 divisor, dividend;
2243 int count_fls, nsec_fls, frequency_fls, sec_fls;
2245 count_fls = fls64(count);
2246 nsec_fls = fls64(nsec);
2247 frequency_fls = fls64(frequency);
2251 * We got @count in @nsec, with a target of sample_freq HZ
2252 * the target period becomes:
2255 * period = -------------------
2256 * @nsec * sample_freq
2261 * Reduce accuracy by one bit such that @a and @b converge
2262 * to a similar magnitude.
2264 #define REDUCE_FLS(a, b) \
2266 if (a##_fls > b##_fls) { \
2276 * Reduce accuracy until either term fits in a u64, then proceed with
2277 * the other, so that finally we can do a u64/u64 division.
2279 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2280 REDUCE_FLS(nsec, frequency);
2281 REDUCE_FLS(sec, count);
2284 if (count_fls + sec_fls > 64) {
2285 divisor = nsec * frequency;
2287 while (count_fls + sec_fls > 64) {
2288 REDUCE_FLS(count, sec);
2292 dividend = count * sec;
2294 dividend = count * sec;
2296 while (nsec_fls + frequency_fls > 64) {
2297 REDUCE_FLS(nsec, frequency);
2301 divisor = nsec * frequency;
2307 return div64_u64(dividend, divisor);
2310 static DEFINE_PER_CPU(int, perf_throttled_count);
2311 static DEFINE_PER_CPU(u64, perf_throttled_seq);
2313 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
2315 struct hw_perf_event *hwc = &event->hw;
2316 s64 period, sample_period;
2319 period = perf_calculate_period(event, nsec, count);
2321 delta = (s64)(period - hwc->sample_period);
2322 delta = (delta + 7) / 8; /* low pass filter */
2324 sample_period = hwc->sample_period + delta;
2329 hwc->sample_period = sample_period;
2331 if (local64_read(&hwc->period_left) > 8*sample_period) {
2333 event->pmu->stop(event, PERF_EF_UPDATE);
2335 local64_set(&hwc->period_left, 0);
2338 event->pmu->start(event, PERF_EF_RELOAD);
2343 * combine freq adjustment with unthrottling to avoid two passes over the
2344 * events. At the same time, make sure, having freq events does not change
2345 * the rate of unthrottling as that would introduce bias.
2347 static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2350 struct perf_event *event;
2351 struct hw_perf_event *hwc;
2352 u64 now, period = TICK_NSEC;
2356 * only need to iterate over all events iff:
2357 * - context have events in frequency mode (needs freq adjust)
2358 * - there are events to unthrottle on this cpu
2360 if (!(ctx->nr_freq || needs_unthr))
2363 raw_spin_lock(&ctx->lock);
2364 perf_pmu_disable(ctx->pmu);
2366 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2367 if (event->state != PERF_EVENT_STATE_ACTIVE)
2370 if (!event_filter_match(event))
2375 if (needs_unthr && hwc->interrupts == MAX_INTERRUPTS) {
2376 hwc->interrupts = 0;
2377 perf_log_throttle(event, 1);
2378 event->pmu->start(event, 0);
2381 if (!event->attr.freq || !event->attr.sample_freq)
2385 * stop the event and update event->count
2387 event->pmu->stop(event, PERF_EF_UPDATE);
2389 now = local64_read(&event->count);
2390 delta = now - hwc->freq_count_stamp;
2391 hwc->freq_count_stamp = now;
2395 * reload only if value has changed
2396 * we have stopped the event so tell that
2397 * to perf_adjust_period() to avoid stopping it
2401 perf_adjust_period(event, period, delta, false);
2403 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
2406 perf_pmu_enable(ctx->pmu);
2407 raw_spin_unlock(&ctx->lock);
2411 * Round-robin a context's events:
2413 static void rotate_ctx(struct perf_event_context *ctx)
2416 * Rotate the first entry last of non-pinned groups. Rotation might be
2417 * disabled by the inheritance code.
2419 if (!ctx->rotate_disable)
2420 list_rotate_left(&ctx->flexible_groups);
2424 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2425 * because they're strictly cpu affine and rotate_start is called with IRQs
2426 * disabled, while rotate_context is called from IRQ context.
2428 static void perf_rotate_context(struct perf_cpu_context *cpuctx)
2430 struct perf_event_context *ctx = NULL;
2431 int rotate = 0, remove = 1;
2433 if (cpuctx->ctx.nr_events) {
2435 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2439 ctx = cpuctx->task_ctx;
2440 if (ctx && ctx->nr_events) {
2442 if (ctx->nr_events != ctx->nr_active)
2449 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2450 perf_pmu_disable(cpuctx->ctx.pmu);
2452 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2454 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
2456 rotate_ctx(&cpuctx->ctx);
2460 perf_event_sched_in(cpuctx, ctx, current);
2462 perf_pmu_enable(cpuctx->ctx.pmu);
2463 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2466 list_del_init(&cpuctx->rotation_list);
2469 void perf_event_task_tick(void)
2471 struct list_head *head = &__get_cpu_var(rotation_list);
2472 struct perf_cpu_context *cpuctx, *tmp;
2473 struct perf_event_context *ctx;
2476 WARN_ON(!irqs_disabled());
2478 __this_cpu_inc(perf_throttled_seq);
2479 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2481 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2483 perf_adjust_freq_unthr_context(ctx, throttled);
2485 ctx = cpuctx->task_ctx;
2487 perf_adjust_freq_unthr_context(ctx, throttled);
2489 if (cpuctx->jiffies_interval == 1 ||
2490 !(jiffies % cpuctx->jiffies_interval))
2491 perf_rotate_context(cpuctx);
2495 static int event_enable_on_exec(struct perf_event *event,
2496 struct perf_event_context *ctx)
2498 if (!event->attr.enable_on_exec)
2501 event->attr.enable_on_exec = 0;
2502 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2505 __perf_event_mark_enabled(event);
2511 * Enable all of a task's events that have been marked enable-on-exec.
2512 * This expects task == current.
2514 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2516 struct perf_event *event;
2517 unsigned long flags;
2521 local_irq_save(flags);
2522 if (!ctx || !ctx->nr_events)
2526 * We must ctxsw out cgroup events to avoid conflict
2527 * when invoking perf_task_event_sched_in() later on
2528 * in this function. Otherwise we end up trying to
2529 * ctxswin cgroup events which are already scheduled
2532 perf_cgroup_sched_out(current, NULL);
2534 raw_spin_lock(&ctx->lock);
2535 task_ctx_sched_out(ctx);
2537 list_for_each_entry(event, &ctx->event_list, event_entry) {
2538 ret = event_enable_on_exec(event, ctx);
2544 * Unclone this context if we enabled any event.
2549 raw_spin_unlock(&ctx->lock);
2552 * Also calls ctxswin for cgroup events, if any:
2554 perf_event_context_sched_in(ctx, ctx->task);
2556 local_irq_restore(flags);
2560 * Cross CPU call to read the hardware event
2562 static void __perf_event_read(void *info)
2564 struct perf_event *event = info;
2565 struct perf_event_context *ctx = event->ctx;
2566 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2569 * If this is a task context, we need to check whether it is
2570 * the current task context of this cpu. If not it has been
2571 * scheduled out before the smp call arrived. In that case
2572 * event->count would have been updated to a recent sample
2573 * when the event was scheduled out.
2575 if (ctx->task && cpuctx->task_ctx != ctx)
2578 raw_spin_lock(&ctx->lock);
2579 if (ctx->is_active) {
2580 update_context_time(ctx);
2581 update_cgrp_time_from_event(event);
2583 update_event_times(event);
2584 if (event->state == PERF_EVENT_STATE_ACTIVE)
2585 event->pmu->read(event);
2586 raw_spin_unlock(&ctx->lock);
2589 static inline u64 perf_event_count(struct perf_event *event)
2591 return local64_read(&event->count) + atomic64_read(&event->child_count);
2594 static u64 perf_event_read(struct perf_event *event)
2597 * If event is enabled and currently active on a CPU, update the
2598 * value in the event structure:
2600 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2601 smp_call_function_single(event->oncpu,
2602 __perf_event_read, event, 1);
2603 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2604 struct perf_event_context *ctx = event->ctx;
2605 unsigned long flags;
2607 raw_spin_lock_irqsave(&ctx->lock, flags);
2609 * may read while context is not active
2610 * (e.g., thread is blocked), in that case
2611 * we cannot update context time
2613 if (ctx->is_active) {
2614 update_context_time(ctx);
2615 update_cgrp_time_from_event(event);
2617 update_event_times(event);
2618 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2621 return perf_event_count(event);
2625 * Initialize the perf_event context in a task_struct:
2627 static void __perf_event_init_context(struct perf_event_context *ctx)
2629 raw_spin_lock_init(&ctx->lock);
2630 mutex_init(&ctx->mutex);
2631 INIT_LIST_HEAD(&ctx->pinned_groups);
2632 INIT_LIST_HEAD(&ctx->flexible_groups);
2633 INIT_LIST_HEAD(&ctx->event_list);
2634 atomic_set(&ctx->refcount, 1);
2637 static struct perf_event_context *
2638 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2640 struct perf_event_context *ctx;
2642 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2646 __perf_event_init_context(ctx);
2649 get_task_struct(task);
2656 static struct task_struct *
2657 find_lively_task_by_vpid(pid_t vpid)
2659 struct task_struct *task;
2666 task = find_task_by_vpid(vpid);
2668 get_task_struct(task);
2672 return ERR_PTR(-ESRCH);
2674 /* Reuse ptrace permission checks for now. */
2676 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2681 put_task_struct(task);
2682 return ERR_PTR(err);
2687 * Returns a matching context with refcount and pincount.
2689 static struct perf_event_context *
2690 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
2692 struct perf_event_context *ctx;
2693 struct perf_cpu_context *cpuctx;
2694 unsigned long flags;
2698 /* Must be root to operate on a CPU event: */
2699 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2700 return ERR_PTR(-EACCES);
2703 * We could be clever and allow to attach a event to an
2704 * offline CPU and activate it when the CPU comes up, but
2707 if (!cpu_online(cpu))
2708 return ERR_PTR(-ENODEV);
2710 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
2719 ctxn = pmu->task_ctx_nr;
2724 ctx = perf_lock_task_context(task, ctxn, &flags);
2728 raw_spin_unlock_irqrestore(&ctx->lock, flags);
2730 ctx = alloc_perf_context(pmu, task);
2736 mutex_lock(&task->perf_event_mutex);
2738 * If it has already passed perf_event_exit_task().
2739 * we must see PF_EXITING, it takes this mutex too.
2741 if (task->flags & PF_EXITING)
2743 else if (task->perf_event_ctxp[ctxn])
2748 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2750 mutex_unlock(&task->perf_event_mutex);
2752 if (unlikely(err)) {
2764 return ERR_PTR(err);
2767 static void perf_event_free_filter(struct perf_event *event);
2769 static void free_event_rcu(struct rcu_head *head)
2771 struct perf_event *event;
2773 event = container_of(head, struct perf_event, rcu_head);
2775 put_pid_ns(event->ns);
2776 perf_event_free_filter(event);
2780 static void ring_buffer_put(struct ring_buffer *rb);
2782 static void free_event(struct perf_event *event)
2784 irq_work_sync(&event->pending);
2786 if (!event->parent) {
2787 if (event->attach_state & PERF_ATTACH_TASK)
2788 static_key_slow_dec_deferred(&perf_sched_events);
2789 if (event->attr.mmap || event->attr.mmap_data)
2790 atomic_dec(&nr_mmap_events);
2791 if (event->attr.comm)
2792 atomic_dec(&nr_comm_events);
2793 if (event->attr.task)
2794 atomic_dec(&nr_task_events);
2795 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2796 put_callchain_buffers();
2797 if (is_cgroup_event(event)) {
2798 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
2799 static_key_slow_dec_deferred(&perf_sched_events);
2804 ring_buffer_put(event->rb);
2808 if (is_cgroup_event(event))
2809 perf_detach_cgroup(event);
2812 event->destroy(event);
2815 put_ctx(event->ctx);
2817 call_rcu(&event->rcu_head, free_event_rcu);
2820 int perf_event_release_kernel(struct perf_event *event)
2822 struct perf_event_context *ctx = event->ctx;
2824 WARN_ON_ONCE(ctx->parent_ctx);
2826 * There are two ways this annotation is useful:
2828 * 1) there is a lock recursion from perf_event_exit_task
2829 * see the comment there.
2831 * 2) there is a lock-inversion with mmap_sem through
2832 * perf_event_read_group(), which takes faults while
2833 * holding ctx->mutex, however this is called after
2834 * the last filedesc died, so there is no possibility
2835 * to trigger the AB-BA case.
2837 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
2838 raw_spin_lock_irq(&ctx->lock);
2839 perf_group_detach(event);
2840 raw_spin_unlock_irq(&ctx->lock);
2841 perf_remove_from_context(event);
2842 mutex_unlock(&ctx->mutex);
2848 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2851 * Called when the last reference to the file is gone.
2853 static int perf_release(struct inode *inode, struct file *file)
2855 struct perf_event *event = file->private_data;
2856 struct task_struct *owner;
2858 file->private_data = NULL;
2861 owner = ACCESS_ONCE(event->owner);
2863 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2864 * !owner it means the list deletion is complete and we can indeed
2865 * free this event, otherwise we need to serialize on
2866 * owner->perf_event_mutex.
2868 smp_read_barrier_depends();
2871 * Since delayed_put_task_struct() also drops the last
2872 * task reference we can safely take a new reference
2873 * while holding the rcu_read_lock().
2875 get_task_struct(owner);
2880 mutex_lock(&owner->perf_event_mutex);
2882 * We have to re-check the event->owner field, if it is cleared
2883 * we raced with perf_event_exit_task(), acquiring the mutex
2884 * ensured they're done, and we can proceed with freeing the
2888 list_del_init(&event->owner_entry);
2889 mutex_unlock(&owner->perf_event_mutex);
2890 put_task_struct(owner);
2893 return perf_event_release_kernel(event);
2896 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
2898 struct perf_event *child;
2904 mutex_lock(&event->child_mutex);
2905 total += perf_event_read(event);
2906 *enabled += event->total_time_enabled +
2907 atomic64_read(&event->child_total_time_enabled);
2908 *running += event->total_time_running +
2909 atomic64_read(&event->child_total_time_running);
2911 list_for_each_entry(child, &event->child_list, child_list) {
2912 total += perf_event_read(child);
2913 *enabled += child->total_time_enabled;
2914 *running += child->total_time_running;
2916 mutex_unlock(&event->child_mutex);
2920 EXPORT_SYMBOL_GPL(perf_event_read_value);
2922 static int perf_event_read_group(struct perf_event *event,
2923 u64 read_format, char __user *buf)
2925 struct perf_event *leader = event->group_leader, *sub;
2926 int n = 0, size = 0, ret = -EFAULT;
2927 struct perf_event_context *ctx = leader->ctx;
2929 u64 count, enabled, running;
2931 mutex_lock(&ctx->mutex);
2932 count = perf_event_read_value(leader, &enabled, &running);
2934 values[n++] = 1 + leader->nr_siblings;
2935 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2936 values[n++] = enabled;
2937 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2938 values[n++] = running;
2939 values[n++] = count;
2940 if (read_format & PERF_FORMAT_ID)
2941 values[n++] = primary_event_id(leader);
2943 size = n * sizeof(u64);
2945 if (copy_to_user(buf, values, size))
2950 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2953 values[n++] = perf_event_read_value(sub, &enabled, &running);
2954 if (read_format & PERF_FORMAT_ID)
2955 values[n++] = primary_event_id(sub);
2957 size = n * sizeof(u64);
2959 if (copy_to_user(buf + ret, values, size)) {
2967 mutex_unlock(&ctx->mutex);
2972 static int perf_event_read_one(struct perf_event *event,
2973 u64 read_format, char __user *buf)
2975 u64 enabled, running;
2979 values[n++] = perf_event_read_value(event, &enabled, &running);
2980 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2981 values[n++] = enabled;
2982 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2983 values[n++] = running;
2984 if (read_format & PERF_FORMAT_ID)
2985 values[n++] = primary_event_id(event);
2987 if (copy_to_user(buf, values, n * sizeof(u64)))
2990 return n * sizeof(u64);
2994 * Read the performance event - simple non blocking version for now
2997 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2999 u64 read_format = event->attr.read_format;
3003 * Return end-of-file for a read on a event that is in
3004 * error state (i.e. because it was pinned but it couldn't be
3005 * scheduled on to the CPU at some point).
3007 if (event->state == PERF_EVENT_STATE_ERROR)
3010 if (count < event->read_size)
3013 WARN_ON_ONCE(event->ctx->parent_ctx);
3014 if (read_format & PERF_FORMAT_GROUP)
3015 ret = perf_event_read_group(event, read_format, buf);
3017 ret = perf_event_read_one(event, read_format, buf);
3023 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3025 struct perf_event *event = file->private_data;
3027 return perf_read_hw(event, buf, count);
3030 static unsigned int perf_poll(struct file *file, poll_table *wait)
3032 struct perf_event *event = file->private_data;
3033 struct ring_buffer *rb;
3034 unsigned int events = POLL_HUP;
3037 * Race between perf_event_set_output() and perf_poll(): perf_poll()
3038 * grabs the rb reference but perf_event_set_output() overrides it.
3039 * Here is the timeline for two threads T1, T2:
3040 * t0: T1, rb = rcu_dereference(event->rb)
3041 * t1: T2, old_rb = event->rb
3042 * t2: T2, event->rb = new rb
3043 * t3: T2, ring_buffer_detach(old_rb)
3044 * t4: T1, ring_buffer_attach(rb1)
3045 * t5: T1, poll_wait(event->waitq)
3047 * To avoid this problem, we grab mmap_mutex in perf_poll()
3048 * thereby ensuring that the assignment of the new ring buffer
3049 * and the detachment of the old buffer appear atomic to perf_poll()
3051 mutex_lock(&event->mmap_mutex);
3054 rb = rcu_dereference(event->rb);
3056 ring_buffer_attach(event, rb);
3057 events = atomic_xchg(&rb->poll, 0);
3061 mutex_unlock(&event->mmap_mutex);
3063 poll_wait(file, &event->waitq, wait);
3068 static void perf_event_reset(struct perf_event *event)
3070 (void)perf_event_read(event);
3071 local64_set(&event->count, 0);
3072 perf_event_update_userpage(event);
3076 * Holding the top-level event's child_mutex means that any
3077 * descendant process that has inherited this event will block
3078 * in sync_child_event if it goes to exit, thus satisfying the
3079 * task existence requirements of perf_event_enable/disable.
3081 static void perf_event_for_each_child(struct perf_event *event,
3082 void (*func)(struct perf_event *))
3084 struct perf_event *child;
3086 WARN_ON_ONCE(event->ctx->parent_ctx);
3087 mutex_lock(&event->child_mutex);
3089 list_for_each_entry(child, &event->child_list, child_list)
3091 mutex_unlock(&event->child_mutex);
3094 static void perf_event_for_each(struct perf_event *event,
3095 void (*func)(struct perf_event *))
3097 struct perf_event_context *ctx = event->ctx;
3098 struct perf_event *sibling;
3100 WARN_ON_ONCE(ctx->parent_ctx);
3101 mutex_lock(&ctx->mutex);
3102 event = event->group_leader;
3104 perf_event_for_each_child(event, func);
3106 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3107 perf_event_for_each_child(event, func);
3108 mutex_unlock(&ctx->mutex);
3111 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3113 struct perf_event_context *ctx = event->ctx;
3117 if (!is_sampling_event(event))
3120 if (copy_from_user(&value, arg, sizeof(value)))
3126 raw_spin_lock_irq(&ctx->lock);
3127 if (event->attr.freq) {
3128 if (value > sysctl_perf_event_sample_rate) {
3133 event->attr.sample_freq = value;
3135 event->attr.sample_period = value;
3136 event->hw.sample_period = value;
3139 raw_spin_unlock_irq(&ctx->lock);
3144 static const struct file_operations perf_fops;
3146 static struct perf_event *perf_fget_light(int fd, int *fput_needed)
3150 file = fget_light(fd, fput_needed);
3152 return ERR_PTR(-EBADF);
3154 if (file->f_op != &perf_fops) {
3155 fput_light(file, *fput_needed);
3157 return ERR_PTR(-EBADF);
3160 return file->private_data;
3163 static int perf_event_set_output(struct perf_event *event,
3164 struct perf_event *output_event);
3165 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3167 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3169 struct perf_event *event = file->private_data;
3170 void (*func)(struct perf_event *);
3174 case PERF_EVENT_IOC_ENABLE:
3175 func = perf_event_enable;
3177 case PERF_EVENT_IOC_DISABLE:
3178 func = perf_event_disable;
3180 case PERF_EVENT_IOC_RESET:
3181 func = perf_event_reset;
3184 case PERF_EVENT_IOC_REFRESH:
3185 return perf_event_refresh(event, arg);
3187 case PERF_EVENT_IOC_PERIOD:
3188 return perf_event_period(event, (u64 __user *)arg);
3190 case PERF_EVENT_IOC_SET_OUTPUT:
3192 struct perf_event *output_event = NULL;
3193 int fput_needed = 0;
3197 output_event = perf_fget_light(arg, &fput_needed);
3198 if (IS_ERR(output_event))
3199 return PTR_ERR(output_event);
3202 ret = perf_event_set_output(event, output_event);
3204 fput_light(output_event->filp, fput_needed);
3209 case PERF_EVENT_IOC_SET_FILTER:
3210 return perf_event_set_filter(event, (void __user *)arg);
3216 if (flags & PERF_IOC_FLAG_GROUP)
3217 perf_event_for_each(event, func);
3219 perf_event_for_each_child(event, func);
3224 int perf_event_task_enable(void)
3226 struct perf_event *event;
3228 mutex_lock(¤t->perf_event_mutex);
3229 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3230 perf_event_for_each_child(event, perf_event_enable);
3231 mutex_unlock(¤t->perf_event_mutex);
3236 int perf_event_task_disable(void)
3238 struct perf_event *event;
3240 mutex_lock(¤t->perf_event_mutex);
3241 list_for_each_entry(event, ¤t->perf_event_list, owner_entry)
3242 perf_event_for_each_child(event, perf_event_disable);
3243 mutex_unlock(¤t->perf_event_mutex);
3248 static int perf_event_index(struct perf_event *event)
3250 if (event->hw.state & PERF_HES_STOPPED)
3253 if (event->state != PERF_EVENT_STATE_ACTIVE)
3256 return event->pmu->event_idx(event);
3259 static void calc_timer_values(struct perf_event *event,
3266 *now = perf_clock();
3267 ctx_time = event->shadow_ctx_time + *now;
3268 *enabled = ctx_time - event->tstamp_enabled;
3269 *running = ctx_time - event->tstamp_running;
3272 void __weak perf_update_user_clock(struct perf_event_mmap_page *userpg, u64 now)
3277 * Callers need to ensure there can be no nesting of this function, otherwise
3278 * the seqlock logic goes bad. We can not serialize this because the arch
3279 * code calls this from NMI context.
3281 void perf_event_update_userpage(struct perf_event *event)
3283 struct perf_event_mmap_page *userpg;
3284 struct ring_buffer *rb;
3285 u64 enabled, running, now;
3289 * compute total_time_enabled, total_time_running
3290 * based on snapshot values taken when the event
3291 * was last scheduled in.
3293 * we cannot simply called update_context_time()
3294 * because of locking issue as we can be called in
3297 calc_timer_values(event, &now, &enabled, &running);
3298 rb = rcu_dereference(event->rb);
3302 userpg = rb->user_page;
3305 * Disable preemption so as to not let the corresponding user-space
3306 * spin too long if we get preempted.
3311 userpg->index = perf_event_index(event);
3312 userpg->offset = perf_event_count(event);
3314 userpg->offset -= local64_read(&event->hw.prev_count);
3316 userpg->time_enabled = enabled +
3317 atomic64_read(&event->child_total_time_enabled);
3319 userpg->time_running = running +
3320 atomic64_read(&event->child_total_time_running);
3322 perf_update_user_clock(userpg, now);
3331 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3333 struct perf_event *event = vma->vm_file->private_data;
3334 struct ring_buffer *rb;
3335 int ret = VM_FAULT_SIGBUS;
3337 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3338 if (vmf->pgoff == 0)
3344 rb = rcu_dereference(event->rb);
3348 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3351 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
3355 get_page(vmf->page);
3356 vmf->page->mapping = vma->vm_file->f_mapping;
3357 vmf->page->index = vmf->pgoff;
3366 static void ring_buffer_attach(struct perf_event *event,
3367 struct ring_buffer *rb)
3369 unsigned long flags;
3371 if (!list_empty(&event->rb_entry))
3374 spin_lock_irqsave(&rb->event_lock, flags);
3375 if (!list_empty(&event->rb_entry))
3378 list_add(&event->rb_entry, &rb->event_list);
3380 spin_unlock_irqrestore(&rb->event_lock, flags);
3383 static void ring_buffer_detach(struct perf_event *event,
3384 struct ring_buffer *rb)
3386 unsigned long flags;
3388 if (list_empty(&event->rb_entry))
3391 spin_lock_irqsave(&rb->event_lock, flags);
3392 list_del_init(&event->rb_entry);
3393 wake_up_all(&event->waitq);
3394 spin_unlock_irqrestore(&rb->event_lock, flags);
3397 static void ring_buffer_wakeup(struct perf_event *event)
3399 struct ring_buffer *rb;
3402 rb = rcu_dereference(event->rb);
3406 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3407 wake_up_all(&event->waitq);
3413 static void rb_free_rcu(struct rcu_head *rcu_head)
3415 struct ring_buffer *rb;
3417 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3421 static struct ring_buffer *ring_buffer_get(struct perf_event *event)
3423 struct ring_buffer *rb;
3426 rb = rcu_dereference(event->rb);
3428 if (!atomic_inc_not_zero(&rb->refcount))
3436 static void ring_buffer_put(struct ring_buffer *rb)
3438 struct perf_event *event, *n;
3439 unsigned long flags;
3441 if (!atomic_dec_and_test(&rb->refcount))
3444 spin_lock_irqsave(&rb->event_lock, flags);
3445 list_for_each_entry_safe(event, n, &rb->event_list, rb_entry) {
3446 list_del_init(&event->rb_entry);
3447 wake_up_all(&event->waitq);
3449 spin_unlock_irqrestore(&rb->event_lock, flags);
3451 call_rcu(&rb->rcu_head, rb_free_rcu);
3454 static void perf_mmap_open(struct vm_area_struct *vma)
3456 struct perf_event *event = vma->vm_file->private_data;
3458 atomic_inc(&event->mmap_count);
3461 static void perf_mmap_close(struct vm_area_struct *vma)
3463 struct perf_event *event = vma->vm_file->private_data;
3465 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
3466 unsigned long size = perf_data_size(event->rb);
3467 struct user_struct *user = event->mmap_user;
3468 struct ring_buffer *rb = event->rb;
3470 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
3471 vma->vm_mm->pinned_vm -= event->mmap_locked;
3472 rcu_assign_pointer(event->rb, NULL);
3473 ring_buffer_detach(event, rb);
3474 mutex_unlock(&event->mmap_mutex);
3476 ring_buffer_put(rb);
3481 static const struct vm_operations_struct perf_mmap_vmops = {
3482 .open = perf_mmap_open,
3483 .close = perf_mmap_close,
3484 .fault = perf_mmap_fault,
3485 .page_mkwrite = perf_mmap_fault,
3488 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3490 struct perf_event *event = file->private_data;
3491 unsigned long user_locked, user_lock_limit;
3492 struct user_struct *user = current_user();
3493 unsigned long locked, lock_limit;
3494 struct ring_buffer *rb;
3495 unsigned long vma_size;
3496 unsigned long nr_pages;
3497 long user_extra, extra;
3498 int ret = 0, flags = 0;
3501 * Don't allow mmap() of inherited per-task counters. This would
3502 * create a performance issue due to all children writing to the
3505 if (event->cpu == -1 && event->attr.inherit)
3508 if (!(vma->vm_flags & VM_SHARED))
3511 vma_size = vma->vm_end - vma->vm_start;
3512 nr_pages = (vma_size / PAGE_SIZE) - 1;
3515 * If we have rb pages ensure they're a power-of-two number, so we
3516 * can do bitmasks instead of modulo.
3518 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3521 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3524 if (vma->vm_pgoff != 0)
3527 WARN_ON_ONCE(event->ctx->parent_ctx);
3528 mutex_lock(&event->mmap_mutex);
3530 if (event->rb->nr_pages == nr_pages)
3531 atomic_inc(&event->rb->refcount);
3537 user_extra = nr_pages + 1;
3538 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3541 * Increase the limit linearly with more CPUs:
3543 user_lock_limit *= num_online_cpus();
3545 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3548 if (user_locked > user_lock_limit)
3549 extra = user_locked - user_lock_limit;
3551 lock_limit = rlimit(RLIMIT_MEMLOCK);
3552 lock_limit >>= PAGE_SHIFT;
3553 locked = vma->vm_mm->pinned_vm + extra;
3555 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3556 !capable(CAP_IPC_LOCK)) {
3563 if (vma->vm_flags & VM_WRITE)
3564 flags |= RING_BUFFER_WRITABLE;
3566 rb = rb_alloc(nr_pages,
3567 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3574 rcu_assign_pointer(event->rb, rb);
3576 atomic_long_add(user_extra, &user->locked_vm);
3577 event->mmap_locked = extra;
3578 event->mmap_user = get_current_user();
3579 vma->vm_mm->pinned_vm += event->mmap_locked;
3581 perf_event_update_userpage(event);
3585 atomic_inc(&event->mmap_count);
3586 mutex_unlock(&event->mmap_mutex);
3588 vma->vm_flags |= VM_RESERVED;
3589 vma->vm_ops = &perf_mmap_vmops;
3594 static int perf_fasync(int fd, struct file *filp, int on)
3596 struct inode *inode = filp->f_path.dentry->d_inode;
3597 struct perf_event *event = filp->private_data;
3600 mutex_lock(&inode->i_mutex);
3601 retval = fasync_helper(fd, filp, on, &event->fasync);
3602 mutex_unlock(&inode->i_mutex);
3610 static const struct file_operations perf_fops = {
3611 .llseek = no_llseek,
3612 .release = perf_release,
3615 .unlocked_ioctl = perf_ioctl,
3616 .compat_ioctl = perf_ioctl,
3618 .fasync = perf_fasync,
3624 * If there's data, ensure we set the poll() state and publish everything
3625 * to user-space before waking everybody up.
3628 void perf_event_wakeup(struct perf_event *event)
3630 ring_buffer_wakeup(event);
3632 if (event->pending_kill) {
3633 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3634 event->pending_kill = 0;
3638 static void perf_pending_event(struct irq_work *entry)
3640 struct perf_event *event = container_of(entry,
3641 struct perf_event, pending);
3643 if (event->pending_disable) {
3644 event->pending_disable = 0;
3645 __perf_event_disable(event);
3648 if (event->pending_wakeup) {
3649 event->pending_wakeup = 0;
3650 perf_event_wakeup(event);
3655 * We assume there is only KVM supporting the callbacks.
3656 * Later on, we might change it to a list if there is
3657 * another virtualization implementation supporting the callbacks.
3659 struct perf_guest_info_callbacks *perf_guest_cbs;
3661 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3663 perf_guest_cbs = cbs;
3666 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3668 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3670 perf_guest_cbs = NULL;
3673 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3675 static void __perf_event_header__init_id(struct perf_event_header *header,
3676 struct perf_sample_data *data,
3677 struct perf_event *event)
3679 u64 sample_type = event->attr.sample_type;
3681 data->type = sample_type;
3682 header->size += event->id_header_size;
3684 if (sample_type & PERF_SAMPLE_TID) {
3685 /* namespace issues */
3686 data->tid_entry.pid = perf_event_pid(event, current);
3687 data->tid_entry.tid = perf_event_tid(event, current);
3690 if (sample_type & PERF_SAMPLE_TIME)
3691 data->time = perf_clock();
3693 if (sample_type & PERF_SAMPLE_ID)
3694 data->id = primary_event_id(event);
3696 if (sample_type & PERF_SAMPLE_STREAM_ID)
3697 data->stream_id = event->id;
3699 if (sample_type & PERF_SAMPLE_CPU) {
3700 data->cpu_entry.cpu = raw_smp_processor_id();
3701 data->cpu_entry.reserved = 0;
3705 void perf_event_header__init_id(struct perf_event_header *header,
3706 struct perf_sample_data *data,
3707 struct perf_event *event)
3709 if (event->attr.sample_id_all)
3710 __perf_event_header__init_id(header, data, event);
3713 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3714 struct perf_sample_data *data)
3716 u64 sample_type = data->type;
3718 if (sample_type & PERF_SAMPLE_TID)
3719 perf_output_put(handle, data->tid_entry);
3721 if (sample_type & PERF_SAMPLE_TIME)
3722 perf_output_put(handle, data->time);
3724 if (sample_type & PERF_SAMPLE_ID)
3725 perf_output_put(handle, data->id);
3727 if (sample_type & PERF_SAMPLE_STREAM_ID)
3728 perf_output_put(handle, data->stream_id);
3730 if (sample_type & PERF_SAMPLE_CPU)
3731 perf_output_put(handle, data->cpu_entry);
3734 void perf_event__output_id_sample(struct perf_event *event,
3735 struct perf_output_handle *handle,
3736 struct perf_sample_data *sample)
3738 if (event->attr.sample_id_all)
3739 __perf_event__output_id_sample(handle, sample);
3742 static void perf_output_read_one(struct perf_output_handle *handle,
3743 struct perf_event *event,
3744 u64 enabled, u64 running)
3746 u64 read_format = event->attr.read_format;
3750 values[n++] = perf_event_count(event);
3751 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3752 values[n++] = enabled +
3753 atomic64_read(&event->child_total_time_enabled);
3755 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3756 values[n++] = running +
3757 atomic64_read(&event->child_total_time_running);
3759 if (read_format & PERF_FORMAT_ID)
3760 values[n++] = primary_event_id(event);
3762 __output_copy(handle, values, n * sizeof(u64));
3766 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3768 static void perf_output_read_group(struct perf_output_handle *handle,
3769 struct perf_event *event,
3770 u64 enabled, u64 running)
3772 struct perf_event *leader = event->group_leader, *sub;
3773 u64 read_format = event->attr.read_format;
3777 values[n++] = 1 + leader->nr_siblings;
3779 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3780 values[n++] = enabled;
3782 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3783 values[n++] = running;
3785 if (leader != event)
3786 leader->pmu->read(leader);
3788 values[n++] = perf_event_count(leader);
3789 if (read_format & PERF_FORMAT_ID)
3790 values[n++] = primary_event_id(leader);
3792 __output_copy(handle, values, n * sizeof(u64));
3794 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3798 sub->pmu->read(sub);
3800 values[n++] = perf_event_count(sub);
3801 if (read_format & PERF_FORMAT_ID)
3802 values[n++] = primary_event_id(sub);
3804 __output_copy(handle, values, n * sizeof(u64));
3808 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
3809 PERF_FORMAT_TOTAL_TIME_RUNNING)
3811 static void perf_output_read(struct perf_output_handle *handle,
3812 struct perf_event *event)
3814 u64 enabled = 0, running = 0, now;
3815 u64 read_format = event->attr.read_format;
3818 * compute total_time_enabled, total_time_running
3819 * based on snapshot values taken when the event
3820 * was last scheduled in.
3822 * we cannot simply called update_context_time()
3823 * because of locking issue as we are called in
3826 if (read_format & PERF_FORMAT_TOTAL_TIMES)
3827 calc_timer_values(event, &now, &enabled, &running);
3829 if (event->attr.read_format & PERF_FORMAT_GROUP)
3830 perf_output_read_group(handle, event, enabled, running);
3832 perf_output_read_one(handle, event, enabled, running);
3835 void perf_output_sample(struct perf_output_handle *handle,
3836 struct perf_event_header *header,
3837 struct perf_sample_data *data,
3838 struct perf_event *event)
3840 u64 sample_type = data->type;
3842 perf_output_put(handle, *header);
3844 if (sample_type & PERF_SAMPLE_IP)
3845 perf_output_put(handle, data->ip);
3847 if (sample_type & PERF_SAMPLE_TID)
3848 perf_output_put(handle, data->tid_entry);
3850 if (sample_type & PERF_SAMPLE_TIME)
3851 perf_output_put(handle, data->time);
3853 if (sample_type & PERF_SAMPLE_ADDR)
3854 perf_output_put(handle, data->addr);
3856 if (sample_type & PERF_SAMPLE_ID)
3857 perf_output_put(handle, data->id);
3859 if (sample_type & PERF_SAMPLE_STREAM_ID)
3860 perf_output_put(handle, data->stream_id);
3862 if (sample_type & PERF_SAMPLE_CPU)
3863 perf_output_put(handle, data->cpu_entry);
3865 if (sample_type & PERF_SAMPLE_PERIOD)
3866 perf_output_put(handle, data->period);
3868 if (sample_type & PERF_SAMPLE_READ)
3869 perf_output_read(handle, event);
3871 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3872 if (data->callchain) {
3875 if (data->callchain)
3876 size += data->callchain->nr;
3878 size *= sizeof(u64);
3880 __output_copy(handle, data->callchain, size);
3883 perf_output_put(handle, nr);
3887 if (sample_type & PERF_SAMPLE_RAW) {
3889 perf_output_put(handle, data->raw->size);
3890 __output_copy(handle, data->raw->data,
3897 .size = sizeof(u32),
3900 perf_output_put(handle, raw);
3904 if (!event->attr.watermark) {
3905 int wakeup_events = event->attr.wakeup_events;
3907 if (wakeup_events) {
3908 struct ring_buffer *rb = handle->rb;
3909 int events = local_inc_return(&rb->events);
3911 if (events >= wakeup_events) {
3912 local_sub(wakeup_events, &rb->events);
3913 local_inc(&rb->wakeup);
3918 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
3919 if (data->br_stack) {
3922 size = data->br_stack->nr
3923 * sizeof(struct perf_branch_entry);
3925 perf_output_put(handle, data->br_stack->nr);
3926 perf_output_copy(handle, data->br_stack->entries, size);
3929 * we always store at least the value of nr
3932 perf_output_put(handle, nr);
3937 void perf_prepare_sample(struct perf_event_header *header,
3938 struct perf_sample_data *data,
3939 struct perf_event *event,
3940 struct pt_regs *regs)
3942 u64 sample_type = event->attr.sample_type;
3944 header->type = PERF_RECORD_SAMPLE;
3945 header->size = sizeof(*header) + event->header_size;
3948 header->misc |= perf_misc_flags(regs);
3950 __perf_event_header__init_id(header, data, event);
3952 if (sample_type & PERF_SAMPLE_IP)
3953 data->ip = perf_instruction_pointer(regs);
3955 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3958 data->callchain = perf_callchain(regs);
3960 if (data->callchain)
3961 size += data->callchain->nr;
3963 header->size += size * sizeof(u64);
3966 if (sample_type & PERF_SAMPLE_RAW) {
3967 int size = sizeof(u32);
3970 size += data->raw->size;
3972 size += sizeof(u32);
3974 WARN_ON_ONCE(size & (sizeof(u64)-1));
3975 header->size += size;
3978 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
3979 int size = sizeof(u64); /* nr */
3980 if (data->br_stack) {
3981 size += data->br_stack->nr
3982 * sizeof(struct perf_branch_entry);
3984 header->size += size;
3988 static void perf_event_output(struct perf_event *event,
3989 struct perf_sample_data *data,
3990 struct pt_regs *regs)
3992 struct perf_output_handle handle;
3993 struct perf_event_header header;
3995 /* protect the callchain buffers */
3998 perf_prepare_sample(&header, data, event, regs);
4000 if (perf_output_begin(&handle, event, header.size))
4003 perf_output_sample(&handle, &header, data, event);
4005 perf_output_end(&handle);
4015 struct perf_read_event {
4016 struct perf_event_header header;
4023 perf_event_read_event(struct perf_event *event,
4024 struct task_struct *task)
4026 struct perf_output_handle handle;
4027 struct perf_sample_data sample;
4028 struct perf_read_event read_event = {
4030 .type = PERF_RECORD_READ,
4032 .size = sizeof(read_event) + event->read_size,
4034 .pid = perf_event_pid(event, task),
4035 .tid = perf_event_tid(event, task),
4039 perf_event_header__init_id(&read_event.header, &sample, event);
4040 ret = perf_output_begin(&handle, event, read_event.header.size);
4044 perf_output_put(&handle, read_event);
4045 perf_output_read(&handle, event);
4046 perf_event__output_id_sample(event, &handle, &sample);
4048 perf_output_end(&handle);
4052 * task tracking -- fork/exit
4054 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
4057 struct perf_task_event {
4058 struct task_struct *task;
4059 struct perf_event_context *task_ctx;
4062 struct perf_event_header header;
4072 static void perf_event_task_output(struct perf_event *event,
4073 struct perf_task_event *task_event)
4075 struct perf_output_handle handle;
4076 struct perf_sample_data sample;
4077 struct task_struct *task = task_event->task;
4078 int ret, size = task_event->event_id.header.size;
4080 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4082 ret = perf_output_begin(&handle, event,
4083 task_event->event_id.header.size);
4087 task_event->event_id.pid = perf_event_pid(event, task);
4088 task_event->event_id.ppid = perf_event_pid(event, current);
4090 task_event->event_id.tid = perf_event_tid(event, task);
4091 task_event->event_id.ptid = perf_event_tid(event, current);
4093 perf_output_put(&handle, task_event->event_id);
4095 perf_event__output_id_sample(event, &handle, &sample);
4097 perf_output_end(&handle);
4099 task_event->event_id.header.size = size;
4102 static int perf_event_task_match(struct perf_event *event)
4104 if (event->state < PERF_EVENT_STATE_INACTIVE)
4107 if (!event_filter_match(event))
4110 if (event->attr.comm || event->attr.mmap ||
4111 event->attr.mmap_data || event->attr.task)
4117 static void perf_event_task_ctx(struct perf_event_context *ctx,
4118 struct perf_task_event *task_event)
4120 struct perf_event *event;
4122 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4123 if (perf_event_task_match(event))
4124 perf_event_task_output(event, task_event);
4128 static void perf_event_task_event(struct perf_task_event *task_event)
4130 struct perf_cpu_context *cpuctx;
4131 struct perf_event_context *ctx;
4136 list_for_each_entry_rcu(pmu, &pmus, entry) {
4137 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4138 if (cpuctx->active_pmu != pmu)
4140 perf_event_task_ctx(&cpuctx->ctx, task_event);
4142 ctx = task_event->task_ctx;
4144 ctxn = pmu->task_ctx_nr;
4147 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4150 perf_event_task_ctx(ctx, task_event);
4152 put_cpu_ptr(pmu->pmu_cpu_context);
4157 static void perf_event_task(struct task_struct *task,
4158 struct perf_event_context *task_ctx,
4161 struct perf_task_event task_event;
4163 if (!atomic_read(&nr_comm_events) &&
4164 !atomic_read(&nr_mmap_events) &&
4165 !atomic_read(&nr_task_events))
4168 task_event = (struct perf_task_event){
4170 .task_ctx = task_ctx,
4173 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4175 .size = sizeof(task_event.event_id),
4181 .time = perf_clock(),
4185 perf_event_task_event(&task_event);
4188 void perf_event_fork(struct task_struct *task)
4190 perf_event_task(task, NULL, 1);
4197 struct perf_comm_event {
4198 struct task_struct *task;
4203 struct perf_event_header header;
4210 static void perf_event_comm_output(struct perf_event *event,
4211 struct perf_comm_event *comm_event)
4213 struct perf_output_handle handle;
4214 struct perf_sample_data sample;
4215 int size = comm_event->event_id.header.size;
4218 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4219 ret = perf_output_begin(&handle, event,
4220 comm_event->event_id.header.size);
4225 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4226 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4228 perf_output_put(&handle, comm_event->event_id);
4229 __output_copy(&handle, comm_event->comm,
4230 comm_event->comm_size);
4232 perf_event__output_id_sample(event, &handle, &sample);
4234 perf_output_end(&handle);
4236 comm_event->event_id.header.size = size;
4239 static int perf_event_comm_match(struct perf_event *event)
4241 if (event->state < PERF_EVENT_STATE_INACTIVE)
4244 if (!event_filter_match(event))
4247 if (event->attr.comm)
4253 static void perf_event_comm_ctx(struct perf_event_context *ctx,
4254 struct perf_comm_event *comm_event)
4256 struct perf_event *event;
4258 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4259 if (perf_event_comm_match(event))
4260 perf_event_comm_output(event, comm_event);
4264 static void perf_event_comm_event(struct perf_comm_event *comm_event)
4266 struct perf_cpu_context *cpuctx;
4267 struct perf_event_context *ctx;
4268 char comm[TASK_COMM_LEN];
4273 memset(comm, 0, sizeof(comm));
4274 strlcpy(comm, comm_event->task->comm, sizeof(comm));
4275 size = ALIGN(strlen(comm)+1, sizeof(u64));
4277 comm_event->comm = comm;
4278 comm_event->comm_size = size;
4280 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
4282 list_for_each_entry_rcu(pmu, &pmus, entry) {
4283 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4284 if (cpuctx->active_pmu != pmu)
4286 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
4288 ctxn = pmu->task_ctx_nr;
4292 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4294 perf_event_comm_ctx(ctx, comm_event);
4296 put_cpu_ptr(pmu->pmu_cpu_context);
4301 void perf_event_comm(struct task_struct *task)
4303 struct perf_comm_event comm_event;
4304 struct perf_event_context *ctx;
4307 for_each_task_context_nr(ctxn) {
4308 ctx = task->perf_event_ctxp[ctxn];
4312 perf_event_enable_on_exec(ctx);
4315 if (!atomic_read(&nr_comm_events))
4318 comm_event = (struct perf_comm_event){
4324 .type = PERF_RECORD_COMM,
4333 perf_event_comm_event(&comm_event);
4340 struct perf_mmap_event {
4341 struct vm_area_struct *vma;
4343 const char *file_name;
4347 struct perf_event_header header;
4357 static void perf_event_mmap_output(struct perf_event *event,
4358 struct perf_mmap_event *mmap_event)
4360 struct perf_output_handle handle;
4361 struct perf_sample_data sample;
4362 int size = mmap_event->event_id.header.size;
4365 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4366 ret = perf_output_begin(&handle, event,
4367 mmap_event->event_id.header.size);
4371 mmap_event->event_id.pid = perf_event_pid(event, current);
4372 mmap_event->event_id.tid = perf_event_tid(event, current);
4374 perf_output_put(&handle, mmap_event->event_id);
4375 __output_copy(&handle, mmap_event->file_name,
4376 mmap_event->file_size);
4378 perf_event__output_id_sample(event, &handle, &sample);
4380 perf_output_end(&handle);
4382 mmap_event->event_id.header.size = size;
4385 static int perf_event_mmap_match(struct perf_event *event,
4386 struct perf_mmap_event *mmap_event,
4389 if (event->state < PERF_EVENT_STATE_INACTIVE)
4392 if (!event_filter_match(event))
4395 if ((!executable && event->attr.mmap_data) ||
4396 (executable && event->attr.mmap))
4402 static void perf_event_mmap_ctx(struct perf_event_context *ctx,
4403 struct perf_mmap_event *mmap_event,
4406 struct perf_event *event;
4408 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4409 if (perf_event_mmap_match(event, mmap_event, executable))
4410 perf_event_mmap_output(event, mmap_event);
4414 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4416 struct perf_cpu_context *cpuctx;
4417 struct perf_event_context *ctx;
4418 struct vm_area_struct *vma = mmap_event->vma;
4419 struct file *file = vma->vm_file;
4427 memset(tmp, 0, sizeof(tmp));
4431 * d_path works from the end of the rb backwards, so we
4432 * need to add enough zero bytes after the string to handle
4433 * the 64bit alignment we do later.
4435 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4437 name = strncpy(tmp, "//enomem", sizeof(tmp));
4440 name = d_path(&file->f_path, buf, PATH_MAX);
4442 name = strncpy(tmp, "//toolong", sizeof(tmp));
4446 if (arch_vma_name(mmap_event->vma)) {
4447 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4453 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4455 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4456 vma->vm_end >= vma->vm_mm->brk) {
4457 name = strncpy(tmp, "[heap]", sizeof(tmp));
4459 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4460 vma->vm_end >= vma->vm_mm->start_stack) {
4461 name = strncpy(tmp, "[stack]", sizeof(tmp));
4465 name = strncpy(tmp, "//anon", sizeof(tmp));
4470 size = ALIGN(strlen(name)+1, sizeof(u64));
4472 mmap_event->file_name = name;
4473 mmap_event->file_size = size;
4475 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4478 list_for_each_entry_rcu(pmu, &pmus, entry) {
4479 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4480 if (cpuctx->active_pmu != pmu)
4482 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4483 vma->vm_flags & VM_EXEC);
4485 ctxn = pmu->task_ctx_nr;
4489 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4491 perf_event_mmap_ctx(ctx, mmap_event,
4492 vma->vm_flags & VM_EXEC);
4495 put_cpu_ptr(pmu->pmu_cpu_context);
4502 void perf_event_mmap(struct vm_area_struct *vma)
4504 struct perf_mmap_event mmap_event;
4506 if (!atomic_read(&nr_mmap_events))
4509 mmap_event = (struct perf_mmap_event){
4515 .type = PERF_RECORD_MMAP,
4516 .misc = PERF_RECORD_MISC_USER,
4521 .start = vma->vm_start,
4522 .len = vma->vm_end - vma->vm_start,
4523 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
4527 perf_event_mmap_event(&mmap_event);
4531 * IRQ throttle logging
4534 static void perf_log_throttle(struct perf_event *event, int enable)
4536 struct perf_output_handle handle;
4537 struct perf_sample_data sample;
4541 struct perf_event_header header;
4545 } throttle_event = {
4547 .type = PERF_RECORD_THROTTLE,
4549 .size = sizeof(throttle_event),
4551 .time = perf_clock(),
4552 .id = primary_event_id(event),
4553 .stream_id = event->id,
4557 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4559 perf_event_header__init_id(&throttle_event.header, &sample, event);
4561 ret = perf_output_begin(&handle, event,
4562 throttle_event.header.size);
4566 perf_output_put(&handle, throttle_event);
4567 perf_event__output_id_sample(event, &handle, &sample);
4568 perf_output_end(&handle);
4572 * Generic event overflow handling, sampling.
4575 static int __perf_event_overflow(struct perf_event *event,
4576 int throttle, struct perf_sample_data *data,
4577 struct pt_regs *regs)
4579 int events = atomic_read(&event->event_limit);
4580 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 seq = __this_cpu_read(perf_throttled_seq);
4592 if (seq != hwc->interrupts_seq) {
4593 hwc->interrupts_seq = seq;
4594 hwc->interrupts = 1;
4597 if (unlikely(throttle
4598 && hwc->interrupts >= max_samples_per_tick)) {
4599 __this_cpu_inc(perf_throttled_count);
4600 hwc->interrupts = MAX_INTERRUPTS;
4601 perf_log_throttle(event, 0);
4606 if (event->attr.freq) {
4607 u64 now = perf_clock();
4608 s64 delta = now - hwc->freq_time_stamp;
4610 hwc->freq_time_stamp = now;
4612 if (delta > 0 && delta < 2*TICK_NSEC)
4613 perf_adjust_period(event, delta, hwc->last_period, true);
4617 * XXX event_limit might not quite work as expected on inherited
4621 event->pending_kill = POLL_IN;
4622 if (events && atomic_dec_and_test(&event->event_limit)) {
4624 event->pending_kill = POLL_HUP;
4625 event->pending_disable = 1;
4626 irq_work_queue(&event->pending);
4629 if (event->overflow_handler)
4630 event->overflow_handler(event, data, regs);
4632 perf_event_output(event, data, regs);
4634 if (event->fasync && event->pending_kill) {
4635 event->pending_wakeup = 1;
4636 irq_work_queue(&event->pending);
4642 int perf_event_overflow(struct perf_event *event,
4643 struct perf_sample_data *data,
4644 struct pt_regs *regs)
4646 return __perf_event_overflow(event, 1, data, regs);
4650 * Generic software event infrastructure
4653 struct swevent_htable {
4654 struct swevent_hlist *swevent_hlist;
4655 struct mutex hlist_mutex;
4658 /* Recursion avoidance in each contexts */
4659 int recursion[PERF_NR_CONTEXTS];
4662 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4665 * We directly increment event->count and keep a second value in
4666 * event->hw.period_left to count intervals. This period event
4667 * is kept in the range [-sample_period, 0] so that we can use the
4671 static u64 perf_swevent_set_period(struct perf_event *event)
4673 struct hw_perf_event *hwc = &event->hw;
4674 u64 period = hwc->last_period;
4678 hwc->last_period = hwc->sample_period;
4681 old = val = local64_read(&hwc->period_left);
4685 nr = div64_u64(period + val, period);
4686 offset = nr * period;
4688 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
4694 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
4695 struct perf_sample_data *data,
4696 struct pt_regs *regs)
4698 struct hw_perf_event *hwc = &event->hw;
4702 overflow = perf_swevent_set_period(event);
4704 if (hwc->interrupts == MAX_INTERRUPTS)
4707 for (; overflow; overflow--) {
4708 if (__perf_event_overflow(event, throttle,
4711 * We inhibit the overflow from happening when
4712 * hwc->interrupts == MAX_INTERRUPTS.
4720 static void perf_swevent_event(struct perf_event *event, u64 nr,
4721 struct perf_sample_data *data,
4722 struct pt_regs *regs)
4724 struct hw_perf_event *hwc = &event->hw;
4726 local64_add(nr, &event->count);
4731 if (!is_sampling_event(event))
4734 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
4736 return perf_swevent_overflow(event, 1, data, regs);
4738 data->period = event->hw.last_period;
4740 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4741 return perf_swevent_overflow(event, 1, data, regs);
4743 if (local64_add_negative(nr, &hwc->period_left))
4746 perf_swevent_overflow(event, 0, data, regs);
4749 static int perf_exclude_event(struct perf_event *event,
4750 struct pt_regs *regs)
4752 if (event->hw.state & PERF_HES_STOPPED)
4756 if (event->attr.exclude_user && user_mode(regs))
4759 if (event->attr.exclude_kernel && !user_mode(regs))
4766 static int perf_swevent_match(struct perf_event *event,
4767 enum perf_type_id type,
4769 struct perf_sample_data *data,
4770 struct pt_regs *regs)
4772 if (event->attr.type != type)
4775 if (event->attr.config != event_id)
4778 if (perf_exclude_event(event, regs))
4784 static inline u64 swevent_hash(u64 type, u32 event_id)
4786 u64 val = event_id | (type << 32);
4788 return hash_64(val, SWEVENT_HLIST_BITS);
4791 static inline struct hlist_head *
4792 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
4794 u64 hash = swevent_hash(type, event_id);
4796 return &hlist->heads[hash];
4799 /* For the read side: events when they trigger */
4800 static inline struct hlist_head *
4801 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
4803 struct swevent_hlist *hlist;
4805 hlist = rcu_dereference(swhash->swevent_hlist);
4809 return __find_swevent_head(hlist, type, event_id);
4812 /* For the event head insertion and removal in the hlist */
4813 static inline struct hlist_head *
4814 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
4816 struct swevent_hlist *hlist;
4817 u32 event_id = event->attr.config;
4818 u64 type = event->attr.type;
4821 * Event scheduling is always serialized against hlist allocation
4822 * and release. Which makes the protected version suitable here.
4823 * The context lock guarantees that.
4825 hlist = rcu_dereference_protected(swhash->swevent_hlist,
4826 lockdep_is_held(&event->ctx->lock));
4830 return __find_swevent_head(hlist, type, event_id);
4833 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4835 struct perf_sample_data *data,
4836 struct pt_regs *regs)
4838 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4839 struct perf_event *event;
4840 struct hlist_node *node;
4841 struct hlist_head *head;
4844 head = find_swevent_head_rcu(swhash, type, event_id);
4848 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4849 if (perf_swevent_match(event, type, event_id, data, regs))
4850 perf_swevent_event(event, nr, data, regs);
4856 int perf_swevent_get_recursion_context(void)
4858 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4860 return get_recursion_context(swhash->recursion);
4862 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
4864 inline void perf_swevent_put_recursion_context(int rctx)
4866 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4868 put_recursion_context(swhash->recursion, rctx);
4871 void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
4873 struct perf_sample_data data;
4876 preempt_disable_notrace();
4877 rctx = perf_swevent_get_recursion_context();
4881 perf_sample_data_init(&data, addr);
4883 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
4885 perf_swevent_put_recursion_context(rctx);
4886 preempt_enable_notrace();
4889 static void perf_swevent_read(struct perf_event *event)
4893 static int perf_swevent_add(struct perf_event *event, int flags)
4895 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
4896 struct hw_perf_event *hwc = &event->hw;
4897 struct hlist_head *head;
4899 if (is_sampling_event(event)) {
4900 hwc->last_period = hwc->sample_period;
4901 perf_swevent_set_period(event);
4904 hwc->state = !(flags & PERF_EF_START);
4906 head = find_swevent_head(swhash, event);
4907 if (WARN_ON_ONCE(!head))
4910 hlist_add_head_rcu(&event->hlist_entry, head);
4915 static void perf_swevent_del(struct perf_event *event, int flags)
4917 hlist_del_rcu(&event->hlist_entry);
4920 static void perf_swevent_start(struct perf_event *event, int flags)
4922 event->hw.state = 0;
4925 static void perf_swevent_stop(struct perf_event *event, int flags)
4927 event->hw.state = PERF_HES_STOPPED;
4930 /* Deref the hlist from the update side */
4931 static inline struct swevent_hlist *
4932 swevent_hlist_deref(struct swevent_htable *swhash)
4934 return rcu_dereference_protected(swhash->swevent_hlist,
4935 lockdep_is_held(&swhash->hlist_mutex));
4938 static void swevent_hlist_release(struct swevent_htable *swhash)
4940 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
4945 rcu_assign_pointer(swhash->swevent_hlist, NULL);
4946 kfree_rcu(hlist, rcu_head);
4949 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4951 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4953 mutex_lock(&swhash->hlist_mutex);
4955 if (!--swhash->hlist_refcount)
4956 swevent_hlist_release(swhash);
4958 mutex_unlock(&swhash->hlist_mutex);
4961 static void swevent_hlist_put(struct perf_event *event)
4965 if (event->cpu != -1) {
4966 swevent_hlist_put_cpu(event, event->cpu);
4970 for_each_possible_cpu(cpu)
4971 swevent_hlist_put_cpu(event, cpu);
4974 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4976 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
4979 mutex_lock(&swhash->hlist_mutex);
4981 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
4982 struct swevent_hlist *hlist;
4984 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4989 rcu_assign_pointer(swhash->swevent_hlist, hlist);
4991 swhash->hlist_refcount++;
4993 mutex_unlock(&swhash->hlist_mutex);
4998 static int swevent_hlist_get(struct perf_event *event)
5001 int cpu, failed_cpu;
5003 if (event->cpu != -1)
5004 return swevent_hlist_get_cpu(event, event->cpu);
5007 for_each_possible_cpu(cpu) {
5008 err = swevent_hlist_get_cpu(event, cpu);
5018 for_each_possible_cpu(cpu) {
5019 if (cpu == failed_cpu)
5021 swevent_hlist_put_cpu(event, cpu);
5028 struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
5030 static void sw_perf_event_destroy(struct perf_event *event)
5032 u64 event_id = event->attr.config;
5034 WARN_ON(event->parent);
5036 static_key_slow_dec(&perf_swevent_enabled[event_id]);
5037 swevent_hlist_put(event);
5040 static int perf_swevent_init(struct perf_event *event)
5042 int event_id = event->attr.config;
5044 if (event->attr.type != PERF_TYPE_SOFTWARE)
5048 case PERF_COUNT_SW_CPU_CLOCK:
5049 case PERF_COUNT_SW_TASK_CLOCK:
5056 if (event_id >= PERF_COUNT_SW_MAX)
5059 if (!event->parent) {
5062 err = swevent_hlist_get(event);
5066 static_key_slow_inc(&perf_swevent_enabled[event_id]);
5067 event->destroy = sw_perf_event_destroy;
5073 static int perf_swevent_event_idx(struct perf_event *event)
5078 static struct pmu perf_swevent = {
5079 .task_ctx_nr = perf_sw_context,
5081 .event_init = perf_swevent_init,
5082 .add = perf_swevent_add,
5083 .del = perf_swevent_del,
5084 .start = perf_swevent_start,
5085 .stop = perf_swevent_stop,
5086 .read = perf_swevent_read,
5088 .event_idx = perf_swevent_event_idx,
5091 #ifdef CONFIG_EVENT_TRACING
5093 static int perf_tp_filter_match(struct perf_event *event,
5094 struct perf_sample_data *data)
5096 void *record = data->raw->data;
5098 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5103 static int perf_tp_event_match(struct perf_event *event,
5104 struct perf_sample_data *data,
5105 struct pt_regs *regs)
5107 if (event->hw.state & PERF_HES_STOPPED)
5110 * All tracepoints are from kernel-space.
5112 if (event->attr.exclude_kernel)
5115 if (!perf_tp_filter_match(event, data))
5121 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5122 struct pt_regs *regs, struct hlist_head *head, int rctx)
5124 struct perf_sample_data data;
5125 struct perf_event *event;
5126 struct hlist_node *node;
5128 struct perf_raw_record raw = {
5133 perf_sample_data_init(&data, addr);
5136 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5137 if (perf_tp_event_match(event, &data, regs))
5138 perf_swevent_event(event, count, &data, regs);
5141 perf_swevent_put_recursion_context(rctx);
5143 EXPORT_SYMBOL_GPL(perf_tp_event);
5145 static void tp_perf_event_destroy(struct perf_event *event)
5147 perf_trace_destroy(event);
5150 static int perf_tp_event_init(struct perf_event *event)
5154 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5157 err = perf_trace_init(event);
5161 event->destroy = tp_perf_event_destroy;
5166 static struct pmu perf_tracepoint = {
5167 .task_ctx_nr = perf_sw_context,
5169 .event_init = perf_tp_event_init,
5170 .add = perf_trace_add,
5171 .del = perf_trace_del,
5172 .start = perf_swevent_start,
5173 .stop = perf_swevent_stop,
5174 .read = perf_swevent_read,
5176 .event_idx = perf_swevent_event_idx,
5179 static inline void perf_tp_register(void)
5181 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5184 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5189 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5192 filter_str = strndup_user(arg, PAGE_SIZE);
5193 if (IS_ERR(filter_str))
5194 return PTR_ERR(filter_str);
5196 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5202 static void perf_event_free_filter(struct perf_event *event)
5204 ftrace_profile_free_filter(event);
5209 static inline void perf_tp_register(void)
5213 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5218 static void perf_event_free_filter(struct perf_event *event)
5222 #endif /* CONFIG_EVENT_TRACING */
5224 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5225 void perf_bp_event(struct perf_event *bp, void *data)
5227 struct perf_sample_data sample;
5228 struct pt_regs *regs = data;
5230 perf_sample_data_init(&sample, bp->attr.bp_addr);
5232 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5233 perf_swevent_event(bp, 1, &sample, regs);
5238 * hrtimer based swevent callback
5241 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
5243 enum hrtimer_restart ret = HRTIMER_RESTART;
5244 struct perf_sample_data data;
5245 struct pt_regs *regs;
5246 struct perf_event *event;
5249 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5251 if (event->state != PERF_EVENT_STATE_ACTIVE)
5252 return HRTIMER_NORESTART;
5254 event->pmu->read(event);
5256 perf_sample_data_init(&data, 0);
5257 data.period = event->hw.last_period;
5258 regs = get_irq_regs();
5260 if (regs && !perf_exclude_event(event, regs)) {
5261 if (!(event->attr.exclude_idle && is_idle_task(current)))
5262 if (perf_event_overflow(event, &data, regs))
5263 ret = HRTIMER_NORESTART;
5266 period = max_t(u64, 10000, event->hw.sample_period);
5267 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5272 static void perf_swevent_start_hrtimer(struct perf_event *event)
5274 struct hw_perf_event *hwc = &event->hw;
5277 if (!is_sampling_event(event))
5280 period = local64_read(&hwc->period_left);
5285 local64_set(&hwc->period_left, 0);
5287 period = max_t(u64, 10000, hwc->sample_period);
5289 __hrtimer_start_range_ns(&hwc->hrtimer,
5290 ns_to_ktime(period), 0,
5291 HRTIMER_MODE_REL_PINNED, 0);
5294 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5296 struct hw_perf_event *hwc = &event->hw;
5298 if (is_sampling_event(event)) {
5299 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
5300 local64_set(&hwc->period_left, ktime_to_ns(remaining));
5302 hrtimer_cancel(&hwc->hrtimer);
5306 static void perf_swevent_init_hrtimer(struct perf_event *event)
5308 struct hw_perf_event *hwc = &event->hw;
5310 if (!is_sampling_event(event))
5313 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5314 hwc->hrtimer.function = perf_swevent_hrtimer;
5317 * Since hrtimers have a fixed rate, we can do a static freq->period
5318 * mapping and avoid the whole period adjust feedback stuff.
5320 if (event->attr.freq) {
5321 long freq = event->attr.sample_freq;
5323 event->attr.sample_period = NSEC_PER_SEC / freq;
5324 hwc->sample_period = event->attr.sample_period;
5325 local64_set(&hwc->period_left, hwc->sample_period);
5326 event->attr.freq = 0;
5331 * Software event: cpu wall time clock
5334 static void cpu_clock_event_update(struct perf_event *event)
5339 now = local_clock();
5340 prev = local64_xchg(&event->hw.prev_count, now);
5341 local64_add(now - prev, &event->count);
5344 static void cpu_clock_event_start(struct perf_event *event, int flags)
5346 local64_set(&event->hw.prev_count, local_clock());
5347 perf_swevent_start_hrtimer(event);
5350 static void cpu_clock_event_stop(struct perf_event *event, int flags)
5352 perf_swevent_cancel_hrtimer(event);
5353 cpu_clock_event_update(event);
5356 static int cpu_clock_event_add(struct perf_event *event, int flags)
5358 if (flags & PERF_EF_START)
5359 cpu_clock_event_start(event, flags);
5364 static void cpu_clock_event_del(struct perf_event *event, int flags)
5366 cpu_clock_event_stop(event, flags);
5369 static void cpu_clock_event_read(struct perf_event *event)
5371 cpu_clock_event_update(event);
5374 static int cpu_clock_event_init(struct perf_event *event)
5376 if (event->attr.type != PERF_TYPE_SOFTWARE)
5379 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5382 perf_swevent_init_hrtimer(event);
5387 static struct pmu perf_cpu_clock = {
5388 .task_ctx_nr = perf_sw_context,
5390 .event_init = cpu_clock_event_init,
5391 .add = cpu_clock_event_add,
5392 .del = cpu_clock_event_del,
5393 .start = cpu_clock_event_start,
5394 .stop = cpu_clock_event_stop,
5395 .read = cpu_clock_event_read,
5397 .event_idx = perf_swevent_event_idx,
5401 * Software event: task time clock
5404 static void task_clock_event_update(struct perf_event *event, u64 now)
5409 prev = local64_xchg(&event->hw.prev_count, now);
5411 local64_add(delta, &event->count);
5414 static void task_clock_event_start(struct perf_event *event, int flags)
5416 local64_set(&event->hw.prev_count, event->ctx->time);
5417 perf_swevent_start_hrtimer(event);
5420 static void task_clock_event_stop(struct perf_event *event, int flags)
5422 perf_swevent_cancel_hrtimer(event);
5423 task_clock_event_update(event, event->ctx->time);
5426 static int task_clock_event_add(struct perf_event *event, int flags)
5428 if (flags & PERF_EF_START)
5429 task_clock_event_start(event, flags);
5434 static void task_clock_event_del(struct perf_event *event, int flags)
5436 task_clock_event_stop(event, PERF_EF_UPDATE);
5439 static void task_clock_event_read(struct perf_event *event)
5441 u64 now = perf_clock();
5442 u64 delta = now - event->ctx->timestamp;
5443 u64 time = event->ctx->time + delta;
5445 task_clock_event_update(event, time);
5448 static int task_clock_event_init(struct perf_event *event)
5450 if (event->attr.type != PERF_TYPE_SOFTWARE)
5453 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5456 perf_swevent_init_hrtimer(event);
5461 static struct pmu perf_task_clock = {
5462 .task_ctx_nr = perf_sw_context,
5464 .event_init = task_clock_event_init,
5465 .add = task_clock_event_add,
5466 .del = task_clock_event_del,
5467 .start = task_clock_event_start,
5468 .stop = task_clock_event_stop,
5469 .read = task_clock_event_read,
5471 .event_idx = perf_swevent_event_idx,
5474 static void perf_pmu_nop_void(struct pmu *pmu)
5478 static int perf_pmu_nop_int(struct pmu *pmu)
5483 static void perf_pmu_start_txn(struct pmu *pmu)
5485 perf_pmu_disable(pmu);
5488 static int perf_pmu_commit_txn(struct pmu *pmu)
5490 perf_pmu_enable(pmu);
5494 static void perf_pmu_cancel_txn(struct pmu *pmu)
5496 perf_pmu_enable(pmu);
5499 static int perf_event_idx_default(struct perf_event *event)
5501 return event->hw.idx + 1;
5505 * Ensures all contexts with the same task_ctx_nr have the same
5506 * pmu_cpu_context too.
5508 static void *find_pmu_context(int ctxn)
5515 list_for_each_entry(pmu, &pmus, entry) {
5516 if (pmu->task_ctx_nr == ctxn)
5517 return pmu->pmu_cpu_context;
5523 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
5527 for_each_possible_cpu(cpu) {
5528 struct perf_cpu_context *cpuctx;
5530 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5532 if (cpuctx->active_pmu == old_pmu)
5533 cpuctx->active_pmu = pmu;
5537 static void free_pmu_context(struct pmu *pmu)
5541 mutex_lock(&pmus_lock);
5543 * Like a real lame refcount.
5545 list_for_each_entry(i, &pmus, entry) {
5546 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5547 update_pmu_context(i, pmu);
5552 free_percpu(pmu->pmu_cpu_context);
5554 mutex_unlock(&pmus_lock);
5556 static struct idr pmu_idr;
5559 type_show(struct device *dev, struct device_attribute *attr, char *page)
5561 struct pmu *pmu = dev_get_drvdata(dev);
5563 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5566 static struct device_attribute pmu_dev_attrs[] = {
5571 static int pmu_bus_running;
5572 static struct bus_type pmu_bus = {
5573 .name = "event_source",
5574 .dev_attrs = pmu_dev_attrs,
5577 static void pmu_dev_release(struct device *dev)
5582 static int pmu_dev_alloc(struct pmu *pmu)
5586 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5590 pmu->dev->groups = pmu->attr_groups;
5591 device_initialize(pmu->dev);
5592 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5596 dev_set_drvdata(pmu->dev, pmu);
5597 pmu->dev->bus = &pmu_bus;
5598 pmu->dev->release = pmu_dev_release;
5599 ret = device_add(pmu->dev);
5607 put_device(pmu->dev);
5611 static struct lock_class_key cpuctx_mutex;
5612 static struct lock_class_key cpuctx_lock;
5614 int perf_pmu_register(struct pmu *pmu, char *name, int type)
5618 mutex_lock(&pmus_lock);
5620 pmu->pmu_disable_count = alloc_percpu(int);
5621 if (!pmu->pmu_disable_count)
5630 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5634 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5642 if (pmu_bus_running) {
5643 ret = pmu_dev_alloc(pmu);
5649 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5650 if (pmu->pmu_cpu_context)
5651 goto got_cpu_context;
5653 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5654 if (!pmu->pmu_cpu_context)
5657 for_each_possible_cpu(cpu) {
5658 struct perf_cpu_context *cpuctx;
5660 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5661 __perf_event_init_context(&cpuctx->ctx);
5662 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
5663 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
5664 cpuctx->ctx.type = cpu_context;
5665 cpuctx->ctx.pmu = pmu;
5666 cpuctx->jiffies_interval = 1;
5667 INIT_LIST_HEAD(&cpuctx->rotation_list);
5668 cpuctx->active_pmu = pmu;
5672 if (!pmu->start_txn) {
5673 if (pmu->pmu_enable) {
5675 * If we have pmu_enable/pmu_disable calls, install
5676 * transaction stubs that use that to try and batch
5677 * hardware accesses.
5679 pmu->start_txn = perf_pmu_start_txn;
5680 pmu->commit_txn = perf_pmu_commit_txn;
5681 pmu->cancel_txn = perf_pmu_cancel_txn;
5683 pmu->start_txn = perf_pmu_nop_void;
5684 pmu->commit_txn = perf_pmu_nop_int;
5685 pmu->cancel_txn = perf_pmu_nop_void;
5689 if (!pmu->pmu_enable) {
5690 pmu->pmu_enable = perf_pmu_nop_void;
5691 pmu->pmu_disable = perf_pmu_nop_void;
5694 if (!pmu->event_idx)
5695 pmu->event_idx = perf_event_idx_default;
5697 list_add_rcu(&pmu->entry, &pmus);
5700 mutex_unlock(&pmus_lock);
5705 device_del(pmu->dev);
5706 put_device(pmu->dev);
5709 if (pmu->type >= PERF_TYPE_MAX)
5710 idr_remove(&pmu_idr, pmu->type);
5713 free_percpu(pmu->pmu_disable_count);
5717 void perf_pmu_unregister(struct pmu *pmu)
5719 mutex_lock(&pmus_lock);
5720 list_del_rcu(&pmu->entry);
5721 mutex_unlock(&pmus_lock);
5724 * We dereference the pmu list under both SRCU and regular RCU, so
5725 * synchronize against both of those.
5727 synchronize_srcu(&pmus_srcu);
5730 free_percpu(pmu->pmu_disable_count);
5731 if (pmu->type >= PERF_TYPE_MAX)
5732 idr_remove(&pmu_idr, pmu->type);
5733 device_del(pmu->dev);
5734 put_device(pmu->dev);
5735 free_pmu_context(pmu);
5738 struct pmu *perf_init_event(struct perf_event *event)
5740 struct pmu *pmu = NULL;
5744 idx = srcu_read_lock(&pmus_srcu);
5747 pmu = idr_find(&pmu_idr, event->attr.type);
5751 ret = pmu->event_init(event);
5757 list_for_each_entry_rcu(pmu, &pmus, entry) {
5759 ret = pmu->event_init(event);
5763 if (ret != -ENOENT) {
5768 pmu = ERR_PTR(-ENOENT);
5770 srcu_read_unlock(&pmus_srcu, idx);
5776 * Allocate and initialize a event structure
5778 static struct perf_event *
5779 perf_event_alloc(struct perf_event_attr *attr, int cpu,
5780 struct task_struct *task,
5781 struct perf_event *group_leader,
5782 struct perf_event *parent_event,
5783 perf_overflow_handler_t overflow_handler,
5787 struct perf_event *event;
5788 struct hw_perf_event *hwc;
5791 if ((unsigned)cpu >= nr_cpu_ids) {
5792 if (!task || cpu != -1)
5793 return ERR_PTR(-EINVAL);
5796 event = kzalloc(sizeof(*event), GFP_KERNEL);
5798 return ERR_PTR(-ENOMEM);
5801 * Single events are their own group leaders, with an
5802 * empty sibling list:
5805 group_leader = event;
5807 mutex_init(&event->child_mutex);
5808 INIT_LIST_HEAD(&event->child_list);
5810 INIT_LIST_HEAD(&event->group_entry);
5811 INIT_LIST_HEAD(&event->event_entry);
5812 INIT_LIST_HEAD(&event->sibling_list);
5813 INIT_LIST_HEAD(&event->rb_entry);
5815 init_waitqueue_head(&event->waitq);
5816 init_irq_work(&event->pending, perf_pending_event);
5818 mutex_init(&event->mmap_mutex);
5821 event->attr = *attr;
5822 event->group_leader = group_leader;
5826 event->parent = parent_event;
5828 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5829 event->id = atomic64_inc_return(&perf_event_id);
5831 event->state = PERF_EVENT_STATE_INACTIVE;
5834 event->attach_state = PERF_ATTACH_TASK;
5835 #ifdef CONFIG_HAVE_HW_BREAKPOINT
5837 * hw_breakpoint is a bit difficult here..
5839 if (attr->type == PERF_TYPE_BREAKPOINT)
5840 event->hw.bp_target = task;
5844 if (!overflow_handler && parent_event) {
5845 overflow_handler = parent_event->overflow_handler;
5846 context = parent_event->overflow_handler_context;
5849 event->overflow_handler = overflow_handler;
5850 event->overflow_handler_context = context;
5853 event->state = PERF_EVENT_STATE_OFF;
5858 hwc->sample_period = attr->sample_period;
5859 if (attr->freq && attr->sample_freq)
5860 hwc->sample_period = 1;
5861 hwc->last_period = hwc->sample_period;
5863 local64_set(&hwc->period_left, hwc->sample_period);
5866 * we currently do not support PERF_FORMAT_GROUP on inherited events
5868 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5871 pmu = perf_init_event(event);
5877 else if (IS_ERR(pmu))
5882 put_pid_ns(event->ns);
5884 return ERR_PTR(err);
5887 if (!event->parent) {
5888 if (event->attach_state & PERF_ATTACH_TASK)
5889 static_key_slow_inc(&perf_sched_events.key);
5890 if (event->attr.mmap || event->attr.mmap_data)
5891 atomic_inc(&nr_mmap_events);
5892 if (event->attr.comm)
5893 atomic_inc(&nr_comm_events);
5894 if (event->attr.task)
5895 atomic_inc(&nr_task_events);
5896 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5897 err = get_callchain_buffers();
5900 return ERR_PTR(err);
5908 static int perf_copy_attr(struct perf_event_attr __user *uattr,
5909 struct perf_event_attr *attr)
5914 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5918 * zero the full structure, so that a short copy will be nice.
5920 memset(attr, 0, sizeof(*attr));
5922 ret = get_user(size, &uattr->size);
5926 if (size > PAGE_SIZE) /* silly large */
5929 if (!size) /* abi compat */
5930 size = PERF_ATTR_SIZE_VER0;
5932 if (size < PERF_ATTR_SIZE_VER0)
5936 * If we're handed a bigger struct than we know of,
5937 * ensure all the unknown bits are 0 - i.e. new
5938 * user-space does not rely on any kernel feature
5939 * extensions we dont know about yet.
5941 if (size > sizeof(*attr)) {
5942 unsigned char __user *addr;
5943 unsigned char __user *end;
5946 addr = (void __user *)uattr + sizeof(*attr);
5947 end = (void __user *)uattr + size;
5949 for (; addr < end; addr++) {
5950 ret = get_user(val, addr);
5956 size = sizeof(*attr);
5959 ret = copy_from_user(attr, uattr, size);
5963 if (attr->__reserved_1)
5966 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5969 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5972 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
5973 u64 mask = attr->branch_sample_type;
5975 /* only using defined bits */
5976 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
5979 /* at least one branch bit must be set */
5980 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
5983 /* kernel level capture: check permissions */
5984 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
5985 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5988 /* propagate priv level, when not set for branch */
5989 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
5991 /* exclude_kernel checked on syscall entry */
5992 if (!attr->exclude_kernel)
5993 mask |= PERF_SAMPLE_BRANCH_KERNEL;
5995 if (!attr->exclude_user)
5996 mask |= PERF_SAMPLE_BRANCH_USER;
5998 if (!attr->exclude_hv)
5999 mask |= PERF_SAMPLE_BRANCH_HV;
6001 * adjust user setting (for HW filter setup)
6003 attr->branch_sample_type = mask;
6010 put_user(sizeof(*attr), &uattr->size);
6016 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6018 struct ring_buffer *rb = NULL, *old_rb = NULL;
6024 /* don't allow circular references */
6025 if (event == output_event)
6029 * Don't allow cross-cpu buffers
6031 if (output_event->cpu != event->cpu)
6035 * If its not a per-cpu rb, it must be the same task.
6037 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6041 mutex_lock(&event->mmap_mutex);
6042 /* Can't redirect output if we've got an active mmap() */
6043 if (atomic_read(&event->mmap_count))
6047 /* get the rb we want to redirect to */
6048 rb = ring_buffer_get(output_event);
6054 rcu_assign_pointer(event->rb, rb);
6056 ring_buffer_detach(event, old_rb);
6059 mutex_unlock(&event->mmap_mutex);
6062 ring_buffer_put(old_rb);
6068 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6070 * @attr_uptr: event_id type attributes for monitoring/sampling
6073 * @group_fd: group leader event fd
6075 SYSCALL_DEFINE5(perf_event_open,
6076 struct perf_event_attr __user *, attr_uptr,
6077 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6079 struct perf_event *group_leader = NULL, *output_event = NULL;
6080 struct perf_event *event, *sibling;
6081 struct perf_event_attr attr;
6082 struct perf_event_context *ctx;
6083 struct file *event_file = NULL;
6084 struct file *group_file = NULL;
6085 struct task_struct *task = NULL;
6089 int fput_needed = 0;
6092 /* for future expandability... */
6093 if (flags & ~PERF_FLAG_ALL)
6096 err = perf_copy_attr(attr_uptr, &attr);
6100 if (!attr.exclude_kernel) {
6101 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6106 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6111 * In cgroup mode, the pid argument is used to pass the fd
6112 * opened to the cgroup directory in cgroupfs. The cpu argument
6113 * designates the cpu on which to monitor threads from that
6116 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6119 event_fd = get_unused_fd_flags(O_RDWR);
6123 if (group_fd != -1) {
6124 group_leader = perf_fget_light(group_fd, &fput_needed);
6125 if (IS_ERR(group_leader)) {
6126 err = PTR_ERR(group_leader);
6129 group_file = group_leader->filp;
6130 if (flags & PERF_FLAG_FD_OUTPUT)
6131 output_event = group_leader;
6132 if (flags & PERF_FLAG_FD_NO_GROUP)
6133 group_leader = NULL;
6136 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
6137 task = find_lively_task_by_vpid(pid);
6139 err = PTR_ERR(task);
6144 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
6146 if (IS_ERR(event)) {
6147 err = PTR_ERR(event);
6151 if (flags & PERF_FLAG_PID_CGROUP) {
6152 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6157 * - that has cgroup constraint on event->cpu
6158 * - that may need work on context switch
6160 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6161 static_key_slow_inc(&perf_sched_events.key);
6165 * Special case software events and allow them to be part of
6166 * any hardware group.
6171 (is_software_event(event) != is_software_event(group_leader))) {
6172 if (is_software_event(event)) {
6174 * If event and group_leader are not both a software
6175 * event, and event is, then group leader is not.
6177 * Allow the addition of software events to !software
6178 * groups, this is safe because software events never
6181 pmu = group_leader->pmu;
6182 } else if (is_software_event(group_leader) &&
6183 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6185 * In case the group is a pure software group, and we
6186 * try to add a hardware event, move the whole group to
6187 * the hardware context.
6194 * Get the target context (task or percpu):
6196 ctx = find_get_context(pmu, task, cpu);
6203 put_task_struct(task);
6208 * Look up the group leader (we will attach this event to it):
6214 * Do not allow a recursive hierarchy (this new sibling
6215 * becoming part of another group-sibling):
6217 if (group_leader->group_leader != group_leader)
6220 * Do not allow to attach to a group in a different
6221 * task or CPU context:
6224 if (group_leader->ctx->type != ctx->type)
6227 if (group_leader->ctx != ctx)
6232 * Only a group leader can be exclusive or pinned
6234 if (attr.exclusive || attr.pinned)
6239 err = perf_event_set_output(event, output_event);
6244 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6245 if (IS_ERR(event_file)) {
6246 err = PTR_ERR(event_file);
6251 struct perf_event_context *gctx = group_leader->ctx;
6253 mutex_lock(&gctx->mutex);
6254 perf_remove_from_context(group_leader);
6255 list_for_each_entry(sibling, &group_leader->sibling_list,
6257 perf_remove_from_context(sibling);
6260 mutex_unlock(&gctx->mutex);
6264 event->filp = event_file;
6265 WARN_ON_ONCE(ctx->parent_ctx);
6266 mutex_lock(&ctx->mutex);
6269 perf_install_in_context(ctx, group_leader, cpu);
6271 list_for_each_entry(sibling, &group_leader->sibling_list,
6273 perf_install_in_context(ctx, sibling, cpu);
6278 perf_install_in_context(ctx, event, cpu);
6280 perf_unpin_context(ctx);
6281 mutex_unlock(&ctx->mutex);
6283 event->owner = current;
6285 mutex_lock(¤t->perf_event_mutex);
6286 list_add_tail(&event->owner_entry, ¤t->perf_event_list);
6287 mutex_unlock(¤t->perf_event_mutex);
6290 * Precalculate sample_data sizes
6292 perf_event__header_size(event);
6293 perf_event__id_header_size(event);
6296 * Drop the reference on the group_event after placing the
6297 * new event on the sibling_list. This ensures destruction
6298 * of the group leader will find the pointer to itself in
6299 * perf_group_detach().
6301 fput_light(group_file, fput_needed);
6302 fd_install(event_fd, event_file);
6306 perf_unpin_context(ctx);
6312 put_task_struct(task);
6314 fput_light(group_file, fput_needed);
6316 put_unused_fd(event_fd);
6321 * perf_event_create_kernel_counter
6323 * @attr: attributes of the counter to create
6324 * @cpu: cpu in which the counter is bound
6325 * @task: task to profile (NULL for percpu)
6328 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
6329 struct task_struct *task,
6330 perf_overflow_handler_t overflow_handler,
6333 struct perf_event_context *ctx;
6334 struct perf_event *event;
6338 * Get the target context (task or percpu):
6341 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
6342 overflow_handler, context);
6343 if (IS_ERR(event)) {
6344 err = PTR_ERR(event);
6348 ctx = find_get_context(event->pmu, task, cpu);
6355 WARN_ON_ONCE(ctx->parent_ctx);
6356 mutex_lock(&ctx->mutex);
6357 perf_install_in_context(ctx, event, cpu);
6359 perf_unpin_context(ctx);
6360 mutex_unlock(&ctx->mutex);
6367 return ERR_PTR(err);
6369 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6371 static void sync_child_event(struct perf_event *child_event,
6372 struct task_struct *child)
6374 struct perf_event *parent_event = child_event->parent;
6377 if (child_event->attr.inherit_stat)
6378 perf_event_read_event(child_event, child);
6380 child_val = perf_event_count(child_event);
6383 * Add back the child's count to the parent's count:
6385 atomic64_add(child_val, &parent_event->child_count);
6386 atomic64_add(child_event->total_time_enabled,
6387 &parent_event->child_total_time_enabled);
6388 atomic64_add(child_event->total_time_running,
6389 &parent_event->child_total_time_running);
6392 * Remove this event from the parent's list
6394 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6395 mutex_lock(&parent_event->child_mutex);
6396 list_del_init(&child_event->child_list);
6397 mutex_unlock(&parent_event->child_mutex);
6400 * Release the parent event, if this was the last
6403 fput(parent_event->filp);
6407 __perf_event_exit_task(struct perf_event *child_event,
6408 struct perf_event_context *child_ctx,
6409 struct task_struct *child)
6411 if (child_event->parent) {
6412 raw_spin_lock_irq(&child_ctx->lock);
6413 perf_group_detach(child_event);
6414 raw_spin_unlock_irq(&child_ctx->lock);
6417 perf_remove_from_context(child_event);
6420 * It can happen that the parent exits first, and has events
6421 * that are still around due to the child reference. These
6422 * events need to be zapped.
6424 if (child_event->parent) {
6425 sync_child_event(child_event, child);
6426 free_event(child_event);
6430 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
6432 struct perf_event *child_event, *tmp;
6433 struct perf_event_context *child_ctx;
6434 unsigned long flags;
6436 if (likely(!child->perf_event_ctxp[ctxn])) {
6437 perf_event_task(child, NULL, 0);
6441 local_irq_save(flags);
6443 * We can't reschedule here because interrupts are disabled,
6444 * and either child is current or it is a task that can't be
6445 * scheduled, so we are now safe from rescheduling changing
6448 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
6451 * Take the context lock here so that if find_get_context is
6452 * reading child->perf_event_ctxp, we wait until it has
6453 * incremented the context's refcount before we do put_ctx below.
6455 raw_spin_lock(&child_ctx->lock);
6456 task_ctx_sched_out(child_ctx);
6457 child->perf_event_ctxp[ctxn] = NULL;
6459 * If this context is a clone; unclone it so it can't get
6460 * swapped to another process while we're removing all
6461 * the events from it.
6463 unclone_ctx(child_ctx);
6464 update_context_time(child_ctx);
6465 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6468 * Report the task dead after unscheduling the events so that we
6469 * won't get any samples after PERF_RECORD_EXIT. We can however still
6470 * get a few PERF_RECORD_READ events.
6472 perf_event_task(child, child_ctx, 0);
6475 * We can recurse on the same lock type through:
6477 * __perf_event_exit_task()
6478 * sync_child_event()
6479 * fput(parent_event->filp)
6481 * mutex_lock(&ctx->mutex)
6483 * But since its the parent context it won't be the same instance.
6485 mutex_lock(&child_ctx->mutex);
6488 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6490 __perf_event_exit_task(child_event, child_ctx, child);
6492 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
6494 __perf_event_exit_task(child_event, child_ctx, child);
6497 * If the last event was a group event, it will have appended all
6498 * its siblings to the list, but we obtained 'tmp' before that which
6499 * will still point to the list head terminating the iteration.
6501 if (!list_empty(&child_ctx->pinned_groups) ||
6502 !list_empty(&child_ctx->flexible_groups))
6505 mutex_unlock(&child_ctx->mutex);
6511 * When a child task exits, feed back event values to parent events.
6513 void perf_event_exit_task(struct task_struct *child)
6515 struct perf_event *event, *tmp;
6518 mutex_lock(&child->perf_event_mutex);
6519 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6521 list_del_init(&event->owner_entry);
6524 * Ensure the list deletion is visible before we clear
6525 * the owner, closes a race against perf_release() where
6526 * we need to serialize on the owner->perf_event_mutex.
6529 event->owner = NULL;
6531 mutex_unlock(&child->perf_event_mutex);
6533 for_each_task_context_nr(ctxn)
6534 perf_event_exit_task_context(child, ctxn);
6537 static void perf_free_event(struct perf_event *event,
6538 struct perf_event_context *ctx)
6540 struct perf_event *parent = event->parent;
6542 if (WARN_ON_ONCE(!parent))
6545 mutex_lock(&parent->child_mutex);
6546 list_del_init(&event->child_list);
6547 mutex_unlock(&parent->child_mutex);
6551 perf_group_detach(event);
6552 list_del_event(event, ctx);
6557 * free an unexposed, unused context as created by inheritance by
6558 * perf_event_init_task below, used by fork() in case of fail.
6560 void perf_event_free_task(struct task_struct *task)
6562 struct perf_event_context *ctx;
6563 struct perf_event *event, *tmp;
6566 for_each_task_context_nr(ctxn) {
6567 ctx = task->perf_event_ctxp[ctxn];
6571 mutex_lock(&ctx->mutex);
6573 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6575 perf_free_event(event, ctx);
6577 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6579 perf_free_event(event, ctx);
6581 if (!list_empty(&ctx->pinned_groups) ||
6582 !list_empty(&ctx->flexible_groups))
6585 mutex_unlock(&ctx->mutex);
6591 void perf_event_delayed_put(struct task_struct *task)
6595 for_each_task_context_nr(ctxn)
6596 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6600 * inherit a event from parent task to child task:
6602 static struct perf_event *
6603 inherit_event(struct perf_event *parent_event,
6604 struct task_struct *parent,
6605 struct perf_event_context *parent_ctx,
6606 struct task_struct *child,
6607 struct perf_event *group_leader,
6608 struct perf_event_context *child_ctx)
6610 struct perf_event *child_event;
6611 unsigned long flags;
6614 * Instead of creating recursive hierarchies of events,
6615 * we link inherited events back to the original parent,
6616 * which has a filp for sure, which we use as the reference
6619 if (parent_event->parent)
6620 parent_event = parent_event->parent;
6622 child_event = perf_event_alloc(&parent_event->attr,
6625 group_leader, parent_event,
6627 if (IS_ERR(child_event))
6632 * Make the child state follow the state of the parent event,
6633 * not its attr.disabled bit. We hold the parent's mutex,
6634 * so we won't race with perf_event_{en, dis}able_family.
6636 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6637 child_event->state = PERF_EVENT_STATE_INACTIVE;
6639 child_event->state = PERF_EVENT_STATE_OFF;
6641 if (parent_event->attr.freq) {
6642 u64 sample_period = parent_event->hw.sample_period;
6643 struct hw_perf_event *hwc = &child_event->hw;
6645 hwc->sample_period = sample_period;
6646 hwc->last_period = sample_period;
6648 local64_set(&hwc->period_left, sample_period);
6651 child_event->ctx = child_ctx;
6652 child_event->overflow_handler = parent_event->overflow_handler;
6653 child_event->overflow_handler_context
6654 = parent_event->overflow_handler_context;
6657 * Precalculate sample_data sizes
6659 perf_event__header_size(child_event);
6660 perf_event__id_header_size(child_event);
6663 * Link it up in the child's context:
6665 raw_spin_lock_irqsave(&child_ctx->lock, flags);
6666 add_event_to_ctx(child_event, child_ctx);
6667 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
6670 * Get a reference to the parent filp - we will fput it
6671 * when the child event exits. This is safe to do because
6672 * we are in the parent and we know that the filp still
6673 * exists and has a nonzero count:
6675 atomic_long_inc(&parent_event->filp->f_count);
6678 * Link this into the parent event's child list
6680 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6681 mutex_lock(&parent_event->child_mutex);
6682 list_add_tail(&child_event->child_list, &parent_event->child_list);
6683 mutex_unlock(&parent_event->child_mutex);
6688 static int inherit_group(struct perf_event *parent_event,
6689 struct task_struct *parent,
6690 struct perf_event_context *parent_ctx,
6691 struct task_struct *child,
6692 struct perf_event_context *child_ctx)
6694 struct perf_event *leader;
6695 struct perf_event *sub;
6696 struct perf_event *child_ctr;
6698 leader = inherit_event(parent_event, parent, parent_ctx,
6699 child, NULL, child_ctx);
6701 return PTR_ERR(leader);
6702 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6703 child_ctr = inherit_event(sub, parent, parent_ctx,
6704 child, leader, child_ctx);
6705 if (IS_ERR(child_ctr))
6706 return PTR_ERR(child_ctr);
6712 inherit_task_group(struct perf_event *event, struct task_struct *parent,
6713 struct perf_event_context *parent_ctx,
6714 struct task_struct *child, int ctxn,
6718 struct perf_event_context *child_ctx;
6720 if (!event->attr.inherit) {
6725 child_ctx = child->perf_event_ctxp[ctxn];
6728 * This is executed from the parent task context, so
6729 * inherit events that have been marked for cloning.
6730 * First allocate and initialize a context for the
6734 child_ctx = alloc_perf_context(event->pmu, child);
6738 child->perf_event_ctxp[ctxn] = child_ctx;
6741 ret = inherit_group(event, parent, parent_ctx,
6751 * Initialize the perf_event context in task_struct
6753 int perf_event_init_context(struct task_struct *child, int ctxn)
6755 struct perf_event_context *child_ctx, *parent_ctx;
6756 struct perf_event_context *cloned_ctx;
6757 struct perf_event *event;
6758 struct task_struct *parent = current;
6759 int inherited_all = 1;
6760 unsigned long flags;
6763 if (likely(!parent->perf_event_ctxp[ctxn]))
6767 * If the parent's context is a clone, pin it so it won't get
6770 parent_ctx = perf_pin_task_context(parent, ctxn);
6773 * No need to check if parent_ctx != NULL here; since we saw
6774 * it non-NULL earlier, the only reason for it to become NULL
6775 * is if we exit, and since we're currently in the middle of
6776 * a fork we can't be exiting at the same time.
6780 * Lock the parent list. No need to lock the child - not PID
6781 * hashed yet and not running, so nobody can access it.
6783 mutex_lock(&parent_ctx->mutex);
6786 * We dont have to disable NMIs - we are only looking at
6787 * the list, not manipulating it:
6789 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
6790 ret = inherit_task_group(event, parent, parent_ctx,
6791 child, ctxn, &inherited_all);
6797 * We can't hold ctx->lock when iterating the ->flexible_group list due
6798 * to allocations, but we need to prevent rotation because
6799 * rotate_ctx() will change the list from interrupt context.
6801 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6802 parent_ctx->rotate_disable = 1;
6803 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6805 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
6806 ret = inherit_task_group(event, parent, parent_ctx,
6807 child, ctxn, &inherited_all);
6812 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6813 parent_ctx->rotate_disable = 0;
6815 child_ctx = child->perf_event_ctxp[ctxn];
6817 if (child_ctx && inherited_all) {
6819 * Mark the child context as a clone of the parent
6820 * context, or of whatever the parent is a clone of.
6822 * Note that if the parent is a clone, the holding of
6823 * parent_ctx->lock avoids it from being uncloned.
6825 cloned_ctx = parent_ctx->parent_ctx;
6827 child_ctx->parent_ctx = cloned_ctx;
6828 child_ctx->parent_gen = parent_ctx->parent_gen;
6830 child_ctx->parent_ctx = parent_ctx;
6831 child_ctx->parent_gen = parent_ctx->generation;
6833 get_ctx(child_ctx->parent_ctx);
6836 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6837 mutex_unlock(&parent_ctx->mutex);
6839 perf_unpin_context(parent_ctx);
6840 put_ctx(parent_ctx);
6846 * Initialize the perf_event context in task_struct
6848 int perf_event_init_task(struct task_struct *child)
6852 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
6853 mutex_init(&child->perf_event_mutex);
6854 INIT_LIST_HEAD(&child->perf_event_list);
6856 for_each_task_context_nr(ctxn) {
6857 ret = perf_event_init_context(child, ctxn);
6865 static void __init perf_event_init_all_cpus(void)
6867 struct swevent_htable *swhash;
6870 for_each_possible_cpu(cpu) {
6871 swhash = &per_cpu(swevent_htable, cpu);
6872 mutex_init(&swhash->hlist_mutex);
6873 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
6877 static void __cpuinit perf_event_init_cpu(int cpu)
6879 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6881 mutex_lock(&swhash->hlist_mutex);
6882 if (swhash->hlist_refcount > 0) {
6883 struct swevent_hlist *hlist;
6885 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6887 rcu_assign_pointer(swhash->swevent_hlist, hlist);
6889 mutex_unlock(&swhash->hlist_mutex);
6892 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
6893 static void perf_pmu_rotate_stop(struct pmu *pmu)
6895 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6897 WARN_ON(!irqs_disabled());
6899 list_del_init(&cpuctx->rotation_list);
6902 static void __perf_event_exit_context(void *__info)
6904 struct perf_event_context *ctx = __info;
6905 struct perf_event *event, *tmp;
6907 perf_pmu_rotate_stop(ctx->pmu);
6909 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
6910 __perf_remove_from_context(event);
6911 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
6912 __perf_remove_from_context(event);
6915 static void perf_event_exit_cpu_context(int cpu)
6917 struct perf_event_context *ctx;
6921 idx = srcu_read_lock(&pmus_srcu);
6922 list_for_each_entry_rcu(pmu, &pmus, entry) {
6923 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
6925 mutex_lock(&ctx->mutex);
6926 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6927 mutex_unlock(&ctx->mutex);
6929 srcu_read_unlock(&pmus_srcu, idx);
6932 static void perf_event_exit_cpu(int cpu)
6934 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
6936 mutex_lock(&swhash->hlist_mutex);
6937 swevent_hlist_release(swhash);
6938 mutex_unlock(&swhash->hlist_mutex);
6940 perf_event_exit_cpu_context(cpu);
6943 static inline void perf_event_exit_cpu(int cpu) { }
6947 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
6951 for_each_online_cpu(cpu)
6952 perf_event_exit_cpu(cpu);
6958 * Run the perf reboot notifier at the very last possible moment so that
6959 * the generic watchdog code runs as long as possible.
6961 static struct notifier_block perf_reboot_notifier = {
6962 .notifier_call = perf_reboot,
6963 .priority = INT_MIN,
6966 static int __cpuinit
6967 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6969 unsigned int cpu = (long)hcpu;
6971 switch (action & ~CPU_TASKS_FROZEN) {
6973 case CPU_UP_PREPARE:
6974 case CPU_DOWN_FAILED:
6975 perf_event_init_cpu(cpu);
6978 case CPU_UP_CANCELED:
6979 case CPU_DOWN_PREPARE:
6980 perf_event_exit_cpu(cpu);
6990 void __init perf_event_init(void)
6996 perf_event_init_all_cpus();
6997 init_srcu_struct(&pmus_srcu);
6998 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
6999 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7000 perf_pmu_register(&perf_task_clock, NULL, -1);
7002 perf_cpu_notifier(perf_cpu_notify);
7003 register_reboot_notifier(&perf_reboot_notifier);
7005 ret = init_hw_breakpoint();
7006 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7008 /* do not patch jump label more than once per second */
7009 jump_label_rate_limit(&perf_sched_events, HZ);
7012 static int __init perf_event_sysfs_init(void)
7017 mutex_lock(&pmus_lock);
7019 ret = bus_register(&pmu_bus);
7023 list_for_each_entry(pmu, &pmus, entry) {
7024 if (!pmu->name || pmu->type < 0)
7027 ret = pmu_dev_alloc(pmu);
7028 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7030 pmu_bus_running = 1;
7034 mutex_unlock(&pmus_lock);
7038 device_initcall(perf_event_sysfs_init);
7040 #ifdef CONFIG_CGROUP_PERF
7041 static struct cgroup_subsys_state *perf_cgroup_create(
7042 struct cgroup_subsys *ss, struct cgroup *cont)
7044 struct perf_cgroup *jc;
7046 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
7048 return ERR_PTR(-ENOMEM);
7050 jc->info = alloc_percpu(struct perf_cgroup_info);
7053 return ERR_PTR(-ENOMEM);
7059 static void perf_cgroup_destroy(struct cgroup_subsys *ss,
7060 struct cgroup *cont)
7062 struct perf_cgroup *jc;
7063 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7064 struct perf_cgroup, css);
7065 free_percpu(jc->info);
7069 static int __perf_cgroup_move(void *info)
7071 struct task_struct *task = info;
7072 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7076 static void perf_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
7077 struct cgroup_taskset *tset)
7079 struct task_struct *task;
7081 cgroup_taskset_for_each(task, cgrp, tset)
7082 task_function_call(task, __perf_cgroup_move, task);
7085 static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7086 struct cgroup *old_cgrp, struct task_struct *task)
7089 * cgroup_exit() is called in the copy_process() failure path.
7090 * Ignore this case since the task hasn't ran yet, this avoids
7091 * trying to poke a half freed task state from generic code.
7093 if (!(task->flags & PF_EXITING))
7096 task_function_call(task, __perf_cgroup_move, task);
7099 struct cgroup_subsys perf_subsys = {
7100 .name = "perf_event",
7101 .subsys_id = perf_subsys_id,
7102 .create = perf_cgroup_create,
7103 .destroy = perf_cgroup_destroy,
7104 .exit = perf_cgroup_exit,
7105 .attach = perf_cgroup_attach,
7107 #endif /* CONFIG_CGROUP_PERF */