gpio: improve claim diagnostics and fix gpio-delay offset handling
v3 cover gpio: improve claim diagnostics and fix gpio-delay offset handling
This series improves diagnosability of GPIO offset collisions and fixes four related bugs in the gpio-delay driver that were uncovered while debugging such a collision.
Patch 1 logs, at dev_dbg() level, which label already holds a GPIO offset when a new request collides with it, from within dm_gpio_request() itself, so a collision can be root-caused from the uclass layer when actively debugging.
Patch 2 adds the same diagnostic to the "gpio" command, at printf() level. -EBUSY from gpio_request() is a normal, expected result there (a pin already owned by a driver or hog), so the command already tolerates it silently; this patch only prints which label holds the pin on that already-tolerated path.
Patch 3 fixes gpio_delay_probe()/gpio_delay_xlate() to size and validate against the driver's actual GPIO count (from its "gpios" property) instead of a hardcoded 32, correctly sizing the claimed/name tracking arrays and matching the pattern used by the generic uclass xlate implementation.
Patch 4 fixes gpio_delay_xlate() to propagate the requested index into desc->offset. Previously every consumer's descriptor came back with offset 0 regardless of which index was requested, so on boards where gpio-delay wraps more than one real GPIO line, every consumer past the first silently collided with the first consumer's already-claimed offset 0 and was never toggled. This was the root cause behind the series; the other patches are diagnostic and robustness fixes found along the way.
Patch 5 adds a .remove hook to gpio-delay to release the real GPIOs it wraps. The driver requests them via gpio_request_by_name_nodev(), which strdup()s a label into the wrapped device's own uc_priv->name[]; with no .remove, those requests and labels were never released on teardown.
Patch 6 fixes a related pre-existing leak: gpio_delay_probe() bailed out on the first failed GPIO request without freeing the ones it had already requested, and patch 5's new .remove hook doesn't cover this either since .remove is only called for devices that finished probing.
Patches 3-6 are tagged Fixes: c866f2f197e2 ("gpio: Add GPIO delay driver").
Changes in v2:
- Patch 1: downgraded from dev_warn() to dev_dbg(), since -EBUSY is a normal/expected result for some callers (e.g. cmd/gpio.c, and sandbox tests that deliberately exercise this path), and warning there fired on legitimate operations, not just misconfiguration (Simon)
- New patch 2: moved the user-facing diagnostic into the "gpio" command instead, where -EBUSY is already known to be expected (Simon)
- New patch 5: additional related fix found while further exercising the driver
Changes in v3:
- Patch 5: added an in-code comment explaining why the device_active() guard is needed, so a future reader doesn't need the commit message to understand it (Simon)
- New patch 6: fixes gpio_delay_probe()'s partial-request leak on failure, reusing the free helper factored out of patch 5's .remove hook (Suggested-by: Simon)
Sandbox coverage for the gpio-delay fixes in patches 3-5 (exercising the offset-propagation, gpio_count/bounds, and remove-path fixes) is posted as a separate, dependent series:
https://patchwork.ozlabs.org/project/uboot/list/?series=516335
Pranav Sanwal (6):
gpio: uclass: log when a gpio offset is already claimed
gpio: cmd: warn when a gpio offset is already claimed
gpio: delay: set gpio_count and validate index against it
gpio: delay: fix offset propagation in gpio_delay_xlate()
gpio: delay: free wrapped real GPIOs on remove
gpio: delay: free already-requested GPIOs on probe failure
cmd/gpio.c | 11 +++++++++-- drivers/gpio/gpio-delay.c | 40 ++++++++++++++++++++++++++++++++++++-- drivers/gpio/gpio-uclass.c | 5 ++++- 3 files changed, 51 insertions(+), 5 deletions(-)
Signature · 1 line
2.43.7
PATCH v3 1/6 gpio: uclass: log when a gpio offset is already claimed
dm_gpio_request() returns -EBUSY when the requested offset is already claimed, but gives no indication of which label already holds it. Any caller going through the uclass API directly, rather than the "gpio" command, has no way to see this at all.
Log it at dev_dbg() level: silent by default, available when actively debugging with this device's debug output enabled.
--- drivers/gpio/gpio-uclass.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpio/gpio-uclass.c +4 -1
@@ -391,8 +391,11 @@ int dm_gpio_request(struct gpio_desc *desc, const char *label)int ret; uc_priv =dev_get_uclass_priv (dev); -if (gpio_is_claimed (uc_priv, desc->offset)) +if (gpio_is_claimed (uc_priv, desc->offset)) { +dev_dbg (dev,"gpio offset %u already claimed by '%s', requested label '%s'\n" , + desc->offset, uc_priv->name[desc->offset], label);return -EBUSY ; + } str =strdup (label);if (!str)return -ENOMEM ;
Signature · 1 line
2.43.7
PATCH v3 2/6 gpio: cmd: warn when a gpio offset is already claimed
In reply to Pranav SanwalThe "gpio" command silently swallows -EBUSY from gpio_request(), since a pin already claimed by a driver or hog is a normal, expected condition for "gpio set"/"gpio toggle" etc. This makes an unexpected offset collision (e.g. two consumers unintentionally routed to the same underlying offset) opaque to debug from the command line.
Print which label already holds the pin when this happens, using the device/offset already resolved by gpio_lookup_name().
--- cmd/gpio.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
cmd/gpio.c +9 -2
@@ -137,6 +137,8 @@ static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc, {unsigned int gpio;enum gpio_cmd sub_cmd; +struct udevice *dev; +unsigned int offset;int value;const char *str_cmd, *str_gpio =NULL ;#ifdef CONFIG_CMD_GPIO_READ
@@ -217,7 +219,7 @@ static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,* code here to use the GPIO uclass interface instead of the numbered * GPIO compatibility layer. */ - ret =gpio_lookup_name (str_gpio,NULL ,NULL , &gpio); + ret =gpio_lookup_name (str_gpio, &dev, &offset, &gpio);if (ret) {printf ("GPIO: '%s' not found\n" , str_gpio);return cmd_process_error (cmdtp, ret);
@@ -230,7 +232,12 @@ static int do_gpio(struct cmd_tbl *cmdtp, int flag, int argc,#endif /* grab the pin before we tweak it */ ret =gpio_request (gpio,"cmd_gpio" ); -if (ret && ret != -EBUSY ) { +if (IS_ENABLED (CONFIG_DM_GPIO ) && ret == -EBUSY ) { +struct gpio_dev_priv *uc_priv =dev_get_uclass_priv (dev); + +printf ("gpio: pin %s (gpio %u) already claimed by '%s'\n" , + str_gpio, gpio, uc_priv->name[offset]); + }else if (ret && ret != -EBUSY ) {printf ("gpio: requesting pin %u failed\n" , gpio);return -1 ; }
Signature · 1 line
2.43.7
PATCH v3 3/6 gpio: delay: set gpio_count and validate index against it
In reply to Pranav Sanwalgpio_delay_probe() never set the uclass's gpio_count for this device, leaving it at 0. This left gpio_post_probe()'s claimed/name tracking arrays sized for zero entries, and left gpio_delay_xlate()'s bounds check validating the requested index against a hardcoded 32 that has no relation to how many lines this instance actually manages (ngpio, from its own "gpios" property).
Set gpio_count in probe() to the real count, and validate against it in xlate(), matching the pattern already used by the generic gpio_xlate_offs_flags() default implementation.
--- drivers/gpio/gpio-delay.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpio/gpio-delay.c +5 -1
@@ -68,12 +68,13 @@ static int gpio_delay_direction_output(struct udevice *dev, unsigned int offset,static int gpio_delay_xlate (struct udevice *dev,struct gpio_desc *desc,struct ofnode_phandle_args *args) { +struct gpio_dev_priv *uc_priv =dev_get_uclass_priv (dev);struct gpio_delay_priv *priv =dev_get_priv (dev);if (args->args_count <3 )return -EINVAL ; -if (args->args[0 ] >=32 ) +if (args->args[0 ] >= uc_priv->gpio_count)return -EINVAL ;struct gpio_delay_desc *d = &priv->descs[args->args[0 ]];
@@ -97,6 +98,7 @@ static const struct dm_gpio_ops gpio_delay_ops = {static int gpio_delay_probe (struct udevice *dev) { +struct gpio_dev_priv *uc_priv =dev_get_uclass_priv (dev);struct gpio_delay_priv *priv =dev_get_priv (dev);struct gpio_delay_desc *d;ofnode node =dev_ofnode (dev);
@@ -112,6 +114,8 @@ static int gpio_delay_probe(struct udevice *dev)if (!priv->descs)return -ENOMEM ; + uc_priv->gpio_count = ngpio; +/* Request all GPIOs described in the controller node */ for (i =0 ; i < ngpio; i++) { d = &priv->descs[i];
Signature · 1 line
2.43.7
PATCH v3 4/6 gpio: delay: fix offset propagation in gpio_delay_xlate()
In reply to Pranav Sanwalgpio_delay_xlate() indexed priv->descs[] correctly using args->args[0], but never propagated that same index into desc->offset. Since gpio_desc_init() initializes offset to 0 before xlate() runs, every consumer's returned descriptor ended up with offset 0 regardless of which index it actually requested.
On boards where gpio-delay wraps more than one real GPIO line, this caused every consumer past the first to silently collide with the first consumer's already-claimed offset 0 in dm_gpio_request(), which returns -EBUSY. The second consumer's real GPIO line was therefore never toggled.
Fix gpio_delay_xlate() to record the requested index into desc->offset so each consumer's descriptor is routed to the correct underlying real GPIO.
--- drivers/gpio/gpio-delay.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/gpio/gpio-delay.c +2 -0
@@ -77,6 +77,8 @@ static int gpio_delay_xlate(struct udevice *dev, struct gpio_desc *desc,if (args->args[0 ] >= uc_priv->gpio_count)return -EINVAL ; + desc->offset = args->args[0 ]; +struct gpio_delay_desc *d = &priv->descs[args->args[0 ]]; d->ramp_up_us = args->args[1 ];
Signature · 1 line
2.43.7
PATCH v3 5/6 gpio: delay: free wrapped real GPIOs on remove
In reply to Pranav Sanwalgpio_delay_probe() requests the real GPIOs it wraps via gpio_request_by_name_nodev(), which strdup()s a label into the real GPIO device's own uc_priv->name[] array. The driver had no .remove hook, so those requests, and their strdup'd labels, were never released when the gpio-delay device was torn down.
Add gpio_delay_remove() to free each wrapped real GPIO. Guard each free with device_active() on the real GPIO's device: generic DM teardown (e.g. dm_leak_check_end()'s uclass-by-uclass destroy) does not guarantee a consumer is removed before the provider it wraps, so the real GPIO device may already be inactive with its uclass_priv freed by the time this runs.
--- drivers/gpio/gpio-delay.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
drivers/gpio/gpio-delay.c +28 -0
@@ -90,6 +90,33 @@ static int gpio_delay_xlate(struct udevice *dev, struct gpio_desc *desc,return 0 ; } +static void gpio_delay_free_wrapped (struct udevice *dev,int count) +{ +struct gpio_delay_priv *priv =dev_get_priv (dev); +int i; + +for (i =0 ; i < count; i++) { +/* +* Generic DM teardown (e.g. dm_leak_check_end()'s +* uclass-by-uclass destroy) does not guarantee a consumer is +* removed before the provider it wraps, so the real GPIO +* device may already be inactive with its uclass_priv freed +* by the time this runs. +*/ +if (device_active (priv->descs[i].real_gpio.dev)) +dm_gpio_free (dev, &priv->descs[i].real_gpio); + } +} + +static int gpio_delay_remove (struct udevice *dev) +{ +struct gpio_dev_priv *uc_priv =dev_get_uclass_priv (dev); + +gpio_delay_free_wrapped (dev, uc_priv->gpio_count); + +return 0 ; +} +static const struct dm_gpio_ops gpio_delay_ops = { .direction_output = gpio_delay_direction_output, .direction_input = gpio_delay_direction_input,
@@ -142,4 +169,5 @@ U_BOOT_DRIVER(gpio_delay) = { .ops = &gpio_delay_ops, .priv_auto = sizeof(struct gpio_delay_priv), .probe = gpio_delay_probe, + .remove = gpio_delay_remove, };
Signature · 1 line
2.43.7
PATCH v3 6/6 gpio: delay: free already-requested GPIOs on probe failure
In reply to Pranav Sanwalgpio_delay_probe() requests each wrapped real GPIO in a loop and returns immediately if gpio_request_by_name_nodev() fails partway through, without freeing the GPIOs it already successfully requested. gpio_delay_remove() does not cover this either, since .remove is only called for devices that finished probing.
Free what was already requested via the same gpio_delay_free_wrapped() helper gpio_delay_remove() uses, bounded by the number of GPIOs actually requested so far rather than the full gpio_count.
--- drivers/gpio/gpio-delay.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpio/gpio-delay.c +3 -1
@@ -150,8 +150,10 @@ static int gpio_delay_probe(struct udevice *dev) d = &priv->descs[i]; ret =gpio_request_by_name_nodev (node,"gpios" , i, &d->real_gpio,0 ); -if (ret) +if (ret) { +gpio_delay_free_wrapped (dev, i);return ret; + } }return 0 ;
Signature · 1 line
2.43.7
Re: [PATCH v3 6/6] gpio: delay: free already-requested GPIOs on probe failure
Quoted 12 lines · Pranav Sanwal
gpio: delay: free already-requested GPIOs on probe failure
gpio_delay_probe()requests each wrapped real GPIO in a loop and returns immediately ifgpio_request_by_name_nodev()fails partway through, without freeing the GPIOs it already successfully requested.gpio_delay_remove()does not cover this either, since .remove is only called for devices that finished probing.Free what was already requested via the same
gpio_delay_free_wrapped()helpergpio_delay_remove()uses, bounded by the number of GPIOs actually requested so far rather than the fullgpio_count.
Quoted 6 lines
Fixes c866f2f197e2 ("gpio: Add GPIO delay driver")Suggested-by Simon Glass <[email protected]>Signed-off-by Pranav Sanwal <[email protected]>
drivers/gpio/gpio-delay.c| 4 +++-1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 10 lines · Pranav Sanwal
This series improves diagnosability of GPIO offset collisions and fixes four related bugs in the gpio-delay driver that were uncovered while debugging such a collision.
Patch 1 logs, at
dev_dbg()level, which label already holds a GPIO offset when a new request collides with it, from withindm_gpio_request()itself, so a collision can be root-caused from the uclass layer when actively debugging.[...]
Applied to u-boot/next, thanks!
[1/6] gpio: uclass: log when a gpio offset is already claimed
commit: e3aa1ec1e3a319dd842486c2f933a0dd1958c1b4
[2/6] gpio: cmd: warn when a gpio offset is already claimed
commit: 826d8b8b1771dba53fc799d312abfbd852728c04
[3/6] gpio: delay: set gpio_count and validate index against it
commit: 04e3d07ebe6f4cf620754f828d4a7df6089a8277
[4/6] gpio: delay: fix offset propagation in gpio_delay_xlate()
commit: 8df3881e173db02cb49306e2857d67c4502812aa
[5/6] gpio: delay: free wrapped real GPIOs on remove
commit: 55752c23945b47fc68afa35102108e60108fc7d9
[6/6] gpio: delay: free already-requested GPIOs on probe failure
commit: bb6283adea943068e4ef93f0bdc91c33a887f003Signature · 1 line
Tom