From patchwork Thu Oct 26 14:29:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shanker Donthineni X-Patchwork-Id: 13437613 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 90926C25B67 for ; Thu, 26 Oct 2023 14:30:34 +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=SoBPDiE20CTKZFtplX/IoizkiycGASOIrIlUn1KHBzU=; b=WBWEkW0cn4EhqG rvI1uWzocQes3WqvotwfdQiSCZYbb3mgE4cV3bFafzdJiWXLIprCzWAUOhSrGkm+KvxQOfkVgX/6H PDcdYqKgzEG3k8D6fTUD1epIIEeN7p+iVw002rvNje2vaqpXSnuoP9iJOATARSug5sWB8UbZR50qa CfdMk3eBTi75EhrQeBEIEPeyVDj9vfxts0E9gccg33qkeIgPOjKvsxBTDTjJZsl1Dq2GiijkMFxnp R0uYPfcYdcv+RqIvDVxYGJyWBFfvvgl93nA+GWNe8EBimbs2r3vKu2ZHt4ZZEOmqneCTx5AdfQmg2 CtlUU6Zi2B3UuYdyHw6g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qw1NA-00Eg5i-1X; Thu, 26 Oct 2023 14:30:08 +0000 Received: from mail-dm6nam10on20607.outbound.protection.outlook.com ([2a01:111:f400:7e88::607] helo=NAM10-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qw1N7-00Eg48-2t for linux-arm-kernel@lists.infradead.org; Thu, 26 Oct 2023 14:30:07 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ItNun+G3BzlsZkxb/748FZVhW3QpwWt35+DrDwWrz4XNPmh6+NXz6D5g3uhvWTzfVYRbYu7n5ne/G3YAPx50LFLmU0IF3jY+nCgdm6MG2eC7eGybnNQYVe1EjfSRD4+Y4vpRBxhpj+TWuWPwIbLUawnUkd9Ihacrx7WFqLU6w7Uo6MWt4xm9p3gbVwdKCv98SNSEx56YMeDTvCYKOdACLdL915erp5lErRZXip61snLes5EkadeGfBjRYUhrUgjjT3RzjZ9a1Mky0siaNV5BoyK87WV27SFnLKFiFP8t3e2VuQ3J4Tq/1QocxjDIvqtEpO1vnVVMPxAI2cJdTuXgNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Z8vZomnN5rNqqbKECQcCaybDFuv7aqiwNix0/nAe+UA=; b=LHJWLSQyewlJhUyFKIpSKpC58mkvlNI/+ASKvoI38XEcUX8YWpi5a7S2tdsZsm6LJymr7uc84eUpRF5jFnmWwAjge7iv0nDgqe6n4sNoahYIGM+pcCRxER5b+FbI/ac8wctytyr+2DL75D5XE2P3TDl3Qtyos/QOpV2yV5eU5HgSQ5XuCEt3ukH1Rg/XhbdCbPBJVK8iRrnd1itWiM/NU08B1a8VqSZ7XAiR3HpcVjEsLvDIC1LnecBMEqMfy1jKuR00Ga1529p3B9Nc8ZeJS53DtGTXNiRmFz+SO2kItFLry2TQNTLSE7n+iwNCJwAX/5hFrm/UnjCSzGEULEWDhw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=arm.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Z8vZomnN5rNqqbKECQcCaybDFuv7aqiwNix0/nAe+UA=; b=XeQuyZN2PKiX+uyvCB2pGgNDFVnIl3lPiHMSpxcSYtJD5iWjpsrDo5i1v74n/Vv45l8L5KOstj6mIold+9yRKfalQP16BmRWr6MLrQP4SAm4XhbcRkGuUVwku6DUeeX5rx51YC+U0SzQoGtYVnHdugz8htonngHq3DihjaZJG7jb2pnPcpFX2i7nvnRwZPXfgFAMtiaaMjiaQ2Ek87B0KORw+S2Gu6TNJMc7PLVc8AQiPilAh5dz/fi+B+mHhfSKFwv/s+ScN54R4xL/naBWHNWq+b5cMxqgz7HiKesVBK2ZEa9Khl65JbVCdK8QUmtnhf4KpnnAVTdXxTgQZRrFQQ== Received: from CH0P221CA0021.NAMP221.PROD.OUTLOOK.COM (2603:10b6:610:11c::17) by DM4PR12MB6325.namprd12.prod.outlook.com (2603:10b6:8:a4::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6933.19; Thu, 26 Oct 2023 14:29:59 +0000 Received: from SN1PEPF0002BA50.namprd03.prod.outlook.com (2603:10b6:610:11c:cafe::2e) by CH0P221CA0021.outlook.office365.com (2603:10b6:610:11c::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6933.22 via Frontend Transport; Thu, 26 Oct 2023 14:29:59 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by SN1PEPF0002BA50.mail.protection.outlook.com (10.167.242.73) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6933.15 via Frontend Transport; Thu, 26 Oct 2023 14:29:58 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 26 Oct 2023 07:29:36 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 26 Oct 2023 07:29:35 -0700 Received: from SDONTHINENI-DESKTOP.nvidia.com (10.127.8.12) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.986.41 via Frontend Transport; Thu, 26 Oct 2023 07:29:35 -0700 From: Shanker Donthineni To: James Morse CC: Catalin Marinas , Mark Rutland , Will Deacon , Shanker Donthineni , , "Rohit Mathew" , Vikram Sethi Subject: [PATCH 1/1] drivers/perf: Fix kernel panic due to the invalid mon_ctx pointer Date: Thu, 26 Oct 2023 09:29:30 -0500 Message-ID: <20231026142930.2670705-1-sdonthineni@nvidia.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-NVConfidentiality: public X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SN1PEPF0002BA50:EE_|DM4PR12MB6325:EE_ X-MS-Office365-Filtering-Correlation-Id: 3b3623eb-b889-4271-fdc2-08dbd6300847 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: AsYGM6SRVAFO//4M3p/9nr+FY4B/VgT1uRJ4w+89T72/DEzPKqzUDx2EMZ1IHPlLa/cxUzMM6A1qbaLJLb5ZDoU/ygRfcdhiTorySx3RVhmbytglXDJb0l2YDFC3HVyy+HuIF8rsQcoYPYFf6wzEFrjLcoaTaNR+jaU5HaDS/ieppdYlgT3T0EO2Nc3XK2LYwVv0FgqBdB7Anw5Py15MT0oSecLFtZqw3Bcs2w5B/PnR9AIbeNlcqN/EnGkHVlvD4a0d4iC+8RCGStlUZlhUextd8IRsjCfdB3sU19p2INAsRi0cWTSbBA7ZPVnqs/anUHqoBmuvPRU8TeZLeejnzxqxWA9bivqALjNIipi8h3D2c2cI4jTYDW6q7owGzFoAvf/GQZsqFw7p7RNoMsTiOdg51ZCGfVt/UR8lHjPcdBLvbNK5CY/NIbXeOvqehkZKUajg8162slMuZ3THGV+NIhxz1hxPur/Q1jZo1/e/UIpEKQsAfkc11ECxfTzBbEBGMrG6M8OEO+x7H1WkbM47FOjjLLSiWLcYOw0yky90QB4G+X4Sy7fIRJ9sLCcTgo9Fr376axuJwf8aAGnDqWbabCApN6Ha0QoQItSOFNVS7caY4IakfTRQ03N0taDoKg9kaeb/Jtw3SIX2tc8K2QatXz9weGSff2xG3YuPdNGojV1PKcY3LX2+NE7PKCmj47fpado+L6S7Mkg8/Jk4rbAEc/CiGiXtwIt7V+fa6oCQl/exc0fiY+shACh/q8xJItJu X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230031)(4636009)(376002)(39860400002)(396003)(346002)(136003)(230922051799003)(186009)(64100799003)(82310400011)(451199024)(1800799009)(46966006)(40470700004)(36840700001)(26005)(336012)(426003)(107886003)(1076003)(2616005)(47076005)(82740400003)(356005)(7636003)(86362001)(36756003)(36860700001)(83380400001)(40480700001)(40460700003)(70586007)(70206006)(8676002)(4326008)(6916009)(8936002)(54906003)(41300700001)(5660300002)(2906002)(316002)(7696005)(6666004)(478600001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Oct 2023 14:29:58.7360 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3b3623eb-b889-4271-fdc2-08dbd6300847 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: SN1PEPF0002BA50.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6325 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231026_073006_085541_4E45408C X-CRM114-Status: GOOD ( 14.16 ) 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 return pointer from the resctrl_arch_mon_ctx_alloc_no_wait() function is saved in a 32-bit variable 'hwc->idx,' which results in the loss of the upper 32 bits. This, in turn, triggers a kernel panic when attempting to access a corrupted pointer. This patch addresses the issue by utilizing the IDR data structure to keep track of a 32-bit value associated with a pointer (64-bit value). Signed-off-by: Shanker Donthineni --- drivers/perf/resctrl_pmu.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/drivers/perf/resctrl_pmu.c b/drivers/perf/resctrl_pmu.c index 99a2b90b5d83..68cda619da48 100644 --- a/drivers/perf/resctrl_pmu.c +++ b/drivers/perf/resctrl_pmu.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include struct resctrl_pmu { struct pmu pmu; @@ -25,6 +27,9 @@ RESCTRL_PMU_EVENT_ATTR_EXTRACTOR(event, config, 0, 7); RESCTRL_PMU_EVENT_ATTR_EXTRACTOR(domain, config, 16, 23); RESCTRL_PMU_EVENT_ATTR_EXTRACTOR(resctrl_id, config1, 0, 63); +static DEFINE_MUTEX(resctrl_pmu_lock); +static DEFINE_IDR(resctrl_pmu_idr); + static void resctrl_pmu_do_nothing(struct pmu *pmu) { } @@ -69,12 +74,17 @@ static void resctrl_pmu_event_destroy(struct perf_event *event) struct hw_perf_event *hwc = &event->hw; u16 event_num = get_event(event); struct rdt_resource *r; + void *mon_ctx; r = resctrl_event_get_resource(event_num); if (!r) return; - resctrl_arch_mon_ctx_free(r, event_num, hwc->idx); + mutex_lock(&resctrl_pmu_lock); + mon_ctx = idr_remove(&resctrl_pmu_idr, hwc->idx); + mutex_unlock(&resctrl_pmu_lock); + + resctrl_arch_mon_ctx_free(r, event_num, mon_ctx); } static int resctrl_pmu_event_init(struct perf_event *event) @@ -87,6 +97,7 @@ static int resctrl_pmu_event_init(struct perf_event *event) struct rdt_domain *d; u16 event_num, domain_id; u32 closid, rmid; + void *mon_ctx; int err; u64 id; @@ -144,7 +155,12 @@ static int resctrl_pmu_event_init(struct perf_event *event) return -EINVAL; } - hwc->idx = resctrl_arch_mon_ctx_alloc_no_wait(r, event_num); + mon_ctx = resctrl_arch_mon_ctx_alloc_no_wait(r, event_num); + + mutex_lock(&resctrl_pmu_lock); + hwc->idx = idr_alloc(&resctrl_pmu_idr, mon_ctx, 0, INT_MAX, GFP_KERNEL); + mutex_unlock(&resctrl_pmu_lock); + if (hwc->idx == -ENOSPC) return -ENOSPC; event->destroy = resctrl_pmu_event_destroy; @@ -162,6 +178,7 @@ static void resctrl_pmu_event_update(struct perf_event *event) struct rdt_resource *r; struct rdt_domain *d; u32 closid, rmid; + void *mon_ctx; int err; event_num = get_event(event); @@ -179,11 +196,15 @@ static void resctrl_pmu_event_update(struct perf_event *event) if (!cpumask_test_cpu(smp_processor_id(), &d->cpu_mask)) return; + mutex_lock(&resctrl_pmu_lock); + mon_ctx = idr_find(&resctrl_pmu_idr, hwc->idx); + mutex_unlock(&resctrl_pmu_lock); + do { prev = local64_read(&hwc->prev_count); err = resctrl_arch_rmid_read(r, d, closid, rmid, - event_num, &now, hwc->idx); + event_num, &now, mon_ctx); if (err) return; } while (local64_cmpxchg(&hwc->prev_count, prev, now) != prev);