Skip to contentSkip to conversation

ACPI / hotplug / PCI: lost acpiphp_put_context in acpiphp_grab_context()

PATCH Superseded Vasily Averin 3 participants 6 messages Jun 13, 2020 – Jun 24, 2020

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

PATCH 1/1 ACPI / hotplug / PCI: lost acpiphp_put_context in acpiphp_grab_context()

Fixes edf5bf34d408 ("ACPI / dock: Use callback pointers from devices' ACPI hotplug contexts")
---
 drivers/pci/hotplug/acpiphp_glue.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/pci/hotplug/acpiphp_glue.c +2 -0
index b386995..e49ec95 100644
@@ -124,6 +124,8 @@ static struct acpiphp_context *acpiphp_grab_context(struct acpi_device *adev)
 	acpi_lock_hp_context();
 	context = acpiphp_get_context(adev);
 	if (!context || context->func.parent->is_going_away) {
+		if (context)
+			acpiphp_put_context(context);
 		acpi_unlock_hp_context();
 		return NULL;
 	}
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 2 lines · Vasily Averin
Fixes edf5bf34d408 ("ACPI / dock: Use callback pointers from devices' ACPI hotplug contexts")

Thanks for the fix ->

Quoted 10 lines
---
 drivers/pci/hotplug/acpiphp_glue.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/pci/hotplug/acpiphp_glue.c +0 -0
index b386995..e49ec95 100644

drivers/pci/hotplug/acpiphp_glue.c static struct acpiphp_context *acpiphp_grab_context(struct acpi_device *adev) · line 128

