Skip to contentSkip to conversation

dma-buf: use struct_size macro

PATCH In review Christian König 3 participants 4 messages Oct 8, 2020 DMA buffer sharing framework

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dma-buf: use struct_size macro

Instead of manually calculating the structure size.

---
 drivers/dma-buf/dma-resv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma-buf/dma-resv.c +1 -1
index 1c8f2581cb09..bb5a42b10c29 100644
@@ -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;
 
Signature · 6 lines
2.17.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Christian König

Instead of manually calculating the structure size.

Quoted 24 lines
---
 drivers/dma-buf/dma-resv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma-buf/dma-resv.c +1 -2
index 1c8f2581cb09..bb5a42b10c29 100644
@@ -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
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw

Am 08.10.20 um 10:17 schrieb Daniel Vetter:

Quoted 6 lines
Quoted 3 lines · Christian König

Instead of manually calculating the structure size.

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.

Quoted 18 lines
Quoted 18 lines
---
  drivers/dma-buf/dma-resv.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma-buf/dma-resv.c +1 -1
index 1c8f2581cb09..bb5a42b10c29 100644
@@ -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;
 
Quoted 9 lines
Quoted 7 lines

--
2.17.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw
Quoted 7 lines · Christian König

Am 08.10.20 um 10:17 schrieb Daniel Vetter:

Quoted 6 lines
Quoted 3 lines · Christian König

Instead of manually calculating the structure size.

Quoted 5 lines

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]:

"

Review

Quoted 4 lines
(quoted diff) +4 -0
+       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

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines

---
  drivers/dma-buf/dma-resv.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dma-buf/dma-resv.c +1 -2
index 1c8f2581cb09..bb5a42b10c29 100644
@@ -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

Quoted 7 lines
Quoted 6 lines
Quoted 4 lines

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel