Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Hilman <khilman@baylibre.com>
To: Rosen Penev <rosenp@gmail.com>, linux-omap@vger.kernel.org
Cc: Aaro Koskinen <aaro.koskinen@iki.fi>,
	Andreas Kemnade <andreas@kemnade.info>,
	Roger Quadros <rogerq@kernel.org>,
	Tony Lindgren <tony@atomide.com>,
	Russell King <linux@armlinux.org.uk>, Kees Cook <kees@kernel.org>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	"moderated list:ARM SUB-ARCHITECTURES"
	<linux-arm-kernel@lists.infradead.org>,
	open list <linux-kernel@vger.kernel.org>,
	"open list:KERNEL HARDENING (not covered by other
	areas):Keyword:b__counted_by(_le|_be)?b"
	<linux-hardening@vger.kernel.org>
Subject: Re: [PATCHv3] ARM: omap2: simplify allocation for omap_device
Date: Fri, 08 May 2026 12:12:08 -0700	[thread overview]
Message-ID: <7hv7cx90mv.fsf@baylibre.com> (raw)
In-Reply-To: <20260330213528.18187-1-rosenp@gmail.com>

Rosen Penev <rosenp@gmail.com> writes:

> 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.
>
> Signed-off-by: Rosen Penev <rosenp@gmail.com>

Thanks for this cleanup.

> ---
>  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(-)
>
> diff --git a/arch/arm/mach-omap2/omap_device.c b/arch/arm/mach-omap2/omap_device.c
> index 79db4c49ffc9..77a75b0b9ae6 100644
> --- a/arch/arm/mach-omap2/omap_device.c
> +++ b/arch/arm/mach-omap2/omap_device.c
> @@ -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;

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

> +	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);
>  }

Kevin


      reply	other threads:[~2026-05-08 19:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-30 21:35 [PATCHv3] ARM: omap2: simplify allocation for omap_device Rosen Penev
2026-05-08 19:12 ` Kevin Hilman [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7hv7cx90mv.fsf@baylibre.com \
    --to=khilman@baylibre.com \
    --cc=aaro.koskinen@iki.fi \
    --cc=andreas@kemnade.info \
    --cc=gustavoars@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-omap@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=rogerq@kernel.org \
    --cc=rosenp@gmail.com \
    --cc=tony@atomide.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox