Skip to contentSkip to conversation

xen/acpi,device-tree: Introduce generic CPU topology support for ACPI and DT

PATCH Superseded Hirokazu Takahashi 2 participants 6 messages Oct 19 – Oct 23 1 message not in the archive

v12 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v12 cover xen/acpi,device-tree: Introduce generic CPU topology support for ACPI and DT

Hello,

This is v12 of the ARM64 CPU topology discovery patch series.

Changes in v12:
- Error reporting for memory allocation failures in init_cpu_topology() into a single error path.
- Emit an error message from init_cpu_topology() when dt_init_cpu_topology() or acpi_init_cpu_topology() returns an error, explicitly indicating that CPU topology initialization has failed.
- Drop the Reviewed-by tag from patch 0001 due to changes in common/cpu-topology.c.
- In dt_smp_init_cpus(), add a comment explaining that the boot core remains valid even if arch_cpu_init() fails.
- dt_init_cpu_topology() returns -EINVAL if a leaf node lacks the cpu phandle property or if the target node is not a valid CPU node.
- dt_init_cpu_topology() continues parsing subsequent threads instead of returning early when a referenced CPU is not tracked by Xen.
- Update the log message when skipping an unmapped CPU to reflect that secondary CPUs have not yet been brought up at this stage.
- Remove trailing periods from log messages.
- Drop the Reviewed-by tag from patch 0001 due to changes in common/cpu-topology.c.

Changes in v11:
- When parsing the Device Tree, ignore CPU nodes if the corresponding physical CPU failed to boot, rather than treating it as an error.
- Drop the dependency on UNSUPPORTED for CPU topology support.
- Print an error message if memory allocation fails in init_cpu_topology().
- Fix a compilation error involving cpumask_set_cpu().
- Call map_cpu_to_dt_node() to record the CPU ID to DT node mapping only when arch_cpu_init() succeeds, or if the CPU is the boot CPU (treated as successful since the boot CPU is already up and running).
- Use XENLOG_ERR as the log level for error messages emitted when DT CPU topology parsing fails.
- Add ASSERT(cpu_topology) to acpi_init_cpu_topology() for consistency.
- Code cleanups:
* Use dt_node_name_is_equal() instead of dt_node_cmp().
* Drop redundant "ERROR:" and "WARNING:" prefixes from log messages.
* Move header file includes into #ifdef / #else blocks where they are actually needed.
* Adjust spacing to comply with the Xen coding style.

Changes in v10:
- Postpone the invocation of init_cpu_topology() until after nr_cpu_ids is finalized.
- Improve the Device Tree cpu-map node parsing logic:
* Rename functions and variables to accurately reflect the DT nodes being processed.
* Simplify the parsing algorithm by eliminating recursive calls and avoiding passing INVALID_TOPO_ID as an argument.
- Unify all license headers to GPL-2.0-only.
- Return an error instead of triggering an ASSERT() when duplicate CPU definitions are detected.
- Replace BUG_ON() with ASSERT() for condition checks in dt_init_cpu_topology().
- Update commit messages to remove details that no longer match the current implementation.

Changes in v9:
- Add bounds checking for the number of private resources in the PPTT processor table.
- Verify the validity of entries when indexing into the map_cpu_acpiid table.
- Coding style adjustments (following Xen standards):
* Place logical operators ('||', '&&') at the end of lines when splitting multi-line conditions.
* Place arithmetic operators ('+', '-') at the end of lines when wrapping long assignment statements.
* Enclose bitwise operations in parentheses within conditional expressions that combine logical operators.
* Combine variable declaration and initialization where applicable.

Changes in v8:
- PPTT Table Validation & Hardening:
* Relaxed processor node length check to allow

    proc->header.length >= table_size.

* Treated root nodes (nodes with parent == 0) as equivalent to ACPI_PPTT_PHYSICAL_PACKAGE nodes.

- Build & Kconfig Adjustments:
* Removed leftovers of ACPI_CPU_TOPOLOGY definition from xen/drivers/acpi/Kconfig.

- Code Cleanups & Style Conformance:
* Moved INVALID_ACPIID definition to xen/acpi.h to make it available cross-architecture.
* Removed ARM-specific references from comments in architecture-agnostic code.
* Added explicit parentheses around bitwise operations in logical expressions.
* Placed binary logical operators (||, &&) at the end of lines when splitting conditional statements.
* Combined variable declarations and initializations where appropriate.
* Removed trailing full stops ('.') from printk log messages.
* Fixed printk format specifier mix-up.
* Explicitly included <xen/cpu-topology.h> in xen/common/cpu.c.

Changes in v7:
- PPTT Table Validation & Hardening:
* find_pptt_node(): Added leaf node verification (ACPI 6.3+).
* acpi_init_cpu_topology(): Added bounds checks to ensure proc->parent validly references memory strictly inside the PPTT table bounds.
* Factored out shared table checking helper functions used by both find_pptt_node() and acpi_init_cpu_topology().
* Added subtable alignment checks and strengthened processor structure size validation.

- Graceful Fallback & Allocation:
* Treated missing topology definitions as benign conditions.
* Used xvzalloc_array() for temporary memory allocations during initialization.

- Build & Kconfig Adjustments:
* Updated Kconfig to use "depends on UNSUPPORTED" instead of "if UNSUPPORTED".

- Code Cleanups & Style Conformance:
* Used designated initializers for structure data initializations.
* Consolidated topology helpers with IS_ENABLED().
* Removed redundant error checks and reduced unnecessary pointer casts.
* Switched type-based sizeof(type) to variable-based sizeof(*var).
* Added leading spaces before labels ( out:)

Changes in v6:
- Renamed the series subject from "xen/arm: Device Tree based CPU topology support" to reflect the inclusion of generic ACPI PPTT topology parsing.

- Infrastructure & Kconfig:
* Updated Kconfig to make both DT_CPU_TOPOLOGY and ACPI_CPU_TOPOLOGY select GENERIC_CPU_TOPOLOGY.
* Ensured that if topology parsing from either DT or ACPI PPTT fails, the CPU topology table is freed to fall back to the non-topology behavior.
* Moved the cpu_topology definition to cpu.c so that variables and functions in cpu-topology.c can be completely freed after Xen init.

- Topology Logic (DT / ACPI):
* Set the cluster ID to 0 when the cluster definition is missing from the Device Tree cpu-map node.
* Handled cases where cluster info is missing upon reaching a physical package in the PPTT parser by assuming one cluster per socket.
* Split out the import of ACPI PPTT definitions from the Linux kernel (including unused definitions) into a separate standalone patch.

- Robustness & Safety:
* Avoided assuming np becomes NULL after exiting dt_for_each_child_node(); explicitly return NULL instead.
* Added bounds-checking ASSERTs for values returned by cpumask_first().
* Added explicit braces {} to nested if statements to clarify else scoping and maintain style symmetry.
* Enforced an upper limit on the PPTT parsing loop iterations to prevent infinite loops on corrupted tables.
* Treat the absence of a physical package node definition in PPTT as a parsing error.

- Optimization & Efficiency:
* Optimized setup_siblings_masks() to eliminate redundant loop iterations.
* Dynamically allocate the temporary data storage used during ACPI PPTT parsing.

- Code Cleanup & Refactoring:
* Replaced the invalid_topo_id constant with the INVALID_TOPO_ID macro.
* Initialized all members of the cpu_map array with INVALID_TOPO_ID.
* Added a blank line between <xen/...> and <asm/...> header groups.
* Reordered #ifdef blocks to prioritize generic logic over architecture- specific ones.
* Corrected code indentation.
* Applied the static specifier to file-local data structures and functions.
* Minimized the use of fixed-width uint32_t types, restricting its use only where strictly required by the 32-bit ACPI ID specification.
* Declared map_cpu_acpiid[] as static and introduced the helper function acpi_map_cpu_acpiid() for managed access.
* Renamed local variables to more intuitive names.
* Simplified the implementation of get_logical_id().
* Refactored PPTT parsing to reduce type casting by using container_of() and switching from char * to void * pointers.
* Removed redundant error checks.
* Cleaned up unused variables and eliminated debug print statements.

Changes in v5:
- Extracted CPU topology information from the ACPI PPTT.
- Corrected the erroneous use of CONFIG_CPU_TOPOLOGY to CONFIG_GENERIC_CPU_TOPOLOGY.

Changes in v4:
- Only display the CPU topology configuration prompt in common/Kconfig if the architecture defines HAS_GENERIC_CPU_TOPOLOGY.
- Move the definition of the global 'cpu_topology' pointer to common/cpu-topology.c.
- Update the Makefile to explicitly build objects as .init.o when all functions and data within a file are annotated with __init/__initdata, ensuring their memory is reclaimed after system initialization.
- Add an error log in the CPU-to-node mapping function for out-of-bounds cases.
- Use ARRAY_SIZE() instead of raw macros when guarding array accesses.
- Rename variables and functions to avoid ambiguous or misleading terms:
- Avoid 'cpuid' to prevent confusion with x86 CPUID features/instructions.
- Avoid 'node' where it could be confused with a NUMA node, explicitly renaming them to clarify they refer to a Device Tree node.
- Move local variable declarations into the narrowest possible scope.
- Replace the unsupported "%pOF" printk format specifier with "%s" and explicit node name retrieval.
- Remove #include <dt-cpu-topology.h> from cpu-topology.h, and ensure the header directly includes only what its definitions require.
- Remove #include <xen/device_tree.h> from dt-cpu-topology.h, replacing it with a forward declaration of 'struct dt_device_node'.
- Use 'const' qualifiers for pointer declarations where the pointed-to structure is not modified.
- Explicitly #include <asm/processor.h> in cpu-topology.h to guarantee that arch-specific definitions of cpu_to_core() and cpu_to_socket() take precedence over the generic fallbacks.
- Introduce inline initialization functions for cpu_sibling_mask and cpu_core_mask in cpu-topology.h, providing separate variants for both when CONFIG_GENERIC_CPU_TOPOLOGY is enabled and disabled.

Changes in v3:
- Use (nr_cpu_ids - 1) as the maximum CPU ID here. The fix for the sparse map mismatch issue on ARM Xen has been split out into a separate patch.
- Switch topology sibling masks to cpumask_var_t for dynamic allocation.
- Allow the system to keep running with a degraded fallback even if the topology table allocation fails.
- Remove the temporary definitions of cpu_to_core() and cpu_to_socket() from RISC-V and PPC processor.h.
- Minimize the use of #ifdef blocks, leveraging compiler Dead Code Elimination (DCE) where possible.
- Clean up the code to follow the Xen coding style. Please let me know if I missed any style nits!
- Verify successful builds across x86, RISC-V, and PPC environments.

Changes in v2:
- Generate topology information even when ACPI is enabled. Note that this is a temporary implementation and doesn't yet parse the PPTT (Processor Properties Topology Table).
- Added support for cpu-map node in Device Tree that doesn't contain explicit cluster node definitions.

Changes in v1 from the previous series "Introduce Device Tree based NUMA support for ARM Xen":

1. Optimized Memory Allocation:
The series now allocates only the minimum required memory area to manage the essential data for the CPUs.

2. Flexible Device Tree Parsing:
The parsing logic no longer depends on the definition order of the 'cpu' nodes and 'cpu-map' nodes in the Device Tree. They can now be read correctly even if their orders do not match.

3. CPU Hotplug Readiness:
To support future CPU hotplug, the system assumes that inactive CPUs are also described in the Device Tree. Xen will pre-load and generate the topology information for these inactive CPUs during the boot phase so it stays available in memory.

Thank you,
Hirokazu Takahashi

Hirokazu Takahashi (4):
xen/device-tree: Parse 'cpu-map' node for CPU topology exploration xen/sched: Link CPU topology to scheduler
xen/sched: Make cpu_nr_siblings() architecture-specific xen/acpi: Parse PPTT to initialize CPU topology

 xen/arch/arm/Kconfig                   |   1 +
 xen/arch/arm/acpi/boot.c               |   2 +
 xen/arch/arm/include/asm/processor.h   |   4 -
 xen/arch/arm/setup.c                   |   3 +
 xen/arch/arm/smpboot.c                 |  18 +-
 xen/arch/ppc/include/asm/processor.h   |   4 -
 xen/arch/riscv/include/asm/processor.h |   4 -
 xen/arch/x86/include/asm/acpi.h        |   2 -
 xen/arch/x86/include/asm/processor.h   |   1 +
 xen/common/Kconfig                     |  22 ++
 xen/common/Makefile                    |   1 +
 xen/common/cpu-topology.c              |  73 +++++
 xen/common/cpu.c                       |   5 +
 xen/common/device-tree/Makefile        |   1 +
 xen/common/device-tree/cpu-topology.c  | 435 +++++++++++++++++++++++++
 xen/common/sched/credit2.c             |  21 +-
 xen/common/sysctl.c                    |   1 +
 xen/drivers/acpi/Makefile              |   1 +
 xen/drivers/acpi/topology.c            | 343 +++++++++++++++++++
 xen/include/xen/acpi.h                 |  17 +
 xen/include/xen/cpu-topology.h         |  76 +++++
 xen/include/xen/dt-cpu-topology.h      |  35 ++
 22 files changed, 1032 insertions(+), 38 deletions(-)
 create mode 100644 xen/common/cpu-topology.c
 create mode 100644 xen/common/device-tree/cpu-topology.c
 create mode 100644 xen/drivers/acpi/topology.c
 create mode 100644 xen/include/xen/cpu-topology.h
 create mode 100644 xen/include/xen/dt-cpu-topology.h
Signature · 1 line
2.43.0
#1 lore raw

PATCH v12 1/4 xen/device-tree: Parse 'cpu-map' node for CPU topology exploration

Parse the 'cpu-map' node in the Device Tree to extract CPU topology information. If the 'cpu-map' node is absent, simply ignore it.

---
Changes in v12:
- Error reporting for memory allocation failures in init_cpu_topology() into a single error path.
- Emit an error message from init_cpu_topology() when dt_init_cpu_topology() or acpi_init_cpu_topology() returns an error, explicitly indicating that CPU topology initialization has failed.
- Drop the Reviewed-by tag from patch 0001 due to changes in common/cpu-topology.c.
- In dt_smp_init_cpus(), add a comment explaining that the boot core remains valid even if arch_cpu_init() fails.
- dt_init_cpu_topology() returns -EINVAL if a leaf node lacks the cpu phandle property or if the target node is not a valid CPU node.
- dt_init_cpu_topology() continues parsing subsequent threads instead of returning early when a referenced CPU is not tracked by Xen.
- Update the log message when skipping an unmapped CPU to reflect that secondary CPUs have not yet been brought up at this stage.
- Remove trailing periods from log messages.
- Drop the Reviewed-by tag from patch 0001 due to changes in common/cpu-topology.c.

 xen/arch/arm/Kconfig                  |   1 +
 xen/arch/arm/setup.c                  |   3 +
 xen/arch/arm/smpboot.c                |  10 +
 xen/common/Kconfig                    |  22 ++
 xen/common/Makefile                   |   1 +
 xen/common/cpu-topology.c             |  73 +++++
 xen/common/cpu.c                      |   5 +
 xen/common/device-tree/Makefile       |   1 +
 xen/common/device-tree/cpu-topology.c | 370 ++++++++++++++++++++++++++
 xen/drivers/acpi/Makefile             |   1 +
 xen/drivers/acpi/topology.c           |  43 +++
 xen/include/xen/acpi.h                |  13 +
 xen/include/xen/cpu-topology.h        |  34 +++
 xen/include/xen/dt-cpu-topology.h     |  35 +++
 14 files changed, 612 insertions(+)
 create mode 100644 xen/common/cpu-topology.c
 create mode 100644 xen/common/device-tree/cpu-topology.c
 create mode 100644 xen/drivers/acpi/topology.c
 create mode 100644 xen/include/xen/cpu-topology.h
 create mode 100644 xen/include/xen/dt-cpu-topology.h
xen/arch/arm/Kconfig +1 -0
index b9c5c40678..3d0f689220 100644
@@ -19,6 +19,7 @@ config ARM
 	select HAS_ALTERNATIVE if HAS_VMAP
 	select HAS_DEVICE_TREE_DISCOVERY
 	select HAS_DOM0LESS
+	select HAS_GENERIC_CPU_TOPOLOGY
 	select HAS_GRANT_CACHE_FLUSH if GRANT_TABLE
 	select HAS_SHARED_INFO
 	select HAS_STACK_PROTECTOR
xen/arch/arm/setup.c +3 -0
index 2cd1c021b5..c1aa06bd31 100644
@@ -10,6 +10,7 @@
 
 #include <xen/bootinfo.h>
 #include <xen/compile.h>
+#include <xen/cpu-topology.h>
 #include <xen/device_tree.h>
 #include <xen/dom0less-build.h>
 #include <xen/domain_page.h>
@@ -375,6 +376,8 @@ void asmlinkage __init noreturn start_xen(unsigned long fdt_paddr)
     nr_cpu_ids = smp_get_max_cpus();
     printk(XENLOG_INFO "SMP: Allowing %u CPUs\n", nr_cpu_ids);
 
+    init_cpu_topology();
+
     /*
      * Some errata relies on SMCCC version which is detected by psci_init()
      * (called from smp_init_cpus()).
xen/arch/arm/smpboot.c +10 -0
index 8c9072f3c1..d34396f9b1 100644
@@ -9,10 +9,12 @@
 
 #include <xen/acpi.h>
 #include <xen/cpu.h>
+#include <xen/cpu-topology.h>
 #include <xen/cpumask.h>
 #include <xen/delay.h>
 #include <xen/device_tree.h>
 #include <xen/domain_page.h>
+#include <xen/dt-cpu-topology.h>
 #include <xen/errno.h>
 #include <xen/init.h>
 #include <xen/mm.h>
@@ -245,6 +247,14 @@ static void __init dt_smp_init_cpus(void)
         }
         else
             tmp_map[i] = hwid;
+
+        /*
+         * Because the boot CPU (i == 0) is already running, treat it as valid
+         * even if arch_cpu_init() fails (e.g. due to a missing enable-method).
+         */
+        if ( (rc >= 0) || (i == 0) )
+            /* Pass the info to dt_init_cpu_topology() */
+            map_cpu_to_dt_node(i, cpu);
     }
 
     if ( !bootcpu_valid )
xen/common/Kconfig +22 -0
index 5b289e444f..fb42f0aa8b 100644
@@ -140,6 +140,9 @@ config HAS_EX_TABLE
 config HAS_FAST_MULTIPLY
 	bool
 
+config HAS_GENERIC_CPU_TOPOLOGY
+	bool
+
 config HAS_IOPORTS
 	bool
 
@@ -194,6 +197,25 @@ config VM_EVENT
 config NEEDS_LIBELF
 	bool
 
+config GENERIC_CPU_TOPOLOGY
+	bool
+
+config DT_CPU_TOPOLOGY
+	bool "Device tree based CPU topology support"
+	depends on HAS_GENERIC_CPU_TOPOLOGY && DEVICE_TREE_PARSE
+	select GENERIC_CPU_TOPOLOGY
+	help
+	  Retrieve CPU topology information from the device tree to optimize
+	  vCPU scheduling.
+
+config ACPI_CPU_TOPOLOGY
+	bool "ACPI based CPU topology support"
+	depends on HAS_GENERIC_CPU_TOPOLOGY && ACPI
+	select GENERIC_CPU_TOPOLOGY
+	help
+	  Retrieve CPU topology information from the ACPI PPTT to optimize
+	  vCPU scheduling.
+
 config NUMA
 	bool
 
xen/common/Makefile +1 -0
index f69d47d189..d0eb39a633 100644
@@ -5,6 +5,7 @@ obj-$(CONFIG_GENERIC_BUG_FRAME) += bug.o
 obj-$(CONFIG_HYPFS_CONFIG) += config_data.o
 obj-$(CONFIG_CORE_PARKING) += core_parking.o
 obj-y += cpu.o
+obj-$(CONFIG_GENERIC_CPU_TOPOLOGY) += cpu-topology.init.o
 obj-$(CONFIG_DEBUG_TRACE) += debugtrace.o
 obj-$(CONFIG_HAS_DEVICE_TREE_DISCOVERY) += device.o
 obj-$(filter-out $(CONFIG_X86),$(CONFIG_ACPI)) += device.o
xen/common/cpu-topology.c new file +73 -0

73 lines not on this page · raw message

xen/common/cpu.c +5 -0
index f09af0444b..9591ada60a 100644
@@ -1,5 +1,6 @@
 #include <xen/cpumask.h>
 #include <xen/cpu.h>
+#include <xen/cpu-topology.h>
 #include <xen/event.h>
 #include <xen/init.h>
 #include <xen/sched.h>
@@ -46,6 +47,10 @@ const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
 #undef MASK_DECLARE_2
 #undef MASK_DECLARE_1
 
+#ifdef CONFIG_GENERIC_CPU_TOPOLOGY
+struct cpu_topology *__ro_after_init cpu_topology;
+#endif /* CONFIG_GENERIC_CPU_TOPOLOGY */
+
 static DEFINE_RWLOCK(cpu_add_remove_lock);
 
 bool get_cpu_maps(void)
xen/common/device-tree/Makefile +1 -0
index 730d202349..b15b3382d2 100644
@@ -1,6 +1,7 @@
 obj-y += bootfdt.init.o
 obj-$(CONFIG_HAS_DEVICE_TREE_DISCOVERY) += bootinfo-fdt.init.o
 obj-$(CONFIG_HAS_DEVICE_TREE_DISCOVERY) += bootinfo.init.o
+obj-$(CONFIG_DT_CPU_TOPOLOGY) += cpu-topology.init.o
 obj-y += device-tree.o
 obj-$(CONFIG_DOMAIN_BUILD_HELPERS) += domain-build.init.o
 obj-$(filter $(CONFIG_DOM0LESS_BOOT),$(CONFIG_HAS_DEVICE_TREE_DISCOVERY)) += dom0less-build.init.o
xen/common/device-tree/cpu-topology.c new file +370 -0

370 lines not on this page · raw message

xen/drivers/acpi/Makefile +1 -0
index 477408afbe..6d676e91d4 100644
@@ -7,6 +7,7 @@ obj-$(CONFIG_ACPI_NUMA) += numa.o
 obj-y += osl.o
 obj-$(CONFIG_PM_STATS) += pmstat.o
 obj-$(CONFIG_PM_OP) += pm-op.o
+obj-$(CONFIG_ACPI_CPU_TOPOLOGY) += topology.init.o
 
 obj-$(CONFIG_X86) += hwregs.o
 obj-$(CONFIG_X86) += reboot.o
xen/drivers/acpi/topology.c new file +43 -0

43 lines not on this page · raw message

xen/include/xen/acpi.h +13 -0
index 2fdf38cf74..cbb02e0f35 100644
@@ -135,6 +135,19 @@ static inline int acpi_boot_table_init(void)
 
 #endif 	/*!CONFIG_ACPI*/
 
+#ifdef CONFIG_ACPI_CPU_TOPOLOGY
+
+int acpi_init_cpu_topology(void);
+
+#else /* CONFIG_ACPI_CPU_TOPOLOGY */
+
+static inline int acpi_init_cpu_topology(void)
+{
+    return -EOPNOTSUPP;
+}
+
+#endif /* CONFIG_ACPI_CPU_TOPOLOGY */
+
 int get_cpu_id(u32 acpi_id);
 
 unsigned int acpi_register_gsi (u32 gsi, int edge_level, int active_high_low);
xen/include/xen/cpu-topology.h new file +34 -0
new file mode 100644
index 0000000000..27b9bd443e
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef XEN_CPU_TOPOLOGY_H
+#define XEN_CPU_TOPOLOGY_H
+
+#ifdef CONFIG_GENERIC_CPU_TOPOLOGY
+
+#include <xen/cpumask.h>
+
+struct cpu_topology {
+    cpumask_var_t thread_sibling;
+    cpumask_var_t core_sibling;
+    cpumask_var_t cluster_sibling;
+};
+
+extern struct cpu_topology *cpu_topology;
+void init_cpu_topology(void);
+
+#else /* CONFIG_GENERIC_CPU_TOPOLOGY */
+
+static inline void init_cpu_topology(void) {}
+
+#endif /* CONFIG_GENERIC_CPU_TOPOLOGY */
+
+#endif /* XEN_CPU_TOPOLOGY_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/include/xen/dt-cpu-topology.h new file +35 -0
new file mode 100644
index 0000000000..43f7b31bf6
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef XEN_DT_CPU_TOPOLOGY_H
+#define XEN_DT_CPU_TOPOLOGY_H
+
+struct dt_device_node;
+
+#ifdef CONFIG_DT_CPU_TOPOLOGY
+
+void map_cpu_to_dt_node(unsigned int cpu, struct dt_device_node *cpu_node);
+int dt_init_cpu_topology(void);
+
+#else /* CONFIG_DT_CPU_TOPOLOGY */
+
+#include <xen/errno.h>
+
+static inline void map_cpu_to_dt_node(unsigned int cpu,
+                                      struct dt_device_node *cpu_node) {}
+static inline int dt_init_cpu_topology(void)
+{
+    return -EOPNOTSUPP;
+}
+
+#endif /* CONFIG_DT_CPU_TOPOLOGY */
+
+#endif /* XEN_DT_CPU_TOPOLOGY_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v12 1/4] xen/device-tree: Parse 'cpu-map' node for CPU topology exploration

Quoted 25 lines · Hirokazu Takahashi

Parse the 'cpu-map' node in the Device Tree to extract CPU topology information. If the 'cpu-map' node is absent, simply ignore it.

---
Changes in v12:
- Error reporting for memory allocation failures in init_cpu_topology() into a single error path.
- Emit an error message from init_cpu_topology() when dt_init_cpu_topology() or acpi_init_cpu_topology() returns an error, explicitly indicating that CPU topology initialization has failed.
- Drop the Reviewed-by tag from patch 0001 due to changes in common/cpu-topology.c.
- In dt_smp_init_cpus(), add a comment explaining that the boot core remains valid even if arch_cpu_init() fails.
- dt_init_cpu_topology() returns -EINVAL if a leaf node lacks the cpu phandle property or if the target node is not a valid CPU node.
- dt_init_cpu_topology() continues parsing subsequent threads instead of returning early when a referenced CPU is not tracked by Xen.
- Update the log message when skipping an unmapped CPU to reflect that secondary CPUs have not yet been brought up at this stage.
- Remove trailing periods from log messages.
- Drop the Reviewed-by tag from patch 0001 due to changes in common/cpu-topology.c.

Quoted 11 lines
 xen/arch/arm/Kconfig                  |   1 +
 xen/arch/arm/setup.c                  |   3 +
 xen/arch/arm/smpboot.c                |  10 +
 xen/common/Kconfig                    |  22 ++
 xen/common/Makefile                   |   1 +
 xen/common/cpu-topology.c             |  73 +++++
 xen/common/cpu.c                      |   5 +
 xen/common/device-tree/Makefile       |   1 +
 xen/common/device-tree/cpu-topology.c | 370 ++++++++++++++++++++++++++
 xen/drivers/acpi/Makefile             |   1 +
 xen/drivers/acpi/topology.c           |  43 +++

Shouldn't this file be called cpu-topology like the DT equivalent?

Quoted 48 lines
 xen/include/xen/acpi.h                |  13 +
 xen/include/xen/cpu-topology.h        |  34 +++
 xen/include/xen/dt-cpu-topology.h     |  35 +++
 14 files changed, 612 insertions(+)
 create mode 100644 xen/common/cpu-topology.c
 create mode 100644 xen/common/device-tree/cpu-topology.c
 create mode 100644 xen/drivers/acpi/topology.c
 create mode 100644 xen/include/xen/cpu-topology.h
 create mode 100644 xen/include/xen/dt-cpu-topology.h
xen/arch/arm/Kconfig +1 -0
index b9c5c40678..3d0f689220 100644
@@ -19,6 +19,7 @@ config ARM
 	select HAS_ALTERNATIVE if HAS_VMAP
 	select HAS_DEVICE_TREE_DISCOVERY
 	select HAS_DOM0LESS
+	select HAS_GENERIC_CPU_TOPOLOGY
 	select HAS_GRANT_CACHE_FLUSH if GRANT_TABLE
 	select HAS_SHARED_INFO
 	select HAS_STACK_PROTECTOR
xen/arch/arm/setup.c +3 -0
index 2cd1c021b5..c1aa06bd31 100644
@@ -10,6 +10,7 @@
 
 #include <xen/bootinfo.h>
 #include <xen/compile.h>
+#include <xen/cpu-topology.h>
 #include <xen/device_tree.h>
 #include <xen/dom0less-build.h>
 #include <xen/domain_page.h>
@@ -375,6 +376,8 @@ void asmlinkage __init noreturn start_xen(unsigned long fdt_paddr)
     nr_cpu_ids = smp_get_max_cpus();
     printk(XENLOG_INFO "SMP: Allowing %u CPUs\n", nr_cpu_ids);
 
+    init_cpu_topology();
+
     /*
      * Some errata relies on SMCCC version which is detected by psci_init()
      * (called from smp_init_cpus()).
xen/arch/arm/smpboot.c +0 -0
index 8c9072f3c1..d34396f9b1 100644

xen/arch/arm/smpboot.c line 17

Quoted 9 lines
xen/arch/arm/smpboot.c +2 -0
@@ -9,10 +9,12 @@
 
 #include <xen/acpi.h>
 #include <xen/cpu.h>
+#include <xen/cpu-topology.h>
 #include <xen/cpumask.h>
 #include <xen/delay.h>
 #include <xen/device_tree.h>
 #include <xen/domain_page.h>
+#include <xen/dt-cpu-topology.h>

Nothing in this file seems to use this header

Quoted 110 lines
xen/arch/arm/smpboot.c +8 -0
 #include <xen/errno.h>
 #include <xen/init.h>
 #include <xen/mm.h>
@@ -245,6 +247,14 @@ static void __init dt_smp_init_cpus(void)
         }
         else
             tmp_map[i] = hwid;
+
+        /*
+         * Because the boot CPU (i == 0) is already running, treat it as valid
+         * even if arch_cpu_init() fails (e.g. due to a missing enable-method).
+         */
+        if ( (rc >= 0) || (i == 0) )
+            /* Pass the info to dt_init_cpu_topology() */
+            map_cpu_to_dt_node(i, cpu);
     }
 
     if ( !bootcpu_valid )
xen/common/Kconfig +22 -0
index 5b289e444f..fb42f0aa8b 100644
@@ -140,6 +140,9 @@ config HAS_EX_TABLE
 config HAS_FAST_MULTIPLY
 	bool
 
+config HAS_GENERIC_CPU_TOPOLOGY
+	bool
+
 config HAS_IOPORTS
 	bool
 
@@ -194,6 +197,25 @@ config VM_EVENT
 config NEEDS_LIBELF
 	bool
 
+config GENERIC_CPU_TOPOLOGY
+	bool
+
+config DT_CPU_TOPOLOGY
+	bool "Device tree based CPU topology support"
+	depends on HAS_GENERIC_CPU_TOPOLOGY && DEVICE_TREE_PARSE
+	select GENERIC_CPU_TOPOLOGY
+	help
+	  Retrieve CPU topology information from the device tree to optimize
+	  vCPU scheduling.
+
+config ACPI_CPU_TOPOLOGY
+	bool "ACPI based CPU topology support"
+	depends on HAS_GENERIC_CPU_TOPOLOGY && ACPI
+	select GENERIC_CPU_TOPOLOGY
+	help
+	  Retrieve CPU topology information from the ACPI PPTT to optimize
+	  vCPU scheduling.
+
 config NUMA
 	bool
 
xen/common/Makefile +1 -0
index f69d47d189..d0eb39a633 100644
@@ -5,6 +5,7 @@ obj-$(CONFIG_GENERIC_BUG_FRAME) += bug.o
 obj-$(CONFIG_HYPFS_CONFIG) += config_data.o
 obj-$(CONFIG_CORE_PARKING) += core_parking.o
 obj-y += cpu.o
+obj-$(CONFIG_GENERIC_CPU_TOPOLOGY) += cpu-topology.init.o
 obj-$(CONFIG_DEBUG_TRACE) += debugtrace.o
 obj-$(CONFIG_HAS_DEVICE_TREE_DISCOVERY) += device.o
 obj-$(filter-out $(CONFIG_X86),$(CONFIG_ACPI)) += device.o
xen/common/cpu-topology.c new file +34 -0
new file mode 100644
index 0000000000..8c64532e28
@@ -0,0 +1,73 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/acpi.h>
+#include <xen/cpu-topology.h>
+#include <xen/cpumask.h>
+#include <xen/dt-cpu-topology.h>
+#include <xen/init.h>
+#include <xen/xvmalloc.h>
+
+static void __init free_topology_table(void)
+{
+    unsigned int cpu;
+
+    if ( !cpu_topology )
+        return;
+
+    for ( cpu = 0; cpu < nr_cpu_ids; cpu++ )
+    {
+        free_cpumask_var(cpu_topology[cpu].thread_sibling);
+        free_cpumask_var(cpu_topology[cpu].core_sibling);
+        free_cpumask_var(cpu_topology[cpu].cluster_sibling);
+    }
+
+    XVFREE(cpu_topology);
+}
+
+void __init init_cpu_topology(void)
+{
+    unsigned int cpu;
+    int ret = 0;
+
+    if ( num_possible_cpus() == 1 )
+        return;
+

xen/common/cpu-topology.c line 44

Quoted 10 lines
xen/common/cpu-topology.c new file +10 -0
+    cpu_topology = xvzalloc_array(struct cpu_topology, nr_cpu_ids);
+
+    if ( cpu_topology )
+    {
+        for ( cpu = 0; cpu < nr_cpu_ids; cpu++ )
+        {
+            if ( !zalloc_cpumask_var(&cpu_topology[cpu].thread_sibling) ||
+                 !zalloc_cpumask_var(&cpu_topology[cpu].core_sibling) ||
+                 !zalloc_cpumask_var(&cpu_topology[cpu].cluster_sibling) )
+                ret = -ENOMEM;

The loop continues allocating after the first error which is incorrect.

xen/common/cpu-topology.c line 51

Quoted 7 lines
xen/common/cpu-topology.c new file +7 -0
+        }
+    }
+    else
+        ret = -ENOMEM;
+
+    if ( ret != 0 )
+        printk(XENLOG_WARNING "CPU topology table allocation failed\n");

After printing this one we will also print the "Failed to initialize..." one. Please goto the error.

xen/common/cpu-topology.c line 58

Quoted 7 lines
xen/common/cpu-topology.c new file +7 -0
+    else if ( acpi_disabled )
+        ret = dt_init_cpu_topology();
+    else
+        ret = acpi_init_cpu_topology();
+
+    /* Free the CPU topology table if initialization fails. */
+    if ( ret != 0 )

In v11 I asked you to print error on real failure, not all the time e.g. on - EOPNOTSUPP or ENOENT as a result of not finding cpu-map.

xen/common/cpu-topology.c line 60

Quoted 2 lines
xen/common/cpu-topology.c new file +2 -0
+    {
+        printk("Failed to initialize cpu topology (error %d)\n", ret);

XENLOG_ERR/WARNING?

Quoted 201 lines
xen/common/cpu-topology.c new file +13 -0
+        free_topology_table();
+    }
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/common/cpu.c +5 -0
index f09af0444b..9591ada60a 100644
@@ -1,5 +1,6 @@
 #include <xen/cpumask.h>
 #include <xen/cpu.h>
+#include <xen/cpu-topology.h>
 #include <xen/event.h>
 #include <xen/init.h>
 #include <xen/sched.h>
@@ -46,6 +47,10 @@ const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
 #undef MASK_DECLARE_2
 #undef MASK_DECLARE_1
 
+#ifdef CONFIG_GENERIC_CPU_TOPOLOGY
+struct cpu_topology *__ro_after_init cpu_topology;
+#endif /* CONFIG_GENERIC_CPU_TOPOLOGY */
+
 static DEFINE_RWLOCK(cpu_add_remove_lock);
 
 bool get_cpu_maps(void)
xen/common/device-tree/Makefile +1 -0
index 730d202349..b15b3382d2 100644
@@ -1,6 +1,7 @@
 obj-y += bootfdt.init.o
 obj-$(CONFIG_HAS_DEVICE_TREE_DISCOVERY) += bootinfo-fdt.init.o
 obj-$(CONFIG_HAS_DEVICE_TREE_DISCOVERY) += bootinfo.init.o
+obj-$(CONFIG_DT_CPU_TOPOLOGY) += cpu-topology.init.o
 obj-y += device-tree.o
 obj-$(CONFIG_DOMAIN_BUILD_HELPERS) += domain-build.init.o
 obj-$(filter $(CONFIG_DOM0LESS_BOOT),$(CONFIG_HAS_DEVICE_TREE_DISCOVERY)) += dom0less-build.init.o
xen/common/device-tree/cpu-topology.c new file +148 -0
new file mode 100644
index 0000000000..bfcf153cc4
@@ -0,0 +1,370 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Derived from Linux kernel 7.0's $drivers/base/arch_topology.c
+ * Parse cpu topology information.
+ */
+
+#include <xen/acpi.h>
+#include <xen/cpu-topology.h>
+#include <xen/cpumask.h>
+#include <xen/device_tree.h>
+#include <xen/errno.h>
+#include <xen/init.h>
+
+#define INVALID_TOPO_ID (~0U)
+
+struct cpu_map {
+    unsigned int thread_id;
+    unsigned int core_id;
+    unsigned int cluster_id;
+    unsigned int socket_id;
+};
+
+static struct cpu_map __initdata cpu_map[NR_CPUS] = {
+    [0 ... NR_CPUS - 1] = {
+        .thread_id = INVALID_TOPO_ID,
+        .core_id = INVALID_TOPO_ID,
+        .cluster_id = INVALID_TOPO_ID,
+        .socket_id = INVALID_TOPO_ID,
+    },
+};
+static struct dt_device_node *__initdata dt_cpu_table[NR_CPUS];
+
+static void __init setup_siblings_masks(unsigned int target_cpu)
+{
+    struct cpu_topology *target_topo = &cpu_topology[target_cpu];
+    const struct cpu_map *target_map = &cpu_map[target_cpu];
+    unsigned int cpu;
+
+    /* Update cluster, core and thread sibling masks */
+    for_each_possible_cpu(cpu)
+    {
+        struct cpu_topology *cpu_topo = &cpu_topology[cpu];
+        const struct cpu_map *map = &cpu_map[cpu];
+
+        if ( target_cpu > cpu )
+            continue;
+
+        if ( target_map->socket_id != map->socket_id )
+            continue;
+
+        cpumask_set_cpu(target_cpu, cpu_topo->core_sibling);
+        cpumask_set_cpu(cpu, target_topo->core_sibling);
+
+        if ( target_map->cluster_id != map->cluster_id )
+            continue;
+
+        cpumask_set_cpu(target_cpu, cpu_topo->cluster_sibling);
+        cpumask_set_cpu(cpu, target_topo->cluster_sibling);
+
+        if ( target_map->core_id != map->core_id )
+            continue;
+
+        cpumask_set_cpu(target_cpu, cpu_topo->thread_sibling);
+        cpumask_set_cpu(cpu, target_topo->thread_sibling);
+    }
+}
+
+static const struct dt_device_node *__init dt_find_child_node_by_name(
+    const struct dt_device_node *dt,
+    const char *name)
+{
+    const struct dt_device_node *np;
+
+    dt_for_each_child_node(dt, np)
+        if ( np->name && dt_node_name_is_equal(np, name) )
+            return np;
+
+    return NULL;
+}
+
+void __init map_cpu_to_dt_node(unsigned int cpu,
+                               struct dt_device_node *cpu_node)
+{
+    if ( cpu < ARRAY_SIZE(dt_cpu_table) )
+        dt_cpu_table[cpu] = cpu_node;
+    else
+        printk(XENLOG_WARNING
+               "cpu %u exceeds the max cpus %zu\n",
+               cpu, ARRAY_SIZE(dt_cpu_table));
+}
+
+static unsigned int __init cpu_node_to_id(
+    const struct dt_device_node *cpu_node)
+{
+    unsigned int cpu;
+
+    for_each_possible_cpu(cpu)
+        if ( cpu_node == dt_cpu_table[cpu] )
+            return cpu;
+
+    return INVALID_TOPO_ID;
+}
+
+/*
+ * This function returns the Xen cpu number of the DT node.
+ */
+static int __init get_cpu_for_node(
+    const struct dt_device_node *dt_node, unsigned int *cpu)
+{
+    const struct dt_device_node *cpu_node =
+        dt_parse_phandle(dt_node, "cpu", 0);
+
+    if ( !cpu_node || !dt_device_type_is_equal(cpu_node, "cpu") )
+    {
+        *cpu = INVALID_TOPO_ID;
+        return -EINVAL;
+    }
+
+    *cpu = cpu_node_to_id(cpu_node);
+
+    return 0;
+}
+
+static bool __init is_cpu_map_empty(unsigned int cpu)
+{
+    return (cpu_map[cpu].socket_id == INVALID_TOPO_ID) &&
+           (cpu_map[cpu].cluster_id == INVALID_TOPO_ID) &&
+           (cpu_map[cpu].core_id == INVALID_TOPO_ID) &&
+           (cpu_map[cpu].thread_id == INVALID_TOPO_ID);
+}
+
+static int __init parse_core(const struct dt_device_node *core,
+                             unsigned int socket_id,
+                             unsigned int cluster_id,
+                             unsigned int core_id)
+{
+    bool leaf = true;
+    unsigned int thread_id;
+    unsigned int cpu;
+    int ret;
+
+    for ( thread_id = 0; ; thread_id++ )
+    {
+        const struct dt_device_node *thread;
+        char name[20];
+
+        snprintf(name, sizeof(name), "thread%u", thread_id);
+        thread = dt_find_child_node_by_name(core, name);

xen/common/device-tree/cpu-topology.c line 158

Quoted 10 lines
xen/common/device-tree/cpu-topology.c new file +10 -0
+
+        if ( !thread )
+            break;
+
+        leaf = false;
+        ret = get_cpu_for_node(thread, &cpu);
+        if ( ret )
+        {
+            printk(XENLOG_ERR
+                   "%s: No CPU found for thread\n", dt_node_name(thread));

Use dt_node_full_name(). Otherwise the printed name can be ambiguous.

xen/common/device-tree/cpu-topology.c line 166

Quoted 8 lines
xen/common/device-tree/cpu-topology.c new file +8 -0
+            return ret;
+        }
+
+        if ( cpu == INVALID_TOPO_ID )
+        {
+            /*
+             * Ignore this CPU node definition as the corresponding physical
+             * CPU failed to boot.

You updated the printk message but not the comment. Please fix.

~Michal

#3 lore raw

RE: [PATCH v12 1/4] xen/device-tree: Parse 'cpu-map' node for CPU topology exploration

Hi Michal,

Quoted 4 lines
Quoted 3 lines
 xen/common/device-tree/cpu-topology.c | 370 ++++++++++++++++++++++++++
 xen/drivers/acpi/Makefile             |   1 +
 xen/drivers/acpi/topology.c           |  43 +++

Shouldn't this file be called cpu-topology like the DT equivalent?

Okay.

Quoted 6 lines
Quoted 6 lines
xen/arch/arm/smpboot.c +0 -0
index 8c9072f3c1..d34396f9b1 100644
@@ -9,10 +9,12 @@
 
Quoted 9 lines

xen/arch/arm/smpboot.c line 17

Quoted 8 lines
xen/arch/arm/smpboot.c +2 -0
 #include <xen/acpi.h>
 #include <xen/cpu.h>
+#include <xen/cpu-topology.h>
 #include <xen/cpumask.h>
 #include <xen/delay.h>
 #include <xen/device_tree.h>
 #include <xen/domain_page.h>
+#include <xen/dt-cpu-topology.h>

Nothing in this file seems to use this header

This header is required because this file uses map_cpu_to_dt_node(). This extern declaration is provided in it.

Quoted 19 lines
Quoted 5 lines
xen/arch/arm/smpboot.c +5 -0
+void __init init_cpu_topology(void)
+{
+    unsigned int cpu;
+    int ret = 0;
+

xen/arch/arm/smpboot.c

Quoted 10 lines
xen/arch/arm/smpboot.c +10 -0
+    if ( num_possible_cpus() == 1 )
+        return;
+
+    cpu_topology = xvzalloc_array(struct cpu_topology, nr_cpu_ids);
+
+    if ( cpu_topology )
+    {
+        for ( cpu = 0; cpu < nr_cpu_ids; cpu++ )
+        {
+            if ( !zalloc_cpumask_var(&cpu_topology[cpu].thread_sibling)
||
> +                 !zalloc_cpumask_var(&cpu_topology[cpu].core_sibling)
||
Quoted 1 line
Quoted 4 lines

+ !zalloc_cpumask_var(&cpu_topology[cpu].cluster_sibling)
)

Quoted 1 line

+ ret = -ENOMEM;

The loop continues allocating after the first error which is incorrect.

You are right. I will fix it.

Quoted 9 lines

xen/arch/arm/smpboot.c

Quoted 7 lines
xen/arch/arm/smpboot.c +7 -0
+        }
+    }
+    else
+        ret = -ENOMEM;
+
+    if ( ret != 0 )
+        printk(XENLOG_WARNING "CPU topology table allocation failed\n");

After printing this one we will also print the "Failed to initialize..." one. Please goto the error.

Okay.

Quoted 9 lines

xen/arch/arm/smpboot.c

Quoted 7 lines
xen/arch/arm/smpboot.c +7 -0
+    else if ( acpi_disabled )
+        ret = dt_init_cpu_topology();
+    else
+        ret = acpi_init_cpu_topology();
+
+    /* Free the CPU topology table if initialization fails. */
+    if ( ret != 0 )

In v11 I asked you to print error on real failure, not all the time e.g. on - EOPNOTSUPP or ENOENT as a result of not finding cpu-map.

Okay

Quoted 3 lines

xen/arch/arm/smpboot.c

Quoted 2 lines
xen/arch/arm/smpboot.c +2 -0
+    {
+        printk("Failed to initialize cpu topology (error %d)\n", ret);

XENLOG_ERR/WARNING?

I will fix it.

Quoted 10 lines

xen/arch/arm/smpboot.c

Quoted 9 lines
xen/arch/arm/smpboot.c +9 -0
+        if ( !thread )
+            break;
+
+        leaf = false;
+        ret = get_cpu_for_node(thread, &cpu);
+        if ( ret )
+        {
+            printk(XENLOG_ERR
+                   "%s: No CPU found for thread\n", dt_node_name(thread));

Use dt_node_full_name(). Otherwise the printed name can be ambiguous.

Okay.

Quoted 9 lines

xen/arch/arm/smpboot.c

Quoted 8 lines
xen/arch/arm/smpboot.c +8 -0
+            return ret;
+        }
+
+        if ( cpu == INVALID_TOPO_ID )
+        {
+            /*
+             * Ignore this CPU node definition as the corresponding physical
+             * CPU failed to boot.

You updated the printk message but not the comment. Please fix.

Okay

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw

Re: [PATCH v12 2/4] xen/sched: Link CPU topology to scheduler

Quoted 40 lines · Hirokazu Takahashi

Make CPU topology information available to the Xen scheduler. Additionally, ensure that this topology information is displayed when executing the 'xl info -n' command.

---
 xen/arch/arm/include/asm/processor.h   |  4 --
 xen/arch/arm/smpboot.c                 |  8 +---
 xen/arch/ppc/include/asm/processor.h   |  4 --
 xen/arch/riscv/include/asm/processor.h |  4 --
 xen/common/device-tree/cpu-topology.c  | 65 ++++++++++++++++++++++++++
 xen/common/sched/credit2.c             |  6 +++
 xen/common/sysctl.c                    |  1 +
 xen/drivers/acpi/topology.c            |  3 ++
 xen/include/xen/cpu-topology.h         | 41 +++++++++++++++-
 9 files changed, 115 insertions(+), 21 deletions(-)
xen/arch/arm/include/asm/processor.h +0 -4
index 8ee8f88fb4..fe4125cd75 100644
@@ -614,10 +614,6 @@ void show_stack(const struct cpu_user_regs *regs);
 
 #define cpu_relax() barrier() /* Could yield? */
 
-/* All a bit UP for the moment */
-#define cpu_to_core(_cpu)   (0)
-#define cpu_to_socket(_cpu) (0)
-
 struct vcpu;
 void vcpu_regs_hyp_to_user(const struct vcpu *vcpu,
                            struct vcpu_guest_core_regs *regs);
xen/arch/arm/smpboot.c +0 -0
index d34396f9b1..cf4858eb37 100644
@@ -93,13 +93,7 @@ static int setup_cpu_sibling_map(int cpu)
          !zalloc_cpumask_var(&per_cpu(cpu_core_mask, cpu)) )

xen/arch/arm/smpboot.c static int setup_cpu_sibling_map(int cpu) · line 96

Quoted 10 lines
xen/arch/arm/smpboot.c +1 -7
         return -ENOMEM;
 
-    /*
-     * Currently we assume there is no multithread and NUMA, so
-     * a CPU is a sibling with itself, and the all possible CPUs
-     * are supposed to belong to the same socket (NUMA node).
-     */
-    cpumask_set_cpu(cpu, per_cpu(cpu_sibling_mask, cpu));
-    cpumask_copy(per_cpu(cpu_core_mask, cpu), &cpu_possible_map);
+    init_cpu_sibling_map(cpu);

The comment above the cpu_sibling_mask definition is now stale.

Quoted 55 lines
xen/arch/arm/smpboot.c +0 -0
     return 0;
 }
xen/arch/ppc/include/asm/processor.h +0 -4
index 242346cab9..1bf6f6c66c 100644
@@ -141,10 +141,6 @@
 /* Macro to adjust thread priority for hardware multithreading */
 #define HMT_very_low()  asm volatile ( "or %r31, %r31, %r31" )
 
-/* TODO: This isn't correct */
-#define cpu_to_core(cpu)   (0)
-#define cpu_to_socket(cpu) (0)
-
 /*
  * User-accessible registers: most of these need to be saved/restored
  * for every nested Xen invocation.
xen/arch/riscv/include/asm/processor.h +0 -4
index b1745c1071..c5340a924f 100644
@@ -52,10 +52,6 @@ struct cpu_user_regs
     unsigned long hstatus;
 };
 
-/* TODO: need to implement */
-#define cpu_to_core(cpu)   0
-#define cpu_to_socket(cpu) 0
-
 static inline void cpu_relax(void)
 {
 #ifdef __riscv_zihintpause
xen/common/device-tree/cpu-topology.c +14 -0
index bfcf153cc4..b7b1acc73d 100644
@@ -344,6 +344,67 @@ static int __init parse_dt_topology(void)
     return parse_package(map);
 }
 
+static void __init setup_cpu_topology_ids(void)
+{
+    unsigned int cpu;
+    unsigned int next_core_id = 0;
+    unsigned int next_cluster_id = 0;
+    unsigned int next_socket_id = 0;
+
+    for_each_possible_cpu(cpu)
+    {
+        unsigned int first_cpu;
+        struct cpu_topology *topo = &cpu_topology[cpu];
+
+        first_cpu = cpumask_first(topo->thread_sibling);
+        ASSERT(first_cpu < nr_cpu_ids);

xen/common/device-tree/cpu-topology.c static int __init parse_dt_topology(void) · line 370

Quoted 10 lines
xen/common/device-tree/cpu-topology.c +10 -0
+
+        if ( first_cpu == cpu )
+        {
+            topo->phys_core_id = next_core_id;
+            next_core_id++;
+        }
+        else
+        {
+            topo->phys_core_id = cpu_topology[first_cpu].phys_core_id;
+        }

No braces for single line statements.

Quoted 8 lines
xen/common/device-tree/cpu-topology.c +8 -0
+
+        first_cpu = cpumask_first(topo->cluster_sibling);
+        if ( first_cpu >= nr_cpu_ids )
+        {
+            /* Clustering is not supported */
+            topo->phys_cluster_id = 0;
+        }
+        else

xen/common/device-tree/cpu-topology.c static int __init parse_dt_topology(void) · line 388

Quoted 10 lines
xen/common/device-tree/cpu-topology.c +10 -0
+        {
+            if ( first_cpu == cpu )
+            {
+                topo->phys_cluster_id = next_cluster_id;
+                next_cluster_id++;
+            }
+            else
+            {
+                topo->phys_cluster_id = cpu_topology[first_cpu].phys_cluster_id;
+            }

No braces for single line statements.

Quoted 4 lines
xen/common/device-tree/cpu-topology.c +4 -0
+        }
+
+        first_cpu = cpumask_first(topo->core_sibling);
+        ASSERT(first_cpu < nr_cpu_ids);

xen/common/device-tree/cpu-topology.c static int __init parse_dt_topology(void) · line 402

Quoted 10 lines
xen/common/device-tree/cpu-topology.c +10 -0
+
+        if ( first_cpu == cpu )
+        {
+            topo->phys_socket_id = next_socket_id;
+            next_socket_id++;
+        }
+        else
+        {
+            topo->phys_socket_id = cpu_topology[first_cpu].phys_socket_id;
+        }

No braces for single line statements.

Quoted 84 lines
xen/common/device-tree/cpu-topology.c +9 -0
+
+        topo->num_siblings = cpumask_weight(topo->thread_sibling);
+    }
+}
+
 int __init dt_init_cpu_topology(void)
 {
     unsigned int cpu;
@@ -353,9 +414,13 @@ int __init dt_init_cpu_topology(void)
 
     ret = parse_dt_topology();
     if ( ret == 0 )
+    {
         for_each_possible_cpu(cpu)
             setup_siblings_masks(cpu);
 
+        setup_cpu_topology_ids();
+    }
+
     return ret;
 }
 
xen/common/sched/credit2.c +6 -0
index c32e753bb4..335cadb8b1 100644
@@ -9,6 +9,7 @@
  * Based on an earlier verson by Emmanuel Ackaouy.
  */
 
+#include <xen/cpu-topology.h>
 #include <xen/errno.h>
 #include <xen/init.h>
 #include <xen/lib.h>
@@ -35,6 +36,11 @@
  */
 static unsigned int cpu_nr_siblings(unsigned int cpu)
 {
+#ifdef CONFIG_GENERIC_CPU_TOPOLOGY
+    if ( cpu_topology )
+        return cpu_topology[cpu].num_siblings;
+#endif
+
 #ifdef CONFIG_X86
     return cpu_data[cpu].x86_num_siblings;
 #else
xen/common/sysctl.c +1 -0
index 8fb5ff0af3..6f1839b359 100644
@@ -28,6 +28,7 @@
 #include <xen/pmstat.h>
 #include <xen/livepatch.h>
 #include <xen/coverage.h>
+#include <xen/cpu-topology.h>
 
 long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl)
 {
xen/drivers/acpi/topology.c +3 -0
index f9836447b2..32f5b97c3e 100644
@@ -24,6 +24,9 @@ int __init acpi_init_cpu_topology(void)
     {
         struct cpu_topology *topo = &cpu_topology[cpu];
 
+        topo->phys_core_id = cpu;
+        topo->num_siblings = 1;
+
         cpumask_set_cpu(cpu, topo->thread_sibling);
         cpumask_copy(topo->core_sibling, &cpu_possible_map);
         cpumask_copy(topo->cluster_sibling, &cpu_possible_map);
xen/include/xen/cpu-topology.h +1 -2
index 27b9bd443e..52ee93d4d0 100644
@@ -3,23 +3,60 @@
 #ifndef XEN_CPU_TOPOLOGY_H
 #define XEN_CPU_TOPOLOGY_H
 
-#ifdef CONFIG_GENERIC_CPU_TOPOLOGY
-
 #include <xen/cpumask.h>
+#include <xen/percpu.h>

xen/include/xen/cpu-topology.h line 17

Quoted 10 lines
xen/include/xen/cpu-topology.h +5 -0
+
+#include <asm/processor.h>
+#include <asm/smp.h>
 
 struct cpu_topology {
     cpumask_var_t thread_sibling;
     cpumask_var_t core_sibling;
     cpumask_var_t cluster_sibling;
+    unsigned int phys_core_id;
+    unsigned int phys_cluster_id;

phys_cluster_id and cluster_sibling are only used to compute each other. Are they really needed?

Quoted 4 lines
xen/include/xen/cpu-topology.h +2 -0
+    unsigned int phys_socket_id;
+    unsigned int num_siblings;
 };
 

xen/include/xen/cpu-topology.h line 31

Quoted 10 lines
xen/include/xen/cpu-topology.h +9 -0
 extern struct cpu_topology *cpu_topology;
+
+static inline void init_cpu_sibling_map(unsigned int cpu)
+{
+    if ( IS_ENABLED(CONFIG_GENERIC_CPU_TOPOLOGY) && cpu_topology )
+    {
+        cpumask_copy(per_cpu(cpu_sibling_mask, cpu),
+                     cpu_topology[cpu].thread_sibling);
+        cpumask_copy(per_cpu(cpu_core_mask, cpu),
+                     cpu_topology[cpu].core_sibling);

cpu_topology[] is built from all CPUs, so a CPU that then fails to boot stays in its siblings masks. I think a CPU should be added to these masks only once it is online.

~Michal

#6 lore raw

RE: [PATCH v12 2/4] xen/sched: Link CPU topology to scheduler

Hi Michal,

Thank you for the review.

Quoted 8 lines
Quoted 8 lines
xen/arch/arm/smpboot.c +0 -0
index d34396f9b1..cf4858eb37 100644
@@ -93,13 +93,7 @@ static int setup_cpu_sibling_map(int cpu)
          !zalloc_cpumask_var(&per_cpu(cpu_core_mask, cpu)) )
         return -ENOMEM;
 
Quoted 9 lines

xen/arch/arm/smpboot.c static int setup_cpu_sibling_map(int cpu) · line 96

Quoted 8 lines
xen/arch/arm/smpboot.c +1 -7
-    /*
-     * Currently we assume there is no multithread and NUMA, so
-     * a CPU is a sibling with itself, and the all possible CPUs
-     * are supposed to belong to the same socket (NUMA node).
-     */
-    cpumask_set_cpu(cpu, per_cpu(cpu_sibling_mask, cpu));
-    cpumask_copy(per_cpu(cpu_core_mask, cpu), &cpu_possible_map);
+    init_cpu_sibling_map(cpu);

The comment above the cpu_sibling_mask definition is now stale.

Yes. This patch removes the comment.

Quoted 10 lines
Quoted 10 lines
xen/arch/arm/smpboot.c +10 -0
+    for_each_possible_cpu(cpu)
+    {
+        unsigned int first_cpu;
+        struct cpu_topology *topo = &cpu_topology[cpu];
+
+        first_cpu = cpumask_first(topo->thread_sibling);
+        ASSERT(first_cpu < nr_cpu_ids);
+
+        if ( first_cpu == cpu )
+        {
Quoted 8 lines

xen/arch/arm/smpboot.c

Quoted 7 lines
xen/arch/arm/smpboot.c +7 -0
+            topo->phys_core_id = next_core_id;
+            next_core_id++;
+        }
+        else
+        {
+            topo->phys_core_id = cpu_topology[first_cpu].phys_core_id;
+        }

No braces for single line statements.

Okay.

Quoted 18 lines
Quoted 8 lines
xen/arch/arm/smpboot.c +8 -0
+
+        first_cpu = cpumask_first(topo->cluster_sibling);
+        if ( first_cpu >= nr_cpu_ids )
+        {
+            /* Clustering is not supported */
+            topo->phys_cluster_id = 0;
+        }
+        else

xen/arch/arm/smpboot.c

Quoted 10 lines
xen/arch/arm/smpboot.c +10 -0
+        {
+            if ( first_cpu == cpu )
+            {
+                topo->phys_cluster_id = next_cluster_id;
+                next_cluster_id++;
+            }
+            else
+            {
+                topo->phys_cluster_id = cpu_topology[first_cpu].phys_cluster_id;
+            }
Quoted 1 line

No braces for single line statements.

Okay.

Quoted 7 lines
Quoted 7 lines
xen/arch/arm/smpboot.c +7 -0
+        }
+
+        first_cpu = cpumask_first(topo->core_sibling);
+        ASSERT(first_cpu < nr_cpu_ids);
+
+        if ( first_cpu == cpu )
+        {
Quoted 8 lines

xen/arch/arm/smpboot.c

Quoted 7 lines
xen/arch/arm/smpboot.c +7 -0
+            topo->phys_socket_id = next_socket_id;
+            next_socket_id++;
+        }
+        else
+        {
+            topo->phys_socket_id = cpu_topology[first_cpu].phys_socket_id;
+        }

No braces for single line statements.

Okay.

Quoted 8 lines

xen/arch/arm/smpboot.c

Quoted 6 lines
 struct cpu_topology {
     cpumask_var_t thread_sibling;
     cpumask_var_t core_sibling;
     cpumask_var_t cluster_sibling;
xen/arch/arm/smpboot.c +2 -0
+    unsigned int phys_core_id;
+    unsigned int phys_cluster_id;

phys_cluster_id and cluster_sibling are only used to compute each other. Are they really needed?

I kept them because I plan to make it possible to specify "cluster" for the credit2_runqueue parameter.

Quoted 4 lines
Quoted 4 lines
xen/arch/arm/smpboot.c +2 -0
+    unsigned int phys_socket_id;
+    unsigned int num_siblings;
 };
Quoted 13 lines

xen/arch/arm/smpboot.c

Quoted 10 lines

extern struct cpu_topology *cpu_topology;

xen/arch/arm/smpboot.c +9 -0
+
+static inline void init_cpu_sibling_map(unsigned int cpu)
+{
+    if ( IS_ENABLED(CONFIG_GENERIC_CPU_TOPOLOGY) && cpu_topology )
+    {
+        cpumask_copy(per_cpu(cpu_sibling_mask, cpu),
+                     cpu_topology[cpu].thread_sibling);
+        cpumask_copy(per_cpu(cpu_core_mask, cpu),
+                     cpu_topology[cpu].core_sibling);

cpu_topology[] is built from all CPUs, so a CPU that then fails to boot stays in its siblings masks. I think a CPU should be added to these masks only once it is online.

The original code prior to this patch initialized the CPU sibling map in the CPU_UP_PREPARE Notifier before bringing up a secondary core. I agree that it is appropriate to initialize it after the core is actually brought up, similar to how x86 Xen does it.

Thank you,
Hirokazu Takahashi