Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp792376imm; Mon, 21 May 2018 14:33:19 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpZQhU6WJRNvI7/HU+Y6paeSqHRo98oO/NVOt2AH6F4yPaeooPz4CN3/VJN/MpLonmx/9XP X-Received: by 2002:a63:b248:: with SMTP id t8-v6mr25940pgo.174.1526938398952; Mon, 21 May 2018 14:33:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526938398; cv=none; d=google.com; s=arc-20160816; b=UklQsOp5PZIp3P9gnoedVY/Dc+v0102aBgBMcL1eNaESeCCd6+9zo5LuhbgWsTBxNM K4UAKRkaPPHmFlE4eFC3TaQVEKZe809xrHU19w052KS2wRHqBd3yCuwRHFVYsiJsQ/HL oVgQdllQw2k4PYV4FPTGTnwDTxPaS8zci9x82nqQ8FbWTznAZvdXJ8E3r4wvtGA/bHaX uf7pUcMqNL/WYtqiNDXqvQTNTND3XsdjhMSXg6QdKE6BCb8B4VwY1qzjPyAp4n33lLDW LYhFgz5f9h/uoW6ARguN/nU2LFHwhLFVHHsVcQiOLx9JykcmxmRkZziEgc/+IhXBpE0N OEOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=yfrU2E67PkXzbgSI4WaSxOxsYVRtp63Z33as4skqHOI=; b=KkvzsQkRKwKtRgeVEgoRQFSHQidYh8aPavlcb3tcByNnVGsqUo1t9Q1BfBRku18TYm YPLOCftlVGVVPg5KBVDAXktj/++eZwxoGyxRB3EburGlvzC6kqiwc4PSQFrnW6xQrgmB JVJdI1T5LEzs7oTWWBQO83hvytk6AUzvmZe3jZ+L+hvPku+PVSylv8zpWX3GXdvEBn0O MYn2YW+xPxVswPPynd0/eHUnGvrHqAcO00hftVe/mm6VevTET28ubBAnRl8K7KblrsFN 7IsXlwF4iy/7sXrRmhg8sX3dq47FWt1fsGcFWthZBflqoAHLbosS1Kr9SE+L8mcjuieI ZeXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=sAMQEFFb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c13-v6si10566217pgq.616.2018.05.21.14.33.04; Mon, 21 May 2018 14:33:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=sAMQEFFb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932539AbeEUV0S (ORCPT + 99 others); Mon, 21 May 2018 17:26:18 -0400 Received: from mail.kernel.org ([198.145.29.99]:41022 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754444AbeEUV0M (ORCPT ); Mon, 21 May 2018 17:26:12 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A9B7820875; Mon, 21 May 2018 21:26:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1526937972; bh=Q0MUaYuTRqHOgincwgeypSbVW2L7qZPmh78zc3211Rw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sAMQEFFbFgLULMDyqEzhA2zEy3UM1xSM8s49UCFnqawNP56RM1DxIB/sUaFnpFyNy x9zUzGW7Ovm6h0zy2/PGv41IPCZcQqdMhW9nq+A9P9m037fq66z8Wo2UO1umz2ZWJJ F60fnHfV9FSBD7LA1XOoDvzqMLrg41ZbrcnLBDco= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Konrad Rzeszutek Wilk , Thomas Gleixner , Borislav Petkov , Ingo Molnar Subject: [PATCH 4.16 066/110] x86/bugs, KVM: Support the combination of guest and host IBRS Date: Mon, 21 May 2018 23:12:03 +0200 Message-Id: <20180521210512.061816675@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180521210503.823249477@linuxfoundation.org> References: <20180521210503.823249477@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Konrad Rzeszutek Wilk commit 5cf687548705412da47c9cec342fd952d71ed3d5 upstream A guest may modify the SPEC_CTRL MSR from the value used by the kernel. Since the kernel doesn't use IBRS, this means a value of zero is what is needed in the host. But the 336996-Speculative-Execution-Side-Channel-Mitigations.pdf refers to the other bits as reserved so the kernel should respect the boot time SPEC_CTRL value and use that. This allows to deal with future extensions to the SPEC_CTRL interface if any at all. Note: This uses wrmsrl() instead of native_wrmsl(). I does not make any difference as paravirt will over-write the callq *0xfff.. with the wrmsrl assembler code. Signed-off-by: Konrad Rzeszutek Wilk Signed-off-by: Thomas Gleixner Reviewed-by: Borislav Petkov Reviewed-by: Ingo Molnar Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/nospec-branch.h | 10 ++++++++++ arch/x86/kernel/cpu/bugs.c | 18 ++++++++++++++++++ arch/x86/kvm/svm.c | 6 ++---- arch/x86/kvm/vmx.c | 6 ++---- 4 files changed, 32 insertions(+), 8 deletions(-) --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -228,6 +228,16 @@ enum spectre_v2_mitigation { extern void x86_spec_ctrl_set(u64); extern u64 x86_spec_ctrl_get_default(void); +/* + * On VMENTER we must preserve whatever view of the SPEC_CTRL MSR + * the guest has, while on VMEXIT we restore the host view. This + * would be easier if SPEC_CTRL were architecturally maskable or + * shadowable for guests but this is not (currently) the case. + * Takes the guest view of SPEC_CTRL MSR as a parameter. + */ +extern void x86_spec_ctrl_set_guest(u64); +extern void x86_spec_ctrl_restore_host(u64); + extern char __indirect_thunk_start[]; extern char __indirect_thunk_end[]; --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -123,6 +123,24 @@ u64 x86_spec_ctrl_get_default(void) } EXPORT_SYMBOL_GPL(x86_spec_ctrl_get_default); +void x86_spec_ctrl_set_guest(u64 guest_spec_ctrl) +{ + if (!boot_cpu_has(X86_FEATURE_IBRS)) + return; + if (x86_spec_ctrl_base != guest_spec_ctrl) + wrmsrl(MSR_IA32_SPEC_CTRL, guest_spec_ctrl); +} +EXPORT_SYMBOL_GPL(x86_spec_ctrl_set_guest); + +void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl) +{ + if (!boot_cpu_has(X86_FEATURE_IBRS)) + return; + if (x86_spec_ctrl_base != guest_spec_ctrl) + wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); +} +EXPORT_SYMBOL_GPL(x86_spec_ctrl_restore_host); + #ifdef RETPOLINE static bool spectre_v2_bad_module; --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -5401,8 +5401,7 @@ static void svm_vcpu_run(struct kvm_vcpu * is no need to worry about the conditional branch over the wrmsr * being speculatively taken. */ - if (svm->spec_ctrl) - native_wrmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl); + x86_spec_ctrl_set_guest(svm->spec_ctrl); asm volatile ( "push %%" _ASM_BP "; \n\t" @@ -5514,8 +5513,7 @@ static void svm_vcpu_run(struct kvm_vcpu if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); - if (svm->spec_ctrl) - native_wrmsrl(MSR_IA32_SPEC_CTRL, 0); + x86_spec_ctrl_restore_host(svm->spec_ctrl); /* Eliminate branch target predictions from guest mode */ vmexit_fill_RSB(); --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -9468,8 +9468,7 @@ static void __noclone vmx_vcpu_run(struc * is no need to worry about the conditional branch over the wrmsr * being speculatively taken. */ - if (vmx->spec_ctrl) - native_wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl); + x86_spec_ctrl_set_guest(vmx->spec_ctrl); vmx->__launched = vmx->loaded_vmcs->launched; asm( @@ -9607,8 +9606,7 @@ static void __noclone vmx_vcpu_run(struc if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); - if (vmx->spec_ctrl) - native_wrmsrl(MSR_IA32_SPEC_CTRL, 0); + x86_spec_ctrl_restore_host(vmx->spec_ctrl); /* Eliminate branch target predictions from guest mode */ vmexit_fill_RSB();