Skip to contentSkip to conversation

leds: pca955x: kzalloc + kcalloc to single kzalloc

PATCH In review Rosen Penev 2 participants 5 messages Mar 20 – Apr 9 LED

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 leds: pca955x: kzalloc + kcalloc to single kzalloc

Two fewer allocations as a result.

Required placing some structs before others as flexible array members require a complete definition. Declaration is not enough.

Added __counted_by support for one of the structs for extra runtime analysis.

---
 drivers/leds/leds-pca955x.c | 45 ++++++++++++++-----------------------
 1 file changed, 17 insertions(+), 28 deletions(-)
drivers/leds/leds-pca955x.c +17 -28
index 2007fe6217ec..ee5f02eaa3c9 100644
@@ -112,19 +112,6 @@ static const struct pca955x_chipdef pca955x_chipdefs[] = {
 	},
 };
 
-struct pca955x {
-	struct mutex lock;
-	struct pca955x_led *leds;
-	const struct pca955x_chipdef	*chipdef;
-	struct i2c_client	*client;
-	unsigned long active_blink;
-	unsigned long active_pins;
-	unsigned long blink_period;
-#ifdef CONFIG_LEDS_PCA955X_GPIO
-	struct gpio_chip gpio;
-#endif
-};
-
 struct pca955x_led {
 	struct pca955x	*pca955x;
 	struct led_classdev	led_cdev;
@@ -137,8 +124,21 @@ struct pca955x_led {
 #define led_to_pca955x(l)	container_of(l, struct pca955x_led, led_cdev)
 
 struct pca955x_platform_data {
-	struct pca955x_led	*leds;
 	int			num_leds;
+	struct pca955x_led	leds[] __counted_by(num_leds);
+};
+
+struct pca955x {
+	struct mutex lock;
+	const struct pca955x_chipdef	*chipdef;
+	struct i2c_client	*client;
+	unsigned long active_blink;
+	unsigned long active_pins;
+	unsigned long blink_period;
+#ifdef CONFIG_LEDS_PCA955X_GPIO
+	struct gpio_chip gpio;
+#endif
+	struct pca955x_led leds[];
 };
 
 /* 8 bits per input register */
@@ -542,15 +542,11 @@ pca955x_get_pdata(struct i2c_client *client, const struct pca955x_chipdef *chip)
 	if (count > chip->bits)
 		return ERR_PTR(-ENODEV);
 
-	pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
+	pdata = devm_kzalloc(&client->dev, struct_size(pdata, leds, chip->bits), GFP_KERNEL);
 	if (!pdata)
 		return ERR_PTR(-ENOMEM);
 
-	pdata->leds = devm_kcalloc(&client->dev,
-				   chip->bits, sizeof(struct pca955x_led),
-				   GFP_KERNEL);
-	if (!pdata->leds)
-		return ERR_PTR(-ENOMEM);
+	pdata->num_leds = chip->bits;
 
 	device_for_each_child_node(&client->dev, child) {
 		u32 reg;
@@ -568,8 +564,6 @@ pca955x_get_pdata(struct i2c_client *client, const struct pca955x_chipdef *chip)
 		fwnode_property_read_u32(child, "type", &led->type);
 	}
 
-	pdata->num_leds = chip->bits;
-
 	return pdata;
 }
 
@@ -623,15 +617,10 @@ static int pca955x_probe(struct i2c_client *client)
 		return -ENODEV;
 	}
 
-	pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL);
+	pca955x = devm_kzalloc(&client->dev, struct_size(pca955x, leds, chip->bits), GFP_KERNEL);
 	if (!pca955x)
 		return -ENOMEM;
 
-	pca955x->leds = devm_kcalloc(&client->dev, chip->bits,
-				     sizeof(*pca955x_led), GFP_KERNEL);
-	if (!pca955x->leds)
-		return -ENOMEM;
-
 	i2c_set_clientdata(client, pca955x);
 
 	mutex_init(&pca955x->lock);
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 38 lines · Rosen Penev

