linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Mike Rapoport <rppt@kernel.org>
To: James Gowans <jgowans@amazon.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	Alex Graf <graf@amazon.de>
Subject: Re: [PATCH] memblocks: Move late alloc warning down to phys alloc
Date: Tue, 18 Jun 2024 14:02:20 +0300	[thread overview]
Message-ID: <ZnFpPCSTAUj90FJF@kernel.org> (raw)
In-Reply-To: <20240614133016.134150-1-jgowans@amazon.com>

On Fri, Jun 14, 2024 at 03:30:16PM +0200, James Gowans wrote:
> Subject: [PATCH] memblocks: Move late alloc warning down to phys alloc

Nit: memblock

> 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?
 
> 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 <jgowans@amazon.com>
> Cc: Mike Rapoport <rppt@kernel.org>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Alex Graf <graf@amazon.de>
> ---
>  mm/memblock.c | 18 +++++++++++-------
>  1 file changed, 11 insertions(+), 7 deletions(-)
> 
> diff --git a/mm/memblock.c b/mm/memblock.c
> index d09136e040d3..dd4f237dc1fc 100644
> --- a/mm/memblock.c
> +++ b/mm/memblock.c
> @@ -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;
> +	}

I'd move this before alignment check.

> +
>  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;
> -- 
> 2.34.1
> 

-- 
Sincerely yours,
Mike.


  reply	other threads:[~2024-06-18 11:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-14 13:30 James Gowans
2024-06-18 11:02 ` Mike Rapoport [this message]
2024-06-19  7:53   ` Gowans, James
2024-06-19  9:24     ` Mike Rapoport

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZnFpPCSTAUj90FJF@kernel.org \
    --to=rppt@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=graf@amazon.de \
    --cc=jgowans@amazon.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox