Skip to contentSkip to conversation

arch/powerpc: Use dma_zalloc_coherent

PATCH Applied Sabyasachi Gupta 5 participants 7 messages Nov 5, 2018 – Nov 27, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arch/powerpc: Use dma_zalloc_coherent

Replaced dma_alloc_coherent + memset with dma_zalloc_coherent

---
 arch/powerpc/platforms/pasemi/dma_lib.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
arch/powerpc/platforms/pasemi/dma_lib.c +1 -3
index 53384eb..d18d164 100644
@@ -255,15 +255,13 @@ int pasemi_dma_alloc_ring(struct pasemi_dmachan *chan, int ring_size)
 
 	chan->ring_size = ring_size;
 
-	chan->ring_virt = dma_alloc_coherent(&dma_pdev->dev,
+	chan->ring_virt = dma_zalloc_coherent(&dma_pdev->dev,
 					     ring_size * sizeof(u64),
 					     &chan->ring_dma, GFP_KERNEL);
 
 	if (!chan->ring_virt)
 		return -ENOMEM;
 
-	memset(chan->ring_virt, 0, ring_size * sizeof(u64));
-
 	return 0;
 }
 EXPORT_SYMBOL(pasemi_dma_alloc_ring);
Signature · 1 line
2.7.4
#1 lore raw
Quoted 4 lines · Sabyasachi Gupta

Replaced dma_alloc_coherent + memset with dma_zalloc_coherent

Any comment on this patch?

Quoted 28 lines
---
 arch/powerpc/platforms/pasemi/dma_lib.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
arch/powerpc/platforms/pasemi/dma_lib.c +1 -4
index 53384eb..d18d164 100644
@@ -255,15 +255,13 @@ int pasemi_dma_alloc_ring(struct pasemi_dmachan *chan, int ring_size)
 
        chan->ring_size = ring_size;
 
-       chan->ring_virt = dma_alloc_coherent(&dma_pdev->dev,
+       chan->ring_virt = dma_zalloc_coherent(&dma_pdev->dev,
                                             ring_size * sizeof(u64),
                                             &chan->ring_dma, GFP_KERNEL);
 
        if (!chan->ring_virt)
                return -ENOMEM;
 
-       memset(chan->ring_virt, 0, ring_size * sizeof(u64));
-
        return 0;
 }
 EXPORT_SYMBOL(pasemi_dma_alloc_ring);
--

2.7.4

#2 lore raw
Quoted 7 lines · Sabyasachi Gupta
Quoted 3 lines · Sabyasachi Gupta

Replaced dma_alloc_coherent + memset with dma_zalloc_coherent

Any comment on this patch?

It's obviously correct.

You might realign the arguments on the next lines to the open parenthesis.

Perhaps there should be new function calls added for symmetry to the other alloc functions for multiplication overflow protection.

Perhaps:

void *dma_alloc_array_coherent()
void *dma_calloc_coherent()

Something like

---
 include/linux/dma-mapping.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
include/linux/dma-mapping.h +19 -0
index 15bd41447025..95bebf8883b1 100644
@@ -565,6 +565,25 @@ static inline void *dma_alloc_coherent(struct device *dev, size_t size,
 			(gfp & __GFP_NOWARN) ? DMA_ATTR_NO_WARN : 0);
 }
 
+static inline void *dma_alloc_array_coherent(struct device *dev,
+					     size_t n, size_t size,
+					     dma_addr_t *dma_handle, gfp_t gfp)
+{
+	size_t bytes;
+
+	if (unlikely(check_mul_overflow(n, size, &bytes)))
+		return NULL;
+	return dma_alloc_coherent(dev, bytes, dma_handle, gfp);
+}
+
+static inline void *dma_calloc_coherent(struct device *dev,
+					size_t n, size_t size,
+					dma_addr_t *dma_handle, gfp_t gfp)
+{
+	return dma_alloc_array_coherent(dev, n, size, dma_handle,
+					gfp | __GFP_ZERO);
+}
+
 static inline void dma_free_coherent(struct device *dev, size_t size,
 		void *cpu_addr, dma_addr_t dma_handle)
 {

---

Quoted 1 line

arch/powerpc/platforms/pasemi/dma_lib.c

Quoted 1 line
arch/powerpc/platforms/pasemi/dma_lib.c +0 -0

[]

Quoted 17 lines
Quoted 17 lines
arch/powerpc/platforms/pasemi/dma_lib.c +1 -3
@@ -255,15 +255,13 @@ int pasemi_dma_alloc_ring(struct pasemi_dmachan *chan, int ring_size)
 
        chan->ring_size = ring_size;
 
-       chan->ring_virt = dma_alloc_coherent(&dma_pdev->dev,
+       chan->ring_virt = dma_zalloc_coherent(&dma_pdev->dev,
                                             ring_size * sizeof(u64),
                                             &chan->ring_dma, GFP_KERNEL);
 en
        if (!chan->ring_virt)
                return -ENOMEM;
 
-       memset(chan->ring_virt, 0, ring_size * sizeof(u64));
-
        return 0;
 }
 EXPORT_SYMBOL(pasemi_dma_alloc_ring);
#3 lore raw

Hi Joe,

Quoted 46 lines · Joe Perches
Quoted 7 lines · Sabyasachi Gupta
Quoted 3 lines · Sabyasachi Gupta

Replaced dma_alloc_coherent + memset with dma_zalloc_coherent

Any comment on this patch?

It's obviously correct.

You might realign the arguments on the next lines to the open parenthesis.

Perhaps there should be new function calls added for symmetry to the other alloc functions for multiplication overflow protection.

Perhaps:

void *dma_alloc_array_coherent()
void *dma_calloc_coherent()

Something like

---
 include/linux/dma-mapping.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
include/linux/dma-mapping.h +9 -0
index 15bd41447025..95bebf8883b1 100644
@@ -565,6 +565,25 @@ static inline void *dma_alloc_coherent(struct device *dev, size_t size,
                        (gfp & __GFP_NOWARN) ? DMA_ATTR_NO_WARN : 0);
 }
 
