From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E979CC433F5 for ; Fri, 11 Mar 2022 09:23:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347480AbiCKJYh (ORCPT ); Fri, 11 Mar 2022 04:24:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347461AbiCKJYb (ORCPT ); Fri, 11 Mar 2022 04:24:31 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 73A954ECD7 for ; Fri, 11 Mar 2022 01:23:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646990606; 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=KZy6BPxycAJaka0W+AV2zCpor0oUVfNi2iR5X6RpaRg=; b=HIncIemFBG00nLhJ0ef100LdmjOtV8OLEBMFYeAR+KzMOCSWvyKpfSzTglrafWPGXaIlip +GHojIsVbLwCYJiX/r6wo80E93fOziSAVDT0Fs4Aovt84lmXrXNUD//Z0qfDDtIabVsOiW S3+K0yBz+mqpnKwcU5xX3YYUkiZFpwA= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-41-ZLbfXVgtPjuvnL5ILpHANQ-1; Fri, 11 Mar 2022 04:23:25 -0500 X-MC-Unique: ZLbfXVgtPjuvnL5ILpHANQ-1 Received: by mail-ej1-f70.google.com with SMTP id lf15-20020a170906ae4f00b006da86a43346so4612428ejb.14 for ; Fri, 11 Mar 2022 01:23:24 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=KZy6BPxycAJaka0W+AV2zCpor0oUVfNi2iR5X6RpaRg=; b=ArPZn0Je2JvNzSBG3stCMpONMphjO8pQw3urvJckTLHKHG8PffZH9oLocHe12IpdRW 1ragOFUV6/zekx0AHcepkiKEYYVPb/fygZSPxy0ON5Cn91U1rAEqev6cYzkYAkMbYc2p mjybRsulmtINlN4vGu4Snf4pZbedvNsi4pV9tXxgd26c5wM7oxKnsNcOPSWBRl7HshQD UdgSv4iMEiqmrU41Y/M6Gw46w9QMNdkJcWiHGSjFsDu2+B4RKMffGNquWJn0HFP2PqlN N4p6E4F7z9WDlzEpLcmnNOCQ0OoYYLbR7s5XL4hgRfnDpkv2ead2Gk60gv/GNIJZZunV TiSw== X-Gm-Message-State: AOAM531EAPSI+ReRpnXUUUn77yt5ZObhc1fvKSi1ffJPEeISxmkPcl6e dWy44HZWmRXKei5CnEWT0cgxJMKI1bJE6c7By40QoSUe2U/vGLszbvG12zNeqNVIpMMp8FSyzuZ dhYb2aVMAA1ms3GM0sH9iHQaW X-Received: by 2002:a17:906:7304:b0:6da:9243:865 with SMTP id di4-20020a170906730400b006da92430865mr7722527ejc.665.1646990603909; Fri, 11 Mar 2022 01:23:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJyg6ktZq/p2wn7aGR4nCdUvXHDS4TXu9ovOWhhookzQBwQNB90SyHFawfKKEPoRuAamdnRlKQ== X-Received: by 2002:a17:906:7304:b0:6da:9243:865 with SMTP id di4-20020a170906730400b006da92430865mr7722513ejc.665.1646990603635; Fri, 11 Mar 2022 01:23:23 -0800 (PST) Received: from fedora (nat-2.ign.cz. [91.219.240.2]) by smtp.gmail.com with ESMTPSA id q5-20020aa7cc05000000b004129baa5a94sm2989712edt.64.2022.03.11.01.23.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Mar 2022 01:23:23 -0800 (PST) From: Vitaly Kuznetsov To: Paolo Bonzini Cc: jmattson@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: Re: [PATCH 2/2] KVM: x86: synthesize CPUID leaf 0x80000021h if useful In-Reply-To: <20220309170928.1032664-3-pbonzini@redhat.com> References: <20220309170928.1032664-1-pbonzini@redhat.com> <20220309170928.1032664-3-pbonzini@redhat.com> Date: Fri, 11 Mar 2022 10:23:22 +0100 Message-ID: <8735joalmd.fsf@redhat.com> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Paolo Bonzini writes: > Guests should have X86_BUG_NULL_SEG if and only if the host has the bug. > Use the info from static_cpu_has_bug to form the 0x80000021 CPUID leaf > that was defined for Zen3. Userspace can then set the bit even on older > CPUs that do not have the bug, such as Zen2. > > Do the same for X86_FEATURE_LFENCE_RDTSC as well, since various processors > have had very different ways of detecting it and not all of them are > available to userspace. > > Signed-off-by: Paolo Bonzini > --- > arch/x86/kvm/cpuid.c | 25 +++++++++++++++++++++++++ > 1 file changed, 25 insertions(+) > > diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c > index 30832aad402f..58b0b4e0263c 100644 > --- a/arch/x86/kvm/cpuid.c > +++ b/arch/x86/kvm/cpuid.c > @@ -723,6 +723,19 @@ static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array, > /* Hypervisor leaves are always synthesized by __do_cpuid_func. */ > return entry; > > + case 0x80000000: > + /* > + * 0x80000021 is sometimes synthesized by __do_cpuid_func, which > + * would result in out-of-bounds calls to do_host_cpuid. > + */ > + { > + static int max_cpuid_80000000; > + if (!READ_ONCE(max_cpuid_80000000)) > + WRITE_ONCE(max_cpuid_80000000, cpuid_eax(0x80000000)); > + if (function > READ_ONCE(max_cpuid_80000000)) Out of pure curiosity: what READ_ONCE/WRITE_ONCEs are for here? > + return entry; > + } > + This hunk seems to have a small side effect beyond its description: previously, KVM_CPUID_FLAG_SIGNIFCANT_INDEX was always returned for 0x8000001d leaf, even when it wasn't present on the host. With the change, we will return 'entry' directly from here, with no flag set. This is likely insignificant in the absence of the leaf. > default: > break; > } > @@ -1069,6 +1082,14 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) > break; > case 0x80000000: > entry->eax = min(entry->eax, 0x80000021); > + /* > + * Serializing LFENCE is reported in a multitude of ways, > + * and NullSegClearsBase is not reported in CPUID on Zen2; > + * help userspace by providing the CPUID leaf ourselves. > + */ > + if (static_cpu_has(X86_FEATURE_LFENCE_RDTSC) > + || !static_cpu_has_bug(X86_BUG_NULL_SEG)) > + entry->eax = max(entry->eax, 0x80000021); > break; > case 0x80000001: > cpuid_entry_override(entry, CPUID_8000_0001_EDX); > @@ -1155,6 +1176,10 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) > * EAX 13 PCMSR, Prefetch control MSR > */ > entry->eax &= BIT(0) | BIT(2) | BIT(6); > + if (static_cpu_has(X86_FEATURE_LFENCE_RDTSC)) > + entry->eax |= BIT(2); > + if (!static_cpu_has_bug(X86_BUG_NULL_SEG)) > + entry->eax |= BIT(6); > break; > /*Add support for Centaur's CPUID instruction*/ > case 0xC0000000: -- Vitaly