mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Rosen Penev <rosenp@gmail.com>,
	dmaengine@vger.kernel.org, Andy Shevchenko <andy@kernel.org>,
	Vinod Koul <vkoul@kernel.org>, Frank Li <Frank.Li@kernel.org>,
	Kees Cook <kees@kernel.org>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	"open list:INTEL MID (Mobile Internet Device) PLATFORM"
	<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: [PATCHv2] dmaengine: hsu: use kzalloc_flex()
Date: Tue, 14 Apr 2026 15:41:09 +0300	[thread overview]
Message-ID: <ad415SF1zIrCof8W@ashevche-desk.local> (raw)
In-Reply-To: <CAHp75Vf_Q4OqYgEOBhoFxpKpAkw5_+GJxQCTbA6LnbR0xhOnMA@mail.gmail.com>

On Wed, Apr 01, 2026 at 04:32:13PM +0300, Andy Shevchenko wrote:
> On Wed, Apr 1, 2026 at 12:31 AM Rosen Penev <rosenp@gmail.com> wrote:
> > On Mon, Mar 30, 2026 at 9:29 PM Andy Shevchenko
> > <andy.shevchenko@gmail.com> wrote:
> > > On Mon, Mar 30, 2026 at 11:41 PM Rosen Penev <rosenp@gmail.com> wrote:
> > > > On Mon, Mar 30, 2026 at 1:46 AM Andy Shevchenko
> > > > <andy.shevchenko@gmail.com> wrote:
> > > > > On Sat, Mar 28, 2026 at 9:17 PM Rosen Penev <rosenp@gmail.com> wrote:

...

> > > > > > -       hsu = devm_kzalloc(chip->dev, sizeof(*hsu), GFP_KERNEL);
> > > > > > +       /* Calculate nr_channels from the IO space length */
> > > > > > +       nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
> > > > > > +       hsu = devm_kzalloc(chip->dev, struct_size(hsu, chan, nr_channels), GFP_KERNEL);
> > > > > >         if (!hsu)
> > > > > >                 return -ENOMEM;
> > > > > >
> > > > > > -       chip->hsu = hsu;
> > > > > > -
> > > > > > -       /* Calculate nr_channels from the IO space length */
> > > > > > -       hsu->nr_channels = (chip->length - chip->offset) / HSU_DMA_CHAN_LENGTH;
> > > > > > +       hsu->nr_channels = nr_channels;
> > > > > >
> > > > > > -       hsu->chan = devm_kcalloc(chip->dev, hsu->nr_channels,
> > > > > > -                                sizeof(*hsu->chan), GFP_KERNEL);
> > > > > > -       if (!hsu->chan)
> > > > > > -               return -ENOMEM;
> > > > > > +       chip->hsu = hsu;
> > > > >
> > > > > Don't know these _flex() APIs enough, but can we leave the chip->hsu =
> > > > > hsu; in the same place as it's now?
> > > > __counted_by requires the first assignment after allocation to be the
> > > > counting variable. The _flex macros do this automatically for GCC15
> > > > and above.
> > >
> > > Why? The hsu member has nothing to do with VLA, where is this
> > > requirement coming from? My understanding is that the check should
> > > imply the minimum sizeof of the data structure and the compiler should
> > > know that way before doing any allocations.
> > Not sure I follow. This patch changes hsu's chan member to a FAM.
> > Where is VLA coming from?
> 
> VLA: variable-length array
> FAM: flexible array member
> The second one is VLA member + size member.
> 
> What your patch is doing is changing a pointer to VLA member.
> 
> > The current code is devm_kzalloc(x, struct_size()). When it gets
> > introduced, I'm sure there will be a treewide conversion to
> > devm_kzalloc_flex, which would automatically set the counting variable
> > for >=GCC15.
> >
> > It's best practice to assign right after since kzalloc_flex does it anyways.
> 
> Still, I'm not convinced we should blindly follow this rule. The
> length needs to be known before accessing the VLA, but it's okay to
> access other members. Leaving hsu member assignment where it's now is
> fine, no need to move it around.
> 
> > > My understanding seems in align with what Gustavo blogged:
> > > https://people.kernel.org/gustavoars/how-to-use-the-new-counted_by-attribute-in-c-and-linux
> > >
> > > The same is written in the GCC patch description
> > > https://gcc.gnu.org/pipermail/gcc-patches/2024-May/653123.html

If you agree with my reasoning, please send a v4, I will give you a tag.

Otherwise I really would like to understand the justification why the
assignment going first is the best practice and how it may help the developer.

-- 
With Best Regards,
Andy Shevchenko



  reply	other threads:[~2026-04-14 12:41 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-28 19:16 Rosen Penev
2026-03-30  8:45 ` Andy Shevchenko
2026-03-30 20:41   ` Rosen Penev
2026-03-31  4:28     ` Andy Shevchenko
2026-03-31 21:30       ` Rosen Penev
2026-04-01 13:32         ` Andy Shevchenko
2026-04-14 12:41           ` Andy Shevchenko [this message]
2026-04-14 20:46             ` Rosen Penev
2026-04-14 23:55               ` Andy Shevchenko

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=ad415SF1zIrCof8W@ashevche-desk.local \
    --to=andriy.shevchenko@intel.com \
    --cc=Frank.Li@kernel.org \
    --cc=andy.shevchenko@gmail.com \
    --cc=andy@kernel.org \
    --cc=dmaengine@vger.kernel.org \
    --cc=gustavoars@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rosenp@gmail.com \
    --cc=vkoul@kernel.org \
    /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

all inboxes | Powered by JetHome®