Skip to contentSkip to conversation

Introduce SMP Cache Topology

PATCH Superseded Zhao Liu 4 participants 28 messages Sep 8, 2024 – Dec 17, 2024 KVM

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Zhao Liu

author
#0 lore raw

v2 cover Introduce SMP Cache Topology

Hi all,

Compared with previous Patch v1 [1], I've put the cache properties list into -machine, this is to meet current needs and also remain compatible with my future topology support (more discussion details, pls refer [2]).

This series is based on the commit 1581a0bc928d ("Merge tag 'pull-ufs- 20240906' of https://gitlab.com/jeuk20.kim/qemu into staging ufs queue").

Background
==========

The x86 and ARM (RISCV) need to allow user to configure cache properties (current only topology):
* For x86, the default cache topology model (of max/host CPU) does not always match the Host's real physical cache topology. Performance can increase when the configured virtual topology is closer to the physical topology than a default topology would be.
* For ARM, QEMU can't get the cache topology information from the CPU registers, then user configuration is necessary. Additionally, the cache information is also needed for MPAM emulation (for TCG) to build the right PPTT. (Originally from Jonathan)

About smp-cache
===============

In this version, smp-cache is implemented as a array integrated in -machine. Though -machine currently can't support JSON format, this is the one of the directions of future.

An example is as follows:

smp_cache=smp-cache.0.cache=l1i,smp-cache.0.topology=core,smp-cache.1.cache=l1d,smp-cache.1.topology=core,smp-cache.2.cache=l2,smp-cache.2.topology=module,smp-cache.3.cache=l3,smp-cache.3.topology=die

"cache" specifies the cache that the properties will be applied on. This field is the combination of cache level and cache type. Now it supports "l1d" (L1 data cache), "l1i" (L1 instruction cache), "l2" (L2 unified cache) and "l3" (L3 unified cache).

"topology" field accepts CPU topology levels including "thread", "core", "module", "cluster", "die", "socket", "book", "drawer" and a special value "default".

The "default" is introduced to make it easier for libvirt to set a default parameter value without having to care about the specific machine (because currently there is no proper way for machine to expose supported topology levels and caches).

If "default" is set, then the cache topology will follow the architecture's default cache topology model. If other CPU topology level is set, the cache will be shared at corresponding CPU topology level.

Welcome your comment!

[1]: Patch v1: https://lore.kernel.org/qemu-devel/[email protected]/
[2]: API disscussion: https://lore.kernel.org/qemu-devel/[email protected]/

Thanks and Best Regards,
Zhao
---
Changelog:

Main changes since Patch v1:
* Dropped handwriten smp-cache object and integrated cache properties list into MachineState and used -machine to configure SMP cache properties. (Markus)
* Dropped prefix of CpuTopologyLevel enumeration. (Markus)
* Rename CPU_TOPO_LEVEL_* to CPU_TOPOLOGY_LEVEL_* to match the QAPI's generated code. (Markus)
* Renamed SMPCacheProperty/SMPCacheProperties (QAPI structures) to SmpCacheProperties/SmpCachePropertiesWrapper. (Markus)
* Renamed SMPCacheName (QAPI structure) to SmpCacheLevelAndType and dropped prefix. (Markus)
* Renamed 'name' field in SmpCacheProperties to 'cache', since the type and level of the cache in SMP system could be able to specify all of these kinds of cache explicitly enough.
* Renamed 'topo' field in SmpCacheProperties to 'topology'. (Markus)
* Returned error information when user repeats setting cache properties. (Markus)
* Renamed SmpCacheLevelAndType to CacheLevelAndType, since this representation is general across SMP or hybrid system.
* Dropped machine_check_smp_cache_support() and did the check when -machine parses smp-cache in machine_parse_smp_cache().

Main changes since RFC v2:
* Dropped cpu-topology.h and cpu-topology.c since QAPI has the helper (CpuTopologyLevel_str) to convert enum to string. (Markus)
* Fixed text format in machine.json (CpuTopologyLevel naming, 2 spaces between sentences). (Markus)
* Added a new level "default" to de-compatibilize some arch-specific topo settings. (Daniel)
* Moved CpuTopologyLevel to qapi/machine-common.json, at where the cache enumeration and smp-cache object would be added.
- If smp-cache object is defined in qapi/machine.json, storage-daemon will complain about the qmp cmds in qapi/machine.json during compiling.
* Referred to Daniel's suggestion to introduce cache JSON list, though as a standalone object since -smp/-machine can't support JSON.
* Linked machine's smp_cache to smp-cache object instead of a builtin structure. This is to get around the fact that the keyval format of -machine can't support JSON.
* Wrapped the cache topology level access into a helper.
* Split as a separate commit to just include compatibility checking and topology checking.
* Allow setting "default" topology level even though the cache isn't supported by machine. (Daniel)
* Rewrote the document of smp-cache object.

Main changes since RFC v1:
* Split CpuTopology renaimg out of this RFC.
* Use QAPI to enumerate CPU topology levels.
* Drop string_to_cpu_topo() since QAPI will help to parse the topo levels.
* Set has_*_cache field in machine_get_smp(). (JeeHeng)
* Use "*_cache=topo_level" as -smp example as the original "level" term for a cache has a totally different meaning. (Jonathan)
---
Zhao Liu (7):
hw/core: Make CPU topology enumeration arch-agnostic qapi/qom: Define cache enumeration and properties hw/core: Add smp cache topology for machine
hw/core: Check smp cache topology support for machine i386/cpu: Support thread and module level cache topology i386/cpu: Update cache topology with machine's configuration i386/pc: Support cache topology in -machine for PC machine

 hw/core/machine-smp.c      | 119 +++++++++++++++++++++++
 hw/core/machine.c          |  44 +++++++++
 hw/i386/pc.c               |   4 +
 hw/i386/x86-common.c       |   4 +-
 include/hw/boards.h        |  13 +++
 include/hw/i386/topology.h |  22 +----
 qapi/machine-common.json   |  96 ++++++++++++++++++-
 qemu-options.hx            |  28 +++++-
 target/i386/cpu.c          | 191 ++++++++++++++++++++++---------------
 target/i386/cpu.h          |   4 +-
 10 files changed, 425 insertions(+), 100 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/7 hw/core: Make CPU topology enumeration arch-agnostic

Cache topology needs to be defined based on CPU topology levels. Thus, define CPU topology enumeration in qapi/machine.json to make it generic for all architectures.

To match the general topology naming style, rename CPU_TOPO_LEVEL_* to CPU_TOPOLOGY_LEVEL_*, and rename SMT and package levels to thread and socket.

Also, enumerate additional topology levels for non-i386 arches, and add a CPU_TOPOLOGY_LEVEL_DEFAULT to help future smp-cache object to work with compatibility requirement of arch-specific cache topology models.

---
Changes since Patch v1:
* Dropped prefix of CpuTopologyLevel enumeration. (Markus)
* Rename CPU_TOPO_LEVEL_* to CPU_TOPOLOGY_LEVEL_* to match the QAPI's generated code. (Markus)

Changes since RFC v2:
* Dropped cpu-topology.h and cpu-topology.c since QAPI has the helper (CpuTopologyLevel_str) to convert enum to string. (Markus)
* Fixed text format in machine.json (CpuTopologyLevel naming, 2 spaces between sentences). (Markus)
* Added a new level "default" to de-compatibilize some arch-specific topo settings. (Daniel)
* Moved CpuTopologyLevel to qapi/machine-common.json, at where the cache enumeration and smp-cache object would be added.
- If smp-cache object is defined in qapi/machine.json, storage-daemon will complain about the qmp cmds in qapi/machine.json during compiling.

Changes since RFC v1:
* Used QAPI to enumerate CPU topology levels.
* Dropped string_to_cpu_topo() since QAPI will help to parse the topo levels.

---
 hw/i386/x86-common.c       |   4 +-
 include/hw/i386/topology.h |  22 +-----
 qapi/machine-common.json   |  46 +++++++++++-
 target/i386/cpu.c          | 144 ++++++++++++++++++-------------------
 target/i386/cpu.h          |   4 +-
 5 files changed, 124 insertions(+), 96 deletions(-)