Two fewer allocations as a result.

Required placing some structs before others as flexible array members require a complete definition. Declaration is not enough.

Added __counted_by support for one of the structs for extra runtime analysis.

---
 drivers/leds/leds-pca955x.c | 45 ++++++++++++++-----------------------
 1 file changed, 17 insertions(+), 28 deletions(-)
drivers/leds/leds-pca955x.c +0 -13
index 2007fe6217ec..ee5f02eaa3c9 100644
@@ -112,19 +112,6 @@ static const struct pca955x_chipdef pca955x_chipdefs[] = {
 	},
 };
 
-struct pca955x {
-	struct mutex lock;
-	struct pca955x_led *leds;
-	const struct pca955x_chipdef	*chipdef;
-	struct i2c_client	*client;
-	unsigned long active_blink;
-	unsigned long active_pins;
-	unsigned long blink_period;
-#ifdef CONFIG_LEDS_PCA955X_GPIO
-	struct gpio_chip gpio;
-#endif
-};
-
 struct pca955x_led {
 	struct pca955x	*pca955x;
 	struct led_classdev	led_cdev;

drivers/leds/leds-pca955x.c struct pca955x_led { · line 128

Quoted 6 lines
drivers/leds/leds-pca955x.c +1 -1
@@ -137,8 +124,21 @@ struct pca955x_led {
 #define led_to_pca955x(l)	container_of(l, struct pca955x_led, led_cdev)
 
 struct pca955x_platform_data {
-	struct pca955x_led	*leds;
 	int			num_leds;
+	struct pca955x_led	leds[] __counted_by(num_leds);

Where is the memory allocated to this now?

Why do we need this in both structs?

Quoted 63 lines
drivers/leds/leds-pca955x.c +16 -14
+};
+
+struct pca955x {
+	struct mutex lock;
+	const struct pca955x_chipdef	*chipdef;
+	struct i2c_client	*client;
+	unsigned long active_blink;
+	unsigned long active_pins;
+	unsigned long blink_period;
+#ifdef CONFIG_LEDS_PCA955X_GPIO
+	struct gpio_chip gpio;
+#endif
+	struct pca955x_led leds[];
 };
 
 /* 8 bits per input register */
@@ -542,15 +542,11 @@ pca955x_get_pdata(struct i2c_client *client, const struct pca955x_chipdef *chip)
 	if (count > chip->bits)
 		return ERR_PTR(-ENODEV);
 
-	pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
+	pdata = devm_kzalloc(&client->dev, struct_size(pdata, leds, chip->bits), GFP_KERNEL);
 	if (!pdata)
 		return ERR_PTR(-ENOMEM);
 
-	pdata->leds = devm_kcalloc(&client->dev,
-				   chip->bits, sizeof(struct pca955x_led),
-				   GFP_KERNEL);
-	if (!pdata->leds)
-		return ERR_PTR(-ENOMEM);
+	pdata->num_leds = chip->bits;
 
 	device_for_each_child_node(&client->dev, child) {
 		u32 reg;
@@ -568,8 +564,6 @@ pca955x_get_pdata(struct i2c_client *client, const struct pca955x_chipdef *chip)
 		fwnode_property_read_u32(child, "type", &led->type);
 	}
 
-	pdata->num_leds = chip->bits;
-
 	return pdata;
 }
 
@@ -623,15 +617,10 @@ static int pca955x_probe(struct i2c_client *client)
 		return -ENODEV;
 	}
 
-	pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL);
+	pca955x = devm_kzalloc(&client->dev, struct_size(pca955x, leds, chip->bits), GFP_KERNEL);
 	if (!pca955x)
 		return -ENOMEM;
 
