From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7C5F5606AA83 for ; Thu, 19 Nov 2026 18:53:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=sZS5emFv; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=pyVk6f8+ DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=pLq92bALDfzVSwPCFwlrCOZ9rlSOsAmXjgSBQp7hHGc=; b=sZS5emFvptiwLTsoX2z2dHusZj Lhtn4g/FRF2RFlzwxRk2Jfd2v7kQHKRMSUCILtaDZ2W9Y669unK9BDEPDv1AWHYogielbrnZ7LEH5 GMDu28ZLA1xW1jNRbGK2jR1FJgkK+CW8vEVJ+MnAiNVb3tba+PjiwzqkRuDou9n1OkzuJT9A6mSoU 6bs294A8er7zhT3ZrtGCg+xt4060A3ogsomOJFbn1/0g1ygOSxBDZvuCq5/iUOXnxlv0s4ZA/V/ke W7nHrImhZ/+plmuQ9BXEMriFv+KnanrHphc83qKf7cYiCvwta6HftJREu/Nk+QlDb6t8QA+WbBEOT QsfD8yNQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xU7FN-0000000D43l-26DF; Thu, 19 Nov 2026 18:52:37 +0000 Received: from mail-oi1-x246.google.com ([2607:f8b0:4864:20::246]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xU7FB-0000000D3qt-3EjP for linux-arm-kernel@lists.infradead.org; Thu, 19 Nov 2026 18:52:26 +0000 Received: by mail-oi1-x246.google.com with SMTP id 5614622812f47-4e778acdbc6so171525b6e.3 for ; Thu, 19 Nov 2026 10:52:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795114345; x=1795719145; darn=lists.infradead.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=pLq92bALDfzVSwPCFwlrCOZ9rlSOsAmXjgSBQp7hHGc=; b=pyVk6f8+jUSJjDR/+rSEXd0hj5N4L2PunLF0AxQGvtRN54TFkpLsoe5ZEAAgC5ACzr iaSRKeX+4deCVmf1R5SiA4CrXkDBHTcsZot+LPDLLJDgRRgsKaSpg1dJEdsH6WGit6nJ HW77eRcTwhOVBAA79BKCeu4LnDmPxmtKa732wdo0ck81WD8u62BvE/TxZ4A6RXhSyl8u C393wyToL221M7557uIrnlIm77nht9tdaxcOKX9saJHHozjAyV2c534LnZdBv7oD+BVy odZ9SkP9yySCit2xHCUM1iky+bO4u98zN83gemnFrd6s8deL0aZuROGNQtGhsT3Wka0C oEzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795114345; x=1795719145; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=pLq92bALDfzVSwPCFwlrCOZ9rlSOsAmXjgSBQp7hHGc=; b=PLDWm0m/yUTtfVM8stJ7+6E9YduzOq81ESk5xxwDeGFPiGf1BbG/CLukl9nfUbK31H V6FVfBiQDydV966R+CtCvTFL6DUxbNPydOeW9Y2AQZlFhHLnM/CAsFYYaZl14p/q82zm RaCpD0t6icKn1OG6SY9fPO0rZcFprT4ThgasYHA43j3W3h+HAw2tzhIH4yQqPenEH0iQ 8Rm9AAGQ+dOmx740JRfqOSHwnkgVhRcB9XF2zNFe4f2iOq7YI78k4HOQpTraynzQA4v3 s37KFylPTDexJ1BKB2LH8kmP7Djl3diX4g6TSXRxT8QGRctR+DlaQEA29uEOYo2mIJuU zhAw== X-Forwarded-Encrypted: i=1; AL+hErGqzIUl1YmVxJer/wEr2e7B6GCoCD6nsVYFzsiffsUKyUy2oeDOdr+MnhG/WQgMDvpbA15zd8ZFEksDI3iUux9r@lists.infradead.org X-Gm-Message-State: APuU4Z+knLHpbrOWTUXH7j0koxpix0HokKRZ/M++n+stVnnEgZre8rvQ pY8ruHsy9d3kiGM2TUIC7FKolqI7QQFSwkkmGKgvC6Mli1hyA1Y4pGHX6sY20oJ4KC0D5IftnKd k8YA7YKWVQ7ZKrPzN3TV0TVcosA== X-Received: from ioo10-n2.prod.google.com ([2002:a05:6602:864a:20b0:9c7:8645:9285]) (user=coltonlewis job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:178b:b0:55c:8ed:4db8 with SMTP id 5614622812f47-57a0cbda8b5mr953892b6e.62.1795114344372; Thu, 19 Nov 2026 10:52:24 -0800 (PST) Date: Thu, 19 Nov 2026 18:51:51 +0000 In-Reply-To: <20261119185215.632334-1-coltonlewis@google.com> X-Archived-By: Mime-Version: 1.0 References: <20261119185215.632334-1-coltonlewis@google.com> X-Mailer: git-send-email 2.56.1.809.ga0b61035b5-goog Message-ID: <20261119185215.632334-10-coltonlewis@google.com> Subject: [PATCH v10 09/33] perf: arm_pmuv3: Allocate counter indices from high to low From: Colton Lewis To: Marc Zyngier , Oliver Upton Cc: Catalin Marinas , Will Deacon , Mark Rutland , Peter Zijlstra , Ingo Molnar , Paolo Bonzini , Russell King , Jonathan Corbet , Fuad Tabba , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Shuah Khan , James Clark , Alexandru Elisei , Mingwei Zhang , Ganapatrao Kulkarni , kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261119_105225_833985_8D22F449 X-CRM114-Status: GOOD ( 15.45 ) X-Spam-Score: -9.5 (---------) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-26.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIMWL_WL_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 MDCR_EL2.HPMN assigns low-index general-purpose counters (0..HPMN-1) to the guest and high-index counters (HPMN..N-1) to the host. Allocate host event counters from high index to low index to minimize collisions with guest counter partitions and avoid rescheduling host perf events on vCPU load when enough upper counters are free. When allocating a 64-bit chained pair (even counter idx - 1 paired with odd counter idx), verify that both idx and idx - 1 are present in cpuc->cntr_mask so an odd HPMN boundary cannot allocate a guest-owned even counter as the lower half of a host chained event. Tested-by: James Clark Signed-off-by: Colton Lewis --- drivers/perf/arm_pmuv3.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/drivers/perf/arm_pmuv3.c b/drivers/perf/arm_pmuv3.c index 9ce915754b0a5..667ce27d2347c 100644 --- a/drivers/perf/arm_pmuv3.c +++ b/drivers/perf/arm_pmuv3.c @@ -957,10 +957,12 @@ static int armv8pmu_get_single_idx(struct pmu_hw_events *cpuc) { int idx; - for_each_set_bit(idx, cpuc->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS) { - if (!test_and_set_bit(idx, cpuc->used_mask)) + for (idx = ARMV8_PMU_MAX_GENERAL_COUNTERS - 1; idx >= 0; idx--) { + if (test_bit(idx, cpuc->cntr_mask) && + !test_and_set_bit(idx, cpuc->used_mask)) return idx; } + return -EAGAIN; } @@ -972,17 +974,22 @@ static int armv8pmu_get_chain_idx(struct pmu_hw_events *cpuc) * Chaining requires two consecutive event counters, where * the lower idx must be even. */ - for_each_set_bit(idx, cpuc->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS) { + for (idx = ARMV8_PMU_MAX_GENERAL_COUNTERS - 1; idx >= 0; idx--) { if (!(idx & 0x1)) continue; - if (!test_and_set_bit(idx, cpuc->used_mask)) { - /* Check if the preceding even counter is available */ - if (!test_and_set_bit(idx - 1, cpuc->used_mask)) - return idx; - /* Release the Odd counter */ - clear_bit(idx, cpuc->used_mask); + + if (test_bit(idx, cpuc->cntr_mask) && + test_bit(idx - 1, cpuc->cntr_mask)) { + if (!test_and_set_bit(idx, cpuc->used_mask)) { + /* Check if the preceding even counter is available */ + if (!test_and_set_bit(idx - 1, cpuc->used_mask)) + return idx; + /* Release the Odd counter */ + clear_bit(idx, cpuc->used_mask); + } } } + return -EAGAIN; } -- 2.56.1.708.g77570a6a1d-goog