From patchwork Tue Oct 10 14:50:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shyam Sundar S K X-Patchwork-Id: 13415688 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 B5F90CD8C99 for ; Tue, 10 Oct 2023 14:50:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233225AbjJJOuw (ORCPT ); Tue, 10 Oct 2023 10:50:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233232AbjJJOus (ORCPT ); Tue, 10 Oct 2023 10:50:48 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2074.outbound.protection.outlook.com [40.107.102.74]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E5C3C9 for ; Tue, 10 Oct 2023 07:50:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PlXaoPYB9GbBIG4RYe1DHqA3ujt5CdegmAtRDs2t+qvRz2JQvqFBd69cIvGUt5hLS5q2f2seT0lXEaYX6XvK5OO3G20sqhoUksQb7h54//uGGZOovk031Bv8hBIlsS61fIoaO/LBBLcvd/wuwXOazXWXS73OQAzj22Qy0KJ4uJXdJatcbL5N3JW48tjTM29/qOaIIvAwsxLk45+ovKswHIibypxH3PQcfU0x0qBNcsKAtEbiueGRC+x1pYfLUBpx2JJPdnIUhwer2+w6jdbPcTGDOGmrEYW6pslICriqHl2h09WHqHG4BnckjareS4lI+k3yjG2GyDqKLwgqGYOv2Q== 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=5q7kvMsd4tRov/VaM0dnNnLDyKg/STHZmJwsRLoOVYE=; b=aGMuWddgEHjRUuly+rdMgtxg+ClnrBNbqQ+29ev+wVKI8urcbV9e0BCT5LCUmyd3OYSK7XBnXWDlDnEDwKR/Yopuz8IoEIRbaQWHOhdKBA5HUNpONLi4a5fJp3XXxn7gNhnW4jxB8AC+3PcLwi4ar9ObZQ49SALsHIGVPkvczc1LcuDX8sHelr+GNY3OziQz5k3zkKvi84a7lWKLH6H6Kfl0jmpG5ksVT97LAaQatxKXsXtQX5FgrN53zHx3mh56DbpM3KsxAL89B20m7J8tGUBPqdz3iiGUS+MpMV8qWDbCGOi5uKO425NwFoGlB1eolZdwScK9U5Lu+Z7LuUbKYQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5q7kvMsd4tRov/VaM0dnNnLDyKg/STHZmJwsRLoOVYE=; b=OsMIQAY4JFb8iaB5t6bpFPD4IvBSsBP17X1yx1N/6+ngTgUDQEQCPZVBn00PDIbvTTCku5TV4clebfI/JQdzoibAqEFNt2Tq+IZHrwGUnGe6Rfkw82ML/LainkypAH4323KorwkF4mMoXbJDipeRhUDB2U19SLffTzxDWJUeBjw= Received: from DM6PR07CA0077.namprd07.prod.outlook.com (2603:10b6:5:337::10) by SJ0PR12MB5488.namprd12.prod.outlook.com (2603:10b6:a03:3ad::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6863.38; Tue, 10 Oct 2023 14:50:37 +0000 Received: from CY4PEPF0000E9CD.namprd03.prod.outlook.com (2603:10b6:5:337:cafe::47) by DM6PR07CA0077.outlook.office365.com (2603:10b6:5:337::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6886.27 via Frontend Transport; Tue, 10 Oct 2023 14:50:37 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9CD.mail.protection.outlook.com (10.167.241.140) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6838.22 via Frontend Transport; Tue, 10 Oct 2023 14:50:37 +0000 Received: from jatayu.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Tue, 10 Oct 2023 09:50:34 -0500 From: Shyam Sundar S K To: , , CC: , , , Shyam Sundar S K Subject: [PATCH v8 1/3] platform/x86/amd/pmc: Use flex array when calling amd_pmc_stb_debugfs_open_v2() Date: Tue, 10 Oct 2023 20:20:01 +0530 Message-ID: <20231010145003.139932-1-Shyam-sundar.S-k@amd.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9CD:EE_|SJ0PR12MB5488:EE_ X-MS-Office365-Filtering-Correlation-Id: 0ad721ba-eefc-4191-cf6a-08dbc9a043ef X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: VaoqcY/12wpZKMEpITFbsNeHj5YaZwOpy5gdxdeGZEOcAlsHtcGmKl6FqcmbfzAmrY17OJP/Uf9fwUwGJ2kgcsb46gtzHEMGZKUSBk9194enZyeo/iJzvuS//Q2bYB5SruYwlVHPkNO1ZiRJGUYvmbeP8Ak6yo0ANpHzXkrxPJ3hJgX73J9tlJioZYlm1WyESYeeA53T6Zxj9e1FxLtFkscwRcSEutThyYak6kBdC2cnx5GnVSjEku16IyY5ze8F7oJ2Dj3DCI5VpNcd+vxNmn95q28oW+t8PLGO7jkFBi6I/l8grBdGdavAQ0SzBr1b1p8l4Y3CW12S/gPbxErOTXSNF106YIRC5mD91j7jtp/ZpfpfUjp2yF88z6csOS7prx4DvwSYu7GRMHdznCtJVBISQhLDBiWsTMsCa1W23sgrF9zWHriTLBgtamRt1vI6U/IyIzO0vAEUSmEque8Db11R0MXKIh96/HzKe+5aElb6nkdt/ed3V2mEY4oJLDi0n6S6ZKNXbWvLZvV+jyFomCV++BMuvoUjVvDWEGTzDenjAgEsKicv0ewWM7NazsX23YX6mhCI0lbc8okr3BlFCu9n6J3/RMiWgkjbx7eD74EptdamEm3tXm72b2Dc4gChf5eD7O81W2phX66qlXZzugJKHYetZvFjuJaGqc/5lwzu8qHkoETmne/bcdpqdcJYHynSeF+0kd99einAhHvQtePRtvgrMuMJtj5w4XhwNWuD0R9rm1h0b3uUncWPAF24Krdw5NKzOu4HblClUqFrjA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(39860400002)(396003)(376002)(346002)(136003)(230922051799003)(64100799003)(186009)(1800799009)(82310400011)(451199024)(36840700001)(40470700004)(46966006)(40460700003)(1076003)(7696005)(478600001)(2616005)(6666004)(47076005)(336012)(426003)(26005)(16526019)(83380400001)(2906002)(5660300002)(54906003)(110136005)(70586007)(70206006)(4326008)(8676002)(8936002)(316002)(41300700001)(356005)(82740400003)(81166007)(36860700001)(36756003)(86362001)(40480700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Oct 2023 14:50:37.3703 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0ad721ba-eefc-4191-cf6a-08dbc9a043ef X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9CD.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB5488 Precedence: bulk List-ID: X-Mailing-List: platform-driver-x86@vger.kernel.org Currently in amd_pmc_stb_debugfs_open_v2() the buffer size is assumed to be fixed and a second call to amd_pmc_stb_debugfs_open_v2() may race with a process holding open another fd. This could change "fsize" to a bigger size causing an out of bounds read. Instead create a struct with a flexarray to solve this. Suggested-by: Hans de Goede Reviewed-by: Hans de Goede Signed-off-by: Sanket Goswami Signed-off-by: Shyam Sundar S K --- v7->v8: - use min() v6->v7: - No change v6: - Handle release buffer case as per Hans remarks - based on review-ilpo branch v5: - new patch based on comments in v4 from Hans. - based on review-ilpo branch drivers/platform/x86/amd/pmc/pmc.c | 32 ++++++++++++++++++------------ 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/drivers/platform/x86/amd/pmc/pmc.c b/drivers/platform/x86/amd/pmc/pmc.c index c92dd5077a16..609bad470fe3 100644 --- a/drivers/platform/x86/amd/pmc/pmc.c +++ b/drivers/platform/x86/amd/pmc/pmc.c @@ -52,7 +52,7 @@ #define AMD_S2D_REGISTER_ARGUMENT 0xA88 /* STB Spill to DRAM Parameters */ -#define S2D_TELEMETRY_BYTES_MAX 0x100000 +#define S2D_TELEMETRY_BYTES_MAX 0x100000U #define S2D_TELEMETRY_DRAMBYTES_MAX 0x1000000 /* STB Spill to DRAM Message Definition */ @@ -122,6 +122,11 @@ enum s2d_arg { S2D_DRAM_SIZE, }; +struct amd_pmc_stb_v2_data { + size_t size; + u8 data[] __counted_by(size); +}; + struct amd_pmc_bit_map { const char *name; u32 bit_mask; @@ -239,7 +244,8 @@ static const struct file_operations amd_pmc_stb_debugfs_fops = { static int amd_pmc_stb_debugfs_open_v2(struct inode *inode, struct file *filp) { struct amd_pmc_dev *dev = filp->f_inode->i_private; - u32 *buf, fsize, num_samples, val, stb_rdptr_offset = 0; + u32 fsize, num_samples, val, stb_rdptr_offset = 0; + struct amd_pmc_stb_v2_data *flex_arr; int ret; /* Write dummy postcode while reading the STB buffer */ @@ -247,10 +253,6 @@ static int amd_pmc_stb_debugfs_open_v2(struct inode *inode, struct file *filp) if (ret) dev_err(dev->dev, "error writing to STB: %d\n", ret); - buf = kzalloc(S2D_TELEMETRY_BYTES_MAX, GFP_KERNEL); - if (!buf) - return -ENOMEM; - /* Spill to DRAM num_samples uses separate SMU message port */ dev->msg_port = 1; @@ -264,10 +266,16 @@ static int amd_pmc_stb_debugfs_open_v2(struct inode *inode, struct file *filp) dev->msg_port = 0; if (ret) { dev_err(dev->dev, "error: S2D_NUM_SAMPLES not supported : %d\n", ret); - kfree(buf); return ret; } + fsize = min(num_samples, S2D_TELEMETRY_BYTES_MAX); + flex_arr = kmalloc(struct_size(flex_arr, data, fsize), GFP_KERNEL); + if (!flex_arr) + return -ENOMEM; + + flex_arr->size = fsize; + /* Start capturing data from the last push location */ if (num_samples > S2D_TELEMETRY_BYTES_MAX) { fsize = S2D_TELEMETRY_BYTES_MAX; @@ -277,8 +285,8 @@ static int amd_pmc_stb_debugfs_open_v2(struct inode *inode, struct file *filp) stb_rdptr_offset = 0; } - memcpy_fromio(buf, dev->stb_virt_addr + stb_rdptr_offset, fsize); - filp->private_data = buf; + memcpy_fromio(flex_arr->data, dev->stb_virt_addr + stb_rdptr_offset, fsize); + filp->private_data = flex_arr; return 0; } @@ -286,11 +294,9 @@ static int amd_pmc_stb_debugfs_open_v2(struct inode *inode, struct file *filp) static ssize_t amd_pmc_stb_debugfs_read_v2(struct file *filp, char __user *buf, size_t size, loff_t *pos) { - if (!filp->private_data) - return -EINVAL; + struct amd_pmc_stb_v2_data *data = filp->private_data; - return simple_read_from_buffer(buf, size, pos, filp->private_data, - S2D_TELEMETRY_BYTES_MAX); + return simple_read_from_buffer(buf, size, pos, data->data, data->size); } static int amd_pmc_stb_debugfs_release_v2(struct inode *inode, struct file *filp) From patchwork Tue Oct 10 14:50:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shyam Sundar S K X-Patchwork-Id: 13415690 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 ABD92CD8C9C for ; Tue, 10 Oct 2023 14:50:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233161AbjJJOu4 (ORCPT ); Tue, 10 Oct 2023 10:50:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233082AbjJJOut (ORCPT ); Tue, 10 Oct 2023 10:50:49 -0400 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2089.outbound.protection.outlook.com [40.107.220.89]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B2B87FB for ; Tue, 10 Oct 2023 07:50:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AVHl4UMrkY4oLuUhDvU2+DgUUp5EoI2EY8h51G81K8SyfKMSNwS88b2DhrqkkQsl9HV4BXjdR92vMxNlGJAD5jUFJifRyrAspLI3mez/Lbse+lH1B+MvJBbZ0rY1tkdaw6mBFq1wzkuXJip5H9nAJTQrAaz/xCzTSQrALvWGv/UcqUZDdnlFCYC2vH/Lt6wosG21Wljs+k1I7ILRQhArE4Csx2z0wzTKyMOCmQpX1GRiPkj1Aukd/nEdQQLYpBV18WdkYfY5jUr7SNM7Hg6IZfGDwTkZN0YwqyrqofqcsZzqMM74uDz0qi1typd2/cA3AfAezSOv4pzLSgw1CDx04A== 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=ECPdvIAd0uxE7XigW/bUUoSdua5hGv8kmyGJc9fa3Pk=; b=nbU2Iv2+tg0WscRqwc3gY0SXIQRPKWW2y9R6i0MSpXo5kVmaIgHbRV1g7I7GDxOz9EXpcCbz/OoFEH7qvncYWadrjM0/dcl0FXHTa2XzC/PI3JmArQfF7wDZRKPzAGtLBcwflZHVFdEq7Kvn5XuqzWH9+TEpBJwHsw2oZ6vRMAp6DRQTha+0IMtc05xcRzsUorzVLSArTQRfd66irXx3ywsOj94MG4gCJw6SJsQxlcFJg1CIEd3FHPJdLC7dFajujvuXTQajujruNpY6D8onfzhs7swsFkkCvhCGxtXidAUMJWmeK1YFr+tjNTyu1V0UY4mVvZF0zQs+VeBBjF/+Yg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ECPdvIAd0uxE7XigW/bUUoSdua5hGv8kmyGJc9fa3Pk=; b=ZF7KeCXLts04oI98bQtifmEJsT4CTb9gp+KN94n0mGqirKg8j2PWj5HxuOP5wUeMW8KszxZ8h2phDhcRbLozdNqNIcxgBd72sY9FbuMxZOfKKMIr0sqBNF+bKkRNPDOk01CtXo6lmCrAOlybFDCUqN6xOE2M0HNCdTMrrQ/bUjQ= Received: from CH0PR03CA0002.namprd03.prod.outlook.com (2603:10b6:610:b0::7) by SN7PR12MB8148.namprd12.prod.outlook.com (2603:10b6:806:351::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6863.37; Tue, 10 Oct 2023 14:50:41 +0000 Received: from CY4PEPF0000E9D0.namprd03.prod.outlook.com (2603:10b6:610:b0:cafe::4f) by CH0PR03CA0002.outlook.office365.com (2603:10b6:610:b0::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6863.38 via Frontend Transport; Tue, 10 Oct 2023 14:50:40 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D0.mail.protection.outlook.com (10.167.241.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6838.22 via Frontend Transport; Tue, 10 Oct 2023 14:50:40 +0000 Received: from jatayu.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Tue, 10 Oct 2023 09:50:37 -0500 From: Shyam Sundar S K To: , , CC: , , , Shyam Sundar S K Subject: [PATCH v8 2/3] platform/x86/amd/pmc: Handle overflow cases where the num_samples range is higher Date: Tue, 10 Oct 2023 20:20:02 +0530 Message-ID: <20231010145003.139932-2-Shyam-sundar.S-k@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231010145003.139932-1-Shyam-sundar.S-k@amd.com> References: <20231010145003.139932-1-Shyam-sundar.S-k@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D0:EE_|SN7PR12MB8148:EE_ X-MS-Office365-Filtering-Correlation-Id: bf3a4cfd-6bc4-4c42-13bb-08dbc9a045bf X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: b0wqiI98PvOS48pkgrK69fcLOD7hO0rU/XZsjUSTVb/nGdTrmkNEYdYqgSc4zhnzQEh5Mhb0aruVS3EFOZ8dMkZcSm4llIEPKbD5eJ7Vr+hoAQGXfEVcgiaAGpKIFWUIHXKeZUAfzeVShNVyYlVtVhB5qRlVu5YvsBF0bT9J72qPB826fo3UYjItD11lfkd2btj1RxulbWDqksuBQv+pcBDQVxPRR4dvdMY3OTEZLptV2JGEDsk5Uzt21g4ZxJW013wmXlHYaRI6M2f0PKBemadBhBA8OliVUWmpEPmOnYuLXkhRewhkzpFxF7rXQTS/KsLub+VOqU5Gw6A0ObTDNBXuxOByeaWpaUdTxu/ci8+74naq+xGq0fbJJwfM5xp2wUpqWYfl9p9ndMXouDBwU2kIsx2n3175zWjSMmx6eByf33F6PSISiUsdFoG+K2LpRkpnx4W0DcqNmrLtjnTEIS7etHNASYgAWSDoq79smfQn/SLAXy0cVcRUQH0lCUHAi1p3R6ZVF3gN41r9Z1sA+7rJI+IZUlBseAe7O/ic3+e/Gk5OdgynRgqBh3sqHoShvEgvdONMY4I3ix/HhCeD3yZTx6lJSVFCAr4XDqJbM2aqXLfJl069iZ6Q6s+/aYzn07yd3IlHjwaPq/vp8X8YCvCNG9l4MUcaGHooYxd2rFtzRzrbk+RAeJfM0tXoXnQhVJzhrolh+tLsdZlFCwKAtAqvMRwGmbqUHx4ZoaUhzHWEy5AAGM6xc1NNBwImIqrA X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(39860400002)(136003)(376002)(346002)(396003)(230922051799003)(82310400011)(451199024)(186009)(1800799009)(64100799003)(46966006)(40470700004)(36840700001)(82740400003)(2616005)(7696005)(1076003)(478600001)(41300700001)(2906002)(336012)(83380400001)(426003)(47076005)(316002)(70586007)(110136005)(5660300002)(54906003)(70206006)(8676002)(4326008)(16526019)(26005)(8936002)(40460700003)(36756003)(40480700001)(36860700001)(356005)(81166007)(86362001)(6666004)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Oct 2023 14:50:40.3907 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: bf3a4cfd-6bc4-4c42-13bb-08dbc9a045bf X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D0.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB8148 Precedence: bulk List-ID: X-Mailing-List: platform-driver-x86@vger.kernel.org In amd_pmc_stb_debugfs_open_v2(), the stb buffer is created based on the num_samples and the read/write pointer offset. This holds good when the num_samples reported by PMFW is less than S2D_TELEMETRY_BYTES_MAX; where the stb buffer gets filled from 0th position until S2D_TELEMETRY_BYTES_MAX - 1 based on the read/write pointer offset. But when the num_samples exceeds the S2D_TELEMETRY_BYTES_MAX, the current code does not handle it well as it does not account for the cases where the stb buffer has to filled up as a circular buffer. Handle this scenario into two cases, where first memcpy will have the samples from location: (num_samples % S2D_TELEMETRY_BYTES_MAX) - (S2D_TELEMETRY_BYTES_MAX - 1) and next memcpy will have the newest ones i.e. 0 - (num_samples % S2D_TELEMETRY_BYTES_MAX - 1) Suggested-by: Hans de Goede Reviewed-by: Hans de Goede Signed-off-by: Sanket Goswami Signed-off-by: Shyam Sundar S K --- v7->v8: - No change v6->v7: - No change v5->v6: - Make changes as per Hans on v5 - based on review-ilpo branch v4->v5: - Fix exisiting code problems when reading stb buffer as a circular data - based on review-ilpo branch v3->v4: - Update code branches and commit-msg as per Ilpo's remark. v2->v3: - no change v1->v2: - rebase to 'review-hans' branch - drop 2/4 of v1 (https://patchwork.kernel.org/project/platform-driver-x86/list/?series=775324&state=%2A&archive=both) drivers/platform/x86/amd/pmc/pmc.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/drivers/platform/x86/amd/pmc/pmc.c b/drivers/platform/x86/amd/pmc/pmc.c index 609bad470fe3..e190ec5a602f 100644 --- a/drivers/platform/x86/amd/pmc/pmc.c +++ b/drivers/platform/x86/amd/pmc/pmc.c @@ -276,16 +276,23 @@ static int amd_pmc_stb_debugfs_open_v2(struct inode *inode, struct file *filp) flex_arr->size = fsize; - /* Start capturing data from the last push location */ + /* + * Start capturing data from the last push location. + * This is for general cases, where the stb limits + * are meant for standard usage. + */ if (num_samples > S2D_TELEMETRY_BYTES_MAX) { - fsize = S2D_TELEMETRY_BYTES_MAX; - stb_rdptr_offset = num_samples - fsize; + /* First read oldest data starting 1 behind last write till end of ringbuffer */ + stb_rdptr_offset = num_samples % S2D_TELEMETRY_BYTES_MAX; + fsize = S2D_TELEMETRY_BYTES_MAX - stb_rdptr_offset; + + memcpy_fromio(flex_arr->data, dev->stb_virt_addr + stb_rdptr_offset, fsize); + /* Second copy the newer samples from offset 0 - last write */ + memcpy_fromio(flex_arr->data + fsize, dev->stb_virt_addr, stb_rdptr_offset); } else { - fsize = num_samples; - stb_rdptr_offset = 0; + memcpy_fromio(flex_arr->data, dev->stb_virt_addr, fsize); } - memcpy_fromio(flex_arr->data, dev->stb_virt_addr + stb_rdptr_offset, fsize); filp->private_data = flex_arr; return 0; From patchwork Tue Oct 10 14:50:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Shyam Sundar S K X-Patchwork-Id: 13415689 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 A2903CD8C99 for ; Tue, 10 Oct 2023 14:50:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232969AbjJJOuz (ORCPT ); Tue, 10 Oct 2023 10:50:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233242AbjJJOuv (ORCPT ); Tue, 10 Oct 2023 10:50:51 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2079.outbound.protection.outlook.com [40.107.94.79]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C6CC107 for ; Tue, 10 Oct 2023 07:50:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TEWnyXWef3LtgDYw0dUhcO9Dc8uEEMaH4Pgp0w60t8cDPSdv3J708/b4iNs+vbHfNiHWJtXa815Z/xGDOtyrotX0i6jQ2rE/7/N2ouXV0rV41mYqQ9CvXvEYVTjpBTxo57kgGLLLnH2bXyR7nViXZpi3aMyaALarg36eZzu5SQjJGqXmTDwPhQVrYwYG++bzO3IweiB4zao0g2x8gkbhWj0ZU+cfVJZZmjSLCixwhIcZboNrVQfsmCospCYtDpMbjDYtigYxR/qYs3/QvgY+ujLXqsvtXgk5KTW46QiX9pfE3NUmPF7r2QbT75w7rwyPS+MrYcTjyEdi2Qsy2vmBnA== 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=6Udzed/WCldWJJ41c4IuGDMu/UPJj+kTrXMjUQ0ELEk=; b=DybEAoFMDOT9TyUU5yrnY34mwX+enaEjaKtAM6UzyxCeZklJfAX3RIgLgSHh70oYPo1T5F0A936AkGsdiZW3LwNZkpxr5dFYTeW/w6O959IrTXizsMubNywQNGqBCBuKBKSxIU5Spz20/Vh1wwtgMUFmgUX0USyKbXakj5xRw5l4I2SZbHssuyEVf6BmwmkDbb01NY4E5A+/ZqFmNAVdt9fdCEGYhButv30gdTG5A7lKmaabCJQfTh9JrqFBzAUTKMDQVsGCpBmK70BPEpaLKEixiZCYJDUVOidEtNhTjk4ZSty6GAFNlK6xGyreAGehvEC5ufpi79XArx7OvUyArQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=6Udzed/WCldWJJ41c4IuGDMu/UPJj+kTrXMjUQ0ELEk=; b=2fLRWzMAKIcxI4ZzKyE3I2nZSAQwLiZXXozvHPVvPpQYonWeWpFSjOWVUjb0aqdY+nXX9Zk4ifNVQjQmI24FJ0Neb77DumYDt136J8nBic7zYODr8VaROVfciHgTX8iKXOWOPIUEfpnY+8Dhz3vhgLgM+N2qzsdVPUyzFdxhZDU= Received: from CH0P221CA0043.NAMP221.PROD.OUTLOOK.COM (2603:10b6:610:11d::23) by MW4PR12MB6976.namprd12.prod.outlook.com (2603:10b6:303:20a::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6838.39; Tue, 10 Oct 2023 14:50:42 +0000 Received: from CY4PEPF0000E9D4.namprd03.prod.outlook.com (2603:10b6:610:11d:cafe::1b) by CH0P221CA0043.outlook.office365.com (2603:10b6:610:11d::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6863.37 via Frontend Transport; Tue, 10 Oct 2023 14:50:42 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000E9D4.mail.protection.outlook.com (10.167.241.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6838.22 via Frontend Transport; Tue, 10 Oct 2023 14:50:42 +0000 Received: from jatayu.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Tue, 10 Oct 2023 09:50:39 -0500 From: Shyam Sundar S K To: , , CC: , , , Shyam Sundar S K , Harsh Jain Subject: [PATCH v8 3/3] platform/x86/amd/pmc: Add dump_custom_stb module parameter Date: Tue, 10 Oct 2023 20:20:03 +0530 Message-ID: <20231010145003.139932-3-Shyam-sundar.S-k@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231010145003.139932-1-Shyam-sundar.S-k@amd.com> References: <20231010145003.139932-1-Shyam-sundar.S-k@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D4:EE_|MW4PR12MB6976:EE_ X-MS-Office365-Filtering-Correlation-Id: d6e999fc-6ef0-4a06-24fe-08dbc9a046f8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Z1/yIdc0Ye0p9a6AdFCaC+/JYJbwjqYAWJZlK34Tr7T1+xz8eTraj+ZgaQfzgLUdo6GnFwLWonSDeYNQJ+8lJHLo5iWXNo6nOZMocE4uk5UHvIQ8/zJ1amSFps1g0HuCwHXAlvylJMns/hOaEk4cvDLIrANCOU+4BopPzDE2Ayhqhk4Uiy7AGhDoqQxuNFBuXbiTqMA6igJtGOL/Buy8WxmRa/8sMWrwmWueJbWKvrJlAQp7se1OWu0NP7bGm4Rd8+J9Fs2fR/AUe+3cqMHbMS7in1DUeRXnsYIepGzkWjFMch2QR/XXwjxU2RJUbVfw7QjN7EYiil2J5svfiJW/vpeUR26Rx9czYcshbMPuDCIfLSnOhxyKiFb+v3S3Icj3/kVmqkB/RxsDkcQfGAX93Ppm1kVfxcwpVv32pB63LsXe5aPM+M8px9PdWrVA6xvK5uAAu7GHGegApBeWflrjIzSmzqOf0vAcLCtYCJiy+YHhkTsrRHBpQdz2L8SdoRueJ+qAXdJP19FRwIYzHgrx16YfBCCIuFhJ2YGEHkpcsr/5aTg1wb+G8BUAh+5oPTdK4TOVTgTbRJwzoG21KT2ATl7TZpH9Z5jjmK5JrK24/UciIct1daFlhLNv8PD4DeVsas2KURTssErSYFJSpnhVVQVG6MM+8REXt7BTK4yMTpwZXomojvsVyuHT/NgXKF+JB9XHifL6bIuya1GNm4iuS6hg2OrAUqM4fY5wQwUnKyK5I+KOner1bjq5p/Wq+kpjOmkhjQv23vM9JNjpxouW5w== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(4636009)(136003)(346002)(376002)(39860400002)(396003)(230922051799003)(82310400011)(1800799009)(451199024)(64100799003)(186009)(46966006)(36840700001)(40470700004)(1076003)(40460700003)(356005)(36756003)(40480700001)(86362001)(81166007)(82740400003)(36860700001)(16526019)(26005)(47076005)(426003)(336012)(2906002)(83380400001)(66574015)(2616005)(6666004)(7696005)(478600001)(4326008)(8936002)(8676002)(41300700001)(316002)(5660300002)(70586007)(54906003)(110136005)(70206006)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Oct 2023 14:50:42.4737 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d6e999fc-6ef0-4a06-24fe-08dbc9a046f8 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D4.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB6976 Precedence: bulk List-ID: X-Mailing-List: platform-driver-x86@vger.kernel.org There have been instances when the default size (1M) of the STB is not sufficient to get the complete traces of the failure. In such scenarios we can use a module_param to enable full trace that shall contain more debugging data. This is not a regular case and hence not enabling this capability by default. With this change, there will be two cases on how the driver fetches the stb data: 1) A special case (proposed now) - which is required only for certain platforms. Here, a new module param will be supplied to the driver that will have a special PMFW supporting enhanced dram sizes for getting the stb data. Without the special PMFW support, just setting the module param will not help to get the enhanced stb data. To adapt to this change, we will have a new amd_pmc_stb_handle_efr() to handle enhanced firmware reporting mechanism. Note that, since num_samples based r/w pointer offset calculation is not required for enhanced firmware reporting we will have this mailbox command sent only in case of regular STB cases. 2) Current code branch which fetches the stb data based on the parameters like the num_samples, fsize and the r/w pointer. Reviewed-by: Ilpo Järvinen Reviewed-by: Hans de Goede Co-developed-by: Harsh Jain Signed-off-by: Harsh Jain Signed-off-by: Sanket Goswami Signed-off-by: Shyam Sundar S K --- v7->v8: - No change v6->v7: - No change v5->v6: - Make changes as per Hans on v5 - based on review-ilpo branch v4->v5: - Fix exisiting code problems when reading stb buffer as a circular data - based on review-ilpo branch v3->v4: - Update code branches and commit-msg as per Ilpo's remark. v2->v3: - no change v1->v2: - rebase to 'review-hans' branch - drop 2/4 of v1 (https://patchwork.kernel.org/project/platform-driver-x86/list/?series=775324&state=%2A&archive=both) drivers/platform/x86/amd/pmc/pmc.c | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/drivers/platform/x86/amd/pmc/pmc.c b/drivers/platform/x86/amd/pmc/pmc.c index e190ec5a602f..9d041000084e 100644 --- a/drivers/platform/x86/amd/pmc/pmc.c +++ b/drivers/platform/x86/amd/pmc/pmc.c @@ -53,6 +53,7 @@ /* STB Spill to DRAM Parameters */ #define S2D_TELEMETRY_BYTES_MAX 0x100000U +#define S2D_RSVD_RAM_SPACE 0x100000 #define S2D_TELEMETRY_DRAMBYTES_MAX 0x1000000 /* STB Spill to DRAM Message Definition */ @@ -165,6 +166,10 @@ static bool disable_workarounds; module_param(disable_workarounds, bool, 0644); MODULE_PARM_DESC(disable_workarounds, "Disable workarounds for platform bugs"); +static bool dump_custom_stb; +module_param(dump_custom_stb, bool, 0644); +MODULE_PARM_DESC(dump_custom_stb, "Enable to dump full STB buffer"); + static struct amd_pmc_dev pmc; static int amd_pmc_send_cmd(struct amd_pmc_dev *dev, u32 arg, u32 *data, u8 msg, bool ret); static int amd_pmc_read_stb(struct amd_pmc_dev *dev, u32 *buf); @@ -241,6 +246,25 @@ static const struct file_operations amd_pmc_stb_debugfs_fops = { .release = amd_pmc_stb_debugfs_release, }; +/* Enhanced STB Firmware Reporting Mechanism */ +static int amd_pmc_stb_handle_efr(struct file *filp) +{ + struct amd_pmc_dev *dev = filp->f_inode->i_private; + struct amd_pmc_stb_v2_data *flex_arr; + u32 fsize; + + fsize = dev->dram_size - S2D_RSVD_RAM_SPACE; + flex_arr = kmalloc(struct_size(flex_arr, data, fsize), GFP_KERNEL); + if (!flex_arr) + return -ENOMEM; + + flex_arr->size = fsize; + memcpy_fromio(flex_arr->data, dev->stb_virt_addr, fsize); + filp->private_data = flex_arr; + + return 0; +} + static int amd_pmc_stb_debugfs_open_v2(struct inode *inode, struct file *filp) { struct amd_pmc_dev *dev = filp->f_inode->i_private; @@ -260,6 +284,14 @@ static int amd_pmc_stb_debugfs_open_v2(struct inode *inode, struct file *filp) if (ret) dev_warn_once(dev->dev, "S2D force flush not supported\n"); + /* + * We have a custom stb size and the PMFW is supposed to give + * the enhanced dram size. Note that we land here only for the + * platforms that support enhanced dram size reporting. + */ + if (dump_custom_stb) + return amd_pmc_stb_handle_efr(filp); + /* Get the num_samples to calculate the last push location */ ret = amd_pmc_send_cmd(dev, S2D_NUM_SAMPLES, &num_samples, dev->s2d_msg_id, true); /* Clear msg_port for other SMU operation */