Skip to contentSkip to conversation

Fix dwc_pcie pmu driver issues

PATCH Applied Yunhui Cui 3 participants 6 messages Feb 20, 2025 – Mar 1, 2025

Subsystems (2)
lore
Share & tools
lore

Yunhui Cui

author
#0 lore raw

v4 cover Fix dwc_pcie pmu driver issues

v1:
Only sent the patch titled "fix duplicate pci_dev devices".

v2:
1. New patch: "fix the incorrect reference count".
2. The "fix duplicate pci_dev devices" change is made in two places:
One is to remove the modification of the PMU name. The other is to change "for_each_pci_dev" to "pci_get_domain_bus_and_slot".

v3:
patch[1]: Remove the logic of "fixing the reference count". patch[2]: When pr_err, also output the sbdf.

v4:
patch[2]: Restore "platform_dwc_pcie" to "dwc_pcie_pmu".

Yunhui Cui (2):
perf/dwc_pcie: fix some unreleased resources
perf/dwc_pcie: fix duplicate pci_dev devices

 drivers/perf/dwc_pcie_pmu.c | 51 ++++++++++++++++++++++++-------------
 1 file changed, 34 insertions(+), 17 deletions(-)
Signature · 1 line
2.39.2
author
#1 lore raw

PATCH v4 1/2 perf/dwc_pcie: fix some unreleased resources

Release leaked resources, such as plat_dev and dev_info.

---
 drivers/perf/dwc_pcie_pmu.c | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)
drivers/perf/dwc_pcie_pmu.c +22 -11
index cccecae9823f..19fa2ba8dd67 100644
@@ -572,8 +572,10 @@ static int dwc_pcie_register_dev(struct pci_dev *pdev)
 		return PTR_ERR(plat_dev);
 
 	dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL);
-	if (!dev_info)
+	if (!dev_info) {
+		platform_device_unregister(plat_dev);
 		return -ENOMEM;
+	}
 
 	/* Cache platform device to handle pci device hotplug */
 	dev_info->plat_dev = plat_dev;
@@ -730,6 +732,15 @@ static struct platform_driver dwc_pcie_pmu_driver = {
 	.driver = {.name = "dwc_pcie_pmu",},
 };
 
+static void dwc_pcie_cleanup_devices(void)
+{
+	struct dwc_pcie_dev_info *dev_info, *tmp;
+
+	list_for_each_entry_safe(dev_info, tmp, &dwc_pcie_dev_info_head, dev_node) {
+		dwc_pcie_unregister_dev(dev_info);
+	}
+}
+
 static int __init dwc_pcie_pmu_init(void)
 {
 	struct pci_dev *pdev = NULL;
@@ -742,7 +753,7 @@ static int __init dwc_pcie_pmu_init(void)
 		ret = dwc_pcie_register_dev(pdev);
 		if (ret) {
 			pci_dev_put(pdev);
-			return ret;
+			goto err_cleanup;
 		}
 	}
 
@@ -751,35 +762,35 @@ static int __init dwc_pcie_pmu_init(void)
 				      dwc_pcie_pmu_online_cpu,
 				      dwc_pcie_pmu_offline_cpu);
 	if (ret < 0)
-		return ret;
+		goto err_cleanup;
 
 	dwc_pcie_pmu_hp_state = ret;
 
 	ret = platform_driver_register(&dwc_pcie_pmu_driver);
 	if (ret)
-		goto platform_driver_register_err;
+		goto err_remove_cpuhp;
 
 	ret = bus_register_notifier(&pci_bus_type, &dwc_pcie_pmu_nb);
 	if (ret)
-		goto platform_driver_register_err;
+		goto err_unregister_driver;
 	notify = true;
 
 	return 0;
 
-platform_driver_register_err:
+err_unregister_driver:
+	platform_driver_unregister(&dwc_pcie_pmu_driver);
+err_remove_cpuhp:
 	cpuhp_remove_multi_state(dwc_pcie_pmu_hp_state);
-
+err_cleanup:
+	dwc_pcie_cleanup_devices();
 	return ret;
 }
 
 static void __exit dwc_pcie_pmu_exit(void)
 {
-	struct dwc_pcie_dev_info *dev_info, *tmp;
-
 	if (notify)
 		bus_unregister_notifier(&pci_bus_type, &dwc_pcie_pmu_nb);
-	list_for_each_entry_safe(dev_info, tmp, &dwc_pcie_dev_info_head, dev_node)
-		dwc_pcie_unregister_dev(dev_info);
+	dwc_pcie_cleanup_devices();
 	platform_driver_unregister(&dwc_pcie_pmu_driver);
 	cpuhp_remove_multi_state(dwc_pcie_pmu_hp_state);
 }
Signature · 1 line
2.39.2
maintainer Reviewed-by on v4 1/2
#2 lore raw

Re: [PATCH v4 1/2] perf/dwc_pcie: fix some unreleased resources

Quoted 83 lines · Yunhui Cui

Release leaked resources, such as plat_dev and dev_info.

---
  drivers/perf/dwc_pcie_pmu.c | 33 ++++++++++++++++++++++-----------
  1 file changed, 22 insertions(+), 11 deletions(-)
drivers/perf/dwc_pcie_pmu.c +21 -7
index cccecae9823f..19fa2ba8dd67 100644
@@ -572,8 +572,10 @@ static int dwc_pcie_register_dev(struct pci_dev *pdev)
  		return PTR_ERR(plat_dev);
 
  	dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL);
-	if (!dev_info)
+	if (!dev_info) {
+		platform_device_unregister(plat_dev);
  		return -ENOMEM;
+	}
 
  	/* Cache platform device to handle pci device hotplug */
  	dev_info->plat_dev = plat_dev;
@@ -730,6 +732,15 @@ static struct platform_driver dwc_pcie_pmu_driver = {
  	.driver = {.name = "dwc_pcie_pmu",},
  };
 
+static void dwc_pcie_cleanup_devices(void)
+{
+	struct dwc_pcie_dev_info *dev_info, *tmp;
+
+	list_for_each_entry_safe(dev_info, tmp, &dwc_pcie_dev_info_head, dev_node) {
+		dwc_pcie_unregister_dev(dev_info);
+	}
+}
+
  static int __init dwc_pcie_pmu_init(void)
  {
  	struct pci_dev *pdev = NULL;
@@ -742,7 +753,7 @@ static int __init dwc_pcie_pmu_init(void)
  		ret = dwc_pcie_register_dev(pdev);
  		if (ret) {
  			pci_dev_put(pdev);
-			return ret;
+			goto err_cleanup;
  		}
  	}
 
@@ -751,35 +762,35 @@ static int __init dwc_pcie_pmu_init(void)
  				      dwc_pcie_pmu_online_cpu,
  				      dwc_pcie_pmu_offline_cpu);
  	if (ret < 0)
-		return ret;
+		goto err_cleanup;
 
  	dwc_pcie_pmu_hp_state = ret;
 
  	ret = platform_driver_register(&dwc_pcie_pmu_driver);
  	if (ret)
-		goto platform_driver_register_err;
+		goto err_remove_cpuhp;
 
  	ret = bus_register_notifier(&pci_bus_type, &dwc_pcie_pmu_nb);
  	if (ret)
-		goto platform_driver_register_err;
+		goto err_unregister_driver;
  	notify = true;
 
  	return 0;
 
-platform_driver_register_err:
+err_unregister_driver:
+	platform_driver_unregister(&dwc_pcie_pmu_driver);
+err_remove_cpuhp:
  	cpuhp_remove_multi_state(dwc_pcie_pmu_hp_state);
-
+err_cleanup:
+	dwc_pcie_cleanup_devices();
  	return ret;
  }
 
  static void __exit dwc_pcie_pmu_exit(void)
  {

drivers/perf/dwc_pcie_pmu.c static int __init dwc_pcie_pmu_init(void) · line 796

Quoted 10 lines
drivers/perf/dwc_pcie_pmu.c +1 -4
-	struct dwc_pcie_dev_info *dev_info, *tmp;
-
  	if (notify)
  		bus_unregister_notifier(&pci_bus_type, &dwc_pcie_pmu_nb);
-	list_for_each_entry_safe(dev_info, tmp, &dwc_pcie_dev_info_head, dev_node)
-		dwc_pcie_unregister_dev(dev_info);
+	dwc_pcie_cleanup_devices();
  	platform_driver_unregister(&dwc_pcie_pmu_driver);
  	cpuhp_remove_multi_state(dwc_pcie_pmu_hp_state);
  }

LGTM. Thanks.

Shuai

author
#3 lore raw

PATCH v4 2/2 perf/dwc_pcie: fix duplicate pci_dev devices

In reply to Yunhui Cui

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.

---
 drivers/perf/dwc_pcie_pmu.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
drivers/perf/dwc_pcie_pmu.c +12 -6
index 19fa2ba8dd67..f851e070760c 100644
@@ -565,9 +565,7 @@ static int dwc_pcie_register_dev(struct pci_dev *pdev)
 	u32 sbdf;
 
 	sbdf = (pci_domain_nr(pdev->bus) << 16) | PCI_DEVID(pdev->bus->number, pdev->devfn);
-	plat_dev = platform_device_register_data(NULL, "dwc_pcie_pmu", sbdf,
-						 pdev, sizeof(*pdev));
-
+	plat_dev = platform_device_register_simple("dwc_pcie_pmu", sbdf, NULL, 0);
 	if (IS_ERR(plat_dev))
 		return PTR_ERR(plat_dev);
 
