acpi: Set proper maximum size for "etc/table-loader" blob
v3 cover acpi: Set proper maximum size for "etc/table-loader" blob
Fix and cleanup initializing the maximum size of mutable ACPI blobs.
v1/v2 -> v3:
- 'acpi: Set proper maximum size for "etc/table-loader" blob' -- Move "etc/table-loader" change to separate patch
-- Extend description
-- Fixup maximum size (now really use 64k)
- Add some patches to cleanup/refactor the code. I avoided using a new enum for the different tables for now, using the table names should be good enough and is simple.
David Hildenbrand (4):
acpi: Set proper maximum size for "etc/table-loader" blob
microvm: Don't open-code "etc/table-loader"
acpi: Move maximum size logic into acpi_add_rom_blob()
acpi: Set proper maximum size for "etc/acpi/rsdp" blob
hw/acpi/utils.c | 17 +++++++++++++++-- hw/arm/virt-acpi-build.c | 12 ++++++------ hw/i386/acpi-build.c | 7 +++---- hw/i386/acpi-microvm.c | 16 ++++++---------- include/hw/acpi/aml-build.h | 3 --- include/hw/acpi/utils.h | 3 +-- 6 files changed, 31 insertions(+), 27 deletions(-)
Signature · 1 line
2.29.2
PATCH v3 1/4 acpi: Set proper maximum size for "etc/table-loader" blob
The resizeable memory region / RAMBlock that is created for the cmd blob has a maximum size of whole host pages (e.g., 4k), because RAMBlocks work on full host pages. In addition, in i386 ACPI code:
acpi_align_size(tables->linker->cmd_blob, ACPI_BUILD_ALIGN_SIZE);
makes sure to align to multiples of 4k, padding with 0.
For example, if our cmd_blob is created with a size of 2k, the maximum size is 4k - we cannot grow beyond that. Growing might be required due to guest action when rebuilding the tables, but also on incoming migration.
This automatic generation of the maximum size used to be sufficient, however, there are cases where we cross host pages now when growing at
runtime: we exceed the maximum size of the RAMBlock and can crash QEMU when trying to resize the resizeable memory region / RAMBlock:
$ build/qemu-system-x86_64 --enable-kvm \
-machine q35,nvdimm=on \
-smp 1 \
-cpu host \
-m size=2G,slots=8,maxmem=4G \
-object memory-backend-file,id=mem0,mem-path=/tmp/nvdimm,size=256M \
-device nvdimm,label-size=131072,memdev=mem0,id=nvdimm0,slot=1 \
-nodefaults \
-device vmgenid \
-device intel-iommuResults in:
Unexpected error in qemu_ram_resize() at ../softmmu/physmem.c:1850:
qemu-system-x86_64: Size too large: /rom@etc/table-loader:
0x2000 > 0x1000: Invalid argument
In this configuration, we consume exactly 4k (32 entries, 128 bytes each) when creating the VM. However, once the guest boots up and maps the MCFG, we also create the MCFG table and end up consuming 2 additional entries (pointer + checksum) -- which is where we try resizing the memory region / RAMBlock, however, the maximum size does not allow for it.
Currently, we get the following maximum sizes for our different mutable tables based on behavior of resizeable RAMBlock:
hw table max_size
------- ---------------------------------------------------------
virt "etc/acpi/tables" ACPI_BUILD_TABLE_MAX_SIZE (0x200000) virt "etc/table-loader" HOST_PAGE_ALIGN(initial_size) virt "etc/acpi/rsdp" HOST_PAGE_ALIGN(initial_size)
i386 "etc/acpi/tables" ACPI_BUILD_TABLE_MAX_SIZE (0x200000) i386 "etc/table-loader" HOST_PAGE_ALIGN(initial_size) i386 "etc/acpi/rsdp" HOST_PAGE_ALIGN(initial_size)
microvm "etc/acpi/tables" ACPI_BUILD_TABLE_MAX_SIZE (0x200000) microvm "etc/table-loader" HOST_PAGE_ALIGN(initial_size) microvm "etc/acpi/rsdp" HOST_PAGE_ALIGN(initial_size)
Let's set the maximum table size for "etc/table-loader" to 64k, so we can properly grow at runtime, which should be good enough for the future.
Migration is not concerned with the maximum size of a RAMBlock, only with the used size - so existing setups are not affected. Of course, we cannot migrate a VM that would have crash when started on older QEMU from new QEMU to older QEMU without failing early on the destination when synchronizing the RAM state:
qemu-system-x86_64: Size too large: /rom@etc/table-loader: 0x2000 > 0x1000: Invalid argument
qemu-system-x86_64: error while loading state for instance 0x0 of device 'ram'
qemu-system-x86_64: load of migration failed: Invalid argumentWe'll refactor the code next, to make sure we get rid of this implicit behavior for "etc/acpi/rsdp" as well and to make the code easier to grasp.
--- hw/arm/virt-acpi-build.c | 3 ++- hw/i386/acpi-build.c | 3 ++- hw/i386/acpi-microvm.c | 2 +- include/hw/acpi/aml-build.h | 1 + 4 files changed, 6 insertions(+), 3 deletions(-)
hw/arm/virt-acpi-build.c +2 -1
@@ -865,7 +865,8 @@ void virt_acpi_setup(VirtMachineState *vms) build_state->linker_mr =acpi_add_rom_blob (virt_acpi_build_update, build_state, - tables.linker->cmd_blob,ACPI_BUILD_LOADER_FILE ,0 ); + tables.linker->cmd_blob,ACPI_BUILD_LOADER_FILE , +ACPI_BUILD_LOADER_MAX_SIZE );fw_cfg_add_file (vms->fw_cfg,ACPI_BUILD_TPMLOG_FILE , tables.tcpalog->data,acpi_data_len (tables.tcpalog));
hw/i386/acpi-build.c +2 -1
@@ -2524,7 +2524,8 @@ void acpi_setup(void) build_state->linker_mr =acpi_add_rom_blob (acpi_build_update, build_state, - tables.linker->cmd_blob,ACPI_BUILD_LOADER_FILE ,0 ); + tables.linker->cmd_blob,ACPI_BUILD_LOADER_FILE , +ACPI_BUILD_LOADER_MAX_SIZE );fw_cfg_add_file (x86ms->fw_cfg,ACPI_BUILD_TPMLOG_FILE , tables.tcpalog->data,acpi_data_len (tables.tcpalog));
hw/i386/acpi-microvm.c +1 -1
@@ -255,7 +255,7 @@ void acpi_setup_microvm(MicrovmMachineState *mms) ACPI_BUILD_TABLE_MAX_SIZE);acpi_add_rom_blob (acpi_build_no_update,NULL , tables.linker->cmd_blob, -"etc/table-loader" ,0 ); +"etc/table-loader" ,ACPI_BUILD_LOADER_MAX_SIZE );acpi_add_rom_blob (acpi_build_no_update,NULL , tables.rsdp,ACPI_BUILD_RSDP_FILE ,0 );
include/hw/acpi/aml-build.h +1 -0
@@ -6,6 +6,7 @@/* Reserve RAM space for tables: add another order of magnitude. */ #define ACPI_BUILD_TABLE_MAX_SIZE 0x200000 +#define ACPI_BUILD_LOADER_MAX_SIZE 0x10000#define ACPI_BUILD_APPNAME6 "BOCHS "#define ACPI_BUILD_APPNAME8 "BXPC "
Signature · 1 line
2.29.2
PATCH v3 2/4 microvm: Don't open-code "etc/table-loader"
In reply to David HildenbrandLet's just reuse ACPI_BUILD_LOADER_FILE.
--- hw/i386/acpi-microvm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i386/acpi-microvm.c +1 -1
@@ -255,7 +255,7 @@ void acpi_setup_microvm(MicrovmMachineState *mms) ACPI_BUILD_TABLE_MAX_SIZE);acpi_add_rom_blob (acpi_build_no_update,NULL , tables.linker->cmd_blob, -"etc/table-loader" ,ACPI_BUILD_LOADER_MAX_SIZE ); +ACPI_BUILD_LOADER_FILE ,ACPI_BUILD_LOADER_MAX_SIZE );acpi_add_rom_blob (acpi_build_no_update,NULL , tables.rsdp,ACPI_BUILD_RSDP_FILE ,0 );
Signature · 1 line
2.29.2
PATCH v3 3/4 acpi: Move maximum size logic into acpi_add_rom_blob()
In reply to David HildenbrandWe want to have safety margins for all tables based on the table type. Let's move the maximum size logic into acpi_add_rom_blob() and make it dependent on the table name, so we don't have to replicate for each and every instance that creates such tables.
--- hw/acpi/utils.c | 12 ++++++++++-- hw/arm/virt-acpi-build.c | 13 ++++++------- hw/i386/acpi-build.c | 8 +++----- hw/i386/acpi-microvm.c | 16 ++++++---------- include/hw/acpi/aml-build.h | 4 ---- include/hw/acpi/utils.h | 3 +-- 6 files changed, 26 insertions(+), 30 deletions(-)
hw/acpi/utils.c +10 -2
@@ -27,9 +27,17 @@#include "hw/loader.h" MemoryRegion *acpi_add_rom_blob (FWCfgCallback update,void *opaque, -GArray *blob,const char *name, -uint64_t max_size) +GArray *blob,const char *name) { +uint64_t max_size =0 ; + +/* Reserve RAM space for tables: add another order of magnitude. */ +if (!strcmp (name,ACPI_BUILD_TABLE_FILE )) { + max_size =0x200000 ; + }else if (!strcmp (name,ACPI_BUILD_LOADER_FILE )) { + max_size =0x10000 ; + } +return rom_add_blob (name, blob->data,acpi_data_len (blob), max_size,-1 , name, update, opaque,NULL , true); }
hw/arm/virt-acpi-build.c +6 -7
@@ -859,14 +859,13 @@ void virt_acpi_setup(VirtMachineState *vms)/* Now expose it all to Guest */ build_state->table_mr =acpi_add_rom_blob (virt_acpi_build_update, build_state, tables.table_data, -ACPI_BUILD_TABLE_FILE , -ACPI_BUILD_TABLE_MAX_SIZE ); +ACPI_BUILD_TABLE_FILE );assert (build_state->table_mr !=NULL ); - build_state->linker_mr = -acpi_add_rom_blob (virt_acpi_build_update, build_state, - tables.linker->cmd_blob,ACPI_BUILD_LOADER_FILE , -ACPI_BUILD_LOADER_MAX_SIZE ); + build_state->linker_mr =acpi_add_rom_blob (virt_acpi_build_update, + build_state, + tables.linker->cmd_blob, +ACPI_BUILD_LOADER_FILE );fw_cfg_add_file (vms->fw_cfg,ACPI_BUILD_TPMLOG_FILE , tables.tcpalog->data,acpi_data_len (tables.tcpalog));
@@ -880,7 +879,7 @@ void virt_acpi_setup(VirtMachineState *vms) build_state->rsdp_mr =acpi_add_rom_blob (virt_acpi_build_update, build_state, tables.rsdp, -ACPI_BUILD_RSDP_FILE ,0 ); +ACPI_BUILD_RSDP_FILE );qemu_register_reset (virt_acpi_build_reset, build_state);virt_acpi_build_reset (build_state);
hw/i386/acpi-build.c +3 -5
@@ -2518,14 +2518,12 @@ void acpi_setup(void)/* Now expose it all to Guest */ build_state->table_mr =acpi_add_rom_blob (acpi_build_update, build_state, tables.table_data, -ACPI_BUILD_TABLE_FILE , -ACPI_BUILD_TABLE_MAX_SIZE ); +ACPI_BUILD_TABLE_FILE );assert (build_state->table_mr !=NULL ); build_state->linker_mr =acpi_add_rom_blob (acpi_build_update, build_state, - tables.linker->cmd_blob,ACPI_BUILD_LOADER_FILE , -ACPI_BUILD_LOADER_MAX_SIZE ); + tables.linker->cmd_blob,ACPI_BUILD_LOADER_FILE );fw_cfg_add_file (x86ms->fw_cfg,ACPI_BUILD_TPMLOG_FILE , tables.tcpalog->data,acpi_data_len (tables.tcpalog));
@@ -2564,7 +2562,7 @@ void acpi_setup(void) build_state->rsdp =NULL ; build_state->rsdp_mr =acpi_add_rom_blob (acpi_build_update, build_state, tables.rsdp, -ACPI_BUILD_RSDP_FILE ,0 ); +ACPI_BUILD_RSDP_FILE ); }qemu_register_reset (acpi_build_reset, build_state);
hw/i386/acpi-microvm.c +6 -10
@@ -249,16 +249,12 @@ void acpi_setup_microvm(MicrovmMachineState *mms)acpi_build_microvm (&tables, mms);/* Now expose it all to Guest */ -acpi_add_rom_blob (acpi_build_no_update,NULL , - tables.table_data, -ACPI_BUILD_TABLE_FILE , -ACPI_BUILD_TABLE_MAX_SIZE ); -acpi_add_rom_blob (acpi_build_no_update,NULL , - tables.linker->cmd_blob, -ACPI_BUILD_LOADER_FILE ,ACPI_BUILD_LOADER_MAX_SIZE ); -acpi_add_rom_blob (acpi_build_no_update,NULL , - tables.rsdp, -ACPI_BUILD_RSDP_FILE ,0 ); +acpi_add_rom_blob (acpi_build_no_update,NULL , tables.table_data, +ACPI_BUILD_TABLE_FILE ); +acpi_add_rom_blob (acpi_build_no_update,NULL , tables.linker->cmd_blob, +ACPI_BUILD_LOADER_FILE ); +acpi_add_rom_blob (acpi_build_no_update,NULL , tables.rsdp, +ACPI_BUILD_RSDP_FILE );acpi_build_tables_cleanup (&tables, false); }
include/hw/acpi/aml-build.h +0 -4
@@ -4,10 +4,6 @@#include "hw/acpi/acpi-defs.h" #include "hw/acpi/bios-linker-loader.h" -/* Reserve RAM space for tables: add another order of magnitude. */ -#define ACPI_BUILD_TABLE_MAX_SIZE 0x200000 -#define ACPI_BUILD_LOADER_MAX_SIZE 0x10000 -#define ACPI_BUILD_APPNAME6 "BOCHS "#define ACPI_BUILD_APPNAME8 "BXPC "
include/hw/acpi/utils.h +1 -2
@@ -4,6 +4,5 @@#include "hw/nvram/fw_cfg.h" MemoryRegion *acpi_add_rom_blob (FWCfgCallback update,void *opaque, -GArray *blob,const char *name, -uint64_t max_size); +GArray *blob,const char *name);#endif
Signature · 1 line
2.29.2
PATCH v3 4/4 acpi: Set proper maximum size for "etc/acpi/rsdp" blob
In reply to David HildenbrandLet's also set a maximum size for "etc/acpi/rsdp", so the maximum size doesn't get implicitly set based on the initial table size. In my experiments, the table size was in the range of 22 bytes, so a single page (== what we used until now) seems to be good enough.
Now that we have defined maximum sizes for all currently used table types, let's assert that we catch usage with new tables that need a proper maximum size definition.
Also assert that our initial size does not exceed the maximum size; while qemu_ram_alloc_internal() properly asserts that the initial RAMBlock size is <= its maximum size, the result might differ when the host page size is bigger than 4k.
--- hw/acpi/utils.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
hw/acpi/utils.c +6 -1
@@ -29,14 +29,19 @@MemoryRegion *acpi_add_rom_blob (FWCfgCallback update,void *opaque,GArray *blob,const char *name) { -uint64_t max_size =0 ; +uint64_t max_size;/* Reserve RAM space for tables: add another order of magnitude. */ if (!strcmp (name,ACPI_BUILD_TABLE_FILE )) { max_size =0x200000 ; }else if (!strcmp (name,ACPI_BUILD_LOADER_FILE )) { max_size =0x10000 ; + }else if (!strcmp (name,ACPI_BUILD_RSDP_FILE )) { + max_size =0x1000 ; + }else { +g_assert_not_reached (); } +g_assert (acpi_data_len (blob) <= max_size);return rom_add_blob (name, blob->data,acpi_data_len (blob), max_size,-1 , name, update, opaque,NULL , true);
Signature · 1 line
2.29.2
Re: [PATCH v3 4/4] acpi: Set proper maximum size for "etc/acpi/rsdp" blob
Quoted 40 lines · On Thu, 4 Mar 2021 11:55:54 +0100 David Hildenbrand <[email protected]> wrote:
Let's also set a maximum size for "etc/acpi/rsdp", so the maximum size doesn't get implicitly set based on the initial table size. In my experiments, the table size was in the range of 22 bytes, so a single page (== what we used until now) seems to be good enough.
Now that we have defined maximum sizes for all currently used table types, let's assert that we catch usage with new tables that need a proper maximum size definition.
Also assert that our initial size does not exceed the maximum size; while
qemu_ram_alloc_internal()properly asserts that the initial RAMBlock size is <= its maximum size, the result might differ when the host page size is bigger than 4k.Suggested-by Laszlo Ersek <[email protected]>Cc Alistair Francis <[email protected]>Cc Paolo Bonzini <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Cc Igor Mammedov <[email protected]>Cc Peter Maydell <[email protected]>Cc Shannon Zhao <[email protected]>Cc Marcel Apfelbaum <[email protected]>Cc Paolo Bonzini <[email protected]>Cc Richard Henderson <[email protected]>Cc Laszlo Ersek <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- hw/acpi/utils.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)hw/acpi/utils.c +1
-1@@ -29,14 +29,19 @@MemoryRegion *acpi_add_rom_blob (FWCfgCallback update,void *opaque,GArray *blob,const char *name) { -uint64_t max_size =0 ; +uint64_t max_size;
[...]
hw/acpi/utils.c line 44
Quoted 4 lines
hw/acpi/utils.c +3
-0+ }else { +g_assert_not_reached (); } +g_assert (acpi_data_len (blob) <= max_size);
though it's correct,
but theoretically compiler might get unhappy about uninitialized max_size here
though if it compiles fine with our current CI it should be good enough
Quoted 3 lines
return rom_add_blob(name, blob->data, acpi_data_len(blob), max_size, -1,
name, update, opaque, NULL, true);Re: [PATCH v3 4/4] acpi: Set proper maximum size for "etc/acpi/rsdp" blob
Quoted 49 lines · Igor Mammedov
Quoted 40 lines · On Thu, 4 Mar 2021 11:55:54 +0100 David Hildenbrand <[email protected]> wrote:
Let's also set a maximum size for "etc/acpi/rsdp", so the maximum size doesn't get implicitly set based on the initial table size. In my experiments, the table size was in the range of 22 bytes, so a single page (== what we used until now) seems to be good enough.
Now that we have defined maximum sizes for all currently used table types, let's assert that we catch usage with new tables that need a proper maximum size definition.
Also assert that our initial size does not exceed the maximum size; while
qemu_ram_alloc_internal()properly asserts that the initial RAMBlock size is <= its maximum size, the result might differ when the host page size is bigger than 4k.Suggested-by Laszlo Ersek <[email protected]>Cc Alistair Francis <[email protected]>Cc Paolo Bonzini <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Cc Igor Mammedov <[email protected]>Cc Peter Maydell <[email protected]>Cc Shannon Zhao <[email protected]>Cc Marcel Apfelbaum <[email protected]>Cc Paolo Bonzini <[email protected]>Cc Richard Henderson <[email protected]>Cc Laszlo Ersek <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- hw/acpi/utils.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)hw/acpi/utils.c +1
-1@@ -29,14 +29,19 @@MemoryRegion *acpi_add_rom_blob (FWCfgCallback update,void *opaque,GArray *blob,const char *name) { -uint64_t max_size =0 ; +uint64_t max_size;[...]
hw/acpi/utils.c line 44
Quoted 4 lines
hw/acpi/utils.c +3
-0+ }else { +g_assert_not_reached (); } +g_assert (acpi_data_len (blob) <= max_size);
Quoted 4 lines
though it's correct,
but theoretically compiler might get unhappy about uninitializedmax_sizeherethough if it compiles fine with our current CI it should be good enough
I think the compiler will respect g_assert_not_reached() as intended and suppress warnings.
For example, see block/qed.c:qed_aio_write_data() where be don't have a return statement on g_assert_not_reached() exit paths.
Signature · 3 lines
Thanks, David / dhildenb
Quoted 17 lines · David Hildenbrand
Fix and cleanup initializing the maximum size of mutable ACPI blobs.
v1/v2 -> v3:
- 'acpi: Set proper maximum size for "etc/table-loader" blob' -- Move "etc/table-loader" change to separate patch
-- Extend description
-- Fixup maximum size (now really use 64k)
- Add some patches to cleanup/refactor the code. I avoided using a new enum for the different tables for now, using the table names should be good enough and is simple.David Hildenbrand (4):
acpi: Set proper maximum size for "etc/table-loader" blob
microvm: Don't open-code "etc/table-loader"
acpi: Move maximum size logic intoacpi_add_rom_blob()
acpi: Set proper maximum size for "etc/acpi/rsdp" blob
Quoted 8 lines
hw/acpi/utils.c | 17 +++++++++++++++-- hw/arm/virt-acpi-build.c | 12 ++++++------ hw/i386/acpi-build.c | 7 +++---- hw/i386/acpi-microvm.c | 16 ++++++---------- include/hw/acpi/aml-build.h | 3 --- include/hw/acpi/utils.h | 3 +-- 6 files changed, 31 insertions(+), 27 deletions(-)
series
Quoted 11 lines · David Hildenbrand
Fix and cleanup initializing the maximum size of mutable ACPI blobs.
v1/v2 -> v3:
- 'acpi: Set proper maximum size for "etc/table-loader" blob' -- Move "etc/table-loader" change to separate patch
-- Extend description
-- Fixup maximum size (now really use 64k)
- Add some patches to cleanup/refactor the code. I avoided using a new enum for the different tables for now, using the table names should be good enough and is simple.
Ping, would be nice to get this into v6.0
Signature · 3 lines
Thanks, David / dhildenb
Quoted 17 lines · On Thu, 4 Mar 2021 11:55:50 +0100 David Hildenbrand <[email protected]> wrote:
Fix and cleanup initializing the maximum size of mutable ACPI blobs.
v1/v2 -> v3:
- 'acpi: Set proper maximum size for "etc/table-loader" blob' -- Move "etc/table-loader" change to separate patch
-- Extend description
-- Fixup maximum size (now really use 64k)
- Add some patches to cleanup/refactor the code. I avoided using a new enum for the different tables for now, using the table names should be good enough and is simple.David Hildenbrand (4):
acpi: Set proper maximum size for "etc/table-loader" blob
microvm: Don't open-code "etc/table-loader"
acpi: Move maximum size logic intoacpi_add_rom_blob()
acpi: Set proper maximum size for "etc/acpi/rsdp" blob
Quoted 8 lines
hw/acpi/utils.c | 17 +++++++++++++++-- hw/arm/virt-acpi-build.c | 12 ++++++------ hw/i386/acpi-build.c | 7 +++---- hw/i386/acpi-microvm.c | 16 ++++++---------- include/hw/acpi/aml-build.h | 3 --- include/hw/acpi/utils.h | 3 +-- 6 files changed, 31 insertions(+), 27 deletions(-)