Skip to contentSkip to conversation

PCI: Fix the order in unregister path

PATCH NAK Yajun Deng 2 participants 7 messages Aug 25, 2021 – Aug 31, 2021 PCI

lore
Share & tools
lore

Yajun Deng

author
#0 lore raw

PATCH 1/1 PCI: Fix the order in unregister path

device_del() should be called first and then called put_device() in unregister path, becase if that the final reference count, the device will be cleaned up via device_release() above. So use device_unregister() instead.

Fixes 9885440b16b8 (PCI: Fix pci_host_bridge struct device release/free handling)
---
 drivers/pci/probe.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/pci/probe.c +1 -3
index 0ec5c792c27d..abd481a15a17 100644
@@ -994,9 +994,7 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)
 	return 0;
 
 unregister:
-	put_device(&bridge->dev);
-	device_del(&bridge->dev);
-
+	device_unregister(&bridge->dev);
 free:
 	kfree(bus);
 	return err;
Signature · 1 line
2.32.0
#1 lore raw
Quoted 11 lines · Yajun Deng

device_del() should be called first and then called put_device() in unregister path, becase if that the final reference count, the device will be cleaned up via device_release() above. So use device_unregister() instead.

Fixes 9885440b16b8 (PCI: Fix pci_host_bridge struct device release/free handling)
---
 drivers/pci/probe.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

NAK.

The current code is correct. Go read the comments for device_add/device_del.

drivers/pci/probe.c static int pci_register_host_bridge(struct pci_host_bridge *bridge) · line 996

Quoted 8 lines
drivers/pci/probe.c +0 -0
index 0ec5c792c27d..abd481a15a17 100644
@@ -994,9 +994,7 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)
        return 0;
 
 unregister:

We get here if device_register() failed. Calling device_unregister() in that case is never right.

drivers/pci/probe.c static int pci_register_host_bridge(struct pci_host_bridge *bridge) · line 997

Quoted 1 line
drivers/pci/probe.c +0 -1
-       put_device(&bridge->dev);

This is for the get_device() we do above, not the get the driver core does.

drivers/pci/probe.c static int pci_register_host_bridge(struct pci_host_bridge *bridge) · line 998

Quoted 1 line
drivers/pci/probe.c +0 -1
-       device_del(&bridge->dev);

This undoes the device_add() we do following the comment: "NOTE: this should be called manually _iff_ device_add() was also called manually."

Quoted 8 lines
drivers/pci/probe.c +1 -2
-
+       device_unregister(&bridge->dev);
 free:
        kfree(bus);
        return err;
--

2.32.0

#2 lore raw
Quoted 13 lines · Rob Herring
Quoted 10 lines · Yajun Deng

device_del() should be called first and then called put_device() in unregister path, becase if that the final reference count, the device will be cleaned up via device_release() above. So use device_unregister() instead.

Fixes 9885440b16b8 (PCI: Fix pci_host_bridge struct device release/free handling)

---
drivers/pci/probe.c | 4 +---

1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 3 lines

NAK.

The current code is correct. Go read the comments for device_add/device_del.

But the device_unregister() is only contains device_del() and put_device(). It just put device_del() before put_device().

Quoted 30 lines
Quoted 9 lines
drivers/pci/probe.c +0 -0
index 0ec5c792c27d..abd481a15a17 100644
@@ -994,9 +994,7 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)
 return 0;
 

unregister:

We get here if device_register() failed. Calling device_unregister() in that case is never right.

Quoted 1 line

- put_device(&bridge->dev);

This is for the get_device() we do above, not the get the driver core does.

Quoted 1 line

- device_del(&bridge->dev);

This undoes the device_add() we do following the comment: "NOTE: this should be called manually _iff_ device_add() was also called manually."

Quoted 7 lines

-
+ device_unregister(&bridge->dev);
free:
kfree(bus);
return err;
--
2.32.0

#3 lore raw
Quoted 16 lines · On Wed, Aug 25, 2021 at 10:57 PM <[email protected]> wrote:
Quoted 13 lines · Rob Herring
Quoted 10 lines · Yajun Deng

device_del() should be called first and then called put_device() in unregister path, becase if that the final reference count, the device will be cleaned up via device_release() above. So use device_unregister() instead.

Fixes 9885440b16b8 (PCI: Fix pci_host_bridge struct device release/free handling)

---
drivers/pci/probe.c | 4 +---

1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 6 lines
Quoted 3 lines

NAK.

The current code is correct. Go read the comments for device_add/device_del.

But the device_unregister() is only contains device_del() and put_device(). It just put device_del() before put_device().

And that is the wrong order as we want to undo what the code above did. The put_device here is for the get_device we did. The put_device in device_unregister is for the get_device that device_register did (on success only).

Logically, it is wrong too to call unregister if register failed. That would be like doing this:

p = malloc(1);
if (!p)
free(p);

Rob

#4 lore raw
Quoted 29 lines · Rob Herring
Quoted 13 lines · On Wed, Aug 25, 2021 at 10:57 PM <[email protected]> wrote:

August 25, 2021 9:55 PM, "Rob Herring" <[email protected]> wrote:

On Wed, Aug 25, 2021 at 3:34 AM Yajun Deng <[email protected]> wrote:

device_del() should be called first and then called put_device() in unregister path, becase if that the final reference count, the device will be cleaned up via device_release() above. So use device_unregister() instead.

Fixes 9885440b16b8 (PCI: Fix pci_host_bridge struct device release/free handling)

---
drivers/pci/probe.c | 4 +---

Quoted 8 lines
1 file changed, 1 insertion(+), 3 deletions(-)

NAK.

The current code is correct. Go read the comments for device_add/device_del.

But the device_unregister() is only contains device_del() and put_device(). It just put device_del() before put_device().

And that is the wrong order as we want to undo what the code above did. The put_device here is for the get_device we did. The put_device in device_unregister is for the get_device that device_register did (on success only).

Quoted 7 lines

Logically, it is wrong too to call unregister if register failed. That would be like doing this:

p = malloc(1);
if (!p)
free(p);

This is the raw code:

        err = device_register(&bus->dev);
        if (err)
                goto unregister;
unregister:
        put_device(&bridge->dev);
        device_del(&bridge->dev);

This is my code:

        err = device_register(&bus->dev);
        if (err)
                goto unregister;
 unregister:
        device_unregister(&bridge->dev);

The parameter in device_register() is bus->dev, but the parameter in device_unregister() is bridge->dev.The are different. The bridge->dev is already success before called device_register().So it wouldn't be happen like your code.

Quoted 1 line

Rob

#5 lore raw
Quoted 27 lines · On Thu, Aug 26, 2021 at 9:39 PM <[email protected]> wrote:
Quoted 24 lines · Rob Herring
Quoted 21 lines · On Wed, Aug 25, 2021 at 10:57 PM <[email protected]> wrote:

August 25, 2021 9:55 PM, "Rob Herring" <[email protected]> wrote:

On Wed, Aug 25, 2021 at 3:34 AM Yajun Deng <[email protected]> wrote:

device_del() should be called first and then called put_device() in unregister path, becase if that the final reference count, the device will be cleaned up via device_release() above. So use device_unregister() instead.

Fixes 9885440b16b8 (PCI: Fix pci_host_bridge struct device release/free handling)

---
drivers/pci/probe.c | 4 +---

1 file changed, 1 insertion(+), 3 deletions(-)

NAK.

The current code is correct. Go read the comments for device_add/device_del.

But the device_unregister() is only contains device_del() and put_device(). It just put device_del() before put_device().

Quoted 7 lines
Quoted 7 lines

And that is the wrong order as we want to undo what the code above did. The put_device here is for the get_device we did. The put_device in device_unregister is for the get_device that device_register did (on success only).

Logically, it is wrong too to call unregister if register failed. That would be like doing this:

