Skip to contentSkip to conversation

ACPI: video: Fix refcounting in apple_gmux_backlight_present()

PATCH In review Andy Shevchenko 3 participants 10 messages Jan 23, 2023 – Jan 25, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 ACPI: video: Fix refcounting in apple_gmux_backlight_present()

acpi_dev_get_first_match_dev() gets ACPI device with the bumped refcount. The caller must drop it when it's done.

Fix ACPI device refcounting in apple_gmux_backlight_present().

Fixes 3cf3b7f012f3 ("ACPI: video: Fix Apple GMUX backlight detection")
---
 drivers/acpi/video_detect.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/acpi/video_detect.c +6 -2
index 65cec7bb6d96..0ccde0d4c527 100644
@@ -114,12 +114,14 @@ static bool apple_gmux_backlight_present(void)
 {
 	struct acpi_device *adev;
 	struct device *dev;
+	bool ret;
 
 	adev = acpi_dev_get_first_match_dev(GMUX_ACPI_HID, NULL, -1);
 	if (!adev)
 		return false;
 
-	dev = acpi_get_first_physical_node(adev);
+	dev = get_device(acpi_get_first_physical_node(adev));
+	acpi_dev_put(adev);
 	if (!dev)
 		return false;
 
@@ -127,7 +129,9 @@ static bool apple_gmux_backlight_present(void)
 	 * drivers/platform/x86/apple-gmux.c only supports old style
 	 * Apple GMUX with an IO-resource.
 	 */
-	return pnp_get_resource(to_pnp_dev(dev), IORESOURCE_IO, 0) != NULL;
+	ret = pnp_get_resource(to_pnp_dev(dev), IORESOURCE_IO, 0) != NULL;
+	put_device(dev);
+	return ret;
 }
 
 /* Force to use vendor driver when the ACPI device is known to be
Signature · 1 line
2.39.0
#1 lore raw

PATCH 2/3 ACPI: utils: Add acpi_get_first_match_physical_node()

There are drivers that are using a logic that is combined in the offered acpi_get_first_match_physical_node(). The rationale to have this helper not only redunction of the lines of code, but improving the robustness by properly handling the reference counters on the error paths.

---
 drivers/acpi/utils.c    | 28 ++++++++++++++++++++++++++++
 include/acpi/acpi_bus.h |  3 +++
 include/linux/acpi.h    |  6 ++++++
 3 files changed, 37 insertions(+)
drivers/acpi/utils.c +28 -0
index 2ea14648a661..052e263d2246 100644
@@ -965,6 +965,34 @@ acpi_dev_get_first_match_dev(const char *hid, const char *uid, s64 hrv)
 }
 EXPORT_SYMBOL(acpi_dev_get_first_match_dev);
 
+/**
+ * acpi_get_first_match_physical_node - Return the physical node of the first match of ACPI device
+ * @hid: Hardware ID of the device.
+ * @uid: Unique ID of the device, pass NULL to not check _UID
+ * @hrv: Hardware Revision of the device, pass -1 to not check _HRV
+ *
+ * Return the physical node of the first match of ACPI device if a matching
+ * device was present at the moment of invocation, or NULL otherwise.
+ *
+ * The caller is responsible for invoking put_device() on the returned device.
+ *
+ * See additional information in acpi_dev_present() as well.
+ */
+struct device *acpi_get_first_match_physical_node(const char *hid, const char *uid, s64 hrv)
+{
+	struct acpi_device *adev;
+	struct device *dev;
+
+	adev = acpi_dev_get_first_match_dev(hid, uid, hrv);
+	if (!adev)
+		return NULL;
+
+	dev = get_device(acpi_get_first_physical_node(adev));
+	acpi_dev_put(adev);
+	return dev;
+}
+EXPORT_SYMBOL(acpi_get_first_match_physical_node);
+
 /**
  * acpi_reduced_hardware - Return if this is an ACPI-reduced-hw machine
  *
include/acpi/acpi_bus.h +3 -0
index 0584e9f6e339..e62af2f71362 100644
@@ -777,6 +777,9 @@ acpi_dev_get_first_match_dev(const char *hid, const char *uid, s64 hrv);
 	     adev;							\
 	     adev = acpi_dev_get_next_match_dev(adev, hid, uid, hrv))
 
+struct device *
+acpi_get_first_match_physical_node(const char *hid, const char *uid, s64 hrv);
+
 static inline struct acpi_device *acpi_dev_get(struct acpi_device *adev)
 {
 	return adev ? to_acpi_device(get_device(&adev->dev)) : NULL;
include/linux/acpi.h +6 -0
index 4b12dad5a8a4..29bae77d819a 100644
@@ -814,6 +814,12 @@ acpi_dev_get_first_match_dev(const char *hid, const char *uid, s64 hrv)
 	return NULL;
 }
 
+static inline struct device *
+acpi_get_first_match_physical_node(const char *hid, const char *uid, s64 hrv)
+{
+	return NULL;
+}
+
 static inline bool acpi_reduced_hardware(void)
 {
 	return false;
Signature · 1 line
2.39.0
#2 lore raw

Re: [PATCH v1 2/3] ACPI: utils: Add acpi_get_first_match_physical_node()

Quoted 4 lines · Andy Shevchenko

There are drivers that are using a logic that is combined in the offered acpi_get_first_match_physical_node(). The rationale to have this helper not only redunction of the lines of code, but improving the robustness by properly handling the reference counters on the error paths.

After rebasing on top of the latest code base (with Hans' patches included) I checked the users of similar code flow and found that there is no sense to provide this helper now. It's only one user for this API as is, otherwise it needs an access to struct acpi_device, which the proposed API doesn't provide.

Hence, self-NAK.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

PATCH 3/3 ACPI: video: Switch to use acpi_get_first_match_physical_node()

In reply to Andy Shevchenko

Refactor the code to use acpi_get_first_match_physical_node(), which is currently open coded.

---
 drivers/acpi/video_detect.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/acpi/video_detect.c +1 -7
index 0ccde0d4c527..b23773347e67 100644
@@ -112,16 +112,10 @@ static bool nvidia_wmi_ec_supported(void)
 
 static bool apple_gmux_backlight_present(void)
 {
-	struct acpi_device *adev;
 	struct device *dev;
 	bool ret;
 
-	adev = acpi_dev_get_first_match_dev(GMUX_ACPI_HID, NULL, -1);
-	if (!adev)
-		return false;
-
-	dev = get_device(acpi_get_first_physical_node(adev));
-	acpi_dev_put(adev);
+	dev = acpi_get_first_match_physical_node(GMUX_ACPI_HID, NULL, -1);
 	if (!dev)
 		return false;
 
Signature · 1 line
2.39.0
#4 lore raw
In reply to Andy Shevchenko

Hi Andy,

Quoted 7 lines · Andy Shevchenko

acpi_dev_get_first_match_dev() gets ACPI device with the bumped refcount. The caller must drop it when it's done.

Fix ACPI device refcounting in apple_gmux_backlight_present().

Fixes 3cf3b7f012f3 ("ACPI: video: Fix Apple GMUX backlight detection")

Thank you for your work on this, much appreciated and I like the new acpi_get_first_match_physical_node().

But I don't think this patch is a good idea. There is a regression related to apple_gmux_backlight_present() with a patch-set fixing it pending.

And that patch-set actually removes this function. Adding a fix for this real, but not really important leak now, will just make backporting the actual fix harder.

So I would prefer for this patch to not go in and to go for (a to be submitted v2) of the patch-set fixing the regression right away instead.

Regards,

Hans

Quoted 35 lines
---
 drivers/acpi/video_detect.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/acpi/video_detect.c +6 -2
index 65cec7bb6d96..0ccde0d4c527 100644
@@ -114,12 +114,14 @@ static bool apple_gmux_backlight_present(void)
 {
 	struct acpi_device *adev;
 	struct device *dev;
+	bool ret;
 
 	adev = acpi_dev_get_first_match_dev(GMUX_ACPI_HID, NULL, -1);
 	if (!adev)
 		return false;
 
-	dev = acpi_get_first_physical_node(adev);
+	dev = get_device(acpi_get_first_physical_node(adev));
+	acpi_dev_put(adev);
 	if (!dev)
 		return false;
 
@@ -127,7 +129,9 @@ static bool apple_gmux_backlight_present(void)
 	 * drivers/platform/x86/apple-gmux.c only supports old style
 	 * Apple GMUX with an IO-resource.
 	 */
