Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp1178997rdf; Wed, 22 Nov 2023 07:35:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IHsxuxj1ou8Qj19ArdK0OAmri2dwsZ81Bw0bq0Zc0G/3BSnI25rxmsQJYePxNZk2R2Cjr0R X-Received: by 2002:a17:902:7d84:b0:1cf:6780:4225 with SMTP id a4-20020a1709027d8400b001cf67804225mr2634421plm.55.1700667304467; Wed, 22 Nov 2023 07:35:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700667304; cv=none; d=google.com; s=arc-20160816; b=jhTuCm9bt7RSenVmmPTUPtdhg6u+K8hya7XQGpES5sMRz9LzV++7iSxDQKpHsK5gdQ UbtAUGmzhcDKSZLXqQEmU0L+5reAsRZ7UYI+vhAPqA+MUxhVeKlS4fM2xB2xZvlDYs4W xeBJcGPfbXtVdExITqiIJ7wTew/UjImCpIiQ47tVOWUNYMgueuehM9dpteLuzlBZBe8G Zb4ssz57Iu+D34Dt/DKwhhIVwQutYNCdMvvbnRg2C56NYbROmd0EdXB++Q1+jEpQferM ofm53FMlZkxDD+AFPMYTKzSR9bRM2gxvrk/1EicVqjuFF9to/rWVPXS6u0NVOxa9OcYh ZY1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=F/CMk5Fztrly772CrQJU6bDFc3WIzPkQVcJZzEA/Y+k=; fh=4qZJs+1Th4Hm+yDQ6Sfef5taEHXNObyDs+Afk5fdyJs=; b=k1JH4V2LagQ1ylnzzpzhJO7afbGK2Vdk0glVH/IIukUt0LAiAw1x00Fq+aGod+8uYo vxOyAgaCa2k8HKneBUhSbMkKczuCQ8DQSwEYD6OZFUJJee+QHEFT0FcOniI2M28WyEb3 rMdwIMKfjDSzHr8OqUw57hE7UvfFjE6vhvp8vCO+WdAKvFATbfc+2XeIs5l3Cl3r5N1Y yknklAUKlGouUcl2Nty3egXrvp4m1n+UnfVKB2txX/uMEYDWpdHYtzBIaoOCtghij4ok dsX/M3fHB2iG17PxiRCp4CBeS6VMu1Vi8pb/Fr+cs517xzG/Zj7QmPoS8dhBLGn4d7nf G6Hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rsx1ZVw8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id z14-20020a170903018e00b001cf676859ffsi5636675plg.367.2023.11.22.07.34.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 07:35:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rsx1ZVw8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 6D7C08276C0C; Wed, 22 Nov 2023 07:34:35 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343967AbjKVPc2 (ORCPT + 99 others); Wed, 22 Nov 2023 10:32:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41570 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229634AbjKVPc0 (ORCPT ); Wed, 22 Nov 2023 10:32:26 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F5311B9; Wed, 22 Nov 2023 07:32:22 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E3679C433D9; Wed, 22 Nov 2023 15:32:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700667142; bh=wZOl31CRHaL+j/d3aJ+92oVepGGCgSusF0xwqJ0xWVA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rsx1ZVw8rrJ/ib+aFZJWW05Sa2njqfHQt4XYJQgvKO3N44Mp0MczOsS3UMXKYAq3Y aR+vYeQPmo/lMYrxIFyOwksfOZlgcGGQgwm3Uskjd+/dzEXHrGSFre8GQqm6IO6bcy LLBtNpC+PXIozk2jXyXTR5ZrwPjDFLxI6jFzNpEBOZv2F2Q/WBZ9YnB5lIesIxlsx0 XupYcP232GeXUXdjErWFJhoWzCbO10S9ZgIBAC0xf0O//HC9HBbSCJOtE9IoHk5LMp 0S11A21nDYxJUSFYcanv1TVijFU3WEMn5dTYALrwHIzgA5u+cnSVXymBHX9SLEuB+X vo1wApn+gvf7g== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Zhang Rui , Thomas Gleixner , Peter Zijlstra , Sasha Levin , rafael@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, linux-acpi@vger.kernel.org Subject: [PATCH AUTOSEL 6.6 02/17] x86/acpi: Ignore invalid x2APIC entries Date: Wed, 22 Nov 2023 10:31:31 -0500 Message-ID: <20231122153212.852040-2-sashal@kernel.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231122153212.852040-1-sashal@kernel.org> References: <20231122153212.852040-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.6.2 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.3 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Wed, 22 Nov 2023 07:34:35 -0800 (PST) From: Zhang Rui [ Upstream commit ec9aedb2aa1ab7ac420c00b31f5edc5be15ec167 ] Currently, the kernel enumerates the possible CPUs by parsing both ACPI MADT Local APIC entries and x2APIC entries. So CPUs with "valid" APIC IDs, even if they have duplicated APIC IDs in Local APIC and x2APIC, are always enumerated. Below is what ACPI MADT Local APIC and x2APIC describes on an Ivebridge-EP system, [02Ch 0044 1] Subtable Type : 00 [Processor Local APIC] [02Fh 0047 1] Local Apic ID : 00 ... [164h 0356 1] Subtable Type : 00 [Processor Local APIC] [167h 0359 1] Local Apic ID : 39 [16Ch 0364 1] Subtable Type : 00 [Processor Local APIC] [16Fh 0367 1] Local Apic ID : FF ... [3ECh 1004 1] Subtable Type : 09 [Processor Local x2APIC] [3F0h 1008 4] Processor x2Apic ID : 00000000 ... [B5Ch 2908 1] Subtable Type : 09 [Processor Local x2APIC] [B60h 2912 4] Processor x2Apic ID : 00000077 As a result, kernel shows "smpboot: Allowing 168 CPUs, 120 hotplug CPUs". And this wastes significant amount of memory for the per-cpu data. Plus this also breaks https://lore.kernel.org/all/87edm36qqb.ffs@tglx/, because __max_logical_packages is over-estimated by the APIC IDs in the x2APIC entries. According to https://uefi.org/specs/ACPI/6.5/05_ACPI_Software_Programming_Model.html#processor-local-x2apic-structure: "[Compatibility note] On some legacy OSes, Logical processors with APIC ID values less than 255 (whether in XAPIC or X2APIC mode) must use the Processor Local APIC structure to convey their APIC information to OSPM, and those processors must be declared in the DSDT using the Processor() keyword. Logical processors with APIC ID values 255 and greater must use the Processor Local x2APIC structure and be declared using the Device() keyword." Therefore prevent the registration of x2APIC entries with an APIC ID less than 255 if the local APIC table enumerates valid APIC IDs. [ tglx: Simplify the logic ] Signed-off-by: Zhang Rui Signed-off-by: Thomas Gleixner Tested-by: Peter Zijlstra Link: https://lore.kernel.org/r/20230702162802.344176-1-rui.zhang@intel.com Signed-off-by: Sasha Levin --- arch/x86/kernel/acpi/boot.c | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/arch/x86/kernel/acpi/boot.c b/arch/x86/kernel/acpi/boot.c index c55c0ef47a187..fc5bce1b50476 100644 --- a/arch/x86/kernel/acpi/boot.c +++ b/arch/x86/kernel/acpi/boot.c @@ -63,6 +63,7 @@ int acpi_fix_pin2_polarity __initdata; #ifdef CONFIG_X86_LOCAL_APIC static u64 acpi_lapic_addr __initdata = APIC_DEFAULT_PHYS_BASE; +static bool has_lapic_cpus __initdata; static bool acpi_support_online_capable; #endif @@ -232,6 +233,14 @@ acpi_parse_x2apic(union acpi_subtable_headers *header, const unsigned long end) if (!acpi_is_processor_usable(processor->lapic_flags)) return 0; + /* + * According to https://uefi.org/specs/ACPI/6.5/05_ACPI_Software_Programming_Model.html#processor-local-x2apic-structure + * when MADT provides both valid LAPIC and x2APIC entries, the APIC ID + * in x2APIC must be equal or greater than 0xff. + */ + if (has_lapic_cpus && apic_id < 0xff) + return 0; + /* * We need to register disabled CPU as well to permit * counting disabled CPUs. This allows us to size @@ -1114,10 +1123,7 @@ static int __init early_acpi_parse_madt_lapic_addr_ovr(void) static int __init acpi_parse_madt_lapic_entries(void) { - int count; - int x2count = 0; - int ret; - struct acpi_subtable_proc madt_proc[2]; + int count, x2count = 0; if (!boot_cpu_has(X86_FEATURE_APIC)) return -ENODEV; @@ -1126,21 +1132,11 @@ static int __init acpi_parse_madt_lapic_entries(void) acpi_parse_sapic, MAX_LOCAL_APIC); if (!count) { - memset(madt_proc, 0, sizeof(madt_proc)); - madt_proc[0].id = ACPI_MADT_TYPE_LOCAL_APIC; - madt_proc[0].handler = acpi_parse_lapic; - madt_proc[1].id = ACPI_MADT_TYPE_LOCAL_X2APIC; - madt_proc[1].handler = acpi_parse_x2apic; - ret = acpi_table_parse_entries_array(ACPI_SIG_MADT, - sizeof(struct acpi_table_madt), - madt_proc, ARRAY_SIZE(madt_proc), MAX_LOCAL_APIC); - if (ret < 0) { - pr_err("Error parsing LAPIC/X2APIC entries\n"); - return ret; - } - - count = madt_proc[0].count; - x2count = madt_proc[1].count; + count = acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_APIC, + acpi_parse_lapic, MAX_LOCAL_APIC); + has_lapic_cpus = count > 0; + x2count = acpi_table_parse_madt(ACPI_MADT_TYPE_LOCAL_X2APIC, + acpi_parse_x2apic, MAX_LOCAL_APIC); } if (!count && !x2count) { pr_err("No LAPIC entries present\n"); -- 2.42.0