gpio: don't compare raw GPIO descriptor pointers directly
Subsystems (2)
cover gpio: don't compare raw GPIO descriptor pointers directly
Handling of shared GPIOs in the kernel needs some improvements. Let's start with a simple change of not comparing GPIO descriptor pointers directly as there's nothing that guarantees that the same physical pin will always be represented by a single GPIO descriptor obtained by calling gpiod_get().
For merging: I suggest to take patch 1/2 through the GPIO tree and provide an immutable tag for the regulator tree.
---
Bartosz Golaszewski (2):
gpio: provide gpiod_is_equal()
regulator: don't compare raw GPIO descriptor pointers
drivers/gpio/gpiolib.c | 14 ++++++++++++++ drivers/regulator/core.c | 2 +- include/linux/gpio/consumer.h | 9 +++++++++ 3 files changed, 24 insertions(+), 1 deletion(-)
---
base-commit: 0af2f6be1b4281385b618cb86ad946eded089ac8
change-id: 20250402-gpiod-is-equal-4e28bb8ab842
Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>
PATCH 1/2 gpio: provide gpiod_is_equal()
From: Bartosz Golaszewski <[email protected]>
There are users in the kernel that directly compare raw GPIO descriptor pointers in order to determine whether they refer to the same physical GPIO pin. This accidentally works like this but is not guaranteed by any API contract. Let's provide a comparator function that hides the actual logic.
--- drivers/gpio/gpiolib.c | 14 ++++++++++++++ include/linux/gpio/consumer.h | 9 +++++++++ 2 files changed, 23 insertions(+)
drivers/gpio/gpiolib.c +14 -0
@@ -265,6 +265,20 @@ struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc) }EXPORT_SYMBOL_GPL (gpiod_to_gpio_device); +/** +* gpiod_is_equal() - Check if two GPIO descriptors refer to the same pin. +* @desc: Descriptor to compare. +* @other: The second descriptor to compare against. +* +* Returns: +* True if the descriptors refer to the same physical pin. False otherwise. +*/ +bool gpiod_is_equal (struct gpio_desc *desc,struct gpio_desc *other) +{ +return desc == other; +} +EXPORT_SYMBOL_GPL (gpiod_is_equal); +/** * gpio_device_get_base() - Get the base GPIO number allocated by this device * @gdev: GPIO device
include/linux/gpio/consumer.h +9 -0
@@ -180,6 +180,8 @@ struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,enum gpiod_flags flags, const char *label); +bool gpiod_is_equal (struct gpio_desc *desc,struct gpio_desc *other); +#else /* CONFIG_GPIOLIB */ #include <linux/bug.h>
@@ -547,6 +549,13 @@ struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev, return ERR_PTR(-ENOSYS); } +static inline bool +gpiod_is_equal (struct gpio_desc *desc,struct gpio_desc *other) +{ +WARN_ON (desc || other); +return false; +} +#endif /* CONFIG_GPIOLIB */ #if IS_ENABLED (CONFIG_GPIOLIB ) &&IS_ENABLED (CONFIG_HTE )
Signature · 1 line
2.45.2
Re: [PATCH 1/2] gpio: provide gpiod_is_equal()
Quoted 7 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
There are users in the kernel that directly compare raw GPIO descriptor pointers in order to determine whether they refer to the same physical GPIO pin. This accidentally works like this but is not guaranteed by any API contract. Let's provide a comparator function that hides the actual logic.
Re: [PATCH 1/2] gpio: provide gpiod_is_equal()
In reply to Bartosz GolaszewskiQuoted 7 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
There are users in the kernel that directly compare raw GPIO descriptor pointers in order to determine whether they refer to the same physical GPIO pin. This accidentally works like this but is not guaranteed by any API contract. Let's provide a comparator function that hides the actual logic.
...
drivers/gpio/gpiolib.c struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc) · line 278
Quoted 3 lines
drivers/gpio/gpiolib.c +3
-0+bool gpiod_is_equal (struct gpio_desc *desc,struct gpio_desc *other) +{ +return desc == other;
I think it's better to have the one checked against NULL. That's how comparators make more sense, see, for example, 1b1bb7b29b10 ("drivers:
base: Don't match devices with NULL of_node/fwnode/etc").
Ideally it should be IS_ERR_OR_NULL(), but we have here a principal disagreement, so this might be yet another (buggy) function in GPIOLIB.
Quoted 1 line
+}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH 1/2] gpio: provide gpiod_is_equal()
Quoted 12 lines · Andy Shevchenko
Quoted 7 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
There are users in the kernel that directly compare raw GPIO descriptor pointers in order to determine whether they refer to the same physical GPIO pin. This accidentally works like this but is not guaranteed by any API contract. Let's provide a comparator function that hides the actual logic.
...
Quoted 8 lines
drivers/gpio/gpiolib.c struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc) · line 278
Quoted 3 lines
drivers/gpio/gpiolib.c +3
-0+bool gpiod_is_equal (struct gpio_desc *desc,struct gpio_desc *other) +{ +return desc == other;I think it's better to have the one checked against NULL. That's how comparators make more sense, see, for example,
1b1bb7b29b10("drivers:
base: Don't match devices with NULL of_node/fwnode/etc").
Yeah I guess it can be improved in a follow-up.
Quoted 3 lines
Ideally it should be
IS_ERR_OR_NULL(), but we have here a principal disagreement, so this might be yet another (buggy) function in GPIOLIB.
Our disagreement has nothing to do with this. In fact validating against IS_ERR() (and returning false) makes more sense than just accepting IS_ERR() in acting like it's a valid descriptor.
I will send a follow-up tomorrow.
Bartosz
PATCH 2/2 regulator: don't compare raw GPIO descriptor pointers
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
There's no API contract saying that two GPIO descriptor pointers obtained with a call to gpiod_get() (or one of the variants), that refer to the same physical GPIO pin, always point to the same structure. Use the dedicated comparator function.
--- drivers/regulator/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/regulator/core.c +1 -1
@@ -2617,7 +2617,7 @@ static int regulator_ena_gpio_request(struct regulator_dev *rdev,mutex_lock (®ulator_list_mutex);list_for_each_entry (pin, ®ulator_ena_gpio_list, list) { -if (pin->gpiod == gpiod) { +if (gpiod_is_equal (pin->gpiod, gpiod)) {rdev_dbg (rdev,"GPIO is already used\n" ); goto update_ena_gpio_to_rdev; }
Signature · 1 line
2.45.2
Mark Brown
maintainerQuoted 2 lines · Bartosz Golaszewski
For merging: I suggest to take patch 1/2 through the GPIO tree and provide an immutable tag for the regulator tree.
Sounds good to me.
Bartosz Golaszewski
AppliedRe: (subset) [PATCH 0/2] gpio: don't compare raw GPIO descriptor pointers directly
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Quoted 10 lines · Bartosz Golaszewski
Handling of shared GPIOs in the kernel needs some improvements. Let's start with a simple change of not comparing GPIO descriptor pointers directly as there's nothing that guarantees that the same physical pin will always be represented by a single GPIO descriptor obtained by calling
gpiod_get().For merging: I suggest to take patch 1/2 through the GPIO tree and provide an immutable tag for the regulator tree.
[...]
Applied, thanks!
[1/2] gpio: provide gpiod_is_equal()
https://git.kernel.org/brgl/linux/c/265daffe788aa1cc5925d0afcde4fe6e99c66638
Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>
Mark Brown
maintainerRe: (subset) [PATCH 0/2] gpio: don't compare raw GPIO descriptor pointers directly
In reply to Bartosz GolaszewskiQuoted 10 lines · Bartosz Golaszewski
Handling of shared GPIOs in the kernel needs some improvements. Let's start with a simple change of not comparing GPIO descriptor pointers directly as there's nothing that guarantees that the same physical pin will always be represented by a single GPIO descriptor obtained by calling
gpiod_get().For merging: I suggest to take patch 1/2 through the GPIO tree and provide an immutable tag for the regulator tree.
[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-next
Thanks!
[2/2] regulator: don't compare raw GPIO descriptor pointers
commit: aaf6223ea2a1ff9316a81bf851fd5a0e82635b60
All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark