Skip to contentSkip to conversation

platform/x86: intel_pmc_core: do not create a static struct device

PATCH In review Greg Kroah-Hartman 3 participants 3 messages Sep 23, 2020 – Sep 24, 2020 x86 platform drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/x86: intel_pmc_core: do not create a static struct device

A struct device is a dynamic structure, with reference counting. "Tricking" the kernel to make a dynamic structure static, by working around the driver core release detection logic, is not nice.

Because of this, this code has been used as an example for others on "how to do things", which is just about the worst thing possible to have happen.

Fix this all up by making the platform device dynamic and providing a real release function.

Cc Rajneesh Bhardwaj <[email protected]>
Cc Vishwanath Somayaji <[email protected]>
Cc Darren Hart <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Rajat Jain <[email protected]>
Fixes b02f6a2ef0a1 ("platform/x86: intel_pmc_core: Attach using APCI HID "INT33A1"")
---
 drivers/platform/x86/intel_pmc_core_pltdrv.c | 26 +++++++++++++-------
 1 file changed, 17 insertions(+), 9 deletions(-)
drivers/platform/x86/intel_pmc_core_pltdrv.c +17 -9
index 731281855cc8..73797680b895 100644
@@ -20,15 +20,10 @@
 
 static void intel_pmc_core_release(struct device *dev)
 {
-	/* Nothing to do. */
+	kfree(dev);
 }
 
-static struct platform_device pmc_core_device = {
-	.name = "intel_pmc_core",
-	.dev  = {
-		.release = intel_pmc_core_release,
-	},
-};
+static struct platform_device *pmc_core_device;
 
 /*
  * intel_pmc_core_platform_ids is the list of platforms where we want to
@@ -52,6 +47,8 @@ MODULE_DEVICE_TABLE(x86cpu, intel_pmc_core_platform_ids);
 
 static int __init pmc_core_platform_init(void)
 {
+	int retval;
+
 	/* Skip creating the platform device if ACPI already has a device */
 	if (acpi_dev_present("INT33A1", NULL, -1))
 		return -ENODEV;
@@ -59,12 +56,23 @@ static int __init pmc_core_platform_init(void)
 	if (!x86_match_cpu(intel_pmc_core_platform_ids))
 		return -ENODEV;
 
-	return platform_device_register(&pmc_core_device);
+	pmc_core_device = kzalloc(sizeof(*pmc_core_device), GFP_KERNEL);
+	if (!pmc_core_device)
+		return -ENOMEM;
+
+	pmc_core_device->name = "intel_pmc_core";
+	pmc_core_device->dev.release = intel_pmc_core_release;
+
+	retval = platform_device_register(pmc_core_device);
+	if (retval)
+		kfree(pmc_core_device);
+
+	return retval;
 }
 
 static void __exit pmc_core_platform_exit(void)
 {
-	platform_device_unregister(&pmc_core_device);
+	platform_device_unregister(pmc_core_device);
 }
 
 module_init(pmc_core_platform_init);
Signature · 1 line
2.28.0
Acked-by on v1 1/1
#1 lore raw
Quoted 12 lines · Greg Kroah-Hartman

A struct device is a dynamic structure, with reference counting. "Tricking" the kernel to make a dynamic structure static, by working around the driver core release detection logic, is not nice.

Because of this, this code has been used as an example for others on "how to do things", which is just about the worst thing possible to have happen.

Fix this all up by making the platform device dynamic and providing a real release function.

Quoted 10 lines
Cc Rajneesh Bhardwaj <[email protected]>
Cc Vishwanath Somayaji <[email protected]>
Cc Darren Hart <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Rajat Jain <[email protected]>
Fixes b02f6a2ef0a1 ("platform/x86: intel_pmc_core: Attach using APCI HID "INT33A1"")
Quoted 64 lines
---
 drivers/platform/x86/intel_pmc_core_pltdrv.c | 26 +++++++++++++-------
 1 file changed, 17 insertions(+), 9 deletions(-)
drivers/platform/x86/intel_pmc_core_pltdrv.c +17 -10
index 731281855cc8..73797680b895 100644
@@ -20,15 +20,10 @@
 
 static void intel_pmc_core_release(struct device *dev)
 {
-       /* Nothing to do. */
+       kfree(dev);
 }
 