Quoted 5 lines
drivers/pci/hotplug/acpiphp_glue.c +2 -0
@@ -124,6 +124,8 @@ static struct acpiphp_context *acpiphp_grab_context(struct acpi_device *adev)
        acpi_lock_hp_context();
        context = acpiphp_get_context(adev);
        if (!context || context->func.parent->is_going_away) {
+               if (context)
+                       acpiphp_put_context(context);

-> but I'd prefer:

        if (context && context->func.parent->is_going_away) {
                      acpiphp_put_context(context);
                      context = NULL;
       }
       if (!context) {

drivers/pci/hotplug/acpiphp_glue.c static struct acpiphp_context *acpiphp_grab_context(struct acpi_device *adev)

Quoted 4 lines
drivers/pci/hotplug/acpiphp_glue.c +0 -1
                acpi_unlock_hp_context();
                return NULL;
        }
--

Thanks!

#2 lore raw

PATCH v2 1/1 ACPI / hotplug / PCI: lost acpiphp_put_context in acpiphp_grab_context()

v2: followed to rafael@'s proposal

Fixes edf5bf34d408 ("ACPI / dock: Use callback pointers from devices' ACPI hotplug contexts")
---
 drivers/pci/hotplug/acpiphp_glue.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/pci/hotplug/acpiphp_glue.c +5 -1
index b4c92cee13f8..a0923a65e636 100644
@@ -123,7 +123,11 @@ static struct acpiphp_context *acpiphp_grab_context(struct acpi_device *adev)
 
 	acpi_lock_hp_context();
 	context = acpiphp_get_context(adev);
-	if (!context || context->func.parent->is_going_away) {
+	if (context && context->func.parent->is_going_away) {
+		acpiphp_put_context(context);
+		context = NULL;
+	}
+	if (!context) {
 		acpi_unlock_hp_context();
 		return NULL;
 	}
Signature · 1 line
2.17.1
#3 lore raw
Quoted 15 lines · CEST Vasily Averin

v2: followed to rafael@'s proposal

Fixes edf5bf34d408 ("ACPI / dock: Use callback pointers from devices' ACPI hotplug contexts")
---
 drivers/pci/hotplug/acpiphp_glue.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/pci/hotplug/acpiphp_glue.c +0 -0
index b4c92cee13f8..a0923a65e636 100644
@@ -123,7 +123,11 @@ static struct acpiphp_context *acpiphp_grab_context(struct acpi_device *adev)
 
 	acpi_lock_hp_context();

drivers/pci/hotplug/acpiphp_glue.c static struct acpiphp_context *acpiphp_grab_context(struct acpi_device *adev) · line 133

Quoted 10 lines
drivers/pci/hotplug/acpiphp_glue.c +5 -1
 	context = acpiphp_get_context(adev);
-	if (!context || context->func.parent->is_going_away) {
+	if (context && context->func.parent->is_going_away) {
+		acpiphp_put_context(context);
+		context = NULL;
+	}
+	if (!context) {
 		acpi_unlock_hp_context();
 		return NULL;
 	}

Thanks for following my suggestion, but it occurred to me that it could still be done in a better way.

So instead of the above I'd prefer to apply the following change (added PCI and Bjorn for visibility):

---
From: Rafael J. Wysocki <[email protected]>
Subject: [PATCH] PCI: hotplug: ACPI: Fix context refcounting in acpiphp_grab_context()

If context is not NULL in acpiphp_grab_context(), but the is_going_away flag is set for the device's parent, the reference counter of the context needs to be decremented before returning NULL or the context will never be freed, so make that happen.

Fixes edf5bf34d408 ("ACPI / dock: Use callback pointers from devices' ACPI hotplug contexts")
Cc 3.15+ <[email protected]> # 3.15+
---
 drivers/pci/hotplug/acpiphp_glue.c |   14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/pci/hotplug/acpiphp_glue.c +11 -3
Index: linux-pm/drivers/pci/hotplug/acpiphp_glue.c
===================================================================
@@ -122,13 +122,21 @@ static struct acpiphp_context *acpiphp_g
 	struct acpiphp_context *context;
 
 	acpi_lock_hp_context();
+
 	context = acpiphp_get_context(adev);
-	if (!context || context->func.parent->is_going_away) {
-		acpi_unlock_hp_context();
-		return NULL;
+	if (!context)
+		goto unlock;
+
+	if (context->func.parent->is_going_away) {
+		acpiphp_put_context(context);
+		context = NULL;
+		goto unlock;
 	}
+
 	get_bridge(context->func.parent);
 	acpiphp_put_context(context);
+
+unlock:
 	acpi_unlock_hp_context();
 	return context;
 }
#4 lore raw
Quoted 9 lines · Rafael J. Wysocki
Quoted 3 lines · CEST Vasily Averin

v2: followed to rafael@'s proposal

Fixes edf5bf34d408 ("ACPI / dock: Use callback pointers from devices' ACPI hotplug contexts")

Thanks for following my suggestion, but it occurred to me that it could still be done in a better way.

So instead of the above I'd prefer to apply the following change (added PCI and Bjorn for visibility):

Thank you,
however could you please tell me what do you think about following variant?

 drivers/pci/hotplug/acpiphp_glue.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/pci/hotplug/acpiphp_glue.c +8 -7
index b4c92cee13f8..5875c3654b52 100644
@@ -119,16 +119,17 @@ static inline void put_bridge(struct acpiphp_bridge *bridge)
 
 static struct acpiphp_context *acpiphp_grab_context(struct acpi_device *adev)
 {
-	struct acpiphp_context *context;
+	struct acpiphp_context *c, *context = NULL;
 
 	acpi_lock_hp_context();
-	context = acpiphp_get_context(adev);
-	if (!context || context->func.parent->is_going_away) {
-		acpi_unlock_hp_context();
-		return NULL;
+	c = acpiphp_get_context(adev);
+	if (c) {
+		if (c->func.parent->is_going_away == false) {
+			get_bridge(c->func.parent);
+			context = c;
+		}
+		acpiphp_put_context(c);
 	}
-	get_bridge(context->func.parent);
-	acpiphp_put_context(context);
 	acpi_unlock_hp_context();
 	return context;
 }
#5 lore raw
Quoted 37 lines · Vasily Averin
Quoted 9 lines · Rafael J. Wysocki
Quoted 3 lines · CEST Vasily Averin

v2: followed to rafael@'s proposal

Fixes edf5bf34d408 ("ACPI / dock: Use callback pointers from devices' ACPI hotplug contexts")

Thanks for following my suggestion, but it occurred to me that it could still be done in a better way.

So instead of the above I'd prefer to apply the following change (added PCI and Bjorn for visibility):

Thank you,
however could you please tell me what do you think about following variant?

 drivers/pci/hotplug/acpiphp_glue.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/pci/hotplug/acpiphp_glue.c +4 -5
index b4c92cee13f8..5875c3654b52 100644
@@ -119,16 +119,17 @@ static inline void put_bridge(struct acpiphp_bridge *bridge)
 
 static struct acpiphp_context *acpiphp_grab_context(struct acpi_device *adev)
 {
-       struct acpiphp_context *context;
+       struct acpiphp_context *c, *context = NULL;
 
        acpi_lock_hp_context();
-       context = acpiphp_get_context(adev);
-       if (!context || context->func.parent->is_going_away) {
-               acpi_unlock_hp_context();
-               return NULL;
+       c = acpiphp_get_context(adev);
+       if (c) {
+               if (c->func.parent->is_going_away == false) {

drivers/pci/hotplug/acpiphp_glue.c static inline void put_bridge(struct acpiphp_bridge *bridge) · line 135

Quoted 10 lines
drivers/pci/hotplug/acpiphp_glue.c +4 -2
+                       get_bridge(c->func.parent);
+                       context = c;
+               }
+               acpiphp_put_context(c);
        }
-       get_bridge(context->func.parent);
-       acpiphp_put_context(context);
        acpi_unlock_hp_context();
        return context;
 }

There are reasons to do it the way I want:
- The indentation level is minimum.
- The real purpose of this function is to get a parent bridge reference, so that needs to be the central piece of it rather than the checks for exceptional cases.
- The second local var is not necessary.

Thanks!