[PATCH] dma-buf: dma-heap: Add a size limitation for allocation

From: guangming.cao
Date: Fri Dec 17 2021 - 04:40:38 EST


From: Guangming <Guangming.Cao@xxxxxxxxxxxx>

Currently, there is no size check for allocation.

If the alloc size is larger than DRAM, it will cause OOM issue.
Besides, if it runs on a process that won't be killed by OOM flow, it will
cause a kernel exception finally, and we couldn't find the correct
memory usage by dma-buf dump api such as "dma_buf_debug_show" since the
allocation is still on going and the corresponding dmabuf is not exported.

However, it sounds not simple enough to adding a count to count how many
pages has been allocated before allocating done.
So adding a size limitation here to prevent this case.

Signed-off-by: Guangming <Guangming.Cao@xxxxxxxxxxxx>
Signed-off-by: jianjiao zeng <jianjiao.zeng@xxxxxxxxxxxx>
---
drivers/dma-buf/dma-heap.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c
index 56bf5ad01ad5..8b75998a106c 100644
--- a/drivers/dma-buf/dma-heap.c
+++ b/drivers/dma-buf/dma-heap.c
@@ -107,6 +107,9 @@ static long dma_heap_ioctl_allocate(struct file *file, void *data)
if (heap_allocation->heap_flags & ~DMA_HEAP_VALID_HEAP_FLAGS)
return -EINVAL;

+ if (heap_allocation->len / PAGE_SIZE > totalram_pages() / 2)
+ return -EINVAL;
+
fd = dma_heap_buffer_alloc(heap, heap_allocation->len,
heap_allocation->fd_flags,
heap_allocation->heap_flags);
--
2.17.1