Skip to contentSkip to conversation

drm: Fix PCI device refcount leak

PATCH Applied Xiongfeng Wang 3 participants 9 messages Nov 22, 2022 – Nov 25, 2022

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

cover drm: Fix PCI device refcount leak

As comment of pci_get_class() says, it returns a pci_device with its refcount increased and decreased the refcount for the input parameter @from if it is not NULL.

If we use pci_get_class() to iterate all the PCI device of a certain class, when we find the expected device and break the iteration loop, the refcount of the found PCI device is increased. When finish using the PCI device, we need to call pci_dev_put() to decrease the refcount.

Xiongfeng Wang (2):
drm/radeon: Fix PCI device refcount leak in radeon_atrm_get_bios() drm/amdgpu: Fix PCI device refcount leak in amdgpu_atrm_get_bios()

 drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c | 1 +
 drivers/gpu/drm/radeon/radeon_bios.c     | 1 +
 2 files changed, 2 insertions(+)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/2 drm/radeon: Fix PCI device refcount leak in radeon_atrm_get_bios()

As comment of pci_get_class() says, it returns a pci_device with its refcount increased and decreased the refcount for the input parameter @from if it is not NULL.

If we break the loop in radeon_atrm_get_bios() with 'pdev' not NULL, we need to call pci_dev_put() to decrease the refcount. Add the missing pci_dev_put() to avoid refcount leak.

Fixes d8ade3526b2a ("drm/radeon: handle non-VGA class pci devices with ATRM")
Fixes c61e2775873f ("drm/radeon: split ATRM support out from the ATPX handler (v3)")
---
 drivers/gpu/drm/radeon/radeon_bios.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/radeon/radeon_bios.c +1 -0
index 33121655d50b..2df6ce3e32cb 100644
@@ -227,6 +227,7 @@ static bool radeon_atrm_get_bios(struct radeon_device *rdev)
 
 	if (!found)
 		return false;
