Skip to contentSkip to conversation

arch_topology: Introduce nr_possible_packages

RFC PATCH In review Feng Tang 2 participants 4 messages May 12 – May 13

Subsystems (2)
lore
Share & tools
lore

Feng Tang

author
#0 lore raw

RFC PATCH 1/1 arch_topology: Introduce nr_possible_packages

In multi-sockets platform, kernel or driver code may need the number of packages for chosing different code directions. Some architecture already provide such kind of interface like x86, which is being used in some architecture code and drivers.

Add similar interface 'nr_possible_packages' for platforms which can get package topology information by parsing ACPI tables in boot phase.

---
 drivers/base/arch_topology.c  | 21 +++++++++++++++++++++
 include/linux/arch_topology.h |  5 +++++
 2 files changed, 26 insertions(+)
drivers/base/arch_topology.c +21 -0
index 8c5e47c28d9a..796d8a7aceea 100644
@@ -850,6 +850,16 @@ static bool __init acpi_cpu_is_threaded(int cpu)
 	return !!is_threaded;
 }
 
+unsigned int nr_possible_packages __ro_after_init = 1;
+EXPORT_SYMBOL(nr_possible_packages);
+
+/*
+ * Assuming silicon has a sane package ID decoding method to not have
+ * an ID bigger than 255 (1 byte).
+ */
+#define MAX_PACKAGE_ID	255
+DECLARE_BITMAP(package_id_mask, MAX_PACKAGE_ID + 1);
+
 /*
  * Propagate the topology information of the processor_topology_node tree to the
  * cpu_topology array.
@@ -912,6 +922,13 @@ __weak int __init parse_acpi_topology(void)
 		cpu_topology[cpu].cluster_id = topology_id;
 		topology_id = find_acpi_cpu_topology_package(cpu);
 		cpu_topology[cpu].package_id = topology_id;
+
+
+		if (topology_id >= 0 && topology_id <= MAX_PACKAGE_ID)
+			bitmap_set(package_id_mask, topology_id, 1);
+		else
+			pr_warn("ACPI: abnormal package ID: %d !\n",
+				topology_id);
 	}
 
 	/*
@@ -927,6 +944,10 @@ __weak int __init parse_acpi_topology(void)
 
 	cpu_smt_set_num_threads(max_smt_thread_num, max_smt_thread_num);
 	xa_destroy(&hetero_cpu);
+
+	/* Count the number of possible packages in system */
+	nr_possible_packages = bitmap_weight(package_id_mask, MAX_PACKAGE_ID + 1);
+	pr_info("ACPI: System has %u Package(s) detected\n", nr_possible_packages);
 	return 0;
 }
 
include/linux/arch_topology.h +5 -0
index ebd7f8935f96..dee076cc9c7a 100644
@@ -111,4 +111,9 @@ static inline bool topology_core_has_smt(int cpu) { return false; }
 
 #endif /* CONFIG_GENERIC_ARCH_TOPOLOGY */
 
+
+#if defined(CONFIG_ARM64) || defined(CONFIG_RISCV)
+extern unsigned int nr_possible_packages;
+#endif
+
 #endif /* _LINUX_ARCH_TOPOLOGY_H_ */

base-commit: 50897c955902c93ae71c38698abb910525ebdc89

Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw
Quoted 19 lines · Feng Tang

In multi-sockets platform, kernel or driver code may need the number of packages for chosing different code directions. Some architecture already provide such kind of interface like x86, which is being used in some architecture code and drivers.

Add similar interface 'nr_possible_packages' for platforms which can get package topology information by parsing ACPI tables in boot phase.

---
 drivers/base/arch_topology.c  | 21 +++++++++++++++++++++
 include/linux/arch_topology.h |  5 +++++
 2 files changed, 26 insertions(+)
drivers/base/arch_topology.c +0 -0
index 8c5e47c28d9a..796d8a7aceea 100644

drivers/base/arch_topology.c static bool __init acpi_cpu_is_threaded(int cpu) · line 854

Quoted 5 lines
drivers/base/arch_topology.c +2 -0
@@ -850,6 +850,16 @@ static bool __init acpi_cpu_is_threaded(int cpu)
 	return !!is_threaded;
 }
 
+unsigned int nr_possible_packages __ro_after_init = 1;
+EXPORT_SYMBOL(nr_possible_packages);

EXPORT_SYMBOL_GPL() please?

