From patchwork Thu Nov 9 08:21:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13450740 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2237CC4167D for ; Thu, 9 Nov 2023 08:22:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=KowWtjCI2ODRj+yGTNYqp9yxOJt1nT2Ad2dSg3fBtQk=; b=BmBNCQ0vNqdsDJ CkzV1gEiGoLtfRo5BB2aAlxHt7peqXzpDWwZ7vyPvj5eJf+ofPgxHXBQGh3fDZWoD0ltIEdRWEpms 7USj5XFtuoGES7rOIsKYrt+gzWg9n57yJK0lLvJxVGkOXk0j3anPSNv5+ZM94JHwYhys2Der3khou spxdmCRp3DP/VvYSBODgR3JEPWB8NmI0awHsZsUJQJlf90qEI2Y6prDNL9RzcAZlj3kMdm1qxFE97 FQoFFrEjWUdwc6pXSKkAC2LLD6paHdF0UhJ+r0CfoPAnRiVKlOMzIwk0kfDhydtS0A6VW2spCrX71 IwHhEWurXlyanOzMIAGQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r10ID-005bAr-15; Thu, 09 Nov 2023 08:21:37 +0000 Received: from mail-wr1-x42a.google.com ([2a00:1450:4864:20::42a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r10I9-005b98-2X for linux-arm-kernel@lists.infradead.org; Thu, 09 Nov 2023 08:21:35 +0000 Received: by mail-wr1-x42a.google.com with SMTP id ffacd0b85a97d-32dff08bbdbso358531f8f.2 for ; Thu, 09 Nov 2023 00:21:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1699518090; x=1700122890; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Zx8fHoMz9t2Gib58EAFChiWLDxqT6xmc1EVOXsB54GM=; b=io32C9xqPm1AfX4x/tqNPFWiLfcqXsRyDy5wSv3lE9x+HnbLllrsSWHKarcxP0Q88y hCj1CRQ+Hzu7Q4fOs19joGnKrV1yn0oHOMe27FuywwakJezfKBFymRCWu6MCXcH/tpza I0mHuyS21Me0wAUi5ark0iJooTbbLDbgl8az7Qpc1CNnk58BumC5uTFCKvANTR0Vtt71 oojMxZd/xSraSkc7Z6+CSDAs1KWiCASo0liFnL2ryIhGtZ+UvneClG6FQQmXCTlZGcZt 4NYzS0KuYj5BiBGoxKWCPhiNY1pnTdcfHqpWegO3Jvc3HWqIZmZf8lBLInIqYK+v5IGZ Xn2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699518090; x=1700122890; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Zx8fHoMz9t2Gib58EAFChiWLDxqT6xmc1EVOXsB54GM=; b=he7pNjflbda0EtOXZ8t/roE5YM94MWHVSGpVocz5YHZ2uIqv02obskRbU90kMN4UHS bZH8k8ecBiypP6IUHiehLt+xdnzFB0J1WVEwIDLmK5CXNcW6QJ3OFLkxdH6W57cxl5yU 2eZ0NUe+qc6ip5Xujd+kwFbTvGEnw90sIcvpxyVtZL/1lbWrWdbiQFLD6cc0lzdGTf/O C5QGJFftLop5XSqJFuzedTTfiMvr+R46WFOcJdehuy3XvF6iV08KEqXV6mf5N0Dgom5A LlMBdxfpg4NWd82O+gx1eAWO+MFaBgyy5f5NURd7dASxapkF6UA2C4xLJvKAoGYdajgJ IpTw== X-Gm-Message-State: AOJu0YzZwfcz3gl0cBxoLesvP+ryR1zVzaoOWo6bffJ3Itcw8DvW8iJE al0wcG65eTrfZ1m6R4Ne61SwbQ== X-Google-Smtp-Source: AGHT+IGniq9nlBb4PH71R4r1mS9EUVv34YGMqQ4t648eQ9pLjugNiZlIlsamHM2xFhcJMWzKwDzDCQ== X-Received: by 2002:a5d:4352:0:b0:32d:bc6e:7f0d with SMTP id u18-20020a5d4352000000b0032dbc6e7f0dmr2702303wrr.18.1699518090397; Thu, 09 Nov 2023 00:21:30 -0800 (PST) Received: from alex-rivos.home (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id m17-20020a056000181100b0032da6f17ffdsm6821506wrh.38.2023.11.09.00.21.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 00:21:30 -0800 (PST) From: Alexandre Ghiti To: Atish Patra , Anup Patel , Will Deacon , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Alexandre Ghiti Subject: [PATCH] drivers: perf: Check find_first_bit() return value Date: Thu, 9 Nov 2023 09:21:28 +0100 Message-Id: <20231109082128.40777-1-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231109_002134_089104_3DDFC422 X-CRM114-Status: GOOD ( 12.48 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org We must check the return value of find_first_bit() before using the return value as an index array since it happens to overflow the array and then panic: [ 107.318430] Kernel BUG [#1] [ 107.319434] CPU: 3 PID: 1238 Comm: kill Tainted: G E 6.6.0-rc6ubuntu-defconfig #2 [ 107.319465] Hardware name: riscv-virtio,qemu (DT) [ 107.319551] epc : pmu_sbi_ovf_handler+0x3a4/0x3ae [ 107.319840] ra : pmu_sbi_ovf_handler+0x52/0x3ae [ 107.319868] epc : ffffffff80a0a77c ra : ffffffff80a0a42a sp : ffffaf83fecda350 [ 107.319884] gp : ffffffff823961a8 tp : ffffaf8083db1dc0 t0 : ffffaf83fecda480 [ 107.319899] t1 : ffffffff80cafe62 t2 : 000000000000ff00 s0 : ffffaf83fecda520 [ 107.319921] s1 : ffffaf83fecda380 a0 : 00000018fca29df0 a1 : ffffffffffffffff [ 107.319936] a2 : 0000000001073734 a3 : 0000000000000004 a4 : 0000000000000000 [ 107.319951] a5 : 0000000000000040 a6 : 000000001d1c8774 a7 : 0000000000504d55 [ 107.319965] s2 : ffffffff82451f10 s3 : ffffffff82724e70 s4 : 000000000000003f [ 107.319980] s5 : 0000000000000011 s6 : ffffaf8083db27c0 s7 : 0000000000000000 [ 107.319995] s8 : 0000000000000001 s9 : 00007fffb45d6558 s10: 00007fffb45d81a0 [ 107.320009] s11: ffffaf7ffff60000 t3 : 0000000000000004 t4 : 0000000000000000 [ 107.320023] t5 : ffffaf7f80000000 t6 : ffffaf8000000000 [ 107.320037] status: 0000000200000100 badaddr: 0000000000000000 cause: 0000000000000003 [ 107.320081] [] pmu_sbi_ovf_handler+0x3a4/0x3ae [ 107.320112] [] handle_percpu_devid_irq+0x9e/0x1a0 [ 107.320131] [] generic_handle_domain_irq+0x28/0x36 [ 107.320148] [] riscv_intc_irq+0x36/0x4e [ 107.320166] [] handle_riscv_irq+0x54/0x86 [ 107.320189] [] do_irq+0x64/0x96 [ 107.320271] Code: 85a6 855e b097 ff7f 80e7 9220 b709 9002 4501 bbd9 (9002) 6097 [ 107.320585] ---[ end trace 0000000000000000 ]--- [ 107.320704] Kernel panic - not syncing: Fatal exception in interrupt [ 107.320775] SMP: stopping secondary CPUs [ 107.321219] Kernel Offset: 0x0 from 0xffffffff80000000 [ 107.333051] ---[ end Kernel panic - not syncing: Fatal exception in interrupt ]--- Fixes: 4905ec2fb7e6 ("RISC-V: Add sscofpmf extension support") Signed-off-by: Alexandre Ghiti Reviewed-by: Atish Patra --- 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 439da49dd0a9..93d729213140 100644 --- a/drivers/perf/riscv_pmu_sbi.c +++ b/drivers/perf/riscv_pmu_sbi.c @@ -687,6 +687,11 @@ static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev) /* Firmware counter don't support overflow yet */ fidx = find_first_bit(cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS); + if (fidx == RISCV_MAX_COUNTERS) { + csr_clear(CSR_SIP, BIT(riscv_pmu_irq_num)); + return IRQ_NONE; + } + event = cpu_hw_evt->events[fidx]; if (!event) { csr_clear(CSR_SIP, BIT(riscv_pmu_irq_num));