From: Changyuan Lyu <changyuanl@google.com>
To: rppt@kernel.org, akpm@linux-foundation.org
Cc: anthony.yznaga@oracle.com, arnd@arndb.de, ashish.kalra@amd.com,
benh@kernel.crashing.org, bp@alien8.de, catalin.marinas@arm.com,
changyuanl@google.com, corbet@lwn.net,
dave.hansen@linux.intel.com, devicetree@vger.kernel.org,
dwmw2@infradead.org, ebiederm@xmission.com, graf@amazon.com,
hpa@zytor.com, jgowans@amazon.com, kexec@lists.infradead.org,
krzk@kernel.org, linux-arm-kernel@lists.infradead.org,
linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-mm@kvack.org, luto@kernel.org, mark.rutland@arm.com,
mingo@redhat.com, pasha.tatashin@soleen.com,
pbonzini@redhat.com, peterz@infradead.org, ptyadav@amazon.de,
robh@kernel.org, rostedt@goodmis.org, saravanak@google.com,
skinsburskii@linux.microsoft.com, tglx@linutronix.de,
thomas.lendacky@amd.com, will@kernel.org, x86@kernel.org
Subject: Re: [PATCH v6 12/14] memblock: add KHO support for reserve_mem
Date: Thu, 24 Apr 2025 01:32:58 -0700 [thread overview]
Message-ID: <20250424083258.2228122-1-changyuanl@google.com> (raw)
In-Reply-To: <aAeaJ2iqkrv_ffhT@kernel.org>
On Tue, Apr 22, 2025 at 16:31:19 +0300, Mike Rapoport <rppt@kernel.org> wrote:
> On Thu, Apr 10, 2025 at 10:37:43PM -0700, Changyuan Lyu wrote:
> > [...]
> > +static struct notifier_block reserve_mem_kho_nb = {
> > + .notifier_call = reserve_mem_kho_notifier,
> > +};
> > +
> > +static void __init prepare_kho_fdt(void)
> > +{
> > + int err = 0, i;
> > + void *fdt;
> > +
> > + if (!reserved_mem_count)
> > + return;
>
> It's better to have this check in reserve_mem_init() before registering kho
> notifier.
Sounds good!
> > +
> > + kho_fdt = alloc_page(GFP_KERNEL);
> > + if (!kho_fdt) {
> > + kho_fdt = ERR_PTR(-ENOMEM);
>
> Do we really care about having errno in kho_fdt? I think NULL would work
> just fine.
I was originally using ERR_PTR(-ENOMEM) and NULL to differentiate the following
2 cases:
1. prepare_kho_fdt() failed,
2. reserved_mem_count == 0, so no memblock FDT was created.
Based on the suggestion above, since now we only register the notifier when
reserved_mem_count == 0, case 2 shall never happen. So NULL is enough.
> > + return;
>
> And actually, it makes sense to me to return -ENOMEM here and let
> reserve_mem_init() bail out before registering notifier if fdt preparation
> failed.
>
> That will save the checks in reserve_mem_kho_finalize() because it would be
> called only if we have reserve_mem areas and fdt is ready.
>
Sounds good!
> > + }
> > +
> > + fdt = page_to_virt(kho_fdt);
> > +
> > + err |= fdt_create(fdt, PAGE_SIZE);
> > + err |= fdt_finish_reservemap(fdt);
> > +
> > + err |= fdt_begin_node(fdt, "");
> > + err |= fdt_property_string(fdt, "compatible", MEMBLOCK_KHO_NODE_COMPATIBLE);
> > + for (i = 0; i < reserved_mem_count; i++) {
> > + struct reserve_mem_table *map = &reserved_mem_table[i];
> > +
> > + 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));
> > + err |= fdt_property(fdt, "size", &map->size, sizeof(map->size));
> > + err |= fdt_end_node(fdt);
> > + }
> > + err |= fdt_end_node(fdt);
> > +
> > + err |= fdt_finish(fdt);
> > +
> > + if (err) {
> > + pr_err("failed to prepare memblock FDT for KHO: %d\n", err);
> > + put_page(kho_fdt);
> > + kho_fdt = ERR_PTR(-EINVAL);
> > + }
> > +}
> > +
> > +static int __init reserve_mem_init(void)
> > +{
> > + if (!kho_is_enabled())
> > + return 0;
> > +
> > + prepare_kho_fdt();
> > +
> > + return register_kho_notifier(&reserve_mem_kho_nb);
> > +}
> > +late_initcall(reserve_mem_init);
> > +
> > +static void *kho_fdt_in __initdata;
> > +
> > +static void *__init reserve_mem_kho_retrieve_fdt(void)
> > +{
> > + phys_addr_t fdt_phys;
> > + struct folio *fdt_folio;
> > + void *fdt;
> > + int err;
> > +
> > + err = kho_retrieve_subtree(MEMBLOCK_KHO_FDT, &fdt_phys);
> > + if (err) {
> > + if (err != -ENOENT)
> > + pr_warn("failed to retrieve FDT '%s' from KHO: %d\n",
> > + MEMBLOCK_KHO_FDT, err);
> > + return ERR_PTR(err);
>
> Wouldn't just 'return NULL' work here?
If we have multiple `reserve_mem` in the kernel command line,
reserve_mem_kho_revive() will also be called multiple times. However
reserve_mem_kho_retrieve_fdt() should only be called once.
Here I am returning the ERR_PTR(err) such that if the first
reserve_mem_kho_retrieve_fdt() failed, subsequent reserve_mem_kho_revive()
can tell that reserve_mem_kho_retrieve_fdt() has failed so no need to try it
again. If we return NULL here, subsequent reserve_mem_kho_revive() would find
kho_fdt_in == NULL, and it could not tell whether it was due to previously
failed reserve_mem_kho_retrieve_fdt(), or it is the first
reserve_mem_kho_revive().
> > + }
> > +
> > + fdt_folio = kho_restore_folio(fdt_phys);
> > + if (!fdt_folio) {
> > + pr_warn("failed to restore memblock KHO FDT (0x%llx)\n", fdt_phys);
> > + return ERR_PTR(-EFAULT);
> > + }
> > +
> > + fdt = page_to_virt(folio_page(fdt_folio, 0));
>
> fdt = folio_address(folio);
Fixed.
> > +
> > + err = fdt_node_check_compatible(fdt, 0, MEMBLOCK_KHO_NODE_COMPATIBLE);
> > + if (err) {
> > + pr_warn("FDT '%s' is incompatible with '%s': %d\n",
> > + MEMBLOCK_KHO_FDT, MEMBLOCK_KHO_NODE_COMPATIBLE, err);
> > + return ERR_PTR(-EINVAL);
> > + }
> > +
> > + return fdt;
> > +}
> > +
> > +static bool __init reserve_mem_kho_revive(const char *name, phys_addr_t size,
> > + phys_addr_t align)
> > +{
> > + int err, len_start, len_size, offset;
> > + const phys_addr_t *p_start, *p_size;
> > + const void *fdt;
> > +
> > + if (!kho_fdt_in)
> > + kho_fdt_in = reserve_mem_kho_retrieve_fdt();
>
> I'd invert this and move to reserve_mem_kho_retrieve_fdt(), so there it
> would be
>
> if (kho_fdt_in)
> return kho_fdt_in;
>
> /* actually retrieve the fdt */
> kho_fdt_in = fdt;
>
> return fdt;
>
> and here
>
> fdt = reserve_mem_kho_retrieve_fdt();
> if (!fdt)
> return false;
Ah Ok, this is more elegant!
> > +
> > + if (IS_ERR(kho_fdt_in))
> > + return false;
> > +
> > + fdt = kho_fdt_in;
> > +
> >[...]
> > --
> > 2.49.0.604.gff1f9ca942-goog
> >
>
> --
> Sincerely yours,
> Mike.
Best,
Changyuan
---- 8< ----
From 7ad4379062aa9709d3702bfc53d237d0c1a4e326 Mon Sep 17 00:00:00 2001
From: Changyuan Lyu <changyuanl@google.com>
Date: Thu, 24 Apr 2025 01:10:24 -0700
Subject: [PATCH] fixup! memblock: add KHO support for reserve_mem
This patch includes the suggested changes from
https://lore.kernel.org/lkml/aAeaJ2iqkrv_ffhT@kernel.org/ and can be
squashed with "memblock: add KHO support for reserve_mem".
Fixes: 2e257a656639 ("memblock: add KHO support for reserve_mem")
Suggested-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
---
mm/memblock.c | 69 +++++++++++++++++++++++++--------------------------
1 file changed, 34 insertions(+), 35 deletions(-)
diff --git a/mm/memblock.c b/mm/memblock.c
index 3571a859f2fe1..d38a72f07ea0c 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -2490,15 +2490,6 @@ static int reserve_mem_kho_finalize(struct kho_serialization *ser)
{
int err = 0, i;
- if (!reserved_mem_count)
- return NOTIFY_DONE;
-
- if (IS_ERR(kho_fdt)) {
- err = PTR_ERR(kho_fdt);
- pr_err("memblock FDT was not prepared successfully: %d\n", err);
- return notifier_from_errno(err);
- }
-
for (i = 0; i < reserved_mem_count; i++) {
struct reserve_mem_table *map = &reserved_mem_table[i];
@@ -2528,19 +2519,14 @@ static struct notifier_block reserve_mem_kho_nb = {
.notifier_call = reserve_mem_kho_notifier,
};
-static void __init prepare_kho_fdt(void)
+static int __init prepare_kho_fdt(void)
{
int err = 0, i;
void *fdt;
- if (!reserved_mem_count)
- return;
-
kho_fdt = alloc_page(GFP_KERNEL);
- if (!kho_fdt) {
- kho_fdt = ERR_PTR(-ENOMEM);
- return;
- }
+ if (!kho_fdt)
+ return -ENOMEM;
fdt = page_to_virt(kho_fdt);
@@ -2565,18 +2551,30 @@ static void __init prepare_kho_fdt(void)
if (err) {
pr_err("failed to prepare memblock FDT for KHO: %d\n", err);
put_page(kho_fdt);
- kho_fdt = ERR_PTR(-EINVAL);
+ kho_fdt = NULL;
}
+
+ return err;
}
static int __init reserve_mem_init(void)
{
- if (!kho_is_enabled())
+ int err;
+
+ if (!kho_is_enabled() || !reserved_mem_count)
return 0;
- prepare_kho_fdt();
+ err = prepare_kho_fdt();
+ if (err)
+ return err;
+
+ err = register_kho_notifier(&reserve_mem_kho_nb);
+ if (err) {
+ put_page(kho_fdt);
+ kho_fdt = NULL;
+ }
- return register_kho_notifier(&reserve_mem_kho_nb);
+ return err;
}
late_initcall(reserve_mem_init);
@@ -2586,33 +2584,38 @@ static void *__init reserve_mem_kho_retrieve_fdt(void)
{
phys_addr_t fdt_phys;
struct folio *fdt_folio;
- void *fdt;
int err;
+ if (kho_fdt_in)
+ return kho_fdt_in;
+
err = kho_retrieve_subtree(MEMBLOCK_KHO_FDT, &fdt_phys);
if (err) {
if (err != -ENOENT)
pr_warn("failed to retrieve FDT '%s' from KHO: %d\n",
MEMBLOCK_KHO_FDT, err);
- return ERR_PTR(err);
+ goto out;
}
fdt_folio = kho_restore_folio(fdt_phys);
if (!fdt_folio) {
pr_warn("failed to restore memblock KHO FDT (0x%llx)\n", fdt_phys);
- return ERR_PTR(-EFAULT);
+ err = -EFAULT;
+ goto out;
}
- fdt = page_to_virt(folio_page(fdt_folio, 0));
+ kho_fdt_in = folio_address(fdt_folio);
- err = fdt_node_check_compatible(fdt, 0, MEMBLOCK_KHO_NODE_COMPATIBLE);
+ err = fdt_node_check_compatible(kho_fdt_in, 0, MEMBLOCK_KHO_NODE_COMPATIBLE);
if (err) {
pr_warn("FDT '%s' is incompatible with '%s': %d\n",
MEMBLOCK_KHO_FDT, MEMBLOCK_KHO_NODE_COMPATIBLE, err);
- return ERR_PTR(-EINVAL);
+ err = -EFAULT;
}
-
- return fdt;
+out:
+ if (err)
+ kho_fdt_in = ERR_PTR(err);
+ return kho_fdt_in;
}
static bool __init reserve_mem_kho_revive(const char *name, phys_addr_t size,
@@ -2622,14 +2625,10 @@ static bool __init reserve_mem_kho_revive(const char *name, phys_addr_t size,
const phys_addr_t *p_start, *p_size;
const void *fdt;
- if (!kho_fdt_in)
- kho_fdt_in = reserve_mem_kho_retrieve_fdt();
-
- if (IS_ERR(kho_fdt_in))
+ fdt = reserve_mem_kho_retrieve_fdt();
+ if (IS_ERR(fdt))
return false;
- fdt = kho_fdt_in;
-
offset = fdt_subnode_offset(fdt, 0, name);
if (offset < 0) {
pr_warn("FDT '%s' has no child '%s': %d\n",
--
2.49.0.805.g082f7c87e0-goog
next prev parent reply other threads:[~2025-04-24 8:33 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-11 5:37 [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 01/14] memblock: add MEMBLOCK_RSRV_KERN flag Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 02/14] memblock: Add support for scratch memory Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 03/14] memblock: introduce memmap_init_kho_scratch() Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 04/14] kexec: add Kexec HandOver (KHO) generation helpers Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 05/14] kexec: add KHO parsing support Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 06/14] kexec: enable KHO support for memory preservation Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 07/14] kexec: add KHO support to kexec file loads Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 08/14] kexec: add config option for KHO Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 09/14] arm64: add KHO support Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 10/14] x86/setup: use memblock_reserve_kern for memory used by kernel Changyuan Lyu
2025-04-28 22:15 ` Dave Hansen
2025-04-11 5:37 ` [PATCH v6 11/14] x86: add KHO support Changyuan Lyu
2025-04-28 22:05 ` Dave Hansen
2025-04-29 8:06 ` Mike Rapoport
2025-04-29 16:06 ` Dave Hansen
2025-04-29 16:32 ` Mike Rapoport
2025-04-29 15:53 ` Mike Rapoport
2025-04-29 16:05 ` Dave Hansen
2025-04-29 16:34 ` Mike Rapoport
2025-04-11 5:37 ` [PATCH v6 12/14] memblock: add KHO support for reserve_mem Changyuan Lyu
2025-04-22 13:31 ` Mike Rapoport
2025-04-24 8:32 ` Changyuan Lyu [this message]
2025-04-11 5:37 ` [PATCH v6 13/14] Documentation: add documentation for KHO Changyuan Lyu
2025-04-11 5:37 ` [PATCH v6 14/14] Documentation: KHO: Add memblock bindings Changyuan Lyu
2025-04-28 22:19 ` [PATCH v6 00/14] kexec: introduce Kexec HandOver (KHO) Dave Hansen
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=20250424083258.2228122-1-changyuanl@google.com \
--to=changyuanl@google.com \
--cc=akpm@linux-foundation.org \
--cc=anthony.yznaga@oracle.com \
--cc=arnd@arndb.de \
--cc=ashish.kalra@amd.com \
--cc=benh@kernel.crashing.org \
--cc=bp@alien8.de \
--cc=catalin.marinas@arm.com \
--cc=corbet@lwn.net \
--cc=dave.hansen@linux.intel.com \
--cc=devicetree@vger.kernel.org \
--cc=dwmw2@infradead.org \
--cc=ebiederm@xmission.com \
--cc=graf@amazon.com \
--cc=hpa@zytor.com \
--cc=jgowans@amazon.com \
--cc=kexec@lists.infradead.org \
--cc=krzk@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=luto@kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=pasha.tatashin@soleen.com \
--cc=pbonzini@redhat.com \
--cc=peterz@infradead.org \
--cc=ptyadav@amazon.de \
--cc=robh@kernel.org \
--cc=rostedt@goodmis.org \
--cc=rppt@kernel.org \
--cc=saravanak@google.com \
--cc=skinsburskii@linux.microsoft.com \
--cc=tglx@linutronix.de \
--cc=thomas.lendacky@amd.com \
--cc=will@kernel.org \
--cc=x86@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