All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mike Rapoport <rppt@kernel.org>
To: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: linux-mm@kvack.org, Alexander Potapenko <glider@google.com>,
	Marco Elver <elver@google.com>,
	Dmitry Vyukov <dvyukov@google.com>
Subject: Re: [PATCH] mm: Fix memblock_free_late() when using deferred struct page
Date: Fri, 6 Feb 2026 12:33:28 +0200	[thread overview]
Message-ID: <aYXDeN0WGQG8-cxe@kernel.org> (raw)
In-Reply-To: <c3769af482913dd7ee9d5302c8afa990255691a5.camel@kernel.crashing.org>

(added KMSAN folks)

On Wed, Feb 04, 2026 at 08:02:13PM +1100, Benjamin Herrenschmidt wrote:
> On Wed, 2026-02-04 at 09:39 +0200, Mike Rapoport wrote:
> > > I might be missing something but I don't see what would restrict
> > > this
> > > to the early pre-initialized struct pages other than that
> > > early_page_initialised() test, so we can't rely on anything in
> > > struct
> > > page inside memblock_free_pages().
> > 
> > Right, we can't rely on PG_Reserved being cleared for uninitialized
> > pages :/
> > 
> > But I overlooked an easier and actually reliable way: use
> > free_reserved_area() instead of memblock_free_late().
> 
> You mean replace all callers of memblock_free_late() and kill it ?

That would be great, but with all the subtle differences you note below
it's for the future :)

> Or make memblock_free_late() use free_reserved_area() instead of
> memblock_free_pages() ? :-)

Yes, I think either calling free_reserved_page() in the loop in
memblock_free_late() or replacing the entire loop with free_reserved_area().
 
> The former misses:
>  - totalram_pages_inc() and kmemleak_free_part_phys() in
> memblock_free_late()
> 
> They also both miss as far as I can tell:
> 
> 	if (!kmsan_memblock_free_pages(page, order)) {
> 		/* KMSAN will take care of these pages. */
> 		return;
> 	}
> 
> But I don't know if that matters, I don't know anything about kmsan :-)

AFAIU, here kmsan allocates metadata for each page freed to buddy, but it
handles reserved memory differently anyway, so it shouldn't be a problem.
 
> There are other subtle differences between the two implementations
> which probably boil down to the same thing but it's been a while and I
> don't have time today to dig into the gory details :-)
> 
> ie, one does
> 
> 	clear_page_tag_ref(page);
> 	__free_pages_core(page, order, MEMINIT_EARLY);
> 
> ie, clear_page_tag_ref() is done once for the whole "order" (though in
> the memblock_free_late() order is always 0), then __free_pages_core()
> which kind-of hard resets count to 0 etc...
> 
> The other one ends up setting the count to 1 then __free_page() which 
> does a LOT more "stuff" that is new to me since last I looked (such as
> the pcp stuff), ie a lot more convoluted code path, but I don't know if
> it differs practically for that use case :-)

It does not :)
with __free_page() the pages may be freed to PCP lists rather than on
global free lists, but it does not really matter, the pages are free and
buddy can use them.
 
> I assume that the right approach here is to make memblock_free_late()
> call free_reserved_area() instead of memblock_free_pages() so we
> preserve totalram_pages_inc() and kmemleak_free_part_phys() but I might
> be missing something (and I don't know about KMSAN).

free_reserved_page() adjusts totalram internally, so we only need to
preserve kmemleak part.

So it essentially becomes "oneliner" :)

diff --git a/mm/memblock.c b/mm/memblock.c
index e76255e4ff36..6e984bcdf6cd 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -1770,10 +1770,8 @@ void __init memblock_free_late(phys_addr_t base, phys_addr_t size)
 	cursor = PFN_UP(base);
 	end = PFN_DOWN(base + size);
 
-	for (; cursor < end; cursor++) {
-		memblock_free_pages(pfn_to_page(cursor), cursor, 0);
-		totalram_pages_inc();
-	}
+	for (; cursor < end; cursor++)
+		free_reserved_page(pfn_to_page(cursor));
 }
 
 /*
 
> Cheers,
> Ben.

-- 
Sincerely yours,
Mike.


  reply	other threads:[~2026-02-06 10:33 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-03  8:02 [PATCH] mm: Fix memblock_free_late() when using deferred struct page Benjamin Herrenschmidt
2026-02-03 18:40 ` Mike Rapoport
2026-02-03 19:53   ` Benjamin Herrenschmidt
2026-02-04  7:39     ` Mike Rapoport
2026-02-04  9:02       ` Benjamin Herrenschmidt
2026-02-06 10:33         ` Mike Rapoport [this message]
2026-02-10  1:04           ` Benjamin Herrenschmidt
2026-02-10  2:10             ` Benjamin Herrenschmidt
2026-02-10  6:17               ` Benjamin Herrenschmidt
2026-02-10  8:34                 ` Benjamin Herrenschmidt
2026-02-10 14:32                   ` Mike Rapoport
2026-02-10 23:23                     ` Benjamin Herrenschmidt
2026-02-11  5:20                       ` Mike Rapoport
2026-02-16  5:34                       ` Benjamin Herrenschmidt
2026-02-16  6:51                         ` Benjamin Herrenschmidt
2026-02-16  4:53                     ` Benjamin Herrenschmidt
2026-02-16 15:28                       ` Mike Rapoport
2026-02-16 10:36           ` Alexander Potapenko
2026-02-17  8:28 ` [PATCH v2] " Benjamin Herrenschmidt
2026-02-17 12:32   ` Mike Rapoport
2026-02-17 22:00     ` Benjamin Herrenschmidt
2026-02-17 21:47   ` Benjamin Herrenschmidt
2026-02-18  0:15     ` Benjamin Herrenschmidt
2026-02-18  8:05       ` Mike Rapoport
2026-02-19  2:48         ` Benjamin Herrenschmidt
2026-02-19 10:16           ` Mike Rapoport
2026-02-19 22:46             ` Benjamin Herrenschmidt
2026-02-20  4:57               ` Benjamin Herrenschmidt
2026-02-20  9:09                 ` Mike Rapoport
2026-02-23  2:54                   ` Benjamin Herrenschmidt
2026-02-24  5:56                   ` Benjamin Herrenschmidt
2026-02-20  9:00               ` Mike Rapoport
2026-02-20  5:12             ` Benjamin Herrenschmidt
2026-02-20  5:15             ` Benjamin Herrenschmidt
2026-02-20  5:47             ` Benjamin Herrenschmidt

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=aYXDeN0WGQG8-cxe@kernel.org \
    --to=rppt@kernel.org \
    --cc=benh@kernel.crashing.org \
    --cc=dvyukov@google.com \
    --cc=elver@google.com \
    --cc=glider@google.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.