Skip to content

Commit 7f35b42

Browse files
cuiyunhuiwilldeacon
authored andcommitted
perf/dwc_pcie: fix duplicate pci_dev devices
During platform_device_register, wrongly using struct device pci_dev as platform_data caused a kmemdup copy of pci_dev. Worse still, accessing the duplicated device leads to list corruption as its mutex content (e.g., list, magic) remains the same as the original. Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com> Reviewed-by: Shuai Xue <xueshuai@linux.alibaba.com> Link: https://lore.kernel.org/r/20250220121716.50324-3-cuiyunhui@bytedance.com Signed-off-by: Will Deacon <will@kernel.org>
1 parent 6eb1e8e commit 7f35b42

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

drivers/perf/dwc_pcie_pmu.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,7 @@ static int dwc_pcie_register_dev(struct pci_dev *pdev)
565565
u32 sbdf;
566566

567567
sbdf = (pci_domain_nr(pdev->bus) << 16) | PCI_DEVID(pdev->bus->number, pdev->devfn);
568-
plat_dev = platform_device_register_data(NULL, "dwc_pcie_pmu", sbdf,
569-
pdev, sizeof(*pdev));
570-
568+
plat_dev = platform_device_register_simple("dwc_pcie_pmu", sbdf, NULL, 0);
571569
if (IS_ERR(plat_dev))
572570
return PTR_ERR(plat_dev);
573571

@@ -616,18 +614,26 @@ static struct notifier_block dwc_pcie_pmu_nb = {
616614

617615
static int dwc_pcie_pmu_probe(struct platform_device *plat_dev)
618616
{
619-
struct pci_dev *pdev = plat_dev->dev.platform_data;
617+
struct pci_dev *pdev;
620618
struct dwc_pcie_pmu *pcie_pmu;
621619
char *name;
622620
u32 sbdf;
623621
u16 vsec;
624622
int ret;
625623

624+
sbdf = plat_dev->id;
625+
pdev = pci_get_domain_bus_and_slot(sbdf >> 16, PCI_BUS_NUM(sbdf & 0xffff),
626+
sbdf & 0xff);
627+
if (!pdev) {
628+
pr_err("No pdev found for the sbdf 0x%x\n", sbdf);
629+
return -ENODEV;
630+
}
631+
626632
vsec = dwc_pcie_des_cap(pdev);
627633
if (!vsec)
628634
return -ENODEV;
629635

630-
sbdf = plat_dev->id;
636+
pci_dev_put(pdev);
631637
name = devm_kasprintf(&plat_dev->dev, GFP_KERNEL, "dwc_rootport_%x", sbdf);
632638
if (!name)
633639
return -ENOMEM;
@@ -642,7 +648,7 @@ static int dwc_pcie_pmu_probe(struct platform_device *plat_dev)
642648
pcie_pmu->on_cpu = -1;
643649
pcie_pmu->pmu = (struct pmu){
644650
.name = name,
645-
.parent = &pdev->dev,
651+
.parent = &plat_dev->dev,
646652
.module = THIS_MODULE,
647653
.attr_groups = dwc_pcie_attr_groups,
648654
.capabilities = PERF_PMU_CAP_NO_EXCLUDE,

0 commit comments

Comments
 (0)