igb: Cleanups to fix static initialization
[linux-2.6/btrfs-unstable.git] / kernel / events / core.c
blobf83a71a3e46d75547e540ed317f99531838f408b
1 /*
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
12 #include <linux/fs.h>
13 #include <linux/mm.h>
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/tick.h>
22 #include <linux/sysfs.h>
23 #include <linux/dcache.h>
24 #include <linux/percpu.h>
25 #include <linux/ptrace.h>
26 #include <linux/reboot.h>
27 #include <linux/vmstat.h>
28 #include <linux/device.h>
29 #include <linux/export.h>
30 #include <linux/vmalloc.h>
31 #include <linux/hardirq.h>
32 #include <linux/rculist.h>
33 #include <linux/uaccess.h>
34 #include <linux/syscalls.h>
35 #include <linux/anon_inodes.h>
36 #include <linux/kernel_stat.h>
37 #include <linux/perf_event.h>
38 #include <linux/ftrace_event.h>
39 #include <linux/hw_breakpoint.h>
40 #include <linux/mm_types.h>
41 #include <linux/cgroup.h>
43 #include "internal.h"
45 #include <asm/irq_regs.h>
47 struct remote_function_call {
48 struct task_struct *p;
49 int (*func)(void *info);
50 void *info;
51 int ret;
54 static void remote_function(void *data)
56 struct remote_function_call *tfc = data;
57 struct task_struct *p = tfc->p;
59 if (p) {
60 tfc->ret = -EAGAIN;
61 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
62 return;
65 tfc->ret = tfc->func(tfc->info);
68 /**
69 * task_function_call - call a function on the cpu on which a task runs
70 * @p: the task to evaluate
71 * @func: the function to be called
72 * @info: the function call argument
74 * Calls the function @func when the task is currently running. This might
75 * be on the current CPU, which just calls the function directly
77 * returns: @func return value, or
78 * -ESRCH - when the process isn't running
79 * -EAGAIN - when the process moved away
81 static int
82 task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
84 struct remote_function_call data = {
85 .p = p,
86 .func = func,
87 .info = info,
88 .ret = -ESRCH, /* No such (running) process */
91 if (task_curr(p))
92 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
94 return data.ret;
97 /**
98 * cpu_function_call - call a function on the cpu
99 * @func: the function to be called
100 * @info: the function call argument
102 * Calls the function @func on the remote cpu.
104 * returns: @func return value or -ENXIO when the cpu is offline
106 static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
108 struct remote_function_call data = {
109 .p = NULL,
110 .func = func,
111 .info = info,
112 .ret = -ENXIO, /* No such CPU */
115 smp_call_function_single(cpu, remote_function, &data, 1);
117 return data.ret;
120 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
121 PERF_FLAG_FD_OUTPUT |\
122 PERF_FLAG_PID_CGROUP |\
123 PERF_FLAG_FD_CLOEXEC)
126 * branch priv levels that need permission checks
128 #define PERF_SAMPLE_BRANCH_PERM_PLM \
129 (PERF_SAMPLE_BRANCH_KERNEL |\
130 PERF_SAMPLE_BRANCH_HV)
132 enum event_type_t {
133 EVENT_FLEXIBLE = 0x1,
134 EVENT_PINNED = 0x2,
135 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
139 * perf_sched_events : >0 events exist
140 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
142 struct static_key_deferred perf_sched_events __read_mostly;
143 static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
144 static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
146 static atomic_t nr_mmap_events __read_mostly;
147 static atomic_t nr_comm_events __read_mostly;
148 static atomic_t nr_task_events __read_mostly;
149 static atomic_t nr_freq_events __read_mostly;
151 static LIST_HEAD(pmus);
152 static DEFINE_MUTEX(pmus_lock);
153 static struct srcu_struct pmus_srcu;
156 * perf event paranoia level:
157 * -1 - not paranoid at all
158 * 0 - disallow raw tracepoint access for unpriv
159 * 1 - disallow cpu events for unpriv
160 * 2 - disallow kernel profiling for unpriv
162 int sysctl_perf_event_paranoid __read_mostly = 1;
164 /* Minimum for 512 kiB + 1 user control page */
165 int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
168 * max perf event sample rate
170 #define DEFAULT_MAX_SAMPLE_RATE 100000
171 #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
172 #define DEFAULT_CPU_TIME_MAX_PERCENT 25
174 int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
176 static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
177 static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
179 static int perf_sample_allowed_ns __read_mostly =
180 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
182 void update_perf_cpu_limits(void)
184 u64 tmp = perf_sample_period_ns;
186 tmp *= sysctl_perf_cpu_time_max_percent;
187 do_div(tmp, 100);
188 ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
191 static int perf_rotate_context(struct perf_cpu_context *cpuctx);
193 int perf_proc_update_handler(struct ctl_table *table, int write,
194 void __user *buffer, size_t *lenp,
195 loff_t *ppos)
197 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
199 if (ret || !write)
200 return ret;
202 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
203 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
204 update_perf_cpu_limits();
206 return 0;
209 int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
211 int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
212 void __user *buffer, size_t *lenp,
213 loff_t *ppos)
215 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
217 if (ret || !write)
218 return ret;
220 update_perf_cpu_limits();
222 return 0;
226 * perf samples are done in some very critical code paths (NMIs).
227 * If they take too much CPU time, the system can lock up and not
228 * get any real work done. This will drop the sample rate when
229 * we detect that events are taking too long.
231 #define NR_ACCUMULATED_SAMPLES 128
232 static DEFINE_PER_CPU(u64, running_sample_length);
234 static void perf_duration_warn(struct irq_work *w)
236 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
237 u64 avg_local_sample_len;
238 u64 local_samples_len;
240 local_samples_len = __get_cpu_var(running_sample_length);
241 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
243 printk_ratelimited(KERN_WARNING
244 "perf interrupt took too long (%lld > %lld), lowering "
245 "kernel.perf_event_max_sample_rate to %d\n",
246 avg_local_sample_len, allowed_ns >> 1,
247 sysctl_perf_event_sample_rate);
250 static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
252 void perf_sample_event_took(u64 sample_len_ns)
254 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
255 u64 avg_local_sample_len;
256 u64 local_samples_len;
258 if (allowed_ns == 0)
259 return;
261 /* decay the counter by 1 average sample */
262 local_samples_len = __get_cpu_var(running_sample_length);
263 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
264 local_samples_len += sample_len_ns;
265 __get_cpu_var(running_sample_length) = local_samples_len;
268 * note: this will be biased artifically low until we have
269 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
270 * from having to maintain a count.
272 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
274 if (avg_local_sample_len <= allowed_ns)
275 return;
277 if (max_samples_per_tick <= 1)
278 return;
280 max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
281 sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
282 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
284 update_perf_cpu_limits();
286 if (!irq_work_queue(&perf_duration_work)) {
287 early_printk("perf interrupt took too long (%lld > %lld), lowering "
288 "kernel.perf_event_max_sample_rate to %d\n",
289 avg_local_sample_len, allowed_ns >> 1,
290 sysctl_perf_event_sample_rate);
294 static atomic64_t perf_event_id;
296 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
297 enum event_type_t event_type);
299 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
300 enum event_type_t event_type,
301 struct task_struct *task);
303 static void update_context_time(struct perf_event_context *ctx);
304 static u64 perf_event_time(struct perf_event *event);
306 void __weak perf_event_print_debug(void) { }
308 extern __weak const char *perf_pmu_name(void)
310 return "pmu";
313 static inline u64 perf_clock(void)
315 return local_clock();
318 static inline struct perf_cpu_context *
319 __get_cpu_context(struct perf_event_context *ctx)
321 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
324 static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
325 struct perf_event_context *ctx)
327 raw_spin_lock(&cpuctx->ctx.lock);
328 if (ctx)
329 raw_spin_lock(&ctx->lock);
332 static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
333 struct perf_event_context *ctx)
335 if (ctx)
336 raw_spin_unlock(&ctx->lock);
337 raw_spin_unlock(&cpuctx->ctx.lock);
340 #ifdef CONFIG_CGROUP_PERF
343 * perf_cgroup_info keeps track of time_enabled for a cgroup.
344 * This is a per-cpu dynamically allocated data structure.
346 struct perf_cgroup_info {
347 u64 time;
348 u64 timestamp;
351 struct perf_cgroup {
352 struct cgroup_subsys_state css;
353 struct perf_cgroup_info __percpu *info;
357 * Must ensure cgroup is pinned (css_get) before calling
358 * this function. In other words, we cannot call this function
359 * if there is no cgroup event for the current CPU context.
361 static inline struct perf_cgroup *
362 perf_cgroup_from_task(struct task_struct *task)
364 return container_of(task_css(task, perf_event_cgrp_id),
365 struct perf_cgroup, css);
368 static inline bool
369 perf_cgroup_match(struct perf_event *event)
371 struct perf_event_context *ctx = event->ctx;
372 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
374 /* @event doesn't care about cgroup */
375 if (!event->cgrp)
376 return true;
378 /* wants specific cgroup scope but @cpuctx isn't associated with any */
379 if (!cpuctx->cgrp)
380 return false;
383 * Cgroup scoping is recursive. An event enabled for a cgroup is
384 * also enabled for all its descendant cgroups. If @cpuctx's
385 * cgroup is a descendant of @event's (the test covers identity
386 * case), it's a match.
388 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
389 event->cgrp->css.cgroup);
392 static inline void perf_put_cgroup(struct perf_event *event)
394 css_put(&event->cgrp->css);
397 static inline void perf_detach_cgroup(struct perf_event *event)
399 perf_put_cgroup(event);
400 event->cgrp = NULL;
403 static inline int is_cgroup_event(struct perf_event *event)
405 return event->cgrp != NULL;
408 static inline u64 perf_cgroup_event_time(struct perf_event *event)
410 struct perf_cgroup_info *t;
412 t = per_cpu_ptr(event->cgrp->info, event->cpu);
413 return t->time;
416 static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
418 struct perf_cgroup_info *info;
419 u64 now;
421 now = perf_clock();
423 info = this_cpu_ptr(cgrp->info);
425 info->time += now - info->timestamp;
426 info->timestamp = now;
429 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
431 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
432 if (cgrp_out)
433 __update_cgrp_time(cgrp_out);
436 static inline void update_cgrp_time_from_event(struct perf_event *event)
438 struct perf_cgroup *cgrp;
441 * ensure we access cgroup data only when needed and
442 * when we know the cgroup is pinned (css_get)
444 if (!is_cgroup_event(event))
445 return;
447 cgrp = perf_cgroup_from_task(current);
449 * Do not update time when cgroup is not active
451 if (cgrp == event->cgrp)
452 __update_cgrp_time(event->cgrp);
455 static inline void
456 perf_cgroup_set_timestamp(struct task_struct *task,
457 struct perf_event_context *ctx)
459 struct perf_cgroup *cgrp;
460 struct perf_cgroup_info *info;
463 * ctx->lock held by caller
464 * ensure we do not access cgroup data
465 * unless we have the cgroup pinned (css_get)
467 if (!task || !ctx->nr_cgroups)
468 return;
470 cgrp = perf_cgroup_from_task(task);
471 info = this_cpu_ptr(cgrp->info);
472 info->timestamp = ctx->timestamp;
475 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
476 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
479 * reschedule events based on the cgroup constraint of task.
481 * mode SWOUT : schedule out everything
482 * mode SWIN : schedule in based on cgroup for next
484 void perf_cgroup_switch(struct task_struct *task, int mode)
486 struct perf_cpu_context *cpuctx;
487 struct pmu *pmu;
488 unsigned long flags;
491 * disable interrupts to avoid geting nr_cgroup
492 * changes via __perf_event_disable(). Also
493 * avoids preemption.
495 local_irq_save(flags);
498 * we reschedule only in the presence of cgroup
499 * constrained events.
501 rcu_read_lock();
503 list_for_each_entry_rcu(pmu, &pmus, entry) {
504 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
505 if (cpuctx->unique_pmu != pmu)
506 continue; /* ensure we process each cpuctx once */
509 * perf_cgroup_events says at least one
510 * context on this CPU has cgroup events.
512 * ctx->nr_cgroups reports the number of cgroup
513 * events for a context.
515 if (cpuctx->ctx.nr_cgroups > 0) {
516 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
517 perf_pmu_disable(cpuctx->ctx.pmu);
519 if (mode & PERF_CGROUP_SWOUT) {
520 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
522 * must not be done before ctxswout due
523 * to event_filter_match() in event_sched_out()
525 cpuctx->cgrp = NULL;
528 if (mode & PERF_CGROUP_SWIN) {
529 WARN_ON_ONCE(cpuctx->cgrp);
531 * set cgrp before ctxsw in to allow
532 * event_filter_match() to not have to pass
533 * task around
535 cpuctx->cgrp = perf_cgroup_from_task(task);
536 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
538 perf_pmu_enable(cpuctx->ctx.pmu);
539 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
543 rcu_read_unlock();
545 local_irq_restore(flags);
548 static inline void perf_cgroup_sched_out(struct task_struct *task,
549 struct task_struct *next)
551 struct perf_cgroup *cgrp1;
552 struct perf_cgroup *cgrp2 = NULL;
555 * we come here when we know perf_cgroup_events > 0
557 cgrp1 = perf_cgroup_from_task(task);
560 * next is NULL when called from perf_event_enable_on_exec()
561 * that will systematically cause a cgroup_switch()
563 if (next)
564 cgrp2 = perf_cgroup_from_task(next);
567 * only schedule out current cgroup events if we know
568 * that we are switching to a different cgroup. Otherwise,
569 * do no touch the cgroup events.
571 if (cgrp1 != cgrp2)
572 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
575 static inline void perf_cgroup_sched_in(struct task_struct *prev,
576 struct task_struct *task)
578 struct perf_cgroup *cgrp1;
579 struct perf_cgroup *cgrp2 = NULL;
582 * we come here when we know perf_cgroup_events > 0
584 cgrp1 = perf_cgroup_from_task(task);
586 /* prev can never be NULL */
587 cgrp2 = perf_cgroup_from_task(prev);
590 * only need to schedule in cgroup events if we are changing
591 * cgroup during ctxsw. Cgroup events were not scheduled
592 * out of ctxsw out if that was not the case.
594 if (cgrp1 != cgrp2)
595 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
598 static inline int perf_cgroup_connect(int fd, struct perf_event *event,
599 struct perf_event_attr *attr,
600 struct perf_event *group_leader)
602 struct perf_cgroup *cgrp;
603 struct cgroup_subsys_state *css;
604 struct fd f = fdget(fd);
605 int ret = 0;
607 if (!f.file)
608 return -EBADF;
610 css = css_tryget_from_dir(f.file->f_dentry, &perf_event_cgrp_subsys);
611 if (IS_ERR(css)) {
612 ret = PTR_ERR(css);
613 goto out;
616 cgrp = container_of(css, struct perf_cgroup, css);
617 event->cgrp = cgrp;
620 * all events in a group must monitor
621 * the same cgroup because a task belongs
622 * to only one perf cgroup at a time
624 if (group_leader && group_leader->cgrp != cgrp) {
625 perf_detach_cgroup(event);
626 ret = -EINVAL;
628 out:
629 fdput(f);
630 return ret;
633 static inline void
634 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
636 struct perf_cgroup_info *t;
637 t = per_cpu_ptr(event->cgrp->info, event->cpu);
638 event->shadow_ctx_time = now - t->timestamp;
641 static inline void
642 perf_cgroup_defer_enabled(struct perf_event *event)
645 * when the current task's perf cgroup does not match
646 * the event's, we need to remember to call the
647 * perf_mark_enable() function the first time a task with
648 * a matching perf cgroup is scheduled in.
650 if (is_cgroup_event(event) && !perf_cgroup_match(event))
651 event->cgrp_defer_enabled = 1;
654 static inline void
655 perf_cgroup_mark_enabled(struct perf_event *event,
656 struct perf_event_context *ctx)
658 struct perf_event *sub;
659 u64 tstamp = perf_event_time(event);
661 if (!event->cgrp_defer_enabled)
662 return;
664 event->cgrp_defer_enabled = 0;
666 event->tstamp_enabled = tstamp - event->total_time_enabled;
667 list_for_each_entry(sub, &event->sibling_list, group_entry) {
668 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
669 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
670 sub->cgrp_defer_enabled = 0;
674 #else /* !CONFIG_CGROUP_PERF */
676 static inline bool
677 perf_cgroup_match(struct perf_event *event)
679 return true;
682 static inline void perf_detach_cgroup(struct perf_event *event)
685 static inline int is_cgroup_event(struct perf_event *event)
687 return 0;
690 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
692 return 0;
695 static inline void update_cgrp_time_from_event(struct perf_event *event)
699 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
703 static inline void perf_cgroup_sched_out(struct task_struct *task,
704 struct task_struct *next)
708 static inline void perf_cgroup_sched_in(struct task_struct *prev,
709 struct task_struct *task)
713 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
714 struct perf_event_attr *attr,
715 struct perf_event *group_leader)
717 return -EINVAL;
720 static inline void
721 perf_cgroup_set_timestamp(struct task_struct *task,
722 struct perf_event_context *ctx)
726 void
727 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
731 static inline void
732 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
736 static inline u64 perf_cgroup_event_time(struct perf_event *event)
738 return 0;
741 static inline void
742 perf_cgroup_defer_enabled(struct perf_event *event)
746 static inline void
747 perf_cgroup_mark_enabled(struct perf_event *event,
748 struct perf_event_context *ctx)
751 #endif
754 * set default to be dependent on timer tick just
755 * like original code
757 #define PERF_CPU_HRTIMER (1000 / HZ)
759 * function must be called with interrupts disbled
761 static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
763 struct perf_cpu_context *cpuctx;
764 enum hrtimer_restart ret = HRTIMER_NORESTART;
765 int rotations = 0;
767 WARN_ON(!irqs_disabled());
769 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
771 rotations = perf_rotate_context(cpuctx);
774 * arm timer if needed
776 if (rotations) {
777 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
778 ret = HRTIMER_RESTART;
781 return ret;
784 /* CPU is going down */
785 void perf_cpu_hrtimer_cancel(int cpu)
787 struct perf_cpu_context *cpuctx;
788 struct pmu *pmu;
789 unsigned long flags;
791 if (WARN_ON(cpu != smp_processor_id()))
792 return;
794 local_irq_save(flags);
796 rcu_read_lock();
798 list_for_each_entry_rcu(pmu, &pmus, entry) {
799 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
801 if (pmu->task_ctx_nr == perf_sw_context)
802 continue;
804 hrtimer_cancel(&cpuctx->hrtimer);
807 rcu_read_unlock();
809 local_irq_restore(flags);
812 static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
814 struct hrtimer *hr = &cpuctx->hrtimer;
815 struct pmu *pmu = cpuctx->ctx.pmu;
816 int timer;
818 /* no multiplexing needed for SW PMU */
819 if (pmu->task_ctx_nr == perf_sw_context)
820 return;
823 * check default is sane, if not set then force to
824 * default interval (1/tick)
826 timer = pmu->hrtimer_interval_ms;
827 if (timer < 1)
828 timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
830 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
832 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
833 hr->function = perf_cpu_hrtimer_handler;
836 static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
838 struct hrtimer *hr = &cpuctx->hrtimer;
839 struct pmu *pmu = cpuctx->ctx.pmu;
841 /* not for SW PMU */
842 if (pmu->task_ctx_nr == perf_sw_context)
843 return;
845 if (hrtimer_active(hr))
846 return;
848 if (!hrtimer_callback_running(hr))
849 __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
850 0, HRTIMER_MODE_REL_PINNED, 0);
853 void perf_pmu_disable(struct pmu *pmu)
855 int *count = this_cpu_ptr(pmu->pmu_disable_count);
856 if (!(*count)++)
857 pmu->pmu_disable(pmu);
860 void perf_pmu_enable(struct pmu *pmu)
862 int *count = this_cpu_ptr(pmu->pmu_disable_count);
863 if (!--(*count))
864 pmu->pmu_enable(pmu);
867 static DEFINE_PER_CPU(struct list_head, rotation_list);
870 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
871 * because they're strictly cpu affine and rotate_start is called with IRQs
872 * disabled, while rotate_context is called from IRQ context.
874 static void perf_pmu_rotate_start(struct pmu *pmu)
876 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
877 struct list_head *head = &__get_cpu_var(rotation_list);
879 WARN_ON(!irqs_disabled());
881 if (list_empty(&cpuctx->rotation_list))
882 list_add(&cpuctx->rotation_list, head);
885 static void get_ctx(struct perf_event_context *ctx)
887 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
890 static void put_ctx(struct perf_event_context *ctx)
892 if (atomic_dec_and_test(&ctx->refcount)) {
893 if (ctx->parent_ctx)
894 put_ctx(ctx->parent_ctx);
895 if (ctx->task)
896 put_task_struct(ctx->task);
897 kfree_rcu(ctx, rcu_head);
901 static void unclone_ctx(struct perf_event_context *ctx)
903 if (ctx->parent_ctx) {
904 put_ctx(ctx->parent_ctx);
905 ctx->parent_ctx = NULL;
907 ctx->generation++;
910 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
913 * only top level events have the pid namespace they were created in
915 if (event->parent)
916 event = event->parent;
918 return task_tgid_nr_ns(p, event->ns);
921 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
924 * only top level events have the pid namespace they were created in
926 if (event->parent)
927 event = event->parent;
929 return task_pid_nr_ns(p, event->ns);
933 * If we inherit events we want to return the parent event id
934 * to userspace.
936 static u64 primary_event_id(struct perf_event *event)
938 u64 id = event->id;
940 if (event->parent)
941 id = event->parent->id;
943 return id;
947 * Get the perf_event_context for a task and lock it.
948 * This has to cope with with the fact that until it is locked,
949 * the context could get moved to another task.
951 static struct perf_event_context *
952 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
954 struct perf_event_context *ctx;
956 retry:
958 * One of the few rules of preemptible RCU is that one cannot do
959 * rcu_read_unlock() while holding a scheduler (or nested) lock when
960 * part of the read side critical section was preemptible -- see
961 * rcu_read_unlock_special().
963 * Since ctx->lock nests under rq->lock we must ensure the entire read
964 * side critical section is non-preemptible.
966 preempt_disable();
967 rcu_read_lock();
968 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
969 if (ctx) {
971 * If this context is a clone of another, it might
972 * get swapped for another underneath us by
973 * perf_event_task_sched_out, though the
974 * rcu_read_lock() protects us from any context
975 * getting freed. Lock the context and check if it
976 * got swapped before we could get the lock, and retry
977 * if so. If we locked the right context, then it
978 * can't get swapped on us any more.
980 raw_spin_lock_irqsave(&ctx->lock, *flags);
981 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
982 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
983 rcu_read_unlock();
984 preempt_enable();
985 goto retry;
988 if (!atomic_inc_not_zero(&ctx->refcount)) {
989 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
990 ctx = NULL;
993 rcu_read_unlock();
994 preempt_enable();
995 return ctx;
999 * Get the context for a task and increment its pin_count so it
1000 * can't get swapped to another task. This also increments its
1001 * reference count so that the context can't get freed.
1003 static struct perf_event_context *
1004 perf_pin_task_context(struct task_struct *task, int ctxn)
1006 struct perf_event_context *ctx;
1007 unsigned long flags;
1009 ctx = perf_lock_task_context(task, ctxn, &flags);
1010 if (ctx) {
1011 ++ctx->pin_count;
1012 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1014 return ctx;
1017 static void perf_unpin_context(struct perf_event_context *ctx)
1019 unsigned long flags;
1021 raw_spin_lock_irqsave(&ctx->lock, flags);
1022 --ctx->pin_count;
1023 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1027 * Update the record of the current time in a context.
1029 static void update_context_time(struct perf_event_context *ctx)
1031 u64 now = perf_clock();
1033 ctx->time += now - ctx->timestamp;
1034 ctx->timestamp = now;
1037 static u64 perf_event_time(struct perf_event *event)
1039 struct perf_event_context *ctx = event->ctx;
1041 if (is_cgroup_event(event))
1042 return perf_cgroup_event_time(event);
1044 return ctx ? ctx->time : 0;
1048 * Update the total_time_enabled and total_time_running fields for a event.
1049 * The caller of this function needs to hold the ctx->lock.
1051 static void update_event_times(struct perf_event *event)
1053 struct perf_event_context *ctx = event->ctx;
1054 u64 run_end;
1056 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1057 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1058 return;
1060 * in cgroup mode, time_enabled represents
1061 * the time the event was enabled AND active
1062 * tasks were in the monitored cgroup. This is
1063 * independent of the activity of the context as
1064 * there may be a mix of cgroup and non-cgroup events.
1066 * That is why we treat cgroup events differently
1067 * here.
1069 if (is_cgroup_event(event))
1070 run_end = perf_cgroup_event_time(event);
1071 else if (ctx->is_active)
1072 run_end = ctx->time;
1073 else
1074 run_end = event->tstamp_stopped;
1076 event->total_time_enabled = run_end - event->tstamp_enabled;
1078 if (event->state == PERF_EVENT_STATE_INACTIVE)
1079 run_end = event->tstamp_stopped;
1080 else
1081 run_end = perf_event_time(event);
1083 event->total_time_running = run_end - event->tstamp_running;
1088 * Update total_time_enabled and total_time_running for all events in a group.
1090 static void update_group_times(struct perf_event *leader)
1092 struct perf_event *event;
1094 update_event_times(leader);
1095 list_for_each_entry(event, &leader->sibling_list, group_entry)
1096 update_event_times(event);
1099 static struct list_head *
1100 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1102 if (event->attr.pinned)
1103 return &ctx->pinned_groups;
1104 else
1105 return &ctx->flexible_groups;
1109 * Add a event from the lists for its context.
1110 * Must be called with ctx->mutex and ctx->lock held.
1112 static void
1113 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1115 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1116 event->attach_state |= PERF_ATTACH_CONTEXT;
1119 * If we're a stand alone event or group leader, we go to the context
1120 * list, group events are kept attached to the group so that
1121 * perf_group_detach can, at all times, locate all siblings.
1123 if (event->group_leader == event) {
1124 struct list_head *list;
1126 if (is_software_event(event))
1127 event->group_flags |= PERF_GROUP_SOFTWARE;
1129 list = ctx_group_list(event, ctx);
1130 list_add_tail(&event->group_entry, list);
1133 if (is_cgroup_event(event))
1134 ctx->nr_cgroups++;
1136 if (has_branch_stack(event))
1137 ctx->nr_branch_stack++;
1139 list_add_rcu(&event->event_entry, &ctx->event_list);
1140 if (!ctx->nr_events)
1141 perf_pmu_rotate_start(ctx->pmu);
1142 ctx->nr_events++;
1143 if (event->attr.inherit_stat)
1144 ctx->nr_stat++;
1146 ctx->generation++;
1150 * Initialize event state based on the perf_event_attr::disabled.
1152 static inline void perf_event__state_init(struct perf_event *event)
1154 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1155 PERF_EVENT_STATE_INACTIVE;
1159 * Called at perf_event creation and when events are attached/detached from a
1160 * group.
1162 static void perf_event__read_size(struct perf_event *event)
1164 int entry = sizeof(u64); /* value */
1165 int size = 0;
1166 int nr = 1;
1168 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1169 size += sizeof(u64);
1171 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1172 size += sizeof(u64);
1174 if (event->attr.read_format & PERF_FORMAT_ID)
1175 entry += sizeof(u64);
1177 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1178 nr += event->group_leader->nr_siblings;
1179 size += sizeof(u64);
1182 size += entry * nr;
1183 event->read_size = size;
1186 static void perf_event__header_size(struct perf_event *event)
1188 struct perf_sample_data *data;
1189 u64 sample_type = event->attr.sample_type;
1190 u16 size = 0;
1192 perf_event__read_size(event);
1194 if (sample_type & PERF_SAMPLE_IP)
1195 size += sizeof(data->ip);
1197 if (sample_type & PERF_SAMPLE_ADDR)
1198 size += sizeof(data->addr);
1200 if (sample_type & PERF_SAMPLE_PERIOD)
1201 size += sizeof(data->period);
1203 if (sample_type & PERF_SAMPLE_WEIGHT)
1204 size += sizeof(data->weight);
1206 if (sample_type & PERF_SAMPLE_READ)
1207 size += event->read_size;
1209 if (sample_type & PERF_SAMPLE_DATA_SRC)
1210 size += sizeof(data->data_src.val);
1212 if (sample_type & PERF_SAMPLE_TRANSACTION)
1213 size += sizeof(data->txn);
1215 event->header_size = size;
1218 static void perf_event__id_header_size(struct perf_event *event)
1220 struct perf_sample_data *data;
1221 u64 sample_type = event->attr.sample_type;
1222 u16 size = 0;
1224 if (sample_type & PERF_SAMPLE_TID)
1225 size += sizeof(data->tid_entry);
1227 if (sample_type & PERF_SAMPLE_TIME)
1228 size += sizeof(data->time);
1230 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1231 size += sizeof(data->id);
1233 if (sample_type & PERF_SAMPLE_ID)
1234 size += sizeof(data->id);
1236 if (sample_type & PERF_SAMPLE_STREAM_ID)
1237 size += sizeof(data->stream_id);
1239 if (sample_type & PERF_SAMPLE_CPU)
1240 size += sizeof(data->cpu_entry);
1242 event->id_header_size = size;
1245 static void perf_group_attach(struct perf_event *event)
1247 struct perf_event *group_leader = event->group_leader, *pos;
1250 * We can have double attach due to group movement in perf_event_open.
1252 if (event->attach_state & PERF_ATTACH_GROUP)
1253 return;
1255 event->attach_state |= PERF_ATTACH_GROUP;
1257 if (group_leader == event)
1258 return;
1260 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1261 !is_software_event(event))
1262 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1264 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1265 group_leader->nr_siblings++;
1267 perf_event__header_size(group_leader);
1269 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1270 perf_event__header_size(pos);
1274 * Remove a event from the lists for its context.
1275 * Must be called with ctx->mutex and ctx->lock held.
1277 static void
1278 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1280 struct perf_cpu_context *cpuctx;
1282 * We can have double detach due to exit/hot-unplug + close.
1284 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
1285 return;
1287 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1289 if (is_cgroup_event(event)) {
1290 ctx->nr_cgroups--;
1291 cpuctx = __get_cpu_context(ctx);
1293 * if there are no more cgroup events
1294 * then cler cgrp to avoid stale pointer
1295 * in update_cgrp_time_from_cpuctx()
1297 if (!ctx->nr_cgroups)
1298 cpuctx->cgrp = NULL;
1301 if (has_branch_stack(event))
1302 ctx->nr_branch_stack--;
1304 ctx->nr_events--;
1305 if (event->attr.inherit_stat)
1306 ctx->nr_stat--;
1308 list_del_rcu(&event->event_entry);
1310 if (event->group_leader == event)
1311 list_del_init(&event->group_entry);
1313 update_group_times(event);
1316 * If event was in error state, then keep it
1317 * that way, otherwise bogus counts will be
1318 * returned on read(). The only way to get out
1319 * of error state is by explicit re-enabling
1320 * of the event
1322 if (event->state > PERF_EVENT_STATE_OFF)
1323 event->state = PERF_EVENT_STATE_OFF;
1325 ctx->generation++;
1328 static void perf_group_detach(struct perf_event *event)
1330 struct perf_event *sibling, *tmp;
1331 struct list_head *list = NULL;
1334 * We can have double detach due to exit/hot-unplug + close.
1336 if (!(event->attach_state & PERF_ATTACH_GROUP))
1337 return;
1339 event->attach_state &= ~PERF_ATTACH_GROUP;
1342 * If this is a sibling, remove it from its group.
1344 if (event->group_leader != event) {
1345 list_del_init(&event->group_entry);
1346 event->group_leader->nr_siblings--;
1347 goto out;
1350 if (!list_empty(&event->group_entry))
1351 list = &event->group_entry;
1354 * If this was a group event with sibling events then
1355 * upgrade the siblings to singleton events by adding them
1356 * to whatever list we are on.
1358 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
1359 if (list)
1360 list_move_tail(&sibling->group_entry, list);
1361 sibling->group_leader = sibling;
1363 /* Inherit group flags from the previous leader */
1364 sibling->group_flags = event->group_flags;
1367 out:
1368 perf_event__header_size(event->group_leader);
1370 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1371 perf_event__header_size(tmp);
1374 static inline int
1375 event_filter_match(struct perf_event *event)
1377 return (event->cpu == -1 || event->cpu == smp_processor_id())
1378 && perf_cgroup_match(event);
1381 static void
1382 event_sched_out(struct perf_event *event,
1383 struct perf_cpu_context *cpuctx,
1384 struct perf_event_context *ctx)
1386 u64 tstamp = perf_event_time(event);
1387 u64 delta;
1389 * An event which could not be activated because of
1390 * filter mismatch still needs to have its timings
1391 * maintained, otherwise bogus information is return
1392 * via read() for time_enabled, time_running:
1394 if (event->state == PERF_EVENT_STATE_INACTIVE
1395 && !event_filter_match(event)) {
1396 delta = tstamp - event->tstamp_stopped;
1397 event->tstamp_running += delta;
1398 event->tstamp_stopped = tstamp;
1401 if (event->state != PERF_EVENT_STATE_ACTIVE)
1402 return;
1404 perf_pmu_disable(event->pmu);
1406 event->state = PERF_EVENT_STATE_INACTIVE;
1407 if (event->pending_disable) {
1408 event->pending_disable = 0;
1409 event->state = PERF_EVENT_STATE_OFF;
1411 event->tstamp_stopped = tstamp;
1412 event->pmu->del(event, 0);
1413 event->oncpu = -1;
1415 if (!is_software_event(event))
1416 cpuctx->active_oncpu--;
1417 ctx->nr_active--;
1418 if (event->attr.freq && event->attr.sample_freq)
1419 ctx->nr_freq--;
1420 if (event->attr.exclusive || !cpuctx->active_oncpu)
1421 cpuctx->exclusive = 0;
1423 perf_pmu_enable(event->pmu);
1426 static void
1427 group_sched_out(struct perf_event *group_event,
1428 struct perf_cpu_context *cpuctx,
1429 struct perf_event_context *ctx)
1431 struct perf_event *event;
1432 int state = group_event->state;
1434 event_sched_out(group_event, cpuctx, ctx);
1437 * Schedule out siblings (if any):
1439 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1440 event_sched_out(event, cpuctx, ctx);
1442 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
1443 cpuctx->exclusive = 0;
1447 * Cross CPU call to remove a performance event
1449 * We disable the event on the hardware level first. After that we
1450 * remove it from the context list.
1452 static int __perf_remove_from_context(void *info)
1454 struct perf_event *event = info;
1455 struct perf_event_context *ctx = event->ctx;
1456 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1458 raw_spin_lock(&ctx->lock);
1459 event_sched_out(event, cpuctx, ctx);
1460 list_del_event(event, ctx);
1461 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1462 ctx->is_active = 0;
1463 cpuctx->task_ctx = NULL;
1465 raw_spin_unlock(&ctx->lock);
1467 return 0;
1472 * Remove the event from a task's (or a CPU's) list of events.
1474 * CPU events are removed with a smp call. For task events we only
1475 * call when the task is on a CPU.
1477 * If event->ctx is a cloned context, callers must make sure that
1478 * every task struct that event->ctx->task could possibly point to
1479 * remains valid. This is OK when called from perf_release since
1480 * that only calls us on the top-level context, which can't be a clone.
1481 * When called from perf_event_exit_task, it's OK because the
1482 * context has been detached from its task.
1484 static void perf_remove_from_context(struct perf_event *event)
1486 struct perf_event_context *ctx = event->ctx;
1487 struct task_struct *task = ctx->task;
1489 lockdep_assert_held(&ctx->mutex);
1491 if (!task) {
1493 * Per cpu events are removed via an smp call and
1494 * the removal is always successful.
1496 cpu_function_call(event->cpu, __perf_remove_from_context, event);
1497 return;
1500 retry:
1501 if (!task_function_call(task, __perf_remove_from_context, event))
1502 return;
1504 raw_spin_lock_irq(&ctx->lock);
1506 * If we failed to find a running task, but find the context active now
1507 * that we've acquired the ctx->lock, retry.
1509 if (ctx->is_active) {
1510 raw_spin_unlock_irq(&ctx->lock);
1511 goto retry;
1515 * Since the task isn't running, its safe to remove the event, us
1516 * holding the ctx->lock ensures the task won't get scheduled in.
1518 list_del_event(event, ctx);
1519 raw_spin_unlock_irq(&ctx->lock);
1523 * Cross CPU call to disable a performance event
1525 int __perf_event_disable(void *info)
1527 struct perf_event *event = info;
1528 struct perf_event_context *ctx = event->ctx;
1529 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1532 * If this is a per-task event, need to check whether this
1533 * event's task is the current task on this cpu.
1535 * Can trigger due to concurrent perf_event_context_sched_out()
1536 * flipping contexts around.
1538 if (ctx->task && cpuctx->task_ctx != ctx)
1539 return -EINVAL;
1541 raw_spin_lock(&ctx->lock);
1544 * If the event is on, turn it off.
1545 * If it is in error state, leave it in error state.
1547 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1548 update_context_time(ctx);
1549 update_cgrp_time_from_event(event);
1550 update_group_times(event);
1551 if (event == event->group_leader)
1552 group_sched_out(event, cpuctx, ctx);
1553 else
1554 event_sched_out(event, cpuctx, ctx);
1555 event->state = PERF_EVENT_STATE_OFF;
1558 raw_spin_unlock(&ctx->lock);
1560 return 0;
1564 * Disable a event.
1566 * If event->ctx is a cloned context, callers must make sure that
1567 * every task struct that event->ctx->task could possibly point to
1568 * remains valid. This condition is satisifed when called through
1569 * perf_event_for_each_child or perf_event_for_each because they
1570 * hold the top-level event's child_mutex, so any descendant that
1571 * goes to exit will block in sync_child_event.
1572 * When called from perf_pending_event it's OK because event->ctx
1573 * is the current context on this CPU and preemption is disabled,
1574 * hence we can't get into perf_event_task_sched_out for this context.
1576 void perf_event_disable(struct perf_event *event)
1578 struct perf_event_context *ctx = event->ctx;
1579 struct task_struct *task = ctx->task;
1581 if (!task) {
1583 * Disable the event on the cpu that it's on
1585 cpu_function_call(event->cpu, __perf_event_disable, event);
1586 return;
1589 retry:
1590 if (!task_function_call(task, __perf_event_disable, event))
1591 return;
1593 raw_spin_lock_irq(&ctx->lock);
1595 * If the event is still active, we need to retry the cross-call.
1597 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1598 raw_spin_unlock_irq(&ctx->lock);
1600 * Reload the task pointer, it might have been changed by
1601 * a concurrent perf_event_context_sched_out().
1603 task = ctx->task;
1604 goto retry;
1608 * Since we have the lock this context can't be scheduled
1609 * in, so we can change the state safely.
1611 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1612 update_group_times(event);
1613 event->state = PERF_EVENT_STATE_OFF;
1615 raw_spin_unlock_irq(&ctx->lock);
1617 EXPORT_SYMBOL_GPL(perf_event_disable);
1619 static void perf_set_shadow_time(struct perf_event *event,
1620 struct perf_event_context *ctx,
1621 u64 tstamp)
1624 * use the correct time source for the time snapshot
1626 * We could get by without this by leveraging the
1627 * fact that to get to this function, the caller
1628 * has most likely already called update_context_time()
1629 * and update_cgrp_time_xx() and thus both timestamp
1630 * are identical (or very close). Given that tstamp is,
1631 * already adjusted for cgroup, we could say that:
1632 * tstamp - ctx->timestamp
1633 * is equivalent to
1634 * tstamp - cgrp->timestamp.
1636 * Then, in perf_output_read(), the calculation would
1637 * work with no changes because:
1638 * - event is guaranteed scheduled in
1639 * - no scheduled out in between
1640 * - thus the timestamp would be the same
1642 * But this is a bit hairy.
1644 * So instead, we have an explicit cgroup call to remain
1645 * within the time time source all along. We believe it
1646 * is cleaner and simpler to understand.
1648 if (is_cgroup_event(event))
1649 perf_cgroup_set_shadow_time(event, tstamp);
1650 else
1651 event->shadow_ctx_time = tstamp - ctx->timestamp;
1654 #define MAX_INTERRUPTS (~0ULL)
1656 static void perf_log_throttle(struct perf_event *event, int enable);
1658 static int
1659 event_sched_in(struct perf_event *event,
1660 struct perf_cpu_context *cpuctx,
1661 struct perf_event_context *ctx)
1663 u64 tstamp = perf_event_time(event);
1664 int ret = 0;
1666 if (event->state <= PERF_EVENT_STATE_OFF)
1667 return 0;
1669 event->state = PERF_EVENT_STATE_ACTIVE;
1670 event->oncpu = smp_processor_id();
1673 * Unthrottle events, since we scheduled we might have missed several
1674 * ticks already, also for a heavily scheduling task there is little
1675 * guarantee it'll get a tick in a timely manner.
1677 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1678 perf_log_throttle(event, 1);
1679 event->hw.interrupts = 0;
1683 * The new state must be visible before we turn it on in the hardware:
1685 smp_wmb();
1687 perf_pmu_disable(event->pmu);
1689 if (event->pmu->add(event, PERF_EF_START)) {
1690 event->state = PERF_EVENT_STATE_INACTIVE;
1691 event->oncpu = -1;
1692 ret = -EAGAIN;
1693 goto out;
1696 event->tstamp_running += tstamp - event->tstamp_stopped;
1698 perf_set_shadow_time(event, ctx, tstamp);
1700 if (!is_software_event(event))
1701 cpuctx->active_oncpu++;
1702 ctx->nr_active++;
1703 if (event->attr.freq && event->attr.sample_freq)
1704 ctx->nr_freq++;
1706 if (event->attr.exclusive)
1707 cpuctx->exclusive = 1;
1709 out:
1710 perf_pmu_enable(event->pmu);
1712 return ret;
1715 static int
1716 group_sched_in(struct perf_event *group_event,
1717 struct perf_cpu_context *cpuctx,
1718 struct perf_event_context *ctx)
1720 struct perf_event *event, *partial_group = NULL;
1721 struct pmu *pmu = ctx->pmu;
1722 u64 now = ctx->time;
1723 bool simulate = false;
1725 if (group_event->state == PERF_EVENT_STATE_OFF)
1726 return 0;
1728 pmu->start_txn(pmu);
1730 if (event_sched_in(group_event, cpuctx, ctx)) {
1731 pmu->cancel_txn(pmu);
1732 perf_cpu_hrtimer_restart(cpuctx);
1733 return -EAGAIN;
1737 * Schedule in siblings as one group (if any):
1739 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1740 if (event_sched_in(event, cpuctx, ctx)) {
1741 partial_group = event;
1742 goto group_error;
1746 if (!pmu->commit_txn(pmu))
1747 return 0;
1749 group_error:
1751 * Groups can be scheduled in as one unit only, so undo any
1752 * partial group before returning:
1753 * The events up to the failed event are scheduled out normally,
1754 * tstamp_stopped will be updated.
1756 * The failed events and the remaining siblings need to have
1757 * their timings updated as if they had gone thru event_sched_in()
1758 * and event_sched_out(). This is required to get consistent timings
1759 * across the group. This also takes care of the case where the group
1760 * could never be scheduled by ensuring tstamp_stopped is set to mark
1761 * the time the event was actually stopped, such that time delta
1762 * calculation in update_event_times() is correct.
1764 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1765 if (event == partial_group)
1766 simulate = true;
1768 if (simulate) {
1769 event->tstamp_running += now - event->tstamp_stopped;
1770 event->tstamp_stopped = now;
1771 } else {
1772 event_sched_out(event, cpuctx, ctx);
1775 event_sched_out(group_event, cpuctx, ctx);
1777 pmu->cancel_txn(pmu);
1779 perf_cpu_hrtimer_restart(cpuctx);
1781 return -EAGAIN;
1785 * Work out whether we can put this event group on the CPU now.
1787 static int group_can_go_on(struct perf_event *event,
1788 struct perf_cpu_context *cpuctx,
1789 int can_add_hw)
1792 * Groups consisting entirely of software events can always go on.
1794 if (event->group_flags & PERF_GROUP_SOFTWARE)
1795 return 1;
1797 * If an exclusive group is already on, no other hardware
1798 * events can go on.
1800 if (cpuctx->exclusive)
1801 return 0;
1803 * If this group is exclusive and there are already
1804 * events on the CPU, it can't go on.
1806 if (event->attr.exclusive && cpuctx->active_oncpu)
1807 return 0;
1809 * Otherwise, try to add it if all previous groups were able
1810 * to go on.
1812 return can_add_hw;
1815 static void add_event_to_ctx(struct perf_event *event,
1816 struct perf_event_context *ctx)
1818 u64 tstamp = perf_event_time(event);
1820 list_add_event(event, ctx);
1821 perf_group_attach(event);
1822 event->tstamp_enabled = tstamp;
1823 event->tstamp_running = tstamp;
1824 event->tstamp_stopped = tstamp;
1827 static void task_ctx_sched_out(struct perf_event_context *ctx);
1828 static void
1829 ctx_sched_in(struct perf_event_context *ctx,
1830 struct perf_cpu_context *cpuctx,
1831 enum event_type_t event_type,
1832 struct task_struct *task);
1834 static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1835 struct perf_event_context *ctx,
1836 struct task_struct *task)
1838 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1839 if (ctx)
1840 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1841 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1842 if (ctx)
1843 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1847 * Cross CPU call to install and enable a performance event
1849 * Must be called with ctx->mutex held
1851 static int __perf_install_in_context(void *info)
1853 struct perf_event *event = info;
1854 struct perf_event_context *ctx = event->ctx;
1855 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1856 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1857 struct task_struct *task = current;
1859 perf_ctx_lock(cpuctx, task_ctx);
1860 perf_pmu_disable(cpuctx->ctx.pmu);
1863 * If there was an active task_ctx schedule it out.
1865 if (task_ctx)
1866 task_ctx_sched_out(task_ctx);
1869 * If the context we're installing events in is not the
1870 * active task_ctx, flip them.
1872 if (ctx->task && task_ctx != ctx) {
1873 if (task_ctx)
1874 raw_spin_unlock(&task_ctx->lock);
1875 raw_spin_lock(&ctx->lock);
1876 task_ctx = ctx;
1879 if (task_ctx) {
1880 cpuctx->task_ctx = task_ctx;
1881 task = task_ctx->task;
1884 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
1886 update_context_time(ctx);
1888 * update cgrp time only if current cgrp
1889 * matches event->cgrp. Must be done before
1890 * calling add_event_to_ctx()
1892 update_cgrp_time_from_event(event);
1894 add_event_to_ctx(event, ctx);
1897 * Schedule everything back in
1899 perf_event_sched_in(cpuctx, task_ctx, task);
1901 perf_pmu_enable(cpuctx->ctx.pmu);
1902 perf_ctx_unlock(cpuctx, task_ctx);
1904 return 0;
1908 * Attach a performance event to a context
1910 * First we add the event to the list with the hardware enable bit
1911 * in event->hw_config cleared.
1913 * If the event is attached to a task which is on a CPU we use a smp
1914 * call to enable it in the task context. The task might have been
1915 * scheduled away, but we check this in the smp call again.
1917 static void
1918 perf_install_in_context(struct perf_event_context *ctx,
1919 struct perf_event *event,
1920 int cpu)
1922 struct task_struct *task = ctx->task;
1924 lockdep_assert_held(&ctx->mutex);
1926 event->ctx = ctx;
1927 if (event->cpu != -1)
1928 event->cpu = cpu;
1930 if (!task) {
1932 * Per cpu events are installed via an smp call and
1933 * the install is always successful.
1935 cpu_function_call(cpu, __perf_install_in_context, event);
1936 return;
1939 retry:
1940 if (!task_function_call(task, __perf_install_in_context, event))
1941 return;
1943 raw_spin_lock_irq(&ctx->lock);
1945 * If we failed to find a running task, but find the context active now
1946 * that we've acquired the ctx->lock, retry.
1948 if (ctx->is_active) {
1949 raw_spin_unlock_irq(&ctx->lock);
1950 goto retry;
1954 * Since the task isn't running, its safe to add the event, us holding
1955 * the ctx->lock ensures the task won't get scheduled in.
1957 add_event_to_ctx(event, ctx);
1958 raw_spin_unlock_irq(&ctx->lock);
1962 * Put a event into inactive state and update time fields.
1963 * Enabling the leader of a group effectively enables all
1964 * the group members that aren't explicitly disabled, so we
1965 * have to update their ->tstamp_enabled also.
1966 * Note: this works for group members as well as group leaders
1967 * since the non-leader members' sibling_lists will be empty.
1969 static void __perf_event_mark_enabled(struct perf_event *event)
1971 struct perf_event *sub;
1972 u64 tstamp = perf_event_time(event);
1974 event->state = PERF_EVENT_STATE_INACTIVE;
1975 event->tstamp_enabled = tstamp - event->total_time_enabled;
1976 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1977 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1978 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
1983 * Cross CPU call to enable a performance event
1985 static int __perf_event_enable(void *info)
1987 struct perf_event *event = info;
1988 struct perf_event_context *ctx = event->ctx;
1989 struct perf_event *leader = event->group_leader;
1990 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1991 int err;
1994 * There's a time window between 'ctx->is_active' check
1995 * in perf_event_enable function and this place having:
1996 * - IRQs on
1997 * - ctx->lock unlocked
1999 * where the task could be killed and 'ctx' deactivated
2000 * by perf_event_exit_task.
2002 if (!ctx->is_active)
2003 return -EINVAL;
2005 raw_spin_lock(&ctx->lock);
2006 update_context_time(ctx);
2008 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2009 goto unlock;
2012 * set current task's cgroup time reference point
2014 perf_cgroup_set_timestamp(current, ctx);
2016 __perf_event_mark_enabled(event);
2018 if (!event_filter_match(event)) {
2019 if (is_cgroup_event(event))
2020 perf_cgroup_defer_enabled(event);
2021 goto unlock;
2025 * If the event is in a group and isn't the group leader,
2026 * then don't put it on unless the group is on.
2028 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
2029 goto unlock;
2031 if (!group_can_go_on(event, cpuctx, 1)) {
2032 err = -EEXIST;
2033 } else {
2034 if (event == leader)
2035 err = group_sched_in(event, cpuctx, ctx);
2036 else
2037 err = event_sched_in(event, cpuctx, ctx);
2040 if (err) {
2042 * If this event can't go on and it's part of a
2043 * group, then the whole group has to come off.
2045 if (leader != event) {
2046 group_sched_out(leader, cpuctx, ctx);
2047 perf_cpu_hrtimer_restart(cpuctx);
2049 if (leader->attr.pinned) {
2050 update_group_times(leader);
2051 leader->state = PERF_EVENT_STATE_ERROR;
2055 unlock:
2056 raw_spin_unlock(&ctx->lock);
2058 return 0;
2062 * Enable a event.
2064 * If event->ctx is a cloned context, callers must make sure that
2065 * every task struct that event->ctx->task could possibly point to
2066 * remains valid. This condition is satisfied when called through
2067 * perf_event_for_each_child or perf_event_for_each as described
2068 * for perf_event_disable.
2070 void perf_event_enable(struct perf_event *event)
2072 struct perf_event_context *ctx = event->ctx;
2073 struct task_struct *task = ctx->task;
2075 if (!task) {
2077 * Enable the event on the cpu that it's on
2079 cpu_function_call(event->cpu, __perf_event_enable, event);
2080 return;
2083 raw_spin_lock_irq(&ctx->lock);
2084 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2085 goto out;
2088 * If the event is in error state, clear that first.
2089 * That way, if we see the event in error state below, we
2090 * know that it has gone back into error state, as distinct
2091 * from the task having been scheduled away before the
2092 * cross-call arrived.
2094 if (event->state == PERF_EVENT_STATE_ERROR)
2095 event->state = PERF_EVENT_STATE_OFF;
2097 retry:
2098 if (!ctx->is_active) {
2099 __perf_event_mark_enabled(event);
2100 goto out;
2103 raw_spin_unlock_irq(&ctx->lock);
2105 if (!task_function_call(task, __perf_event_enable, event))
2106 return;
2108 raw_spin_lock_irq(&ctx->lock);
2111 * If the context is active and the event is still off,
2112 * we need to retry the cross-call.
2114 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
2116 * task could have been flipped by a concurrent
2117 * perf_event_context_sched_out()
2119 task = ctx->task;
2120 goto retry;
2123 out:
2124 raw_spin_unlock_irq(&ctx->lock);
2126 EXPORT_SYMBOL_GPL(perf_event_enable);
2128 int perf_event_refresh(struct perf_event *event, int refresh)
2131 * not supported on inherited events
2133 if (event->attr.inherit || !is_sampling_event(event))
2134 return -EINVAL;
2136 atomic_add(refresh, &event->event_limit);
2137 perf_event_enable(event);
2139 return 0;
2141 EXPORT_SYMBOL_GPL(perf_event_refresh);
2143 static void ctx_sched_out(struct perf_event_context *ctx,
2144 struct perf_cpu_context *cpuctx,
2145 enum event_type_t event_type)
2147 struct perf_event *event;
2148 int is_active = ctx->is_active;
2150 ctx->is_active &= ~event_type;
2151 if (likely(!ctx->nr_events))
2152 return;
2154 update_context_time(ctx);
2155 update_cgrp_time_from_cpuctx(cpuctx);
2156 if (!ctx->nr_active)
2157 return;
2159 perf_pmu_disable(ctx->pmu);
2160 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
2161 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2162 group_sched_out(event, cpuctx, ctx);
2165 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
2166 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
2167 group_sched_out(event, cpuctx, ctx);
2169 perf_pmu_enable(ctx->pmu);
2173 * Test whether two contexts are equivalent, i.e. whether they have both been
2174 * cloned from the same version of the same context.
2176 * Equivalence is measured using a generation number in the context that is
2177 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2178 * and list_del_event().
2180 static int context_equiv(struct perf_event_context *ctx1,
2181 struct perf_event_context *ctx2)
2183 /* Pinning disables the swap optimization */
2184 if (ctx1->pin_count || ctx2->pin_count)
2185 return 0;
2187 /* If ctx1 is the parent of ctx2 */
2188 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2189 return 1;
2191 /* If ctx2 is the parent of ctx1 */
2192 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2193 return 1;
2196 * If ctx1 and ctx2 have the same parent; we flatten the parent
2197 * hierarchy, see perf_event_init_context().
2199 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2200 ctx1->parent_gen == ctx2->parent_gen)
2201 return 1;
2203 /* Unmatched */
2204 return 0;
2207 static void __perf_event_sync_stat(struct perf_event *event,
2208 struct perf_event *next_event)
2210 u64 value;
2212 if (!event->attr.inherit_stat)
2213 return;
2216 * Update the event value, we cannot use perf_event_read()
2217 * because we're in the middle of a context switch and have IRQs
2218 * disabled, which upsets smp_call_function_single(), however
2219 * we know the event must be on the current CPU, therefore we
2220 * don't need to use it.
2222 switch (event->state) {
2223 case PERF_EVENT_STATE_ACTIVE:
2224 event->pmu->read(event);
2225 /* fall-through */
2227 case PERF_EVENT_STATE_INACTIVE:
2228 update_event_times(event);
2229 break;
2231 default:
2232 break;
2236 * In order to keep per-task stats reliable we need to flip the event
2237 * values when we flip the contexts.
2239 value = local64_read(&next_event->count);
2240 value = local64_xchg(&event->count, value);
2241 local64_set(&next_event->count, value);
2243 swap(event->total_time_enabled, next_event->total_time_enabled);
2244 swap(event->total_time_running, next_event->total_time_running);
2247 * Since we swizzled the values, update the user visible data too.
2249 perf_event_update_userpage(event);
2250 perf_event_update_userpage(next_event);
2253 static void perf_event_sync_stat(struct perf_event_context *ctx,
2254 struct perf_event_context *next_ctx)
2256 struct perf_event *event, *next_event;
2258 if (!ctx->nr_stat)
2259 return;
2261 update_context_time(ctx);
2263 event = list_first_entry(&ctx->event_list,
2264 struct perf_event, event_entry);
2266 next_event = list_first_entry(&next_ctx->event_list,
2267 struct perf_event, event_entry);
2269 while (&event->event_entry != &ctx->event_list &&
2270 &next_event->event_entry != &next_ctx->event_list) {
2272 __perf_event_sync_stat(event, next_event);
2274 event = list_next_entry(event, event_entry);
2275 next_event = list_next_entry(next_event, event_entry);
2279 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2280 struct task_struct *next)
2282 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
2283 struct perf_event_context *next_ctx;
2284 struct perf_event_context *parent, *next_parent;
2285 struct perf_cpu_context *cpuctx;
2286 int do_switch = 1;
2288 if (likely(!ctx))
2289 return;
2291 cpuctx = __get_cpu_context(ctx);
2292 if (!cpuctx->task_ctx)
2293 return;
2295 rcu_read_lock();
2296 next_ctx = next->perf_event_ctxp[ctxn];
2297 if (!next_ctx)
2298 goto unlock;
2300 parent = rcu_dereference(ctx->parent_ctx);
2301 next_parent = rcu_dereference(next_ctx->parent_ctx);
2303 /* If neither context have a parent context; they cannot be clones. */
2304 if (!parent && !next_parent)
2305 goto unlock;
2307 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
2309 * Looks like the two contexts are clones, so we might be
2310 * able to optimize the context switch. We lock both
2311 * contexts and check that they are clones under the
2312 * lock (including re-checking that neither has been
2313 * uncloned in the meantime). It doesn't matter which
2314 * order we take the locks because no other cpu could
2315 * be trying to lock both of these tasks.
2317 raw_spin_lock(&ctx->lock);
2318 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
2319 if (context_equiv(ctx, next_ctx)) {
2321 * XXX do we need a memory barrier of sorts
2322 * wrt to rcu_dereference() of perf_event_ctxp
2324 task->perf_event_ctxp[ctxn] = next_ctx;
2325 next->perf_event_ctxp[ctxn] = ctx;
2326 ctx->task = next;
2327 next_ctx->task = task;
2328 do_switch = 0;
2330 perf_event_sync_stat(ctx, next_ctx);
2332 raw_spin_unlock(&next_ctx->lock);
2333 raw_spin_unlock(&ctx->lock);
2335 unlock:
2336 rcu_read_unlock();
2338 if (do_switch) {
2339 raw_spin_lock(&ctx->lock);
2340 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2341 cpuctx->task_ctx = NULL;
2342 raw_spin_unlock(&ctx->lock);
2346 #define for_each_task_context_nr(ctxn) \
2347 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2350 * Called from scheduler to remove the events of the current task,
2351 * with interrupts disabled.
2353 * We stop each event and update the event value in event->count.
2355 * This does not protect us against NMI, but disable()
2356 * sets the disabled bit in the control field of event _before_
2357 * accessing the event control register. If a NMI hits, then it will
2358 * not restart the event.
2360 void __perf_event_task_sched_out(struct task_struct *task,
2361 struct task_struct *next)
2363 int ctxn;
2365 for_each_task_context_nr(ctxn)
2366 perf_event_context_sched_out(task, ctxn, next);
2369 * if cgroup events exist on this CPU, then we need
2370 * to check if we have to switch out PMU state.
2371 * cgroup event are system-wide mode only
2373 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2374 perf_cgroup_sched_out(task, next);
2377 static void task_ctx_sched_out(struct perf_event_context *ctx)
2379 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2381 if (!cpuctx->task_ctx)
2382 return;
2384 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2385 return;
2387 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2388 cpuctx->task_ctx = NULL;
2392 * Called with IRQs disabled
2394 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2395 enum event_type_t event_type)
2397 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
2400 static void
2401 ctx_pinned_sched_in(struct perf_event_context *ctx,
2402 struct perf_cpu_context *cpuctx)
2404 struct perf_event *event;
2406 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2407 if (event->state <= PERF_EVENT_STATE_OFF)
2408 continue;
2409 if (!event_filter_match(event))
2410 continue;
2412 /* may need to reset tstamp_enabled */
2413 if (is_cgroup_event(event))
2414 perf_cgroup_mark_enabled(event, ctx);
2416 if (group_can_go_on(event, cpuctx, 1))
2417 group_sched_in(event, cpuctx, ctx);
2420 * If this pinned group hasn't been scheduled,
2421 * put it in error state.
2423 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2424 update_group_times(event);
2425 event->state = PERF_EVENT_STATE_ERROR;
2430 static void
2431 ctx_flexible_sched_in(struct perf_event_context *ctx,
2432 struct perf_cpu_context *cpuctx)
2434 struct perf_event *event;
2435 int can_add_hw = 1;
2437 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2438 /* Ignore events in OFF or ERROR state */
2439 if (event->state <= PERF_EVENT_STATE_OFF)
2440 continue;
2442 * Listen to the 'cpu' scheduling filter constraint
2443 * of events:
2445 if (!event_filter_match(event))
2446 continue;
2448 /* may need to reset tstamp_enabled */
2449 if (is_cgroup_event(event))
2450 perf_cgroup_mark_enabled(event, ctx);
2452 if (group_can_go_on(event, cpuctx, can_add_hw)) {
2453 if (group_sched_in(event, cpuctx, ctx))
2454 can_add_hw = 0;
2459 static void
2460 ctx_sched_in(struct perf_event_context *ctx,
2461 struct perf_cpu_context *cpuctx,
2462 enum event_type_t event_type,
2463 struct task_struct *task)
2465 u64 now;
2466 int is_active = ctx->is_active;
2468 ctx->is_active |= event_type;
2469 if (likely(!ctx->nr_events))
2470 return;
2472 now = perf_clock();
2473 ctx->timestamp = now;
2474 perf_cgroup_set_timestamp(task, ctx);
2476 * First go through the list and put on any pinned groups
2477 * in order to give them the best chance of going on.
2479 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
2480 ctx_pinned_sched_in(ctx, cpuctx);
2482 /* Then walk through the lower prio flexible groups */
2483 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
2484 ctx_flexible_sched_in(ctx, cpuctx);
2487 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
2488 enum event_type_t event_type,
2489 struct task_struct *task)
2491 struct perf_event_context *ctx = &cpuctx->ctx;
2493 ctx_sched_in(ctx, cpuctx, event_type, task);
2496 static void perf_event_context_sched_in(struct perf_event_context *ctx,
2497 struct task_struct *task)
2499 struct perf_cpu_context *cpuctx;
2501 cpuctx = __get_cpu_context(ctx);
2502 if (cpuctx->task_ctx == ctx)
2503 return;
2505 perf_ctx_lock(cpuctx, ctx);
2506 perf_pmu_disable(ctx->pmu);
2508 * We want to keep the following priority order:
2509 * cpu pinned (that don't need to move), task pinned,
2510 * cpu flexible, task flexible.
2512 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2514 if (ctx->nr_events)
2515 cpuctx->task_ctx = ctx;
2517 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2519 perf_pmu_enable(ctx->pmu);
2520 perf_ctx_unlock(cpuctx, ctx);
2523 * Since these rotations are per-cpu, we need to ensure the
2524 * cpu-context we got scheduled on is actually rotating.
2526 perf_pmu_rotate_start(ctx->pmu);
2530 * When sampling the branck stack in system-wide, it may be necessary
2531 * to flush the stack on context switch. This happens when the branch
2532 * stack does not tag its entries with the pid of the current task.
2533 * Otherwise it becomes impossible to associate a branch entry with a
2534 * task. This ambiguity is more likely to appear when the branch stack
2535 * supports priv level filtering and the user sets it to monitor only
2536 * at the user level (which could be a useful measurement in system-wide
2537 * mode). In that case, the risk is high of having a branch stack with
2538 * branch from multiple tasks. Flushing may mean dropping the existing
2539 * entries or stashing them somewhere in the PMU specific code layer.
2541 * This function provides the context switch callback to the lower code
2542 * layer. It is invoked ONLY when there is at least one system-wide context
2543 * with at least one active event using taken branch sampling.
2545 static void perf_branch_stack_sched_in(struct task_struct *prev,
2546 struct task_struct *task)
2548 struct perf_cpu_context *cpuctx;
2549 struct pmu *pmu;
2550 unsigned long flags;
2552 /* no need to flush branch stack if not changing task */
2553 if (prev == task)
2554 return;
2556 local_irq_save(flags);
2558 rcu_read_lock();
2560 list_for_each_entry_rcu(pmu, &pmus, entry) {
2561 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2564 * check if the context has at least one
2565 * event using PERF_SAMPLE_BRANCH_STACK
2567 if (cpuctx->ctx.nr_branch_stack > 0
2568 && pmu->flush_branch_stack) {
2570 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2572 perf_pmu_disable(pmu);
2574 pmu->flush_branch_stack();
2576 perf_pmu_enable(pmu);
2578 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2582 rcu_read_unlock();
2584 local_irq_restore(flags);
2588 * Called from scheduler to add the events of the current task
2589 * with interrupts disabled.
2591 * We restore the event value and then enable it.
2593 * This does not protect us against NMI, but enable()
2594 * sets the enabled bit in the control field of event _before_
2595 * accessing the event control register. If a NMI hits, then it will
2596 * keep the event running.
2598 void __perf_event_task_sched_in(struct task_struct *prev,
2599 struct task_struct *task)
2601 struct perf_event_context *ctx;
2602 int ctxn;
2604 for_each_task_context_nr(ctxn) {
2605 ctx = task->perf_event_ctxp[ctxn];
2606 if (likely(!ctx))
2607 continue;
2609 perf_event_context_sched_in(ctx, task);
2612 * if cgroup events exist on this CPU, then we need
2613 * to check if we have to switch in PMU state.
2614 * cgroup event are system-wide mode only
2616 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2617 perf_cgroup_sched_in(prev, task);
2619 /* check for system-wide branch_stack events */
2620 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2621 perf_branch_stack_sched_in(prev, task);
2624 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2626 u64 frequency = event->attr.sample_freq;
2627 u64 sec = NSEC_PER_SEC;
2628 u64 divisor, dividend;
2630 int count_fls, nsec_fls, frequency_fls, sec_fls;
2632 count_fls = fls64(count);
2633 nsec_fls = fls64(nsec);
2634 frequency_fls = fls64(frequency);
2635 sec_fls = 30;
2638 * We got @count in @nsec, with a target of sample_freq HZ
2639 * the target period becomes:
2641 * @count * 10^9
2642 * period = -------------------
2643 * @nsec * sample_freq
2648 * Reduce accuracy by one bit such that @a and @b converge
2649 * to a similar magnitude.
2651 #define REDUCE_FLS(a, b) \
2652 do { \
2653 if (a##_fls > b##_fls) { \
2654 a >>= 1; \
2655 a##_fls--; \
2656 } else { \
2657 b >>= 1; \
2658 b##_fls--; \
2660 } while (0)
2663 * Reduce accuracy until either term fits in a u64, then proceed with
2664 * the other, so that finally we can do a u64/u64 division.
2666 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2667 REDUCE_FLS(nsec, frequency);
2668 REDUCE_FLS(sec, count);
2671 if (count_fls + sec_fls > 64) {
2672 divisor = nsec * frequency;
2674 while (count_fls + sec_fls > 64) {
2675 REDUCE_FLS(count, sec);
2676 divisor >>= 1;
2679 dividend = count * sec;
2680 } else {
2681 dividend = count * sec;
2683 while (nsec_fls + frequency_fls > 64) {
2684 REDUCE_FLS(nsec, frequency);
2685 dividend >>= 1;
2688 divisor = nsec * frequency;
2691 if (!divisor)
2692 return dividend;
2694 return div64_u64(dividend, divisor);
2697 static DEFINE_PER_CPU(int, perf_throttled_count);
2698 static DEFINE_PER_CPU(u64, perf_throttled_seq);
2700 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
2702 struct hw_perf_event *hwc = &event->hw;
2703 s64 period, sample_period;
2704 s64 delta;
2706 period = perf_calculate_period(event, nsec, count);
2708 delta = (s64)(period - hwc->sample_period);
2709 delta = (delta + 7) / 8; /* low pass filter */
2711 sample_period = hwc->sample_period + delta;
2713 if (!sample_period)
2714 sample_period = 1;
2716 hwc->sample_period = sample_period;
2718 if (local64_read(&hwc->period_left) > 8*sample_period) {
2719 if (disable)
2720 event->pmu->stop(event, PERF_EF_UPDATE);
2722 local64_set(&hwc->period_left, 0);
2724 if (disable)
2725 event->pmu->start(event, PERF_EF_RELOAD);
2730 * combine freq adjustment with unthrottling to avoid two passes over the
2731 * events. At the same time, make sure, having freq events does not change
2732 * the rate of unthrottling as that would introduce bias.
2734 static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2735 int needs_unthr)
2737 struct perf_event *event;
2738 struct hw_perf_event *hwc;
2739 u64 now, period = TICK_NSEC;
2740 s64 delta;
2743 * only need to iterate over all events iff:
2744 * - context have events in frequency mode (needs freq adjust)
2745 * - there are events to unthrottle on this cpu
2747 if (!(ctx->nr_freq || needs_unthr))
2748 return;
2750 raw_spin_lock(&ctx->lock);
2751 perf_pmu_disable(ctx->pmu);
2753 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
2754 if (event->state != PERF_EVENT_STATE_ACTIVE)
2755 continue;
2757 if (!event_filter_match(event))
2758 continue;
2760 perf_pmu_disable(event->pmu);
2762 hwc = &event->hw;
2764 if (hwc->interrupts == MAX_INTERRUPTS) {
2765 hwc->interrupts = 0;
2766 perf_log_throttle(event, 1);
2767 event->pmu->start(event, 0);
2770 if (!event->attr.freq || !event->attr.sample_freq)
2771 goto next;
2774 * stop the event and update event->count
2776 event->pmu->stop(event, PERF_EF_UPDATE);
2778 now = local64_read(&event->count);
2779 delta = now - hwc->freq_count_stamp;
2780 hwc->freq_count_stamp = now;
2783 * restart the event
2784 * reload only if value has changed
2785 * we have stopped the event so tell that
2786 * to perf_adjust_period() to avoid stopping it
2787 * twice.
2789 if (delta > 0)
2790 perf_adjust_period(event, period, delta, false);
2792 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
2793 next:
2794 perf_pmu_enable(event->pmu);
2797 perf_pmu_enable(ctx->pmu);
2798 raw_spin_unlock(&ctx->lock);
2802 * Round-robin a context's events:
2804 static void rotate_ctx(struct perf_event_context *ctx)
2807 * Rotate the first entry last of non-pinned groups. Rotation might be
2808 * disabled by the inheritance code.
2810 if (!ctx->rotate_disable)
2811 list_rotate_left(&ctx->flexible_groups);
2815 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2816 * because they're strictly cpu affine and rotate_start is called with IRQs
2817 * disabled, while rotate_context is called from IRQ context.
2819 static int perf_rotate_context(struct perf_cpu_context *cpuctx)
2821 struct perf_event_context *ctx = NULL;
2822 int rotate = 0, remove = 1;
2824 if (cpuctx->ctx.nr_events) {
2825 remove = 0;
2826 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2827 rotate = 1;
2830 ctx = cpuctx->task_ctx;
2831 if (ctx && ctx->nr_events) {
2832 remove = 0;
2833 if (ctx->nr_events != ctx->nr_active)
2834 rotate = 1;
2837 if (!rotate)
2838 goto done;
2840 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2841 perf_pmu_disable(cpuctx->ctx.pmu);
2843 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2844 if (ctx)
2845 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
2847 rotate_ctx(&cpuctx->ctx);
2848 if (ctx)
2849 rotate_ctx(ctx);
2851 perf_event_sched_in(cpuctx, ctx, current);
2853 perf_pmu_enable(cpuctx->ctx.pmu);
2854 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2855 done:
2856 if (remove)
2857 list_del_init(&cpuctx->rotation_list);
2859 return rotate;
2862 #ifdef CONFIG_NO_HZ_FULL
2863 bool perf_event_can_stop_tick(void)
2865 if (atomic_read(&nr_freq_events) ||
2866 __this_cpu_read(perf_throttled_count))
2867 return false;
2868 else
2869 return true;
2871 #endif
2873 void perf_event_task_tick(void)
2875 struct list_head *head = &__get_cpu_var(rotation_list);
2876 struct perf_cpu_context *cpuctx, *tmp;
2877 struct perf_event_context *ctx;
2878 int throttled;
2880 WARN_ON(!irqs_disabled());
2882 __this_cpu_inc(perf_throttled_seq);
2883 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2885 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2886 ctx = &cpuctx->ctx;
2887 perf_adjust_freq_unthr_context(ctx, throttled);
2889 ctx = cpuctx->task_ctx;
2890 if (ctx)
2891 perf_adjust_freq_unthr_context(ctx, throttled);
2895 static int event_enable_on_exec(struct perf_event *event,
2896 struct perf_event_context *ctx)
2898 if (!event->attr.enable_on_exec)
2899 return 0;
2901 event->attr.enable_on_exec = 0;
2902 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2903 return 0;
2905 __perf_event_mark_enabled(event);
2907 return 1;
2911 * Enable all of a task's events that have been marked enable-on-exec.
2912 * This expects task == current.
2914 static void perf_event_enable_on_exec(struct perf_event_context *ctx)
2916 struct perf_event *event;
2917 unsigned long flags;
2918 int enabled = 0;
2919 int ret;
2921 local_irq_save(flags);
2922 if (!ctx || !ctx->nr_events)
2923 goto out;
2926 * We must ctxsw out cgroup events to avoid conflict
2927 * when invoking perf_task_event_sched_in() later on
2928 * in this function. Otherwise we end up trying to
2929 * ctxswin cgroup events which are already scheduled
2930 * in.
2932 perf_cgroup_sched_out(current, NULL);
2934 raw_spin_lock(&ctx->lock);
2935 task_ctx_sched_out(ctx);
2937 list_for_each_entry(event, &ctx->event_list, event_entry) {
2938 ret = event_enable_on_exec(event, ctx);
2939 if (ret)
2940 enabled = 1;
2944 * Unclone this context if we enabled any event.
2946 if (enabled)
2947 unclone_ctx(ctx);
2949 raw_spin_unlock(&ctx->lock);
2952 * Also calls ctxswin for cgroup events, if any:
2954 perf_event_context_sched_in(ctx, ctx->task);
2955 out:
2956 local_irq_restore(flags);
2960 * Cross CPU call to read the hardware event
2962 static void __perf_event_read(void *info)
2964 struct perf_event *event = info;
2965 struct perf_event_context *ctx = event->ctx;
2966 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2969 * If this is a task context, we need to check whether it is
2970 * the current task context of this cpu. If not it has been
2971 * scheduled out before the smp call arrived. In that case
2972 * event->count would have been updated to a recent sample
2973 * when the event was scheduled out.
2975 if (ctx->task && cpuctx->task_ctx != ctx)
2976 return;
2978 raw_spin_lock(&ctx->lock);
2979 if (ctx->is_active) {
2980 update_context_time(ctx);
2981 update_cgrp_time_from_event(event);
2983 update_event_times(event);
2984 if (event->state == PERF_EVENT_STATE_ACTIVE)
2985 event->pmu->read(event);
2986 raw_spin_unlock(&ctx->lock);
2989 static inline u64 perf_event_count(struct perf_event *event)
2991 return local64_read(&event->count) + atomic64_read(&event->child_count);
2994 static u64 perf_event_read(struct perf_event *event)
2997 * If event is enabled and currently active on a CPU, update the
2998 * value in the event structure:
3000 if (event->state == PERF_EVENT_STATE_ACTIVE) {
3001 smp_call_function_single(event->oncpu,
3002 __perf_event_read, event, 1);
3003 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
3004 struct perf_event_context *ctx = event->ctx;
3005 unsigned long flags;
3007 raw_spin_lock_irqsave(&ctx->lock, flags);
3009 * may read while context is not active
3010 * (e.g., thread is blocked), in that case
3011 * we cannot update context time
3013 if (ctx->is_active) {
3014 update_context_time(ctx);
3015 update_cgrp_time_from_event(event);
3017 update_event_times(event);
3018 raw_spin_unlock_irqrestore(&ctx->lock, flags);
3021 return perf_event_count(event);
3025 * Initialize the perf_event context in a task_struct:
3027 static void __perf_event_init_context(struct perf_event_context *ctx)
3029 raw_spin_lock_init(&ctx->lock);
3030 mutex_init(&ctx->mutex);
3031 INIT_LIST_HEAD(&ctx->pinned_groups);
3032 INIT_LIST_HEAD(&ctx->flexible_groups);
3033 INIT_LIST_HEAD(&ctx->event_list);
3034 atomic_set(&ctx->refcount, 1);
3037 static struct perf_event_context *
3038 alloc_perf_context(struct pmu *pmu, struct task_struct *task)
3040 struct perf_event_context *ctx;
3042 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3043 if (!ctx)
3044 return NULL;
3046 __perf_event_init_context(ctx);
3047 if (task) {
3048 ctx->task = task;
3049 get_task_struct(task);
3051 ctx->pmu = pmu;
3053 return ctx;
3056 static struct task_struct *
3057 find_lively_task_by_vpid(pid_t vpid)
3059 struct task_struct *task;
3060 int err;
3062 rcu_read_lock();
3063 if (!vpid)
3064 task = current;
3065 else
3066 task = find_task_by_vpid(vpid);
3067 if (task)
3068 get_task_struct(task);
3069 rcu_read_unlock();
3071 if (!task)
3072 return ERR_PTR(-ESRCH);
3074 /* Reuse ptrace permission checks for now. */
3075 err = -EACCES;
3076 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3077 goto errout;
3079 return task;
3080 errout:
3081 put_task_struct(task);
3082 return ERR_PTR(err);
3087 * Returns a matching context with refcount and pincount.
3089 static struct perf_event_context *
3090 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
3092 struct perf_event_context *ctx;
3093 struct perf_cpu_context *cpuctx;
3094 unsigned long flags;
3095 int ctxn, err;
3097 if (!task) {
3098 /* Must be root to operate on a CPU event: */
3099 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3100 return ERR_PTR(-EACCES);
3103 * We could be clever and allow to attach a event to an
3104 * offline CPU and activate it when the CPU comes up, but
3105 * that's for later.
3107 if (!cpu_online(cpu))
3108 return ERR_PTR(-ENODEV);
3110 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
3111 ctx = &cpuctx->ctx;
3112 get_ctx(ctx);
3113 ++ctx->pin_count;
3115 return ctx;
3118 err = -EINVAL;
3119 ctxn = pmu->task_ctx_nr;
3120 if (ctxn < 0)
3121 goto errout;
3123 retry:
3124 ctx = perf_lock_task_context(task, ctxn, &flags);
3125 if (ctx) {
3126 unclone_ctx(ctx);
3127 ++ctx->pin_count;
3128 raw_spin_unlock_irqrestore(&ctx->lock, flags);
3129 } else {
3130 ctx = alloc_perf_context(pmu, task);
3131 err = -ENOMEM;
3132 if (!ctx)
3133 goto errout;
3135 err = 0;
3136 mutex_lock(&task->perf_event_mutex);
3138 * If it has already passed perf_event_exit_task().
3139 * we must see PF_EXITING, it takes this mutex too.
3141 if (task->flags & PF_EXITING)
3142 err = -ESRCH;
3143 else if (task->perf_event_ctxp[ctxn])
3144 err = -EAGAIN;
3145 else {
3146 get_ctx(ctx);
3147 ++ctx->pin_count;
3148 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
3150 mutex_unlock(&task->perf_event_mutex);
3152 if (unlikely(err)) {
3153 put_ctx(ctx);
3155 if (err == -EAGAIN)
3156 goto retry;
3157 goto errout;
3161 return ctx;
3163 errout:
3164 return ERR_PTR(err);
3167 static void perf_event_free_filter(struct perf_event *event);
3169 static void free_event_rcu(struct rcu_head *head)
3171 struct perf_event *event;
3173 event = container_of(head, struct perf_event, rcu_head);
3174 if (event->ns)
3175 put_pid_ns(event->ns);
3176 perf_event_free_filter(event);
3177 kfree(event);
3180 static void ring_buffer_put(struct ring_buffer *rb);
3181 static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb);
3183 static void unaccount_event_cpu(struct perf_event *event, int cpu)
3185 if (event->parent)
3186 return;
3188 if (has_branch_stack(event)) {
3189 if (!(event->attach_state & PERF_ATTACH_TASK))
3190 atomic_dec(&per_cpu(perf_branch_stack_events, cpu));
3192 if (is_cgroup_event(event))
3193 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3196 static void unaccount_event(struct perf_event *event)
3198 if (event->parent)
3199 return;
3201 if (event->attach_state & PERF_ATTACH_TASK)
3202 static_key_slow_dec_deferred(&perf_sched_events);
3203 if (event->attr.mmap || event->attr.mmap_data)
3204 atomic_dec(&nr_mmap_events);
3205 if (event->attr.comm)
3206 atomic_dec(&nr_comm_events);
3207 if (event->attr.task)
3208 atomic_dec(&nr_task_events);
3209 if (event->attr.freq)
3210 atomic_dec(&nr_freq_events);
3211 if (is_cgroup_event(event))
3212 static_key_slow_dec_deferred(&perf_sched_events);
3213 if (has_branch_stack(event))
3214 static_key_slow_dec_deferred(&perf_sched_events);
3216 unaccount_event_cpu(event, event->cpu);
3219 static void __free_event(struct perf_event *event)
3221 if (!event->parent) {
3222 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3223 put_callchain_buffers();
3226 if (event->destroy)
3227 event->destroy(event);
3229 if (event->ctx)
3230 put_ctx(event->ctx);
3232 call_rcu(&event->rcu_head, free_event_rcu);
3234 static void free_event(struct perf_event *event)
3236 irq_work_sync(&event->pending);
3238 unaccount_event(event);
3240 if (event->rb) {
3241 struct ring_buffer *rb;
3244 * Can happen when we close an event with re-directed output.
3246 * Since we have a 0 refcount, perf_mmap_close() will skip
3247 * over us; possibly making our ring_buffer_put() the last.
3249 mutex_lock(&event->mmap_mutex);
3250 rb = event->rb;
3251 if (rb) {
3252 rcu_assign_pointer(event->rb, NULL);
3253 ring_buffer_detach(event, rb);
3254 ring_buffer_put(rb); /* could be last */
3256 mutex_unlock(&event->mmap_mutex);
3259 if (is_cgroup_event(event))
3260 perf_detach_cgroup(event);
3263 __free_event(event);
3266 int perf_event_release_kernel(struct perf_event *event)
3268 struct perf_event_context *ctx = event->ctx;
3270 WARN_ON_ONCE(ctx->parent_ctx);
3272 * There are two ways this annotation is useful:
3274 * 1) there is a lock recursion from perf_event_exit_task
3275 * see the comment there.
3277 * 2) there is a lock-inversion with mmap_sem through
3278 * perf_event_read_group(), which takes faults while
3279 * holding ctx->mutex, however this is called after
3280 * the last filedesc died, so there is no possibility
3281 * to trigger the AB-BA case.
3283 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
3284 raw_spin_lock_irq(&ctx->lock);
3285 perf_group_detach(event);
3286 raw_spin_unlock_irq(&ctx->lock);
3287 perf_remove_from_context(event);
3288 mutex_unlock(&ctx->mutex);
3290 free_event(event);
3292 return 0;
3294 EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3297 * Called when the last reference to the file is gone.
3299 static void put_event(struct perf_event *event)
3301 struct task_struct *owner;
3303 if (!atomic_long_dec_and_test(&event->refcount))
3304 return;
3306 rcu_read_lock();
3307 owner = ACCESS_ONCE(event->owner);
3309 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3310 * !owner it means the list deletion is complete and we can indeed
3311 * free this event, otherwise we need to serialize on
3312 * owner->perf_event_mutex.
3314 smp_read_barrier_depends();
3315 if (owner) {
3317 * Since delayed_put_task_struct() also drops the last
3318 * task reference we can safely take a new reference
3319 * while holding the rcu_read_lock().
3321 get_task_struct(owner);
3323 rcu_read_unlock();
3325 if (owner) {
3326 mutex_lock(&owner->perf_event_mutex);
3328 * We have to re-check the event->owner field, if it is cleared
3329 * we raced with perf_event_exit_task(), acquiring the mutex
3330 * ensured they're done, and we can proceed with freeing the
3331 * event.
3333 if (event->owner)
3334 list_del_init(&event->owner_entry);
3335 mutex_unlock(&owner->perf_event_mutex);
3336 put_task_struct(owner);
3339 perf_event_release_kernel(event);
3342 static int perf_release(struct inode *inode, struct file *file)
3344 put_event(file->private_data);
3345 return 0;
3348 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
3350 struct perf_event *child;
3351 u64 total = 0;
3353 *enabled = 0;
3354 *running = 0;
3356 mutex_lock(&event->child_mutex);
3357 total += perf_event_read(event);
3358 *enabled += event->total_time_enabled +
3359 atomic64_read(&event->child_total_time_enabled);
3360 *running += event->total_time_running +
3361 atomic64_read(&event->child_total_time_running);
3363 list_for_each_entry(child, &event->child_list, child_list) {
3364 total += perf_event_read(child);
3365 *enabled += child->total_time_enabled;
3366 *running += child->total_time_running;
3368 mutex_unlock(&event->child_mutex);
3370 return total;
3372 EXPORT_SYMBOL_GPL(perf_event_read_value);
3374 static int perf_event_read_group(struct perf_event *event,
3375 u64 read_format, char __user *buf)
3377 struct perf_event *leader = event->group_leader, *sub;
3378 int n = 0, size = 0, ret = -EFAULT;
3379 struct perf_event_context *ctx = leader->ctx;
3380 u64 values[5];
3381 u64 count, enabled, running;
3383 mutex_lock(&ctx->mutex);
3384 count = perf_event_read_value(leader, &enabled, &running);
3386 values[n++] = 1 + leader->nr_siblings;
3387 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3388 values[n++] = enabled;
3389 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3390 values[n++] = running;
3391 values[n++] = count;
3392 if (read_format & PERF_FORMAT_ID)
3393 values[n++] = primary_event_id(leader);
3395 size = n * sizeof(u64);
3397 if (copy_to_user(buf, values, size))
3398 goto unlock;
3400 ret = size;
3402 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3403 n = 0;
3405 values[n++] = perf_event_read_value(sub, &enabled, &running);
3406 if (read_format & PERF_FORMAT_ID)
3407 values[n++] = primary_event_id(sub);
3409 size = n * sizeof(u64);
3411 if (copy_to_user(buf + ret, values, size)) {
3412 ret = -EFAULT;
3413 goto unlock;
3416 ret += size;
3418 unlock:
3419 mutex_unlock(&ctx->mutex);
3421 return ret;
3424 static int perf_event_read_one(struct perf_event *event,
3425 u64 read_format, char __user *buf)
3427 u64 enabled, running;
3428 u64 values[4];
3429 int n = 0;
3431 values[n++] = perf_event_read_value(event, &enabled, &running);
3432 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3433 values[n++] = enabled;
3434 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3435 values[n++] = running;
3436 if (read_format & PERF_FORMAT_ID)
3437 values[n++] = primary_event_id(event);
3439 if (copy_to_user(buf, values, n * sizeof(u64)))
3440 return -EFAULT;
3442 return n * sizeof(u64);
3446 * Read the performance event - simple non blocking version for now
3448 static ssize_t
3449 perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3451 u64 read_format = event->attr.read_format;
3452 int ret;
3455 * Return end-of-file for a read on a event that is in
3456 * error state (i.e. because it was pinned but it couldn't be
3457 * scheduled on to the CPU at some point).
3459 if (event->state == PERF_EVENT_STATE_ERROR)
3460 return 0;
3462 if (count < event->read_size)
3463 return -ENOSPC;
3465 WARN_ON_ONCE(event->ctx->parent_ctx);
3466 if (read_format & PERF_FORMAT_GROUP)
3467 ret = perf_event_read_group(event, read_format, buf);
3468 else
3469 ret = perf_event_read_one(event, read_format, buf);
3471 return ret;
3474 static ssize_t
3475 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3477 struct perf_event *event = file->private_data;
3479 return perf_read_hw(event, buf, count);
3482 static unsigned int perf_poll(struct file *file, poll_table *wait)
3484 struct perf_event *event = file->private_data;
3485 struct ring_buffer *rb;
3486 unsigned int events = POLL_HUP;
3489 * Pin the event->rb by taking event->mmap_mutex; otherwise
3490 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
3492 mutex_lock(&event->mmap_mutex);
3493 rb = event->rb;
3494 if (rb)
3495 events = atomic_xchg(&rb->poll, 0);
3496 mutex_unlock(&event->mmap_mutex);
3498 poll_wait(file, &event->waitq, wait);
3500 return events;
3503 static void perf_event_reset(struct perf_event *event)
3505 (void)perf_event_read(event);
3506 local64_set(&event->count, 0);
3507 perf_event_update_userpage(event);
3511 * Holding the top-level event's child_mutex means that any
3512 * descendant process that has inherited this event will block
3513 * in sync_child_event if it goes to exit, thus satisfying the
3514 * task existence requirements of perf_event_enable/disable.
3516 static void perf_event_for_each_child(struct perf_event *event,
3517 void (*func)(struct perf_event *))
3519 struct perf_event *child;
3521 WARN_ON_ONCE(event->ctx->parent_ctx);
3522 mutex_lock(&event->child_mutex);
3523 func(event);
3524 list_for_each_entry(child, &event->child_list, child_list)
3525 func(child);
3526 mutex_unlock(&event->child_mutex);
3529 static void perf_event_for_each(struct perf_event *event,
3530 void (*func)(struct perf_event *))
3532 struct perf_event_context *ctx = event->ctx;
3533 struct perf_event *sibling;
3535 WARN_ON_ONCE(ctx->parent_ctx);
3536 mutex_lock(&ctx->mutex);
3537 event = event->group_leader;
3539 perf_event_for_each_child(event, func);
3540 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3541 perf_event_for_each_child(sibling, func);
3542 mutex_unlock(&ctx->mutex);
3545 static int perf_event_period(struct perf_event *event, u64 __user *arg)
3547 struct perf_event_context *ctx = event->ctx;
3548 int ret = 0, active;
3549 u64 value;
3551 if (!is_sampling_event(event))
3552 return -EINVAL;
3554 if (copy_from_user(&value, arg, sizeof(value)))
3555 return -EFAULT;
3557 if (!value)
3558 return -EINVAL;
3560 raw_spin_lock_irq(&ctx->lock);
3561 if (event->attr.freq) {
3562 if (value > sysctl_perf_event_sample_rate) {
3563 ret = -EINVAL;
3564 goto unlock;
3567 event->attr.sample_freq = value;
3568 } else {
3569 event->attr.sample_period = value;
3570 event->hw.sample_period = value;
3573 active = (event->state == PERF_EVENT_STATE_ACTIVE);
3574 if (active) {
3575 perf_pmu_disable(ctx->pmu);
3576 event->pmu->stop(event, PERF_EF_UPDATE);
3579 local64_set(&event->hw.period_left, 0);
3581 if (active) {
3582 event->pmu->start(event, PERF_EF_RELOAD);
3583 perf_pmu_enable(ctx->pmu);
3586 unlock:
3587 raw_spin_unlock_irq(&ctx->lock);
3589 return ret;
3592 static const struct file_operations perf_fops;
3594 static inline int perf_fget_light(int fd, struct fd *p)
3596 struct fd f = fdget(fd);
3597 if (!f.file)
3598 return -EBADF;
3600 if (f.file->f_op != &perf_fops) {
3601 fdput(f);
3602 return -EBADF;
3604 *p = f;
3605 return 0;
3608 static int perf_event_set_output(struct perf_event *event,
3609 struct perf_event *output_event);
3610 static int perf_event_set_filter(struct perf_event *event, void __user *arg);
3612 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3614 struct perf_event *event = file->private_data;
3615 void (*func)(struct perf_event *);
3616 u32 flags = arg;
3618 switch (cmd) {
3619 case PERF_EVENT_IOC_ENABLE:
3620 func = perf_event_enable;
3621 break;
3622 case PERF_EVENT_IOC_DISABLE:
3623 func = perf_event_disable;
3624 break;
3625 case PERF_EVENT_IOC_RESET:
3626 func = perf_event_reset;
3627 break;
3629 case PERF_EVENT_IOC_REFRESH:
3630 return perf_event_refresh(event, arg);
3632 case PERF_EVENT_IOC_PERIOD:
3633 return perf_event_period(event, (u64 __user *)arg);
3635 case PERF_EVENT_IOC_ID:
3637 u64 id = primary_event_id(event);
3639 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
3640 return -EFAULT;
3641 return 0;
3644 case PERF_EVENT_IOC_SET_OUTPUT:
3646 int ret;
3647 if (arg != -1) {
3648 struct perf_event *output_event;
3649 struct fd output;
3650 ret = perf_fget_light(arg, &output);
3651 if (ret)
3652 return ret;
3653 output_event = output.file->private_data;
3654 ret = perf_event_set_output(event, output_event);
3655 fdput(output);
3656 } else {
3657 ret = perf_event_set_output(event, NULL);
3659 return ret;
3662 case PERF_EVENT_IOC_SET_FILTER:
3663 return perf_event_set_filter(event, (void __user *)arg);
3665 default:
3666 return -ENOTTY;
3669 if (flags & PERF_IOC_FLAG_GROUP)
3670 perf_event_for_each(event, func);
3671 else
3672 perf_event_for_each_child(event, func);
3674 return 0;
3677 int perf_event_task_enable(void)
3679 struct perf_event *event;
3681 mutex_lock(&current->perf_event_mutex);
3682 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3683 perf_event_for_each_child(event, perf_event_enable);
3684 mutex_unlock(&current->perf_event_mutex);
3686 return 0;
3689 int perf_event_task_disable(void)
3691 struct perf_event *event;
3693 mutex_lock(&current->perf_event_mutex);
3694 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3695 perf_event_for_each_child(event, perf_event_disable);
3696 mutex_unlock(&current->perf_event_mutex);
3698 return 0;
3701 static int perf_event_index(struct perf_event *event)
3703 if (event->hw.state & PERF_HES_STOPPED)
3704 return 0;
3706 if (event->state != PERF_EVENT_STATE_ACTIVE)
3707 return 0;
3709 return event->pmu->event_idx(event);
3712 static void calc_timer_values(struct perf_event *event,
3713 u64 *now,
3714 u64 *enabled,
3715 u64 *running)
3717 u64 ctx_time;
3719 *now = perf_clock();
3720 ctx_time = event->shadow_ctx_time + *now;
3721 *enabled = ctx_time - event->tstamp_enabled;
3722 *running = ctx_time - event->tstamp_running;
3725 static void perf_event_init_userpage(struct perf_event *event)
3727 struct perf_event_mmap_page *userpg;
3728 struct ring_buffer *rb;
3730 rcu_read_lock();
3731 rb = rcu_dereference(event->rb);
3732 if (!rb)
3733 goto unlock;
3735 userpg = rb->user_page;
3737 /* Allow new userspace to detect that bit 0 is deprecated */
3738 userpg->cap_bit0_is_deprecated = 1;
3739 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
3741 unlock:
3742 rcu_read_unlock();
3745 void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
3750 * Callers need to ensure there can be no nesting of this function, otherwise
3751 * the seqlock logic goes bad. We can not serialize this because the arch
3752 * code calls this from NMI context.
3754 void perf_event_update_userpage(struct perf_event *event)
3756 struct perf_event_mmap_page *userpg;
3757 struct ring_buffer *rb;
3758 u64 enabled, running, now;
3760 rcu_read_lock();
3761 rb = rcu_dereference(event->rb);
3762 if (!rb)
3763 goto unlock;
3766 * compute total_time_enabled, total_time_running
3767 * based on snapshot values taken when the event
3768 * was last scheduled in.
3770 * we cannot simply called update_context_time()
3771 * because of locking issue as we can be called in
3772 * NMI context
3774 calc_timer_values(event, &now, &enabled, &running);
3776 userpg = rb->user_page;
3778 * Disable preemption so as to not let the corresponding user-space
3779 * spin too long if we get preempted.
3781 preempt_disable();
3782 ++userpg->lock;
3783 barrier();
3784 userpg->index = perf_event_index(event);
3785 userpg->offset = perf_event_count(event);
3786 if (userpg->index)
3787 userpg->offset -= local64_read(&event->hw.prev_count);
3789 userpg->time_enabled = enabled +
3790 atomic64_read(&event->child_total_time_enabled);
3792 userpg->time_running = running +
3793 atomic64_read(&event->child_total_time_running);
3795 arch_perf_update_userpage(userpg, now);
3797 barrier();
3798 ++userpg->lock;
3799 preempt_enable();
3800 unlock:
3801 rcu_read_unlock();
3804 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3806 struct perf_event *event = vma->vm_file->private_data;
3807 struct ring_buffer *rb;
3808 int ret = VM_FAULT_SIGBUS;
3810 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3811 if (vmf->pgoff == 0)
3812 ret = 0;
3813 return ret;
3816 rcu_read_lock();
3817 rb = rcu_dereference(event->rb);
3818 if (!rb)
3819 goto unlock;
3821 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3822 goto unlock;
3824 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
3825 if (!vmf->page)
3826 goto unlock;
3828 get_page(vmf->page);
3829 vmf->page->mapping = vma->vm_file->f_mapping;
3830 vmf->page->index = vmf->pgoff;
3832 ret = 0;
3833 unlock:
3834 rcu_read_unlock();
3836 return ret;
3839 static void ring_buffer_attach(struct perf_event *event,
3840 struct ring_buffer *rb)
3842 unsigned long flags;
3844 if (!list_empty(&event->rb_entry))
3845 return;
3847 spin_lock_irqsave(&rb->event_lock, flags);
3848 if (list_empty(&event->rb_entry))
3849 list_add(&event->rb_entry, &rb->event_list);
3850 spin_unlock_irqrestore(&rb->event_lock, flags);
3853 static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb)
3855 unsigned long flags;
3857 if (list_empty(&event->rb_entry))
3858 return;
3860 spin_lock_irqsave(&rb->event_lock, flags);
3861 list_del_init(&event->rb_entry);
3862 wake_up_all(&event->waitq);
3863 spin_unlock_irqrestore(&rb->event_lock, flags);
3866 static void ring_buffer_wakeup(struct perf_event *event)
3868 struct ring_buffer *rb;
3870 rcu_read_lock();
3871 rb = rcu_dereference(event->rb);
3872 if (rb) {
3873 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3874 wake_up_all(&event->waitq);
3876 rcu_read_unlock();
3879 static void rb_free_rcu(struct rcu_head *rcu_head)
3881 struct ring_buffer *rb;
3883 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3884 rb_free(rb);
3887 static struct ring_buffer *ring_buffer_get(struct perf_event *event)
3889 struct ring_buffer *rb;
3891 rcu_read_lock();
3892 rb = rcu_dereference(event->rb);
3893 if (rb) {
3894 if (!atomic_inc_not_zero(&rb->refcount))
3895 rb = NULL;
3897 rcu_read_unlock();
3899 return rb;
3902 static void ring_buffer_put(struct ring_buffer *rb)
3904 if (!atomic_dec_and_test(&rb->refcount))
3905 return;
3907 WARN_ON_ONCE(!list_empty(&rb->event_list));
3909 call_rcu(&rb->rcu_head, rb_free_rcu);
3912 static void perf_mmap_open(struct vm_area_struct *vma)
3914 struct perf_event *event = vma->vm_file->private_data;
3916 atomic_inc(&event->mmap_count);
3917 atomic_inc(&event->rb->mmap_count);
3921 * A buffer can be mmap()ed multiple times; either directly through the same
3922 * event, or through other events by use of perf_event_set_output().
3924 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3925 * the buffer here, where we still have a VM context. This means we need
3926 * to detach all events redirecting to us.
3928 static void perf_mmap_close(struct vm_area_struct *vma)
3930 struct perf_event *event = vma->vm_file->private_data;
3932 struct ring_buffer *rb = event->rb;
3933 struct user_struct *mmap_user = rb->mmap_user;
3934 int mmap_locked = rb->mmap_locked;
3935 unsigned long size = perf_data_size(rb);
3937 atomic_dec(&rb->mmap_count);
3939 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
3940 return;
3942 /* Detach current event from the buffer. */
3943 rcu_assign_pointer(event->rb, NULL);
3944 ring_buffer_detach(event, rb);
3945 mutex_unlock(&event->mmap_mutex);
3947 /* If there's still other mmap()s of this buffer, we're done. */
3948 if (atomic_read(&rb->mmap_count)) {
3949 ring_buffer_put(rb); /* can't be last */
3950 return;
3954 * No other mmap()s, detach from all other events that might redirect
3955 * into the now unreachable buffer. Somewhat complicated by the
3956 * fact that rb::event_lock otherwise nests inside mmap_mutex.
3958 again:
3959 rcu_read_lock();
3960 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
3961 if (!atomic_long_inc_not_zero(&event->refcount)) {
3963 * This event is en-route to free_event() which will
3964 * detach it and remove it from the list.
3966 continue;
3968 rcu_read_unlock();
3970 mutex_lock(&event->mmap_mutex);
3972 * Check we didn't race with perf_event_set_output() which can
3973 * swizzle the rb from under us while we were waiting to
3974 * acquire mmap_mutex.
3976 * If we find a different rb; ignore this event, a next
3977 * iteration will no longer find it on the list. We have to
3978 * still restart the iteration to make sure we're not now
3979 * iterating the wrong list.
3981 if (event->rb == rb) {
3982 rcu_assign_pointer(event->rb, NULL);
3983 ring_buffer_detach(event, rb);
3984 ring_buffer_put(rb); /* can't be last, we still have one */
3986 mutex_unlock(&event->mmap_mutex);
3987 put_event(event);
3990 * Restart the iteration; either we're on the wrong list or
3991 * destroyed its integrity by doing a deletion.
3993 goto again;
3995 rcu_read_unlock();
3998 * It could be there's still a few 0-ref events on the list; they'll
3999 * get cleaned up by free_event() -- they'll also still have their
4000 * ref on the rb and will free it whenever they are done with it.
4002 * Aside from that, this buffer is 'fully' detached and unmapped,
4003 * undo the VM accounting.
4006 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4007 vma->vm_mm->pinned_vm -= mmap_locked;
4008 free_uid(mmap_user);
4010 ring_buffer_put(rb); /* could be last */
4013 static const struct vm_operations_struct perf_mmap_vmops = {
4014 .open = perf_mmap_open,
4015 .close = perf_mmap_close,
4016 .fault = perf_mmap_fault,
4017 .page_mkwrite = perf_mmap_fault,
4020 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4022 struct perf_event *event = file->private_data;
4023 unsigned long user_locked, user_lock_limit;
4024 struct user_struct *user = current_user();
4025 unsigned long locked, lock_limit;
4026 struct ring_buffer *rb;
4027 unsigned long vma_size;
4028 unsigned long nr_pages;
4029 long user_extra, extra;
4030 int ret = 0, flags = 0;
4033 * Don't allow mmap() of inherited per-task counters. This would
4034 * create a performance issue due to all children writing to the
4035 * same rb.
4037 if (event->cpu == -1 && event->attr.inherit)
4038 return -EINVAL;
4040 if (!(vma->vm_flags & VM_SHARED))
4041 return -EINVAL;
4043 vma_size = vma->vm_end - vma->vm_start;
4044 nr_pages = (vma_size / PAGE_SIZE) - 1;
4047 * If we have rb pages ensure they're a power-of-two number, so we
4048 * can do bitmasks instead of modulo.
4050 if (nr_pages != 0 && !is_power_of_2(nr_pages))
4051 return -EINVAL;
4053 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4054 return -EINVAL;
4056 if (vma->vm_pgoff != 0)
4057 return -EINVAL;
4059 WARN_ON_ONCE(event->ctx->parent_ctx);
4060 again:
4061 mutex_lock(&event->mmap_mutex);
4062 if (event->rb) {
4063 if (event->rb->nr_pages != nr_pages) {
4064 ret = -EINVAL;
4065 goto unlock;
4068 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4070 * Raced against perf_mmap_close() through
4071 * perf_event_set_output(). Try again, hope for better
4072 * luck.
4074 mutex_unlock(&event->mmap_mutex);
4075 goto again;
4078 goto unlock;
4081 user_extra = nr_pages + 1;
4082 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4085 * Increase the limit linearly with more CPUs:
4087 user_lock_limit *= num_online_cpus();
4089 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4091 extra = 0;
4092 if (user_locked > user_lock_limit)
4093 extra = user_locked - user_lock_limit;
4095 lock_limit = rlimit(RLIMIT_MEMLOCK);
4096 lock_limit >>= PAGE_SHIFT;
4097 locked = vma->vm_mm->pinned_vm + extra;
4099 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4100 !capable(CAP_IPC_LOCK)) {
4101 ret = -EPERM;
4102 goto unlock;
4105 WARN_ON(event->rb);
4107 if (vma->vm_flags & VM_WRITE)
4108 flags |= RING_BUFFER_WRITABLE;
4110 rb = rb_alloc(nr_pages,
4111 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4112 event->cpu, flags);
4114 if (!rb) {
4115 ret = -ENOMEM;
4116 goto unlock;
4119 atomic_set(&rb->mmap_count, 1);
4120 rb->mmap_locked = extra;
4121 rb->mmap_user = get_current_user();
4123 atomic_long_add(user_extra, &user->locked_vm);
4124 vma->vm_mm->pinned_vm += extra;
4126 ring_buffer_attach(event, rb);
4127 rcu_assign_pointer(event->rb, rb);
4129 perf_event_init_userpage(event);
4130 perf_event_update_userpage(event);
4132 unlock:
4133 if (!ret)
4134 atomic_inc(&event->mmap_count);
4135 mutex_unlock(&event->mmap_mutex);
4138 * Since pinned accounting is per vm we cannot allow fork() to copy our
4139 * vma.
4141 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
4142 vma->vm_ops = &perf_mmap_vmops;
4144 return ret;
4147 static int perf_fasync(int fd, struct file *filp, int on)
4149 struct inode *inode = file_inode(filp);
4150 struct perf_event *event = filp->private_data;
4151 int retval;
4153 mutex_lock(&inode->i_mutex);
4154 retval = fasync_helper(fd, filp, on, &event->fasync);
4155 mutex_unlock(&inode->i_mutex);
4157 if (retval < 0)
4158 return retval;
4160 return 0;
4163 static const struct file_operations perf_fops = {
4164 .llseek = no_llseek,
4165 .release = perf_release,
4166 .read = perf_read,
4167 .poll = perf_poll,
4168 .unlocked_ioctl = perf_ioctl,
4169 .compat_ioctl = perf_ioctl,
4170 .mmap = perf_mmap,
4171 .fasync = perf_fasync,
4175 * Perf event wakeup
4177 * If there's data, ensure we set the poll() state and publish everything
4178 * to user-space before waking everybody up.
4181 void perf_event_wakeup(struct perf_event *event)
4183 ring_buffer_wakeup(event);
4185 if (event->pending_kill) {
4186 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
4187 event->pending_kill = 0;
4191 static void perf_pending_event(struct irq_work *entry)
4193 struct perf_event *event = container_of(entry,
4194 struct perf_event, pending);
4196 if (event->pending_disable) {
4197 event->pending_disable = 0;
4198 __perf_event_disable(event);
4201 if (event->pending_wakeup) {
4202 event->pending_wakeup = 0;
4203 perf_event_wakeup(event);
4208 * We assume there is only KVM supporting the callbacks.
4209 * Later on, we might change it to a list if there is
4210 * another virtualization implementation supporting the callbacks.
4212 struct perf_guest_info_callbacks *perf_guest_cbs;
4214 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4216 perf_guest_cbs = cbs;
4217 return 0;
4219 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4221 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4223 perf_guest_cbs = NULL;
4224 return 0;
4226 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4228 static void
4229 perf_output_sample_regs(struct perf_output_handle *handle,
4230 struct pt_regs *regs, u64 mask)
4232 int bit;
4234 for_each_set_bit(bit, (const unsigned long *) &mask,
4235 sizeof(mask) * BITS_PER_BYTE) {
4236 u64 val;
4238 val = perf_reg_value(regs, bit);
4239 perf_output_put(handle, val);
4243 static void perf_sample_regs_user(struct perf_regs_user *regs_user,
4244 struct pt_regs *regs)
4246 if (!user_mode(regs)) {
4247 if (current->mm)
4248 regs = task_pt_regs(current);
4249 else
4250 regs = NULL;
4253 if (regs) {
4254 regs_user->regs = regs;
4255 regs_user->abi = perf_reg_abi(current);
4260 * Get remaining task size from user stack pointer.
4262 * It'd be better to take stack vma map and limit this more
4263 * precisly, but there's no way to get it safely under interrupt,
4264 * so using TASK_SIZE as limit.
4266 static u64 perf_ustack_task_size(struct pt_regs *regs)
4268 unsigned long addr = perf_user_stack_pointer(regs);
4270 if (!addr || addr >= TASK_SIZE)
4271 return 0;
4273 return TASK_SIZE - addr;
4276 static u16
4277 perf_sample_ustack_size(u16 stack_size, u16 header_size,
4278 struct pt_regs *regs)
4280 u64 task_size;
4282 /* No regs, no stack pointer, no dump. */
4283 if (!regs)
4284 return 0;
4287 * Check if we fit in with the requested stack size into the:
4288 * - TASK_SIZE
4289 * If we don't, we limit the size to the TASK_SIZE.
4291 * - remaining sample size
4292 * If we don't, we customize the stack size to
4293 * fit in to the remaining sample size.
4296 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
4297 stack_size = min(stack_size, (u16) task_size);
4299 /* Current header size plus static size and dynamic size. */
4300 header_size += 2 * sizeof(u64);
4302 /* Do we fit in with the current stack dump size? */
4303 if ((u16) (header_size + stack_size) < header_size) {
4305 * If we overflow the maximum size for the sample,
4306 * we customize the stack dump size to fit in.
4308 stack_size = USHRT_MAX - header_size - sizeof(u64);
4309 stack_size = round_up(stack_size, sizeof(u64));
4312 return stack_size;
4315 static void
4316 perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4317 struct pt_regs *regs)
4319 /* Case of a kernel thread, nothing to dump */
4320 if (!regs) {
4321 u64 size = 0;
4322 perf_output_put(handle, size);
4323 } else {
4324 unsigned long sp;
4325 unsigned int rem;
4326 u64 dyn_size;
4329 * We dump:
4330 * static size
4331 * - the size requested by user or the best one we can fit
4332 * in to the sample max size
4333 * data
4334 * - user stack dump data
4335 * dynamic size
4336 * - the actual dumped size
4339 /* Static size. */
4340 perf_output_put(handle, dump_size);
4342 /* Data. */
4343 sp = perf_user_stack_pointer(regs);
4344 rem = __output_copy_user(handle, (void *) sp, dump_size);
4345 dyn_size = dump_size - rem;
4347 perf_output_skip(handle, rem);
4349 /* Dynamic size. */
4350 perf_output_put(handle, dyn_size);
4354 static void __perf_event_header__init_id(struct perf_event_header *header,
4355 struct perf_sample_data *data,
4356 struct perf_event *event)
4358 u64 sample_type = event->attr.sample_type;
4360 data->type = sample_type;
4361 header->size += event->id_header_size;
4363 if (sample_type & PERF_SAMPLE_TID) {
4364 /* namespace issues */
4365 data->tid_entry.pid = perf_event_pid(event, current);
4366 data->tid_entry.tid = perf_event_tid(event, current);
4369 if (sample_type & PERF_SAMPLE_TIME)
4370 data->time = perf_clock();
4372 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
4373 data->id = primary_event_id(event);
4375 if (sample_type & PERF_SAMPLE_STREAM_ID)
4376 data->stream_id = event->id;
4378 if (sample_type & PERF_SAMPLE_CPU) {
4379 data->cpu_entry.cpu = raw_smp_processor_id();
4380 data->cpu_entry.reserved = 0;
4384 void perf_event_header__init_id(struct perf_event_header *header,
4385 struct perf_sample_data *data,
4386 struct perf_event *event)
4388 if (event->attr.sample_id_all)
4389 __perf_event_header__init_id(header, data, event);
4392 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4393 struct perf_sample_data *data)
4395 u64 sample_type = data->type;
4397 if (sample_type & PERF_SAMPLE_TID)
4398 perf_output_put(handle, data->tid_entry);
4400 if (sample_type & PERF_SAMPLE_TIME)
4401 perf_output_put(handle, data->time);
4403 if (sample_type & PERF_SAMPLE_ID)
4404 perf_output_put(handle, data->id);
4406 if (sample_type & PERF_SAMPLE_STREAM_ID)
4407 perf_output_put(handle, data->stream_id);
4409 if (sample_type & PERF_SAMPLE_CPU)
4410 perf_output_put(handle, data->cpu_entry);
4412 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4413 perf_output_put(handle, data->id);
4416 void perf_event__output_id_sample(struct perf_event *event,
4417 struct perf_output_handle *handle,
4418 struct perf_sample_data *sample)
4420 if (event->attr.sample_id_all)
4421 __perf_event__output_id_sample(handle, sample);
4424 static void perf_output_read_one(struct perf_output_handle *handle,
4425 struct perf_event *event,
4426 u64 enabled, u64 running)
4428 u64 read_format = event->attr.read_format;
4429 u64 values[4];
4430 int n = 0;
4432 values[n++] = perf_event_count(event);
4433 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4434 values[n++] = enabled +
4435 atomic64_read(&event->child_total_time_enabled);
4437 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4438 values[n++] = running +
4439 atomic64_read(&event->child_total_time_running);
4441 if (read_format & PERF_FORMAT_ID)
4442 values[n++] = primary_event_id(event);
4444 __output_copy(handle, values, n * sizeof(u64));
4448 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4450 static void perf_output_read_group(struct perf_output_handle *handle,
4451 struct perf_event *event,
4452 u64 enabled, u64 running)
4454 struct perf_event *leader = event->group_leader, *sub;
4455 u64 read_format = event->attr.read_format;
4456 u64 values[5];
4457 int n = 0;
4459 values[n++] = 1 + leader->nr_siblings;
4461 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4462 values[n++] = enabled;
4464 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4465 values[n++] = running;
4467 if (leader != event)
4468 leader->pmu->read(leader);
4470 values[n++] = perf_event_count(leader);
4471 if (read_format & PERF_FORMAT_ID)
4472 values[n++] = primary_event_id(leader);
4474 __output_copy(handle, values, n * sizeof(u64));
4476 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4477 n = 0;
4479 if ((sub != event) &&
4480 (sub->state == PERF_EVENT_STATE_ACTIVE))
4481 sub->pmu->read(sub);
4483 values[n++] = perf_event_count(sub);
4484 if (read_format & PERF_FORMAT_ID)
4485 values[n++] = primary_event_id(sub);
4487 __output_copy(handle, values, n * sizeof(u64));
4491 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4492 PERF_FORMAT_TOTAL_TIME_RUNNING)
4494 static void perf_output_read(struct perf_output_handle *handle,
4495 struct perf_event *event)
4497 u64 enabled = 0, running = 0, now;
4498 u64 read_format = event->attr.read_format;
4501 * compute total_time_enabled, total_time_running
4502 * based on snapshot values taken when the event
4503 * was last scheduled in.
4505 * we cannot simply called update_context_time()
4506 * because of locking issue as we are called in
4507 * NMI context
4509 if (read_format & PERF_FORMAT_TOTAL_TIMES)
4510 calc_timer_values(event, &now, &enabled, &running);
4512 if (event->attr.read_format & PERF_FORMAT_GROUP)
4513 perf_output_read_group(handle, event, enabled, running);
4514 else
4515 perf_output_read_one(handle, event, enabled, running);
4518 void perf_output_sample(struct perf_output_handle *handle,
4519 struct perf_event_header *header,
4520 struct perf_sample_data *data,
4521 struct perf_event *event)
4523 u64 sample_type = data->type;
4525 perf_output_put(handle, *header);
4527 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4528 perf_output_put(handle, data->id);
4530 if (sample_type & PERF_SAMPLE_IP)
4531 perf_output_put(handle, data->ip);
4533 if (sample_type & PERF_SAMPLE_TID)
4534 perf_output_put(handle, data->tid_entry);
4536 if (sample_type & PERF_SAMPLE_TIME)
4537 perf_output_put(handle, data->time);
4539 if (sample_type & PERF_SAMPLE_ADDR)
4540 perf_output_put(handle, data->addr);
4542 if (sample_type & PERF_SAMPLE_ID)
4543 perf_output_put(handle, data->id);
4545 if (sample_type & PERF_SAMPLE_STREAM_ID)
4546 perf_output_put(handle, data->stream_id);
4548 if (sample_type & PERF_SAMPLE_CPU)
4549 perf_output_put(handle, data->cpu_entry);
4551 if (sample_type & PERF_SAMPLE_PERIOD)
4552 perf_output_put(handle, data->period);
4554 if (sample_type & PERF_SAMPLE_READ)
4555 perf_output_read(handle, event);
4557 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4558 if (data->callchain) {
4559 int size = 1;
4561 if (data->callchain)
4562 size += data->callchain->nr;
4564 size *= sizeof(u64);
4566 __output_copy(handle, data->callchain, size);
4567 } else {
4568 u64 nr = 0;
4569 perf_output_put(handle, nr);
4573 if (sample_type & PERF_SAMPLE_RAW) {
4574 if (data->raw) {
4575 perf_output_put(handle, data->raw->size);
4576 __output_copy(handle, data->raw->data,
4577 data->raw->size);
4578 } else {
4579 struct {
4580 u32 size;
4581 u32 data;
4582 } raw = {
4583 .size = sizeof(u32),
4584 .data = 0,
4586 perf_output_put(handle, raw);
4590 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4591 if (data->br_stack) {
4592 size_t size;
4594 size = data->br_stack->nr
4595 * sizeof(struct perf_branch_entry);
4597 perf_output_put(handle, data->br_stack->nr);
4598 perf_output_copy(handle, data->br_stack->entries, size);
4599 } else {
4601 * we always store at least the value of nr
4603 u64 nr = 0;
4604 perf_output_put(handle, nr);
4608 if (sample_type & PERF_SAMPLE_REGS_USER) {
4609 u64 abi = data->regs_user.abi;
4612 * If there are no regs to dump, notice it through
4613 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4615 perf_output_put(handle, abi);
4617 if (abi) {
4618 u64 mask = event->attr.sample_regs_user;
4619 perf_output_sample_regs(handle,
4620 data->regs_user.regs,
4621 mask);
4625 if (sample_type & PERF_SAMPLE_STACK_USER) {
4626 perf_output_sample_ustack(handle,
4627 data->stack_user_size,
4628 data->regs_user.regs);
4631 if (sample_type & PERF_SAMPLE_WEIGHT)
4632 perf_output_put(handle, data->weight);
4634 if (sample_type & PERF_SAMPLE_DATA_SRC)
4635 perf_output_put(handle, data->data_src.val);
4637 if (sample_type & PERF_SAMPLE_TRANSACTION)
4638 perf_output_put(handle, data->txn);
4640 if (!event->attr.watermark) {
4641 int wakeup_events = event->attr.wakeup_events;
4643 if (wakeup_events) {
4644 struct ring_buffer *rb = handle->rb;
4645 int events = local_inc_return(&rb->events);
4647 if (events >= wakeup_events) {
4648 local_sub(wakeup_events, &rb->events);
4649 local_inc(&rb->wakeup);
4655 void perf_prepare_sample(struct perf_event_header *header,
4656 struct perf_sample_data *data,
4657 struct perf_event *event,
4658 struct pt_regs *regs)
4660 u64 sample_type = event->attr.sample_type;
4662 header->type = PERF_RECORD_SAMPLE;
4663 header->size = sizeof(*header) + event->header_size;
4665 header->misc = 0;
4666 header->misc |= perf_misc_flags(regs);
4668 __perf_event_header__init_id(header, data, event);
4670 if (sample_type & PERF_SAMPLE_IP)
4671 data->ip = perf_instruction_pointer(regs);
4673 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4674 int size = 1;
4676 data->callchain = perf_callchain(event, regs);
4678 if (data->callchain)
4679 size += data->callchain->nr;
4681 header->size += size * sizeof(u64);
4684 if (sample_type & PERF_SAMPLE_RAW) {
4685 int size = sizeof(u32);
4687 if (data->raw)
4688 size += data->raw->size;
4689 else
4690 size += sizeof(u32);
4692 WARN_ON_ONCE(size & (sizeof(u64)-1));
4693 header->size += size;
4696 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4697 int size = sizeof(u64); /* nr */
4698 if (data->br_stack) {
4699 size += data->br_stack->nr
4700 * sizeof(struct perf_branch_entry);
4702 header->size += size;
4705 if (sample_type & PERF_SAMPLE_REGS_USER) {
4706 /* regs dump ABI info */
4707 int size = sizeof(u64);
4709 perf_sample_regs_user(&data->regs_user, regs);
4711 if (data->regs_user.regs) {
4712 u64 mask = event->attr.sample_regs_user;
4713 size += hweight64(mask) * sizeof(u64);
4716 header->size += size;
4719 if (sample_type & PERF_SAMPLE_STACK_USER) {
4721 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4722 * processed as the last one or have additional check added
4723 * in case new sample type is added, because we could eat
4724 * up the rest of the sample size.
4726 struct perf_regs_user *uregs = &data->regs_user;
4727 u16 stack_size = event->attr.sample_stack_user;
4728 u16 size = sizeof(u64);
4730 if (!uregs->abi)
4731 perf_sample_regs_user(uregs, regs);
4733 stack_size = perf_sample_ustack_size(stack_size, header->size,
4734 uregs->regs);
4737 * If there is something to dump, add space for the dump
4738 * itself and for the field that tells the dynamic size,
4739 * which is how many have been actually dumped.
4741 if (stack_size)
4742 size += sizeof(u64) + stack_size;
4744 data->stack_user_size = stack_size;
4745 header->size += size;
4749 static void perf_event_output(struct perf_event *event,
4750 struct perf_sample_data *data,
4751 struct pt_regs *regs)
4753 struct perf_output_handle handle;
4754 struct perf_event_header header;
4756 /* protect the callchain buffers */
4757 rcu_read_lock();
4759 perf_prepare_sample(&header, data, event, regs);
4761 if (perf_output_begin(&handle, event, header.size))
4762 goto exit;
4764 perf_output_sample(&handle, &header, data, event);
4766 perf_output_end(&handle);
4768 exit:
4769 rcu_read_unlock();
4773 * read event_id
4776 struct perf_read_event {
4777 struct perf_event_header header;
4779 u32 pid;
4780 u32 tid;
4783 static void
4784 perf_event_read_event(struct perf_event *event,
4785 struct task_struct *task)
4787 struct perf_output_handle handle;
4788 struct perf_sample_data sample;
4789 struct perf_read_event read_event = {
4790 .header = {
4791 .type = PERF_RECORD_READ,
4792 .misc = 0,
4793 .size = sizeof(read_event) + event->read_size,
4795 .pid = perf_event_pid(event, task),
4796 .tid = perf_event_tid(event, task),
4798 int ret;
4800 perf_event_header__init_id(&read_event.header, &sample, event);
4801 ret = perf_output_begin(&handle, event, read_event.header.size);
4802 if (ret)
4803 return;
4805 perf_output_put(&handle, read_event);
4806 perf_output_read(&handle, event);
4807 perf_event__output_id_sample(event, &handle, &sample);
4809 perf_output_end(&handle);
4812 typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
4814 static void
4815 perf_event_aux_ctx(struct perf_event_context *ctx,
4816 perf_event_aux_output_cb output,
4817 void *data)
4819 struct perf_event *event;
4821 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4822 if (event->state < PERF_EVENT_STATE_INACTIVE)
4823 continue;
4824 if (!event_filter_match(event))
4825 continue;
4826 output(event, data);
4830 static void
4831 perf_event_aux(perf_event_aux_output_cb output, void *data,
4832 struct perf_event_context *task_ctx)
4834 struct perf_cpu_context *cpuctx;
4835 struct perf_event_context *ctx;
4836 struct pmu *pmu;
4837 int ctxn;
4839 rcu_read_lock();
4840 list_for_each_entry_rcu(pmu, &pmus, entry) {
4841 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4842 if (cpuctx->unique_pmu != pmu)
4843 goto next;
4844 perf_event_aux_ctx(&cpuctx->ctx, output, data);
4845 if (task_ctx)
4846 goto next;
4847 ctxn = pmu->task_ctx_nr;
4848 if (ctxn < 0)
4849 goto next;
4850 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4851 if (ctx)
4852 perf_event_aux_ctx(ctx, output, data);
4853 next:
4854 put_cpu_ptr(pmu->pmu_cpu_context);
4857 if (task_ctx) {
4858 preempt_disable();
4859 perf_event_aux_ctx(task_ctx, output, data);
4860 preempt_enable();
4862 rcu_read_unlock();
4866 * task tracking -- fork/exit
4868 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
4871 struct perf_task_event {
4872 struct task_struct *task;
4873 struct perf_event_context *task_ctx;
4875 struct {
4876 struct perf_event_header header;
4878 u32 pid;
4879 u32 ppid;
4880 u32 tid;
4881 u32 ptid;
4882 u64 time;
4883 } event_id;
4886 static int perf_event_task_match(struct perf_event *event)
4888 return event->attr.comm || event->attr.mmap ||
4889 event->attr.mmap2 || event->attr.mmap_data ||
4890 event->attr.task;
4893 static void perf_event_task_output(struct perf_event *event,
4894 void *data)
4896 struct perf_task_event *task_event = data;
4897 struct perf_output_handle handle;
4898 struct perf_sample_data sample;
4899 struct task_struct *task = task_event->task;
4900 int ret, size = task_event->event_id.header.size;
4902 if (!perf_event_task_match(event))
4903 return;
4905 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
4907 ret = perf_output_begin(&handle, event,
4908 task_event->event_id.header.size);
4909 if (ret)
4910 goto out;
4912 task_event->event_id.pid = perf_event_pid(event, task);
4913 task_event->event_id.ppid = perf_event_pid(event, current);
4915 task_event->event_id.tid = perf_event_tid(event, task);
4916 task_event->event_id.ptid = perf_event_tid(event, current);
4918 perf_output_put(&handle, task_event->event_id);
4920 perf_event__output_id_sample(event, &handle, &sample);
4922 perf_output_end(&handle);
4923 out:
4924 task_event->event_id.header.size = size;
4927 static void perf_event_task(struct task_struct *task,
4928 struct perf_event_context *task_ctx,
4929 int new)
4931 struct perf_task_event task_event;
4933 if (!atomic_read(&nr_comm_events) &&
4934 !atomic_read(&nr_mmap_events) &&
4935 !atomic_read(&nr_task_events))
4936 return;
4938 task_event = (struct perf_task_event){
4939 .task = task,
4940 .task_ctx = task_ctx,
4941 .event_id = {
4942 .header = {
4943 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4944 .misc = 0,
4945 .size = sizeof(task_event.event_id),
4947 /* .pid */
4948 /* .ppid */
4949 /* .tid */
4950 /* .ptid */
4951 .time = perf_clock(),
4955 perf_event_aux(perf_event_task_output,
4956 &task_event,
4957 task_ctx);
4960 void perf_event_fork(struct task_struct *task)
4962 perf_event_task(task, NULL, 1);
4966 * comm tracking
4969 struct perf_comm_event {
4970 struct task_struct *task;
4971 char *comm;
4972 int comm_size;
4974 struct {
4975 struct perf_event_header header;
4977 u32 pid;
4978 u32 tid;
4979 } event_id;
4982 static int perf_event_comm_match(struct perf_event *event)
4984 return event->attr.comm;
4987 static void perf_event_comm_output(struct perf_event *event,
4988 void *data)
4990 struct perf_comm_event *comm_event = data;
4991 struct perf_output_handle handle;
4992 struct perf_sample_data sample;
4993 int size = comm_event->event_id.header.size;
4994 int ret;
4996 if (!perf_event_comm_match(event))
4997 return;
4999 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
5000 ret = perf_output_begin(&handle, event,
5001 comm_event->event_id.header.size);
5003 if (ret)
5004 goto out;
5006 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5007 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5009 perf_output_put(&handle, comm_event->event_id);
5010 __output_copy(&handle, comm_event->comm,
5011 comm_event->comm_size);
5013 perf_event__output_id_sample(event, &handle, &sample);
5015 perf_output_end(&handle);
5016 out:
5017 comm_event->event_id.header.size = size;
5020 static void perf_event_comm_event(struct perf_comm_event *comm_event)
5022 char comm[TASK_COMM_LEN];
5023 unsigned int size;
5025 memset(comm, 0, sizeof(comm));
5026 strlcpy(comm, comm_event->task->comm, sizeof(comm));
5027 size = ALIGN(strlen(comm)+1, sizeof(u64));
5029 comm_event->comm = comm;
5030 comm_event->comm_size = size;
5032 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
5034 perf_event_aux(perf_event_comm_output,
5035 comm_event,
5036 NULL);
5039 void perf_event_comm(struct task_struct *task)
5041 struct perf_comm_event comm_event;
5042 struct perf_event_context *ctx;
5043 int ctxn;
5045 rcu_read_lock();
5046 for_each_task_context_nr(ctxn) {
5047 ctx = task->perf_event_ctxp[ctxn];
5048 if (!ctx)
5049 continue;
5051 perf_event_enable_on_exec(ctx);
5053 rcu_read_unlock();
5055 if (!atomic_read(&nr_comm_events))
5056 return;
5058 comm_event = (struct perf_comm_event){
5059 .task = task,
5060 /* .comm */
5061 /* .comm_size */
5062 .event_id = {
5063 .header = {
5064 .type = PERF_RECORD_COMM,
5065 .misc = 0,
5066 /* .size */
5068 /* .pid */
5069 /* .tid */
5073 perf_event_comm_event(&comm_event);
5077 * mmap tracking
5080 struct perf_mmap_event {
5081 struct vm_area_struct *vma;
5083 const char *file_name;
5084 int file_size;
5085 int maj, min;
5086 u64 ino;
5087 u64 ino_generation;
5089 struct {
5090 struct perf_event_header header;
5092 u32 pid;
5093 u32 tid;
5094 u64 start;
5095 u64 len;
5096 u64 pgoff;
5097 } event_id;
5100 static int perf_event_mmap_match(struct perf_event *event,
5101 void *data)
5103 struct perf_mmap_event *mmap_event = data;
5104 struct vm_area_struct *vma = mmap_event->vma;
5105 int executable = vma->vm_flags & VM_EXEC;
5107 return (!executable && event->attr.mmap_data) ||
5108 (executable && (event->attr.mmap || event->attr.mmap2));
5111 static void perf_event_mmap_output(struct perf_event *event,
5112 void *data)
5114 struct perf_mmap_event *mmap_event = data;
5115 struct perf_output_handle handle;
5116 struct perf_sample_data sample;
5117 int size = mmap_event->event_id.header.size;
5118 int ret;
5120 if (!perf_event_mmap_match(event, data))
5121 return;
5123 if (event->attr.mmap2) {
5124 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5125 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5126 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5127 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
5128 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
5131 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5132 ret = perf_output_begin(&handle, event,
5133 mmap_event->event_id.header.size);
5134 if (ret)
5135 goto out;
5137 mmap_event->event_id.pid = perf_event_pid(event, current);
5138 mmap_event->event_id.tid = perf_event_tid(event, current);
5140 perf_output_put(&handle, mmap_event->event_id);
5142 if (event->attr.mmap2) {
5143 perf_output_put(&handle, mmap_event->maj);
5144 perf_output_put(&handle, mmap_event->min);
5145 perf_output_put(&handle, mmap_event->ino);
5146 perf_output_put(&handle, mmap_event->ino_generation);
5149 __output_copy(&handle, mmap_event->file_name,
5150 mmap_event->file_size);
5152 perf_event__output_id_sample(event, &handle, &sample);
5154 perf_output_end(&handle);
5155 out:
5156 mmap_event->event_id.header.size = size;
5159 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
5161 struct vm_area_struct *vma = mmap_event->vma;
5162 struct file *file = vma->vm_file;
5163 int maj = 0, min = 0;
5164 u64 ino = 0, gen = 0;
5165 unsigned int size;
5166 char tmp[16];
5167 char *buf = NULL;
5168 char *name;
5170 if (file) {
5171 struct inode *inode;
5172 dev_t dev;
5174 buf = kmalloc(PATH_MAX, GFP_KERNEL);
5175 if (!buf) {
5176 name = "//enomem";
5177 goto cpy_name;
5180 * d_path() works from the end of the rb backwards, so we
5181 * need to add enough zero bytes after the string to handle
5182 * the 64bit alignment we do later.
5184 name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64));
5185 if (IS_ERR(name)) {
5186 name = "//toolong";
5187 goto cpy_name;
5189 inode = file_inode(vma->vm_file);
5190 dev = inode->i_sb->s_dev;
5191 ino = inode->i_ino;
5192 gen = inode->i_generation;
5193 maj = MAJOR(dev);
5194 min = MINOR(dev);
5195 goto got_name;
5196 } else {
5197 name = (char *)arch_vma_name(vma);
5198 if (name)
5199 goto cpy_name;
5201 if (vma->vm_start <= vma->vm_mm->start_brk &&
5202 vma->vm_end >= vma->vm_mm->brk) {
5203 name = "[heap]";
5204 goto cpy_name;
5206 if (vma->vm_start <= vma->vm_mm->start_stack &&
5207 vma->vm_end >= vma->vm_mm->start_stack) {
5208 name = "[stack]";
5209 goto cpy_name;
5212 name = "//anon";
5213 goto cpy_name;
5216 cpy_name:
5217 strlcpy(tmp, name, sizeof(tmp));
5218 name = tmp;
5219 got_name:
5221 * Since our buffer works in 8 byte units we need to align our string
5222 * size to a multiple of 8. However, we must guarantee the tail end is
5223 * zero'd out to avoid leaking random bits to userspace.
5225 size = strlen(name)+1;
5226 while (!IS_ALIGNED(size, sizeof(u64)))
5227 name[size++] = '\0';
5229 mmap_event->file_name = name;
5230 mmap_event->file_size = size;
5231 mmap_event->maj = maj;
5232 mmap_event->min = min;
5233 mmap_event->ino = ino;
5234 mmap_event->ino_generation = gen;
5236 if (!(vma->vm_flags & VM_EXEC))
5237 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
5239 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
5241 perf_event_aux(perf_event_mmap_output,
5242 mmap_event,
5243 NULL);
5245 kfree(buf);
5248 void perf_event_mmap(struct vm_area_struct *vma)
5250 struct perf_mmap_event mmap_event;
5252 if (!atomic_read(&nr_mmap_events))
5253 return;
5255 mmap_event = (struct perf_mmap_event){
5256 .vma = vma,
5257 /* .file_name */
5258 /* .file_size */
5259 .event_id = {
5260 .header = {
5261 .type = PERF_RECORD_MMAP,
5262 .misc = PERF_RECORD_MISC_USER,
5263 /* .size */
5265 /* .pid */
5266 /* .tid */
5267 .start = vma->vm_start,
5268 .len = vma->vm_end - vma->vm_start,
5269 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
5271 /* .maj (attr_mmap2 only) */
5272 /* .min (attr_mmap2 only) */
5273 /* .ino (attr_mmap2 only) */
5274 /* .ino_generation (attr_mmap2 only) */
5277 perf_event_mmap_event(&mmap_event);
5281 * IRQ throttle logging
5284 static void perf_log_throttle(struct perf_event *event, int enable)
5286 struct perf_output_handle handle;
5287 struct perf_sample_data sample;
5288 int ret;
5290 struct {
5291 struct perf_event_header header;
5292 u64 time;
5293 u64 id;
5294 u64 stream_id;
5295 } throttle_event = {
5296 .header = {
5297 .type = PERF_RECORD_THROTTLE,
5298 .misc = 0,
5299 .size = sizeof(throttle_event),
5301 .time = perf_clock(),
5302 .id = primary_event_id(event),
5303 .stream_id = event->id,
5306 if (enable)
5307 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
5309 perf_event_header__init_id(&throttle_event.header, &sample, event);
5311 ret = perf_output_begin(&handle, event,
5312 throttle_event.header.size);
5313 if (ret)
5314 return;
5316 perf_output_put(&handle, throttle_event);
5317 perf_event__output_id_sample(event, &handle, &sample);
5318 perf_output_end(&handle);
5322 * Generic event overflow handling, sampling.
5325 static int __perf_event_overflow(struct perf_event *event,
5326 int throttle, struct perf_sample_data *data,
5327 struct pt_regs *regs)
5329 int events = atomic_read(&event->event_limit);
5330 struct hw_perf_event *hwc = &event->hw;
5331 u64 seq;
5332 int ret = 0;
5335 * Non-sampling counters might still use the PMI to fold short
5336 * hardware counters, ignore those.
5338 if (unlikely(!is_sampling_event(event)))
5339 return 0;
5341 seq = __this_cpu_read(perf_throttled_seq);
5342 if (seq != hwc->interrupts_seq) {
5343 hwc->interrupts_seq = seq;
5344 hwc->interrupts = 1;
5345 } else {
5346 hwc->interrupts++;
5347 if (unlikely(throttle
5348 && hwc->interrupts >= max_samples_per_tick)) {
5349 __this_cpu_inc(perf_throttled_count);
5350 hwc->interrupts = MAX_INTERRUPTS;
5351 perf_log_throttle(event, 0);
5352 tick_nohz_full_kick();
5353 ret = 1;
5357 if (event->attr.freq) {
5358 u64 now = perf_clock();
5359 s64 delta = now - hwc->freq_time_stamp;
5361 hwc->freq_time_stamp = now;
5363 if (delta > 0 && delta < 2*TICK_NSEC)
5364 perf_adjust_period(event, delta, hwc->last_period, true);
5368 * XXX event_limit might not quite work as expected on inherited
5369 * events
5372 event->pending_kill = POLL_IN;
5373 if (events && atomic_dec_and_test(&event->event_limit)) {
5374 ret = 1;
5375 event->pending_kill = POLL_HUP;
5376 event->pending_disable = 1;
5377 irq_work_queue(&event->pending);
5380 if (event->overflow_handler)
5381 event->overflow_handler(event, data, regs);
5382 else
5383 perf_event_output(event, data, regs);
5385 if (event->fasync && event->pending_kill) {
5386 event->pending_wakeup = 1;
5387 irq_work_queue(&event->pending);
5390 return ret;
5393 int perf_event_overflow(struct perf_event *event,
5394 struct perf_sample_data *data,
5395 struct pt_regs *regs)
5397 return __perf_event_overflow(event, 1, data, regs);
5401 * Generic software event infrastructure
5404 struct swevent_htable {
5405 struct swevent_hlist *swevent_hlist;
5406 struct mutex hlist_mutex;
5407 int hlist_refcount;
5409 /* Recursion avoidance in each contexts */
5410 int recursion[PERF_NR_CONTEXTS];
5413 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5416 * We directly increment event->count and keep a second value in
5417 * event->hw.period_left to count intervals. This period event
5418 * is kept in the range [-sample_period, 0] so that we can use the
5419 * sign as trigger.
5422 u64 perf_swevent_set_period(struct perf_event *event)
5424 struct hw_perf_event *hwc = &event->hw;
5425 u64 period = hwc->last_period;
5426 u64 nr, offset;
5427 s64 old, val;
5429 hwc->last_period = hwc->sample_period;
5431 again:
5432 old = val = local64_read(&hwc->period_left);
5433 if (val < 0)
5434 return 0;
5436 nr = div64_u64(period + val, period);
5437 offset = nr * period;
5438 val -= offset;
5439 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
5440 goto again;
5442 return nr;
5445 static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
5446 struct perf_sample_data *data,
5447 struct pt_regs *regs)
5449 struct hw_perf_event *hwc = &event->hw;
5450 int throttle = 0;
5452 if (!overflow)
5453 overflow = perf_swevent_set_period(event);
5455 if (hwc->interrupts == MAX_INTERRUPTS)
5456 return;
5458 for (; overflow; overflow--) {
5459 if (__perf_event_overflow(event, throttle,
5460 data, regs)) {
5462 * We inhibit the overflow from happening when
5463 * hwc->interrupts == MAX_INTERRUPTS.
5465 break;
5467 throttle = 1;
5471 static void perf_swevent_event(struct perf_event *event, u64 nr,
5472 struct perf_sample_data *data,
5473 struct pt_regs *regs)
5475 struct hw_perf_event *hwc = &event->hw;
5477 local64_add(nr, &event->count);
5479 if (!regs)
5480 return;
5482 if (!is_sampling_event(event))
5483 return;
5485 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5486 data->period = nr;
5487 return perf_swevent_overflow(event, 1, data, regs);
5488 } else
5489 data->period = event->hw.last_period;
5491 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
5492 return perf_swevent_overflow(event, 1, data, regs);
5494 if (local64_add_negative(nr, &hwc->period_left))
5495 return;
5497 perf_swevent_overflow(event, 0, data, regs);
5500 static int perf_exclude_event(struct perf_event *event,
5501 struct pt_regs *regs)
5503 if (event->hw.state & PERF_HES_STOPPED)
5504 return 1;
5506 if (regs) {
5507 if (event->attr.exclude_user && user_mode(regs))
5508 return 1;
5510 if (event->attr.exclude_kernel && !user_mode(regs))
5511 return 1;
5514 return 0;
5517 static int perf_swevent_match(struct perf_event *event,
5518 enum perf_type_id type,
5519 u32 event_id,
5520 struct perf_sample_data *data,
5521 struct pt_regs *regs)
5523 if (event->attr.type != type)
5524 return 0;
5526 if (event->attr.config != event_id)
5527 return 0;
5529 if (perf_exclude_event(event, regs))
5530 return 0;
5532 return 1;
5535 static inline u64 swevent_hash(u64 type, u32 event_id)
5537 u64 val = event_id | (type << 32);
5539 return hash_64(val, SWEVENT_HLIST_BITS);
5542 static inline struct hlist_head *
5543 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
5545 u64 hash = swevent_hash(type, event_id);
5547 return &hlist->heads[hash];
5550 /* For the read side: events when they trigger */
5551 static inline struct hlist_head *
5552 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
5554 struct swevent_hlist *hlist;
5556 hlist = rcu_dereference(swhash->swevent_hlist);
5557 if (!hlist)
5558 return NULL;
5560 return __find_swevent_head(hlist, type, event_id);
5563 /* For the event head insertion and removal in the hlist */
5564 static inline struct hlist_head *
5565 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
5567 struct swevent_hlist *hlist;
5568 u32 event_id = event->attr.config;
5569 u64 type = event->attr.type;
5572 * Event scheduling is always serialized against hlist allocation
5573 * and release. Which makes the protected version suitable here.
5574 * The context lock guarantees that.
5576 hlist = rcu_dereference_protected(swhash->swevent_hlist,
5577 lockdep_is_held(&event->ctx->lock));
5578 if (!hlist)
5579 return NULL;
5581 return __find_swevent_head(hlist, type, event_id);
5584 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5585 u64 nr,
5586 struct perf_sample_data *data,
5587 struct pt_regs *regs)
5589 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5590 struct perf_event *event;
5591 struct hlist_head *head;
5593 rcu_read_lock();
5594 head = find_swevent_head_rcu(swhash, type, event_id);
5595 if (!head)
5596 goto end;
5598 hlist_for_each_entry_rcu(event, head, hlist_entry) {
5599 if (perf_swevent_match(event, type, event_id, data, regs))
5600 perf_swevent_event(event, nr, data, regs);
5602 end:
5603 rcu_read_unlock();
5606 int perf_swevent_get_recursion_context(void)
5608 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5610 return get_recursion_context(swhash->recursion);
5612 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
5614 inline void perf_swevent_put_recursion_context(int rctx)
5616 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5618 put_recursion_context(swhash->recursion, rctx);
5621 void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
5623 struct perf_sample_data data;
5624 int rctx;
5626 preempt_disable_notrace();
5627 rctx = perf_swevent_get_recursion_context();
5628 if (rctx < 0)
5629 return;
5631 perf_sample_data_init(&data, addr, 0);
5633 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
5635 perf_swevent_put_recursion_context(rctx);
5636 preempt_enable_notrace();
5639 static void perf_swevent_read(struct perf_event *event)
5643 static int perf_swevent_add(struct perf_event *event, int flags)
5645 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
5646 struct hw_perf_event *hwc = &event->hw;
5647 struct hlist_head *head;
5649 if (is_sampling_event(event)) {
5650 hwc->last_period = hwc->sample_period;
5651 perf_swevent_set_period(event);
5654 hwc->state = !(flags & PERF_EF_START);
5656 head = find_swevent_head(swhash, event);
5657 if (WARN_ON_ONCE(!head))
5658 return -EINVAL;
5660 hlist_add_head_rcu(&event->hlist_entry, head);
5662 return 0;
5665 static void perf_swevent_del(struct perf_event *event, int flags)
5667 hlist_del_rcu(&event->hlist_entry);
5670 static void perf_swevent_start(struct perf_event *event, int flags)
5672 event->hw.state = 0;
5675 static void perf_swevent_stop(struct perf_event *event, int flags)
5677 event->hw.state = PERF_HES_STOPPED;
5680 /* Deref the hlist from the update side */
5681 static inline struct swevent_hlist *
5682 swevent_hlist_deref(struct swevent_htable *swhash)
5684 return rcu_dereference_protected(swhash->swevent_hlist,
5685 lockdep_is_held(&swhash->hlist_mutex));
5688 static void swevent_hlist_release(struct swevent_htable *swhash)
5690 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
5692 if (!hlist)
5693 return;
5695 rcu_assign_pointer(swhash->swevent_hlist, NULL);
5696 kfree_rcu(hlist, rcu_head);
5699 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5701 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5703 mutex_lock(&swhash->hlist_mutex);
5705 if (!--swhash->hlist_refcount)
5706 swevent_hlist_release(swhash);
5708 mutex_unlock(&swhash->hlist_mutex);
5711 static void swevent_hlist_put(struct perf_event *event)
5713 int cpu;
5715 for_each_possible_cpu(cpu)
5716 swevent_hlist_put_cpu(event, cpu);
5719 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5721 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
5722 int err = 0;
5724 mutex_lock(&swhash->hlist_mutex);
5726 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
5727 struct swevent_hlist *hlist;
5729 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5730 if (!hlist) {
5731 err = -ENOMEM;
5732 goto exit;
5734 rcu_assign_pointer(swhash->swevent_hlist, hlist);
5736 swhash->hlist_refcount++;
5737 exit:
5738 mutex_unlock(&swhash->hlist_mutex);
5740 return err;
5743 static int swevent_hlist_get(struct perf_event *event)
5745 int err;
5746 int cpu, failed_cpu;
5748 get_online_cpus();
5749 for_each_possible_cpu(cpu) {
5750 err = swevent_hlist_get_cpu(event, cpu);
5751 if (err) {
5752 failed_cpu = cpu;
5753 goto fail;
5756 put_online_cpus();
5758 return 0;
5759 fail:
5760 for_each_possible_cpu(cpu) {
5761 if (cpu == failed_cpu)
5762 break;
5763 swevent_hlist_put_cpu(event, cpu);
5766 put_online_cpus();
5767 return err;
5770 struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
5772 static void sw_perf_event_destroy(struct perf_event *event)
5774 u64 event_id = event->attr.config;
5776 WARN_ON(event->parent);
5778 static_key_slow_dec(&perf_swevent_enabled[event_id]);
5779 swevent_hlist_put(event);
5782 static int perf_swevent_init(struct perf_event *event)
5784 u64 event_id = event->attr.config;
5786 if (event->attr.type != PERF_TYPE_SOFTWARE)
5787 return -ENOENT;
5790 * no branch sampling for software events
5792 if (has_branch_stack(event))
5793 return -EOPNOTSUPP;
5795 switch (event_id) {
5796 case PERF_COUNT_SW_CPU_CLOCK:
5797 case PERF_COUNT_SW_TASK_CLOCK:
5798 return -ENOENT;
5800 default:
5801 break;
5804 if (event_id >= PERF_COUNT_SW_MAX)
5805 return -ENOENT;
5807 if (!event->parent) {
5808 int err;
5810 err = swevent_hlist_get(event);
5811 if (err)
5812 return err;
5814 static_key_slow_inc(&perf_swevent_enabled[event_id]);
5815 event->destroy = sw_perf_event_destroy;
5818 return 0;
5821 static int perf_swevent_event_idx(struct perf_event *event)
5823 return 0;
5826 static struct pmu perf_swevent = {
5827 .task_ctx_nr = perf_sw_context,
5829 .event_init = perf_swevent_init,
5830 .add = perf_swevent_add,
5831 .del = perf_swevent_del,
5832 .start = perf_swevent_start,
5833 .stop = perf_swevent_stop,
5834 .read = perf_swevent_read,
5836 .event_idx = perf_swevent_event_idx,
5839 #ifdef CONFIG_EVENT_TRACING
5841 static int perf_tp_filter_match(struct perf_event *event,
5842 struct perf_sample_data *data)
5844 void *record = data->raw->data;
5846 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5847 return 1;
5848 return 0;
5851 static int perf_tp_event_match(struct perf_event *event,
5852 struct perf_sample_data *data,
5853 struct pt_regs *regs)
5855 if (event->hw.state & PERF_HES_STOPPED)
5856 return 0;
5858 * All tracepoints are from kernel-space.
5860 if (event->attr.exclude_kernel)
5861 return 0;
5863 if (!perf_tp_filter_match(event, data))
5864 return 0;
5866 return 1;
5869 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
5870 struct pt_regs *regs, struct hlist_head *head, int rctx,
5871 struct task_struct *task)
5873 struct perf_sample_data data;
5874 struct perf_event *event;
5876 struct perf_raw_record raw = {
5877 .size = entry_size,
5878 .data = record,
5881 perf_sample_data_init(&data, addr, 0);
5882 data.raw = &raw;
5884 hlist_for_each_entry_rcu(event, head, hlist_entry) {
5885 if (perf_tp_event_match(event, &data, regs))
5886 perf_swevent_event(event, count, &data, regs);
5890 * If we got specified a target task, also iterate its context and
5891 * deliver this event there too.
5893 if (task && task != current) {
5894 struct perf_event_context *ctx;
5895 struct trace_entry *entry = record;
5897 rcu_read_lock();
5898 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
5899 if (!ctx)
5900 goto unlock;
5902 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5903 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5904 continue;
5905 if (event->attr.config != entry->type)
5906 continue;
5907 if (perf_tp_event_match(event, &data, regs))
5908 perf_swevent_event(event, count, &data, regs);
5910 unlock:
5911 rcu_read_unlock();
5914 perf_swevent_put_recursion_context(rctx);
5916 EXPORT_SYMBOL_GPL(perf_tp_event);
5918 static void tp_perf_event_destroy(struct perf_event *event)
5920 perf_trace_destroy(event);
5923 static int perf_tp_event_init(struct perf_event *event)
5925 int err;
5927 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5928 return -ENOENT;
5931 * no branch sampling for tracepoint events
5933 if (has_branch_stack(event))
5934 return -EOPNOTSUPP;
5936 err = perf_trace_init(event);
5937 if (err)
5938 return err;
5940 event->destroy = tp_perf_event_destroy;
5942 return 0;
5945 static struct pmu perf_tracepoint = {
5946 .task_ctx_nr = perf_sw_context,
5948 .event_init = perf_tp_event_init,
5949 .add = perf_trace_add,
5950 .del = perf_trace_del,
5951 .start = perf_swevent_start,
5952 .stop = perf_swevent_stop,
5953 .read = perf_swevent_read,
5955 .event_idx = perf_swevent_event_idx,
5958 static inline void perf_tp_register(void)
5960 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
5963 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5965 char *filter_str;
5966 int ret;
5968 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5969 return -EINVAL;
5971 filter_str = strndup_user(arg, PAGE_SIZE);
5972 if (IS_ERR(filter_str))
5973 return PTR_ERR(filter_str);
5975 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5977 kfree(filter_str);
5978 return ret;
5981 static void perf_event_free_filter(struct perf_event *event)
5983 ftrace_profile_free_filter(event);
5986 #else
5988 static inline void perf_tp_register(void)
5992 static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5994 return -ENOENT;
5997 static void perf_event_free_filter(struct perf_event *event)
6001 #endif /* CONFIG_EVENT_TRACING */
6003 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6004 void perf_bp_event(struct perf_event *bp, void *data)
6006 struct perf_sample_data sample;
6007 struct pt_regs *regs = data;
6009 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
6011 if (!bp->hw.state && !perf_exclude_event(bp, regs))
6012 perf_swevent_event(bp, 1, &sample, regs);
6014 #endif
6017 * hrtimer based swevent callback
6020 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
6022 enum hrtimer_restart ret = HRTIMER_RESTART;
6023 struct perf_sample_data data;
6024 struct pt_regs *regs;
6025 struct perf_event *event;
6026 u64 period;
6028 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
6030 if (event->state != PERF_EVENT_STATE_ACTIVE)
6031 return HRTIMER_NORESTART;
6033 event->pmu->read(event);
6035 perf_sample_data_init(&data, 0, event->hw.last_period);
6036 regs = get_irq_regs();
6038 if (regs && !perf_exclude_event(event, regs)) {
6039 if (!(event->attr.exclude_idle && is_idle_task(current)))
6040 if (__perf_event_overflow(event, 1, &data, regs))
6041 ret = HRTIMER_NORESTART;
6044 period = max_t(u64, 10000, event->hw.sample_period);
6045 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
6047 return ret;
6050 static void perf_swevent_start_hrtimer(struct perf_event *event)
6052 struct hw_perf_event *hwc = &event->hw;
6053 s64 period;
6055 if (!is_sampling_event(event))
6056 return;
6058 period = local64_read(&hwc->period_left);
6059 if (period) {
6060 if (period < 0)
6061 period = 10000;
6063 local64_set(&hwc->period_left, 0);
6064 } else {
6065 period = max_t(u64, 10000, hwc->sample_period);
6067 __hrtimer_start_range_ns(&hwc->hrtimer,
6068 ns_to_ktime(period), 0,
6069 HRTIMER_MODE_REL_PINNED, 0);
6072 static void perf_swevent_cancel_hrtimer(struct perf_event *event)
6074 struct hw_perf_event *hwc = &event->hw;
6076 if (is_sampling_event(event)) {
6077 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
6078 local64_set(&hwc->period_left, ktime_to_ns(remaining));
6080 hrtimer_cancel(&hwc->hrtimer);
6084 static void perf_swevent_init_hrtimer(struct perf_event *event)
6086 struct hw_perf_event *hwc = &event->hw;
6088 if (!is_sampling_event(event))
6089 return;
6091 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6092 hwc->hrtimer.function = perf_swevent_hrtimer;
6095 * Since hrtimers have a fixed rate, we can do a static freq->period
6096 * mapping and avoid the whole period adjust feedback stuff.
6098 if (event->attr.freq) {
6099 long freq = event->attr.sample_freq;
6101 event->attr.sample_period = NSEC_PER_SEC / freq;
6102 hwc->sample_period = event->attr.sample_period;
6103 local64_set(&hwc->period_left, hwc->sample_period);
6104 hwc->last_period = hwc->sample_period;
6105 event->attr.freq = 0;
6110 * Software event: cpu wall time clock
6113 static void cpu_clock_event_update(struct perf_event *event)
6115 s64 prev;
6116 u64 now;
6118 now = local_clock();
6119 prev = local64_xchg(&event->hw.prev_count, now);
6120 local64_add(now - prev, &event->count);
6123 static void cpu_clock_event_start(struct perf_event *event, int flags)
6125 local64_set(&event->hw.prev_count, local_clock());
6126 perf_swevent_start_hrtimer(event);
6129 static void cpu_clock_event_stop(struct perf_event *event, int flags)
6131 perf_swevent_cancel_hrtimer(event);
6132 cpu_clock_event_update(event);
6135 static int cpu_clock_event_add(struct perf_event *event, int flags)
6137 if (flags & PERF_EF_START)
6138 cpu_clock_event_start(event, flags);
6140 return 0;
6143 static void cpu_clock_event_del(struct perf_event *event, int flags)
6145 cpu_clock_event_stop(event, flags);
6148 static void cpu_clock_event_read(struct perf_event *event)
6150 cpu_clock_event_update(event);
6153 static int cpu_clock_event_init(struct perf_event *event)
6155 if (event->attr.type != PERF_TYPE_SOFTWARE)
6156 return -ENOENT;
6158 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
6159 return -ENOENT;
6162 * no branch sampling for software events
6164 if (has_branch_stack(event))
6165 return -EOPNOTSUPP;
6167 perf_swevent_init_hrtimer(event);
6169 return 0;
6172 static struct pmu perf_cpu_clock = {
6173 .task_ctx_nr = perf_sw_context,
6175 .event_init = cpu_clock_event_init,
6176 .add = cpu_clock_event_add,
6177 .del = cpu_clock_event_del,
6178 .start = cpu_clock_event_start,
6179 .stop = cpu_clock_event_stop,
6180 .read = cpu_clock_event_read,
6182 .event_idx = perf_swevent_event_idx,
6186 * Software event: task time clock
6189 static void task_clock_event_update(struct perf_event *event, u64 now)
6191 u64 prev;
6192 s64 delta;
6194 prev = local64_xchg(&event->hw.prev_count, now);
6195 delta = now - prev;
6196 local64_add(delta, &event->count);
6199 static void task_clock_event_start(struct perf_event *event, int flags)
6201 local64_set(&event->hw.prev_count, event->ctx->time);
6202 perf_swevent_start_hrtimer(event);
6205 static void task_clock_event_stop(struct perf_event *event, int flags)
6207 perf_swevent_cancel_hrtimer(event);
6208 task_clock_event_update(event, event->ctx->time);
6211 static int task_clock_event_add(struct perf_event *event, int flags)
6213 if (flags & PERF_EF_START)
6214 task_clock_event_start(event, flags);
6216 return 0;
6219 static void task_clock_event_del(struct perf_event *event, int flags)
6221 task_clock_event_stop(event, PERF_EF_UPDATE);
6224 static void task_clock_event_read(struct perf_event *event)
6226 u64 now = perf_clock();
6227 u64 delta = now - event->ctx->timestamp;
6228 u64 time = event->ctx->time + delta;
6230 task_clock_event_update(event, time);
6233 static int task_clock_event_init(struct perf_event *event)
6235 if (event->attr.type != PERF_TYPE_SOFTWARE)
6236 return -ENOENT;
6238 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
6239 return -ENOENT;
6242 * no branch sampling for software events
6244 if (has_branch_stack(event))
6245 return -EOPNOTSUPP;
6247 perf_swevent_init_hrtimer(event);
6249 return 0;
6252 static struct pmu perf_task_clock = {
6253 .task_ctx_nr = perf_sw_context,
6255 .event_init = task_clock_event_init,
6256 .add = task_clock_event_add,
6257 .del = task_clock_event_del,
6258 .start = task_clock_event_start,
6259 .stop = task_clock_event_stop,
6260 .read = task_clock_event_read,
6262 .event_idx = perf_swevent_event_idx,
6265 static void perf_pmu_nop_void(struct pmu *pmu)
6269 static int perf_pmu_nop_int(struct pmu *pmu)
6271 return 0;
6274 static void perf_pmu_start_txn(struct pmu *pmu)
6276 perf_pmu_disable(pmu);
6279 static int perf_pmu_commit_txn(struct pmu *pmu)
6281 perf_pmu_enable(pmu);
6282 return 0;
6285 static void perf_pmu_cancel_txn(struct pmu *pmu)
6287 perf_pmu_enable(pmu);
6290 static int perf_event_idx_default(struct perf_event *event)
6292 return event->hw.idx + 1;
6296 * Ensures all contexts with the same task_ctx_nr have the same
6297 * pmu_cpu_context too.
6299 static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
6301 struct pmu *pmu;
6303 if (ctxn < 0)
6304 return NULL;
6306 list_for_each_entry(pmu, &pmus, entry) {
6307 if (pmu->task_ctx_nr == ctxn)
6308 return pmu->pmu_cpu_context;
6311 return NULL;
6314 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
6316 int cpu;
6318 for_each_possible_cpu(cpu) {
6319 struct perf_cpu_context *cpuctx;
6321 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6323 if (cpuctx->unique_pmu == old_pmu)
6324 cpuctx->unique_pmu = pmu;
6328 static void free_pmu_context(struct pmu *pmu)
6330 struct pmu *i;
6332 mutex_lock(&pmus_lock);
6334 * Like a real lame refcount.
6336 list_for_each_entry(i, &pmus, entry) {
6337 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
6338 update_pmu_context(i, pmu);
6339 goto out;
6343 free_percpu(pmu->pmu_cpu_context);
6344 out:
6345 mutex_unlock(&pmus_lock);
6347 static struct idr pmu_idr;
6349 static ssize_t
6350 type_show(struct device *dev, struct device_attribute *attr, char *page)
6352 struct pmu *pmu = dev_get_drvdata(dev);
6354 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6356 static DEVICE_ATTR_RO(type);
6358 static ssize_t
6359 perf_event_mux_interval_ms_show(struct device *dev,
6360 struct device_attribute *attr,
6361 char *page)
6363 struct pmu *pmu = dev_get_drvdata(dev);
6365 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6368 static ssize_t
6369 perf_event_mux_interval_ms_store(struct device *dev,
6370 struct device_attribute *attr,
6371 const char *buf, size_t count)
6373 struct pmu *pmu = dev_get_drvdata(dev);
6374 int timer, cpu, ret;
6376 ret = kstrtoint(buf, 0, &timer);
6377 if (ret)
6378 return ret;
6380 if (timer < 1)
6381 return -EINVAL;
6383 /* same value, noting to do */
6384 if (timer == pmu->hrtimer_interval_ms)
6385 return count;
6387 pmu->hrtimer_interval_ms = timer;
6389 /* update all cpuctx for this PMU */
6390 for_each_possible_cpu(cpu) {
6391 struct perf_cpu_context *cpuctx;
6392 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6393 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6395 if (hrtimer_active(&cpuctx->hrtimer))
6396 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6399 return count;
6401 static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
6403 static struct attribute *pmu_dev_attrs[] = {
6404 &dev_attr_type.attr,
6405 &dev_attr_perf_event_mux_interval_ms.attr,
6406 NULL,
6408 ATTRIBUTE_GROUPS(pmu_dev);
6410 static int pmu_bus_running;
6411 static struct bus_type pmu_bus = {
6412 .name = "event_source",
6413 .dev_groups = pmu_dev_groups,
6416 static void pmu_dev_release(struct device *dev)
6418 kfree(dev);
6421 static int pmu_dev_alloc(struct pmu *pmu)
6423 int ret = -ENOMEM;
6425 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6426 if (!pmu->dev)
6427 goto out;
6429 pmu->dev->groups = pmu->attr_groups;
6430 device_initialize(pmu->dev);
6431 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6432 if (ret)
6433 goto free_dev;
6435 dev_set_drvdata(pmu->dev, pmu);
6436 pmu->dev->bus = &pmu_bus;
6437 pmu->dev->release = pmu_dev_release;
6438 ret = device_add(pmu->dev);
6439 if (ret)
6440 goto free_dev;
6442 out:
6443 return ret;
6445 free_dev:
6446 put_device(pmu->dev);
6447 goto out;
6450 static struct lock_class_key cpuctx_mutex;
6451 static struct lock_class_key cpuctx_lock;
6453 int perf_pmu_register(struct pmu *pmu, const char *name, int type)
6455 int cpu, ret;
6457 mutex_lock(&pmus_lock);
6458 ret = -ENOMEM;
6459 pmu->pmu_disable_count = alloc_percpu(int);
6460 if (!pmu->pmu_disable_count)
6461 goto unlock;
6463 pmu->type = -1;
6464 if (!name)
6465 goto skip_type;
6466 pmu->name = name;
6468 if (type < 0) {
6469 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6470 if (type < 0) {
6471 ret = type;
6472 goto free_pdc;
6475 pmu->type = type;
6477 if (pmu_bus_running) {
6478 ret = pmu_dev_alloc(pmu);
6479 if (ret)
6480 goto free_idr;
6483 skip_type:
6484 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6485 if (pmu->pmu_cpu_context)
6486 goto got_cpu_context;
6488 ret = -ENOMEM;
6489 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6490 if (!pmu->pmu_cpu_context)
6491 goto free_dev;
6493 for_each_possible_cpu(cpu) {
6494 struct perf_cpu_context *cpuctx;
6496 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6497 __perf_event_init_context(&cpuctx->ctx);
6498 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
6499 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
6500 cpuctx->ctx.type = cpu_context;
6501 cpuctx->ctx.pmu = pmu;
6503 __perf_cpu_hrtimer_init(cpuctx, cpu);
6505 INIT_LIST_HEAD(&cpuctx->rotation_list);
6506 cpuctx->unique_pmu = pmu;
6509 got_cpu_context:
6510 if (!pmu->start_txn) {
6511 if (pmu->pmu_enable) {
6513 * If we have pmu_enable/pmu_disable calls, install
6514 * transaction stubs that use that to try and batch
6515 * hardware accesses.
6517 pmu->start_txn = perf_pmu_start_txn;
6518 pmu->commit_txn = perf_pmu_commit_txn;
6519 pmu->cancel_txn = perf_pmu_cancel_txn;
6520 } else {
6521 pmu->start_txn = perf_pmu_nop_void;
6522 pmu->commit_txn = perf_pmu_nop_int;
6523 pmu->cancel_txn = perf_pmu_nop_void;
6527 if (!pmu->pmu_enable) {
6528 pmu->pmu_enable = perf_pmu_nop_void;
6529 pmu->pmu_disable = perf_pmu_nop_void;
6532 if (!pmu->event_idx)
6533 pmu->event_idx = perf_event_idx_default;
6535 list_add_rcu(&pmu->entry, &pmus);
6536 ret = 0;
6537 unlock:
6538 mutex_unlock(&pmus_lock);
6540 return ret;
6542 free_dev:
6543 device_del(pmu->dev);
6544 put_device(pmu->dev);
6546 free_idr:
6547 if (pmu->type >= PERF_TYPE_MAX)
6548 idr_remove(&pmu_idr, pmu->type);
6550 free_pdc:
6551 free_percpu(pmu->pmu_disable_count);
6552 goto unlock;
6555 void perf_pmu_unregister(struct pmu *pmu)
6557 mutex_lock(&pmus_lock);
6558 list_del_rcu(&pmu->entry);
6559 mutex_unlock(&pmus_lock);
6562 * We dereference the pmu list under both SRCU and regular RCU, so
6563 * synchronize against both of those.
6565 synchronize_srcu(&pmus_srcu);
6566 synchronize_rcu();
6568 free_percpu(pmu->pmu_disable_count);
6569 if (pmu->type >= PERF_TYPE_MAX)
6570 idr_remove(&pmu_idr, pmu->type);
6571 device_del(pmu->dev);
6572 put_device(pmu->dev);
6573 free_pmu_context(pmu);
6576 struct pmu *perf_init_event(struct perf_event *event)
6578 struct pmu *pmu = NULL;
6579 int idx;
6580 int ret;
6582 idx = srcu_read_lock(&pmus_srcu);
6584 rcu_read_lock();
6585 pmu = idr_find(&pmu_idr, event->attr.type);
6586 rcu_read_unlock();
6587 if (pmu) {
6588 event->pmu = pmu;
6589 ret = pmu->event_init(event);
6590 if (ret)
6591 pmu = ERR_PTR(ret);
6592 goto unlock;
6595 list_for_each_entry_rcu(pmu, &pmus, entry) {
6596 event->pmu = pmu;
6597 ret = pmu->event_init(event);
6598 if (!ret)
6599 goto unlock;
6601 if (ret != -ENOENT) {
6602 pmu = ERR_PTR(ret);
6603 goto unlock;
6606 pmu = ERR_PTR(-ENOENT);
6607 unlock:
6608 srcu_read_unlock(&pmus_srcu, idx);
6610 return pmu;
6613 static void account_event_cpu(struct perf_event *event, int cpu)
6615 if (event->parent)
6616 return;
6618 if (has_branch_stack(event)) {
6619 if (!(event->attach_state & PERF_ATTACH_TASK))
6620 atomic_inc(&per_cpu(perf_branch_stack_events, cpu));
6622 if (is_cgroup_event(event))
6623 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
6626 static void account_event(struct perf_event *event)
6628 if (event->parent)
6629 return;
6631 if (event->attach_state & PERF_ATTACH_TASK)
6632 static_key_slow_inc(&perf_sched_events.key);
6633 if (event->attr.mmap || event->attr.mmap_data)
6634 atomic_inc(&nr_mmap_events);
6635 if (event->attr.comm)
6636 atomic_inc(&nr_comm_events);
6637 if (event->attr.task)
6638 atomic_inc(&nr_task_events);
6639 if (event->attr.freq) {
6640 if (atomic_inc_return(&nr_freq_events) == 1)
6641 tick_nohz_full_kick_all();
6643 if (has_branch_stack(event))
6644 static_key_slow_inc(&perf_sched_events.key);
6645 if (is_cgroup_event(event))
6646 static_key_slow_inc(&perf_sched_events.key);
6648 account_event_cpu(event, event->cpu);
6652 * Allocate and initialize a event structure
6654 static struct perf_event *
6655 perf_event_alloc(struct perf_event_attr *attr, int cpu,
6656 struct task_struct *task,
6657 struct perf_event *group_leader,
6658 struct perf_event *parent_event,
6659 perf_overflow_handler_t overflow_handler,
6660 void *context)
6662 struct pmu *pmu;
6663 struct perf_event *event;
6664 struct hw_perf_event *hwc;
6665 long err = -EINVAL;
6667 if ((unsigned)cpu >= nr_cpu_ids) {
6668 if (!task || cpu != -1)
6669 return ERR_PTR(-EINVAL);
6672 event = kzalloc(sizeof(*event), GFP_KERNEL);
6673 if (!event)
6674 return ERR_PTR(-ENOMEM);
6677 * Single events are their own group leaders, with an
6678 * empty sibling list:
6680 if (!group_leader)
6681 group_leader = event;
6683 mutex_init(&event->child_mutex);
6684 INIT_LIST_HEAD(&event->child_list);
6686 INIT_LIST_HEAD(&event->group_entry);
6687 INIT_LIST_HEAD(&event->event_entry);
6688 INIT_LIST_HEAD(&event->sibling_list);
6689 INIT_LIST_HEAD(&event->rb_entry);
6690 INIT_LIST_HEAD(&event->active_entry);
6691 INIT_HLIST_NODE(&event->hlist_entry);
6694 init_waitqueue_head(&event->waitq);
6695 init_irq_work(&event->pending, perf_pending_event);
6697 mutex_init(&event->mmap_mutex);
6699 atomic_long_set(&event->refcount, 1);
6700 event->cpu = cpu;
6701 event->attr = *attr;
6702 event->group_leader = group_leader;
6703 event->pmu = NULL;
6704 event->oncpu = -1;
6706 event->parent = parent_event;
6708 event->ns = get_pid_ns(task_active_pid_ns(current));
6709 event->id = atomic64_inc_return(&perf_event_id);
6711 event->state = PERF_EVENT_STATE_INACTIVE;
6713 if (task) {
6714 event->attach_state = PERF_ATTACH_TASK;
6716 if (attr->type == PERF_TYPE_TRACEPOINT)
6717 event->hw.tp_target = task;
6718 #ifdef CONFIG_HAVE_HW_BREAKPOINT
6720 * hw_breakpoint is a bit difficult here..
6722 else if (attr->type == PERF_TYPE_BREAKPOINT)
6723 event->hw.bp_target = task;
6724 #endif
6727 if (!overflow_handler && parent_event) {
6728 overflow_handler = parent_event->overflow_handler;
6729 context = parent_event->overflow_handler_context;
6732 event->overflow_handler = overflow_handler;
6733 event->overflow_handler_context = context;
6735 perf_event__state_init(event);
6737 pmu = NULL;
6739 hwc = &event->hw;
6740 hwc->sample_period = attr->sample_period;
6741 if (attr->freq && attr->sample_freq)
6742 hwc->sample_period = 1;
6743 hwc->last_period = hwc->sample_period;
6745 local64_set(&hwc->period_left, hwc->sample_period);
6748 * we currently do not support PERF_FORMAT_GROUP on inherited events
6750 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6751 goto err_ns;
6753 pmu = perf_init_event(event);
6754 if (!pmu)
6755 goto err_ns;
6756 else if (IS_ERR(pmu)) {
6757 err = PTR_ERR(pmu);
6758 goto err_ns;
6761 if (!event->parent) {
6762 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6763 err = get_callchain_buffers();
6764 if (err)
6765 goto err_pmu;
6769 return event;
6771 err_pmu:
6772 if (event->destroy)
6773 event->destroy(event);
6774 err_ns:
6775 if (event->ns)
6776 put_pid_ns(event->ns);
6777 kfree(event);
6779 return ERR_PTR(err);
6782 static int perf_copy_attr(struct perf_event_attr __user *uattr,
6783 struct perf_event_attr *attr)
6785 u32 size;
6786 int ret;
6788 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6789 return -EFAULT;
6792 * zero the full structure, so that a short copy will be nice.
6794 memset(attr, 0, sizeof(*attr));
6796 ret = get_user(size, &uattr->size);
6797 if (ret)
6798 return ret;
6800 if (size > PAGE_SIZE) /* silly large */
6801 goto err_size;
6803 if (!size) /* abi compat */
6804 size = PERF_ATTR_SIZE_VER0;
6806 if (size < PERF_ATTR_SIZE_VER0)
6807 goto err_size;
6810 * If we're handed a bigger struct than we know of,
6811 * ensure all the unknown bits are 0 - i.e. new
6812 * user-space does not rely on any kernel feature
6813 * extensions we dont know about yet.
6815 if (size > sizeof(*attr)) {
6816 unsigned char __user *addr;
6817 unsigned char __user *end;
6818 unsigned char val;
6820 addr = (void __user *)uattr + sizeof(*attr);
6821 end = (void __user *)uattr + size;
6823 for (; addr < end; addr++) {
6824 ret = get_user(val, addr);
6825 if (ret)
6826 return ret;
6827 if (val)
6828 goto err_size;
6830 size = sizeof(*attr);
6833 ret = copy_from_user(attr, uattr, size);
6834 if (ret)
6835 return -EFAULT;
6837 /* disabled for now */
6838 if (attr->mmap2)
6839 return -EINVAL;
6841 if (attr->__reserved_1)
6842 return -EINVAL;
6844 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6845 return -EINVAL;
6847 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6848 return -EINVAL;
6850 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
6851 u64 mask = attr->branch_sample_type;
6853 /* only using defined bits */
6854 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6855 return -EINVAL;
6857 /* at least one branch bit must be set */
6858 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6859 return -EINVAL;
6861 /* propagate priv level, when not set for branch */
6862 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6864 /* exclude_kernel checked on syscall entry */
6865 if (!attr->exclude_kernel)
6866 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6868 if (!attr->exclude_user)
6869 mask |= PERF_SAMPLE_BRANCH_USER;
6871 if (!attr->exclude_hv)
6872 mask |= PERF_SAMPLE_BRANCH_HV;
6874 * adjust user setting (for HW filter setup)
6876 attr->branch_sample_type = mask;
6878 /* privileged levels capture (kernel, hv): check permissions */
6879 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
6880 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6881 return -EACCES;
6884 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
6885 ret = perf_reg_validate(attr->sample_regs_user);
6886 if (ret)
6887 return ret;
6890 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
6891 if (!arch_perf_have_user_stack_dump())
6892 return -ENOSYS;
6895 * We have __u32 type for the size, but so far
6896 * we can only use __u16 as maximum due to the
6897 * __u16 sample size limit.
6899 if (attr->sample_stack_user >= USHRT_MAX)
6900 ret = -EINVAL;
6901 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
6902 ret = -EINVAL;
6905 out:
6906 return ret;
6908 err_size:
6909 put_user(sizeof(*attr), &uattr->size);
6910 ret = -E2BIG;
6911 goto out;
6914 static int
6915 perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
6917 struct ring_buffer *rb = NULL, *old_rb = NULL;
6918 int ret = -EINVAL;
6920 if (!output_event)
6921 goto set;
6923 /* don't allow circular references */
6924 if (event == output_event)
6925 goto out;
6928 * Don't allow cross-cpu buffers
6930 if (output_event->cpu != event->cpu)
6931 goto out;
6934 * If its not a per-cpu rb, it must be the same task.
6936 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6937 goto out;
6939 set:
6940 mutex_lock(&event->mmap_mutex);
6941 /* Can't redirect output if we've got an active mmap() */
6942 if (atomic_read(&event->mmap_count))
6943 goto unlock;
6945 old_rb = event->rb;
6947 if (output_event) {
6948 /* get the rb we want to redirect to */
6949 rb = ring_buffer_get(output_event);
6950 if (!rb)
6951 goto unlock;
6954 if (old_rb)
6955 ring_buffer_detach(event, old_rb);
6957 if (rb)
6958 ring_buffer_attach(event, rb);
6960 rcu_assign_pointer(event->rb, rb);
6962 if (old_rb) {
6963 ring_buffer_put(old_rb);
6965 * Since we detached before setting the new rb, so that we
6966 * could attach the new rb, we could have missed a wakeup.
6967 * Provide it now.
6969 wake_up_all(&event->waitq);
6972 ret = 0;
6973 unlock:
6974 mutex_unlock(&event->mmap_mutex);
6976 out:
6977 return ret;
6981 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6983 * @attr_uptr: event_id type attributes for monitoring/sampling
6984 * @pid: target pid
6985 * @cpu: target cpu
6986 * @group_fd: group leader event fd
6988 SYSCALL_DEFINE5(perf_event_open,
6989 struct perf_event_attr __user *, attr_uptr,
6990 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6992 struct perf_event *group_leader = NULL, *output_event = NULL;
6993 struct perf_event *event, *sibling;
6994 struct perf_event_attr attr;
6995 struct perf_event_context *ctx;
6996 struct file *event_file = NULL;
6997 struct fd group = {NULL, 0};
6998 struct task_struct *task = NULL;
6999 struct pmu *pmu;
7000 int event_fd;
7001 int move_group = 0;
7002 int err;
7003 int f_flags = O_RDWR;
7005 /* for future expandability... */
7006 if (flags & ~PERF_FLAG_ALL)
7007 return -EINVAL;
7009 err = perf_copy_attr(attr_uptr, &attr);
7010 if (err)
7011 return err;
7013 if (!attr.exclude_kernel) {
7014 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7015 return -EACCES;
7018 if (attr.freq) {
7019 if (attr.sample_freq > sysctl_perf_event_sample_rate)
7020 return -EINVAL;
7024 * In cgroup mode, the pid argument is used to pass the fd
7025 * opened to the cgroup directory in cgroupfs. The cpu argument
7026 * designates the cpu on which to monitor threads from that
7027 * cgroup.
7029 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
7030 return -EINVAL;
7032 if (flags & PERF_FLAG_FD_CLOEXEC)
7033 f_flags |= O_CLOEXEC;
7035 event_fd = get_unused_fd_flags(f_flags);
7036 if (event_fd < 0)
7037 return event_fd;
7039 if (group_fd != -1) {
7040 err = perf_fget_light(group_fd, &group);
7041 if (err)
7042 goto err_fd;
7043 group_leader = group.file->private_data;
7044 if (flags & PERF_FLAG_FD_OUTPUT)
7045 output_event = group_leader;
7046 if (flags & PERF_FLAG_FD_NO_GROUP)
7047 group_leader = NULL;
7050 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
7051 task = find_lively_task_by_vpid(pid);
7052 if (IS_ERR(task)) {
7053 err = PTR_ERR(task);
7054 goto err_group_fd;
7058 get_online_cpus();
7060 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
7061 NULL, NULL);
7062 if (IS_ERR(event)) {
7063 err = PTR_ERR(event);
7064 goto err_task;
7067 if (flags & PERF_FLAG_PID_CGROUP) {
7068 err = perf_cgroup_connect(pid, event, &attr, group_leader);
7069 if (err) {
7070 __free_event(event);
7071 goto err_task;
7075 account_event(event);
7078 * Special case software events and allow them to be part of
7079 * any hardware group.
7081 pmu = event->pmu;
7083 if (group_leader &&
7084 (is_software_event(event) != is_software_event(group_leader))) {
7085 if (is_software_event(event)) {
7087 * If event and group_leader are not both a software
7088 * event, and event is, then group leader is not.
7090 * Allow the addition of software events to !software
7091 * groups, this is safe because software events never
7092 * fail to schedule.
7094 pmu = group_leader->pmu;
7095 } else if (is_software_event(group_leader) &&
7096 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
7098 * In case the group is a pure software group, and we
7099 * try to add a hardware event, move the whole group to
7100 * the hardware context.
7102 move_group = 1;
7107 * Get the target context (task or percpu):
7109 ctx = find_get_context(pmu, task, event->cpu);
7110 if (IS_ERR(ctx)) {
7111 err = PTR_ERR(ctx);
7112 goto err_alloc;
7115 if (task) {
7116 put_task_struct(task);
7117 task = NULL;
7121 * Look up the group leader (we will attach this event to it):
7123 if (group_leader) {
7124 err = -EINVAL;
7127 * Do not allow a recursive hierarchy (this new sibling
7128 * becoming part of another group-sibling):
7130 if (group_leader->group_leader != group_leader)
7131 goto err_context;
7133 * Do not allow to attach to a group in a different
7134 * task or CPU context:
7136 if (move_group) {
7137 if (group_leader->ctx->type != ctx->type)
7138 goto err_context;
7139 } else {
7140 if (group_leader->ctx != ctx)
7141 goto err_context;
7145 * Only a group leader can be exclusive or pinned
7147 if (attr.exclusive || attr.pinned)
7148 goto err_context;
7151 if (output_event) {
7152 err = perf_event_set_output(event, output_event);
7153 if (err)
7154 goto err_context;
7157 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
7158 f_flags);
7159 if (IS_ERR(event_file)) {
7160 err = PTR_ERR(event_file);
7161 goto err_context;
7164 if (move_group) {
7165 struct perf_event_context *gctx = group_leader->ctx;
7167 mutex_lock(&gctx->mutex);
7168 perf_remove_from_context(group_leader);
7171 * Removing from the context ends up with disabled
7172 * event. What we want here is event in the initial
7173 * startup state, ready to be add into new context.
7175 perf_event__state_init(group_leader);
7176 list_for_each_entry(sibling, &group_leader->sibling_list,
7177 group_entry) {
7178 perf_remove_from_context(sibling);
7179 perf_event__state_init(sibling);
7180 put_ctx(gctx);
7182 mutex_unlock(&gctx->mutex);
7183 put_ctx(gctx);
7186 WARN_ON_ONCE(ctx->parent_ctx);
7187 mutex_lock(&ctx->mutex);
7189 if (move_group) {
7190 synchronize_rcu();
7191 perf_install_in_context(ctx, group_leader, event->cpu);
7192 get_ctx(ctx);
7193 list_for_each_entry(sibling, &group_leader->sibling_list,
7194 group_entry) {
7195 perf_install_in_context(ctx, sibling, event->cpu);
7196 get_ctx(ctx);
7200 perf_install_in_context(ctx, event, event->cpu);
7201 perf_unpin_context(ctx);
7202 mutex_unlock(&ctx->mutex);
7204 put_online_cpus();
7206 event->owner = current;
7208 mutex_lock(&current->perf_event_mutex);
7209 list_add_tail(&event->owner_entry, &current->perf_event_list);
7210 mutex_unlock(&current->perf_event_mutex);
7213 * Precalculate sample_data sizes
7215 perf_event__header_size(event);
7216 perf_event__id_header_size(event);
7219 * Drop the reference on the group_event after placing the
7220 * new event on the sibling_list. This ensures destruction
7221 * of the group leader will find the pointer to itself in
7222 * perf_group_detach().
7224 fdput(group);
7225 fd_install(event_fd, event_file);
7226 return event_fd;
7228 err_context:
7229 perf_unpin_context(ctx);
7230 put_ctx(ctx);
7231 err_alloc:
7232 free_event(event);
7233 err_task:
7234 put_online_cpus();
7235 if (task)
7236 put_task_struct(task);
7237 err_group_fd:
7238 fdput(group);
7239 err_fd:
7240 put_unused_fd(event_fd);
7241 return err;
7245 * perf_event_create_kernel_counter
7247 * @attr: attributes of the counter to create
7248 * @cpu: cpu in which the counter is bound
7249 * @task: task to profile (NULL for percpu)
7251 struct perf_event *
7252 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
7253 struct task_struct *task,
7254 perf_overflow_handler_t overflow_handler,
7255 void *context)
7257 struct perf_event_context *ctx;
7258 struct perf_event *event;
7259 int err;
7262 * Get the target context (task or percpu):
7265 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
7266 overflow_handler, context);
7267 if (IS_ERR(event)) {
7268 err = PTR_ERR(event);
7269 goto err;
7272 account_event(event);
7274 ctx = find_get_context(event->pmu, task, cpu);
7275 if (IS_ERR(ctx)) {
7276 err = PTR_ERR(ctx);
7277 goto err_free;
7280 WARN_ON_ONCE(ctx->parent_ctx);
7281 mutex_lock(&ctx->mutex);
7282 perf_install_in_context(ctx, event, cpu);
7283 perf_unpin_context(ctx);
7284 mutex_unlock(&ctx->mutex);
7286 return event;
7288 err_free:
7289 free_event(event);
7290 err:
7291 return ERR_PTR(err);
7293 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
7295 void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
7297 struct perf_event_context *src_ctx;
7298 struct perf_event_context *dst_ctx;
7299 struct perf_event *event, *tmp;
7300 LIST_HEAD(events);
7302 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
7303 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
7305 mutex_lock(&src_ctx->mutex);
7306 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
7307 event_entry) {
7308 perf_remove_from_context(event);
7309 unaccount_event_cpu(event, src_cpu);
7310 put_ctx(src_ctx);
7311 list_add(&event->migrate_entry, &events);
7313 mutex_unlock(&src_ctx->mutex);
7315 synchronize_rcu();
7317 mutex_lock(&dst_ctx->mutex);
7318 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7319 list_del(&event->migrate_entry);
7320 if (event->state >= PERF_EVENT_STATE_OFF)
7321 event->state = PERF_EVENT_STATE_INACTIVE;
7322 account_event_cpu(event, dst_cpu);
7323 perf_install_in_context(dst_ctx, event, dst_cpu);
7324 get_ctx(dst_ctx);
7326 mutex_unlock(&dst_ctx->mutex);
7328 EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
7330 static void sync_child_event(struct perf_event *child_event,
7331 struct task_struct *child)
7333 struct perf_event *parent_event = child_event->parent;
7334 u64 child_val;
7336 if (child_event->attr.inherit_stat)
7337 perf_event_read_event(child_event, child);
7339 child_val = perf_event_count(child_event);
7342 * Add back the child's count to the parent's count:
7344 atomic64_add(child_val, &parent_event->child_count);
7345 atomic64_add(child_event->total_time_enabled,
7346 &parent_event->child_total_time_enabled);
7347 atomic64_add(child_event->total_time_running,
7348 &parent_event->child_total_time_running);
7351 * Remove this event from the parent's list
7353 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7354 mutex_lock(&parent_event->child_mutex);
7355 list_del_init(&child_event->child_list);
7356 mutex_unlock(&parent_event->child_mutex);
7359 * Release the parent event, if this was the last
7360 * reference to it.
7362 put_event(parent_event);
7365 static void
7366 __perf_event_exit_task(struct perf_event *child_event,
7367 struct perf_event_context *child_ctx,
7368 struct task_struct *child)
7370 if (child_event->parent) {
7371 raw_spin_lock_irq(&child_ctx->lock);
7372 perf_group_detach(child_event);
7373 raw_spin_unlock_irq(&child_ctx->lock);
7376 perf_remove_from_context(child_event);
7379 * It can happen that the parent exits first, and has events
7380 * that are still around due to the child reference. These
7381 * events need to be zapped.
7383 if (child_event->parent) {
7384 sync_child_event(child_event, child);
7385 free_event(child_event);
7389 static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
7391 struct perf_event *child_event, *tmp;
7392 struct perf_event_context *child_ctx;
7393 unsigned long flags;
7395 if (likely(!child->perf_event_ctxp[ctxn])) {
7396 perf_event_task(child, NULL, 0);
7397 return;
7400 local_irq_save(flags);
7402 * We can't reschedule here because interrupts are disabled,
7403 * and either child is current or it is a task that can't be
7404 * scheduled, so we are now safe from rescheduling changing
7405 * our context.
7407 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
7410 * Take the context lock here so that if find_get_context is
7411 * reading child->perf_event_ctxp, we wait until it has
7412 * incremented the context's refcount before we do put_ctx below.
7414 raw_spin_lock(&child_ctx->lock);
7415 task_ctx_sched_out(child_ctx);
7416 child->perf_event_ctxp[ctxn] = NULL;
7418 * If this context is a clone; unclone it so it can't get
7419 * swapped to another process while we're removing all
7420 * the events from it.
7422 unclone_ctx(child_ctx);
7423 update_context_time(child_ctx);
7424 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
7427 * Report the task dead after unscheduling the events so that we
7428 * won't get any samples after PERF_RECORD_EXIT. We can however still
7429 * get a few PERF_RECORD_READ events.
7431 perf_event_task(child, child_ctx, 0);
7434 * We can recurse on the same lock type through:
7436 * __perf_event_exit_task()
7437 * sync_child_event()
7438 * put_event()
7439 * mutex_lock(&ctx->mutex)
7441 * But since its the parent context it won't be the same instance.
7443 mutex_lock(&child_ctx->mutex);
7445 again:
7446 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
7447 group_entry)
7448 __perf_event_exit_task(child_event, child_ctx, child);
7450 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
7451 group_entry)
7452 __perf_event_exit_task(child_event, child_ctx, child);
7455 * If the last event was a group event, it will have appended all
7456 * its siblings to the list, but we obtained 'tmp' before that which
7457 * will still point to the list head terminating the iteration.
7459 if (!list_empty(&child_ctx->pinned_groups) ||
7460 !list_empty(&child_ctx->flexible_groups))
7461 goto again;
7463 mutex_unlock(&child_ctx->mutex);
7465 put_ctx(child_ctx);
7469 * When a child task exits, feed back event values to parent events.
7471 void perf_event_exit_task(struct task_struct *child)
7473 struct perf_event *event, *tmp;
7474 int ctxn;
7476 mutex_lock(&child->perf_event_mutex);
7477 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
7478 owner_entry) {
7479 list_del_init(&event->owner_entry);
7482 * Ensure the list deletion is visible before we clear
7483 * the owner, closes a race against perf_release() where
7484 * we need to serialize on the owner->perf_event_mutex.
7486 smp_wmb();
7487 event->owner = NULL;
7489 mutex_unlock(&child->perf_event_mutex);
7491 for_each_task_context_nr(ctxn)
7492 perf_event_exit_task_context(child, ctxn);
7495 static void perf_free_event(struct perf_event *event,
7496 struct perf_event_context *ctx)
7498 struct perf_event *parent = event->parent;
7500 if (WARN_ON_ONCE(!parent))
7501 return;
7503 mutex_lock(&parent->child_mutex);
7504 list_del_init(&event->child_list);
7505 mutex_unlock(&parent->child_mutex);
7507 put_event(parent);
7509 perf_group_detach(event);
7510 list_del_event(event, ctx);
7511 free_event(event);
7515 * free an unexposed, unused context as created by inheritance by
7516 * perf_event_init_task below, used by fork() in case of fail.
7518 void perf_event_free_task(struct task_struct *task)
7520 struct perf_event_context *ctx;
7521 struct perf_event *event, *tmp;
7522 int ctxn;
7524 for_each_task_context_nr(ctxn) {
7525 ctx = task->perf_event_ctxp[ctxn];
7526 if (!ctx)
7527 continue;
7529 mutex_lock(&ctx->mutex);
7530 again:
7531 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
7532 group_entry)
7533 perf_free_event(event, ctx);
7535 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
7536 group_entry)
7537 perf_free_event(event, ctx);
7539 if (!list_empty(&ctx->pinned_groups) ||
7540 !list_empty(&ctx->flexible_groups))
7541 goto again;
7543 mutex_unlock(&ctx->mutex);
7545 put_ctx(ctx);
7549 void perf_event_delayed_put(struct task_struct *task)
7551 int ctxn;
7553 for_each_task_context_nr(ctxn)
7554 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
7558 * inherit a event from parent task to child task:
7560 static struct perf_event *
7561 inherit_event(struct perf_event *parent_event,
7562 struct task_struct *parent,
7563 struct perf_event_context *parent_ctx,
7564 struct task_struct *child,
7565 struct perf_event *group_leader,
7566 struct perf_event_context *child_ctx)
7568 struct perf_event *child_event;
7569 unsigned long flags;
7572 * Instead of creating recursive hierarchies of events,
7573 * we link inherited events back to the original parent,
7574 * which has a filp for sure, which we use as the reference
7575 * count:
7577 if (parent_event->parent)
7578 parent_event = parent_event->parent;
7580 child_event = perf_event_alloc(&parent_event->attr,
7581 parent_event->cpu,
7582 child,
7583 group_leader, parent_event,
7584 NULL, NULL);
7585 if (IS_ERR(child_event))
7586 return child_event;
7588 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
7589 free_event(child_event);
7590 return NULL;
7593 get_ctx(child_ctx);
7596 * Make the child state follow the state of the parent event,
7597 * not its attr.disabled bit. We hold the parent's mutex,
7598 * so we won't race with perf_event_{en, dis}able_family.
7600 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
7601 child_event->state = PERF_EVENT_STATE_INACTIVE;
7602 else
7603 child_event->state = PERF_EVENT_STATE_OFF;
7605 if (parent_event->attr.freq) {
7606 u64 sample_period = parent_event->hw.sample_period;
7607 struct hw_perf_event *hwc = &child_event->hw;
7609 hwc->sample_period = sample_period;
7610 hwc->last_period = sample_period;
7612 local64_set(&hwc->period_left, sample_period);
7615 child_event->ctx = child_ctx;
7616 child_event->overflow_handler = parent_event->overflow_handler;
7617 child_event->overflow_handler_context
7618 = parent_event->overflow_handler_context;
7621 * Precalculate sample_data sizes
7623 perf_event__header_size(child_event);
7624 perf_event__id_header_size(child_event);
7627 * Link it up in the child's context:
7629 raw_spin_lock_irqsave(&child_ctx->lock, flags);
7630 add_event_to_ctx(child_event, child_ctx);
7631 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
7634 * Link this into the parent event's child list
7636 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7637 mutex_lock(&parent_event->child_mutex);
7638 list_add_tail(&child_event->child_list, &parent_event->child_list);
7639 mutex_unlock(&parent_event->child_mutex);
7641 return child_event;
7644 static int inherit_group(struct perf_event *parent_event,
7645 struct task_struct *parent,
7646 struct perf_event_context *parent_ctx,
7647 struct task_struct *child,
7648 struct perf_event_context *child_ctx)
7650 struct perf_event *leader;
7651 struct perf_event *sub;
7652 struct perf_event *child_ctr;
7654 leader = inherit_event(parent_event, parent, parent_ctx,
7655 child, NULL, child_ctx);
7656 if (IS_ERR(leader))
7657 return PTR_ERR(leader);
7658 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7659 child_ctr = inherit_event(sub, parent, parent_ctx,
7660 child, leader, child_ctx);
7661 if (IS_ERR(child_ctr))
7662 return PTR_ERR(child_ctr);
7664 return 0;
7667 static int
7668 inherit_task_group(struct perf_event *event, struct task_struct *parent,
7669 struct perf_event_context *parent_ctx,
7670 struct task_struct *child, int ctxn,
7671 int *inherited_all)
7673 int ret;
7674 struct perf_event_context *child_ctx;
7676 if (!event->attr.inherit) {
7677 *inherited_all = 0;
7678 return 0;
7681 child_ctx = child->perf_event_ctxp[ctxn];
7682 if (!child_ctx) {
7684 * This is executed from the parent task context, so
7685 * inherit events that have been marked for cloning.
7686 * First allocate and initialize a context for the
7687 * child.
7690 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
7691 if (!child_ctx)
7692 return -ENOMEM;
7694 child->perf_event_ctxp[ctxn] = child_ctx;
7697 ret = inherit_group(event, parent, parent_ctx,
7698 child, child_ctx);
7700 if (ret)
7701 *inherited_all = 0;
7703 return ret;
7707 * Initialize the perf_event context in task_struct
7709 int perf_event_init_context(struct task_struct *child, int ctxn)
7711 struct perf_event_context *child_ctx, *parent_ctx;
7712 struct perf_event_context *cloned_ctx;
7713 struct perf_event *event;
7714 struct task_struct *parent = current;
7715 int inherited_all = 1;
7716 unsigned long flags;
7717 int ret = 0;
7719 if (likely(!parent->perf_event_ctxp[ctxn]))
7720 return 0;
7723 * If the parent's context is a clone, pin it so it won't get
7724 * swapped under us.
7726 parent_ctx = perf_pin_task_context(parent, ctxn);
7729 * No need to check if parent_ctx != NULL here; since we saw
7730 * it non-NULL earlier, the only reason for it to become NULL
7731 * is if we exit, and since we're currently in the middle of
7732 * a fork we can't be exiting at the same time.
7736 * Lock the parent list. No need to lock the child - not PID
7737 * hashed yet and not running, so nobody can access it.
7739 mutex_lock(&parent_ctx->mutex);
7742 * We dont have to disable NMIs - we are only looking at
7743 * the list, not manipulating it:
7745 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
7746 ret = inherit_task_group(event, parent, parent_ctx,
7747 child, ctxn, &inherited_all);
7748 if (ret)
7749 break;
7753 * We can't hold ctx->lock when iterating the ->flexible_group list due
7754 * to allocations, but we need to prevent rotation because
7755 * rotate_ctx() will change the list from interrupt context.
7757 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7758 parent_ctx->rotate_disable = 1;
7759 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7761 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
7762 ret = inherit_task_group(event, parent, parent_ctx,
7763 child, ctxn, &inherited_all);
7764 if (ret)
7765 break;
7768 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7769 parent_ctx->rotate_disable = 0;
7771 child_ctx = child->perf_event_ctxp[ctxn];
7773 if (child_ctx && inherited_all) {
7775 * Mark the child context as a clone of the parent
7776 * context, or of whatever the parent is a clone of.
7778 * Note that if the parent is a clone, the holding of
7779 * parent_ctx->lock avoids it from being uncloned.
7781 cloned_ctx = parent_ctx->parent_ctx;
7782 if (cloned_ctx) {
7783 child_ctx->parent_ctx = cloned_ctx;
7784 child_ctx->parent_gen = parent_ctx->parent_gen;
7785 } else {
7786 child_ctx->parent_ctx = parent_ctx;
7787 child_ctx->parent_gen = parent_ctx->generation;
7789 get_ctx(child_ctx->parent_ctx);
7792 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7793 mutex_unlock(&parent_ctx->mutex);
7795 perf_unpin_context(parent_ctx);
7796 put_ctx(parent_ctx);
7798 return ret;
7802 * Initialize the perf_event context in task_struct
7804 int perf_event_init_task(struct task_struct *child)
7806 int ctxn, ret;
7808 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7809 mutex_init(&child->perf_event_mutex);
7810 INIT_LIST_HEAD(&child->perf_event_list);
7812 for_each_task_context_nr(ctxn) {
7813 ret = perf_event_init_context(child, ctxn);
7814 if (ret)
7815 return ret;
7818 return 0;
7821 static void __init perf_event_init_all_cpus(void)
7823 struct swevent_htable *swhash;
7824 int cpu;
7826 for_each_possible_cpu(cpu) {
7827 swhash = &per_cpu(swevent_htable, cpu);
7828 mutex_init(&swhash->hlist_mutex);
7829 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
7833 static void perf_event_init_cpu(int cpu)
7835 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7837 mutex_lock(&swhash->hlist_mutex);
7838 if (swhash->hlist_refcount > 0) {
7839 struct swevent_hlist *hlist;
7841 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7842 WARN_ON(!hlist);
7843 rcu_assign_pointer(swhash->swevent_hlist, hlist);
7845 mutex_unlock(&swhash->hlist_mutex);
7848 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
7849 static void perf_pmu_rotate_stop(struct pmu *pmu)
7851 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7853 WARN_ON(!irqs_disabled());
7855 list_del_init(&cpuctx->rotation_list);
7858 static void __perf_event_exit_context(void *__info)
7860 struct perf_event_context *ctx = __info;
7861 struct perf_event *event;
7863 perf_pmu_rotate_stop(ctx->pmu);
7865 rcu_read_lock();
7866 list_for_each_entry_rcu(event, &ctx->event_list, event_entry)
7867 __perf_remove_from_context(event);
7868 rcu_read_unlock();
7871 static void perf_event_exit_cpu_context(int cpu)
7873 struct perf_event_context *ctx;
7874 struct pmu *pmu;
7875 int idx;
7877 idx = srcu_read_lock(&pmus_srcu);
7878 list_for_each_entry_rcu(pmu, &pmus, entry) {
7879 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
7881 mutex_lock(&ctx->mutex);
7882 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7883 mutex_unlock(&ctx->mutex);
7885 srcu_read_unlock(&pmus_srcu, idx);
7888 static void perf_event_exit_cpu(int cpu)
7890 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
7892 perf_event_exit_cpu_context(cpu);
7894 mutex_lock(&swhash->hlist_mutex);
7895 swevent_hlist_release(swhash);
7896 mutex_unlock(&swhash->hlist_mutex);
7898 #else
7899 static inline void perf_event_exit_cpu(int cpu) { }
7900 #endif
7902 static int
7903 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7905 int cpu;
7907 for_each_online_cpu(cpu)
7908 perf_event_exit_cpu(cpu);
7910 return NOTIFY_OK;
7914 * Run the perf reboot notifier at the very last possible moment so that
7915 * the generic watchdog code runs as long as possible.
7917 static struct notifier_block perf_reboot_notifier = {
7918 .notifier_call = perf_reboot,
7919 .priority = INT_MIN,
7922 static int
7923 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7925 unsigned int cpu = (long)hcpu;
7927 switch (action & ~CPU_TASKS_FROZEN) {
7929 case CPU_UP_PREPARE:
7930 case CPU_DOWN_FAILED:
7931 perf_event_init_cpu(cpu);
7932 break;
7934 case CPU_UP_CANCELED:
7935 case CPU_DOWN_PREPARE:
7936 perf_event_exit_cpu(cpu);
7937 break;
7938 default:
7939 break;
7942 return NOTIFY_OK;
7945 void __init perf_event_init(void)
7947 int ret;
7949 idr_init(&pmu_idr);
7951 perf_event_init_all_cpus();
7952 init_srcu_struct(&pmus_srcu);
7953 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7954 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7955 perf_pmu_register(&perf_task_clock, NULL, -1);
7956 perf_tp_register();
7957 perf_cpu_notifier(perf_cpu_notify);
7958 register_reboot_notifier(&perf_reboot_notifier);
7960 ret = init_hw_breakpoint();
7961 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
7963 /* do not patch jump label more than once per second */
7964 jump_label_rate_limit(&perf_sched_events, HZ);
7967 * Build time assertion that we keep the data_head at the intended
7968 * location. IOW, validation we got the __reserved[] size right.
7970 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
7971 != 1024);
7974 static int __init perf_event_sysfs_init(void)
7976 struct pmu *pmu;
7977 int ret;
7979 mutex_lock(&pmus_lock);
7981 ret = bus_register(&pmu_bus);
7982 if (ret)
7983 goto unlock;
7985 list_for_each_entry(pmu, &pmus, entry) {
7986 if (!pmu->name || pmu->type < 0)
7987 continue;
7989 ret = pmu_dev_alloc(pmu);
7990 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7992 pmu_bus_running = 1;
7993 ret = 0;
7995 unlock:
7996 mutex_unlock(&pmus_lock);
7998 return ret;
8000 device_initcall(perf_event_sysfs_init);
8002 #ifdef CONFIG_CGROUP_PERF
8003 static struct cgroup_subsys_state *
8004 perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
8006 struct perf_cgroup *jc;
8008 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
8009 if (!jc)
8010 return ERR_PTR(-ENOMEM);
8012 jc->info = alloc_percpu(struct perf_cgroup_info);
8013 if (!jc->info) {
8014 kfree(jc);
8015 return ERR_PTR(-ENOMEM);
8018 return &jc->css;
8021 static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
8023 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
8025 free_percpu(jc->info);
8026 kfree(jc);
8029 static int __perf_cgroup_move(void *info)
8031 struct task_struct *task = info;
8032 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
8033 return 0;
8036 static void perf_cgroup_attach(struct cgroup_subsys_state *css,
8037 struct cgroup_taskset *tset)
8039 struct task_struct *task;
8041 cgroup_taskset_for_each(task, tset)
8042 task_function_call(task, __perf_cgroup_move, task);
8045 static void perf_cgroup_exit(struct cgroup_subsys_state *css,
8046 struct cgroup_subsys_state *old_css,
8047 struct task_struct *task)
8050 * cgroup_exit() is called in the copy_process() failure path.
8051 * Ignore this case since the task hasn't ran yet, this avoids
8052 * trying to poke a half freed task state from generic code.
8054 if (!(task->flags & PF_EXITING))
8055 return;
8057 task_function_call(task, __perf_cgroup_move, task);
8060 struct cgroup_subsys perf_event_cgrp_subsys = {
8061 .css_alloc = perf_cgroup_css_alloc,
8062 .css_free = perf_cgroup_css_free,
8063 .exit = perf_cgroup_exit,
8064 .attach = perf_cgroup_attach,
8066 #endif /* CONFIG_CGROUP_PERF */