Skip to contentSkip to conversation

dma: contiguous: Check return value of dma_contiguous_reserve_area()

PATCH Applied Shanker Donthineni 3 participants 3 messages Jan 29 – Feb 2 DMA mapping helpers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dma: contiguous: Check return value of dma_contiguous_reserve_area()

Commit 8f1fc1bf1a3d ("dma: contiguous: Reserve default CMA heap") introduced a bug where dma_heap_cma_register_heap() is called with a NULL pointer when dma_contiguous_reserve_area() fails to reserve the CMA area.

When dma_contiguous_reserve_area() fails, dma_contiguous_default_area remains NULL (initialized as a global variable), but the code doesn't check the return value and proceeds to call dma_heap_cma_register_heap() with this NULL pointer.

Later during boot, add_cma_heaps() iterates through the dma_areas[] array and attempts to register heaps. When it encounters the NULL pointer stored by the earlier call, it crashes in __add_cma_heap() -> dma_heap_add() when trying to dereference the NULL CMA pointer.

The crash manifests as:

  Unable to handle kernel NULL pointer dereference at virtual address
  0000000000000038
  ...
  Call trace:
   dma_heap_add+0x40/0x2b0
   __add_cma_heap+0x80/0xe0
   add_cma_heaps+0x64/0xb0
   do_one_initcall+0x60/0x318
   kernel_init_freeable+0x260/0x2f0
   kernel_init+0x2c/0x168
   ret_from_fork+0x10/0x20

Fix this by checking the return value of dma_contiguous_reserve_area() and only calling dma_heap_cma_register_heap() when the reservation succeeds.

Fixes 8f1fc1bf1a3d ("dma: contiguous: Reserve default CMA heap")
---
 kernel/dma/contiguous.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
kernel/dma/contiguous.c +6 -4
index d8fd6f779f797..92c539d76412b 100644
@@ -247,10 +247,12 @@ void __init dma_contiguous_reserve(phys_addr_t limit)
 		pr_debug("%s: reserving %ld MiB for global area\n", __func__,
 			 (unsigned long)selected_size / SZ_1M);
 
-		dma_contiguous_reserve_area(selected_size, selected_base,
-					    selected_limit,
-					    &dma_contiguous_default_area,
-					    fixed);
+		ret = dma_contiguous_reserve_area(selected_size, selected_base,
+						  selected_limit,
+						  &dma_contiguous_default_area,
+						  fixed);
+		if (ret)
+			return;
 
 		ret = dma_heap_cma_register_heap(dma_contiguous_default_area);
 		if (ret)
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 29 lines · Shanker Donthineni

Commit 8f1fc1bf1a3d ("dma: contiguous: Reserve default CMA heap") introduced a bug where dma_heap_cma_register_heap() is called with a NULL pointer when dma_contiguous_reserve_area() fails to reserve the CMA area.

When dma_contiguous_reserve_area() fails, dma_contiguous_default_area remains NULL (initialized as a global variable), but the code doesn't check the return value and proceeds to call dma_heap_cma_register_heap() with this NULL pointer.

Later during boot, add_cma_heaps() iterates through the dma_areas[] array and attempts to register heaps. When it encounters the NULL pointer stored by the earlier call, it crashes in __add_cma_heap() -> dma_heap_add() when trying to dereference the NULL CMA pointer.

The crash manifests as:

  Unable to handle kernel NULL pointer dereference at virtual address
  0000000000000038
  ...
  Call trace:
   dma_heap_add+0x40/0x2b0
   __add_cma_heap+0x80/0xe0
   add_cma_heaps+0x64/0xb0
   do_one_initcall+0x60/0x318
   kernel_init_freeable+0x260/0x2f0
   kernel_init+0x2c/0x168
   ret_from_fork+0x10/0x20
Quoted 6 lines

Fix this by checking the return value of dma_contiguous_reserve_area() and only calling dma_heap_cma_register_heap() when the reservation succeeds.

Fixes 8f1fc1bf1a3d ("dma: contiguous: Reserve default CMA heap")

Might be good to add a pr_warn when dma_contiguous_reserve_area fails, since we won't get the pr_warn for a dma_heap_cma_register_heap failure when we return early.

Either way:

maintainer Applied
#2 lore raw
In reply to Shanker Donthineni
Quoted 28 lines · Shanker Donthineni

Commit 8f1fc1bf1a3d ("dma: contiguous: Reserve default CMA heap") introduced a bug where dma_heap_cma_register_heap() is called with a NULL pointer when dma_contiguous_reserve_area() fails to reserve the CMA area.

When dma_contiguous_reserve_area() fails, dma_contiguous_default_area remains NULL (initialized as a global variable), but the code doesn't check the return value and proceeds to call dma_heap_cma_register_heap() with this NULL pointer.

Later during boot, add_cma_heaps() iterates through the dma_areas[] array and attempts to register heaps. When it encounters the NULL pointer stored by the earlier call, it crashes in __add_cma_heap() -> dma_heap_add() when trying to dereference the NULL CMA pointer.

The crash manifests as:

   Unable to handle kernel NULL pointer dereference at virtual address
   0000000000000038
   ...
   Call trace:
    dma_heap_add+0x40/0x2b0
    __add_cma_heap+0x80/0xe0
    add_cma_heaps+0x64/0xb0
    do_one_initcall+0x60/0x318
    kernel_init_freeable+0x260/0x2f0
    kernel_init+0x2c/0x168
    ret_from_fork+0x10/0x20
Quoted 6 lines

Fix this by checking the return value of dma_contiguous_reserve_area() and only calling dma_heap_cma_register_heap() when the reservation succeeds.

Fixes 8f1fc1bf1a3d ("dma: contiguous: Reserve default CMA heap")

Applied to dma-mapping-fixes, thanks!

Quoted 15 lines
---
  kernel/dma/contiguous.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
kernel/dma/contiguous.c +0 -3
index d8fd6f779f797..92c539d76412b 100644
@@ -247,10 +247,12 @@ void __init dma_contiguous_reserve(phys_addr_t limit)
  		pr_debug("%s: reserving %ld MiB for global area\n", __func__,
  			 (unsigned long)selected_size / SZ_1M);
 
-		dma_contiguous_reserve_area(selected_size, selected_base,
-					    selected_limit,
-					    &dma_contiguous_default_area,

kernel/dma/contiguous.c void __init dma_contiguous_reserve(phys_addr_t limit) · line 258

Quoted 10 lines
kernel/dma/contiguous.c +6 -1
-					    fixed);
+		ret = dma_contiguous_reserve_area(selected_size, selected_base,
+						  selected_limit,
+						  &dma_contiguous_default_area,
+						  fixed);
+		if (ret)
+			return;
 
  		ret = dma_heap_cma_register_heap(dma_contiguous_default_area);
  		if (ret)

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland