From nobody Thu Sep 18 21:39:47 2025 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 96FABC4321E for ; Thu, 1 Dec 2022 23:27:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231886AbiLAX1t (ORCPT ); Thu, 1 Dec 2022 18:27:49 -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-Google-Smtp-Source: AA0mqf728o14x2qPtbsV/svyt/B0IMOGeXVA6OC8OQvU7e9OrEVnZ5ZliFnbfztRETZFFS+A8vGyIpBzzc0= 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use the virt callback to disable SVM (and set GIF=3D1) 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 =20 -#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 *callbac= k); 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) } } =20 -/** 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 =20 -#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; =20 @@ -823,7 +823,7 @@ void cpu_emergency_disable_virtualization(void) { lockdep_assert_irqs_disabled(); =20 -#if IS_ENABLED(CONFIG_KVM_INTEL) +#if IS_ENABLED(CONFIG_KVM_INTEL) || IS_ENABLED(CONFIG_KVM_AMD) cpu_emergency_virt_cb *callback; =20 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(); } =20 #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 =20 #include @@ -565,6 +566,11 @@ void __svm_write_tsc_multiplier(u64 multiplier) preempt_enable(); } =20 +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 __initda= ta =3D { .pmu_ops =3D &amd_pmu_ops, }; =20 +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; =20 + 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; =20 err_kvm_init: - kvm_x86_vendor_exit(); + __svm_exit(); return r; } =20 static void __exit svm_exit(void) { kvm_exit(); - kvm_x86_vendor_exit(); + __svm_exit(); } =20 module_init(svm_init) --=20 2.39.0.rc0.267.gcb52ba06e7-goog