linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Pratyush Yadav <pratyush@kernel.org>
To: Mike Rapoport <rppt@kernel.org>
Cc: Pratyush Yadav <pratyush@kernel.org>,
	 Pasha Tatashin <pasha.tatashin@soleen.com>,
	 Alexander Graf <graf@amazon.com>,
	 Changyuan Lyu <changyuanl@google.com>,
	 Andrew Morton <akpm@linux-foundation.org>,
	Baoquan He <bhe@redhat.com>,
	 kexec@lists.infradead.org, linux-kernel@vger.kernel.org,
	 linux-mm@kvack.org,  Michal Clapinski <mclapinski@google.com>
Subject: Re: [PATCH] kho: initialize tail pages for higher order folios properly
Date: Fri, 13 Jun 2025 16:22:32 +0200	[thread overview]
Message-ID: <mafs0wm9fn2l3.fsf@kernel.org> (raw)
In-Reply-To: <aEmUU4L3eStEsYQM@kernel.org>

On Wed, Jun 11 2025, Mike Rapoport wrote:

> On Wed, Jun 11, 2025 at 04:01:52PM +0200, Pratyush Yadav wrote:
>> On Wed, Jun 11 2025, Mike Rapoport wrote:
>> 
>> > On Wed, Jun 11, 2025 at 09:14:55AM -0400, Pasha Tatashin wrote:
>> >> On Wed, Jun 11, 2025 at 9:06 AM Pratyush Yadav <pratyush@kernel.org> wrote:
>> >> >
>> >> > On Tue, Jun 10 2025, Pasha Tatashin wrote:
>> >> >
>> >> > >> > > I think it should be the other way around, KHO should depend on
>> >> > >> > > !DEFERRED_STRUCT_PAGE_INIT.
>> >> > >> >
>> >> > >> > Agreed, and this is what I first tried, but that does not work, there
>> >> > >> > is some circular dependency breaking the build. If you feel
>> >> > >> > adventurous you can try that :-)
>> >> > >>
>> >> > >> Hmm, weird, worked for me :/
>> >> >
>> >> > Worked for me as well.
>> >> >
>> >> > >
>> >> > > I am super confused, it did not work for me over weekend, and now it
>> >> > > is working. Even `make menuconfig` would not work. Anyways, I will put
>> >> > > it in the appropriate place.
>> >> > >
>> >> > >>
>> >> > >> > > > We will need to teah KHO to work with deferred struct page init. I
>> >> > >> > > > suspect, we could init preserved struct pages and then skip over them
>> >> > >> > > > during deferred init.
>> >> > >> > >
>> >> > >> > > We could, but with that would mean we'll run this before SMP and it's not
>> >> > >> > > desirable. Also, init_deferred_page() for a random page requires
>> >> > >> >
>> >> > >> > We already run KHO init before smp_init:
>> >> > >> > start_kernel() -> mm_core_init() -> kho_memory_init() ->
>> >> > >> > kho_restore_folio() -> struct pages must be already initialized here!
>> >> > >> >
>> >> > >> > While deferred struct pages are initialized:
>> >> > >> > start_kernel() -> rest_init() -> kernel_init() ->
>> >> > >> > kernel_init_freeable() -> page_alloc_init_late() ->
>> >> > >> > deferred_init_memmap()
>> >> > >> >
>> >> > >> > If the number of preserved pages that is needed during early boot is
>> >> > >> > relatively small, that it should not be an issue to pre-initialize
>> >> > >> > struct pages for them before deferred struct pages are initialized. We
>> >> > >> > already pre-initialize some  "struct pages" that are needed during
>> >> > >> > early boot before the reset are initialized, see deferred_grow_zone()
>> >> > >>
>> >> > >> deferred_grow_zone() takes a chunk in the beginning of uninitialized range,
>> >> > >> with kho we are talking about some random pages. If we preinit them early,
>> >> > >> deferred_init_memmap() will overwrite them.
>> >> > >
>> >> > > Yes, this is why I am saying that we would need to skip the KHO
>> >> > > initialized "struct pages" somehow during deferred initialization. If
>> >> > > we create an ordered by PFN list of early-initialized KHO struct
>> >> > > pages, skipping during deferred initialization could be done
>> >> > > efficiently.
>> >> >
>> >> > Or keep things simple and don't use any KHO struct pages during early
>> >> > init. You can access the page itself, just don't use its struct page.
>> >> >
>> >> > Currently the only user of kho_restore_folio() during init is
>> >> > kho_memory_init(). The FDT is accessed by doing
>> >> > phys_to_virt(kho_in.fdt_phys) anyway, so there is really no need for
>> >> > restoring the folio so early. It can be done later, for example when LUO
>> >> > does the finish event, to clean up and free the folio.
>> >> 
>> >> Good suggestion, however, KHO does not have any sophisticated users
>> >> that we are going to be adding as part of the live update work in the
>> >> future: IR, KVM, early VCPU threads, and so on. So, while today, this
>> >> might work, in the future, I am not sure if we should expect struct
>> >> pages are not accessed until after deferred initialization or simply
>> >> fix it once and for all.
>> >
>> > KHO already accesses stuct page early and uses page->private for order.
>> > Since preserved memory is reserved in memblock, deferred init of struct
>> > pages won't touch those pages, we just need to make sure they are properly 
>> 
>> Not strictly true. Some of them might have been initialized from
>> free_area_init() -> memmap_init() (the ones not eligible for deferred
>> init), which happens before KHO makes its memblock reservations.
>> 
>> > initialized at some point. If we don't expect many kho_restore_folio()
>> > before page_alloc_init_late() we can use init_deferred_page() for early
>> > accesses.
>> 
>> I tried doing this when looking into this initially, but it doesn't work
>> for some reason.
>> 
>>     static void kho_restore_page(struct page *page, unsigned int order)
>>     {
>>     	unsigned int i, nr_pages = (1 << order);
>>     
>>     	/* Head page gets refcount of 1. */
>>     	init_deferred_page(page_to_pfn(page), NUMA_NO_NODE);
>
>
> This would do
>
> 	if (early_page_initialised(pfn, nid))
> 		return;
>
> 	__init_page_from_nid(pfn, nid);
>
> and I'm really surprised it didn't crash in early_page_initialised()
> because of NUMA_NO_NODE :)

