From nobody Thu Feb 12 03:19:46 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 16963C77B60 for ; Fri, 28 Apr 2023 11:03:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345729AbjD1LDy (ORCPT ); Fri, 28 Apr 2023 07:03:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47074 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345422AbjD1LDu (ORCPT ); Fri, 28 Apr 2023 07:03:50 -0400 Received: from mta-01.yadro.com (mta-02.yadro.com [89.207.88.252]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A92BE5FE7 for ; Fri, 28 Apr 2023 04:03:19 -0700 (PDT) Received: from mta-01.yadro.com (localhost.localdomain [127.0.0.1]) by mta-01.yadro.com (Proxmox) with ESMTP id 13BE1342206; Fri, 28 Apr 2023 14:03:18 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yadro.com; h=cc :cc:content-transfer-encoding:content-type:content-type:date :from:from:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=mta-01; bh=2ARaWPcFnQtQOS5X6+ 4cxKxG4soHJDao6hU1X26hpVU=; b=E6MC7JcPQ+Ena6ZEkWHLQWzR01F0TXIBw5 8AfJxkDKItd90h0SCFbIOiDPd2USvGTL+sbRRDwLOmMJtJbDu0DEzWNDG7piU4WU sG9BCq2bBrqK8Zrt4h7RAKxeIL/+30pyERCx+uDzkON/eb+AIWJchW6qghfpFocO 3BUH1hkkA= Received: from T-EXCH-07.corp.yadro.com (unknown [172.17.10.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mta-01.yadro.com (Proxmox) with ESMTPS id 090153421FF; Fri, 28 Apr 2023 14:03:18 +0300 (MSK) Received: from T-Exch-05.corp.yadro.com (172.17.10.109) by T-EXCH-07.corp.yadro.com (172.17.11.57) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.2.1118.9; Fri, 28 Apr 2023 14:03:17 +0300 Received: from rdma-x86-2.dev.yadro.com (172.17.27.123) by T-Exch-05.corp.yadro.com (172.17.10.109) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.2.1118.9; Fri, 28 Apr 2023 14:03:17 +0300 From: Viacheslav Mitrofanov CC: , Viacheslav Mitrofanov , "Atish Patra" , Anup Patel , Will Deacon , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Albert Ou , , , Subject: [PATCH 1/1] perf: RISC-V: Limit the number of counters returned from SBI. Date: Fri, 28 Apr 2023 11:02:56 +0000 Message-ID: <20230428110256.711352-2-v.v.mitrofanov@yadro.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230428110256.711352-1-v.v.mitrofanov@yadro.com> References: <20230428110256.711352-1-v.v.mitrofanov@yadro.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [172.17.27.123] X-ClientProxiedBy: T-EXCH-01.corp.yadro.com (172.17.10.101) To T-Exch-05.corp.yadro.com (172.17.10.109) To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Perf gets the number of supported counters from SBI. If it happens that the number of returned counters more than RISCV_MAX_COUNTERS the code trusts it. It does not lead to an immediate problem but can potentially lead to it. Prevent getting more than RISCV_MAX_COUNTERS from SBI. Signed-off-by: Viacheslav Mitrofanov Reviewed-by: Andrew Jones --- drivers/perf/riscv_pmu_sbi.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c index 70cb50fd41c2..0183bf911bfb 100644 --- a/drivers/perf/riscv_pmu_sbi.c +++ b/drivers/perf/riscv_pmu_sbi.c @@ -867,6 +867,11 @@ static int pmu_sbi_device_probe(struct platform_device= *pdev) pr_err("SBI PMU extension doesn't provide any counters\n"); goto out_free; } + /* It is possible to get from SBI more than max number of counters */ + if (num_counters > RISCV_MAX_COUNTERS) { + pr_warn("SBI returned more than maximum number of counters\n"); + num_counters =3D RISCV_MAX_COUNTERS; + } =20 /* cache all the information about counters now */ if (pmu_sbi_get_ctrinfo(num_counters, &cmask)) --=20 2.37.2