-	pca955x->leds = devm_kcalloc(&client->dev, chip->bits,
-				     sizeof(*pca955x_led), GFP_KERNEL);
-	if (!pca955x->leds)
-		return -ENOMEM;
-
 	i2c_set_clientdata(client, pca955x);
 
 	mutex_init(&pca955x->lock);
Signature · 1 line
2.53.0
Signature · 1 line
Lee Jones [李琼斯]
#2 lore raw
Quoted 43 lines · Lee Jones
Quoted 40 lines · Rosen Penev

Two fewer allocations as a result.

Required placing some structs before others as flexible array members require a complete definition. Declaration is not enough.

Added __counted_by support for one of the structs for extra runtime analysis.

---
 drivers/leds/leds-pca955x.c | 45 ++++++++++++++-----------------------
 1 file changed, 17 insertions(+), 28 deletions(-)
drivers/leds/leds-pca955x.c +0 -13
index 2007fe6217ec..ee5f02eaa3c9 100644
@@ -112,19 +112,6 @@ static const struct pca955x_chipdef pca955x_chipdefs[] = {
      },
 };
 
-struct pca955x {
-     struct mutex lock;
-     struct pca955x_led *leds;
-     const struct pca955x_chipdef    *chipdef;
-     struct i2c_client       *client;
-     unsigned long active_blink;
-     unsigned long active_pins;
-     unsigned long blink_period;
-#ifdef CONFIG_LEDS_PCA955X_GPIO
-     struct gpio_chip gpio;
-#endif
-};
-
 struct pca955x_led {
      struct pca955x  *pca955x;
      struct led_classdev     led_cdev;
@@ -137,8 +124,21 @@ struct pca955x_led {
 #define led_to_pca955x(l)    container_of(l, struct pca955x_led, led_cdev)
 
Quoted 6 lines

drivers/leds/leds-pca955x.c struct pca955x_led { · line 128

Quoted 4 lines
drivers/leds/leds-pca955x.c +1 -1
 struct pca955x_platform_data {
-     struct pca955x_led      *leds;
      int                     num_leds;
+     struct pca955x_led      leds[] __counted_by(num_leds);

Where is the memory allocated to this now?

The kzalloc call was updated to use struct_size to allocate it together with the struct.

Quoted 2 lines

Why do we need this in both structs?

kzalloc and kcalloc can be combined in both locations.

Quoted 67 lines
Quoted 62 lines
drivers/leds/leds-pca955x.c +16 -15
+};
+
+struct pca955x {
+     struct mutex lock;
+     const struct pca955x_chipdef    *chipdef;
+     struct i2c_client       *client;
+     unsigned long active_blink;
+     unsigned long active_pins;
+     unsigned long blink_period;
+#ifdef CONFIG_LEDS_PCA955X_GPIO
+     struct gpio_chip gpio;
+#endif
+     struct pca955x_led leds[];
 };
 
 /* 8 bits per input register */
@@ -542,15 +542,11 @@ pca955x_get_pdata(struct i2c_client *client, const struct pca955x_chipdef *chip)
      if (count > chip->bits)
              return ERR_PTR(-ENODEV);
 
-     pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
+     pdata = devm_kzalloc(&client->dev, struct_size(pdata, leds, chip->bits), GFP_KERNEL);
      if (!pdata)
              return ERR_PTR(-ENOMEM);
 
-     pdata->leds = devm_kcalloc(&client->dev,
-                                chip->bits, sizeof(struct pca955x_led),
-                                GFP_KERNEL);
-     if (!pdata->leds)
-             return ERR_PTR(-ENOMEM);
+     pdata->num_leds = chip->bits;
 
      device_for_each_child_node(&client->dev, child) {
              u32 reg;
@@ -568,8 +564,6 @@ pca955x_get_pdata(struct i2c_client *client, const struct pca955x_chipdef *chip)
              fwnode_property_read_u32(child, "type", &led->type);
      }
 
-     pdata->num_leds = chip->bits;
-
      return pdata;
 }
 
@@ -623,15 +617,10 @@ static int pca955x_probe(struct i2c_client *client)
              return -ENODEV;
      }
 
-     pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL);
+     pca955x = devm_kzalloc(&client->dev, struct_size(pca955x, leds, chip->bits), GFP_KERNEL);
      if (!pca955x)
              return -ENOMEM;
 
