Re: [GIT PULL] percpu fix for v5.9-rc6
From: Gustavo A. R. Silva
Date: Fri Sep 18 2020 - 12:17:33 EST
On Thu, Sep 17, 2020 at 06:05:13PM -0700, Linus Torvalds wrote:
> On Thu, Sep 17, 2020 at 1:45 PM Dennis Zhou <dennis@xxxxxxxxxx> wrote:
> >
> >
> > diff --git a/mm/percpu.c b/mm/percpu.c
> > index f4709629e6de..1ed1a349eab8 100644
> > --- a/mm/percpu.c
> > +++ b/mm/percpu.c
> > @@ -1316,7 +1316,7 @@ static struct pcpu_chunk * __init pcpu_alloc_first_chunk(unsigned long tmp_addr,
> >
> > /* allocate chunk */
> > alloc_size = sizeof(struct pcpu_chunk) +
> > - BITS_TO_LONGS(region_size >> PAGE_SHIFT);
> > + BITS_TO_LONGS(region_size >> PAGE_SHIFT) * sizeof(unsigned long);
>
> Hmm.
>
> Wouldn't this be cleaner as
>
> alloc_size =struct_size(chunk, populated,
> BITS_TO_LONGS(region_size >> PAGE_SHIFT) );
Yeah; the above is much better. Please, use that helper.
> and looking at this, I realize that I thought we enabled warnings for
> 'sizeof()' of flexible array structures to avoid these kinds of
> mistakes, but that must clearly have happened only in a dream of mine.
If you were to try to apply the sizeof() operator to the flexible-array member
alone: sizeof(chunk->populated); you would get a warning because such arrays
have incomplete type, see below:
mm/percpu.c: In function ‘pcpu_alloc_first_chunk’:
mm/percpu.c:1320:52: error: invalid application of ‘sizeof’ to incomplete type ‘long unsigned int[]’
1320 | BITS_TO_LONGS(region_size >> PAGE_SHIFT) * sizeof(chunk->populated);
| ^
However, in this case, sizeof() is being applied to the object type, which doesn't
cause a warning, but still is an error-prone coding practice. For instance, this
is the bugfix[1], for a 4-year old bug introduced by the combination of weak code
and this commit[2]. This bug could have been prevented by either adopting better
coding practices or through the use[3] of the recent struct_size() helper.
So please, whenever you can use it, do so. :)
Thanks
--
Gustavo
[1] https://git.kernel.org/linus/cffaaf0c816238c45cd2d06913476c83eb50f682
[2] https://git.kernel.org/linus/57384592c43375d2c9a14d82aebbdc95fdda9e9d
[3] https://git.kernel.org/linus/553d66cb1e8667aadb57e3804775c5ce1724a49b