Skip to contentSkip to conversation

gpiolib: use __counted_by() for GPIO descriptors

PATCH In review Bartosz Golaszewski 3 participants 5 messages Dec 19, 2023 – Dec 20, 2023 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpiolib: use __counted_by() for GPIO descriptors

From: Bartosz Golaszewski <[email protected]>

Pull the array of GPIO descriptors into struct gpio_device as a flexible array and use __counted_by() to control its size.

---
 drivers/gpio/gpiolib.c | 14 ++----
 drivers/gpio/gpiolib.h | 98 +++++++++++++++++++++---------------------
 2 files changed, 52 insertions(+), 60 deletions(-)
drivers/gpio/gpiolib.c +3 -11
index c9ca809b55de..7ebeb5bb7918 100644
@@ -18,6 +18,7 @@
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/of.h>
+#include <linux/overflow.h>
 #include <linux/pinctrl/consumer.h>
 #include <linux/seq_file.h>
 #include <linux/slab.h>
@@ -669,7 +670,6 @@ static void gpiodev_release(struct device *dev)
 
 	ida_free(&gpio_ida, gdev->id);
 	kfree_const(gdev->label);
-	kfree(gdev->descs);
 	kfree(gdev);
 }
 
@@ -831,7 +831,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 	 * First: allocate and populate the internal stat container, and
 	 * set up the struct device.
 	 */
-	gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
+	gdev = kzalloc(struct_size(gdev, descs, gc->ngpio), GFP_KERNEL);
 	if (!gdev)
 		return -ENOMEM;
 	gdev->dev.bus = &gpio_bus_type;
@@ -873,16 +873,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 	if (ret)
 		goto err_free_dev_name;
 
-	gdev->descs = kcalloc(gc->ngpio, sizeof(*gdev->descs), GFP_KERNEL);
-	if (!gdev->descs) {
-		ret = -ENOMEM;
-		goto err_free_dev_name;
-	}
-
 	gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL);
 	if (!gdev->label) {
 		ret = -ENOMEM;
-		goto err_free_descs;
+		goto err_free_dev_name;
 	}
 
 	gdev->ngpio = gc->ngpio;
@@ -1021,8 +1015,6 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 		list_del(&gdev->list);
 err_free_label:
 	kfree_const(gdev->label);
-err_free_descs:
-	kfree(gdev->descs);
 err_free_dev_name:
 	kfree(dev_name(&gdev->dev));
 err_free_ida:
drivers/gpio/gpiolib.h +49 -49

134 lines not on this page · raw message

Signature · 1 line
2.40.1
#1 lore raw
Quoted 5 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Pull the array of GPIO descriptors into struct gpio_device as a flexible array and use __counted_by() to control its size.

Looks ok to me, though that might be because it looks a lot like cdev's struct linereq.

Cheers,
Kent.

#2 lore raw
In reply to Bartosz Golaszewski
Quoted 4 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Pull the array of GPIO descriptors into struct gpio_device as a flexible array and use __counted_by() to control its size.

How big is the struct gpio_device? Unifying like this might provoke subtle errors on very fragmented memory, where k*alloc() might not find enough free space. Note, k*alloc() guarantees finding only memory for a single page. With PAGE_SIZE = 4k, this might be an issue.

I would suggest, if nothing prevents us from switching, to use kvmalloc().

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 7 lines · Andy Shevchenko
Quoted 4 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Pull the array of GPIO descriptors into struct gpio_device as a flexible array and use __counted_by() to control its size.

Quoted 7 lines

How big is the struct gpio_device? Unifying like this might provoke subtle errors on very fragmented memory, where k*alloc() might not find enough free space. Note, k*alloc() guarantees finding only memory for a single page. With PAGE_SIZE = 4k, this might be an issue.

I would suggest, if nothing prevents us from switching, to use kvmalloc().

That's a good point but there's another thing. We need to call gpiochip_get_ngpios() before the allocation. I need to revisit this one.

Bart

Quoted 5 lines

--
With Best Regards,
Andy Shevchenko

#4 lore raw
Quoted 15 lines · Bartosz Golaszewski
Quoted 12 lines · Andy Shevchenko
Quoted 4 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Pull the array of GPIO descriptors into struct gpio_device as a flexible array and use __counted_by() to control its size.

How big is the struct gpio_device? Unifying like this might provoke subtle errors on very fragmented memory, where k*alloc() might not find enough free space. Note, k*alloc() guarantees finding only memory for a single page. With PAGE_SIZE = 4k, this might be an issue.

Quoted 7 lines
Quoted 2 lines

I would suggest, if nothing prevents us from switching, to use kvmalloc().

That's a good point but there's another thing. We need to call gpiochip_get_ngpios() before the allocation. I need to revisit this one.

It's trickier than I thought. We need the struct device to exist (have its software node assigned) before we check the property but we cannot allocate gdev (embedding struct device) + descs before we have read it. Eh, maybe it's not worth it and let's keep the two allocations separate.

Bart

Quoted 7 lines

Bart

Quoted 5 lines

--
With Best Regards,
Andy Shevchenko