]> www.infradead.org Git - users/hch/misc.git/commitdiff
of: unittest: Fix device reference count leak in of_unittest_pci_node_verify
authorMa Ke <make24@iscas.ac.cn>
Tue, 30 Sep 2025 08:16:18 +0000 (16:16 +0800)
committerRob Herring (Arm) <robh@kernel.org>
Wed, 1 Oct 2025 14:33:23 +0000 (09:33 -0500)
In of_unittest_pci_node_verify(), when the add parameter is false,
device_find_any_child() obtains a reference to a child device. This
function implicitly calls get_device() to increment the device's
reference count before returning the pointer. However, the caller
fails to properly release this reference by calling put_device(),
leading to a device reference count leak. Add put_device() in the else
branch immediately after child_dev is no longer needed.

As the comment of device_find_any_child states: "NOTE: you will need
to drop the reference with put_device() after use".

Found by code review.

Cc: stable@vger.kernel.org
Fixes: 26409dd04589 ("of: unittest: Add pci_dt_testdrv pci driver")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
drivers/of/unittest.c

index e3503ec20f6cc7e271d57ef0b9f7f6c5d40922a1..388e9ec2cccf8309ef77d832d453a0cc77e97523 100644 (file)
@@ -4300,6 +4300,7 @@ static int of_unittest_pci_node_verify(struct pci_dev *pdev, bool add)
                unittest(!np, "Child device tree node is not removed\n");
                child_dev = device_find_any_child(&pdev->dev);
                unittest(!child_dev, "Child device is not removed\n");
+               put_device(child_dev);
        }
 
 failed: