From: Feng Tang <feng.tang@linux.alibaba.com>
To: Sudeep Holla <sudeep.holla@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
rafael@kernel.org, Danilo Krummrich <dakr@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
David Hildenbrand <david@kernel.org>,
linux-kernel@vger.kernel.org, driver-core@lists.linux.dev,
Feng Tang <feng.tang@linux.alibaba.com>
Subject: [PATCH v2] arch_topology: Introduce nr_possible_packages
Date: Tue, 14 Jul 2026 13:43:17 +0800 [thread overview]
Message-ID: <20260714054317.73219-1-feng.tang@linux.alibaba.com> (raw)
For multi-sockets platforms kernel or driver code may need the number
of packages to chose different code directions. Some architecture
already provides such kind of interface like x86, which is being used
in its 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,
which was verified to show the correct number of packages on some
1-socket and 2-sockets production arm64 servers from different vendors.
It has been used locally by some arm64 PMU driver, and cross-socket
timer-consistency check code, which are to be posted.
Signed-off-by: Feng Tang <feng.tang@linux.alibaba.com>
---
since v1:
* fix the potential overflow issue for 32b package ID (Sudeep)
* add real use case in commit log (Sudeep)
since RFC:
* use EXPORT_SYMBOL_GPL instead of EXPORT_SYMBOL (Greg)
* change the possible max package ID to 2047 (Greg)
* remove the CONFIG_ARM64/RISCV limit for 'nr_possible_packages' (Greg)
v1: https://lore.kernel.org/lkml/20260515144435.93035-1-feng.tang@linux.alibaba.com/
RFC: https://lore.kernel.org/lkml/20260512150505.43871-1-feng.tang@linux.alibaba.com/
drivers/base/arch_topology.c | 26 ++++++++++++++++++++++++++
include/linux/arch_topology.h | 5 +++++
2 files changed, 31 insertions(+)
diff --git a/drivers/base/arch_topology.c b/drivers/base/arch_topology.c
index 8c5e47c28d9a..2ee27cc05b6b 100644
--- a/drivers/base/arch_topology.c
+++ b/drivers/base/arch_topology.c
@@ -850,6 +850,29 @@ static bool __init acpi_cpu_is_threaded(int cpu)
return !!is_threaded;
}
+unsigned int nr_possible_packages __ro_after_init;
+EXPORT_SYMBOL(nr_possible_packages);
+
+static int package_ids[1 << CONFIG_NODES_SHIFT] __initdata;
+static inline void count_packages(unsigned int pkg_id)
+{
+ int max_packages = 1 << CONFIG_NODES_SHIFT;
+ int i;
+
+ /* This check will be skipped for the first caller */
+ for (i = 0; i < nr_possible_packages; i++)
+ if (package_ids[i] == pkg_id)
+ return;
+
+ if (nr_possible_packages == max_packages) {
+ pr_warn("Number of packages exceeds kernel NUMA node limits [%d]!\n",
+ max_packages);
+ return;
+ }
+
+ package_ids[nr_possible_packages++] = pkg_id;
+}
+
/*
* Propagate the topology information of the processor_topology_node tree to the
* cpu_topology array.
@@ -912,6 +935,7 @@ __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;
+ count_packages(topology_id);
}
/*
@@ -927,6 +951,8 @@ __weak int __init parse_acpi_topology(void)
cpu_smt_set_num_threads(max_smt_thread_num, max_smt_thread_num);
xa_destroy(&hetero_cpu);
+
+ pr_info("ACPI: System has %u Package(s) detected\n", nr_possible_packages);
return 0;
}
diff --git a/include/linux/arch_topology.h b/include/linux/arch_topology.h
index ebd7f8935f96..dee076cc9c7a 100644
--- a/include/linux/arch_topology.h
+++ b/include/linux/arch_topology.h
@@ -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_ */
--
2.39.5 (Apple Git-154)
next reply other threads:[~2026-07-14 5:43 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-14 5:43 Feng Tang [this message]
2026-07-21 14:32 ` [PATCH v2] arch_topology: Introduce nr_possible_packages Sudeep Holla
2026-07-22 6:50 ` Feng Tang
2026-07-22 8:38 ` Sudeep Holla
2026-07-22 8:41 ` Sudeep Holla
2026-07-23 8:27 ` Feng Tang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260714054317.73219-1-feng.tang@linux.alibaba.com \
--to=feng.tang@linux.alibaba.com \
--cc=catalin.marinas@arm.com \
--cc=dakr@kernel.org \
--cc=david@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rafael@kernel.org \
--cc=sudeep.holla@kernel.org \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).