Commit 2e96176d authored by Yazen Ghannam's avatar Yazen Ghannam Committed by Wen Zhiwei
Browse files

hwmon: (k10temp) Check return value of amd_smn_read()

stable inclusion
from stable-v6.6.50
commit 63ca5b467011eed4da00f164277d3aa1968f3e28
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/IAYKL6

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=63ca5b467011eed4da00f164277d3aa1968f3e28



--------------------------------

[ Upstream commit c2d79cc5455c891de6c93e1e0c73d806e299c54f ]

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.

Signed-off-by: default avatarYazen Ghannam <yazen.ghannam@amd.com>
Signed-off-by: default avatarBorislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: default avatarMario Limonciello <mario.limonciello@amd.com>
Acked-by: default avatarGuenter Roeck <linux@roeck-us.net>
Link: https://lore.kernel.org/r/20240606-fix-smn-bad-read-v4-3-ffde21931c3f@amd.com


Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
Conflicts:
 drivers/hwmon/k10temp.c
Signed-off-by: default avatarWen Zhiwei <wenzhiwei@kylinos.cn>
parent b1f0d55a
Loading
Loading
Loading
Loading
+28 −10
Original line number Diff line number Diff line
@@ -159,8 +159,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)
@@ -228,6 +229,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) {
@@ -246,14 +248,18 @@ static int k10temp_read_temp(struct device *dev, u32 attr, int channel,
		case 2 ... 13:		/* Tccd{1-12} */
			if (hygon_f18h_m4h())
				hygon_read_temp(data, channel, &regval);
			else
				amd_smn_read(amd_pci_dev_to_node_id(data->pdev),
			else {
        	                ret = amd_smn_read(amd_pci_dev_to_node_id(data->pdev),
	                                           ZEN_CCD_TEMP(data->ccd_offset, channel - 2),
	                                           &regval);

				if (ret)
					return ret;
			}
			*val = (regval & ZEN_CCD_TEMP_MASK) * 125 - 49000;
			break;
		default:
			return -EOPNOTSUPP;
			return ret;
		}
		break;
	case hwmon_temp_max:
@@ -269,7 +275,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;
}
@@ -407,8 +413,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), &regval);
                /*
                 * 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), &regval))
                        continue;

		if (regval & ZEN_CCD_TEMP_VALID)
			data->show_temp |= BIT(TCCD_BIT(i));
	}