hw/i386/x86-common.c +2 -2
index 992ea1f25e94..b21d2ab97349 100644
@@ -273,12 +273,12 @@ void x86_cpu_pre_plug(HotplugHandler *hotplug_dev,
 
     if (ms->smp.modules > 1) {
         env->nr_modules = ms->smp.modules;
-        set_bit(CPU_TOPO_LEVEL_MODULE, env->avail_cpu_topo);
+        set_bit(CPU_TOPOLOGY_LEVEL_MODULE, env->avail_cpu_topo);
     }
 
     if (ms->smp.dies > 1) {
         env->nr_dies = ms->smp.dies;
-        set_bit(CPU_TOPO_LEVEL_DIE, env->avail_cpu_topo);
+        set_bit(CPU_TOPOLOGY_LEVEL_DIE, env->avail_cpu_topo);
     }
 
     /*
include/hw/i386/topology.h +3 -19
index dff49fce1154..bf740383038b 100644
@@ -39,7 +39,7 @@
  *  CPUID Fn8000_0008_ECX[ApicIdCoreIdSize[3:0]] is set to apicid_core_width().
  */
 
-
+#include "qapi/qapi-types-machine-common.h"
 #include "qemu/bitops.h"
 
 /*
@@ -62,22 +62,6 @@ typedef struct X86CPUTopoInfo {
     unsigned threads_per_core;
 } X86CPUTopoInfo;
 
-/*
- * CPUTopoLevel is the general i386 topology hierarchical representation,
- * ordered by increasing hierarchical relationship.
- * Its enumeration value is not bound to the type value of Intel (CPUID[0x1F])
- * or AMD (CPUID[0x80000026]).
- */
-enum CPUTopoLevel {
-    CPU_TOPO_LEVEL_INVALID,
-    CPU_TOPO_LEVEL_SMT,
-    CPU_TOPO_LEVEL_CORE,
-    CPU_TOPO_LEVEL_MODULE,
-    CPU_TOPO_LEVEL_DIE,
-    CPU_TOPO_LEVEL_PACKAGE,
-    CPU_TOPO_LEVEL_MAX,
-};
-
 /* Return the bit width needed for 'count' IDs */
 static unsigned apicid_bitwidth_for_count(unsigned count)
 {
@@ -212,8 +196,8 @@ static inline apic_id_t x86_apicid_from_cpu_idx(X86CPUTopoInfo *topo_info,
  */
 static inline bool x86_has_extended_topo(unsigned long *topo_bitmap)
 {
-    return test_bit(CPU_TOPO_LEVEL_MODULE, topo_bitmap) ||
-           test_bit(CPU_TOPO_LEVEL_DIE, topo_bitmap);
+    return test_bit(CPU_TOPOLOGY_LEVEL_MODULE, topo_bitmap) ||
+           test_bit(CPU_TOPOLOGY_LEVEL_DIE, topo_bitmap);
 }
 
 #endif /* HW_I386_TOPOLOGY_H */
qapi/machine-common.json +45 -1

55 lines not on this page · raw message

target/i386/cpu.c +72 -72

453 lines not on this page · raw message

target/i386/cpu.h +2 -2
index 14edd57a37ba..afe2b5fd3382 100644
@@ -1665,7 +1665,7 @@ typedef struct CPUCacheInfo {
      * Used to encode CPUID[4].EAX[bits 25:14] or
      * CPUID[0x8000001D].EAX[bits 25:14].
      */
-    enum CPUTopoLevel share_level;
+    CpuTopologyLevel share_level;
 } CPUCacheInfo;
 
 
@@ -1995,7 +1995,7 @@ typedef struct CPUArchState {
     unsigned nr_modules;
 
     /* Bitmap of available CPU topology levels for this CPU. */
-    DECLARE_BITMAP(avail_cpu_topo, CPU_TOPO_LEVEL_MAX);
+    DECLARE_BITMAP(avail_cpu_topo, CPU_TOPOLOGY_LEVEL__MAX);
 } CPUX86State;
 
 struct kvm_msrs;
Signature · 1 line
2.34.1
Reviewed-by on v2 1/7
#2 lore raw

Re: [PATCH v2 1/7] hw/core: Make CPU topology enumeration arch-agnostic

Quoted 8 lines · On Sun, 8 Sep 2024 20:59:14 +0800 Zhao Liu <[email protected]> wrote:

Cache topology needs to be defined based on CPU topology levels. Thus, define CPU topology enumeration in qapi/machine.json to make it generic for all architectures.

To match the general topology naming style, rename CPU_TOPO_LEVEL_* to CPU_TOPOLOGY_LEVEL_*, and rename SMT and package levels to thread and socket.

Quoted 6 lines

Also, enumerate additional topology levels for non-i386 arches, and add a CPU_TOPOLOGY_LEVEL_DEFAULT to help future smp-cache object to work with compatibility requirement of arch-specific cache topology models.

LGTM

author
#3 lore raw

PATCH v2 2/7 qapi/qom: Define cache enumeration and properties

In reply to Zhao Liu

The x86 and ARM need to allow user to configure cache properties (current only topology):
* For x86, the default cache topology model (of max/host CPU) does not always match the Host's real physical cache topology. Performance can increase when the configured virtual topology is closer to the physical topology than a default topology would be.
* For ARM, QEMU can't get the cache topology information from the CPU registers, then user configuration is necessary. Additionally, the cache information is also needed for MPAM emulation (for TCG) to build the right PPTT.

Define smp-cache related enumeration and properties in QAPI, so that user could configure cache properties for SMP system through -machine in the subsequent patch.

Cache enumeration (CacheLevelAndType) is implemented as the combination of cache level (level 1/2/3) and cache type (data/instruction/unified).

Currently, separated L1 cache (L1 data cache and L1 instruction cache) with unified higher-level cache (e.g., unified L2 and L3 caches), is the most common cache architectures.

Therefore, enumerate the L1 D-cache, L1 I-cache, L2 cache and L3 cache with smp-cache object to add the basic cache topology support. Other kinds of caches (e.g., L1 unified or L2/L3 separated caches) can be added directly into CacheLevelAndType if necessary.

Cache properties (SmpCacheProperties) currently only contains cache topology information, and other cache properties can be added in it if necessary.

Note, define cache topology based on CPU topology level with two reasons:

1. In practice, a cache will always be bound to the CPU container (either private in the CPU container or shared among multiple containers), and CPU container is often expressed in terms of CPU topology level.
2. The x86's cache-related CPUIDs encode cache topology based on APIC ID's CPU topology layout. And the ACPI PPTT table that ARM/RISCV relies on also requires CPU containers to help indicate the private shared hierarchy of the cache. Therefore, for SMP systems, it is natural to use the CPU topology hierarchy directly in QEMU to define the cache topology.

---
Suggested by credit:
* Referred to Daniel's suggestion to introduce cache object list.
---
Changes since Patch v1:
* Renamed SMPCacheProperty/SMPCacheProperties (QAPI structures) to SmpCacheProperties/SmpCachePropertiesWrapper. (Markus)
* Renamed SMPCacheName (QAPI structure) to SmpCacheLevelAndType and dropped prefix. (Markus)
* Renamed 'name' field in SmpCacheProperties to 'cache', since the type and level of the cache in SMP system could be able to specify all of these kinds of cache explicitly enough.
* Renamed 'topo' field in SmpCacheProperties to 'topology'. (Markus)
* Returned error information when user repeats setting cache properties. (Markus)
* Renamed SmpCacheLevelAndType to CacheLevelAndType, since this representation is general across SMP or hybrid system.
* Dropped handwriten smp-cache object and integrated cache pproperties list into MachineState (in next patch). (Markus)
* Added the reason why x86 and ARM need to configure cache information. (Markus and Jonathan)

Changes since RFC v2:
* New commit to implement cache list with JSON format instead of multiple sub-options in -smp.

---
 qapi/machine-common.json | 50 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
qapi/machine-common.json +50 -0
index 148a2c8dccca..f6fe1a208214 100644
@@ -63,3 +63,53 @@
 { 'enum': 'CpuTopologyLevel',
   'data': [ 'invalid', 'thread', 'core', 'module', 'cluster',
             'die', 'socket', 'book', 'drawer', 'default' ] }
+
+##
+# @CacheLevelAndType:
+#
+# Caches a system may have.  The enumeration value here is the
+# combination of cache level and cache type.
+#
+# @l1d: L1 data cache.
+#
+# @l1i: L1 instruction cache.
+#
+# @l2: L2 (unified) cache.
+#
+# @l3: L3 (unified) cache
+#
+# Since: 9.1
+##
+{ 'enum': 'CacheLevelAndType',
+  'data': [ 'l1d', 'l1i', 'l2', 'l3' ] }
+
+##
+# @SmpCacheProperties:
+#
+# Cache information for SMP system.
+#
+# @cache: Cache name, which is the combination of cache level
+#     and cache type.
+#
+# @topology: Cache topology level.  It accepts the CPU topology
+#     enumeration as the parameter, i.e., CPUs in the same
+#     topology container share the same cache.
+#
+# Since: 9.1
+##
+{ 'struct': 'SmpCacheProperties',
+  'data': {
+  'cache': 'CacheLevelAndType',
+  'topology': 'CpuTopologyLevel' } }
+
+##
+# @SmpCachePropertiesWrapper:
+#
+# List wrapper of SmpCacheProperties.
+#
+# @caches: the list of SmpCacheProperties.
+#
+# Since 9.1
+##
+{ 'struct': 'SmpCachePropertiesWrapper',
+  'data': { 'caches': ['SmpCacheProperties'] } }
Signature · 1 line
2.34.1
Reviewed-by on v2 2/7
#4 lore raw

Re: [PATCH v2 2/7] qapi/qom: Define cache enumeration and properties

Quoted 34 lines · On Sun, 8 Sep 2024 20:59:15 +0800 Zhao Liu <[email protected]> wrote:

The x86 and ARM need to allow user to configure cache properties (current only topology):
* For x86, the default cache topology model (of max/host CPU) does not always match the Host's real physical cache topology. Performance can increase when the configured virtual topology is closer to the physical topology than a default topology would be.
* For ARM, QEMU can't get the cache topology information from the CPU registers, then user configuration is necessary. Additionally, the cache information is also needed for MPAM emulation (for TCG) to build the right PPTT.

Define smp-cache related enumeration and properties in QAPI, so that user could configure cache properties for SMP system through -machine in the subsequent patch.

Cache enumeration (CacheLevelAndType) is implemented as the combination of cache level (level 1/2/3) and cache type (data/instruction/unified).

Currently, separated L1 cache (L1 data cache and L1 instruction cache) with unified higher-level cache (e.g., unified L2 and L3 caches), is the most common cache architectures.

Therefore, enumerate the L1 D-cache, L1 I-cache, L2 cache and L3 cache with smp-cache object to add the basic cache topology support. Other kinds of caches (e.g., L1 unified or L2/L3 separated caches) can be added directly into CacheLevelAndType if necessary.

Cache properties (SmpCacheProperties) currently only contains cache topology information, and other cache properties can be added in it if necessary.

Note, define cache topology based on CPU topology level with two reasons:

Quoted 7 lines

1. In practice, a cache will always be bound to the CPU container (either private in the CPU container or shared among multiple containers), and CPU container is often expressed in terms of CPU topology level.
2. The x86's cache-related CPUIDs encode cache topology based on APIC ID's CPU topology layout. And the ACPI PPTT table that ARM/RISCV relies on also requires CPU containers to help indicate the private

Really trivial but CPU Containers are a different ACPI concept. For PPTT they are referred to as Processor Groups. Wonderfully they 'might match a Processor Container in the namespace' which rather implies they might not. In QEMU they always will because the next bit of the spec matters. "In that case this entry will match the value of the _UID method of the associated processor container. Where there is a match it must be represented."

So having said all that, CPU container is probably fine as a description.

Quoted 7 lines

shared hierarchy of the cache. Therefore, for SMP systems, it is natural to use the CPU topology hierarchy directly in QEMU to define the cache topology.

Seems fine but my gut would be to combine this and next patch so we can see how it is used (assuming no one asked for it to be separate!)

Version numbers need an update I guess.

qapi/machine-common.json line 112

Quoted 8 lines
qapi/machine-common.json +8 -0
+##
+# @SmpCachePropertiesWrapper:
+#
+# List wrapper of SmpCacheProperties.
+#
+# @caches: the list of SmpCacheProperties.
+#
+# Since 9.1

Needs updating to 9.2 I guess.

Quoted 3 lines
qapi/machine-common.json +3 -0
+##
+{ 'struct': 'SmpCachePropertiesWrapper',
+  'data': { 'caches': ['SmpCacheProperties'] } }
author
#5 lore raw

Re: [PATCH v2 2/7] qapi/qom: Define cache enumeration and properties

Hi Jonathan,

Thanks for your review and feedback!

[snip]

Quoted 19 lines
Quoted 10 lines

Note, define cache topology based on CPU topology level with two reasons:

1. In practice, a cache will always be bound to the CPU container (either private in the CPU container or shared among multiple containers), and CPU container is often expressed in terms of CPU topology level.
2. The x86's cache-related CPUIDs encode cache topology based on APIC ID's CPU topology layout. And the ACPI PPTT table that ARM/RISCV relies on also requires CPU containers to help indicate the private

Really trivial but CPU Containers are a different ACPI concept. For PPTT they are referred to as Processor Groups. Wonderfully they 'might match a Processor Container in the namespace' which rather implies they might not. In QEMU they always will because the next bit of the spec matters. "In that case this entry will match the value of the _UID method of the associated processor container. Where there is a match it must be represented."

Quoted 1 line

So having said all that, CPU container is probably fine as a description.

Thanks for the explanation!

Quoted 10 lines
Quoted 7 lines

shared hierarchy of the cache. Therefore, for SMP systems, it is natural to use the CPU topology hierarchy directly in QEMU to define the cache topology.

Seems fine but my gut would be to combine this and next patch so we can see how it is used (assuming no one asked for it to be separate!)

No problem. I intended to make it easier to review the QAPI part, but these two patches were simple enough that I was happy to combine them.

Quoted 1 line

Version numbers need an update I guess.

Ah, yes!

Quoted 1 line

Thanks!

Quoted 10 lines

qapi/machine-common.json line 112

Quoted 8 lines
qapi/machine-common.json +8 -0
+##
+# @SmpCachePropertiesWrapper:
+#
+# List wrapper of SmpCacheProperties.
+#
+# @caches: the list of SmpCacheProperties.
+#
+# Since 9.1

Needs updating to 9.2 I guess.

Yes, I think so, too.

Thanks,
Zhao

Quoted 4 lines
Quoted 3 lines
qapi/machine-common.json +3 -0
+##
+{ 'struct': 'SmpCachePropertiesWrapper',
+  'data': { 'caches': ['SmpCacheProperties'] } }
author
#6 lore raw

PATCH v2 3/7 hw/core: Add smp cache topology for machine

In reply to Zhao Liu

With smp-cache object support, add smp cache topology for machine by linking the smp-cache object.

Also add a helper to access cache topology level.

---
Changes since Patch v1:
* Integrated cache properties list into MachineState and used -machine to configure SMP cache properties. (Markus)

Changes since RFC v2:
* Linked machine's smp_cache to smp-cache object instead of a builtin structure. This is to get around the fact that the keyval format of -machine can't support JSON.
* Wrapped the cache topology level access into a helper.

---
 hw/core/machine-smp.c | 41 ++++++++++++++++++++++++++++++++++++++++
 hw/core/machine.c     | 44 +++++++++++++++++++++++++++++++++++++++++++
 include/hw/boards.h   | 10 ++++++++++
 3 files changed, 95 insertions(+)
hw/core/machine-smp.c +41 -0
index 5d8d7edcbd3f..b517c3471d1a 100644
@@ -261,6 +261,41 @@ void machine_parse_smp_config(MachineState *ms,
     }
 }
 
+bool machine_parse_smp_cache(MachineState *ms,
+                             const SmpCachePropertiesList *caches,
+                             Error **errp)
+{
+    const SmpCachePropertiesList *node;
+    DECLARE_BITMAP(caches_bitmap, CACHE_LEVEL_AND_TYPE__MAX);
+
+    for (node = caches; node; node = node->next) {
+        /* Prohibit users from setting the cache topology level to invalid. */
+        if (node->value->topology == CPU_TOPOLOGY_LEVEL_INVALID) {
+            error_setg(errp,
+                       "Invalid cache topology level: %s. "
+                       "The topology should match the "
+                       "valid CPU topology level",
+                       CpuTopologyLevel_str(node->value->topology));
+            return false;
+        }
+
+        /* Prohibit users from repeating settings. */
+        if (test_bit(node->value->cache, caches_bitmap)) {
+            error_setg(errp,
+                       "Invalid cache properties: %s. "
+                       "The cache properties are duplicated",
+                       CacheLevelAndType_str(node->value->cache));
+            return false;
+        } else {
+            ms->smp_cache.props[node->value->cache].topology =
+                node->value->topology;
+            set_bit(node->value->cache, caches_bitmap);
+        }
+    }
+
+    return true;
+}
+
 unsigned int machine_topo_get_cores_per_socket(const MachineState *ms)
 {
     return ms->smp.cores * ms->smp.modules * ms->smp.clusters * ms->smp.dies;
@@ -270,3 +305,9 @@ unsigned int machine_topo_get_threads_per_socket(const MachineState *ms)
 {
     return ms->smp.threads * machine_topo_get_cores_per_socket(ms);
 }
+
+CpuTopologyLevel machine_get_cache_topo_level(const MachineState *ms,
+                                              CacheLevelAndType cache)
+{
+    return ms->smp_cache.props[cache].topology;
+}
hw/core/machine.c +44 -0

62 lines not on this page · raw message

include/hw/boards.h +10 -0
index 9a492770cbb9..64439dc7da2c 100644
@@ -43,8 +43,13 @@ void machine_set_cpu_numa_node(MachineState *machine,
                                Error **errp);
 void machine_parse_smp_config(MachineState *ms,
                               const SMPConfiguration *config, Error **errp);
+bool machine_parse_smp_cache(MachineState *ms,
+                             const SmpCachePropertiesList *caches,
+                             Error **errp);
 unsigned int machine_topo_get_cores_per_socket(const MachineState *ms);
 unsigned int machine_topo_get_threads_per_socket(const MachineState *ms);
+CpuTopologyLevel machine_get_cache_topo_level(const MachineState *ms,
+                                              CacheLevelAndType cache);
 void machine_memory_devices_init(MachineState *ms, hwaddr base, uint64_t size);
 
 /**
@@ -363,6 +368,10 @@ typedef struct CpuTopology {
     unsigned int max_cpus;
 } CpuTopology;
 
+typedef struct SmpCache {
+    SmpCacheProperties props[CACHE_LEVEL_AND_TYPE__MAX];
+} SmpCache;
+
 /**
  * MachineState:
  */
@@ -413,6 +422,7 @@ struct MachineState {
     AccelState *accelerator;
     CPUArchIdList *possible_cpus;
     CpuTopology smp;
+    SmpCache smp_cache;
     struct NVDIMMState *nvdimms_state;
     struct NumaState *numa_state;
 };
Signature · 1 line
2.34.1
Reviewed-by on v2 3/7
#7 lore raw

Re: [PATCH v2 3/7] hw/core: Add smp cache topology for machine

Quoted 7 lines · On Sun, 8 Sep 2024 20:59:16 +0800 Zhao Liu <[email protected]> wrote:

With smp-cache object support, add smp cache topology for machine by linking the smp-cache object.

Also add a helper to access cache topology level.

Minor stuff. The property stuff is something I seems to mostly get wrong so needs more eyes but fwiw looks fine to me.
With the tweaks suggested below.

Quoted 28 lines

---
Changes since Patch v1:
* Integrated cache properties list into MachineState and used -machine to configure SMP cache properties. (Markus)

Changes since RFC v2:
* Linked machine's smp_cache to smp-cache object instead of a builtin structure. This is to get around the fact that the keyval format of -machine can't support JSON.
* Wrapped the cache topology level access into a helper.

---
 hw/core/machine-smp.c | 41 ++++++++++++++++++++++++++++++++++++++++
 hw/core/machine.c     | 44 +++++++++++++++++++++++++++++++++++++++++++
 include/hw/boards.h   | 10 ++++++++++
 3 files changed, 95 insertions(+)
hw/core/machine-smp.c +4 -0
index 5d8d7edcbd3f..b517c3471d1a 100644
@@ -261,6 +261,41 @@ void machine_parse_smp_config(MachineState *ms,
     }
 }
 
+bool machine_parse_smp_cache(MachineState *ms,
+                             const SmpCachePropertiesList *caches,
+                             Error **errp)
+{

hw/core/machine-smp.c void machine_parse_smp_config(MachineState *ms, · line 277

Quoted 10 lines
hw/core/machine-smp.c +10 -0
+    const SmpCachePropertiesList *node;
+    DECLARE_BITMAP(caches_bitmap, CACHE_LEVEL_AND_TYPE__MAX);
+
+    for (node = caches; node; node = node->next) {
+        /* Prohibit users from setting the cache topology level to invalid. */
+        if (node->value->topology == CPU_TOPOLOGY_LEVEL_INVALID) {
+            error_setg(errp,
+                       "Invalid cache topology level: %s. "
+                       "The topology should match the "
+                       "valid CPU topology level",

I think that's too much wrapping for an error message. Makes them hard to grep for.

hw/core/machine-smp.c void machine_parse_smp_config(MachineState *ms, · line 289

Quoted 12 lines
hw/core/machine-smp.c +12 -0
+                       CpuTopologyLevel_str(node->value->topology));
+            return false;
+        }
+
+        /* Prohibit users from repeating settings. */
+        if (test_bit(node->value->cache, caches_bitmap)) {
+            error_setg(errp,
+                       "Invalid cache properties: %s. "
+                       "The cache properties are duplicated",
+                       CacheLevelAndType_str(node->value->cache));
+            return false;
+        } else {

returned anyway in the above path, so can drop the else and reduce indent a little.

Quoted 9 lines
hw/core/machine-smp.c +9 -0
+            ms->smp_cache.props[node->value->cache].topology =
+                node->value->topology;
+            set_bit(node->value->cache, caches_bitmap);
+        }
+    }
+
+    return true;
+}
+
author
#8 lore raw

Re: [PATCH v2 3/7] hw/core: Add smp cache topology for machine

Quoted 11 lines · Jonathan Cameron

Date: Tue, 17 Sep 2024 10:00:48 +0100
From: Jonathan Cameron <[email protected]>
Subject: Re: [PATCH v2 3/7] hw/core: Add smp cache topology for machine
X-Mailer: Claws Mail 4.1.0 (GTK 3.24.33; x86_64-w64-mingw32)

Quoted 3 lines · On Sun, 8 Sep 2024 20:59:16 +0800 Zhao Liu <[email protected]> wrote:

With smp-cache object support, add smp cache topology for machine by linking the smp-cache object.

Quoted 7 lines
Quoted 4 lines

Also add a helper to access cache topology level.

Minor stuff. The property stuff is something I seems to mostly get wrong so needs more eyes but fwiw looks fine to me.

Yes and thank you!

Quoted 37 lines

With the tweaks suggested below.

Quoted 33 lines

---
Changes since Patch v1:
* Integrated cache properties list into MachineState and used -machine to configure SMP cache properties. (Markus)

Changes since RFC v2:
* Linked machine's smp_cache to smp-cache object instead of a builtin structure. This is to get around the fact that the keyval format of -machine can't support JSON.
* Wrapped the cache topology level access into a helper.

---
 hw/core/machine-smp.c | 41 ++++++++++++++++++++++++++++++++++++++++
 hw/core/machine.c     | 44 +++++++++++++++++++++++++++++++++++++++++++
 include/hw/boards.h   | 10 ++++++++++
 3 files changed, 95 insertions(+)
hw/core/machine-smp.c +9 -0
index 5d8d7edcbd3f..b517c3471d1a 100644
@@ -261,6 +261,41 @@ void machine_parse_smp_config(MachineState *ms,
     }
 }
 
+bool machine_parse_smp_cache(MachineState *ms,
+                             const SmpCachePropertiesList *caches,
+                             Error **errp)
+{
+    const SmpCachePropertiesList *node;
+    DECLARE_BITMAP(caches_bitmap, CACHE_LEVEL_AND_TYPE__MAX);
+
+    for (node = caches; node; node = node->next) {
+        /* Prohibit users from setting the cache topology level to invalid. */
Quoted 8 lines

hw/core/machine-smp.c void machine_parse_smp_config(MachineState *ms, · line 277

Quoted 5 lines
hw/core/machine-smp.c +5 -0
+        if (node->value->topology == CPU_TOPOLOGY_LEVEL_INVALID) {
+            error_setg(errp,
+                       "Invalid cache topology level: %s. "
+                       "The topology should match the "
+                       "valid CPU topology level",

I think that's too much wrapping for an error message. Makes them hard to grep for.

I understand you mean the last sentence should not be on separate lines but should be continuous in one line, right?

Quoted 13 lines

hw/core/machine-smp.c void machine_parse_smp_config(MachineState *ms, · line 289

Quoted 12 lines
hw/core/machine-smp.c +12 -0
+                       CpuTopologyLevel_str(node->value->topology));
+            return false;
+        }
+
+        /* Prohibit users from repeating settings. */
+        if (test_bit(node->value->cache, caches_bitmap)) {
+            error_setg(errp,
+                       "Invalid cache properties: %s. "
+                       "The cache properties are duplicated",
+                       CacheLevelAndType_str(node->value->cache));
+            return false;
+        } else {
Quoted 2 lines

returned anyway in the above path, so can drop the else and reduce indent a little.

Sure.

Thanks,
Zhao

Quoted 11 lines
Quoted 9 lines
hw/core/machine-smp.c +9 -0
+            ms->smp_cache.props[node->value->cache].topology =
+                node->value->topology;
+            set_bit(node->value->cache, caches_bitmap);
+        }
+    }
+
+    return true;
+}
+
author
#9 lore raw

PATCH v2 4/7 hw/core: Check smp cache topology support for machine

In reply to Zhao Liu

Add cache_supported flags in SMPCompatProps to allow machines to configure various caches support.

And check the compatibility of the cache properties with the machine support in machine_parse_smp_cache().

---
Changes since Patch v1:
* Dropped machine_check_smp_cache_support() and did the check when -machine parses smp-cache in machine_parse_smp_cache().

Changes since RFC v2:
* Split as a separate commit to just include compatibility checking and topology checking.
* Allow setting "default" topology level even though the cache isn't supported by machine. (Daniel)

---
 hw/core/machine-smp.c | 78 +++++++++++++++++++++++++++++++++++++++++++
 include/hw/boards.h   |  3 ++
 2 files changed, 81 insertions(+)
hw/core/machine-smp.c +78 -0
index b517c3471d1a..9a281946762f 100644
@@ -261,10 +261,47 @@ void machine_parse_smp_config(MachineState *ms,
     }
 }
 
+static bool machine_check_topo_support(MachineState *ms,
+                                       CpuTopologyLevel topo,
+                                       Error **errp)
+{
+    MachineClass *mc = MACHINE_GET_CLASS(ms);
+
+    if ((topo == CPU_TOPOLOGY_LEVEL_MODULE && !mc->smp_props.modules_supported) ||
+        (topo == CPU_TOPOLOGY_LEVEL_CLUSTER && !mc->smp_props.clusters_supported) ||
+        (topo == CPU_TOPOLOGY_LEVEL_DIE && !mc->smp_props.dies_supported) ||
+        (topo == CPU_TOPOLOGY_LEVEL_BOOK && !mc->smp_props.books_supported) ||
+        (topo == CPU_TOPOLOGY_LEVEL_DRAWER && !mc->smp_props.drawers_supported)) {
+        error_setg(errp,
+                   "Invalid topology level: %s. "
+                   "The topology level is not supported by this machine",
+                   CpuTopologyLevel_str(topo));
+        return false;
+    }
+
+    return true;
+}
+
+/*
+ * When both cache1 and cache2 are configured with specific topology levels
+ * (not default level), is cache1's topology level higher than cache2?
+ */
+static bool smp_cache_topo_cmp(const SmpCache *smp_cache,
+                               CacheLevelAndType cache1,
+                               CacheLevelAndType cache2)
+{
+    if (smp_cache->props[cache1].topology != CPU_TOPOLOGY_LEVEL_DEFAULT &&
+        smp_cache->props[cache1].topology > smp_cache->props[cache2].topology) {
+        return true;
+    }
+    return false;
+}
+
 bool machine_parse_smp_cache(MachineState *ms,
                              const SmpCachePropertiesList *caches,
                              Error **errp)
 {
+    MachineClass *mc = MACHINE_GET_CLASS(ms);
     const SmpCachePropertiesList *node;
     DECLARE_BITMAP(caches_bitmap, CACHE_LEVEL_AND_TYPE__MAX);
 
@@ -293,6 +330,47 @@ bool machine_parse_smp_cache(MachineState *ms,
         }
     }
 
+    for (int i = 0; i < CACHE_LEVEL_AND_TYPE__MAX; i++) {
+        const SmpCacheProperties *props = &ms->smp_cache.props[i];
+
+        /*
+         * Allow setting "default" topology level even though the cache
+         * isn't supported by machine.
+         */
+        if (props->topology != CPU_TOPOLOGY_LEVEL_DEFAULT &&
+            !mc->smp_props.cache_supported[props->cache]) {
+            error_setg(errp,
+                       "%s cache topology not supported by this machine",
+                       CacheLevelAndType_str(node->value->cache));
+            return false;
+        }
+
+        if (!machine_check_topo_support(ms, props->topology, errp)) {
+            return false;
+        }
+    }
+
+    if (smp_cache_topo_cmp(&ms->smp_cache,
+                           CACHE_LEVEL_AND_TYPE_L1D,
+                           CACHE_LEVEL_AND_TYPE_L2) ||
+        smp_cache_topo_cmp(&ms->smp_cache,
+                           CACHE_LEVEL_AND_TYPE_L1I,
+                           CACHE_LEVEL_AND_TYPE_L2)) {
+        error_setg(errp,
+                   "Invalid smp cache topology. "
+                   "L2 cache topology level shouldn't be lower than L1 cache");
+        return false;
+    }
+
+    if (smp_cache_topo_cmp(&ms->smp_cache,
+                           CACHE_LEVEL_AND_TYPE_L2,
+                           CACHE_LEVEL_AND_TYPE_L3)) {
+        error_setg(errp,
+                   "Invalid smp cache topology. "
+                   "L3 cache topology level shouldn't be lower than L2 cache");
+        return false;
+    }
+
     return true;
 }
 
include/hw/boards.h +3 -0
index 64439dc7da2c..6c3cdfa15f50 100644
@@ -150,6 +150,8 @@ typedef struct {
  * @books_supported - whether books are supported by the machine
  * @drawers_supported - whether drawers are supported by the machine
  * @modules_supported - whether modules are supported by the machine
+ * @cache_supported - whether cache topologies (l1d, l1i, l2 and l3) are
+ *                    supported by the machine
  */
 typedef struct {
     bool prefer_sockets;
@@ -159,6 +161,7 @@ typedef struct {
     bool books_supported;
     bool drawers_supported;
     bool modules_supported;
+    bool cache_supported[CACHE_LEVEL_AND_TYPE__MAX];
 } SMPCompatProps;
 
 /**
Signature · 1 line
2.34.1
Reviewed-by on v2 4/7
#10 lore raw

Re: [PATCH v2 4/7] hw/core: Check smp cache topology support for machine

Quoted 8 lines · On Sun, 8 Sep 2024 20:59:17 +0800 Zhao Liu <[email protected]> wrote:

Add cache_supported flags in SMPCompatProps to allow machines to configure various caches support.

And check the compatibility of the cache properties with the machine support in machine_parse_smp_cache().

Just a few trivial comments inline.

FWIW with or without those changes.

Quoted 31 lines

---
Changes since Patch v1:
* Dropped machine_check_smp_cache_support() and did the check when -machine parses smp-cache in machine_parse_smp_cache().

Changes since RFC v2:
* Split as a separate commit to just include compatibility checking and topology checking.
* Allow setting "default" topology level even though the cache isn't supported by machine. (Daniel)

---
 hw/core/machine-smp.c | 78 +++++++++++++++++++++++++++++++++++++++++++
 include/hw/boards.h   |  3 ++
 2 files changed, 81 insertions(+)
hw/core/machine-smp.c +1 -0
index b517c3471d1a..9a281946762f 100644
@@ -261,10 +261,47 @@ void machine_parse_smp_config(MachineState *ms,
 
 
 bool machine_parse_smp_cache(MachineState *ms,
                              const SmpCachePropertiesList *caches,
                              Error **errp)
 {
+    MachineClass *mc = MACHINE_GET_CLASS(ms);
     const SmpCachePropertiesList *node;
     DECLARE_BITMAP(caches_bitmap, CACHE_LEVEL_AND_TYPE__MAX);
 

hw/core/machine-smp.c bool machine_parse_smp_cache(MachineState *ms, · line 338

Quoted 9 lines
hw/core/machine-smp.c +6 -0
@@ -293,6 +330,47 @@ bool machine_parse_smp_cache(MachineState *ms,
         }
     }
 
+    for (int i = 0; i < CACHE_LEVEL_AND_TYPE__MAX; i++) {
+        const SmpCacheProperties *props = &ms->smp_cache.props[i];
+
+        /*
+         * Allow setting "default" topology level even though the cache
+         * isn't supported by machine.

I'd flip the comment as the condition is doing the opposite.

* Reject non "default" topology level if the cache isn't
* supported by the machine.

Quoted 6 lines
hw/core/machine-smp.c +6 -0
+         */
+        if (props->topology != CPU_TOPOLOGY_LEVEL_DEFAULT &&
+            !mc->smp_props.cache_supported[props->cache]) {
+            error_setg(errp,
+                       "%s cache topology not supported by this machine",
+                       CacheLevelAndType_str(node->value->cache));

hw/core/machine-smp.c bool machine_parse_smp_cache(MachineState *ms, · line 354

Quoted 10 lines
hw/core/machine-smp.c +10 -0
+            return false;
+        }
+
+        if (!machine_check_topo_support(ms, props->topology, errp)) {
+            return false;
+        }
+    }
+
+    if (smp_cache_topo_cmp(&ms->smp_cache,
+                           CACHE_LEVEL_AND_TYPE_L1D,

Short line wrap. Maybe combine the two lines above and similar cases. Up to you though, I don't feel that strongly.

Quoted 21 lines
hw/core/machine-smp.c +19 -0
+                           CACHE_LEVEL_AND_TYPE_L2) ||
+        smp_cache_topo_cmp(&ms->smp_cache,
+                           CACHE_LEVEL_AND_TYPE_L1I,
+                           CACHE_LEVEL_AND_TYPE_L2)) {
+        error_setg(errp,
+                   "Invalid smp cache topology. "
+                   "L2 cache topology level shouldn't be lower than L1 cache");
+        return false;
+    }
+
+    if (smp_cache_topo_cmp(&ms->smp_cache,
+                           CACHE_LEVEL_AND_TYPE_L2,
+                           CACHE_LEVEL_AND_TYPE_L3)) {
+        error_setg(errp,
+                   "Invalid smp cache topology. "
+                   "L3 cache topology level shouldn't be lower than L2 cache");
+        return false;
+    }
+
     return true;
 }
author
#11 lore raw

Re: [PATCH v2 4/7] hw/core: Check smp cache topology support for machine

Quoted 15 lines · Jonathan Cameron

Date: Tue, 17 Sep 2024 09:56:12 +0100
From: Jonathan Cameron <[email protected]>
Subject: Re: [PATCH v2 4/7] hw/core: Check smp cache topology support for
machine
X-Mailer: Claws Mail 4.1.0 (GTK 3.24.33; x86_64-w64-mingw32)

Quoted 6 lines · On Sun, 8 Sep 2024 20:59:17 +0800 Zhao Liu <[email protected]> wrote:

Add cache_supported flags in SMPCompatProps to allow machines to configure various caches support.

And check the compatibility of the cache properties with the machine support in machine_parse_smp_cache().

Quoted 7 lines
Quoted 2 lines

Just a few trivial comments inline.

FWIW with or without those changes.

Thanks!

[snip]

Quoted 4 lines

hw/core/machine-smp.c bool machine_parse_smp_cache(MachineState *ms, · line 338

Quoted 3 lines
hw/core/machine-smp.c +3 -0
+        /*
+         * Allow setting "default" topology level even though the cache
+         * isn't supported by machine.

I'd flip the comment as the condition is doing the opposite.

OK, it's more intuitive.

Quoted 13 lines

* Reject non "default" topology level if the cache isn't
* supported by the machine.

Quoted 11 lines
hw/core/machine-smp.c +11 -0
+         */
+        if (props->topology != CPU_TOPOLOGY_LEVEL_DEFAULT &&
+            !mc->smp_props.cache_supported[props->cache]) {
+            error_setg(errp,
+                       "%s cache topology not supported by this machine",
+                       CacheLevelAndType_str(node->value->cache));
+            return false;
+        }
+
+        if (!machine_check_topo_support(ms, props->topology, errp)) {
+            return false;
Quoted 8 lines

hw/core/machine-smp.c bool machine_parse_smp_cache(MachineState *ms, · line 354

Quoted 5 lines
hw/core/machine-smp.c +5 -0
+        }
+    }
+
+    if (smp_cache_topo_cmp(&ms->smp_cache,
+                           CACHE_LEVEL_AND_TYPE_L1D,

Short line wrap. Maybe combine the two lines above and similar cases.

Like this?

smp_cache_topo_cmp(&ms->smp_cache, CACHE_LEVEL_AND_TYPE_L1D,
CACHE_LEVEL_AND_TYPE_L2)

Quoted 12 lines

Up to you though, I don't feel that strongly.

Quoted 10 lines
hw/core/machine-smp.c +10 -0
+                           CACHE_LEVEL_AND_TYPE_L2) ||
+        smp_cache_topo_cmp(&ms->smp_cache,
+                           CACHE_LEVEL_AND_TYPE_L1I,
+                           CACHE_LEVEL_AND_TYPE_L2)) {
+        error_setg(errp,
+                   "Invalid smp cache topology. "
+                   "L2 cache topology level shouldn't be lower than L1 cache");
+        return false;
+    }
+

Regards,
Zhao

#12 lore raw

Re: [PATCH v2 4/7] hw/core: Check smp cache topology support for machine

Quoted 57 lines · On Mon, 7 Oct 2024 19:12:29 +0800 Zhao Liu <[email protected]> wrote:
Quoted 15 lines · Jonathan Cameron

Date: Tue, 17 Sep 2024 09:56:12 +0100
From: Jonathan Cameron <[email protected]>
Subject: Re: [PATCH v2 4/7] hw/core: Check smp cache topology support for
machine
X-Mailer: Claws Mail 4.1.0 (GTK 3.24.33; x86_64-w64-mingw32)

Quoted 6 lines · On Sun, 8 Sep 2024 20:59:17 +0800 Zhao Liu <[email protected]> wrote:

Add cache_supported flags in SMPCompatProps to allow machines to configure various caches support.

And check the compatibility of the cache properties with the machine support in machine_parse_smp_cache().

Quoted 7 lines
Quoted 2 lines

Just a few trivial comments inline.

FWIW with or without those changes.

Thanks!

[snip]

Quoted 4 lines

hw/core/machine-smp.c bool machine_parse_smp_cache(MachineState *ms, · line 338

Quoted 3 lines
hw/core/machine-smp.c +3 -0
+        /*
+         * Allow setting "default" topology level even though the cache
+         * isn't supported by machine.  

I'd flip the comment as the condition is doing the opposite.

OK, it's more intuitive.

Quoted 13 lines

* Reject non "default" topology level if the cache isn't
* supported by the machine.

Quoted 11 lines
hw/core/machine-smp.c +11 -0
+         */
+        if (props->topology != CPU_TOPOLOGY_LEVEL_DEFAULT &&
+            !mc->smp_props.cache_supported[props->cache]) {
+            error_setg(errp,
+                       "%s cache topology not supported by this machine",
+                       CacheLevelAndType_str(node->value->cache));
+            return false;
+        }
+
+        if (!machine_check_topo_support(ms, props->topology, errp)) {
+            return false;
Quoted 8 lines

hw/core/machine-smp.c bool machine_parse_smp_cache(MachineState *ms, · line 354

Quoted 5 lines
hw/core/machine-smp.c +5 -0
+        }
+    }
+
+    if (smp_cache_topo_cmp(&ms->smp_cache,
+                           CACHE_LEVEL_AND_TYPE_L1D,  

Short line wrap. Maybe combine the two lines above and similar cases.

Quoted 4 lines

Like this?

smp_cache_topo_cmp(&ms->smp_cache, CACHE_LEVEL_AND_TYPE_L1D,
CACHE_LEVEL_AND_TYPE_L2)

yes - that's what I had in mind.

Quoted 18 lines
Quoted 13 lines

Up to you though, I don't feel that strongly.

Quoted 10 lines
hw/core/machine-smp.c +10 -0
+                           CACHE_LEVEL_AND_TYPE_L2) ||
+        smp_cache_topo_cmp(&ms->smp_cache,
+                           CACHE_LEVEL_AND_TYPE_L1I,
+                           CACHE_LEVEL_AND_TYPE_L2)) {
+        error_setg(errp,
+                   "Invalid smp cache topology. "
+                   "L2 cache topology level shouldn't be lower than L1 cache");
+        return false;
+    }
+  

Regards,
Zhao

author
#13 lore raw

PATCH v2 5/7 i386/cpu: Support thread and module level cache topology

In reply to Zhao Liu

Allow cache to be defined at the thread and module level. This increases flexibility for x86 users to customize their cache topology.

---
 target/i386/cpu.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
target/i386/cpu.c +6 -4
index e3a81bc64922..e9f755000356 100644
@@ -241,9 +241,15 @@ static uint32_t max_thread_ids_for_cache(X86CPUTopoInfo *topo_info,
     uint32_t num_ids = 0;
 
     switch (share_level) {
+    case CPU_TOPOLOGY_LEVEL_THREAD:
+        num_ids = 1;
+        break;
     case CPU_TOPOLOGY_LEVEL_CORE:
         num_ids = 1 << apicid_core_offset(topo_info);
         break;
+    case CPU_TOPOLOGY_LEVEL_MODULE:
+        num_ids = 1 << apicid_module_offset(topo_info);
+        break;
     case CPU_TOPOLOGY_LEVEL_DIE:
         num_ids = 1 << apicid_die_offset(topo_info);
         break;
@@ -251,10 +257,6 @@ static uint32_t max_thread_ids_for_cache(X86CPUTopoInfo *topo_info,
         num_ids = 1 << apicid_pkg_offset(topo_info);
         break;
     default:
-        /*
-         * Currently there is no use case for THREAD and MODULE, so use
-         * assert directly to facilitate debugging.
-         */
         g_assert_not_reached();
     }
 
Signature · 1 line
2.34.1
Reviewed-by on v2 5/7
#14 lore raw

Re: [PATCH v2 5/7] i386/cpu: Support thread and module level cache topology

Quoted 5 lines · On Sun, 8 Sep 2024 20:59:18 +0800 Zhao Liu <[email protected]> wrote:

Allow cache to be defined at the thread and module level. This increases flexibility for x86 users to customize their cache topology.

Will be interesting to see if anyone uses the thread level, but no harm in supporting it. I guess this would be a case of RDT / MPAM etc as I'm not sure I've seen an SMT processor with private caches. Some old papers seems to suggest that it might make sense for smt 8 and above.

Anyhow, patch is fine

author
#15 lore raw

Re: [PATCH v2 5/7] i386/cpu: Support thread and module level cache topology

Quoted 9 lines · Jonathan Cameron

Date: Tue, 17 Sep 2024 10:05:08 +0100
From: Jonathan Cameron <[email protected]>
Subject: Re: [PATCH v2 5/7] i386/cpu: Support thread and module level cache
topology
X-Mailer: Claws Mail 4.1.0 (GTK 3.24.33; x86_64-w64-mingw32)

On Sun, 8 Sep 2024 20:59:18 +0800
Zhao Liu <[email protected]> wrote:

Quoted 8 lines
Quoted 5 lines

Allow cache to be defined at the thread and module level. This increases flexibility for x86 users to customize their cache topology.

Will be interesting to see if anyone uses the thread level, but no harm in supporting it.

x86 CPU has a legacy property "x-l1-cache-per-thread". This is the old QEMU cache topology behavior, kept for compatibility. Now add thread level and I can refactor the code for this thread level.

Quoted 4 lines

I guess this would be a case of RDT
/ MPAM etc as I'm not sure I've seen an SMT processor with private caches. Some old papers seems to suggest that it might make sense for smt 8 and above.

Thanks for the hint, I'll think about whether some of the RDT / MPAM cases can be applied here.

Quoted 2 lines

Anyhow, patch is fine

Thanks!

-Zhao

author
#16 lore raw

PATCH v2 6/7 i386/cpu: Update cache topology with machine's configuration

In reply to Zhao Liu

User will configure smp cache topology via -machine smp-cache.

For this case, update the x86 CPUs' cache topology with user's configuration in MachineState.

---
Changes since RFC v2:
* Used smp_cache array to override cache topology.
* Wrapped the updating into a function.

---
 target/i386/cpu.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
target/i386/cpu.c +39 -0
index e9f755000356..6d9f7dc0872a 100644
@@ -7597,6 +7597,38 @@ static void x86_cpu_hyperv_realize(X86CPU *cpu)
     cpu->hyperv_limits[2] = 0;
 }
 
+#ifndef CONFIG_USER_ONLY
+static void x86_cpu_update_smp_cache_topo(MachineState *ms, X86CPU *cpu)
+{
+    CPUX86State *env = &cpu->env;
+    CpuTopologyLevel level;
+
+    level = machine_get_cache_topo_level(ms, CACHE_LEVEL_AND_TYPE_L1D);
+    if (level != CPU_TOPOLOGY_LEVEL_DEFAULT) {
+        env->cache_info_cpuid4.l1d_cache->share_level = level;
+        env->cache_info_amd.l1d_cache->share_level = level;
+    }
+
+    level = machine_get_cache_topo_level(ms, CACHE_LEVEL_AND_TYPE_L1I);
+    if (level != CPU_TOPOLOGY_LEVEL_DEFAULT) {
+        env->cache_info_cpuid4.l1i_cache->share_level = level;
+        env->cache_info_amd.l1i_cache->share_level = level;
+    }
+
+    level = machine_get_cache_topo_level(ms, CACHE_LEVEL_AND_TYPE_L2);
+    if (level != CPU_TOPOLOGY_LEVEL_DEFAULT) {
+        env->cache_info_cpuid4.l2_cache->share_level = level;
+        env->cache_info_amd.l2_cache->share_level = level;
+    }
+
+    level = machine_get_cache_topo_level(ms, CACHE_LEVEL_AND_TYPE_L3);
+    if (level != CPU_TOPOLOGY_LEVEL_DEFAULT) {
+        env->cache_info_cpuid4.l3_cache->share_level = level;
+        env->cache_info_amd.l3_cache->share_level = level;
+    }
+}
+#endif
+
 static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
 {
     CPUState *cs = CPU(dev);
@@ -7821,6 +7853,13 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
 
 #ifndef CONFIG_USER_ONLY
     MachineState *ms = MACHINE(qdev_get_machine());
+
+    /*
+     * TODO: Add a SMPCompatProps.has_caches flag to avoid useless Updates
+     * if user didn't set smp_cache.
+     */
+    x86_cpu_update_smp_cache_topo(ms, cpu);
+
     qemu_register_reset(x86_cpu_machine_reset_cb, cpu);
 
     if (cpu->env.features[FEAT_1_EDX] & CPUID_APIC || ms->smp.cpus > 1) {
Signature · 1 line
2.34.1
#17 lore raw

Re: [PATCH v2 6/7] i386/cpu: Update cache topology with machine's configuration

Quoted 70 lines · On Sun, 8 Sep 2024 20:59:19 +0800 Zhao Liu <[email protected]> wrote:

User will configure smp cache topology via -machine smp-cache.

For this case, update the x86 CPUs' cache topology with user's configuration in MachineState.

---
Changes since RFC v2:
* Used smp_cache array to override cache topology.
* Wrapped the updating into a function.

---
 target/i386/cpu.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
target/i386/cpu.c +18 -0
index e9f755000356..6d9f7dc0872a 100644
@@ -7597,6 +7597,38 @@ static void x86_cpu_hyperv_realize(X86CPU *cpu)
     cpu->hyperv_limits[2] = 0;
 }
 
+#ifndef CONFIG_USER_ONLY
+static void x86_cpu_update_smp_cache_topo(MachineState *ms, X86CPU
 *cpu) +{
+    CPUX86State *env = &cpu->env;
+    CpuTopologyLevel level;
+
+    level = machine_get_cache_topo_level(ms,
 CACHE_LEVEL_AND_TYPE_L1D);
+    if (level != CPU_TOPOLOGY_LEVEL_DEFAULT) {
+        env->cache_info_cpuid4.l1d_cache->share_level = level;
+        env->cache_info_amd.l1d_cache->share_level = level;
+    }
+
+    level = machine_get_cache_topo_level(ms,
 CACHE_LEVEL_AND_TYPE_L1I);
+    if (level != CPU_TOPOLOGY_LEVEL_DEFAULT) {
+        env->cache_info_cpuid4.l1i_cache->share_level = level;
+        env->cache_info_amd.l1i_cache->share_level = level;
+    }
+
+    level = machine_get_cache_topo_level(ms,

CACHE_LEVEL_AND_TYPE_L2);

target/i386/cpu.c +6 -0
+    if (level != CPU_TOPOLOGY_LEVEL_DEFAULT) {
+        env->cache_info_cpuid4.l2_cache->share_level = level;
+        env->cache_info_amd.l2_cache->share_level = level;
+    }
+
+    level = machine_get_cache_topo_level(ms,

CACHE_LEVEL_AND_TYPE_L3);

target/i386/cpu.c +10 -0
+    if (level != CPU_TOPOLOGY_LEVEL_DEFAULT) {
+        env->cache_info_cpuid4.l3_cache->share_level = level;
+        env->cache_info_amd.l3_cache->share_level = level;
+    }
+}
+#endif
+
 static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
 {
     CPUState *cs = CPU(dev);
@@ -7821,6 +7853,13 @@ static void x86_cpu_realizefn(DeviceState
 *dev, Error **errp) 
 #ifndef CONFIG_USER_ONLY
     MachineState *ms = MACHINE(qdev_get_machine());
+
+    /*
+     * TODO: Add a SMPCompatProps.has_caches flag to avoid useless

Updates

target/i386/cpu.c static void x86_cpu_realizefn(DeviceState *dev, Error **errp) · line 7860

Quoted 2 lines
target/i386/cpu.c +2 -0
+     * if user didn't set smp_cache.
+     */

Hi Zhao,

Thanks for sending this patchset so quickly. I really appreciate the TODO already :) It also helps me avoid going through every single layer, especially when I want to avoid matching system registers in ARM, particularly when there's no description in the command line.

Quoted 6 lines
target/i386/cpu.c +2 -0
+    x86_cpu_update_smp_cache_topo(ms, cpu);
+
     qemu_register_reset(x86_cpu_machine_reset_cb, cpu);
 
     if (cpu->env.features[FEAT_1_EDX] & CPUID_APIC || ms->smp.cpus >

1) {

author
#18 lore raw

Re: [PATCH v2 6/7] i386/cpu: Update cache topology with machine's configuration

Hi Ali,

[snip]

Quoted 10 lines

target/i386/cpu.c static void x86_cpu_realizefn(DeviceState *dev, Error **errp) · line 7860

Quoted 6 lines
target/i386/cpu.c +3 -0
+
+    /*
+     * TODO: Add a SMPCompatProps.has_caches flag to avoid useless

Updates

target/i386/cpu.c +2 -0
+     * if user didn't set smp_cache.
+     */

Hi Zhao,

Thanks for sending this patchset so quickly. I really appreciate the TODO already :)

Welcome! And I'm also sorry for a long silence. Now I'm back from the vacation and will keep pushing this series forward.

Quoted 3 lines

It also helps me avoid going through every single
layer, especially when I want to avoid matching system registers in ARM, particularly when there's no description in the command line.

Great! I also noticed your patch for this "TODO" and will help you review it soon.

Regards,
Zhao

Quoted 7 lines
Quoted 6 lines
target/i386/cpu.c +2 -0
+    x86_cpu_update_smp_cache_topo(ms, cpu);
+
     qemu_register_reset(x86_cpu_machine_reset_cb, cpu);
 
     if (cpu->env.features[FEAT_1_EDX] & CPUID_APIC || ms->smp.cpus >

1) {

Reviewed-by on v2 6/7
#19 lore raw

Re: [PATCH v2 6/7] i386/cpu: Update cache topology with machine's configuration

In reply to Zhao Liu
Quoted 7 lines · On Sun, 8 Sep 2024 20:59:19 +0800 Zhao Liu <[email protected]> wrote:

User will configure smp cache topology via -machine smp-cache.

For this case, update the x86 CPUs' cache topology with user's configuration in MachineState.

Seems simple enough.

author
#20 lore raw

Re: [PATCH v2 6/7] i386/cpu: Update cache topology with machine's configuration

Quoted 11 lines · Jonathan Cameron

Date: Tue, 17 Sep 2024 10:06:41 +0100
From: Jonathan Cameron <[email protected]>
Subject: Re: [PATCH v2 6/7] i386/cpu: Update cache topology with machine's
configuration
X-Mailer: Claws Mail 4.1.0 (GTK 3.24.33; x86_64-w64-mingw32)

Quoted 2 lines · On Sun, 8 Sep 2024 20:59:19 +0800 Zhao Liu <[email protected]> wrote:

User will configure smp cache topology via -machine smp-cache.

Quoted 7 lines
Quoted 5 lines

For this case, update the x86 CPUs' cache topology with user's configuration in MachineState.

Seems simple enough.

Thanks!

-Zhao

author
#21 lore raw

PATCH v2 7/7 i386/pc: Support cache topology in -machine for PC machine

In reply to Zhao Liu

Allow user to configure l1d, l1i, l2 and l3 cache topologies for PC machine.

Additionally, add the document of "-machine smp-cache" in qemu-options.hx.

---
Changes since Patch v1:
* Merged document into this patch. (Markus)

Changes since RFC v2:
* Used cache_supported array.

---
 hw/i386/pc.c    |  4 ++++
 qemu-options.hx | 28 +++++++++++++++++++++++++++-
 2 files changed, 31 insertions(+), 1 deletion(-)
hw/i386/pc.c +4 -0
index ba0ff511836c..d562fd25aad2 100644
@@ -1788,6 +1788,10 @@ static void pc_machine_class_init(ObjectClass *oc, void *data)
     mc->nvdimm_supported = true;
     mc->smp_props.dies_supported = true;
     mc->smp_props.modules_supported = true;
+    mc->smp_props.cache_supported[CACHE_LEVEL_AND_TYPE_L1D] = true;
+    mc->smp_props.cache_supported[CACHE_LEVEL_AND_TYPE_L1I] = true;
+    mc->smp_props.cache_supported[CACHE_LEVEL_AND_TYPE_L2] = true;
+    mc->smp_props.cache_supported[CACHE_LEVEL_AND_TYPE_L3] = true;
     mc->default_ram_id = "pc.ram";
     pcmc->default_smbios_ep_type = SMBIOS_ENTRY_POINT_TYPE_AUTO;
 
qemu-options.hx +27 -1
index d94e2cbbaeb1..3936ff3e77f9 100644
@@ -39,7 +39,8 @@ DEF("machine", HAS_ARG, QEMU_OPTION_machine, \
     "                memory-encryption=@var{} memory encryption object to use (default=none)\n"
     "                hmat=on|off controls ACPI HMAT support (default=off)\n"
     "                memory-backend='backend-id' specifies explicitly provided backend for main RAM (default=none)\n"
-    "                cxl-fmw.0.targets.0=firsttarget,cxl-fmw.0.targets.1=secondtarget,cxl-fmw.0.size=size[,cxl-fmw.0.interleave-granularity=granularity]\n",
+    "                cxl-fmw.0.targets.0=firsttarget,cxl-fmw.0.targets.1=secondtarget,cxl-fmw.0.size=size[,cxl-fmw.0.interleave-granularity=granularity]\n"
+    "                smp-cache.0.cache=cachename,smp-cache.0.topology=topologylevel\n",
     QEMU_ARCH_ALL)
 SRST
 ``-machine [type=]name[,prop=value[,...]]``
@@ -159,6 +160,31 @@ SRST
         ::
 
             -machine cxl-fmw.0.targets.0=cxl.0,cxl-fmw.0.targets.1=cxl.1,cxl-fmw.0.size=128G,cxl-fmw.0.interleave-granularity=512
+
+    ``smp-cache.0.cache=cachename,smp-cache.0.topology=topologylevel``
+        Define cache properties (now only the cache topology level) for SMP
+        system.
+
+        ``cache=cachename`` specifies the cache that the properties will be
+        applied on. This field is the combination of cache level and cache
+        type. Currently it supports ``l1d`` (L1 data cache), ``l1i`` (L1
+        instruction cache), ``l2`` (L2 unified cache) and ``l3`` (L3 unified
+        cache).
+
+        ``topology=topologylevel`` sets the cache topology level. It accepts
+        CPU topology levels including ``thread``, ``core``, ``module``,
+        ``cluster``, ``die``, ``socket``, ``book``, ``drawer`` and a special
+        value ``default``. If ``default`` is set, then the cache topology will
+        follow the architecture's default cache topology model. If other CPU
+        topology level is set, the cache will be shared at corresponding CPU
+        topology level. For example, ``topology=core`` makes the cache shared
+        in a core.
+
+        Example:
+
+        ::
+
+            -machine smp-cache.0.cache=l1d,smp-cache.0.topology=core,smp-cache.1.cache=l1i,smp-cache.1.topology=core
 ERST
 
 DEF("M", HAS_ARG, QEMU_OPTION_M,
Signature · 1 line
2.34.1
Reviewed-by on v2 7/7
#22 lore raw

Re: [PATCH v2 7/7] i386/pc: Support cache topology in -machine for PC machine

Quoted 8 lines · On Sun, 8 Sep 2024 20:59:20 +0800 Zhao Liu <[email protected]> wrote:

Allow user to configure l1d, l1i, l2 and l3 cache topologies for PC machine.

Additionally, add the document of "-machine smp-cache" in qemu-options.hx.

Trivial language suggestions.
In general looks good to me.

Hopefully QOM maintainers and others will get to this soon. I'd like Ali's ARM series to land this cycle as well as the lack of this support has been a pain point for us for a while.

Jonathan

Quoted 31 lines

---
Changes since Patch v1:
* Merged document into this patch. (Markus)

Changes since RFC v2:
* Used cache_supported array.

---
 hw/i386/pc.c    |  4 ++++
 qemu-options.hx | 28 +++++++++++++++++++++++++++-
 2 files changed, 31 insertions(+), 1 deletion(-)
hw/i386/pc.c +4 -0
index ba0ff511836c..d562fd25aad2 100644
@@ -1788,6 +1788,10 @@ static void pc_machine_class_init(ObjectClass *oc, void *data)
     mc->nvdimm_supported = true;
     mc->smp_props.dies_supported = true;
     mc->smp_props.modules_supported = true;
+    mc->smp_props.cache_supported[CACHE_LEVEL_AND_TYPE_L1D] = true;
+    mc->smp_props.cache_supported[CACHE_LEVEL_AND_TYPE_L1I] = true;
+    mc->smp_props.cache_supported[CACHE_LEVEL_AND_TYPE_L2] = true;
+    mc->smp_props.cache_supported[CACHE_LEVEL_AND_TYPE_L3] = true;
     mc->default_ram_id = "pc.ram";
     pcmc->default_smbios_ep_type = SMBIOS_ENTRY_POINT_TYPE_AUTO;
 
qemu-options.hx +0 -0
index d94e2cbbaeb1..3936ff3e77f9 100644

qemu-options.hx DEF("machine", HAS_ARG, QEMU_OPTION_machine, \ · line 43

Quoted 6 lines
qemu-options.hx +2 -1
@@ -39,7 +39,8 @@ DEF("machine", HAS_ARG, QEMU_OPTION_machine, \
     "                memory-encryption=@var{} memory encryption object to use (default=none)\n"
     "                hmat=on|off controls ACPI HMAT support (default=off)\n"
     "                memory-backend='backend-id' specifies explicitly provided backend for main RAM (default=none)\n"
-    "                cxl-fmw.0.targets.0=firsttarget,cxl-fmw.0.targets.1=secondtarget,cxl-fmw.0.size=size[,cxl-fmw.0.interleave-granularity=granularity]\n",
+    "                cxl-fmw.0.targets.0=firsttarget,cxl-fmw.0.targets.1=secondtarget,cxl-fmw.0.size=size[,cxl-fmw.0.interleave-granularity=granularity]\n"
+    "                smp-cache.0.cache=cachename,smp-cache.0.topology=topologylevel\n",

Now my cxl-fmw stuff has competition for most hideous element :) When we add a few more properties maybe we'll get an even longer line!

qemu-options.hx SRST · line 166

Quoted 11 lines
qemu-options.hx +4 -0
     QEMU_ARCH_ALL)
 SRST
 ``-machine [type=]name[,prop=value[,...]]``
@@ -159,6 +160,31 @@ SRST
         ::
 
             -machine cxl-fmw.0.targets.0=cxl.0,cxl-fmw.0.targets.1=cxl.1,cxl-fmw.0.size=128G,cxl-fmw.0.interleave-granularity=512
+
+    ``smp-cache.0.cache=cachename,smp-cache.0.topology=topologylevel``
+        Define cache properties (now only the cache topology level) for SMP
+        system.

I'd drop the 'now only' bit. Just means we have add noise updating that
later. It's easy enough to look down and see what is available anyway give
the parameter docs follow immediately after this.

qemu-options.hx SRST · line 170

Quoted 4 lines
qemu-options.hx +4 -0
+
+        ``cache=cachename`` specifies the cache that the properties will be
+        applied on. This field is the combination of cache level and cache
+        type. Currently it supports ``l1d`` (L1 data cache), ``l1i`` (L1

Drop the word Currently as I don't think it adds anything to he meaning. We are never going to add docs that say 'previously it supported' or 'in the future it will support'.

	   "Supports ...

qemu-options.hx SRST · line 178

Quoted 8 lines
qemu-options.hx +8 -0
+        instruction cache), ``l2`` (L2 unified cache) and ``l3`` (L3 unified
+        cache).
+
+        ``topology=topologylevel`` sets the cache topology level. It accepts
+        CPU topology levels including ``thread``, ``core``, ``module``,
+        ``cluster``, ``die``, ``socket``, ``book``, ``drawer`` and a special
+        value ``default``. If ``default`` is set, then the cache topology will
+        follow the architecture's default cache topology model. If other CPU

If another topology level is set

would be clearer. I briefly read this as saying the topology for another CPU
rather than a different value here.

qemu-options.hx SRST · line 181

Quoted 3 lines
qemu-options.hx +3 -0
+        topology level is set, the cache will be shared at corresponding CPU
+        topology level. For example, ``topology=core`` makes the cache shared
+        in a core.

"by all threads within a core." perhaps?

Quoted 9 lines
qemu-options.hx +6 -0
+
+        Example:
+
+        ::
+
+            -machine smp-cache.0.cache=l1d,smp-cache.0.topology=core,smp-cache.1.cache=l1i,smp-cache.1.topology=core
 ERST
 
 DEF("M", HAS_ARG, QEMU_OPTION_M,
author
#23 lore raw

Re: [PATCH v2 7/7] i386/pc: Support cache topology in -machine for PC machine

Quoted 21 lines · Jonathan Cameron

Date: Tue, 17 Sep 2024 10:16:31 +0100
From: Jonathan Cameron <[email protected]>
Subject: Re: [PATCH v2 7/7] i386/pc: Support cache topology in -machine for
PC machine
X-Mailer: Claws Mail 4.1.0 (GTK 3.24.33; x86_64-w64-mingw32)

Quoted 8 lines · On Sun, 8 Sep 2024 20:59:20 +0800 Zhao Liu <[email protected]> wrote:

Allow user to configure l1d, l1i, l2 and l3 cache topologies for PC machine.

Additionally, add the document of "-machine smp-cache" in qemu-options.hx.

Trivial language suggestions.
In general looks good to me.

Quoted 8 lines

Hopefully QOM maintainers and others will get to this soon. I'd like Ali's ARM series to land this cycle as well as the lack of this support has been a pain point for us for a while.

Jonathan

Thanks! I'll refresh a new version.

[snip]

Quoted 6 lines
Quoted 6 lines
qemu-options.hx +0 -0
index d94e2cbbaeb1..3936ff3e77f9 100644
@@ -39,7 +39,8 @@ DEF("machine", HAS_ARG, QEMU_OPTION_machine, \
     "                memory-encryption=@var{} memory encryption object to use (default=none)\n"
Quoted 8 lines

qemu-options.hx DEF("machine", HAS_ARG, QEMU_OPTION_machine, \ · line 43

Quoted 5 lines
qemu-options.hx +2 -1
     "                hmat=on|off controls ACPI HMAT support (default=off)\n"
     "                memory-backend='backend-id' specifies explicitly provided backend for main RAM (default=none)\n"
-    "                cxl-fmw.0.targets.0=firsttarget,cxl-fmw.0.targets.1=secondtarget,cxl-fmw.0.size=size[,cxl-fmw.0.interleave-granularity=granularity]\n",
+    "                cxl-fmw.0.targets.0=firsttarget,cxl-fmw.0.targets.1=secondtarget,cxl-fmw.0.size=size[,cxl-fmw.0.interleave-granularity=granularity]\n"
+    "                smp-cache.0.cache=cachename,smp-cache.0.topology=topologylevel\n",

Now my cxl-fmw stuff has competition for most hideous element :) When we add a few more properties maybe we'll get an even longer line!

May JSON support can save us :). When I have time I will consider this. Command line's keyval format is more convenient for configuring a single element in an array.

Quoted 6 lines
Quoted 6 lines
qemu-options.hx +0 -0
     QEMU_ARCH_ALL)
 SRST
 ``-machine [type=]name[,prop=value[,...]]``
@@ -159,6 +160,31 @@ SRST
         ::
 
Quoted 9 lines

qemu-options.hx SRST · line 166

Quoted 5 lines
qemu-options.hx +4 -0
             -machine cxl-fmw.0.targets.0=cxl.0,cxl-fmw.0.targets.1=cxl.1,cxl-fmw.0.size=128G,cxl-fmw.0.interleave-granularity=512
+
+    ``smp-cache.0.cache=cachename,smp-cache.0.topology=topologylevel``
+        Define cache properties (now only the cache topology level) for SMP
+        system.

I'd drop the 'now only' bit. Just means we have add noise updating that
later. It's easy enough to look down and see what is available anyway give
the parameter docs follow immediately after this.

Agree.

Quoted 11 lines

qemu-options.hx SRST · line 170

Quoted 4 lines
qemu-options.hx +4 -0
+
+        ``cache=cachename`` specifies the cache that the properties will be
+        applied on. This field is the combination of cache level and cache
+        type. Currently it supports ``l1d`` (L1 data cache), ``l1i`` (L1

Drop the word Currently as I don't think it adds anything to he meaning. We are never going to add docs that say 'previously it supported' or 'in the future it will support'.

	   "Supports ...

Thanks! I will change to "It supports ..."

Quoted 8 lines

qemu-options.hx SRST · line 178

Quoted 8 lines
qemu-options.hx +8 -0
+        instruction cache), ``l2`` (L2 unified cache) and ``l3`` (L3 unified
+        cache).
+
+        ``topology=topologylevel`` sets the cache topology level. It accepts
+        CPU topology levels including ``thread``, ``core``, ``module``,
+        ``cluster``, ``die``, ``socket``, ``book``, ``drawer`` and a special
+        value ``default``. If ``default`` is set, then the cache topology will
+        follow the architecture's default cache topology model. If other CPU
Quoted 4 lines

If another topology level is set

would be clearer. I briefly read this as saying the topology for another CPU
rather than a different value here.

Ah, yes, I agree.

Quoted 4 lines

qemu-options.hx SRST · line 181

Quoted 3 lines
qemu-options.hx +3 -0
+        topology level is set, the cache will be shared at corresponding CPU
+        topology level. For example, ``topology=core`` makes the cache shared
+        in a core.

"by all threads within a core." perhaps?

Nice, it's more accurate.

Thanks,
Zhao

#24 lore raw
In reply to Zhao Liu
Quoted 9 lines · Zhao Liu

Hi all,

Compared with previous Patch v1 [1], I've put the cache properties list into -machine, this is to meet current needs and also remain compatible with my future topology support (more discussion details, pls refer [2]).

This series is based on the commit 1581a0bc928d ("Merge tag 'pull-ufs- 20240906' of https://gitlab.com/jeuk20.kim/qemu into staging ufs queue").

Needs review from QOM maintainers.

Quoted 134 lines

Background
==========

The x86 and ARM (RISCV) need to allow user to configure cache properties (current only topology):
* For x86, the default cache topology model (of max/host CPU) does not always match the Host's real physical cache topology. Performance can increase when the configured virtual topology is closer to the physical topology than a default topology would be.
* For ARM, QEMU can't get the cache topology information from the CPU registers, then user configuration is necessary. Additionally, the cache information is also needed for MPAM emulation (for TCG) to build the right PPTT. (Originally from Jonathan)

About smp-cache
===============

In this version, smp-cache is implemented as a array integrated in -machine. Though -machine currently can't support JSON format, this is the one of the directions of future.

An example is as follows:

smp_cache=smp-cache.0.cache=l1i,smp-cache.0.topology=core,smp-cache.1.cache=l1d,smp-cache.1.topology=core,smp-cache.2.cache=l2,smp-cache.2.topology=module,smp-cache.3.cache=l3,smp-cache.3.topology=die

"cache" specifies the cache that the properties will be applied on. This field is the combination of cache level and cache type. Now it supports "l1d" (L1 data cache), "l1i" (L1 instruction cache), "l2" (L2 unified cache) and "l3" (L3 unified cache).

"topology" field accepts CPU topology levels including "thread", "core", "module", "cluster", "die", "socket", "book", "drawer" and a special value "default".

The "default" is introduced to make it easier for libvirt to set a default parameter value without having to care about the specific machine (because currently there is no proper way for machine to expose supported topology levels and caches).

If "default" is set, then the cache topology will follow the architecture's default cache topology model. If other CPU topology level is set, the cache will be shared at corresponding CPU topology level.

Welcome your comment!

[1]: Patch v1: https://lore.kernel.org/qemu-devel/[email protected]/
[2]: API disscussion: https://lore.kernel.org/qemu-devel/[email protected]/

Thanks and Best Regards,
Zhao
---
Changelog:

Main changes since Patch v1:
* Dropped handwriten smp-cache object and integrated cache properties list into MachineState and used -machine to configure SMP cache properties. (Markus)
* Dropped prefix of CpuTopologyLevel enumeration. (Markus)
* Rename CPU_TOPO_LEVEL_* to CPU_TOPOLOGY_LEVEL_* to match the QAPI's generated code. (Markus)
* Renamed SMPCacheProperty/SMPCacheProperties (QAPI structures) to SmpCacheProperties/SmpCachePropertiesWrapper. (Markus)
* Renamed SMPCacheName (QAPI structure) to SmpCacheLevelAndType and dropped prefix. (Markus)
* Renamed 'name' field in SmpCacheProperties to 'cache', since the type and level of the cache in SMP system could be able to specify all of these kinds of cache explicitly enough.
* Renamed 'topo' field in SmpCacheProperties to 'topology'. (Markus)
* Returned error information when user repeats setting cache properties. (Markus)
* Renamed SmpCacheLevelAndType to CacheLevelAndType, since this representation is general across SMP or hybrid system.
* Dropped machine_check_smp_cache_support() and did the check when -machine parses smp-cache in machine_parse_smp_cache().

Main changes since RFC v2:
* Dropped cpu-topology.h and cpu-topology.c since QAPI has the helper (CpuTopologyLevel_str) to convert enum to string. (Markus)
* Fixed text format in machine.json (CpuTopologyLevel naming, 2 spaces between sentences). (Markus)
* Added a new level "default" to de-compatibilize some arch-specific topo settings. (Daniel)
* Moved CpuTopologyLevel to qapi/machine-common.json, at where the cache enumeration and smp-cache object would be added.
- If smp-cache object is defined in qapi/machine.json, storage-daemon will complain about the qmp cmds in qapi/machine.json during compiling.
* Referred to Daniel's suggestion to introduce cache JSON list, though as a standalone object since -smp/-machine can't support JSON.
* Linked machine's smp_cache to smp-cache object instead of a builtin structure. This is to get around the fact that the keyval format of -machine can't support JSON.
* Wrapped the cache topology level access into a helper.
* Split as a separate commit to just include compatibility checking and topology checking.
* Allow setting "default" topology level even though the cache isn't supported by machine. (Daniel)
* Rewrote the document of smp-cache object.

Main changes since RFC v1:
* Split CpuTopology renaimg out of this RFC.
* Use QAPI to enumerate CPU topology levels.
* Drop string_to_cpu_topo() since QAPI will help to parse the topo levels.
* Set has_*_cache field in machine_get_smp(). (JeeHeng)
* Use "*_cache=topo_level" as -smp example as the original "level" term for a cache has a totally different meaning. (Jonathan)
---
Zhao Liu (7):
hw/core: Make CPU topology enumeration arch-agnostic qapi/qom: Define cache enumeration and properties hw/core: Add smp cache topology for machine
hw/core: Check smp cache topology support for machine i386/cpu: Support thread and module level cache topology i386/cpu: Update cache topology with machine's configuration i386/pc: Support cache topology in -machine for PC machine

 hw/core/machine-smp.c      | 119 +++++++++++++++++++++++
 hw/core/machine.c          |  44 +++++++++
 hw/i386/pc.c               |   4 +
 hw/i386/x86-common.c       |   4 +-
 include/hw/boards.h        |  13 +++
 include/hw/i386/topology.h |  22 +----
 qapi/machine-common.json   |  96 ++++++++++++++++++-
 qemu-options.hx            |  28 +++++-
 target/i386/cpu.c          | 191 ++++++++++++++++++++++---------------
 target/i386/cpu.h          |   4 +-
 10 files changed, 425 insertions(+), 100 deletions(-)
Signature · 1 line
2.34.1
#25 lore raw
In reply to Zhao Liu
Quoted 141 lines · On Sun, 8 Sep 2024 20:59:13 +0800 Zhao Liu <[email protected]> wrote:

Hi all,

Compared with previous Patch v1 [1], I've put the cache properties list into -machine, this is to meet current needs and also remain compatible with my future topology support (more discussion details, pls refer [2]).

This series is based on the commit 1581a0bc928d ("Merge tag 'pull-ufs- 20240906' of https://gitlab.com/jeuk20.kim/qemu into staging ufs queue").

Background
==========

The x86 and ARM (RISCV) need to allow user to configure cache properties (current only topology):
* For x86, the default cache topology model (of max/host CPU) does
not always match the Host's real physical cache topology. Performance can increase when the configured virtual topology is closer to the
physical topology than a default topology would be.
* For ARM, QEMU can't get the cache topology information from the CPU registers, then user configuration is necessary. Additionally, the cache information is also needed for MPAM emulation (for TCG) to build the right PPTT. (Originally from Jonathan)

About smp-cache
===============

In this version, smp-cache is implemented as a array integrated in -machine. Though -machine currently can't support JSON format, this is the one of the directions of future.

An example is as follows:

smp_cache=smp-cache.0.cache=l1i,smp-cache.0.topology=core,smp-cache.1.cache=l1d,smp-cache.1.topology=core,smp-cache.2.cache=l2,smp-cache.2.topology=module,smp-cache.3.cache=l3,smp-cache.3.topology=die

"cache" specifies the cache that the properties will be applied on. This field is the combination of cache level and cache type. Now it supports "l1d" (L1 data cache), "l1i" (L1 instruction cache), "l2" (L2 unified cache) and "l3" (L3 unified cache).

"topology" field accepts CPU topology levels including "thread", "core", "module", "cluster", "die", "socket", "book", "drawer" and a special value "default".

The "default" is introduced to make it easier for libvirt to set a default parameter value without having to care about the specific machine (because currently there is no proper way for machine to expose supported topology levels and caches).

If "default" is set, then the cache topology will follow the architecture's default cache topology model. If other CPU topology level is set, the cache will be shared at corresponding CPU topology level.

Welcome your comment!

[1]: Patch
v1: https://lore.kernel.org/qemu-devel/[email protected]/ [2]: API disscussion: https://lore.kernel.org/qemu-devel/[email protected]/

Thanks and Best Regards,
Zhao
---
Changelog:

Main changes since Patch v1:
* Dropped handwriten smp-cache object and integrated cache properties list into MachineState and used -machine to configure SMP cache properties. (Markus)
* Dropped prefix of CpuTopologyLevel enumeration. (Markus)
* Rename CPU_TOPO_LEVEL_* to CPU_TOPOLOGY_LEVEL_* to match the QAPI's generated code. (Markus)
* Renamed SMPCacheProperty/SMPCacheProperties (QAPI structures) to SmpCacheProperties/SmpCachePropertiesWrapper. (Markus)
* Renamed SMPCacheName (QAPI structure) to SmpCacheLevelAndType and dropped prefix. (Markus)
* Renamed 'name' field in SmpCacheProperties to 'cache', since the type and level of the cache in SMP system could be able to specify all of these kinds of cache explicitly enough.
* Renamed 'topo' field in SmpCacheProperties to 'topology'. (Markus)
* Returned error information when user repeats setting cache properties. (Markus)
* Renamed SmpCacheLevelAndType to CacheLevelAndType, since this representation is general across SMP or hybrid system.
* Dropped machine_check_smp_cache_support() and did the check when -machine parses smp-cache in machine_parse_smp_cache().

Main changes since RFC v2:
* Dropped cpu-topology.h and cpu-topology.c since QAPI has the helper (CpuTopologyLevel_str) to convert enum to string. (Markus)
* Fixed text format in machine.json (CpuTopologyLevel naming, 2
spaces between sentences). (Markus)
* Added a new level "default" to de-compatibilize some arch-specific topo settings. (Daniel)
* Moved CpuTopologyLevel to qapi/machine-common.json, at where the cache enumeration and smp-cache object would be added.
- If smp-cache object is defined in qapi/machine.json,
storage-daemon will complain about the qmp cmds in qapi/machine.json during compiling.
* Referred to Daniel's suggestion to introduce cache JSON list,
though as a standalone object since -smp/-machine can't support JSON.
* Linked machine's smp_cache to smp-cache object instead of a builtin structure. This is to get around the fact that the keyval format of -machine can't support JSON.
* Wrapped the cache topology level access into a helper.
* Split as a separate commit to just include compatibility checking
and topology checking.
* Allow setting "default" topology level even though the cache isn't supported by machine. (Daniel)
* Rewrote the document of smp-cache object.

Main changes since RFC v1:
* Split CpuTopology renaimg out of this RFC.
* Use QAPI to enumerate CPU topology levels.
* Drop string_to_cpu_topo() since QAPI will help to parse the topo levels.
* Set has_*_cache field in machine_get_smp(). (JeeHeng)
* Use "*_cache=topo_level" as -smp example as the original "level" term for a cache has a totally different meaning. (Jonathan)
---
Zhao Liu (7):
hw/core: Make CPU topology enumeration arch-agnostic qapi/qom: Define cache enumeration and properties hw/core: Add smp cache topology for machine
hw/core: Check smp cache topology support for machine i386/cpu: Support thread and module level cache topology i386/cpu: Update cache topology with machine's configuration i386/pc: Support cache topology in -machine for PC machine

 hw/core/machine-smp.c      | 119 +++++++++++++++++++++++
 hw/core/machine.c          |  44 +++++++++
 hw/i386/pc.c               |   4 +
 hw/i386/x86-common.c       |   4 +-
 include/hw/boards.h        |  13 +++
 include/hw/i386/topology.h |  22 +----
 qapi/machine-common.json   |  96 ++++++++++++++++++-
 qemu-options.hx            |  28 +++++-
 target/i386/cpu.c          | 191
Quoted 3 lines

++++++++++++++++++++++--------------- target/i386/cpu.h |
4 +- 10 files changed, 425 insertions(+), 100 deletions(-)

Hi Zhao,

I wonder if this patch-set requires rebase for the new cycle?

Cheers,
Alireza

author
#26 lore raw
Quoted 4 lines

Hi Zhao,

I wonder if this patch-set requires rebase for the new cycle?

Hi Alireza,

Yes, as some general patches are merged.

(Pls refer the v5: https://lore.kernel.org/qemu-devel/[email protected]/)

I'll send a new version this week, to include the remaining patches (i386 path and your has_caches flag):

i386/cpu: Support thread and module level cache topology i386/cpu: Update cache topology with machine's configuration i386/pc: Support cache topology in -machine for PC machine i386/cpu: add has_caches flag to check smp_cache configuration

Regards,
Zhao

#27 lore raw

On Wed, 18 Dec 2024 00:20:53 +0800
Zhao Liu <[email protected]> wrote:

Hi Zhao,

Quoted 11 lines
Quoted 4 lines

Hi Zhao,

I wonder if this patch-set requires rebase for the new cycle?

Hi Alireza,

Yes, as some general patches are merged.

(Pls refer the
v5: https://lore.kernel.org/qemu-devel/[email protected]/)

Oops! Thanks for the pointer.

Quoted 3 lines

I'll send a new version this week, to include the remaining patches (i386 path and your has_caches flag):

That's amazing ;)

Quoted 10 lines

i386/cpu: Support thread and module level cache topology i386/cpu: Update cache topology with machine's configuration i386/pc: Support cache topology in -machine for PC machine i386/cpu: add has_caches flag to check smp_cache configuration

Regards,
Zhao