From patchwork Thu Apr 25 12:46:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junhao He X-Patchwork-Id: 13643288 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 9882CC4345F for ; Thu, 25 Apr 2024 12:49:35 +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:References:In-Reply-To: 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: List-Owner; bh=rav/ZcWaNhgfVOeWow8FViUD8rMB9C5e1wxwhhmSqeQ=; b=Gn2kNXG/nJhIX8 ha7rQjAaWM5TmnixvGAWYPQtToIznESuWA5nHDFFJA+leiysPUvDNj8pCIvxlkjK/fU2CWTrdtUuw /m6mqyNXhMl6XnzBB3BW38822phiBzWOk14pZOVjZNtnXe2x3Ykr1Lt1sdGahEn2d/cGwUdhU1Za2 AgJ+LLGrxdM6HRSWXKz+QFLikMQc2ahf9nseSH4x1ym70ubmbqBD9nwABm/dHsvhlqBXahwyc5mze ze2ReMp3nps+T4AiAhvdAbD/JWtgjVwMJ12KtoAMUQvT586GsVTDVylYsC4p8ZMHKSFQExR6m/bO9 WvAu7nhX6sEZvOo+t7wA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzyXP-00000008IxO-2cwq; Thu, 25 Apr 2024 12:49:19 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzyXH-00000008IrX-2b3M for linux-arm-kernel@bombadil.infradead.org; Thu, 25 Apr 2024 12:49:11 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Content-Transfer-Encoding :MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From: Sender:Reply-To:Content-ID:Content-Description; bh=InL+DnMbkcpJDpPVU5sRM0o8vBGkgJ7D3hGbgXWgZ+Q=; b=JKgrGYimtAt5wMTxz6re14PdmG o0i73KK5KM2ABuz/6HHfs0jMZtcPiW2oslSJ7h7+MVyiLccs2k7/Httv+x/eDhbQ2/zjO8Hcv0CZ3 YJygl6GrBtb7Kc18I3b4NdbypgGcSYXpJ2DKS4dLncSB94iu+y/SxZW+MhV2cJ5bLM2Ij5LDOCWri u2L3zXxjCjoGmzPenH18LpbOd/oKzdaQCh83TWzHPZOBlFRbzpGF4AT43d+HuvH9bsMOnqwRtWuQJ ipnzboBJiwfl1eQnukhCqhiCVNkMWQlwYmZz+r6Gs6Ztrnpp+o1ij128EIAWLXx+RTPmCSqAms+db Vu74RZkw==; Received: from szxga01-in.huawei.com ([45.249.212.187]) by desiato.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzyXE-0000000EsN2-1qGN for linux-arm-kernel@lists.infradead.org; Thu, 25 Apr 2024 12:49:10 +0000 Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VQFvH16Y8zwTrt; Thu, 25 Apr 2024 20:45:31 +0800 (CST) Received: from dggpeml500002.china.huawei.com (unknown [7.185.36.158]) by mail.maildlp.com (Postfix) with ESMTPS id 21A23140258; Thu, 25 Apr 2024 20:48:43 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpeml500002.china.huawei.com (7.185.36.158) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Thu, 25 Apr 2024 20:48:42 +0800 From: Junhao He To: , , , CC: , , , , , Subject: [PATCH 1/3] drivers/perf: hisi_pcie: Fix out-of-bound access when valid event group Date: Thu, 25 Apr 2024 20:46:25 +0800 Message-ID: <20240425124627.13764-2-hejunhao3@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240425124627.13764-1-hejunhao3@huawei.com> References: <20240425124627.13764-1-hejunhao3@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpeml500002.china.huawei.com (7.185.36.158) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240425_134908_882103_3C8240A9 X-CRM114-Status: GOOD ( 17.81 ) 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 The perf tool allows users to create event groups through following cmd [1], but the driver does not check whether the array index is out of bounds when writing data to the event_group array. If the number of events in an event_group is greater than HISI_PCIE_MAX_COUNTERS, the memory write overflow of event_group array occurs. Add array index check to fix the possible array out of bounds violation, and return directly when write new events are written to array bounds. There are 9 different events in an event_group. [1] perf stat -e '{pmu/event1/, ... ,pmu/event9/}' Fixes: 8404b0fbc7fb ("drivers/perf: hisi: Add driver for HiSilicon PCIe PMU") Signed-off-by: Junhao He Reviewed-by: Jijie Shao Acked-by: Jonathan Cameron --- drivers/perf/hisilicon/hisi_pcie_pmu.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/drivers/perf/hisilicon/hisi_pcie_pmu.c b/drivers/perf/hisilicon/hisi_pcie_pmu.c index 5d1f0e9fdb08..dba399125658 100644 --- a/drivers/perf/hisilicon/hisi_pcie_pmu.c +++ b/drivers/perf/hisilicon/hisi_pcie_pmu.c @@ -350,15 +350,27 @@ static bool hisi_pcie_pmu_validate_event_group(struct perf_event *event) return false; for (num = 0; num < counters; num++) { + /* + * If we find a related event, then it's a valid group + * since we don't need to allocate a new counter for it. + */ if (hisi_pcie_pmu_cmp_event(event_group[num], sibling)) break; } + /* + * Otherwise it's a new event but if there's no available counter, + * fail the check since we cannot schedule all the events in + * the group simultaneously. + */ + if (num == HISI_PCIE_MAX_COUNTERS) + return false; + if (num == counters) event_group[counters++] = sibling; } - return counters <= HISI_PCIE_MAX_COUNTERS; + return true; } static int hisi_pcie_pmu_event_init(struct perf_event *event) From patchwork Thu Apr 25 12:46:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junhao He X-Patchwork-Id: 13643286 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 45C63C4345F for ; Thu, 25 Apr 2024 12:49:16 +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:References:In-Reply-To: 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: List-Owner; bh=LNBDpA5JymaaWQK6QzYNyvczO4Fi8CLFmjBR5e9sHBI=; b=pmBUAOWpz01FGL /V4nNi9GKhjuSeax61xQJneSqjXnpTkYkx3aG0IT5ZOIhncrrCDhGabUhabEyuXJl6PIiCQYrWE0v AYkypQ2JThXrTmYof3MH9Gi32vtNz7ZWtfkscAtcQL2cUcNNVlp8O196VXlUbLfkwtpNQeEVHGDjt CWEd3YdSvR+gEVPWtkwJhYwfDF4T/eje14DM5IbNFPBRLj/AnuFb2swCl9YJtkK6mc+MnnRDY74fI qxr9hS510F2qhJfQ/NKcJgQtL+/71xJWB4eyEvoy04KEwB5yHuxH1l5uq4o7ZKrMLJVrU4yu6WuHw BssJPM7cB+PEqWCzUzXw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzyX5-00000008ImA-1uzF; Thu, 25 Apr 2024 12:48:59 +0000 Received: from szxga02-in.huawei.com ([45.249.212.188]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzyX2-00000008Ij3-0d87 for linux-arm-kernel@lists.infradead.org; Thu, 25 Apr 2024 12:48:57 +0000 Received: from mail.maildlp.com (unknown [172.19.88.105]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VQFxk1HNzzcc1t; Thu, 25 Apr 2024 20:47:38 +0800 (CST) Received: from dggpeml500002.china.huawei.com (unknown [7.185.36.158]) by mail.maildlp.com (Postfix) with ESMTPS id 493AA1401E9; Thu, 25 Apr 2024 20:48:43 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpeml500002.china.huawei.com (7.185.36.158) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Thu, 25 Apr 2024 20:48:43 +0800 From: Junhao He To: , , , CC: , , , , , Subject: [PATCH 2/3] drivers/perf: hisi: hns3: Fix out-of-bound access when valid event group Date: Thu, 25 Apr 2024 20:46:26 +0800 Message-ID: <20240425124627.13764-3-hejunhao3@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240425124627.13764-1-hejunhao3@huawei.com> References: <20240425124627.13764-1-hejunhao3@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpeml500002.china.huawei.com (7.185.36.158) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240425_054856_378904_3CA02971 X-CRM114-Status: GOOD ( 17.31 ) 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 The perf tool allows users to create event groups through following cmd [1], but the driver does not check whether the array index is out of bounds when writing data to the event_group array. If the number of events in an event_group is greater than HNS3_PMU_MAX_HW_EVENTS, the memory write overflow of event_group array occurs. Add array index check to fix the possible array out of bounds violation, and return directly when write new events are written to array bounds. There are 9 different events in an event_group. [1] perf stat -e '{pmu/event1/, ... ,pmu/event9/} Fixes: 66637ab137b4 ("drivers/perf: hisi: add driver for HNS3 PMU") Signed-off-by: Junhao He Signed-off-by: Hao Chen Reviewed-by: Jijie Shao Acked-by: Jonathan Cameron --- drivers/perf/hisilicon/hns3_pmu.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/drivers/perf/hisilicon/hns3_pmu.c b/drivers/perf/hisilicon/hns3_pmu.c index 16869bf5bf4c..cbdd53b0a034 100644 --- a/drivers/perf/hisilicon/hns3_pmu.c +++ b/drivers/perf/hisilicon/hns3_pmu.c @@ -1085,15 +1085,27 @@ static bool hns3_pmu_validate_event_group(struct perf_event *event) return false; for (num = 0; num < counters; num++) { + /* + * If we find a related event, then it's a valid group + * since we don't need to allocate a new counter for it. + */ if (hns3_pmu_cmp_event(event_group[num], sibling)) break; } + /* + * Otherwise it's a new event but if there's no available counter, + * fail the check since we cannot schedule all the events in + * the group simultaneously. + */ + if (num == HNS3_PMU_MAX_HW_EVENTS) + return false; + if (num == counters) event_group[counters++] = sibling; } - return counters <= HNS3_PMU_MAX_HW_EVENTS; + return true; } static u32 hns3_pmu_get_filter_condition(struct perf_event *event) From patchwork Thu Apr 25 12:46:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junhao He X-Patchwork-Id: 13643287 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 73DC0C4345F for ; Thu, 25 Apr 2024 12:49:30 +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:References:In-Reply-To: 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: List-Owner; bh=pyHYlhc7T+3H0P0i7QCFSxp6kt7GuU5Otg0KRe4XRUo=; b=xz+OUROXUncIof /4/bTlKTk0ZaBbd1OAluoQIYTefoA+SK18eu4Xa1mp1sgmnrdgR9gVRQLxLxC6E6uKM+uPrI5hE8P 18R3wIzpQonHqXG8fmnSu++EQvYwD6aSHu1CB92dXitLauZHLvkmygscuZi2gutJCDszlhKrXki2w bsxCTrJxvPgGJPhFJcyTRYqIhQAPYyNEwyOrUSSc8WVZWZJVso1blHcKa7e11IHrLDr74uVD0/bPN 0h3NP5L/LqpoCmSfofTGlRF2euxP6WgFW51w2HGbci53+jkEL+2yWteJjB5EKWV67LlP7ENhh2vUJ xbc7OPnGqA4h2cLjW5qA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzyXO-00000008Iwv-3ugt; Thu, 25 Apr 2024 12:49:18 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzyXF-00000008Iqh-32Us for linux-arm-kernel@bombadil.infradead.org; Thu, 25 Apr 2024 12:49:09 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Content-Transfer-Encoding :MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From: Sender:Reply-To:Content-ID:Content-Description; bh=g8A/lrMUgUBxL40xEMC2iS5a4RC8Y8TIZdvFa4f+V0o=; b=TbMnVzianRUgBHr0vW8Fz743NZ pIoXyEAlg/5ykQ6nGcdLdwOO2EOxiG4UX1gQi79+KtH2poE4wCbRbWLV+0ZBgGFXn34gaZZSUW2Xg lr1DPolG8mRg/oTSH9h44sTn/9iIxWOQRT3ne6fzgA54uNVgHhmLc52ZAjrQYehZyCr2jCsk3LIxp EmdKbSVUcB1nVKDmcNyFiJ9Y9/H9QC09eeyXvKK+XiGSukrhIOrDw6jk6op6L2zsFl09gmsgr5R+Y bZFEdb/GDc0jlSoI0BFhjnxrOrj5awN1NIIgsdGW65UD5TU3GXIb1vaHt6JlTBIGnVwLyrsTppIHv dFgHwLJA==; Received: from szxga02-in.huawei.com ([45.249.212.188]) by desiato.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzyXC-0000000EsN1-0rJk for linux-arm-kernel@lists.infradead.org; Thu, 25 Apr 2024 12:49:08 +0000 Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VQFtw2VTJzXmWW; Thu, 25 Apr 2024 20:45:12 +0800 (CST) Received: from dggpeml500002.china.huawei.com (unknown [7.185.36.158]) by mail.maildlp.com (Postfix) with ESMTPS id 74F1018007F; Thu, 25 Apr 2024 20:48:43 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpeml500002.china.huawei.com (7.185.36.158) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Thu, 25 Apr 2024 20:48:43 +0800 From: Junhao He To: , , , CC: , , , , , Subject: [PATCH 3/3] drivers/perf: hisi: hns3: Actually use devm_add_action_or_reset() Date: Thu, 25 Apr 2024 20:46:27 +0800 Message-ID: <20240425124627.13764-4-hejunhao3@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240425124627.13764-1-hejunhao3@huawei.com> References: <20240425124627.13764-1-hejunhao3@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpeml500002.china.huawei.com (7.185.36.158) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240425_134906_708144_90C627D4 X-CRM114-Status: GOOD ( 11.27 ) 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 From: Hao Chen pci_alloc_irq_vectors() allocates an irq vector. When devm_add_action() fails, the irq vector is not freed, which leads to a memory leak. Replace the devm_add_action with devm_add_action_or_reset to ensure the irq vector can be destroyed when it fails. Fixes: 66637ab137b4 ("drivers/perf: hisi: add driver for HNS3 PMU") Signed-off-by: Hao Chen Signed-off-by: Junhao He Reviewed-by: Jijie Shao Acked-by: Jonathan Cameron --- drivers/perf/hisilicon/hns3_pmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/perf/hisilicon/hns3_pmu.c b/drivers/perf/hisilicon/hns3_pmu.c index cbdd53b0a034..60062eaa342a 100644 --- a/drivers/perf/hisilicon/hns3_pmu.c +++ b/drivers/perf/hisilicon/hns3_pmu.c @@ -1527,7 +1527,7 @@ static int hns3_pmu_irq_register(struct pci_dev *pdev, return ret; } - ret = devm_add_action(&pdev->dev, hns3_pmu_free_irq, pdev); + ret = devm_add_action_or_reset(&pdev->dev, hns3_pmu_free_irq, pdev); if (ret) { pci_err(pdev, "failed to add free irq action, ret = %d.\n", ret); return ret;