From patchwork Tue Feb 13 21:04:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 200599 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp807610dyb; Tue, 13 Feb 2024 13:05:45 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXrSHfLex3g777VpVUioO87Znyq5u781EynnkENZo2vlpOHL5maI2Azz1V+x0xLzEtvw30PfJj9yrSZVRxUXSdXVD9wBA== X-Google-Smtp-Source: AGHT+IFvR0tbVxyx1DEAGZir1aBAfHmHZi2UkArfgEphUVIAgJ4HpYqAT6p+KnP1+PhmPOVkMxeK X-Received: by 2002:a05:620a:2626:b0:785:d7d2:8d6f with SMTP id z38-20020a05620a262600b00785d7d28d6fmr916499qko.48.1707858344918; Tue, 13 Feb 2024 13:05:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707858344; cv=pass; d=google.com; s=arc-20160816; b=WGztO2oQnK9t/pERl31RcfiReFP8gK2MNvgTVE+esHe2t/Q4aVyyDgm0HZuRKqXWop WJHMKwM+0dMhBWA8SFSc2dkqeuprfrrEzR3wl3E3bHxOTim5sQlawkFvtegb28WdXdvx v5BjyV2gXEJ10BZhavDkXrAwrEV2YFXrJUbKg5OSQSo11U37aYN3XTuSEDJYHuMeov1U rK65SrCuUOfx/vJPiEBNXxCp1qAAkVuo3JgTP68ueNA7tRPomDuylUQ6tKLAp/KHxIpZ MfAajtpGY8/Pc69HhW4mKcfKnRB4j67N1jDyA7vUOVbx9Uvb8qeP3jSWmO2ZtMaXsy/u gXAw== 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=1lKOz3r5YAV9yvgGm1hRjNwyrAYja9t+7zcOsEFt+AM=; fh=r+OrslvOXdzNKXs4i5+XnTm6BxjEPg0omgxNniQYUWk=; b=mEhMIXc/gTKefyPpHS4pM2P1AFWM5SwOvdl4EmvsZtNNthc9UMiv2LbVb+7GoD2lMv Ft9ALtF96AFaFlXd6WYasyj7yyT1474L8ff9HHpOHHYMReplhIteUR/KI4FENz+BSHIO Vmeuu4P7wwEVxiDI1bJ/GoZo/iBDGZmhXbhwQxW3fN/lwvBk5PjFKcFAexll2zu+FGhP ljW7SHp19Lkfe57E1Waqdyr8PW2JYEAp9lYVOZwfB6tUxUcq2wJ0gwwdhLNI5/nVFSsW x1UYzH4yY1c2KR3QgnoXmQEjHAk1tFxlduGsWYyB6K+FFCrqfZg3KGlekpUb85rtVmr4 z7Vw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Op16XB2b; 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-64245-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64245-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de X-Forwarded-Encrypted: i=2; AJvYcCWqBag2RRqdhq8ubfyIg94kp6kiVYp7+R32nEbzTcGyGVsFrtuguPp1AaSb1TO0BYxb6Yl5PQcaIax+4kUdq8XKF2URQw== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id w21-20020a05620a445500b00785564a436asi10327744qkp.92.2024.02.13.13.05.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 13:05:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64245-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Op16XB2b; 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-64245-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64245-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id A573C1C2249E for ; Tue, 13 Feb 2024 21:05:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 98233629E1; Tue, 13 Feb 2024 21:04:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Op16XB2b"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="OjxW5LyE" 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 2AA8A6216A for ; Tue, 13 Feb 2024 21:04:09 +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=1707858251; cv=none; b=hYg0bf6FQ6O9WhGUkAzueH3TUU6YGnyFHqMlABarSj7t7JJ7svGGuXMJoOQ+e+NDGG2k5Nkff3j1GvG/tSlEUOdcYo/E/JDdfOoMNwcWPc/2s/JMxg20vrpzQxvBsefJ7cK8NcgfhZUbtK1tAcmljawFDqPCiDPvEAikmTBLHsY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707858251; c=relaxed/simple; bh=8sN0N+03xfHFR8zfQVU/fwvb27xVjfSxvPiL9JAXKTY=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=SDkWhT77NorWECWVfLkC1ZbtoMFRLti1/CjKEcnTArp9xLFeMPWMqBU4+bvXaL8TdtF5KTDmuLtTqyAe2Wubqz3TLKePw2FSzqfIPiB+NAbEc38FyOhDDxo5srqjeTLHSSUjFfhyMgoxp94+5G0y/Hp7ulSDKORCHcleJtcoDeU= 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=Op16XB2b; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=OjxW5LyE; 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: <20240212153624.830571770@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1707858247; 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=1lKOz3r5YAV9yvgGm1hRjNwyrAYja9t+7zcOsEFt+AM=; b=Op16XB2bRB6DXBGrAFHj+Bzv+wcdaoh/oBDSls5+6bEM/RiBVIGN5JAfhe1YJ5SOh5/W6P 9DsNyR4y3N+FkeFHc/4k/NXOK+yMR7J40JNKE9IFxZR2yN42B2GLFYNFLqA0YyV6Az2IqG 3J34bHDuUElk8RjDU0zleRtkX0FMhgPsjnz0BEng7Q7qYrT32a6N0L4zkYDH80ZdCbIrh4 1d5BtjHjLhxRsLMG7OJTmNdvxTxeUDgR2MAvO1dCEFK3qu8JWLM5tX+P3hsgRVSp/VTdND y+NE3aV7AGr9fycXjJ4PWkT+tWfCfb7xuZg/WI7IVVfl/a4ahLzElKibjW2qDA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1707858247; 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=1lKOz3r5YAV9yvgGm1hRjNwyrAYja9t+7zcOsEFt+AM=; b=OjxW5LyEqea/SN8Su3VowiBnOVZkyE/Bs3cFl1fS7NuS+IwvDsLBvh401am9FRTkOEaU6I QqtNQiPEDWN7+xDQ== 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)" , Wang Wendy Subject: [patch V6 06/19] x86/cpu: Provide a sane leaf 0xb/0x1f parser References: <20240212153109.330805450@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:04:07 +0100 (CET) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790819271817827612 X-GMAIL-MSGID: 1790819271817827612 From: Thomas Gleixner detect_extended_topology() along with it's early() variant is a classic example for duct tape engineering: - It evaluates an array of subleafs with a boatload of local variables for the relevant topology levels instead of using an array to save the enumerated information and propagate it to the right level - It has no boundary checks for subleafs - It prevents updating the die_id with a crude workaround instead of checking for leaf 0xb which does not provide die information. - It's broken vs. the number of dies evaluation as it uses: num_processors[DIE_LEVEL] / num_processors[CORE_LEVEL] which "works" only correctly if there is none of the intermediate topology levels (MODULE/TILE) enumerated. There is zero value in trying to "fix" that code as the only proper fix is to rewrite it from scratch. Implement a sane parser with proper code documentation, which will be used for the consolidated topology evaluation in the next step. Signed-off-by: Thomas Gleixner Tested-by: Juergen Gross Tested-by: Sohil Mehta Tested-by: Michael Kelley Tested-by: Zhang Rui Tested-by: Wang Wendy --- V6: Add 'Fixing it up' to the SMT warning printk - Arjan. --- arch/x86/kernel/cpu/Makefile | 2 arch/x86/kernel/cpu/topology.h | 12 +++ arch/x86/kernel/cpu/topology_ext.c | 130 +++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kernel/cpu/topology_ext.c --- --- a/arch/x86/kernel/cpu/Makefile +++ b/arch/x86/kernel/cpu/Makefile @@ -18,7 +18,7 @@ KMSAN_SANITIZE_common.o := n KCSAN_SANITIZE_common.o := n obj-y := cacheinfo.o scattered.o -obj-y += topology_common.o topology.o +obj-y += topology_common.o topology_ext.o topology.o obj-y += common.o obj-y += rdrand.o obj-y += match.o --- a/arch/x86/kernel/cpu/topology.h +++ b/arch/x86/kernel/cpu/topology.h @@ -16,6 +16,7 @@ void cpu_init_topology(struct cpuinfo_x8 void cpu_parse_topology(struct cpuinfo_x86 *c); void topology_set_dom(struct topo_scan *tscan, enum x86_topology_domains dom, unsigned int shift, unsigned int ncpus); +bool cpu_parse_topology_ext(struct topo_scan *tscan); static inline u32 topo_shift_apicid(u32 apicid, enum x86_topology_domains dom) { @@ -36,4 +37,15 @@ static inline u32 topo_domain_mask(enum return (1U << x86_topo_system.dom_shifts[dom]) - 1; } +/* + * Update a domain level after the fact without propagating. Used to fixup + * broken CPUID enumerations. + */ +static inline void topology_update_dom(struct topo_scan *tscan, enum x86_topology_domains dom, + unsigned int shift, unsigned int ncpus) +{ + tscan->dom_shifts[dom] = shift; + tscan->dom_ncpus[dom] = ncpus; +} + #endif /* ARCH_X86_TOPOLOGY_H */ --- /dev/null +++ b/arch/x86/kernel/cpu/topology_ext.c @@ -0,0 +1,130 @@ +// SPDX-License-Identifier: GPL-2.0 +#include + +#include +#include +#include + +#include "cpu.h" + +enum topo_types { + INVALID_TYPE = 0, + SMT_TYPE = 1, + CORE_TYPE = 2, + MAX_TYPE_0B = 3, + MODULE_TYPE = 3, + TILE_TYPE = 4, + DIE_TYPE = 5, + DIEGRP_TYPE = 6, + MAX_TYPE_1F = 7, +}; + +/* + * Use a lookup table for the case that there are future types > 6 which + * describe an intermediate domain level which does not exist today. + */ +static const unsigned int topo_domain_map_0b_1f[MAX_TYPE_1F] = { + [SMT_TYPE] = TOPO_SMT_DOMAIN, + [CORE_TYPE] = TOPO_CORE_DOMAIN, + [MODULE_TYPE] = TOPO_MODULE_DOMAIN, + [TILE_TYPE] = TOPO_TILE_DOMAIN, + [DIE_TYPE] = TOPO_DIE_DOMAIN, + [DIEGRP_TYPE] = TOPO_DIEGRP_DOMAIN, +}; + +static inline bool topo_subleaf(struct topo_scan *tscan, u32 leaf, u32 subleaf, + unsigned int *last_dom) +{ + unsigned int dom, maxtype; + const unsigned int *map; + struct { + // eax + u32 x2apic_shift : 5, // Number of bits to shift APIC ID right + // for the topology ID at the next level + : 27; // Reserved + // ebx + u32 num_processors : 16, // Number of processors at current level + : 16; // Reserved + // ecx + u32 level : 8, // Current topology level. Same as sub leaf number + type : 8, // Level type. If 0, invalid + : 16; // Reserved + // edx + u32 x2apic_id : 32; // X2APIC ID of the current logical processor + } sl; + + switch (leaf) { + case 0x0b: maxtype = MAX_TYPE_0B; map = topo_domain_map_0b_1f; break; + case 0x1f: maxtype = MAX_TYPE_1F; map = topo_domain_map_0b_1f; break; + default: return false; + } + + cpuid_subleaf(leaf, subleaf, &sl); + + if (!sl.num_processors || sl.type == INVALID_TYPE) + return false; + + if (sl.type >= maxtype) { + pr_err_once("Topology: leaf 0x%x:%d Unknown domain type %u\n", + leaf, subleaf, sl.type); + /* + * It really would have been too obvious to make the domain + * type space sparse and leave a few reserved types between + * the points which might change instead of following the + * usual "this can be fixed in software" principle. + */ + dom = *last_dom + 1; + } else { + dom = map[sl.type]; + *last_dom = dom; + } + + if (!dom) { + tscan->c->topo.initial_apicid = sl.x2apic_id; + } else if (tscan->c->topo.initial_apicid != sl.x2apic_id) { + pr_warn_once(FW_BUG "CPUID leaf 0x%x subleaf %d APIC ID mismatch %x != %x\n", + leaf, subleaf, tscan->c->topo.initial_apicid, sl.x2apic_id); + } + + topology_set_dom(tscan, dom, sl.x2apic_shift, sl.num_processors); + return true; +} + +static bool parse_topology_leaf(struct topo_scan *tscan, u32 leaf) +{ + unsigned int last_dom; + u32 subleaf; + + /* Read all available subleafs and populate the levels */ + for (subleaf = 0, last_dom = 0; topo_subleaf(tscan, leaf, subleaf, &last_dom); subleaf++); + + /* If subleaf 0 failed to parse, give up */ + if (!subleaf) + return false; + + /* + * There are machines in the wild which have shift 0 in the subleaf + * 0, but advertise 2 logical processors at that level. They are + * truly SMT. + */ + if (!tscan->dom_shifts[TOPO_SMT_DOMAIN] && tscan->dom_ncpus[TOPO_SMT_DOMAIN] > 1) { + unsigned int sft = get_count_order(tscan->dom_ncpus[TOPO_SMT_DOMAIN]); + + pr_warn_once(FW_BUG "CPUID leaf 0x%x subleaf 0 has shift level 0 but %u CPUs. Fixing it up.\n", + leaf, tscan->dom_ncpus[TOPO_SMT_DOMAIN]); + topology_update_dom(tscan, TOPO_SMT_DOMAIN, sft, tscan->dom_ncpus[TOPO_SMT_DOMAIN]); + } + + set_cpu_cap(tscan->c, X86_FEATURE_XTOPOLOGY); + return true; +} + +bool cpu_parse_topology_ext(struct topo_scan *tscan) +{ + /* Intel: Try leaf 0x1F first. */ + if (tscan->c->cpuid_level >= 0x1f && parse_topology_leaf(tscan, 0x1f)) + return true; + + /* Intel/AMD: Fall back to leaf 0xB if available */ + return tscan->c->cpuid_level >= 0x0b && parse_topology_leaf(tscan, 0x0b); +}