Skip to contentSkip to conversation

gpiolib: Provide to_gpio_device() helper

PATCH In review Andy Shevchenko 3 participants 6 messages Nov 28, 2022 – Nov 29, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 gpiolib: Provide to_gpio_device() helper

Provide to_gpio_device() helper which can be utilized in the existing and future code.

While at it, make sure it becomes no-op at compilation time.

---
v2: made helper static inline (Bart)

 drivers/gpio/gpiolib.c | 2 +-
 drivers/gpio/gpiolib.h | 9 +++++++--
 2 files changed, 8 insertions(+), 3 deletions(-)
drivers/gpio/gpiolib.c +1 -1
index a11480634213..ccd47da4ec66 100644
@@ -548,7 +548,7 @@ EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
 
 static void gpiodevice_release(struct device *dev)
 {
-	struct gpio_device *gdev = container_of(dev, struct gpio_device, dev);
+	struct gpio_device *gdev = to_gpio_device(dev);
 	unsigned long flags;
 
 	spin_lock_irqsave(&gpio_lock, flags);
drivers/gpio/gpiolib.h +7 -2
index d900ecdbac46..e443c1023a37 100644
@@ -20,9 +20,9 @@
 
 /**
  * struct gpio_device - internal state container for GPIO devices
- * @id: numerical ID number for the GPIO chip
  * @dev: the GPIO device struct
  * @chrdev: character device for the GPIO device
+ * @id: numerical ID number for the GPIO chip
  * @mockdev: class device used by the deprecated sysfs interface (may be
  * NULL)
  * @owner: helps prevent removal of modules exporting active GPIOs
@@ -47,9 +47,9 @@
  * userspace.
  */
 struct gpio_device {
-	int			id;
 	struct device		dev;
 	struct cdev		chrdev;
+	int			id;
 	struct device		*mockdev;
 	struct module		*owner;
 	struct gpio_chip	*chip;
@@ -72,6 +72,11 @@ struct gpio_device {
 #endif
 };
 
+static inline struct gpio_device *to_gpio_device(struct device *dev)
+{
+	return container_of(dev, struct gpio_device, dev);
+}
+
 /* gpio suffixes used for ACPI and device tree lookup */
 static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" };
 
Signature · 1 line
2.35.1
#1 lore raw

PATCH v2 2/2 gpiolib: Introduce gpio_device_get() and gpio_device_put()

Introduce gpio_device_get() and gpio_device_put() helpers and convert existing users.

---
v2: no changes

 drivers/gpio/gpiolib-cdev.c | 21 +++++++++------------
 drivers/gpio/gpiolib.c      | 14 ++++++--------
 drivers/gpio/gpiolib.h      | 10 ++++++++++
 3 files changed, 25 insertions(+), 20 deletions(-)
drivers/gpio/gpiolib-cdev.c +9 -12
index 8ecec2f56321..a76f2e79a776 100644
@@ -265,7 +265,7 @@ static void linehandle_free(struct linehandle_state *lh)
 		if (lh->descs[i])
 			gpiod_free(lh->descs[i]);
 	kfree(lh->label);
-	put_device(&lh->gdev->dev);
+	gpio_device_put(lh->gdev);
 	kfree(lh);
 }
 
@@ -307,8 +307,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
 	lh = kzalloc(sizeof(*lh), GFP_KERNEL);
 	if (!lh)
 		return -ENOMEM;
-	lh->gdev = gdev;
-	get_device(&gdev->dev);
+	lh->gdev = gpio_device_get(gdev);
 
 	if (handlereq.consumer_label[0] != '\0') {
 		/* label is only initialized if consumer_label is set */
@@ -1487,7 +1486,7 @@ static void linereq_free(struct linereq *lr)
 	}
 	kfifo_free(&lr->events);
 	kfree(lr->label);
-	put_device(&lr->gdev->dev);
+	gpio_device_put(lr->gdev);
 	kfree(lr);
 }
 
@@ -1557,8 +1556,7 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip)
 	if (!lr)
 		return -ENOMEM;
 
