[PATCH v5 2/3] x86/topology: Avoid wasting 128k for package id array

Prarit Bhargava
2017-11-05T16:54:51Z

From: Andi Kleen <ak@linux.intel.com>

I was looking at large early boot allocations and noticed that since
(1f12e32f x86/topology: Create logical package id) every 64bit system
allocates a 128k array to convert logical package ids.

This happens because the array is sized to (MAX_LOCAL_APIC * u16) = 128k.
This is a lot of waste especially for most systems which have one or two
sockets.

Use a dynamically allocated array of size logical_packages to map
the logical and physical packages.

[v2]: Decrease logical_packages when the last thread in a socket is
removed.
[v3]: Add more logic to keep logical and physical package IDs
in synch.
[v4]: Keep logical mapping static by using hybrid approach of a small logical
to physical array and keeping logical cpu information in cpu_data.
[v5]: Change kmalloc to GFP_ATOMIC to fix "sleeping function" warning on
virtual machines. Remove phys_pkg_id. Add spinlock to avoid concurrency
issues.

Signed-off-by: Andi Kleen <ak@linux.intel.com>
Signed-off-by: Prarit Bhargava <prarit@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: x86@kernel.org
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Piotr Luc <piotr.luc@intel.com>
Cc: Kan Liang <kan.liang@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Stephane Eranian <eranian@google.com>
Cc: Prarit Bhargava <prarit@redhat.com>
Cc: Arvind Yadav <arvind.yadav.cs@gmail.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Christian Borntraeger <borntraeger@de.ibm.com>
Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: He Chen <he.chen@linux.intel.com>
Cc: Mathias Krause <minipli@googlemail.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
---
arch/x86/kernel/smpboot.c | 97 +++++++++++++++++++++++++----------------------
1 file changed, 51 insertions(+), 46 deletions(-)

diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c
index ad59edd84de7..580261f8a2bf 100644
--- a/arch/x86/kernel/smpboot.c
+++ b/arch/x86/kernel/smpboot.c
@@ -99,13 +99,12 @@ DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map);
DEFINE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
EXPORT_PER_CPU_SYMBOL(cpu_info);

-/* Logical package management. We might want to allocate that dynamically */
-static int *physical_to_logical_pkg __read_mostly;
-static unsigned long *physical_package_map __read_mostly;;
-static unsigned int max_physical_pkg_id __read_mostly;
+/* Logical package management.*/
unsigned int __max_logical_packages __read_mostly;
EXPORT_SYMBOL(__max_logical_packages);
+static u16 *logical_to_physical_pkg_map;
static unsigned int logical_packages __read_mostly;
+static DEFINE_SPINLOCK(logical_pkg_lock);

/* Maximum number of SMT threads on any online core */
int __max_smt_threads __read_mostly;
@@ -278,6 +277,31 @@ static void notrace start_secondary(void *unused)
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
}

+/**
+ * topology_phys_to_logical_pkg - Map a physical package id to a logical
+ *
+ * Returns logical package id or -1 if not found
+ */
+int topology_phys_to_logical_pkg(unsigned int phys_pkg)
+{
+ int log_pkg;
+ int found = 0;
+ unsigned long flags;
+
+ spin_lock_irqsave(&logical_pkg_lock, flags);
+ for (log_pkg = 0; log_pkg < logical_packages; log_pkg++)
+ if (logical_to_physical_pkg_map[log_pkg] == phys_pkg) {
+ found = 1;
+ break;
+ }
+ spin_unlock_irqrestore(&logical_pkg_lock, flags);
+
+ if (found)
+ return log_pkg;
+ return -1;
+}
+EXPORT_SYMBOL(topology_phys_to_logical_pkg);
+
/**
* topology_update_package_map - Update the physical to logical package map
* @pkg: The physical package id as retrieved via CPUID
@@ -285,17 +309,12 @@ static void notrace start_secondary(void *unused)
*/
int topology_update_package_map(unsigned int pkg, unsigned int cpu)
{
- unsigned int new;
-
- /* Called from early boot ? */
- if (!physical_package_map)
- return 0;
-
- if (pkg >= max_physical_pkg_id)
- return -EINVAL;
+ int new;
+ u16 *ltp_pkg_map_new;
+ unsigned long flags;

- /* Set the logical package id */
- if (test_and_set_bit(pkg, physical_package_map))
+ new = topology_phys_to_logical_pkg(pkg);
+ if (new >= 0)
goto found;

if (logical_packages >= __max_logical_packages) {
@@ -305,34 +324,31 @@ int topology_update_package_map(unsigned int pkg, unsigned int cpu)
}

new = logical_packages++;
- if (new != pkg) {
- pr_info("CPU %u Converting physical %u to logical package %u\n",
- cpu, pkg, new);
+
+ /* Allocate and copy a new array */
+ ltp_pkg_map_new = kmalloc(logical_packages * sizeof(u16), GFP_ATOMIC);
+ BUG_ON(!ltp_pkg_map_new);
+ spin_lock_irqsave(&logical_pkg_lock, flags);
+ if (logical_to_physical_pkg_map) {
+ memcpy(ltp_pkg_map_new, logical_to_physical_pkg_map,
+ logical_packages * sizeof(u16));
+ kfree(logical_to_physical_pkg_map);
}
- physical_to_logical_pkg[pkg] = new;
+ logical_to_physical_pkg_map = ltp_pkg_map_new;
+ logical_to_physical_pkg_map[new] = pkg;
+ spin_unlock_irqrestore(&logical_pkg_lock, flags);

+ if (pkg != new)
+ pr_info("CPU %u Converting physical %u to logical package %u\n",
+ cpu, pkg, new);
found:
- cpu_data(cpu).logical_proc_id = physical_to_logical_pkg[pkg];
+ cpu_data(cpu).logical_proc_id = new;
return 0;
}

-/**
- * topology_phys_to_logical_pkg - Map a physical package id to a logical
- *
- * Returns logical package id or -1 if not found
- */
-int topology_phys_to_logical_pkg(unsigned int phys_pkg)
-{
- if (phys_pkg >= max_physical_pkg_id)
- return -1;
- return physical_to_logical_pkg[phys_pkg];
-}
-EXPORT_SYMBOL(topology_phys_to_logical_pkg);
-
static void __init smp_init_package_map(struct cpuinfo_x86 *c, unsigned int cpu)
{
unsigned int ncpus;
- size_t size;

/*
* Today neither Intel nor AMD support heterogenous systems. That
@@ -363,21 +379,10 @@ static void __init smp_init_package_map(struct cpuinfo_x86 *c, unsigned int cpu)
}

__max_logical_packages = DIV_ROUND_UP(total_cpus, ncpus);
- logical_packages = 0;
-
- /*
- * Possibly larger than what we need as the number of apic ids per
- * package can be smaller than the actual used apic ids.
- */
- max_physical_pkg_id = DIV_ROUND_UP(MAX_LOCAL_APIC, ncpus);
- size = max_physical_pkg_id * sizeof(unsigned int);
- physical_to_logical_pkg = kmalloc(size, GFP_KERNEL);
- memset(physical_to_logical_pkg, 0xff, size);
- size = BITS_TO_LONGS(max_physical_pkg_id) * sizeof(unsigned long);
- physical_package_map = kzalloc(size, GFP_KERNEL);
-
pr_info("Max logical packages: %u\n", __max_logical_packages);

+ logical_packages = 0;
+
topology_update_package_map(c->phys_proc_id, cpu);
}

--
2.15.0.rc0.39.g2f0e14e64