Skip to contentSkip to conversation

ARM: omap2: simplify allocation for omap_device

PATCH Applied Rosen Penev 2 participants 3 messages Mar 30 – May 11

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

PATCH v3 1/1 ARM: omap2: simplify allocation for omap_device

Use a flexible array member (FAM) to combine hwmods array allocation with the omap_device structure. This reduces the number of allocations from two separate calls (one for the device, one for the array) to a single allocation, improving efficiency and reducing memory fragmentation.

The FAM approach also enables bounds checking through __counted_by(), which provides runtime verification that array accesses stay within the allocated size. This improves security and helps catch bugs during development.

Simplify error handling by removing the unnecessary multi-label goto pattern. The new code is more straightforward: allocate, verify, copy data, and either return success or error immediately.

Also removes the now-redundant kfree(od->hwmods) in omap_device_delete() since the hwmods array is now embedded in the structure rather than separately allocated.

---
v3: add a verbose description explaining why. Powered by Claude Sonnet 4.5.
v2: remove kfree and fix compilation.

 arch/arm/mach-omap2/omap_device.c | 29 ++++++++++-------------------
 arch/arm/mach-omap2/omap_device.h |  4 ++--
 2 files changed, 12 insertions(+), 21 deletions(-)
arch/arm/mach-omap2/omap_device.c +10 -19
index 79db4c49ffc9..77a75b0b9ae6 100644
@@ -307,35 +307,27 @@ static struct omap_device *omap_device_alloc(struct platform_device *pdev,
 	int ret = -ENOMEM;
 	struct omap_device *od;
 	int i;
-	struct omap_hwmod **hwmods;
+	struct omap_hwmod *hwmod;
 
-	od = kzalloc_obj(struct omap_device);
-	if (!od)
-		goto oda_exit1;
+	od = kzalloc_flex(*od, hwmods, oh_cnt);
+	if (!od) {
+		dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
+		return ERR_PTR(ret);
+	}
 
 	od->hwmods_cnt = oh_cnt;
+	memcpy(od->hwmods, ohs, oh_cnt * sizeof(*od->hwmods));
 
-	hwmods = kmemdup_array(ohs, oh_cnt, sizeof(*hwmods), GFP_KERNEL);
-	if (!hwmods)
-		goto oda_exit2;
-
-	od->hwmods = hwmods;
 	od->pdev = pdev;
 	pdev->archdata.od = od;
 
 	for (i = 0; i < oh_cnt; i++) {
-		hwmods[i]->od = od;
-		_add_hwmod_clocks_clkdev(od, hwmods[i]);
+		hwmod = od->hwmods[i];
+		hwmod->od = od;
+		_add_hwmod_clocks_clkdev(od, hwmod);
 	}
 
 	return od;
-
-oda_exit2:
-	kfree(od);
-oda_exit1:
-	dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
-
-	return ERR_PTR(ret);
 }
 
 static void omap_device_delete(struct omap_device *od)
@@ -344,7 +336,6 @@ static void omap_device_delete(struct omap_device *od)
 		return;
 
 	od->pdev->archdata.od = NULL;
-	kfree(od->hwmods);
 	kfree(od);
 }
 
arch/arm/mach-omap2/omap_device.h +2 -2
index aa8096ecb23c..fae09cfce137 100644
@@ -37,11 +37,11 @@
 /**
  * struct omap_device - omap_device wrapper for platform_devices
  * @pdev: platform_device
- * @hwmods: (one .. many per omap_device)
  * @hwmods_cnt: ARRAY_SIZE() of @hwmods
  * @_state: one of OMAP_DEVICE_STATE_* (see above)
  * @flags: device flags
  * @_driver_status: one of BUS_NOTIFY_*_DRIVER from <linux/device.h>
+ * @hwmods: (one .. many per omap_device)
  *
  * Integrates omap_hwmod data into Linux platform_device.
  *
@@ -51,11 +51,11 @@
  */
 struct omap_device {
 	struct platform_device		*pdev;
-	struct omap_hwmod		**hwmods;
 	unsigned long			_driver_status;
 	u8				hwmods_cnt;
 	u8				_state;
 	u8                              flags;
+	struct omap_hwmod		*hwmods[] __counted_by(hwmods_cnt);
 };
 
 /* Device driver interface (call via platform_data fn ptrs) */

