From nobody Thu Sep 18 21:40:33 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 CB532C4332F for ; Thu, 1 Dec 2022 23:27:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231882AbiLAX1b (ORCPT ); Thu, 1 Dec 2022 18:27:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231708AbiLAX1Q (ORCPT ); Thu, 1 Dec 2022 18:27:16 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74012CA7AF for ; Thu, 1 Dec 2022 15:27:11 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-349423f04dbso31721177b3.13 for ; Thu, 01 Dec 2022 15:27:11 -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=oOjLsBhtQc4Zwk7j8jnMy1RaD3WNvOTCrnpAR6Y5Wh4=; b=iD7DAvdmg/6zF71OGW1W55vNfYRUgmUrzSQ3x+UEEnpmklMmMgQUfTEP1QKNKMSlRX TxjAJfp7C02va3ptgeSSWV94pjJBk1+6p5N1AvvEj2k2S9IHil3K+fjpWRQDEypcl0j/ j8ocO0aDVQ+QsjPt4v69TDSd3UXkBlYv9XKDk9zA79T74/aCGjrFuLNH5IIHtiZbw8kp DRWUreIUMwKrhLY3Lmbubt7OnAPJZZ0DizPcMzAQADuXs8vWwGmX9o7ltMQKioHYfJMD FYstpPF/MfDvZBN6JBFlSsWFdfMXXtd1YSwGg5tndJytuH6jeVnlrEFk42zns1yga60d blLw== 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=oOjLsBhtQc4Zwk7j8jnMy1RaD3WNvOTCrnpAR6Y5Wh4=; b=CezxbrfaN87sQXdfPrqyg8byoCmaZXiSxqQRcwm5fowzmaiNmJ8q0XBjcu2rwT9ZaW +uzeG2yx6jxh5k/vcG3vLk3mrLb1WMyGYQFos43q/sVSt7+GQDX4lX2/l38AinqeP/f4 YG/h9wsFWm94mXekvpwO5MQXUlIjyidVThsNxa+WZ7iRLnexqLzX8tpb7Tg2xJYr1tC+ 5Rpf6mslFijf43TtggCcFu8fPeA2UMsBb1WE4qKVPYN6EXr18J05JosMr63AffQAzbFp /XoGbD4EKze+VOR3cdF9QygdP+LqYDMJlgAcQFNLXivkZPfwLXMIVZvrn2ghwvms0+Kf dIQg== X-Gm-Message-State: ANoB5pnrNRToyPNQRB2kAoB2oS/GVgUfwiHoIUlfrP+U0LirUJpl44CZ 6sMklm22c/6NNr3XHVwMm7UR3vPBclQ= X-Google-Smtp-Source: AA0mqf6e3fI8ZKhR812Q6WWT615sHyFSm1ucZmUC1BB457teQBEMBCmdWiFc6wIBLpE6L5mS6tfUF2fQ+xw= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:9e84:0:b0:6dc:a18d:9ee5 with SMTP id p4-20020a259e84000000b006dca18d9ee5mr48977176ybq.88.1669937230726; Thu, 01 Dec 2022 15:27:10 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 1 Dec 2022 23:26:44 +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-6-seanjc@google.com> Subject: [PATCH 05/16] x86/reboot: KVM: Handle VMXOFF in KVM's 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 KVM VMX's reboot/crash callback to do VMXOFF in an emergency instead of manually and blindly doing VMXOFF. There's no need to attempt VMXOFF if KVM (or some other out-of-tree hypervisor) isn't loaded/active, i.e. if the CPU can't possibly be post-VMXON. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/virtext.h | 10 ---------- arch/x86/kernel/reboot.c | 30 +++++++++--------------------- arch/x86/kvm/vmx/vmx.c | 8 +++++--- 3 files changed, 14 insertions(+), 34 deletions(-) diff --git a/arch/x86/include/asm/virtext.h b/arch/x86/include/asm/virtext.h index 0acb14806a74..4f61283b1f52 100644 --- a/arch/x86/include/asm/virtext.h +++ b/arch/x86/include/asm/virtext.h @@ -70,16 +70,6 @@ static inline void __cpu_emergency_vmxoff(void) cpu_vmxoff(); } =20 -/** Disable VMX if it is supported and enabled on the current CPU - */ -static inline void cpu_emergency_vmxoff(void) -{ - if (cpu_has_vmx()) - __cpu_emergency_vmxoff(); -} - - - =20 /* * SVM functions: diff --git a/arch/x86/kernel/reboot.c b/arch/x86/kernel/reboot.c index 40ea16ecb3b8..85ea1fdebf4c 100644 --- a/arch/x86/kernel/reboot.c +++ b/arch/x86/kernel/reboot.c @@ -788,13 +788,7 @@ void machine_crash_shutdown(struct pt_regs *regs) #endif =20 #if IS_ENABLED(CONFIG_KVM_INTEL) -/* - * This is used to VMCLEAR all VMCSs loaded on the - * processor. And when loading kvm_intel module, the - * callback function pointer will be assigned. - * - * protected by rcu. - */ +/* RCU-protected callback to disable virtualization prior to reboot. */ static cpu_emergency_virt_cb __rcu *cpu_emergency_virt_callback; =20 void cpu_emergency_register_virt_callback(cpu_emergency_virt_cb *callback) @@ -815,17 +809,6 @@ void cpu_emergency_unregister_virt_callback(cpu_emerge= ncy_virt_cb *callback) synchronize_rcu(); } EXPORT_SYMBOL_GPL(cpu_emergency_unregister_virt_callback); - -static inline void cpu_crash_vmclear_loaded_vmcss(void) -{ - cpu_emergency_virt_cb *callback; - - rcu_read_lock(); - callback =3D rcu_dereference(cpu_emergency_virt_callback); - if (callback) - callback(); - rcu_read_unlock(); -} #endif =20 /* This is the CPU performing the emergency shutdown work. */ @@ -841,10 +824,15 @@ void cpu_emergency_disable_virtualization(void) lockdep_assert_irqs_disabled(); =20 #if IS_ENABLED(CONFIG_KVM_INTEL) - cpu_crash_vmclear_loaded_vmcss(); -#endif + cpu_emergency_virt_cb *callback; =20 - cpu_emergency_vmxoff(); + rcu_read_lock(); + callback =3D rcu_dereference(cpu_emergency_virt_callback); + if (callback) + callback(); + rcu_read_unlock(); +#endif + /* KVM_AMD doesn't yet utilize the common callback. */ cpu_emergency_svm_disable(); } =20 diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 6e0cc4d03884..6f3ade75a670 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -743,7 +743,7 @@ static int vmx_set_guest_uret_msr(struct vcpu_vmx *vmx, return ret; } =20 -static void crash_vmclear_local_loaded_vmcss(void) +static void vmx_emergency_disable(void) { int cpu =3D raw_smp_processor_id(); struct loaded_vmcs *v; @@ -751,6 +751,8 @@ static void crash_vmclear_local_loaded_vmcss(void) list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), loaded_vmcss_on_cpu_link) vmcs_clear(v->vmcs); + + __cpu_emergency_vmxoff(); } =20 static void __loaded_vmcs_clear(void *arg) @@ -8526,7 +8528,7 @@ static void __vmx_exit(void) { allow_smaller_maxphyaddr =3D false; =20 - cpu_emergency_unregister_virt_callback(crash_vmclear_local_loaded_vmcss); + cpu_emergency_unregister_virt_callback(vmx_emergency_disable); =20 vmx_cleanup_l1d_flush(); } @@ -8576,7 +8578,7 @@ static int __init vmx_init(void) pi_init_cpu(cpu); } =20 - cpu_emergency_register_virt_callback(crash_vmclear_local_loaded_vmcss); + cpu_emergency_register_virt_callback(vmx_emergency_disable); =20 vmx_check_vmcs12_offsets(); =20 --=20 2.39.0.rc0.267.gcb52ba06e7-goog