Skip to contentSkip to conversation

lib/dma-debug.c: fix memory leakage

PATCH Applied [email protected] 3 participants 3 messages Feb 22, 2018

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

PATCH 1/1 lib/dma-debug.c: fix memory leakage

From: Miles Chen <miles.chen-NuS5LvNUpcJWk0Htik3J/[email protected]>

Marty reported a memory leakage introduced by commit 3aaabbf1c39e ("lib/dma-debug.c: fix incorrect pfn calculation"). Fix it by checking the virtual address before allocating the entry.

This patch also use virt_addr_valid() instead of virt_to_page() to check if a virtual address is linear.

---
 lib/dma-debug.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
lib/dma-debug.c +5 -5
index 1b34d210452c..7f5cdc1e6b29 100644
@@ -1491,12 +1491,12 @@ void debug_dma_alloc_coherent(struct device *dev, size_t size,
 	if (unlikely(virt == NULL))
 		return;
 
-	entry = dma_entry_alloc();
-	if (!entry)
+	/* handle vmalloc and linear addresses */
+	if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
 		return;
 
-	/* handle vmalloc and linear addresses */
-	if (!is_vmalloc_addr(virt) && !virt_to_page(virt))
+	entry = dma_entry_alloc();
+	if (!entry)
 		return;
 
 	entry->type      = dma_debug_coherent;
@@ -1528,7 +1528,7 @@ void debug_dma_free_coherent(struct device *dev, size_t size,
 	};
 
 	/* handle vmalloc and linear addresses */
-	if (!is_vmalloc_addr(virt) && !virt_to_page(virt))
+	if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
 		return;
 
 	if (is_vmalloc_addr(virt))
Signature · 1 line
2.12.5
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · On 22/02/18 11:22, miles.chen-NuS5LvNUpcJWk0Htik3J/[email protected] wrote:

From: Miles Chen <miles.chen-NuS5LvNUpcJWk0Htik3J/[email protected]>

Marty reported a memory leakage introduced by commit 3aaabbf1c39e ("lib/dma-debug.c: fix incorrect pfn calculation"). Fix it by checking the virtual address before allocating the entry.

Oops, seems I failed to look closely enough at the surrounding diff context when I reviewed the original patch... :(

Quoted 2 lines

This patch also use virt_addr_valid() instead of virt_to_page() to check if a virtual address is linear.

That's an area I happen to have been looking at lately and now understand a lot better, so fully agreed there as well.

Maybe also worth having a proper Fixes: tag instead of just naming the commit in prose?

Thanks,
Robin.

Quoted 37 lines
---
  lib/dma-debug.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
lib/dma-debug.c +5 -5
index 1b34d210452c..7f5cdc1e6b29 100644
@@ -1491,12 +1491,12 @@ void debug_dma_alloc_coherent(struct device *dev, size_t size,
  	if (unlikely(virt == NULL))
  		return;
 
-	entry = dma_entry_alloc();
-	if (!entry)
+	/* handle vmalloc and linear addresses */
+	if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
  		return;
 
-	/* handle vmalloc and linear addresses */
-	if (!is_vmalloc_addr(virt) && !virt_to_page(virt))
+	entry = dma_entry_alloc();
+	if (!entry)
  		return;
 
  	entry->type      = dma_debug_coherent;
@@ -1528,7 +1528,7 @@ void debug_dma_free_coherent(struct device *dev, size_t size,
  	};
 
  	/* handle vmalloc and linear addresses */
-	if (!is_vmalloc_addr(virt) && !virt_to_page(virt))
+	if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
  		return;
 
  	if (is_vmalloc_addr(virt))
#2 lore raw

Thanks, applied to the dma mapping tree for 4.16, including the Fixes tag.