Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796365673; b=L8uuHLJJPNHjuJW5pIpA0zU0UuY7gtIPvPaG8zxpsAx8WZTwpuPQLnSiqf0OlScd57Bs JYBaq2e7YC6bhGCYmb1bcSzprOLXe22qqFkS58W4aqKrCKiOtANKBI6bY9Yow7yhB2SDu +Erx4RMWtewuy1OuUjf5RKLspPa92OEwaVuM9mODsteqq0Ylv+3hYf4QUQV19dZfuCvfV OYs46FQ7/ZXRGCnZMldnkNx5n1pA1Pf/j3Z1Sy7VJVA3K3Rbgxm2SxygGqIydtm/cYCCL u4yd83teEAHa5V5XD0WCZiWcFDNO62V/UUUejdrcY/UkA2Bf9aJFkL557dj7tNKfAXg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796365673; 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=Rx71dArVCBegpelkI5ThiMhcSglbPoEtwDHV5Yr/KE8=; b=QkTjuzxZWp4qRLY09GY6gQL82EvbUZoxj2BeLMoGmWSK+STDHBwQ066RLsEsKX3nLhUx V6lp9Q2tsjfWHQxFIDumBRLMrK6sIsWyPXyj1mURe9qT6lK7sYicKiNFAZ7Cylmt7ets5 4/viqciKXSJIinvgUqhIoooioGu5R1bkO8+WsjfOKNjy7AIMIftpA99OnHiW5FBhs3eyC MbbYwMo8nSc7ZM9GrajdQt/MAcJYbHGb3YsUxcT3StgdS0IesrfdTNf0d0D31ILat3uiZ ynmd4XCHchGHxefVQ9u1Tp+pLeRo1jE/XXIrWkmkI7HUUThoMfJK+dPIs1t4ExXFo1A== 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=SRtETdZ6; arc=none smtp.remote-ip=74.125.82.200 Received: from mail-dy1-f200.google.com (mail-dy1-f200.google.com [74.125.82.200]) (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 6E86B606A32F for ; Fri, 4 Dec 2026 06:27:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6E86B606A32F 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=SRtETdZ6 Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-382af5cfc58so1687926eec.0 for ; Thu, 03 Dec 2026 22:27:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796365672; x=1796970472; 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=Rx71dArVCBegpelkI5ThiMhcSglbPoEtwDHV5Yr/KE8=; b=SRtETdZ6mo2l32R1Kf6gs2QcCD19MgxmMKK/f71cr6/MtCf7iHhpGLbhN4ZsslQJ4C WSMdi9t+XkwsDHswXfDnClaOOxhrzkYbtWUtNd/cM6kAN6o41UkH5pIGVmzIPAJuA9Cx 7Djt2DgYf1YAxdqUVZ2vosqWpuviSs8cq3q+lOslhV/o/q9CSIDDgOn59YSZd86h1zR8 FikcmDTiqJAEh88fsI9mGZ16ewFUQhjXlSZU0WwoDSUJUhnCTK5gWwUbABEHPKGbCyn5 j0IjyWg6fg9C3OuOuwfFlfsyRnRpwM4cpj0dZLBTnYfp55rn/MYl3Gbk17F0jnkyqfAW KTaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796365672; x=1796970472; 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=Rx71dArVCBegpelkI5ThiMhcSglbPoEtwDHV5Yr/KE8=; b=PueRAwXkZJOPYfkAEDqcR9b0VdRb2+kFozClMIn2w8pRSb0PT977wHNmIKQd6EoYbA 6Hr1w9bszO61BuKtpffcUtmFNUb9Uhp3vnRUvMheocWF/NBsLiT99DgYFUWF2fBhkv2C qOxGSbpte6X+yCClGWjIuTN2zx7wBY63i7kDI4ar1mXP8/zxTvDIc527eyxLtGojjfBZ 0w6Fs/TSA5iec4tosfnUXNgLnijW3lQQzI26kSsCRRTeA8ThVXKYrQUY13bBYXSgCE/I lABXsrHKw3fZ11PX3qJPeC0CFHsA4p4eknB6uHWVB0O02bNVJUtYycUTvFZeh77xUs1X zjSw== X-Forwarded-Encrypted: i=1; AL+hErFBQcOTZZql3o26v9W6pSBG+kSviEzir99nBjrTki6JXTXRLyKQpDs7D5JREsHQ/HwxOk8=@vger.kernel.org X-Gm-Message-State: APuU4Z9uCANtuN1C3QfK9FjP0U9EkVxMcjw6gUdeqlAmxLD3NeGDfa7o DztqrPPcN+6HXf16qa1uDPxS4ZRuTd8u2xCA5/m5v/XgZU11nL2FIZv7czzqyuP9tQFE2o8Uhlj LxjWmF6VgVg== X-Received: from dybnb34.prod.google.com ([2002:a05:7300:cca2:b0:386:e30f:2e14]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:61db:b0:387:4a26:f4d1 with SMTP id 5a478bee46e88-38752239e99mr1584551eec.41.1796365671256; Thu, 03 Dec 2026 22:27:51 -0800 (PST) Date: Thu, 3 Dec 2026 22:27:46 -0800 X-Mailing-List: bpf@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.915.g69d5ec7298-goog Message-ID: Subject: [PATCH v6 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: Jiri Olsa , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Ian Rogers 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. Enabled time has accrued for any state > PERF_EVENT_STATE_OFF since the original event state machine, so this isn't a regression from a particular commit; hybrid and big.LITTLE PMU support just made the problem far more visible in practice. 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. - During sched_in, merge_sched_in() is the only place moving events from ON to {ERROR, INACTIVE}: pinned groups that fail to schedule enter ERROR, while flexible groups that fail move (with their enabled siblings) to INACTIVE via group_set_inactive(), which replaces group_update_userpage(), and are placed on epc->flexible_active behind the ACTIVE groups. event_sched_in() and partial group rollback in group_sched_in() return events to ON. - event_sched_out() and group_sched_out() handle INACTIVE events by moving them back to ON without calling pmu->del() or adjusting active_oncpu/nr_freq, so context switch and rotation reset INACTIVE groups via the existing flexible_active walk. - Enabling a sibling whose group leader is INACTIVE moves it straight to INACTIVE in __perf_event_enable() so it accrues enabled time with the group. - perf_promote_sibling_to_leader() adds promoted ACTIVE siblings to the head and INACTIVE siblings to the tail of flexible_active so ctx_event_to_rotate() keeps picking an ACTIVE group first. - perf_event_read() serializes on ctx->lock for both ON and INACTIVE events. Suggested-by: Peter Zijlstra Signed-off-by: Ian Rogers --- v6: Address Sashiko review of v5: https://lore.kernel.org/all/sashiko-outbox-248507@kernel.org/ - Only update the userpage of an INACTIVE event in event_sched_out() when it is mmapped, as in event_set_inactive(), so an event redirected with PERF_EVENT_IOC_SET_OUTPUT doesn't overwrite the ring buffer owner's userpage. - Use event_set_inactive() with the PMU disabled when enabling a sibling of an INACTIVE leader in __perf_event_enable(), so a PMU interrupt can't nest the userpage update. v5: Rework following Peter Zijlstra's simplified version and review on v3/v4: https://lore.kernel.org/all/20261202142639.GE83911@noisy.programming.kicks-ass.net/ https://lore.kernel.org/all/20261203121520.GN2883950@noisy.programming.kicks-ass.net/ - Drop epc->flexible_inactive and put INACTIVE flexible groups on epc->flexible_active after the ACTIVE ones. - Drop the __perf_effective_state()/perf_event_set_state() sibling inheritance from v4 and instead set siblings INACTIVE directly in group_set_inactive() (replacing event_update_userpage() and group_update_userpage() in place). - Roll back to ON in event_sched_in() and group_sched_in() so merge_sched_in() does the only sched_in ON -> {ERROR, INACTIVE} transition, avoiding siblings stuck in INACTIVE after pinned ERROR or pending_disable rollback, and avoiding enabling OFF siblings. - Match an INACTIVE leader when enabling a sibling in __perf_event_enable(), and document this transition in perf_event.h. - Use list_add() for promoted ACTIVE siblings and list_add_tail() for INACTIVE siblings in perf_promote_sibling_to_leader(). - Drop the Fixes: tag, as enabled time has always accrued for states > PERF_EVENT_STATE_OFF. 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. - De-duplicate pending_disable handling in event_sched_out(). - Make merge_sched_in() branch directly on group_can_go_on() and group_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/ --- include/linux/perf_event.h | 63 +++++++++----- kernel/events/core.c | 165 ++++++++++++++++++++++--------------- 2 files changed, 140 insertions(+), 88 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index e1e11500df09..a8094f02edb8 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -639,28 +639,48 @@ 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; + * + * - enable() of a group sibling whose leader is INACTIVE moves it directly + * to INACTIVE; + * + * - 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 +702,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; diff --git a/kernel/events/core.c b/kernel/events/core.c index 366fca000ff0..e287e96c3976 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -2023,7 +2023,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) @@ -2373,6 +2373,8 @@ static void perf_promote_sibling_to_leader(struct perf_event *sibling, add_event_to_groups(sibling, ctx); if (sibling->state == PERF_EVENT_STATE_ACTIVE) + list_add(&sibling->active_list, get_event_list(sibling)); + else if (sibling->state == PERF_EVENT_STATE_INACTIVE) list_add_tail(&sibling->active_list, get_event_list(sibling)); } @@ -2473,14 +2475,15 @@ 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 +2493,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,16 +2507,23 @@ 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 if (refcount_read(&event->mmap_count)) { + /* + * active events get this through ->del(). + */ + perf_event_update_userpage(event); + } } static void @@ -2520,7 +2531,7 @@ group_sched_out(struct perf_event *group_event, struct perf_event_context *ctx) { struct perf_event *event; - 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); @@ -2702,7 +2713,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); @@ -2716,8 +2727,7 @@ static void __perf_event_disable(struct perf_event *event, group_sched_out(event, ctx); /* - * But only mark the leader OFF; the siblings will remain - * INACTIVE. + * But only mark the leader OFF; the siblings will remain ON. */ __event_disable(event, ctx, PERF_EVENT_STATE_OFF); @@ -2859,7 +2869,7 @@ 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); + perf_event_set_state(event, PERF_EVENT_STATE_ON); event->oncpu = -1; ret = -EAGAIN; goto out; @@ -2886,7 +2896,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); @@ -3250,6 +3260,8 @@ perf_install_in_context(struct perf_event_context *ctx, raw_spin_unlock_irq(&ctx->lock); } +static inline void event_set_inactive(struct perf_event *event); + /* * Cross CPU call to enable a performance event */ @@ -3261,13 +3273,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) @@ -3278,10 +3290,19 @@ static void __perf_event_enable(struct perf_event *event, /* * If the event is in a group and isn't the group leader, - * then don't put it on unless the group is on. - */ - if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) + * then don't put it on unless the group is on. If the leader + * is INACTIVE (waiting for counters), match its state so the + * sibling accrues enabled time with the group. + */ + if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) { + if (leader->state == PERF_EVENT_STATE_INACTIVE) { + /* Avoid PMU interrupts nesting the userpage update. */ + perf_pmu_disable(event->pmu_ctx->pmu); + event_set_inactive(event); + perf_pmu_enable(event->pmu_ctx->pmu); + } return; + } task_ctx = cpuctx->task_ctx; if (ctx->task) @@ -3304,7 +3325,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); @@ -4107,26 +4128,27 @@ static noinline int visit_groups_merge(struct perf_event_context *ctx, * * IOW, we must not miss EVENT_TIME edges. */ -static inline bool event_update_userpage(struct perf_event *event) +static inline void event_set_inactive(struct perf_event *event) { + perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); + if (likely(!refcount_read(&event->mmap_count))) - return false; + return; - perf_event_update_time(event); perf_event_update_userpage(event); - - return true; } -static inline void group_update_userpage(struct perf_event *group_event) +static inline void group_set_inactive(struct perf_event *group_event) { struct perf_event *event; - if (!event_update_userpage(group_event)) - return; + event_set_inactive(group_event); + for_each_sibling_event(event, group_event) { + if (event->state <= PERF_EVENT_STATE_OFF) + continue; - for_each_sibling_event(event, group_event) - event_update_userpage(event); + event_set_inactive(event); + } } struct merge_sched_data { @@ -4154,29 +4176,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); + + event->pmu_ctx->rotate_necessary = 1; + perf_mux_hrtimer_restart(cpc); + + group_set_inactive(event); + list_add_tail(&event->active_list, + &event->pmu_ctx->flexible_active); } return 0; @@ -4702,10 +4733,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 +5063,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; } @@ -9155,7 +9186,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 +9210,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 +13585,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 +14631,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 +15069,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.915.g69d5ec7298-goog