drivers/base/arch_topology.c | 26 ++++++++++++++++++++++++++ include/linux/arch_topology.h | 5 +++++ 2 files changed, 31 insertions(+)
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)
On Tue, Jul 14, 2026 at 01:43:17PM +0800, Feng Tang wrote:
> 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;
Since all you need is just the total number of packages, this whole
array may not be required.
static unsigned int topology_count_packages(const struct cpumask *cpus)
{
unsigned int cpu, prev, count = 0;
int package_id;
for_each_cpu(cpu, cpus) {
package_id = cpu_topology[cpu].package_id;
if (package_id < 0)
continue;
for_each_cpu(prev, cpus) {
if (prev == cpu) {
count++;
break;
}
if (cpu_topology[prev].package_id == package_id)
break;
}
}
return count;
}
Something like this would suffice, no ?
--
Regards,
Sudeep
On Wed, Jul 22, 2026 at 09:41:54AM +0100, Sudeep Holla wrote:
> On Tue, Jul 14, 2026 at 01:43:17PM +0800, Feng Tang wrote:
> > 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.
[...]
> >
> > +unsigned int nr_possible_packages __ro_after_init;
> > +EXPORT_SYMBOL(nr_possible_packages);
> > +
> > +static int package_ids[1 << CONFIG_NODES_SHIFT] __initdata;
Thanks for the suggestion!
> Since all you need is just the total number of packages, this whole
> array may not be required.
Yes. I also made it '__initdata' to be freed later.
> static unsigned int topology_count_packages(const struct cpumask *cpus)
> {
> unsigned int cpu, prev, count = 0;
> int package_id;
>
> for_each_cpu(cpu, cpus) {
> package_id = cpu_topology[cpu].package_id;
> if (package_id < 0)
> continue;
>
> for_each_cpu(prev, cpus) {
> if (prev == cpu) {
> count++;
> break;
> }
>
> if (cpu_topology[prev].package_id == package_id)
> break;
> }
> }
>
> return count;
> }
>
> Something like this would suffice, no ?
This is a good point, that I don't have to call count_packages()
and use global array for each cpu, but do this package counting
all together after CPUs have been parsed and before exitting
parse_acpi_topology().
One thing is, from PPTT table, the package ID is u32, so we
can't say 0xffffffff (-1) is illegal (though I think it's insane
to really use it :)). Anyway, I'll test more in this direction.
Thanks,
Feng
On Tue, Jul 14, 2026 at 01:43:17PM +0800, Feng Tang wrote: > 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) This is undone in this version, why ? > * change the possible max package ID to 2047 (Greg) > * remove the CONFIG_ARM64/RISCV limit for 'nr_possible_packages' (Greg) Again this is still there, misleading or v2 is back to RFC, /me confused. > > v1: https://lore.kernel.org/lkml/20260515144435.93035-1-feng.tang@linux.alibaba.com/ Comments about users is still ignored. -- Regards, Sudeep P.S.: Please avoid personal pings as it will not gain attention from other interested parties.
On Tue, Jul 21, 2026 at 03:32:03PM +0100, Sudeep Holla wrote: > On Tue, Jul 14, 2026 at 01:43:17PM +0800, Feng Tang wrote: > > 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) > > This is undone in this version, why ? > > > * change the possible max package ID to 2047 (Greg) > > * remove the CONFIG_ARM64/RISCV limit for 'nr_possible_packages' (Greg) > > Again this is still there, misleading or v2 is back to RFC, /me confused. Thanks for the review! My fault! that I messed up with the locally saved RFC and V1 patch. I was focusing on fixing the problems you mentioned. > > > > v1: https://lore.kernel.org/lkml/20260515144435.93035-1-feng.tang@linux.alibaba.com/ > > Comments about users is still ignored. I described the user case in commit log of this version. It was locally used by several drivers and other code, maybe I should repost it together with those code. btw, is there any problem with the new package accounting code? Thanks, Feng > -- > Regards, > Sudeep > > P.S.: Please avoid personal pings as it will not gain attention from other > interested parties.
On Wed, Jul 22, 2026 at 02:50:48PM +0800, Feng Tang wrote: > On Tue, Jul 21, 2026 at 03:32:03PM +0100, Sudeep Holla wrote: > > On Tue, Jul 14, 2026 at 01:43:17PM +0800, Feng Tang wrote: > > > 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) > > > > This is undone in this version, why ? > > > > > * change the possible max package ID to 2047 (Greg) > > > * remove the CONFIG_ARM64/RISCV limit for 'nr_possible_packages' (Greg) > > > > Again this is still there, misleading or v2 is back to RFC, /me confused. > > Thanks for the review! > > My fault! that I messed up with the locally saved RFC and V1 patch. I was > focusing on fixing the problems you mentioned. > > > > > > > v1: https://lore.kernel.org/lkml/20260515144435.93035-1-feng.tang@linux.alibaba.com/ > > > > Comments about users is still ignored. > > I described the user case in commit log of this version. It was > locally used by several drivers and other code, maybe I should > repost it together with those code. > Yes please. > btw, is there any problem with the new package accounting code? > Depends on the users really, because it can be simplified based on the usage. Regards, Sudeep
© 2016 - 2026 Red Hat, Inc.