Skip to contentSkip to conversation

dma-remap: Use kvmalloc_array/kvfree for larger dma memory remap

PATCH Superseded gaoxu 00016977 2 participants 3 messages May 6, 2023 – May 16, 2023 DMA mapping helpers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dma-remap: Use kvmalloc_array/kvfree for larger dma memory remap

If dma_direct_alloc() alloc memory in size of 64MB, the inner function dma_common_contiguous_remap() will allocate 128KB memory by invoking the function kmalloc_array(). and the kmalloc_array seems to fail to try to allocate 128KB mem. work around by doing kvmalloc_array instead.

---
 kernel/dma/remap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/dma/remap.c +2 -2
@@ -43,13 +43,13 @@ void *dma_common_contiguous_remap(struct page *page, size_t size,
 	void *vaddr;
 	int i;
 
-	pages = kmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
+	pages = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
 	if (!pages)
 		return NULL;
 	for (i = 0; i < count; i++)
 		pages[i] = nth_page(page, i);
 	vaddr = vmap(pages, count, VM_DMA_COHERENT, prot);
-	kfree(pages);
+	kvfree(pages);
 
 	return vaddr;
 }

--
2.17.1

author
#1 lore raw

Please help to review, thanks.

-----Original Message-----
From: gaoxu 00016977
Sent: 2023年5月6日 15:34
To: '[email protected]' <[email protected]>; '[email protected]' <[email protected]>
Cc: '[email protected]' <[email protected]>; '[email protected]' <[email protected]>; '[email protected]' <[email protected]>; '[email protected]' <[email protected]>; yipengxiang 00013268 <[email protected]>; wangbintian 00013160 <[email protected]>; hanfeng 00012985 <[email protected]>
Subject: [PATCH] dma-remap: Use kvmalloc_array/kvfree for larger dma memory remap

If dma_direct_alloc() alloc memory in size of 64MB, the inner function
dma_common_contiguous_remap() will allocate 128KB memory by invoking the function kmalloc_array(). and the kmalloc_array seems to fail to try to allocate 128KB mem. work around by doing kvmalloc_array instead.

---
 kernel/dma/remap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/dma/remap.c +2 -2
@@ -43,13 +43,13 @@ void *dma_common_contiguous_remap(struct page *page, size_t size,
 	void *vaddr;
 	int i;
 
-	pages = kmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
+	pages = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
 	if (!pages)
 		return NULL;
 	for (i = 0; i < count; i++)
 		pages[i] = nth_page(page, i);
 	vaddr = vmap(pages, count, VM_DMA_COHERENT, prot);
-	kfree(pages);
+	kvfree(pages);
 
 	return vaddr;
 }

--
2.17.1

Reviewed-by on v1 1/1
#2 lore raw
In reply to gaoxu 00016977
Quoted 5 lines · On Sat, May 6, 2023 at 12:33 AM gaoxu 00016977 <[email protected]> wrote:

If dma_direct_alloc() alloc memory in size of 64MB, the inner function dma_common_contiguous_remap() will allocate 128KB memory by invoking the function kmalloc_array(). and the kmalloc_array seems to fail to try to allocate 128KB mem. work around by doing kvmalloc_array instead.

On systems like Android it's not unusual for order 5 allocation to fail. The change seems safe to me with no downside since kvmalloc will try kmalloc first.

Quoted 2 lines
Quoted 26 lines
---
 kernel/dma/remap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/dma/remap.c +2 -3
@@ -43,13 +43,13 @@ void *dma_common_contiguous_remap(struct page *page, size_t size,
        void *vaddr;
        int i;
 
-       pages = kmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
+       pages = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
        if (!pages)
                return NULL;
        for (i = 0; i < count; i++)
                pages[i] = nth_page(page, i);
        vaddr = vmap(pages, count, VM_DMA_COHERENT, prot);
-       kfree(pages);
+       kvfree(pages);
 
        return vaddr;
 }
--

2.17.1