-     pca955x->leds = devm_kcalloc(&client->dev, chip->bits,
-                                  sizeof(*pca955x_led), GFP_KERNEL);
-     if (!pca955x->leds)
-             return -ENOMEM;
-
      i2c_set_clientdata(client, pca955x);
 
      mutex_init(&pca955x->lock);
--
Quoted 2 lines

2.53.0

--
Lee Jones [李琼斯]

maintainer
#3 lore raw
Quoted 44 lines · Rosen Penev
Quoted 43 lines · Lee Jones
Quoted 40 lines · Rosen Penev

Two fewer allocations as a result.

Required placing some structs before others as flexible array members require a complete definition. Declaration is not enough.

Added __counted_by support for one of the structs for extra runtime analysis.

---
 drivers/leds/leds-pca955x.c | 45 ++++++++++++++-----------------------
 1 file changed, 17 insertions(+), 28 deletions(-)
drivers/leds/leds-pca955x.c +0 -13
index 2007fe6217ec..ee5f02eaa3c9 100644
@@ -112,19 +112,6 @@ static const struct pca955x_chipdef pca955x_chipdefs[] = {
      },
 };
 
-struct pca955x {
-     struct mutex lock;
-     struct pca955x_led *leds;
-     const struct pca955x_chipdef    *chipdef;
-     struct i2c_client       *client;
-     unsigned long active_blink;
-     unsigned long active_pins;
-     unsigned long blink_period;
-#ifdef CONFIG_LEDS_PCA955X_GPIO
-     struct gpio_chip gpio;
-#endif
-};
-
 struct pca955x_led {
      struct pca955x  *pca955x;
      struct led_classdev     led_cdev;
@@ -137,8 +124,21 @@ struct pca955x_led {
 #define led_to_pca955x(l)    container_of(l, struct pca955x_led, led_cdev)
 
Quoted 4 lines
Quoted 4 lines

drivers/leds/leds-pca955x.c struct pca955x_led { · line 128

Quoted 4 lines
drivers/leds/leds-pca955x.c +1 -1
 struct pca955x_platform_data {
-     struct pca955x_led      *leds;
      int                     num_leds;
+     struct pca955x_led      leds[] __counted_by(num_leds);

I'm not sure we should we be embedding the full 'struct pca955x_led' (which contains runtime framework structures like 'struct led_classdev') directly inside the platform data? Besides platform data should ideally be restricted to static configuration properties rather than runtime state.

Quoted 6 lines
Quoted 1 line

Where is the memory allocated to this now?

The kzalloc call was updated to use struct_size to allocate it together with the struct.

Quoted 2 lines

Why do we need this in both structs?

kzalloc and kcalloc can be combined in both locations.

I believe my original question might have been misunderstood. I was asking about the architectural design choice. Why does the same 'leds' array need to exist in both the 'pca955x' runtime structure and the 'pca955x_platform_data' configuration structure? Could we look at eliminating this redundancy to avoid storing duplicate data?

Quoted 13 lines
Quoted 13 lines

drivers/leds/leds-pca955x.c struct pca955x_led { · line 140

Quoted 13 lines
drivers/leds/leds-pca955x.c +12 -0
+};
+
+struct pca955x {
+     struct mutex lock;
+     const struct pca955x_chipdef    *chipdef;
+     struct i2c_client       *client;
+     unsigned long active_blink;
+     unsigned long active_pins;
+     unsigned long blink_period;
+#ifdef CONFIG_LEDS_PCA955X_GPIO
+     struct gpio_chip gpio;
+#endif

#ifdef statements are to be avoided in C files.

Please find another way to do this (if it's required).

Quoted 55 lines
Quoted 54 lines
Quoted 49 lines
drivers/leds/leds-pca955x.c +4 -15
+     struct pca955x_led leds[];
 };
 
 /* 8 bits per input register */
@@ -542,15 +542,11 @@ pca955x_get_pdata(struct i2c_client *client, const struct pca955x_chipdef *chip)
      if (count > chip->bits)
              return ERR_PTR(-ENODEV);
 
-     pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
+     pdata = devm_kzalloc(&client->dev, struct_size(pdata, leds, chip->bits), GFP_KERNEL);
      if (!pdata)
              return ERR_PTR(-ENOMEM);
 
-     pdata->leds = devm_kcalloc(&client->dev,
-                                chip->bits, sizeof(struct pca955x_led),
-                                GFP_KERNEL);
-     if (!pdata->leds)
-             return ERR_PTR(-ENOMEM);
+     pdata->num_leds = chip->bits;
 
      device_for_each_child_node(&client->dev, child) {
              u32 reg;
@@ -568,8 +564,6 @@ pca955x_get_pdata(struct i2c_client *client, const struct pca955x_chipdef *chip)
              fwnode_property_read_u32(child, "type", &led->type);
      }
 
-     pdata->num_leds = chip->bits;
-
      return pdata;
 }
 
@@ -623,15 +617,10 @@ static int pca955x_probe(struct i2c_client *client)
              return -ENODEV;
      }
 
-     pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL);
+     pca955x = devm_kzalloc(&client->dev, struct_size(pca955x, leds, chip->bits), GFP_KERNEL);
      if (!pca955x)
              return -ENOMEM;
 
-     pca955x->leds = devm_kcalloc(&client->dev, chip->bits,
-                                  sizeof(*pca955x_led), GFP_KERNEL);
-     if (!pca955x->leds)
-             return -ENOMEM;
-
      i2c_set_clientdata(client, pca955x);
 
      mutex_init(&pca955x->lock);
--
Quoted 2 lines

2.53.0

--
Lee Jones [李琼斯]

Signature · 1 line
Lee Jones [李琼斯]
#4 lore raw
Quoted 62 lines · Lee Jones
Quoted 44 lines · Rosen Penev
Quoted 43 lines · Lee Jones
Quoted 40 lines · Rosen Penev

Two fewer allocations as a result.

Required placing some structs before others as flexible array members require a complete definition. Declaration is not enough.

Added __counted_by support for one of the structs for extra runtime analysis.

---
 drivers/leds/leds-pca955x.c | 45 ++++++++++++++-----------------------
 1 file changed, 17 insertions(+), 28 deletions(-)
drivers/leds/leds-pca955x.c +0 -13
index 2007fe6217ec..ee5f02eaa3c9 100644
@@ -112,19 +112,6 @@ static const struct pca955x_chipdef pca955x_chipdefs[] = {
      },
 };
 
-struct pca955x {
-     struct mutex lock;
-     struct pca955x_led *leds;
-     const struct pca955x_chipdef    *chipdef;
-     struct i2c_client       *client;
-     unsigned long active_blink;
-     unsigned long active_pins;
-     unsigned long blink_period;
-#ifdef CONFIG_LEDS_PCA955X_GPIO
-     struct gpio_chip gpio;
-#endif
-};
-
 struct pca955x_led {
      struct pca955x  *pca955x;
      struct led_classdev     led_cdev;
@@ -137,8 +124,21 @@ struct pca955x_led {
 #define led_to_pca955x(l)    container_of(l, struct pca955x_led, led_cdev)
 
Quoted 4 lines
Quoted 4 lines

drivers/leds/leds-pca955x.c struct pca955x_led { · line 128

Quoted 4 lines
drivers/leds/leds-pca955x.c +1 -1
 struct pca955x_platform_data {
-     struct pca955x_led      *leds;
      int                     num_leds;
+     struct pca955x_led      leds[] __counted_by(num_leds);

I'm not sure we should we be embedding the full 'struct pca955x_led' (which contains runtime framework structures like 'struct led_classdev') directly inside the platform data? Besides platform data should ideally be restricted to static configuration properties rather than runtime state.

Quoted 4 lines
Quoted 1 line

Where is the memory allocated to this now?

The kzalloc call was updated to use struct_size to allocate it together with the struct.

Quoted 1 line
Quoted 8 lines
Quoted 2 lines
Quoted 1 line

Why do we need this in both structs?

kzalloc and kcalloc can be combined in both locations.

I believe my original question might have been misunderstood. I was asking about the architectural design choice. Why does the same 'leds' array need to exist in both the 'pca955x' runtime structure and the 'pca955x_platform_data' configuration structure? Could we look at eliminating this redundancy to avoid storing duplicate data?

From what I see, this bit of code handles missing pdata.

Quoted 17 lines
Quoted 14 lines
Quoted 14 lines
Quoted 4 lines
drivers/leds/leds-pca955x.c +2 -0
+};
+

drivers/leds/leds-pca955x.c struct pca955x_led { · line 140

Quoted 10 lines
drivers/leds/leds-pca955x.c +10 -0
+struct pca955x {
+     struct mutex lock;
+     const struct pca955x_chipdef    *chipdef;
+     struct i2c_client       *client;
+     unsigned long active_blink;
+     unsigned long active_pins;
+     unsigned long blink_period;
+#ifdef CONFIG_LEDS_PCA955X_GPIO
+     struct gpio_chip gpio;
+#endif

#ifdef statements are to be avoided in C files.

Quoted 1 line

Please find another way to do this (if it's required).

This is not my code.

Quoted 59 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
drivers/leds/leds-pca955x.c +4 -14
+     struct pca955x_led leds[];
 };
 
 /* 8 bits per input register */
@@ -542,15 +542,11 @@ pca955x_get_pdata(struct i2c_client *client, const struct pca955x_chipdef *chip)
      if (count > chip->bits)
              return ERR_PTR(-ENODEV);
 
-     pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
+     pdata = devm_kzalloc(&client->dev, struct_size(pdata, leds, chip->bits), GFP_KERNEL);
      if (!pdata)
              return ERR_PTR(-ENOMEM);
 
-     pdata->leds = devm_kcalloc(&client->dev,
-                                chip->bits, sizeof(struct pca955x_led),
-                                GFP_KERNEL);
-     if (!pdata->leds)
-             return ERR_PTR(-ENOMEM);
+     pdata->num_leds = chip->bits;
 
      device_for_each_child_node(&client->dev, child) {
              u32 reg;
@@ -568,8 +564,6 @@ pca955x_get_pdata(struct i2c_client *client, const struct pca955x_chipdef *chip)
              fwnode_property_read_u32(child, "type", &led->type);
      }
 
-     pdata->num_leds = chip->bits;
-
      return pdata;
 }
 
@@ -623,15 +617,10 @@ static int pca955x_probe(struct i2c_client *client)
              return -ENODEV;
      }
 
-     pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL);
+     pca955x = devm_kzalloc(&client->dev, struct_size(pca955x, leds, chip->bits), GFP_KERNEL);
      if (!pca955x)
              return -ENOMEM;
 
-     pca955x->leds = devm_kcalloc(&client->dev, chip->bits,
-                                  sizeof(*pca955x_led), GFP_KERNEL);
-     if (!pca955x->leds)
-             return -ENOMEM;
-
      i2c_set_clientdata(client, pca955x);
 
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
drivers/leds/leds-pca955x.c +0 -1
      mutex_init(&pca955x->lock);
--

2.53.0

--
Lee Jones [李琼斯]

--
Lee Jones [李琼斯]