Skip to contentSkip to conversation

mm/cma: Fix crash on CMA allocation if bitmap allocation fails

PATCH In review Yue Hu 3 participants 4 messages Mar 25, 2019 – Mar 26, 2019

Subsystems (2)
lore
Share & tools
lore

Yue Hu

author
#0 lore raw

PATCH 1/1 mm/cma: Fix crash on CMA allocation if bitmap allocation fails

From: Yue Hu <[email protected]>

A previous commit f022d8cb7ec7 ("mm: cma: Don't crash on allocation if CMA area can't be activated") fixes the crash issue when activation fails via setting cma->count as 0, same logic exists if bitmap allocation fails.

---
 mm/cma.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/cma.c +3 -1
index f5bf819..991a6ce 100644
@@ -106,8 +106,10 @@ static int __init cma_activate_area(struct cma *cma)
 
 	cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
 
-	if (!cma->bitmap)
+	if (!cma->bitmap) {
+		cma->count = 0;
 		return -ENOMEM;
+	}
 
 	WARN_ON_ONCE(!pfn_valid(pfn));
 	zone = page_zone(pfn_to_page(pfn));
Signature · 1 line
1.9.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Yue Hu

From: Yue Hu <[email protected]>

A previous commit f022d8cb7ec7 ("mm: cma: Don't crash on allocation if CMA area can't be activated") fixes the crash issue when activation fails via setting cma->count as 0, same logic exists if bitmap allocation fails.

Looks good to me. Just wondering if cma->count = 0 should be wrapped around in a helper which explicitly states that this cma cannot be used for allocation.

Nonetheless.

maintainer
#2 lore raw
In reply to Yue Hu
Quoted 11 lines · On Mon, 25 Mar 2019 16:13:09 +0800 Yue Hu <[email protected]> wrote:

From: Yue Hu <[email protected]>

A previous commit f022d8cb7ec7 ("mm: cma: Don't crash on allocation if CMA area can't be activated") fixes the crash issue when activation fails via setting cma->count as 0, same logic exists if bitmap allocation fails.

mm/cma.c +0 -0
@@ -106,8 +106,10 @@ static int __init cma_activate_area(struct cma *cma)
 

mm/cma.c static int __init cma_activate_area(struct cma *cma) · line 115

Quoted 10 lines
mm/cma.c +3 -1
 	cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
 
-	if (!cma->bitmap)
+	if (!cma->bitmap) {
+		cma->count = 0;
 		return -ENOMEM;
+	}
 
 	WARN_ON_ONCE(!pfn_valid(pfn));
 	zone = page_zone(pfn_to_page(pfn));

I'm unsure whether this is needed.

kmalloc() within __init code is generally considered to be a "can't fail".

If this was the only issue then I guess I'd take the patch if only for documentation/clarity purposes. But cma_areas[] is in bss and is guaranteed to be all-zeroes, so I suspect this bug is a can't-happen. And we could revert f022d8cb7ec7 if we could be bothered (I can't).

author
#3 lore raw
Quoted 26 lines · On Mon, 25 Mar 2019 15:15:41 -0700 Andrew Morton <[email protected]>…
Quoted 11 lines · On Mon, 25 Mar 2019 16:13:09 +0800 Yue Hu <[email protected]> wrote:

From: Yue Hu <[email protected]>

A previous commit f022d8cb7ec7 ("mm: cma: Don't crash on allocation if CMA area can't be activated") fixes the crash issue when activation fails via setting cma->count as 0, same logic exists if bitmap allocation fails.

mm/cma.c +0 -0
@@ -106,8 +106,10 @@ static int __init cma_activate_area(struct cma *cma)
 

mm/cma.c static int __init cma_activate_area(struct cma *cma) · line 115

Quoted 10 lines
mm/cma.c +3 -1
 	cma->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
 
-	if (!cma->bitmap)
+	if (!cma->bitmap) {
+		cma->count = 0;
 		return -ENOMEM;
+	}
 
 	WARN_ON_ONCE(!pfn_valid(pfn));
 	zone = page_zone(pfn_to_page(pfn));  

I'm unsure whether this is needed.

Quoted 6 lines

kmalloc() within __init code is generally considered to be a "can't fail".

If this was the only issue then I guess I'd take the patch if only for documentation/clarity purposes. But cma_areas[] is in bss and is guaranteed to be all-zeroes, so I suspect this bug is a can't-happen.

However, firstly cma->count will be assigned to size >> PAGE_SHIFT in cma_init_reserved_mem().

Quoted 3 lines

And we could revert f022d8cb7ec7 if we could be bothered (I can't).