Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp491303lqt; Thu, 6 Jun 2024 09:17:29 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUB7B7oqMZBUKpphtF931qAuKzreNi5cFu533g/wJT+kqlMnTlqQ3uNR+f770LnL2K8nVaG+rahVeqaSfHVZcFEQkjXcuZmWnX2gKOzAA== X-Google-Smtp-Source: AGHT+IFg/ucXdGLusVtUaXYBe4/BzZIksuf/7CMOoTsAyJ7oPil0j0xKz/wVxLy9u+4mBXdF5qvF X-Received: by 2002:a17:907:7786:b0:a68:c9fa:f19f with SMTP id a640c23a62f3a-a6cdb0f542cmr2011866b.53.1717690649342; Thu, 06 Jun 2024 09:17:29 -0700 (PDT) Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a6c806db4eesi83620566b.396.2024.06.06.09.17.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jun 2024 09:17:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-204671-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=4IgLUVhq; arc=fail (signature failed); spf=pass (google.com: domain of linux-kernel+bounces-204671-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-204671-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id DFAD31F2674A for ; Thu, 6 Jun 2024 16:17:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6D36119B3C0; Thu, 6 Jun 2024 16:13:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="4IgLUVhq" Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2053.outbound.protection.outlook.com [40.107.244.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7FE0D19938C; Thu, 6 Jun 2024 16:13:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.244.53 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717690407; cv=fail; b=MXZBaEJtSOcpXYRghghdTYAnfQeouINCPrfuPuM1/hxpND/lWinDAwyS6Y68BEweFDw/9oBGMHTHS0Eqf6/NWhopatjQUk6KeTBorZtj9K8XQ7AMiQDcPQp5ulxRLITcPHcrdcOiCGsd7e4VVFNclxXAHIccm1eDUq6mWfOMdp8= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717690407; c=relaxed/simple; bh=PMQ6PBDRlaZoSnBPMlsnw249a40yN1TcPjfZBEYP530=; h=From:Date:Subject:MIME-Version:Content-Type:Message-ID:References: In-Reply-To:To:CC; b=QJSHO4PpEcSku/LdqNkxxzCwyUNBVtB62RN6gIUOXZmxTIAl3V3yr9BNqVudAas8jQWvuZyOK+GcM0ugNwe7OUEIV45e0tLkTF7UEGie2Gnd7+bjM1FilyQ6OCFFnYsgZZhrejJoLvxfT+dnCQ7bpuqiBhdPHhtefRsCUFfFr0A= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=4IgLUVhq; arc=fail smtp.client-ip=40.107.244.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ixswb4gDrWwJYSWLFCeo8LMzAevzMHz6qinFjytjaZaQIV0hcUuEM7+sPoD/OCTI2S5mdSTlE7KlFkuJqpv7SwMZLRYBWi2b6+Xnh9+K6kopCBVFduPQ7cZJ4Ai+IMuA5lznOgvS3EXL0qu9GF7HXUra8XF1hMWk2HX9ncJKBk/u0qNCuCEkayPkTzFZeePg8WJqpLeOX5GilQMIq8A/G+stZNqohB2K9AQ+E5gG5Z2HmY2Xw1F7G2W8Lz5x2NwxJDupjJOiJ5e/uZfZVffOG7aswbqSRGzgr1h4hzqTZrW6sxDSRkCM7Zng4iNdzPQk/7P+fCWiGwJEe2V1o9X5ng== 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=m3avHqH3H9cvY8+8JDGI6G5xgoIKzMDnfcPcP/x6ovs=; b=ir3ZeJ2LsdJw4D5968QHwyHqKxoq58HVTWEZOnZdPYB5om7dY6wuy/a4ubarTnakdTW4xtwMT5Qb7MKI5NIOmNDObcqsn0x3C2bR+LMiTfM/1nS4pX4c2Y0oRDynKcTxWVskgdxM30e3fsa7S7ACVH7pywZRFPzIGRTv6O3klBjKVLuQFMSXS+awTyXui5HbOZsRx5PoeKLw9eeYuBuLQ17sUR7p+HW++2TRZ92rUmiA1z7nNxCrfqpCSr9SlDOoYh6T8OqkIABNuIojZkLkUFaD+GBodB9FtIkcge69CqGpZ3m0LnVjcJg+nU2GIjttanLUtUARCDFA6Q+Z/Kzr3Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org 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 (0) 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=m3avHqH3H9cvY8+8JDGI6G5xgoIKzMDnfcPcP/x6ovs=; b=4IgLUVhqDm42OBCGJTXPh8du8lhCDZm+PtP18RYSHoCMFIqeWg7Wc9kA862UeBYpPyJRge65PO8IxbR0EHjFFxxO6s0k9PeU4yPHEwsmcprQFMVN+bnplUMoj4FArhabj7N1ZkA0k1XUVhnPI/CpR6kXGvB2+SaxpvlOzCvGOCc= Received: from CH0PR04CA0096.namprd04.prod.outlook.com (2603:10b6:610:75::11) by IA1PR12MB8467.namprd12.prod.outlook.com (2603:10b6:208:448::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.32; Thu, 6 Jun 2024 16:13:21 +0000 Received: from CH2PEPF00000146.namprd02.prod.outlook.com (2603:10b6:610:75:cafe::66) by CH0PR04CA0096.outlook.office365.com (2603:10b6:610:75::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7656.21 via Frontend Transport; Thu, 6 Jun 2024 16:13:21 +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 CH2PEPF00000146.mail.protection.outlook.com (10.167.244.103) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7633.15 via Frontend Transport; Thu, 6 Jun 2024 16:13:21 +0000 Received: from quartz-7b1chost.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.35; Thu, 6 Jun 2024 11:13:20 -0500 From: Yazen Ghannam Date: Thu, 6 Jun 2024 11:12:56 -0500 Subject: [PATCH v4 3/8] hwmon: (k10temp) Check return value of amd_smn_read() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-ID: <20240606-fix-smn-bad-read-v4-3-ffde21931c3f@amd.com> References: <20240606-fix-smn-bad-read-v4-0-ffde21931c3f@amd.com> In-Reply-To: <20240606-fix-smn-bad-read-v4-0-ffde21931c3f@amd.com> To: Guenter Roeck , , Yazen Ghannam CC: Mario Limonciello , , , , X-Mailer: b4 0.13.0 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: CH2PEPF00000146:EE_|IA1PR12MB8467:EE_ X-MS-Office365-Filtering-Correlation-Id: 08905f71-b40f-4d92-bfc0-08dc86439609 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|1800799015|36860700004|376005|82310400017; X-Microsoft-Antispam-Message-Info: =?utf-8?B?VUdWSFpYSXd6U1Ruc3ppUytwQm9lRUNZTW83cz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VzlucTM2bEtwVjhiMGc3Y3IvQVlvM1RQWXFZUEU0b1ovaTR0ZlVnSld0NUhi?= =?utf-8?B?MS9QQjBsNHVwdlVidU5DcE84aFVyWklNWm9WeEwybVRpTGc9PQ==?= 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)(1800799015)(36860700004)(376005)(82310400017);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Jun 2024 16:13:21.6982 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 08905f71-b40f-4d92-bfc0-08dc86439609 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: CH2PEPF00000146.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8467 Check the return value of amd_smn_read() before saving a value. This ensures invalid values aren't saved or used. There are three cases here with slightly different behavior. 1) read_tempreg_nb_zen(): This is a function pointer which does not include a return code. In this case, set the register value to 0 on failure. This enforces Read-as-Zero behavior. 2) k10temp_read_temp(): This function does have return codes, so return the error code from the failed register read. Continued operation is not necessary, since there is no valid data from the register. Furthermore, if the register value was set to 0, then the following operation would underflow. 3) k10temp_get_ccd_support(): This function reads the same register from multiple CCD instances in a loop. And a bitmask is formed if a specific bit is set in each register instance. The loop should continue on a failed register read, skipping the bit check. Furthermore, the __must_check attribute will be added to amd_smn_read(). Therefore, this change is required to avoid compile-time warnings. Signed-off-by: Yazen Ghannam Reviewed-by: Mario Limonciello Acked-by: Guenter Roeck --- drivers/hwmon/k10temp.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/drivers/hwmon/k10temp.c b/drivers/hwmon/k10temp.c index 8092312c0a87..6cad35e7f182 100644 --- a/drivers/hwmon/k10temp.c +++ b/drivers/hwmon/k10temp.c @@ -153,8 +153,9 @@ static void read_tempreg_nb_f15(struct pci_dev *pdev, u32 *regval) static void read_tempreg_nb_zen(struct pci_dev *pdev, u32 *regval) { - amd_smn_read(amd_pci_dev_to_node_id(pdev), - ZEN_REPORTED_TEMP_CTRL_BASE, regval); + if (amd_smn_read(amd_pci_dev_to_node_id(pdev), + ZEN_REPORTED_TEMP_CTRL_BASE, regval)) + *regval = 0; } static long get_raw_temp(struct k10temp_data *data) @@ -205,6 +206,7 @@ static int k10temp_read_temp(struct device *dev, u32 attr, int channel, long *val) { struct k10temp_data *data = dev_get_drvdata(dev); + int ret = -EOPNOTSUPP; u32 regval; switch (attr) { @@ -221,13 +223,17 @@ static int k10temp_read_temp(struct device *dev, u32 attr, int channel, *val = 0; break; case 2 ... 13: /* Tccd{1-12} */ - amd_smn_read(amd_pci_dev_to_node_id(data->pdev), - ZEN_CCD_TEMP(data->ccd_offset, channel - 2), - ®val); + ret = amd_smn_read(amd_pci_dev_to_node_id(data->pdev), + ZEN_CCD_TEMP(data->ccd_offset, channel - 2), + ®val); + + if (ret) + return ret; + *val = (regval & ZEN_CCD_TEMP_MASK) * 125 - 49000; break; default: - return -EOPNOTSUPP; + return ret; } break; case hwmon_temp_max: @@ -243,7 +249,7 @@ static int k10temp_read_temp(struct device *dev, u32 attr, int channel, - ((regval >> 24) & 0xf)) * 500 + 52000; break; default: - return -EOPNOTSUPP; + return ret; } return 0; } @@ -381,8 +387,20 @@ static void k10temp_get_ccd_support(struct pci_dev *pdev, int i; for (i = 0; i < limit; i++) { - amd_smn_read(amd_pci_dev_to_node_id(pdev), - ZEN_CCD_TEMP(data->ccd_offset, i), ®val); + /* + * Ignore inaccessible CCDs. + * + * Some systems will return a register value of 0, and the TEMP_VALID + * bit check below will naturally fail. + * + * Other systems will return a PCI_ERROR_RESPONSE (0xFFFFFFFF) for + * the register value. And this will incorrectly pass the TEMP_VALID + * bit check. + */ + if (amd_smn_read(amd_pci_dev_to_node_id(pdev), + ZEN_CCD_TEMP(data->ccd_offset, i), ®val)) + continue; + if (regval & ZEN_CCD_TEMP_VALID) data->show_temp |= BIT(TCCD_BIT(i)); } -- 2.34.1