From nobody Thu Apr 2 23:54:12 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3733228506A for ; Sat, 14 Feb 2026 01:27:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771032430; cv=none; b=HEwTcGIklg1rHkdt2SKmdWhaRHa/xPuZlVIawsaTP08kSoTLGO5gkliPVGhGPDoQA7xsqgM6kHkNhIrJtS968AB+o7Dpn+r/Vu8grwj2FVEOA3jEzoHxqKvmF43yOXOuHyEN7sdIPq0IkxH5wDpJ7wGjxWSocPv7qtOFzJx9VdY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771032430; c=relaxed/simple; bh=4EUvtuubXDsq2YIfpcwadRuQgjt5GtaI9Om4D5BZP9I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bgdpoFNpW6rSoxG5XjQWqOCb4/9NcJUItk6xTvfCbDUoPWwOeZMZXQOKHmAlPZjunUWFXFy7gPIbO+GyqHcfLcEIcjIlOoTCH92MRjyMVAtcF4FTqw3vguuJkjOeFAQQLyJapFqQRmwfuUeZ6a0DqJwrtK7daGIVJz0B9MWjy3o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=gKzzSoaK; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gKzzSoaK" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-353c9d644b0so1069067a91.2 for ; Fri, 13 Feb 2026 17:27:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771032427; x=1771637227; darn=vger.kernel.org; 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=W7MxKQiK0Ayh5SRrSHD6eYo36EclamwmJq0KYQ9dvh4=; b=gKzzSoaKu3CK5x+WIOQlXsAY004mj7S0TcoHF3W4e7t61Z1pK2G38ouo9Wdto0DfE7 ux04nTFlASGw3lmZDUngUaV8wc0gsIL5KqqpLTU9FU1FsP5RfqQo5NGPaLqjA2BhOaMk vvNrvjYdF0/A7Xl7qncHbdITQliJiQ/+1S5vg6Isys4Fj1Q2AKNHvlAYvT7oEna5PjsB DC1YsgCZr6gIvJ2CqMnLkK4KjvhhGuJYqjNiAtIlq6/W/CY3fGWr/hhBhKiHjSfYhWNf IL2FzFZQ24MjDlzMPuGItR4YrgZ5o45tv9BGlqYN2JIBiA2WUK5gdRIJRRIH58ZNUtg5 rceA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771032427; x=1771637227; 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=W7MxKQiK0Ayh5SRrSHD6eYo36EclamwmJq0KYQ9dvh4=; b=bOusZD/jjNM5GTf0RxHR/h24OtNAT6RTQU0KLXJAFMqmH5AE8WQj54DkufxnXeOCm2 cRXrGU27tqIWP7smKHffs2k1Tz520Pq3rR663lk2q8ymIM0YJp2yRfl+AWG8eOdn7tII zERAPhw6PKJzDi748mQtzTG90NfBsCyNMjUQ4QLXo0dxhVqJb9OeW5UWKpJtMOTxl1LJ rlpiSi9RqbsMiF6gk0QTsQmIhY4197NDCRp75dKbkm+0GDJ0+TH/3Xe4/QhKnahOsrDz 2vSWr62FoA+QH/G6VMkMvGOyqyj3K+11HRVeLszShbMMPSUixf0n97JoX510QlB+YRmV LyhQ== X-Gm-Message-State: AOJu0YxSo0yRlyFRiRQOowV67TQMuzM+uqzXbf3WzHXVuzS52pbZxevZ aC2d7gwa1uiPeHfQvppKgFr3eioD0HG87S9zuWpZoLB8NDKO2mRTk2O8EB4uonSbY12lQYIdj0C HVaVhXA== X-Received: from pjbfv20.prod.google.com ([2002:a17:90b:e94:b0:354:a52b:ba9d]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1c0c:b0:356:2eac:b649 with SMTP id 98e67ed59e1d1-358448265e8mr1145384a91.7.1771032427400; Fri, 13 Feb 2026 17:27:07 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 13 Feb 2026 17:26:47 -0800 In-Reply-To: <20260214012702.2368778-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260214012702.2368778-1-seanjc@google.com> X-Mailer: git-send-email 2.53.0.310.g728cabbaf7-goog Message-ID: <20260214012702.2368778-2-seanjc@google.com> Subject: [PATCH v3 01/16] KVM: x86: Move kvm_rebooting to x86 From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Kiryl Shutsemau , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Sean Christopherson , Paolo Bonzini Cc: linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, kvm@vger.kernel.org, linux-perf-users@vger.kernel.org, Chao Gao , Xu Yilun , Dan Williams Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move kvm_rebooting, which is only read by x86, to KVM x86 so that it can be moved again to core x86 code. Add a "shutdown" arch hook to facilate setting the flag in KVM x86, along with a pile of comments to provide more context around what KVM x86 is doing and why. Reviewed-by: Chao Gao Acked-by: Dave Hansen Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 22 ++++++++++++++++++++++ arch/x86/kvm/x86.h | 1 + include/linux/kvm_host.h | 8 +++++++- virt/kvm/kvm_main.c | 14 +++++++------- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index db3f393192d9..77edc24f8309 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -700,6 +700,9 @@ static void drop_user_return_notifiers(void) kvm_on_user_return(&msrs->urn); } =20 +__visible bool kvm_rebooting; +EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_rebooting); + /* * Handle a fault on a hardware virtualization (VMX or SVM) instruction. * @@ -13178,6 +13181,25 @@ int kvm_arch_enable_virtualization_cpu(void) return 0; } =20 +void kvm_arch_shutdown(void) +{ + /* + * Set kvm_rebooting to indicate that KVM has asynchronously disabled + * hardware virtualization, i.e. that errors and/or exceptions on SVM + * and VMX instructions are expected and should be ignored. + */ + kvm_rebooting =3D true; + + /* + * Ensure kvm_rebooting is visible before IPIs are sent to other CPUs + * to disable virtualization. Effectively pairs with the reception of + * the IPI (kvm_rebooting is read in task/exception context, but only + * _needs_ to be read as %true after the IPI function callback disables + * virtualization). + */ + smp_wmb(); +} + void kvm_arch_disable_virtualization_cpu(void) { kvm_x86_call(disable_virtualization_cpu)(); diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 94d4f07aaaa0..b314649e5c02 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -54,6 +54,7 @@ struct kvm_host_values { u64 arch_capabilities; }; =20 +extern bool kvm_rebooting; void kvm_spurious_fault(void); =20 #define SIZE_OF_MEMSLOTS_HASHTABLE \ diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 2c7d76262898..981b55c0a3a7 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1630,6 +1630,13 @@ static inline void kvm_create_vcpu_debugfs(struct kv= m_vcpu *vcpu) {} #endif =20 #ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING +/* + * kvm_arch_shutdown() is invoked immediately prior to forcefully disabling + * hardware virtualization on all CPUs via IPI function calls (in preparat= ion + * for shutdown or reboot), e.g. to allow arch code to prepare for disabli= ng + * virtualization while KVM may be actively running vCPUs. + */ +void kvm_arch_shutdown(void); /* * kvm_arch_{enable,disable}_virtualization() are called on one CPU, under * kvm_usage_lock, immediately after/before 0=3D>1 and 1=3D>0 transitions = of @@ -2305,7 +2312,6 @@ static inline bool kvm_check_request(int req, struct = kvm_vcpu *vcpu) =20 #ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING extern bool enable_virt_at_load; -extern bool kvm_rebooting; #endif =20 extern unsigned int halt_poll_ns; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 571cf0d6ec01..e081e7244299 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5593,13 +5593,15 @@ bool enable_virt_at_load =3D true; module_param(enable_virt_at_load, bool, 0444); EXPORT_SYMBOL_FOR_KVM_INTERNAL(enable_virt_at_load); =20 -__visible bool kvm_rebooting; -EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_rebooting); - static DEFINE_PER_CPU(bool, virtualization_enabled); static DEFINE_MUTEX(kvm_usage_lock); static int kvm_usage_count; =20 +__weak void kvm_arch_shutdown(void) +{ + +} + __weak void kvm_arch_enable_virtualization(void) { =20 @@ -5653,10 +5655,9 @@ static int kvm_offline_cpu(unsigned int cpu) =20 static void kvm_shutdown(void *data) { + kvm_arch_shutdown(); + /* - * Disable hardware virtualization and set kvm_rebooting to indicate - * that KVM has asynchronously disabled hardware virtualization, i.e. - * that relevant errors and exceptions aren't entirely unexpected. * Some flavors of hardware virtualization need to be disabled before * transferring control to firmware (to perform shutdown/reboot), e.g. * on x86, virtualization can block INIT interrupts, which are used by @@ -5665,7 +5666,6 @@ static void kvm_shutdown(void *data) * 100% comprehensive. */ pr_info("kvm: exiting hardware virtualization\n"); - kvm_rebooting =3D true; on_each_cpu(kvm_disable_virtualization_cpu, NULL, 1); } =20 --=20 2.53.0.310.g728cabbaf7-goog