From: Pasha Tatashin <pasha.tatashin@soleen.com>
To: Mike Rapoport <rppt@kernel.org>
Cc: akpm@linux-foundation.org, brauner@kernel.org, corbet@lwn.net,
graf@amazon.com, jgg@ziepe.ca, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org, linux-mm@kvack.org,
masahiroy@kernel.org, ojeda@kernel.org, pratyush@kernel.org,
rdunlap@infradead.org, tj@kernel.org
Subject: Re: [PATCH v8 8/8] memblock: Unpreserve memory in case of error
Date: Sun, 26 Oct 2025 13:41:30 -0400 [thread overview]
Message-ID: <CA+CK2bCUc5Q5PxCy3jGN9CC48Zz_evq51d7Hps7=r9g28z7tig@mail.gmail.com> (raw)
In-Reply-To: <aP5Mcr9WCt5CHon6@kernel.org>
On Sun, Oct 26, 2025 at 12:29 PM Mike Rapoport <rppt@kernel.org> wrote:
>
> On Fri, Oct 24, 2025 at 12:10:02PM -0400, Pasha Tatashin wrote:
> > If there is an error half way through KHO memory preservation, we should
> > rollback and unpreserve everything that is partially preserved.
> >
> > Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> > Suggested-by: Pratyush Yadav <pratyush@kernel.org>
> > ---
> > mm/memblock.c | 26 ++++++++++++++++++++++----
> > 1 file changed, 22 insertions(+), 4 deletions(-)
> >
> > diff --git a/mm/memblock.c b/mm/memblock.c
> > index e3bef9b35d63..5ceaa02af7d6 100644
> > --- a/mm/memblock.c
> > +++ b/mm/memblock.c
> > @@ -2447,6 +2447,7 @@ int reserve_mem_release_by_name(const char *name)
> >
> > static int __init prepare_kho_fdt(void)
> > {
> > + bool fdt_folio_preserved = false;
>
> fdt_preserved is enough IMHO.
>
> > int err = 0, i;
> > struct page *fdt_page;
> > void *fdt;
> > @@ -2462,12 +2463,14 @@ static int __init prepare_kho_fdt(void)
> >
> > err |= fdt_begin_node(fdt, "");
> > err |= fdt_property_string(fdt, "compatible", MEMBLOCK_KHO_NODE_COMPATIBLE);
> > - for (i = 0; i < reserved_mem_count; i++) {
> > + for (i = 0; !err && i < reserved_mem_count; i++) {
> > struct reserve_mem_table *map = &reserved_mem_table[i];
> > struct page *page = phys_to_page(map->start);
> > unsigned int nr_pages = map->size >> PAGE_SHIFT;
> >
> > - err |= kho_preserve_pages(page, nr_pages);
> > + err = kho_preserve_pages(page, nr_pages);
> > + if (err)
> > + break;
>
> Please
>
> goto err_unpreserve;
While we can do that, we loose some symmetry of not performing
fdt_end_node() and fdt_finish() if fdt lib ever adds some debugging
facility to make sure that open nodes/trees are properly clodes, this
is going to flag that. I prefer my current implementation.
>
> > err |= fdt_begin_node(fdt, map->name);
> > err |= fdt_property_string(fdt, "compatible", RESERVE_MEM_KHO_NODE_COMPATIBLE);
> > err |= fdt_property(fdt, "start", &map->start, sizeof(map->start));
>
> if (err)
> goto err_unpreserve;
>
> and drop !err from the loop condition.
That is going to miss one 'nr_preserved++' . We cannot do that, we
could move it to the beginning of the loop, but I prefer keeping err
right in the condition.
>
> > @@ -2477,12 +2480,27 @@ static int __init prepare_kho_fdt(void)
> > err |= fdt_end_node(fdt);
> > err |= fdt_finish(fdt);
> >
> > - err |= kho_preserve_folio(page_folio(fdt_page));
> > -
> > if (!err)
> > + err = kho_preserve_folio(page_folio(fdt_page));
> > +
> > + if (!err) {
> > + fdt_folio_preserved = true;
> > err = kho_add_subtree(MEMBLOCK_KHO_FDT, fdt);
> > + }
> >
> > if (err) {
> > + int nr_reserve_map_preserved = i;
>
> nr_preserved is clear enough.
Sure.
> Also let's declare it before the preservation loop and count it there. Than
> we can make loop variable local which makes it safer against certain side
> channel attacks. I.e the loop that preserves the memory would be
Sure.
>
> for (unsigned int i = 0; i < reserve_mem_count; i++ nr_preserved++)
>
> > +
> > + for (i = 0; i < nr_reserve_map_preserved; i++) {
> > + struct reserve_mem_table *map = &reserved_mem_table[i];
> > + struct page *page = phys_to_page(map->start);
> > + unsigned int nr_pages = map->size >> PAGE_SHIFT;
> > +
> > + kho_unpreserve_pages(page, nr_pages);
> > + }
> > + if (fdt_folio_preserved)
> > + kho_unpreserve_folio(page_folio(fdt_page));
> > +
> > pr_err("failed to prepare memblock FDT for KHO: %d\n", err);
> > put_page(fdt_page);
> > }
> > --
> > 2.51.1.821.gb6fe4d2222-goog
> >
>
> --
> Sincerely yours,
> Mike.
next prev parent reply other threads:[~2025-10-26 17:42 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-24 16:09 [PATCH v8 0/8] liveupdate: Rework KHO for in-kernel users Pasha Tatashin
2025-10-24 16:09 ` [PATCH v8 1/8] kho: allow to drive kho from within kernel Pasha Tatashin
2025-10-24 16:09 ` [PATCH v8 2/8] kho: make debugfs interface optional Pasha Tatashin
2025-10-24 16:09 ` [PATCH v8 3/8] kho: drop notifiers Pasha Tatashin
2025-10-24 16:43 ` Pratyush Yadav
2025-10-26 16:23 ` Mike Rapoport
2025-10-24 16:09 ` [PATCH v8 4/8] kho: add interfaces to unpreserve folios and page ranges Pasha Tatashin
2025-10-24 16:09 ` [PATCH v8 5/8] kho: don't unpreserve memory during abort Pasha Tatashin
2025-10-24 16:33 ` Pratyush Yadav
2025-10-24 16:10 ` [PATCH v8 6/8] liveupdate: kho: move to kernel/liveupdate Pasha Tatashin
2025-10-24 16:10 ` [PATCH v8 7/8] liveupdate: kho: move kho debugfs directory to liveupdate Pasha Tatashin
2025-10-26 16:32 ` Mike Rapoport
2025-10-26 16:47 ` Andrew Morton
2025-10-24 16:10 ` [PATCH v8 8/8] memblock: Unpreserve memory in case of error Pasha Tatashin
2025-10-24 16:43 ` Pratyush Yadav
2025-10-26 16:29 ` Mike Rapoport
2025-10-26 17:41 ` Pasha Tatashin [this message]
2025-10-27 6:58 ` Mike Rapoport
2025-10-27 6:56 ` 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='CA+CK2bCUc5Q5PxCy3jGN9CC48Zz_evq51d7Hps7=r9g28z7tig@mail.gmail.com' \
--to=pasha.tatashin@soleen.com \
--cc=akpm@linux-foundation.org \
--cc=brauner@kernel.org \
--cc=corbet@lwn.net \
--cc=graf@amazon.com \
--cc=jgg@ziepe.ca \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=masahiroy@kernel.org \
--cc=ojeda@kernel.org \
--cc=pratyush@kernel.org \
--cc=rdunlap@infradead.org \
--cc=rppt@kernel.org \
--cc=tj@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