gpio: protect the pointer to gpio_chip in gpio_device with SRCU
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Tue, 23 Jan 2024 11:01:10 +0000 (12:01 +0100)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Mon, 12 Feb 2024 10:00:31 +0000 (11:00 +0100)
commitd83cee3d2bb131c7fca7e19f1e33dc7530fd7083
treedc59f13db90d3fa22c44d0348534bedadc3dce33
parent47d8b4c1d868148c8fb51b785a89e58ca2d02c4d
gpio: protect the pointer to gpio_chip in gpio_device with SRCU

Ensure we cannot crash if the GPIO device gets unregistered (and the
chip pointer set to NULL) during any of the API calls.

To that end: wait for all users of gdev->chip to exit their read-only
SRCU critical sections in gpiochip_remove().

For brevity: add a guard class which can be instantiated at the top of
every function requiring read-only access to the chip pointer and use it
in all API calls taking a GPIO descriptor as argument. In places where
we only deal with the GPIO device - use regular guard() helpers and
rcu_dereference() for chip access. Do the same in API calls taking a
const pointer to gpio_desc.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
drivers/gpio/gpiolib-cdev.c
drivers/gpio/gpiolib-sysfs.c
drivers/gpio/gpiolib.c
drivers/gpio/gpiolib.h