numa: add 'memmap-type' option for memory type configuration
v5 cover numa: add 'memmap-type' option for memory type configuration
Hi all,
This is v5 of the SPM (Specific Purpose Memory) patch. Thank you for the feedback on v4.
Changes in v5:
- Renamed 'spm=on|off' to 'memmap-type=normal|spm|reserved' enum (per Gregory's suggestion for better extensibility)
- Updated QAPI schema with new NumaMemmapType enum
- Updated documentation to describe SPM as a policy hint (per Jonathan's feedback)
Use case:
This feature allows marking NUMA node memory as Specific Purpose Memory (SPM) or reserved in the E820 table. SPM serves as a hint to the guest that this memory might be managed by device drivers based on guest policy
Example usage:
-object memory-backend-ram,size=8G,id=m0
-object memory-backend-memfd,size=8G,id=m1
-numa node,nodeid=0,memdev=m0
-numa node,nodeid=1,memdev=m1,memmap-type=spm
Supported memmap-type values:
- normal: Regular system RAM (E820 type 1, default)
- spm: Specific Purpose Memory (E820 type 0xEFFFFFFF), a hint
that this memory might be managed by device drivers
- reserved: Reserved memory (E820 type 2), not usable as RAM
Please review. Thanks!
Best regards,
Jerry Huang
Signature · 1 line
2.34.1
PATCH v5 1/1 numa: add 'memmap-type' option for memory type configuration
Add a 'memmap-type' option to NUMA node configuration that allows specifying the memory type for a NUMA node.
Supported values:
- normal: Regular system RAM (E820 type 1, default)
- spm: Specific Purpose Memory (E820 type 0xEFFFFFFF)
- reserved: Reserved memory (E820 type 2)
The 'spm' type indicates Specific Purpose Memory - a hint to the guest that this memory might be managed by device drivers based on guest policy. The 'reserved' type marks memory as not usable as RAM.
Note: This option is only supported on x86 platforms.
Usage:
-numa node,nodeid=1,memdev=m1,memmap-type=spm
--- hw/core/numa.c | 19 ++++++++++ hw/i386/e820_memory_layout.c | 72 ++++++++++++++++++++++++++++++++++++ hw/i386/e820_memory_layout.h | 12 +++--- hw/i386/pc.c | 61 ++++++++++++++++++++++++++++++ include/system/numa.h | 7 ++++ qapi/machine.json | 24 ++++++++++++ qemu-options.hx | 14 ++++++- 7 files changed, 202 insertions(+), 7 deletions(-)
hw/core/numa.c +19 -0
@@ -38,6 +38,7 @@#include "hw/mem/pc-dimm.h" #include "hw/core/boards.h" #include "hw/mem/memory-device.h" +#include "hw/i386/x86.h" #include "qemu/option.h" #include "qemu/config-file.h" #include "qemu/cutils.h"
@@ -164,6 +165,24 @@ static void parse_numa_node(MachineState *ms, NumaNodeOptions *node, numa_info[nodenr].node_memdev =MEMORY_BACKEND (o); } +if (node->has_memmap_type && node->memmap_type !=NUMA_MEMMAP_TYPE_NORMAL ) { +if (!object_dynamic_cast (OBJECT (ms),TYPE_X86_MACHINE )) { +error_setg (errp,"memmap-type=%s is only supported on x86 machines" , +NumaMemmapType_str (node->memmap_type)); +return ; + } +switch (node->memmap_type) { +case NUMA_MEMMAP_TYPE_SPM : + numa_info[nodenr].memmap_type =NUMA_MEMMAP_SPM ; +break ; +case NUMA_MEMMAP_TYPE_RESERVED : + numa_info[nodenr].memmap_type =NUMA_MEMMAP_RESERVED ; +break ; +default : +break ; + } + } + numa_info[nodenr].present = true; max_numa_nodeid =MAX (max_numa_nodeid, nodenr +1 ); ms->numa_state->num_nodes++;
hw/i386/e820_memory_layout.c +72 -0
75 lines not on this page · raw message
hw/i386/e820_memory_layout.h +7 -5
@@ -10,11 +10,12 @@#define HW_I386_E820_MEMORY_LAYOUT_H /* e820 types */ -#define E820_RAM 1 -#define E820_RESERVED 2 -#define E820_ACPI 3 -#define E820_NVS 4 -#define E820_UNUSABLE 5 +#define E820_RAM 1 +#define E820_RESERVED 2 +#define E820_ACPI 3 +#define E820_NVS 4 +#define E820_UNUSABLE 5 +#define E820_SOFT_RESERVED 0xEFFFFFFFstruct e820_entry {uint64_t address;
@@ -26,5 +27,6 @@ void e820_add_entry(uint64_t address, uint64_t length, uint32_t type);bool e820_get_entry (int index,uint32_t type,uint64_t *address,uint64_t *length);int e820_get_table (struct e820_entry **table); +bool e820_update_entry_type (uint64_t start,uint64_t length,uint32_t new_type);#endif
hw/i386/pc.c +61 -0
73 lines not on this page · raw message
include/system/numa.h +7 -0
@@ -35,12 +35,19 @@ enum {#define UINT16_BITS 16 +typedef enum { +NUMA_MEMMAP_NORMAL =0 , +NUMA_MEMMAP_SPM , +NUMA_MEMMAP_RESERVED , +}NumaMemmapTypeInternal ; + typedef struct NodeInfo { uint64_t node_mem; struct HostMemoryBackend *node_memdev; bool present; bool has_cpu; bool has_gi; + NumaMemmapTypeInternal memmap_type; uint8_t lb_info_provided; uint16_t initiator; uint8_t distance[MAX_NODES];
qapi/machine.json +24 -0
@@ -464,6 +464,22 @@ { 'enum': 'NumaOptionsType', 'data': [ 'node', 'dist', 'cpu', 'hmat-lb', 'hmat-cache' ] } +## +# @NumaMemmapType: +# +# Memory mapping type for a NUMA node. +# +# @normal: Normal system RAM (E820 type 1) +# +# @spm: Specific Purpose Memory (E820 type 0xEFFFFFFF) +# +# @reserved: Reserved memory (E820 type 2) +# +# Since: 10.2 +## +{ 'enum': 'NumaMemmapType', + 'data': ['normal', 'spm', 'reserved'] } + ## # @NumaOptions: #
@@ -500,6 +516,13 @@ # @memdev: memory backend object. If specified for one node, it must # be specified for all nodes. # +# @memmap-type: specifies the memory type for this NUMA node. +# 'normal' (default) is regular system RAM. +# 'spm' is Specific Purpose Memory - a hint to the guest that +# this memory might be managed by device drivers based on policy. +# 'reserved' is reserved memory, not usable as RAM. +# Currently only supported on x86. (since 10.2) +# # @initiator: defined in ACPI 6.3 Chapter 5.2.27.3 Table 5-145, points # to the nodeid which has the memory controller responsible for # this NUMA node. This field provides additional information as
@@ -514,6 +537,7 @@ '*cpus': ['uint16'], '*mem': 'size', '*memdev': 'str', + '*memmap-type': 'NumaMemmapType', '*initiator': 'uint16' }} ##
qemu-options.hx +12 -2
@@ -433,7 +433,7 @@ ERST DEF("numa", HAS_ARG, QEMU_OPTION_numa, "-numa node[,mem=size][,cpus=firstcpu[-lastcpu]][,nodeid=node][,initiator=node]\n" - "-numa node[,memdev=id][,cpus=firstcpu[-lastcpu]][,nodeid=node][,initiator=node]\n" + "-numa node[,memdev=id][,cpus=firstcpu[-lastcpu]][,nodeid=node][,initiator=node][,memmap-type=normal|spm|reserved]\n" "-numa dist,src=source,dst=destination,val=distance\n" "-numa cpu,node-id=node[,socket-id=x][,core-id=y][,thread-id=z]\n" "-numa hmat-lb,initiator=node,target=node,hierarchy=memory|first-level|second-level|third-level,data-type=access-latency|read-latency|write-latency[,latency=lat][,bandwidth=bw]\n"
@@ -442,7 +442,7 @@ DEF("numa", HAS_ARG, QEMU_OPTION_numa, SRST ``-numa node[,mem=size][,cpus=firstcpu[-lastcpu]][,nodeid=node][,initiator=initiator]`` \ -``-numa node[,memdev=id][,cpus=firstcpu[-lastcpu]][,nodeid=node][,initiator=initiator]`` +``-numa node[,memdev=id][,cpus=firstcpu[-lastcpu]][,nodeid=node][,initiator=initiator][,memmap-type=type]`` \ ``-numa dist,src=source,dst=destination,val=distance`` \
@@ -510,6 +510,16 @@ SRST largest bandwidth) to this NUMA node. Note that this option can be set only when the machine property 'hmat' is set to 'on'. + '\ ``memmap-type``\ ' specifies the memory type for this NUMA node: + + - ``normal`` (default): Regular system RAM (E820 type 1) + - ``spm``: Specific Purpose Memory (E820 type 0xEFFFFFFF). This is a + hint to the guest that the memory might be managed by device drivers + based on guest policy. + - ``reserved``: Reserved memory (E820 type 2), not usable as RAM. + + This option is only supported on x86 platforms. + Following example creates a machine with 2 NUMA nodes, node 0 has CPU. node 1 has only memory, and its initiator is node 0. Note that because node 0 has CPU, by default the initiator of node 0 is itself
Signature · 1 line
2.34.1
Quoted 13 lines · fanhuang
hw/i386/e820_memory_layout.c +13
-0+bool e820_update_entry_type (uint64_t start,uint64_t length,uint32_t new_type) +{ +uint64_t end = start + length; +assert (!e820_done); + +/* For E820_SOFT_RESERVED, validate range is within E820_RAM */ +if (new_type ==E820_SOFT_RESERVED ) { +bool range_in_ram = false; + +for (size_t j =0 ; j < e820_entries; j++) { +uint64_t ram_start =le64_to_cpu (e820_table[j].address); +uint64_t ram_end = ram_start +le64_to_cpu (e820_table[j].length); +uint32_t ram_type =le32_to_cpu (e820_table[j].type);
hw/i386/e820_memory_layout.c bool e820_get_entry(int idx, uint32_t type, uint64_t *address, uint64_t *length) · line 72
Quoted 10 lines
hw/i386/e820_memory_layout.c +10
-0+ +if (ram_type ==E820_RAM && ram_start <= start && ram_end >= end) { + range_in_ram = true; +break ; + } + } +if (!range_in_ram) { +return false; + } + }
------- nit --------
this can be shortened: (j == e820_entries) implies not found.
if (new_type == E820_SOFT_RESERVED) {
for (size_t j = 0; j < e820_entries; j++) {
uint64_t ram_start = le64_to_cpu(e820_table[j].address);
uint64_t ram_end = ram_start + le64_to_cpu(e820_table[j].length);
uint32_t ram_type = le32_to_cpu(e820_table[j].type);
if (ram_type == E820_RAM && ram_start <= start && ram_end >= end)
break;
}
if j == e820_entries: /* Not found */
return false;
}Haven't looked at the surrounding code, this seems like you might define
static bool e820_has_ram_region(start, end) { /* above code */ }
then the 820_update_entry_type lines can just be
if (new_type == E820_SOFT_RESERVED &&
!e820_has_ram_region(start, end))
return false;only worth it if e820_has_ram_region() deduplicates other stuff.
-------------------
I don't see anything else immediately concerning and the above is more of a nit than anything.
I haven't tested this, you might at least add the OS-facing results to the changelog so someone can replicate your setup.
For example the early-boot print showing E820 and ACPI SRAT data.
[reasonable memdev definitions]
-numa node,nodeid=0,memdev=m0
-numa node,nodeid=1,memdev=m1,memmap-type=spm -numa node,nodeid=2,memdev=m2,memmap-type=reserved
resulting in:
[ 0.000000] BIOS-provided physical RAM map: [ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x00000000afffffff] usable [ 0.000000] BIOS-e820: [mem 0x0000000100000000-0x000000c04fffffff] usable [ 0.000000] BIOS-e820: [mem 0x000000c050000000-0x000001004fffffff] soft reserved [ 0.000000] BIOS-e820: [mem 0x0000010050000000-0x000001006fffffff] reserved
[ 0.003409] ACPI: SRAT: Node 0 PXM 0 [mem 0x00000000-0xafffffff] [ 0.003410] ACPI: SRAT: Node 0 PXM 0 [mem 0x100000000-0xc04fffffff] [ 0.003411] ACPI: SRAT: Node 1 PXM 1 [mem 0xc050000000-0x1004fffffff] hotplug [ 0.003411] ACPI: SRAT: Node 2 PXM 2 [mem 0x10050000000-0x1006fffffff] hotplug
(fake entries above)
(i don't actually know if RESERVED gets added to SRAT)
This would at least show the node-association and the types are correct.
Thanks,
~Gregory
Quoted 79 lines · Gregory Price
Quoted 13 lines · fanhuang
hw/i386/e820_memory_layout.c +13
-0+bool e820_update_entry_type (uint64_t start,uint64_t length,uint32_t new_type) +{ +uint64_t end = start + length; +assert (!e820_done); + +/* For E820_SOFT_RESERVED, validate range is within E820_RAM */ +if (new_type ==E820_SOFT_RESERVED ) { +bool range_in_ram = false; + +for (size_t j =0 ; j < e820_entries; j++) { +uint64_t ram_start =le64_to_cpu (e820_table[j].address); +uint64_t ram_end = ram_start +le64_to_cpu (e820_table[j].length); +uint32_t ram_type =le32_to_cpu (e820_table[j].type);hw/i386/e820_memory_layout.c bool e820_get_entry(int idx, uint32_t type, uint64_t *address, uint64_t *length) · line 72
Quoted 10 lines
hw/i386/e820_memory_layout.c +10
-0+ +if (ram_type ==E820_RAM && ram_start <= start && ram_end >= end) { + range_in_ram = true; +break ; + } + } +if (!range_in_ram) { +return false; + } + }------- nit --------
this can be shortened: (j ==e820_entries) implies not found.if (new_type == E820_SOFT_RESERVED) { for (size_t j = 0; j < e820_entries; j++) { uint64_t ram_start = le64_to_cpu(e820_table[j].address); uint64_t ram_end = ram_start + le64_to_cpu(e820_table[j].length); uint32_t ram_type = le32_to_cpu(e820_table[j].type); if (ram_type == E820_RAM && ram_start <= start && ram_end >= end) break; } if j == e820_entries: /* Not found */ return false; }Haven't looked at the surrounding code, this seems like you might define
static bool e820_has_ram_region(start, end) { /* above code */ }then the 820_update_entry_type lines can just be
if (new_type == E820_SOFT_RESERVED && !e820_has_ram_region(start, end)) return false;only worth it if
e820_has_ram_region()deduplicates other stuff.
-------------------I don't see anything else immediately concerning and the above is more of a nit than anything.
I haven't tested this, you might at least add the OS-facing results to the changelog so someone can replicate your setup.
For example the early-boot print showing E820 and ACPI SRAT data.
[reasonable memdev definitions]
-numa node,nodeid=0,memdev=m0
-numa node,nodeid=1,memdev=m1,memmap-type=spm -numa node,nodeid=2,memdev=m2,memmap-type=reservedresulting in:
[ 0.000000] BIOS-provided physical RAM map: [ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x00000000afffffff] usable [ 0.000000] BIOS-e820: [mem 0x0000000100000000-0x000000c04fffffff] usable [ 0.000000] BIOS-e820: [mem 0x000000c050000000-0x000001004fffffff] soft reserved [ 0.000000] BIOS-e820: [mem 0x0000010050000000-0x000001006fffffff] reserved[ 0.003409] ACPI: SRAT: Node 0 PXM 0 [mem 0x00000000-0xafffffff] [ 0.003410] ACPI: SRAT: Node 0 PXM 0 [mem 0x100000000-0xc04fffffff] [ 0.003411] ACPI: SRAT: Node 1 PXM 1 [mem 0xc050000000-0x1004fffffff] hotplug [ 0.003411] ACPI: SRAT: Node 2 PXM 2 [mem 0x10050000000-0x1006fffffff] hotplug
Quoted 7 lines
(fake entries above)
(i don't actually know if RESERVED gets added to SRAT)This would at least show the node-association and the types are correct.
Thanks,
~Gregory
Hi Gregory,
Thanks for the review and feedback on v5!
Regarding your suggestions:
1. Simplifying the range_in_ram check:
I've updated the code following your suggestion, with one adjustment - since the loop variable 'j' is declared with block scope in C99/C11, it needs to be declared outside the for loop to remain accessible after the loop exits:
if (new_type == E820_SOFT_RESERVED) {
size_t j;
for (j = 0; j < e820_entries; j++) {
uint64_t ram_start = le64_to_cpu(e820_table[j].address);
uint64_t ram_end = ram_start + le64_to_cpu(e820_table[j].length);
uint32_t ram_type = le32_to_cpu(e820_table[j].type); if (ram_type == E820_RAM && ram_start <= start && ram_end >= end)
break;
}
if (j == e820_entries) /* Not found */
return false;}
This saves a variable, though it slightly trades off readability. Would you prefer this approach, or do you think the original 'range_in_ram' boolean makes the intent clearer? Happy to go either way.
2. Extracting e820_has_ram_region() helper:
I searched the codebase and found that the other places using E820_RAM (fw_cfg.c, tdx.c) are iterating to collect all RAM entries, rather than checking if a range falls within RAM. Since there's no other code that would benefit from this helper, I'll keep it inline for now. If similar logic is needed elsewhere in the future, we can refactor then.
3. Adding OS-facing test results:
Great suggestion! I've replied the E820 and SRAT output examples to the v5 cover letter, so reviewers can verify the expected behavior.
Thanks,
Jerry
Quoted 29 lines · fanhuang
Hi all,
This is v5 of the SPM (Specific Purpose Memory) patch. Thank you for the feedback on v4.
Changes in v5:
- Renamed 'spm=on|off' to 'memmap-type=normal|spm|reserved' enum (per Gregory's suggestion for better extensibility)
- Updated QAPI schema with new NumaMemmapType enum
- Updated documentation to describe SPM as a policy hint (per Jonathan's feedback)Use case:
This feature allows marking NUMA node memory as Specific Purpose Memory (SPM) or reserved in the E820 table. SPM serves as a hint to the guest that this memory might be managed by device drivers based on guest policyExample usage:
-object memory-backend-ram,size=8G,id=m0
-object memory-backend-memfd,size=8G,id=m1
-numa node,nodeid=0,memdev=m0
-numa node,nodeid=1,memdev=m1,memmap-type=spmSupported memmap-type values:
- normal: Regular system RAM (E820 type 1, default)
- spm: Specific Purpose Memory (E820 type0xEFFFFFFF), a hint
that this memory might be managed by device drivers
- reserved: Reserved memory (E820 type 2), not usable as RAM
Quoted 5 lines
Please review. Thanks!
Best regards,
Jerry Huang
Hi all,
Here are the OS-facing test results showing E820 and ACPI SRAT data for different memmap-type configurations:
1. memmap-type=spm
~~~~~~~~~~~~~~~~~~
-numa node,cpus=4-7,nodeid=1,memdev=m1,memmap-type=spm
Guest dmesg output:
[ 0.000000] BIOS-e820: [mem 0x0000000100000000-0x000000027fffffff] usable [ 0.000000] BIOS-e820: [mem 0x0000000280000000-0x000000047fffffff] soft reserved [ 0.000000] BIOS-e820: [mem 0x000000fd00000000-0x000000ffffffffff]
reserved
[ 0.042578] ACPI: SRAT: Node 0 PXM 0 [mem 0x00000000-0x0009ffff] [ 0.042580] ACPI: SRAT: Node 0 PXM 0 [mem 0x00100000-0x7fffffff] [ 0.042581] ACPI: SRAT: Node 0 PXM 0 [mem 0x100000000-0x27fffffff] [ 0.042582] ACPI: SRAT: Node 1 PXM 1 [mem 0x280000000-0x47fffffff]
2. memmap-type=reserved
~~~~~~~~~~~~~~~~~~~~~~~
-numa node,cpus=4-7,nodeid=1,memdev=m1,memmap-type=reserved
Guest dmesg output:
[ 0.000000] BIOS-e820: [mem 0x0000000100000000-0x000000027fffffff] usable [ 0.000000] BIOS-e820: [mem 0x0000000280000000-0x000000047fffffff] reserved [ 0.000000] BIOS-e820: [mem 0x000000fd00000000-0x000000ffffffffff]
reserved
[ 0.042728] ACPI: SRAT: Node 0 PXM 0 [mem 0x00000000-0x0009ffff] [ 0.042729] ACPI: SRAT: Node 0 PXM 0 [mem 0x00100000-0x7fffffff] [ 0.042731] ACPI: SRAT: Node 0 PXM 0 [mem 0x100000000-0x27fffffff] [ 0.042732] ACPI: SRAT: Node 1 PXM 1 [mem 0x280000000-0x47fffffff]
3. normal (default, no memmap-type specified)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-numa node,cpus=4-7,nodeid=1,memdev=m1
Guest dmesg output:
[ 0.000000] BIOS-e820: [mem 0x0000000100000000-0x000000047fffffff] usable [ 0.000000] BIOS-e820: [mem 0x000000fd00000000-0x000000ffffffffff]
reserved
[ 0.042413] ACPI: SRAT: Node 0 PXM 0 [mem 0x00000000-0x0009ffff] [ 0.042414] ACPI: SRAT: Node 0 PXM 0 [mem 0x00100000-0x7fffffff] [ 0.042416] ACPI: SRAT: Node 0 PXM 0 [mem 0x100000000-0x27fffffff] [ 0.042417] ACPI: SRAT: Node 1 PXM 1 [mem 0x280000000-0x47fffffff]
The results show:
- Node association is correct (Node 1 at 0x280000000-0x47fffffff)
- E820 types are correctly applied (usable/soft reserved/reserved)
- SRAT entries are generated for all configurations