hvmloader: indicate dynamically allocated memory as ACPI NVS in e820
PATCH 1/1 hvmloader: indicate dynamically allocated memory as ACPI NVS in e820
Guest kernel does need to know in some cases where the tables are located to treat these regions properly. One example is kexec process where the first kernel needs to pass firmware region locations to the second kernel which is now a requirement after 02a3e3cdb7f12 ("x86/boot: Parse SRAT table and count immovable memory regions").
The memory that hvmloader allocates in the reserved region mostly contains these useful tables and could be safely indicated as ACPI without the need to designate a sub-region specially for that. Making it non-reclaimable (ACPI NVS) in contrast with ACPI reclaim (ACPI table) memory would avoid potential reuse of this memory by the guest taking into account this region may contain runtime structures like VM86 TSS, etc. If necessary, those can be moved away later and the region marked as reclaimable.
---
Changes in v2.1:
- fixed previously missed uint32_t occurence
Changes in v2:
- gave more information on NVS type selection and potential alternatives in the description
- minor type fixes suggested
--- tools/firmware/hvmloader/e820.c | 21 +++++++++++++++++---- tools/firmware/hvmloader/util.c | 6 ++++++ tools/firmware/hvmloader/util.h | 3 +++ 3 files changed, 26 insertions(+), 4 deletions(-)
tools/firmware/hvmloader/e820.c +17 -4
@@ -155,6 +155,8 @@ int build_e820_table(struct e820entry *e820, {unsigned int nr =0 , i, j;uint32_t low_mem_end = hvm_info->low_mem_pgend <<PAGE_SHIFT ; +unsigned long firmware_mem_end = +RESERVED_MEMORY_DYNAMIC_START + (mem_mfns_allocated () <<PAGE_SHIFT );if ( !lowmem_reserved_base ) lowmem_reserved_base =0xA0000 ;
@@ -199,8 +201,19 @@ int build_e820_table(struct e820entry *e820, nr++;/* +* Mark populated reserved memory that contains ACPI and other tables as +* ACPI NVS (non-reclaimable) space - that should help the guest to treat +* it correctly later (e.g. pass to the next kernel on kexec). +*/ + + e820[nr].addr =RESERVED_MEMBASE ; + e820[nr].size = firmware_mem_end -RESERVED_MEMBASE ; + e820[nr].type =E820_NVS ; + nr++; + +/* * Explicitly reserve space for special pages. -* This space starts at RESERVED_MEMBASE an extends to cover various +* This space starts after ACPI region and extends to cover various * fixed hardware mappings (e.g., LAPIC, IOAPIC, default SVGA framebuffer). * * If igd_opregion_pgbase we need to split the RESERVED region in two.
@@ -210,8 +223,8 @@ int build_e820_table(struct e820entry *e820, {uint32_t igd_opregion_base = igd_opregion_pgbase <<PAGE_SHIFT ; - e820[nr].addr =RESERVED_MEMBASE ; - e820[nr].size = (uint32_t ) igd_opregion_base -RESERVED_MEMBASE ; + e820[nr].addr = firmware_mem_end; + e820[nr].size = igd_opregion_base - firmware_mem_end; e820[nr].type =E820_RESERVED ; nr++;
@@ -227,7 +240,7 @@ int build_e820_table(struct e820entry *e820, }else { - e820[nr].addr =RESERVED_MEMBASE ; + e820[nr].addr = firmware_mem_end; e820[nr].size = (uint32_t )-e820[nr].addr; e820[nr].type =E820_RESERVED ; nr++;
tools/firmware/hvmloader/util.c +6 -0
@@ -444,6 +444,12 @@ void mem_hole_populate_ram(xen_pfn_t mfn, uint32_t nr_mfns)static uint32_t alloc_up =RESERVED_MEMORY_DYNAMIC_START -1 ;static uint32_t alloc_down =RESERVED_MEMORY_DYNAMIC_END ; +unsigned long mem_mfns_allocated (void ) +{ +return (alloc_up >>PAGE_SHIFT ) - + ((RESERVED_MEMORY_DYNAMIC_START -1 ) >>PAGE_SHIFT ); +} +xen_pfn_t mem_hole_alloc (uint32_t nr_mfns) { alloc_down -= nr_mfns <<PAGE_SHIFT ;
tools/firmware/hvmloader/util.h +3 -0
@@ -200,6 +200,9 @@ void mem_hole_populate_ram(xen_pfn_t mfn, uint32_t nr_mfns);/* Allocate a memory hole below 4GB. */ xen_pfn_t mem_hole_alloc (uint32_t nr_mfns); +/* Return number of pages allocated */ +unsigned long mem_mfns_allocated (void ); +/* Allocate memory in a reserved region below 4GB. */ void *mem_alloc (uint32_t size,uint32_t align);#define virt_to_phys (v) ((unsigned long)(v))
Signature · 1 line
2.7.4
Quoted 5 lines · Igor Druzhinin
Guest kernel does need to know in some cases where the tables are located to treat these regions properly. One example is kexec process where the first kernel needs to pass firmware region locations to the second kernel which is now a requirement after
02a3e3cdb7f12("x86/boot: Parse SRAT table and count immovable memory regions").
Can you add a note that this is a Linux commit? Albeit there's a reference to kexec above I don't it's entirely clear it's a Linux commit.
Quoted 8 lines
The memory that hvmloader allocates in the reserved region mostly contains these useful tables and could be safely indicated as ACPI without the need to designate a sub-region specially for that. Making it non-reclaimable (ACPI NVS) in contrast with ACPI reclaim (ACPI table) memory would avoid potential reuse of this memory by the guest taking into account this region may contain runtime structures like VM86 TSS, etc. If necessary, those can be moved away later and the region marked as reclaimable.
By looking at domain_construct_memmap from libxl I think the same problem is not present on that case as regions are properly marked as RESERVED or ACPI as required?
Quoted 2 lines
Signed-off-by Igor Druzhinin <[email protected]>
Just one question below and one nit.
Quoted 50 lines
---
Changes in v2.1:
- fixed previously misseduint32_toccurenceChanges in v2:
- gave more information on NVS type selection and potential alternatives in the description
- minor type fixes suggested--- tools/firmware/hvmloader/e820.c | 21 +++++++++++++++++---- tools/firmware/hvmloader/util.c | 6 ++++++ tools/firmware/hvmloader/util.h | 3 +++ 3 files changed, 26 insertions(+), 4 deletions(-)tools/firmware/hvmloader/e820.c +14
-1@@ -155,6 +155,8 @@ int build_e820_table(struct e820entry *e820, {unsigned int nr =0 , i, j;uint32_t low_mem_end = hvm_info->low_mem_pgend <<PAGE_SHIFT ; +unsigned long firmware_mem_end = +RESERVED_MEMORY_DYNAMIC_START + (mem_mfns_allocated () <<PAGE_SHIFT );if ( !lowmem_reserved_base ) lowmem_reserved_base =0xA0000 ;@@ -199,8 +201,19 @@ int build_e820_table(struct e820entry *e820, nr++;/* +* Mark populated reserved memory that contains ACPI and other tables as +* ACPI NVS (non-reclaimable) space - that should help the guest to treat +* it correctly later (e.g. pass to the next kernel on kexec). +*/ + + e820[nr].addr =RESERVED_MEMBASE ; + e820[nr].size = firmware_mem_end -RESERVED_MEMBASE ; + e820[nr].type =E820_NVS ; + nr++; + +/* * Explicitly reserve space for special pages. -* This space starts at RESERVED_MEMBASE an extends to cover various +* This space starts after ACPI region and extends to cover various * fixed hardware mappings (e.g., LAPIC, IOAPIC, default SVGA framebuffer). * * If igd_opregion_pgbase we need to split the RESERVED region in two.
tools/firmware/hvmloader/e820.c int build_e820_table(struct e820entry *e820, · line 227
Quoted 7 lines
tools/firmware/hvmloader/e820.c +2
-2@@ -210,8 +223,8 @@ int build_e820_table(struct e820entry *e820, {uint32_t igd_opregion_base = igd_opregion_pgbase <<PAGE_SHIFT ; - e820[nr].addr =RESERVED_MEMBASE ; - e820[nr].size = (uint32_t ) igd_opregion_base -RESERVED_MEMBASE ; + e820[nr].addr = firmware_mem_end; + e820[nr].size = igd_opregion_base - firmware_mem_end;
Is there anything between firmware_mem_end and igd_opregion_base now? You already account for RESERVED_MEMBASE to firmware_mem_end.
Quoted 17 lines
tools/firmware/hvmloader/e820.c +1
-1e820[nr].type =E820_RESERVED ; nr++;@@ -227,7 +240,7 @@ int build_e820_table(struct e820entry *e820, }else { - e820[nr].addr =RESERVED_MEMBASE ; + e820[nr].addr = firmware_mem_end; e820[nr].size = (uint32_t )-e820[nr].addr; e820[nr].type =E820_RESERVED ; nr++;tools/firmware/hvmloader/util.c +0
-0
tools/firmware/hvmloader/util.c void mem_hole_populate_ram(xen_pfn_t mfn, uint32_t nr_mfns) · line 447
Quoted 4 lines
tools/firmware/hvmloader/util.c +1
-0@@ -444,6 +444,12 @@ void mem_hole_populate_ram(xen_pfn_t mfn, uint32_t nr_mfns)static uint32_t alloc_up =RESERVED_MEMORY_DYNAMIC_START -1 ;static uint32_t alloc_down =RESERVED_MEMORY_DYNAMIC_END ; +unsigned long mem_mfns_allocated (void )
mem_pages_allocated might be better.
Thanks, Roger.
Quoted 10 lines · Roger Pau Monné
Quoted 5 lines · Igor Druzhinin
Guest kernel does need to know in some cases where the tables are located to treat these regions properly. One example is kexec process where the first kernel needs to pass firmware region locations to the second kernel which is now a requirement after
02a3e3cdb7f12("x86/boot: Parse SRAT table and count immovable memory regions").Can you add a note that this is a Linux commit? Albeit there's a reference to kexec above I don't it's entirely clear it's a Linux commit.
Ok.
Quoted 9 lines
Quoted 8 lines
The memory that hvmloader allocates in the reserved region mostly contains these useful tables and could be safely indicated as ACPI without the need to designate a sub-region specially for that. Making it non-reclaimable (ACPI NVS) in contrast with ACPI reclaim (ACPI table) memory would avoid potential reuse of this memory by the guest taking into account this region may contain runtime structures like VM86 TSS, etc. If necessary, those can be moved away later and the region marked as reclaimable.
Quoted 3 lines
By looking at
domain_construct_memmapfrom libxl I think the same problem is not present on that case as regions are properly marked as RESERVED or ACPI as required?
Uhh, I simply forgot that PVH also constructs e820 - it seems it's doing it properly though. I want to makes e820 map as close as possible between PVH and HVM - let me see if I can improve my HVM version.
Quoted 58 lines
Quoted 2 lines
Signed-off-by Igor Druzhinin <[email protected]>Just one question below and one nit.
Quoted 53 lines
---
Changes in v2.1:
- fixed previously misseduint32_toccurenceChanges in v2:
- gave more information on NVS type selection and potential alternatives in the description
- minor type fixes suggested--- tools/firmware/hvmloader/e820.c | 21 +++++++++++++++++---- tools/firmware/hvmloader/util.c | 6 ++++++ tools/firmware/hvmloader/util.h | 3 +++ 3 files changed, 26 insertions(+), 4 deletions(-)tools/firmware/hvmloader/e820.c +14
-1@@ -155,6 +155,8 @@ int build_e820_table(struct e820entry *e820, {unsigned int nr =0 , i, j;uint32_t low_mem_end = hvm_info->low_mem_pgend <<PAGE_SHIFT ; +unsigned long firmware_mem_end = +RESERVED_MEMORY_DYNAMIC_START + (mem_mfns_allocated () <<PAGE_SHIFT );if ( !lowmem_reserved_base ) lowmem_reserved_base =0xA0000 ;@@ -199,8 +201,19 @@ int build_e820_table(struct e820entry *e820, nr++;/* +* Mark populated reserved memory that contains ACPI and other tables as +* ACPI NVS (non-reclaimable) space - that should help the guest to treat +* it correctly later (e.g. pass to the next kernel on kexec). +*/ + + e820[nr].addr =RESERVED_MEMBASE ; + e820[nr].size = firmware_mem_end -RESERVED_MEMBASE ; + e820[nr].type =E820_NVS ; + nr++; + +/* * Explicitly reserve space for special pages. -* This space starts at RESERVED_MEMBASE an extends to cover various +* This space starts after ACPI region and extends to cover various * fixed hardware mappings (e.g., LAPIC, IOAPIC, default SVGA framebuffer). * * If igd_opregion_pgbase we need to split the RESERVED region in two. @@ -210,8 +223,8 @@ int build_e820_table(struct e820entry *e820, {uint32_t igd_opregion_base = igd_opregion_pgbase <<PAGE_SHIFT ;
Quoted 7 lines
tools/firmware/hvmloader/e820.c int build_e820_table(struct e820entry *e820, · line 227
Quoted 4 lines
tools/firmware/hvmloader/e820.c +2
-2- e820[nr].addr =RESERVED_MEMBASE ; - e820[nr].size = (uint32_t ) igd_opregion_base -RESERVED_MEMBASE ; + e820[nr].addr = firmware_mem_end; + e820[nr].size = igd_opregion_base - firmware_mem_end;Is there anything between
firmware_mem_endandigd_opregion_basenow? You already account forRESERVED_MEMBASEtofirmware_mem_end.
It's possible that there is something in between. IGD opregion is allocated dynamically from above and occupies a couple of pages - there is a gap between the top of a populated region and it where other structures could be located. I don't want to tie e820 to the current allocation order.
Quoted 20 lines
Quoted 20 lines
tools/firmware/hvmloader/e820.c +1
-1e820[nr].type =E820_RESERVED ; nr++;@@ -227,7 +240,7 @@ int build_e820_table(struct e820entry *e820, }else { - e820[nr].addr =RESERVED_MEMBASE ; + e820[nr].addr = firmware_mem_end; e820[nr].size = (uint32_t )-e820[nr].addr; e820[nr].type =E820_RESERVED ; nr++;tools/firmware/hvmloader/util.c +0
-0@@ -444,6 +444,12 @@ void mem_hole_populate_ram(xen_pfn_t mfn, uint32_t nr_mfns)static uint32_t alloc_up =RESERVED_MEMORY_DYNAMIC_START -1 ;static uint32_t alloc_down =RESERVED_MEMORY_DYNAMIC_END ;
Quoted 3 lines
tools/firmware/hvmloader/util.c void mem_hole_populate_ram(xen_pfn_t mfn, uint32_t nr_mfns) · line 447
Quoted 1 line
tools/firmware/hvmloader/util.c +1
-0+unsigned long mem_mfns_allocated (void )
mem_pages_allocatedmight be better.
Ok, I'll see if I keep this function in a new version.
Igor
Quoted 5 lines · Igor Druzhinin
Guest kernel does need to know in some cases where the tables are located to treat these regions properly. One example is kexec process where the first kernel needs to pass firmware region locations to the second kernel which is now a requirement after
02a3e3cdb7f12("x86/boot: Parse SRAT table and count immovable memory regions").
I'm still struggling with the connection here: Reserved regions surely are "immovable" too, aren't they? Where's the connection to the E820 map in the first place - the change cited above is entirely about SRAT? And I can't imagine kexec getting away with passing on ACPI NVS regions, but not reserved ones.
Quoted 7 lines
The memory that hvmloader allocates in the reserved region mostly contains these useful tables and could be safely indicated as ACPI without the need to designate a sub-region specially for that. Making it non-reclaimable (ACPI NVS) in contrast with ACPI reclaim (ACPI table) memory would avoid potential reuse of this memory by the guest taking into account this region may contain runtime structures like VM86 TSS, etc. If necessary, those can be moved away later and the region marked as reclaimable.
Some of this may want reflecting also ...
tools/firmware/hvmloader/e820.c int build_e820_table(struct e820entry *e820, · line 212
Quoted 13 lines
tools/firmware/hvmloader/e820.c +9
-0@@ -199,8 +201,19 @@ int build_e820_table(struct e820entry *e820, nr++;/* +* Mark populated reserved memory that contains ACPI and other tables as +* ACPI NVS (non-reclaimable) space - that should help the guest to treat +* it correctly later (e.g. pass to the next kernel on kexec). +*/ + + e820[nr].addr =RESERVED_MEMBASE ; + e820[nr].size = firmware_mem_end -RESERVED_MEMBASE ; + e820[nr].type =E820_NVS ; + nr++;
... in the comment you introduce here.
Jan
Quoted 9 lines · Jan Beulich
Quoted 5 lines · Igor Druzhinin
Guest kernel does need to know in some cases where the tables are located to treat these regions properly. One example is kexec process where the first kernel needs to pass firmware region locations to the second kernel which is now a requirement after
02a3e3cdb7f12("x86/boot: Parse SRAT table and count immovable memory regions").I'm still struggling with the connection here: Reserved regions surely are "immovable" too, aren't they?
"Immovable" regions here are RAM that doesn't go away by hot-unplug. That change was necessary in Linux to avoid image randomized placement to these regions.
Quoted 5 lines
Where's the connection to
the E820 map in the first place - the change cited above is entirely about SRAT? And I can't imagine kexec getting away with passing on ACPI NVS regions, but not reserved ones.
They got away with it for as long as kexec exists I think. The point was that those reserved regions were not accessed during early boot as long as kexec kernel stays at transition tables. Now ACPI portion of it is accessed which highlighted our imprecise reporting of memory layout to the guest - which I think should be fixed either way.
I'm not going to argue if reserved regions should be mapped to transition tables or not - I don't think it's important in context related to this patch. There were already several kernel releases without that mappings and those also should be able to invoke kdump.
Igor
Quoted 20 lines · Igor Druzhinin
Quoted 9 lines · Jan Beulich
Quoted 5 lines · Igor Druzhinin
Guest kernel does need to know in some cases where the tables are located to treat these regions properly. One example is kexec process where the first kernel needs to pass firmware region locations to the second kernel which is now a requirement after
02a3e3cdb7f12("x86/boot: Parse SRAT table and count immovable memory regions").I'm still struggling with the connection here: Reserved regions surely are "immovable" too, aren't they?
"Immovable" regions here are RAM that doesn't go away by hot-unplug. That change was necessary in Linux to avoid image randomized placement to these regions.
Quoted 5 lines
Where's the connection to
the E820 map in the first place - the change cited above is entirely about SRAT? And I can't imagine kexec getting away with passing on ACPI NVS regions, but not reserved ones.
Quoted 5 lines
They got away with it for as long as kexec exists I think. The point was that those reserved regions were not accessed during early boot as long as kexec kernel stays at transition tables. Now ACPI portion of it is accessed which highlighted our imprecise reporting of memory layout to the guest - which I think should be fixed either way.
Is this to mean they map ACPI regions into the transition page tables, but not reserved regions? If so, perhaps that's what the description wants to say (and then possibly with a reference to the commit introducing this into Linux, instead of the seemingly unrelated SRAT one)?
Jan
Quoted 7 lines
I'm not going to argue if reserved regions should be mapped to transition tables or not - I don't think it's important in context related to this patch. There were already several kernel releases without that mappings and those also should be able to invoke kdump.
Igor
Quoted 21 lines · Jan Beulich
Quoted 20 lines · Igor Druzhinin
Quoted 9 lines · Jan Beulich
Quoted 5 lines · Igor Druzhinin
Guest kernel does need to know in some cases where the tables are located to treat these regions properly. One example is kexec process where the first kernel needs to pass firmware region locations to the second kernel which is now a requirement after
02a3e3cdb7f12("x86/boot: Parse SRAT table and count immovable memory regions").I'm still struggling with the connection here: Reserved regions surely are "immovable" too, aren't they?
"Immovable" regions here are RAM that doesn't go away by hot-unplug. That change was necessary in Linux to avoid image randomized placement to these regions.
Quoted 5 lines
Where's the connection to
the E820 map in the first place - the change cited above is entirely about SRAT? And I can't imagine kexec getting away with passing on ACPI NVS regions, but not reserved ones.
Quoted 8 lines
Quoted 5 lines
They got away with it for as long as kexec exists I think. The point was that those reserved regions were not accessed during early boot as long as kexec kernel stays at transition tables. Now ACPI portion of it is accessed which highlighted our imprecise reporting of memory layout to the guest - which I think should be fixed either way.
Is this to mean they map ACPI regions into the transition page tables, but not reserved regions?
Yes.
Quoted 4 lines
If so, perhaps that's what the description
wants to say (and then possibly with a reference to the commit introducing this into Linux, instead of the seemingly unrelated SRAT one)?
The referenced commit is not unrelated - it's the commit introducing the access causing kexec transition to fail. But I can add another one pointing to the mapping of ACPI tables that was supposed to fix the failure caused by the first one.
Igor
Quoted 3 lines · Igor Druzhinin
The referenced commit is not unrelated - it's the commit introducing the access causing kexec transition to fail. But I can add another one pointing to the mapping of ACPI tables that was supposed to fix the failure caused by the first one.
Oh, okay, I finally understand. The purpose of the change is unrelated, it's what get_acpi_srat_table() does which causes the problem. Hopefully this will be more obvious with the other commit also referenced. How about additionally changing "which is now a requirement after" to "which is now a requirement in order for ... to work" or "which is a prerequisite for ... actually functioning" or some such?
Jan