Skip to content

Commit 94ff46d

Browse files
jgowansrppt
authored andcommitted
memblock: Move late alloc warning down to phys alloc
If a driver/subsystem tries to do an allocation after the memblock allocations have been freed and the memory handed to the buddy allocator, it will not actually be legal to use that allocation: the buddy allocator owns the memory. Currently this mis-use is handled by the memblock function which does allocations and returns virtual addresses by printing a warning and doing a kmalloc instead. However the physical allocation function does not to do this check - callers of the physical alloc function are unprotected against mis-use. Improve the error catching here by moving the check into the physical allocation function which is used by the virtual addr allocation function. Signed-off-by: James Gowans <[email protected]> Cc: Mike Rapoport <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Alex Graf <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mike Rapoport (IBM) <[email protected]>
1 parent d9d814e commit 94ff46d

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

mm/memblock.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,17 @@ phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
14491449
if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
14501450
nid = NUMA_NO_NODE;
14511451

1452+
/*
1453+
* Detect any accidental use of these APIs after slab is ready, as at
1454+
* this moment memblock may be deinitialized already and its
1455+
* internal data may be destroyed (after execution of memblock_free_all)
1456+
*/
1457+
if (WARN_ON_ONCE(slab_is_available())) {
1458+
void *vaddr = kzalloc_node(size, GFP_NOWAIT, nid);
1459+
1460+
return vaddr ? virt_to_phys(vaddr) : 0;
1461+
}
1462+
14521463
if (!align) {
14531464
/* Can't use WARNs this early in boot on powerpc */
14541465
dump_stack();
@@ -1574,13 +1585,6 @@ static void * __init memblock_alloc_internal(
15741585
{
15751586
phys_addr_t alloc;
15761587

1577-
/*
1578-
* Detect any accidental use of these APIs after slab is ready, as at
1579-
* this moment memblock may be deinitialized already and its
1580-
* internal data may be destroyed (after execution of memblock_free_all)
1581-
*/
1582-
if (WARN_ON_ONCE(slab_is_available()))
1583-
return kzalloc_node(size, GFP_NOWAIT, nid);
15841588

15851589
if (max_addr > memblock.current_limit)
15861590
max_addr = memblock.current_limit;

0 commit comments

Comments
 (0)