Skip to contentSkip to conversation

ARM/dma-mapping: use kvzalloc for fallback memory allocation need

PATCH Superseded Deepak R Varma 1 participant 2 messages Dec 20, 2022 – Jan 20, 2023 Arm (32-bit)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARM/dma-mapping: use kvzalloc for fallback memory allocation need

When the memory sizes are not known upfront, it is preferred to use the kvzalloc helper function instead of direct conditional evaluation of size and kzalloc/vzalloc fallback design. The kvzalloc helper function in this case is more efficient as it avoids indefinite kzalloc retries when a small memory size is needed but is unavailable.
This LWN article has further details on the advantages of using kvzalloc in case of fallback memory allocation needs:
https://lwn.net/Articles/711653/

This patch proposal is based on following Coccinelle warning using the kvmalloc.cocci semantic patch.

arch/arm/mm/dma-mapping.c:858:28-29: WARNING opportunity for kvmalloc

---
Note: The patch proposal is compile tested only.

 arch/arm/mm/dma-mapping.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
arch/arm/mm/dma-mapping.c +1 -4
index c135f6e37a00..2b79af377a81 100644
@@ -855,10 +855,7 @@ static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
 	int i = 0;
 	int order_idx = 0;
 
-	if (array_size <= PAGE_SIZE)
-		pages = kzalloc(array_size, GFP_KERNEL);
-	else
-		pages = vzalloc(array_size);
+	pages = kvzalloc(array_size, GFP_KERNEL);
 	if (!pages)
 		return NULL;
 

--
2.34.1

#1 lore raw
Quoted 9 lines · Deepak R Varma

When the memory sizes are not known upfront, it is preferred to use the kvzalloc helper function instead of direct conditional evaluation of size and kzalloc/vzalloc fallback design. The kvzalloc helper function in this case is more efficient as it avoids indefinite kzalloc retries when a small memory size is needed but is unavailable.
This LWN article has further details on the advantages of using kvzalloc in case of fallback memory allocation needs:
https://lwn.net/Articles/711653/

Quoted 6 lines

This patch proposal is based on following Coccinelle warning using the kvmalloc.cocci semantic patch.

arch/arm/mm/dma-mapping.c:858:28-29: WARNING opportunity for kvmalloc

Hello,
May I please request a review and feedback on this patch proposal?

Thank you,
./drv

Quoted 25 lines

---
Note: The patch proposal is compile tested only.

 arch/arm/mm/dma-mapping.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
arch/arm/mm/dma-mapping.c +1 -5
index c135f6e37a00..2b79af377a81 100644
@@ -855,10 +855,7 @@ static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
 	int i = 0;
 	int order_idx = 0;
 
-	if (array_size <= PAGE_SIZE)
-		pages = kzalloc(array_size, GFP_KERNEL);
-	else
-		pages = vzalloc(array_size);
+	pages = kvzalloc(array_size, GFP_KERNEL);
 	if (!pages)
 		return NULL;
 
--

2.34.1