diff mbox series

iio: light: cm32181: Unregister second I2C client if present

Message ID 20230206063616.981225-1-kai.heng.feng@canonical.com (mailing list archive)
State Superseded
Headers show
Series iio: light: cm32181: Unregister second I2C client if present | expand

Commit Message

Kai-Heng Feng Feb. 6, 2023, 6:36 a.m. UTC
If a second client that talks to the actual I2C address was created in
probe(), there should be a corresponding cleanup in remove() to avoid
leakage.

So if the "client" is not the same one used by I2C core, unregister it
accordingly.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2152281
Fixes: c1e62062ff54 ("iio: light: cm32181: Handle CM3218 ACPI devices with 2 I2C resources")
Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
---
 drivers/iio/light/cm32181.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

Comments

Hans de Goede Feb. 6, 2023, 9:51 a.m. UTC | #1
Hi,

On 2/6/23 07:36, Kai-Heng Feng wrote:
> If a second client that talks to the actual I2C address was created in
> probe(), there should be a corresponding cleanup in remove() to avoid
> leakage.
> 
> So if the "client" is not the same one used by I2C core, unregister it
> accordingly.
> 
> Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2152281
> Fixes: c1e62062ff54 ("iio: light: cm32181: Handle CM3218 ACPI devices with 2 I2C resources")
> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>

Thank you for fixing this, patch looks good to me:

Reviewed-by: Hans de Goede <hdegoede@redhat.com>

Regards,

Hans




> ---
>  drivers/iio/light/cm32181.c | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
> index b1674a5bfa368..37439e103d273 100644
> --- a/drivers/iio/light/cm32181.c
> +++ b/drivers/iio/light/cm32181.c
> @@ -488,6 +488,15 @@ static int cm32181_probe(struct i2c_client *client)
>  	return 0;
>  }
>  
> +static void cm32181_remove(struct i2c_client *client)
> +{
> +	struct cm32181_chip *cm32181 = iio_priv(i2c_get_clientdata(client));
> +
> +	/* Unregister the dummy client */
> +	if (cm32181->client != client)
> +		i2c_unregister_device(cm32181->client);
> +}
> +
>  static int cm32181_suspend(struct device *dev)
>  {
>  	struct cm32181_chip *cm32181 = iio_priv(dev_get_drvdata(dev));
> @@ -531,6 +540,7 @@ static struct i2c_driver cm32181_driver = {
>  		.pm = pm_sleep_ptr(&cm32181_pm_ops),
>  	},
>  	.probe_new	= cm32181_probe,
> +	.remove		= cm32181_remove,
>  };
>  
>  module_i2c_driver(cm32181_driver);
Jonathan Cameron Feb. 6, 2023, 2:36 p.m. UTC | #2
On Mon,  6 Feb 2023 14:36:16 +0800
Kai-Heng Feng <kai.heng.feng@canonical.com> wrote:

> If a second client that talks to the actual I2C address was created in
> probe(), there should be a corresponding cleanup in remove() to avoid
> leakage.
> 
> So if the "client" is not the same one used by I2C core, unregister it
> accordingly.
> 
> Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2152281
> Fixes: c1e62062ff54 ("iio: light: cm32181: Handle CM3218 ACPI devices with 2 I2C resources")
> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>

Whilst this fixes the leak, it introduces a race between unregistering
some of the hardware and the later removal of userspace interfaces. 
This needs to be handled via a devm_add_action_or_reset()
registered handler so it's automatically cleaned up at the right place
in the driver remove flow.

Jonathan


