From nobody Thu Sep 18 21:54:29 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 A36C5EB64DD for ; Fri, 21 Jul 2023 20:19:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231251AbjGUUTc (ORCPT ); Fri, 21 Jul 2023 16:19:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229601AbjGUUTT (ORCPT ); Fri, 21 Jul 2023 16:19:19 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 101EB30CB for ; Fri, 21 Jul 2023 13:19:11 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1b9de7951easo13877795ad.0 for ; Fri, 21 Jul 2023 13:19:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689970750; x=1690575550; 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=IIqhdp8WqL0kWTvtPZo4YKSDQAXM01F7uHc3z46B11M=; b=N6+HAnOlKDhLqVNY9edBHUTpkSa+XY7oBMtLhV0W+2bPtkJJTDUxW1hvkMsrnQHj98 oiQUl+6svRNCcwf0uFTQJMji4LvzrNr2XGACfpM6JfC8YBqlliASriW/eTzwiz4Cj+qw Q7/LFi0tCAvBmJlOSW1stt1jDJo1Q/5KJupzQaFRZ4qAZtYc4HevNfBNLG3CVgQQlkBK bL97aMTEWyf0Y5VvSMMlEKaugqItpB+/9EF1SUMbEX1j6K5nBib+wPVofkxxhooK+5V1 rq7l+I5yiOcLjTL43F7br+tt0K4eb8nGYIQxsVTwHV54sBhWTWHF8rKe2hB8x2y2ZwjI A0wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689970750; x=1690575550; 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=IIqhdp8WqL0kWTvtPZo4YKSDQAXM01F7uHc3z46B11M=; b=agctSLOvUP/67+A8uWmWhor/2LrzwObkf/q5QkFG/1zPQ22RrIlwkDYW798FVe3t9i MuiB5RT5uKJBqFsijszvmYM/HXBVOCPPaYZIc5CrWHWeJ22nO/oTqFqLA3L1Y1XbsvIS WuT0Q69LH6DPz2/hfbKd7N3j8dwSOQhxk02IHXmcjK/68YyZJMZd7cM4MROMYVmoMedl 9Zn0qMwjXzRtnI5gLg3gMBV7tuPsoDSldIV2tFKBuKoSvK+OCDnxk6DmHnnHkW6y07hX tiihyoef3Akoy7aVNJmg5E+xDt1NFmpmCFjoqApHIMPHFogYir2zcePL7vBGbA3/gTmp 9QAA== X-Gm-Message-State: ABy/qLZwVD8+7ibW8P0q3WcgQdUa2lqCZwy5FFZVD9YmFriHBwfo6HQg upf7wXiQ1ed0CTkRNkbw598Q+1/Mw3U= X-Google-Smtp-Source: APBJJlE/7vXHTCq9jJ6EV6q2DuyaSpyg4++NHPOMxgndH9mm+1zOSzr4NdqmL9LH5P57nPMWJEhYoKBpUWY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:2291:b0:1b8:866f:6fc1 with SMTP id b17-20020a170903229100b001b8866f6fc1mr11914plh.0.1689970750480; Fri, 21 Jul 2023 13:19:10 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 21 Jul 2023 13:18:43 -0700 In-Reply-To: <20230721201859.2307736-1-seanjc@google.com> Mime-Version: 1.0 References: <20230721201859.2307736-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Message-ID: <20230721201859.2307736-4-seanjc@google.com> Subject: [PATCH v4 03/19] 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: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Andrew Cooper , Kai Huang , Chao Gao 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 a hypervisor, i.e. KVM, isn't loaded/active, i.e. if the CPU can't possibly be post-VMXON. Signed-off-by: Sean Christopherson Reviewed-by: Kai Huang --- arch/x86/include/asm/virtext.h | 10 ---------- arch/x86/kernel/reboot.c | 29 +++++++++-------------------- arch/x86/kvm/vmx/vmx.c | 8 +++++--- 3 files changed, 14 insertions(+), 33 deletions(-) diff --git a/arch/x86/include/asm/virtext.h b/arch/x86/include/asm/virtext.h index 3b12e6b99412..5bc29fab15da 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 62ccedeb5e2b..d2d0f2672a64 100644 --- a/arch/x86/kernel/reboot.c +++ b/arch/x86/kernel/reboot.c @@ -787,13 +787,7 @@ void machine_crash_shutdown(struct pt_regs *regs) } #endif =20 -/* - * 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) } EXPORT_SYMBOL_GPL(cpu_emergency_unregister_virt_callback); =20 -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(); -} - /* This is the CPU performing the emergency shutdown work. */ int crashing_cpu =3D -1; =20 @@ -836,9 +819,15 @@ int crashing_cpu =3D -1; */ void cpu_emergency_disable_virtualization(void) { - cpu_crash_vmclear_loaded_vmcss(); + 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(); + + /* 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 019cefc65142..682c20b33a96 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -744,7 +744,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; @@ -752,6 +752,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) @@ -8590,7 +8592,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(); } @@ -8640,7 +8642,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.41.0.487.g6d72f3e995-goog