-	return pnp_get_resource(to_pnp_dev(dev), IORESOURCE_IO, 0) != NULL;
+	ret = pnp_get_resource(to_pnp_dev(dev), IORESOURCE_IO, 0) != NULL;
+	put_device(dev);
+	return ret;
 }
 
 /* Force to use vendor driver when the ACPI device is known to be
#5 lore raw
Quoted 5 lines · Hans de Goede
Quoted 4 lines · Andy Shevchenko

acpi_dev_get_first_match_dev() gets ACPI device with the bumped refcount. The caller must drop it when it's done.

Fix ACPI device refcounting in apple_gmux_backlight_present().

...

Quoted 7 lines

Thank you for your work on this, much appreciated and I like the new acpi_get_first_match_physical_node().

But I don't think this patch is a good idea. There is a regression related to apple_gmux_backlight_present() with a patch-set fixing it pending.

Quoted 7 lines

And that patch-set actually removes this function. Adding a fix for this real, but not really important leak now, will just make backporting the actual fix harder.

So I would prefer for this patch to not go in and to go for (a to be submitted v2) of the patch-set fixing the regression right away instead.

Maybe I missed something, but I noticed that you actually moved (not killed) the code which is currently in this function. If it's the case, I prefer my fix to be imported first.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
Quoted 21 lines · Andy Shevchenko
Quoted 5 lines · Hans de Goede
Quoted 4 lines · Andy Shevchenko

acpi_dev_get_first_match_dev() gets ACPI device with the bumped refcount. The caller must drop it when it's done.

Fix ACPI device refcounting in apple_gmux_backlight_present().

...

Quoted 11 lines

Thank you for your work on this, much appreciated and I like the new acpi_get_first_match_physical_node().

But I don't think this patch is a good idea. There is a regression related to apple_gmux_backlight_present() with a patch-set fixing it pending.

And that patch-set actually removes this function. Adding a fix for this real, but not really important leak now, will just make backporting the actual fix harder.

Quoted 7 lines
Quoted 3 lines

So I would prefer for this patch to not go in and to go for (a to be submitted v2) of the patch-set fixing the regression right away instead.

Maybe I missed something, but I noticed that you actually moved (not killed) the code which is currently in this function. If it's the case, I prefer my fix to be imported first.

Well, what about making the new code not leak?

That way the separate fix won't be necessary any more, will it?

#7 lore raw
Quoted 8 lines · Rafael J. Wysocki
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Hans de Goede
Quoted 4 lines · Andy Shevchenko

acpi_dev_get_first_match_dev() gets ACPI device with the bumped refcount. The caller must drop it when it's done.

Fix ACPI device refcounting in apple_gmux_backlight_present().

...

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines

Thank you for your work on this, much appreciated and I like the new acpi_get_first_match_physical_node().

But I don't think this patch is a good idea. There is a regression related to apple_gmux_backlight_present() with a patch-set fixing it pending.

And that patch-set actually removes this function. Adding a fix for this real, but not really important leak now, will just make backporting the actual fix harder.

So I would prefer for this patch to not go in and to go for (a to be submitted v2) of the patch-set fixing the regression right away instead.

Quoted 7 lines
Quoted 3 lines

Maybe I missed something, but I noticed that you actually moved (not killed) the code which is currently in this function. If it's the case, I prefer my fix to be imported first.

Well, what about making the new code not leak?

That way the separate fix won't be necessary any more, will it?

Yes, it will.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw
In reply to Andy Shevchenko

Hi,

Quoted 20 lines · Andy Shevchenko
Quoted 5 lines · Hans de Goede
Quoted 4 lines · Andy Shevchenko

acpi_dev_get_first_match_dev() gets ACPI device with the bumped refcount. The caller must drop it when it's done.

Fix ACPI device refcounting in apple_gmux_backlight_present().

...

Quoted 11 lines

Thank you for your work on this, much appreciated and I like the new acpi_get_first_match_physical_node().

But I don't think this patch is a good idea. There is a regression related to apple_gmux_backlight_present() with a patch-set fixing it pending.

And that patch-set actually removes this function. Adding a fix for this real, but not really important leak now, will just make backporting the actual fix harder.

Quoted 7 lines
Quoted 3 lines

So I would prefer for this patch to not go in and to go for (a to be submitted v2) of the patch-set fixing the regression right away instead.

Maybe I missed something, but I noticed that you actually moved (not killed) the code which is currently in this function. If it's the case, I prefer my fix to be imported first.

The code is not really moved, patch 2/3 of my patch-set factors out the detection code from drivers/platform/x86/apple-gmux.c's probe function. The new factored out code uses a similar construct as the apple_gmux_backlight_present() code (including the same leak).

Then patch 3/3 drops apple_gmux_backlight_present() and calls the new factored out probe code.

I'll fix the leak in v2 and then add the 3 patches to pdx86/fixes for the next pull-req to Linus (thus also fixing the leak).

Regards,

Hans

#9 lore raw
Quoted 7 lines · Hans de Goede
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Hans de Goede
Quoted 4 lines · Andy Shevchenko

acpi_dev_get_first_match_dev() gets ACPI device with the bumped refcount. The caller must drop it when it's done.

Fix ACPI device refcounting in apple_gmux_backlight_present().

...

Quoted 24 lines
Quoted 11 lines
Quoted 11 lines

Thank you for your work on this, much appreciated and I like the new acpi_get_first_match_physical_node().

But I don't think this patch is a good idea. There is a regression related to apple_gmux_backlight_present() with a patch-set fixing it pending.

And that patch-set actually removes this function. Adding a fix for this real, but not really important leak now, will just make backporting the actual fix harder.

Quoted 7 lines
Quoted 3 lines

So I would prefer for this patch to not go in and to go for (a to be submitted v2) of the patch-set fixing the regression right away instead.

Maybe I missed something, but I noticed that you actually moved (not killed) the code which is currently in this function. If it's the case, I prefer my fix to be imported first.

The code is not really moved, patch 2/3 of my patch-set factors out the detection code from drivers/platform/x86/apple-gmux.c's probe function. The new factored out code uses a similar construct as the apple_gmux_backlight_present() code (including the same leak).

Quoted 5 lines

Then patch 3/3 drops apple_gmux_backlight_present() and calls the new factored out probe code.

I'll fix the leak in v2 and then add the 3 patches to pdx86/fixes for the next pull-req to Linus (thus also fixing the leak).

As agreed with Rafael, thanks!

Signature · 2 lines
With Best Regards,
Andy Shevchenko