> ---
>  drivers/iio/light/cm32181.c | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
> index b1674a5bfa368..37439e103d273 100644
> --- a/drivers/iio/light/cm32181.c
> +++ b/drivers/iio/light/cm32181.c
> @@ -488,6 +488,15 @@ static int cm32181_probe(struct i2c_client *client)
>  	return 0;
>  }
>  
> +static void cm32181_remove(struct i2c_client *client)
> +{
> +	struct cm32181_chip *cm32181 = iio_priv(i2c_get_clientdata(client));
> +
> +	/* Unregister the dummy client */
> +	if (cm32181->client != client)
> +		i2c_unregister_device(cm32181->client);
> +}
> +
>  static int cm32181_suspend(struct device *dev)
>  {
>  	struct cm32181_chip *cm32181 = iio_priv(dev_get_drvdata(dev));
> @@ -531,6 +540,7 @@ static struct i2c_driver cm32181_driver = {
>  		.pm = pm_sleep_ptr(&cm32181_pm_ops),
>  	},
>  	.probe_new	= cm32181_probe,
> +	.remove		= cm32181_remove,
>  };
>  
>  module_i2c_driver(cm32181_driver);
Hans de Goede Feb. 6, 2023, 2:41 p.m. UTC | #3
Hi,

On 2/6/23 15:36, Jonathan Cameron wrote:
> On Mon,  6 Feb 2023 14:36:16 +0800
> Kai-Heng Feng <kai.heng.feng@canonical.com> wrote:
> 
>> If a second client that talks to the actual I2C address was created in
>> probe(), there should be a corresponding cleanup in remove() to avoid
>> leakage.
>>
>> So if the "client" is not the same one used by I2C core, unregister it
>> accordingly.
>>
>> Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2152281
>> Fixes: c1e62062ff54 ("iio: light: cm32181: Handle CM3218 ACPI devices with 2 I2C resources")
>> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
> 
> Whilst this fixes the leak, it introduces a race between unregistering
> some of the hardware and the later removal of userspace interfaces. 
> This needs to be handled via a devm_add_action_or_reset()
> registered handler so it's automatically cleaned up at the right place
> in the driver remove flow.

Good point, I missed that issue.

Regards,

Hans



>> ---
>>  drivers/iio/light/cm32181.c | 10 ++++++++++
>>  1 file changed, 10 insertions(+)
>>
>> diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
>> index b1674a5bfa368..37439e103d273 100644
>> --- a/drivers/iio/light/cm32181.c
>> +++ b/drivers/iio/light/cm32181.c
>> @@ -488,6 +488,15 @@ static int cm32181_probe(struct i2c_client *client)
>>  	return 0;
>>  }
>>  
>> +static void cm32181_remove(struct i2c_client *client)
>> +{
>> +	struct cm32181_chip *cm32181 = iio_priv(i2c_get_clientdata(client));
>> +
>> +	/* Unregister the dummy client */
>> +	if (cm32181->client != client)
>> +		i2c_unregister_device(cm32181->client);
>> +}
>> +
>>  static int cm32181_suspend(struct device *dev)
>>  {
>>  	struct cm32181_chip *cm32181 = iio_priv(dev_get_drvdata(dev));
>> @@ -531,6 +540,7 @@ static struct i2c_driver cm32181_driver = {
>>  		.pm = pm_sleep_ptr(&cm32181_pm_ops),
>>  	},
>>  	.probe_new	= cm32181_probe,
>> +	.remove		= cm32181_remove,
>>  };
>>  
>>  module_i2c_driver(cm32181_driver);
>
diff mbox series

Patch

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index b1674a5bfa368..37439e103d273 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -488,6 +488,15 @@  static int cm32181_probe(struct i2c_client *client)
 	return 0;
 }
 
+static void cm32181_remove(struct i2c_client *client)
+{
+	struct cm32181_chip *cm32181 = iio_priv(i2c_get_clientdata(client));
+
+	/* Unregister the dummy client */
+	if (cm32181->client != client)
+		i2c_unregister_device(cm32181->client);
+}
+
 static int cm32181_suspend(struct device *dev)
 {
 	struct cm32181_chip *cm32181 = iio_priv(dev_get_drvdata(dev));
@@ -531,6 +540,7 @@  static struct i2c_driver cm32181_driver = {
 		.pm = pm_sleep_ptr(&cm32181_pm_ops),
 	},
 	.probe_new	= cm32181_probe,
+	.remove		= cm32181_remove,
 };
 
 module_i2c_driver(cm32181_driver);