From patchwork Mon Sep 16 16:22:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Kuznetsov X-Patchwork-Id: 11147569 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 659ED14DB for ; Mon, 16 Sep 2019 16:23:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4E3A22189D for ; Mon, 16 Sep 2019 16:23:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732592AbfIPQXV (ORCPT ); Mon, 16 Sep 2019 12:23:21 -0400 Received: from mx1.redhat.com ([209.132.183.28]:46072 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729110AbfIPQXG (ORCPT ); Mon, 16 Sep 2019 12:23:06 -0400 Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B77888BE70 for ; Mon, 16 Sep 2019 16:23:05 +0000 (UTC) Received: by mail-wr1-f71.google.com with SMTP id h6so122059wrh.6 for ; Mon, 16 Sep 2019 09:23:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DnhcrFhYT0NJ78dPeEqKXN/V1tHyuglzQzoOeMUR4QY=; b=TfexS36Y/FkJYPWbRsXxs6vn5x6y3qXz0CzeZLFZhoo9o/ZrthlyH8XFDe80bazCae qrv0lCxAC8BhRYpVzWZxyuJ1ivArqAxL1iZAQcymyOi9ALKrfqtL5tHSh5fuOphhckB8 sqcthbL/DmaTzpFqvtWXT0MZxPjE36g+rQodbTAmP1bUo/OrO37Gw9kv36leiBOhqzMV e3L0Rm0D6HLFN6JpFvNVFZ5f11g9ODUZBs+jj2FHJTpG158R4raa6AoNoFQW7fGOfRL5 +9CODt+U1MclHmq7sANPQ7R4SdEXW/cs7FRsZunopUKpFiDtlS4wkVaeUKqHglw98ESH goTg== X-Gm-Message-State: APjAAAU3njjRKBG9XKHFcCTwAokPC6AM5QwyvIoekqvCczCngTCXrPya BcLiFMKbrYf2geRRWAWxUFD7G/htrgl/7HC9GGO6o3QFht5g1kdbrXyxkXO7prYT15OBx+t0H/p fKmZv1oV9oATZ X-Received: by 2002:a1c:2144:: with SMTP id h65mr32645wmh.114.1568650984107; Mon, 16 Sep 2019 09:23:04 -0700 (PDT) X-Google-Smtp-Source: APXvYqwqBQYTcgo16PkRqlPSOV/k7mPpgorRRib7zPPDxcWoF61Nz7u2WkI+Bq4Zjx3zql/YwldjxQ== X-Received: by 2002:a1c:2144:: with SMTP id h65mr32612wmh.114.1568650983843; Mon, 16 Sep 2019 09:23:03 -0700 (PDT) Received: from vitty.brq.redhat.com (nat-pool-brq-t.redhat.com. [213.175.37.10]) by smtp.gmail.com with ESMTPSA id q10sm78370575wrd.39.2019.09.16.09.23.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2019 09:23:01 -0700 (PDT) From: Vitaly Kuznetsov To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org, x86@kernel.org, Paolo Bonzini , =?utf-8?b?UmFkaW0gS3I=?= =?utf-8?b?xI1tw6HFmQ==?= , Sean Christopherson , Jim Mattson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , "Peter Zijlstra (Intel)" , Michael Kelley , Roman Kagan Subject: [PATCH 1/3] cpu/SMT: create and export cpu_smt_possible() Date: Mon, 16 Sep 2019 18:22:56 +0200 Message-Id: <20190916162258.6528-2-vkuznets@redhat.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190916162258.6528-1-vkuznets@redhat.com> References: <20190916162258.6528-1-vkuznets@redhat.com> MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org KVM needs to know if SMT is theoretically possible, this means it is supported and not forcefully disabled ('nosmt=force'). Create and export cpu_smt_possible() answering this question. Signed-off-by: Vitaly Kuznetsov --- include/linux/cpu.h | 2 ++ kernel/cpu.c | 11 +++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/include/linux/cpu.h b/include/linux/cpu.h index fcb1386bb0d4..6d48fc456d58 100644 --- a/include/linux/cpu.h +++ b/include/linux/cpu.h @@ -201,12 +201,14 @@ enum cpuhp_smt_control { extern enum cpuhp_smt_control cpu_smt_control; extern void cpu_smt_disable(bool force); extern void cpu_smt_check_topology(void); +extern bool cpu_smt_possible(void); extern int cpuhp_smt_enable(void); extern int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval); #else # define cpu_smt_control (CPU_SMT_NOT_IMPLEMENTED) static inline void cpu_smt_disable(bool force) { } static inline void cpu_smt_check_topology(void) { } +static inline bool cpu_smt_possible(void) { return false; } static inline int cpuhp_smt_enable(void) { return 0; } static inline int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval) { return 0; } #endif diff --git a/kernel/cpu.c b/kernel/cpu.c index e84c0873559e..2f8c2631e641 100644 --- a/kernel/cpu.c +++ b/kernel/cpu.c @@ -389,8 +389,7 @@ enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED; void __init cpu_smt_disable(bool force) { - if (cpu_smt_control == CPU_SMT_FORCE_DISABLED || - cpu_smt_control == CPU_SMT_NOT_SUPPORTED) + if (!cpu_smt_possible()) return; if (force) { @@ -435,6 +434,14 @@ static inline bool cpu_smt_allowed(unsigned int cpu) */ return !per_cpu(cpuhp_state, cpu).booted_once; } + +/* Returns true if SMT is not supported of forcefully (irreversibly) disabled */ +bool cpu_smt_possible(void) +{ + return cpu_smt_control != CPU_SMT_FORCE_DISABLED && + cpu_smt_control != CPU_SMT_NOT_SUPPORTED; +} +EXPORT_SYMBOL_GPL(cpu_smt_possible); #else static inline bool cpu_smt_allowed(unsigned int cpu) { return true; } #endif From patchwork Mon Sep 16 16:22:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Kuznetsov X-Patchwork-Id: 11147565 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 453071745 for ; Mon, 16 Sep 2019 16:23:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 23F1321907 for ; Mon, 16 Sep 2019 16:23:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732149AbfIPQXJ (ORCPT ); Mon, 16 Sep 2019 12:23:09 -0400 Received: from mx1.redhat.com ([209.132.183.28]:19984 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730792AbfIPQXI (ORCPT ); Mon, 16 Sep 2019 12:23:08 -0400 Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9CA8D8D0F8 for ; Mon, 16 Sep 2019 16:23:07 +0000 (UTC) Received: by mail-wr1-f69.google.com with SMTP id l6so128146wrn.2 for ; Mon, 16 Sep 2019 09:23:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hTif/hdyQ2dgFmG83XOhs3M15HFTZ4c2i4bvJhs3seA=; b=TYu/idX64DGLE/YmRGkt9yUVJV2hOlotHDxDSYhJ91jXbdEwQz8FWD4gzwwm9rhbai HCCU3jPM2xRGeX+Y7nvXaWFKZCm4H61Iy4JVkvik+tndyJtOt1w4OlAOAg9LAHtCaeqs 7jm5dpcyL6towjJAsB/+PTPDKNGQ4pU1UWiDC4DBeGmdH6DarAMLPPFpLstYvBFMUvaA +Lu/Ut6zok8mcTgXk5ZThte1eeQh9h0lIvFn7RJ5StVRCEGodqA5vIixHaNZYEFpL2gE 4YUqS6LwDUkp+4hwqZj97vu1j0cg4sq2mrREGNOIoFq0EfWdlwT0vMLkVkBX9aN+J6Hl 6s/w== X-Gm-Message-State: APjAAAWjFVCR4mY4bHnRwHHwXuJjQgxU2t2nrq4drdMUnaDtj0OT6TAu b++5JoSouq6255wGpz9QD8x4RTb1bTcrbl6MzUVEcWNUHhtlWESiXwz07qhBb+KbnJPvJsMDQxz he3Db5irup9Fu X-Received: by 2002:a7b:c3c6:: with SMTP id t6mr20520wmj.5.1568650985771; Mon, 16 Sep 2019 09:23:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqwJ2BsBwbNvJMLRpYPdToi80JcADK8zG0+oHLr+0eXOrKGkWIoV4bSie2tXYamBJOS5FHSDNw== X-Received: by 2002:a7b:c3c6:: with SMTP id t6mr20494wmj.5.1568650985442; Mon, 16 Sep 2019 09:23:05 -0700 (PDT) Received: from vitty.brq.redhat.com (nat-pool-brq-t.redhat.com. [213.175.37.10]) by smtp.gmail.com with ESMTPSA id q10sm78370575wrd.39.2019.09.16.09.23.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2019 09:23:04 -0700 (PDT) From: Vitaly Kuznetsov To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org, x86@kernel.org, Paolo Bonzini , =?utf-8?b?UmFkaW0gS3I=?= =?utf-8?b?xI1tw6HFmQ==?= , Sean Christopherson , Jim Mattson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , "Peter Zijlstra (Intel)" , Michael Kelley , Roman Kagan Subject: [PATCH 2/3] KVM: x86: hyper-v: set NoNonArchitecturalCoreSharing CPUID bit when SMT is impossible Date: Mon, 16 Sep 2019 18:22:57 +0200 Message-Id: <20190916162258.6528-3-vkuznets@redhat.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190916162258.6528-1-vkuznets@redhat.com> References: <20190916162258.6528-1-vkuznets@redhat.com> MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Hyper-V 2019 doesn't expose MD_CLEAR CPUID bit to guests when it cannot guarantee that two virtual processors won't end up running on sibling SMT threads without knowing about it. This is done as an optimization as in this case there is nothing the guest can do to protect itself against MDS and issuing additional flush requests is just pointless. On bare metal the topology is known, however, when Hyper-V is running nested (e.g. on top of KVM) it needs an additional piece of information: a confirmation that the exposed topology (wrt vCPU placement on different SMT threads) is trustworthy. NoNonArchitecturalCoreSharing (CPUID 0x40000004 EAX bit 18) is described in TLFS as follows: "Indicates that a virtual processor will never share a physical core with another virtual processor, except for virtual processors that are reported as sibling SMT threads." From KVM we can give such guarantee in two cases: - SMT is unsupported or forcefully disabled (just 'disabled' doesn't work as it can become re-enabled during the lifetime of the guest). - vCPUs are properly pinned so the scheduler won't put them on sibling SMT threads (when they're not reported as such). This patch reports NoNonArchitecturalCoreSharing bit in to userspace in the first case. The second case is outside of KVM's domain of responsibility (as vCPU pinning is actually done by someone who manages KVM's userspace - e.g. libvirt pinning QEMU threads). Signed-off-by: Vitaly Kuznetsov --- arch/x86/include/asm/hyperv-tlfs.h | 7 +++++++ arch/x86/kvm/hyperv.c | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/hyperv-tlfs.h b/arch/x86/include/asm/hyperv-tlfs.h index af78cd72b8f3..989a1efe7f5e 100644 --- a/arch/x86/include/asm/hyperv-tlfs.h +++ b/arch/x86/include/asm/hyperv-tlfs.h @@ -170,6 +170,13 @@ /* Recommend using enlightened VMCS */ #define HV_X64_ENLIGHTENED_VMCS_RECOMMENDED BIT(14) +/* + * Virtual processor will never share a physical core with another virtual + * processor, except for virtual processors that are reported as sibling SMT + * threads. + */ +#define HV_X64_NO_NONARCH_CORESHARING BIT(18) + /* Nested features. These are HYPERV_CPUID_NESTED_FEATURES.EAX bits. */ #define HV_X64_NESTED_GUEST_MAPPING_FLUSH BIT(18) #define HV_X64_NESTED_MSR_BITMAP BIT(19) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index fff790a3f4ee..9c187d16a9cd 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -23,6 +23,7 @@ #include "ioapic.h" #include "hyperv.h" +#include #include #include #include @@ -1864,7 +1865,8 @@ int kvm_vcpu_ioctl_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, ent->eax |= HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED; if (evmcs_ver) ent->eax |= HV_X64_ENLIGHTENED_VMCS_RECOMMENDED; - + if (!cpu_smt_possible()) + ent->eax |= HV_X64_NO_NONARCH_CORESHARING; /* * Default number of spinlock retry attempts, matches * HyperV 2016. From patchwork Mon Sep 16 16:22:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Kuznetsov X-Patchwork-Id: 11147567 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BBBD514DB for ; Mon, 16 Sep 2019 16:23:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A4ABE218AE for ; Mon, 16 Sep 2019 16:23:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732576AbfIPQXQ (ORCPT ); Mon, 16 Sep 2019 12:23:16 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39994 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731470AbfIPQXJ (ORCPT ); Mon, 16 Sep 2019 12:23:09 -0400 Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E23D8C050E0B for ; Mon, 16 Sep 2019 16:23:08 +0000 (UTC) Received: by mail-wr1-f70.google.com with SMTP id c1so115929wrb.12 for ; Mon, 16 Sep 2019 09:23:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=n34lZLdG/ksf/JKaFfjzz8cm8EDZ8nIYDrb9i3krQTc=; b=NM00STxT4olnRSziQeRaGSexc3iSovh+TC+843IINge4qSF0/XNbDWPqR0YDmJ+xAo w2FGtfsdw8kYJP7iic9B0quqWtOJ6QBrmOEMmXZocUPaSOZ7CMoBHJxU6/r8cwmQW/sG hiQSOkPA10I76GCs7rfhBB4qPSqBEy3wl2zUvKNKrAuVv0MO9UHD5eJkCMUOUezN/AzH rPIiTtkJsMXXrj6taZBxNVHxtLq21BE+mSbWq+3PyJ4RmuTb/lnSIpHf0743Gb08elX7 /n6GxH2Qupf5stYrrZ0NMqQll39k3nlN0YRVGYxFqYYdi0HlBfxoU3ZVt6NkFj9Kxhjf NAOg== X-Gm-Message-State: APjAAAUAqkrlwnoPWsw9n1OHgLk9hgodWRk6TBx+FUpBA1iFOBbPX6uv B7vKYfsHb892ZlGdPRYkJh/A11wBFw6HoU2YIZbjzEHHFzyY3uxj3UtlpgGFEUIZd5tbi6/IkMw wT48Zc62eLzER X-Received: by 2002:a7b:cd96:: with SMTP id y22mr263559wmj.169.1568650987153; Mon, 16 Sep 2019 09:23:07 -0700 (PDT) X-Google-Smtp-Source: APXvYqzOEzFZvaXPQ3JnKoR56GcxQnkFWhDS8m8D8bHNbgjvynKxDkSwMh32zieTRke9CCVfdOzRPw== X-Received: by 2002:a7b:cd96:: with SMTP id y22mr263537wmj.169.1568650986942; Mon, 16 Sep 2019 09:23:06 -0700 (PDT) Received: from vitty.brq.redhat.com (nat-pool-brq-t.redhat.com. [213.175.37.10]) by smtp.gmail.com with ESMTPSA id q10sm78370575wrd.39.2019.09.16.09.23.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2019 09:23:06 -0700 (PDT) From: Vitaly Kuznetsov To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org, x86@kernel.org, Paolo Bonzini , =?utf-8?b?UmFkaW0gS3I=?= =?utf-8?b?xI1tw6HFmQ==?= , Sean Christopherson , Jim Mattson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , "Peter Zijlstra (Intel)" , Michael Kelley , Roman Kagan Subject: [PATCH 3/3] KVM: selftests: hyperv_cpuid: add check for NoNonArchitecturalCoreSharing bit Date: Mon, 16 Sep 2019 18:22:58 +0200 Message-Id: <20190916162258.6528-4-vkuznets@redhat.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190916162258.6528-1-vkuznets@redhat.com> References: <20190916162258.6528-1-vkuznets@redhat.com> MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The bit is supposed to be '1' when SMT is not supported or forcefully disabled and '0' otherwise. Signed-off-by: Vitaly Kuznetsov --- .../selftests/kvm/x86_64/hyperv_cpuid.c | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c b/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c index ee59831fbc98..443a2b54645b 100644 --- a/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +++ b/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c @@ -26,6 +26,25 @@ static void guest_code(void) { } +static int smt_possible(void) +{ + char buf[16]; + FILE *f; + bool res = 1; + + f = fopen("/sys/devices/system/cpu/smt/control", "r"); + if (f) { + if (fread(buf, sizeof(*buf), sizeof(buf), f) > 0) { + if (!strncmp(buf, "forceoff", 8) || + !strncmp(buf, "notsupported", 12)) + res = 0; + } + fclose(f); + } + + return res; +} + static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries, int evmcs_enabled) { @@ -59,6 +78,14 @@ static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries, TEST_ASSERT(!entry->padding[0] && !entry->padding[1] && !entry->padding[2], "padding should be zero"); + if (entry->function == 0x40000004) { + int nononarchcs = !!(entry->eax & (1UL << 18)); + + TEST_ASSERT(nononarchcs == !smt_possible(), + "NoNonArchitecturalCoreSharing bit" + " doesn't reflect SMT setting"); + } + /* * If needed for debug: * fprintf(stdout,