Skip to contentSkip to conversation

gpio: ljca: reduce struct allocation

PATCH Superseded Rosen Penev 3 participants 5 messages Mar 10 – Mar 12

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 gpio: ljca: reduce struct allocation

Convert connect_mode to a flexible array member to avoid calling kcalloc and to combine the allocations.

Add __counted_by for extra runtime analysis.

---
v2: add counting variable

 drivers/gpio/gpio-ljca.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/gpio/gpio-ljca.c +9 -9
index f32d1d237795..61252e0eb444 100644
@@ -63,7 +63,6 @@ struct ljca_gpio_dev {
 	DECLARE_BITMAP(enabled_irqs, LJCA_MAX_GPIO_NUM);
 	DECLARE_BITMAP(reenable_irqs, LJCA_MAX_GPIO_NUM);
 	DECLARE_BITMAP(output_enabled, LJCA_MAX_GPIO_NUM);
-	u8 *connect_mode;
 	/* protect irq bus */
 	struct mutex irq_lock;
 	struct work_struct work;
@@ -72,6 +71,9 @@ struct ljca_gpio_dev {
 
 	u8 obuf[LJCA_GPIO_BUF_SIZE];
 	u8 ibuf[LJCA_GPIO_BUF_SIZE];
+
+	size_t num;
+	u8 connect_mode[] __counted_by(num);
 };
 
 static int ljca_gpio_config(struct ljca_gpio_dev *ljca_gpio, u8 gpio_id,
@@ -400,22 +402,20 @@ static int ljca_gpio_probe(struct auxiliary_device *auxdev,
 			   const struct auxiliary_device_id *aux_dev_id)
 {
 	struct ljca_client *ljca = auxiliary_dev_to_ljca_client(auxdev);
+	struct ljca_gpio_info *gpio_info = dev_get_platdata(&auxdev->dev);
 	struct ljca_gpio_dev *ljca_gpio;
 	struct gpio_irq_chip *girq;
 	int ret;
 
-	ljca_gpio = devm_kzalloc(&auxdev->dev, sizeof(*ljca_gpio), GFP_KERNEL);
+	ljca_gpio = devm_kzalloc(&auxdev->dev,
+				 struct_size(ljca_gpio, connect_mode, gpio_info->num),
+				 GFP_KERNEL);
 	if (!ljca_gpio)
 		return -ENOMEM;
 
+	ljca_gpio->num = gpio_info->num;
 	ljca_gpio->ljca = ljca;
-	ljca_gpio->gpio_info = dev_get_platdata(&auxdev->dev);
-	ljca_gpio->connect_mode = devm_kcalloc(&auxdev->dev,
-					       ljca_gpio->gpio_info->num,
-					       sizeof(*ljca_gpio->connect_mode),
-					       GFP_KERNEL);
-	if (!ljca_gpio->connect_mode)
-		return -ENOMEM;
+	ljca_gpio->gpio_info = gpio_info;
 
 	ret = devm_mutex_init(&auxdev->dev, &ljca_gpio->irq_lock);
 	if (ret)

--
2.53.0

maintainer
#1 lore raw

Hi Rosen,

Quoted 24 lines · Rosen Penev

Convert connect_mode to a flexible array member to avoid calling kcalloc and to combine the allocations.

Add __counted_by for extra runtime analysis.

---
v2: add counting variable

 drivers/gpio/gpio-ljca.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/gpio/gpio-ljca.c +0 -1
index f32d1d237795..61252e0eb444 100644
@@ -63,7 +63,6 @@ struct ljca_gpio_dev {
 	DECLARE_BITMAP(enabled_irqs, LJCA_MAX_GPIO_NUM);
 	DECLARE_BITMAP(reenable_irqs, LJCA_MAX_GPIO_NUM);
 	DECLARE_BITMAP(output_enabled, LJCA_MAX_GPIO_NUM);
-	u8 *connect_mode;
 	/* protect irq bus */
 	struct mutex irq_lock;
 	struct work_struct work;

drivers/gpio/gpio-ljca.c struct ljca_gpio_dev { · line 75

Quoted 5 lines
drivers/gpio/gpio-ljca.c +2 -0
@@ -72,6 +71,9 @@ struct ljca_gpio_dev {
 
 	u8 obuf[LJCA_GPIO_BUF_SIZE];
 	u8 ibuf[LJCA_GPIO_BUF_SIZE];
+
+	size_t num;

num is unsigned int in struct ljca_gpio_info so I'd use that here, too.

Quoted 33 lines
drivers/gpio/gpio-ljca.c +7 -8
+	u8 connect_mode[] __counted_by(num);
 };
 
 static int ljca_gpio_config(struct ljca_gpio_dev *ljca_gpio, u8 gpio_id,
@@ -400,22 +402,20 @@ static int ljca_gpio_probe(struct auxiliary_device *auxdev,
 			   const struct auxiliary_device_id *aux_dev_id)
 {
 	struct ljca_client *ljca = auxiliary_dev_to_ljca_client(auxdev);
+	struct ljca_gpio_info *gpio_info = dev_get_platdata(&auxdev->dev);
 	struct ljca_gpio_dev *ljca_gpio;
 	struct gpio_irq_chip *girq;
 	int ret;
 
-	ljca_gpio = devm_kzalloc(&auxdev->dev, sizeof(*ljca_gpio), GFP_KERNEL);
+	ljca_gpio = devm_kzalloc(&auxdev->dev,
+				 struct_size(ljca_gpio, connect_mode, gpio_info->num),
+				 GFP_KERNEL);
 	if (!ljca_gpio)
 		return -ENOMEM;
 
+	ljca_gpio->num = gpio_info->num;
 	ljca_gpio->ljca = ljca;
-	ljca_gpio->gpio_info = dev_get_platdata(&auxdev->dev);
-	ljca_gpio->connect_mode = devm_kcalloc(&auxdev->dev,
-					       ljca_gpio->gpio_info->num,
-					       sizeof(*ljca_gpio->connect_mode),
-					       GFP_KERNEL);
-	if (!ljca_gpio->connect_mode)
-		return -ENOMEM;
+	ljca_gpio->gpio_info = gpio_info;
 
 	ret = devm_mutex_init(&auxdev->dev, &ljca_gpio->irq_lock);
 	if (ret)
Signature · 3 lines
Regards,

Sakari Ailus
#3 lore raw
Quoted 7 lines · Linus Walleij
Quoted 1 line · Rosen Penev

+ size_t num;

What about naming it num_connect_modes?
(and unsigned in as Sakari says.)

Too long IMO. Makes the most sense to match variable names.

Quoted 3 lines

Yours,
Linus Walleij

maintainer
#4 lore raw
Quoted 9 lines · Rosen Penev
Quoted 6 lines · Linus Walleij
Quoted 1 line · Rosen Penev

+ size_t num;

What about naming it num_connect_modes?
(and unsigned in as Sakari says.)

Too long IMO. Makes the most sense to match variable names.

This is one of those times I would bring up Rusty Russell's API item #6 "The name tells you how to use it." well, does it? Number of what?

Yours,
Linus Walleij