Oh, right. Using the wrong node completely throws
early_page_initialised() off.

>
> What might work here is 
>
> 	pfn = page_to_pfn(page);
> 	__init_page_from_nid(pfn, early_pfn_to_nid(pfn));

Yep, that works. Although this would do early_pfn_to_nid() for each page
so it isn't very efficient. And we also need to make sure memblock does
not go away.

>
>>     	set_page_count(page, 1);
>>     
>>     	/* For higher order folios, tail pages get a page count of zero. */
>>     	for (i = 1; i < nr_pages; i++) {
>>     		init_deferred_page(page_to_pfn(page + i), NUMA_NO_NODE);
>>     		set_page_count(page + i, 0);
>>     	}
>>     
>>     [...]
>> 
[...]

-- 
Regards,
Pratyush Yadav


  reply	other threads:[~2025-06-13 14:22 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-05 17:11 Pratyush Yadav
2025-06-05 20:13 ` Andrew Morton
2025-06-06  8:04 ` Mike Rapoport
2025-06-06 16:23   ` Pratyush Yadav
2025-06-09 19:36     ` Mike Rapoport
2025-06-09 20:07       ` Pasha Tatashin
2025-06-10  5:44         ` Mike Rapoport
2025-06-10 11:20           ` Pasha Tatashin
2025-06-10 16:41             ` Mike Rapoport
2025-06-10 22:33               ` Pasha Tatashin
2025-06-11 13:06                 ` Pratyush Yadav
2025-06-11 13:14                   ` Pasha Tatashin
2025-06-11 13:35                     ` Mike Rapoport
2025-06-11 14:01                       ` Pratyush Yadav
2025-06-11 14:36                         ` Mike Rapoport
2025-06-13 14:22                           ` Pratyush Yadav [this message]
2025-06-13 16:21                             ` Mike Rapoport
2025-06-11 13:38                     ` Pratyush Yadav

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=mafs0wm9fn2l3.fsf@kernel.org \
    --to=pratyush@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=bhe@redhat.com \
    --cc=changyuanl@google.com \
    --cc=graf@amazon.com \
    --cc=kexec@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mclapinski@google.com \
    --cc=pasha.tatashin@soleen.com \
    --cc=rppt@kernel.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