-	lr->gdev = gdev;
-	get_device(&gdev->dev);
+	lr->gdev = gpio_device_get(gdev);
 
 	for (i = 0; i < ulr.num_lines; i++) {
 		lr->lines[i].req = lr;
@@ -1806,7 +1804,7 @@ static void lineevent_free(struct lineevent_state *le)
 	if (le->desc)
 		gpiod_free(le->desc);
 	kfree(le->label);
-	put_device(&le->gdev->dev);
+	gpio_device_put(le->gdev);
 	kfree(le);
 }
 
@@ -1972,8 +1970,7 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)
 	le = kzalloc(sizeof(*le), GFP_KERNEL);
 	if (!le)
 		return -ENOMEM;
-	le->gdev = gdev;
-	get_device(&gdev->dev);
+	le->gdev = gpio_device_get(gdev);
 
 	if (eventreq.consumer_label[0] != '\0') {
 		/* label is only initialized if consumer_label is set */
@@ -2521,7 +2518,7 @@ static int gpio_chrdev_open(struct inode *inode, struct file *file)
 
 	init_waitqueue_head(&cdev->wait);
 	INIT_KFIFO(cdev->events);
-	cdev->gdev = gdev;
+	cdev->gdev = gpio_device_get(gdev);
 
 	cdev->lineinfo_changed_nb.notifier_call = lineinfo_changed_notify;
 	ret = blocking_notifier_chain_register(&gdev->notifier,
@@ -2529,7 +2526,6 @@ static int gpio_chrdev_open(struct inode *inode, struct file *file)
 	if (ret)
 		goto out_free_bitmap;
 
-	get_device(&gdev->dev);
 	file->private_data = cdev;
 
 	ret = nonseekable_open(inode, file);
@@ -2542,6 +2538,7 @@ static int gpio_chrdev_open(struct inode *inode, struct file *file)
 	blocking_notifier_chain_unregister(&gdev->notifier,
 					   &cdev->lineinfo_changed_nb);
 out_free_bitmap:
+	gpio_device_put(gdev);
 	bitmap_free(cdev->watched_lines);
 out_free_cdev:
 	kfree(cdev);
@@ -2562,7 +2559,7 @@ static int gpio_chrdev_release(struct inode *inode, struct file *file)
 	bitmap_free(cdev->watched_lines);
 	blocking_notifier_chain_unregister(&gdev->notifier,
 					   &cdev->lineinfo_changed_nb);
-	put_device(&gdev->dev);
+	gpio_device_put(gdev);
 	kfree(cdev);
 
 	return 0;
drivers/gpio/gpiolib.c +6 -8
index ccd47da4ec66..a54bad1ea3e5 100644
@@ -875,7 +875,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 	gpiochip_free_valid_mask(gc);
 	if (gdev->dev.release) {
 		/* release() has been registered by gpiochip_setup_dev() */
-		put_device(&gdev->dev);
+		gpio_device_put(gdev);
 		goto err_print_message;
 	}
 err_remove_from_list:
@@ -962,7 +962,7 @@ void gpiochip_remove(struct gpio_chip *gc)
 	 * gone.
 	 */
 	gcdev_unregister(gdev);
-	put_device(&gdev->dev);
+	gpio_device_put(gdev);
 }
 EXPORT_SYMBOL_GPL(gpiochip_remove);
 
@@ -2053,17 +2053,15 @@ static int validate_desc(const struct gpio_desc *desc, const char *func)
 int gpiod_request(struct gpio_desc *desc, const char *label)
 {
 	int ret = -EPROBE_DEFER;
-	struct gpio_device *gdev;
 
 	VALIDATE_DESC(desc);
-	gdev = desc->gdev;
 
-	if (try_module_get(gdev->owner)) {
+	if (try_module_get(desc->gdev->owner)) {
 		ret = gpiod_request_commit(desc, label);
 		if (ret)
-			module_put(gdev->owner);
+			module_put(desc->gdev->owner);
 		else
-			get_device(&gdev->dev);
+			gpio_device_get(desc->gdev);
 	}
 
 	if (ret)
@@ -2124,7 +2122,7 @@ void gpiod_free(struct gpio_desc *desc)
 {
 	if (desc && desc->gdev && gpiod_free_commit(desc)) {
 		module_put(desc->gdev->owner);
-		put_device(&desc->gdev->dev);
+		gpio_device_put(desc->gdev);
 	} else {
 		WARN_ON(extra_checks);
 	}
drivers/gpio/gpiolib.h +10 -0
index e443c1023a37..c342950ad200 100644
@@ -77,6 +77,16 @@ static inline struct gpio_device *to_gpio_device(struct device *dev)
 	return container_of(dev, struct gpio_device, dev);
 }
 
+static inline struct gpio_device *gpio_device_get(struct gpio_device *gdev)
+{
+	return to_gpio_device(get_device(&gdev->dev));
+}
+
+static inline void gpio_device_put(struct gpio_device *gdev)
+{
+	put_device(&gdev->dev);
+}
+
 /* gpio suffixes used for ACPI and device tree lookup */
 static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" };
 
Signature · 1 line
2.35.1
#3 lore raw
In reply to Andy Shevchenko
Quoted 42 lines · Andy Shevchenko

Provide to_gpio_device() helper which can be utilized in the existing and future code.

While at it, make sure it becomes no-op at compilation time.

---
v2: made helper static inline (Bart)

 drivers/gpio/gpiolib.c | 2 +-
 drivers/gpio/gpiolib.h | 9 +++++++--
 2 files changed, 8 insertions(+), 3 deletions(-)
drivers/gpio/gpiolib.c +1 -1
index a11480634213..ccd47da4ec66 100644
@@ -548,7 +548,7 @@ EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
 
 static void gpiodevice_release(struct device *dev)
 {
-       struct gpio_device *gdev = container_of(dev, struct gpio_device, dev);
+       struct gpio_device *gdev = to_gpio_device(dev);
        unsigned long flags;
 
        spin_lock_irqsave(&gpio_lock, flags);
drivers/gpio/gpiolib.h +1 -1
index d900ecdbac46..e443c1023a37 100644
@@ -20,9 +20,9 @@
 
 /**
  * struct gpio_device - internal state container for GPIO devices
- * @id: numerical ID number for the GPIO chip
  * @dev: the GPIO device struct
  * @chrdev: character device for the GPIO device
+ * @id: numerical ID number for the GPIO chip
  * @mockdev: class device used by the deprecated sysfs interface (may be
  * NULL)
  * @owner: helps prevent removal of modules exporting active GPIOs

drivers/gpio/gpiolib.h line 50

Quoted 4 lines
drivers/gpio/gpiolib.h +0 -1
@@ -47,9 +47,9 @@
  * userspace.
  */
 struct gpio_device {
-       int                     id;

Sorry I didn't notice it before but why the churn? This is not needed for container_of to work.

Bart

Quoted 21 lines
drivers/gpio/gpiolib.h +6 -1
        struct device           dev;
        struct cdev             chrdev;
+       int                     id;
        struct device           *mockdev;
        struct module           *owner;
        struct gpio_chip        *chip;
@@ -72,6 +72,11 @@ struct gpio_device {
 #endif
 };
 
+static inline struct gpio_device *to_gpio_device(struct device *dev)
+{
+       return container_of(dev, struct gpio_device, dev);
+}
+
 /* gpio suffixes used for ACPI and device tree lookup */
 static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" };
 
--

2.35.1

#4 lore raw
Quoted 2 lines · Bartosz Golaszewski

On Mon, Nov 28, 2022 at 8:00 PM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 1 line
Quoted 1 line

While at it, make sure it becomes no-op at compilation time.

^^^

Quoted 6 lines
Quoted 1 line

- int id;

Sorry I didn't notice it before but why the churn? This is not needed for container_of to work.

Quoted 1 line

+ int id;

I hope this explains and can be accepted.

Quoted 1 line
Quoted 1 line

};

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 11 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski

On Mon, Nov 28, 2022 at 8:00 PM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 1 line
Quoted 1 line

While at it, make sure it becomes no-op at compilation time.

^^^

Quoted 9 lines
Quoted 6 lines
Quoted 1 line

- int id;

Sorry I didn't notice it before but why the churn? This is not needed for container_of to work.

Quoted 1 line

+ int id;

I hope this explains and can be accepted.

Ah, alright makes sense. Applying.

Bart