--
2.53.0

maintainer
#1 lore raw
Quoted 14 lines · Rosen Penev

Use a flexible array member (FAM) to combine hwmods array allocation with the omap_device structure. This reduces the number of allocations from two separate calls (one for the device, one for the array) to a single allocation, improving efficiency and reducing memory fragmentation.

The FAM approach also enables bounds checking through __counted_by(), which provides runtime verification that array accesses stay within the allocated size. This improves security and helps catch bugs during development.

Simplify error handling by removing the unnecessary multi-label goto pattern. The new code is more straightforward: allocate, verify, copy data, and either return success or error immediately.

Quoted 5 lines

Also removes the now-redundant kfree(od->hwmods) in omap_device_delete() since the hwmods array is now embedded in the structure rather than separately allocated.

Thanks for this cleanup.

Quoted 19 lines

---
v3: add a verbose description explaining why. Powered by Claude Sonnet 4.5.
v2: remove kfree and fix compilation.

 arch/arm/mach-omap2/omap_device.c | 29 ++++++++++-------------------
 arch/arm/mach-omap2/omap_device.h |  4 ++--
 2 files changed, 12 insertions(+), 21 deletions(-)
arch/arm/mach-omap2/omap_device.c +1 -1
index 79db4c49ffc9..77a75b0b9ae6 100644
@@ -307,35 +307,27 @@ static struct omap_device *omap_device_alloc(struct platform_device *pdev,
 	int ret = -ENOMEM;
 	struct omap_device *od;
 	int i;
-	struct omap_hwmod **hwmods;
+	struct omap_hwmod *hwmod;
 

arch/arm/mach-omap2/omap_device.c static struct omap_device *omap_device_alloc(struct platform_device *pdev, · line 318

Quoted 10 lines
arch/arm/mach-omap2/omap_device.c +5 -3
-	od = kzalloc_obj(struct omap_device);
-	if (!od)
-		goto oda_exit1;
+	od = kzalloc_flex(*od, hwmods, oh_cnt);
+	if (!od) {
+		dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
+		return ERR_PTR(ret);
+	}
 
 	od->hwmods_cnt = oh_cnt;

minor nit: isn't this assignment redundant now, as kalloc_flex() should assign the count?

Quoted 18 lines
arch/arm/mach-omap2/omap_device.c +4 -7
+	memcpy(od->hwmods, ohs, oh_cnt * sizeof(*od->hwmods));
 
 
-	hwmods = kmemdup_array(ohs, oh_cnt, sizeof(*hwmods), GFP_KERNEL);
-	if (!hwmods)
-		goto oda_exit2;
-
-	od->hwmods = hwmods;
 	od->pdev = pdev;
 	pdev->archdata.od = od;
 
 	for (i = 0; i < oh_cnt; i++) {
-		hwmods[i]->od = od;
-		_add_hwmod_clocks_clkdev(od, hwmods[i]);
+		hwmod = od->hwmods[i];
+		hwmod->od = od;
+		_add_hwmod_clocks_clkdev(od, hwmod);
 	}

arch/arm/mach-omap2/omap_device.c static struct omap_device *omap_device_alloc(struct platform_device *pdev, · line 331

Quoted 10 lines
arch/arm/mach-omap2/omap_device.c +0 -7
 
 	return od;
-
-oda_exit2:
-	kfree(od);
-oda_exit1:
-	dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
-
-	return ERR_PTR(ret);
 }

Kevin

maintainer Applied
#2 lore raw
In reply to Rosen Penev
Quoted 11 lines · Rosen Penev

Use a flexible array member (FAM) to combine hwmods array allocation with the omap_device structure. This reduces the number of allocations from two separate calls (one for the device, one for the array) to a single allocation, improving efficiency and reducing memory fragmentation.

The FAM approach also enables bounds checking through __counted_by(), which provides runtime verification that array accesses stay within the allocated size. This improves security and helps catch bugs during development.

[...]

Applied, thanks!

[1/1] ARM: omap2: simplify allocation for omap_device

      commit: 2a7a9e20b9e8b5dc4fdef777e67d390dcb3a8ea4

Best regards,

Signature · 1 line
Kevin Hilman (TI) <[email protected]>