History

entry: Provide infrastructure for work before transitioning to guest mode

Entering a guest is similar to exiting to user space. Pending work like handling signals, rescheduling, task work etc. needs to be handled before that. Provide generic infrastructure to avoid duplication of the same handling code all over the place. The transfer to guest mode handling is different from the exit to usermode handling, e.g. vs. rseq and live patching, so a separate function is used. The initial list of work items handled is: TIF_SIGPENDING, TIF_NEED_RESCHED, TIF_NOTIFY_RESUME Architecture specific TIF flags can be added via defines in the architecture specific include files. The calling convention is also different from the syscall/interrupt entry functions as KVM invokes this from the outer vcpu_run() loop with interrupts and preemption enabled. To prevent missing a pending work item it invokes a check for pending TIF work from interrupt disabled code right before transitioning to guest mode. The lockdep, RCU and tracing state handling is also done directly around the switch to and from guest mode.

Thomas Gleixner committed 6 years ago

Commit 935ace21 parent a5497ba5 files · +144 −1Browse files

Showing the changes to kernel (2 of 5 files). Show all

Unified · Split

2 files
  1. kernel/entry/Makefile +2 −1
  2. kernel/entry/kvm.c +51 −0
kernel/entry/Makefile+2−1
@@ -9,4 +9,5 @@ KCOV_INSTRUMENT := n · 8 unmodified lines
99 CFLAGS_REMOVE_common.o = -fstack-protector -fstack-protector-strong
1010 CFLAGS_common.o += -fno-stack-protector
1111
12−obj-$(CONFIG_GENERIC_ENTRY) += common.o
12+obj-$(CONFIG_GENERIC_ENTRY) += common.o
13+obj-$(CONFIG_KVM_XFER_TO_GUEST_WORK) += kvm.o
kernel/entry/kvm.c+51−0
@@ -0,0 +1,51 @@
1+// SPDX-License-Identifier: GPL-2.0
2+
3+#include <linux/entry-kvm.h>
4+#include <linux/kvm_host.h>
5+
6+static int xfer_to_guest_mode_work(struct kvm_vcpu *vcpu, unsigned long ti_work)
7+{
8+ do {
9+ int ret;
10+
11+ if (ti_work & _TIF_SIGPENDING) {
12+ kvm_handle_signal_exit(vcpu);
13+ return -EINTR;
14+ }
15+
16+ if (ti_work & _TIF_NEED_RESCHED)
17+ schedule();
18+
19+ if (ti_work & _TIF_NOTIFY_RESUME) {
20+ clear_thread_flag(TIF_NOTIFY_RESUME);
21+ tracehook_notify_resume(NULL);
22+ }
23+
24+ ret = arch_xfer_to_guest_mode_handle_work(vcpu, ti_work);
25+ if (ret)
26+ return ret;
27+
28+ ti_work = READ_ONCE(current_thread_info()->flags);
29+ } while (ti_work & XFER_TO_GUEST_MODE_WORK || need_resched());
30+ return 0;
31+}
32+
33+int xfer_to_guest_mode_handle_work(struct kvm_vcpu *vcpu)
34+{
35+ unsigned long ti_work;
36+
37+ /*
38+ * This is invoked from the outer guest loop with interrupts and
39+ * preemption enabled.
40+ *
41+ * KVM invokes xfer_to_guest_mode_work_pending() with interrupts
42+ * disabled in the inner loop before going into guest mode. No need
43+ * to disable interrupts here.
44+ */
45+ ti_work = READ_ONCE(current_thread_info()->flags);
46+ if (!(ti_work & XFER_TO_GUEST_MODE_WORK))
47+ return 0;
48+
49+ return xfer_to_guest_mode_work(vcpu, ti_work);
50+}
51+EXPORT_SYMBOL_GPL(xfer_to_guest_mode_handle_work);