[PATCH 2/2] gpiolib: Fix unaligned used of reference counters

From: Ricardo Ribalda Delgado
Date: Fri Jun 03 2016 - 13:10:26 EST

gpiolib relies on the reference counters to clean up the gpio_device

Although the number of get/put is properly aligned on gpiolib.c
itself, it does not take into consideration how the referece counters
are affected by other external functions such as cdev_add and device_add.

Because of this, after the last call to put_device, the reference counter
has a value of +3, therefore never calling gpiodevice_release.

Due to the fact that some of the device has already been cleaned on
gpiochip_remove, the library will end up OOPsing the kernel (e.g. a call
to of_gpiochip_find_and_xlate).

Signed-off-by: Ricardo Ribalda Delgado <ricardo.ribalda@xxxxxxxxx>

We might want to cc: stable on these two patches

drivers/gpio/gpiolib.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 0626cc96744b..d829f2127524 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -438,7 +438,6 @@ static void gpiodevice_release(struct device *dev)
struct gpio_device *gdev = dev_get_drvdata(dev);

- cdev_del(&gdev->chrdev);
ida_simple_remove(&gpio_ida, gdev->id);
@@ -471,7 +470,6 @@ static int gpiochip_setup_dev(struct gpio_device *gdev)

/* From this point, the .release() function cleans up gpio_device */
gdev->dev.release = gpiodevice_release;
- get_device(&gdev->dev);
pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
__func__, gdev->base, gdev->base + gdev->ngpio - 1,
dev_name(&gdev->dev), gdev->chip->label ? : "generic");
@@ -759,6 +757,8 @@ void gpiochip_remove(struct gpio_chip *chip)
* be removed, else it will be dangling until the last user is
* gone.
+ cdev_del(&gdev->chrdev);
+ device_del(&gdev->dev);