linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] iio: light: cm32181: Unregister second I2C client if present
@ 2023-02-08  7:21 Kai-Heng Feng
  2023-02-13 11:17 ` Hans de Goede
  0 siblings, 1 reply; 3+ messages in thread
From: Kai-Heng Feng @ 2023-02-08  7:21 UTC (permalink / raw)
  To: ktsai, jic23, lars; +Cc: hdegoede, Kai-Heng Feng, linux-iio, linux-kernel

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>
---
v2:
 - Use devm_add_action_or_reset() instead of remove() callback to avoid
   race.

 drivers/iio/light/cm32181.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index b1674a5bfa368..a3e5f56101c9f 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -429,6 +429,16 @@ static const struct iio_info cm32181_info = {
 	.attrs			= &cm32181_attribute_group,
 };
 
+static void cm32181_disable(void *data)
+{
+	struct i2c_client *client = data;
+	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_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
@@ -479,6 +489,12 @@ static int cm32181_probe(struct i2c_client *client)
 		return ret;
 	}
 
+	ret = devm_add_action_or_reset(dev, cm32181_disable, client);
+	if (ret) {
+		dev_err(dev, "%s: add devres action failed\n", __func__);
+		return ret;
+	}
+
 	ret = devm_iio_device_register(dev, indio_dev);
 	if (ret) {
 		dev_err(dev, "%s: regist device failed\n", __func__);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] iio: light: cm32181: Unregister second I2C client if present
  2023-02-08  7:21 [PATCH v2] iio: light: cm32181: Unregister second I2C client if present Kai-Heng Feng
@ 2023-02-13 11:17 ` Hans de Goede
  2023-02-22 13:50   ` Kai-Heng Feng
  0 siblings, 1 reply; 3+ messages in thread
From: Hans de Goede @ 2023-02-13 11:17 UTC (permalink / raw)
  To: Kai-Heng Feng, ktsai, jic23, lars; +Cc: linux-iio, linux-kernel

Hi,

Thank you for working on this, some remarks inline.

On 2/8/23 08:21, 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>
> ---
> v2:
>  - Use devm_add_action_or_reset() instead of remove() callback to avoid
>    race.
> 
>  drivers/iio/light/cm32181.c | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
> 
> diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
> index b1674a5bfa368..a3e5f56101c9f 100644
> --- a/drivers/iio/light/cm32181.c
> +++ b/drivers/iio/light/cm32181.c
> @@ -429,6 +429,16 @@ static const struct iio_info cm32181_info = {
>  	.attrs			= &cm32181_attribute_group,
>  };
>  
> +static void cm32181_disable(void *data)
> +{
> +	struct i2c_client *client = data;
> +	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_probe(struct i2c_client *client)
>  {
>  	struct device *dev = &client->dev;
> @@ -479,6 +489,12 @@ static int cm32181_probe(struct i2c_client *client)
>  		return ret;
>  	}
>  
> +	ret = devm_add_action_or_reset(dev, cm32181_disable, client);
> +	if (ret) {
> +		dev_err(dev, "%s: add devres action failed\n", __func__);
> +		return ret;
> +	}
> +

This is too late, we will still exit without unregistering the client if
the cm32181_reg_init() call fails.

It would be best to do this directly after the i2c_acpi_new_device()
call, so inside the "if (ACPI_HANDLE(dev) && client->addr == SMBUS_ALERT_RESPONSE_ADDRESS) {"
block.

This way you can also remove the "if (cm32181->client != client)"
check from cm32181_disable() since it now only runs when the client
was registered in the first place.

Also please rename cm32181_disable() to cm32181_unregister_dummy_client()
so that the name actually matches what it does.

Regards,

Hans





>  	ret = devm_iio_device_register(dev, indio_dev);
>  	if (ret) {
>  		dev_err(dev, "%s: regist device failed\n", __func__);


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] iio: light: cm32181: Unregister second I2C client if present
  2023-02-13 11:17 ` Hans de Goede
@ 2023-02-22 13:50   ` Kai-Heng Feng
  0 siblings, 0 replies; 3+ messages in thread
From: Kai-Heng Feng @ 2023-02-22 13:50 UTC (permalink / raw)
  To: Hans de Goede; +Cc: ktsai, jic23, lars, linux-iio, linux-kernel

Hi Hans,

On Mon, Feb 13, 2023 at 7:17 PM Hans de Goede <hdegoede@redhat.com> wrote:
>
> Hi,
>
> Thank you for working on this, some remarks inline.
>
> On 2/8/23 08:21, 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>
> > ---
> > v2:
> >  - Use devm_add_action_or_reset() instead of remove() callback to avoid
> >    race.
> >
> >  drivers/iio/light/cm32181.c | 16 ++++++++++++++++
> >  1 file changed, 16 insertions(+)
> >
> > diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
> > index b1674a5bfa368..a3e5f56101c9f 100644
> > --- a/drivers/iio/light/cm32181.c
> > +++ b/drivers/iio/light/cm32181.c
> > @@ -429,6 +429,16 @@ static const struct iio_info cm32181_info = {
> >       .attrs                  = &cm32181_attribute_group,
> >  };
> >
> > +static void cm32181_disable(void *data)
> > +{
> > +     struct i2c_client *client = data;
> > +     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_probe(struct i2c_client *client)
> >  {
> >       struct device *dev = &client->dev;
> > @@ -479,6 +489,12 @@ static int cm32181_probe(struct i2c_client *client)
> >               return ret;
> >       }
> >
> > +     ret = devm_add_action_or_reset(dev, cm32181_disable, client);
> > +     if (ret) {
> > +             dev_err(dev, "%s: add devres action failed\n", __func__);
> > +             return ret;
> > +     }
> > +
>
> This is too late, we will still exit without unregistering the client if
> the cm32181_reg_init() call fails.
>
> It would be best to do this directly after the i2c_acpi_new_device()
> call, so inside the "if (ACPI_HANDLE(dev) && client->addr == SMBUS_ALERT_RESPONSE_ADDRESS) {"
> block.

You are right, not sure what I was thinking but this is much better.

>
> This way you can also remove the "if (cm32181->client != client)"
> check from cm32181_disable() since it now only runs when the client
> was registered in the first place.
>
> Also please rename cm32181_disable() to cm32181_unregister_dummy_client()
> so that the name actually matches what it does.

Sure, will change that in next version.

>
> Regards,
>
> Hans
>
>
>
>
>
> >       ret = devm_iio_device_register(dev, indio_dev);
> >       if (ret) {
> >               dev_err(dev, "%s: regist device failed\n", __func__);
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-02-22 13:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-08  7:21 [PATCH v2] iio: light: cm32181: Unregister second I2C client if present Kai-Heng Feng
2023-02-13 11:17 ` Hans de Goede
2023-02-22 13:50   ` Kai-Heng Feng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).