From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796064072; b=K09bEKzBc0gU88AxUSw1UCYOVirpDruk3jYUGyxdGM4+1Rdladgacy9Gzki/TU5FVzVb ge9ySvLRdw6L0wg7h2JubR0tEklszcPtC4yTaywDPjxl72ikEXLNQArE6yM88nQ2HjPgv MVfMkAZEptCpgWiiKzwImX+xZq1Ytw9tb+zVE7hTcChmW7NDohcTv1rTMvujWMTd040J5 2Qqv4ecAgCfaun4QalhSZWq2M14FyodZ5B7JvYnqlR2SBXfIhHkYUKUywduBq5nUY+Qw7 ghD2LGJF54herxN672FQb8OjUh0kN1mXZd1Hl2cZhCXOXWtCl2cM9iGtQNrgPLVNFgQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796064072; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=OCTibf+d1kUvKxojnWaC/3mhLm3S9ucJSeot0ysMo/8=; b=Hn/uDiOqWM+IY/83aKQkf9q5MptnPQwSx2BQ+Sd/F+MjYDhyNV/ois3urF7EuiiNyWvj n1Cu3A4GxGz5Jpn6kwFWlAoUpgpueF+l4Pddojh+RiFIvq7l5fUjB4yOhTpzV0xPHCpxU pSe6IE7NcKGk+5wUwhWRKJ4h8mtXRdeER6S1WKGkcAvkFKKxX/OYqoO88PZC3oYy+uBfy ng+R/3Fe94U+LECCovy4K6yOMbRstIEozvtR3PfsULgjzIemI5gvFgKZhVzxM41IYaUfM q/SFj9Pekv0RQ4FTtm0n2WqjaAv+Zv3OOhuziFieZp5q+zSYwpg0tNVVRVUOhyg/fcw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=KHXSHUvc; arc=none smtp.remote-ip=74.125.82.198 Received: from mail-dy1-f198.google.com (mail-dy1-f198.google.com [74.125.82.198]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 401DD606A31C for ; Mon, 30 Nov 2026 18:41:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 401DD606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=KHXSHUvc Received: by mail-dy1-f198.google.com with SMTP id 5a478bee46e88-386b8624cf7so4816936eec.1 for ; Mon, 30 Nov 2026 10:41:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796064070; x=1796668870; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=OCTibf+d1kUvKxojnWaC/3mhLm3S9ucJSeot0ysMo/8=; b=KHXSHUvccl8rRjjCHVDGK7g9WT3rbf2RMwcAFTUmYqIeEJtB7BQJCYn1g4a8tLcZuB YIVStqUTE/PEcWHbM84AXxtuFrCjKTDKzNwDtCg0LJ36NKx+vgfKTpn6GyK8dUmhSNO5 M/JgSYAJ0oNsx1DVLxxt0QTfmvKzFxzzffwmW1UzQrNUGgSOgGJr0muAN3Z3fDCiCA/V i4EjAzweb/TONq4+5S0NC3jXl6NqQ1y6ePUoX2U45dOkCJfdHo0NzcJ/H40HkTjCm7N0 zpY7V0e5uHW8PYkQQzzbOqJBcvzPjqLaURXRXEtawn3zEbD1yNUIhcXPG3GOF7Wv5fNB Jp7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796064070; x=1796668870; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=OCTibf+d1kUvKxojnWaC/3mhLm3S9ucJSeot0ysMo/8=; b=CTCfOC9DDxDPuBy0thKnSgtZAdCBvdBw/RfGSBgpJ+R7SSLMrPKTpSfE1Jv9ZStVeD EMCaqZmGCZ9CKD6jTs4e4qJCJkdfaDJpz9lpS5IJKtGtA7KKFcea/HG08X3OkRktVsfN rhIr+OGePiB6p3bb2yTP7aNuFaGt2M1pqFJ71bP2yF+0hcrX1/gGR2HsiJvQgtEUbOjD 3VXpjPu0AaRZEM0CYJVYR7sYJeVQz6ZnS0YubiI6wSBpDvPq6ZWBkTSP+jVjsGPtSP6u Cv0I7IrY4gxJLMzcUUlyKlgo8v/reuzI3Hoz7GpS0FeFWo+KY50LXeZWrf4klsC9Vbeg 9z3w== X-Forwarded-Encrypted: i=1; AL+hErHS7YsMlgrYTw9Hr4SAM5QoKka7gYywjBnlfvtc6NUCkaOk6M9EeNLGMBzC4+A4dVGMmEADbdrrQGtoaMM=@vger.kernel.org X-Gm-Message-State: APuU4Z/aE35ZZ/ZOsRu/mtrn6MUqlA3VtGBQgmQtGezV1tRsmimgbWmi 5om9CzihaIRfXWKmrADwDdljISiZywAk3qHnfKnnceU6O/C9XApsmX3Jm+VqjJ3A3xeWaZeCaLp 2DatTb/JKMg== X-Received: from dya19.prod.google.com ([2002:a05:693c:6213:b0:381:1c8c:a899]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:182f:b0:385:cf42:9b9b with SMTP id 5a478bee46e88-385cf429e7amr33224033eec.8.1796064069703; Mon, 30 Nov 2026 10:41:09 -0800 (PST) Date: Mon, 30 Nov 2026 10:41:05 -0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <8b61be11e09ae7f663253cde769eff5456ebc263.1796063587.git.irogers@google.com> Subject: [PATCH v4 1/2] perf/core: Add PERF_EVENT_STATE_ON to fix filtered event enabled time From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim Cc: Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , James Clark , Andi Kleen , Dapeng Mi , Kan Liang , Thomas Falcon , Stephane Eranian , Ian Rogers , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Perf events that are filtered out during sched_in (via pmu->filter() on hybrid/heterogeneous systems, or event_filter_match() for task+CPU and cgroup events) remain unscheduled while the context is active. Because enabled events previously rested in PERF_EVENT_STATE_INACTIVE, context time accrued into total_time_enabled even when the event could not be scheduled on the current CPU or PMU, while total_time_running did not increase. Tools assume total_time_enabled > total_time_running implies counter multiplexing and scale counts by enabled / running, producing severely inaccurate counts: https://lore.kernel.org/lkml/20250627192417.1157736-13-irogers@google.com/ For example: - A task+CPU event opened on 1 CPU of a 128-CPU system accrues enabled time whenever the task runs on the other 127 CPUs, potentially scaling the count 128x. - On hybrid systems, unpinned task events accrue enabled time while the task runs on a different core type, causing bogus multiplexing scaling (such as the 28.05% / 96.79% scaling on cpu_atom and cpu_core seen in commit d9db9c8db56c ("perf test: Fix perf stat --bpf-counters on hybrid machines"), which caused count mismatches until commit b02027776ac5 ("perf tests: Fix flakiness in BPF counters test on hybrid systems") pinned the workload to a single CPU) and preventing perf stat from accurately merging common hybrid events across core types. Introduce PERF_EVENT_STATE_ON (0) for events that are enabled and not scheduled, reserving PERF_EVENT_STATE_INACTIVE (1) for flexible events that matched filters during merge_sched_in() but failed to go onto the PMU due to insufficient counters or exclusivity constraints: - total_time_enabled only accrues in PERF_EVENT_STATE_INACTIVE and PERF_EVENT_STATE_ACTIVE; filtered-out events stay in PERF_EVENT_STATE_ON and accrue neither enabled nor running time. - Track INACTIVE flexible group leaders on epc->flexible_inactive (via event->active_list) so __pmu_ctx_sched_out() transitions them back to PERF_EVENT_STATE_ON without touching filtered ON events, preserving the context-switch scalability of state-based timekeeping. - Extend __perf_effective_state() so ON siblings of an INACTIVE group leader inherit INACTIVE timekeeping, and update sibling times in perf_event_set_state() when a leader enters or leaves INACTIVE. - Handle PERF_EVENT_STATE_INACTIVE in event_sched_out() without calling pmu->del() or decrementing active_oncpu, and serialize perf_event_read() on ctx->lock for both ON and INACTIVE events. Fixes: 0d3d73aac2ff ("perf/core: Rewrite event timekeeping") Signed-off-by: Ian Rogers --- Resending as a non-RFC v2 following Namhyung Kim's agreement on adding a separate state for filtered events and Ingo Molnar's suggestion to re-send: https://lore.kernel.org/lkml/CAP-5=fV5gtSQOPkOLaAauBcwjgs1ohADs5O3GF+bJOx2tSynwQ@mail.gmail.com/ https://lore.kernel.org/lkml/aS3WXn4nz9GE87bM@gmail.com/ v4: Address review feedback from Peter Zijlstra on v3: https://lore.kernel.org/all/20261125123712.GP1813469@noisy.programming.kicks-ass.net/ https://lore.kernel.org/all/20261125130233.GO2669272@noisy.programming.kicks-ass.net/ - Update the state diagram and comments in perf_event.h as suggested. - Extend __perf_effective_state() and perf_event_set_state() so enabled (ON) siblings inherit INACTIVE from their group leader, removing group_set_inactive() and the INACTIVE special case in __perf_event_enable(), while promoting ON siblings of a detached INACTIVE leader directly to INACTIVE on flexible_inactive. - De-duplicate pending_disable handling in event_sched_out() and update group userpages when scheduling out an INACTIVE leader. - Make merge_sched_in() branch directly on group_can_go_on() and group_sched_in() rather than testing event->state == ON, and document the speculative ACTIVE rollback in event_sched_in(). v3: No changes. v2: Make the untested RFC v1 functional, tested, and handle all state transitions: https://lore.kernel.org/lkml/20250716223924.825772-1-irogers@google.com/ - Add epc->flexible_inactive (and handle sibling promotion on group detach) so __pmu_ctx_sched_out() resets INACTIVE flexible groups back to PERF_EVENT_STATE_ON on context switch / rotation. - Transition flexible groups that fail to schedule in merge_sched_in() (including after can_add_hw == 0 or partial group rollback) to PERF_EVENT_STATE_INACTIVE, while pinned groups still enter PERF_EVENT_STATE_ERROR. - Handle PERF_EVENT_STATE_INACTIVE in event_sched_out() without calling pmu->del() or decrementing active_oncpu/nr_freq, and update the userpage. - Drop premature PERF_EVENT_STATE_ACTIVE in __perf_event_enable(), match INACTIVE leader state when enabling a sibling, serialize perf_event_read() on ctx->lock for both ON and INACTIVE, and drop invalid ERROR -> OFF disable() edge from the state diagram comment. --- include/linux/perf_event.h | 61 ++++++---- kernel/events/core.c | 242 ++++++++++++++++++++++--------------- 2 files changed, 184 insertions(+), 119 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index e1e11500df09..ed6b9eec2024 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -639,28 +639,45 @@ struct perf_addr_filter_range { /* * The normal states are: * - * ACTIVE --. - * ^ | - * | | - * sched_{in,out}() | - * | | - * v | - * ,---> INACTIVE --+ <-. - * | | | - * | {dis,en}able() - * sched_in() | | - * | OFF <--' --+ - * | | - * `---> ERROR ------' + * OFF <----disable()---. + * | | | + * .----------enable() | | + * | | .------' | + * | v / | + * ERROR ON <----------. | + * ^ | | | + * | sched_in() sched_out() | + * | | | | + * +-(no space)--+ | | + * | | | | + * v v | | + * INACTIVE ACTIVE | | + * | | | | + * '-------------+-------------+-----' * * That is: * - * sched_in: INACTIVE -> {ACTIVE,ERROR} - * sched_out: ACTIVE -> INACTIVE - * disable: {ACTIVE,INACTIVE} -> OFF - * enable: {OFF,ERROR} -> INACTIVE + * sched_in: ON -> {ERROR,INACTIVE,ACTIVE} + * sched_out: {INACTIVE,ACTIVE} -> ON + * disable: {ON,INACTIVE,ACTIVE} -> OFF + * enable: {OFF,ERROR} -> ON * - * Where {OFF,ERROR} are disabled states. + * Where {ON,INACTIVE,ACTIVE} are enabled states and {OFF,ERROR} are disabled + * states. + * + * ON differs from INACTIVE as enabled time only increases for INACTIVE and + * ACTIVE events, running time only increases for ACTIVE events. A filter, + * such as the task, cgroup, CPU, or PMU an event should be scheduled on, can + * cause an event during sched_in to remain in the ON state, while lack of + * counters or exclusivity constraints move pinned events to ERROR and + * flexible events to INACTIVE. + * + * Additional special-case transitions not shown above: + * + * - sched_out() with pending_disable moves {INACTIVE,ACTIVE} directly to OFF; + * + * - detaching a group sibling that requires a group leader (such as + * aux_output or PERF_EV_CAP_SIBLING) disables it into ERROR. * * Then we have the {EXIT,REVOKED,DEAD} states which are various shades of * defunct events: @@ -682,9 +699,10 @@ enum perf_event_state { PERF_EVENT_STATE_REVOKED = -4, /* pmu gone, must not touch */ PERF_EVENT_STATE_EXIT = -3, /* task died, still inherit */ PERF_EVENT_STATE_ERROR = -2, /* scheduling error, can enable */ - PERF_EVENT_STATE_OFF = -1, - PERF_EVENT_STATE_INACTIVE = 0, - PERF_EVENT_STATE_ACTIVE = 1, + PERF_EVENT_STATE_OFF = -1, /* regular disabled event */ + PERF_EVENT_STATE_ON = 0, /* enabled but not scheduled */ + PERF_EVENT_STATE_INACTIVE = 1, /* not scheduled due to lack of counters */ + PERF_EVENT_STATE_ACTIVE = 2, /* scheduled and counting */ }; struct file; @@ -969,6 +987,7 @@ struct perf_event_pmu_context { struct list_head pinned_active; struct list_head flexible_active; + struct list_head flexible_inactive; /* Used to identify the per-cpu perf_event_pmu_context */ unsigned int embedded : 1; diff --git a/kernel/events/core.c b/kernel/events/core.c index 366fca000ff0..3e9d70cfa4ba 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -718,11 +718,12 @@ static inline u64 perf_event_clock(struct perf_event *event) * * Event groups make things a little more complicated, but not terribly so. The * rules for a group are that if the group leader is OFF the entire group is - * OFF, irrespective of what the group member states are. This results in - * __perf_effective_state(). + * OFF, irrespective of what the group member states are. Similarly, if the + * group leader is INACTIVE, enabled (ON) siblings are effectively INACTIVE. + * This results in __perf_effective_state(). * * A further ramification is that when a group leader flips between OFF and - * !OFF, we need to update all group member times. + * !OFF, or enters/leaves INACTIVE, we need to update all group member times. * * * NOTE: perf_event_time() is based on the (cgroup) context time, and thus we @@ -733,12 +734,17 @@ static inline u64 perf_event_clock(struct perf_event *event) static __always_inline enum perf_event_state __perf_effective_state(struct perf_event *event) { - struct perf_event *leader = event->group_leader; + enum perf_event_state leader_state = READ_ONCE(event->group_leader->state); + enum perf_event_state state = READ_ONCE(event->state); - if (leader->state <= PERF_EVENT_STATE_OFF) - return leader->state; + if (leader_state <= PERF_EVENT_STATE_OFF) + return leader_state; - return event->state; + if (leader_state == PERF_EVENT_STATE_INACTIVE && + state == PERF_EVENT_STATE_ON) + return PERF_EVENT_STATE_INACTIVE; + + return state; } static __always_inline void @@ -781,10 +787,12 @@ perf_event_set_state(struct perf_event *event, enum perf_event_state state) perf_event_update_time(event); /* - * If a group leader gets enabled/disabled all its siblings - * are affected too. + * If a group leader gets enabled/disabled or enters/leaves INACTIVE, + * all its siblings are affected too. */ - if ((event->state < 0) ^ (state < 0)) + if (((event->state < 0) ^ (state < 0)) || + ((event->state == PERF_EVENT_STATE_INACTIVE) ^ + (state == PERF_EVENT_STATE_INACTIVE))) perf_event_update_sibling_time(event); WRITE_ONCE(event->state, state); @@ -2023,7 +2031,7 @@ list_add_event(struct perf_event *event, struct perf_event_context *ctx) static inline void perf_event__state_init(struct perf_event *event) { event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : - PERF_EVENT_STATE_INACTIVE; + PERF_EVENT_STATE_ON; } static int __perf_event_read_size(u64 read_format, int nr_siblings) @@ -2354,7 +2362,7 @@ static inline struct list_head *get_event_list(struct perf_event *event) /* @sibling must already be unlinked from its old leader's sibling_list. */ static void perf_promote_sibling_to_leader(struct perf_event *sibling, struct perf_event_context *ctx, - int group_caps) + int group_caps, bool inactive) { /* * Events that have PERF_EV_CAP_SIBLING require being part of @@ -2372,14 +2380,19 @@ static void perf_promote_sibling_to_leader(struct perf_event *sibling, if (sibling->attach_state & PERF_ATTACH_CONTEXT) { add_event_to_groups(sibling, ctx); - if (sibling->state == PERF_EVENT_STATE_ACTIVE) + if (sibling->state == PERF_EVENT_STATE_ACTIVE) { list_add_tail(&sibling->active_list, get_event_list(sibling)); + } else if (inactive && sibling->state == PERF_EVENT_STATE_ON) { + perf_event_set_state(sibling, PERF_EVENT_STATE_INACTIVE); + list_add_tail(&sibling->active_list, + &sibling->pmu_ctx->flexible_inactive); + } } perf_event__header_size(sibling); } -static void perf_group_detach(struct perf_event *event) +static void perf_group_detach(struct perf_event *event, bool inactive) { struct perf_event *leader = event->group_leader; struct perf_event *sibling, *tmp; @@ -2404,7 +2417,7 @@ static void perf_group_detach(struct perf_event *event) list_del_init(&event->sibling_list); leader->nr_siblings--; leader->group_generation++; - perf_promote_sibling_to_leader(event, ctx, event->event_caps); + perf_promote_sibling_to_leader(event, ctx, event->event_caps, false); goto out; } @@ -2417,7 +2430,7 @@ static void perf_group_detach(struct perf_event *event) list_del_init(&sibling->sibling_list); /* Inherit group flags from the previous leader */ - perf_promote_sibling_to_leader(sibling, ctx, event->group_caps); + perf_promote_sibling_to_leader(sibling, ctx, event->group_caps, inactive); WARN_ON_ONCE(sibling->ctx != event->ctx); } @@ -2468,19 +2481,49 @@ static inline bool is_event_in_freq_mode(struct perf_event *event) return event->attr.freq && event->attr.sample_freq; } +/* + * Because the userpage is strictly per-event (there is no concept of context, + * so there cannot be a context indirection), every userpage must be updated + * when context time starts :-( + * + * IOW, we must not miss EVENT_TIME edges. + */ +static inline bool event_update_userpage(struct perf_event *event) +{ + if (likely(!refcount_read(&event->mmap_count))) + return false; + + perf_event_update_time(event); + perf_event_update_userpage(event); + + return true; +} + +static inline void group_update_userpage(struct perf_event *group_event) +{ + struct perf_event *event; + + if (!event_update_userpage(group_event)) + return; + + for_each_sibling_event(event, group_event) + event_update_userpage(event); +} + static void event_sched_out(struct perf_event *event, struct perf_event_context *ctx) { struct perf_event_pmu_context *epc = event->pmu_ctx; struct perf_cpu_pmu_context *cpc = this_cpc(epc->pmu); - enum perf_event_state state = PERF_EVENT_STATE_INACTIVE; + enum perf_event_state state = PERF_EVENT_STATE_ON; + bool active = (event->state == PERF_EVENT_STATE_ACTIVE); // XXX cpc serialization, probably per-cpu IRQ disabled WARN_ON_ONCE(event->ctx != ctx); lockdep_assert_held(&ctx->lock); - if (event->state != PERF_EVENT_STATE_ACTIVE) + if (event->state < PERF_EVENT_STATE_INACTIVE) return; /* @@ -2490,10 +2533,11 @@ event_sched_out(struct perf_event *event, struct perf_event_context *ctx) */ list_del_init(&event->active_list); - perf_pmu_disable(event->pmu); - - event->pmu->del(event, 0); - event->oncpu = -1; + if (active) { + perf_pmu_disable(event->pmu); + event->pmu->del(event, 0); + event->oncpu = -1; + } if (event->pending_disable) { event->pending_disable = 0; @@ -2503,30 +2547,38 @@ event_sched_out(struct perf_event *event, struct perf_event_context *ctx) perf_event_set_state(event, state); - if (!is_software_event(event)) - cpc->active_oncpu--; - if (is_event_in_freq_mode(event)) { - ctx->nr_freq--; - epc->nr_freq--; - } - if (event->attr.exclusive || !cpc->active_oncpu) - cpc->exclusive = 0; + if (active) { + if (!is_software_event(event)) + cpc->active_oncpu--; + if (is_event_in_freq_mode(event)) { + ctx->nr_freq--; + epc->nr_freq--; + } + if (event->attr.exclusive || !cpc->active_oncpu) + cpc->exclusive = 0; - perf_pmu_enable(event->pmu); + perf_pmu_enable(event->pmu); + } else { + group_update_userpage(event); + } } static void group_sched_out(struct perf_event *group_event, struct perf_event_context *ctx) { struct perf_event *event; + bool active = (group_event->state == PERF_EVENT_STATE_ACTIVE); - if (group_event->state != PERF_EVENT_STATE_ACTIVE) + if (group_event->state < PERF_EVENT_STATE_INACTIVE) return; perf_assert_pmu_disabled(group_event->pmu_ctx->pmu); event_sched_out(group_event, ctx); + if (!active) + return; + /* * Schedule out siblings (if any): */ @@ -2597,6 +2649,7 @@ __perf_remove_from_context(struct perf_event *event, struct perf_event_pmu_context *pmu_ctx = event->pmu_ctx; enum perf_event_state state = PERF_EVENT_STATE_OFF; unsigned long flags = (unsigned long)info; + bool inactive; perf_pmu_disable(pmu_ctx->pmu); ctx_time_update(cpuctx, ctx); @@ -2619,10 +2672,13 @@ __perf_remove_from_context(struct perf_event *event, if (!(flags & DETACH_GROUP) && event == event->group_leader) group_sched_out(event, ctx); + inactive = (event == event->group_leader && + event->state == PERF_EVENT_STATE_INACTIVE); + __event_disable(event, ctx, state); if (flags & DETACH_GROUP) - perf_group_detach(event); + perf_group_detach(event, inactive); if (flags & DETACH_CHILD) perf_child_detach(event); list_del_event(event, ctx); @@ -2702,7 +2758,7 @@ static void __perf_event_disable(struct perf_event *event, struct perf_event_context *ctx, void *info) { - if (event->state < PERF_EVENT_STATE_INACTIVE) + if (event->state < PERF_EVENT_STATE_ON) return; perf_pmu_disable(event->pmu_ctx->pmu); @@ -2717,7 +2773,7 @@ static void __perf_event_disable(struct perf_event *event, /* * But only mark the leader OFF; the siblings will remain - * INACTIVE. + * ON. */ __event_disable(event, ctx, PERF_EVENT_STATE_OFF); @@ -2859,7 +2915,11 @@ event_sched_in(struct perf_event *event, struct perf_event_context *ctx) perf_log_itrace_start(event); if (event->pmu->add(event, PERF_EF_START)) { - perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); + /* + * Undo the speculative ACTIVE transition; siblings remain ON + * and merge_sched_in() moves the leader to ERROR or INACTIVE. + */ + perf_event_set_state(event, PERF_EVENT_STATE_ON); event->oncpu = -1; ret = -EAGAIN; goto out; @@ -2886,7 +2946,7 @@ group_sched_in(struct perf_event *group_event, struct perf_event_context *ctx) struct perf_event *event, *partial_group = NULL; struct pmu *pmu = group_event->pmu_ctx->pmu; - if (group_event->state == PERF_EVENT_STATE_OFF) + if (group_event->state <= PERF_EVENT_STATE_OFF) return 0; pmu->start_txn(pmu, PERF_PMU_TXN_ADD); @@ -3261,13 +3321,13 @@ static void __perf_event_enable(struct perf_event *event, struct perf_event *leader = event->group_leader; struct perf_event_context *task_ctx; - if (event->state >= PERF_EVENT_STATE_INACTIVE || + if (event->state >= PERF_EVENT_STATE_ON || event->state <= PERF_EVENT_STATE_ERROR) return; ctx_time_freeze(cpuctx, ctx); - perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); + perf_event_set_state(event, PERF_EVENT_STATE_ON); perf_cgroup_event_enable(event, ctx); if (!ctx->is_active) @@ -3304,7 +3364,7 @@ static void _perf_event_enable(struct perf_event *event) struct perf_event_context *ctx = event->ctx; raw_spin_lock_irq(&ctx->lock); - if (event->state >= PERF_EVENT_STATE_INACTIVE || + if (event->state >= PERF_EVENT_STATE_ON || event->state < PERF_EVENT_STATE_ERROR) { out: raw_spin_unlock_irq(&ctx->lock); @@ -3578,6 +3638,10 @@ static void __pmu_ctx_sched_out(struct perf_event_pmu_context *pmu_ctx, &pmu_ctx->flexible_active, active_list) group_sched_out(event, ctx); + list_for_each_entry_safe(event, tmp, + &pmu_ctx->flexible_inactive, + active_list) + group_sched_out(event, ctx); /* * Since we cleared EVENT_FLEXIBLE, also clear * rotate_necessary, is will be reset by @@ -4100,35 +4164,6 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx, return 0; } -/* - * Because the userpage is strictly per-event (there is no concept of context, - * so there cannot be a context indirection), every userpage must be updated - * when context time starts :-( - * - * IOW, we must not miss EVENT_TIME edges. - */ -static inline bool event_update_userpage(struct perf_event *event) -{ - if (likely(!refcount_read(&event->mmap_count))) - return false; - - perf_event_update_time(event); - perf_event_update_userpage(event); - - return true; -} - -static inline void group_update_userpage(struct perf_event *group_event) -{ - struct perf_event *event; - - if (!event_update_userpage(group_event)) - return; - - for_each_sibling_event(event, group_event) - event_update_userpage(event); -} - struct merge_sched_data { int can_add_hw; enum event_type_t event_type; @@ -4154,29 +4189,38 @@ static int merge_sched_in(struct perf_event *event, void *data) !(msd->event_type & EVENT_GUEST)) return 0; - if (group_can_go_on(event, msd->can_add_hw)) { - if (!group_sched_in(event, ctx)) - list_add_tail(&event->active_list, get_event_list(event)); + if (group_can_go_on(event, msd->can_add_hw) && + !group_sched_in(event, ctx)) { + list_add_tail(&event->active_list, get_event_list(event)); + return 0; } - if (event->state == PERF_EVENT_STATE_INACTIVE) { - msd->can_add_hw = 0; - if (event->attr.pinned) { - perf_cgroup_event_disable(event, ctx); - perf_event_set_state(event, PERF_EVENT_STATE_ERROR); + /* + * Partial group rollback in group_sched_in() may have consumed + * pending_disable and moved the event to OFF. + */ + if (event->state <= PERF_EVENT_STATE_OFF) + return 0; - if (*perf_event_fasync(event)) - event->pending_kill = POLL_ERR; + msd->can_add_hw = 0; + if (event->attr.pinned) { + perf_cgroup_event_disable(event, ctx); + perf_event_set_state(event, PERF_EVENT_STATE_ERROR); - event->pending_wakeup = 1; - irq_work_queue(&event->pending_irq); - } else { - struct perf_cpu_pmu_context *cpc = this_cpc(event->pmu_ctx->pmu); + if (*perf_event_fasync(event)) + event->pending_kill = POLL_ERR; - event->pmu_ctx->rotate_necessary = 1; - perf_mux_hrtimer_restart(cpc); - group_update_userpage(event); - } + event->pending_wakeup = 1; + irq_work_queue(&event->pending_irq); + } else { + struct perf_cpu_pmu_context *cpc = this_cpc(event->pmu_ctx->pmu); + + perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); + list_add_tail(&event->active_list, + &event->pmu_ctx->flexible_inactive); + event->pmu_ctx->rotate_necessary = 1; + perf_mux_hrtimer_restart(cpc); + group_update_userpage(event); } return 0; @@ -4702,10 +4746,10 @@ static int event_enable_on_exec(struct perf_event *event, return 0; event->attr.enable_on_exec = 0; - if (event->state >= PERF_EVENT_STATE_INACTIVE) + if (event->state >= PERF_EVENT_STATE_ON) return 0; - perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); + perf_event_set_state(event, PERF_EVENT_STATE_ON); return 1; } @@ -5032,13 +5076,13 @@ static int perf_event_read(struct perf_event *event, bool group) preempt_enable(); ret = data.ret; - } else if (state == PERF_EVENT_STATE_INACTIVE) { + } else if (state >= PERF_EVENT_STATE_ON) { struct perf_event_context *ctx = event->ctx; unsigned long flags; raw_spin_lock_irqsave(&ctx->lock, flags); - state = event->state; - if (state != PERF_EVENT_STATE_INACTIVE) { + if (event->state != state) { + state = event->state; raw_spin_unlock_irqrestore(&ctx->lock, flags); goto again; } @@ -5079,6 +5123,7 @@ __perf_init_event_pmu_context(struct perf_event_pmu_context *epc, struct pmu *pm INIT_LIST_HEAD(&epc->pmu_ctx_entry); INIT_LIST_HEAD(&epc->pinned_active); INIT_LIST_HEAD(&epc->flexible_active); + INIT_LIST_HEAD(&epc->flexible_inactive); atomic_set(&epc->refcount, 1); } @@ -5314,6 +5359,7 @@ static void put_pmu_ctx(struct perf_event_pmu_context *epc) WARN_ON_ONCE(!list_empty(&epc->pinned_active)); WARN_ON_ONCE(!list_empty(&epc->flexible_active)); + WARN_ON_ONCE(!list_empty(&epc->flexible_inactive)); raw_spin_unlock_irqrestore(&ctx->lock, flags); @@ -9155,7 +9201,7 @@ perf_iterate_ctx(struct perf_event_context *ctx, list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { if (!all) { - if (event->state < PERF_EVENT_STATE_INACTIVE) + if (event->state < PERF_EVENT_STATE_ON) continue; if (!event_filter_match(event)) continue; @@ -9179,7 +9225,7 @@ static void perf_iterate_sb_cpu(perf_iterate_f output, void *data) if (!smp_load_acquire(&event->ctx)) continue; - if (event->state < PERF_EVENT_STATE_INACTIVE) + if (event->state < PERF_EVENT_STATE_ON) continue; if (!event_filter_match(event)) continue; @@ -13554,7 +13600,7 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu, event->ns = get_pid_ns(task_active_pid_ns(current)); event->id = atomic64_inc_return(&perf_event_id); - event->state = PERF_EVENT_STATE_INACTIVE; + event->state = PERF_EVENT_STATE_ON; if (parent_event) event->event_caps = parent_event->event_caps; @@ -14600,7 +14646,7 @@ static void __perf_pmu_install_event(struct pmu *pmu, event->pmu_ctx = epc; if (event->state >= PERF_EVENT_STATE_OFF) - event->state = PERF_EVENT_STATE_INACTIVE; + event->state = PERF_EVENT_STATE_ON; perf_install_in_context(ctx, event, cpu); /* @@ -15038,8 +15084,8 @@ inherit_event(struct perf_event *parent_event, * not its attr.disabled bit. We hold the parent's mutex, * so we won't race with perf_event_{en, dis}able_family. */ - if (parent_state >= PERF_EVENT_STATE_INACTIVE) - child_event->state = PERF_EVENT_STATE_INACTIVE; + if (parent_state >= PERF_EVENT_STATE_ON) + child_event->state = PERF_EVENT_STATE_ON; else child_event->state = PERF_EVENT_STATE_OFF; -- 2.56.1.839.g22a8290db9-goog