memblocks: Move late alloc warning down to phys alloc
PATCH 1/1 memblocks: Move late alloc warning down to phys alloc
If a driver/subsystem tries to do an allocation after memblocks 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. This is handled by the memblocks 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.
--- mm/memblock.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
mm/memblock.c +11 -7
@@ -1457,6 +1457,17 @@ phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, align =SMP_CACHE_BYTES ; } +/* +* 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 ; + } + again: found =memblock_find_in_range_node (size, align, start, end, nid, flags);
@@ -1576,13 +1587,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;
Signature · 1 line
2.34.1
Mike Rapoport
maintainerQuoted 1 line · James Gowans
Subject: [PATCH] memblocks: Move late alloc warning down to phys alloc
Nit: memblock
Quoted 8 lines
If a driver/subsystem tries to do an allocation after memblocks 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. This is handled by the memblocks 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.
Did you see such misuse or this is a theoretical issue?
Quoted 20 lines
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]>--- mm/memblock.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)mm/memblock.c +0
-0@@ -1457,6 +1457,17 @@ phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, align =SMP_CACHE_BYTES ; }
mm/memblock.c phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, · line 1469
Quoted 10 lines
mm/memblock.c +10
-0+/* +* 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 ; + }
I'd move this before alignment check.
Quoted 21 lines
mm/memblock.c +1
-7+ again: found =memblock_find_in_range_node (size, align, start, end, nid, flags);@@ -1576,13 +1587,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;Signature · 1 line
2.34.1
Signature · 2 lines
Sincerely yours, Mike.
Quoted 4 lines · Mike Rapoport
Quoted 1 line · James Gowans
Subject: [PATCH] memblocks: Move late alloc warning down to phys alloc
Nit: memblock
Ack!
Quoted 11 lines
Quoted 9 lines
If a driver/subsystem tries to do an allocation after memblocks 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. This is handled by the memblocks 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.
Did you see such misuse or this is a theoretical issue?
Yeah, I was driving the memblock allocator badly when prototyping something. Allocating a large contiguous block of memory for an in- memory filesystem and I was doing the allocation in an initcall, but by that point it was too late. The memblock allocator happily gave me a large chunk of memory, but it was already in use by the buddy allocator, so ended up with memory corruption. Oops! Getting this warning would have made the problem immediately obvious.
Quoted 21 lines
Quoted 21 lines
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]>--- mm/memblock.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)mm/memblock.c +0
-0@@ -1457,6 +1457,17 @@ phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, align =SMP_CACHE_BYTES ; }
Quoted 12 lines
mm/memblock.c phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, · line 1469
Quoted 10 lines
mm/memblock.c +10
-0+/* +* 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 ; + }I'd move this before alignment check.
Ack, will do in V2.
Anything else or should I make the tweaks and post V2?
JG
Mike Rapoport
maintainerQuoted 10 lines · James
Quoted 4 lines · Mike Rapoport
Quoted 1 line · James Gowans
Subject: [PATCH] memblocks: Move late alloc warning down to phys alloc
Nit: memblock
Ack!
Quoted 2 lines
Quoted 2 lines
If a driver/subsystem tries to do an allocation after memblocks have
And another one here :) ^
Quoted 52 lines
Quoted 9 lines
Quoted 7 lines
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. This is handled by the memblocks 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.
Did you see such misuse or this is a theoretical issue?
Yeah, I was driving the memblock allocator badly when prototyping something. Allocating a large contiguous block of memory for an in- memory filesystem and I was doing the allocation in an initcall, but by that point it was too late. The memblock allocator happily gave me a large chunk of memory, but it was already in use by the buddy allocator, so ended up with memory corruption. Oops! Getting this warning would have made the problem immediately obvious.
Quoted 21 lines
Quoted 21 lines
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]>--- mm/memblock.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)mm/memblock.c +0
-0@@ -1457,6 +1457,17 @@ phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, align =SMP_CACHE_BYTES ; }Quoted 12 lines
mm/memblock.c phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, · line 1469
Quoted 10 lines
mm/memblock.c +10
-0+/* +* 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 ; + }I'd move this before alignment check.
Quoted 3 lines
Ack, will do in V2.
Anything else or should I make the tweaks and post V2?
Looks ok to me.
Quoted 1 line
JG
Signature · 2 lines
Sincerely yours, Mike.