@@ -616,18 +614,26 @@ static struct notifier_block dwc_pcie_pmu_nb = {
 
 static int dwc_pcie_pmu_probe(struct platform_device *plat_dev)
 {
-	struct pci_dev *pdev = plat_dev->dev.platform_data;
+	struct pci_dev *pdev;
 	struct dwc_pcie_pmu *pcie_pmu;
 	char *name;
 	u32 sbdf;
 	u16 vsec;
 	int ret;
 
+	sbdf = plat_dev->id;
+	pdev = pci_get_domain_bus_and_slot(sbdf >> 16, PCI_BUS_NUM(sbdf & 0xffff),
+					   sbdf & 0xff);
+	if (!pdev) {
+		pr_err("No pdev found for the sbdf 0x%x\n", sbdf);
+		return -ENODEV;
+	}
+
 	vsec = dwc_pcie_des_cap(pdev);
 	if (!vsec)
 		return -ENODEV;
 
-	sbdf = plat_dev->id;
+	pci_dev_put(pdev);
 	name = devm_kasprintf(&plat_dev->dev, GFP_KERNEL, "dwc_rootport_%x", sbdf);
 	if (!name)
 		return -ENOMEM;
@@ -642,7 +648,7 @@ static int dwc_pcie_pmu_probe(struct platform_device *plat_dev)
 	pcie_pmu->on_cpu = -1;
 	pcie_pmu->pmu = (struct pmu){
 		.name		= name,
-		.parent		= &pdev->dev,
+		.parent		= &plat_dev->dev,
 		.module		= THIS_MODULE,
 		.attr_groups	= dwc_pcie_attr_groups,
 		.capabilities	= PERF_PMU_CAP_NO_EXCLUDE,
Signature · 1 line
2.39.2
maintainer Reviewed-by on v4 2/2
#4 lore raw

Re: [PATCH v4 2/2] perf/dwc_pcie: fix duplicate pci_dev devices

Quoted 55 lines · Yunhui Cui

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.

---
  drivers/perf/dwc_pcie_pmu.c | 18 ++++++++++++------
  1 file changed, 12 insertions(+), 6 deletions(-)
drivers/perf/dwc_pcie_pmu.c +11 -5
index 19fa2ba8dd67..f851e070760c 100644
@@ -565,9 +565,7 @@ static int dwc_pcie_register_dev(struct pci_dev *pdev)
  	u32 sbdf;
 
  	sbdf = (pci_domain_nr(pdev->bus) << 16) | PCI_DEVID(pdev->bus->number, pdev->devfn);
-	plat_dev = platform_device_register_data(NULL, "dwc_pcie_pmu", sbdf,
-						 pdev, sizeof(*pdev));
-
+	plat_dev = platform_device_register_simple("dwc_pcie_pmu", sbdf, NULL, 0);
  	if (IS_ERR(plat_dev))
  		return PTR_ERR(plat_dev);
 
@@ -616,18 +614,26 @@ static struct notifier_block dwc_pcie_pmu_nb = {
 
  static int dwc_pcie_pmu_probe(struct platform_device *plat_dev)
  {
-	struct pci_dev *pdev = plat_dev->dev.platform_data;
+	struct pci_dev *pdev;
  	struct dwc_pcie_pmu *pcie_pmu;
  	char *name;
  	u32 sbdf;
  	u16 vsec;
  	int ret;
 
+	sbdf = plat_dev->id;
+	pdev = pci_get_domain_bus_and_slot(sbdf >> 16, PCI_BUS_NUM(sbdf & 0xffff),
+					   sbdf & 0xff);
+	if (!pdev) {
+		pr_err("No pdev found for the sbdf 0x%x\n", sbdf);
+		return -ENODEV;
+	}
+
  	vsec = dwc_pcie_des_cap(pdev);
  	if (!vsec)
  		return -ENODEV;
 
-	sbdf = plat_dev->id;
+	pci_dev_put(pdev);
  	name = devm_kasprintf(&plat_dev->dev, GFP_KERNEL, "dwc_rootport_%x", sbdf);
  	if (!name)
  		return -ENOMEM;

drivers/perf/dwc_pcie_pmu.c static int dwc_pcie_pmu_probe(struct platform_device *plat_dev) · line 654

Quoted 8 lines
drivers/perf/dwc_pcie_pmu.c +1 -1
@@ -642,7 +648,7 @@ static int dwc_pcie_pmu_probe(struct platform_device *plat_dev)
  	pcie_pmu->on_cpu = -1;
  	pcie_pmu->pmu = (struct pmu){
  		.name		= name,
-		.parent		= &pdev->dev,
+		.parent		= &plat_dev->dev,
  		.module		= THIS_MODULE,
  		.attr_groups	= dwc_pcie_attr_groups,
  		.capabilities	= PERF_PMU_CAP_NO_EXCLUDE,

LGTM. Thanks.

Shuai

maintainer Applied
#5 lore raw
In reply to Yunhui Cui
Quoted 10 lines · Yunhui Cui

v1:
Only sent the patch titled "fix duplicate pci_dev devices".

v2:
1. New patch: "fix the incorrect reference count".
2. The "fix duplicate pci_dev devices" change is made in two places:
One is to remove the modification of the PMU name. The other is to change "for_each_pci_dev" to "pci_get_domain_bus_and_slot".

[...]

Applied to will (for-next/perf), thanks!

[1/2] perf/dwc_pcie: fix some unreleased resources

      https://git.kernel.org/will/c/6eb1e8ef586a
[2/2] perf/dwc_pcie: fix duplicate pci_dev devices
      https://git.kernel.org/will/c/7f35b429802a

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev