From nobody Fri Oct 3 21:07:26 2025 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (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 AAC8E2F0692 for ; Mon, 25 Aug 2025 20:06:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756152394; cv=none; b=eJHMpzBbVIgzdZY/7IG1Hq9Btkrqql2edVCXwLEdRa8QivCAIHHGO7F8z4FDvAph+/BM8lT1+Ozb0bwHeeMvL4GWH4B5RuvDp/92AAiyby3rqCx6nySfOJpOgPF/VNhwYnbO9N50UVc6EbziMSQT0UzPHkPU2l5J1/hOvYMMD6k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756152394; c=relaxed/simple; bh=AqZ16N7USWpldh2rbejgTNbQwTXg4Rackm1lGumECO0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gIMYXpVHuKlt3AgPjetEotgmsHRsJdZewG+OG/Uk4Z8pt036hEQJpwggpF7Cj8NbkYB4wki594q0B8uMIn0VbqJ04pcqvM2MVM8TaYbagomR7fASntqNEESGxuLMTQsA0hp7bYY/qO3k1lfLDDBiPk2k8alsonhBTt+LtmHFazM= 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=IjjvTrPW; arc=none smtp.client-ip=209.85.215.202 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="IjjvTrPW" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b471757d82fso3681131a12.3 for ; Mon, 25 Aug 2025 13:06:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1756152391; x=1756757191; 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=8Yt+mqKBC1lScZpff+gMSGHqLsfTkvQErLtkG0vCOeI=; b=IjjvTrPWJKFUsN0vKgTzYYKlIwrF7yeGJpqqDihdzQtLZXq2NJJ+sAK1NlPHxSIlle Kr9nXTkLcQ/o1UWRWzD3sll+wKF8BMoegHqNZMkeQnpCxpLVGw7W+rkF/gQYtChNCBDS QZm97uzX23OFBECJywjv2yejb2H6Yqfm+mJfj0vUtvNfCAXci06w9LlO809fFJRj3YFv KJ2KVNNReKXNiy5vrYf7RWC1/9UggkAGJ3OlSvADq6pGkJoPf4vVWpvJc7LMIkjkrh5o 0xO61mD8NxvQH+lbzRx1ww5I0Lqa5sQrgv4mYUWU0sKFzPg3i9wbHCJnto16waFVCfVk ZB/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756152391; x=1756757191; 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=8Yt+mqKBC1lScZpff+gMSGHqLsfTkvQErLtkG0vCOeI=; b=k02uc44XZorg/iCRuLo+UiHUL2wRDhepdUVZFREb58mzReVs+JKh+2BbqR3HmeQVTU sD/rMvwTT7rLQfopAwkmr8y4NskdDZR9h3G5Z/Yk2qp4dy/1SsHI56kcJeiURLtdBYhu /9MddmXySKUb0GxaBOQV16YgZtYvy6GVV9+5K0BJ8EvWjo9Rv0X9h2Nh0DkwuewPoxct Lgg2TyZpStQGm9VmxsHJzeDhKu7A5kP99y2OxFHt3Dx/v6Av3SOxQ3YmF/bavx5ZuH97 ooP/X+mfAF4I59tKmdOqZMZGQ2TTFHY0gYsej0hg47qacA6v/PoKvdi74tXiznzKR0I1 58aA== X-Gm-Message-State: AOJu0YyfTspL5aUnccfx308p8FCZH84t4VlrFiQjFM/T/cIzU4CCE89X 4iimptMiJJDoXcgXQsoXoJXbg/tx+ghX45vBN37H7Ysv1FmSQAaRCLf8bUNtoAFFuvWRgtOv/sC VLni08Q== X-Google-Smtp-Source: AGHT+IEhWmQ2UlZ+VF2FZhFLY+yV2efXko8WF7SGmUugSmFKrlZDlQSYCijahvsEl8VwXvH23VQtDtjdo6U= X-Received: from pjbsx15.prod.google.com ([2002:a17:90b:2ccf:b0:31f:1707:80f6]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e888:b0:23d:d0e7:754b with SMTP id d9443c01a7336-2462ee0853dmr157742075ad.18.1756152390785; Mon, 25 Aug 2025 13:06:30 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 25 Aug 2025 13:06:20 -0700 In-Reply-To: <20250825200622.3759571-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: <20250825200622.3759571-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.261.g7ce5a0a67e-goog Message-ID: <20250825200622.3759571-4-seanjc@google.com> Subject: [PATCH 3/5] entry/kvm: KVM: Move KVM details related to signal/-EINTR into KVM proper From: Sean Christopherson To: Marc Zyngier , Oliver Upton , Catalin Marinas , Will Deacon , Tianrui Zhao , Bibo Mao , Huacai Chen , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Peter Zijlstra , Andy Lutomirski , "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Uladzislau Rezki Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, loongarch@lists.linux.dev, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-hyperv@vger.kernel.org, rcu@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move KVM's morphing of pending signals into userspace exits into KVM proper, and drop the @vcpu param from xfer_to_guest_mode_handle_work(). How KVM responds to -EINTR is a detail that really belongs in KVM itself, and invoking kvm_handle_signal_exit() from kernel code creates an inverted module dependency. E.g. attempting to move kvm_handle_signal_exit() into kvm_main.c would generate an linker error when building kvm.ko as a module. Dropping KVM details will also converting the KVM "entry" code into a more generic virtualization framework so that it can be used when running as a Hyper-V root partition. Lastly, eliminating usage of "struct kvm_vcpu" outside of KVM is also nice to have for KVM x86 developers, as keeping the details of kvm_vcpu purely within KVM allows changing the layout of the structure without having to boot into a new kernel, e.g. allows rebuilding and reloading kvm.ko with a modified kvm_vcpu structure as part of debug/development. Signed-off-by: Sean Christopherson --- arch/arm64/kvm/arm.c | 3 +-- arch/loongarch/kvm/vcpu.c | 3 +-- arch/riscv/kvm/vcpu.c | 3 +-- arch/x86/kvm/vmx/vmx.c | 1 - arch/x86/kvm/x86.c | 3 +-- include/linux/entry-kvm.h | 11 +++-------- include/linux/kvm_host.h | 13 ++++++++++++- kernel/entry/kvm.c | 13 +++++-------- 8 files changed, 24 insertions(+), 26 deletions(-) diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 888f7c7abf54..418fd3043467 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -6,7 +6,6 @@ =20 #include #include -#include #include #include #include @@ -1177,7 +1176,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) /* * Check conditions before entering the guest */ - ret =3D xfer_to_guest_mode_handle_work(vcpu); + ret =3D kvm_xfer_to_guest_mode_handle_work(vcpu); if (!ret) ret =3D 1; =20 diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c index ce478151466c..450545d2fc70 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -4,7 +4,6 @@ */ =20 #include -#include #include #include #include @@ -251,7 +250,7 @@ static int kvm_enter_guest_check(struct kvm_vcpu *vcpu) /* * Check conditions before entering the guest */ - ret =3D xfer_to_guest_mode_handle_work(vcpu); + ret =3D kvm_xfer_to_guest_mode_handle_work(vcpu); if (ret < 0) return ret; =20 diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index f001e56403f9..251e787f2ebc 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -7,7 +7,6 @@ */ =20 #include -#include #include #include #include @@ -910,7 +909,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) run->exit_reason =3D KVM_EXIT_UNKNOWN; while (ret > 0) { /* Check conditions before entering the guest */ - ret =3D xfer_to_guest_mode_handle_work(vcpu); + ret =3D kvm_xfer_to_guest_mode_handle_work(vcpu); if (ret) continue; ret =3D 1; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index aa157fe5b7b3..d7c86613e50a 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -28,7 +28,6 @@ #include #include #include -#include =20 #include #include diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a1c49bc681c4..0b13b8bf69e5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -59,7 +59,6 @@ #include #include #include -#include #include #include =20 @@ -11241,7 +11240,7 @@ static int vcpu_run(struct kvm_vcpu *vcpu) =20 if (__xfer_to_guest_mode_work_pending()) { kvm_vcpu_srcu_read_unlock(vcpu); - r =3D xfer_to_guest_mode_handle_work(vcpu); + r =3D kvm_xfer_to_guest_mode_handle_work(vcpu); kvm_vcpu_srcu_read_lock(vcpu); if (r) return r; diff --git a/include/linux/entry-kvm.h b/include/linux/entry-kvm.h index 16149f6625e4..3644de7e6019 100644 --- a/include/linux/entry-kvm.h +++ b/include/linux/entry-kvm.h @@ -21,8 +21,6 @@ _TIF_NOTIFY_SIGNAL | _TIF_NOTIFY_RESUME | \ ARCH_XFER_TO_GUEST_MODE_WORK) =20 -struct kvm_vcpu; - /** * arch_xfer_to_guest_mode_handle_work - Architecture specific xfer to gue= st * mode work handling function. @@ -32,12 +30,10 @@ struct kvm_vcpu; * Invoked from xfer_to_guest_mode_handle_work(). Defaults to NOOP. Can be * replaced by architecture specific code. */ -static inline int arch_xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcp= u, - unsigned long ti_work); +static inline int arch_xfer_to_guest_mode_handle_work(unsigned long ti_wor= k); =20 #ifndef arch_xfer_to_guest_mode_work -static inline int arch_xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcp= u, - unsigned long ti_work) +static inline int arch_xfer_to_guest_mode_handle_work(unsigned long ti_wor= k) { return 0; } @@ -46,11 +42,10 @@ static inline int arch_xfer_to_guest_mode_handle_work(s= truct kvm_vcpu *vcpu, /** * xfer_to_guest_mode_handle_work - Check and handle pending work which ne= eds * to be handled before going to guest mode - * @vcpu: Pointer to current's VCPU data * * Returns: 0 or an error code */ -int xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu); +int xfer_to_guest_mode_handle_work(void); =20 /** * xfer_to_guest_mode_prepare - Perform last minute preparation work that diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 15656b7fba6c..598b9473e46d 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2,7 +2,7 @@ #ifndef __KVM_HOST_H #define __KVM_HOST_H =20 - +#include #include #include #include @@ -2450,6 +2450,17 @@ static inline void kvm_handle_signal_exit(struct kvm= _vcpu *vcpu) vcpu->run->exit_reason =3D KVM_EXIT_INTR; vcpu->stat.signal_exits++; } + +static inline int kvm_xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu) +{ + int r =3D xfer_to_guest_mode_handle_work(); + + if (r) { + WARN_ON_ONCE(r !=3D -EINTR); + kvm_handle_signal_exit(vcpu); + } + return r; +} #endif /* CONFIG_KVM_XFER_TO_GUEST_WORK */ =20 /* diff --git a/kernel/entry/kvm.c b/kernel/entry/kvm.c index 8485f63863af..6fc762eaacca 100644 --- a/kernel/entry/kvm.c +++ b/kernel/entry/kvm.c @@ -1,17 +1,14 @@ // SPDX-License-Identifier: GPL-2.0 =20 #include -#include =20 -static int xfer_to_guest_mode_work(struct kvm_vcpu *vcpu, unsigned long ti= _work) +static int xfer_to_guest_mode_work(unsigned long ti_work) { do { int ret; =20 - if (ti_work & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) { - kvm_handle_signal_exit(vcpu); + if (ti_work & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) return -EINTR; - } =20 if (ti_work & (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)) schedule(); @@ -19,7 +16,7 @@ static int xfer_to_guest_mode_work(struct kvm_vcpu *vcpu,= unsigned long ti_work) if (ti_work & _TIF_NOTIFY_RESUME) resume_user_mode_work(NULL); =20 - ret =3D arch_xfer_to_guest_mode_handle_work(vcpu, ti_work); + ret =3D arch_xfer_to_guest_mode_handle_work(ti_work); if (ret) return ret; =20 @@ -28,7 +25,7 @@ static int xfer_to_guest_mode_work(struct kvm_vcpu *vcpu,= unsigned long ti_work) return 0; } =20 -int xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu) +int xfer_to_guest_mode_handle_work(void) { unsigned long ti_work; =20 @@ -44,6 +41,6 @@ int xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu) if (!(ti_work & XFER_TO_GUEST_MODE_WORK)) return 0; =20 - return xfer_to_guest_mode_work(vcpu, ti_work); + return xfer_to_guest_mode_work(ti_work); } EXPORT_SYMBOL_GPL(xfer_to_guest_mode_handle_work); --=20 2.51.0.261.g7ce5a0a67e-goog