[PATCH 3/5] gpio: cdev: wake up chardev poll() on device unbind

From: Bartosz Golaszewski
Date: Wed Aug 16 2023 - 08:21:33 EST


From: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx>

Emit a new notifier event right after the character device was
unregistered and receive it in the notifier owned by the descriptor
associated with the character device file. Upon reception, wake up the
wait queue so that the user-space be forced out of poll() and need to
go into a new system call which will then fail due to the chip being
gone.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx>
---
drivers/gpio/gpiolib-cdev.c | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c
index 660d2e057451..eb8c0cb71da4 100644
--- a/drivers/gpio/gpiolib-cdev.c
+++ b/drivers/gpio/gpiolib-cdev.c
@@ -32,6 +32,12 @@
#include "gpiolib.h"
#include "gpiolib-cdev.h"

+/*
+ * Let's use a higher value for gpio_device notifications to not conflict with
+ * UAPI defines that can also serve as notifier action arguments.
+ */
+#define GPIO_CDEV_UNREGISTERED 1000
+
/*
* Array sizes must ensure 64-bit alignment and not create holes in the
* struct packing.
@@ -2526,6 +2532,9 @@ static int gpio_chardev_notify(struct notifier_block *nb, unsigned long action,
else
pr_debug_ratelimited("lineinfo event FIFO is full - event dropped\n");
break;
+ case GPIO_CDEV_UNREGISTERED:
+ wake_up_poll(&cdev->wait, EPOLLIN | EPOLLERR);
+ break;
default:
return NOTIFY_DONE;
}
@@ -2761,4 +2770,6 @@ int gpiolib_cdev_register(struct gpio_device *gdev, dev_t devt)
void gpiolib_cdev_unregister(struct gpio_device *gdev)
{
cdev_device_del(&gdev->chrdev, &gdev->dev);
+ blocking_notifier_call_chain(&gdev->notifier,
+ GPIO_CDEV_UNREGISTERED, NULL);
}
--
2.39.2