And you don't have a user for this, so we can't verify how it actually works :(

thanks,

greg k-h

drivers/base/arch_topology.c static bool __init acpi_cpu_is_threaded(int cpu) · line 858

Quoted 3 lines
drivers/base/arch_topology.c +3 -0
+/*
+ * Assuming silicon has a sane package ID decoding method to not have
+ * an ID bigger than 255 (1 byte).

Is that a valid assumption? What about packages bigger than 255? Don't we have those today?

Quoted 22 lines
drivers/base/arch_topology.c +11 -0
+ */
+#define MAX_PACKAGE_ID	255
+DECLARE_BITMAP(package_id_mask, MAX_PACKAGE_ID + 1);
+
 /*
  * Propagate the topology information of the processor_topology_node tree to the
  * cpu_topology array.
@@ -912,6 +922,13 @@ __weak int __init parse_acpi_topology(void)
 		cpu_topology[cpu].cluster_id = topology_id;
 		topology_id = find_acpi_cpu_topology_package(cpu);
 		cpu_topology[cpu].package_id = topology_id;
+
+
+		if (topology_id >= 0 && topology_id <= MAX_PACKAGE_ID)
+			bitmap_set(package_id_mask, topology_id, 1);
+		else
+			pr_warn("ACPI: abnormal package ID: %d !\n",
+				topology_id);
 	}
 
 	/*

drivers/base/arch_topology.c __weak int __init parse_acpi_topology(void) · line 950

Quoted 7 lines
drivers/base/arch_topology.c +4 -0
@@ -927,6 +944,10 @@ __weak int __init parse_acpi_topology(void)
 
 	cpu_smt_set_num_threads(max_smt_thread_num, max_smt_thread_num);
 	xa_destroy(&hetero_cpu);
+
+	/* Count the number of possible packages in system */
+	nr_possible_packages = bitmap_weight(package_id_mask, MAX_PACKAGE_ID + 1);
+	pr_info("ACPI: System has %u Package(s) detected\n", nr_possible_packages);

Why "P" and not "p"?

Quoted 8 lines
drivers/base/arch_topology.c +0 -0
 	return 0;
 }
 
include/linux/arch_topology.h +0 -0
index ebd7f8935f96..dee076cc9c7a 100644

include/linux/arch_topology.h static inline bool topology_core_has_smt(int cpu) { return false; } · line 117

Quoted 7 lines
include/linux/arch_topology.h +4 -0
@@ -111,4 +111,9 @@ static inline bool topology_core_has_smt(int cpu) { return false; }
 
 #endif /* CONFIG_GENERIC_ARCH_TOPOLOGY */
 
+
+#if defined(CONFIG_ARM64) || defined(CONFIG_RISCV)
+extern unsigned int nr_possible_packages;
+#endif

Why the ifdef?

thanks,

greg k-h

author
#2 lore raw

Hi Greg,

Thanks for the review!

Quoted 23 lines · Greg Kroah-Hartman
Quoted 22 lines · Feng Tang

In multi-sockets platform, kernel or driver code may need the number of packages for chosing different code directions. Some architecture already provide such kind of interface like x86, which is being used in some architecture code and drivers.

Add similar interface 'nr_possible_packages' for platforms which can get package topology information by parsing ACPI tables in boot phase.

---
 drivers/base/arch_topology.c  | 21 +++++++++++++++++++++
 include/linux/arch_topology.h |  5 +++++
 2 files changed, 26 insertions(+)
drivers/base/arch_topology.c +0 -0
index 8c5e47c28d9a..796d8a7aceea 100644
@@ -850,6 +850,16 @@ static bool __init acpi_cpu_is_threaded(int cpu)
 	return !!is_threaded;
 }
 
Quoted 4 lines

drivers/base/arch_topology.c static bool __init acpi_cpu_is_threaded(int cpu) · line 854

Quoted 2 lines
drivers/base/arch_topology.c +2 -0
+unsigned int nr_possible_packages __ro_after_init = 1;
+EXPORT_SYMBOL(nr_possible_packages);

EXPORT_SYMBOL_GPL() please?

Will change.

Quoted 2 lines

