gpiolib: hold gpio devices lock until ->descs array is initialised
PATCH RESEND 1/1 gpiolib: hold gpio devices lock until ->descs array is initialised
If a driver consuming the GPIO chip is being probed at the same time as the GPIO driver is registering the chip, it is possible for the consuming driver to see the ->descs array in an uninitialised state. For example, the gpio-keys-polled driver can fail like this:
kernel: gpiod_request: invalid GPIO (no device)
kernel: gpio-keys-polled PRP0001:07: failed to get gpio: -22
kernel: gpio-keys-polled: probe of PRP0001:07 failed with error -22
This patch makes gpiochip_add() hold the lock protecting gpio_devices until it has finished setting desc->gdev on the newly inserted list entry.
---
Resending this one because I failed to cc the maintainers on the original posting, sorry about that.
drivers/gpio/gpiolib.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib.c +2 -2
@@ -1495,11 +1495,11 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data, goto err_free_label; } -spin_unlock_irqrestore (&gpio_lock, flags); -for (i =0 ; i < chip->ngpio; i++) gdev->descs[i].gdev = gdev; +spin_unlock_irqrestore (&gpio_lock, flags); +#ifdef CONFIG_PINCTRL INIT_LIST_HEAD (&gdev->pin_ranges);#endif
Signature · 1 line
2.21.0
Quoted 9 lines · Dan Callaghan
If a driver consuming the GPIO chip is being probed at the same time as the GPIO driver is registering the chip, it is possible for the consuming driver to see the ->descs array in an uninitialised state. For example, the gpio-keys-polled driver can fail like this:
kernel:
gpiod_request: invalid GPIO (no device)
kernel: gpio-keys-polled PRP0001:07: failed to get gpio: -22
kernel: gpio-keys-polled: probe of PRP0001:07 failed with error -22
Quoted 8 lines
This patch makes
gpiochip_add()hold the lock protectinggpio_devicesuntil it has finished settingdesc->gdevon the newly inserted list entry.Signed-off-by Dan Callaghan <[email protected]>---
Resending this one because I failed to cc the maintainers on the original posting, sorry about that.
This makes a lot of sense, I'm impressed that you managed to provoke this error!
Patch applied.
Yours,
Linus Walleij