Received: by 2002:a05:7412:2a91:b0:fc:a2b0:25d7 with SMTP id u17csp180806rdh; Tue, 13 Feb 2024 13:21:29 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU94lHeOBI+vKqao5xXa0TK6UOtjFFBRjTYAGs+P9R+EX8fZgY3hh3okHSprQVbJhOG5wc1WEfe/YwD3YqyNyEqUFWUSCbJfzzixYCeKg== X-Google-Smtp-Source: AGHT+IFtzLY7xAYx/p18f8Fu3a2pdBDD/TyvBMC3eHRv7W3ZgKzqv5kDiunmv+ps2goA/Ri6Alev X-Received: by 2002:a17:902:784b:b0:1d7:88de:cffa with SMTP id e11-20020a170902784b00b001d788decffamr669933pln.19.1707859289298; Tue, 13 Feb 2024 13:21:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707859289; cv=pass; d=google.com; s=arc-20160816; b=XsG9shm0UsagA2MNW5//tCWsg2iMXqf0HWyVtFZb0zDjI+svXbETt0RSkHRFQ4T+iF 51Rm5TCPshUTWsp//Orx4I9EXcOs1bXp/5GPYJc7YVbrY5xv0KQbvrVIJZir0g2gW0Sc tHihOXS7aSPC+aX8U6I1xxfUTJvxa5W0v1Bsou22ezU2O/9Pf5CJQ1xU5rpQw4LUaqPZ 3TeZgXOIHq791vhhNmLl+2H/s7hUdkPYjnWOOUIU524xJsCVUkb7OaCItEMnxlkl/AhX G2xwN4uwbg3kDsDL+lVqoWHic2abddBFAIh5j4iGpUUG3qQZePY48WlnObk5FaKhg8DP kcyw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=date:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:references:subject:cc:to:from:dkim-signature :dkim-signature:message-id; bh=hKp0SumwdMdW6ZHjFxH6LZU4vhmrmECwOTrjjSTK/SA=; fh=VcX4zdCON08Te470IHgIgFXUYVeWdnDwpYyLkigXJ50=; b=fFqHdDHLhbrZbRLzYSvEn+2fLWmsKtV+fgp2mjyylVOU5IzXUkGj8p/XO4vhkXXVYo dL3AIMuK3GRSTdOk0bq481Xr+vQcj1XfqhoszT1cv6JSxhLmU4YfFspqdVcA6I5R1UNv uD8lA9URt1T6O4Za+YBlbau1zC44uBPKEuDI/On2IM6EhnVOtqyrgEsK9SlYPfid0l8r 4KVRuuqOj2QZ0eoTz4jTkms5Z5ZeOypTewMZmEkvV6+2p2I0LDnq1NXZ10XD75yi/Y7J oRYL/epgo+DXLZPmBq0Dgjt+JCP4mCMRWCRdFA/QMp07aDEHXdMEncXyRTvBVFhCLC1w Dvnw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=BHR6gcNm; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-64312-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64312-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de X-Forwarded-Encrypted: i=2; AJvYcCViLgZC5u1f613qujv71v35CFo6p1JAytLaf8djb91/IDKzSAVggEhuOlvE3qqRDVm5E3BzorB88Npu67ggr2jE+7pTpaBXzPpdU6w5cw== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id b7-20020a170903228700b001d8a652d259si2610141plh.350.2024.02.13.13.21.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 13:21:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64312-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=BHR6gcNm; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-64312-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64312-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 0CA222858ED for ; Tue, 13 Feb 2024 21:21:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 283E78612E; Tue, 13 Feb 2024 21:06:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="BHR6gcNm"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="+AOL6JEK" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6FCC985950 for ; Tue, 13 Feb 2024 21:06:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707858374; cv=none; b=c4ZgwenqZsLSlE8i1F1vQTABnoSgRSKWXuFNqiweaQZAnEqoBahN0hYN4OQOffJi0Jw+6GpcRTHxeVpw5BEIX7oimMujn7lBVaGaFEjXYJRkVXiwIga1dkGORy49nVAhQ2AH0+E/Vl7A5OAmw9XaOKWuyMgdmlOPanLAVKpGvfw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707858374; c=relaxed/simple; bh=YxTfdJnty9Os89Sb2GGXBFkpPozcwbCxUT9AWMe/2Kw=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=gldsWz96GhzxbQ3ga8itTNL9NdLhVDiJkvxRJDMTEOt2gNngK2mg4Ult236Q7KTaXABfRZvV42vTssxqGQBcP/5kiEnQj1SN7UmObfkg9Sp5vsQ03m3OPKKU2xH7vF69k0h4A1p1p1lEvvw1iPr5/pgAiNtBHxCFgSP3DCHV5Sw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=BHR6gcNm; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=+AOL6JEK; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Message-ID: <20240213210252.956858282@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1707858371; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=hKp0SumwdMdW6ZHjFxH6LZU4vhmrmECwOTrjjSTK/SA=; b=BHR6gcNmYJRTvXtBPOS+hbP/3i/RFARjh/DJZSBq0792N5j/6z+raERUfhE2eg43tmPNdD AO5XMzISTdy4P9nKmVO71/BLsmybwPoum5LoqeFAU/nEkbDKlQmyx9WbvFDyb16FkXx20o pEv7tQqZmLWdnKS7sphQ6sU+0AymwmOU974eLhXutC7OetjGrmyLaORzdwrHCElYdSNAHc GAYiETKwviuWr2J89cDnN4Zvqad6cLBJTkJ2DSGYP7MS2Bc6jbKbMc4XrsWPcaw3bfAEBk WqYe6safGw+rq3huodw3HcMRXaeMQsvIXVH4NlDnjqKVwYcCo15I7Q6/Qvr5Ew== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1707858371; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=hKp0SumwdMdW6ZHjFxH6LZU4vhmrmECwOTrjjSTK/SA=; b=+AOL6JEKhHTOx4AIFdYPU5LOsLqTaNIRZMcQQGEUMKZahsWF18CH6xgAoVpTrKmfZ+xKN3 +F0HNFEI2qRrAICQ== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, Tom Lendacky , Andrew Cooper , Arjan van de Ven , Huang Rui , Juergen Gross , Dimitri Sivanich , Sohil Mehta , K Prateek Nayak , Kan Liang , Zhang Rui , "Paul E. McKenney" , Feng Tang , Andy Shevchenko , Michael Kelley , "Peter Zijlstra (Intel)" Subject: [patch 26/30] x86/cpu/topology: Retrieve cores per package from topology bitmaps References: <20240213205415.307029033@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Tue, 13 Feb 2024 22:06:10 +0100 (CET) From: Thomas Gleixner Similar to other sizing information the number of cores per package can be established from the topology bitmap. Provide a function for retrieving that information and replace the buggy hack in the CPUID evaluation with it. Signed-off-by: Thomas Gleixner --- arch/x86/kernel/cpu/topology.c | 43 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/topology.h | 11 ++++++++ arch/x86/kernel/cpu/topology_common.c | 18 ++------------ 3 files changed, 57 insertions(+), 15 deletions(-) --- --- a/arch/x86/kernel/cpu/topology.c +++ b/arch/x86/kernel/cpu/topology.c @@ -217,6 +217,49 @@ int topology_get_logical_id(u32 apicid, } EXPORT_SYMBOL_GPL(topology_get_logical_id); +/** + * topology_unit_count - Retrieve the count of specified units at a given topology domain level + * @apicid: The APIC ID which specifies the search range + * @which_units: The domain level specifying the units to count + * @at_level: The domain level at which @which_units have to be counted + * + * This returns the number of possible units according to the enumerated + * information. + * + * E.g. topology_count_units(apicid, TOPO_CORE_DOMAIN, TOPO_PKG_DOMAIN) + * counts the number of possible cores in the package to which @apicid + * belongs. + * + * @at_level must obviously be greater than @which_level to produce useful + * results. If @at_level is equal to @which_units the result is + * unsurprisingly 1. If @at_level is less than @which_units the results + * is by definition undefined and the function returns 0. + */ +unsigned int topology_unit_count(u32 apicid, enum x86_topology_domains which_units, + enum x86_topology_domains at_level) +{ + /* Remove the bits below @at_level to get the proper level ID of @apicid */ + unsigned int lvlid = topo_apicid(apicid, at_level); + unsigned int id, end, cnt = 0; + + if (lvlid >= MAX_LOCAL_APIC) + return 0; + if (!test_bit(lvlid, apic_maps[at_level].map)) + return 0; + if (which_units > at_level) + return 0; + if (which_units == at_level) + return 1; + + /* Calculate the exclusive end */ + end = lvlid + (1U << x86_topo_system.dom_shifts[at_level]); + /* Unfortunately there is no bitmap_weight_range() */ + for (id = find_next_bit(apic_maps[which_units].map, end, lvlid); + id < end; id = find_next_bit(apic_maps[which_units].map, end, ++id)) + cnt++; + return cnt; +} + #ifdef CONFIG_ACPI_HOTPLUG_CPU /** * topology_hotplug_apic - Handle a physical hotplugged APIC after boot --- a/arch/x86/kernel/cpu/topology.h +++ b/arch/x86/kernel/cpu/topology.h @@ -53,4 +53,15 @@ static inline void topology_update_dom(s tscan->dom_ncpus[dom] = ncpus; } +#ifdef CONFIG_X86_LOCAL_APIC +unsigned int topology_unit_count(u32 apicid, enum x86_topology_domains which_units, + enum x86_topology_domains at_level); +#else +static inline unsigned int topology_unit_count(u32 apicid, enum x86_topology_domains which_units, + enum x86_topology_domains at_level) +{ + return 1; +} +#endif + #endif /* ARCH_X86_TOPOLOGY_H */ --- a/arch/x86/kernel/cpu/topology_common.c +++ b/arch/x86/kernel/cpu/topology_common.c @@ -155,25 +155,15 @@ static void topo_set_ids(struct topo_sca c->topo.core_id = (apicid & topo_domain_mask(TOPO_PKG_DOMAIN)) >> x86_topo_system.dom_shifts[TOPO_SMT_DOMAIN]; + /* Maximum number of cores on this package */ + c->x86_max_cores = topology_unit_count(apicid, TOPO_CORE_DOMAIN, TOPO_PKG_DOMAIN); + c->topo.amd_node_id = tscan->amd_node_id; if (c->x86_vendor == X86_VENDOR_AMD) cpu_topology_fixup_amd(tscan); } -static void topo_set_max_cores(struct topo_scan *tscan) -{ - /* - * Bug compatible for now. This is broken on hybrid systems: - * 8 cores SMT + 8 cores w/o SMT - * tscan.dom_ncpus[TOPO_DIEGRP_DOMAIN] = 24; 24 / 2 = 12 !! - * - * Cannot be fixed without further topology enumeration changes. - */ - tscan->c->x86_max_cores = tscan->dom_ncpus[TOPO_DIEGRP_DOMAIN] >> - x86_topo_system.dom_shifts[TOPO_SMT_DOMAIN]; -} - void cpu_parse_topology(struct cpuinfo_x86 *c) { unsigned int dom, cpu = smp_processor_id(); @@ -201,7 +191,6 @@ void cpu_parse_topology(struct cpuinfo_x } topo_set_ids(&tscan); - topo_set_max_cores(&tscan); } void __init cpu_init_topology(struct cpuinfo_x86 *c) @@ -223,7 +212,6 @@ void __init cpu_init_topology(struct cpu } topo_set_ids(&tscan); - topo_set_max_cores(&tscan); /* * AMD systems have Nodes per package which cannot be mapped to