Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp11011480rwd; Thu, 22 Jun 2023 07:41:22 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7PrNi/6nHxpmJWbDi1IvBTzGZ9ybLQPtxOLWo3T74UCd/HzT0N8rMNkJs+Xjqx+MBtgqBZ X-Received: by 2002:a05:6a20:9191:b0:10b:9dc1:c5e5 with SMTP id v17-20020a056a20919100b0010b9dc1c5e5mr18501554pzd.34.1687444882061; Thu, 22 Jun 2023 07:41:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687444882; cv=none; d=google.com; s=arc-20160816; b=GRLaFe2lSmEfxpPF0kpN4O0SddvypX0kN2ap7BDa20nbOG0M3I/EF3NyOAlAUURuNf PGBSDiNJhYLEG0G/zOc/u6K8czPGLrlHhmvbFubiiv8vzxWxLNm/BZG83onQ8plgfjtH Ay4VOqcdiaJgDyxPGXa1fvX+sBgmvP8gQunb5dBxmWZKjzHgdV7WxZxpzhJ3wI1Hz4y3 zbd6Rq4HI3qpqrr478GN2taTDCAQJLK3LtMr5i+LUSXzDrq+IZmwR5bMutvBFyOz9lAM Hu3X3Mam4KA1kJkccYo9pOpJ14oygGt7oLasEpMigc/UQCu2DYjJIHQbrhNTAVf/oSW/ rwvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:dkim-signature:dkim-signature:from; bh=FXNabFu3R+CbSGJ3eO4pu2dnBfEqHysqMWZ3iEe7Y0Q=; b=Eu8cJdAGyMv9DlRW/Mktb67jcgaVP56Y0LSHvg/J7AMSL+ypSV/DdeJDLSQo9Azioo GGhPsLDC9W//aMnVm7fTbC0566DB37q8gAQ9LC8GObOXSgmOnRZbZNLaB54AAa/e7sNz 6QGdgq1gXBOu4kVqwvVHrl0+rzKQsGCHJDSxrIl+Kkxu93bDKT6dVo4NorczXRFBRy90 OeHiyeysmxPc4oTCrP6r304G4sfaz7xeHd3J4jvTDAeEdCyPmTxXQbuh5ivPDvbUG36/ TuHgboP7Q+djLzqjys4HE6Lyzsw1gpilge1pV6iHzL4fjPSRtTLzwQ4BFQ/J2l+ggNry HZ0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Q709QPTQ; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q15-20020a056a00150f00b0066a4eb18f6csi3210079pfu.93.2023.06.22.07.41.09; Thu, 22 Jun 2023 07:41:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Q709QPTQ; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230355AbjFVO13 (ORCPT + 99 others); Thu, 22 Jun 2023 10:27:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229628AbjFVO11 (ORCPT ); Thu, 22 Jun 2023 10:27:27 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B91919B7 for ; Thu, 22 Jun 2023 07:27:26 -0700 (PDT) From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1687444044; 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: in-reply-to:in-reply-to:references:references; bh=FXNabFu3R+CbSGJ3eO4pu2dnBfEqHysqMWZ3iEe7Y0Q=; b=Q709QPTQzQFuc/s1h83xeJ+U2hg4ZjFW0TzYMIZWZ4t+NvNDqdwmfiluHuy97ZOyoFmloE 0Eu63qHBkAKwQHtGlKs1mMMEqSpnwBhmULzITMXod6Nat2eTg1bC9DaZRU8eRYUnquDFaN u2BvF0exfdBFQaonxker0buFCYKPtEGhsXkCjK30PON11Wu84dqh49uYjrpmHpX2I6UBqk 4XEvEMvyQoJu065VgwNdLfmNeXtyNWj9SbV8p4sUynCFPJOKujeVpz255uHrf0EGW+nfgD YgUZzFoihyoqSLhskz0waOASK3G6spHrpzk+lxNd5dzE2BkQI8Y/m+AqcgUIAA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1687444044; 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: in-reply-to:in-reply-to:references:references; bh=FXNabFu3R+CbSGJ3eO4pu2dnBfEqHysqMWZ3iEe7Y0Q=; b=nFRGSvijeky8vzMenKyb5Cz8B4Ih/YoXcicZ1GDA3etHLquMupGs7aus2yCTKMLf1MOAaV cSQpU451LOV4C5Bg== To: Feng Tang , Peter Zijlstra Cc: Ingo Molnar , Borislav Petkov , Dave Hansen , "H . Peter Anvin" , David Woodhouse , "Paul E . McKenney" , x86@kernel.org, linux-kernel@vger.kernel.org, rui.zhang@intel.com, tim.c.chen@intel.com Subject: Re: [Patch v2 2/2] x86/tsc: use logical_packages as a better estimation of socket numbers In-Reply-To: References: <20230613052523.1106821-1-feng.tang@intel.com> <20230613052523.1106821-2-feng.tang@intel.com> <20230615092021.GE1683497@hirez.programming.kicks-ass.net> Date: Thu, 22 Jun 2023 16:27:23 +0200 Message-ID: <87h6qz7et0.ffs@tglx> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 16 2023 at 15:18, Feng Tang wrote: > On Thu, Jun 15, 2023 at 11:20:21AM +0200, Peter Zijlstra wrote: > Yes. Rui is working on a MADT based parsing which may take a while > before being stable, given all kinds of fancy firmware out there. Please not yet another mad table parser. The topology can be evaluated during early boot via: 1) The APIC IDs of the possible CPUs. 2) CPUID leaf 0xb or 0x1f where the topmost subleaf gives the number of bits to shift the APIC ID right for the package/socket Trying to accomodate for anything else than the documented enumeration is crazy. If fancy firmware is broken then they can keep the pieces. So something like the below should just work. I fundamentally hate the hackery in topology.c, but cleaning this mess up is a completely different problem and already worked on. Thanks, tglx --- --- a/arch/x86/include/asm/apic.h +++ b/arch/x86/include/asm/apic.h @@ -509,9 +509,12 @@ extern int default_check_phys_apicid_pre #ifdef CONFIG_SMP bool apic_id_is_primary_thread(unsigned int id); void apic_smt_update(void); +extern unsigned int apic_to_pkg_shift; +bool logical_packages_update(u32 apicid); #else static inline bool apic_id_is_primary_thread(unsigned int id) { return false; } static inline void apic_smt_update(void) { } +static inline bool logical_packages_update(u32 apicid) { return true; } #endif struct msi_msg; --- a/arch/x86/kernel/acpi/boot.c +++ b/arch/x86/kernel/acpi/boot.c @@ -177,6 +177,9 @@ static int acpi_register_lapic(int id, u return -EINVAL; } + if (!logical_packages_update(acpiid)) + return -EINVAL; + if (!enabled) { ++disabled_cpus; return -EINVAL; --- a/arch/x86/kernel/cpu/amd.c +++ b/arch/x86/kernel/cpu/amd.c @@ -692,6 +692,8 @@ static void early_init_amd(struct cpuinf } } + detect_extended_topology_early(c); + if (cpu_has(c, X86_FEATURE_TOPOEXT)) smp_num_siblings = ((cpuid_ebx(0x8000001e) >> 8) & 0xff) + 1; } --- a/arch/x86/kernel/cpu/topology.c +++ b/arch/x86/kernel/cpu/topology.c @@ -29,6 +29,8 @@ unsigned int __max_die_per_package __rea EXPORT_SYMBOL(__max_die_per_package); #ifdef CONFIG_SMP +unsigned int apic_to_pkg_shift __ro_after_init; + /* * Check if given CPUID extended topology "leaf" is implemented */ @@ -66,7 +68,7 @@ int detect_extended_topology_early(struc { #ifdef CONFIG_SMP unsigned int eax, ebx, ecx, edx; - int leaf; + int leaf, subleaf; leaf = detect_extended_topology_leaf(c); if (leaf < 0) @@ -80,6 +82,14 @@ int detect_extended_topology_early(struc */ c->initial_apicid = edx; smp_num_siblings = max_t(int, smp_num_siblings, LEVEL_MAX_SIBLINGS(ebx)); + + for (subleaf = 1; subleaf < 8; subleaf++) { + cpuid_count(leaf, subleaf, &eax, &ebx, &ecx, &edx); + + if (ebx == 0 || !LEAFB_SUBTYPE(ecx)) + break; + apic_to_pkg_shift = BITS_SHIFT_NEXT_LEVEL(eax); + } #endif return 0; } --- a/arch/x86/kernel/smpboot.c +++ b/arch/x86/kernel/smpboot.c @@ -1501,17 +1501,50 @@ void __init native_smp_prepare_boot_cpu( native_pv_lock_init(); } +bool logical_packages_update(u32 apicid) +{ + unsigned int pkg; + + if (!apic_to_pkg_shift) + return true; + + pkg = (apicid >> apic_to_pkg_shift) + 1; + if (pkg <= __max_logical_packages) + return true; + + if (system_state == SYSTEM_BOOTING) { + __max_logical_packages = pkg; + return true; + } + + pr_err("Physical hotplug APICID %x package %u > max logical packages %u\n", + apicid, pkg, __max_logical_packages); + return false; +} + void __init calculate_max_logical_packages(void) { - int ncpus; + unsigned int ncpus, npkg; /* * Today neither Intel nor AMD support heterogeneous systems so * extrapolate the boot cpu's data to all packages. */ ncpus = cpu_data(0).booted_cores * topology_max_smt_threads(); - __max_logical_packages = DIV_ROUND_UP(total_cpus, ncpus); - pr_info("Max logical packages: %u\n", __max_logical_packages); + npkg = DIV_ROUND_UP(total_cpus, ncpus); + + /* Did logical_packages_update() set up __max_logical_packages? */ + if (!__max_logical_packages) { + __max_logical_packages = npkg; + } else { + pr_info("Max logical packages ACPI enumeration: %u\n", + __max_logical_packages); + if (npkg <= __max_logical_packages) + return; + __max_logical_packages = npkg; + } + + pr_info("Max logical packages estimated: %u\n", __max_logical_packages); } void __init native_smp_cpus_done(unsigned int max_cpus)