diff mbox series

[v2] ARM/dma-mapping: use kvzalloc() in __iommu_alloc_buffer()

Message ID 20200827154332.41571-1-efremov@linux.com (mailing list archive)
State New, archived
Headers show
Series [v2] ARM/dma-mapping: use kvzalloc() in __iommu_alloc_buffer() | expand

Commit Message

Denis Efremov (Oracle) Aug. 27, 2020, 3:43 p.m. UTC
Use kvzalloc() in __iommu_alloc_buffer() instead of open-coding it.
Size computation wrapped in array_size() macro to prevent potential
integer overflows.

Signed-off-by: Denis Efremov <efremov@linux.com>
---
Changes in v2:
 - array_size() added

 arch/arm/mm/dma-mapping.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

Comments

Robin Murphy Aug. 27, 2020, 6:27 p.m. UTC | #1
On 2020-08-27 16:43, Denis Efremov wrote:
> Use kvzalloc() in __iommu_alloc_buffer() instead of open-coding it.
> Size computation wrapped in array_size() macro to prevent potential
> integer overflows.

FWIW I'm currently working on a much more thorough cleanup by way of 
removing this code entirely [1] ;)

Robin.

[1] 
https://lore.kernel.org/linux-arm-kernel/cover.1597931875.git.robin.murphy@arm.com/

> Signed-off-by: Denis Efremov <efremov@linux.com>
> ---
> Changes in v2:
>   - array_size() added
> 
>   arch/arm/mm/dma-mapping.c | 6 +-----
>   1 file changed, 1 insertion(+), 5 deletions(-)
> 
> diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c
> index 8a8949174b1c..c1f864ff7b84 100644
> --- a/arch/arm/mm/dma-mapping.c
> +++ b/arch/arm/mm/dma-mapping.c
> @@ -1199,14 +1199,10 @@ static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
>   {
>   	struct page **pages;
>   	int count = size >> PAGE_SHIFT;
> -	int array_size = count * sizeof(struct page *);
>   	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(sizeof(*pages), count), GFP_KERNEL);
>   	if (!pages)
>   		return NULL;
>   
>
diff mbox series

Patch

diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c
index 8a8949174b1c..c1f864ff7b84 100644
--- a/arch/arm/mm/dma-mapping.c
+++ b/arch/arm/mm/dma-mapping.c
@@ -1199,14 +1199,10 @@  static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
 {
 	struct page **pages;
 	int count = size >> PAGE_SHIFT;
-	int array_size = count * sizeof(struct page *);
 	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(sizeof(*pages), count), GFP_KERNEL);
 	if (!pages)
 		return NULL;