Message ID | 20201008081013.27384-1-christian.koenig@amd.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | dma-buf: use struct_size macro | expand |
On Thu, Oct 8, 2020 at 10:10 AM Christian König <ckoenig.leichtzumerken@gmail.com> wrote: > > Instead of manually calculating the structure size. > > Signed-off-by: Christian König <christian.koenig@amd.com> Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch> > --- > drivers/dma-buf/dma-resv.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c > index 1c8f2581cb09..bb5a42b10c29 100644 > --- a/drivers/dma-buf/dma-resv.c > +++ b/drivers/dma-buf/dma-resv.c > @@ -63,7 +63,7 @@ static struct dma_resv_list *dma_resv_list_alloc(unsigned int shared_max) > { > struct dma_resv_list *list; > > - list = kmalloc(offsetof(typeof(*list), shared[shared_max]), GFP_KERNEL); > + list = kmalloc(struct_size(list, shared, shared_max), GFP_KERNEL); > if (!list) > return NULL; > > -- > 2.17.1 > > _______________________________________________ > dri-devel mailing list > dri-devel@lists.freedesktop.org > https://lists.freedesktop.org/mailman/listinfo/dri-devel
Am 08.10.20 um 10:17 schrieb Daniel Vetter: > On Thu, Oct 8, 2020 at 10:10 AM Christian König > <ckoenig.leichtzumerken@gmail.com> wrote: >> Instead of manually calculating the structure size. >> >> Signed-off-by: Christian König <christian.koenig@amd.com> > Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch> Thanks, could anybody (not me) come up with a cocci script for this? I'm pretty sure we have used this pattern more than once in the kernel. Thanks, Christian. > >> --- >> drivers/dma-buf/dma-resv.c | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c >> index 1c8f2581cb09..bb5a42b10c29 100644 >> --- a/drivers/dma-buf/dma-resv.c >> +++ b/drivers/dma-buf/dma-resv.c >> @@ -63,7 +63,7 @@ static struct dma_resv_list *dma_resv_list_alloc(unsigned int shared_max) >> { >> struct dma_resv_list *list; >> >> - list = kmalloc(offsetof(typeof(*list), shared[shared_max]), GFP_KERNEL); >> + list = kmalloc(struct_size(list, shared, shared_max), GFP_KERNEL); >> if (!list) >> return NULL; >> >> -- >> 2.17.1 >> >> _______________________________________________ >> dri-devel mailing list >> dri-devel@lists.freedesktop.org >> https://lists.freedesktop.org/mailman/listinfo/dri-devel > >
On Thu, Oct 08, 2020 at 10:40:10AM +0200, Christian König wrote: > Am 08.10.20 um 10:17 schrieb Daniel Vetter: > > On Thu, Oct 8, 2020 at 10:10 AM Christian König > > <ckoenig.leichtzumerken@gmail.com> wrote: > > > Instead of manually calculating the structure size. > > > > > > Signed-off-by: Christian König <christian.koenig@amd.com> > > Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch> Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org> > > Thanks, could anybody (not me) come up with a cocci script for this? > > I'm pretty sure we have used this pattern more than once in the kernel. > Sure thing. I'll take care of that. I saw this same pattern, recently[1]: " > + size = offsetof(struct pmt_crashlog_priv, entry[pdev->num_resources]); > + priv = devm_kzalloc(&pdev->dev, size, GFP_KERNEL); > + if (!priv) > + return -ENOMEM; struct_size() " [1] https://lore.kernel.org/lkml/CAHp75VcP58Ub=gmbRVy0TPJtntKvnQZoi3tOakxE0qsEqzGPVA@mail.gmail.com/ Thanks -- Gustavo > > > > > > --- > > > drivers/dma-buf/dma-resv.c | 2 +- > > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > > > diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c > > > index 1c8f2581cb09..bb5a42b10c29 100644 > > > --- a/drivers/dma-buf/dma-resv.c > > > +++ b/drivers/dma-buf/dma-resv.c > > > @@ -63,7 +63,7 @@ static struct dma_resv_list *dma_resv_list_alloc(unsigned int shared_max) > > > { > > > struct dma_resv_list *list; > > > > > > - list = kmalloc(offsetof(typeof(*list), shared[shared_max]), GFP_KERNEL); > > > + list = kmalloc(struct_size(list, shared, shared_max), GFP_KERNEL); > > > if (!list) > > > return NULL; > > > > > > -- > > > 2.17.1 > > > > > > _______________________________________________ > > > dri-devel mailing list > > > dri-devel@lists.freedesktop.org > > > https://lists.freedesktop.org/mailman/listinfo/dri-devel > > > > >
diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c index 1c8f2581cb09..bb5a42b10c29 100644 --- a/drivers/dma-buf/dma-resv.c +++ b/drivers/dma-buf/dma-resv.c @@ -63,7 +63,7 @@ static struct dma_resv_list *dma_resv_list_alloc(unsigned int shared_max) { struct dma_resv_list *list; - list = kmalloc(offsetof(typeof(*list), shared[shared_max]), GFP_KERNEL); + list = kmalloc(struct_size(list, shared, shared_max), GFP_KERNEL); if (!list) return NULL;
Instead of manually calculating the structure size. Signed-off-by: Christian König <christian.koenig@amd.com> --- drivers/dma-buf/dma-resv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)