Skip to contentSkip to conversation

mm/page_alloc: check return value of memblock_alloc_node_nopanic()

PATCH Superseded Mike Rapoport 2 participants 4 messages Jan 16, 2019 – Jan 17, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/page_alloc: check return value of memblock_alloc_node_nopanic()

There are two early memory allocations that use memblock_alloc_node_nopanic() and do not check its return value. While this happens very early during boot and chances that the allocation will fail are diminishing, it is still worth to have proper checks for the allocation errors.

---
 mm/page_alloc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
mm/page_alloc.c +8 -1
index d295c9bc01a8..7801accbe02a 100644
@@ -6376,10 +6376,14 @@ static void __ref setup_usemap(struct pglist_data *pgdat,
 {
 	unsigned long usemapsize = usemap_size(zone_start_pfn, zonesize);
 	zone->pageblock_flags = NULL;
-	if (usemapsize)
+	if (usemapsize) {
 		zone->pageblock_flags =
 			memblock_alloc_node_nopanic(usemapsize,
 							 pgdat->node_id);
+		if (!zone->pageblock_flags)
+			panic("Failed to allocate %ld bytes for zone %s pageblock flags\n",
+			      usemapsize, zone->name);
+	}
 }
 #else
 static inline void setup_usemap(struct pglist_data *pgdat, struct zone *zone,
@@ -6609,6 +6613,9 @@ static void __ref alloc_node_mem_map(struct pglist_data *pgdat)
 		end = ALIGN(end, MAX_ORDER_NR_PAGES);
 		size =  (end - start) * sizeof(struct page);
 		map = memblock_alloc_node_nopanic(size, pgdat->node_id);
+		if (!map)
+			panic("Failed to allocate %ld bytes for memory map\n",
+			      size);
 		pgdat->node_mem_map = map + offset;
 	}
 	pr_debug("%s: node %d, pgdat %08lx, node_mem_map %08lx\n",
Signature · 1 line
2.7.4
#1 lore raw

This seems very reasonable, but if the code is just going to panic if the allocation fails, why not call memblock_alloc_node() instead?

If there is a reason we'd prefer to call memblock_alloc_node_nopanic(), I'd like to see pgdat->nodeid printed in the panic message as well.

#2 lore raw
Quoted 3 lines · William Kucharski

This seems very reasonable, but if the code is just going to panic if the allocation fails, why not call memblock_alloc_node() instead?

I've sent patches [1] that remove panic() from memblock_alloc*() and drop _nopanic variants. After they will be (hopefully) merged, memblock_alloc_node() will return NULL on error.

Quoted 2 lines

If there is a reason we'd prefer to call memblock_alloc_node_nopanic(), I'd like to see pgdat->nodeid printed in the panic message as well.

Sure.

[1] https://lore.kernel.org/lkml/[email protected]/

Signature · 2 lines
Sincerely yours,
Mike.
Reviewed-by
#3 lore raw
Quoted 9 lines · Mike Rapoport
Quoted 3 lines · William Kucharski

This seems very reasonable, but if the code is just going to panic if the allocation fails, why not call memblock_alloc_node() instead?

Quoted 8 lines

I've sent patches [1] that remove panic() from memblock_alloc*() and drop _nopanic variants. After they will be (hopefully) merged, memblock_alloc_node() will return NULL on error.

Quoted 2 lines

If there is a reason we'd prefer to call memblock_alloc_node_nopanic(), I'd like to see pgdat->nodeid printed in the panic message as well.

Sure.

Thanks for the quick response.