linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] i2c: mux: don't access GPIOLIB internal structures
@ 2023-10-11 13:02 Bartosz Golaszewski
  2023-10-11 13:02 ` [PATCH 1/3] gpiolib: provide gpio_device_to_device() Bartosz Golaszewski
                   ` (4 more replies)
  0 siblings, 5 replies; 17+ messages in thread
From: Bartosz Golaszewski @ 2023-10-11 13:02 UTC (permalink / raw)
  To: Linus Walleij, Andy Shevchenko, Peter Korsgaard, Peter Rosin
  Cc: linux-gpio, linux-kernel, linux-i2c, Bartosz Golaszewski

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

The backstory for this short series is that we are identyfing and
removing all unauthorized uses of GPIOLIB structures across the kernel.

For example: there are many users that access struct gpio_chip when the
only user allowed to safely do this is the provider of that chip.

We are very close to removing gpiochip_find(). Another function that
poses a similar problem is gpiod_to_chip() which also returns the
address of the underlying gpio_chip without assuring that it will not go
away e.g. due to a hot-unplug event or a device unbind.

We'll need to replace it with gpiod_to_gpio_device() across the entire
tree. Let's start by actually providing it and adding the first user:
the i2c-mux-gpio driver which dereferences the otherwise opaque struct
gpio_desc.

Let's also add a helper that allows to retrieve the address of the
struct device backing the GPIO device as this is another valid use-case.

Finally, let's un-include the GPIO private header and fix the code to
access the device in a safe way.

As the change is pretty minor, it would be best if patch 3/3 could be
acked by the I2C mux maintainers and went through the GPIO tree.
Otherwise, I can apply patches 1 and 2 and provide an immutable branch.

Bartosz Golaszewski (3):
  gpiolib: provide gpio_device_to_device()
  gpiolib: provide gpiod_to_gpio_device()
  i2c: mux: gpio: don't fiddle with GPIOLIB internals

 drivers/gpio/gpiolib.c           | 38 ++++++++++++++++++++++++++++++++
 drivers/i2c/muxes/i2c-mux-gpio.c | 12 +++++-----
 include/linux/gpio/driver.h      |  3 +++
 3 files changed, 47 insertions(+), 6 deletions(-)

-- 
2.39.2


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

* [PATCH 1/3] gpiolib: provide gpio_device_to_device()
  2023-10-11 13:02 [PATCH 0/3] i2c: mux: don't access GPIOLIB internal structures Bartosz Golaszewski
@ 2023-10-11 13:02 ` Bartosz Golaszewski
  2023-10-11 14:57   ` Peter Rosin
  2023-10-11 15:18   ` Andy Shevchenko
  2023-10-11 13:02 ` [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device() Bartosz Golaszewski
                   ` (3 subsequent siblings)
  4 siblings, 2 replies; 17+ messages in thread
From: Bartosz Golaszewski @ 2023-10-11 13:02 UTC (permalink / raw)
  To: Linus Walleij, Andy Shevchenko, Peter Korsgaard, Peter Rosin
  Cc: linux-gpio, linux-kernel, linux-i2c, Bartosz Golaszewski

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

There are users in the kernel who need to retrieve the address of the
struct device backing the GPIO device. Currently they needlessly poke in
the internals of GPIOLIB. Add a dedicated getter function.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 drivers/gpio/gpiolib.c      | 17 +++++++++++++++++
 include/linux/gpio/driver.h |  2 ++
 2 files changed, 19 insertions(+)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 191f9c87b4d0..ca2b5b424284 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -1169,6 +1169,23 @@ void gpio_device_put(struct gpio_device *gdev)
 }
 EXPORT_SYMBOL_GPL(gpio_device_put);
 
+/**
+ * gpio_device_to_device() - Retrieve the address of the underlying struct
+ *                           device.
+ * @gdev: GPIO device for which to return the address.
+ *
+ * This does not increase the reference count of the GPIO device nor the
+ * underlying struct device.
+ *
+ * Returns:
+ * Address of struct device backing this GPIO device.
+ */
+struct device *gpio_device_to_device(struct gpio_device *gdev)
+{
+	return &gdev->dev;
+}
+EXPORT_SYMBOL_GPL(gpio_device_to_device);
+
 #ifdef CONFIG_GPIOLIB_IRQCHIP
 
 /*
diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h
index f8ad7f40100c..0484bf90b25d 100644
--- a/include/linux/gpio/driver.h
+++ b/include/linux/gpio/driver.h
@@ -618,6 +618,8 @@ void gpio_device_put(struct gpio_device *gdev);
 DEFINE_FREE(gpio_device_put, struct gpio_device *,
 	    if (IS_ERR_OR_NULL(_T)) gpio_device_put(_T));
 
+struct device *gpio_device_to_device(struct gpio_device *gdev);
+
 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset);
 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset);
 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset);
-- 
2.39.2


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

* [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device()
  2023-10-11 13:02 [PATCH 0/3] i2c: mux: don't access GPIOLIB internal structures Bartosz Golaszewski
  2023-10-11 13:02 ` [PATCH 1/3] gpiolib: provide gpio_device_to_device() Bartosz Golaszewski
@ 2023-10-11 13:02 ` Bartosz Golaszewski
  2023-10-11 14:58   ` Peter Rosin
                     ` (2 more replies)
  2023-10-11 13:02 ` [PATCH 3/3] i2c: mux: gpio: don't fiddle with GPIOLIB internals Bartosz Golaszewski
                   ` (2 subsequent siblings)
  4 siblings, 3 replies; 17+ messages in thread
From: Bartosz Golaszewski @ 2023-10-11 13:02 UTC (permalink / raw)
  To: Linus Walleij, Andy Shevchenko, Peter Korsgaard, Peter Rosin
  Cc: linux-gpio, linux-kernel, linux-i2c, Bartosz Golaszewski

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Accessing struct gpio_chip backing a GPIO device is only allowed for the
actual providers of that chip.

Similarly to how we introduced gpio_device_find() in order to replace
the abused gpiochip_find(), let's introduce a counterpart to
gpiod_to_chip() that returns a reference to the GPIO device owning the
descriptor. This is done in order to later remove gpiod_to_chip()
entirely.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 drivers/gpio/gpiolib.c      | 21 +++++++++++++++++++++
 include/linux/gpio/driver.h |  1 +
 2 files changed, 22 insertions(+)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index ca2b5b424284..1e0ed6f5bcd5 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -220,6 +220,27 @@ struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
 }
 EXPORT_SYMBOL_GPL(gpiod_to_chip);
 
+/**
+ * gpiod_to_gpio_device() - Return the GPIO device to which this descriptor
+ *                          belongs.
+ * @desc: Descriptor for which to return the GPIO device.
+ *
+ * This *DOES NOT* increase the reference count of the GPIO device as it's
+ * expected that the descriptor is requested and the users already holds a
+ * reference to the device.
+ *
+ * Returns:
+ * Address of the GPIO device owning this descriptor.
+ */
+struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc)
+{
+	if (!desc)
+		return NULL;
+
+	return desc->gdev;
+}
+EXPORT_SYMBOL_GPL(gpiod_to_gpio_device);
+
 /**
  * gpio_device_get_chip() - Get the gpio_chip implementation of this GPIO device
  * @gdev: GPIO device
diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h
index 0484bf90b25d..7a8725be1225 100644
--- a/include/linux/gpio/driver.h
+++ b/include/linux/gpio/driver.h
@@ -784,6 +784,7 @@ int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset);
 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset);
 
 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
+struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc);
 
 #else /* CONFIG_GPIOLIB */
 
-- 
2.39.2


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

* [PATCH 3/3] i2c: mux: gpio: don't fiddle with GPIOLIB internals
  2023-10-11 13:02 [PATCH 0/3] i2c: mux: don't access GPIOLIB internal structures Bartosz Golaszewski
  2023-10-11 13:02 ` [PATCH 1/3] gpiolib: provide gpio_device_to_device() Bartosz Golaszewski
  2023-10-11 13:02 ` [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device() Bartosz Golaszewski
@ 2023-10-11 13:02 ` Bartosz Golaszewski
  2023-10-11 14:59   ` Peter Rosin
  2023-10-12  6:59 ` [PATCH 0/3] i2c: mux: don't access GPIOLIB internal structures Linus Walleij
  2023-10-13  6:50 ` Bartosz Golaszewski
  4 siblings, 1 reply; 17+ messages in thread
From: Bartosz Golaszewski @ 2023-10-11 13:02 UTC (permalink / raw)
  To: Linus Walleij, Andy Shevchenko, Peter Korsgaard, Peter Rosin
  Cc: linux-gpio, linux-kernel, linux-i2c, Bartosz Golaszewski

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Use the relevant API functions to retrieve the address of the
underlying struct device instead of accessing GPIOLIB private structures
manually.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 drivers/i2c/muxes/i2c-mux-gpio.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/i2c/muxes/i2c-mux-gpio.c b/drivers/i2c/muxes/i2c-mux-gpio.c
index 5d5cbe0130cd..48a872a8196b 100644
--- a/drivers/i2c/muxes/i2c-mux-gpio.c
+++ b/drivers/i2c/muxes/i2c-mux-gpio.c
@@ -14,8 +14,7 @@
 #include <linux/slab.h>
 #include <linux/bits.h>
 #include <linux/gpio/consumer.h>
-/* FIXME: stop poking around inside gpiolib */
-#include "../../gpio/gpiolib.h"
+#include <linux/gpio/driver.h>
 
 struct gpiomux {
 	struct i2c_mux_gpio_platform_data data;
@@ -176,7 +175,8 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev)
 	}
 
 	for (i = 0; i < ngpios; i++) {
-		struct device *gpio_dev;
+		struct gpio_device *gdev;
+		struct device *dev;
 		struct gpio_desc *gpiod;
 		enum gpiod_flags flag;
 
@@ -195,9 +195,9 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev)
 		if (!muxc->mux_locked)
 			continue;
 
-		/* FIXME: find a proper way to access the GPIO device */
-		gpio_dev = &gpiod->gdev->dev;
-		muxc->mux_locked = i2c_root_adapter(gpio_dev) == root;
+		gdev = gpiod_to_gpio_device(gpiod);
+		dev = gpio_device_to_device(gdev);
+		muxc->mux_locked = i2c_root_adapter(dev) == root;
 	}
 
 	if (muxc->mux_locked)
-- 
2.39.2


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

* Re: [PATCH 1/3] gpiolib: provide gpio_device_to_device()
  2023-10-11 13:02 ` [PATCH 1/3] gpiolib: provide gpio_device_to_device() Bartosz Golaszewski
@ 2023-10-11 14:57   ` Peter Rosin
  2023-10-11 15:18   ` Andy Shevchenko
  1 sibling, 0 replies; 17+ messages in thread
From: Peter Rosin @ 2023-10-11 14:57 UTC (permalink / raw)
  To: Bartosz Golaszewski, Linus Walleij, Andy Shevchenko, Peter Korsgaard
  Cc: linux-gpio, linux-kernel, linux-i2c, Bartosz Golaszewski

Hi!

2023-10-11 at 15:02, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> 
> There are users in the kernel who need to retrieve the address of the
> struct device backing the GPIO device. Currently they needlessly poke in
> the internals of GPIOLIB. Add a dedicated getter function.
> 
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Reviewed-by: Peter Rosin <peda@axentia.se>

Cheers,
Peter

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

* Re: [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device()
  2023-10-11 13:02 ` [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device() Bartosz Golaszewski
@ 2023-10-11 14:58   ` Peter Rosin
  2023-10-11 15:23   ` Andy Shevchenko
  2023-10-11 15:28   ` Andy Shevchenko
  2 siblings, 0 replies; 17+ messages in thread
From: Peter Rosin @ 2023-10-11 14:58 UTC (permalink / raw)
  To: Bartosz Golaszewski, Linus Walleij, Andy Shevchenko, Peter Korsgaard
  Cc: linux-gpio, linux-kernel, linux-i2c, Bartosz Golaszewski

Hi!

2023-10-11 at 15:02, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> 
> Accessing struct gpio_chip backing a GPIO device is only allowed for the
> actual providers of that chip.
> 
> Similarly to how we introduced gpio_device_find() in order to replace
> the abused gpiochip_find(), let's introduce a counterpart to
> gpiod_to_chip() that returns a reference to the GPIO device owning the
> descriptor. This is done in order to later remove gpiod_to_chip()
> entirely.
> 
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Reviewed-by: Peter Rosin <peda@axentia.se>

Cheers,
Peter

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

* Re: [PATCH 3/3] i2c: mux: gpio: don't fiddle with GPIOLIB internals
  2023-10-11 13:02 ` [PATCH 3/3] i2c: mux: gpio: don't fiddle with GPIOLIB internals Bartosz Golaszewski
@ 2023-10-11 14:59   ` Peter Rosin
  2023-10-11 15:03     ` Bartosz Golaszewski
  2023-10-11 15:18     ` Andy Shevchenko
  0 siblings, 2 replies; 17+ messages in thread
From: Peter Rosin @ 2023-10-11 14:59 UTC (permalink / raw)
  To: Bartosz Golaszewski, Linus Walleij, Andy Shevchenko, Peter Korsgaard
  Cc: linux-gpio, linux-kernel, linux-i2c, Bartosz Golaszewski

Hi!

2023-10-11 at 15:02, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> 
> Use the relevant API functions to retrieve the address of the
> underlying struct device instead of accessing GPIOLIB private structures
> manually.
> 
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> ---
>  drivers/i2c/muxes/i2c-mux-gpio.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/i2c/muxes/i2c-mux-gpio.c b/drivers/i2c/muxes/i2c-mux-gpio.c
> index 5d5cbe0130cd..48a872a8196b 100644
> --- a/drivers/i2c/muxes/i2c-mux-gpio.c
> +++ b/drivers/i2c/muxes/i2c-mux-gpio.c
> @@ -14,8 +14,7 @@
>  #include <linux/slab.h>
>  #include <linux/bits.h>
>  #include <linux/gpio/consumer.h>
> -/* FIXME: stop poking around inside gpiolib */
> -#include "../../gpio/gpiolib.h"
> +#include <linux/gpio/driver.h>
>  
>  struct gpiomux {
>  	struct i2c_mux_gpio_platform_data data;
> @@ -176,7 +175,8 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev)
>  	}
>  
>  	for (i = 0; i < ngpios; i++) {
> -		struct device *gpio_dev;
> +		struct gpio_device *gdev;
> +		struct device *dev;
>  		struct gpio_desc *gpiod;
>  		enum gpiod_flags flag;
>  
> @@ -195,9 +195,9 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev)
>  		if (!muxc->mux_locked)
>  			continue;
>  
> -		/* FIXME: find a proper way to access the GPIO device */
> -		gpio_dev = &gpiod->gdev->dev;
> -		muxc->mux_locked = i2c_root_adapter(gpio_dev) == root;
> +		gdev = gpiod_to_gpio_device(gpiod);
> +		dev = gpio_device_to_device(gdev);
> +		muxc->mux_locked = i2c_root_adapter(dev) == root;
>  	}
>  
>  	if (muxc->mux_locked)

Very nice to see that wart gone! The only small question I have
is if these helpers are needed elsewhere, or if a more "direct"
gpiod_to_device() would have been sufficient? That said, I have
zero problem with this new code as-is, and that detail is of
course squarely in gpio-land.

Acked-by: Peter Rosin <peda@axentia.se>

Cheers,
Peter

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

* Re: [PATCH 3/3] i2c: mux: gpio: don't fiddle with GPIOLIB internals
  2023-10-11 14:59   ` Peter Rosin
@ 2023-10-11 15:03     ` Bartosz Golaszewski
  2023-10-11 16:41       ` Wolfram Sang
  2023-10-11 15:18     ` Andy Shevchenko
  1 sibling, 1 reply; 17+ messages in thread
From: Bartosz Golaszewski @ 2023-10-11 15:03 UTC (permalink / raw)
  To: Peter Rosin
  Cc: Linus Walleij, Andy Shevchenko, Peter Korsgaard, linux-gpio,
	linux-kernel, linux-i2c, Bartosz Golaszewski

On Wed, Oct 11, 2023 at 4:59 PM Peter Rosin <peda@axentia.se> wrote:
>
> Hi!
>
> 2023-10-11 at 15:02, Bartosz Golaszewski wrote:
> > From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> >
> > Use the relevant API functions to retrieve the address of the
> > underlying struct device instead of accessing GPIOLIB private structures
> > manually.
> >
> > Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> > ---
> >  drivers/i2c/muxes/i2c-mux-gpio.c | 12 ++++++------
> >  1 file changed, 6 insertions(+), 6 deletions(-)
> >
> > diff --git a/drivers/i2c/muxes/i2c-mux-gpio.c b/drivers/i2c/muxes/i2c-mux-gpio.c
> > index 5d5cbe0130cd..48a872a8196b 100644
> > --- a/drivers/i2c/muxes/i2c-mux-gpio.c
> > +++ b/drivers/i2c/muxes/i2c-mux-gpio.c
> > @@ -14,8 +14,7 @@
> >  #include <linux/slab.h>
> >  #include <linux/bits.h>
> >  #include <linux/gpio/consumer.h>
> > -/* FIXME: stop poking around inside gpiolib */
> > -#include "../../gpio/gpiolib.h"
> > +#include <linux/gpio/driver.h>
> >
> >  struct gpiomux {
> >       struct i2c_mux_gpio_platform_data data;
> > @@ -176,7 +175,8 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev)
> >       }
> >
> >       for (i = 0; i < ngpios; i++) {
> > -             struct device *gpio_dev;
> > +             struct gpio_device *gdev;
> > +             struct device *dev;
> >               struct gpio_desc *gpiod;
> >               enum gpiod_flags flag;
> >
> > @@ -195,9 +195,9 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev)
> >               if (!muxc->mux_locked)
> >                       continue;
> >
> > -             /* FIXME: find a proper way to access the GPIO device */
> > -             gpio_dev = &gpiod->gdev->dev;
> > -             muxc->mux_locked = i2c_root_adapter(gpio_dev) == root;
> > +             gdev = gpiod_to_gpio_device(gpiod);
> > +             dev = gpio_device_to_device(gdev);
> > +             muxc->mux_locked = i2c_root_adapter(dev) == root;
> >       }
> >
> >       if (muxc->mux_locked)
>
> Very nice to see that wart gone! The only small question I have
> is if these helpers are needed elsewhere, or if a more "direct"
> gpiod_to_device() would have been sufficient? That said, I have
> zero problem with this new code as-is, and that detail is of
> course squarely in gpio-land.
>
> Acked-by: Peter Rosin <peda@axentia.se>

