Message ID | 20230124163319.2277355-5-kim.phillips@amd.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2254288wrn; Tue, 24 Jan 2023 08:43:14 -0800 (PST) X-Google-Smtp-Source: AMrXdXshaOtbv8WpuG5paHtN8pmKD1oRLmy6BzCrm3PAQoS4aqmNQMGJabo7jv8B8XMWtuxlaXpY X-Received: by 2002:a05:6a00:2194:b0:58a:fa8d:ecce with SMTP id h20-20020a056a00219400b0058afa8deccemr33343506pfi.29.1674578593887; Tue, 24 Jan 2023 08:43:13 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1674578593; cv=pass; d=google.com; s=arc-20160816; b=giAhSpP8oDw/FIqq8KR5Lm4R6eOghVexgH2MgAFwa+BfgO4lynOZm61RTRsgJmSmT7 QI//cLwOVjpM/3A3fWWCZIsWI3fZm6asIzLMRGOqvvNRJ2mbr2RsBD/5c0YOZFLxrwUA L2h9i5hkuICA4an9OIjFkLVlQI14LpbWf9NxmW4YjSFnpUtaXLQkaFr1b3gtkmJjCLGK 1Lso2KvKktIGVKw0m2gpNOVH7DT264lu9VJNhn8zORkAxqhCmGCFBTwgfbfXE0z7UNLM jfmebC04xi83p44cm3f6unsf57lGGSACybFQGKpOlMEzmshtrHkM6ipOMAINd0jyrF+z +jYg== ARC-Message-Signature: i=2; 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=9yDG4uvs1w0obZGxxDm+raAwQbQJ/zFfwkygQNbaUkQ=; b=AMRyG/q66eVvETuOTuMkQBu4qe4y+cOvrZEvDq0e6KsC37d0Jtaub6DrkX12MoU4dC xvLYb+IzyBHnSCOhLFbL/0m1RhqSZ2SBeGyg1PRjHZ8HkNrWqDeG8cbzPOv6Dmcfy/yw ns6zoeizdn7zq9MRpeWneVDXljrBZRNPE8n4u8llBHQVbD0Kgw1hvrF44KY9+cm7WM5V y+BmPPQYhcpy8RlJOdqr7ZIwQH+wjTJqUBba00Aox/p2EqZsmeAZELhpAN0QYcWGxXsn 4kQp1u2+QS3EKEjycStoTEQADBm6m2nTJg8uX7SfjH/ZzK/kWPMYJnxN3mWp9zs320T2 Idqg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=4L1iWQMW; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g7-20020aa79dc7000000b005726e8b1cedsi2555275pfq.345.2023.01.24.08.43.02; Tue, 24 Jan 2023 08:43:13 -0800 (PST) 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=@amd.com header.s=selector1 header.b=4L1iWQMW; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234305AbjAXQfC (ORCPT <rfc822;rust.linux@gmail.com> + 99 others); Tue, 24 Jan 2023 11:35:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232991AbjAXQfA (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 24 Jan 2023 11:35:00 -0500 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2048.outbound.protection.outlook.com [40.107.237.48]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E59554B8B3; Tue, 24 Jan 2023 08:34:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kLOHp3yhTp7M1ZPuGk4HLV4m7ECj3WQM7hVhwxIJR6X+uiNjdEyUlOJHc8uhAlB5oVcHw9lFMG4DW19As3f8xwCa9LcfNUlQP42wkD39AyTV3PAoUMpAvCqM8rNIxwLKTWHHb0iCfqufcmyQMcAMSBEs6KRqv1akvY4XM1Oh2Yf6EMa5SPz+18YJG0uhdLtUEgXZJIHDpVvudQbOxXWavijvD4x7jt4Ro+Hl20uhbqdnKZVwVhzWFLfhnYrz5RrvuzrPNjmCXqk7pk3imhf+QvyLteZhmE01/4k/2Uj6JVZ9TcyznkvX8uj10jrjYUN7wE8zx9zkL5JvwajaRAaDHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=9yDG4uvs1w0obZGxxDm+raAwQbQJ/zFfwkygQNbaUkQ=; b=nuolWiO5nCYMhF2ZGdJX64pzBuh6/eU8GxW5bqXYDRRKp9SGPlIBowAIBAsnQHg0C5suezXa1D5SY4g4nYPyYpcy8CbRLtoOoxZdxA1ehyctH4dTfJv+RdlLYIiFsadR37BfKz1wK5IRNW0H/V6tbKZV+HiVRXIlMR5ajwN/ZMP/1ldAM8nPL5sByDkTBxpQWgeHGZn3KGIS9y0rWSb+p2+ZjdjsNDrUfK/FEJSuUIsf8SInml3nPPe5yW38KGl0YIkSRZmwn+TU46RfLAayQdxHVqIxaisXwsbxoiFbLrAkDIqN93hE2XByC1QMLVE5K4DNRZEYmOuLoBDsYmVKWA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=9yDG4uvs1w0obZGxxDm+raAwQbQJ/zFfwkygQNbaUkQ=; b=4L1iWQMWjsfCGcDiFOHJkfUg19DGSjMVP8amXd5liLIUeTCN+ebg+56r4jdcZhMZr+bCUPyo5fUyvyAME+DjKvJSL4C0RvPq+pV7zwA4Bxr0TADhbi5oO8XQJ76Xu+vIUFOoNpo3+llgueAvdcqSWJyP5hiR8b0F5T9V1L30ohA= Received: from BN9PR03CA0273.namprd03.prod.outlook.com (2603:10b6:408:f5::8) by SN7PR12MB7022.namprd12.prod.outlook.com (2603:10b6:806:261::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 16:34:31 +0000 Received: from BN8NAM11FT013.eop-nam11.prod.protection.outlook.com (2603:10b6:408:f5:cafe::b8) by BN9PR03CA0273.outlook.office365.com (2603:10b6:408:f5::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6043.17 via Frontend Transport; Tue, 24 Jan 2023 16:34:31 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT013.mail.protection.outlook.com (10.13.176.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6043.17 via Frontend Transport; Tue, 24 Jan 2023 16:34:31 +0000 Received: from fritz.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Tue, 24 Jan 2023 10:34:29 -0600 From: Kim Phillips <kim.phillips@amd.com> To: <x86@kernel.org> CC: Kim Phillips <kim.phillips@amd.com>, Borislav Petkov <bp@alien8.de>, "Boris Ostrovsky" <boris.ostrovsky@oracle.com>, Dave Hansen <dave.hansen@linux.intel.com>, "H. Peter Anvin" <hpa@zytor.com>, Ingo Molnar <mingo@redhat.com>, Joao Martins <joao.m.martins@oracle.com>, Jonathan Corbet <corbet@lwn.net>, Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>, "Paolo Bonzini" <pbonzini@redhat.com>, Sean Christopherson <seanjc@google.com>, Thomas Gleixner <tglx@linutronix.de>, David Woodhouse <dwmw@amazon.co.uk>, Greg Kroah-Hartman <gregkh@linuxfoundation.org>, Juergen Gross <jgross@suse.com>, Peter Zijlstra <peterz@infradead.org>, Tony Luck <tony.luck@intel.com>, Tom Lendacky <thomas.lendacky@amd.com>, "Alexey Kardashevskiy" <aik@amd.com>, <kvm@vger.kernel.org>, <linux-doc@vger.kernel.org>, <linux-kernel@vger.kernel.org> Subject: [PATCH v9 4/8] x86/cpu, kvm: Move X86_FEATURE_LFENCE_RDTSC to its native leaf Date: Tue, 24 Jan 2023 10:33:15 -0600 Message-ID: <20230124163319.2277355-5-kim.phillips@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230124163319.2277355-1-kim.phillips@amd.com> References: <20230124163319.2277355-1-kim.phillips@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN8NAM11FT013:EE_|SN7PR12MB7022:EE_ X-MS-Office365-Filtering-Correlation-Id: 6202ddb3-4f19-4dde-135a-08dafe28de95 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bbu1aKUXRx7V+fK0gOPBOFDCKk15ThGBYdSLMPw55HL1v/+Y2gQhNTOf8FLjy4q6wVLhAbCOJpyXgMUiOcl87/8UDab7VrOCvtFQaq7bWMhoWWyBDdc2+D5j6mGOe4GRmyKX1XXYsUK9kaXhx55zMD2Bxw5oxgeOfyxPsnSaClXDyboiS956tbGMnRarbTBr3oJFI4Bnd/ggKnCNCR/WGv0GXUPB2wlfUnV0gKEgZs/LXPCJykXgPZ7zqRXYAck3VC9n1tjbiawr6up1rb40EPRzJq67qaKzWCNHvLIO20sLr1vaPt+DBZyV7RTiLOwhA6BezNJLFS4ovtsEDczFS34dwLsO+oszDn2F56d4m6DsAarMQqaZXxDaTpaLWHn1sS8XC2JOOrH7SKcinx6vy9qoCOTuH3wU0Id5hH4ePZnH4lGw+P1N/mjArtTgI1NxBNQmJ4F8xZ11lTv9NXt3cSURqOEk22b4znwQCjUVN+8PJmpsF2L8+3W2sl1SUZYkcbbVYqk1mkGZqYDHPLQF4DnU0HPREqgb9Uf0XDlnIgbv8HGmxrI2h1vopEhd/teVh17WJoiyPDcXyCZ3lO9qNiXz5t/JYclHdMqT58SbRMN707fRhqQdGgPRZV50x6AiyShYiQ5W6FI2ygNB18vZWEEMW6rBGmZHZ/c52I9cajW0Lk/D9yG/QXImsgsJ8qtqDcxPiqYr1FvsmnNC2T2v6yLGIW54KeWKKG5p59TWITs= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(396003)(136003)(39860400002)(346002)(451199015)(36840700001)(40470700004)(46966006)(36860700001)(83380400001)(82740400003)(81166007)(2906002)(86362001)(44832011)(8936002)(5660300002)(356005)(41300700001)(4326008)(7416002)(82310400005)(186003)(40460700003)(40480700001)(16526019)(70206006)(6916009)(8676002)(26005)(316002)(47076005)(336012)(6666004)(426003)(70586007)(2616005)(54906003)(1076003)(478600001)(7696005)(36756003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 16:34:31.2336 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6202ddb3-4f19-4dde-135a-08dafe28de95 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT013.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7022 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755922923707329606?= X-GMAIL-MSGID: =?utf-8?q?1755922923707329606?= |
Series |
x86/cpu, kvm: Support AMD Automatic IBRS
|
|
Commit Message
Kim Phillips
Jan. 24, 2023, 4:33 p.m. UTC
The LFENCE always serializing feature bit was defined as scattered
LFENCE_RDTSC and its native leaf bit position open-coded for KVM.
Add it to its newly added CPUID leaf 0x80000021 EAX proper.
With LFENCE_RDTSC is in its proper place, the kernel's set_cpu_cap()
will effectively sythesize the feature for KVM going forward.
Drop the bit description comments now it's more self-describing.
Also, in amd_init(), don't bother setting DE_CFG[1] any more.
Signed-off-by: Kim Phillips <kim.phillips@amd.com>
---
arch/x86/include/asm/cpufeatures.h | 3 ++-
arch/x86/kernel/cpu/amd.c | 2 +-
arch/x86/kvm/cpuid.c | 5 +----
3 files changed, 4 insertions(+), 6 deletions(-)
Comments
On Tue, Jan 24, 2023, Kim Phillips wrote: > The LFENCE always serializing feature bit was defined as scattered > LFENCE_RDTSC and its native leaf bit position open-coded for KVM. > Add it to its newly added CPUID leaf 0x80000021 EAX proper. > With LFENCE_RDTSC is in its proper place, the kernel's set_cpu_cap() > will effectively sythesize the feature for KVM going forward. > > Drop the bit description comments now it's more self-describing. > > Also, in amd_init(), don't bother setting DE_CFG[1] any more. > > Signed-off-by: Kim Phillips <kim.phillips@amd.com> > --- > arch/x86/include/asm/cpufeatures.h | 3 ++- > arch/x86/kernel/cpu/amd.c | 2 +- > arch/x86/kvm/cpuid.c | 5 +---- > 3 files changed, 4 insertions(+), 6 deletions(-) > > diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h > index 4637fd7a84d6..e975822951b2 100644 > --- a/arch/x86/include/asm/cpufeatures.h > +++ b/arch/x86/include/asm/cpufeatures.h > @@ -97,7 +97,7 @@ > #define X86_FEATURE_SYSENTER32 ( 3*32+15) /* "" sysenter in IA32 userspace */ > #define X86_FEATURE_REP_GOOD ( 3*32+16) /* REP microcode works well */ > #define X86_FEATURE_AMD_LBR_V2 ( 3*32+17) /* AMD Last Branch Record Extension Version 2 */ > -#define X86_FEATURE_LFENCE_RDTSC ( 3*32+18) /* "" LFENCE synchronizes RDTSC */ > +/* FREE, was #define X86_FEATURE_LFENCE_RDTSC ( 3*32+18) "" LFENCE synchronizes RDTSC */ > #define X86_FEATURE_ACC_POWER ( 3*32+19) /* AMD Accumulated Power Mechanism */ > #define X86_FEATURE_NOPL ( 3*32+20) /* The NOPL (0F 1F) instructions */ > #define X86_FEATURE_ALWAYS ( 3*32+21) /* "" Always-present feature */ > @@ -432,6 +432,7 @@ > > /* AMD-defined Extended Feature 2 EAX, CPUID level 0x80000021 (EAX), word 20 */ > #define X86_FEATURE_NO_NESTED_DATA_BP (20*32+ 0) /* "" No Nested Data Breakpoints */ > +#define X86_FEATURE_LFENCE_RDTSC (20*32+ 2) /* "" LFENCE always serializing / synchronizes RDTSC */ > > /* > * BUG word(s) > diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c > index f769d6d08b43..208c2ce8598a 100644 > --- a/arch/x86/kernel/cpu/amd.c > +++ b/arch/x86/kernel/cpu/amd.c > @@ -956,7 +956,7 @@ static void init_amd(struct cpuinfo_x86 *c) > > init_amd_cacheinfo(c); > > - if (cpu_has(c, X86_FEATURE_XMM2)) { > + if (!cpu_has(c, X86_FEATURE_LFENCE_RDTSC) && cpu_has(c, X86_FEATURE_XMM2)) { > /* > * Use LFENCE for execution serialization. On families which > * don't have that MSR, LFENCE is already serializing. > diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c > index 9764499acce2..448b5de98b8f 100644 > --- a/arch/x86/kvm/cpuid.c > +++ b/arch/x86/kvm/cpuid.c > @@ -742,12 +742,9 @@ void kvm_set_cpu_caps(void) > F(SME_COHERENT)); > > kvm_cpu_cap_mask(CPUID_8000_0021_EAX, > - F(NO_NESTED_DATA_BP) | > - BIT(2) /* LFENCE Always serializing */ | 0 /* SmmPgCfgLock */ | > + F(NO_NESTED_DATA_BP) | F(LFENCE_RDTSC) | 0 /* SmmPgCfgLock */ | > BIT(6) /* NULL_SEL_CLR_BASE */ | 0 /* PrefetchCtlMsr */ > ); > - if (cpu_feature_enabled(X86_FEATURE_LFENCE_RDTSC)) > - kvm_cpu_caps[CPUID_8000_0021_EAX] |= BIT(2) /* LFENCE Always serializing */; Gah, I was wrong. I lost track of the fact that kvm_cpu_cap_mask() does an actual CPUID, i.e. the oddball code is necessary to manual synthesize the flag. Boris, can you fold this in? --- arch/x86/kvm/cpuid.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index fb32e084a40f..12455dc5afe5 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -745,6 +745,19 @@ void kvm_set_cpu_caps(void) F(NO_NESTED_DATA_BP) | F(LFENCE_RDTSC) | 0 /* SmmPgCfgLock */ | BIT(6) /* NULL_SEL_CLR_BASE */ | 0 /* PrefetchCtlMsr */ ); + + /* + * Synthesize "LFENCE is serializing" into the AMD-defined entry in + * KVM's supported CPUID if the feature is reported as supported by the + * kernel. LFENCE_RDTSC was a Linux-defined synthetic feature long + * before AMD joined the bandwagon, e.g. LFENCE is serializing on most + * CPUs that support SSE2. On CPUs that don't support AMD's leaf, + * kvm_cpu_cap_mask() will unfortunately drop the flag due to ANDing + * the mask with the raw host CPUID, and reporting support in AMD's + * leaf can make it easier for userspace to detect the feature. + */ + if (cpu_feature_enabled(X86_FEATURE_LFENCE_RDTSC)) + kvm_cpu_cap_set(X86_FEATURE_LFENCE_RDTSC); if (!static_cpu_has_bug(X86_BUG_NULL_SEG)) kvm_cpu_caps[CPUID_8000_0021_EAX] |= BIT(6) /* NULL_SEL_CLR_BASE */; kvm_cpu_caps[CPUID_8000_0021_EAX] |= BIT(9) /* NO_SMM_CTL_MSR */; base-commit: f607476ee37397a72a2abb687bc170ce0bbec780
On Tue, Jan 24, 2023 at 09:32:55PM +0000, Sean Christopherson wrote: > Boris, can you fold this in? Sure, see below. --- From: Kim Phillips <kim.phillips@amd.com> Date: Tue, 24 Jan 2023 10:33:15 -0600 Subject: [PATCH] x86/cpu, kvm: Move X86_FEATURE_LFENCE_RDTSC to its native leaf The LFENCE always serializing feature bit was defined as scattered LFENCE_RDTSC and its native leaf bit position open-coded for KVM. Add it to its newly added CPUID leaf 0x80000021 EAX proper. With LFENCE_RDTSC in its proper place, the kernel's set_cpu_cap() will effectively synthesize the feature for KVM going forward. Also, DE_CFG[1] doesn't need to be set on such CPUs anymore. [ bp: Massage and merge diff from Sean. ] Signed-off-by: Kim Phillips <kim.phillips@amd.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Acked-by: Sean Christopherson <seanjc@google.com> Link: https://lore.kernel.org/r/20230124163319.2277355-5-kim.phillips@amd.com --- arch/x86/include/asm/cpufeatures.h | 3 ++- arch/x86/kernel/cpu/amd.c | 2 +- arch/x86/kvm/cpuid.c | 16 +++++++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index 1b2d40a96b97..901128ed4c7a 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -97,7 +97,7 @@ #define X86_FEATURE_SYSENTER32 ( 3*32+15) /* "" sysenter in IA32 userspace */ #define X86_FEATURE_REP_GOOD ( 3*32+16) /* REP microcode works well */ #define X86_FEATURE_AMD_LBR_V2 ( 3*32+17) /* AMD Last Branch Record Extension Version 2 */ -#define X86_FEATURE_LFENCE_RDTSC ( 3*32+18) /* "" LFENCE synchronizes RDTSC */ +/* FREE, was #define X86_FEATURE_LFENCE_RDTSC ( 3*32+18) "" LFENCE synchronizes RDTSC */ #define X86_FEATURE_ACC_POWER ( 3*32+19) /* AMD Accumulated Power Mechanism */ #define X86_FEATURE_NOPL ( 3*32+20) /* The NOPL (0F 1F) instructions */ #define X86_FEATURE_ALWAYS ( 3*32+21) /* "" Always-present feature */ @@ -429,6 +429,7 @@ /* AMD-defined Extended Feature 2 EAX, CPUID level 0x80000021 (EAX), word 20 */ #define X86_FEATURE_NO_NESTED_DATA_BP (20*32+ 0) /* "" No Nested Data Breakpoints */ +#define X86_FEATURE_LFENCE_RDTSC (20*32+ 2) /* "" LFENCE always serializing / synchronizes RDTSC */ /* * BUG word(s) diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c index f769d6d08b43..208c2ce8598a 100644 --- a/arch/x86/kernel/cpu/amd.c +++ b/arch/x86/kernel/cpu/amd.c @@ -956,7 +956,7 @@ static void init_amd(struct cpuinfo_x86 *c) init_amd_cacheinfo(c); - if (cpu_has(c, X86_FEATURE_XMM2)) { + if (!cpu_has(c, X86_FEATURE_LFENCE_RDTSC) && cpu_has(c, X86_FEATURE_XMM2)) { /* * Use LFENCE for execution serialization. On families which * don't have that MSR, LFENCE is already serializing. diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index aa3a6dc74e95..12455dc5afe5 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -742,12 +742,22 @@ void kvm_set_cpu_caps(void) F(SME_COHERENT)); kvm_cpu_cap_mask(CPUID_8000_0021_EAX, - F(NO_NESTED_DATA_BP) | - BIT(2) /* LFENCE Always serializing */ | 0 /* SmmPgCfgLock */ | + F(NO_NESTED_DATA_BP) | F(LFENCE_RDTSC) | 0 /* SmmPgCfgLock */ | BIT(6) /* NULL_SEL_CLR_BASE */ | 0 /* PrefetchCtlMsr */ ); + + /* + * Synthesize "LFENCE is serializing" into the AMD-defined entry in + * KVM's supported CPUID if the feature is reported as supported by the + * kernel. LFENCE_RDTSC was a Linux-defined synthetic feature long + * before AMD joined the bandwagon, e.g. LFENCE is serializing on most + * CPUs that support SSE2. On CPUs that don't support AMD's leaf, + * kvm_cpu_cap_mask() will unfortunately drop the flag due to ANDing + * the mask with the raw host CPUID, and reporting support in AMD's + * leaf can make it easier for userspace to detect the feature. + */ if (cpu_feature_enabled(X86_FEATURE_LFENCE_RDTSC)) - kvm_cpu_caps[CPUID_8000_0021_EAX] |= BIT(2) /* LFENCE Always serializing */; + kvm_cpu_cap_set(X86_FEATURE_LFENCE_RDTSC); if (!static_cpu_has_bug(X86_BUG_NULL_SEG)) kvm_cpu_caps[CPUID_8000_0021_EAX] |= BIT(6) /* NULL_SEL_CLR_BASE */; kvm_cpu_caps[CPUID_8000_0021_EAX] |= BIT(9) /* NO_SMM_CTL_MSR */;
diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index 4637fd7a84d6..e975822951b2 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -97,7 +97,7 @@ #define X86_FEATURE_SYSENTER32 ( 3*32+15) /* "" sysenter in IA32 userspace */ #define X86_FEATURE_REP_GOOD ( 3*32+16) /* REP microcode works well */ #define X86_FEATURE_AMD_LBR_V2 ( 3*32+17) /* AMD Last Branch Record Extension Version 2 */ -#define X86_FEATURE_LFENCE_RDTSC ( 3*32+18) /* "" LFENCE synchronizes RDTSC */ +/* FREE, was #define X86_FEATURE_LFENCE_RDTSC ( 3*32+18) "" LFENCE synchronizes RDTSC */ #define X86_FEATURE_ACC_POWER ( 3*32+19) /* AMD Accumulated Power Mechanism */ #define X86_FEATURE_NOPL ( 3*32+20) /* The NOPL (0F 1F) instructions */ #define X86_FEATURE_ALWAYS ( 3*32+21) /* "" Always-present feature */ @@ -432,6 +432,7 @@ /* AMD-defined Extended Feature 2 EAX, CPUID level 0x80000021 (EAX), word 20 */ #define X86_FEATURE_NO_NESTED_DATA_BP (20*32+ 0) /* "" No Nested Data Breakpoints */ +#define X86_FEATURE_LFENCE_RDTSC (20*32+ 2) /* "" LFENCE always serializing / synchronizes RDTSC */ /* * BUG word(s) diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c index f769d6d08b43..208c2ce8598a 100644 --- a/arch/x86/kernel/cpu/amd.c +++ b/arch/x86/kernel/cpu/amd.c @@ -956,7 +956,7 @@ static void init_amd(struct cpuinfo_x86 *c) init_amd_cacheinfo(c); - if (cpu_has(c, X86_FEATURE_XMM2)) { + if (!cpu_has(c, X86_FEATURE_LFENCE_RDTSC) && cpu_has(c, X86_FEATURE_XMM2)) { /* * Use LFENCE for execution serialization. On families which * don't have that MSR, LFENCE is already serializing. diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 9764499acce2..448b5de98b8f 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -742,12 +742,9 @@ void kvm_set_cpu_caps(void) F(SME_COHERENT)); kvm_cpu_cap_mask(CPUID_8000_0021_EAX, - F(NO_NESTED_DATA_BP) | - BIT(2) /* LFENCE Always serializing */ | 0 /* SmmPgCfgLock */ | + F(NO_NESTED_DATA_BP) | F(LFENCE_RDTSC) | 0 /* SmmPgCfgLock */ | BIT(6) /* NULL_SEL_CLR_BASE */ | 0 /* PrefetchCtlMsr */ ); - if (cpu_feature_enabled(X86_FEATURE_LFENCE_RDTSC)) - kvm_cpu_caps[CPUID_8000_0021_EAX] |= BIT(2) /* LFENCE Always serializing */; if (!static_cpu_has_bug(X86_BUG_NULL_SEG)) kvm_cpu_caps[CPUID_8000_0021_EAX] |= BIT(6) /* NULL_SEL_CLR_BASE */; kvm_cpu_caps[CPUID_8000_0021_EAX] |= BIT(9) /* NO_SMM_CTL_MSR */;