+	pci_dev_put(pdev);
 
 	rdev->bios = kmalloc(size, GFP_KERNEL);
 	if (!rdev->bios) {
Signature · 1 line
2.20.1
#2 lore raw

Re: [PATCH 1/2] drm/radeon: Fix PCI device refcount leak in radeon_atrm_get_bios()

Quoted 8 lines · Xiongfeng Wang

As comment of pci_get_class() says, it returns a pci_device with its refcount increased and decreased the refcount for the input parameter @from if it is not NULL.

If we break the loop in radeon_atrm_get_bios() with 'pdev' not NULL, we need to call pci_dev_put() to decrease the refcount. Add the missing pci_dev_put() to avoid refcount leak.

For both patches, I think pci_dev_put() needs to go into the loops. There are 2 or more GPUs on the systems where this is relevant.

Alex

Quoted 23 lines
Fixes d8ade3526b2a ("drm/radeon: handle non-VGA class pci devices with ATRM")
Fixes c61e2775873f ("drm/radeon: split ATRM support out from the ATPX handler (v3)")
---
 drivers/gpu/drm/radeon/radeon_bios.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/radeon/radeon_bios.c +1 -1
index 33121655d50b..2df6ce3e32cb 100644
@@ -227,6 +227,7 @@ static bool radeon_atrm_get_bios(struct radeon_device *rdev)
 
        if (!found)
                return false;
+       pci_dev_put(pdev);
 
        rdev->bios = kmalloc(size, GFP_KERNEL);
        if (!rdev->bios) {
--

2.20.1

#3 lore raw

Re: [PATCH 1/2] drm/radeon: Fix PCI device refcount leak in radeon_atrm_get_bios()

Quoted 7 lines · Alex Deucher
Quoted 5 lines · Xiongfeng Wang

As comment of pci_get_class() says, it returns a pci_device with its refcount increased and decreased the refcount for the input parameter @from if it is not NULL.

Quoted 7 lines
Quoted 3 lines

If we break the loop in radeon_atrm_get_bios() with 'pdev' not NULL, we need to call pci_dev_put() to decrease the refcount. Add the missing pci_dev_put() to avoid refcount leak.

For both patches, I think pci_dev_put() needs to go into the loops. There are 2 or more GPUs on the systems where this is relevant.

As per the logic, the loop breaks when it finds a valid ATRM handle. So dev_put is required only for that device.

When inside the loop this happens - "decreased the refcount for the input parameter @from if it is not NULL"

Thanks,
Lijo

Quoted 25 lines

Alex

Quoted 23 lines
Fixes d8ade3526b2a ("drm/radeon: handle non-VGA class pci devices with ATRM")
Fixes c61e2775873f ("drm/radeon: split ATRM support out from the ATPX handler (v3)")
---
  drivers/gpu/drm/radeon/radeon_bios.c | 1 +
  1 file changed, 1 insertion(+)
drivers/gpu/drm/radeon/radeon_bios.c +1 -1
index 33121655d50b..2df6ce3e32cb 100644
@@ -227,6 +227,7 @@ static bool radeon_atrm_get_bios(struct radeon_device *rdev)
 
         if (!found)
                 return false;
+       pci_dev_put(pdev);
 
         rdev->bios = kmalloc(size, GFP_KERNEL);
         if (!rdev->bios) {
--

2.20.1

#4 lore raw

Re: [PATCH 1/2] drm/radeon: Fix PCI device refcount leak in radeon_atrm_get_bios()

Quoted 15 lines · Lijo
Quoted 11 lines · Alex Deucher
Quoted 8 lines · Xiongfeng Wang

As comment of pci_get_class() says, it returns a pci_device with its refcount increased and decreased the refcount for the input parameter @from if it is not NULL.

If we break the loop in radeon_atrm_get_bios() with 'pdev' not NULL, we need to call pci_dev_put() to decrease the refcount. Add the missing pci_dev_put() to avoid refcount leak.

Quoted 6 lines
Quoted 3 lines

For both patches, I think pci_dev_put() needs to go into the loops. There are 2 or more GPUs on the systems where this is relevant.

As per the logic, the loop breaks when it finds a valid ATRM handle. So dev_put is required only for that device.

Sure, but what if the handle is on the second DISPLAY_VGA or DISPLAY_OTHER class PCI device on the system? We've already called pci_get_class() for the first PCI device that matched.

Alex

Quoted 32 lines

When inside the loop this happens - "decreased the refcount for the input parameter @from if it is not NULL"

Thanks,
Lijo

Quoted 25 lines

Alex

Quoted 23 lines
Fixes d8ade3526b2a ("drm/radeon: handle non-VGA class pci devices with ATRM")
Fixes c61e2775873f ("drm/radeon: split ATRM support out from the ATPX handler (v3)")
---
  drivers/gpu/drm/radeon/radeon_bios.c | 1 +
  1 file changed, 1 insertion(+)
drivers/gpu/drm/radeon/radeon_bios.c +1 -1
index 33121655d50b..2df6ce3e32cb 100644
@@ -227,6 +227,7 @@ static bool radeon_atrm_get_bios(struct radeon_device *rdev)
 
         if (!found)
                 return false;
+       pci_dev_put(pdev);
 
         rdev->bios = kmalloc(size, GFP_KERNEL);
         if (!rdev->bios) {
--

2.20.1

#5 lore raw

Re: [PATCH 1/2] drm/radeon: Fix PCI device refcount leak in radeon_atrm_get_bios()

[AMD Official Use Only - General]

When only second GPU has valid ATRM handle -
then it stays inside the loop and in the next call to pci_get_class(), it passes pdev reference to first GPU as the "from" param. That time it drops the reference count of "from" device.

Thanks,
Lijo
________________________________
From: Alex Deucher <[email protected]>
Sent: Tuesday, November 22, 2022 9:55:33 PM
To: Lazar, Lijo <[email protected]>
Cc: Xiongfeng Wang <[email protected]>; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Pan, Xinhui <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; Zhang, Hawking <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>
Subject: Re: [PATCH 1/2] drm/radeon: Fix PCI device refcount leak in radeon_atrm_get_bios()

Quoted 15 lines · Lijo
Quoted 11 lines · Alex Deucher
Quoted 8 lines · Xiongfeng Wang

As comment of pci_get_class() says, it returns a pci_device with its refcount increased and decreased the refcount for the input parameter @from if it is not NULL.

If we break the loop in radeon_atrm_get_bios() with 'pdev' not NULL, we need to call pci_dev_put() to decrease the refcount. Add the missing pci_dev_put() to avoid refcount leak.

Quoted 6 lines
Quoted 3 lines

For both patches, I think pci_dev_put() needs to go into the loops. There are 2 or more GPUs on the systems where this is relevant.

As per the logic, the loop breaks when it finds a valid ATRM handle. So dev_put is required only for that device.

Sure, but what if the handle is on the second DISPLAY_VGA or DISPLAY_OTHER class PCI device on the system? We've already called pci_get_class() for the first PCI device that matched.

Alex

Quoted 32 lines

When inside the loop this happens - "decreased the refcount for the input parameter @from if it is not NULL"

Thanks,
Lijo

Quoted 25 lines

Alex

Quoted 23 lines
Fixes d8ade3526b2a ("drm/radeon: handle non-VGA class pci devices with ATRM")
Fixes c61e2775873f ("drm/radeon: split ATRM support out from the ATPX handler (v3)")
---
  drivers/gpu/drm/radeon/radeon_bios.c | 1 +
  1 file changed, 1 insertion(+)
drivers/gpu/drm/radeon/radeon_bios.c +1 -1
index 33121655d50b..2df6ce3e32cb 100644
@@ -227,6 +227,7 @@ static bool radeon_atrm_get_bios(struct radeon_device *rdev)
 
         if (!found)
                 return false;
+       pci_dev_put(pdev);
 
         rdev->bios = kmalloc(size, GFP_KERNEL);
         if (!rdev->bios) {
--

2.20.1

#6 lore raw

Re: [PATCH 1/2] drm/radeon: Fix PCI device refcount leak in radeon_atrm_get_bios()

Quoted 5 lines · Lijo

[AMD Official Use Only - General]

When only second GPU has valid ATRM handle -
then it stays inside the loop and in the next call to pci_get_class(), it passes pdev reference to first GPU as the "from" param. That time it drops the reference count of "from" device.

ah, right, that was the part I missed. Thanks.

Alex

Quoted 71 lines

Thanks,
Lijo
________________________________
From: Alex Deucher <[email protected]>
Sent: Tuesday, November 22, 2022 9:55:33 PM
To: Lazar, Lijo <[email protected]>
Cc: Xiongfeng Wang <[email protected]>; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Pan, Xinhui <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; Zhang, Hawking <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>
Subject: Re: [PATCH 1/2] drm/radeon: Fix PCI device refcount leak in radeon_atrm_get_bios()

Quoted 15 lines · Lijo
Quoted 11 lines · Alex Deucher
Quoted 8 lines · Xiongfeng Wang

As comment of pci_get_class() says, it returns a pci_device with its refcount increased and decreased the refcount for the input parameter @from if it is not NULL.

If we break the loop in radeon_atrm_get_bios() with 'pdev' not NULL, we need to call pci_dev_put() to decrease the refcount. Add the missing pci_dev_put() to avoid refcount leak.

Quoted 6 lines
Quoted 3 lines

For both patches, I think pci_dev_put() needs to go into the loops. There are 2 or more GPUs on the systems where this is relevant.

As per the logic, the loop breaks when it finds a valid ATRM handle. So dev_put is required only for that device.

Sure, but what if the handle is on the second DISPLAY_VGA or DISPLAY_OTHER class PCI device on the system? We've already called pci_get_class() for the first PCI device that matched.

Alex

Quoted 32 lines

When inside the loop this happens - "decreased the refcount for the input parameter @from if it is not NULL"

Thanks,
Lijo

Quoted 25 lines

Alex

Quoted 23 lines
Fixes d8ade3526b2a ("drm/radeon: handle non-VGA class pci devices with ATRM")
Fixes c61e2775873f ("drm/radeon: split ATRM support out from the ATPX handler (v3)")
---
  drivers/gpu/drm/radeon/radeon_bios.c | 1 +
  1 file changed, 1 insertion(+)
drivers/gpu/drm/radeon/radeon_bios.c +1 -1
index 33121655d50b..2df6ce3e32cb 100644
@@ -227,6 +227,7 @@ static bool radeon_atrm_get_bios(struct radeon_device *rdev)
 
         if (!found)
                 return false;
+       pci_dev_put(pdev);
 
         rdev->bios = kmalloc(size, GFP_KERNEL);
         if (!rdev->bios) {
--

2.20.1

#7 lore raw

PATCH 2/2 drm/amdgpu: Fix PCI device refcount leak in amdgpu_atrm_get_bios()

In reply to Xiongfeng Wang

As comment of pci_get_class() says, it returns a pci_device with its refcount increased and decreased the refcount for the input parameter @from if it is not NULL.

If we break the loop in amdgpu_atrm_get_bios() with 'pdev' not NULL, we need to call pci_dev_put() to decrease the refcount. Add the missing pci_dev_put() to avoid refcount leak.

Fixes d38ceaf99ed0 ("drm/amdgpu: add core driver (v4)")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c +1 -0
index e363f56c72af..30c28a69e847 100644
@@ -317,6 +317,7 @@ static bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
 
 	if (!found)
 		return false;
+	pci_dev_put(pdev);
 
 	adev->bios = kmalloc(size, GFP_KERNEL);
 	if (!adev->bios) {
Signature · 1 line
2.20.1
Applied
#8 lore raw

Re: [PATCH 2/2] drm/amdgpu: Fix PCI device refcount leak in amdgpu_atrm_get_bios()

Applied the series. Thanks!

Alex

Quoted 30 lines · Xiongfeng Wang

As comment of pci_get_class() says, it returns a pci_device with its refcount increased and decreased the refcount for the input parameter @from if it is not NULL.

If we break the loop in amdgpu_atrm_get_bios() with 'pdev' not NULL, we need to call pci_dev_put() to decrease the refcount. Add the missing pci_dev_put() to avoid refcount leak.

Fixes d38ceaf99ed0 ("drm/amdgpu: add core driver (v4)")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c +1 -1
index e363f56c72af..30c28a69e847 100644
@@ -317,6 +317,7 @@ static bool amdgpu_atrm_get_bios(struct amdgpu_device *adev)
 
        if (!found)
                return false;
+       pci_dev_put(pdev);
 
        adev->bios = kmalloc(size, GFP_KERNEL);
        if (!adev->bios) {
--

2.20.1