Skip to contentSkip to conversation

i2c: mux: don't access GPIOLIB internal structures

PATCH In review Bartosz Golaszewski 6 participants 17 messages Oct 11, 2023 – Oct 13, 2023

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

cover i2c: mux: don't access GPIOLIB internal structures

From: Bartosz Golaszewski <[email protected]>

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(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/3 gpiolib: provide gpio_device_to_device()

From: Bartosz Golaszewski <[email protected]>

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.

---
 drivers/gpio/gpiolib.c      | 17 +++++++++++++++++
 include/linux/gpio/driver.h |  2 ++
 2 files changed, 19 insertions(+)
drivers/gpio/gpiolib.c +17 -0
index 191f9c87b4d0..ca2b5b424284 100644
@@ -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
 
 /*
include/linux/gpio/driver.h +2 -0
index f8ad7f40100c..0484bf90b25d 100644
@@ -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);
Signature · 1 line
2.39.2
#4 lore raw

PATCH 2/3 gpiolib: provide gpiod_to_gpio_device()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

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.

---
 drivers/gpio/gpiolib.c      | 21 +++++++++++++++++++++
 include/linux/gpio/driver.h |  1 +
 2 files changed, 22 insertions(+)
drivers/gpio/gpiolib.c +21 -0
index ca2b5b424284..1e0ed6f5bcd5 100644
@@ -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
include/linux/gpio/driver.h +1 -0
index 0484bf90b25d..7a8725be1225 100644
@@ -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 */
 
Signature · 1 line
2.39.2
#10 lore raw

PATCH 3/3 i2c: mux: gpio: don't fiddle with GPIOLIB internals

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

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

---
 drivers/i2c/muxes/i2c-mux-gpio.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/i2c/muxes/i2c-mux-gpio.c +6 -6
index 5d5cbe0130cd..48a872a8196b 100644
@@ -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)
Signature · 1 line
2.39.2
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] gpiolib: provide gpio_device_to_device()

In reply to Bartosz Golaszewski

Hi!

Quoted 7 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

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.

Cheers,
Peter

Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device()

In reply to Bartosz Golaszewski

Hi!

Quoted 5 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

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

Quoted 7 lines

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.

Cheers,
Peter

Acked-by on v1 3/3
#11 lore raw

Re: [PATCH 3/3] i2c: mux: gpio: don't fiddle with GPIOLIB internals

In reply to Bartosz Golaszewski

Hi!

Quoted 38 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

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

---
 drivers/i2c/muxes/i2c-mux-gpio.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/i2c/muxes/i2c-mux-gpio.c +3 -3
index 5d5cbe0130cd..48a872a8196b 100644
@@ -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;

drivers/i2c/muxes/i2c-mux-gpio.c static int i2c_mux_gpio_probe(struct platform_device *pdev) · line 203

Quoted 10 lines
drivers/i2c/muxes/i2c-mux-gpio.c +3 -3
 
-		/* 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.

Cheers,
Peter

#12 lore raw

Re: [PATCH 3/3] i2c: mux: gpio: don't fiddle with GPIOLIB internals

Quoted 59 lines · Peter Rosin

Hi!

Quoted 38 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

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

---
 drivers/i2c/muxes/i2c-mux-gpio.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/i2c/muxes/i2c-mux-gpio.c +3 -3
index 5d5cbe0130cd..48a872a8196b 100644
@@ -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;

drivers/i2c/muxes/i2c-mux-gpio.c static int i2c_mux_gpio_probe(struct platform_device *pdev) · line 203

Quoted 10 lines
drivers/i2c/muxes/i2c-mux-gpio.c +3 -3
 
-             /* 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.

Quoted 1 line

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

Quoted 3 lines

Cheers,
Peter

#14 lore raw

Re: [PATCH 3/3] i2c: mux: gpio: don't fiddle with GPIOLIB internals

In reply to Peter Rosin
Quoted 9 lines · Peter Rosin
Quoted 3 lines · Bartosz Golaszewski

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.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by on v1 1/3
#3 lore raw

Re: [PATCH 1/3] gpiolib: provide gpio_device_to_device()

In reply to Bartosz Golaszewski
Quoted 6 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

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.

...

Quoted 1 line

+struct device *gpio_device_to_device(struct gpio_device *gdev)

Hmm... Maybe gpio_device_to_dev()?

Whatever, good enough to me

--
With Best Regards,
Andy Shevchenko...

#6 lore raw

Re: [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device()

In reply to Bartosz Golaszewski
Quoted 11 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

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?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw

Re: [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device()

In reply to Bartosz Golaszewski

On Wed, Oct 11, 2023 at 4:02 PM Bartosz Golaszewski <[email protected]> wrote:

...

drivers/gpio/gpiolib.c struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) · line 240

Quoted 6 lines
drivers/gpio/gpiolib.c +6 -0
+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).

Quoted 1 line

+}

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

Re: [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device()

In reply to Andy Shevchenko
Quoted 14 lines · Andy Shevchenko
Quoted 11 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

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.

Quoted 6 lines

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

Quoted 4 lines

--
With Best Regards,
Andy Shevchenko

Acked-by
#13 lore raw

Re: [PATCH 3/3] i2c: mux: gpio: don't fiddle with GPIOLIB internals

In reply to Bartosz Golaszewski

Hi Bartosz,

Quoted 3 lines
Quoted 3 lines

drivers/i2c/muxes/i2c-mux-gpio.c line 17

Quoted 3 lines
drivers/i2c/muxes/i2c-mux-gpio.c +1 -2
-/* FIXME: stop poking around inside gpiolib */
-#include "../../gpio/gpiolib.h"
+#include <linux/gpio/driver.h>

Hooray! \o/

Quoted 3 lines

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.

#8 lore raw

Re: [PATCH 2/3] gpiolib: provide gpiod_to_gpio_device()

In reply to Bartosz Golaszewski
Quoted 4 lines · Bartosz Golaszewski

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/[email protected]/

Yours,
Linus Walleij

Acked-by on the v1 series
#15 lore raw
In reply to Bartosz Golaszewski

Hi Bartosz,

Quoted 22 lines · Bartosz Golaszewski

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.

Quoted 8 lines

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:

Good job!

Yours,
Linus Walleij

#16 lore raw
In reply to Bartosz Golaszewski
Quoted 34 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

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

Quoted 8 lines
 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