Struct gpio_device now provides a revocable provider to the underlying
struct gpio_chip.  Leverage revocable for line_fileops so that it doesn't
need to handle the synchronization by accessing the SRCU explicitly.

Also, it's unneeded to hold a reference count to the struct gpio_device
while the file is opening.  The struct gpio_device
(i.e., (struct gpio_chip *)->gpiodev)) is valid as long as struct
gpio_chip is valid.

Signed-off-by: Tzung-Bi Shih <[email protected]>
---
 drivers/gpio/gpiolib-cdev.c | 53 +++++++++++++++++++++++++------------
 1 file changed, 36 insertions(+), 17 deletions(-)

diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c
index f7c6f1367235..f078d135a581 100644
--- a/drivers/gpio/gpiolib-cdev.c
+++ b/drivers/gpio/gpiolib-cdev.c
@@ -488,7 +488,7 @@ struct line {
 
 /**
  * struct linereq - contains the state of a userspace line request
- * @gdev: the GPIO device the line request pertains to
+ * @chip_rev: revocable consumer handle for the corresponding struct gpio_chip
  * @label: consumer label used to tag GPIO descriptors
  * @num_lines: the number of lines in the lines array
  * @wait: wait queue that handles blocking reads of events
@@ -503,7 +503,7 @@ struct line {
  * @lines: the lines held by this line request, with @num_lines elements.
  */
 struct linereq {
-       struct gpio_device *gdev;
+       struct revocable *chip_rev;
        const char *label;
        u32 num_lines;
        wait_queue_head_t wait;
@@ -1437,10 +1437,10 @@ static long linereq_ioctl(struct file *file, unsigned 
int cmd,
 {
        struct linereq *lr = file->private_data;
        void __user *ip = (void __user *)arg;
+       struct gpio_chip *gc;
 
-       guard(srcu)(&lr->gdev->srcu);
-
-       if (!rcu_access_pointer(lr->gdev->chip))
+       REVOCABLE_TRY_ACCESS_WITH(lr->chip_rev, gc);
+       if (!gc)
                return -ENODEV;
 
        switch (cmd) {
@@ -1468,10 +1468,10 @@ static __poll_t linereq_poll(struct file *file,
 {
        struct linereq *lr = file->private_data;
        __poll_t events = 0;
+       struct gpio_chip *gc;
 
-       guard(srcu)(&lr->gdev->srcu);
-
-       if (!rcu_access_pointer(lr->gdev->chip))
+       REVOCABLE_TRY_ACCESS_WITH(lr->chip_rev, gc);
+       if (!gc)
                return EPOLLHUP | EPOLLERR;
 
        poll_wait(file, &lr->wait, wait);
@@ -1490,10 +1490,10 @@ static ssize_t linereq_read(struct file *file, char 
__user *buf,
        struct gpio_v2_line_event le;
        ssize_t bytes_read = 0;
        int ret;
+       struct gpio_chip *gc;
 
-       guard(srcu)(&lr->gdev->srcu);
-
-       if (!rcu_access_pointer(lr->gdev->chip))
+       REVOCABLE_TRY_ACCESS_WITH(lr->chip_rev, gc);
+       if (!gc)
                return -ENODEV;
 
        if (count < sizeof(le))
@@ -1537,9 +1537,17 @@ static void linereq_free(struct linereq *lr)
 {
        unsigned int i;
 
-       if (lr->device_unregistered_nb.notifier_call)
-               blocking_notifier_chain_unregister(&lr->gdev->device_notifier,
-                                                  &lr->device_unregistered_nb);
+       if (lr->device_unregistered_nb.notifier_call) {
+               struct gpio_chip *gc;
+
+               REVOCABLE_TRY_ACCESS_WITH(lr->chip_rev, gc);
+               if (gc) {
+                       struct gpio_device *gdev = gc->gpiodev;
+
+                       
blocking_notifier_chain_unregister(&gdev->device_notifier,
+                                       &lr->device_unregistered_nb);
+               }
+       }
 
        for (i = 0; i < lr->num_lines; i++) {
                if (lr->lines[i].desc) {
@@ -1549,7 +1557,8 @@ static void linereq_free(struct linereq *lr)
        }
        kfifo_free(&lr->events);
        kfree(lr->label);
-       gpio_device_put(lr->gdev);
+       if (lr->chip_rev)
+               revocable_free(lr->chip_rev);
        kvfree(lr);
 }
 
@@ -1565,9 +1574,15 @@ static int linereq_release(struct inode *inode, struct 
file *file)
 static void linereq_show_fdinfo(struct seq_file *out, struct file *file)
 {
        struct linereq *lr = file->private_data;
-       struct device *dev = &lr->gdev->dev;
+       struct gpio_chip *gc;
+       struct device *dev;
        u16 i;
 
+       REVOCABLE_TRY_ACCESS_WITH(lr->chip_rev, gc);
+       if (!gc)
+               return;
+       dev = &gc->gpiodev->dev;
+
        seq_printf(out, "gpio-chip:\t%s\n", dev_name(dev));
 
        for (i = 0; i < lr->num_lines; i++)
@@ -1620,7 +1635,11 @@ static int linereq_create(struct gpio_device *gdev, void 
__user *ip)
                return -ENOMEM;
        lr->num_lines = ulr.num_lines;
 
-       lr->gdev = gpio_device_get(gdev);
+       lr->chip_rev = revocable_alloc(gdev->chip_rp);
+       if (!lr->chip_rev) {
+               ret = -ENOMEM;
+               goto out_free_linereq;
+       }
 
        for (i = 0; i < ulr.num_lines; i++) {
                lr->lines[i].req = lr;
-- 
2.52.0.457.g6b5491de43-goog


Reply via email to