From: Dan Carpenter <dan.carpenter@oracle.com>
To: Segher Boessenkool <segher@kernel.crashing.org>
Cc: Alexey Kardashevskiy <aik@ozlabs.ru>,
Alistair Popple <alistair@popple.id.au>,
Mark Hairgrove <mhairgrove@nvidia.com>,
kernel-janitors@vger.kernel.org,
Paul Mackerras <paulus@samba.org>,
linuxppc-dev@lists.ozlabs.org
Subject: Re: [PATCH] powerpc/powernv/npu: Allocate enough memory in pnv_try_setup_npu_table_group()
Date: Sat, 12 Jan 2019 10:51:05 +0000 [thread overview]
Message-ID: <20190112105105.GP1718@kadam> (raw)
In-Reply-To: <20190112073441.GJ14180@gate.crashing.org>
On Sat, Jan 12, 2019 at 01:34:42AM -0600, Segher Boessenkool wrote:
> On Sat, Jan 12, 2019 at 08:44:26AM +0300, Dan Carpenter wrote:
> > On Sat, Jan 12, 2019 at 11:30:35AM +1100, Balbir Singh wrote:
> > > On Wed, Jan 09, 2019 at 01:23:29PM +0300, Dan Carpenter wrote:
> > > > There is a typo so we accidentally allocate enough memory for a pointer
> > > > when we wanted to allocate enough for a struct.
> > > >
> > > > Fixes: 0bd971676e68 ("powerpc/powernv/npu: Add compound IOMMU groups")
> > > > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > > > ---
> > > > arch/powerpc/platforms/powernv/npu-dma.c | 2 +-
> > > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > > >
> > > > diff --git a/arch/powerpc/platforms/powernv/npu-dma.c b/arch/powerpc/platforms/powernv/npu-dma.c
> > > > index d7f742ed48ba..3f58c7dbd581 100644
> > > > --- a/arch/powerpc/platforms/powernv/npu-dma.c
> > > > +++ b/arch/powerpc/platforms/powernv/npu-dma.c
> > > > @@ -564,7 +564,7 @@ struct iommu_table_group *pnv_try_setup_npu_table_group(struct pnv_ioda_pe *pe)
> > > > }
> > > > } else {
> > > > /* Create a group for 1 GPU and attached NPUs for POWER8 */
> > > > - pe->npucomp = kzalloc(sizeof(pe->npucomp), GFP_KERNEL);
> > > > + pe->npucomp = kzalloc(sizeof(*pe->npucomp), GFP_KERNEL);
> > >
> > > To avoid these in the future, I wonder if instead of sizeof(pe->npucomp), we insist on
> > > sizeof structure
> > >
> > > pe->npucomp = kzalloc(sizeof(struct npucomp), GFP_KERNEL);
> > >
> >
> > The latest kernel fashion is sizeof(*ptr). It can go wrong either way.
> > I don't have strong feelings about it. These sorts of bugs don't last
> > long because they're caught in testing or with static analysis.
>
> And it is easy to see someone forgot the * in "sizeof *ptr", and with
> experience it will just automatically look wrong if it is forgotten; but
> it isn't obvious at all if the wrong struct is used, which cannot happen
> with the *ptr form, but happens frequently with the "sizeof(struct x)"
> form.
It doesn't happen very frequently. I look for this with Smatch. The
results I see are when the first few members of a struct are a header
and the actual size can vary.
hdr = alloc(sizeof(struct larger_struct));
regards,
dan carpenter
WARNING: multiple messages have this Message-ID (diff)
From: Dan Carpenter <dan.carpenter@oracle.com>
To: Segher Boessenkool <segher@kernel.crashing.org>
Cc: Alexey Kardashevskiy <aik@ozlabs.ru>,
Alistair Popple <alistair@popple.id.au>,
Mark Hairgrove <mhairgrove@nvidia.com>,
kernel-janitors@vger.kernel.org,
Paul Mackerras <paulus@samba.org>,
linuxppc-dev@lists.ozlabs.org
Subject: Re: [PATCH] powerpc/powernv/npu: Allocate enough memory in pnv_try_setup_npu_table_group()
Date: Sat, 12 Jan 2019 13:51:05 +0300 [thread overview]
Message-ID: <20190112105105.GP1718@kadam> (raw)
In-Reply-To: <20190112073441.GJ14180@gate.crashing.org>
On Sat, Jan 12, 2019 at 01:34:42AM -0600, Segher Boessenkool wrote:
> On Sat, Jan 12, 2019 at 08:44:26AM +0300, Dan Carpenter wrote:
> > On Sat, Jan 12, 2019 at 11:30:35AM +1100, Balbir Singh wrote:
> > > On Wed, Jan 09, 2019 at 01:23:29PM +0300, Dan Carpenter wrote:
> > > > There is a typo so we accidentally allocate enough memory for a pointer
> > > > when we wanted to allocate enough for a struct.
> > > >
> > > > Fixes: 0bd971676e68 ("powerpc/powernv/npu: Add compound IOMMU groups")
> > > > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > > > ---
> > > > arch/powerpc/platforms/powernv/npu-dma.c | 2 +-
> > > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > > >
> > > > diff --git a/arch/powerpc/platforms/powernv/npu-dma.c b/arch/powerpc/platforms/powernv/npu-dma.c
> > > > index d7f742ed48ba..3f58c7dbd581 100644
> > > > --- a/arch/powerpc/platforms/powernv/npu-dma.c
> > > > +++ b/arch/powerpc/platforms/powernv/npu-dma.c
> > > > @@ -564,7 +564,7 @@ struct iommu_table_group *pnv_try_setup_npu_table_group(struct pnv_ioda_pe *pe)
> > > > }
> > > > } else {
> > > > /* Create a group for 1 GPU and attached NPUs for POWER8 */
> > > > - pe->npucomp = kzalloc(sizeof(pe->npucomp), GFP_KERNEL);
> > > > + pe->npucomp = kzalloc(sizeof(*pe->npucomp), GFP_KERNEL);
> > >
> > > To avoid these in the future, I wonder if instead of sizeof(pe->npucomp), we insist on
> > > sizeof structure
> > >
> > > pe->npucomp = kzalloc(sizeof(struct npucomp), GFP_KERNEL);
> > >
> >
> > The latest kernel fashion is sizeof(*ptr). It can go wrong either way.
> > I don't have strong feelings about it. These sorts of bugs don't last
> > long because they're caught in testing or with static analysis.
>
> And it is easy to see someone forgot the * in "sizeof *ptr", and with
> experience it will just automatically look wrong if it is forgotten; but
> it isn't obvious at all if the wrong struct is used, which cannot happen
> with the *ptr form, but happens frequently with the "sizeof(struct x)"
> form.
It doesn't happen very frequently. I look for this with Smatch. The
results I see are when the first few members of a struct are a header
and the actual size can vary.
hdr = alloc(sizeof(struct larger_struct));
regards,
dan carpenter
next prev parent reply other threads:[~2019-01-12 10:51 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-09 10:23 [PATCH] powerpc/powernv/npu: Allocate enough memory in pnv_try_setup_npu_table_group() Dan Carpenter
2019-01-09 10:23 ` Dan Carpenter
2019-01-09 11:54 ` Michael Ellerman
2019-01-09 11:54 ` Michael Ellerman
2019-01-11 2:12 ` Alexey Kardashevskiy
2019-01-11 2:12 ` Alexey Kardashevskiy
2019-01-12 0:30 ` Balbir Singh
2019-01-12 0:30 ` Balbir Singh
2019-01-12 5:44 ` Dan Carpenter
2019-01-12 5:44 ` Dan Carpenter
2019-01-12 7:34 ` Segher Boessenkool
2019-01-12 7:34 ` Segher Boessenkool
2019-01-12 10:51 ` Dan Carpenter [this message]
2019-01-12 10:51 ` Dan Carpenter
2019-01-14 10:12 ` Michael Ellerman
2019-01-14 10:12 ` Michael Ellerman
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=20190112105105.GP1718@kadam \
--to=dan.carpenter@oracle.com \
--cc=aik@ozlabs.ru \
--cc=alistair@popple.id.au \
--cc=kernel-janitors@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=mhairgrove@nvidia.com \
--cc=paulus@samba.org \
--cc=segher@kernel.crashing.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.