Re: [PULL 10/13] numa: Enable numa for SGX EPC sections

2022-01-13 Thread Daniel P . Berrangé
On Wed, Dec 15, 2021 at 09:25:12PM +0100, Paolo Bonzini wrote:
> From: Yang Zhong 
> 
> The basic SGX did not enable numa for SGX EPC sections, which
> result in all EPC sections located in numa node 0. This patch
> enable SGX numa function in the guest and the EPC section can
> work with RAM as one numa node.
> 
> The Guest kernel related log:
> [0.009981] ACPI: SRAT: Node 0 PXM 0 [mem 0x18000-0x183ff]
> [0.009982] ACPI: SRAT: Node 1 PXM 1 [mem 0x18400-0x185bf]
> The SRAT table can normally show SGX EPC sections menory info in different
> numa nodes.
> 
> The SGX EPC numa related command:
>  ..
>  -m 4G,maxmem=20G \
>  -smp sockets=2,cores=2 \
>  -cpu host,+sgx-provisionkey \
>  -object memory-backend-ram,size=2G,host-nodes=0,policy=bind,id=node0 \
>  -object 
> memory-backend-epc,id=mem0,size=64M,prealloc=on,host-nodes=0,policy=bind \
>  -numa node,nodeid=0,cpus=0-1,memdev=node0 \
>  -object memory-backend-ram,size=2G,host-nodes=1,policy=bind,id=node1 \
>  -object 
> memory-backend-epc,id=mem1,size=28M,prealloc=on,host-nodes=1,policy=bind \
>  -numa node,nodeid=1,cpus=2-3,memdev=node1 \
>  -M 
> sgx-epc.0.memdev=mem0,sgx-epc.0.node=0,sgx-epc.1.memdev=mem1,sgx-epc.1.node=1 
> \
>  ..
> 
> Signed-off-by: Yang Zhong 
> Message-Id: <20211101162009.62161-2-yang.zh...@intel.com>
> Signed-off-by: Paolo Bonzini 
> ---
>  hw/core/numa.c|  5 ++---
>  hw/i386/acpi-build.c  |  2 ++
>  hw/i386/sgx-epc.c |  3 +++
>  hw/i386/sgx-stub.c|  4 
>  hw/i386/sgx.c | 44 +++
>  include/hw/i386/sgx-epc.h |  3 +++
>  monitor/hmp-cmds.c|  1 +
>  qapi/machine.json | 10 -
>  qemu-options.hx   |  4 ++--
>  9 files changed, 70 insertions(+), 6 deletions(-)

> diff --git a/qapi/machine.json b/qapi/machine.json
> index f1839acf20..edeab6084b 100644
> --- a/qapi/machine.json
> +++ b/qapi/machine.json
> @@ -1207,12 +1207,15 @@
>  #
>  # @memdev: memory backend linked with device
>  #
> +# @node: the numa node

This needs a "(Since: 7.0)" annotation

> +#
>  # Since: 6.2
>  ##
>  { 'struct': 'SgxEPCDeviceInfo',
>'data': { '*id': 'str',
>  'memaddr': 'size',
>  'size': 'size',
> +'node': 'int',
>  'memdev': 'str'
>}
>  }
> @@ -1285,10 +1288,15 @@
>  #
>  # @memdev: memory backend linked with device
>  #
> +# @node: the numa node

Likewise

> +#
>  # Since: 6.2
>  ##
>  { 'struct': 'SgxEPC',
> -  'data': { 'memdev': 'str' } }
> +  'data': { 'memdev': 'str',
> +'node': 'int'
> +  }
> +}
>  

Regards,
Daniel
-- 
|: https://berrange.com  -o-https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o-https://fstop138.berrange.com :|
|: https://entangle-photo.org-o-https://www.instagram.com/dberrange :|




[PULL 10/13] numa: Enable numa for SGX EPC sections

2021-12-15 Thread Paolo Bonzini
From: Yang Zhong 

The basic SGX did not enable numa for SGX EPC sections, which
result in all EPC sections located in numa node 0. This patch
enable SGX numa function in the guest and the EPC section can
work with RAM as one numa node.

The Guest kernel related log:
[0.009981] ACPI: SRAT: Node 0 PXM 0 [mem 0x18000-0x183ff]
[0.009982] ACPI: SRAT: Node 1 PXM 1 [mem 0x18400-0x185bf]
The SRAT table can normally show SGX EPC sections menory info in different
numa nodes.

The SGX EPC numa related command:
 ..
 -m 4G,maxmem=20G \
 -smp sockets=2,cores=2 \
 -cpu host,+sgx-provisionkey \
 -object memory-backend-ram,size=2G,host-nodes=0,policy=bind,id=node0 \
 -object 
memory-backend-epc,id=mem0,size=64M,prealloc=on,host-nodes=0,policy=bind \
 -numa node,nodeid=0,cpus=0-1,memdev=node0 \
 -object memory-backend-ram,size=2G,host-nodes=1,policy=bind,id=node1 \
 -object 
memory-backend-epc,id=mem1,size=28M,prealloc=on,host-nodes=1,policy=bind \
 -numa node,nodeid=1,cpus=2-3,memdev=node1 \
 -M 