+static inline void *dma_alloc_array_coherent(struct device *dev,
+                                            size_t n, size_t size,
+                                            dma_addr_t *dma_handle, gfp_t gfp)
+{
+       size_t bytes;
+
+       if (unlikely(check_mul_overflow(n, size, &bytes)))
+               return NULL;
+       return dma_alloc_coherent(dev, bytes, dma_handle, gfp);

include/linux/dma-mapping.h static inline void *dma_alloc_coherent(struct device *dev, size_t size, · line 586

Quoted 10 lines
include/linux/dma-mapping.h +10 -0
+}
+
+static inline void *dma_calloc_coherent(struct device *dev,
+                                       size_t n, size_t size,
+                                       dma_addr_t *dma_handle, gfp_t gfp)
+{
+       return dma_alloc_array_coherent(dev, n, size, dma_handle,
+                                       gfp | __GFP_ZERO);
+}
+

If I understood correctly, you are talking about adding these 2 new inline functions. We can do that.

Can you please help to understand the consumers of these 2 new inline ?

Quoted 26 lines
include/linux/dma-mapping.h +0 -1
 static inline void dma_free_coherent(struct device *dev, size_t size,
                void *cpu_addr, dma_addr_t dma_handle)
 {
 
---
Quoted 1 line

arch/powerpc/platforms/pasemi/dma_lib.c

Quoted 1 line
arch/powerpc/platforms/pasemi/dma_lib.c +0 -0

[]

Quoted 17 lines
Quoted 17 lines
arch/powerpc/platforms/pasemi/dma_lib.c +1 -3
@@ -255,15 +255,13 @@ int pasemi_dma_alloc_ring(struct pasemi_dmachan *chan, int ring_size)
 
        chan->ring_size = ring_size;
 
-       chan->ring_virt = dma_alloc_coherent(&dma_pdev->dev,
+       chan->ring_virt = dma_zalloc_coherent(&dma_pdev->dev,
                                             ring_size * sizeof(u64),
                                             &chan->ring_dma, GFP_KERNEL);
 en
        if (!chan->ring_virt)
                return -ENOMEM;
 
-       memset(chan->ring_virt, 0, ring_size * sizeof(u64));
-
        return 0;
 }
 EXPORT_SYMBOL(pasemi_dma_alloc_ring);
#4 lore raw
Quoted 1 line · Souptick Joarder

Hi Joe,

Hi back.

Quoted 60 lines · Joe Perches
Quoted 45 lines · Sabyasachi Gupta
Quoted 7 lines
Quoted 3 lines · Sabyasachi Gupta

Replaced dma_alloc_coherent + memset with dma_zalloc_coherent

Any comment on this patch?

It's obviously correct.

You might realign the arguments on the next lines to the open parenthesis.

Perhaps there should be new function calls added for symmetry to the other alloc functions for multiplication overflow protection.

Perhaps:

void *dma_alloc_array_coherent()
void *dma_calloc_coherent()

Something like

---
 include/linux/dma-mapping.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
include/linux/dma-mapping.h +9 -0
index 15bd41447025..95bebf8883b1 100644
@@ -565,6 +565,25 @@ static inline void *dma_alloc_coherent(struct device *dev, size_t size,
                        (gfp & __GFP_NOWARN) ? DMA_ATTR_NO_WARN : 0);
 }
 
+static inline void *dma_alloc_array_coherent(struct device *dev,
+                                            size_t n, size_t size,
+                                            dma_addr_t *dma_handle, gfp_t gfp)
+{
+       size_t bytes;
+
+       if (unlikely(check_mul_overflow(n, size, &bytes)))
+               return NULL;
+       return dma_alloc_coherent(dev, bytes, dma_handle, gfp);

include/linux/dma-mapping.h static inline void *dma_alloc_coherent(struct device *dev, size_t size, · line 586

Quoted 10 lines
include/linux/dma-mapping.h +10 -0
+}
+
+static inline void *dma_calloc_coherent(struct device *dev,
+                                       size_t n, size_t size,
+                                       dma_addr_t *dma_handle, gfp_t gfp)
+{
+       return dma_alloc_array_coherent(dev, n, size, dma_handle,
+                                       gfp | __GFP_ZERO);
+}
+

If I understood correctly, you are talking about adding these 2 new inline functions. We can do that.

Quoted 1 line

Can you please help to understand the consumers of these 2 new inline ?

Any call to dma_alloc_coherent with a multiply
_might_overflow the multiplication. The
check_mul_overflow simply avoids the overflow.

#5 lore raw
In reply to Sabyasachi Gupta
Quoted 8 lines · Sabyasachi Gupta
Quoted 4 lines · Sabyasachi Gupta

Replaced dma_alloc_coherent + memset with dma_zalloc_coherent

Any comment on this patch?

Wait longer :)

I'm still chasing bugs in 4.20-rc2, I haven't started merging many patches for 4.21 yet.

Your patches are tracked in patchwork here:
https://patchwork.ozlabs.org/project/linuxppc-dev/list/?submitter=75253

If they're still in "new" state around rc5 then feel free to ping me again.

cheers

Quoted 24 lines
Quoted 24 lines
arch/powerpc/platforms/pasemi/dma_lib.c +1 -4
index 53384eb..d18d164 100644
@@ -255,15 +255,13 @@ int pasemi_dma_alloc_ring(struct pasemi_dmachan *chan, int ring_size)
 
        chan->ring_size = ring_size;
 
-       chan->ring_virt = dma_alloc_coherent(&dma_pdev->dev,
+       chan->ring_virt = dma_zalloc_coherent(&dma_pdev->dev,
                                             ring_size * sizeof(u64),
                                             &chan->ring_dma, GFP_KERNEL);
 
        if (!chan->ring_virt)
                return -ENOMEM;
 
-       memset(chan->ring_virt, 0, ring_size * sizeof(u64));
-
        return 0;
 }
 EXPORT_SYMBOL(pasemi_dma_alloc_ring);
--

2.7.4