And you don't have a user for this, so we can't verify how it actually works :(

Good point. Internally we have some PMU driver using this, but the HW is not public and the drive hasn't been posted yet.

As for validation, we tested on production 1P platform, internal 2P platform, and QEMU's multi-socket case.

I checked a production arm64 2P platform 'Kunpeng 920', and from the output of "cat /sys/devices/system/cpu/cpu*/topology/physical_package_id", the patch should work fine. I don't have root right of it yet, will try to really run the patch on it.

Quoted 10 lines

thanks,

greg k-h

drivers/base/arch_topology.c static bool __init acpi_cpu_is_threaded(int cpu) · line 858

Quoted 3 lines
drivers/base/arch_topology.c +3 -0
+/*
+ * Assuming silicon has a sane package ID decoding method to not have
+ * an ID bigger than 255 (1 byte).

Is that a valid assumption? What about packages bigger than 255? Don't we have those today?

Myself have only touched 8P (16P) HW, which may limit my imagination :) We can change it to 1024 or 2048, though I'm curious how the cross-socket latency and coherency could be maintained for huge platforms.

Quoted 24 lines
Quoted 24 lines
drivers/base/arch_topology.c +11 -0
+ */
+#define MAX_PACKAGE_ID	255
+DECLARE_BITMAP(package_id_mask, MAX_PACKAGE_ID + 1);
+
 /*
  * Propagate the topology information of the processor_topology_node tree to the
  * cpu_topology array.
@@ -912,6 +922,13 @@ __weak int __init parse_acpi_topology(void)
 		cpu_topology[cpu].cluster_id = topology_id;
 		topology_id = find_acpi_cpu_topology_package(cpu);
 		cpu_topology[cpu].package_id = topology_id;
+
+
+		if (topology_id >= 0 && topology_id <= MAX_PACKAGE_ID)
+			bitmap_set(package_id_mask, topology_id, 1);
+		else
+			pr_warn("ACPI: abnormal package ID: %d !\n",
+				topology_id);
 	}
 
 	/*
@@ -927,6 +944,10 @@ __weak int __init parse_acpi_topology(void)
 
Quoted 8 lines

drivers/base/arch_topology.c __weak int __init parse_acpi_topology(void) · line 950

Quoted 6 lines
drivers/base/arch_topology.c +4 -0
 	cpu_smt_set_num_threads(max_smt_thread_num, max_smt_thread_num);
 	xa_destroy(&hetero_cpu);
+
+	/* Count the number of possible packages in system */
+	nr_possible_packages = bitmap_weight(package_id_mask, MAX_PACKAGE_ID + 1);
+	pr_info("ACPI: System has %u Package(s) detected\n", nr_possible_packages);

Why "P" and not "p"?

:) will change.

Quoted 10 lines
Quoted 10 lines
drivers/base/arch_topology.c +0 -0
 	return 0;
 }
 
include/linux/arch_topology.h +0 -0
index ebd7f8935f96..dee076cc9c7a 100644
@@ -111,4 +111,9 @@ static inline bool topology_core_has_smt(int cpu) { return false; }
 
Quoted 8 lines

include/linux/arch_topology.h static inline bool topology_core_has_smt(int cpu) { return false; } · line 117

Quoted 6 lines
include/linux/arch_topology.h +4 -0
 #endif /* CONFIG_GENERIC_ARCH_TOPOLOGY */
 
+
+#if defined(CONFIG_ARM64) || defined(CONFIG_RISCV)
+extern unsigned int nr_possible_packages;
+#endif

Why the ifdef?

Aha, initial version of patch doesn't have it, as I think many architectures may need this.

It was added later as the 'parse_acpi_topology()' function is under this 'ifdef'.

I can move the 'nr_possible_packages' for future expandability if no objection.

Thanks,
Feng

Quoted 3 lines

thanks,

greg k-h

author
#3 lore raw

On Wed, May 13, 2026 at 09:35:30AM +0800, Feng Tang wrote:
[...]

Quoted 6 lines
Quoted 2 lines

And you don't have a user for this, so we can't verify how it actually works :(

Good point. Internally we have some PMU driver using this, but the HW is not public and the drive hasn't been posted yet.

Quoted 7 lines

As for validation, we tested on production 1P platform, internal 2P platform, and QEMU's multi-socket case.

I checked a production arm64 2P platform 'Kunpeng 920', and from the output of "cat /sys/devices/system/cpu/cpu*/topology/physical_package_id", the patch should work fine. I don't have root right of it yet, will try to really run the patch on it.

Just tried 7.1-rc3 + this patch on the 2P arm64 'Kunpeng 920' server, and it showed the correct package number:

"[ 0.011284] ACPI: System has 2 Package(s) detected"

Thanks,
Feng