History

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.

Sean Christopherson committed 1 year ago

Commit 6d0386e1 parent 0ebac018 files · +24 −26Browse files

Unified · Split

8 files
  1. arch/arm64/kvm/arm.c +1 −2
  2. arch/loongarch/kvm/vcpu.c +1 −2
  3. arch/riscv/kvm/vcpu.c +1 −2
  4. arch/x86/kvm/vmx/vmx.c +0 −1
  5. arch/x86/kvm/x86.c +1 −2
  6. include/linux/entry-kvm.h +3 −8
  7. include/linux/kvm_host.h +12 −1
  8. kernel/entry/kvm.c +5 −8
arch/arm64/kvm/arm.c+1−2
@@ -6,7 +6,6 @@ · 5 unmodified lines
6 6
7#include <linux/bug.h>7#include <linux/bug.h>
8#include <linux/cpu_pm.h>8#include <linux/cpu_pm.h>
9#include <linux/entry-kvm.h>
10#include <linux/errno.h>9#include <linux/errno.h>
11#include <linux/err.h>10#include <linux/err.h>
12#include <linux/kvm_host.h>11#include <linux/kvm_host.h>
@@ -1177,7 +1176,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) · 1164 unmodified lines
1177 /*1176 /*
1178 * Check conditions before entering the guest1177 * Check conditions before entering the guest
1179 */1178 */
1180 ret = xfer_to_guest_mode_handle_work(vcpu);1179 ret = kvm_xfer_to_guest_mode_handle_work(vcpu);
1181 if (!ret)1180 if (!ret)
1182 ret = 1;1181 ret = 1;
1183 1182
arch/loongarch/kvm/vcpu.c+1−2
@@ -4,7 +4,6 @@ · 3 unmodified lines
4 */4 */
5 5
6#include <linux/kvm_host.h>6#include <linux/kvm_host.h>
7#include <linux/entry-kvm.h>
8#include <asm/fpu.h>7#include <asm/fpu.h>
9#include <asm/lbt.h>8#include <asm/lbt.h>
10#include <asm/loongarch.h>9#include <asm/loongarch.h>
@@ -251,7 +250,7 @@ static int kvm_enter_guest_check(struct kvm_vcpu *vcpu) · 240 unmodified lines
251 /*250 /*
252 * Check conditions before entering the guest251 * Check conditions before entering the guest
253 */252 */
254 ret = xfer_to_guest_mode_handle_work(vcpu);253 ret = kvm_xfer_to_guest_mode_handle_work(vcpu);
255 if (ret < 0)254 if (ret < 0)
256 return ret;255 return ret;
257 256
arch/riscv/kvm/vcpu.c+1−2
@@ -7,7 +7,6 @@ · 6 unmodified lines
7 */7 */
8 8
9#include <linux/bitops.h>9#include <linux/bitops.h>
10#include <linux/entry-kvm.h>
11#include <linux/errno.h>10#include <linux/errno.h>
12#include <linux/err.h>11#include <linux/err.h>
13#include <linux/kdebug.h>12#include <linux/kdebug.h>
@@ -910,7 +909,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) · 896 unmodified lines
910 run->exit_reason = KVM_EXIT_UNKNOWN;909 run->exit_reason = KVM_EXIT_UNKNOWN;
911 while (ret > 0) {910 while (ret > 0) {
912 /* Check conditions before entering the guest */911 /* 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);
914 if (ret)913 if (ret)
915 continue;914 continue;
916 ret = 1;915 ret = 1;
arch/x86/kvm/vmx/vmx.c+0−1
@@ -28,7 +28,6 @@ · 27 unmodified lines
28#include <linux/slab.h>28#include <linux/slab.h>
29#include <linux/tboot.h>29#include <linux/tboot.h>
30#include <linux/trace_events.h>30#include <linux/trace_events.h>
31#include <linux/entry-kvm.h>
32 31
33#include <asm/apic.h>32#include <asm/apic.h>
34#include <asm/asm.h>33#include <asm/asm.h>
arch/x86/kvm/x86.c+1−2
@@ -59,7 +59,6 @@ · 58 unmodified lines
59#include <linux/sched/stat.h>59#include <linux/sched/stat.h>
60#include <linux/sched/isolation.h>60#include <linux/sched/isolation.h>
61#include <linux/mem_encrypt.h>61#include <linux/mem_encrypt.h>
62#include <linux/entry-kvm.h>
63#include <linux/suspend.h>62#include <linux/suspend.h>
64#include <linux/smp.h>63#include <linux/smp.h>
65 64
@@ -11241,7 +11240,7 @@ static int vcpu_run(struct kvm_vcpu *vcpu) · 11175 unmodified lines
11241 11240
11242 if (__xfer_to_guest_mode_work_pending()) {11241 if (__xfer_to_guest_mode_work_pending()) {
11243 kvm_vcpu_srcu_read_unlock(vcpu);11242 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);
11245 kvm_vcpu_srcu_read_lock(vcpu);11244 kvm_vcpu_srcu_read_lock(vcpu);
11246 if (r)11245 if (r)
11247 return r;11246 return r;
include/linux/entry-kvm.h+3−8
@@ -21,8 +21,6 @@ · 20 unmodified lines
21 _TIF_NOTIFY_SIGNAL | _TIF_NOTIFY_RESUME | \21 _TIF_NOTIFY_SIGNAL | _TIF_NOTIFY_RESUME | \
22 ARCH_XFER_TO_GUEST_MODE_WORK)22 ARCH_XFER_TO_GUEST_MODE_WORK)
23 23
24struct kvm_vcpu;
25
26/**24/**
27 * arch_xfer_to_guest_mode_handle_work - Architecture specific xfer to guest25 * arch_xfer_to_guest_mode_handle_work - Architecture specific xfer to guest
28 * mode work handling function.26 * mode work handling function.
@@ -32,12 +30,10 @@ struct kvm_vcpu; · 3 unmodified lines
32 * Invoked from xfer_to_guest_mode_handle_work(). Defaults to NOOP. Can be30 * Invoked from xfer_to_guest_mode_handle_work(). Defaults to NOOP. Can be
33 * replaced by architecture specific code.31 * replaced by architecture specific code.
34 */32 */
35static inline int arch_xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu,33static inline int arch_xfer_to_guest_mode_handle_work(unsigned long ti_work);
36 unsigned long ti_work);
37 34
38#ifndef arch_xfer_to_guest_mode_work35#ifndef arch_xfer_to_guest_mode_work
39static inline int arch_xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu,36static inline int arch_xfer_to_guest_mode_handle_work(unsigned long ti_work)
40 unsigned long ti_work)
41{37{
42 return 0;38 return 0;
43}39}
@@ -46,11 +42,10 @@ static inline int arch_xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu, · 2 unmodified lines
46/**42/**
47 * xfer_to_guest_mode_handle_work - Check and handle pending work which needs43 * xfer_to_guest_mode_handle_work - Check and handle pending work which needs
48 * to be handled before going to guest mode44 * to be handled before going to guest mode
49 * @vcpu: Pointer to current's VCPU data
50 *45 *
51 * Returns: 0 or an error code46 * Returns: 0 or an error code
52 */47 */
53int xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu);48int xfer_to_guest_mode_handle_work(void);
54 49
55/**50/**
56 * xfer_to_guest_mode_prepare - Perform last minute preparation work that51 * xfer_to_guest_mode_prepare - Perform last minute preparation work that
include/linux/kvm_host.h+12−1
@@ -2,7 +2,7 @@ · 1 unmodified line
2#ifndef __KVM_HOST_H2#ifndef __KVM_HOST_H
3#define __KVM_HOST_H3#define __KVM_HOST_H
4 4
5 5#include <linux/entry-kvm.h>
6#include <linux/types.h>6#include <linux/types.h>
7#include <linux/hardirq.h>7#include <linux/hardirq.h>
8#include <linux/list.h>8#include <linux/list.h>
@@ -2450,6 +2450,17 @@ static inline void kvm_handle_signal_exit(struct kvm_vcpu *vcpu) · 2441 unmodified lines
2450 vcpu->run->exit_reason = KVM_EXIT_INTR;2450 vcpu->run->exit_reason = KVM_EXIT_INTR;
2451 vcpu->stat.signal_exits++;2451 vcpu->stat.signal_exits++;
2452}2452}
2453
2454static 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}
2453#endif /* CONFIG_KVM_XFER_TO_GUEST_WORK */2464#endif /* CONFIG_KVM_XFER_TO_GUEST_WORK */
2454 2465
2455/*2466/*
kernel/entry/kvm.c+5−8
@@ -1,17 +1,14 @@
1// SPDX-License-Identifier: GPL-2.01// SPDX-License-Identifier: GPL-2.0
2 2
3#include <linux/entry-kvm.h>3#include <linux/entry-kvm.h>
4#include <linux/kvm_host.h>
5 4
6static int xfer_to_guest_mode_work(struct kvm_vcpu *vcpu, unsigned long ti_work)5static int xfer_to_guest_mode_work(unsigned long ti_work)
7{6{
8 do {7 do {
9 int ret;8 int ret;
10 9
11 if (ti_work & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) {10 if (ti_work & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
12 kvm_handle_signal_exit(vcpu);
13 return -EINTR;11 return -EINTR;
14 }
15 12
16 if (ti_work & (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY))13 if (ti_work & (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY))
17 schedule();14 schedule();
@@ -19,7 +16,7 @@ static int xfer_to_guest_mode_work(struct kvm_vcpu *vcpu, unsigned long ti_work) · 1 unmodified line
19 if (ti_work & _TIF_NOTIFY_RESUME)16 if (ti_work & _TIF_NOTIFY_RESUME)
20 resume_user_mode_work(NULL);17 resume_user_mode_work(NULL);
21 18
22 ret = arch_xfer_to_guest_mode_handle_work(vcpu, ti_work);19 ret = arch_xfer_to_guest_mode_handle_work(ti_work);
23 if (ret)20 if (ret)
24 return ret;21 return ret;
25 22
@@ -28,7 +25,7 @@ static int xfer_to_guest_mode_work(struct kvm_vcpu *vcpu, unsigned long ti_work) · 2 unmodified lines
28 return 0;25 return 0;
29}26}
30 27
31int xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu)28int xfer_to_guest_mode_handle_work(void)
32{29{
33 unsigned long ti_work;30 unsigned long ti_work;
34 31
@@ -44,6 +41,6 @@ int xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu) · 9 unmodified lines
44 if (!(ti_work & XFER_TO_GUEST_MODE_WORK))41 if (!(ti_work & XFER_TO_GUEST_MODE_WORK))
45 return 0;42 return 0;
46 43
47 return xfer_to_guest_mode_work(vcpu, ti_work);44 return xfer_to_guest_mode_work(ti_work);
48}45}
49EXPORT_SYMBOL_GPL(xfer_to_guest_mode_handle_work);46EXPORT_SYMBOL_GPL(xfer_to_guest_mode_handle_work);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t