]> Git Repo - J-linux.git/commitdiff
memblock: Move late alloc warning down to phys alloc
authorJames Gowans <[email protected]>
Wed, 19 Jun 2024 09:55:55 +0000 (11:55 +0200)
committerMike Rapoport (IBM) <[email protected]>
Wed, 19 Jun 2024 15:05:25 +0000 (18:05 +0300)
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]>
mm/memblock.c

index adb5a3c4bf06b5099ec2bf6306a59ae6c4eb7715..692dc551c0fde487999898b147614ecef102b307 100644 (file)
@@ -1449,6 +1449,17 @@ phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
        if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n"))
                nid = NUMA_NO_NODE;
 
+       /*
+        * Detect any accidental use of these APIs after slab is ready, as at
+        * this moment memblock may be deinitialized already and its
+        * internal data may be destroyed (after execution of memblock_free_all)
+        */
+       if (WARN_ON_ONCE(slab_is_available())) {
+               void *vaddr = kzalloc_node(size, GFP_NOWAIT, nid);
+
+               return vaddr ? virt_to_phys(vaddr) : 0;
+       }
+
        if (!align) {
                /* Can't use WARNs this early in boot on powerpc */
                dump_stack();
@@ -1574,13 +1585,6 @@ static void * __init memblock_alloc_internal(
 {
        phys_addr_t alloc;
 
-       /*
-        * Detect any accidental use of these APIs after slab is ready, as at
-        * this moment memblock may be deinitialized already and its
-        * internal data may be destroyed (after execution of memblock_free_all)
-        */
-       if (WARN_ON_ONCE(slab_is_available()))
-               return kzalloc_node(size, GFP_NOWAIT, nid);
 
        if (max_addr > memblock.current_limit)
                max_addr = memblock.current_limit;
This page took 0.052122 seconds and 4 git commands to generate.