public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 0/2] Fix dwc_pcie pmu driver issues
@ 2025-02-20 12:17 Yunhui Cui
  2025-02-20 12:17 ` [PATCH v4 1/2] perf/dwc_pcie: fix some unreleased resources Yunhui Cui
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Yunhui Cui @ 2025-02-20 12:17 UTC (permalink / raw)
  To: xueshuai, renyu.zj, will, mark.rutland, linux-arm-kernel,
	linux-perf-users, linux-kernel
  Cc: 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".

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(-)

-- 
2.39.2


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v4 1/2] perf/dwc_pcie: fix some unreleased resources
  2025-02-20 12:17 [PATCH v4 0/2] Fix dwc_pcie pmu driver issues Yunhui Cui
@ 2025-02-20 12:17 ` Yunhui Cui
  2025-02-24  1:52   ` Shuai Xue
  2025-02-20 12:17 ` [PATCH v4 2/2] perf/dwc_pcie: fix duplicate pci_dev devices Yunhui Cui
  2025-03-01  7:05 ` [PATCH v4 0/2] Fix dwc_pcie pmu driver issues Will Deacon
  2 siblings, 1 reply; 6+ messages in thread
From: Yunhui Cui @ 2025-02-20 12:17 UTC (permalink / raw)
  To: xueshuai, renyu.zj, will, mark.rutland, linux-arm-kernel,
	linux-perf-users, linux-kernel
  Cc: Yunhui Cui

Release leaked resources, such as plat_dev and dev_info.

Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
 drivers/perf/dwc_pcie_pmu.c | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)

diff --git a/drivers/perf/dwc_pcie_pmu.c b/drivers/perf/dwc_pcie_pmu.c
index cccecae9823f..19fa2ba8dd67 100644
--- a/drivers/perf/dwc_pcie_pmu.c
+++ b/drivers/perf/dwc_pcie_pmu.c
@@ -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);
 }
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v4 2/2] perf/dwc_pcie: fix duplicate pci_dev devices
  2025-02-20 12:17 [PATCH v4 0/2] Fix dwc_pcie pmu driver issues Yunhui Cui
  2025-02-20 12:17 ` [PATCH v4 1/2] perf/dwc_pcie: fix some unreleased resources Yunhui Cui
@ 2025-02-20 12:17 ` Yunhui Cui
  2025-02-24  1:54   ` Shuai Xue
  2025-03-01  7:05 ` [PATCH v4 0/2] Fix dwc_pcie pmu driver issues Will Deacon
  2 siblings, 1 reply; 6+ messages in thread
From: Yunhui Cui @ 2025-02-20 12:17 UTC (permalink / raw)
  To: xueshuai, renyu.zj, will, mark.rutland, linux-arm-kernel,
	linux-perf-users, linux-kernel
  Cc: 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.

Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
 drivers/perf/dwc_pcie_pmu.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/drivers/perf/dwc_pcie_pmu.c b/drivers/perf/dwc_pcie_pmu.c
index 19fa2ba8dd67..f851e070760c 100644
--- a/drivers/perf/dwc_pcie_pmu.c
+++ b/drivers/perf/dwc_pcie_pmu.c
@@ -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,
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v4 1/2] perf/dwc_pcie: fix some unreleased resources
  2025-02-20 12:17 ` [PATCH v4 1/2] perf/dwc_pcie: fix some unreleased resources Yunhui Cui
@ 2025-02-24  1:52   ` Shuai Xue
  0 siblings, 0 replies; 6+ messages in thread
From: Shuai Xue @ 2025-02-24  1:52 UTC (permalink / raw)
  To: Yunhui Cui, renyu.zj, will, mark.rutland, linux-arm-kernel,
	linux-perf-users, linux-kernel



在 2025/2/20 20:17, Yunhui Cui 写道:
> Release leaked resources, such as plat_dev and dev_info.
> 
> Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
> ---
>   drivers/perf/dwc_pcie_pmu.c | 33 ++++++++++++++++++++++-----------
>   1 file changed, 22 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/perf/dwc_pcie_pmu.c b/drivers/perf/dwc_pcie_pmu.c
> index cccecae9823f..19fa2ba8dd67 100644
> --- a/drivers/perf/dwc_pcie_pmu.c
> +++ b/drivers/perf/dwc_pcie_pmu.c
> @@ -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);
>   }


LGTM. Thanks.

Reviewed-by: Shuai Xue <xueshuai@linux.alibaba.com>

Shuai

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v4 2/2] perf/dwc_pcie: fix duplicate pci_dev devices
  2025-02-20 12:17 ` [PATCH v4 2/2] perf/dwc_pcie: fix duplicate pci_dev devices Yunhui Cui
@ 2025-02-24  1:54   ` Shuai Xue
  0 siblings, 0 replies; 6+ messages in thread
From: Shuai Xue @ 2025-02-24  1:54 UTC (permalink / raw)
  To: Yunhui Cui, renyu.zj, will, mark.rutland, linux-arm-kernel,
	linux-perf-users, linux-kernel



在 2025/2/20 20:17, 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.
> 
> Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
> ---
>   drivers/perf/dwc_pcie_pmu.c | 18 ++++++++++++------
>   1 file changed, 12 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/perf/dwc_pcie_pmu.c b/drivers/perf/dwc_pcie_pmu.c
> index 19fa2ba8dd67..f851e070760c 100644
> --- a/drivers/perf/dwc_pcie_pmu.c
> +++ b/drivers/perf/dwc_pcie_pmu.c
> @@ -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,


LGTM. Thanks.

Reviewed-by: Shuai Xue <xueshuai@linux.alibaba.com>

Shuai

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v4 0/2] Fix dwc_pcie pmu driver issues
  2025-02-20 12:17 [PATCH v4 0/2] Fix dwc_pcie pmu driver issues Yunhui Cui
  2025-02-20 12:17 ` [PATCH v4 1/2] perf/dwc_pcie: fix some unreleased resources Yunhui Cui
  2025-02-20 12:17 ` [PATCH v4 2/2] perf/dwc_pcie: fix duplicate pci_dev devices Yunhui Cui
@ 2025-03-01  7:05 ` Will Deacon
  2 siblings, 0 replies; 6+ messages in thread
From: Will Deacon @ 2025-03-01  7:05 UTC (permalink / raw)
  To: xueshuai, renyu.zj, mark.rutland, linux-arm-kernel,
	linux-perf-users, linux-kernel, Yunhui Cui
  Cc: catalin.marinas, kernel-team, Will Deacon

On Thu, 20 Feb 2025 20:17:14 +0800, Yunhui Cui wrote:
> 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,
-- 
Will

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-03-01  7:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-20 12:17 [PATCH v4 0/2] Fix dwc_pcie pmu driver issues Yunhui Cui
2025-02-20 12:17 ` [PATCH v4 1/2] perf/dwc_pcie: fix some unreleased resources Yunhui Cui
2025-02-24  1:52   ` Shuai Xue
2025-02-20 12:17 ` [PATCH v4 2/2] perf/dwc_pcie: fix duplicate pci_dev devices Yunhui Cui
2025-02-24  1:54   ` Shuai Xue
2025-03-01  7:05 ` [PATCH v4 0/2] Fix dwc_pcie pmu driver issues Will Deacon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox