Skip to content

Commit 6d0386e

Browse files
sean-jcliuw
authored andcommitted
entry/kvm: KVM: Move KVM details related to signal/-EINTR into KVM proper
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 <seanjc@google.com> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Wei Liu <wei.liu@kernel.org>
1 parent 0ebac01 commit 6d0386e

8 files changed

Lines changed: 24 additions & 26 deletions

File tree

arch/arm64/kvm/arm.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include <linux/bug.h>
88
#include <linux/cpu_pm.h>
9-
#include <linux/entry-kvm.h>
109
#include <linux/errno.h>
1110
#include <linux/err.h>
1211
#include <linux/kvm_host.h>
@@ -1177,7 +1176,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
11771176
/*
11781177
* Check conditions before entering the guest
11791178
*/
1180-
ret = xfer_to_guest_mode_handle_work(vcpu);
1179+
ret = kvm_xfer_to_guest_mode_handle_work(vcpu);
11811180
if (!ret)
11821181
ret = 1;
11831182

arch/loongarch/kvm/vcpu.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55

66
#include <linux/kvm_host.h>
7-
#include <linux/entry-kvm.h>
87
#include <asm/fpu.h>
98
#include <asm/lbt.h>
109
#include <asm/loongarch.h>
@@ -251,7 +250,7 @@ static int kvm_enter_guest_check(struct kvm_vcpu *vcpu)
251250
/*
252251
* Check conditions before entering the guest
253252
*/
254-
ret = xfer_to_guest_mode_handle_work(vcpu);
253+
ret = kvm_xfer_to_guest_mode_handle_work(vcpu);
255254
if (ret < 0)
256255
return ret;
257256

arch/riscv/kvm/vcpu.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
#include <linux/bitops.h>
10-
#include <linux/entry-kvm.h>
1110
#include <linux/errno.h>
1211
#include <linux/err.h>
1312
#include <linux/kdebug.h>
@@ -910,7 +909,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
910909
run->exit_reason = KVM_EXIT_UNKNOWN;
911910
while (ret > 0) {
912911
/* Check conditions before entering the guest */
913-
ret = xfer_to_guest_mode_handle_work(vcpu);
912+
ret = kvm_xfer_to_guest_mode_handle_work(vcpu);
914913
if (ret)
915914
continue;
916915
ret = 1;

arch/x86/kvm/vmx/vmx.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include <linux/slab.h>
2929
#include <linux/tboot.h>
3030
#include <linux/trace_events.h>
31-
#include <linux/entry-kvm.h>
3231

3332
#include <asm/apic.h>
3433
#include <asm/asm.h>

arch/x86/kvm/x86.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
#include <linux/sched/stat.h>
6060
#include <linux/sched/isolation.h>
6161
#include <linux/mem_encrypt.h>
62-
#include <linux/entry-kvm.h>
6362
#include <linux/suspend.h>
6463
#include <linux/smp.h>
6564

@@ -11241,7 +11240,7 @@ static int vcpu_run(struct kvm_vcpu *vcpu)
1124111240

1124211241
if (__xfer_to_guest_mode_work_pending()) {
1124311242
kvm_vcpu_srcu_read_unlock(vcpu);
11244-
r = xfer_to_guest_mode_handle_work(vcpu);
11243+
r = kvm_xfer_to_guest_mode_handle_work(vcpu);
1124511244
kvm_vcpu_srcu_read_lock(vcpu);
1124611245
if (r)
1124711246
return r;

include/linux/entry-kvm.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
_TIF_NOTIFY_SIGNAL | _TIF_NOTIFY_RESUME | \
2222
ARCH_XFER_TO_GUEST_MODE_WORK)
2323

24-
struct kvm_vcpu;
25-
2624
/**
2725
* arch_xfer_to_guest_mode_handle_work - Architecture specific xfer to guest
2826
* mode work handling function.
@@ -32,12 +30,10 @@ struct kvm_vcpu;
3230
* Invoked from xfer_to_guest_mode_handle_work(). Defaults to NOOP. Can be
3331
* replaced by architecture specific code.
3432
*/
35-
static inline int arch_xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu,
36-
unsigned long ti_work);
33+
static inline int arch_xfer_to_guest_mode_handle_work(unsigned long ti_work);
3734

3835
#ifndef arch_xfer_to_guest_mode_work
39-
static inline int arch_xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu,
40-
unsigned long ti_work)
36+
static inline int arch_xfer_to_guest_mode_handle_work(unsigned long ti_work)
4137
{
4238
return 0;
4339
}
@@ -46,11 +42,10 @@ static inline int arch_xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu,
4642
/**
4743
* xfer_to_guest_mode_handle_work - Check and handle pending work which needs
4844
* to be handled before going to guest mode
49-
* @vcpu: Pointer to current's VCPU data
5045
*
5146
* Returns: 0 or an error code
5247
*/
53-
int xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu);
48+
int xfer_to_guest_mode_handle_work(void);
5449

5550
/**
5651
* xfer_to_guest_mode_prepare - Perform last minute preparation work that

include/linux/kvm_host.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifndef __KVM_HOST_H
33
#define __KVM_HOST_H
44

5-
5+
#include <linux/entry-kvm.h>
66
#include <linux/types.h>
77
#include <linux/hardirq.h>
88
#include <linux/list.h>
@@ -2450,6 +2450,17 @@ static inline void kvm_handle_signal_exit(struct kvm_vcpu *vcpu)
24502450
vcpu->run->exit_reason = KVM_EXIT_INTR;
24512451
vcpu->stat.signal_exits++;
24522452
}
2453+
2454+
static inline int kvm_xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu)
2455+
{
2456+
int r = xfer_to_guest_mode_handle_work();
2457+
2458+
if (r) {
2459+
WARN_ON_ONCE(r != -EINTR);
2460+
kvm_handle_signal_exit(vcpu);
2461+
}
2462+
return r;
2463+
}
24532464
#endif /* CONFIG_KVM_XFER_TO_GUEST_WORK */
24542465

24552466
/*

kernel/entry/kvm.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
// SPDX-License-Identifier: GPL-2.0
22

33
#include <linux/entry-kvm.h>
4-
#include <linux/kvm_host.h>
54

6-
static int xfer_to_guest_mode_work(struct kvm_vcpu *vcpu, unsigned long ti_work)
5+
static int xfer_to_guest_mode_work(unsigned long ti_work)
76
{
87
do {
98
int ret;
109

11-
if (ti_work & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) {
12-
kvm_handle_signal_exit(vcpu);
10+
if (ti_work & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
1311
return -EINTR;
14-
}
1512

1613
if (ti_work & (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY))
1714
schedule();
1815

1916
if (ti_work & _TIF_NOTIFY_RESUME)
2017
resume_user_mode_work(NULL);
2118

22-
ret = arch_xfer_to_guest_mode_handle_work(vcpu, ti_work);
19+
ret = arch_xfer_to_guest_mode_handle_work(ti_work);
2320
if (ret)
2421
return ret;
2522

@@ -28,7 +25,7 @@ static int xfer_to_guest_mode_work(struct kvm_vcpu *vcpu, unsigned long ti_work)
2825
return 0;
2926
}
3027

31-
int xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu)
28+
int xfer_to_guest_mode_handle_work(void)
3229
{
3330
unsigned long ti_work;
3431

@@ -44,6 +41,6 @@ int xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu)
4441
if (!(ti_work & XFER_TO_GUEST_MODE_WORK))
4542
return 0;
4643

47-
return xfer_to_guest_mode_work(vcpu, ti_work);
44+
return xfer_to_guest_mode_work(ti_work);
4845
}
4946
EXPORT_SYMBOL_GPL(xfer_to_guest_mode_handle_work);

0 commit comments

Comments
 (0)