2024-02-02 16:35:24

by Andrew Bresticker

[permalink] [raw]
Subject: [PATCH 0/2] efi: Fixes for EFI_MEMORY_SP memory on RISC-V and ARM64

Two small fixes to enable the use soft-reserved/special-purpose memory
(EFI_MEMORY_SP) with dax_kmem on RISC-V (and ARM64, I think, though I
don't have a platform to test it on).

Patch 1 fixes a trivial integer narrowing bug. Patch 2 prevents adding
memblocks for unusable memory (including soft-resreved memory) so that it
can later be hotplugged by dax_kmem.

Tested on a RISC-V platform that presents a range of EFI_MEMORY_SP with
Bjorn's MEMORY_HOTPLUG series[0] applied.

[0]: https://lore.kernel.org/lkml/[email protected]/

Andrew Bresticker (2):
efi: runtime: Fix potential overflow of soft-reserved region size
efi: Don't add memblocks for unusable memory

drivers/firmware/efi/arm-runtime.c | 2 +-
drivers/firmware/efi/efi-init.c | 12 +-----------
drivers/firmware/efi/riscv-runtime.c | 2 +-
3 files changed, 3 insertions(+), 13 deletions(-)

--
2.34.1



2024-02-02 16:35:33

by Andrew Bresticker

[permalink] [raw]
Subject: [PATCH 1/2] efi: runtime: Fix potential overflow of soft-reserved region size

md_size will have been narrowed if we have >= 4GB worth of pages in a
soft-reserved region.

Signed-off-by: Andrew Bresticker <[email protected]>
---
drivers/firmware/efi/arm-runtime.c | 2 +-
drivers/firmware/efi/riscv-runtime.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/firmware/efi/arm-runtime.c b/drivers/firmware/efi/arm-runtime.c
index 83f5bb57fa4c..f369256c7003 100644
--- a/drivers/firmware/efi/arm-runtime.c
+++ b/drivers/firmware/efi/arm-runtime.c
@@ -107,7 +107,7 @@ static int __init arm_enable_runtime_services(void)
efi_memory_desc_t *md;

for_each_efi_memory_desc(md) {
- int md_size = md->num_pages << EFI_PAGE_SHIFT;
+ unsigned long md_size = md->num_pages << EFI_PAGE_SHIFT;
struct resource *res;

if (!(md->attribute & EFI_MEMORY_SP))
diff --git a/drivers/firmware/efi/riscv-runtime.c b/drivers/firmware/efi/riscv-runtime.c
index 09525fb5c240..9da79b8169d2 100644
--- a/drivers/firmware/efi/riscv-runtime.c
+++ b/drivers/firmware/efi/riscv-runtime.c
@@ -85,7 +85,7 @@ static int __init riscv_enable_runtime_services(void)
efi_memory_desc_t *md;

for_each_efi_memory_desc(md) {
- int md_size = md->num_pages << EFI_PAGE_SHIFT;
+ unsigned long md_size = md->num_pages << EFI_PAGE_SHIFT;
struct resource *res;

if (!(md->attribute & EFI_MEMORY_SP))
--
2.34.1


2024-02-02 16:35:50

by Andrew Bresticker

[permalink] [raw]
Subject: [PATCH 2/2] efi: Don't add memblocks for unusable memory

Adding memblocks (even if nomap) for such regions unnecessarily consumes
resources by creating struct pages for memory that may never be used or,
in the case of soft-reserved regions, prevents the memory from later
being hotplugged in by dax_kmem. This is also consistent with how x86
handles unusable memory found in the EFI memory map.

Signed-off-by: Andrew Bresticker <[email protected]>
---
drivers/firmware/efi/efi-init.c | 12 +-----------
1 file changed, 1 insertion(+), 11 deletions(-)

diff --git a/drivers/firmware/efi/efi-init.c b/drivers/firmware/efi/efi-init.c
index d4987d013080..f05bacac89b7 100644
--- a/drivers/firmware/efi/efi-init.c
+++ b/drivers/firmware/efi/efi-init.c
@@ -24,13 +24,6 @@

unsigned long __initdata screen_info_table = EFI_INVALID_TABLE_ADDR;

-static int __init is_memory(efi_memory_desc_t *md)
-{
- if (md->attribute & (EFI_MEMORY_WB|EFI_MEMORY_WT|EFI_MEMORY_WC))
- return 1;
- return 0;
-}
-
/*
* Translate a EFI virtual address into a physical address: this is necessary,
* as some data members of the EFI system table are virtually remapped after
@@ -195,12 +188,9 @@ static __init void reserve_regions(void)
memrange_efi_to_native(&paddr, &npages);
size = npages << PAGE_SHIFT;

- if (is_memory(md)) {
+ if (is_usable_memory(md)) {
early_init_dt_add_memory_arch(paddr, size);

- if (!is_usable_memory(md))
- memblock_mark_nomap(paddr, size);
-
/* keep ACPI reclaim memory intact for kexec etc. */
if (md->type == EFI_ACPI_RECLAIM_MEMORY)
memblock_reserve(paddr, size);
--
2.34.1


2024-02-02 16:38:19

by Ard Biesheuvel

[permalink] [raw]
Subject: Re: [PATCH 1/2] efi: runtime: Fix potential overflow of soft-reserved region size

Hi Andrew,

On Fri, 2 Feb 2024 at 17:34, Andrew Bresticker <[email protected]> wrote:
>
> md_size will have been narrowed if we have >= 4GB worth of pages in a
> soft-reserved region.
>
> Signed-off-by: Andrew Bresticker <[email protected]>
> ---
> drivers/firmware/efi/arm-runtime.c | 2 +-
> drivers/firmware/efi/riscv-runtime.c | 2 +-
> 2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/firmware/efi/arm-runtime.c b/drivers/firmware/efi/arm-runtime.c
> index 83f5bb57fa4c..f369256c7003 100644
> --- a/drivers/firmware/efi/arm-runtime.c
> +++ b/drivers/firmware/efi/arm-runtime.c
> @@ -107,7 +107,7 @@ static int __init arm_enable_runtime_services(void)
> efi_memory_desc_t *md;
>
> for_each_efi_memory_desc(md) {
> - int md_size = md->num_pages << EFI_PAGE_SHIFT;
> + unsigned long md_size = md->num_pages << EFI_PAGE_SHIFT;

Better use u64 here, and below as well.


> struct resource *res;
>
> if (!(md->attribute & EFI_MEMORY_SP))
> diff --git a/drivers/firmware/efi/riscv-runtime.c b/drivers/firmware/efi/riscv-runtime.c
> index 09525fb5c240..9da79b8169d2 100644
> --- a/drivers/firmware/efi/riscv-runtime.c
> +++ b/drivers/firmware/efi/riscv-runtime.c
> @@ -85,7 +85,7 @@ static int __init riscv_enable_runtime_services(void)
> efi_memory_desc_t *md;
>
> for_each_efi_memory_desc(md) {
> - int md_size = md->num_pages << EFI_PAGE_SHIFT;
> + unsigned long md_size = md->num_pages << EFI_PAGE_SHIFT;
> struct resource *res;
>
> if (!(md->attribute & EFI_MEMORY_SP))
> --
> 2.34.1
>
>

2024-02-02 16:45:43

by Ard Biesheuvel

[permalink] [raw]
Subject: Re: [PATCH 2/2] efi: Don't add memblocks for unusable memory

On Fri, 2 Feb 2024 at 17:34, Andrew Bresticker <[email protected]> wrote:
>
> Adding memblocks (even if nomap) for such regions unnecessarily consumes
> resources by creating struct pages for memory that may never be used or,
> in the case of soft-reserved regions, prevents the memory from later
> being hotplugged in by dax_kmem. This is also consistent with how x86
> handles unusable memory found in the EFI memory map.
>

x86 doesn't care as much about memory vs device semantics as ARM does.

This affects the output of memblock_is_[region_]memory(), so we'd have
to double check that none of those uses get broken by this.

If the soft reserved regions need to be omitted from memblock, we can
deal with that separately perhaps, but changing it at this level seems
inappropriate to me.


> Signed-off-by: Andrew Bresticker <[email protected]>
> ---
> drivers/firmware/efi/efi-init.c | 12 +-----------
> 1 file changed, 1 insertion(+), 11 deletions(-)
>
> diff --git a/drivers/firmware/efi/efi-init.c b/drivers/firmware/efi/efi-init.c
> index d4987d013080..f05bacac89b7 100644
> --- a/drivers/firmware/efi/efi-init.c
> +++ b/drivers/firmware/efi/efi-init.c
> @@ -24,13 +24,6 @@
>
> unsigned long __initdata screen_info_table = EFI_INVALID_TABLE_ADDR;
>
> -static int __init is_memory(efi_memory_desc_t *md)
> -{
> - if (md->attribute & (EFI_MEMORY_WB|EFI_MEMORY_WT|EFI_MEMORY_WC))
> - return 1;
> - return 0;
> -}
> -
> /*
> * Translate a EFI virtual address into a physical address: this is necessary,
> * as some data members of the EFI system table are virtually remapped after
> @@ -195,12 +188,9 @@ static __init void reserve_regions(void)
> memrange_efi_to_native(&paddr, &npages);
> size = npages << PAGE_SHIFT;
>
> - if (is_memory(md)) {
> + if (is_usable_memory(md)) {
> early_init_dt_add_memory_arch(paddr, size);
>
> - if (!is_usable_memory(md))
> - memblock_mark_nomap(paddr, size);
> -
> /* keep ACPI reclaim memory intact for kexec etc. */
> if (md->type == EFI_ACPI_RECLAIM_MEMORY)
> memblock_reserve(paddr, size);
> --
> 2.34.1
>

2024-02-02 18:13:51

by Andrew Bresticker

[permalink] [raw]
Subject: Re: [PATCH 2/2] efi: Don't add memblocks for unusable memory

On Fri, Feb 2, 2024 at 11:45 AM Ard Biesheuvel <[email protected]> wrote:
>
> On Fri, 2 Feb 2024 at 17:34, Andrew Bresticker <[email protected]> wrote:
> >
> > Adding memblocks (even if nomap) for such regions unnecessarily consumes
> > resources by creating struct pages for memory that may never be used or,
> > in the case of soft-reserved regions, prevents the memory from later
> > being hotplugged in by dax_kmem. This is also consistent with how x86
> > handles unusable memory found in the EFI memory map.
> >
>
> x86 doesn't care as much about memory vs device semantics as ARM does.
>
> This affects the output of memblock_is_[region_]memory(), so we'd have
> to double check that none of those uses get broken by this.
>
> If the soft reserved regions need to be omitted from memblock, we can
> deal with that separately perhaps, but changing it at this level seems
> inappropriate to me.

Sure, I can constrain this to just the soft-reserved regions.

-Andrew

>
>
> > Signed-off-by: Andrew Bresticker <[email protected]>
> > ---
> > drivers/firmware/efi/efi-init.c | 12 +-----------
> > 1 file changed, 1 insertion(+), 11 deletions(-)
> >
> > diff --git a/drivers/firmware/efi/efi-init.c b/drivers/firmware/efi/efi-init.c
> > index d4987d013080..f05bacac89b7 100644
> > --- a/drivers/firmware/efi/efi-init.c
> > +++ b/drivers/firmware/efi/efi-init.c
> > @@ -24,13 +24,6 @@
> >
> > unsigned long __initdata screen_info_table = EFI_INVALID_TABLE_ADDR;
> >
> > -static int __init is_memory(efi_memory_desc_t *md)
> > -{
> > - if (md->attribute & (EFI_MEMORY_WB|EFI_MEMORY_WT|EFI_MEMORY_WC))
> > - return 1;
> > - return 0;
> > -}
> > -
> > /*
> > * Translate a EFI virtual address into a physical address: this is necessary,
> > * as some data members of the EFI system table are virtually remapped after
> > @@ -195,12 +188,9 @@ static __init void reserve_regions(void)
> > memrange_efi_to_native(&paddr, &npages);
> > size = npages << PAGE_SHIFT;
> >
> > - if (is_memory(md)) {
> > + if (is_usable_memory(md)) {
> > early_init_dt_add_memory_arch(paddr, size);
> >
> > - if (!is_usable_memory(md))
> > - memblock_mark_nomap(paddr, size);
> > -
> > /* keep ACPI reclaim memory intact for kexec etc. */
> > if (md->type == EFI_ACPI_RECLAIM_MEMORY)
> > memblock_reserve(paddr, size);
> > --
> > 2.34.1
> >