You are right that the register and unregister are different devices. However, your change is still wrong. The device_register is actually irrelevant.

Quoted 5 lines
Quoted 5 lines

p = malloc(1);
if (!p)
free(p);

Quoted 7 lines

This is the raw code:

        err = device_register(&bus->dev);
        if (err)
                goto unregister;
unregister:
        put_device(&bridge->dev);
        device_del(&bridge->dev);

The pertinent parts are this:

err = device_add(&bridge->dev); // which calls get_device() itself, so there's the first ref

if (err) {
    put_device(&bridge->dev);
    goto free;
}

bus->bridge = get_device(&bridge->dev); // This is the 2nd ref which the PCI core holds
...
unregister:

    put_device(&bridge->dev);  // This is the put for the get_device
just above here.
    device_del(&bridge->dev);  // Then this does the 2nd put.

The get_device and put_device are paired, and the device_add and device_del are paired.

As I said earlier, go read the kerneldoc for device_add. For your convenience, here's the important part:

device_add:
* Rule of thumb is: if device_add() succeeds, you should call
* device_del() when you want to get rid of it. If device_add() has
* *not* succeeded, use *only* put_device() to drop the reference
* count.

device_del:
* NOTE: this should be called manually _iff_ device_add() was
* also called manually.

Rob

#6 lore raw
Quoted 33 lines · Rob Herring
Quoted 31 lines · On Thu, Aug 26, 2021 at 9:39 PM <[email protected]> wrote:

August 26, 2021 8:01 PM, "Rob Herring" <[email protected]> wrote:

On Wed, Aug 25, 2021 at 10:57 PM <[email protected]> wrote:

August 25, 2021 9:55 PM, "Rob Herring" <[email protected]> wrote:

On Wed, Aug 25, 2021 at 3:34 AM Yajun Deng <[email protected]> wrote:

device_del() should be called first and then called put_device() in unregister path, becase if that the final reference count, the device will be cleaned up via device_release() above. So use device_unregister() instead.

Fixes 9885440b16b8 (PCI: Fix pci_host_bridge struct device release/free handling)

---
drivers/pci/probe.c | 4 +---

1 file changed, 1 insertion(+), 3 deletions(-)

NAK.

The current code is correct. Go read the comments for device_add/device_del.

But the device_unregister() is only contains device_del() and put_device(). It just put device_del() before put_device().

And that is the wrong order as we want to undo what the code above did. The put_device here is for the get_device we did. The put_device in device_unregister is for the get_device that device_register did (on success only).

Quoted 7 lines
Quoted 2 lines

Logically, it is wrong too to call unregister if register failed. That would be like doing this:

You are right that the register and unregister are different devices. However, your change is still wrong. The device_register is actually irrelevant.

OK, the original order is right, it was my mistake.

Quoted 45 lines
Quoted 11 lines

p = malloc(1);
if (!p)
free(p);

This is the raw code:
err = device_register(&bus->dev);
if (err)
goto unregister;
unregister:
put_device(&bridge->dev);
device_del(&bridge->dev);

The pertinent parts are this:

err = device_add(&bridge->dev); // which calls get_device() itself, so there's the first ref
if (err) {
put_device(&bridge->dev);
goto free;
}
bus->bridge = get_device(&bridge->dev); // This is the 2nd ref which the PCI core holds
...
unregister:
put_device(&bridge->dev); // This is the put for the get_device just above here.
device_del(&bridge->dev); // Then this does the 2nd put.

The get_device and put_device are paired, and the device_add and device_del are paired.

As I said earlier, go read the kerneldoc for device_add. For your convenience, here's the important part:

device_add:
* Rule of thumb is: if device_add() succeeds, you should call
* device_del() when you want to get rid of it. If device_add() has
* *not* succeeded, use *only* put_device() to drop the reference
* count.

device_del:
* NOTE: this should be called manually _iff_ device_add() was
* also called manually.

Rob