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
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/idr.h>
17 #include <linux/file.h>
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/hash.h>
21 #include <linux/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/cgroup.h>
38 #include <linux/perf_event.h>
39 #include <linux/trace_events.h>
40 #include <linux/hw_breakpoint.h>
41 #include <linux/mm_types.h>
42 #include <linux/module.h>
43 #include <linux/mman.h>
44 #include <linux/compat.h>
45 #include <linux/bpf.h>
46 #include <linux/filter.h>
47 #include <linux/namei.h>
48 #include <linux/parser.h>
49 #include <linux/sched/clock.h>
50 #include <linux/sched/mm.h>
51 #include <linux/proc_ns.h>
52 #include <linux/mount.h>
56 #include <asm/irq_regs.h>
58 typedef int (*remote_function_f)(void *);
60 struct remote_function_call {
61 struct task_struct *p;
62 remote_function_f func;
67 static void remote_function(void *data)
69 struct remote_function_call *tfc = data;
70 struct task_struct *p = tfc->p;
74 if (task_cpu(p) != smp_processor_id())
78 * Now that we're on right CPU with IRQs disabled, we can test
79 * if we hit the right task without races.
82 tfc->ret = -ESRCH; /* No such (running) process */
87 tfc->ret = tfc->func(tfc->info);
91 * task_function_call - call a function on the cpu on which a task runs
92 * @p: the task to evaluate
93 * @func: the function to be called
94 * @info: the function call argument
96 * Calls the function @func when the task is currently running. This might
97 * be on the current CPU, which just calls the function directly
99 * returns: @func return value, or
100 * -ESRCH - when the process isn't running
101 * -EAGAIN - when the process moved away
104 task_function_call(struct task_struct *p, remote_function_f func, void *info)
106 struct remote_function_call data = {
115 ret = smp_call_function_single(task_cpu(p), remote_function, &data, 1);
118 } while (ret == -EAGAIN);
124 * cpu_function_call - call a function on the cpu
125 * @func: the function to be called
126 * @info: the function call argument
128 * Calls the function @func on the remote cpu.
130 * returns: @func return value or -ENXIO when the cpu is offline
132 static int cpu_function_call(int cpu, remote_function_f func, void *info)
134 struct remote_function_call data = {
138 .ret = -ENXIO, /* No such CPU */
141 smp_call_function_single(cpu, remote_function, &data, 1);
146 static inline struct perf_cpu_context *
147 __get_cpu_context(struct perf_event_context *ctx)
149 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
152 static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
153 struct perf_event_context *ctx)
155 raw_spin_lock(&cpuctx->ctx.lock);
157 raw_spin_lock(&ctx->lock);
160 static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
161 struct perf_event_context *ctx)
164 raw_spin_unlock(&ctx->lock);
165 raw_spin_unlock(&cpuctx->ctx.lock);
168 #define TASK_TOMBSTONE ((void *)-1L)
170 static bool is_kernel_event(struct perf_event *event)
172 return READ_ONCE(event->owner) == TASK_TOMBSTONE;
176 * On task ctx scheduling...
178 * When !ctx->nr_events a task context will not be scheduled. This means
179 * we can disable the scheduler hooks (for performance) without leaving
180 * pending task ctx state.
182 * This however results in two special cases:
184 * - removing the last event from a task ctx; this is relatively straight
185 * forward and is done in __perf_remove_from_context.
187 * - adding the first event to a task ctx; this is tricky because we cannot
188 * rely on ctx->is_active and therefore cannot use event_function_call().
189 * See perf_install_in_context().
191 * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set.
194 typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *,
195 struct perf_event_context *, void *);
197 struct event_function_struct {
198 struct perf_event *event;
203 static int event_function(void *info)
205 struct event_function_struct *efs = info;
206 struct perf_event *event = efs->event;
207 struct perf_event_context *ctx = event->ctx;
208 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
209 struct perf_event_context *task_ctx = cpuctx->task_ctx;
212 lockdep_assert_irqs_disabled();
214 perf_ctx_lock(cpuctx, task_ctx);
216 * Since we do the IPI call without holding ctx->lock things can have
217 * changed, double check we hit the task we set out to hit.
220 if (ctx->task != current) {
226 * We only use event_function_call() on established contexts,
227 * and event_function() is only ever called when active (or
228 * rather, we'll have bailed in task_function_call() or the
229 * above ctx->task != current test), therefore we must have
230 * ctx->is_active here.
232 WARN_ON_ONCE(!ctx->is_active);
234 * And since we have ctx->is_active, cpuctx->task_ctx must
237 WARN_ON_ONCE(task_ctx != ctx);
239 WARN_ON_ONCE(&cpuctx->ctx != ctx);
242 efs->func(event, cpuctx, ctx, efs->data);
244 perf_ctx_unlock(cpuctx, task_ctx);
249 static void event_function_call(struct perf_event *event, event_f func, void *data)
251 struct perf_event_context *ctx = event->ctx;
252 struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */
253 struct event_function_struct efs = {
259 if (!event->parent) {
261 * If this is a !child event, we must hold ctx::mutex to
262 * stabilize the the event->ctx relation. See
263 * perf_event_ctx_lock().
265 lockdep_assert_held(&ctx->mutex);
269 cpu_function_call(event->cpu, event_function, &efs);
273 if (task == TASK_TOMBSTONE)
277 if (!task_function_call(task, event_function, &efs))
280 raw_spin_lock_irq(&ctx->lock);
282 * Reload the task pointer, it might have been changed by
283 * a concurrent perf_event_context_sched_out().
286 if (task == TASK_TOMBSTONE) {
287 raw_spin_unlock_irq(&ctx->lock);
290 if (ctx->is_active) {
291 raw_spin_unlock_irq(&ctx->lock);
294 func(event, NULL, ctx, data);
295 raw_spin_unlock_irq(&ctx->lock);
299 * Similar to event_function_call() + event_function(), but hard assumes IRQs
300 * are already disabled and we're on the right CPU.
302 static void event_function_local(struct perf_event *event, event_f func, void *data)
304 struct perf_event_context *ctx = event->ctx;
305 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
306 struct task_struct *task = READ_ONCE(ctx->task);
307 struct perf_event_context *task_ctx = NULL;
309 lockdep_assert_irqs_disabled();
312 if (task == TASK_TOMBSTONE)
318 perf_ctx_lock(cpuctx, task_ctx);
321 if (task == TASK_TOMBSTONE)
326 * We must be either inactive or active and the right task,
327 * otherwise we're screwed, since we cannot IPI to somewhere
330 if (ctx->is_active) {
331 if (WARN_ON_ONCE(task != current))
334 if (WARN_ON_ONCE(cpuctx->task_ctx != ctx))
338 WARN_ON_ONCE(&cpuctx->ctx != ctx);
341 func(event, cpuctx, ctx, data);
343 perf_ctx_unlock(cpuctx, task_ctx);
346 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
347 PERF_FLAG_FD_OUTPUT |\
348 PERF_FLAG_PID_CGROUP |\
349 PERF_FLAG_FD_CLOEXEC)
352 * branch priv levels that need permission checks
354 #define PERF_SAMPLE_BRANCH_PERM_PLM \
355 (PERF_SAMPLE_BRANCH_KERNEL |\
356 PERF_SAMPLE_BRANCH_HV)
359 EVENT_FLEXIBLE = 0x1,
362 /* see ctx_resched() for details */
364 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
368 * perf_sched_events : >0 events exist
369 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
372 static void perf_sched_delayed(struct work_struct *work);
373 DEFINE_STATIC_KEY_FALSE(perf_sched_events);
374 static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed);
375 static DEFINE_MUTEX(perf_sched_mutex);
376 static atomic_t perf_sched_count;
378 static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
379 static DEFINE_PER_CPU(int, perf_sched_cb_usages);
380 static DEFINE_PER_CPU(struct pmu_event_list, pmu_sb_events);
382 static atomic_t nr_mmap_events __read_mostly;
383 static atomic_t nr_comm_events __read_mostly;
384 static atomic_t nr_namespaces_events __read_mostly;
385 static atomic_t nr_task_events __read_mostly;
386 static atomic_t nr_freq_events __read_mostly;
387 static atomic_t nr_switch_events __read_mostly;
389 static LIST_HEAD(pmus);
390 static DEFINE_MUTEX(pmus_lock);
391 static struct srcu_struct pmus_srcu;
392 static cpumask_var_t perf_online_mask;
395 * perf event paranoia level:
396 * -1 - not paranoid at all
397 * 0 - disallow raw tracepoint access for unpriv
398 * 1 - disallow cpu events for unpriv
399 * 2 - disallow kernel profiling for unpriv
401 int sysctl_perf_event_paranoid __read_mostly = 2;
403 /* Minimum for 512 kiB + 1 user control page */
404 int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
407 * max perf event sample rate
409 #define DEFAULT_MAX_SAMPLE_RATE 100000
410 #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
411 #define DEFAULT_CPU_TIME_MAX_PERCENT 25
413 int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
415 static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
416 static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
418 static int perf_sample_allowed_ns __read_mostly =
419 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
421 static void update_perf_cpu_limits(void)
423 u64 tmp = perf_sample_period_ns;
425 tmp *= sysctl_perf_cpu_time_max_percent;
426 tmp = div_u64(tmp, 100);
430 WRITE_ONCE(perf_sample_allowed_ns, tmp);
433 static int perf_rotate_context(struct perf_cpu_context *cpuctx);
435 int perf_proc_update_handler(struct ctl_table *table, int write,
436 void __user *buffer, size_t *lenp,
439 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
445 * If throttling is disabled don't allow the write:
447 if (sysctl_perf_cpu_time_max_percent == 100 ||
448 sysctl_perf_cpu_time_max_percent == 0)
451 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
452 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
453 update_perf_cpu_limits();
458 int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
460 int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
461 void __user *buffer, size_t *lenp,
464 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
469 if (sysctl_perf_cpu_time_max_percent == 100 ||
470 sysctl_perf_cpu_time_max_percent == 0) {
472 "perf: Dynamic interrupt throttling disabled, can hang your system!\n");
473 WRITE_ONCE(perf_sample_allowed_ns, 0);
475 update_perf_cpu_limits();
482 * perf samples are done in some very critical code paths (NMIs).
483 * If they take too much CPU time, the system can lock up and not
484 * get any real work done. This will drop the sample rate when
485 * we detect that events are taking too long.
487 #define NR_ACCUMULATED_SAMPLES 128
488 static DEFINE_PER_CPU(u64, running_sample_length);
490 static u64 __report_avg;
491 static u64 __report_allowed;
493 static void perf_duration_warn(struct irq_work *w)
495 printk_ratelimited(KERN_INFO
496 "perf: interrupt took too long (%lld > %lld), lowering "
497 "kernel.perf_event_max_sample_rate to %d\n",
498 __report_avg, __report_allowed,
499 sysctl_perf_event_sample_rate);
502 static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
504 void perf_sample_event_took(u64 sample_len_ns)
506 u64 max_len = READ_ONCE(perf_sample_allowed_ns);
514 /* Decay the counter by 1 average sample. */
515 running_len = __this_cpu_read(running_sample_length);
516 running_len -= running_len/NR_ACCUMULATED_SAMPLES;
517 running_len += sample_len_ns;
518 __this_cpu_write(running_sample_length, running_len);
521 * Note: this will be biased artifically low until we have
522 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
523 * from having to maintain a count.
525 avg_len = running_len/NR_ACCUMULATED_SAMPLES;
526 if (avg_len <= max_len)
529 __report_avg = avg_len;
530 __report_allowed = max_len;
533 * Compute a throttle threshold 25% below the current duration.
535 avg_len += avg_len / 4;
536 max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent;
542 WRITE_ONCE(perf_sample_allowed_ns, avg_len);
543 WRITE_ONCE(max_samples_per_tick, max);
545 sysctl_perf_event_sample_rate = max * HZ;
546 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
548 if (!irq_work_queue(&perf_duration_work)) {
549 early_printk("perf: interrupt took too long (%lld > %lld), lowering "
550 "kernel.perf_event_max_sample_rate to %d\n",
551 __report_avg, __report_allowed,
552 sysctl_perf_event_sample_rate);
556 static atomic64_t perf_event_id;
558 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
559 enum event_type_t event_type);
561 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
562 enum event_type_t event_type,
563 struct task_struct *task);
565 static void update_context_time(struct perf_event_context *ctx);
566 static u64 perf_event_time(struct perf_event *event);
568 void __weak perf_event_print_debug(void) { }
570 extern __weak const char *perf_pmu_name(void)
575 static inline u64 perf_clock(void)
577 return local_clock();
580 static inline u64 perf_event_clock(struct perf_event *event)
582 return event->clock();
586 * State based event timekeeping...
588 * The basic idea is to use event->state to determine which (if any) time
589 * fields to increment with the current delta. This means we only need to
590 * update timestamps when we change state or when they are explicitly requested
593 * Event groups make things a little more complicated, but not terribly so. The
594 * rules for a group are that if the group leader is OFF the entire group is
595 * OFF, irrespecive of what the group member states are. This results in
596 * __perf_effective_state().
598 * A futher ramification is that when a group leader flips between OFF and
599 * !OFF, we need to update all group member times.
602 * NOTE: perf_event_time() is based on the (cgroup) context time, and thus we
603 * need to make sure the relevant context time is updated before we try and
604 * update our timestamps.
607 static __always_inline enum perf_event_state
608 __perf_effective_state(struct perf_event *event)
610 struct perf_event *leader = event->group_leader;
612 if (leader->state <= PERF_EVENT_STATE_OFF)
613 return leader->state;
618 static __always_inline void
619 __perf_update_times(struct perf_event *event, u64 now, u64 *enabled, u64 *running)
621 enum perf_event_state state = __perf_effective_state(event);
622 u64 delta = now - event->tstamp;
624 *enabled = event->total_time_enabled;
625 if (state >= PERF_EVENT_STATE_INACTIVE)
628 *running = event->total_time_running;
629 if (state >= PERF_EVENT_STATE_ACTIVE)
633 static void perf_event_update_time(struct perf_event *event)
635 u64 now = perf_event_time(event);
637 __perf_update_times(event, now, &event->total_time_enabled,
638 &event->total_time_running);
642 static void perf_event_update_sibling_time(struct perf_event *leader)
644 struct perf_event *sibling;
646 list_for_each_entry(sibling, &leader->sibling_list, group_entry)
647 perf_event_update_time(sibling);
651 perf_event_set_state(struct perf_event *event, enum perf_event_state state)
653 if (event->state == state)
656 perf_event_update_time(event);
658 * If a group leader gets enabled/disabled all its siblings
661 if ((event->state < 0) ^ (state < 0))
662 perf_event_update_sibling_time(event);
664 WRITE_ONCE(event->state, state);
667 #ifdef CONFIG_CGROUP_PERF
670 perf_cgroup_match(struct perf_event *event)
672 struct perf_event_context *ctx = event->ctx;
673 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
675 /* @event doesn't care about cgroup */
679 /* wants specific cgroup scope but @cpuctx isn't associated with any */
684 * Cgroup scoping is recursive. An event enabled for a cgroup is
685 * also enabled for all its descendant cgroups. If @cpuctx's
686 * cgroup is a descendant of @event's (the test covers identity
687 * case), it's a match.
689 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
690 event->cgrp->css.cgroup);
693 static inline void perf_detach_cgroup(struct perf_event *event)
695 css_put(&event->cgrp->css);
699 static inline int is_cgroup_event(struct perf_event *event)
701 return event->cgrp != NULL;
704 static inline u64 perf_cgroup_event_time(struct perf_event *event)
706 struct perf_cgroup_info *t;
708 t = per_cpu_ptr(event->cgrp->info, event->cpu);
712 static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
714 struct perf_cgroup_info *info;
719 info = this_cpu_ptr(cgrp->info);
721 info->time += now - info->timestamp;
722 info->timestamp = now;
725 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
727 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
729 __update_cgrp_time(cgrp_out);
732 static inline void update_cgrp_time_from_event(struct perf_event *event)
734 struct perf_cgroup *cgrp;
737 * ensure we access cgroup data only when needed and
738 * when we know the cgroup is pinned (css_get)
740 if (!is_cgroup_event(event))
743 cgrp = perf_cgroup_from_task(current, event->ctx);
745 * Do not update time when cgroup is not active
747 if (cgroup_is_descendant(cgrp->css.cgroup, event->cgrp->css.cgroup))
748 __update_cgrp_time(event->cgrp);
752 perf_cgroup_set_timestamp(struct task_struct *task,
753 struct perf_event_context *ctx)
755 struct perf_cgroup *cgrp;
756 struct perf_cgroup_info *info;
759 * ctx->lock held by caller
760 * ensure we do not access cgroup data
761 * unless we have the cgroup pinned (css_get)
763 if (!task || !ctx->nr_cgroups)
766 cgrp = perf_cgroup_from_task(task, ctx);
767 info = this_cpu_ptr(cgrp->info);
768 info->timestamp = ctx->timestamp;
771 static DEFINE_PER_CPU(struct list_head, cgrp_cpuctx_list);
773 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
774 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
777 * reschedule events based on the cgroup constraint of task.
779 * mode SWOUT : schedule out everything
780 * mode SWIN : schedule in based on cgroup for next
782 static void perf_cgroup_switch(struct task_struct *task, int mode)
784 struct perf_cpu_context *cpuctx;
785 struct list_head *list;
789 * Disable interrupts and preemption to avoid this CPU's
790 * cgrp_cpuctx_entry to change under us.
792 local_irq_save(flags);
794 list = this_cpu_ptr(&cgrp_cpuctx_list);
795 list_for_each_entry(cpuctx, list, cgrp_cpuctx_entry) {
796 WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0);
798 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
799 perf_pmu_disable(cpuctx->ctx.pmu);
801 if (mode & PERF_CGROUP_SWOUT) {
802 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
804 * must not be done before ctxswout due
805 * to event_filter_match() in event_sched_out()
810 if (mode & PERF_CGROUP_SWIN) {
811 WARN_ON_ONCE(cpuctx->cgrp);
813 * set cgrp before ctxsw in to allow
814 * event_filter_match() to not have to pass
816 * we pass the cpuctx->ctx to perf_cgroup_from_task()
817 * because cgorup events are only per-cpu
819 cpuctx->cgrp = perf_cgroup_from_task(task,
821 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
823 perf_pmu_enable(cpuctx->ctx.pmu);
824 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
827 local_irq_restore(flags);
830 static inline void perf_cgroup_sched_out(struct task_struct *task,
831 struct task_struct *next)
833 struct perf_cgroup *cgrp1;
834 struct perf_cgroup *cgrp2 = NULL;
838 * we come here when we know perf_cgroup_events > 0
839 * we do not need to pass the ctx here because we know
840 * we are holding the rcu lock
842 cgrp1 = perf_cgroup_from_task(task, NULL);
843 cgrp2 = perf_cgroup_from_task(next, NULL);
846 * only schedule out current cgroup events if we know
847 * that we are switching to a different cgroup. Otherwise,
848 * do no touch the cgroup events.
851 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
856 static inline void perf_cgroup_sched_in(struct task_struct *prev,
857 struct task_struct *task)
859 struct perf_cgroup *cgrp1;
860 struct perf_cgroup *cgrp2 = NULL;
864 * we come here when we know perf_cgroup_events > 0
865 * we do not need to pass the ctx here because we know
866 * we are holding the rcu lock
868 cgrp1 = perf_cgroup_from_task(task, NULL);
869 cgrp2 = perf_cgroup_from_task(prev, NULL);
872 * only need to schedule in cgroup events if we are changing
873 * cgroup during ctxsw. Cgroup events were not scheduled
874 * out of ctxsw out if that was not the case.
877 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
882 static inline int perf_cgroup_connect(int fd, struct perf_event *event,
883 struct perf_event_attr *attr,
884 struct perf_event *group_leader)
886 struct perf_cgroup *cgrp;
887 struct cgroup_subsys_state *css;
888 struct fd f = fdget(fd);
894 css = css_tryget_online_from_dir(f.file->f_path.dentry,
895 &perf_event_cgrp_subsys);
901 cgrp = container_of(css, struct perf_cgroup, css);
905 * all events in a group must monitor
906 * the same cgroup because a task belongs
907 * to only one perf cgroup at a time
909 if (group_leader && group_leader->cgrp != cgrp) {
910 perf_detach_cgroup(event);
919 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
921 struct perf_cgroup_info *t;
922 t = per_cpu_ptr(event->cgrp->info, event->cpu);
923 event->shadow_ctx_time = now - t->timestamp;
927 * Update cpuctx->cgrp so that it is set when first cgroup event is added and
928 * cleared when last cgroup event is removed.
931 list_update_cgroup_event(struct perf_event *event,
932 struct perf_event_context *ctx, bool add)
934 struct perf_cpu_context *cpuctx;
935 struct list_head *cpuctx_entry;
937 if (!is_cgroup_event(event))
940 if (add && ctx->nr_cgroups++)
942 else if (!add && --ctx->nr_cgroups)
945 * Because cgroup events are always per-cpu events,
946 * this will always be called from the right CPU.
948 cpuctx = __get_cpu_context(ctx);
949 cpuctx_entry = &cpuctx->cgrp_cpuctx_entry;
950 /* cpuctx->cgrp is NULL unless a cgroup event is active in this CPU .*/
952 struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx);
954 list_add(cpuctx_entry, this_cpu_ptr(&cgrp_cpuctx_list));
955 if (cgroup_is_descendant(cgrp->css.cgroup, event->cgrp->css.cgroup))
958 list_del(cpuctx_entry);
963 #else /* !CONFIG_CGROUP_PERF */
966 perf_cgroup_match(struct perf_event *event)
971 static inline void perf_detach_cgroup(struct perf_event *event)
974 static inline int is_cgroup_event(struct perf_event *event)
979 static inline void update_cgrp_time_from_event(struct perf_event *event)
983 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
987 static inline void perf_cgroup_sched_out(struct task_struct *task,
988 struct task_struct *next)
992 static inline void perf_cgroup_sched_in(struct task_struct *prev,
993 struct task_struct *task)
997 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
998 struct perf_event_attr *attr,
999 struct perf_event *group_leader)
1005 perf_cgroup_set_timestamp(struct task_struct *task,
1006 struct perf_event_context *ctx)
1011 perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
1016 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
1020 static inline u64 perf_cgroup_event_time(struct perf_event *event)
1026 list_update_cgroup_event(struct perf_event *event,
1027 struct perf_event_context *ctx, bool add)
1034 * set default to be dependent on timer tick just
1035 * like original code
1037 #define PERF_CPU_HRTIMER (1000 / HZ)
1039 * function must be called with interrupts disabled
1041 static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
1043 struct perf_cpu_context *cpuctx;
1046 lockdep_assert_irqs_disabled();
1048 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
1049 rotations = perf_rotate_context(cpuctx);
1051 raw_spin_lock(&cpuctx->hrtimer_lock);
1053 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
1055 cpuctx->hrtimer_active = 0;
1056 raw_spin_unlock(&cpuctx->hrtimer_lock);
1058 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
1061 static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
1063 struct hrtimer *timer = &cpuctx->hrtimer;
1064 struct pmu *pmu = cpuctx->ctx.pmu;
1067 /* no multiplexing needed for SW PMU */
1068 if (pmu->task_ctx_nr == perf_sw_context)
1072 * check default is sane, if not set then force to
1073 * default interval (1/tick)
1075 interval = pmu->hrtimer_interval_ms;
1077 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
1079 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
1081 raw_spin_lock_init(&cpuctx->hrtimer_lock);
1082 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
1083 timer->function = perf_mux_hrtimer_handler;
1086 static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
1088 struct hrtimer *timer = &cpuctx->hrtimer;
1089 struct pmu *pmu = cpuctx->ctx.pmu;
1090 unsigned long flags;
1092 /* not for SW PMU */
1093 if (pmu->task_ctx_nr == perf_sw_context)
1096 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
1097 if (!cpuctx->hrtimer_active) {
1098 cpuctx->hrtimer_active = 1;
1099 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
1100 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
1102 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
1107 void perf_pmu_disable(struct pmu *pmu)
1109 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1111 pmu->pmu_disable(pmu);
1114 void perf_pmu_enable(struct pmu *pmu)
1116 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1118 pmu->pmu_enable(pmu);
1121 static DEFINE_PER_CPU(struct list_head, active_ctx_list);
1124 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
1125 * perf_event_task_tick() are fully serialized because they're strictly cpu
1126 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
1127 * disabled, while perf_event_task_tick is called from IRQ context.
1129 static void perf_event_ctx_activate(struct perf_event_context *ctx)
1131 struct list_head *head = this_cpu_ptr(&active_ctx_list);
1133 lockdep_assert_irqs_disabled();
1135 WARN_ON(!list_empty(&ctx->active_ctx_list));
1137 list_add(&ctx->active_ctx_list, head);
1140 static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
1142 lockdep_assert_irqs_disabled();
1144 WARN_ON(list_empty(&ctx->active_ctx_list));
1146 list_del_init(&ctx->active_ctx_list);
1149 static void get_ctx(struct perf_event_context *ctx)
1151 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
1154 static void free_ctx(struct rcu_head *head)
1156 struct perf_event_context *ctx;
1158 ctx = container_of(head, struct perf_event_context, rcu_head);
1159 kfree(ctx->task_ctx_data);
1163 static void put_ctx(struct perf_event_context *ctx)
1165 if (atomic_dec_and_test(&ctx->refcount)) {
1166 if (ctx->parent_ctx)
1167 put_ctx(ctx->parent_ctx);
1168 if (ctx->task && ctx->task != TASK_TOMBSTONE)
1169 put_task_struct(ctx->task);
1170 call_rcu(&ctx->rcu_head, free_ctx);
1175 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1176 * perf_pmu_migrate_context() we need some magic.
1178 * Those places that change perf_event::ctx will hold both
1179 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1181 * Lock ordering is by mutex address. There are two other sites where
1182 * perf_event_context::mutex nests and those are:
1184 * - perf_event_exit_task_context() [ child , 0 ]
1185 * perf_event_exit_event()
1186 * put_event() [ parent, 1 ]
1188 * - perf_event_init_context() [ parent, 0 ]
1189 * inherit_task_group()
1192 * perf_event_alloc()
1194 * perf_try_init_event() [ child , 1 ]
1196 * While it appears there is an obvious deadlock here -- the parent and child
1197 * nesting levels are inverted between the two. This is in fact safe because
1198 * life-time rules separate them. That is an exiting task cannot fork, and a
1199 * spawning task cannot (yet) exit.
1201 * But remember that that these are parent<->child context relations, and
1202 * migration does not affect children, therefore these two orderings should not
1205 * The change in perf_event::ctx does not affect children (as claimed above)
1206 * because the sys_perf_event_open() case will install a new event and break
1207 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1208 * concerned with cpuctx and that doesn't have children.
1210 * The places that change perf_event::ctx will issue:
1212 * perf_remove_from_context();
1213 * synchronize_rcu();
1214 * perf_install_in_context();
1216 * to affect the change. The remove_from_context() + synchronize_rcu() should
1217 * quiesce the event, after which we can install it in the new location. This
1218 * means that only external vectors (perf_fops, prctl) can perturb the event
1219 * while in transit. Therefore all such accessors should also acquire
1220 * perf_event_context::mutex to serialize against this.
1222 * However; because event->ctx can change while we're waiting to acquire
1223 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1228 * task_struct::perf_event_mutex
1229 * perf_event_context::mutex
1230 * perf_event::child_mutex;
1231 * perf_event_context::lock
1232 * perf_event::mmap_mutex
1235 static struct perf_event_context *
1236 perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
1238 struct perf_event_context *ctx;
1242 ctx = READ_ONCE(event->ctx);
1243 if (!atomic_inc_not_zero(&ctx->refcount)) {
1249 mutex_lock_nested(&ctx->mutex, nesting);
1250 if (event->ctx != ctx) {
1251 mutex_unlock(&ctx->mutex);
1259 static inline struct perf_event_context *
1260 perf_event_ctx_lock(struct perf_event *event)
1262 return perf_event_ctx_lock_nested(event, 0);
1265 static void perf_event_ctx_unlock(struct perf_event *event,
1266 struct perf_event_context *ctx)
1268 mutex_unlock(&ctx->mutex);
1273 * This must be done under the ctx->lock, such as to serialize against
1274 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1275 * calling scheduler related locks and ctx->lock nests inside those.
1277 static __must_check struct perf_event_context *
1278 unclone_ctx(struct perf_event_context *ctx)
1280 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1282 lockdep_assert_held(&ctx->lock);
1285 ctx->parent_ctx = NULL;
1291 static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p,
1296 * only top level events have the pid namespace they were created in
1299 event = event->parent;
1301 nr = __task_pid_nr_ns(p, type, event->ns);
1302 /* avoid -1 if it is idle thread or runs in another ns */
1303 if (!nr && !pid_alive(p))
1308 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1310 return perf_event_pid_type(event, p, __PIDTYPE_TGID);
1313 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1315 return perf_event_pid_type(event, p, PIDTYPE_PID);
1319 * If we inherit events we want to return the parent event id
1322 static u64 primary_event_id(struct perf_event *event)
1327 id = event->parent->id;
1333 * Get the perf_event_context for a task and lock it.
1335 * This has to cope with with the fact that until it is locked,
1336 * the context could get moved to another task.
1338 static struct perf_event_context *
1339 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
1341 struct perf_event_context *ctx;
1345 * One of the few rules of preemptible RCU is that one cannot do
1346 * rcu_read_unlock() while holding a scheduler (or nested) lock when
1347 * part of the read side critical section was irqs-enabled -- see
1348 * rcu_read_unlock_special().
1350 * Since ctx->lock nests under rq->lock we must ensure the entire read
1351 * side critical section has interrupts disabled.
1353 local_irq_save(*flags);
1355 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
1358 * If this context is a clone of another, it might
1359 * get swapped for another underneath us by
1360 * perf_event_task_sched_out, though the
1361 * rcu_read_lock() protects us from any context
1362 * getting freed. Lock the context and check if it
1363 * got swapped before we could get the lock, and retry
1364 * if so. If we locked the right context, then it
1365 * can't get swapped on us any more.
1367 raw_spin_lock(&ctx->lock);
1368 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
1369 raw_spin_unlock(&ctx->lock);
1371 local_irq_restore(*flags);
1375 if (ctx->task == TASK_TOMBSTONE ||
1376 !atomic_inc_not_zero(&ctx->refcount)) {
1377 raw_spin_unlock(&ctx->lock);
1380 WARN_ON_ONCE(ctx->task != task);
1385 local_irq_restore(*flags);
1390 * Get the context for a task and increment its pin_count so it
1391 * can't get swapped to another task. This also increments its
1392 * reference count so that the context can't get freed.
1394 static struct perf_event_context *
1395 perf_pin_task_context(struct task_struct *task, int ctxn)
1397 struct perf_event_context *ctx;
1398 unsigned long flags;
1400 ctx = perf_lock_task_context(task, ctxn, &flags);
1403 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1408 static void perf_unpin_context(struct perf_event_context *ctx)
1410 unsigned long flags;
1412 raw_spin_lock_irqsave(&ctx->lock, flags);
1414 raw_spin_unlock_irqrestore(&ctx->lock, flags);
1418 * Update the record of the current time in a context.
1420 static void update_context_time(struct perf_event_context *ctx)
1422 u64 now = perf_clock();
1424 ctx->time += now - ctx->timestamp;
1425 ctx->timestamp = now;
1428 static u64 perf_event_time(struct perf_event *event)
1430 struct perf_event_context *ctx = event->ctx;
1432 if (is_cgroup_event(event))
1433 return perf_cgroup_event_time(event);
1435 return ctx ? ctx->time : 0;
1438 static enum event_type_t get_event_type(struct perf_event *event)
1440 struct perf_event_context *ctx = event->ctx;
1441 enum event_type_t event_type;
1443 lockdep_assert_held(&ctx->lock);
1446 * It's 'group type', really, because if our group leader is
1447 * pinned, so are we.
1449 if (event->group_leader != event)
1450 event = event->group_leader;
1452 event_type = event->attr.pinned ? EVENT_PINNED : EVENT_FLEXIBLE;
1454 event_type |= EVENT_CPU;
1459 static struct list_head *
1460 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1462 if (event->attr.pinned)
1463 return &ctx->pinned_groups;
1465 return &ctx->flexible_groups;
1469 * Add a event from the lists for its context.
1470 * Must be called with ctx->mutex and ctx->lock held.
1473 list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1475 lockdep_assert_held(&ctx->lock);
1477 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1478 event->attach_state |= PERF_ATTACH_CONTEXT;
1480 event->tstamp = perf_event_time(event);
1483 * If we're a stand alone event or group leader, we go to the context
1484 * list, group events are kept attached to the group so that
1485 * perf_group_detach can, at all times, locate all siblings.
1487 if (event->group_leader == event) {
1488 struct list_head *list;
1490 event->group_caps = event->event_caps;
1492 list = ctx_group_list(event, ctx);
1493 list_add_tail(&event->group_entry, list);
1496 list_update_cgroup_event(event, ctx, true);
1498 list_add_rcu(&event->event_entry, &ctx->event_list);
1500 if (event->attr.inherit_stat)
1507 * Initialize event state based on the perf_event_attr::disabled.
1509 static inline void perf_event__state_init(struct perf_event *event)
1511 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1512 PERF_EVENT_STATE_INACTIVE;
1515 static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
1517 int entry = sizeof(u64); /* value */
1521 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1522 size += sizeof(u64);
1524 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1525 size += sizeof(u64);
1527 if (event->attr.read_format & PERF_FORMAT_ID)
1528 entry += sizeof(u64);
1530 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1532 size += sizeof(u64);
1536 event->read_size = size;
1539 static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
1541 struct perf_sample_data *data;
1544 if (sample_type & PERF_SAMPLE_IP)
1545 size += sizeof(data->ip);
1547 if (sample_type & PERF_SAMPLE_ADDR)
1548 size += sizeof(data->addr);
1550 if (sample_type & PERF_SAMPLE_PERIOD)
1551 size += sizeof(data->period);
1553 if (sample_type & PERF_SAMPLE_WEIGHT)
1554 size += sizeof(data->weight);
1556 if (sample_type & PERF_SAMPLE_READ)
1557 size += event->read_size;
1559 if (sample_type & PERF_SAMPLE_DATA_SRC)
1560 size += sizeof(data->data_src.val);
1562 if (sample_type & PERF_SAMPLE_TRANSACTION)
1563 size += sizeof(data->txn);
1565 if (sample_type & PERF_SAMPLE_PHYS_ADDR)
1566 size += sizeof(data->phys_addr);
1568 event->header_size = size;
1572 * Called at perf_event creation and when events are attached/detached from a
1575 static void perf_event__header_size(struct perf_event *event)
1577 __perf_event_read_size(event,
1578 event->group_leader->nr_siblings);
1579 __perf_event_header_size(event, event->attr.sample_type);
1582 static void perf_event__id_header_size(struct perf_event *event)
1584 struct perf_sample_data *data;
1585 u64 sample_type = event->attr.sample_type;
1588 if (sample_type & PERF_SAMPLE_TID)
1589 size += sizeof(data->tid_entry);
1591 if (sample_type & PERF_SAMPLE_TIME)
1592 size += sizeof(data->time);
1594 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1595 size += sizeof(data->id);
1597 if (sample_type & PERF_SAMPLE_ID)
1598 size += sizeof(data->id);
1600 if (sample_type & PERF_SAMPLE_STREAM_ID)
1601 size += sizeof(data->stream_id);
1603 if (sample_type & PERF_SAMPLE_CPU)
1604 size += sizeof(data->cpu_entry);
1606 event->id_header_size = size;
1609 static bool perf_event_validate_size(struct perf_event *event)
1612 * The values computed here will be over-written when we actually
1615 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1616 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1617 perf_event__id_header_size(event);
1620 * Sum the lot; should not exceed the 64k limit we have on records.
1621 * Conservative limit to allow for callchains and other variable fields.
1623 if (event->read_size + event->header_size +
1624 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1630 static void perf_group_attach(struct perf_event *event)
1632 struct perf_event *group_leader = event->group_leader, *pos;
1634 lockdep_assert_held(&event->ctx->lock);
1637 * We can have double attach due to group movement in perf_event_open.
1639 if (event->attach_state & PERF_ATTACH_GROUP)
1642 event->attach_state |= PERF_ATTACH_GROUP;
1644 if (group_leader == event)
1647 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1649 group_leader->group_caps &= event->event_caps;
1651 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1652 group_leader->nr_siblings++;
1654 perf_event__header_size(group_leader);
1656 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1657 perf_event__header_size(pos);
1661 * Remove a event from the lists for its context.
1662 * Must be called with ctx->mutex and ctx->lock held.
1665 list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1667 WARN_ON_ONCE(event->ctx != ctx);
1668 lockdep_assert_held(&ctx->lock);
1671 * We can have double detach due to exit/hot-unplug + close.
1673 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
1676 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1678 list_update_cgroup_event(event, ctx, false);
1681 if (event->attr.inherit_stat)
1684 list_del_rcu(&event->event_entry);
1686 if (event->group_leader == event)
1687 list_del_init(&event->group_entry);
1690 * If event was in error state, then keep it
1691 * that way, otherwise bogus counts will be
1692 * returned on read(). The only way to get out
1693 * of error state is by explicit re-enabling
1696 if (event->state > PERF_EVENT_STATE_OFF)
1697 perf_event_set_state(event, PERF_EVENT_STATE_OFF);
1702 static void perf_group_detach(struct perf_event *event)
1704 struct perf_event *sibling, *tmp;
1705 struct list_head *list = NULL;
1707 lockdep_assert_held(&event->ctx->lock);
1710 * We can have double detach due to exit/hot-unplug + close.
1712 if (!(event->attach_state & PERF_ATTACH_GROUP))
1715 event->attach_state &= ~PERF_ATTACH_GROUP;
1718 * If this is a sibling, remove it from its group.
1720 if (event->group_leader != event) {
1721 list_del_init(&event->group_entry);
1722 event->group_leader->nr_siblings--;
1726 if (!list_empty(&event->group_entry))
1727 list = &event->group_entry;
1730 * If this was a group event with sibling events then
1731 * upgrade the siblings to singleton events by adding them
1732 * to whatever list we are on.
1734 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
1736 list_move_tail(&sibling->group_entry, list);
1737 sibling->group_leader = sibling;
1739 /* Inherit group flags from the previous leader */
1740 sibling->group_caps = event->group_caps;
1742 WARN_ON_ONCE(sibling->ctx != event->ctx);
1746 perf_event__header_size(event->group_leader);
1748 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1749 perf_event__header_size(tmp);
1752 static bool is_orphaned_event(struct perf_event *event)
1754 return event->state == PERF_EVENT_STATE_DEAD;
1757 static inline int __pmu_filter_match(struct perf_event *event)
1759 struct pmu *pmu = event->pmu;
1760 return pmu->filter_match ? pmu->filter_match(event) : 1;
1764 * Check whether we should attempt to schedule an event group based on
1765 * PMU-specific filtering. An event group can consist of HW and SW events,
1766 * potentially with a SW leader, so we must check all the filters, to
1767 * determine whether a group is schedulable:
1769 static inline int pmu_filter_match(struct perf_event *event)
1771 struct perf_event *child;
1773 if (!__pmu_filter_match(event))
1776 list_for_each_entry(child, &event->sibling_list, group_entry) {
1777 if (!__pmu_filter_match(child))
1785 event_filter_match(struct perf_event *event)
1787 return (event->cpu == -1 || event->cpu == smp_processor_id()) &&
1788 perf_cgroup_match(event) && pmu_filter_match(event);
1792 event_sched_out(struct perf_event *event,
1793 struct perf_cpu_context *cpuctx,
1794 struct perf_event_context *ctx)
1796 enum perf_event_state state = PERF_EVENT_STATE_INACTIVE;
1798 WARN_ON_ONCE(event->ctx != ctx);
1799 lockdep_assert_held(&ctx->lock);
1801 if (event->state != PERF_EVENT_STATE_ACTIVE)
1804 perf_pmu_disable(event->pmu);
1806 event->pmu->del(event, 0);
1809 if (event->pending_disable) {
1810 event->pending_disable = 0;
1811 state = PERF_EVENT_STATE_OFF;
1813 perf_event_set_state(event, state);
1815 if (!is_software_event(event))
1816 cpuctx->active_oncpu--;
1817 if (!--ctx->nr_active)
1818 perf_event_ctx_deactivate(ctx);
1819 if (event->attr.freq && event->attr.sample_freq)
1821 if (event->attr.exclusive || !cpuctx->active_oncpu)
1822 cpuctx->exclusive = 0;
1824 perf_pmu_enable(event->pmu);
1828 group_sched_out(struct perf_event *group_event,
1829 struct perf_cpu_context *cpuctx,
1830 struct perf_event_context *ctx)
1832 struct perf_event *event;
1834 if (group_event->state != PERF_EVENT_STATE_ACTIVE)
1837 perf_pmu_disable(ctx->pmu);
1839 event_sched_out(group_event, cpuctx, ctx);
1842 * Schedule out siblings (if any):
1844 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1845 event_sched_out(event, cpuctx, ctx);
1847 perf_pmu_enable(ctx->pmu);
1849 if (group_event->attr.exclusive)
1850 cpuctx->exclusive = 0;
1853 #define DETACH_GROUP 0x01UL
1856 * Cross CPU call to remove a performance event
1858 * We disable the event on the hardware level first. After that we
1859 * remove it from the context list.
1862 __perf_remove_from_context(struct perf_event *event,
1863 struct perf_cpu_context *cpuctx,
1864 struct perf_event_context *ctx,
1867 unsigned long flags = (unsigned long)info;
1869 if (ctx->is_active & EVENT_TIME) {
1870 update_context_time(ctx);
1871 update_cgrp_time_from_cpuctx(cpuctx);
1874 event_sched_out(event, cpuctx, ctx);
1875 if (flags & DETACH_GROUP)
1876 perf_group_detach(event);
1877 list_del_event(event, ctx);
1879 if (!ctx->nr_events && ctx->is_active) {
1882 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
1883 cpuctx->task_ctx = NULL;
1889 * Remove the event from a task's (or a CPU's) list of events.
1891 * If event->ctx is a cloned context, callers must make sure that
1892 * every task struct that event->ctx->task could possibly point to
1893 * remains valid. This is OK when called from perf_release since
1894 * that only calls us on the top-level context, which can't be a clone.
1895 * When called from perf_event_exit_task, it's OK because the
1896 * context has been detached from its task.
1898 static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
1900 struct perf_event_context *ctx = event->ctx;
1902 lockdep_assert_held(&ctx->mutex);
1904 event_function_call(event, __perf_remove_from_context, (void *)flags);
1907 * The above event_function_call() can NO-OP when it hits
1908 * TASK_TOMBSTONE. In that case we must already have been detached
1909 * from the context (by perf_event_exit_event()) but the grouping
1910 * might still be in-tact.
1912 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1913 if ((flags & DETACH_GROUP) &&
1914 (event->attach_state & PERF_ATTACH_GROUP)) {
1916 * Since in that case we cannot possibly be scheduled, simply
1919 raw_spin_lock_irq(&ctx->lock);
1920 perf_group_detach(event);
1921 raw_spin_unlock_irq(&ctx->lock);
1926 * Cross CPU call to disable a performance event
1928 static void __perf_event_disable(struct perf_event *event,
1929 struct perf_cpu_context *cpuctx,
1930 struct perf_event_context *ctx,
1933 if (event->state < PERF_EVENT_STATE_INACTIVE)
1936 if (ctx->is_active & EVENT_TIME) {
1937 update_context_time(ctx);
1938 update_cgrp_time_from_event(event);
1941 if (event == event->group_leader)
1942 group_sched_out(event, cpuctx, ctx);
1944 event_sched_out(event, cpuctx, ctx);
1946 perf_event_set_state(event, PERF_EVENT_STATE_OFF);
1952 * If event->ctx is a cloned context, callers must make sure that
1953 * every task struct that event->ctx->task could possibly point to
1954 * remains valid. This condition is satisifed when called through
1955 * perf_event_for_each_child or perf_event_for_each because they
1956 * hold the top-level event's child_mutex, so any descendant that
1957 * goes to exit will block in perf_event_exit_event().
1959 * When called from perf_pending_event it's OK because event->ctx
1960 * is the current context on this CPU and preemption is disabled,
1961 * hence we can't get into perf_event_task_sched_out for this context.
1963 static void _perf_event_disable(struct perf_event *event)
1965 struct perf_event_context *ctx = event->ctx;
1967 raw_spin_lock_irq(&ctx->lock);
1968 if (event->state <= PERF_EVENT_STATE_OFF) {
1969 raw_spin_unlock_irq(&ctx->lock);
1972 raw_spin_unlock_irq(&ctx->lock);
1974 event_function_call(event, __perf_event_disable, NULL);
1977 void perf_event_disable_local(struct perf_event *event)
1979 event_function_local(event, __perf_event_disable, NULL);
1983 * Strictly speaking kernel users cannot create groups and therefore this
1984 * interface does not need the perf_event_ctx_lock() magic.
1986 void perf_event_disable(struct perf_event *event)
1988 struct perf_event_context *ctx;
1990 ctx = perf_event_ctx_lock(event);
1991 _perf_event_disable(event);
1992 perf_event_ctx_unlock(event, ctx);
1994 EXPORT_SYMBOL_GPL(perf_event_disable);
1996 void perf_event_disable_inatomic(struct perf_event *event)
1998 event->pending_disable = 1;
1999 irq_work_queue(&event->pending);
2002 static void perf_set_shadow_time(struct perf_event *event,
2003 struct perf_event_context *ctx)
2006 * use the correct time source for the time snapshot
2008 * We could get by without this by leveraging the
2009 * fact that to get to this function, the caller
2010 * has most likely already called update_context_time()
2011 * and update_cgrp_time_xx() and thus both timestamp
2012 * are identical (or very close). Given that tstamp is,
2013 * already adjusted for cgroup, we could say that:
2014 * tstamp - ctx->timestamp
2016 * tstamp - cgrp->timestamp.
2018 * Then, in perf_output_read(), the calculation would
2019 * work with no changes because:
2020 * - event is guaranteed scheduled in
2021 * - no scheduled out in between
2022 * - thus the timestamp would be the same
2024 * But this is a bit hairy.
2026 * So instead, we have an explicit cgroup call to remain
2027 * within the time time source all along. We believe it
2028 * is cleaner and simpler to understand.
2030 if (is_cgroup_event(event))
2031 perf_cgroup_set_shadow_time(event, event->tstamp);
2033 event->shadow_ctx_time = event->tstamp - ctx->timestamp;
2036 #define MAX_INTERRUPTS (~0ULL)
2038 static void perf_log_throttle(struct perf_event *event, int enable);
2039 static void perf_log_itrace_start(struct perf_event *event);
2042 event_sched_in(struct perf_event *event,
2043 struct perf_cpu_context *cpuctx,
2044 struct perf_event_context *ctx)
2048 lockdep_assert_held(&ctx->lock);
2050 if (event->state <= PERF_EVENT_STATE_OFF)
2053 WRITE_ONCE(event->oncpu, smp_processor_id());
2055 * Order event::oncpu write to happen before the ACTIVE state is
2056 * visible. This allows perf_event_{stop,read}() to observe the correct
2057 * ->oncpu if it sees ACTIVE.
2060 perf_event_set_state(event, PERF_EVENT_STATE_ACTIVE);
2063 * Unthrottle events, since we scheduled we might have missed several
2064 * ticks already, also for a heavily scheduling task there is little
2065 * guarantee it'll get a tick in a timely manner.
2067 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
2068 perf_log_throttle(event, 1);
2069 event->hw.interrupts = 0;
2072 perf_pmu_disable(event->pmu);
2074 perf_set_shadow_time(event, ctx);
2076 perf_log_itrace_start(event);
2078 if (event->pmu->add(event, PERF_EF_START)) {
2079 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
2085 if (!is_software_event(event))
2086 cpuctx->active_oncpu++;
2087 if (!ctx->nr_active++)
2088 perf_event_ctx_activate(ctx);
2089 if (event->attr.freq && event->attr.sample_freq)
2092 if (event->attr.exclusive)
2093 cpuctx->exclusive = 1;
2096 perf_pmu_enable(event->pmu);
2102 group_sched_in(struct perf_event *group_event,
2103 struct perf_cpu_context *cpuctx,
2104 struct perf_event_context *ctx)
2106 struct perf_event *event, *partial_group = NULL;
2107 struct pmu *pmu = ctx->pmu;
2109 if (group_event->state == PERF_EVENT_STATE_OFF)
2112 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
2114 if (event_sched_in(group_event, cpuctx, ctx)) {
2115 pmu->cancel_txn(pmu);
2116 perf_mux_hrtimer_restart(cpuctx);
2121 * Schedule in siblings as one group (if any):
2123 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
2124 if (event_sched_in(event, cpuctx, ctx)) {
2125 partial_group = event;
2130 if (!pmu->commit_txn(pmu))
2135 * Groups can be scheduled in as one unit only, so undo any
2136 * partial group before returning:
2137 * The events up to the failed event are scheduled out normally.
2139 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
2140 if (event == partial_group)
2143 event_sched_out(event, cpuctx, ctx);
2145 event_sched_out(group_event, cpuctx, ctx);
2147 pmu->cancel_txn(pmu);
2149 perf_mux_hrtimer_restart(cpuctx);
2155 * Work out whether we can put this event group on the CPU now.
2157 static int group_can_go_on(struct perf_event *event,
2158 struct perf_cpu_context *cpuctx,
2162 * Groups consisting entirely of software events can always go on.
2164 if (event->group_caps & PERF_EV_CAP_SOFTWARE)
2167 * If an exclusive group is already on, no other hardware
2170 if (cpuctx->exclusive)
2173 * If this group is exclusive and there are already
2174 * events on the CPU, it can't go on.
2176 if (event->attr.exclusive && cpuctx->active_oncpu)
2179 * Otherwise, try to add it if all previous groups were able
2185 static void add_event_to_ctx(struct perf_event *event,
2186 struct perf_event_context *ctx)
2188 list_add_event(event, ctx);
2189 perf_group_attach(event);
2192 static void ctx_sched_out(struct perf_event_context *ctx,
2193 struct perf_cpu_context *cpuctx,
2194 enum event_type_t event_type);
2196 ctx_sched_in(struct perf_event_context *ctx,
2197 struct perf_cpu_context *cpuctx,
2198 enum event_type_t event_type,
2199 struct task_struct *task);
2201 static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2202 struct perf_event_context *ctx,
2203 enum event_type_t event_type)
2205 if (!cpuctx->task_ctx)
2208 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2211 ctx_sched_out(ctx, cpuctx, event_type);
2214 static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2215 struct perf_event_context *ctx,
2216 struct task_struct *task)
2218 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2220 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2221 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2223 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2227 * We want to maintain the following priority of scheduling:
2228 * - CPU pinned (EVENT_CPU | EVENT_PINNED)
2229 * - task pinned (EVENT_PINNED)
2230 * - CPU flexible (EVENT_CPU | EVENT_FLEXIBLE)
2231 * - task flexible (EVENT_FLEXIBLE).
2233 * In order to avoid unscheduling and scheduling back in everything every
2234 * time an event is added, only do it for the groups of equal priority and
2237 * This can be called after a batch operation on task events, in which case
2238 * event_type is a bit mask of the types of events involved. For CPU events,
2239 * event_type is only either EVENT_PINNED or EVENT_FLEXIBLE.
2241 static void ctx_resched(struct perf_cpu_context *cpuctx,
2242 struct perf_event_context *task_ctx,
2243 enum event_type_t event_type)
2245 enum event_type_t ctx_event_type = event_type & EVENT_ALL;
2246 bool cpu_event = !!(event_type & EVENT_CPU);
2249 * If pinned groups are involved, flexible groups also need to be
2252 if (event_type & EVENT_PINNED)
2253 event_type |= EVENT_FLEXIBLE;
2255 perf_pmu_disable(cpuctx->ctx.pmu);
2257 task_ctx_sched_out(cpuctx, task_ctx, event_type);
2260 * Decide which cpu ctx groups to schedule out based on the types
2261 * of events that caused rescheduling:
2262 * - EVENT_CPU: schedule out corresponding groups;
2263 * - EVENT_PINNED task events: schedule out EVENT_FLEXIBLE groups;
2264 * - otherwise, do nothing more.
2267 cpu_ctx_sched_out(cpuctx, ctx_event_type);
2268 else if (ctx_event_type & EVENT_PINNED)
2269 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2271 perf_event_sched_in(cpuctx, task_ctx, current);
2272 perf_pmu_enable(cpuctx->ctx.pmu);
2276 * Cross CPU call to install and enable a performance event
2278 * Very similar to remote_function() + event_function() but cannot assume that
2279 * things like ctx->is_active and cpuctx->task_ctx are set.
2281 static int __perf_install_in_context(void *info)
2283 struct perf_event *event = info;
2284 struct perf_event_context *ctx = event->ctx;
2285 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2286 struct perf_event_context *task_ctx = cpuctx->task_ctx;
2287 bool reprogram = true;
2290 raw_spin_lock(&cpuctx->ctx.lock);
2292 raw_spin_lock(&ctx->lock);
2295 reprogram = (ctx->task == current);
2298 * If the task is running, it must be running on this CPU,
2299 * otherwise we cannot reprogram things.
2301 * If its not running, we don't care, ctx->lock will
2302 * serialize against it becoming runnable.
2304 if (task_curr(ctx->task) && !reprogram) {
2309 WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx);
2310 } else if (task_ctx) {
2311 raw_spin_lock(&task_ctx->lock);
2315 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2316 add_event_to_ctx(event, ctx);
2317 ctx_resched(cpuctx, task_ctx, get_event_type(event));
2319 add_event_to_ctx(event, ctx);
2323 perf_ctx_unlock(cpuctx, task_ctx);
2329 * Attach a performance event to a context.
2331 * Very similar to event_function_call, see comment there.
2334 perf_install_in_context(struct perf_event_context *ctx,
2335 struct perf_event *event,
2338 struct task_struct *task = READ_ONCE(ctx->task);
2340 lockdep_assert_held(&ctx->mutex);
2342 if (event->cpu != -1)
2346 * Ensures that if we can observe event->ctx, both the event and ctx
2347 * will be 'complete'. See perf_iterate_sb_cpu().
2349 smp_store_release(&event->ctx, ctx);
2352 cpu_function_call(cpu, __perf_install_in_context, event);
2357 * Should not happen, we validate the ctx is still alive before calling.
2359 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2363 * Installing events is tricky because we cannot rely on ctx->is_active
2364 * to be set in case this is the nr_events 0 -> 1 transition.
2366 * Instead we use task_curr(), which tells us if the task is running.
2367 * However, since we use task_curr() outside of rq::lock, we can race
2368 * against the actual state. This means the result can be wrong.
2370 * If we get a false positive, we retry, this is harmless.
2372 * If we get a false negative, things are complicated. If we are after
2373 * perf_event_context_sched_in() ctx::lock will serialize us, and the
2374 * value must be correct. If we're before, it doesn't matter since
2375 * perf_event_context_sched_in() will program the counter.
2377 * However, this hinges on the remote context switch having observed
2378 * our task->perf_event_ctxp[] store, such that it will in fact take
2379 * ctx::lock in perf_event_context_sched_in().
2381 * We do this by task_function_call(), if the IPI fails to hit the task
2382 * we know any future context switch of task must see the
2383 * perf_event_ctpx[] store.
2387 * This smp_mb() orders the task->perf_event_ctxp[] store with the
2388 * task_cpu() load, such that if the IPI then does not find the task
2389 * running, a future context switch of that task must observe the
2394 if (!task_function_call(task, __perf_install_in_context, event))
2397 raw_spin_lock_irq(&ctx->lock);
2399 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
2401 * Cannot happen because we already checked above (which also
2402 * cannot happen), and we hold ctx->mutex, which serializes us
2403 * against perf_event_exit_task_context().
2405 raw_spin_unlock_irq(&ctx->lock);
2409 * If the task is not running, ctx->lock will avoid it becoming so,
2410 * thus we can safely install the event.
2412 if (task_curr(task)) {
2413 raw_spin_unlock_irq(&ctx->lock);
2416 add_event_to_ctx(event, ctx);
2417 raw_spin_unlock_irq(&ctx->lock);
2421 * Cross CPU call to enable a performance event
2423 static void __perf_event_enable(struct perf_event *event,
2424 struct perf_cpu_context *cpuctx,
2425 struct perf_event_context *ctx,
2428 struct perf_event *leader = event->group_leader;
2429 struct perf_event_context *task_ctx;
2431 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2432 event->state <= PERF_EVENT_STATE_ERROR)
2436 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2438 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
2440 if (!ctx->is_active)
2443 if (!event_filter_match(event)) {
2444 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
2449 * If the event is in a group and isn't the group leader,
2450 * then don't put it on unless the group is on.
2452 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2453 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
2457 task_ctx = cpuctx->task_ctx;
2459 WARN_ON_ONCE(task_ctx != ctx);
2461 ctx_resched(cpuctx, task_ctx, get_event_type(event));
2467 * If event->ctx is a cloned context, callers must make sure that
2468 * every task struct that event->ctx->task could possibly point to
2469 * remains valid. This condition is satisfied when called through
2470 * perf_event_for_each_child or perf_event_for_each as described
2471 * for perf_event_disable.
2473 static void _perf_event_enable(struct perf_event *event)
2475 struct perf_event_context *ctx = event->ctx;
2477 raw_spin_lock_irq(&ctx->lock);
2478 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2479 event->state < PERF_EVENT_STATE_ERROR) {
2480 raw_spin_unlock_irq(&ctx->lock);
2485 * If the event is in error state, clear that first.
2487 * That way, if we see the event in error state below, we know that it
2488 * has gone back into error state, as distinct from the task having
2489 * been scheduled away before the cross-call arrived.
2491 if (event->state == PERF_EVENT_STATE_ERROR)
2492 event->state = PERF_EVENT_STATE_OFF;
2493 raw_spin_unlock_irq(&ctx->lock);
2495 event_function_call(event, __perf_event_enable, NULL);
2499 * See perf_event_disable();
2501 void perf_event_enable(struct perf_event *event)
2503 struct perf_event_context *ctx;
2505 ctx = perf_event_ctx_lock(event);
2506 _perf_event_enable(event);
2507 perf_event_ctx_unlock(event, ctx);
2509 EXPORT_SYMBOL_GPL(perf_event_enable);
2511 struct stop_event_data {
2512 struct perf_event *event;
2513 unsigned int restart;
2516 static int __perf_event_stop(void *info)
2518 struct stop_event_data *sd = info;
2519 struct perf_event *event = sd->event;
2521 /* if it's already INACTIVE, do nothing */
2522 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2525 /* matches smp_wmb() in event_sched_in() */
2529 * There is a window with interrupts enabled before we get here,
2530 * so we need to check again lest we try to stop another CPU's event.
2532 if (READ_ONCE(event->oncpu) != smp_processor_id())
2535 event->pmu->stop(event, PERF_EF_UPDATE);
2538 * May race with the actual stop (through perf_pmu_output_stop()),
2539 * but it is only used for events with AUX ring buffer, and such
2540 * events will refuse to restart because of rb::aux_mmap_count==0,
2541 * see comments in perf_aux_output_begin().
2543 * Since this is happening on a event-local CPU, no trace is lost
2547 event->pmu->start(event, 0);
2552 static int perf_event_stop(struct perf_event *event, int restart)
2554 struct stop_event_data sd = {
2561 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2564 /* matches smp_wmb() in event_sched_in() */
2568 * We only want to restart ACTIVE events, so if the event goes
2569 * inactive here (event->oncpu==-1), there's nothing more to do;
2570 * fall through with ret==-ENXIO.
2572 ret = cpu_function_call(READ_ONCE(event->oncpu),
2573 __perf_event_stop, &sd);
2574 } while (ret == -EAGAIN);
2580 * In order to contain the amount of racy and tricky in the address filter
2581 * configuration management, it is a two part process:
2583 * (p1) when userspace mappings change as a result of (1) or (2) or (3) below,
2584 * we update the addresses of corresponding vmas in
2585 * event::addr_filters_offs array and bump the event::addr_filters_gen;
2586 * (p2) when an event is scheduled in (pmu::add), it calls
2587 * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync()
2588 * if the generation has changed since the previous call.
2590 * If (p1) happens while the event is active, we restart it to force (p2).
2592 * (1) perf_addr_filters_apply(): adjusting filters' offsets based on
2593 * pre-existing mappings, called once when new filters arrive via SET_FILTER
2595 * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly
2596 * registered mapping, called for every new mmap(), with mm::mmap_sem down
2598 * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process
2601 void perf_event_addr_filters_sync(struct perf_event *event)
2603 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
2605 if (!has_addr_filter(event))
2608 raw_spin_lock(&ifh->lock);
2609 if (event->addr_filters_gen != event->hw.addr_filters_gen) {
2610 event->pmu->addr_filters_sync(event);
2611 event->hw.addr_filters_gen = event->addr_filters_gen;
2613 raw_spin_unlock(&ifh->lock);
2615 EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync);
2617 static int _perf_event_refresh(struct perf_event *event, int refresh)
2620 * not supported on inherited events
2622 if (event->attr.inherit || !is_sampling_event(event))
2625 atomic_add(refresh, &event->event_limit);
2626 _perf_event_enable(event);
2632 * See perf_event_disable()
2634 int perf_event_refresh(struct perf_event *event, int refresh)
2636 struct perf_event_context *ctx;
2639 ctx = perf_event_ctx_lock(event);
2640 ret = _perf_event_refresh(event, refresh);
2641 perf_event_ctx_unlock(event, ctx);
2645 EXPORT_SYMBOL_GPL(perf_event_refresh);
2647 static void ctx_sched_out(struct perf_event_context *ctx,
2648 struct perf_cpu_context *cpuctx,
2649 enum event_type_t event_type)
2651 int is_active = ctx->is_active;
2652 struct perf_event *event;
2654 lockdep_assert_held(&ctx->lock);
2656 if (likely(!ctx->nr_events)) {
2658 * See __perf_remove_from_context().
2660 WARN_ON_ONCE(ctx->is_active);
2662 WARN_ON_ONCE(cpuctx->task_ctx);
2666 ctx->is_active &= ~event_type;
2667 if (!(ctx->is_active & EVENT_ALL))
2671 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2672 if (!ctx->is_active)
2673 cpuctx->task_ctx = NULL;
2677 * Always update time if it was set; not only when it changes.
2678 * Otherwise we can 'forget' to update time for any but the last
2679 * context we sched out. For example:
2681 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
2682 * ctx_sched_out(.event_type = EVENT_PINNED)
2684 * would only update time for the pinned events.
2686 if (is_active & EVENT_TIME) {
2687 /* update (and stop) ctx time */
2688 update_context_time(ctx);
2689 update_cgrp_time_from_cpuctx(cpuctx);
2692 is_active ^= ctx->is_active; /* changed bits */
2694 if (!ctx->nr_active || !(is_active & EVENT_ALL))
2697 perf_pmu_disable(ctx->pmu);
2698 if (is_active & EVENT_PINNED) {
2699 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2700 group_sched_out(event, cpuctx, ctx);
2703 if (is_active & EVENT_FLEXIBLE) {
2704 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
2705 group_sched_out(event, cpuctx, ctx);
2707 perf_pmu_enable(ctx->pmu);
2711 * Test whether two contexts are equivalent, i.e. whether they have both been
2712 * cloned from the same version of the same context.
2714 * Equivalence is measured using a generation number in the context that is
2715 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2716 * and list_del_event().
2718 static int context_equiv(struct perf_event_context *ctx1,
2719 struct perf_event_context *ctx2)
2721 lockdep_assert_held(&ctx1->lock);
2722 lockdep_assert_held(&ctx2->lock);
2724 /* Pinning disables the swap optimization */
2725 if (ctx1->pin_count || ctx2->pin_count)
2728 /* If ctx1 is the parent of ctx2 */
2729 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2732 /* If ctx2 is the parent of ctx1 */
2733 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2737 * If ctx1 and ctx2 have the same parent; we flatten the parent
2738 * hierarchy, see perf_event_init_context().
2740 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2741 ctx1->parent_gen == ctx2->parent_gen)
2748 static void __perf_event_sync_stat(struct perf_event *event,
2749 struct perf_event *next_event)
2753 if (!event->attr.inherit_stat)
2757 * Update the event value, we cannot use perf_event_read()
2758 * because we're in the middle of a context switch and have IRQs
2759 * disabled, which upsets smp_call_function_single(), however
2760 * we know the event must be on the current CPU, therefore we
2761 * don't need to use it.
2763 if (event->state == PERF_EVENT_STATE_ACTIVE)
2764 event->pmu->read(event);
2766 perf_event_update_time(event);
2769 * In order to keep per-task stats reliable we need to flip the event
2770 * values when we flip the contexts.
2772 value = local64_read(&next_event->count);
2773 value = local64_xchg(&event->count, value);
2774 local64_set(&next_event->count, value);
2776 swap(event->total_time_enabled, next_event->total_time_enabled);
2777 swap(event->total_time_running, next_event->total_time_running);
2780 * Since we swizzled the values, update the user visible data too.
2782 perf_event_update_userpage(event);
2783 perf_event_update_userpage(next_event);
2786 static void perf_event_sync_stat(struct perf_event_context *ctx,
2787 struct perf_event_context *next_ctx)
2789 struct perf_event *event, *next_event;
2794 update_context_time(ctx);
2796 event = list_first_entry(&ctx->event_list,
2797 struct perf_event, event_entry);
2799 next_event = list_first_entry(&next_ctx->event_list,
2800 struct perf_event, event_entry);
2802 while (&event->event_entry != &ctx->event_list &&
2803 &next_event->event_entry != &next_ctx->event_list) {
2805 __perf_event_sync_stat(event, next_event);
2807 event = list_next_entry(event, event_entry);
2808 next_event = list_next_entry(next_event, event_entry);
2812 static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2813 struct task_struct *next)
2815 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
2816 struct perf_event_context *next_ctx;
2817 struct perf_event_context *parent, *next_parent;
2818 struct perf_cpu_context *cpuctx;
2824 cpuctx = __get_cpu_context(ctx);
2825 if (!cpuctx->task_ctx)
2829 next_ctx = next->perf_event_ctxp[ctxn];
2833 parent = rcu_dereference(ctx->parent_ctx);
2834 next_parent = rcu_dereference(next_ctx->parent_ctx);
2836 /* If neither context have a parent context; they cannot be clones. */
2837 if (!parent && !next_parent)
2840 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
2842 * Looks like the two contexts are clones, so we might be
2843 * able to optimize the context switch. We lock both
2844 * contexts and check that they are clones under the
2845 * lock (including re-checking that neither has been
2846 * uncloned in the meantime). It doesn't matter which
2847 * order we take the locks because no other cpu could
2848 * be trying to lock both of these tasks.
2850 raw_spin_lock(&ctx->lock);
2851 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
2852 if (context_equiv(ctx, next_ctx)) {
2853 WRITE_ONCE(ctx->task, next);
2854 WRITE_ONCE(next_ctx->task, task);
2856 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2859 * RCU_INIT_POINTER here is safe because we've not
2860 * modified the ctx and the above modification of
2861 * ctx->task and ctx->task_ctx_data are immaterial
2862 * since those values are always verified under
2863 * ctx->lock which we're now holding.
2865 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
2866 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
2870 perf_event_sync_stat(ctx, next_ctx);
2872 raw_spin_unlock(&next_ctx->lock);
2873 raw_spin_unlock(&ctx->lock);
2879 raw_spin_lock(&ctx->lock);
2880 task_ctx_sched_out(cpuctx, ctx, EVENT_ALL);
2881 raw_spin_unlock(&ctx->lock);
2885 static DEFINE_PER_CPU(struct list_head, sched_cb_list);
2887 void perf_sched_cb_dec(struct pmu *pmu)
2889 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2891 this_cpu_dec(perf_sched_cb_usages);
2893 if (!--cpuctx->sched_cb_usage)
2894 list_del(&cpuctx->sched_cb_entry);
2898 void perf_sched_cb_inc(struct pmu *pmu)
2900 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2902 if (!cpuctx->sched_cb_usage++)
2903 list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list));
2905 this_cpu_inc(perf_sched_cb_usages);
2909 * This function provides the context switch callback to the lower code
2910 * layer. It is invoked ONLY when the context switch callback is enabled.
2912 * This callback is relevant even to per-cpu events; for example multi event
2913 * PEBS requires this to provide PID/TID information. This requires we flush
2914 * all queued PEBS records before we context switch to a new task.
2916 static void perf_pmu_sched_task(struct task_struct *prev,
2917 struct task_struct *next,
2920 struct perf_cpu_context *cpuctx;
2926 list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry) {
2927 pmu = cpuctx->ctx.pmu; /* software PMUs will not have sched_task */
2929 if (WARN_ON_ONCE(!pmu->sched_task))
2932 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2933 perf_pmu_disable(pmu);
2935 pmu->sched_task(cpuctx->task_ctx, sched_in);
2937 perf_pmu_enable(pmu);
2938 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2942 static void perf_event_switch(struct task_struct *task,
2943 struct task_struct *next_prev, bool sched_in);
2945 #define for_each_task_context_nr(ctxn) \
2946 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2949 * Called from scheduler to remove the events of the current task,
2950 * with interrupts disabled.
2952 * We stop each event and update the event value in event->count.
2954 * This does not protect us against NMI, but disable()
2955 * sets the disabled bit in the control field of event _before_
2956 * accessing the event control register. If a NMI hits, then it will
2957 * not restart the event.
2959 void __perf_event_task_sched_out(struct task_struct *task,
2960 struct task_struct *next)
2964 if (__this_cpu_read(perf_sched_cb_usages))
2965 perf_pmu_sched_task(task, next, false);
2967 if (atomic_read(&nr_switch_events))
2968 perf_event_switch(task, next, false);
2970 for_each_task_context_nr(ctxn)
2971 perf_event_context_sched_out(task, ctxn, next);
2974 * if cgroup events exist on this CPU, then we need
2975 * to check if we have to switch out PMU state.
2976 * cgroup event are system-wide mode only
2978 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
2979 perf_cgroup_sched_out(task, next);
2983 * Called with IRQs disabled
2985 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2986 enum event_type_t event_type)
2988 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
2992 ctx_pinned_sched_in(struct perf_event_context *ctx,
2993 struct perf_cpu_context *cpuctx)
2995 struct perf_event *event;
2997 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2998 if (event->state <= PERF_EVENT_STATE_OFF)
3000 if (!event_filter_match(event))
3003 if (group_can_go_on(event, cpuctx, 1))
3004 group_sched_in(event, cpuctx, ctx);
3007 * If this pinned group hasn't been scheduled,
3008 * put it in error state.
3010 if (event->state == PERF_EVENT_STATE_INACTIVE)
3011 perf_event_set_state(event, PERF_EVENT_STATE_ERROR);
3016 ctx_flexible_sched_in(struct perf_event_context *ctx,
3017 struct perf_cpu_context *cpuctx)
3019 struct perf_event *event;
3022 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
3023 /* Ignore events in OFF or ERROR state */
3024 if (event->state <= PERF_EVENT_STATE_OFF)
3027 * Listen to the 'cpu' scheduling filter constraint
3030 if (!event_filter_match(event))
3033 if (group_can_go_on(event, cpuctx, can_add_hw)) {
3034 if (group_sched_in(event, cpuctx, ctx))
3041 ctx_sched_in(struct perf_event_context *ctx,
3042 struct perf_cpu_context *cpuctx,
3043 enum event_type_t event_type,
3044 struct task_struct *task)
3046 int is_active = ctx->is_active;
3049 lockdep_assert_held(&ctx->lock);
3051 if (likely(!ctx->nr_events))
3054 ctx->is_active |= (event_type | EVENT_TIME);
3057 cpuctx->task_ctx = ctx;
3059 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
3062 is_active ^= ctx->is_active; /* changed bits */
3064 if (is_active & EVENT_TIME) {
3065 /* start ctx time */
3067 ctx->timestamp = now;
3068 perf_cgroup_set_timestamp(task, ctx);
3072 * First go through the list and put on any pinned groups
3073 * in order to give them the best chance of going on.
3075 if (is_active & EVENT_PINNED)
3076 ctx_pinned_sched_in(ctx, cpuctx);
3078 /* Then walk through the lower prio flexible groups */
3079 if (is_active & EVENT_FLEXIBLE)
3080 ctx_flexible_sched_in(ctx, cpuctx);
3083 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
3084 enum event_type_t event_type,
3085 struct task_struct *task)
3087 struct perf_event_context *ctx = &cpuctx->ctx;
3089 ctx_sched_in(ctx, cpuctx, event_type, task);
3092 static void perf_event_context_sched_in(struct perf_event_context *ctx,
3093 struct task_struct *task)
3095 struct perf_cpu_context *cpuctx;
3097 cpuctx = __get_cpu_context(ctx);
3098 if (cpuctx->task_ctx == ctx)
3101 perf_ctx_lock(cpuctx, ctx);
3103 * We must check ctx->nr_events while holding ctx->lock, such
3104 * that we serialize against perf_install_in_context().
3106 if (!ctx->nr_events)
3109 perf_pmu_disable(ctx->pmu);
3111 * We want to keep the following priority order:
3112 * cpu pinned (that don't need to move), task pinned,
3113 * cpu flexible, task flexible.
3115 * However, if task's ctx is not carrying any pinned
3116 * events, no need to flip the cpuctx's events around.
3118 if (!list_empty(&ctx->pinned_groups))
3119 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3120 perf_event_sched_in(cpuctx, ctx, task);
3121 perf_pmu_enable(ctx->pmu);
3124 perf_ctx_unlock(cpuctx, ctx);
3128 * Called from scheduler to add the events of the current task
3129 * with interrupts disabled.
3131 * We restore the event value and then enable it.
3133 * This does not protect us against NMI, but enable()
3134 * sets the enabled bit in the control field of event _before_
3135 * accessing the event control register. If a NMI hits, then it will
3136 * keep the event running.
3138 void __perf_event_task_sched_in(struct task_struct *prev,
3139 struct task_struct *task)
3141 struct perf_event_context *ctx;
3145 * If cgroup events exist on this CPU, then we need to check if we have
3146 * to switch in PMU state; cgroup event are system-wide mode only.
3148 * Since cgroup events are CPU events, we must schedule these in before
3149 * we schedule in the task events.
3151 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
3152 perf_cgroup_sched_in(prev, task);
3154 for_each_task_context_nr(ctxn) {
3155 ctx = task->perf_event_ctxp[ctxn];
3159 perf_event_context_sched_in(ctx, task);
3162 if (atomic_read(&nr_switch_events))
3163 perf_event_switch(task, prev, true);
3165 if (__this_cpu_read(perf_sched_cb_usages))
3166 perf_pmu_sched_task(prev, task, true);
3169 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
3171 u64 frequency = event->attr.sample_freq;
3172 u64 sec = NSEC_PER_SEC;
3173 u64 divisor, dividend;
3175 int count_fls, nsec_fls, frequency_fls, sec_fls;
3177 count_fls = fls64(count);
3178 nsec_fls = fls64(nsec);
3179 frequency_fls = fls64(frequency);
3183 * We got @count in @nsec, with a target of sample_freq HZ
3184 * the target period becomes:
3187 * period = -------------------
3188 * @nsec * sample_freq
3193 * Reduce accuracy by one bit such that @a and @b converge
3194 * to a similar magnitude.
3196 #define REDUCE_FLS(a, b) \
3198 if (a##_fls > b##_fls) { \
3208 * Reduce accuracy until either term fits in a u64, then proceed with
3209 * the other, so that finally we can do a u64/u64 division.
3211 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
3212 REDUCE_FLS(nsec, frequency);
3213 REDUCE_FLS(sec, count);
3216 if (count_fls + sec_fls > 64) {
3217 divisor = nsec * frequency;
3219 while (count_fls + sec_fls > 64) {
3220 REDUCE_FLS(count, sec);
3224 dividend = count * sec;
3226 dividend = count * sec;
3228 while (nsec_fls + frequency_fls > 64) {
3229 REDUCE_FLS(nsec, frequency);
3233 divisor = nsec * frequency;
3239 return div64_u64(dividend, divisor);
3242 static DEFINE_PER_CPU(int, perf_throttled_count);
3243 static DEFINE_PER_CPU(u64, perf_throttled_seq);
3245 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
3247 struct hw_perf_event *hwc = &event->hw;
3248 s64 period, sample_period;
3251 period = perf_calculate_period(event, nsec, count);
3253 delta = (s64)(period - hwc->sample_period);
3254 delta = (delta + 7) / 8; /* low pass filter */
3256 sample_period = hwc->sample_period + delta;
3261 hwc->sample_period = sample_period;
3263 if (local64_read(&hwc->period_left) > 8*sample_period) {
3265 event->pmu->stop(event, PERF_EF_UPDATE);
3267 local64_set(&hwc->period_left, 0);
3270 event->pmu->start(event, PERF_EF_RELOAD);
3275 * combine freq adjustment with unthrottling to avoid two passes over the
3276 * events. At the same time, make sure, having freq events does not change
3277 * the rate of unthrottling as that would introduce bias.
3279 static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3282 struct perf_event *event;
3283 struct hw_perf_event *hwc;
3284 u64 now, period = TICK_NSEC;
3288 * only need to iterate over all events iff:
3289 * - context have events in frequency mode (needs freq adjust)
3290 * - there are events to unthrottle on this cpu
3292 if (!(ctx->nr_freq || needs_unthr))
3295 raw_spin_lock(&ctx->lock);
3296 perf_pmu_disable(ctx->pmu);
3298 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3299 if (event->state != PERF_EVENT_STATE_ACTIVE)
3302 if (!event_filter_match(event))
3305 perf_pmu_disable(event->pmu);
3309 if (hwc->interrupts == MAX_INTERRUPTS) {
3310 hwc->interrupts = 0;
3311 perf_log_throttle(event, 1);
3312 event->pmu->start(event, 0);
3315 if (!event->attr.freq || !event->attr.sample_freq)
3319 * stop the event and update event->count
3321 event->pmu->stop(event, PERF_EF_UPDATE);
3323 now = local64_read(&event->count);
3324 delta = now - hwc->freq_count_stamp;
3325 hwc->freq_count_stamp = now;
3329 * reload only if value has changed
3330 * we have stopped the event so tell that
3331 * to perf_adjust_period() to avoid stopping it
3335 perf_adjust_period(event, period, delta, false);
3337 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
3339 perf_pmu_enable(event->pmu);
3342 perf_pmu_enable(ctx->pmu);
3343 raw_spin_unlock(&ctx->lock);
3347 * Round-robin a context's events:
3349 static void rotate_ctx(struct perf_event_context *ctx)
3352 * Rotate the first entry last of non-pinned groups. Rotation might be
3353 * disabled by the inheritance code.
3355 if (!ctx->rotate_disable)
3356 list_rotate_left(&ctx->flexible_groups);
3359 static int perf_rotate_context(struct perf_cpu_context *cpuctx)
3361 struct perf_event_context *ctx = NULL;
3364 if (cpuctx->ctx.nr_events) {
3365 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3369 ctx = cpuctx->task_ctx;
3370 if (ctx && ctx->nr_events) {
3371 if (ctx->nr_events != ctx->nr_active)
3378 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
3379 perf_pmu_disable(cpuctx->ctx.pmu);
3381 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3383 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
3385 rotate_ctx(&cpuctx->ctx);
3389 perf_event_sched_in(cpuctx, ctx, current);
3391 perf_pmu_enable(cpuctx->ctx.pmu);
3392 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
3398 void perf_event_task_tick(void)
3400 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3401 struct perf_event_context *ctx, *tmp;
3404 lockdep_assert_irqs_disabled();
3406 __this_cpu_inc(perf_throttled_seq);
3407 throttled = __this_cpu_xchg(perf_throttled_count, 0);
3408 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
3410 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
3411 perf_adjust_freq_unthr_context(ctx, throttled);
3414 static int event_enable_on_exec(struct perf_event *event,
3415 struct perf_event_context *ctx)
3417 if (!event->attr.enable_on_exec)
3420 event->attr.enable_on_exec = 0;
3421 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3424 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
3430 * Enable all of a task's events that have been marked enable-on-exec.
3431 * This expects task == current.
3433 static void perf_event_enable_on_exec(int ctxn)
3435 struct perf_event_context *ctx, *clone_ctx = NULL;
3436 enum event_type_t event_type = 0;
3437 struct perf_cpu_context *cpuctx;
3438 struct perf_event *event;
3439 unsigned long flags;
3442 local_irq_save(flags);
3443 ctx = current->perf_event_ctxp[ctxn];
3444 if (!ctx || !ctx->nr_events)
3447 cpuctx = __get_cpu_context(ctx);
3448 perf_ctx_lock(cpuctx, ctx);
3449 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
3450 list_for_each_entry(event, &ctx->event_list, event_entry) {
3451 enabled |= event_enable_on_exec(event, ctx);
3452 event_type |= get_event_type(event);
3456 * Unclone and reschedule this context if we enabled any event.
3459 clone_ctx = unclone_ctx(ctx);
3460 ctx_resched(cpuctx, ctx, event_type);
3462 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
3464 perf_ctx_unlock(cpuctx, ctx);
3467 local_irq_restore(flags);
3473 struct perf_read_data {
3474 struct perf_event *event;
3479 static int __perf_event_read_cpu(struct perf_event *event, int event_cpu)
3481 u16 local_pkg, event_pkg;
3483 if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) {
3484 int local_cpu = smp_processor_id();
3486 event_pkg = topology_physical_package_id(event_cpu);
3487 local_pkg = topology_physical_package_id(local_cpu);
3489 if (event_pkg == local_pkg)
3497 * Cross CPU call to read the hardware event
3499 static void __perf_event_read(void *info)
3501 struct perf_read_data *data = info;
3502 struct perf_event *sub, *event = data->event;
3503 struct perf_event_context *ctx = event->ctx;
3504 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
3505 struct pmu *pmu = event->pmu;
3508 * If this is a task context, we need to check whether it is
3509 * the current task context of this cpu. If not it has been
3510 * scheduled out before the smp call arrived. In that case
3511 * event->count would have been updated to a recent sample
3512 * when the event was scheduled out.
3514 if (ctx->task && cpuctx->task_ctx != ctx)
3517 raw_spin_lock(&ctx->lock);
3518 if (ctx->is_active & EVENT_TIME) {
3519 update_context_time(ctx);
3520 update_cgrp_time_from_event(event);
3523 perf_event_update_time(event);
3525 perf_event_update_sibling_time(event);
3527 if (event->state != PERF_EVENT_STATE_ACTIVE)
3536 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3540 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3541 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3543 * Use sibling's PMU rather than @event's since
3544 * sibling could be on different (eg: software) PMU.
3546 sub->pmu->read(sub);
3550 data->ret = pmu->commit_txn(pmu);
3553 raw_spin_unlock(&ctx->lock);
3556 static inline u64 perf_event_count(struct perf_event *event)
3558 return local64_read(&event->count) + atomic64_read(&event->child_count);
3562 * NMI-safe method to read a local event, that is an event that
3564 * - either for the current task, or for this CPU
3565 * - does not have inherit set, for inherited task events
3566 * will not be local and we cannot read them atomically
3567 * - must not have a pmu::count method
3569 int perf_event_read_local(struct perf_event *event, u64 *value,
3570 u64 *enabled, u64 *running)
3572 unsigned long flags;
3576 * Disabling interrupts avoids all counter scheduling (context
3577 * switches, timer based rotation and IPIs).
3579 local_irq_save(flags);
3582 * It must not be an event with inherit set, we cannot read
3583 * all child counters from atomic context.
3585 if (event->attr.inherit) {
3590 /* If this is a per-task event, it must be for current */
3591 if ((event->attach_state & PERF_ATTACH_TASK) &&
3592 event->hw.target != current) {
3597 /* If this is a per-CPU event, it must be for this CPU */
3598 if (!(event->attach_state & PERF_ATTACH_TASK) &&
3599 event->cpu != smp_processor_id()) {
3605 * If the event is currently on this CPU, its either a per-task event,
3606 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3609 if (event->oncpu == smp_processor_id())
3610 event->pmu->read(event);
3612 *value = local64_read(&event->count);
3613 if (enabled || running) {
3614 u64 now = event->shadow_ctx_time + perf_clock();
3615 u64 __enabled, __running;
3617 __perf_update_times(event, now, &__enabled, &__running);
3619 *enabled = __enabled;
3621 *running = __running;
3624 local_irq_restore(flags);
3629 static int perf_event_read(struct perf_event *event, bool group)
3631 enum perf_event_state state = READ_ONCE(event->state);
3632 int event_cpu, ret = 0;