From patchwork Tue Feb 13 21:06:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 200655 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp815314dyb; Tue, 13 Feb 2024 13:21:50 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUsbwWLkxWUnHlp+0vDPNoAbK4Vre76QkW0eUEh5xOtq2IxHCnfvauqRjaBkeg4TBWHHdU2C7bDKy3K0y4MFIE8y3ox7A== X-Google-Smtp-Source: AGHT+IFnfUBmA8NxtojS+rRfKGE/d3Nv78Q7MPmntjeI5pkD3T0zBKJhLcwqhO1DR7QQlKh/xVDU X-Received: by 2002:a2e:b78a:0:b0:2d0:9324:aab3 with SMTP id n10-20020a2eb78a000000b002d09324aab3mr562866ljo.30.1707859310384; Tue, 13 Feb 2024 13:21:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707859310; cv=pass; d=google.com; s=arc-20160816; b=TIIzBsEVRLVrKRuvykqgmLDku4qS1PCJAOX7okXhtSnW5vUQBgJewztRAiZGvE7JvK zHZpMhWPApvJrlCryi9t3NDGF7iYzDTsBF9mBc5aNKeYUbHmeCwiDsYpAaSW1F+INntz IlPoRg5t7Ype3o6o4rKz0Uyk5MTo3jJqvJv6Wu1OLj+ZmC5ftajuYro++Y+J32CA0RCn 02B46ak+R0WdrDG5ATRdPX34ENEOsdKOuQXNLylmc/Uy/WM2SASoE95nP8mjarCXIWJS 5EFcuKF1uvJqA4EOITCwk8R6Jl4qchsPyXCVi8UzuiXobzKm1+bHkoGgWsPyqvZbL2p7 VH4Q== 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=YfMbdqtsYsuDeqqe+wR3Oh/LyoX9LImhi1CoXPgKw4w=; b=xHj6OkIhiVImpHI5YTLtYJNIfiWxc1UWSG1hOh7gbML6FIKO0tWh9jDpju7fbI0pmg /JBTI3obJTkyTMe3as0HN56+/qdt37OWtaSUdatEP5qKELdUD7U7onSXDFtEOxk8zEBN 1yk/st7gmAlJXYo4CtvOCZKzCyratC8uRN887lhVbfoYNcsHp9wcpYIjSs4PaKek2xWJ pZFAdJDxiVestwxppRErEMKL/CdKsUUpiickcod3aklwtWYshxpJ5GWeLEKgk2gGNdEf e31V4b+UmSt5MYf/Mq3n7u9X88+7G+4MG5JNLiYWAosGHtpbA4cppyI+CL3/olqWszl7 3g8w==; 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-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64312-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de X-Forwarded-Encrypted: i=2; AJvYcCX0P7Bb8lsjaTcqM1OEb+NyRP0uhQLRlQXlZA3Dsr9Knr2kox640UizyeEPJpVUxWakHEIoh3rHaqijh8Gm59SaOfuAEQ== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id s30-20020a508d1e000000b00560c6b87f7dsi3968110eds.57.2024.02.13.13.21.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 13:21:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64312-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; 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-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64312-ouuuleilei=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 am.mirrors.kernel.org (Postfix) with ESMTPS id CC4141F226C8 for ; Tue, 13 Feb 2024 21:21:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 38ED9126F0E; Tue, 13 Feb 2024 21:06:17 +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 Date: Tue, 13 Feb 2024 22:06:10 +0100 (CET) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790820284309375085 X-GMAIL-MSGID: 1790820284309375085 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