From patchwork Thu Dec 1 23:26:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 28595 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp540717wrr; Thu, 1 Dec 2022 15:29:06 -0800 (PST) X-Google-Smtp-Source: AA0mqf5yHyxTbvjyb2Ccb3jb7AVq1FS4KckQuTaU483qV5iRgPGUf6Icm2FE1HJrvqQBCZfTv7Rp X-Received: by 2002:a17:906:381:b0:78c:b8b0:9d35 with SMTP id b1-20020a170906038100b0078cb8b09d35mr50392717eja.586.1669937346000; Thu, 01 Dec 2022 15:29:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669937345; cv=none; d=google.com; s=arc-20160816; b=F/t4Mn3HGtaxF6AxPasv27decZ5ZeexJ/lhFC20/4nXA2al3HzqjbyV+Q/kr6niZkW H1JSQIR2ff2ccMAHHbfdy1LbBU+2af2ilMYRZY4G3MPyNRgoHSScVsCKF5plcaXE05Z4 E4IY2Lf6SFfPdNwkXE1MHPvNNlRMfCxhgkcsjpaH8jawpCYg2s4gyKr6fj72W6fsM4Pc yodwTv2SRjLWCpIAf/XWcOxXS9Ky9umGEwY5pksW5ioYbUsyxR7Iysw4GufXvCTUTKqV 1B53d8WO1QNvwi84kuJaKzzowJthyct7ef5jW+yhjjG2hD8QxR4tGCE3m9tp3eeROd3J M92g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=N1MKEpLC40vm6+kyhw5xs+CtLNykGWLPO1eTQOZjc3U=; b=07/Xc0a6mAuTh4QVCPWFIj9L0QkKsf5HumPwnIENRdfnySioZrpmZRB0v+RHgGQ78/ 1ONeRXu252NbY6gu+leDhHULX6ZubYp16FymKaHUM5sD65c61CotH8410o3qDLcFTQbV CsNE4j7tIe/aOLbTAwi/IrsxY9QYnTAzuXDEe5Vv6SPed5QYqo7K9VJwyWVk/lHTM4X4 uUJZY+c9iffKi1WU1dYmqiOyUSvkuIScLbTgCBMLx+51d5IMRoxYaJPcTrEs3JwPfgTP Jcfw80ZpDVlctyzHIa7bFIcaMxl4pUFXn/wf8ojxjReK4XxvWHNV31Yix7ebpZ6yYjFZ Am2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="GfJDQ/9p"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u1-20020a50eac1000000b0046b55a18bc4si4627223edp.436.2022.12.01.15.28.42; Thu, 01 Dec 2022 15:29:05 -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=@google.com header.s=20210112 header.b="GfJDQ/9p"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231913AbiLAX1r (ORCPT + 99 others); Thu, 1 Dec 2022 18:27:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231863AbiLAX1U (ORCPT ); Thu, 1 Dec 2022 18:27:20 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D836DCC661 for ; Thu, 1 Dec 2022 15:27:12 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id on5-20020a17090b1d0500b0021821a07953so7567251pjb.4 for ; Thu, 01 Dec 2022 15:27:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=N1MKEpLC40vm6+kyhw5xs+CtLNykGWLPO1eTQOZjc3U=; b=GfJDQ/9peKOon7QfuMScKrcJcuXlJoBRDCKCmFSBO1trRWmCwApGQXz1DxOCbQtu/3 Bml2/OASO5LhkfH24P9S8CMso0T48H5ERZZCMnquEISh/5N3YCdSWvIN9vogM77nu7do 1JiFutI9smzRoiX2Xmgn8/FR9FqytQcFoa/ftWaCHX1gXByWXi2MzGFXM5RyGSruj+T4 zpgHhHN+Xr11wpdmxgPwOHkiHSthxQgIuQpf7KAJ1ILeB4m9S2+cGoXynZ0m0yw8Ugbz GVODLTY87cfYie73+3wRWKa1xc0gqkv/xEeLPQBM1DXCuvlAN9k1+NWZLJ456sdiHbXw Uh1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=N1MKEpLC40vm6+kyhw5xs+CtLNykGWLPO1eTQOZjc3U=; b=cl/lYF6kaqcbuKCm26XOCmlY6Bf9KlioSnEDMJtFrqtlhQZAAubQUfISKJ/ScMNJDv WebxHFBFXqrdqd+jf8qv+2vUzayc4rI4LmWf21r0raXUfmpIH0Q7BYG8P/jN2oUWO+Z7 EErE8hSSRfQwxA7sI+vbnmDvXWN5A6MIIJqLJffBdCpkGny9uOSE48dqkwWsu6CIGp6b SzEaLQpIM36X/sIOJ8k3uAJb0RpMWWmRfcPyBh0x2ILsPLXRLyAXZAyvQ9TmAqgJyRCr KB1hwZD9+rNX6mjYcByw3KB7yzv3W18jfbsk9oh5qp9rY9MteZqz2yP5Hl9l4dE/gT/v CYaw== X-Gm-Message-State: ANoB5pl7cp8h9zZtlFEWZu3Tb9Bfb8GBGn0At37esXBraXyCPHcKyY0v XoOU1zFAB2KWqWCDIzNiR6Y/LzG4MOg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:a601:b0:189:8001:b54f with SMTP id u1-20020a170902a60100b001898001b54fmr25838795plq.37.1669937232408; Thu, 01 Dec 2022 15:27:12 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 1 Dec 2022 23:26:45 +0000 In-Reply-To: <20221201232655.290720-1-seanjc@google.com> Mime-Version: 1.0 References: <20221201232655.290720-1-seanjc@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221201232655.290720-7-seanjc@google.com> Subject: [PATCH 06/16] x86/reboot: KVM: Disable SVM during reboot via virt/KVM reboot callback From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Sean Christopherson , Paolo Bonzini Cc: "H. Peter Anvin" , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Andrew Cooper X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751056222033913479?= X-GMAIL-MSGID: =?utf-8?q?1751056222033913479?= Use the virt callback to disable SVM (and set GIF=1) during an emergency instead of blindly attempting to disable SVM. Like the VMX case, if KVM (or an out-of-tree hypervisor) isn't loaded/active, SVM can't be in use. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/reboot.h | 2 +- arch/x86/include/asm/virtext.h | 8 -------- arch/x86/kernel/reboot.c | 6 ++---- arch/x86/kvm/svm/svm.c | 19 +++++++++++++++++-- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/arch/x86/include/asm/reboot.h b/arch/x86/include/asm/reboot.h index 1d098a7d329a..dc2b77e6704b 100644 --- a/arch/x86/include/asm/reboot.h +++ b/arch/x86/include/asm/reboot.h @@ -25,7 +25,7 @@ void __noreturn machine_real_restart(unsigned int type); #define MRR_BIOS 0 #define MRR_APM 1 -#if IS_ENABLED(CONFIG_KVM_INTEL) +#if IS_ENABLED(CONFIG_KVM_INTEL) || IS_ENABLED(CONFIG_KVM_AMD) typedef void (cpu_emergency_virt_cb)(void); void cpu_emergency_register_virt_callback(cpu_emergency_virt_cb *callback); void cpu_emergency_unregister_virt_callback(cpu_emergency_virt_cb *callback); diff --git a/arch/x86/include/asm/virtext.h b/arch/x86/include/asm/virtext.h index 4f61283b1f52..97349410c937 100644 --- a/arch/x86/include/asm/virtext.h +++ b/arch/x86/include/asm/virtext.h @@ -130,12 +130,4 @@ static inline void cpu_svm_disable(void) } } -/** Makes sure SVM is disabled, if it is supported on the CPU - */ -static inline void cpu_emergency_svm_disable(void) -{ - if (cpu_has_svm(NULL)) - cpu_svm_disable(); -} - #endif /* _ASM_X86_VIRTEX_H */ diff --git a/arch/x86/kernel/reboot.c b/arch/x86/kernel/reboot.c index 85ea1fdebf4c..3eec3cdbc054 100644 --- a/arch/x86/kernel/reboot.c +++ b/arch/x86/kernel/reboot.c @@ -787,7 +787,7 @@ void machine_crash_shutdown(struct pt_regs *regs) } #endif -#if IS_ENABLED(CONFIG_KVM_INTEL) +#if IS_ENABLED(CONFIG_KVM_INTEL) || IS_ENABLED(CONFIG_KVM_AMD) /* RCU-protected callback to disable virtualization prior to reboot. */ static cpu_emergency_virt_cb __rcu *cpu_emergency_virt_callback; @@ -823,7 +823,7 @@ void cpu_emergency_disable_virtualization(void) { lockdep_assert_irqs_disabled(); -#if IS_ENABLED(CONFIG_KVM_INTEL) +#if IS_ENABLED(CONFIG_KVM_INTEL) || IS_ENABLED(CONFIG_KVM_AMD) cpu_emergency_virt_cb *callback; rcu_read_lock(); @@ -832,8 +832,6 @@ void cpu_emergency_disable_virtualization(void) callback(); rcu_read_unlock(); #endif - /* KVM_AMD doesn't yet utilize the common callback. */ - cpu_emergency_svm_disable(); } #if defined(CONFIG_SMP) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 46b658d0f46e..d56d5fe42262 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -565,6 +566,11 @@ void __svm_write_tsc_multiplier(u64 multiplier) preempt_enable(); } +static void svm_emergency_disable(void) +{ + cpu_svm_disable(); +} + static void svm_hardware_disable(void) { /* Make sure we clean up behind us */ @@ -5084,6 +5090,13 @@ static struct kvm_x86_init_ops svm_init_ops __initdata = { .pmu_ops = &amd_pmu_ops, }; +static void __svm_exit(void) +{ + kvm_x86_vendor_exit(); + + cpu_emergency_unregister_virt_callback(svm_emergency_disable); +} + static int __init svm_init(void) { int r; @@ -5097,6 +5110,8 @@ static int __init svm_init(void) if (r) return r; + cpu_emergency_register_virt_callback(svm_emergency_disable); + /* * Common KVM initialization _must_ come last, after this, /dev/kvm is * exposed to userspace! @@ -5109,14 +5124,14 @@ static int __init svm_init(void) return 0; err_kvm_init: - kvm_x86_vendor_exit(); + __svm_exit(); return r; } static void __exit svm_exit(void) { kvm_exit(); - kvm_x86_vendor_exit(); + __svm_exit(); } module_init(svm_init)