The bit-banged adapters have no managed unregister action, and the I2C
channels are allocated separately. Clearing the bus array leaves both
the bit-banged adapters and channel storage behind. Register hardware
adapters explicitly and unregister each adapter before freeing its
channel.

This issue was found by our static analysis tool under development.

Fixes: 0f36a3c6af9e ("drm/amdgpu/amdgpu_i2c: Use devm_i2c_add_adapter instead 
of i2c_add_adapter")
Signed-off-by: Guangshuo Li <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_i2c.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_i2c.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_i2c.c
index c576e103a783..af3d4f9b12c7 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_i2c.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_i2c.c
@@ -184,7 +184,7 @@ struct amdgpu_i2c_chan *amdgpu_i2c_create(struct drm_device 
*dev,
                snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
                         "AMDGPU i2c hw bus %s", name);
                i2c->adapter.algo = &amdgpu_atombios_i2c_algo;
-               ret = devm_i2c_add_adapter(dev->dev, &i2c->adapter);
+               ret = i2c_add_adapter(&i2c->adapter);
                if (ret)
                        goto out_free;
        } else {
@@ -239,9 +239,17 @@ void amdgpu_i2c_fini(struct amdgpu_device *adev)
 {
        int i;
 
-       for (i = 0; i < AMDGPU_MAX_I2C_BUS; i++)
-               if (adev->i2c_bus[i])
-                       adev->i2c_bus[i] = NULL;
+       for (i = 0; i < AMDGPU_MAX_I2C_BUS; i++) {
+               struct amdgpu_i2c_chan *i2c = adev->i2c_bus[i];
+
+               if (!i2c)
+                       continue;
+
+               WARN_ON(i2c->has_aux);
+               i2c_del_adapter(&i2c->adapter);
+               kfree(i2c);
+               adev->i2c_bus[i] = NULL;
+       }
 }
 
 /* looks up bus based on id */

-- 
2.43.0

Reply via email to