-static struct platform_device pmc_core_device = {
-       .name = "intel_pmc_core",
-       .dev  = {
-               .release = intel_pmc_core_release,
-       },
-};
+static struct platform_device *pmc_core_device;
 
 /*
  * intel_pmc_core_platform_ids is the list of platforms where we want to
@@ -52,6 +47,8 @@ MODULE_DEVICE_TABLE(x86cpu, intel_pmc_core_platform_ids);
 
 static int __init pmc_core_platform_init(void)
 {
+       int retval;
+
        /* Skip creating the platform device if ACPI already has a device */
        if (acpi_dev_present("INT33A1", NULL, -1))
                return -ENODEV;
@@ -59,12 +56,23 @@ static int __init pmc_core_platform_init(void)
        if (!x86_match_cpu(intel_pmc_core_platform_ids))
                return -ENODEV;
 
-       return platform_device_register(&pmc_core_device);
+       pmc_core_device = kzalloc(sizeof(*pmc_core_device), GFP_KERNEL);
+       if (!pmc_core_device)
+               return -ENOMEM;
+
+       pmc_core_device->name = "intel_pmc_core";
+       pmc_core_device->dev.release = intel_pmc_core_release;
+
+       retval = platform_device_register(pmc_core_device);
+       if (retval)
+               kfree(pmc_core_device);
+
+       return retval;
 }
 
 static void __exit pmc_core_platform_exit(void)
 {
-       platform_device_unregister(&pmc_core_device);
+       platform_device_unregister(pmc_core_device);
 }
 
 module_init(pmc_core_platform_init);
--

2.28.0

Reviewed-by on v1 1/1
#2 lore raw
In reply to Greg Kroah-Hartman

Hi,

Quoted 11 lines · Greg Kroah-Hartman

A struct device is a dynamic structure, with reference counting. "Tricking" the kernel to make a dynamic structure static, by working around the driver core release detection logic, is not nice.

Because of this, this code has been used as an example for others on "how to do things", which is just about the worst thing possible to have happen.

Fix this all up by making the platform device dynamic and providing a real release function.

Quoted 10 lines
Cc Rajneesh Bhardwaj <[email protected]>
Cc Vishwanath Somayaji <[email protected]>
Cc Darren Hart <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Rajat Jain <[email protected]>
Fixes b02f6a2ef0a1 ("platform/x86: intel_pmc_core: Attach using APCI HID "INT33A1"")

Patch looks good to me:

Regards,

Hans

Quoted 62 lines
---
  drivers/platform/x86/intel_pmc_core_pltdrv.c | 26 +++++++++++++-------
  1 file changed, 17 insertions(+), 9 deletions(-)
drivers/platform/x86/intel_pmc_core_pltdrv.c +17 -9
index 731281855cc8..73797680b895 100644
@@ -20,15 +20,10 @@
 
  static void intel_pmc_core_release(struct device *dev)
  {
-	/* Nothing to do. */
+	kfree(dev);
  }
 
-static struct platform_device pmc_core_device = {
-	.name = "intel_pmc_core",
-	.dev  = {
-		.release = intel_pmc_core_release,
-	},
-};
+static struct platform_device *pmc_core_device;
 
  /*
   * intel_pmc_core_platform_ids is the list of platforms where we want to
@@ -52,6 +47,8 @@ MODULE_DEVICE_TABLE(x86cpu, intel_pmc_core_platform_ids);
 
  static int __init pmc_core_platform_init(void)
  {
+	int retval;
+
  	/* Skip creating the platform device if ACPI already has a device */
  	if (acpi_dev_present("INT33A1", NULL, -1))
  		return -ENODEV;
@@ -59,12 +56,23 @@ static int __init pmc_core_platform_init(void)
  	if (!x86_match_cpu(intel_pmc_core_platform_ids))
  		return -ENODEV;
 
-	return platform_device_register(&pmc_core_device);
+	pmc_core_device = kzalloc(sizeof(*pmc_core_device), GFP_KERNEL);
+	if (!pmc_core_device)
+		return -ENOMEM;
+
+	pmc_core_device->name = "intel_pmc_core";
+	pmc_core_device->dev.release = intel_pmc_core_release;
+
+	retval = platform_device_register(pmc_core_device);
+	if (retval)
+		kfree(pmc_core_device);
+
+	return retval;
  }
 
  static void __exit pmc_core_platform_exit(void)
  {
-	platform_device_unregister(&pmc_core_device);
+	platform_device_unregister(pmc_core_device);
  }
 
  module_init(pmc_core_platform_init);