sgx-epc.0.memdev=mem0,sgx-epc.0.node=0,sgx-epc.1.memdev=mem1,sgx-epc.1.node=1 \
 ..

Signed-off-by: Yang Zhong 
Message-Id: <20211101162009.62161-2-yang.zh...@intel.com>
Signed-off-by: Paolo Bonzini 
---
 hw/core/numa.c|  5 ++---
 hw/i386/acpi-build.c  |  2 ++
 hw/i386/sgx-epc.c |  3 +++
 hw/i386/sgx-stub.c|  4 
 hw/i386/sgx.c | 44 +++
 include/hw/i386/sgx-epc.h |  3 +++
 monitor/hmp-cmds.c|  1 +
 qapi/machine.json | 10 -
 qemu-options.hx   |  4 ++--
 9 files changed, 70 insertions(+), 6 deletions(-)

diff --git a/hw/core/numa.c b/hw/core/numa.c
index e6050b2273..1aa05dcf42 100644
--- a/hw/core/numa.c
+++ b/hw/core/numa.c
@@ -784,9 +784,8 @@ static void numa_stat_memory_devices(NumaNodeMem node_mem[])
 break;
 case MEMORY_DEVICE_INFO_KIND_SGX_EPC:
 se = value->u.sgx_epc.data;
-/* TODO: once we support numa, assign to right node */
-node_mem[0].node_mem += se->size;
-node_mem[0].node_plugged_mem += se->size;
+node_mem[se->node].node_mem += se->size;
+node_mem[se->node].node_plugged_mem = 0;
 break;
 default:
 g_assert_not_reached();
diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
index a99c6e4fe3..8383b83ee3 100644
--- a/hw/i386/acpi-build.c
+++ b/hw/i386/acpi-build.c
@@ -2068,6 +2068,8 @@ build_srat(GArray *table_data, BIOSLinker *linker, 
MachineState *machine)
 nvdimm_build_srat(table_data);
 }
 
+sgx_epc_build_srat(table_data);
+
 /*
  * TODO: this part is not in ACPI spec and current linux kernel boots fine
  * without these entries. But I recall there were issues the last time I
diff --git a/hw/i386/sgx-epc.c b/hw/i386/sgx-epc.c
index e508827e78..96b2940d75 100644
--- a/hw/i386/sgx-epc.c
+++ b/hw/i386/sgx-epc.c
@@ -21,6 +21,7 @@
 
 static Property sgx_epc_properties[] = {
 DEFINE_PROP_UINT64(SGX_EPC_ADDR_PROP, SGXEPCDevice, addr, 0),
+DEFINE_PROP_UINT32(SGX_EPC_NUMA_NODE_PROP, SGXEPCDevice, node, 0),
 DEFINE_PROP_LINK(SGX_EPC_MEMDEV_PROP, SGXEPCDevice, hostmem,
  TYPE_MEMORY_BACKEND_EPC, HostMemoryBackendEpc *),
 DEFINE_PROP_END_OF_LIST(),
@@ -139,6 +140,8 @@ static void sgx_epc_md_fill_device_info(const 
MemoryDeviceState *md,
 se->memaddr = epc->addr;
 se->size = object_property_get_uint(OBJECT(epc), SGX_EPC_SIZE_PROP,
 NULL);
+se->node = object_property_get_uint(OBJECT(epc), SGX_EPC_NUMA_NODE_PROP,
+NULL);
 se->memdev = object_get_canonical_path(OBJECT(epc->hostmem));
 
 info->u.sgx_epc.data = se;
diff --git a/hw/i386/sgx-stub.c b/hw/i386/sgx-stub.c
index c9b379e665..26833eb233 100644
--- a/hw/i386/sgx-stub.c
+++ b/hw/i386/sgx-stub.c
@@ -6,6 +6,10 @@
 #include "qapi/error.h"
 #include "qapi/qapi-commands-misc-target.h"
 
+void sgx_epc_build_srat(GArray *table_data)
+{
+}
+
 SGXInfo *qmp_query_sgx(Error **errp)
 {
 error_setg(errp, "SGX support is not compiled in");
diff --git a/hw/i386/sgx.c b/hw/i386/sgx.c
index 8fef3dd8fa..d04299904a 100644
--- a/hw/i386/sgx.c
+++ b/hw/i386/sgx.c
@@ -23,6 +23,7 @@
 #include "sysemu/hw_accel.h"
 #include "sysemu/reset.h"
 #include 
+#include "hw/acpi/aml-build.h"
 
 #define SGX_MAX_EPC_SECTIONS8
 #define SGX_CPUID_EPC_INVALID   0x0
@@ -36,6 +37,46 @@
 
 #define RETRY_NUM   2
 
+static int sgx_epc_device_list(Object *obj, void *opaque)
+{
+GSList **list = opaque;
+
+if (object_dynamic_cast(obj, TYPE_SGX_EPC)) {
+*list = g_slist_append(*list, DEVICE(obj));
+}
+
+object_child_foreach(obj, sgx_epc_device_list, opaque);
+return 0;
+}
+
+static GSList *sgx_epc_get_device_list(void)
+{
+GSList *list = NULL;
+
+object_child_foreach(qdev_