Skip to content

Commit 8c17bbf

Browse files
shuahkhjoergroedel
authored andcommitted
iommu/amd: Fix IOMMU perf counter clobbering during init
init_iommu_perf_ctr() clobbers the register when it checks write access to IOMMU perf counters and fails to restore when they are writable. Add save and restore to fix it. Signed-off-by: Shuah Khan <[email protected]> Fixes: 30861dd ("perf/x86/amd: Add IOMMU Performance Counter resource management") Reviewed-by: Suravee Suthikulpanit <[email protected]> Tested-by: Suravee Suthikulpanit <[email protected]> Signed-off-by: Joerg Roedel <[email protected]>
1 parent bf708cf commit 8c17bbf

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

drivers/iommu/amd_iommu_init.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,27 +1655,39 @@ static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
16551655
static void init_iommu_perf_ctr(struct amd_iommu *iommu)
16561656
{
16571657
struct pci_dev *pdev = iommu->dev;
1658-
u64 val = 0xabcd, val2 = 0;
1658+
u64 val = 0xabcd, val2 = 0, save_reg = 0;
16591659

16601660
if (!iommu_feature(iommu, FEATURE_PC))
16611661
return;
16621662

16631663
amd_iommu_pc_present = true;
16641664

1665+
/* save the value to restore, if writable */
1666+
if (iommu_pc_get_set_reg(iommu, 0, 0, 0, &save_reg, false))
1667+
goto pc_false;
1668+
16651669
/* Check if the performance counters can be written to */
16661670
if ((iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true)) ||
16671671
(iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false)) ||
1668-
(val != val2)) {
1669-
pci_err(pdev, "Unable to write to IOMMU perf counter.\n");
1670-
amd_iommu_pc_present = false;
1671-
return;
1672-
}
1672+
(val != val2))
1673+
goto pc_false;
1674+
1675+
/* restore */
1676+
if (iommu_pc_get_set_reg(iommu, 0, 0, 0, &save_reg, true))
1677+
goto pc_false;
16731678

16741679
pci_info(pdev, "IOMMU performance counters supported\n");
16751680

16761681
val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
16771682
iommu->max_banks = (u8) ((val >> 12) & 0x3f);
16781683
iommu->max_counters = (u8) ((val >> 7) & 0xf);
1684+
1685+
return;
1686+
1687+
pc_false:
1688+
pci_err(pdev, "Unable to read/write to IOMMU perf counter.\n");
1689+
amd_iommu_pc_present = false;
1690+
return;
16791691
}
16801692

16811693
static ssize_t amd_iommu_show_cap(struct device *dev,

0 commit comments

Comments
 (0)