gpiod_to_gpio_device() will be used in at least 10 other places. I
haven't identified any other potential user for
gpio_device_to_device() yet but I haven't looked hard yet either.

Bart

>
> Cheers,
> Peter

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

* Re: [PATCH 3/3] i2c: mux: gpio: don't fiddle with GPIOLIB internals
  2023-10-11 14:59   ` Peter Rosin
  2023-10-11 15:03     ` Bartosz Golaszewski
@ 2023-10-11 15:18     ` Andy Shevchenko
  1 sibling, 0 replies; 17+ messages in thread
From: Andy Shevchenko @ 2023-10-11 15:18 UTC (permalink / raw)
  To: Peter Rosin
  Cc: Bartosz Golaszewski, Linus Walleij, Andy Shevchenko,
	Peter Korsgaard, linux-gpio, linux-kernel, linux-i2c,
	Bartosz Golaszewski

On Wed, Oct 11, 2023 at 5:59 PM Peter Rosin <peda@axentia.se> wrote:
> 2023-10-11 at 15:02, Bartosz Golaszewski wrote:

> > Use the relevant API functions to retrieve the address of the
> > underlying struct device instead of accessing GPIOLIB private structures
> > manually.

> Very nice to see that wart gone! The only small question I have
> is if these helpers are needed elsewhere, or if a more "direct"
> gpiod_to_device() would have been sufficient?

Same concern is here. But I'm fine with the code.

-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH 1/3] gpiolib: provide gpio_device_to_device()
  2023-10-11 13:02 ` [PATCH 1/3] gpiolib: provide gpio_device_to_device() Bartosz Golaszewski
  2023-10-11 14:57   ` Peter Rosin
@ 2023-10-11 15:18   ` Andy Shevchenko
  1 sibling, 0 replies; 17+ messages in thread
