Lines Matching +full:throttle +full:- +full:period +full:- +full:us

1 // SPDX-License-Identifier: GPL-2.0
6 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
7 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra
75 struct task_struct *p = tfc->p; in remote_function()
78 /* -EAGAIN */ in remote_function()
87 tfc->ret = -ESRCH; /* No such (running) process */ in remote_function()
92 tfc->ret = tfc->func(tfc->info); in remote_function()
96 * task_function_call - call a function on the cpu on which a task runs
106 * returns @func return value or -ESRCH or -ENXIO when the process isn't running
115 .ret = -EAGAIN, in task_function_call()
125 if (ret != -EAGAIN) in task_function_call()
135 * cpu_function_call - call a function on the cpu
142 * returns: @func return value or -ENXIO when the cpu is offline
150 .ret = -ENXIO, /* No such CPU */ in cpu_function_call()
161 raw_spin_lock(&cpuctx->ctx.lock); in perf_ctx_lock()
163 raw_spin_lock(&ctx->lock); in perf_ctx_lock()
170 raw_spin_unlock(&ctx->lock); in perf_ctx_unlock()
171 raw_spin_unlock(&cpuctx->ctx.lock); in perf_ctx_unlock()
174 #define TASK_TOMBSTONE ((void *)-1L)
178 return READ_ONCE(event->owner) == TASK_TOMBSTONE; in is_kernel_event()
186 return this_cpu_ptr(&perf_cpu_context)->task_ctx; in perf_cpu_task_ctx()
192 * When !ctx->nr_events a task context will not be scheduled. This means
198 * - removing the last event from a task ctx; this is relatively straight
201 * - adding the first event to a task ctx; this is tricky because we cannot
202 * rely on ctx->is_active and therefore cannot use event_function_call().
205 * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set.
220 struct perf_event *event = efs->event; in event_function()
221 struct perf_event_context *ctx = event->ctx; in event_function()
223 struct perf_event_context *task_ctx = cpuctx->task_ctx; in event_function()
230 * Since we do the IPI call without holding ctx->lock things can have in event_function()
233 if (ctx->task) { in event_function()
234 if (ctx->task != current) { in event_function()
235 ret = -ESRCH; in event_function()
243 * above ctx->task != current test), therefore we must have in event_function()
244 * ctx->is_active here. in event_function()
246 WARN_ON_ONCE(!ctx->is_active); in event_function()
248 * And since we have ctx->is_active, cpuctx->task_ctx must in event_function()
253 WARN_ON_ONCE(&cpuctx->ctx != ctx); in event_function()
256 efs->func(event, cpuctx, ctx, efs->data); in event_function()
265 struct perf_event_context *ctx = event->ctx; in event_function_call()
266 struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */ in event_function_call()
274 if (!event->parent) { in event_function_call()
277 * stabilize the event->ctx relation. See in event_function_call()
280 lockdep_assert_held(&ctx->mutex); in event_function_call()
284 cpu_function_call(event->cpu, event_function, &efs); in event_function_call()
302 task = ctx->task; in event_function_call()
305 if (ctx->is_active) { in event_function_call()
322 struct perf_event_context *ctx = event->ctx; in event_function_local()
324 struct task_struct *task = READ_ONCE(ctx->task); in event_function_local()
338 task = ctx->task; in event_function_local()
348 if (ctx->is_active) { in event_function_local()
352 if (WARN_ON_ONCE(cpuctx->task_ctx != ctx)) in event_function_local()
356 WARN_ON_ONCE(&cpuctx->ctx != ctx); in event_function_local()
418 * -1 - not paranoid at all
419 * 0 - disallow raw tracepoint access for unpriv
420 * 1 - disallow cpu events for unpriv
421 * 2 - disallow kernel profiling for unpriv
466 return -EINVAL; in perf_proc_update_handler()
536 running_len -= running_len/NR_ACCUMULATED_SAMPLES; in perf_sample_event_took()
542 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us in perf_sample_event_took()
553 * Compute a throttle threshold 25% below the current duration. in perf_sample_event_took()
590 return event->clock(); in perf_event_clock()
596 * The basic idea is to use event->state to determine which (if any) time
618 struct perf_event *leader = event->group_leader; in __perf_effective_state()
620 if (leader->state <= PERF_EVENT_STATE_OFF) in __perf_effective_state()
621 return leader->state; in __perf_effective_state()
623 return event->state; in __perf_effective_state()
630 u64 delta = now - event->tstamp; in __perf_update_times()
632 *enabled = event->total_time_enabled; in __perf_update_times()
636 *running = event->total_time_running; in __perf_update_times()
645 __perf_update_times(event, now, &event->total_time_enabled, in perf_event_update_time()
646 &event->total_time_running); in perf_event_update_time()
647 event->tstamp = now; in perf_event_update_time()
661 if (event->state == state) in perf_event_set_state()
669 if ((event->state < 0) ^ (state < 0)) in perf_event_set_state()
672 WRITE_ONCE(event->state, state); in perf_event_set_state()
676 * UP store-release, load-acquire
696 list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { in perf_ctx_disable()
697 if (cgroup && !pmu_ctx->nr_cgroups) in perf_ctx_disable()
699 perf_pmu_disable(pmu_ctx->pmu); in perf_ctx_disable()
707 list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { in perf_ctx_enable()
708 if (cgroup && !pmu_ctx->nr_cgroups) in perf_ctx_enable()
710 perf_pmu_enable(pmu_ctx->pmu); in perf_ctx_enable()
725 if (!event->cgrp) in perf_cgroup_match()
729 if (!cpuctx->cgrp) in perf_cgroup_match()
738 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, in perf_cgroup_match()
739 event->cgrp->css.cgroup); in perf_cgroup_match()
744 css_put(&event->cgrp->css); in perf_detach_cgroup()
745 event->cgrp = NULL; in perf_detach_cgroup()
750 return event->cgrp != NULL; in is_cgroup_event()
757 t = per_cpu_ptr(event->cgrp->info, event->cpu); in perf_cgroup_event_time()
758 return t->time; in perf_cgroup_event_time()
765 t = per_cpu_ptr(event->cgrp->info, event->cpu); in perf_cgroup_event_time_now()
766 if (!__load_acquire(&t->active)) in perf_cgroup_event_time_now()
767 return t->time; in perf_cgroup_event_time_now()
768 now += READ_ONCE(t->timeoffset); in perf_cgroup_event_time_now()
775 info->time += now - info->timestamp; in __update_cgrp_time()
776 info->timestamp = now; in __update_cgrp_time()
780 WRITE_ONCE(info->timeoffset, info->time - info->timestamp); in __update_cgrp_time()
785 struct perf_cgroup *cgrp = cpuctx->cgrp; in update_cgrp_time_from_cpuctx()
792 for (css = &cgrp->css; css; css = css->parent) { in update_cgrp_time_from_cpuctx()
794 info = this_cpu_ptr(cgrp->info); in update_cgrp_time_from_cpuctx()
798 __store_release(&info->active, 0); in update_cgrp_time_from_cpuctx()
814 info = this_cpu_ptr(event->cgrp->info); in update_cgrp_time_from_event()
818 if (info->active) in update_cgrp_time_from_event()
825 struct perf_event_context *ctx = &cpuctx->ctx; in perf_cgroup_set_timestamp()
826 struct perf_cgroup *cgrp = cpuctx->cgrp; in perf_cgroup_set_timestamp()
831 * ctx->lock held by caller in perf_cgroup_set_timestamp()
838 WARN_ON_ONCE(!ctx->nr_cgroups); in perf_cgroup_set_timestamp()
840 for (css = &cgrp->css; css; css = css->parent) { in perf_cgroup_set_timestamp()
842 info = this_cpu_ptr(cgrp->info); in perf_cgroup_set_timestamp()
843 __update_cgrp_time(info, ctx->timestamp, false); in perf_cgroup_set_timestamp()
844 __store_release(&info->active, 1); in perf_cgroup_set_timestamp()
857 * cpuctx->cgrp is set when the first cgroup event enabled, in perf_cgroup_switch()
860 if (READ_ONCE(cpuctx->cgrp) == NULL) in perf_cgroup_switch()
863 WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0); in perf_cgroup_switch()
866 if (READ_ONCE(cpuctx->cgrp) == cgrp) in perf_cgroup_switch()
869 perf_ctx_lock(cpuctx, cpuctx->task_ctx); in perf_cgroup_switch()
870 perf_ctx_disable(&cpuctx->ctx, true); in perf_cgroup_switch()
872 ctx_sched_out(&cpuctx->ctx, EVENT_ALL|EVENT_CGROUP); in perf_cgroup_switch()
878 cpuctx->cgrp = cgrp; in perf_cgroup_switch()
884 ctx_sched_in(&cpuctx->ctx, EVENT_ALL|EVENT_CGROUP); in perf_cgroup_switch()
886 perf_ctx_enable(&cpuctx->ctx, true); in perf_cgroup_switch()
887 perf_ctx_unlock(cpuctx, cpuctx->task_ctx); in perf_cgroup_switch()
901 for (heap_size = 1; css; css = css->parent) in perf_cgroup_ensure_storage()
906 if (heap_size <= cpuctx->heap_size) in perf_cgroup_ensure_storage()
912 ret = -ENOMEM; in perf_cgroup_ensure_storage()
916 raw_spin_lock_irq(&cpuctx->ctx.lock); in perf_cgroup_ensure_storage()
917 if (cpuctx->heap_size < heap_size) { in perf_cgroup_ensure_storage()
918 swap(cpuctx->heap, storage); in perf_cgroup_ensure_storage()
919 if (storage == cpuctx->heap_default) in perf_cgroup_ensure_storage()
921 cpuctx->heap_size = heap_size; in perf_cgroup_ensure_storage()
923 raw_spin_unlock_irq(&cpuctx->ctx.lock); in perf_cgroup_ensure_storage()
941 return -EBADF; in perf_cgroup_connect()
943 css = css_tryget_online_from_dir(f.file->f_path.dentry, in perf_cgroup_connect()
955 event->cgrp = cgrp; in perf_cgroup_connect()
962 if (group_leader && group_leader->cgrp != cgrp) { in perf_cgroup_connect()
964 ret = -EINVAL; in perf_cgroup_connect()
979 event->pmu_ctx->nr_cgroups++; in perf_cgroup_event_enable()
982 * Because cgroup events are always per-cpu events, in perf_cgroup_event_enable()
983 * @ctx == &cpuctx->ctx. in perf_cgroup_event_enable()
987 if (ctx->nr_cgroups++) in perf_cgroup_event_enable()
990 cpuctx->cgrp = perf_cgroup_from_task(current, ctx); in perf_cgroup_event_enable()
1001 event->pmu_ctx->nr_cgroups--; in perf_cgroup_event_disable()
1004 * Because cgroup events are always per-cpu events, in perf_cgroup_event_disable()
1005 * @ctx == &cpuctx->ctx. in perf_cgroup_event_disable()
1009 if (--ctx->nr_cgroups) in perf_cgroup_event_disable()
1012 cpuctx->cgrp = NULL; in perf_cgroup_event_disable()
1044 return -EINVAL; in perf_cgroup_connect()
1095 raw_spin_lock(&cpc->hrtimer_lock); in perf_mux_hrtimer_handler()
1097 hrtimer_forward_now(hr, cpc->hrtimer_interval); in perf_mux_hrtimer_handler()
1099 cpc->hrtimer_active = 0; in perf_mux_hrtimer_handler()
1100 raw_spin_unlock(&cpc->hrtimer_lock); in perf_mux_hrtimer_handler()
1107 struct hrtimer *timer = &cpc->hrtimer; in __perf_mux_hrtimer_init()
1108 struct pmu *pmu = cpc->epc.pmu; in __perf_mux_hrtimer_init()
1115 interval = pmu->hrtimer_interval_ms; in __perf_mux_hrtimer_init()
1117 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; in __perf_mux_hrtimer_init()
1119 cpc->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval); in __perf_mux_hrtimer_init()
1121 raw_spin_lock_init(&cpc->hrtimer_lock); in __perf_mux_hrtimer_init()
1123 timer->function = perf_mux_hrtimer_handler; in __perf_mux_hrtimer_init()
1128 struct hrtimer *timer = &cpc->hrtimer; in perf_mux_hrtimer_restart()
1131 raw_spin_lock_irqsave(&cpc->hrtimer_lock, flags); in perf_mux_hrtimer_restart()
1132 if (!cpc->hrtimer_active) { in perf_mux_hrtimer_restart()
1133 cpc->hrtimer_active = 1; in perf_mux_hrtimer_restart()
1134 hrtimer_forward_now(timer, cpc->hrtimer_interval); in perf_mux_hrtimer_restart()
1137 raw_spin_unlock_irqrestore(&cpc->hrtimer_lock, flags); in perf_mux_hrtimer_restart()
1149 int *count = this_cpu_ptr(pmu->pmu_disable_count); in perf_pmu_disable()
1151 pmu->pmu_disable(pmu); in perf_pmu_disable()
1156 int *count = this_cpu_ptr(pmu->pmu_disable_count); in perf_pmu_enable()
1157 if (!--(*count)) in perf_pmu_enable()
1158 pmu->pmu_enable(pmu); in perf_pmu_enable()
1163 WARN_ON_ONCE(*this_cpu_ptr(pmu->pmu_disable_count) == 0); in perf_assert_pmu_disabled()
1168 refcount_inc(&ctx->refcount); in get_ctx()
1173 if (pmu->task_ctx_cache) in alloc_task_ctx_data()
1174 return kmem_cache_zalloc(pmu->task_ctx_cache, GFP_KERNEL); in alloc_task_ctx_data()
1181 if (pmu->task_ctx_cache && task_ctx_data) in free_task_ctx_data()
1182 kmem_cache_free(pmu->task_ctx_cache, task_ctx_data); in free_task_ctx_data()
1195 if (refcount_dec_and_test(&ctx->refcount)) { in put_ctx()
1196 if (ctx->parent_ctx) in put_ctx()
1197 put_ctx(ctx->parent_ctx); in put_ctx()
1198 if (ctx->task && ctx->task != TASK_TOMBSTONE) in put_ctx()
1199 put_task_struct(ctx->task); in put_ctx()
1200 call_rcu(&ctx->rcu_head, free_ctx); in put_ctx()
1214 * - perf_event_exit_task_context() [ child , 0 ]
1218 * - perf_event_init_context() [ parent, 0 ]
1226 * While it appears there is an obvious deadlock here -- the parent and child
1228 * life-time rules separate them. That is an exiting task cannot fork, and a
1231 * But remember that these are parent<->child context relations, and
1237 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1252 * However; because event->ctx can change while we're waiting to acquire
1253 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1269 * cpuctx->mutex / perf_event_context::mutex
1278 ctx = READ_ONCE(event->ctx); in perf_event_ctx_lock_nested()
1279 if (!refcount_inc_not_zero(&ctx->refcount)) { in perf_event_ctx_lock_nested()
1285 mutex_lock_nested(&ctx->mutex, nesting); in perf_event_ctx_lock_nested()
1286 if (event->ctx != ctx) { in perf_event_ctx_lock_nested()
1287 mutex_unlock(&ctx->mutex); in perf_event_ctx_lock_nested()
1304 mutex_unlock(&ctx->mutex); in perf_event_ctx_unlock()
1309 * This must be done under the ctx->lock, such as to serialize against
1311 * calling scheduler related locks and ctx->lock nests inside those.
1316 struct perf_event_context *parent_ctx = ctx->parent_ctx; in unclone_ctx()
1318 lockdep_assert_held(&ctx->lock); in unclone_ctx()
1321 ctx->parent_ctx = NULL; in unclone_ctx()
1322 ctx->generation++; in unclone_ctx()
1334 if (event->parent) in perf_event_pid_type()
1335 event = event->parent; in perf_event_pid_type()
1337 nr = __task_pid_nr_ns(p, type, event->ns); in perf_event_pid_type()
1338 /* avoid -1 if it is idle thread or runs in another ns */ in perf_event_pid_type()
1340 nr = -1; in perf_event_pid_type()
1360 u64 id = event->id; in primary_event_id()
1362 if (event->parent) in primary_event_id()
1363 id = event->parent->id; in primary_event_id()
1383 * part of the read side critical section was irqs-enabled -- see in perf_lock_task_context()
1386 * Since ctx->lock nests under rq->lock we must ensure the entire read in perf_lock_task_context()
1391 ctx = rcu_dereference(task->perf_event_ctxp); in perf_lock_task_context()
1395 * get swapped for another underneath us by in perf_lock_task_context()
1397 * rcu_read_lock() protects us from any context in perf_lock_task_context()
1401 * can't get swapped on us any more. in perf_lock_task_context()
1403 raw_spin_lock(&ctx->lock); in perf_lock_task_context()
1404 if (ctx != rcu_dereference(task->perf_event_ctxp)) { in perf_lock_task_context()
1405 raw_spin_unlock(&ctx->lock); in perf_lock_task_context()
1411 if (ctx->task == TASK_TOMBSTONE || in perf_lock_task_context()
1412 !refcount_inc_not_zero(&ctx->refcount)) { in perf_lock_task_context()
1413 raw_spin_unlock(&ctx->lock); in perf_lock_task_context()
1416 WARN_ON_ONCE(ctx->task != task); in perf_lock_task_context()
1438 ++ctx->pin_count; in perf_pin_task_context()
1439 raw_spin_unlock_irqrestore(&ctx->lock, flags); in perf_pin_task_context()
1448 raw_spin_lock_irqsave(&ctx->lock, flags); in perf_unpin_context()
1449 --ctx->pin_count; in perf_unpin_context()
1450 raw_spin_unlock_irqrestore(&ctx->lock, flags); in perf_unpin_context()
1460 lockdep_assert_held(&ctx->lock); in __update_context_time()
1463 ctx->time += now - ctx->timestamp; in __update_context_time()
1464 ctx->timestamp = now; in __update_context_time()
1467 * The above: time' = time + (now - timestamp), can be re-arranged in __update_context_time()
1468 * into: time` = now + (time - timestamp), which gives a single value in __update_context_time()
1472 * it's (obviously) not possible to acquire ctx->lock in order to read in __update_context_time()
1475 WRITE_ONCE(ctx->timeoffset, ctx->time - ctx->timestamp); in __update_context_time()
1485 struct perf_event_context *ctx = event->ctx; in perf_event_time()
1493 return ctx->time; in perf_event_time()
1498 struct perf_event_context *ctx = event->ctx; in perf_event_time_now()
1506 if (!(__load_acquire(&ctx->is_active) & EVENT_TIME)) in perf_event_time_now()
1507 return ctx->time; in perf_event_time_now()
1509 now += READ_ONCE(ctx->timeoffset); in perf_event_time_now()
1515 struct perf_event_context *ctx = event->ctx; in get_event_type()
1518 lockdep_assert_held(&ctx->lock); in get_event_type()
1524 if (event->group_leader != event) in get_event_type()
1525 event = event->group_leader; in get_event_type()
1527 event_type = event->attr.pinned ? EVENT_PINNED : EVENT_FLEXIBLE; in get_event_type()
1528 if (!ctx->task) in get_event_type()
1539 RB_CLEAR_NODE(&event->group_node); in init_event_group()
1540 event->group_index = 0; in init_event_group()
1550 if (event->attr.pinned) in get_event_groups()
1551 return &ctx->pinned_groups; in get_event_groups()
1553 return &ctx->flexible_groups; in get_event_groups()
1561 groups->tree = RB_ROOT; in perf_event_groups_init()
1562 groups->index = 0; in perf_event_groups_init()
1570 if (event->cgrp) in event_cgroup()
1571 cgroup = event->cgrp->css.cgroup; in event_cgroup()
1588 if (left_cpu < right->cpu) in perf_event_groups_cmp()
1589 return -1; in perf_event_groups_cmp()
1590 if (left_cpu > right->cpu) in perf_event_groups_cmp()
1594 if (left_pmu < right->pmu_ctx->pmu) in perf_event_groups_cmp()
1595 return -1; in perf_event_groups_cmp()
1596 if (left_pmu > right->pmu_ctx->pmu) in perf_event_groups_cmp()
1610 return -1; in perf_event_groups_cmp()
1621 return -1; in perf_event_groups_cmp()
1628 if (left_group_index < right->group_index) in perf_event_groups_cmp()
1629 return -1; in perf_event_groups_cmp()
1630 if (left_group_index > right->group_index) in perf_event_groups_cmp()
1642 return perf_event_groups_cmp(e->cpu, e->pmu_ctx->pmu, event_cgroup(e), in __group_less()
1643 e->group_index, __node_2_pe(b)) < 0; in __group_less()
1658 return perf_event_groups_cmp(a->cpu, a->pmu, a->cgroup, b->group_index, b); in __group_cmp()
1668 return perf_event_groups_cmp(a->cpu, a->pmu, event_cgroup(b), in __group_cmp_ignore_cgroup()
1669 b->group_index, b); in __group_cmp_ignore_cgroup()
1674 * {@event->cpu, @event->pmu_ctx->pmu, event_cgroup(@event), ++@groups->index}
1681 event->group_index = ++groups->index; in perf_event_groups_insert()
1683 rb_add(&event->group_node, &groups->tree, __group_less); in perf_event_groups_insert()
1705 WARN_ON_ONCE(RB_EMPTY_NODE(&event->group_node) || in perf_event_groups_delete()
1706 RB_EMPTY_ROOT(&groups->tree)); in perf_event_groups_delete()
1708 rb_erase(&event->group_node, &groups->tree); in perf_event_groups_delete()
1738 node = rb_find_first(&key, &groups->tree, __group_cmp); in perf_event_groups_first()
1749 .cpu = event->cpu, in perf_event_groups_next()
1755 next = rb_next_match(&key, &event->group_node, __group_cmp); in perf_event_groups_next()
1770 for (event = rb_entry_safe(rb_first(&((groups)->tree)), \
1772 event = rb_entry_safe(rb_next(&event->group_node), \
1777 * Must be called with ctx->mutex and ctx->lock held.
1782 lockdep_assert_held(&ctx->lock); in list_add_event()
1784 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); in list_add_event()
1785 event->attach_state |= PERF_ATTACH_CONTEXT; in list_add_event()
1787 event->tstamp = perf_event_time(event); in list_add_event()
1794 if (event->group_leader == event) { in list_add_event()
1795 event->group_caps = event->event_caps; in list_add_event()
1799 list_add_rcu(&event->event_entry, &ctx->event_list); in list_add_event()
1800 ctx->nr_events++; in list_add_event()
1801 if (event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT) in list_add_event()
1802 ctx->nr_user++; in list_add_event()
1803 if (event->attr.inherit_stat) in list_add_event()
1804 ctx->nr_stat++; in list_add_event()
1806 if (event->state > PERF_EVENT_STATE_OFF) in list_add_event()
1809 ctx->generation++; in list_add_event()
1810 event->pmu_ctx->nr_events++; in list_add_event()
1818 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : in perf_event__state_init()
1858 size += sizeof(data->ip); in __perf_event_header_size()
1861 size += sizeof(data->addr); in __perf_event_header_size()
1864 size += sizeof(data->period); in __perf_event_header_size()
1867 size += sizeof(data->weight.full); in __perf_event_header_size()
1870 size += event->read_size; in __perf_event_header_size()
1873 size += sizeof(data->data_src.val); in __perf_event_header_size()
1876 size += sizeof(data->txn); in __perf_event_header_size()
1879 size += sizeof(data->phys_addr); in __perf_event_header_size()
1882 size += sizeof(data->cgroup); in __perf_event_header_size()
1885 size += sizeof(data->data_page_size); in __perf_event_header_size()
1888 size += sizeof(data->code_page_size); in __perf_event_header_size()
1890 event->header_size = size; in __perf_event_header_size()
1899 event->read_size = in perf_event__header_size()
1900 __perf_event_read_size(event->attr.read_format, in perf_event__header_size()
1901 event->group_leader->nr_siblings); in perf_event__header_size()
1902 __perf_event_header_size(event, event->attr.sample_type); in perf_event__header_size()
1908 u64 sample_type = event->attr.sample_type; in perf_event__id_header_size()
1912 size += sizeof(data->tid_entry); in perf_event__id_header_size()
1915 size += sizeof(data->time); in perf_event__id_header_size()
1918 size += sizeof(data->id); in perf_event__id_header_size()
1921 size += sizeof(data->id); in perf_event__id_header_size()
1924 size += sizeof(data->stream_id); in perf_event__id_header_size()
1927 size += sizeof(data->cpu_entry); in perf_event__id_header_size()
1929 event->id_header_size = size; in perf_event__id_header_size()
1938 * depends on per-event read_format, also (re)check the existing events.
1945 struct perf_event *sibling, *group_leader = event->group_leader; in perf_event_validate_size()
1947 if (__perf_event_read_size(event->attr.read_format, in perf_event_validate_size()
1948 group_leader->nr_siblings + 1) > 16*1024) in perf_event_validate_size()
1951 if (__perf_event_read_size(group_leader->attr.read_format, in perf_event_validate_size()
1952 group_leader->nr_siblings + 1) > 16*1024) in perf_event_validate_size()
1956 * When creating a new group leader, group_leader->ctx is initialized in perf_event_validate_size()
1958 * for_each_sibling_event() until group_leader->ctx is set. A new group in perf_event_validate_size()
1960 * the non-existent siblings. in perf_event_validate_size()
1966 if (__perf_event_read_size(sibling->attr.read_format, in perf_event_validate_size()
1967 group_leader->nr_siblings + 1) > 16*1024) in perf_event_validate_size()
1976 struct perf_event *group_leader = event->group_leader, *pos; in perf_group_attach()
1978 lockdep_assert_held(&event->ctx->lock); in perf_group_attach()
1984 if (event->attach_state & PERF_ATTACH_GROUP) in perf_group_attach()
1987 event->attach_state |= PERF_ATTACH_GROUP; in perf_group_attach()
1992 WARN_ON_ONCE(group_leader->ctx != event->ctx); in perf_group_attach()
1994 group_leader->group_caps &= event->event_caps; in perf_group_attach()
1996 list_add_tail(&event->sibling_list, &group_leader->sibling_list); in perf_group_attach()
1997 group_leader->nr_siblings++; in perf_group_attach()
1998 group_leader->group_generation++; in perf_group_attach()
2008 * Must be called with ctx->mutex and ctx->lock held.
2013 WARN_ON_ONCE(event->ctx != ctx); in list_del_event()
2014 lockdep_assert_held(&ctx->lock); in list_del_event()
2017 * We can have double detach due to exit/hot-unplug + close. in list_del_event()
2019 if (!(event->attach_state & PERF_ATTACH_CONTEXT)) in list_del_event()
2022 event->attach_state &= ~PERF_ATTACH_CONTEXT; in list_del_event()
2024 ctx->nr_events--; in list_del_event()
2025 if (event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT) in list_del_event()
2026 ctx->nr_user--; in list_del_event()
2027 if (event->attr.inherit_stat) in list_del_event()
2028 ctx->nr_stat--; in list_del_event()
2030 list_del_rcu(&event->event_entry); in list_del_event()
2032 if (event->group_leader == event) in list_del_event()
2039 * of error state is by explicit re-enabling in list_del_event()
2042 if (event->state > PERF_EVENT_STATE_OFF) { in list_del_event()
2047 ctx->generation++; in list_del_event()
2048 event->pmu_ctx->nr_events--; in list_del_event()
2057 if (!event->pmu->aux_output_match) in perf_aux_output_match()
2060 return event->pmu->aux_output_match(aux_event); in perf_aux_output_match()
2069 struct perf_event_context *ctx = event->ctx; in perf_put_aux_event()
2075 if (event->aux_event) { in perf_put_aux_event()
2076 iter = event->aux_event; in perf_put_aux_event()
2077 event->aux_event = NULL; in perf_put_aux_event()
2086 for_each_sibling_event(iter, event->group_leader) { in perf_put_aux_event()
2087 if (iter->aux_event != event) in perf_put_aux_event()
2090 iter->aux_event = NULL; in perf_put_aux_event()
2105 return !!event->attr.aux_output || !!event->attr.aux_sample_size; in perf_need_aux_event()
2123 if (event->attr.aux_output && event->attr.aux_sample_size) in perf_get_aux_event()
2126 if (event->attr.aux_output && in perf_get_aux_event()
2130 if (event->attr.aux_sample_size && !group_leader->pmu->snapshot_aux) in perf_get_aux_event()
2133 if (!atomic_long_inc_not_zero(&group_leader->refcount)) in perf_get_aux_event()
2142 event->aux_event = group_leader; in perf_get_aux_event()
2149 return event->attr.pinned ? &event->pmu_ctx->pinned_active : in get_event_list()
2150 &event->pmu_ctx->flexible_active; in get_event_list()
2161 event_sched_out(event, event->ctx); in perf_remove_sibling_event()
2167 struct perf_event *leader = event->group_leader; in perf_group_detach()
2169 struct perf_event_context *ctx = event->ctx; in perf_group_detach()
2171 lockdep_assert_held(&ctx->lock); in perf_group_detach()
2174 * We can have double detach due to exit/hot-unplug + close. in perf_group_detach()
2176 if (!(event->attach_state & PERF_ATTACH_GROUP)) in perf_group_detach()
2179 event->attach_state &= ~PERF_ATTACH_GROUP; in perf_group_detach()
2187 list_del_init(&event->sibling_list); in perf_group_detach()
2188 event->group_leader->nr_siblings--; in perf_group_detach()
2189 event->group_leader->group_generation++; in perf_group_detach()
2198 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, sibling_list) { in perf_group_detach()
2200 if (sibling->event_caps & PERF_EV_CAP_SIBLING) in perf_group_detach()
2203 sibling->group_leader = sibling; in perf_group_detach()
2204 list_del_init(&sibling->sibling_list); in perf_group_detach()
2207 sibling->group_caps = event->group_caps; in perf_group_detach()
2209 if (sibling->attach_state & PERF_ATTACH_CONTEXT) { in perf_group_detach()
2210 add_event_to_groups(sibling, event->ctx); in perf_group_detach()
2212 if (sibling->state == PERF_EVENT_STATE_ACTIVE) in perf_group_detach()
2213 list_add_tail(&sibling->active_list, get_event_list(sibling)); in perf_group_detach()
2216 WARN_ON_ONCE(sibling->ctx != event->ctx); in perf_group_detach()
2230 struct perf_event *parent_event = event->parent; in perf_child_detach()
2232 if (!(event->attach_state & PERF_ATTACH_CHILD)) in perf_child_detach()
2235 event->attach_state &= ~PERF_ATTACH_CHILD; in perf_child_detach()
2240 lockdep_assert_held(&parent_event->child_mutex); in perf_child_detach()
2243 list_del_init(&event->child_list); in perf_child_detach()
2248 return event->state == PERF_EVENT_STATE_DEAD; in is_orphaned_event()
2254 return (event->cpu == -1 || event->cpu == smp_processor_id()) && in event_filter_match()
2261 struct perf_event_pmu_context *epc = event->pmu_ctx; in event_sched_out()
2262 struct perf_cpu_pmu_context *cpc = this_cpu_ptr(epc->pmu->cpu_pmu_context); in event_sched_out()
2265 // XXX cpc serialization, probably per-cpu IRQ disabled in event_sched_out()
2267 WARN_ON_ONCE(event->ctx != ctx); in event_sched_out()
2268 lockdep_assert_held(&ctx->lock); in event_sched_out()
2270 if (event->state != PERF_EVENT_STATE_ACTIVE) in event_sched_out()
2278 list_del_init(&event->active_list); in event_sched_out()
2280 perf_pmu_disable(event->pmu); in event_sched_out()
2282 event->pmu->del(event, 0); in event_sched_out()
2283 event->oncpu = -1; in event_sched_out()
2285 if (event->pending_disable) { in event_sched_out()
2286 event->pending_disable = 0; in event_sched_out()
2291 if (event->pending_sigtrap) { in event_sched_out()
2292 event->pending_sigtrap = 0; in event_sched_out()
2294 !event->pending_work && in event_sched_out()
2295 !task_work_add(current, &event->pending_task, TWA_RESUME)) { in event_sched_out()
2296 event->pending_work = 1; in event_sched_out()
2298 local_dec(&event->ctx->nr_pending); in event_sched_out()
2305 cpc->active_oncpu--; in event_sched_out()
2306 if (event->attr.freq && event->attr.sample_freq) in event_sched_out()
2307 ctx->nr_freq--; in event_sched_out()
2308 if (event->attr.exclusive || !cpc->active_oncpu) in event_sched_out()
2309 cpc->exclusive = 0; in event_sched_out()
2311 perf_pmu_enable(event->pmu); in event_sched_out()
2319 if (group_event->state != PERF_EVENT_STATE_ACTIVE) in group_sched_out()
2322 perf_assert_pmu_disabled(group_event->pmu_ctx->pmu); in group_sched_out()
2349 struct perf_event_pmu_context *pmu_ctx = event->pmu_ctx; in __perf_remove_from_context()
2352 if (ctx->is_active & EVENT_TIME) { in __perf_remove_from_context()
2362 event->pending_disable = 1; in __perf_remove_from_context()
2370 event->state = PERF_EVENT_STATE_DEAD; in __perf_remove_from_context()
2372 if (!pmu_ctx->nr_events) { in __perf_remove_from_context()
2373 pmu_ctx->rotate_necessary = 0; in __perf_remove_from_context()
2375 if (ctx->task && ctx->is_active) { in __perf_remove_from_context()
2378 cpc = this_cpu_ptr(pmu_ctx->pmu->cpu_pmu_context); in __perf_remove_from_context()
2379 WARN_ON_ONCE(cpc->task_epc && cpc->task_epc != pmu_ctx); in __perf_remove_from_context()
2380 cpc->task_epc = NULL; in __perf_remove_from_context()
2384 if (!ctx->nr_events && ctx->is_active) { in __perf_remove_from_context()
2385 if (ctx == &cpuctx->ctx) in __perf_remove_from_context()
2388 ctx->is_active = 0; in __perf_remove_from_context()
2389 if (ctx->task) { in __perf_remove_from_context()
2390 WARN_ON_ONCE(cpuctx->task_ctx != ctx); in __perf_remove_from_context()
2391 cpuctx->task_ctx = NULL; in __perf_remove_from_context()
2399 * If event->ctx is a cloned context, callers must make sure that
2400 * every task struct that event->ctx->task could possibly point to
2402 * that only calls us on the top-level context, which can't be a clone.
2408 struct perf_event_context *ctx = event->ctx; in perf_remove_from_context()
2410 lockdep_assert_held(&ctx->mutex); in perf_remove_from_context()
2417 raw_spin_lock_irq(&ctx->lock); in perf_remove_from_context()
2418 if (!ctx->is_active) { in perf_remove_from_context()
2421 raw_spin_unlock_irq(&ctx->lock); in perf_remove_from_context()
2424 raw_spin_unlock_irq(&ctx->lock); in perf_remove_from_context()
2437 if (event->state < PERF_EVENT_STATE_INACTIVE) in __perf_event_disable()
2440 if (ctx->is_active & EVENT_TIME) { in __perf_event_disable()
2445 perf_pmu_disable(event->pmu_ctx->pmu); in __perf_event_disable()
2447 if (event == event->group_leader) in __perf_event_disable()
2455 perf_pmu_enable(event->pmu_ctx->pmu); in __perf_event_disable()
2461 * If event->ctx is a cloned context, callers must make sure that
2462 * every task struct that event->ctx->task could possibly point to
2465 * hold the top-level event's child_mutex, so any descendant that
2468 * When called from perf_pending_irq it's OK because event->ctx
2474 struct perf_event_context *ctx = event->ctx; in _perf_event_disable()
2476 raw_spin_lock_irq(&ctx->lock); in _perf_event_disable()
2477 if (event->state <= PERF_EVENT_STATE_OFF) { in _perf_event_disable()
2478 raw_spin_unlock_irq(&ctx->lock); in _perf_event_disable()
2481 raw_spin_unlock_irq(&ctx->lock); in _perf_event_disable()
2507 event->pending_disable = 1; in perf_event_disable_inatomic()
2508 irq_work_queue(&event->pending_irq); in perf_event_disable_inatomic()
2519 struct perf_event_pmu_context *epc = event->pmu_ctx; in event_sched_in()
2520 struct perf_cpu_pmu_context *cpc = this_cpu_ptr(epc->pmu->cpu_pmu_context); in event_sched_in()
2523 WARN_ON_ONCE(event->ctx != ctx); in event_sched_in()
2525 lockdep_assert_held(&ctx->lock); in event_sched_in()
2527 if (event->state <= PERF_EVENT_STATE_OFF) in event_sched_in()
2530 WRITE_ONCE(event->oncpu, smp_processor_id()); in event_sched_in()
2534 * ->oncpu if it sees ACTIVE. in event_sched_in()
2544 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { in event_sched_in()
2546 event->hw.interrupts = 0; in event_sched_in()
2549 perf_pmu_disable(event->pmu); in event_sched_in()
2553 if (event->pmu->add(event, PERF_EF_START)) { in event_sched_in()
2555 event->oncpu = -1; in event_sched_in()
2556 ret = -EAGAIN; in event_sched_in()
2561 cpc->active_oncpu++; in event_sched_in()
2562 if (event->attr.freq && event->attr.sample_freq) in event_sched_in()
2563 ctx->nr_freq++; in event_sched_in()
2565 if (event->attr.exclusive) in event_sched_in()
2566 cpc->exclusive = 1; in event_sched_in()
2569 perf_pmu_enable(event->pmu); in event_sched_in()
2578 struct pmu *pmu = group_event->pmu_ctx->pmu; in group_sched_in()
2580 if (group_event->state == PERF_EVENT_STATE_OFF) in group_sched_in()
2583 pmu->start_txn(pmu, PERF_PMU_TXN_ADD); in group_sched_in()
2598 if (!pmu->commit_txn(pmu)) in group_sched_in()
2616 pmu->cancel_txn(pmu); in group_sched_in()
2617 return -EAGAIN; in group_sched_in()
2625 struct perf_event_pmu_context *epc = event->pmu_ctx; in group_can_go_on()
2626 struct perf_cpu_pmu_context *cpc = this_cpu_ptr(epc->pmu->cpu_pmu_context); in group_can_go_on()
2631 if (event->group_caps & PERF_EV_CAP_SOFTWARE) in group_can_go_on()
2637 if (cpc->exclusive) in group_can_go_on()
2643 if (event->attr.exclusive && !list_empty(get_event_list(event))) in group_can_go_on()
2664 if (!cpuctx->task_ctx) in task_ctx_sched_out()
2667 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) in task_ctx_sched_out()
2676 ctx_sched_in(&cpuctx->ctx, EVENT_PINNED); in perf_event_sched_in()
2679 ctx_sched_in(&cpuctx->ctx, EVENT_FLEXIBLE); in perf_event_sched_in()
2686 * - CPU pinned (EVENT_CPU | EVENT_PINNED)
2687 * - task pinned (EVENT_PINNED)
2688 * - CPU flexible (EVENT_CPU | EVENT_FLEXIBLE)
2689 * - task flexible (EVENT_FLEXIBLE).
2719 perf_ctx_disable(&cpuctx->ctx, false); in ctx_resched()
2728 * - EVENT_CPU: schedule out corresponding groups; in ctx_resched()
2729 * - EVENT_PINNED task events: schedule out EVENT_FLEXIBLE groups; in ctx_resched()
2730 * - otherwise, do nothing more. in ctx_resched()
2733 ctx_sched_out(&cpuctx->ctx, event_type); in ctx_resched()
2735 ctx_sched_out(&cpuctx->ctx, EVENT_FLEXIBLE); in ctx_resched()
2739 perf_ctx_enable(&cpuctx->ctx, false); in ctx_resched()
2747 struct perf_event_context *task_ctx = cpuctx->task_ctx; in perf_pmu_resched()
2758 * things like ctx->is_active and cpuctx->task_ctx are set.
2763 struct perf_event_context *ctx = event->ctx; in __perf_install_in_context()
2765 struct perf_event_context *task_ctx = cpuctx->task_ctx; in __perf_install_in_context()
2769 raw_spin_lock(&cpuctx->ctx.lock); in __perf_install_in_context()
2770 if (ctx->task) { in __perf_install_in_context()
2771 raw_spin_lock(&ctx->lock); in __perf_install_in_context()
2774 reprogram = (ctx->task == current); in __perf_install_in_context()
2780 * If its not running, we don't care, ctx->lock will in __perf_install_in_context()
2783 if (task_curr(ctx->task) && !reprogram) { in __perf_install_in_context()
2784 ret = -ESRCH; in __perf_install_in_context()
2788 WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx); in __perf_install_in_context()
2790 raw_spin_lock(&task_ctx->lock); in __perf_install_in_context()
2794 if (event->state > PERF_EVENT_STATE_OFF && is_cgroup_event(event)) { in __perf_install_in_context()
2800 reprogram = cgroup_is_descendant(cgrp->css.cgroup, in __perf_install_in_context()
2801 event->cgrp->css.cgroup); in __perf_install_in_context()
2832 struct task_struct *task = READ_ONCE(ctx->task); in perf_install_in_context()
2834 lockdep_assert_held(&ctx->mutex); in perf_install_in_context()
2838 if (event->cpu != -1) in perf_install_in_context()
2839 WARN_ON_ONCE(event->cpu != cpu); in perf_install_in_context()
2842 * Ensures that if we can observe event->ctx, both the event and ctx in perf_install_in_context()
2845 smp_store_release(&event->ctx, ctx); in perf_install_in_context()
2850 * that case we need the magic of the IPI to set ctx->is_active. in perf_install_in_context()
2856 ctx->nr_events && !is_cgroup_event(event)) { in perf_install_in_context()
2857 raw_spin_lock_irq(&ctx->lock); in perf_install_in_context()
2858 if (ctx->task == TASK_TOMBSTONE) { in perf_install_in_context()
2859 raw_spin_unlock_irq(&ctx->lock); in perf_install_in_context()
2863 raw_spin_unlock_irq(&ctx->lock); in perf_install_in_context()
2879 * Installing events is tricky because we cannot rely on ctx->is_active in perf_install_in_context()
2880 * to be set in case this is the nr_events 0 -> 1 transition. in perf_install_in_context()
2882 * Instead we use task_curr(), which tells us if the task is running. in perf_install_in_context()
2889 * perf_event_context_sched_in() ctx::lock will serialize us, and the in perf_install_in_context()
2894 * our task->perf_event_ctxp[] store, such that it will in fact take in perf_install_in_context()
2903 * This smp_mb() orders the task->perf_event_ctxp[] store with the in perf_install_in_context()
2913 raw_spin_lock_irq(&ctx->lock); in perf_install_in_context()
2914 task = ctx->task; in perf_install_in_context()
2918 * cannot happen), and we hold ctx->mutex, which serializes us in perf_install_in_context()
2921 raw_spin_unlock_irq(&ctx->lock); in perf_install_in_context()
2925 * If the task is not running, ctx->lock will avoid it becoming so, in perf_install_in_context()
2929 raw_spin_unlock_irq(&ctx->lock); in perf_install_in_context()
2933 raw_spin_unlock_irq(&ctx->lock); in perf_install_in_context()
2944 struct perf_event *leader = event->group_leader; in __perf_event_enable()
2947 if (event->state >= PERF_EVENT_STATE_INACTIVE || in __perf_event_enable()
2948 event->state <= PERF_EVENT_STATE_ERROR) in __perf_event_enable()
2951 if (ctx->is_active) in __perf_event_enable()
2957 if (!ctx->is_active) in __perf_event_enable()
2969 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) { in __perf_event_enable()
2974 task_ctx = cpuctx->task_ctx; in __perf_event_enable()
2975 if (ctx->task) in __perf_event_enable()
2984 * If event->ctx is a cloned context, callers must make sure that
2985 * every task struct that event->ctx->task could possibly point to
2992 struct perf_event_context *ctx = event->ctx; in _perf_event_enable()
2994 raw_spin_lock_irq(&ctx->lock); in _perf_event_enable()
2995 if (event->state >= PERF_EVENT_STATE_INACTIVE || in _perf_event_enable()
2996 event->state < PERF_EVENT_STATE_ERROR) { in _perf_event_enable()
2998 raw_spin_unlock_irq(&ctx->lock); in _perf_event_enable()
3007 * been scheduled away before the cross-call arrived. in _perf_event_enable()
3009 if (event->state == PERF_EVENT_STATE_ERROR) { in _perf_event_enable()
3013 if (event->event_caps & PERF_EV_CAP_SIBLING && in _perf_event_enable()
3014 event->group_leader == event) in _perf_event_enable()
3017 event->state = PERF_EVENT_STATE_OFF; in _perf_event_enable()
3019 raw_spin_unlock_irq(&ctx->lock); in _perf_event_enable()
3045 struct perf_event *event = sd->event; in __perf_event_stop()
3048 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) in __perf_event_stop()
3058 if (READ_ONCE(event->oncpu) != smp_processor_id()) in __perf_event_stop()
3059 return -EAGAIN; in __perf_event_stop()
3061 event->pmu->stop(event, PERF_EF_UPDATE); in __perf_event_stop()
3069 * Since this is happening on an event-local CPU, no trace is lost in __perf_event_stop()
3072 if (sd->restart) in __perf_event_stop()
3073 event->pmu->start(event, 0); in __perf_event_stop()
3087 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) in perf_event_stop()
3095 * inactive here (event->oncpu==-1), there's nothing more to do; in perf_event_stop()
3096 * fall through with ret==-ENXIO. in perf_event_stop()
3098 ret = cpu_function_call(READ_ONCE(event->oncpu), in perf_event_stop()
3100 } while (ret == -EAGAIN); in perf_event_stop()
3119 * pre-existing mappings, called once when new filters arrive via SET_FILTER
3134 raw_spin_lock(&ifh->lock); in perf_event_addr_filters_sync()
3135 if (event->addr_filters_gen != event->hw.addr_filters_gen) { in perf_event_addr_filters_sync()
3136 event->pmu->addr_filters_sync(event); in perf_event_addr_filters_sync()
3137 event->hw.addr_filters_gen = event->addr_filters_gen; in perf_event_addr_filters_sync()
3139 raw_spin_unlock(&ifh->lock); in perf_event_addr_filters_sync()
3148 if (event->attr.inherit || !is_sampling_event(event)) in _perf_event_refresh()
3149 return -EINVAL; in _perf_event_refresh()
3151 atomic_add(refresh, &event->event_limit); in _perf_event_refresh()
3182 if (!bp->attr.disabled) in perf_event_modify_breakpoint()
3189 * Copy event-type-independent attributes that may be modified.
3194 to->sig_data = from->sig_data; in perf_event_modify_copy_attr()
3204 if (event->attr.type != attr->type) in perf_event_modify_attr()
3205 return -EINVAL; in perf_event_modify_attr()
3207 switch (event->attr.type) { in perf_event_modify_attr()
3213 return -EOPNOTSUPP; in perf_event_modify_attr()
3216 WARN_ON_ONCE(event->ctx->parent_ctx); in perf_event_modify_attr()
3218 mutex_lock(&event->child_mutex); in perf_event_modify_attr()
3220 * Event-type-independent attributes must be copied before event-type in perf_event_modify_attr()
3224 perf_event_modify_copy_attr(&event->attr, attr); in perf_event_modify_attr()
3228 list_for_each_entry(child, &event->child_list, child_list) { in perf_event_modify_attr()
3229 perf_event_modify_copy_attr(&child->attr, attr); in perf_event_modify_attr()
3235 mutex_unlock(&event->child_mutex); in perf_event_modify_attr()
3242 struct perf_event_context *ctx = pmu_ctx->ctx; in __pmu_ctx_sched_out()
3244 struct pmu *pmu = pmu_ctx->pmu; in __pmu_ctx_sched_out()
3246 if (ctx->task && !ctx->is_active) { in __pmu_ctx_sched_out()
3249 cpc = this_cpu_ptr(pmu->cpu_pmu_context); in __pmu_ctx_sched_out()
3250 WARN_ON_ONCE(cpc->task_epc && cpc->task_epc != pmu_ctx); in __pmu_ctx_sched_out()
3251 cpc->task_epc = NULL; in __pmu_ctx_sched_out()
3260 &pmu_ctx->pinned_active, in __pmu_ctx_sched_out()
3267 &pmu_ctx->flexible_active, in __pmu_ctx_sched_out()
3275 pmu_ctx->rotate_necessary = 0; in __pmu_ctx_sched_out()
3285 int is_active = ctx->is_active; in ctx_sched_out()
3290 lockdep_assert_held(&ctx->lock); in ctx_sched_out()
3292 if (likely(!ctx->nr_events)) { in ctx_sched_out()
3296 WARN_ON_ONCE(ctx->is_active); in ctx_sched_out()
3297 if (ctx->task) in ctx_sched_out()
3298 WARN_ON_ONCE(cpuctx->task_ctx); in ctx_sched_out()
3315 update_cgrp_time_from_cpuctx(cpuctx, ctx == &cpuctx->ctx); in ctx_sched_out()
3317 * CPU-release for the below ->is_active store, in ctx_sched_out()
3323 ctx->is_active &= ~event_type; in ctx_sched_out()
3324 if (!(ctx->is_active & EVENT_ALL)) in ctx_sched_out()
3325 ctx->is_active = 0; in ctx_sched_out()
3327 if (ctx->task) { in ctx_sched_out()
3328 WARN_ON_ONCE(cpuctx->task_ctx != ctx); in ctx_sched_out()
3329 if (!ctx->is_active) in ctx_sched_out()
3330 cpuctx->task_ctx = NULL; in ctx_sched_out()
3333 is_active ^= ctx->is_active; /* changed bits */ in ctx_sched_out()
3335 list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { in ctx_sched_out()
3336 if (cgroup && !pmu_ctx->nr_cgroups) in ctx_sched_out()
3353 lockdep_assert_held(&ctx1->lock); in context_equiv()
3354 lockdep_assert_held(&ctx2->lock); in context_equiv()
3357 if (ctx1->pin_count || ctx2->pin_count) in context_equiv()
3361 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) in context_equiv()
3365 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) in context_equiv()
3372 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && in context_equiv()
3373 ctx1->parent_gen == ctx2->parent_gen) in context_equiv()
3385 if (!event->attr.inherit_stat) in __perf_event_sync_stat()
3395 if (event->state == PERF_EVENT_STATE_ACTIVE) in __perf_event_sync_stat()
3396 event->pmu->read(event); in __perf_event_sync_stat()
3401 * In order to keep per-task stats reliable we need to flip the event in __perf_event_sync_stat()
3404 value = local64_read(&next_event->count); in __perf_event_sync_stat()
3405 value = local64_xchg(&event->count, value); in __perf_event_sync_stat()
3406 local64_set(&next_event->count, value); in __perf_event_sync_stat()
3408 swap(event->total_time_enabled, next_event->total_time_enabled); in __perf_event_sync_stat()
3409 swap(event->total_time_running, next_event->total_time_running); in __perf_event_sync_stat()
3423 if (!ctx->nr_stat) in perf_event_sync_stat()
3428 event = list_first_entry(&ctx->event_list, in perf_event_sync_stat()
3431 next_event = list_first_entry(&next_ctx->event_list, in perf_event_sync_stat()
3434 while (&event->event_entry != &ctx->event_list && in perf_event_sync_stat()
3435 &next_event->event_entry != &next_ctx->event_list) { in perf_event_sync_stat()
3457 if (!prev_ctx->nr_task_data) in perf_event_swap_task_ctx_data()
3461 &prev_ctx->pmu_ctx_list, &next_ctx->pmu_ctx_list, in perf_event_swap_task_ctx_data()
3464 if (WARN_ON_ONCE(prev_epc->pmu != next_epc->pmu)) in perf_event_swap_task_ctx_data()
3473 if (prev_epc->pmu->swap_task_ctx) in perf_event_swap_task_ctx_data()
3474 prev_epc->pmu->swap_task_ctx(prev_epc, next_epc); in perf_event_swap_task_ctx_data()
3476 swap(prev_epc->task_ctx_data, next_epc->task_ctx_data); in perf_event_swap_task_ctx_data()
3485 list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { in perf_ctx_sched_task_cb()
3486 cpc = this_cpu_ptr(pmu_ctx->pmu->cpu_pmu_context); in perf_ctx_sched_task_cb()
3488 if (cpc->sched_cb_usage && pmu_ctx->pmu->sched_task) in perf_ctx_sched_task_cb()
3489 pmu_ctx->pmu->sched_task(pmu_ctx, sched_in); in perf_ctx_sched_task_cb()
3496 struct perf_event_context *ctx = task->perf_event_ctxp; in perf_event_context_sched_out()
3505 next_ctx = rcu_dereference(next->perf_event_ctxp); in perf_event_context_sched_out()
3509 parent = rcu_dereference(ctx->parent_ctx); in perf_event_context_sched_out()
3510 next_parent = rcu_dereference(next_ctx->parent_ctx); in perf_event_context_sched_out()
3521 * lock (including re-checking that neither has been in perf_event_context_sched_out()
3526 raw_spin_lock(&ctx->lock); in perf_event_context_sched_out()
3527 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); in perf_event_context_sched_out()
3532 /* PMIs are disabled; ctx->nr_pending is stable. */ in perf_event_context_sched_out()
3533 if (local_read(&ctx->nr_pending) || in perf_event_context_sched_out()
3534 local_read(&next_ctx->nr_pending)) { in perf_event_context_sched_out()
3537 * events that rely on the ctx->task relation. in perf_event_context_sched_out()
3539 raw_spin_unlock(&next_ctx->lock); in perf_event_context_sched_out()
3544 WRITE_ONCE(ctx->task, next); in perf_event_context_sched_out()
3545 WRITE_ONCE(next_ctx->task, task); in perf_event_context_sched_out()
3555 * ctx->task and ctx->task_ctx_data are immaterial in perf_event_context_sched_out()
3557 * ctx->lock which we're now holding. in perf_event_context_sched_out()
3559 RCU_INIT_POINTER(task->perf_event_ctxp, next_ctx); in perf_event_context_sched_out()
3560 RCU_INIT_POINTER(next->perf_event_ctxp, ctx); in perf_event_context_sched_out()
3566 raw_spin_unlock(&next_ctx->lock); in perf_event_context_sched_out()
3567 raw_spin_unlock(&ctx->lock); in perf_event_context_sched_out()
3573 raw_spin_lock(&ctx->lock); in perf_event_context_sched_out()
3581 raw_spin_unlock(&ctx->lock); in perf_event_context_sched_out()
3590 struct perf_cpu_pmu_context *cpc = this_cpu_ptr(pmu->cpu_pmu_context); in perf_sched_cb_dec()
3595 if (!--cpc->sched_cb_usage) in perf_sched_cb_dec()
3596 list_del(&cpc->sched_cb_entry); in perf_sched_cb_dec()
3602 struct perf_cpu_pmu_context *cpc = this_cpu_ptr(pmu->cpu_pmu_context); in perf_sched_cb_inc()
3604 if (!cpc->sched_cb_usage++) in perf_sched_cb_inc()
3605 list_add(&cpc->sched_cb_entry, this_cpu_ptr(&sched_cb_list)); in perf_sched_cb_inc()
3615 * This callback is relevant even to per-cpu events; for example multi event
3624 pmu = cpc->epc.pmu; in __perf_pmu_sched_task()
3627 if (WARN_ON_ONCE(!pmu->sched_task)) in __perf_pmu_sched_task()
3630 perf_ctx_lock(cpuctx, cpuctx->task_ctx); in __perf_pmu_sched_task()
3633 pmu->sched_task(cpc->task_epc, sched_in); in __perf_pmu_sched_task()
3636 perf_ctx_unlock(cpuctx, cpuctx->task_ctx); in __perf_pmu_sched_task()
3646 /* cpuctx->task_ctx will be handled in perf_event_context_sched_in/out */ in perf_pmu_sched_task()
3647 if (prev == next || cpuctx->task_ctx) in perf_pmu_sched_task()
3661 * We stop each event and update the event value in event->count.
3663 * This does not protect us against NMI, but disable()
3682 * cgroup event are system-wide mode only in __perf_event_task_sched_out()
3692 return le->group_index < re->group_index; in perf_less_group_idx()
3710 struct perf_event **itrs = heap->data; in __heap_add()
3713 itrs[heap->nr] = event; in __heap_add()
3714 heap->nr++; in __heap_add()
3722 if (!pmu_ctx->ctx->task) in __link_epc()
3725 cpc = this_cpu_ptr(pmu_ctx->pmu->cpu_pmu_context); in __link_epc()
3726 WARN_ON_ONCE(cpc->task_epc && cpc->task_epc != pmu_ctx); in __link_epc()
3727 cpc->task_epc = pmu_ctx; in __link_epc()
3746 if (pmu->filter && pmu->filter(pmu, cpu)) in visit_groups_merge()
3749 if (!ctx->task) { in visit_groups_merge()
3752 .data = cpuctx->heap, in visit_groups_merge()
3754 .size = cpuctx->heap_size, in visit_groups_merge()
3757 lockdep_assert_held(&cpuctx->ctx.lock); in visit_groups_merge()
3760 if (cpuctx->cgrp) in visit_groups_merge()
3761 css = &cpuctx->cgrp->css; in visit_groups_merge()
3770 __heap_add(&event_heap, perf_event_groups_first(groups, -1, pmu, NULL)); in visit_groups_merge()
3777 for (; css; css = css->parent) in visit_groups_merge()
3778 __heap_add(&event_heap, perf_event_groups_first(groups, cpu, pmu, css->cgroup)); in visit_groups_merge()
3782 __link_epc((*evt)->pmu_ctx); in visit_groups_merge()
3783 perf_assert_pmu_disabled((*evt)->pmu_ctx->pmu); in visit_groups_merge()
3804 * Because the userpage is strictly per-event (there is no concept of context,
3806 * when context time starts :-(
3812 if (likely(!atomic_read(&event->mmap_count))) in event_update_userpage()
3834 struct perf_event_context *ctx = event->ctx; in merge_sched_in()
3837 if (event->state <= PERF_EVENT_STATE_OFF) in merge_sched_in()
3845 list_add_tail(&event->active_list, get_event_list(event)); in merge_sched_in()
3848 if (event->state == PERF_EVENT_STATE_INACTIVE) { in merge_sched_in()
3850 if (event->attr.pinned) { in merge_sched_in()
3856 event->pmu_ctx->rotate_necessary = 1; in merge_sched_in()
3857 cpc = this_cpu_ptr(event->pmu_ctx->pmu->cpu_pmu_context); in merge_sched_in()
3881 list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { in ctx_groups_sched_in()
3882 if (cgroup && !pmu_ctx->nr_cgroups) in ctx_groups_sched_in()
3884 pmu_groups_sched_in(ctx, groups, pmu_ctx->pmu); in ctx_groups_sched_in()
3891 pmu_groups_sched_in(ctx, &ctx->flexible_groups, pmu); in __pmu_ctx_sched_in()
3898 int is_active = ctx->is_active; in ctx_sched_in()
3903 lockdep_assert_held(&ctx->lock); in ctx_sched_in()
3905 if (likely(!ctx->nr_events)) in ctx_sched_in()
3913 * CPU-release for the below ->is_active store, in ctx_sched_in()
3919 ctx->is_active |= (event_type | EVENT_TIME); in ctx_sched_in()
3920 if (ctx->task) { in ctx_sched_in()
3922 cpuctx->task_ctx = ctx; in ctx_sched_in()
3924 WARN_ON_ONCE(cpuctx->task_ctx != ctx); in ctx_sched_in()
3927 is_active ^= ctx->is_active; /* changed bits */ in ctx_sched_in()
3934 ctx_groups_sched_in(ctx, &ctx->pinned_groups, cgroup); in ctx_sched_in()
3938 ctx_groups_sched_in(ctx, &ctx->flexible_groups, cgroup); in ctx_sched_in()
3947 ctx = rcu_dereference(task->perf_event_ctxp); in perf_event_context_sched_in()
3951 if (cpuctx->task_ctx == ctx) { in perf_event_context_sched_in()
3964 * We must check ctx->nr_events while holding ctx->lock, such in perf_event_context_sched_in()
3967 if (!ctx->nr_events) in perf_event_context_sched_in()
3979 if (!RB_EMPTY_ROOT(&ctx->pinned_groups.tree)) { in perf_event_context_sched_in()
3980 perf_ctx_disable(&cpuctx->ctx, false); in perf_event_context_sched_in()
3981 ctx_sched_out(&cpuctx->ctx, EVENT_FLEXIBLE); in perf_event_context_sched_in()
3986 perf_ctx_sched_task_cb(cpuctx->task_ctx, true); in perf_event_context_sched_in()
3988 if (!RB_EMPTY_ROOT(&ctx->pinned_groups.tree)) in perf_event_context_sched_in()
3989 perf_ctx_enable(&cpuctx->ctx, false); in perf_event_context_sched_in()
4005 * This does not protect us against NMI, but enable()
4024 u64 frequency = event->attr.sample_freq; in perf_calculate_period()
4037 * the target period becomes: in perf_calculate_period()
4040 * period = ------------------- in perf_calculate_period()
4053 a##_fls--; \ in perf_calculate_period()
4056 b##_fls--; \ in perf_calculate_period()
4100 struct hw_perf_event *hwc = &event->hw; in perf_adjust_period()
4101 s64 period, sample_period; in perf_adjust_period() local
4104 period = perf_calculate_period(event, nsec, count); in perf_adjust_period()
4106 delta = (s64)(period - hwc->sample_period); in perf_adjust_period()
4110 delta -= 7; in perf_adjust_period()
4113 sample_period = hwc->sample_period + delta; in perf_adjust_period()
4118 hwc->sample_period = sample_period; in perf_adjust_period()
4120 if (local64_read(&hwc->period_left) > 8*sample_period) { in perf_adjust_period()
4122 event->pmu->stop(event, PERF_EF_UPDATE); in perf_adjust_period()
4124 local64_set(&hwc->period_left, 0); in perf_adjust_period()
4127 event->pmu->start(event, PERF_EF_RELOAD); in perf_adjust_period()
4141 u64 now, period = TICK_NSEC; in perf_adjust_freq_unthr_context() local
4146 * - context have events in frequency mode (needs freq adjust) in perf_adjust_freq_unthr_context()
4147 * - there are events to unthrottle on this cpu in perf_adjust_freq_unthr_context()
4149 if (!(ctx->nr_freq || unthrottle)) in perf_adjust_freq_unthr_context()
4152 raw_spin_lock(&ctx->lock); in perf_adjust_freq_unthr_context()
4154 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { in perf_adjust_freq_unthr_context()
4155 if (event->state != PERF_EVENT_STATE_ACTIVE) in perf_adjust_freq_unthr_context()
4158 // XXX use visit thingy to avoid the -1,cpu match in perf_adjust_freq_unthr_context()
4162 perf_pmu_disable(event->pmu); in perf_adjust_freq_unthr_context()
4164 hwc = &event->hw; in perf_adjust_freq_unthr_context()
4166 if (hwc->interrupts == MAX_INTERRUPTS) { in perf_adjust_freq_unthr_context()
4167 hwc->interrupts = 0; in perf_adjust_freq_unthr_context()
4169 event->pmu->start(event, 0); in perf_adjust_freq_unthr_context()
4172 if (!event->attr.freq || !event->attr.sample_freq) in perf_adjust_freq_unthr_context()
4176 * stop the event and update event->count in perf_adjust_freq_unthr_context()
4178 event->pmu->stop(event, PERF_EF_UPDATE); in perf_adjust_freq_unthr_context()
4180 now = local64_read(&event->count); in perf_adjust_freq_unthr_context()
4181 delta = now - hwc->freq_count_stamp; in perf_adjust_freq_unthr_context()
4182 hwc->freq_count_stamp = now; in perf_adjust_freq_unthr_context()
4192 perf_adjust_period(event, period, delta, false); in perf_adjust_freq_unthr_context()
4194 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); in perf_adjust_freq_unthr_context()
4196 perf_pmu_enable(event->pmu); in perf_adjust_freq_unthr_context()
4199 raw_spin_unlock(&ctx->lock); in perf_adjust_freq_unthr_context()
4208 * Rotate the first entry last of non-pinned groups. Rotation might be in rotate_ctx()
4211 if (ctx->rotate_disable) in rotate_ctx()
4214 perf_event_groups_delete(&ctx->flexible_groups, event); in rotate_ctx()
4215 perf_event_groups_insert(&ctx->flexible_groups, event); in rotate_ctx()
4226 .pmu = pmu_ctx->pmu, in ctx_event_to_rotate()
4230 event = list_first_entry_or_null(&pmu_ctx->flexible_active, in ctx_event_to_rotate()
4236 tree = &pmu_ctx->ctx->flexible_groups.tree; in ctx_event_to_rotate()
4238 if (!pmu_ctx->ctx->task) { in ctx_event_to_rotate()
4247 key.cpu = -1; in ctx_event_to_rotate()
4264 pmu_ctx->rotate_necessary = 0; in ctx_event_to_rotate()
4282 cpu_epc = &cpc->epc; in perf_rotate_context()
4283 pmu = cpu_epc->pmu; in perf_rotate_context()
4284 task_epc = cpc->task_epc; in perf_rotate_context()
4286 cpu_rotate = cpu_epc->rotate_necessary; in perf_rotate_context()
4287 task_rotate = task_epc ? task_epc->rotate_necessary : 0; in perf_rotate_context()
4292 perf_ctx_lock(cpuctx, cpuctx->task_ctx); in perf_rotate_context()
4305 update_context_time(task_epc->ctx); in perf_rotate_context()
4310 update_context_time(&cpuctx->ctx); in perf_rotate_context()
4312 rotate_ctx(&cpuctx->ctx, cpu_event); in perf_rotate_context()
4313 __pmu_ctx_sched_in(&cpuctx->ctx, pmu); in perf_rotate_context()
4317 rotate_ctx(task_epc->ctx, task_event); in perf_rotate_context()
4320 __pmu_ctx_sched_in(task_epc->ctx, pmu); in perf_rotate_context()
4323 perf_ctx_unlock(cpuctx, cpuctx->task_ctx); in perf_rotate_context()
4340 perf_adjust_freq_unthr_context(&cpuctx->ctx, !!throttled); in perf_event_task_tick()
4343 ctx = rcu_dereference(current->perf_event_ctxp); in perf_event_task_tick()
4352 if (!event->attr.enable_on_exec) in event_enable_on_exec()
4355 event->attr.enable_on_exec = 0; in event_enable_on_exec()
4356 if (event->state >= PERF_EVENT_STATE_INACTIVE) in event_enable_on_exec()
4365 * Enable all of a task's events that have been marked enable-on-exec.
4378 if (WARN_ON_ONCE(current->perf_event_ctxp != ctx)) in perf_event_enable_on_exec()
4381 if (!ctx->nr_events) in perf_event_enable_on_exec()
4388 list_for_each_entry(event, &ctx->event_list, event_entry) { in perf_event_enable_on_exec()
4417 * remove-on-exec, and feeds their values back to parent events.
4426 mutex_lock(&ctx->mutex); in perf_event_remove_on_exec()
4428 if (WARN_ON_ONCE(ctx->task != current)) in perf_event_remove_on_exec()
4431 list_for_each_entry_safe(event, next, &ctx->event_list, event_entry) { in perf_event_remove_on_exec()
4432 if (!event->attr.remove_on_exec) in perf_event_remove_on_exec()
4443 raw_spin_lock_irqsave(&ctx->lock, flags); in perf_event_remove_on_exec()
4446 raw_spin_unlock_irqrestore(&ctx->lock, flags); in perf_event_remove_on_exec()
4449 mutex_unlock(&ctx->mutex); in perf_event_remove_on_exec()
4465 if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) { in __perf_event_read_cpu()
4484 struct perf_event *sub, *event = data->event; in __perf_event_read()
4485 struct perf_event_context *ctx = event->ctx; in __perf_event_read()
4487 struct pmu *pmu = event->pmu; in __perf_event_read()
4493 * event->count would have been updated to a recent sample in __perf_event_read()
4496 if (ctx->task && cpuctx->task_ctx != ctx) in __perf_event_read()
4499 raw_spin_lock(&ctx->lock); in __perf_event_read()
4500 if (ctx->is_active & EVENT_TIME) { in __perf_event_read()
4506 if (data->group) in __perf_event_read()
4509 if (event->state != PERF_EVENT_STATE_ACTIVE) in __perf_event_read()
4512 if (!data->group) { in __perf_event_read()
4513 pmu->read(event); in __perf_event_read()
4514 data->ret = 0; in __perf_event_read()
4518 pmu->start_txn(pmu, PERF_PMU_TXN_READ); in __perf_event_read()
4520 pmu->read(event); in __perf_event_read()
4523 if (sub->state == PERF_EVENT_STATE_ACTIVE) { in __perf_event_read()
4528 sub->pmu->read(sub); in __perf_event_read()
4532 data->ret = pmu->commit_txn(pmu); in __perf_event_read()
4535 raw_spin_unlock(&ctx->lock); in __perf_event_read()
4540 return local64_read(&event->count) + atomic64_read(&event->child_count); in perf_event_count()
4556 * NMI-safe method to read a local event, that is an event that
4558 * - either for the current task, or for this CPU
4559 * - does not have inherit set, for inherited task events
4561 * - must not have a pmu::count method
4579 if (event->attr.inherit) { in perf_event_read_local()
4580 ret = -EOPNOTSUPP; in perf_event_read_local()
4584 /* If this is a per-task event, it must be for current */ in perf_event_read_local()
4585 if ((event->attach_state & PERF_ATTACH_TASK) && in perf_event_read_local()
4586 event->hw.target != current) { in perf_event_read_local()
4587 ret = -EINVAL; in perf_event_read_local()
4591 /* If this is a per-CPU event, it must be for this CPU */ in perf_event_read_local()
4592 if (!(event->attach_state & PERF_ATTACH_TASK) && in perf_event_read_local()
4593 event->cpu != smp_processor_id()) { in perf_event_read_local()
4594 ret = -EINVAL; in perf_event_read_local()
4599 if (event->attr.pinned && event->oncpu != smp_processor_id()) { in perf_event_read_local()
4600 ret = -EBUSY; in perf_event_read_local()
4605 * If the event is currently on this CPU, its either a per-task event, in perf_event_read_local()
4607 * oncpu == -1). in perf_event_read_local()
4609 if (event->oncpu == smp_processor_id()) in perf_event_read_local()
4610 event->pmu->read(event); in perf_event_read_local()
4612 *value = local64_read(&event->count); in perf_event_read_local()
4630 enum perf_event_state state = READ_ONCE(event->state); in perf_event_read()
4642 * Orders the ->state and ->oncpu loads such that if we see in perf_event_read()
4643 * ACTIVE we must also see the right ->oncpu. in perf_event_read()
4649 event_cpu = READ_ONCE(event->oncpu); in perf_event_read()
4669 * Therefore, either way, we'll have an up-to-date event count in perf_event_read()
4677 struct perf_event_context *ctx = event->ctx; in perf_event_read()
4680 raw_spin_lock_irqsave(&ctx->lock, flags); in perf_event_read()
4681 state = event->state; in perf_event_read()
4683 raw_spin_unlock_irqrestore(&ctx->lock, flags); in perf_event_read()
4691 if (ctx->is_active & EVENT_TIME) { in perf_event_read()
4699 raw_spin_unlock_irqrestore(&ctx->lock, flags); in perf_event_read()
4710 raw_spin_lock_init(&ctx->lock); in __perf_event_init_context()
4711 mutex_init(&ctx->mutex); in __perf_event_init_context()
4712 INIT_LIST_HEAD(&ctx->pmu_ctx_list); in __perf_event_init_context()
4713 perf_event_groups_init(&ctx->pinned_groups); in __perf_event_init_context()
4714 perf_event_groups_init(&ctx->flexible_groups); in __perf_event_init_context()
4715 INIT_LIST_HEAD(&ctx->event_list); in __perf_event_init_context()
4716 refcount_set(&ctx->refcount, 1); in __perf_event_init_context()
4722 epc->pmu = pmu; in __perf_init_event_pmu_context()
4723 INIT_LIST_HEAD(&epc->pmu_ctx_entry); in __perf_init_event_pmu_context()
4724 INIT_LIST_HEAD(&epc->pinned_active); in __perf_init_event_pmu_context()
4725 INIT_LIST_HEAD(&epc->flexible_active); in __perf_init_event_pmu_context()
4726 atomic_set(&epc->refcount, 1); in __perf_init_event_pmu_context()
4740 ctx->task = get_task_struct(task); in alloc_perf_context()
4760 return ERR_PTR(-ESRCH); in find_lively_task_by_vpid()
4778 err = perf_allow_cpu(&event->attr); in find_get_context()
4782 cpuctx = per_cpu_ptr(&perf_cpu_context, event->cpu); in find_get_context()
4783 ctx = &cpuctx->ctx; in find_get_context()
4785 raw_spin_lock_irqsave(&ctx->lock, flags); in find_get_context()
4786 ++ctx->pin_count; in find_get_context()
4787 raw_spin_unlock_irqrestore(&ctx->lock, flags); in find_get_context()
4792 err = -EINVAL; in find_get_context()
4797 ++ctx->pin_count; in find_get_context()
4799 raw_spin_unlock_irqrestore(&ctx->lock, flags); in find_get_context()
4805 err = -ENOMEM; in find_get_context()
4810 mutex_lock(&task->perf_event_mutex); in find_get_context()
4815 if (task->flags & PF_EXITING) in find_get_context()
4816 err = -ESRCH; in find_get_context()
4817 else if (task->perf_event_ctxp) in find_get_context()
4818 err = -EAGAIN; in find_get_context()
4821 ++ctx->pin_count; in find_get_context()
4822 rcu_assign_pointer(task->perf_event_ctxp, ctx); in find_get_context()
4824 mutex_unlock(&task->perf_event_mutex); in find_get_context()
4829 if (err == -EAGAIN) in find_get_context()
4848 if (!ctx->task) { in find_get_pmu_context()
4856 cpc = per_cpu_ptr(pmu->cpu_pmu_context, event->cpu); in find_get_pmu_context()
4857 epc = &cpc->epc; in find_get_pmu_context()
4858 raw_spin_lock_irq(&ctx->lock); in find_get_pmu_context()
4859 if (!epc->ctx) { in find_get_pmu_context()
4860 atomic_set(&epc->refcount, 1); in find_get_pmu_context()
4861 epc->embedded = 1; in find_get_pmu_context()
4862 list_add(&epc->pmu_ctx_entry, &ctx->pmu_ctx_list); in find_get_pmu_context()
4863 epc->ctx = ctx; in find_get_pmu_context()
4865 WARN_ON_ONCE(epc->ctx != ctx); in find_get_pmu_context()
4866 atomic_inc(&epc->refcount); in find_get_pmu_context()
4868 raw_spin_unlock_irq(&ctx->lock); in find_get_pmu_context()
4874 return ERR_PTR(-ENOMEM); in find_get_pmu_context()
4876 if (event->attach_state & PERF_ATTACH_TASK_DATA) { in find_get_pmu_context()
4880 return ERR_PTR(-ENOMEM); in find_get_pmu_context()
4889 * lockdep_assert_held(&ctx->mutex); in find_get_pmu_context()
4892 * child_ctx->mutex. in find_get_pmu_context()
4895 raw_spin_lock_irq(&ctx->lock); in find_get_pmu_context()
4896 list_for_each_entry(epc, &ctx->pmu_ctx_list, pmu_ctx_entry) { in find_get_pmu_context()
4897 if (epc->pmu == pmu) { in find_get_pmu_context()
4898 WARN_ON_ONCE(epc->ctx != ctx); in find_get_pmu_context()
4899 atomic_inc(&epc->refcount); in find_get_pmu_context()
4907 list_add(&epc->pmu_ctx_entry, &ctx->pmu_ctx_list); in find_get_pmu_context()
4908 epc->ctx = ctx; in find_get_pmu_context()
4911 if (task_ctx_data && !epc->task_ctx_data) { in find_get_pmu_context()
4912 epc->task_ctx_data = task_ctx_data; in find_get_pmu_context()
4914 ctx->nr_task_data++; in find_get_pmu_context()
4916 raw_spin_unlock_irq(&ctx->lock); in find_get_pmu_context()
4926 WARN_ON_ONCE(!atomic_inc_not_zero(&epc->refcount)); in get_pmu_ctx()
4933 kfree(epc->task_ctx_data); in free_epc_rcu()
4939 struct perf_event_context *ctx = epc->ctx; in put_pmu_ctx()
4945 * lockdep_assert_held(&ctx->mutex); in put_pmu_ctx()
4947 * can't because of the call-site in _free_event()/put_event() in put_pmu_ctx()
4948 * which isn't always called under ctx->mutex. in put_pmu_ctx()
4950 if (!atomic_dec_and_raw_lock_irqsave(&epc->refcount, &ctx->lock, flags)) in put_pmu_ctx()
4953 WARN_ON_ONCE(list_empty(&epc->pmu_ctx_entry)); in put_pmu_ctx()
4955 list_del_init(&epc->pmu_ctx_entry); in put_pmu_ctx()
4956 epc->ctx = NULL; in put_pmu_ctx()
4958 WARN_ON_ONCE(!list_empty(&epc->pinned_active)); in put_pmu_ctx()
4959 WARN_ON_ONCE(!list_empty(&epc->flexible_active)); in put_pmu_ctx()
4961 raw_spin_unlock_irqrestore(&ctx->lock, flags); in put_pmu_ctx()
4963 if (epc->embedded) in put_pmu_ctx()
4966 call_rcu(&epc->rcu_head, free_epc_rcu); in put_pmu_ctx()
4975 if (event->ns) in free_event_rcu()
4976 put_pid_ns(event->ns); in free_event_rcu()
4986 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); in detach_sb_event()
4988 raw_spin_lock(&pel->lock); in detach_sb_event()
4989 list_del_rcu(&event->sb_list); in detach_sb_event()
4990 raw_spin_unlock(&pel->lock); in detach_sb_event()
4995 struct perf_event_attr *attr = &event->attr; in is_sb_event()
4997 if (event->parent) in is_sb_event()
5000 if (event->attach_state & PERF_ATTACH_TASK) in is_sb_event()
5003 if (attr->mmap || attr->mmap_data || attr->mmap2 || in is_sb_event()
5004 attr->comm || attr->comm_exec || in is_sb_event()
5005 attr->task || attr->ksymbol || in is_sb_event()
5006 attr->context_switch || attr->text_poke || in is_sb_event()
5007 attr->bpf_event) in is_sb_event()
5044 if (event->parent) in unaccount_event()
5047 if (event->attach_state & (PERF_ATTACH_TASK | PERF_ATTACH_SCHED_CB)) in unaccount_event()
5049 if (event->attr.mmap || event->attr.mmap_data) in unaccount_event()
5051 if (event->attr.build_id) in unaccount_event()
5053 if (event->attr.comm) in unaccount_event()
5055 if (event->attr.namespaces) in unaccount_event()
5057 if (event->attr.cgroup) in unaccount_event()
5059 if (event->attr.task) in unaccount_event()
5061 if (event->attr.freq) in unaccount_event()
5063 if (event->attr.context_switch) { in unaccount_event()
5071 if (event->attr.ksymbol) in unaccount_event()
5073 if (event->attr.bpf_event) in unaccount_event()
5075 if (event->attr.text_poke) in unaccount_event()
5079 if (!atomic_add_unless(&perf_sched_count, -1, 1)) in unaccount_event()
5099 * 1) cpu-wide events in the presence of per-task events,
5100 * 2) per-task events in the presence of cpu-wide events,
5104 * _free_event()), the latter -- before the first perf_install_in_context().
5108 struct pmu *pmu = event->pmu; in exclusive_event_init()
5114 * Prevent co-existence of per-task and cpu-wide events on the in exclusive_event_init()
5117 * Negative pmu::exclusive_cnt means there are cpu-wide in exclusive_event_init()
5119 * per-task events. in exclusive_event_init()
5123 * to mean "per-task event", because unlike other attach states it in exclusive_event_init()
5126 if (event->attach_state & PERF_ATTACH_TASK) { in exclusive_event_init()
5127 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt)) in exclusive_event_init()
5128 return -EBUSY; in exclusive_event_init()
5130 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt)) in exclusive_event_init()
5131 return -EBUSY; in exclusive_event_init()
5139 struct pmu *pmu = event->pmu; in exclusive_event_destroy()
5145 if (event->attach_state & PERF_ATTACH_TASK) in exclusive_event_destroy()
5146 atomic_dec(&pmu->exclusive_cnt); in exclusive_event_destroy()
5148 atomic_inc(&pmu->exclusive_cnt); in exclusive_event_destroy()
5153 if ((e1->pmu == e2->pmu) && in exclusive_event_match()
5154 (e1->cpu == e2->cpu || in exclusive_event_match()
5155 e1->cpu == -1 || in exclusive_event_match()
5156 e2->cpu == -1)) in exclusive_event_match()
5165 struct pmu *pmu = event->pmu; in exclusive_event_installable()
5167 lockdep_assert_held(&ctx->mutex); in exclusive_event_installable()
5172 list_for_each_entry(iter_event, &ctx->event_list, event_entry) { in exclusive_event_installable()
5185 struct callback_head *head = &event->pending_task; in perf_pending_task_sync()
5187 if (!event->pending_work) in perf_pending_task_sync()
5194 event->pending_work = 0; in perf_pending_task_sync()
5195 local_dec(&event->ctx->nr_pending); in perf_pending_task_sync()
5201 * non-preemptible section in perf_pending_task(). The RCU in perf_pending_task_sync()
5202 * grace period before the event is freed will make sure all in perf_pending_task_sync()
5205 rcuwait_wait_event(&event->pending_work_wait, !event->pending_work, TASK_UNINTERRUPTIBLE); in perf_pending_task_sync()
5210 irq_work_sync(&event->pending_irq); in _free_event()
5217 if (event->rb) { in _free_event()
5219 * Can happen when we close an event with re-directed output. in _free_event()
5222 * over us; possibly making our ring_buffer_put() the last. in _free_event()
5224 mutex_lock(&event->mmap_mutex); in _free_event()
5226 mutex_unlock(&event->mmap_mutex); in _free_event()
5232 if (!event->parent) { in _free_event()
5233 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) in _free_event()
5239 kfree(event->addr_filter_ranges); in _free_event()
5241 if (event->destroy) in _free_event()
5242 event->destroy(event); in _free_event()
5245 * Must be after ->destroy(), due to uprobe_perf_close() using in _free_event()
5248 if (event->hw.target) in _free_event()
5249 put_task_struct(event->hw.target); in _free_event()
5251 if (event->pmu_ctx) in _free_event()
5252 put_pmu_ctx(event->pmu_ctx); in _free_event()
5258 if (event->ctx) in _free_event()
5259 put_ctx(event->ctx); in _free_event()
5262 module_put(event->pmu->module); in _free_event()
5264 call_rcu(&event->rcu_head, free_event_rcu); in _free_event()
5273 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, in free_event()
5275 atomic_long_read(&event->refcount), event)) { in free_event()
5276 /* leak to avoid use-after-free */ in free_event()
5295 * owner->perf_event_mutex. in perf_remove_from_owner()
5297 owner = READ_ONCE(event->owner); in perf_remove_from_owner()
5311 * holding ctx->mutex which would be an inversion wrt. the in perf_remove_from_owner()
5315 * ctx->mutex. in perf_remove_from_owner()
5317 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING); in perf_remove_from_owner()
5320 * We have to re-check the event->owner field, if it is cleared in perf_remove_from_owner()
5325 if (event->owner) { in perf_remove_from_owner()
5326 list_del_init(&event->owner_entry); in perf_remove_from_owner()
5327 smp_store_release(&event->owner, NULL); in perf_remove_from_owner()
5329 mutex_unlock(&owner->perf_event_mutex); in perf_remove_from_owner()
5336 if (!atomic_long_dec_and_test(&event->refcount)) in put_event()
5349 struct perf_event_context *ctx = event->ctx; in perf_event_release_kernel()
5358 WARN_ON_ONCE(event->attach_state & in perf_event_release_kernel()
5367 WARN_ON_ONCE(ctx->parent_ctx); in perf_event_release_kernel()
5373 * Anybody acquiring event->child_mutex after the below loop _must_ in perf_event_release_kernel()
5385 mutex_lock(&event->child_mutex); in perf_event_release_kernel()
5386 list_for_each_entry(child, &event->child_list, child_list) { in perf_event_release_kernel()
5393 ctx = READ_ONCE(child->ctx); in perf_event_release_kernel()
5407 * can re-acquire child_mutex. in perf_event_release_kernel()
5409 mutex_unlock(&event->child_mutex); in perf_event_release_kernel()
5410 mutex_lock(&ctx->mutex); in perf_event_release_kernel()
5411 mutex_lock(&event->child_mutex); in perf_event_release_kernel()
5418 tmp = list_first_entry_or_null(&event->child_list, in perf_event_release_kernel()
5422 list_move(&child->child_list, &free_list); in perf_event_release_kernel()
5429 var = &ctx->refcount; in perf_event_release_kernel()
5432 mutex_unlock(&event->child_mutex); in perf_event_release_kernel()
5433 mutex_unlock(&ctx->mutex); in perf_event_release_kernel()
5447 mutex_unlock(&event->child_mutex); in perf_event_release_kernel()
5450 void *var = &child->ctx->refcount; in perf_event_release_kernel()
5452 list_del(&child->child_list); in perf_event_release_kernel()
5474 perf_event_release_kernel(file->private_data); in perf_release()
5486 mutex_lock(&event->child_mutex); in __perf_event_read_value()
5491 *enabled += event->total_time_enabled + in __perf_event_read_value()
5492 atomic64_read(&event->child_total_time_enabled); in __perf_event_read_value()
5493 *running += event->total_time_running + in __perf_event_read_value()
5494 atomic64_read(&event->child_total_time_running); in __perf_event_read_value()
5496 list_for_each_entry(child, &event->child_list, child_list) { in __perf_event_read_value()
5499 *enabled += child->total_time_enabled; in __perf_event_read_value()
5500 *running += child->total_time_running; in __perf_event_read_value()
5502 mutex_unlock(&event->child_mutex); in __perf_event_read_value()
5523 struct perf_event_context *ctx = leader->ctx; in __perf_read_group_add()
5533 raw_spin_lock_irqsave(&ctx->lock, flags); in __perf_read_group_add()
5539 * - leader->ctx->lock pins leader->sibling_list in __perf_read_group_add()
5540 * - parent->child_mutex pins parent->child_list in __perf_read_group_add()
5541 * - parent->ctx->mutex pins parent->sibling_list in __perf_read_group_add()
5543 * Because parent->ctx != leader->ctx (and child_list nests inside in __perf_read_group_add()
5544 * ctx->mutex), group destruction is not atomic between children, also in __perf_read_group_add()
5554 parent = leader->parent; in __perf_read_group_add()
5556 (parent->group_generation != leader->group_generation || in __perf_read_group_add()
5557 parent->nr_siblings != leader->nr_siblings)) { in __perf_read_group_add()
5558 ret = -ECHILD; in __perf_read_group_add()
5563 * Since we co-schedule groups, {enabled,running} times of siblings in __perf_read_group_add()
5568 values[n++] += leader->total_time_enabled + in __perf_read_group_add()
5569 atomic64_read(&leader->child_total_time_enabled); in __perf_read_group_add()
5573 values[n++] += leader->total_time_running + in __perf_read_group_add()
5574 atomic64_read(&leader->child_total_time_running); in __perf_read_group_add()
5584 values[n++] = atomic64_read(&leader->lost_samples); in __perf_read_group_add()
5591 values[n++] = atomic64_read(&sub->lost_samples); in __perf_read_group_add()
5595 raw_spin_unlock_irqrestore(&ctx->lock, flags); in __perf_read_group_add()
5602 struct perf_event *leader = event->group_leader, *child; in perf_read_group()
5603 struct perf_event_context *ctx = leader->ctx; in perf_read_group()
5607 lockdep_assert_held(&ctx->mutex); in perf_read_group()
5609 values = kzalloc(event->read_size, GFP_KERNEL); in perf_read_group()
5611 return -ENOMEM; in perf_read_group()
5613 values[0] = 1 + leader->nr_siblings; in perf_read_group()
5615 mutex_lock(&leader->child_mutex); in perf_read_group()
5621 list_for_each_entry(child, &leader->child_list, child_list) { in perf_read_group()
5627 mutex_unlock(&leader->child_mutex); in perf_read_group()
5629 ret = event->read_size; in perf_read_group()
5630 if (copy_to_user(buf, values, event->read_size)) in perf_read_group()
5631 ret = -EFAULT; in perf_read_group()
5635 mutex_unlock(&leader->child_mutex); in perf_read_group()
5656 values[n++] = atomic64_read(&event->lost_samples); in perf_read_one()
5659 return -EFAULT; in perf_read_one()
5668 if (event->state > PERF_EVENT_STATE_EXIT) in is_event_hup()
5671 mutex_lock(&event->child_mutex); in is_event_hup()
5672 no_children = list_empty(&event->child_list); in is_event_hup()
5673 mutex_unlock(&event->child_mutex); in is_event_hup()
5678 * Read the performance event - simple non blocking version for now
5683 u64 read_format = event->attr.read_format; in __perf_read()
5687 * Return end-of-file for a read on an event that is in in __perf_read()
5691 if (event->state == PERF_EVENT_STATE_ERROR) in __perf_read()
5694 if (count < event->read_size) in __perf_read()
5695 return -ENOSPC; in __perf_read()
5697 WARN_ON_ONCE(event->ctx->parent_ctx); in __perf_read()
5709 struct perf_event *event = file->private_data; in perf_read()
5726 struct perf_event *event = file->private_data; in perf_poll()
5730 poll_wait(file, &event->waitq, wait); in perf_poll()
5736 * Pin the event->rb by taking event->mmap_mutex; otherwise in perf_poll()
5737 * perf_event_set_output() can swizzle our rb and make us miss wakeups. in perf_poll()
5739 mutex_lock(&event->mmap_mutex); in perf_poll()
5740 rb = event->rb; in perf_poll()
5742 events = atomic_xchg(&rb->poll, 0); in perf_poll()
5743 mutex_unlock(&event->mmap_mutex); in perf_poll()
5750 local64_set(&event->count, 0); in _perf_event_reset()
5761 WARN_ON_ONCE(event->attr.inherit); in perf_event_pause()
5763 count = local64_read(&event->count); in perf_event_pause()
5765 local64_set(&event->count, 0); in perf_event_pause()
5773 * Holding the top-level event's child_mutex means that any
5783 WARN_ON_ONCE(event->ctx->parent_ctx); in perf_event_for_each_child()
5785 mutex_lock(&event->child_mutex); in perf_event_for_each_child()
5787 list_for_each_entry(child, &event->child_list, child_list) in perf_event_for_each_child()
5789 mutex_unlock(&event->child_mutex); in perf_event_for_each_child()
5795 struct perf_event_context *ctx = event->ctx; in perf_event_for_each()
5798 lockdep_assert_held(&ctx->mutex); in perf_event_for_each()
5800 event = event->group_leader; in perf_event_for_each()
5815 if (event->attr.freq) { in __perf_event_period()
5816 event->attr.sample_freq = value; in __perf_event_period()
5818 event->attr.sample_period = value; in __perf_event_period()
5819 event->hw.sample_period = value; in __perf_event_period()
5822 active = (event->state == PERF_EVENT_STATE_ACTIVE); in __perf_event_period()
5824 perf_pmu_disable(event->pmu); in __perf_event_period()
5827 * trying to unthrottle while we already re-started the event. in __perf_event_period()
5829 if (event->hw.interrupts == MAX_INTERRUPTS) { in __perf_event_period()
5830 event->hw.interrupts = 0; in __perf_event_period()
5833 event->pmu->stop(event, PERF_EF_UPDATE); in __perf_event_period()
5836 local64_set(&event->hw.period_left, 0); in __perf_event_period()
5839 event->pmu->start(event, PERF_EF_RELOAD); in __perf_event_period()
5840 perf_pmu_enable(event->pmu); in __perf_event_period()
5846 return event->pmu->check_period(event, value); in perf_event_check_period()
5852 return -EINVAL; in _perf_event_period()
5855 return -EINVAL; in _perf_event_period()
5857 if (event->attr.freq && value > sysctl_perf_event_sample_rate) in _perf_event_period()
5858 return -EINVAL; in _perf_event_period()
5861 return -EINVAL; in _perf_event_period()
5863 if (!event->attr.freq && (value & (1ULL << 63))) in _perf_event_period()
5864 return -EINVAL; in _perf_event_period()
5890 return -EBADF; in perf_fget_light()
5892 if (f.file->f_op != &perf_fops) { in perf_fget_light()
5894 return -EBADF; in perf_fget_light()
5930 return -EFAULT; in _perf_ioctl()
5939 return -EFAULT; in _perf_ioctl()
5946 if (arg != -1) { in _perf_ioctl()
5952 output_event = output.file->private_data; in _perf_ioctl()
5986 rb = rcu_dereference(event->rb); in _perf_ioctl()
5987 if (!rb || !rb->nr_pages) { in _perf_ioctl()
5989 return -EINVAL; in _perf_ioctl()
6010 return -ENOTTY; in _perf_ioctl()
6023 struct perf_event *event = file->private_data; in perf_ioctl()
6048 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ in perf_compat_ioctl()
6066 mutex_lock(&current->perf_event_mutex); in perf_event_task_enable()
6067 list_for_each_entry(event, &current->perf_event_list, owner_entry) { in perf_event_task_enable()
6072 mutex_unlock(&current->perf_event_mutex); in perf_event_task_enable()
6082 mutex_lock(&current->perf_event_mutex); in perf_event_task_disable()
6083 list_for_each_entry(event, &current->perf_event_list, owner_entry) { in perf_event_task_disable()
6088 mutex_unlock(&current->perf_event_mutex); in perf_event_task_disable()
6095 if (event->hw.state & PERF_HES_STOPPED) in perf_event_index()
6098 if (event->state != PERF_EVENT_STATE_ACTIVE) in perf_event_index()
6101 return event->pmu->event_idx(event); in perf_event_index()
6110 rb = rcu_dereference(event->rb); in perf_event_init_userpage()
6114 userpg = rb->user_page; in perf_event_init_userpage()
6117 userpg->cap_bit0_is_deprecated = 1; in perf_event_init_userpage()
6118 userpg->size = offsetof(struct perf_event_mmap_page, __reserved); in perf_event_init_userpage()
6119 userpg->data_offset = PAGE_SIZE; in perf_event_init_userpage()
6120 userpg->data_size = perf_data_size(rb); in perf_event_init_userpage()
6143 rb = rcu_dereference(event->rb); in perf_event_update_userpage()
6158 userpg = rb->user_page; in perf_event_update_userpage()
6164 ++userpg->lock; in perf_event_update_userpage()
6166 userpg->index = perf_event_index(event); in perf_event_update_userpage()
6167 userpg->offset = perf_event_count(event); in perf_event_update_userpage()
6168 if (userpg->index) in perf_event_update_userpage()
6169 userpg->offset -= local64_read(&event->hw.prev_count); in perf_event_update_userpage()
6171 userpg->time_enabled = enabled + in perf_event_update_userpage()
6172 atomic64_read(&event->child_total_time_enabled); in perf_event_update_userpage()
6174 userpg->time_running = running + in perf_event_update_userpage()
6175 atomic64_read(&event->child_total_time_running); in perf_event_update_userpage()
6180 ++userpg->lock; in perf_event_update_userpage()
6189 struct perf_event *event = vmf->vma->vm_file->private_data; in perf_mmap_fault()
6193 if (vmf->flags & FAULT_FLAG_MKWRITE) { in perf_mmap_fault()
6194 if (vmf->pgoff == 0) in perf_mmap_fault()
6200 rb = rcu_dereference(event->rb); in perf_mmap_fault()
6204 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) in perf_mmap_fault()
6207 vmf->page = perf_mmap_to_page(rb, vmf->pgoff); in perf_mmap_fault()
6208 if (!vmf->page) in perf_mmap_fault()
6211 get_page(vmf->page); in perf_mmap_fault()
6212 vmf->page->mapping = vmf->vma->vm_file->f_mapping; in perf_mmap_fault()
6213 vmf->page->index = vmf->pgoff; in perf_mmap_fault()
6228 WARN_ON_ONCE(event->parent); in ring_buffer_attach()
6230 if (event->rb) { in ring_buffer_attach()
6233 * event->rb_entry and wait/clear when adding event->rb_entry. in ring_buffer_attach()
6235 WARN_ON_ONCE(event->rcu_pending); in ring_buffer_attach()
6237 old_rb = event->rb; in ring_buffer_attach()
6238 spin_lock_irqsave(&old_rb->event_lock, flags); in ring_buffer_attach()
6239 list_del_rcu(&event->rb_entry); in ring_buffer_attach()
6240 spin_unlock_irqrestore(&old_rb->event_lock, flags); in ring_buffer_attach()
6242 event->rcu_batches = get_state_synchronize_rcu(); in ring_buffer_attach()
6243 event->rcu_pending = 1; in ring_buffer_attach()
6247 if (event->rcu_pending) { in ring_buffer_attach()
6248 cond_synchronize_rcu(event->rcu_batches); in ring_buffer_attach()
6249 event->rcu_pending = 0; in ring_buffer_attach()
6252 spin_lock_irqsave(&rb->event_lock, flags); in ring_buffer_attach()
6253 list_add_rcu(&event->rb_entry, &rb->event_list); in ring_buffer_attach()
6254 spin_unlock_irqrestore(&rb->event_lock, flags); in ring_buffer_attach()
6264 * mid-air, but then again, whoever does it like this is in ring_buffer_attach()
6270 rcu_assign_pointer(event->rb, rb); in ring_buffer_attach()
6279 wake_up_all(&event->waitq); in ring_buffer_attach()
6287 if (event->parent) in ring_buffer_wakeup()
6288 event = event->parent; in ring_buffer_wakeup()
6291 rb = rcu_dereference(event->rb); in ring_buffer_wakeup()
6293 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) in ring_buffer_wakeup()
6294 wake_up_all(&event->waitq); in ring_buffer_wakeup()
6303 if (event->parent) in ring_buffer_get()
6304 event = event->parent; in ring_buffer_get()
6307 rb = rcu_dereference(event->rb); in ring_buffer_get()
6309 if (!refcount_inc_not_zero(&rb->refcount)) in ring_buffer_get()
6319 if (!refcount_dec_and_test(&rb->refcount)) in ring_buffer_put()
6322 WARN_ON_ONCE(!list_empty(&rb->event_list)); in ring_buffer_put()
6324 call_rcu(&rb->rcu_head, rb_free_rcu); in ring_buffer_put()
6329 struct perf_event *event = vma->vm_file->private_data; in perf_mmap_open()
6331 atomic_inc(&event->mmap_count); in perf_mmap_open()
6332 atomic_inc(&event->rb->mmap_count); in perf_mmap_open()
6334 if (vma->vm_pgoff) in perf_mmap_open()
6335 atomic_inc(&event->rb->aux_mmap_count); in perf_mmap_open()
6337 if (event->pmu->event_mapped) in perf_mmap_open()
6338 event->pmu->event_mapped(event, vma->vm_mm); in perf_mmap_open()
6349 * to detach all events redirecting to us.
6353 struct perf_event *event = vma->vm_file->private_data; in perf_mmap_close()
6355 struct user_struct *mmap_user = rb->mmap_user; in perf_mmap_close()
6356 int mmap_locked = rb->mmap_locked; in perf_mmap_close()
6360 if (event->pmu->event_unmapped) in perf_mmap_close()
6361 event->pmu->event_unmapped(event, vma->vm_mm); in perf_mmap_close()
6364 * The AUX buffer is strictly a sub-buffer, serialize using aux_mutex in perf_mmap_close()
6367 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff && in perf_mmap_close()
6368 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &rb->aux_mutex)) { in perf_mmap_close()
6378 atomic_long_sub(rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->locked_vm); in perf_mmap_close()
6379 atomic64_sub(rb->aux_mmap_locked, &vma->vm_mm->pinned_vm); in perf_mmap_close()
6383 WARN_ON_ONCE(refcount_read(&rb->aux_refcount)); in perf_mmap_close()
6385 mutex_unlock(&rb->aux_mutex); in perf_mmap_close()
6388 if (atomic_dec_and_test(&rb->mmap_count)) in perf_mmap_close()
6391 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) in perf_mmap_close()
6395 mutex_unlock(&event->mmap_mutex); in perf_mmap_close()
6408 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { in perf_mmap_close()
6409 if (!atomic_long_inc_not_zero(&event->refcount)) { in perf_mmap_close()
6411 * This event is en-route to free_event() which will in perf_mmap_close()
6418 mutex_lock(&event->mmap_mutex); in perf_mmap_close()
6421 * swizzle the rb from under us while we were waiting to in perf_mmap_close()
6429 if (event->rb == rb) in perf_mmap_close()
6432 mutex_unlock(&event->mmap_mutex); in perf_mmap_close()
6444 * It could be there's still a few 0-ref events on the list; they'll in perf_mmap_close()
6445 * get cleaned up by free_event() -- they'll also still have their in perf_mmap_close()
6452 atomic_long_sub((size >> PAGE_SHIFT) + 1 - mmap_locked, in perf_mmap_close()
6453 &mmap_user->locked_vm); in perf_mmap_close()
6454 atomic64_sub(mmap_locked, &vma->vm_mm->pinned_vm); in perf_mmap_close()
6470 struct perf_event *event = file->private_data; in perf_mmap()
6482 * Don't allow mmap() of inherited per-task counters. This would in perf_mmap()
6486 if (event->cpu == -1 && event->attr.inherit) in perf_mmap()
6487 return -EINVAL; in perf_mmap()
6489 if (!(vma->vm_flags & VM_SHARED)) in perf_mmap()
6490 return -EINVAL; in perf_mmap()
6496 vma_size = vma->vm_end - vma->vm_start; in perf_mmap()
6498 if (vma->vm_pgoff == 0) { in perf_mmap()
6499 nr_pages = (vma_size / PAGE_SIZE) - 1; in perf_mmap()
6502 * AUX area mapping: if rb->aux_nr_pages != 0, it's already in perf_mmap()
6508 if (!event->rb) in perf_mmap()
6509 return -EINVAL; in perf_mmap()
6513 return -ENOMEM; in perf_mmap()
6515 mutex_lock(&event->mmap_mutex); in perf_mmap()
6516 ret = -EINVAL; in perf_mmap()
6518 rb = event->rb; in perf_mmap()
6522 aux_mutex = &rb->aux_mutex; in perf_mmap()
6525 aux_offset = READ_ONCE(rb->user_page->aux_offset); in perf_mmap()
6526 aux_size = READ_ONCE(rb->user_page->aux_size); in perf_mmap()
6531 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT) in perf_mmap()
6535 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff) in perf_mmap()
6542 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages) in perf_mmap()
6548 if (!atomic_inc_not_zero(&rb->mmap_count)) in perf_mmap()
6552 atomic_inc(&rb->aux_mmap_count); in perf_mmap()
6557 atomic_set(&rb->aux_mmap_count, 1); in perf_mmap()
6564 * If we have rb pages ensure they're a power-of-two number, so we in perf_mmap()
6568 return -EINVAL; in perf_mmap()
6571 return -EINVAL; in perf_mmap()
6573 WARN_ON_ONCE(event->ctx->parent_ctx); in perf_mmap()
6575 mutex_lock(&event->mmap_mutex); in perf_mmap()
6576 if (event->rb) { in perf_mmap()
6577 if (data_page_nr(event->rb) != nr_pages) { in perf_mmap()
6578 ret = -EINVAL; in perf_mmap()
6582 if (!atomic_inc_not_zero(&event->rb->mmap_count)) { in perf_mmap()
6588 mutex_unlock(&event->mmap_mutex); in perf_mmap()
6598 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); in perf_mmap()
6605 user_locked = atomic_long_read(&user->locked_vm); in perf_mmap()
6609 * user->locked_vm > user_lock_limit in perf_mmap()
6620 extra = user_locked - user_lock_limit; in perf_mmap()
6621 user_extra -= extra; in perf_mmap()
6626 locked = atomic64_read(&vma->vm_mm->pinned_vm) + extra; in perf_mmap()
6630 ret = -EPERM; in perf_mmap()
6634 WARN_ON(!rb && event->rb); in perf_mmap()
6636 if (vma->vm_flags & VM_WRITE) in perf_mmap()
6641 event->attr.watermark ? event->attr.wakeup_watermark : 0, in perf_mmap()
6642 event->cpu, flags); in perf_mmap()
6645 ret = -ENOMEM; in perf_mmap()
6649 atomic_set(&rb->mmap_count, 1); in perf_mmap()
6650 rb->mmap_user = get_current_user(); in perf_mmap()
6651 rb->mmap_locked = extra; in perf_mmap()
6659 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages, in perf_mmap()
6660 event->attr.aux_watermark, flags); in perf_mmap()
6662 rb->aux_mmap_locked = extra; in perf_mmap()
6667 atomic_long_add(user_extra, &user->locked_vm); in perf_mmap()
6668 atomic64_add(extra, &vma->vm_mm->pinned_vm); in perf_mmap()
6670 atomic_inc(&event->mmap_count); in perf_mmap()
6672 atomic_dec(&rb->mmap_count); in perf_mmap()
6677 mutex_unlock(&event->mmap_mutex); in perf_mmap()
6684 vma->vm_ops = &perf_mmap_vmops; in perf_mmap()
6686 if (event->pmu->event_mapped) in perf_mmap()
6687 event->pmu->event_mapped(event, vma->vm_mm); in perf_mmap()
6695 struct perf_event *event = filp->private_data; in perf_fasync()
6699 retval = fasync_helper(fd, filp, on, &event->fasync); in perf_fasync()
6723 * to user-space before waking everybody up.
6729 if (event->parent) in perf_event_fasync()
6730 event = event->parent; in perf_event_fasync()
6731 return &event->fasync; in perf_event_fasync()
6738 if (event->pending_kill) { in perf_event_wakeup()
6739 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill); in perf_event_wakeup()
6740 event->pending_kill = 0; in perf_event_wakeup()
6748 * ctx->task or current has changed in the meantime. This can be the in perf_sigtrap()
6751 if (WARN_ON_ONCE(event->ctx->task != current)) in perf_sigtrap()
6758 if (current->flags & PF_EXITING) in perf_sigtrap()
6761 send_sig_perf((void __user *)event->pending_addr, in perf_sigtrap()
6762 event->orig_type, event->attr.sig_data); in perf_sigtrap()
6766 * Deliver the pending work in-event-context or follow the context.
6770 int cpu = READ_ONCE(event->oncpu); in __perf_pending_irq()
6783 if (event->pending_sigtrap) { in __perf_pending_irq()
6784 event->pending_sigtrap = 0; in __perf_pending_irq()
6786 local_dec(&event->ctx->nr_pending); in __perf_pending_irq()
6788 if (event->pending_disable) { in __perf_pending_irq()
6789 event->pending_disable = 0; in __perf_pending_irq()
6796 * CPU-A CPU-B in __perf_pending_irq()
6799 * @pending_disable = CPU-A; in __perf_pending_irq()
6802 * sched-out in __perf_pending_irq()
6803 * @pending_disable = -1; in __perf_pending_irq()
6805 * sched-in in __perf_pending_irq()
6807 * @pending_disable = CPU-B; in __perf_pending_irq()
6813 * But the event runs on CPU-B and wants disabling there. in __perf_pending_irq()
6815 irq_work_queue_on(&event->pending_irq, cpu); in __perf_pending_irq()
6830 * The wakeup isn't bound to the context of the event -- it can happen in perf_pending_irq()
6833 if (event->pending_wakeup) { in perf_pending_irq()
6834 event->pending_wakeup = 0; in perf_pending_irq()
6850 * All accesses to the event must belong to the same implicit RCU read-side in perf_pending_task()
6851 * critical section as the ->pending_work reset. See comment in in perf_pending_task()
6861 if (event->pending_work) { in perf_pending_task()
6862 event->pending_work = 0; in perf_pending_task()
6864 local_dec(&event->ctx->nr_pending); in perf_pending_task()
6865 rcuwait_wake_up(&event->pending_work_wait); in perf_pending_task()
6876 DEFINE_STATIC_CALL_RET0(__perf_guest_state, *perf_guest_cbs->state);
6877 DEFINE_STATIC_CALL_RET0(__perf_guest_get_ip, *perf_guest_cbs->get_ip);
6878 DEFINE_STATIC_CALL_RET0(__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr);
6886 static_call_update(__perf_guest_state, cbs->state); in perf_register_guest_info_callbacks()
6887 static_call_update(__perf_guest_get_ip, cbs->get_ip); in perf_register_guest_info_callbacks()
6889 /* Implementing ->handle_intel_pt_intr is optional. */ in perf_register_guest_info_callbacks()
6890 if (cbs->handle_intel_pt_intr) in perf_register_guest_info_callbacks()
6892 cbs->handle_intel_pt_intr); in perf_register_guest_info_callbacks()
6931 regs_user->abi = perf_reg_abi(current); in perf_sample_regs_user()
6932 regs_user->regs = regs; in perf_sample_regs_user()
6933 } else if (!(current->flags & PF_KTHREAD)) { in perf_sample_regs_user()
6936 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; in perf_sample_regs_user()
6937 regs_user->regs = NULL; in perf_sample_regs_user()
6944 regs_intr->regs = regs; in perf_sample_regs_intr()
6945 regs_intr->abi = perf_reg_abi(current); in perf_sample_regs_intr()
6963 return TASK_SIZE - addr; in perf_ustack_task_size()
6978 * - TASK_SIZE in perf_sample_ustack_size()
6981 * - remaining sample size in perf_sample_ustack_size()
6998 stack_size = USHRT_MAX - header_size - sizeof(u64); in perf_sample_ustack_size()
7021 * - the size requested by user or the best one we can fit in perf_output_sample_ustack()
7024 * - user stack dump data in perf_output_sample_ustack()
7026 * - the actual dumped size in perf_output_sample_ustack()
7035 dyn_size = dump_size - rem; in perf_output_sample_ustack()
7048 struct perf_event *sampler = event->aux_event; in perf_prepare_sample_aux()
7051 data->aux_size = 0; in perf_prepare_sample_aux()
7056 if (WARN_ON_ONCE(READ_ONCE(sampler->state) != PERF_EVENT_STATE_ACTIVE)) in perf_prepare_sample_aux()
7059 if (WARN_ON_ONCE(READ_ONCE(sampler->oncpu) != smp_processor_id())) in perf_prepare_sample_aux()
7070 if (READ_ONCE(rb->aux_in_sampling)) { in perf_prepare_sample_aux()
7071 data->aux_size = 0; in perf_prepare_sample_aux()
7074 data->aux_size = ALIGN(size, sizeof(u64)); in perf_prepare_sample_aux()
7079 return data->aux_size; in perf_prepare_sample_aux()
7091 * Normal ->start()/->stop() callbacks run in IRQ mode in scheduler in perf_pmu_snapshot_aux()
7093 * the IRQ ones, that is, for example, re-starting an event that's just in perf_pmu_snapshot_aux()
7097 * IRQs need to be disabled to prevent IPIs from racing with us. in perf_pmu_snapshot_aux()
7104 WRITE_ONCE(rb->aux_in_sampling, 1); in perf_pmu_snapshot_aux()
7107 ret = event->pmu->snapshot_aux(event, handle, size); in perf_pmu_snapshot_aux()
7110 WRITE_ONCE(rb->aux_in_sampling, 0); in perf_pmu_snapshot_aux()
7120 struct perf_event *sampler = event->aux_event; in perf_aux_sample_output()
7125 if (WARN_ON_ONCE(!sampler || !data->aux_size)) in perf_aux_sample_output()
7132 size = perf_pmu_snapshot_aux(rb, sampler, handle, data->aux_size); in perf_aux_sample_output()
7136 * non-zero surplus that it didn't copy), which in its current in perf_aux_sample_output()
7144 * The pad comes from ALIGN()ing data->aux_size up to u64 in in perf_aux_sample_output()
7147 pad = data->aux_size - size; in perf_aux_sample_output()
7161 * A set of common sample data types saved even for non-sample records
7162 * when event->attr.sample_id_all is set.
7172 data->type = event->attr.sample_type; in __perf_event_header__init_id()
7173 data->sample_flags |= data->type & PERF_SAMPLE_ID_ALL; in __perf_event_header__init_id()
7177 data->tid_entry.pid = perf_event_pid(event, current); in __perf_event_header__init_id()
7178 data->tid_entry.tid = perf_event_tid(event, current); in __perf_event_header__init_id()
7182 data->time = perf_event_clock(event); in __perf_event_header__init_id()
7185 data->id = primary_event_id(event); in __perf_event_header__init_id()
7188 data->stream_id = event->id; in __perf_event_header__init_id()
7191 data->cpu_entry.cpu = raw_smp_processor_id(); in __perf_event_header__init_id()
7192 data->cpu_entry.reserved = 0; in __perf_event_header__init_id()
7200 if (event->attr.sample_id_all) { in perf_event_header__init_id()
7201 header->size += event->id_header_size; in perf_event_header__init_id()
7202 __perf_event_header__init_id(data, event, event->attr.sample_type); in perf_event_header__init_id()
7209 u64 sample_type = data->type; in __perf_event__output_id_sample()
7212 perf_output_put(handle, data->tid_entry); in __perf_event__output_id_sample()
7215 perf_output_put(handle, data->time); in __perf_event__output_id_sample()
7218 perf_output_put(handle, data->id); in __perf_event__output_id_sample()
7221 perf_output_put(handle, data->stream_id); in __perf_event__output_id_sample()
7224 perf_output_put(handle, data->cpu_entry); in __perf_event__output_id_sample()
7227 perf_output_put(handle, data->id); in __perf_event__output_id_sample()
7234 if (event->attr.sample_id_all) in perf_event__output_id_sample()
7242 u64 read_format = event->attr.read_format; in perf_output_read_one()
7249 atomic64_read(&event->child_total_time_enabled); in perf_output_read_one()
7253 atomic64_read(&event->child_total_time_running); in perf_output_read_one()
7258 values[n++] = atomic64_read(&event->lost_samples); in perf_output_read_one()
7267 struct perf_event *leader = event->group_leader, *sub; in perf_output_read_group()
7268 u64 read_format = event->attr.read_format; in perf_output_read_group()
7279 values[n++] = 1 + leader->nr_siblings; in perf_output_read_group()
7288 (leader->state == PERF_EVENT_STATE_ACTIVE)) in perf_output_read_group()
7289 leader->pmu->read(leader); in perf_output_read_group()
7295 values[n++] = atomic64_read(&leader->lost_samples); in perf_output_read_group()
7303 (sub->state == PERF_EVENT_STATE_ACTIVE)) in perf_output_read_group()
7304 sub->pmu->read(sub); in perf_output_read_group()
7310 values[n++] = atomic64_read(&sub->lost_samples); in perf_output_read_group()
7332 u64 read_format = event->attr.read_format; in perf_output_read()
7346 if (event->attr.read_format & PERF_FORMAT_GROUP) in perf_output_read()
7357 u64 sample_type = data->type; in perf_output_sample()
7362 perf_output_put(handle, data->id); in perf_output_sample()
7365 perf_output_put(handle, data->ip); in perf_output_sample()
7368 perf_output_put(handle, data->tid_entry); in perf_output_sample()
7371 perf_output_put(handle, data->time); in perf_output_sample()
7374 perf_output_put(handle, data->addr); in perf_output_sample()
7377 perf_output_put(handle, data->id); in perf_output_sample()
7380 perf_output_put(handle, data->stream_id); in perf_output_sample()
7383 perf_output_put(handle, data->cpu_entry); in perf_output_sample()
7386 perf_output_put(handle, data->period); in perf_output_sample()
7394 size += data->callchain->nr; in perf_output_sample()
7396 __output_copy(handle, data->callchain, size); in perf_output_sample()
7400 struct perf_raw_record *raw = data->raw; in perf_output_sample()
7403 struct perf_raw_frag *frag = &raw->frag; in perf_output_sample()
7405 perf_output_put(handle, raw->size); in perf_output_sample()
7407 if (frag->copy) { in perf_output_sample()
7408 __output_custom(handle, frag->copy, in perf_output_sample()
7409 frag->data, frag->size); in perf_output_sample()
7411 __output_copy(handle, frag->data, in perf_output_sample()
7412 frag->size); in perf_output_sample()
7416 frag = frag->next; in perf_output_sample()
7418 if (frag->pad) in perf_output_sample()
7419 __output_skip(handle, NULL, frag->pad); in perf_output_sample()
7433 if (data->br_stack) { in perf_output_sample()
7436 size = data->br_stack->nr in perf_output_sample()
7439 perf_output_put(handle, data->br_stack->nr); in perf_output_sample()
7441 perf_output_put(handle, data->br_stack->hw_idx); in perf_output_sample()
7442 perf_output_copy(handle, data->br_stack->entries, size); in perf_output_sample()
7453 u64 abi = data->regs_user.abi; in perf_output_sample()
7462 u64 mask = event->attr.sample_regs_user; in perf_output_sample()
7464 data->regs_user.regs, in perf_output_sample()
7471 data->stack_user_size, in perf_output_sample()
7472 data->regs_user.regs); in perf_output_sample()
7476 perf_output_put(handle, data->weight.full); in perf_output_sample()
7479 perf_output_put(handle, data->data_src.val); in perf_output_sample()
7482 perf_output_put(handle, data->txn); in perf_output_sample()
7485 u64 abi = data->regs_intr.abi; in perf_output_sample()
7493 u64 mask = event->attr.sample_regs_intr; in perf_output_sample()
7496 data->regs_intr.regs, in perf_output_sample()
7502 perf_output_put(handle, data->phys_addr); in perf_output_sample()
7505 perf_output_put(handle, data->cgroup); in perf_output_sample()
7508 perf_output_put(handle, data->data_page_size); in perf_output_sample()
7511 perf_output_put(handle, data->code_page_size); in perf_output_sample()
7514 perf_output_put(handle, data->aux_size); in perf_output_sample()
7516 if (data->aux_size) in perf_output_sample()
7520 if (!event->attr.watermark) { in perf_output_sample()
7521 int wakeup_events = event->attr.wakeup_events; in perf_output_sample()
7524 struct perf_buffer *rb = handle->rb; in perf_output_sample()
7525 int events = local_inc_return(&rb->events); in perf_output_sample()
7528 local_sub(wakeup_events, &rb->events); in perf_output_sample()
7529 local_inc(&rb->wakeup); in perf_output_sample()
7552 * Try IRQ-safe get_user_page_fast_only first. in perf_virt_to_phys()
7555 if (current->mm != NULL) { in perf_virt_to_phys()
7640 * Software page-table walkers must disable IRQs, in perf_get_page_size()
7645 mm = current->mm; in perf_get_page_size()
7666 bool kernel = !event->attr.exclude_callchain_kernel; in perf_callchain()
7667 bool user = !event->attr.exclude_callchain_user; in perf_callchain()
7668 /* Disallow cross-task user callchains. */ in perf_callchain()
7669 bool crosstask = event->ctx->task && event->ctx->task != current; in perf_callchain()
7670 const u32 max_stack = event->attr.sample_max_stack; in perf_callchain()
7690 u64 sample_type = event->attr.sample_type; in perf_prepare_sample()
7704 filtered_sample_type &= ~data->sample_flags; in perf_prepare_sample()
7707 /* Make sure it has the correct data->type for output */ in perf_prepare_sample()
7708 data->type = event->attr.sample_type; in perf_prepare_sample()
7715 data->ip = perf_instruction_pointer(regs); in perf_prepare_sample()
7716 data->sample_flags |= PERF_SAMPLE_IP; in perf_prepare_sample()
7723 data->raw = NULL; in perf_prepare_sample()
7724 data->dyn_size += sizeof(u64); in perf_prepare_sample()
7725 data->sample_flags |= PERF_SAMPLE_RAW; in perf_prepare_sample()
7729 data->br_stack = NULL; in perf_prepare_sample()
7730 data->dyn_size += sizeof(u64); in perf_prepare_sample()
7731 data->sample_flags |= PERF_SAMPLE_BRANCH_STACK; in perf_prepare_sample()
7735 perf_sample_regs_user(&data->regs_user, regs); in perf_prepare_sample()
7742 if ((sample_type & ~data->sample_flags) & PERF_SAMPLE_REGS_USER) { in perf_prepare_sample()
7746 if (data->regs_user.regs) { in perf_prepare_sample()
7747 u64 mask = event->attr.sample_regs_user; in perf_prepare_sample()
7751 data->dyn_size += size; in perf_prepare_sample()
7752 data->sample_flags |= PERF_SAMPLE_REGS_USER; in perf_prepare_sample()
7762 u16 stack_size = event->attr.sample_stack_user; in perf_prepare_sample()
7767 data->regs_user.regs); in perf_prepare_sample()
7777 data->stack_user_size = stack_size; in perf_prepare_sample()
7778 data->dyn_size += size; in perf_prepare_sample()
7779 data->sample_flags |= PERF_SAMPLE_STACK_USER; in perf_prepare_sample()
7783 data->weight.full = 0; in perf_prepare_sample()
7784 data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE; in perf_prepare_sample()
7788 data->data_src.val = PERF_MEM_NA; in perf_prepare_sample()
7789 data->sample_flags |= PERF_SAMPLE_DATA_SRC; in perf_prepare_sample()
7793 data->txn = 0; in perf_prepare_sample()
7794 data->sample_flags |= PERF_SAMPLE_TRANSACTION; in perf_prepare_sample()
7798 data->addr = 0; in perf_prepare_sample()
7799 data->sample_flags |= PERF_SAMPLE_ADDR; in perf_prepare_sample()
7806 perf_sample_regs_intr(&data->regs_intr, regs); in perf_prepare_sample()
7808 if (data->regs_intr.regs) { in perf_prepare_sample()
7809 u64 mask = event->attr.sample_regs_intr; in perf_prepare_sample()
7814 data->dyn_size += size; in perf_prepare_sample()
7815 data->sample_flags |= PERF_SAMPLE_REGS_INTR; in perf_prepare_sample()
7819 data->phys_addr = perf_virt_to_phys(data->addr); in perf_prepare_sample()
7820 data->sample_flags |= PERF_SAMPLE_PHYS_ADDR; in perf_prepare_sample()
7828 cgrp = task_css_check(current, perf_event_cgrp_id, 1)->cgroup; in perf_prepare_sample()
7829 data->cgroup = cgroup_id(cgrp); in perf_prepare_sample()
7830 data->sample_flags |= PERF_SAMPLE_CGROUP; in perf_prepare_sample()
7836 * require PERF_SAMPLE_ADDR, kernel implicitly retrieve the data->addr, in perf_prepare_sample()
7840 data->data_page_size = perf_get_page_size(data->addr); in perf_prepare_sample()
7841 data->sample_flags |= PERF_SAMPLE_DATA_PAGE_SIZE; in perf_prepare_sample()
7845 data->code_page_size = perf_get_page_size(data->ip); in perf_prepare_sample()
7846 data->sample_flags |= PERF_SAMPLE_CODE_PAGE_SIZE; in perf_prepare_sample()
7861 size = min_t(size_t, U16_MAX - header_size, in perf_prepare_sample()
7862 event->attr.aux_sample_size); in perf_prepare_sample()
7867 data->dyn_size += size + sizeof(u64); /* size above */ in perf_prepare_sample()
7868 data->sample_flags |= PERF_SAMPLE_AUX; in perf_prepare_sample()
7877 header->type = PERF_RECORD_SAMPLE; in perf_prepare_header()
7878 header->size = perf_sample_data_size(data, event); in perf_prepare_header()
7879 header->misc = perf_misc_flags(regs); in perf_prepare_header()
7889 WARN_ON_ONCE(header->size & 7); in perf_prepare_header()
7969 .size = sizeof(read_event) + event->read_size, in perf_event_read_event()
7997 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { in perf_iterate_ctx()
7999 if (event->state < PERF_EVENT_STATE_INACTIVE) in perf_iterate_ctx()
8014 list_for_each_entry_rcu(event, &pel->list, sb_list) { in perf_iterate_sb_cpu()
8017 * if we observe event->ctx, both event and ctx will be in perf_iterate_sb_cpu()
8020 if (!smp_load_acquire(&event->ctx)) in perf_iterate_sb_cpu()
8023 if (event->state < PERF_EVENT_STATE_INACTIVE) in perf_iterate_sb_cpu()
8032 * Iterate all events that need to receive side-band events.
8058 ctx = rcu_dereference(current->perf_event_ctxp); in perf_iterate_sb()
8067 * Clear all file-based filters at exec, they'll have to be
8068 * re-instated when/if these objects are mmapped again.
8080 raw_spin_lock_irqsave(&ifh->lock, flags); in perf_event_addr_filters_exec()
8081 list_for_each_entry(filter, &ifh->list, entry) { in perf_event_addr_filters_exec()
8082 if (filter->path.dentry) { in perf_event_addr_filters_exec()
8083 event->addr_filter_ranges[count].start = 0; in perf_event_addr_filters_exec()
8084 event->addr_filter_ranges[count].size = 0; in perf_event_addr_filters_exec()
8092 event->addr_filters_gen++; in perf_event_addr_filters_exec()
8093 raw_spin_unlock_irqrestore(&ifh->lock, flags); in perf_event_addr_filters_exec()
8122 struct perf_event *parent = event->parent; in __perf_event_output_stop()
8124 struct perf_buffer *rb = ro->rb; in __perf_event_output_stop()
8137 * ring-buffer, but it will be the child that's actually using it. in __perf_event_output_stop()
8141 * which will make us skip the event that actually needs to be stopped. in __perf_event_output_stop()
8142 * So ring_buffer_attach() has to stop an aux event before re-assigning in __perf_event_output_stop()
8145 if (rcu_dereference(parent->rb) == rb) in __perf_event_output_stop()
8146 ro->err = __perf_event_stop(&sd); in __perf_event_output_stop()
8154 .rb = event->rb, in __perf_pmu_output_stop()
8158 perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false); in __perf_pmu_output_stop()
8159 if (cpuctx->task_ctx) in __perf_pmu_output_stop()
8160 perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop, in __perf_pmu_output_stop()
8174 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) { in perf_pmu_output_stop()
8176 * For per-CPU events, we need to make sure that neither they in perf_pmu_output_stop()
8177 * nor their children are running; for cpu==-1 events it's in perf_pmu_output_stop()
8181 cpu = iter->cpu; in perf_pmu_output_stop()
8182 if (cpu == -1) in perf_pmu_output_stop()
8183 cpu = READ_ONCE(iter->oncpu); in perf_pmu_output_stop()
8185 if (cpu == -1) in perf_pmu_output_stop()
8189 if (err == -EAGAIN) { in perf_pmu_output_stop()
8198 * task tracking -- fork/exit
8220 return event->attr.comm || event->attr.mmap || in perf_event_task_match()
8221 event->attr.mmap2 || event->attr.mmap_data || in perf_event_task_match()
8222 event->attr.task; in perf_event_task_match()
8231 struct task_struct *task = task_event->task; in perf_event_task_output()
8232 int ret, size = task_event->event_id.header.size; in perf_event_task_output()
8237 perf_event_header__init_id(&task_event->event_id.header, &sample, event); in perf_event_task_output()
8240 task_event->event_id.header.size); in perf_event_task_output()
8244 task_event->event_id.pid = perf_event_pid(event, task); in perf_event_task_output()
8245 task_event->event_id.tid = perf_event_tid(event, task); in perf_event_task_output()
8247 if (task_event->event_id.header.type == PERF_RECORD_EXIT) { in perf_event_task_output()
8248 task_event->event_id.ppid = perf_event_pid(event, in perf_event_task_output()
8249 task->real_parent); in perf_event_task_output()
8250 task_event->event_id.ptid = perf_event_pid(event, in perf_event_task_output()
8251 task->real_parent); in perf_event_task_output()
8253 task_event->event_id.ppid = perf_event_pid(event, current); in perf_event_task_output()
8254 task_event->event_id.ptid = perf_event_tid(event, current); in perf_event_task_output()
8257 task_event->event_id.time = perf_event_clock(event); in perf_event_task_output()
8259 perf_output_put(&handle, task_event->event_id); in perf_event_task_output()
8265 task_event->event_id.header.size = size; in perf_event_task_output()
8326 return event->attr.comm; in perf_event_comm_match()
8335 int size = comm_event->event_id.header.size; in perf_event_comm_output()
8341 perf_event_header__init_id(&comm_event->event_id.header, &sample, event); in perf_event_comm_output()
8343 comm_event->event_id.header.size); in perf_event_comm_output()
8348 comm_event->event_id.pid = perf_event_pid(event, comm_event->task); in perf_event_comm_output()
8349 comm_event->event_id.tid = perf_event_tid(event, comm_event->task); in perf_event_comm_output()
8351 perf_output_put(&handle, comm_event->event_id); in perf_event_comm_output()
8352 __output_copy(&handle, comm_event->comm, in perf_event_comm_output()
8353 comm_event->comm_size); in perf_event_comm_output()
8359 comm_event->event_id.header.size = size; in perf_event_comm_output()
8368 strscpy(comm, comm_event->task->comm, sizeof(comm)); in perf_event_comm_event()
8371 comm_event->comm = comm; in perf_event_comm_event()
8372 comm_event->comm_size = size; in perf_event_comm_event()
8374 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; in perf_event_comm_event()
8425 return event->attr.namespaces; in perf_event_namespaces_match()
8434 u16 header_size = namespaces_event->event_id.header.size; in perf_event_namespaces_output()
8440 perf_event_header__init_id(&namespaces_event->event_id.header, in perf_event_namespaces_output()
8443 namespaces_event->event_id.header.size); in perf_event_namespaces_output()
8447 namespaces_event->event_id.pid = perf_event_pid(event, in perf_event_namespaces_output()
8448 namespaces_event->task); in perf_event_namespaces_output()
8449 namespaces_event->event_id.tid = perf_event_tid(event, in perf_event_namespaces_output()
8450 namespaces_event->task); in perf_event_namespaces_output()
8452 perf_output_put(&handle, namespaces_event->event_id); in perf_event_namespaces_output()
8458 namespaces_event->event_id.header.size = header_size; in perf_event_namespaces_output()
8471 ns_inode = ns_path.dentry->d_inode; in perf_fill_ns_link_info()
8472 ns_link_info->dev = new_encode_dev(ns_inode->i_sb->s_dev); in perf_fill_ns_link_info()
8473 ns_link_info->ino = ns_inode->i_ino; in perf_fill_ns_link_info()
8553 return event->attr.cgroup; in perf_event_cgroup_match()
8561 u16 header_size = cgroup_event->event_id.header.size; in perf_event_cgroup_output()
8567 perf_event_header__init_id(&cgroup_event->event_id.header, in perf_event_cgroup_output()
8570 cgroup_event->event_id.header.size); in perf_event_cgroup_output()
8574 perf_output_put(&handle, cgroup_event->event_id); in perf_event_cgroup_output()
8575 __output_copy(&handle, cgroup_event->path, cgroup_event->path_size); in perf_event_cgroup_output()
8581 cgroup_event->event_id.header.size = header_size; in perf_event_cgroup_output()
8610 cgroup_path(cgrp, pathname, PATH_MAX - sizeof(u64)); in perf_event_cgroup()
8666 struct vm_area_struct *vma = mmap_event->vma; in perf_event_mmap_match()
8667 int executable = vma->vm_flags & VM_EXEC; in perf_event_mmap_match()
8669 return (!executable && event->attr.mmap_data) || in perf_event_mmap_match()
8670 (executable && (event->attr.mmap || event->attr.mmap2)); in perf_event_mmap_match()
8679 int size = mmap_event->event_id.header.size; in perf_event_mmap_output()
8680 u32 type = mmap_event->event_id.header.type; in perf_event_mmap_output()
8687 if (event->attr.mmap2) { in perf_event_mmap_output()
8688 mmap_event->event_id.header.type = PERF_RECORD_MMAP2; in perf_event_mmap_output()
8689 mmap_event->event_id.header.size += sizeof(mmap_event->maj); in perf_event_mmap_output()
8690 mmap_event->event_id.header.size += sizeof(mmap_event->min); in perf_event_mmap_output()
8691 mmap_event->event_id.header.size += sizeof(mmap_event->ino); in perf_event_mmap_output()
8692 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); in perf_event_mmap_output()
8693 mmap_event->event_id.header.size += sizeof(mmap_event->prot); in perf_event_mmap_output()
8694 mmap_event->event_id.header.size += sizeof(mmap_event->flags); in perf_event_mmap_output()
8697 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); in perf_event_mmap_output()
8699 mmap_event->event_id.header.size); in perf_event_mmap_output()
8703 mmap_event->event_id.pid = perf_event_pid(event, current); in perf_event_mmap_output()
8704 mmap_event->event_id.tid = perf_event_tid(event, current); in perf_event_mmap_output()
8706 use_build_id = event->attr.build_id && mmap_event->build_id_size; in perf_event_mmap_output()
8708 if (event->attr.mmap2 && use_build_id) in perf_event_mmap_output()
8709 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_BUILD_ID; in perf_event_mmap_output()
8711 perf_output_put(&handle, mmap_event->event_id); in perf_event_mmap_output()
8713 if (event->attr.mmap2) { in perf_event_mmap_output()
8715 u8 size[4] = { (u8) mmap_event->build_id_size, 0, 0, 0 }; in perf_event_mmap_output()
8718 __output_copy(&handle, mmap_event->build_id, BUILD_ID_SIZE_MAX); in perf_event_mmap_output()
8720 perf_output_put(&handle, mmap_event->maj); in perf_event_mmap_output()
8721 perf_output_put(&handle, mmap_event->min); in perf_event_mmap_output()
8722 perf_output_put(&handle, mmap_event->ino); in perf_event_mmap_output()
8723 perf_output_put(&handle, mmap_event->ino_generation); in perf_event_mmap_output()
8725 perf_output_put(&handle, mmap_event->prot); in perf_event_mmap_output()
8726 perf_output_put(&handle, mmap_event->flags); in perf_event_mmap_output()
8729 __output_copy(&handle, mmap_event->file_name, in perf_event_mmap_output()
8730 mmap_event->file_size); in perf_event_mmap_output()
8736 mmap_event->event_id.header.size = size; in perf_event_mmap_output()
8737 mmap_event->event_id.header.type = type; in perf_event_mmap_output()
8742 struct vm_area_struct *vma = mmap_event->vma; in perf_event_mmap_event()
8743 struct file *file = vma->vm_file; in perf_event_mmap_event()
8752 if (vma->vm_flags & VM_READ) in perf_event_mmap_event()
8754 if (vma->vm_flags & VM_WRITE) in perf_event_mmap_event()
8756 if (vma->vm_flags & VM_EXEC) in perf_event_mmap_event()
8759 if (vma->vm_flags & VM_MAYSHARE) in perf_event_mmap_event()
8764 if (vma->vm_flags & VM_LOCKED) in perf_event_mmap_event()
8783 name = file_path(file, buf, PATH_MAX - sizeof(u64)); in perf_event_mmap_event()
8788 inode = file_inode(vma->vm_file); in perf_event_mmap_event()
8789 dev = inode->i_sb->s_dev; in perf_event_mmap_event()
8790 ino = inode->i_ino; in perf_event_mmap_event()
8791 gen = inode->i_generation; in perf_event_mmap_event()
8797 if (vma->vm_ops && vma->vm_ops->name) in perf_event_mmap_event()
8798 name = (char *) vma->vm_ops->name(vma); in perf_event_mmap_event()
8824 mmap_event->file_name = name; in perf_event_mmap_event()
8825 mmap_event->file_size = size; in perf_event_mmap_event()
8826 mmap_event->maj = maj; in perf_event_mmap_event()
8827 mmap_event->min = min; in perf_event_mmap_event()
8828 mmap_event->ino = ino; in perf_event_mmap_event()
8829 mmap_event->ino_generation = gen; in perf_event_mmap_event()
8830 mmap_event->prot = prot; in perf_event_mmap_event()
8831 mmap_event->flags = flags; in perf_event_mmap_event()
8833 if (!(vma->vm_flags & VM_EXEC)) in perf_event_mmap_event()
8834 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; in perf_event_mmap_event()
8836 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; in perf_event_mmap_event()
8839 build_id_parse(vma, mmap_event->build_id, &mmap_event->build_id_size); in perf_event_mmap_event()
8855 /* d_inode(NULL) won't be equal to any mapped user-space file */ in perf_addr_filter_match()
8856 if (!filter->path.dentry) in perf_addr_filter_match()
8859 if (d_inode(filter->path.dentry) != file_inode(file)) in perf_addr_filter_match()
8862 if (filter->offset > offset + size) in perf_addr_filter_match()
8865 if (filter->offset + filter->size < offset) in perf_addr_filter_match()
8875 unsigned long vma_size = vma->vm_end - vma->vm_start; in perf_addr_filter_vma_adjust()
8876 unsigned long off = vma->vm_pgoff << PAGE_SHIFT; in perf_addr_filter_vma_adjust()
8877 struct file *file = vma->vm_file; in perf_addr_filter_vma_adjust()
8882 if (filter->offset < off) { in perf_addr_filter_vma_adjust()
8883 fr->start = vma->vm_start; in perf_addr_filter_vma_adjust()
8884 fr->size = min(vma_size, filter->size - (off - filter->offset)); in perf_addr_filter_vma_adjust()
8886 fr->start = vma->vm_start + filter->offset - off; in perf_addr_filter_vma_adjust()
8887 fr->size = min(vma->vm_end - fr->start, filter->size); in perf_addr_filter_vma_adjust()
8904 if (!vma->vm_file) in __perf_addr_filters_adjust()
8907 raw_spin_lock_irqsave(&ifh->lock, flags); in __perf_addr_filters_adjust()
8908 list_for_each_entry(filter, &ifh->list, entry) { in __perf_addr_filters_adjust()
8910 &event->addr_filter_ranges[count])) in __perf_addr_filters_adjust()
8917 event->addr_filters_gen++; in __perf_addr_filters_adjust()
8918 raw_spin_unlock_irqrestore(&ifh->lock, flags); in __perf_addr_filters_adjust()
8935 if (!(vma->vm_flags & VM_EXEC)) in perf_addr_filters_adjust()
8939 ctx = rcu_dereference(current->perf_event_ctxp); in perf_addr_filters_adjust()
8964 .start = vma->vm_start, in perf_event_mmap()
8965 .len = vma->vm_end - vma->vm_start, in perf_event_mmap()
8966 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, in perf_event_mmap()
9064 return event->attr.context_switch; in perf_event_switch_match()
9077 /* Only CPU-wide events are allowed to see next/prev pid/tid */ in perf_event_switch_output()
9078 if (event->ctx->task) { in perf_event_switch_output()
9079 se->event_id.header.type = PERF_RECORD_SWITCH; in perf_event_switch_output()
9080 se->event_id.header.size = sizeof(se->event_id.header); in perf_event_switch_output()
9082 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE; in perf_event_switch_output()
9083 se->event_id.header.size = sizeof(se->event_id); in perf_event_switch_output()
9084 se->event_id.next_prev_pid = in perf_event_switch_output()
9085 perf_event_pid(event, se->next_prev); in perf_event_switch_output()
9086 se->event_id.next_prev_tid = in perf_event_switch_output()
9087 perf_event_tid(event, se->next_prev); in perf_event_switch_output()
9090 perf_event_header__init_id(&se->event_id.header, &sample, event); in perf_event_switch_output()
9092 ret = perf_output_begin(&handle, &sample, event, se->event_id.header.size); in perf_event_switch_output()
9096 if (event->ctx->task) in perf_event_switch_output()
9097 perf_output_put(&handle, se->event_id.header); in perf_event_switch_output()
9099 perf_output_put(&handle, se->event_id); in perf_event_switch_output()
9127 if (!sched_in && task->on_rq) { in perf_event_switch()
9136 * IRQ throttle logging
9158 .stream_id = event->id, in perf_log_throttle()
9194 return event->attr.ksymbol; in perf_event_ksymbol_match()
9207 perf_event_header__init_id(&ksymbol_event->event_id.header, in perf_event_ksymbol_output()
9210 ksymbol_event->event_id.header.size); in perf_event_ksymbol_output()
9214 perf_output_put(&handle, ksymbol_event->event_id); in perf_event_ksymbol_output()
9215 __output_copy(&handle, ksymbol_event->name, ksymbol_event->name_len); in perf_event_ksymbol_output()
9284 return event->attr.bpf_event; in perf_event_bpf_match()
9297 perf_event_header__init_id(&bpf_event->event_id.header, in perf_event_bpf_output()
9300 bpf_event->event_id.header.size); in perf_event_bpf_output()
9304 perf_output_put(&handle, bpf_event->event_id); in perf_event_bpf_output()
9317 (u64)(unsigned long)prog->bpf_func, in perf_event_bpf_emit_ksymbols()
9318 prog->jited_len, unregister, in perf_event_bpf_emit_ksymbols()
9319 prog->aux->ksym.name); in perf_event_bpf_emit_ksymbols()
9321 for (i = 1; i < prog->aux->func_cnt; i++) { in perf_event_bpf_emit_ksymbols()
9322 struct bpf_prog *subprog = prog->aux->func[i]; in perf_event_bpf_emit_ksymbols()
9326 (u64)(unsigned long)subprog->bpf_func, in perf_event_bpf_emit_ksymbols()
9327 subprog->jited_len, unregister, in perf_event_bpf_emit_ksymbols()
9328 subprog->aux->ksym.name); in perf_event_bpf_emit_ksymbols()
9364 .id = prog->aux->id, in perf_event_bpf_event()
9370 memcpy(bpf_event.event_id.tag, prog->tag, BPF_TAG_SIZE); in perf_event_bpf_event()
9390 return event->attr.text_poke; in perf_event_text_poke_match()
9404 perf_event_header__init_id(&text_poke_event->event_id.header, &sample, event); in perf_event_text_poke_output()
9407 text_poke_event->event_id.header.size); in perf_event_text_poke_output()
9411 perf_output_put(&handle, text_poke_event->event_id); in perf_event_text_poke_output()
9412 perf_output_put(&handle, text_poke_event->old_len); in perf_event_text_poke_output()
9413 perf_output_put(&handle, text_poke_event->new_len); in perf_event_text_poke_output()
9415 __output_copy(&handle, text_poke_event->old_bytes, text_poke_event->old_len); in perf_event_text_poke_output()
9416 __output_copy(&handle, text_poke_event->new_bytes, text_poke_event->new_len); in perf_event_text_poke_output()
9418 if (text_poke_event->pad) in perf_event_text_poke_output()
9419 __output_copy(&handle, &padding, text_poke_event->pad); in perf_event_text_poke_output()
9437 pad = ALIGN(tot, sizeof(u64)) - tot; in perf_event_text_poke()
9460 event->attach_state |= PERF_ATTACH_ITRACE; in perf_event_itrace_started()
9474 if (event->parent) in perf_log_itrace_start()
9475 event = event->parent; in perf_log_itrace_start()
9477 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) || in perf_log_itrace_start()
9478 event->attach_state & PERF_ATTACH_ITRACE) in perf_log_itrace_start()
9509 if (event->parent) in perf_report_aux_output_id()
9510 event = event->parent; in perf_report_aux_output_id()
9531 __perf_event_account_interrupt(struct perf_event *event, int throttle) in __perf_event_account_interrupt() argument
9533 struct hw_perf_event *hwc = &event->hw; in __perf_event_account_interrupt()
9538 if (seq != hwc->interrupts_seq) { in __perf_event_account_interrupt()
9539 hwc->interrupts_seq = seq; in __perf_event_account_interrupt()
9540 hwc->interrupts = 1; in __perf_event_account_interrupt()
9542 hwc->interrupts++; in __perf_event_account_interrupt()
9543 if (unlikely(throttle && in __perf_event_account_interrupt()
9544 hwc->interrupts > max_samples_per_tick)) { in __perf_event_account_interrupt()
9547 hwc->interrupts = MAX_INTERRUPTS; in __perf_event_account_interrupt()
9553 if (event->attr.freq) { in __perf_event_account_interrupt()
9555 s64 delta = now - hwc->freq_time_stamp; in __perf_event_account_interrupt()
9557 hwc->freq_time_stamp = now; in __perf_event_account_interrupt()
9560 perf_adjust_period(event, delta, hwc->last_period, true); in __perf_event_account_interrupt()
9578 if (event->attr.exclude_kernel && !user_mode(regs)) in sample_is_allowed()
9589 int throttle, struct perf_sample_data *data, in __perf_event_overflow() argument
9592 int events = atomic_read(&event->event_limit); in __perf_event_overflow()
9596 * Non-sampling counters might still use the PMI to fold short in __perf_event_overflow()
9602 ret = __perf_event_account_interrupt(event, throttle); in __perf_event_overflow()
9609 event->pending_kill = POLL_IN; in __perf_event_overflow()
9610 if (events && atomic_dec_and_test(&event->event_limit)) { in __perf_event_overflow()
9612 event->pending_kill = POLL_HUP; in __perf_event_overflow()
9616 if (event->attr.sigtrap) { in __perf_event_overflow()
9628 if (!event->pending_sigtrap) { in __perf_event_overflow()
9629 event->pending_sigtrap = pending_id; in __perf_event_overflow()
9630 local_inc(&event->ctx->nr_pending); in __perf_event_overflow()
9631 } else if (event->attr.exclude_kernel && valid_sample) { in __perf_event_overflow()
9639 * 2. Events that can overflow again before the IRQ- in __perf_event_overflow()
9642 * check 32-bit hash of the current IP. in __perf_event_overflow()
9644 WARN_ON_ONCE(event->pending_sigtrap != pending_id); in __perf_event_overflow()
9647 event->pending_addr = 0; in __perf_event_overflow()
9648 if (valid_sample && (data->sample_flags & PERF_SAMPLE_ADDR)) in __perf_event_overflow()
9649 event->pending_addr = data->addr; in __perf_event_overflow()
9650 irq_work_queue(&event->pending_irq); in __perf_event_overflow()
9653 READ_ONCE(event->overflow_handler)(event, data, regs); in __perf_event_overflow()
9655 if (*perf_event_fasync(event) && event->pending_kill) { in __perf_event_overflow()
9656 event->pending_wakeup = 1; in __perf_event_overflow()
9657 irq_work_queue(&event->pending_irq); in __perf_event_overflow()
9686 * We directly increment event->count and keep a second value in
9687 * event->hw.period_left to count intervals. This period event
9688 * is kept in the range [-sample_period, 0] so that we can use the
9694 struct hw_perf_event *hwc = &event->hw; in perf_swevent_set_period()
9695 u64 period = hwc->last_period; in perf_swevent_set_period() local
9699 hwc->last_period = hwc->sample_period; in perf_swevent_set_period()
9701 old = local64_read(&hwc->period_left); in perf_swevent_set_period()
9707 nr = div64_u64(period + val, period); in perf_swevent_set_period()
9708 offset = nr * period; in perf_swevent_set_period()
9709 val -= offset; in perf_swevent_set_period()
9710 } while (!local64_try_cmpxchg(&hwc->period_left, &old, val)); in perf_swevent_set_period()
9719 struct hw_perf_event *hwc = &event->hw; in perf_swevent_overflow()
9720 int throttle = 0; in perf_swevent_overflow() local
9725 if (hwc->interrupts == MAX_INTERRUPTS) in perf_swevent_overflow()
9728 for (; overflow; overflow--) { in perf_swevent_overflow()
9729 if (__perf_event_overflow(event, throttle, in perf_swevent_overflow()
9733 * hwc->interrupts == MAX_INTERRUPTS. in perf_swevent_overflow()
9737 throttle = 1; in perf_swevent_overflow()
9745 struct hw_perf_event *hwc = &event->hw; in perf_swevent_event()
9747 local64_add(nr, &event->count); in perf_swevent_event()
9755 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { in perf_swevent_event()
9756 data->period = nr; in perf_swevent_event()
9759 data->period = event->hw.last_period; in perf_swevent_event()
9761 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) in perf_swevent_event()
9764 if (local64_add_negative(nr, &hwc->period_left)) in perf_swevent_event()
9773 if (event->hw.state & PERF_HES_STOPPED) in perf_exclude_event()
9777 if (event->attr.exclude_user && user_mode(regs)) in perf_exclude_event()
9780 if (event->attr.exclude_kernel && !user_mode(regs)) in perf_exclude_event()
9793 if (event->attr.type != type) in perf_swevent_match()
9796 if (event->attr.config != event_id) in perf_swevent_match()
9817 return &hlist->heads[hash]; in __find_swevent_head()
9826 hlist = rcu_dereference(swhash->swevent_hlist); in find_swevent_head_rcu()
9838 u32 event_id = event->attr.config; in find_swevent_head()
9839 u64 type = event->attr.type; in find_swevent_head()
9846 hlist = rcu_dereference_protected(swhash->swevent_hlist, in find_swevent_head()
9847 lockdep_is_held(&event->ctx->lock)); in find_swevent_head()
9882 return get_recursion_context(swhash->recursion); in perf_swevent_get_recursion_context()
9890 put_recursion_context(swhash->recursion, rctx); in perf_swevent_put_recursion_context()
9927 struct hw_perf_event *hwc = &event->hw; in perf_swevent_add()
9931 hwc->last_period = hwc->sample_period; in perf_swevent_add()
9935 hwc->state = !(flags & PERF_EF_START); in perf_swevent_add()
9939 return -EINVAL; in perf_swevent_add()
9941 hlist_add_head_rcu(&event->hlist_entry, head); in perf_swevent_add()
9949 hlist_del_rcu(&event->hlist_entry); in perf_swevent_del()
9954 event->hw.state = 0; in perf_swevent_start()
9959 event->hw.state = PERF_HES_STOPPED; in perf_swevent_stop()
9966 return rcu_dereference_protected(swhash->swevent_hlist, in swevent_hlist_deref()
9967 lockdep_is_held(&swhash->hlist_mutex)); in swevent_hlist_deref()
9977 RCU_INIT_POINTER(swhash->swevent_hlist, NULL); in swevent_hlist_release()
9985 mutex_lock(&swhash->hlist_mutex); in swevent_hlist_put_cpu()
9987 if (!--swhash->hlist_refcount) in swevent_hlist_put_cpu()
9990 mutex_unlock(&swhash->hlist_mutex); in swevent_hlist_put_cpu()
10006 mutex_lock(&swhash->hlist_mutex); in swevent_hlist_get_cpu()
10013 err = -ENOMEM; in swevent_hlist_get_cpu()
10016 rcu_assign_pointer(swhash->swevent_hlist, hlist); in swevent_hlist_get_cpu()
10018 swhash->hlist_refcount++; in swevent_hlist_get_cpu()
10020 mutex_unlock(&swhash->hlist_mutex); in swevent_hlist_get_cpu()
10053 u64 event_id = event->attr.config; in sw_perf_event_destroy()
10055 WARN_ON(event->parent); in sw_perf_event_destroy()
10066 u64 event_id = event->attr.config; in perf_swevent_init()
10068 if (event->attr.type != PERF_TYPE_SOFTWARE) in perf_swevent_init()
10069 return -ENOENT; in perf_swevent_init()
10075 return -EOPNOTSUPP; in perf_swevent_init()
10079 event->attr.type = perf_cpu_clock.type; in perf_swevent_init()
10080 return -ENOENT; in perf_swevent_init()
10082 event->attr.type = perf_task_clock.type; in perf_swevent_init()
10083 return -ENOENT; in perf_swevent_init()
10090 return -ENOENT; in perf_swevent_init()
10092 if (!event->parent) { in perf_swevent_init()
10100 event->destroy = sw_perf_event_destroy; in perf_swevent_init()
10130 if (event->attr.type != PERF_TYPE_TRACEPOINT) in perf_tp_event_init()
10131 return -ENOENT; in perf_tp_event_init()
10137 return -EOPNOTSUPP; in perf_tp_event_init()
10143 event->destroy = tp_perf_event_destroy; in perf_tp_event_init()
10162 void *record = data->raw->frag.data; in perf_tp_filter_match()
10165 if (event->parent) in perf_tp_filter_match()
10166 event = event->parent; in perf_tp_filter_match()
10168 if (likely(!event->filter) || filter_match_preds(event->filter, record)) in perf_tp_filter_match()
10177 if (event->hw.state & PERF_HES_STOPPED) in perf_tp_event_match()
10180 * If exclude_kernel, only trace user-space tracepoints (uprobes) in perf_tp_event_match()
10182 if (event->attr.exclude_kernel && !user_mode(regs)) in perf_tp_event_match()
10203 perf_tp_event(call->event.type, count, raw_data, size, regs, head, in perf_trace_run_bpf_submit()
10215 if (event->attr.config != entry->type) in __perf_tp_event_target_task()
10218 if (event->attr.sigtrap) in __perf_tp_event_target_task()
10233 perf_event_groups_for_cpu_pmu(event, &ctx->pinned_groups, cpu, pmu) { in perf_tp_event_target_task()
10239 perf_event_groups_for_cpu_pmu(event, &ctx->flexible_groups, cpu, pmu) { in perf_tp_event_target_task()
10270 * Here use the same on-stack perf_sample_data, in perf_tp_event()
10271 * some members in data are event-specific and in perf_tp_event()
10272 * need to be re-computed for different sweveents. in perf_tp_event()
10273 * Re-initialize data->sample_flags safely to avoid in perf_tp_event()
10275 * because data->sample_flags is set. in perf_tp_event()
10290 ctx = rcu_dereference(task->perf_event_ctxp); in perf_tp_event()
10294 raw_spin_lock(&ctx->lock); in perf_tp_event()
10296 raw_spin_unlock(&ctx->lock); in perf_tp_event()
10323 PERF_UPROBE_REF_CTR_OFFSET_SHIFT = 64 - PERF_UPROBE_REF_CTR_OFFSET_BITS,
10362 if (event->attr.type != perf_kprobe.type) in perf_kprobe_event_init()
10363 return -ENOENT; in perf_kprobe_event_init()
10366 return -EACCES; in perf_kprobe_event_init()
10372 return -EOPNOTSUPP; in perf_kprobe_event_init()
10374 is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE; in perf_kprobe_event_init()
10379 event->destroy = perf_kprobe_destroy; in perf_kprobe_event_init()
10386 PMU_FORMAT_ATTR(ref_ctr_offset, "config:32-63");
10422 if (event->attr.type != perf_uprobe.type) in perf_uprobe_event_init()
10423 return -ENOENT; in perf_uprobe_event_init()
10426 return -EACCES; in perf_uprobe_event_init()
10432 return -EOPNOTSUPP; in perf_uprobe_event_init()
10434 is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE; in perf_uprobe_event_init()
10435 ref_ctr_offset = event->attr.config >> PERF_UPROBE_REF_CTR_OFFSET_SHIFT; in perf_uprobe_event_init()
10440 event->destroy = perf_uprobe_destroy; in perf_uprobe_event_init()
10450 perf_pmu_register(&perf_kprobe, "kprobe", -1); in perf_tp_register()
10453 perf_pmu_register(&perf_uprobe, "uprobe", -1); in perf_tp_register()
10478 prog = READ_ONCE(event->prog); in bpf_overflow_handler()
10489 event->orig_overflow_handler(event, data, regs); in bpf_overflow_handler()
10496 if (event->overflow_handler_context) in perf_event_set_bpf_handler()
10498 return -EINVAL; in perf_event_set_bpf_handler()
10500 if (event->prog) in perf_event_set_bpf_handler()
10501 return -EEXIST; in perf_event_set_bpf_handler()
10503 if (prog->type != BPF_PROG_TYPE_PERF_EVENT) in perf_event_set_bpf_handler()
10504 return -EINVAL; in perf_event_set_bpf_handler()
10506 if (event->attr.precise_ip && in perf_event_set_bpf_handler()
10507 prog->call_get_stack && in perf_event_set_bpf_handler()
10508 (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) || in perf_event_set_bpf_handler()
10509 event->attr.exclude_callchain_kernel || in perf_event_set_bpf_handler()
10510 event->attr.exclude_callchain_user)) { in perf_event_set_bpf_handler()
10520 return -EPROTO; in perf_event_set_bpf_handler()
10523 event->prog = prog; in perf_event_set_bpf_handler()
10524 event->bpf_cookie = bpf_cookie; in perf_event_set_bpf_handler()
10525 event->orig_overflow_handler = READ_ONCE(event->overflow_handler); in perf_event_set_bpf_handler()
10526 WRITE_ONCE(event->overflow_handler, bpf_overflow_handler); in perf_event_set_bpf_handler()
10532 struct bpf_prog *prog = event->prog; in perf_event_free_bpf_handler()
10537 WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler); in perf_event_free_bpf_handler()
10538 event->prog = NULL; in perf_event_free_bpf_handler()
10546 return -EOPNOTSUPP; in perf_event_set_bpf_handler()
10559 if (event->pmu == &perf_tracepoint) in perf_event_is_tracing()
10562 if (event->pmu == &perf_kprobe) in perf_event_is_tracing()
10566 if (event->pmu == &perf_uprobe) in perf_event_is_tracing()
10580 is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_KPROBE; in perf_event_set_bpf_prog()
10581 is_uprobe = event->tp_event->flags & TRACE_EVENT_FL_UPROBE; in perf_event_set_bpf_prog()
10582 is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT; in perf_event_set_bpf_prog()
10583 is_syscall_tp = is_syscall_trace_event(event->tp_event); in perf_event_set_bpf_prog()
10586 return -EINVAL; in perf_event_set_bpf_prog()
10588 if (((is_kprobe || is_uprobe) && prog->type != BPF_PROG_TYPE_KPROBE) || in perf_event_set_bpf_prog()
10589 (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) || in perf_event_set_bpf_prog()
10590 (is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT)) in perf_event_set_bpf_prog()
10591 return -EINVAL; in perf_event_set_bpf_prog()
10593 if (prog->type == BPF_PROG_TYPE_KPROBE && prog->aux->sleepable && !is_uprobe) in perf_event_set_bpf_prog()
10595 return -EINVAL; in perf_event_set_bpf_prog()
10598 if (prog->kprobe_override && !is_kprobe) in perf_event_set_bpf_prog()
10599 return -EINVAL; in perf_event_set_bpf_prog()
10602 int off = trace_event_get_offsets(event->tp_event); in perf_event_set_bpf_prog()
10604 if (prog->aux->max_ctx_offset > off) in perf_event_set_bpf_prog()
10605 return -EACCES; in perf_event_set_bpf_prog()
10633 return -ENOENT; in perf_event_set_bpf_prog()
10647 perf_sample_data_init(&sample, bp->attr.bp_addr, 0); in perf_bp_event()
10649 if (!bp->hw.state && !perf_exclude_event(bp, regs)) in perf_bp_event()
10660 int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu); in perf_addr_filter_new()
10667 INIT_LIST_HEAD(&filter->entry); in perf_addr_filter_new()
10668 list_add_tail(&filter->entry, filters); in perf_addr_filter_new()
10678 path_put(&filter->path); in free_filters_list()
10679 list_del(&filter->entry); in free_filters_list()
10697 if (event->parent) in perf_addr_filters_splice()
10700 raw_spin_lock_irqsave(&event->addr_filters.lock, flags); in perf_addr_filters_splice()
10702 list_splice_init(&event->addr_filters.list, &list); in perf_addr_filters_splice()
10704 list_splice(head, &event->addr_filters.list); in perf_addr_filters_splice()
10706 raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags); in perf_addr_filters_splice()
10724 if (!vma->vm_file) in perf_addr_filter_apply()
10739 struct task_struct *task = READ_ONCE(event->ctx->task); in perf_event_addr_filters_apply()
10746 * We may observe TASK_TOMBSTONE, which means that the event tear-down in perf_event_addr_filters_apply()
10752 if (ifh->nr_file_filters) { in perf_event_addr_filters_apply()
10760 raw_spin_lock_irqsave(&ifh->lock, flags); in perf_event_addr_filters_apply()
10761 list_for_each_entry(filter, &ifh->list, entry) { in perf_event_addr_filters_apply()
10762 if (filter->path.dentry) { in perf_event_addr_filters_apply()
10767 event->addr_filter_ranges[count].start = 0; in perf_event_addr_filters_apply()
10768 event->addr_filter_ranges[count].size = 0; in perf_event_addr_filters_apply()
10770 perf_addr_filter_apply(filter, mm, &event->addr_filter_ranges[count]); in perf_event_addr_filters_apply()
10772 event->addr_filter_ranges[count].start = filter->offset; in perf_event_addr_filters_apply()
10773 event->addr_filter_ranges[count].size = filter->size; in perf_event_addr_filters_apply()
10779 event->addr_filters_gen++; in perf_event_addr_filters_apply()
10780 raw_spin_unlock_irqrestore(&ifh->lock, flags); in perf_event_addr_filters_apply()
10782 if (ifh->nr_file_filters) { in perf_event_addr_filters_apply()
10794 * instruction address ranges. Filters are ioctl()ed to us from
10812 IF_ACT_NONE = -1,
10851 int ret = -EINVAL; in perf_event_parse_addr_filter()
10855 return -ENOMEM; in perf_event_parse_addr_filter()
10863 ret = -EINVAL; in perf_event_parse_addr_filter()
10883 filter->action = actions[token]; in perf_event_parse_addr_filter()
10898 ret = kstrtoul(args[0].from, 0, &filter->offset); in perf_event_parse_addr_filter()
10904 ret = kstrtoul(args[1].from, 0, &filter->size); in perf_event_parse_addr_filter()
10915 ret = -ENOMEM; in perf_event_parse_addr_filter()
10933 ret = -EINVAL; in perf_event_parse_addr_filter()
10936 * ACTION "filter" must have a non-zero length region in perf_event_parse_addr_filter()
10939 if (filter->action == PERF_ADDR_FILTER_ACTION_FILTER && in perf_event_parse_addr_filter()
10940 !filter->size) in perf_event_parse_addr_filter()
10948 * For now, we only support file-based filters in perf_event_parse_addr_filter()
10949 * in per-task events; doing so for CPU-wide in perf_event_parse_addr_filter()
10955 ret = -EOPNOTSUPP; in perf_event_parse_addr_filter()
10956 if (!event->ctx->task) in perf_event_parse_addr_filter()
10961 &filter->path); in perf_event_parse_addr_filter()
10965 ret = -EINVAL; in perf_event_parse_addr_filter()
10966 if (!filter->path.dentry || in perf_event_parse_addr_filter()
10967 !S_ISREG(d_inode(filter->path.dentry) in perf_event_parse_addr_filter()
10968 ->i_mode)) in perf_event_parse_addr_filter()
10971 event->addr_filters.nr_file_filters++; in perf_event_parse_addr_filter()
11009 lockdep_assert_held(&event->ctx->mutex); in perf_event_set_addr_filter()
11011 if (WARN_ON_ONCE(event->parent)) in perf_event_set_addr_filter()
11012 return -EINVAL; in perf_event_set_addr_filter()
11018 ret = event->pmu->addr_filters_validate(&filters); in perf_event_set_addr_filter()
11034 event->addr_filters.nr_file_filters = 0; in perf_event_set_addr_filter()
11041 int ret = -EINVAL; in perf_event_set_filter()
11050 struct perf_event_context *ctx = event->ctx; in perf_event_set_filter()
11055 * the tracepoint muck will deadlock against ctx->mutex, but in perf_event_set_filter()
11057 * temporarily drop ctx->mutex. As per perf_event_ctx_lock() we in perf_event_set_filter()
11063 mutex_unlock(&ctx->mutex); in perf_event_set_filter()
11064 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); in perf_event_set_filter()
11065 mutex_lock(&ctx->mutex); in perf_event_set_filter()
11085 u64 period; in perf_swevent_hrtimer() local
11089 if (event->state != PERF_EVENT_STATE_ACTIVE) in perf_swevent_hrtimer()
11092 event->pmu->read(event); in perf_swevent_hrtimer()
11094 perf_sample_data_init(&data, 0, event->hw.last_period); in perf_swevent_hrtimer()
11098 if (!(event->attr.exclude_idle && is_idle_task(current))) in perf_swevent_hrtimer()
11103 period = max_t(u64, 10000, event->hw.sample_period); in perf_swevent_hrtimer()
11104 hrtimer_forward_now(hrtimer, ns_to_ktime(period)); in perf_swevent_hrtimer()
11111 struct hw_perf_event *hwc = &event->hw; in perf_swevent_start_hrtimer()
11112 s64 period; in perf_swevent_start_hrtimer() local
11117 period = local64_read(&hwc->period_left); in perf_swevent_start_hrtimer()
11118 if (period) { in perf_swevent_start_hrtimer()
11119 if (period < 0) in perf_swevent_start_hrtimer()
11120 period = 10000; in perf_swevent_start_hrtimer()
11122 local64_set(&hwc->period_left, 0); in perf_swevent_start_hrtimer()
11124 period = max_t(u64, 10000, hwc->sample_period); in perf_swevent_start_hrtimer()
11126 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period), in perf_swevent_start_hrtimer()
11132 struct hw_perf_event *hwc = &event->hw; in perf_swevent_cancel_hrtimer()
11135 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); in perf_swevent_cancel_hrtimer()
11136 local64_set(&hwc->period_left, ktime_to_ns(remaining)); in perf_swevent_cancel_hrtimer()
11138 hrtimer_cancel(&hwc->hrtimer); in perf_swevent_cancel_hrtimer()
11144 struct hw_perf_event *hwc = &event->hw; in perf_swevent_init_hrtimer()
11149 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); in perf_swevent_init_hrtimer()
11150 hwc->hrtimer.function = perf_swevent_hrtimer; in perf_swevent_init_hrtimer()
11153 * Since hrtimers have a fixed rate, we can do a static freq->period in perf_swevent_init_hrtimer()
11154 * mapping and avoid the whole period adjust feedback stuff. in perf_swevent_init_hrtimer()
11156 if (event->attr.freq) { in perf_swevent_init_hrtimer()
11157 long freq = event->attr.sample_freq; in perf_swevent_init_hrtimer()
11159 event->attr.sample_period = NSEC_PER_SEC / freq; in perf_swevent_init_hrtimer()
11160 hwc->sample_period = event->attr.sample_period; in perf_swevent_init_hrtimer()
11161 local64_set(&hwc->period_left, hwc->sample_period); in perf_swevent_init_hrtimer()
11162 hwc->last_period = hwc->sample_period; in perf_swevent_init_hrtimer()
11163 event->attr.freq = 0; in perf_swevent_init_hrtimer()
11177 prev = local64_xchg(&event->hw.prev_count, now); in cpu_clock_event_update()
11178 local64_add(now - prev, &event->count); in cpu_clock_event_update()
11183 local64_set(&event->hw.prev_count, local_clock()); in cpu_clock_event_start()
11214 if (event->attr.type != perf_cpu_clock.type) in cpu_clock_event_init()
11215 return -ENOENT; in cpu_clock_event_init()
11217 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) in cpu_clock_event_init()
11218 return -ENOENT; in cpu_clock_event_init()
11224 return -EOPNOTSUPP; in cpu_clock_event_init()
11254 prev = local64_xchg(&event->hw.prev_count, now); in task_clock_event_update()
11255 delta = now - prev; in task_clock_event_update()
11256 local64_add(delta, &event->count); in task_clock_event_update()
11261 local64_set(&event->hw.prev_count, event->ctx->time); in task_clock_event_start()
11268 task_clock_event_update(event, event->ctx->time); in task_clock_event_stop()
11288 u64 delta = now - event->ctx->timestamp; in task_clock_event_read()
11289 u64 time = event->ctx->time + delta; in task_clock_event_read()
11296 if (event->attr.type != perf_task_clock.type) in task_clock_event_init()
11297 return -ENOENT; in task_clock_event_init()
11299 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) in task_clock_event_init()
11300 return -ENOENT; in task_clock_event_init()
11306 return -EOPNOTSUPP; in task_clock_event_init()
11389 free_percpu(pmu->cpu_pmu_context); in free_pmu_context()
11401 return scnprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters); in nr_addr_filters_show()
11412 return scnprintf(page, PAGE_SIZE - 1, "%d\n", pmu->type); in type_show()
11423 return scnprintf(page, PAGE_SIZE - 1, "%d\n", pmu->hrtimer_interval_ms); in perf_event_mux_interval_ms_show()
11441 return -EINVAL; in perf_event_mux_interval_ms_store()
11444 if (timer == pmu->hrtimer_interval_ms) in perf_event_mux_interval_ms_store()
11448 pmu->hrtimer_interval_ms = timer; in perf_event_mux_interval_ms_store()
11454 cpc = per_cpu_ptr(pmu->cpu_pmu_context, cpu); in perf_event_mux_interval_ms_store()
11455 cpc->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); in perf_event_mux_interval_ms_store()
11478 if (n == 2 && !pmu->nr_addr_filters) in pmu_dev_is_visible()
11481 return a->mode; in pmu_dev_is_visible()
11507 int ret = -ENOMEM; in pmu_dev_alloc()
11509 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); in pmu_dev_alloc()
11510 if (!pmu->dev) in pmu_dev_alloc()
11513 pmu->dev->groups = pmu->attr_groups; in pmu_dev_alloc()
11514 device_initialize(pmu->dev); in pmu_dev_alloc()
11516 dev_set_drvdata(pmu->dev, pmu); in pmu_dev_alloc()
11517 pmu->dev->bus = &pmu_bus; in pmu_dev_alloc()
11518 pmu->dev->parent = pmu->parent; in pmu_dev_alloc()
11519 pmu->dev->release = pmu_dev_release; in pmu_dev_alloc()
11521 ret = dev_set_name(pmu->dev, "%s", pmu->name); in pmu_dev_alloc()
11525 ret = device_add(pmu->dev); in pmu_dev_alloc()
11529 if (pmu->attr_update) { in pmu_dev_alloc()
11530 ret = sysfs_update_groups(&pmu->dev->kobj, pmu->attr_update); in pmu_dev_alloc()
11539 device_del(pmu->dev); in pmu_dev_alloc()
11542 put_device(pmu->dev); in pmu_dev_alloc()
11554 ret = -ENOMEM; in perf_pmu_register()
11555 pmu->pmu_disable_count = alloc_percpu(int); in perf_pmu_register()
11556 if (!pmu->pmu_disable_count) in perf_pmu_register()
11559 pmu->type = -1; in perf_pmu_register()
11561 ret = -EINVAL; in perf_pmu_register()
11565 pmu->name = name; in perf_pmu_register()
11577 pmu->type = type; in perf_pmu_register()
11579 if (pmu_bus_running && !pmu->dev) { in perf_pmu_register()
11585 ret = -ENOMEM; in perf_pmu_register()
11586 pmu->cpu_pmu_context = alloc_percpu(struct perf_cpu_pmu_context); in perf_pmu_register()
11587 if (!pmu->cpu_pmu_context) in perf_pmu_register()
11593 cpc = per_cpu_ptr(pmu->cpu_pmu_context, cpu); in perf_pmu_register()
11594 __perf_init_event_pmu_context(&cpc->epc, pmu); in perf_pmu_register()
11598 if (!pmu->start_txn) { in perf_pmu_register()
11599 if (pmu->pmu_enable) { in perf_pmu_register()
11605 pmu->start_txn = perf_pmu_start_txn; in perf_pmu_register()
11606 pmu->commit_txn = perf_pmu_commit_txn; in perf_pmu_register()
11607 pmu->cancel_txn = perf_pmu_cancel_txn; in perf_pmu_register()
11609 pmu->start_txn = perf_pmu_nop_txn; in perf_pmu_register()
11610 pmu->commit_txn = perf_pmu_nop_int; in perf_pmu_register()
11611 pmu->cancel_txn = perf_pmu_nop_void; in perf_pmu_register()
11615 if (!pmu->pmu_enable) { in perf_pmu_register()
11616 pmu->pmu_enable = perf_pmu_nop_void; in perf_pmu_register()
11617 pmu->pmu_disable = perf_pmu_nop_void; in perf_pmu_register()
11620 if (!pmu->check_period) in perf_pmu_register()
11621 pmu->check_period = perf_event_nop_int; in perf_pmu_register()
11623 if (!pmu->event_idx) in perf_pmu_register()
11624 pmu->event_idx = perf_event_idx_default; in perf_pmu_register()
11626 list_add_rcu(&pmu->entry, &pmus); in perf_pmu_register()
11627 atomic_set(&pmu->exclusive_cnt, 0); in perf_pmu_register()
11635 if (pmu->dev && pmu->dev != PMU_NULL_DEV) { in perf_pmu_register()
11636 device_del(pmu->dev); in perf_pmu_register()
11637 put_device(pmu->dev); in perf_pmu_register()
11641 idr_remove(&pmu_idr, pmu->type); in perf_pmu_register()
11644 free_percpu(pmu->pmu_disable_count); in perf_pmu_register()
11652 list_del_rcu(&pmu->entry); in perf_pmu_unregister()
11661 free_percpu(pmu->pmu_disable_count); in perf_pmu_unregister()
11662 idr_remove(&pmu_idr, pmu->type); in perf_pmu_unregister()
11663 if (pmu_bus_running && pmu->dev && pmu->dev != PMU_NULL_DEV) { in perf_pmu_unregister()
11664 if (pmu->nr_addr_filters) in perf_pmu_unregister()
11665 device_remove_file(pmu->dev, &dev_attr_nr_addr_filters); in perf_pmu_unregister()
11666 device_del(pmu->dev); in perf_pmu_unregister()
11667 put_device(pmu->dev); in perf_pmu_unregister()
11676 return (event->attr.sample_regs_user & PERF_REG_EXTENDED_MASK) || in has_extended_regs()
11677 (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK); in has_extended_regs()
11685 if (!try_module_get(pmu->module)) in perf_try_init_event()
11686 return -ENODEV; in perf_try_init_event()
11689 * A number of pmu->event_init() methods iterate the sibling_list to, in perf_try_init_event()
11691 * if this is a sibling event, acquire the ctx->mutex to protect in perf_try_init_event()
11694 if (event->group_leader != event && pmu->task_ctx_nr != perf_sw_context) { in perf_try_init_event()
11696 * This ctx->mutex can nest when we're called through in perf_try_init_event()
11699 ctx = perf_event_ctx_lock_nested(event->group_leader, in perf_try_init_event()
11704 event->pmu = pmu; in perf_try_init_event()
11705 ret = pmu->event_init(event); in perf_try_init_event()
11708 perf_event_ctx_unlock(event->group_leader, ctx); in perf_try_init_event()
11711 if (!(pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS) && in perf_try_init_event()
11713 ret = -EOPNOTSUPP; in perf_try_init_event()
11715 if (pmu->capabilities & PERF_PMU_CAP_NO_EXCLUDE && in perf_try_init_event()
11717 ret = -EINVAL; in perf_try_init_event()
11719 if (ret && event->destroy) in perf_try_init_event()
11720 event->destroy(event); in perf_try_init_event()
11724 module_put(pmu->module); in perf_try_init_event()
11738 * Save original type before calling pmu->event_init() since certain in perf_init_event()
11739 * pmus overwrites event->attr.type to forward event to another pmu. in perf_init_event()
11741 event->orig_type = event->attr.type; in perf_init_event()
11744 if (event->parent && event->parent->pmu) { in perf_init_event()
11745 pmu = event->parent->pmu; in perf_init_event()
11755 type = event->attr.type; in perf_init_event()
11757 type = event->attr.config >> PERF_PMU_TYPE_SHIFT; in perf_init_event()
11762 event->attr.config &= PERF_HW_EVENT_MASK; in perf_init_event()
11771 if (event->attr.type != type && type != PERF_TYPE_RAW && in perf_init_event()
11772 !(pmu->capabilities & PERF_PMU_CAP_EXTENDED_HW_TYPE)) in perf_init_event()
11776 if (ret == -ENOENT && event->attr.type != type && !extended_type) { in perf_init_event()
11777 type = event->attr.type; in perf_init_event()
11792 if (ret != -ENOENT) { in perf_init_event()
11798 pmu = ERR_PTR(-ENOENT); in perf_init_event()
11807 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); in attach_sb_event()
11809 raw_spin_lock(&pel->lock); in attach_sb_event()
11810 list_add_rcu(&event->sb_list, &pel->list); in attach_sb_event()
11811 raw_spin_unlock(&pel->lock); in attach_sb_event()
11815 * We keep a list of all !task (and therefore per-cpu) events
11816 * that need to receive side-band records.
11818 * This avoids having to scan all the various PMU per-cpu contexts
11852 if (event->parent) in account_event()
11855 if (event->attach_state & (PERF_ATTACH_TASK | PERF_ATTACH_SCHED_CB)) in account_event()
11857 if (event->attr.mmap || event->attr.mmap_data) in account_event()
11859 if (event->attr.build_id) in account_event()
11861 if (event->attr.comm) in account_event()
11863 if (event->attr.namespaces) in account_event()
11865 if (event->attr.cgroup) in account_event()
11867 if (event->attr.task) in account_event()
11869 if (event->attr.freq) in account_event()
11871 if (event->attr.context_switch) { in account_event()
11879 if (event->attr.ksymbol) in account_event()
11881 if (event->attr.bpf_event) in account_event()
11883 if (event->attr.text_poke) in account_event()
11907 * increments to by-pass the mutex. in account_event()
11931 long err = -EINVAL; in perf_event_alloc()
11935 if (!task || cpu != -1) in perf_event_alloc()
11936 return ERR_PTR(-EINVAL); in perf_event_alloc()
11938 if (attr->sigtrap && !task) { in perf_event_alloc()
11940 return ERR_PTR(-EINVAL); in perf_event_alloc()
11943 node = (cpu >= 0) ? cpu_to_node(cpu) : -1; in perf_event_alloc()
11947 return ERR_PTR(-ENOMEM); in perf_event_alloc()
11956 mutex_init(&event->child_mutex); in perf_event_alloc()
11957 INIT_LIST_HEAD(&event->child_list); in perf_event_alloc()
11959 INIT_LIST_HEAD(&event->event_entry); in perf_event_alloc()
11960 INIT_LIST_HEAD(&event->sibling_list); in perf_event_alloc()
11961 INIT_LIST_HEAD(&event->active_list); in perf_event_alloc()
11963 INIT_LIST_HEAD(&event->rb_entry); in perf_event_alloc()
11964 INIT_LIST_HEAD(&event->active_entry); in perf_event_alloc()
11965 INIT_LIST_HEAD(&event->addr_filters.list); in perf_event_alloc()
11966 INIT_HLIST_NODE(&event->hlist_entry); in perf_event_alloc()
11969 init_waitqueue_head(&event->waitq); in perf_event_alloc()
11970 init_irq_work(&event->pending_irq, perf_pending_irq); in perf_event_alloc()
11971 init_task_work(&event->pending_task, perf_pending_task); in perf_event_alloc()
11972 rcuwait_init(&event->pending_work_wait); in perf_event_alloc()
11974 mutex_init(&event->mmap_mutex); in perf_event_alloc()
11975 raw_spin_lock_init(&event->addr_filters.lock); in perf_event_alloc()
11977 atomic_long_set(&event->refcount, 1); in perf_event_alloc()
11978 event->cpu = cpu; in perf_event_alloc()
11979 event->attr = *attr; in perf_event_alloc()
11980 event->group_leader = group_leader; in perf_event_alloc()
11981 event->pmu = NULL; in perf_event_alloc()
11982 event->oncpu = -1; in perf_event_alloc()
11984 event->parent = parent_event; in perf_event_alloc()
11986 event->ns = get_pid_ns(task_active_pid_ns(current)); in perf_event_alloc()
11987 event->id = atomic64_inc_return(&perf_event_id); in perf_event_alloc()
11989 event->state = PERF_EVENT_STATE_INACTIVE; in perf_event_alloc()
11992 event->event_caps = parent_event->event_caps; in perf_event_alloc()
11995 event->attach_state = PERF_ATTACH_TASK; in perf_event_alloc()
12001 event->hw.target = get_task_struct(task); in perf_event_alloc()
12004 event->clock = &local_clock; in perf_event_alloc()
12006 event->clock = parent_event->clock; in perf_event_alloc()
12009 overflow_handler = parent_event->overflow_handler; in perf_event_alloc()
12010 context = parent_event->overflow_handler_context; in perf_event_alloc()
12013 struct bpf_prog *prog = parent_event->prog; in perf_event_alloc()
12016 event->prog = prog; in perf_event_alloc()
12017 event->orig_overflow_handler = in perf_event_alloc()
12018 parent_event->orig_overflow_handler; in perf_event_alloc()
12024 event->overflow_handler = overflow_handler; in perf_event_alloc()
12025 event->overflow_handler_context = context; in perf_event_alloc()
12027 event->overflow_handler = perf_event_output_backward; in perf_event_alloc()
12028 event->overflow_handler_context = NULL; in perf_event_alloc()
12030 event->overflow_handler = perf_event_output_forward; in perf_event_alloc()
12031 event->overflow_handler_context = NULL; in perf_event_alloc()
12038 hwc = &event->hw; in perf_event_alloc()
12039 hwc->sample_period = attr->sample_period; in perf_event_alloc()
12040 if (attr->freq && attr->sample_freq) in perf_event_alloc()
12041 hwc->sample_period = 1; in perf_event_alloc()
12042 hwc->last_period = hwc->sample_period; in perf_event_alloc()
12044 local64_set(&hwc->period_left, hwc->sample_period); in perf_event_alloc()
12050 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ)) in perf_event_alloc()
12054 event->attr.branch_sample_type = 0; in perf_event_alloc()
12063 * Disallow uncore-task events. Similarly, disallow uncore-cgroup in perf_event_alloc()
12067 if (pmu->task_ctx_nr == perf_invalid_context && (task || cgroup_fd != -1)) { in perf_event_alloc()
12068 err = -EINVAL; in perf_event_alloc()
12072 if (event->attr.aux_output && in perf_event_alloc()
12073 !(pmu->capabilities & PERF_PMU_CAP_AUX_OUTPUT)) { in perf_event_alloc()
12074 err = -EOPNOTSUPP; in perf_event_alloc()
12078 if (cgroup_fd != -1) { in perf_event_alloc()
12089 event->addr_filter_ranges = kcalloc(pmu->nr_addr_filters, in perf_event_alloc()
12092 if (!event->addr_filter_ranges) { in perf_event_alloc()
12093 err = -ENOMEM; in perf_event_alloc()
12101 if (event->parent) { in perf_event_alloc()
12104 raw_spin_lock_irq(&ifh->lock); in perf_event_alloc()
12105 memcpy(event->addr_filter_ranges, in perf_event_alloc()
12106 event->parent->addr_filter_ranges, in perf_event_alloc()
12107 pmu->nr_addr_filters * sizeof(struct perf_addr_filter_range)); in perf_event_alloc()
12108 raw_spin_unlock_irq(&ifh->lock); in perf_event_alloc()
12112 event->addr_filters_gen = 1; in perf_event_alloc()
12115 if (!event->parent) { in perf_event_alloc()
12116 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { in perf_event_alloc()
12117 err = get_callchain_buffers(attr->sample_max_stack); in perf_event_alloc()
12133 if (!event->parent) { in perf_event_alloc()
12134 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) in perf_event_alloc()
12138 kfree(event->addr_filter_ranges); in perf_event_alloc()
12146 if (event->destroy) in perf_event_alloc()
12147 event->destroy(event); in perf_event_alloc()
12148 module_put(pmu->module); in perf_event_alloc()
12150 if (event->hw.target) in perf_event_alloc()
12151 put_task_struct(event->hw.target); in perf_event_alloc()
12152 call_rcu(&event->rcu_head, free_event_rcu); in perf_event_alloc()
12166 ret = get_user(size, &uattr->size); in perf_copy_attr()
12178 if (ret == -E2BIG) in perf_copy_attr()
12183 attr->size = size; in perf_copy_attr()
12185 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3) in perf_copy_attr()
12186 return -EINVAL; in perf_copy_attr()
12188 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) in perf_copy_attr()
12189 return -EINVAL; in perf_copy_attr()
12191 if (attr->read_format & ~(PERF_FORMAT_MAX-1)) in perf_copy_attr()
12192 return -EINVAL; in perf_copy_attr()
12194 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { in perf_copy_attr()
12195 u64 mask = attr->branch_sample_type; in perf_copy_attr()
12198 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) in perf_copy_attr()
12199 return -EINVAL; in perf_copy_attr()
12203 return -EINVAL; in perf_copy_attr()
12209 if (!attr->exclude_kernel) in perf_copy_attr()
12212 if (!attr->exclude_user) in perf_copy_attr()
12215 if (!attr->exclude_hv) in perf_copy_attr()
12220 attr->branch_sample_type = mask; in perf_copy_attr()
12230 if (attr->sample_type & PERF_SAMPLE_REGS_USER) { in perf_copy_attr()
12231 ret = perf_reg_validate(attr->sample_regs_user); in perf_copy_attr()
12236 if (attr->sample_type & PERF_SAMPLE_STACK_USER) { in perf_copy_attr()
12238 return -ENOSYS; in perf_copy_attr()
12245 if (attr->sample_stack_user >= USHRT_MAX) in perf_copy_attr()
12246 return -EINVAL; in perf_copy_attr()
12247 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) in perf_copy_attr()
12248 return -EINVAL; in perf_copy_attr()
12251 if (!attr->sample_max_stack) in perf_copy_attr()
12252 attr->sample_max_stack = sysctl_perf_event_max_stack; in perf_copy_attr()
12254 if (attr->sample_type & PERF_SAMPLE_REGS_INTR) in perf_copy_attr()
12255 ret = perf_reg_validate(attr->sample_regs_intr); in perf_copy_attr()
12258 if (attr->sample_type & PERF_SAMPLE_CGROUP) in perf_copy_attr()
12259 return -EINVAL; in perf_copy_attr()
12261 if ((attr->sample_type & PERF_SAMPLE_WEIGHT) && in perf_copy_attr()
12262 (attr->sample_type & PERF_SAMPLE_WEIGHT_STRUCT)) in perf_copy_attr()
12263 return -EINVAL; in perf_copy_attr()
12265 if (!attr->inherit && attr->inherit_thread) in perf_copy_attr()
12266 return -EINVAL; in perf_copy_attr()
12268 if (attr->remove_on_exec && attr->enable_on_exec) in perf_copy_attr()
12269 return -EINVAL; in perf_copy_attr()
12271 if (attr->sigtrap && !attr->remove_on_exec) in perf_copy_attr()
12272 return -EINVAL; in perf_copy_attr()
12278 put_user(sizeof(*attr), &uattr->size); in perf_copy_attr()
12279 ret = -E2BIG; in perf_copy_attr()
12296 int ret = -EINVAL; in perf_event_set_output()
12299 mutex_lock(&event->mmap_mutex); in perf_event_set_output()
12308 * Don't allow cross-cpu buffers in perf_event_set_output()
12310 if (output_event->cpu != event->cpu) in perf_event_set_output()
12314 * If its not a per-cpu rb, it must be the same task. in perf_event_set_output()
12316 if (output_event->cpu == -1 && output_event->hw.target != event->hw.target) in perf_event_set_output()
12322 if (output_event->clock != event->clock) in perf_event_set_output()
12336 event->pmu != output_event->pmu) in perf_event_set_output()
12341 * output_event is already on rb->event_list, and the list iteration in perf_event_set_output()
12344 * observe !rb->mmap_count. in perf_event_set_output()
12346 mutex_lock_double(&event->mmap_mutex, &output_event->mmap_mutex); in perf_event_set_output()
12349 if (atomic_read(&event->mmap_count)) in perf_event_set_output()
12359 if (!atomic_read(&rb->mmap_count)) { in perf_event_set_output()
12369 mutex_unlock(&event->mmap_mutex); in perf_event_set_output()
12371 mutex_unlock(&output_event->mmap_mutex); in perf_event_set_output()
12383 event->clock = &ktime_get_mono_fast_ns; in perf_event_set_clock()
12388 event->clock = &ktime_get_raw_fast_ns; in perf_event_set_clock()
12393 event->clock = &ktime_get_real_ns; in perf_event_set_clock()
12397 event->clock = &ktime_get_boottime_ns; in perf_event_set_clock()
12401 event->clock = &ktime_get_clocktai_ns; in perf_event_set_clock()
12405 return -EINVAL; in perf_event_set_clock()
12408 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI)) in perf_event_set_clock()
12409 return -EINVAL; in perf_event_set_clock()
12420 if (attr->sigtrap) { in perf_check_permission()
12426 is_capable &= ns_capable(__task_cred(task)->user_ns, CAP_KILL); in perf_check_permission()
12446 * sys_perf_event_open - open a performance event, associate it to a task/cpu
12471 int cgroup_fd = -1; in SYSCALL_DEFINE5()
12475 return -EINVAL; in SYSCALL_DEFINE5()
12494 return -EACCES; in SYSCALL_DEFINE5()
12499 return -EINVAL; in SYSCALL_DEFINE5()
12502 return -EINVAL; in SYSCALL_DEFINE5()
12525 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) in SYSCALL_DEFINE5()
12526 return -EINVAL; in SYSCALL_DEFINE5()
12535 if (group_fd != -1) { in SYSCALL_DEFINE5()
12539 group_leader = group.file->private_data; in SYSCALL_DEFINE5()
12546 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { in SYSCALL_DEFINE5()
12555 group_leader->attr.inherit != attr.inherit) { in SYSCALL_DEFINE5()
12556 err = -EINVAL; in SYSCALL_DEFINE5()
12571 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { in SYSCALL_DEFINE5()
12572 err = -EOPNOTSUPP; in SYSCALL_DEFINE5()
12581 pmu = event->pmu; in SYSCALL_DEFINE5()
12589 if (pmu->task_ctx_nr == perf_sw_context) in SYSCALL_DEFINE5()
12590 event->event_caps |= PERF_EV_CAP_SOFTWARE; in SYSCALL_DEFINE5()
12593 err = down_read_interruptible(&task->signal->exec_update_lock); in SYSCALL_DEFINE5()
12603 err = -EACCES; in SYSCALL_DEFINE5()
12617 mutex_lock(&ctx->mutex); in SYSCALL_DEFINE5()
12619 if (ctx->task == TASK_TOMBSTONE) { in SYSCALL_DEFINE5()
12620 err = -ESRCH; in SYSCALL_DEFINE5()
12631 struct perf_cpu_context *cpuctx = per_cpu_ptr(&perf_cpu_context, event->cpu); in SYSCALL_DEFINE5()
12633 if (!cpuctx->online) { in SYSCALL_DEFINE5()
12634 err = -ENODEV; in SYSCALL_DEFINE5()
12640 err = -EINVAL; in SYSCALL_DEFINE5()
12644 * becoming part of another group-sibling): in SYSCALL_DEFINE5()
12646 if (group_leader->group_leader != group_leader) in SYSCALL_DEFINE5()
12650 if (group_leader->clock != event->clock) in SYSCALL_DEFINE5()
12658 if (group_leader->cpu != event->cpu) in SYSCALL_DEFINE5()
12664 if (group_leader->ctx != ctx) in SYSCALL_DEFINE5()
12686 pmu = group_leader->pmu_ctx->pmu; in SYSCALL_DEFINE5()
12689 (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) { in SYSCALL_DEFINE5()
12700 group_leader->pmu_ctx->pmu != pmu) in SYSCALL_DEFINE5()
12713 event->pmu_ctx = pmu_ctx; in SYSCALL_DEFINE5()
12722 err = -E2BIG; in SYSCALL_DEFINE5()
12727 err = -EINVAL; in SYSCALL_DEFINE5()
12736 err = -EBUSY; in SYSCALL_DEFINE5()
12740 WARN_ON_ONCE(ctx->parent_ctx); in SYSCALL_DEFINE5()
12756 put_pmu_ctx(group_leader->pmu_ctx); in SYSCALL_DEFINE5()
12760 put_pmu_ctx(sibling->pmu_ctx); in SYSCALL_DEFINE5()
12767 * (through the sibling list, which is still in-tact), we can in SYSCALL_DEFINE5()
12770 * By installing siblings first we NO-OP because they're not in SYSCALL_DEFINE5()
12774 sibling->pmu_ctx = pmu_ctx; in SYSCALL_DEFINE5()
12777 perf_install_in_context(ctx, sibling, sibling->cpu); in SYSCALL_DEFINE5()
12785 group_leader->pmu_ctx = pmu_ctx; in SYSCALL_DEFINE5()
12788 perf_install_in_context(ctx, group_leader, group_leader->cpu); in SYSCALL_DEFINE5()
12800 event->owner = current; in SYSCALL_DEFINE5()
12802 perf_install_in_context(ctx, event, event->cpu); in SYSCALL_DEFINE5()
12805 mutex_unlock(&ctx->mutex); in SYSCALL_DEFINE5()
12808 up_read(&task->signal->exec_update_lock); in SYSCALL_DEFINE5()
12812 mutex_lock(&current->perf_event_mutex); in SYSCALL_DEFINE5()
12813 list_add_tail(&event->owner_entry, &current->perf_event_list); in SYSCALL_DEFINE5()
12814 mutex_unlock(&current->perf_event_mutex); in SYSCALL_DEFINE5()
12827 put_pmu_ctx(event->pmu_ctx); in SYSCALL_DEFINE5()
12828 event->pmu_ctx = NULL; /* _free_event() */ in SYSCALL_DEFINE5()
12830 mutex_unlock(&ctx->mutex); in SYSCALL_DEFINE5()
12835 up_read(&task->signal->exec_update_lock); in SYSCALL_DEFINE5()
12873 if (attr->aux_output) in perf_event_create_kernel_counter()
12874 return ERR_PTR(-EINVAL); in perf_event_create_kernel_counter()
12877 overflow_handler, context, -1); in perf_event_create_kernel_counter()
12884 event->owner = TASK_TOMBSTONE; in perf_event_create_kernel_counter()
12885 pmu = event->pmu; in perf_event_create_kernel_counter()
12887 if (pmu->task_ctx_nr == perf_sw_context) in perf_event_create_kernel_counter()
12888 event->event_caps |= PERF_EV_CAP_SOFTWARE; in perf_event_create_kernel_counter()
12899 WARN_ON_ONCE(ctx->parent_ctx); in perf_event_create_kernel_counter()
12900 mutex_lock(&ctx->mutex); in perf_event_create_kernel_counter()
12901 if (ctx->task == TASK_TOMBSTONE) { in perf_event_create_kernel_counter()
12902 err = -ESRCH; in perf_event_create_kernel_counter()
12911 event->pmu_ctx = pmu_ctx; in perf_event_create_kernel_counter()
12922 if (!cpuctx->online) { in perf_event_create_kernel_counter()
12923 err = -ENODEV; in perf_event_create_kernel_counter()
12929 err = -EBUSY; in perf_event_create_kernel_counter()
12933 perf_install_in_context(ctx, event, event->cpu); in perf_event_create_kernel_counter()
12935 mutex_unlock(&ctx->mutex); in perf_event_create_kernel_counter()
12941 event->pmu_ctx = NULL; /* _free_event() */ in perf_event_create_kernel_counter()
12943 mutex_unlock(&ctx->mutex); in perf_event_create_kernel_counter()
12962 put_pmu_ctx(event->pmu_ctx); in __perf_pmu_remove()
12963 list_add(&event->migrate_entry, events); in __perf_pmu_remove()
12967 put_pmu_ctx(sibling->pmu_ctx); in __perf_pmu_remove()
12968 list_add(&sibling->migrate_entry, events); in __perf_pmu_remove()
12978 struct perf_event_context *old_ctx = event->ctx; in __perf_pmu_install_event()
12982 event->cpu = cpu; in __perf_pmu_install_event()
12984 event->pmu_ctx = epc; in __perf_pmu_install_event()
12986 if (event->state >= PERF_EVENT_STATE_OFF) in __perf_pmu_install_event()
12987 event->state = PERF_EVENT_STATE_INACTIVE; in __perf_pmu_install_event()
12991 * Now that event->ctx is updated and visible, put the old ctx. in __perf_pmu_install_event()
13002 * Re-instate events in 2 passes. in __perf_pmu_install()
13010 if (event->group_leader == event) in __perf_pmu_install()
13013 list_del(&event->migrate_entry); in __perf_pmu_install()
13022 list_del(&event->migrate_entry); in __perf_pmu_install()
13033 * Since per-cpu context is persistent, no need to grab an extra in perf_pmu_migrate_context()
13036 src_ctx = &per_cpu_ptr(&perf_cpu_context, src_cpu)->ctx; in perf_pmu_migrate_context()
13037 dst_ctx = &per_cpu_ptr(&perf_cpu_context, dst_cpu)->ctx; in perf_pmu_migrate_context()
13043 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); in perf_pmu_migrate_context()
13045 __perf_pmu_remove(src_ctx, src_cpu, pmu, &src_ctx->pinned_groups, &events); in perf_pmu_migrate_context()
13046 __perf_pmu_remove(src_ctx, src_cpu, pmu, &src_ctx->flexible_groups, &events); in perf_pmu_migrate_context()
13050 * Wait for the events to quiesce before re-instating them. in perf_pmu_migrate_context()
13057 mutex_unlock(&dst_ctx->mutex); in perf_pmu_migrate_context()
13058 mutex_unlock(&src_ctx->mutex); in perf_pmu_migrate_context()
13064 struct perf_event *parent_event = child_event->parent; in sync_child_event()
13067 if (child_event->attr.inherit_stat) { in sync_child_event()
13068 struct task_struct *task = child_event->ctx->task; in sync_child_event()
13079 atomic64_add(child_val, &parent_event->child_count); in sync_child_event()
13080 atomic64_add(child_event->total_time_enabled, in sync_child_event()
13081 &parent_event->child_total_time_enabled); in sync_child_event()
13082 atomic64_add(child_event->total_time_running, in sync_child_event()
13083 &parent_event->child_total_time_running); in sync_child_event()
13089 struct perf_event *parent_event = event->parent; in perf_event_exit_event()
13106 mutex_lock(&parent_event->child_mutex); in perf_event_exit_event()
13111 raw_spin_lock_irq(&ctx->lock); in perf_event_exit_event()
13112 if (event->state > PERF_EVENT_STATE_EXIT) in perf_event_exit_event()
13114 raw_spin_unlock_irq(&ctx->lock); in perf_event_exit_event()
13120 mutex_unlock(&parent_event->child_mutex); in perf_event_exit_event()
13148 * In order to reduce the amount of tricky in ctx tear-down, we hold in perf_event_exit_task_context()
13157 mutex_lock(&child_ctx->mutex); in perf_event_exit_task_context()
13160 * In a single ctx::lock section, de-schedule the events and detach the in perf_event_exit_task_context()
13164 raw_spin_lock_irq(&child_ctx->lock); in perf_event_exit_task_context()
13168 * Now that the context is inactive, destroy the task <-> ctx relation in perf_event_exit_task_context()
13171 RCU_INIT_POINTER(child->perf_event_ctxp, NULL); in perf_event_exit_task_context()
13173 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE); in perf_event_exit_task_context()
13177 raw_spin_unlock_irq(&child_ctx->lock); in perf_event_exit_task_context()
13189 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry) in perf_event_exit_task_context()
13192 mutex_unlock(&child_ctx->mutex); in perf_event_exit_task_context()
13207 mutex_lock(&child->perf_event_mutex); in perf_event_exit_task()
13208 list_for_each_entry_safe(event, tmp, &child->perf_event_list, in perf_event_exit_task()
13210 list_del_init(&event->owner_entry); in perf_event_exit_task()
13215 * we need to serialize on the owner->perf_event_mutex. in perf_event_exit_task()
13217 smp_store_release(&event->owner, NULL); in perf_event_exit_task()
13219 mutex_unlock(&child->perf_event_mutex); in perf_event_exit_task()
13226 * child contexts and sets child->perf_event_ctxp[] to NULL. in perf_event_exit_task()
13235 struct perf_event *parent = event->parent; in perf_free_event()
13240 mutex_lock(&parent->child_mutex); in perf_free_event()
13241 list_del_init(&event->child_list); in perf_free_event()
13242 mutex_unlock(&parent->child_mutex); in perf_free_event()
13246 raw_spin_lock_irq(&ctx->lock); in perf_free_event()
13249 raw_spin_unlock_irq(&ctx->lock); in perf_free_event()
13265 ctx = rcu_access_pointer(task->perf_event_ctxp); in perf_event_free_task()
13269 mutex_lock(&ctx->mutex); in perf_event_free_task()
13270 raw_spin_lock_irq(&ctx->lock); in perf_event_free_task()
13272 * Destroy the task <-> ctx relation and mark the context dead. in perf_event_free_task()
13277 RCU_INIT_POINTER(task->perf_event_ctxp, NULL); in perf_event_free_task()
13278 WRITE_ONCE(ctx->task, TASK_TOMBSTONE); in perf_event_free_task()
13280 raw_spin_unlock_irq(&ctx->lock); in perf_event_free_task()
13283 list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) in perf_event_free_task()
13286 mutex_unlock(&ctx->mutex); in perf_event_free_task()
13297 * _free_event()'s put_task_struct(event->hw.target) will be a in perf_event_free_task()
13298 * use-after-free. in perf_event_free_task()
13302 wait_var_event(&ctx->refcount, refcount_read(&ctx->refcount) == 1); in perf_event_free_task()
13308 WARN_ON_ONCE(task->perf_event_ctxp); in perf_event_delayed_put()
13315 return ERR_PTR(-EBADF); in perf_event_get()
13317 if (file->f_op != &perf_fops) { in perf_event_get()
13319 return ERR_PTR(-EBADF); in perf_event_get()
13327 if (file->f_op != &perf_fops) in perf_get_event()
13328 return ERR_PTR(-EINVAL); in perf_get_event()
13330 return file->private_data; in perf_get_event()
13336 return ERR_PTR(-EINVAL); in perf_event_attrs()
13338 return &event->attr; in perf_event_attrs()
13344 return -EACCES; in perf_allow_kernel()
13354 * - valid pointer on success
13355 * - NULL for orphaned events
13356 * - IS_ERR() on error
13366 enum perf_event_state parent_state = parent_event->state; in inherit_event()
13377 if (parent_event->parent) in inherit_event()
13378 parent_event = parent_event->parent; in inherit_event()
13380 child_event = perf_event_alloc(&parent_event->attr, in inherit_event()
13381 parent_event->cpu, in inherit_event()
13384 NULL, NULL, -1); in inherit_event()
13388 pmu_ctx = find_get_pmu_context(child_event->pmu, child_ctx, child_event); in inherit_event()
13393 child_event->pmu_ctx = pmu_ctx; in inherit_event()
13396 * is_orphaned_event() and list_add_tail(&parent_event->child_list) in inherit_event()
13399 * is_orphaned_event() or they will observe us on the child_list. in inherit_event()
13401 mutex_lock(&parent_event->child_mutex); in inherit_event()
13403 !atomic_long_inc_not_zero(&parent_event->refcount)) { in inherit_event()
13404 mutex_unlock(&parent_event->child_mutex); in inherit_event()
13418 child_event->state = PERF_EVENT_STATE_INACTIVE; in inherit_event()
13420 child_event->state = PERF_EVENT_STATE_OFF; in inherit_event()
13422 if (parent_event->attr.freq) { in inherit_event()
13423 u64 sample_period = parent_event->hw.sample_period; in inherit_event()
13424 struct hw_perf_event *hwc = &child_event->hw; in inherit_event()
13426 hwc->sample_period = sample_period; in inherit_event()
13427 hwc->last_period = sample_period; in inherit_event()
13429 local64_set(&hwc->period_left, sample_period); in inherit_event()
13432 child_event->ctx = child_ctx; in inherit_event()
13433 child_event->overflow_handler = parent_event->overflow_handler; in inherit_event()
13434 child_event->overflow_handler_context in inherit_event()
13435 = parent_event->overflow_handler_context; in inherit_event()
13446 raw_spin_lock_irqsave(&child_ctx->lock, flags); in inherit_event()
13448 child_event->attach_state |= PERF_ATTACH_CHILD; in inherit_event()
13449 raw_spin_unlock_irqrestore(&child_ctx->lock, flags); in inherit_event()
13454 list_add_tail(&child_event->child_list, &parent_event->child_list); in inherit_event()
13455 mutex_unlock(&parent_event->child_mutex); in inherit_event()
13467 * - 0 on success
13468 * - <0 on error
13495 if (sub->aux_event == parent_event && child_ctr && in inherit_group()
13497 return -EINVAL; in inherit_group()
13500 leader->group_generation = parent_event->group_generation; in inherit_group()
13505 * Creates the child task context and tries to inherit the event-group.
13512 * - 0 on success
13513 * - <0 on error
13524 if (!event->attr.inherit || in inherit_task_group()
13525 (event->attr.inherit_thread && !(clone_flags & CLONE_THREAD)) || in inherit_task_group()
13527 (event->attr.sigtrap && (clone_flags & CLONE_CLEAR_SIGHAND))) { in inherit_task_group()
13532 child_ctx = child->perf_event_ctxp; in inherit_task_group()
13542 return -ENOMEM; in inherit_task_group()
13544 child->perf_event_ctxp = child_ctx; in inherit_task_group()
13567 if (likely(!parent->perf_event_ctxp)) in perf_event_init_context()
13572 * swapped under us. in perf_event_init_context()
13580 * it non-NULL earlier, the only reason for it to become NULL in perf_event_init_context()
13586 * Lock the parent list. No need to lock the child - not PID in perf_event_init_context()
13589 mutex_lock(&parent_ctx->mutex); in perf_event_init_context()
13592 * We dont have to disable NMIs - we are only looking at in perf_event_init_context()
13595 perf_event_groups_for_each(event, &parent_ctx->pinned_groups) { in perf_event_init_context()
13603 * We can't hold ctx->lock when iterating the ->flexible_group list due in perf_event_init_context()
13607 raw_spin_lock_irqsave(&parent_ctx->lock, flags); in perf_event_init_context()
13608 parent_ctx->rotate_disable = 1; in perf_event_init_context()
13609 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); in perf_event_init_context()
13611 perf_event_groups_for_each(event, &parent_ctx->flexible_groups) { in perf_event_init_context()
13618 raw_spin_lock_irqsave(&parent_ctx->lock, flags); in perf_event_init_context()
13619 parent_ctx->rotate_disable = 0; in perf_event_init_context()
13621 child_ctx = child->perf_event_ctxp; in perf_event_init_context()
13629 * parent_ctx->lock avoids it from being uncloned. in perf_event_init_context()
13631 cloned_ctx = parent_ctx->parent_ctx; in perf_event_init_context()
13633 child_ctx->parent_ctx = cloned_ctx; in perf_event_init_context()
13634 child_ctx->parent_gen = parent_ctx->parent_gen; in perf_event_init_context()
13636 child_ctx->parent_ctx = parent_ctx; in perf_event_init_context()
13637 child_ctx->parent_gen = parent_ctx->generation; in perf_event_init_context()
13639 get_ctx(child_ctx->parent_ctx); in perf_event_init_context()
13642 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); in perf_event_init_context()
13644 mutex_unlock(&parent_ctx->mutex); in perf_event_init_context()
13659 child->perf_event_ctxp = NULL; in perf_event_init_task()
13660 mutex_init(&child->perf_event_mutex); in perf_event_init_task()
13661 INIT_LIST_HEAD(&child->perf_event_list); in perf_event_init_task()
13682 mutex_init(&swhash->hlist_mutex); in perf_event_init_all_cpus()
13690 __perf_event_init_context(&cpuctx->ctx); in perf_event_init_all_cpus()
13691 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); in perf_event_init_all_cpus()
13692 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); in perf_event_init_all_cpus()
13693 cpuctx->online = cpumask_test_cpu(cpu, perf_online_mask); in perf_event_init_all_cpus()
13694 cpuctx->heap_size = ARRAY_SIZE(cpuctx->heap_default); in perf_event_init_all_cpus()
13695 cpuctx->heap = cpuctx->heap_default; in perf_event_init_all_cpus()
13703 mutex_lock(&swhash->hlist_mutex); in perf_swevent_init_cpu()
13704 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) { in perf_swevent_init_cpu()
13709 rcu_assign_pointer(swhash->swevent_hlist, hlist); in perf_swevent_init_cpu()
13711 mutex_unlock(&swhash->hlist_mutex); in perf_swevent_init_cpu()
13721 raw_spin_lock(&ctx->lock); in __perf_event_exit_context()
13723 list_for_each_entry(event, &ctx->event_list, event_entry) in __perf_event_exit_context()
13725 raw_spin_unlock(&ctx->lock); in __perf_event_exit_context()
13733 // XXX simplify cpuctx->online in perf_event_exit_cpu_context()
13736 ctx = &cpuctx->ctx; in perf_event_exit_cpu_context()
13738 mutex_lock(&ctx->mutex); in perf_event_exit_cpu_context()
13740 cpuctx->online = 0; in perf_event_exit_cpu_context()
13741 mutex_unlock(&ctx->mutex); in perf_event_exit_cpu_context()
13761 ctx = &cpuctx->ctx; in perf_event_init_cpu()
13763 mutex_lock(&ctx->mutex); in perf_event_init_cpu()
13764 cpuctx->online = 1; in perf_event_init_cpu()
13765 mutex_unlock(&ctx->mutex); in perf_event_init_cpu()
13806 perf_pmu_register(&perf_cpu_clock, "cpu_clock", -1); in perf_event_init()
13807 perf_pmu_register(&perf_task_clock, "task_clock", -1); in perf_event_init()
13831 if (pmu_attr->event_str) in perf_event_sysfs_show()
13832 return sprintf(page, "%s\n", pmu_attr->event_str); in perf_event_sysfs_show()
13850 if (pmu->dev) in perf_event_sysfs_init()
13854 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); in perf_event_sysfs_init()
13874 return ERR_PTR(-ENOMEM); in perf_cgroup_css_alloc()
13876 jc->info = alloc_percpu(struct perf_cgroup_info); in perf_cgroup_css_alloc()
13877 if (!jc->info) { in perf_cgroup_css_alloc()
13879 return ERR_PTR(-ENOMEM); in perf_cgroup_css_alloc()
13882 return &jc->css; in perf_cgroup_css_alloc()
13889 free_percpu(jc->info); in perf_cgroup_css_free()
13895 perf_event_cgroup(css->cgroup); in perf_cgroup_css_online()