gpio: cdev: wake up lineevent poll() on device unbind
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Thu, 17 Aug 2023 13:28:31 +0000 (15:28 +0200)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Mon, 21 Aug 2023 13:57:05 +0000 (15:57 +0200)
Add a notifier block to the lineevent_state structure and register it
with the gpio_device's device notifier. Upon reception of an event, 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@linaro.org>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Reviewed-by: Kent Gibson <warthog618@gmail.com>
drivers/gpio/gpiolib-cdev.c

index 7d0c831..35dcaf7 100644 (file)
@@ -1802,6 +1802,7 @@ out_free_linereq:
  * @eflags: the event flags this line was requested with
  * @irq: the interrupt that trigger in response to events on this GPIO
  * @wait: wait queue that handles blocking reads of events
+ * @device_unregistered_nb: notifier block for receiving gdev unregister events
  * @events: KFIFO for the GPIO events
  * @timestamp: cache for the timestamp storing it between hardirq
  * and IRQ thread, used to bring the timestamp close to the actual
@@ -1814,6 +1815,7 @@ struct lineevent_state {
        u32 eflags;
        int irq;
        wait_queue_head_t wait;
+       struct notifier_block device_unregistered_nb;
        DECLARE_KFIFO(events, struct gpioevent_data, 16);
        u64 timestamp;
 };
@@ -1847,6 +1849,17 @@ static __poll_t lineevent_poll(struct file *file,
        return call_poll_locked(file, wait, le->gdev, lineevent_poll_unlocked);
 }
 
+static int lineevent_unregistered_notify(struct notifier_block *nb,
+                                        unsigned long action, void *data)
+{
+       struct lineevent_state *le = container_of(nb, struct lineevent_state,
+                                                 device_unregistered_nb);
+
+       wake_up_poll(&le->wait, EPOLLIN | EPOLLERR);
+
+       return NOTIFY_OK;
+}
+
 struct compat_gpioeevent_data {
        compat_u64      timestamp;
        u32             id;
@@ -1932,6 +1945,9 @@ static ssize_t lineevent_read(struct file *file, char __user *buf,
 
 static void lineevent_free(struct lineevent_state *le)
 {
+       if (le->device_unregistered_nb.notifier_call)
+               blocking_notifier_chain_unregister(&le->gdev->device_notifier,
+                                                  &le->device_unregistered_nb);
        if (le->irq)
                free_irq(le->irq, le);
        if (le->desc)
@@ -2160,6 +2176,12 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)
        INIT_KFIFO(le->events);
        init_waitqueue_head(&le->wait);
 
+       le->device_unregistered_nb.notifier_call = lineevent_unregistered_notify;
+       ret = blocking_notifier_chain_register(&gdev->device_notifier,
+                                              &le->device_unregistered_nb);
+       if (ret)
+               goto out_free_le;
+
        /* Request a thread to read the events */
        ret = request_threaded_irq(irq,
                                   lineevent_irq_handler,