From: Andy Shevchenko @ 2023-10-11 15:18 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Linus Walleij, Andy Shevchenko, Peter Korsgaard, Peter Rosin,
	linux-gpio, linux-kernel, linux-i2c, Bartosz Golaszewski

On Wed, Oct 11, 2023 at 4:02 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>
> There are users in the kernel who need to retrieve the address of the
> struct device backing the GPIO device. Currently they needlessly poke in
> the internals of GPIOLIB. Add a dedicated getter function.

...

> +struct device *gpio_device_to_device(struct gpio_device *gdev)

Hmm... Maybe gpio_device_to_dev()?

Whatever, good enough to me
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>


--
With Best Regards,
Andy Shevchenko...

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

* Re: [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device()
  2023-10-11 13:02 ` [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device() Bartosz Golaszewski
  2023-10-11 14:58   ` Peter Rosin
@ 2023-10-11 15:23   ` Andy Shevchenko
  2023-10-11 15:39     ` Bartosz Golaszewski
  2023-10-11 15:28   ` Andy Shevchenko
  2 siblings, 1 reply; 17+ messages in thread
From: Andy Shevchenko @ 2023-10-11 15:23 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Linus Walleij, Andy Shevchenko, Peter Korsgaard, Peter Rosin,
	linux-gpio, linux-kernel, linux-i2c, Bartosz Golaszewski

On Wed, Oct 11, 2023 at 4:02 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>
> Accessing struct gpio_chip backing a GPIO device is only allowed for the
> actual providers of that chip.
>
> Similarly to how we introduced gpio_device_find() in order to replace
> the abused gpiochip_find(), let's introduce a counterpart to
> gpiod_to_chip() that returns a reference to the GPIO device owning the
> descriptor. This is done in order to later remove gpiod_to_chip()
> entirely.

My concern with this API is the following scenario:
1. One driver requests the GPIO descriptor.
2. Another driver does take an arbitrary number, converts to a
descriptor and calls for this API.

Is there any (potential) problem?

-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device()
  2023-10-11 13:02 ` [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device() Bartosz Golaszewski
  2023-10-11 14:58   ` Peter Rosin
  2023-10-11 15:23   ` Andy Shevchenko
@ 2023-10-11 15:28   ` Andy Shevchenko
  2 siblings, 0 replies; 17+ messages in thread
From: Andy Shevchenko @ 2023-10-11 15:28 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Linus Walleij, Andy Shevchenko, Peter Korsgaard, Peter Rosin,
	linux-gpio, linux-kernel, linux-i2c, Bartosz Golaszewski

On Wed, Oct 11, 2023 at 4:02 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:

...

> +struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc)
> +{
> +       if (!desc)
> +               return NULL;
> +
> +       return desc->gdev;

Wondering if we may use (part of) the validate_desc() (in a form of
VALIDATE_DESC_VOID() macro call).

> +}

-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device()
  2023-10-11 15:23   ` Andy Shevchenko
@ 2023-10-11 15:39     ` Bartosz Golaszewski
  2023-10-12  6:57       ` Linus Walleij
  0 siblings, 1 reply; 17+ messages in thread
From: Bartosz Golaszewski @ 2023-10-11 15:39 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Bartosz Golaszewski, Linus Walleij, Andy Shevchenko,
	Peter Korsgaard, Peter Rosin, linux-gpio, linux-kernel,
	linux-i2c

On Wed, 11 Oct 2023 at 17:23, Andy Shevchenko <andy.shevchenko@gmail.com> wrote:
>
> On Wed, Oct 11, 2023 at 4:02 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
> >
> > From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> >
> > Accessing struct gpio_chip backing a GPIO device is only allowed for the
> > actual providers of that chip.
> >
> > Similarly to how we introduced gpio_device_find() in order to replace
> > the abused gpiochip_find(), let's introduce a counterpart to
> > gpiod_to_chip() that returns a reference to the GPIO device owning the
> > descriptor. This is done in order to later remove gpiod_to_chip()
> > entirely.
>
> My concern with this API is the following scenario:
> 1. One driver requests the GPIO descriptor.
> 2. Another driver does take an arbitrary number, converts to a
> descriptor and calls for this API.
>
> Is there any (potential) problem?

YES! And I have it already on my TODO list! But it's great to know I'm
not the only one seeing it.

Basically we need to

The end-goal should be to make gpio_to_desc() an internal GPIOLIB
symbol. There are still around 10 users outside drivers/gpio/ that
will need to be addressed in one way or another. Preferably by being
converted to using descriptors.

Bart

>
> --
> With Best Regards,
> Andy Shevchenko

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

* Re: [PATCH 3/3] i2c: mux: gpio: don't fiddle with GPIOLIB internals
  2023-10-11 15:03     ` Bartosz Golaszewski
@ 2023-10-11 16:41       ` Wolfram Sang
  0 siblings, 0 replies; 17+ messages in thread
From: Wolfram Sang @ 2023-10-11 16:41 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Peter Rosin, Linus Walleij, Andy Shevchenko, Peter Korsgaard,
	linux-gpio, linux-kernel, linux-i2c, Bartosz Golaszewski

[-- Attachment #1: Type: text/plain, Size: 435 bytes --]

Hi Bartosz,

> > > -/* FIXME: stop poking around inside gpiolib */
> > > -#include "../../gpio/gpiolib.h"
> > > +#include <linux/gpio/driver.h>

Hooray! \o/

> gpiod_to_gpio_device() will be used in at least 10 other places. I
> haven't identified any other potential user for
> gpio_device_to_device() yet but I haven't looked hard yet either.

Same here. Looked a little bit, found nothing.

Acked-by: Wolfram Sang <wsa@kernel.org>


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device()
  2023-10-11 15:39     ` Bartosz Golaszewski
@ 2023-10-12  6:57       ` Linus Walleij
  0 siblings, 0 replies; 17+ messages in thread
From: Linus Walleij @ 2023-10-12  6:57 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Andy Shevchenko, Bartosz Golaszewski, Andy Shevchenko,
	Peter Korsgaard, Peter Rosin, linux-gpio, linux-kernel,
	linux-i2c

On Wed, Oct 11, 2023 at 5:39 PM Bartosz Golaszewski
<bartosz.golaszewski@linaro.org> wrote:

> The end-goal should be to make gpio_to_desc() an internal GPIOLIB
> symbol. There are still around 10 users outside drivers/gpio/ that
> will need to be addressed in one way or another. Preferably by being
> converted to using descriptors.

Conversely desc_to_gpio() as well, here is one fix for the current
merge window (yeah I keep churning away at this...)
https://lore.kernel.org/alsa-devel/20230926-descriptors-asoc-ti-v1-2-60cf4f8adbc5@linaro.org/

Yours,
Linus Walleij

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

* Re: [PATCH 0/3] i2c: mux: don't access GPIOLIB internal structures
  2023-10-11 13:02 [PATCH 0/3] i2c: mux: don't access GPIOLIB internal structures Bartosz Golaszewski
                   ` (2 preceding siblings ...)
  2023-10-11 13:02 ` [PATCH 3/3] i2c: mux: gpio: don't fiddle with GPIOLIB internals Bartosz Golaszewski
@ 2023-10-12  6:59 ` Linus Walleij
  2023-10-13  6:50 ` Bartosz Golaszewski
  4 siblings, 0 replies; 17+ messages in thread
From: Linus Walleij @ 2023-10-12  6:59 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Andy Shevchenko, Peter Korsgaard, Peter Rosin, linux-gpio,
	linux-kernel, linux-i2c, Bartosz Golaszewski

Hi Bartosz,


On Wed, Oct 11, 2023 at 3:02 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:

> The backstory for this short series is that we are identyfing and
> removing all unauthorized uses of GPIOLIB structures across the kernel.
>
> For example: there are many users that access struct gpio_chip when the
> only user allowed to safely do this is the provider of that chip.
>
> We are very close to removing gpiochip_find(). Another function that
> poses a similar problem is gpiod_to_chip() which also returns the
> address of the underlying gpio_chip without assuring that it will not go
> away e.g. due to a hot-unplug event or a device unbind.
>
> We'll need to replace it with gpiod_to_gpio_device() across the entire
> tree. Let's start by actually providing it and adding the first user:
> the i2c-mux-gpio driver which dereferences the otherwise opaque struct
> gpio_desc.
>
> Let's also add a helper that allows to retrieve the address of the
> struct device backing the GPIO device as this is another valid use-case.
>
> Finally, let's un-include the GPIO private header and fix the code to
> access the device in a safe way.
>
> As the change is pretty minor, it would be best if patch 3/3 could be
> acked by the I2C mux maintainers and went through the GPIO tree.
> Otherwise, I can apply patches 1 and 2 and provide an immutable branch.
>
> Bartosz Golaszewski (3):
>   gpiolib: provide gpio_device_to_device()
>   gpiolib: provide gpiod_to_gpio_device()
>   i2c: mux: gpio: don't fiddle with GPIOLIB internals

The series:
Acked-by: Linus Walleij <linus.walleij@linaro.org>
Good job!

Yours,
Linus Walleij

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

* Re: [PATCH 0/3] i2c: mux: don't access GPIOLIB internal structures
  2023-10-11 13:02 [PATCH 0/3] i2c: mux: don't access GPIOLIB internal structures Bartosz Golaszewski
                   ` (3 preceding siblings ...)
  2023-10-12  6:59 ` [PATCH 0/3] i2c: mux: don't access GPIOLIB internal structures Linus Walleij
@ 2023-10-13  6:50 ` Bartosz Golaszewski
  4 siblings, 0 replies; 17+ messages in thread
From: Bartosz Golaszewski @ 2023-10-13  6:50 UTC (permalink / raw)
  To: Linus Walleij, Andy Shevchenko, Peter Korsgaard, Peter Rosin
  Cc: linux-gpio, linux-kernel, linux-i2c, Bartosz Golaszewski

On Wed, Oct 11, 2023 at 3:02 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>
> The backstory for this short series is that we are identyfing and
> removing all unauthorized uses of GPIOLIB structures across the kernel.
>
> For example: there are many users that access struct gpio_chip when the
> only user allowed to safely do this is the provider of that chip.
>
> We are very close to removing gpiochip_find(). Another function that
> poses a similar problem is gpiod_to_chip() which also returns the
> address of the underlying gpio_chip without assuring that it will not go
> away e.g. due to a hot-unplug event or a device unbind.
>
> We'll need to replace it with gpiod_to_gpio_device() across the entire
> tree. Let's start by actually providing it and adding the first user:
> the i2c-mux-gpio driver which dereferences the otherwise opaque struct
> gpio_desc.
>
> Let's also add a helper that allows to retrieve the address of the
> struct device backing the GPIO device as this is another valid use-case.
>
> Finally, let's un-include the GPIO private header and fix the code to
> access the device in a safe way.
>
> As the change is pretty minor, it would be best if patch 3/3 could be
> acked by the I2C mux maintainers and went through the GPIO tree.
> Otherwise, I can apply patches 1 and 2 and provide an immutable branch.
>
> Bartosz Golaszewski (3):
>   gpiolib: provide gpio_device_to_device()
>   gpiolib: provide gpiod_to_gpio_device()
>   i2c: mux: gpio: don't fiddle with GPIOLIB internals
>
>  drivers/gpio/gpiolib.c           | 38 ++++++++++++++++++++++++++++++++
>  drivers/i2c/muxes/i2c-mux-gpio.c | 12 +++++-----
>  include/linux/gpio/driver.h      |  3 +++
>  3 files changed, 47 insertions(+), 6 deletions(-)
>
> --
> 2.39.2
>

Queued for v6.7.

Bart

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

end of thread, other threads:[~2023-10-13  6:50 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-11 13:02 [PATCH 0/3] i2c: mux: don't access GPIOLIB internal structures Bartosz Golaszewski
2023-10-11 13:02 ` [PATCH 1/3] gpiolib: provide gpio_device_to_device() Bartosz Golaszewski
2023-10-11 14:57   ` Peter Rosin
2023-10-11 15:18   ` Andy Shevchenko
2023-10-11 13:02 ` [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device() Bartosz Golaszewski
2023-10-11 14:58   ` Peter Rosin
2023-10-11 15:23   ` Andy Shevchenko
2023-10-11 15:39     ` Bartosz Golaszewski
2023-10-12  6:57       ` Linus Walleij
2023-10-11 15:28   ` Andy Shevchenko
2023-10-11 13:02 ` [PATCH 3/3] i2c: mux: gpio: don't fiddle with GPIOLIB internals Bartosz Golaszewski
2023-10-11 14:59   ` Peter Rosin
2023-10-11 15:03     ` Bartosz Golaszewski
2023-10-11 16:41       ` Wolfram Sang
2023-10-11 15:18     ` Andy Shevchenko
2023-10-12  6:59 ` [PATCH 0/3] i2c: mux: don't access GPIOLIB internal structures Linus Walleij
2023-10-13  6:50 ` Bartosz Golaszewski

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).