Re: [PATCH v2] arch_topology: Introduce nr_possible_packages

Sudeep Holla <[email protected]> Wed, 22 Jul 2026 09:41:54 +0100
Newsgroups dev.linux.lists.driver-core,org.kernel.vger.linux-kernel
Message-ID <20260722-wakeful-lemming-of-snow-04fe9d@sudeepholla>
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 <[email protected]>
> ---
>   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/[email protected]/ 
>   RFC: https://lore.kernel.org/lkml/[email protected]/ 
> 
> 
>  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