History

Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm

Pull KVM fixes from Paolo Bonzini: "s390: - optimization for the exitless interrupt support that was merged in 4.16-rc1 - improve the branch prediction blocking for nested KVM - replace some jump tables with switch statements to improve expoline performance - fixes for multiple epoch facility ARM: - fix the interaction of userspace irqchip VMs with in-kernel irqchip VMs - make sure we can build 32-bit KVM/ARM with gcc-8. x86: - fixes for AMD SEV - fixes for Intel nested VMX, emulated UMIP and a dump_stack() on VM startup - fixes for async page fault migration - small optimization to PV TLB flush (new in 4.16-rc1) - syzkaller fixes Generic: - compiler warning fixes - syzkaller fixes - more improvements to the kvm_stat tool Two more small Spectre fixes are going to reach you via Ingo" * tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: (40 commits) KVM: SVM: Fix SEV LAUNCH_SECRET command KVM: SVM: install RSM intercept KVM: SVM: no need to call access_ok() in LAUNCH_MEASURE command include: psp-sev: Capitalize invalid length enum crypto: ccp: Fix sparse, use plain integer as NULL pointer KVM: X86: Avoid traversing all the cpus for pv tlb flush when steal time is disabled x86/kvm: Make parse_no_xxx __init for kvm KVM: x86: fix backward migration with async_PF kvm: fix warning for non-x86 builds kvm: fix warning for CONFIG_HAVE_KVM_EVENTFD builds tools/kvm_stat: print 'Total' line for multiple events only tools/kvm_stat: group child events indented after parent tools/kvm_stat: separate drilldown and fields filtering tools/kvm_stat: eliminate extra guest/pid selection dialog tools/kvm_stat: mark private methods as such tools/kvm_stat: fix debugfs handling tools/kvm_stat: print error on invalid regex tools/kvm_stat: fix crash when filtering out all non-child trace events tools/kvm_stat: avoid 'is' for equality checks tools/kvm_stat: use a more pythonic way to iterate over dictionaries ...

Linus Torvalds committed 8 years ago

Commit d4858aa2 parents 4a3928c 9c5e0af26 files · +698 −516Browse files

Unified · Split

26 files
  1. Documentation/virtual/kvm/cpuid.txt +4 −0
  2. Documentation/virtual/kvm/msr.txt +2 −1
  3. arch/arm/kvm/hyp/Makefile +5 −0
  4. arch/arm/kvm/hyp/banked-sr.c +4 −0
  5. arch/s390/kvm/intercept.c +29 −22
  6. arch/s390/kvm/interrupt.c +79 −44
  7. arch/s390/kvm/kvm-s390.c +45 −34
  8. arch/s390/kvm/kvm-s390.h +2 −5
  9. arch/s390/kvm/priv.c +96 −96
  10. arch/s390/kvm/vsie.c +20 −0
  11. arch/x86/include/asm/kvm_host.h +0 −3
  12. arch/x86/include/uapi/asm/kvm_para.h +1 −0
  13. arch/x86/kernel/kvm.c +11 −9
  14. arch/x86/kvm/cpuid.c +2 −1
  15. arch/x86/kvm/lapic.c +0 −1
  16. arch/x86/kvm/mmu.c +1 −1
  17. arch/x86/kvm/svm.c +24 −13
  18. arch/x86/kvm/vmx.c +8 −2
  19. arch/x86/kvm/x86.c +3 −4
  20. drivers/crypto/ccp/psp-dev.c +4 −4
  21. include/linux/kvm_host.h +5 −1
  22. include/uapi/linux/psp-sev.h +1 −1
  23. tools/kvm/kvm_stat/kvm_stat +285 −218
  24. tools/kvm/kvm_stat/kvm_stat.txt +2 −2
  25. virt/kvm/arm/arch_timer.c +64 −52
  26. virt/kvm/kvm_main.c +1 −2
Documentation/virtual/kvm/cpuid.txt+4−0
@@ -58,6 +58,10 @@ KVM_FEATURE_PV_TLB_FLUSH || 9 || guest checks this feature bit · 57 unmodified lines
5858 || || before enabling paravirtualized
5959 || || tlb flush.
6060 ------------------------------------------------------------------------------
61+KVM_FEATURE_ASYNC_PF_VMEXIT || 10 || paravirtualized async PF VM exit
62+ || || can be enabled by setting bit 2
63+ || || when writing to msr 0x4b564d02
64+------------------------------------------------------------------------------
6165 KVM_FEATURE_CLOCKSOURCE_STABLE_BIT || 24 || host will warn if no guest-side
6266 || || per-cpu warps are expected in
6367 || || kvmclock.
Documentation/virtual/kvm/msr.txt+2−1
@@ -170,7 +170,8 @@ MSR_KVM_ASYNC_PF_EN: 0x4b564d02 · 169 unmodified lines
170170 when asynchronous page faults are enabled on the vcpu 0 when
171171 disabled. Bit 1 is 1 if asynchronous page faults can be injected
172172 when vcpu is in cpl == 0. Bit 2 is 1 if asynchronous page faults
173− are delivered to L1 as #PF vmexits.
173+ are delivered to L1 as #PF vmexits. Bit 2 can be set only if
174+ KVM_FEATURE_ASYNC_PF_VMEXIT is present in CPUID.
174175
175176 First 4 byte of 64 byte memory location will be written to by
176177 the hypervisor at the time of asynchronous page fault (APF)
arch/arm/kvm/hyp/Makefile+5−0
@@ -7,6 +7,8 @@ ccflags-y += -fno-stack-protector -DDISABLE_BRANCH_PROFILING · 6 unmodified lines
77
88 KVM=../../../../virt/kvm
99
10+CFLAGS_ARMV7VE :=$(call cc-option, -march=armv7ve)
11+
1012 obj-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/hyp/vgic-v2-sr.o
1113 obj-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/hyp/vgic-v3-sr.o
1214 obj-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/hyp/timer-sr.o
@@ -15,7 +17,10 @@ obj-$(CONFIG_KVM_ARM_HOST) += tlb.o · 2 unmodified lines
1517 obj-$(CONFIG_KVM_ARM_HOST) += cp15-sr.o
1618 obj-$(CONFIG_KVM_ARM_HOST) += vfp.o
1719 obj-$(CONFIG_KVM_ARM_HOST) += banked-sr.o
20+CFLAGS_banked-sr.o += $(CFLAGS_ARMV7VE)
21+
1822 obj-$(CONFIG_KVM_ARM_HOST) += entry.o
1923 obj-$(CONFIG_KVM_ARM_HOST) += hyp-entry.o
2024 obj-$(CONFIG_KVM_ARM_HOST) += switch.o
25+CFLAGS_switch.o += $(CFLAGS_ARMV7VE)
2126 obj-$(CONFIG_KVM_ARM_HOST) += s2-setup.o
arch/arm/kvm/hyp/banked-sr.c+4−0
@@ -20,6 +20,10 @@ · 19 unmodified lines
2020
2121 #include <asm/kvm_hyp.h>
2222
23+/*
24+ * gcc before 4.9 doesn't understand -march=armv7ve, so we have to
25+ * trick the assembler.
26+ */
2327 __asm__(".arch_extension virt");
2428
2529 void __hyp_text __banked_save_state(struct kvm_cpu_context *ctxt)
arch/s390/kvm/intercept.c+29−22
@@ -22,22 +22,6 @@ · 21 unmodified lines
2222 #include "trace.h"
2323 #include "trace-s390.h"
2424
25−
26−static const intercept_handler_t instruction_handlers[256] = {
27− [0x01] = kvm_s390_handle_01,
28− [0x82] = kvm_s390_handle_lpsw,
29− [0x83] = kvm_s390_handle_diag,
30− [0xaa] = kvm_s390_handle_aa,
31− [0xae] = kvm_s390_handle_sigp,
32− [0xb2] = kvm_s390_handle_b2,
33− [0xb6] = kvm_s390_handle_stctl,
34− [0xb7] = kvm_s390_handle_lctl,
35− [0xb9] = kvm_s390_handle_b9,
36− [0xe3] = kvm_s390_handle_e3,
37− [0xe5] = kvm_s390_handle_e5,
38− [0xeb] = kvm_s390_handle_eb,
39−};
40−
4125 u8 kvm_s390_get_ilen(struct kvm_vcpu *vcpu)
4226 {
4327 struct kvm_s390_sie_block *sie_block = vcpu->arch.sie_block;
@@ -129,16 +113,39 @@ static int handle_validity(struct kvm_vcpu *vcpu) · 85 unmodified lines
129113
130114 static int handle_instruction(struct kvm_vcpu *vcpu)
131115 {
132− intercept_handler_t handler;
133−
134116 vcpu->stat.exit_instruction++;
135117 trace_kvm_s390_intercept_instruction(vcpu,
136118 vcpu->arch.sie_block->ipa,
137119 vcpu->arch.sie_block->ipb);
138− handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
139− if (handler)
140− return handler(vcpu);
141− return -EOPNOTSUPP;
120+
121+ switch (vcpu->arch.sie_block->ipa >> 8) {
122+ case 0x01:
123+ return kvm_s390_handle_01(vcpu);
124+ case 0x82:
125+ return kvm_s390_handle_lpsw(vcpu);
126+ case 0x83:
127+ return kvm_s390_handle_diag(vcpu);
128+ case 0xaa:
129+ return kvm_s390_handle_aa(vcpu);
130+ case 0xae:
131+ return kvm_s390_handle_sigp(vcpu);
132+ case 0xb2:
133+ return kvm_s390_handle_b2(vcpu);
134+ case 0xb6:
135+ return kvm_s390_handle_stctl(vcpu);
136+ case 0xb7:
137+ return kvm_s390_handle_lctl(vcpu);
138+ case 0xb9:
139+ return kvm_s390_handle_b9(vcpu);
140+ case 0xe3:
141+ return kvm_s390_handle_e3(vcpu);
142+ case 0xe5:
143+ return kvm_s390_handle_e5(vcpu);
144+ case 0xeb:
145+ return kvm_s390_handle_eb(vcpu);
146+ default:
147+ return -EOPNOTSUPP;
148+ }
142149 }
143150
144151 static int inject_prog_on_prog_intercept(struct kvm_vcpu *vcpu)
arch/s390/kvm/interrupt.c+79−44
@@ -169,8 +169,15 @@ static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu) · 168 unmodified lines
169169
170170 static int ckc_irq_pending(struct kvm_vcpu *vcpu)
171171 {
172− if (vcpu->arch.sie_block->ckc >= kvm_s390_get_tod_clock_fast(vcpu->kvm))
172+ const u64 now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
173+ const u64 ckc = vcpu->arch.sie_block->ckc;
174+
175+ if (vcpu->arch.sie_block->gcr[0] & 0x0020000000000000ul) {
176+ if ((s64)ckc >= (s64)now)
177+ return 0;
178+ } else if (ckc >= now) {
173179 return 0;
180+ }
174181 return ckc_interrupts_enabled(vcpu);
175182 }
176183
@@ -187,12 +194,6 @@ static int cpu_timer_irq_pending(struct kvm_vcpu *vcpu) · 10 unmodified lines
187194 return kvm_s390_get_cpu_timer(vcpu) >> 63;
188195 }
189196
190−static inline int is_ioirq(unsigned long irq_type)
191−{
192− return ((irq_type >= IRQ_PEND_IO_ISC_7) &&
193− (irq_type <= IRQ_PEND_IO_ISC_0));
194−}
195−
196197 static uint64_t isc_to_isc_bits(int isc)
197198 {
198199 return (0x80 >> isc) << 24;
@@ -236,10 +237,15 @@ static inline int kvm_s390_gisa_tac_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gis · 37 unmodified lines
236237 return test_and_clear_bit_inv(IPM_BIT_OFFSET + gisc, (unsigned long *) gisa);
237238 }
238239
239−static inline unsigned long pending_irqs(struct kvm_vcpu *vcpu)
240+static inline unsigned long pending_irqs_no_gisa(struct kvm_vcpu *vcpu)
240241 {
241242 return vcpu->kvm->arch.float_int.pending_irqs |
242− vcpu->arch.local_int.pending_irqs |
243+ vcpu->arch.local_int.pending_irqs;
244+}
245+
246+static inline unsigned long pending_irqs(struct kvm_vcpu *vcpu)
247+{
248+ return pending_irqs_no_gisa(vcpu) |
243249 kvm_s390_gisa_get_ipm(vcpu->kvm->arch.gisa) << IRQ_PEND_IO_ISC_7;
244250 }
245251
@@ -337,7 +343,7 @@ static void __reset_intercept_indicators(struct kvm_vcpu *vcpu) · 91 unmodified lines
337343
338344 static void set_intercept_indicators_io(struct kvm_vcpu *vcpu)
339345 {
340− if (!(pending_irqs(vcpu) & IRQ_PEND_IO_MASK))
346+ if (!(pending_irqs_no_gisa(vcpu) & IRQ_PEND_IO_MASK))
341347 return;
342348 else if (psw_ioint_disabled(vcpu))
343349 kvm_s390_set_cpuflags(vcpu, CPUSTAT_IO_INT);
@@ -1011,24 +1017,6 @@ out: · 667 unmodified lines
10111017 return rc;
10121018 }
10131019
1014−typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu);
1015−
1016−static const deliver_irq_t deliver_irq_funcs[] = {
1017− [IRQ_PEND_MCHK_EX] = __deliver_machine_check,
1018− [IRQ_PEND_MCHK_REP] = __deliver_machine_check,
1019− [IRQ_PEND_PROG] = __deliver_prog,
1020− [IRQ_PEND_EXT_EMERGENCY] = __deliver_emergency_signal,
1021− [IRQ_PEND_EXT_EXTERNAL] = __deliver_external_call,
1022− [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc,
1023− [IRQ_PEND_EXT_CPU_TIMER] = __deliver_cpu_timer,
1024− [IRQ_PEND_RESTART] = __deliver_restart,
1025− [IRQ_PEND_SET_PREFIX] = __deliver_set_prefix,
1026− [IRQ_PEND_PFAULT_INIT] = __deliver_pfault_init,
1027− [IRQ_PEND_EXT_SERVICE] = __deliver_service,
1028− [IRQ_PEND_PFAULT_DONE] = __deliver_pfault_done,
1029− [IRQ_PEND_VIRTIO] = __deliver_virtio,
1030−};
1031−
10321020 /* Check whether an external call is pending (deliverable or not) */
10331021 int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
10341022 {
@@ -1066,13 +1054,19 @@ int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) · 31 unmodified lines
10661054
10671055 static u64 __calculate_sltime(struct kvm_vcpu *vcpu)
10681056 {
1069− u64 now, cputm, sltime = 0;
1057+ const u64 now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
1058+ const u64 ckc = vcpu->arch.sie_block->ckc;
1059+ u64 cputm, sltime = 0;
10701060
10711061 if (ckc_interrupts_enabled(vcpu)) {
1072− now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
1073− sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
1074− /* already expired or overflow? */
1075− if (!sltime || vcpu->arch.sie_block->ckc <= now)
1062+ if (vcpu->arch.sie_block->gcr[0] & 0x0020000000000000ul) {
1063+ if ((s64)now < (s64)ckc)
1064+ sltime = tod_to_ns((s64)ckc - (s64)now);
1065+ } else if (now < ckc) {
1066+ sltime = tod_to_ns(ckc - now);
1067+ }
1068+ /* already expired */
1069+ if (!sltime)
10761070 return 0;
10771071 if (cpu_timer_interrupts_enabled(vcpu)) {
10781072 cputm = kvm_s390_get_cpu_timer(vcpu);
@@ -1192,7 +1186,6 @@ void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu) · 113 unmodified lines
11921186 int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
11931187 {
11941188 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1195− deliver_irq_t func;
11961189 int rc = 0;
11971190 unsigned long irq_type;
11981191 unsigned long irqs;
@@ -1212,16 +1205,57 @@ int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu) · 13 unmodified lines
12121205 while ((irqs = deliverable_irqs(vcpu)) && !rc) {
12131206 /* bits are in the reverse order of interrupt priority */
12141207 irq_type = find_last_bit(&irqs, IRQ_PEND_COUNT);
1215− if (is_ioirq(irq_type)) {
1208+ switch (irq_type) {
1209+ case IRQ_PEND_IO_ISC_0:
1210+ case IRQ_PEND_IO_ISC_1:
1211+ case IRQ_PEND_IO_ISC_2:
1212+ case IRQ_PEND_IO_ISC_3:
1213+ case IRQ_PEND_IO_ISC_4:
1214+ case IRQ_PEND_IO_ISC_5:
1215+ case IRQ_PEND_IO_ISC_6:
1216+ case IRQ_PEND_IO_ISC_7:
12161217 rc = __deliver_io(vcpu, irq_type);
1217− } else {
1218− func = deliver_irq_funcs[irq_type];
1219− if (!func) {
1220− WARN_ON_ONCE(func == NULL);
1221− clear_bit(irq_type, &li->pending_irqs);
1222− continue;
1223− }
1224− rc = func(vcpu);
1218+ break;
1219+ case IRQ_PEND_MCHK_EX:
1220+ case IRQ_PEND_MCHK_REP:
1221+ rc = __deliver_machine_check(vcpu);
1222+ break;
1223+ case IRQ_PEND_PROG:
1224+ rc = __deliver_prog(vcpu);
1225+ break;
1226+ case IRQ_PEND_EXT_EMERGENCY:
1227+ rc = __deliver_emergency_signal(vcpu);
1228+ break;
1229+ case IRQ_PEND_EXT_EXTERNAL:
1230+ rc = __deliver_external_call(vcpu);
1231+ break;
1232+ case IRQ_PEND_EXT_CLOCK_COMP:
1233+ rc = __deliver_ckc(vcpu);
1234+ break;
1235+ case IRQ_PEND_EXT_CPU_TIMER:
1236+ rc = __deliver_cpu_timer(vcpu);
1237+ break;
1238+ case IRQ_PEND_RESTART:
1239+ rc = __deliver_restart(vcpu);
1240+ break;
1241+ case IRQ_PEND_SET_PREFIX:
1242+ rc = __deliver_set_prefix(vcpu);
1243+ break;
1244+ case IRQ_PEND_PFAULT_INIT:
1245+ rc = __deliver_pfault_init(vcpu);
1246+ break;
1247+ case IRQ_PEND_EXT_SERVICE:
1248+ rc = __deliver_service(vcpu);
1249+ break;
1250+ case IRQ_PEND_PFAULT_DONE:
1251+ rc = __deliver_pfault_done(vcpu);
1252+ break;
1253+ case IRQ_PEND_VIRTIO:
1254+ rc = __deliver_virtio(vcpu);
1255+ break;
1256+ default:
1257+ WARN_ONCE(1, "Unknown pending irq type %ld", irq_type);
1258+ clear_bit(irq_type, &li->pending_irqs);
12251259 }
12261260 }
12271261
@@ -1701,7 +1735,8 @@ static void __floating_irq_kick(struct kvm *kvm, u64 type) · 473 unmodified lines
17011735 kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_STOP_INT);
17021736 break;
17031737 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1704− kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_IO_INT);
1738+ if (!(type & KVM_S390_INT_IO_AI_MASK && kvm->arch.gisa))
1739+ kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_IO_INT);
17051740 break;
17061741 default:
17071742 kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_EXT_INT);
arch/s390/kvm/kvm-s390.c+45−34
@@ -179,6 +179,28 @@ int kvm_arch_hardware_enable(void) · 178 unmodified lines
179179 static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
180180 unsigned long end);
181181
182+static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta)
183+{
184+ u8 delta_idx = 0;
185+
186+ /*
187+ * The TOD jumps by delta, we have to compensate this by adding
188+ * -delta to the epoch.
189+ */
190+ delta = -delta;
191+
192+ /* sign-extension - we're adding to signed values below */
193+ if ((s64)delta < 0)
194+ delta_idx = -1;
195+
196+ scb->epoch += delta;
197+ if (scb->ecd & ECD_MEF) {
198+ scb->epdx += delta_idx;
199+ if (scb->epoch < delta)
200+ scb->epdx += 1;
201+ }
202+}
203+
182204 /*
183205 * This callback is executed during stop_machine(). All CPUs are therefore
184206 * temporarily stopped. In order not to change guest behavior, we have to
@@ -194,13 +216,17 @@ static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val, · 9 unmodified lines
194216 unsigned long long *delta = v;
195217
196218 list_for_each_entry(kvm, &vm_list, vm_list) {
197− kvm->arch.epoch -= *delta;
198219 kvm_for_each_vcpu(i, vcpu, kvm) {
199− vcpu->arch.sie_block->epoch -= *delta;
220+ kvm_clock_sync_scb(vcpu->arch.sie_block, *delta);
221+ if (i == 0) {
222+ kvm->arch.epoch = vcpu->arch.sie_block->epoch;
223+ kvm->arch.epdx = vcpu->arch.sie_block->epdx;
224+ }
200225 if (vcpu->arch.cputm_enabled)
201226 vcpu->arch.cputm_start += *delta;
202227 if (vcpu->arch.vsie_block)
203− vcpu->arch.vsie_block->epoch -= *delta;
228+ kvm_clock_sync_scb(vcpu->arch.vsie_block,
229+ *delta);
204230 }
205231 }
206232 return NOTIFY_OK;
@@ -902,12 +928,9 @@ static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) · 695 unmodified lines
902928 if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
903929 return -EFAULT;
904930
905− if (test_kvm_facility(kvm, 139))
906− kvm_s390_set_tod_clock_ext(kvm, &gtod);
907− else if (gtod.epoch_idx == 0)
908− kvm_s390_set_tod_clock(kvm, gtod.tod);
909− else
931+ if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx)
910932 return -EINVAL;
933+ kvm_s390_set_tod_clock(kvm, &gtod);
911934
912935 VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
913936 gtod.epoch_idx, gtod.tod);
@@ -932,13 +955,14 @@ static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) · 18 unmodified lines
932955
933956 static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
934957 {
935− u64 gtod;
958+ struct kvm_s390_vm_tod_clock gtod = { 0 };
936959
937− if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
960+ if (copy_from_user(&gtod.tod, (void __user *)attr->addr,
961+ sizeof(gtod.tod)))
938962 return -EFAULT;
939963
940− kvm_s390_set_tod_clock(kvm, gtod);
941− VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod);
964+ kvm_s390_set_tod_clock(kvm, &gtod);
965+ VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod);
942966 return 0;
943967 }
944968
@@ -2389,6 +2413,7 @@ void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) · 1444 unmodified lines
23892413 mutex_lock(&vcpu->kvm->lock);
23902414 preempt_disable();
23912415 vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
2416+ vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx;
23922417 preempt_enable();
23932418 mutex_unlock(&vcpu->kvm->lock);
23942419 if (!kvm_is_ucontrol(vcpu->kvm)) {
@@ -3021,8 +3046,8 @@ retry: · 626 unmodified lines
30213046 return 0;
30223047 }
30233048
3024−void kvm_s390_set_tod_clock_ext(struct kvm *kvm,
3025− const struct kvm_s390_vm_tod_clock *gtod)
3049+void kvm_s390_set_tod_clock(struct kvm *kvm,
3050+ const struct kvm_s390_vm_tod_clock *gtod)
30263051 {
30273052 struct kvm_vcpu *vcpu;
30283053 struct kvm_s390_tod_clock_ext htod;
@@ -3034,10 +3059,12 @@ void kvm_s390_set_tod_clock_ext(struct kvm *kvm, · 5 unmodified lines
30343059 get_tod_clock_ext((char *)&htod);
30353060
30363061 kvm->arch.epoch = gtod->tod - htod.tod;
3037− kvm->arch.epdx = gtod->epoch_idx - htod.epoch_idx;
3038−
3039− if (kvm->arch.epoch > gtod->tod)
3040− kvm->arch.epdx -= 1;
3062+ kvm->arch.epdx = 0;
3063+ if (test_kvm_facility(kvm, 139)) {
3064+ kvm->arch.epdx = gtod->epoch_idx - htod.epoch_idx;
3065+ if (kvm->arch.epoch > gtod->tod)
3066+ kvm->arch.epdx -= 1;
3067+ }
30413068
30423069 kvm_s390_vcpu_block_all(kvm);
30433070 kvm_for_each_vcpu(i, vcpu, kvm) {
@@ -3050,22 +3077,6 @@ void kvm_s390_set_tod_clock_ext(struct kvm *kvm, · 6 unmodified lines
30503077 mutex_unlock(&kvm->lock);
30513078 }
30523079
3053−void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod)
3054−{
3055− struct kvm_vcpu *vcpu;
3056− int i;
3057−
3058− mutex_lock(&kvm->lock);
3059− preempt_disable();
3060− kvm->arch.epoch = tod - get_tod_clock();
3061− kvm_s390_vcpu_block_all(kvm);
3062− kvm_for_each_vcpu(i, vcpu, kvm)
3063− vcpu->arch.sie_block->epoch = kvm->arch.epoch;
3064− kvm_s390_vcpu_unblock_all(kvm);
3065− preempt_enable();
3066− mutex_unlock(&kvm->lock);
3067−}
3068−
30693080 /**
30703081 * kvm_arch_fault_in_page - fault-in guest page if necessary
30713082 * @vcpu: The corresponding virtual cpu
arch/s390/kvm/kvm-s390.h+2−5
@@ -19,8 +19,6 @@ · 18 unmodified lines
1919 #include <asm/processor.h>
2020 #include <asm/sclp.h>
2121
22−typedef int (*intercept_handler_t)(struct kvm_vcpu *vcpu);
23−
2422 /* Transactional Memory Execution related macros */
2523 #define IS_TE_ENABLED(vcpu) ((vcpu->arch.sie_block->ecb & ECB_TE))
2624 #define TDB_FORMAT1 1
@@ -283,9 +281,8 @@ int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu); · 256 unmodified lines
283281 int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu);
284282
285283 /* implemented in kvm-s390.c */
286−void kvm_s390_set_tod_clock_ext(struct kvm *kvm,
287− const struct kvm_s390_vm_tod_clock *gtod);
288−void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod);
284+void kvm_s390_set_tod_clock(struct kvm *kvm,
285+ const struct kvm_s390_vm_tod_clock *gtod);
289286 long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable);
290287 int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long addr);
291288 int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr);
arch/s390/kvm/priv.c+96−96
@@ -85,9 +85,10 @@ int kvm_s390_handle_e3(struct kvm_vcpu *vcpu) · 84 unmodified lines
8585 /* Handle SCK (SET CLOCK) interception */
8686 static int handle_set_clock(struct kvm_vcpu *vcpu)
8787 {
88+ struct kvm_s390_vm_tod_clock gtod = { 0 };
8889 int rc;
8990 u8 ar;
90− u64 op2, val;
91+ u64 op2;
9192
9293 vcpu->stat.instruction_sck++;
9394
@@ -97,12 +98,12 @@ static int handle_set_clock(struct kvm_vcpu *vcpu) · 3 unmodified lines
9798 op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
9899 if (op2 & 7) /* Operand must be on a doubleword boundary */
99100 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
100− rc = read_guest(vcpu, op2, ar, &val, sizeof(val));
101+ rc = read_guest(vcpu, op2, ar, &gtod.tod, sizeof(gtod.tod));
101102 if (rc)
102103 return kvm_s390_inject_prog_cond(vcpu, rc);
103104
104− VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", val);
105− kvm_s390_set_tod_clock(vcpu->kvm, val);
105+ VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", gtod.tod);
106+ kvm_s390_set_tod_clock(vcpu->kvm, &gtod);
106107
107108 kvm_s390_set_psw_cc(vcpu, 0);
108109 return 0;
@@ -795,55 +796,60 @@ out: · 686 unmodified lines
795796 return rc;
796797 }
797798
798−static const intercept_handler_t b2_handlers[256] = {
799− [0x02] = handle_stidp,
800− [0x04] = handle_set_clock,
801− [0x10] = handle_set_prefix,
802− [0x11] = handle_store_prefix,
803− [0x12] = handle_store_cpu_address,
804− [0x14] = kvm_s390_handle_vsie,
805− [0x21] = handle_ipte_interlock,
806− [0x29] = handle_iske,
807− [0x2a] = handle_rrbe,
808− [0x2b] = handle_sske,
809− [0x2c] = handle_test_block,
810− [0x30] = handle_io_inst,
811− [0x31] = handle_io_inst,
812− [0x32] = handle_io_inst,
813− [0x33] = handle_io_inst,
814− [0x34] = handle_io_inst,
815− [0x35] = handle_io_inst,
816− [0x36] = handle_io_inst,
817− [0x37] = handle_io_inst,
818− [0x38] = handle_io_inst,
819− [0x39] = handle_io_inst,
820− [0x3a] = handle_io_inst,
821− [0x3b] = handle_io_inst,
822− [0x3c] = handle_io_inst,
823− [0x50] = handle_ipte_interlock,
824− [0x56] = handle_sthyi,
825− [0x5f] = handle_io_inst,
826− [0x74] = handle_io_inst,
827− [0x76] = handle_io_inst,
828− [0x7d] = handle_stsi,
829− [0xb1] = handle_stfl,
830− [0xb2] = handle_lpswe,
831−};
832−
833799 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
834800 {
835− intercept_handler_t handler;
836−
837− /*
838− * A lot of B2 instructions are priviledged. Here we check for
839− * the privileged ones, that we can handle in the kernel.
840− * Anything else goes to userspace.
841− */
842− handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
843− if (handler)
844− return handler(vcpu);
845−
846− return -EOPNOTSUPP;
801+ switch (vcpu->arch.sie_block->ipa & 0x00ff) {
802+ case 0x02:
803+ return handle_stidp(vcpu);
804+ case 0x04:
805+ return handle_set_clock(vcpu);
806+ case 0x10:
807+ return handle_set_prefix(vcpu);
808+ case 0x11:
809+ return handle_store_prefix(vcpu);
810+ case 0x12:
811+ return handle_store_cpu_address(vcpu);
812+ case 0x14:
813+ return kvm_s390_handle_vsie(vcpu);
814+ case 0x21:
815+ case 0x50:
816+ return handle_ipte_interlock(vcpu);
817+ case 0x29:
818+ return handle_iske(vcpu);
819+ case 0x2a:
820+ return handle_rrbe(vcpu);
821+ case 0x2b:
822+ return handle_sske(vcpu);
823+ case 0x2c:
824+ return handle_test_block(vcpu);
825+ case 0x30:
826+ case 0x31:
827+ case 0x32:
828+ case 0x33:
829+ case 0x34:
830+ case 0x35:
831+ case 0x36:
832+ case 0x37:
833+ case 0x38:
834+ case 0x39:
835+ case 0x3a:
836+ case 0x3b:
837+ case 0x3c:
838+ case 0x5f:
839+ case 0x74:
840+ case 0x76:
841+ return handle_io_inst(vcpu);
842+ case 0x56:
843+ return handle_sthyi(vcpu);
844+ case 0x7d:
845+ return handle_stsi(vcpu);
846+ case 0xb1:
847+ return handle_stfl(vcpu);
848+ case 0xb2:
849+ return handle_lpswe(vcpu);
850+ default:
851+ return -EOPNOTSUPP;
852+ }
847853 }
848854
849855 static int handle_epsw(struct kvm_vcpu *vcpu)
@@ -1105,25 +1111,22 @@ static int handle_essa(struct kvm_vcpu *vcpu) · 255 unmodified lines
11051111 return 0;
11061112 }
11071113
1108−static const intercept_handler_t b9_handlers[256] = {
1109− [0x8a] = handle_ipte_interlock,
1110− [0x8d] = handle_epsw,
1111− [0x8e] = handle_ipte_interlock,
1112− [0x8f] = handle_ipte_interlock,
1113− [0xab] = handle_essa,
1114− [0xaf] = handle_pfmf,
1115−};
1116−
11171114 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
11181115 {
1119− intercept_handler_t handler;
1120−
1121− /* This is handled just as for the B2 instructions. */
1122− handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1123− if (handler)
1124− return handler(vcpu);
1125−
1126− return -EOPNOTSUPP;
1116+ switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1117+ case 0x8a:
1118+ case 0x8e:
1119+ case 0x8f:
1120+ return handle_ipte_interlock(vcpu);
1121+ case 0x8d:
1122+ return handle_epsw(vcpu);
1123+ case 0xab:
1124+ return handle_essa(vcpu);
1125+ case 0xaf:
1126+ return handle_pfmf(vcpu);
1127+ default:
1128+ return -EOPNOTSUPP;
1129+ }
11271130 }
11281131
11291132 int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
@@ -1271,22 +1274,20 @@ static int handle_stctg(struct kvm_vcpu *vcpu) · 141 unmodified lines
12711274 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
12721275 }
12731276
1274−static const intercept_handler_t eb_handlers[256] = {
1275− [0x2f] = handle_lctlg,
1276− [0x25] = handle_stctg,
1277− [0x60] = handle_ri,
1278− [0x61] = handle_ri,
1279− [0x62] = handle_ri,
1280−};
1281−
12821277 int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
12831278 {
1284− intercept_handler_t handler;
1285−
1286− handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
1287− if (handler)
1288− return handler(vcpu);
1289− return -EOPNOTSUPP;
1279+ switch (vcpu->arch.sie_block->ipb & 0x000000ff) {
1280+ case 0x25:
1281+ return handle_stctg(vcpu);
1282+ case 0x2f:
1283+ return handle_lctlg(vcpu);
1284+ case 0x60:
1285+ case 0x61:
1286+ case 0x62:
1287+ return handle_ri(vcpu);
1288+ default:
1289+ return -EOPNOTSUPP;
1290+ }
12901291 }
12911292
12921293 static int handle_tprot(struct kvm_vcpu *vcpu)
@@ -1346,10 +1347,12 @@ out_unlock: · 53 unmodified lines
13461347
13471348 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
13481349 {
1349− /* For e5xx... instructions we only handle TPROT */
1350− if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
1350+ switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1351+ case 0x01:
13511352 return handle_tprot(vcpu);
1352− return -EOPNOTSUPP;
1353+ default:
1354+ return -EOPNOTSUPP;
1355+ }
13531356 }
13541357
13551358 static int handle_sckpf(struct kvm_vcpu *vcpu)
@@ -1380,17 +1383,14 @@ static int handle_ptff(struct kvm_vcpu *vcpu) · 24 unmodified lines
13801383 return 0;
13811384 }
13821385
1383−static const intercept_handler_t x01_handlers[256] = {
1384− [0x04] = handle_ptff,
1385− [0x07] = handle_sckpf,
1386−};
1387−
13881386 int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
13891387 {
1390− intercept_handler_t handler;
1391−
1392− handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1393− if (handler)
1394− return handler(vcpu);
1395− return -EOPNOTSUPP;
1388+ switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1389+ case 0x04:
1390+ return handle_ptff(vcpu);
1391+ case 0x07:
1392+ return handle_sckpf(vcpu);
1393+ default:
1394+ return -EOPNOTSUPP;
1395+ }
13961396 }
arch/s390/kvm/vsie.c+20−0
@@ -821,6 +821,7 @@ static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · 820 unmodified lines
821821 {
822822 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
823823 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
824+ int guest_bp_isolation;
824825 int rc;
825826
826827 handle_last_fault(vcpu, vsie_page);
@@ -831,6 +832,20 @@ static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · 4 unmodified lines
831832 s390_handle_mcck();
832833
833834 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
835+
836+ /* save current guest state of bp isolation override */
837+ guest_bp_isolation = test_thread_flag(TIF_ISOLATE_BP_GUEST);
838+
839+ /*
840+ * The guest is running with BPBC, so we have to force it on for our
841+ * nested guest. This is done by enabling BPBC globally, so the BPBC
842+ * control in the SCB (which the nested guest can modify) is simply
843+ * ignored.
844+ */
845+ if (test_kvm_facility(vcpu->kvm, 82) &&
846+ vcpu->arch.sie_block->fpf & FPF_BPBC)
847+ set_thread_flag(TIF_ISOLATE_BP_GUEST);
848+
834849 local_irq_disable();
835850 guest_enter_irqoff();
836851 local_irq_enable();
@@ -840,6 +855,11 @@ static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · 3 unmodified lines
840855 local_irq_disable();
841856 guest_exit_irqoff();
842857 local_irq_enable();
858+
859+ /* restore guest state for bp isolation override */
860+ if (!guest_bp_isolation)
861+ clear_thread_flag(TIF_ISOLATE_BP_GUEST);
862+
843863 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
844864
845865 if (rc == -EINTR) {
arch/x86/include/asm/kvm_host.h+0−3
@@ -1464,7 +1464,4 @@ static inline int kvm_cpu_get_apicid(int mps_cpu) · 1463 unmodified lines
14641464 #define put_smstate(type, buf, offset, val) \
14651465 *(type *)((buf) + (offset) - 0x7e00) = val
14661466
1467−void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
1468− unsigned long start, unsigned long end);
1469−
14701467 #endif /* _ASM_X86_KVM_HOST_H */
arch/x86/include/uapi/asm/kvm_para.h+1−0
@@ -26,6 +26,7 @@ · 25 unmodified lines
2626 #define KVM_FEATURE_PV_EOI 6
2727 #define KVM_FEATURE_PV_UNHALT 7
2828 #define KVM_FEATURE_PV_TLB_FLUSH 9
29+#define KVM_FEATURE_ASYNC_PF_VMEXIT 10
2930
3031 /* The last 8 bits are used to indicate how to interpret the flags field
3132 * in pvclock structure. If no bits are set, all flags are ignored.
arch/x86/kernel/kvm.c+11−9
@@ -49,7 +49,7 @@ · 48 unmodified lines
4949
5050 static int kvmapf = 1;
5151
52−static int parse_no_kvmapf(char *arg)
52+static int __init parse_no_kvmapf(char *arg)
5353 {
5454 kvmapf = 0;
5555 return 0;
@@ -58,7 +58,7 @@ static int parse_no_kvmapf(char *arg) · 2 unmodified lines
5858 early_param("no-kvmapf", parse_no_kvmapf);
5959
6060 static int steal_acc = 1;
61−static int parse_no_stealacc(char *arg)
61+static int __init parse_no_stealacc(char *arg)
6262 {
6363 steal_acc = 0;
6464 return 0;
@@ -67,7 +67,7 @@ static int parse_no_stealacc(char *arg) · 2 unmodified lines
6767 early_param("no-steal-acc", parse_no_stealacc);
6868
6969 static int kvmclock_vsyscall = 1;
70−static int parse_no_kvmclock_vsyscall(char *arg)
70+static int __init parse_no_kvmclock_vsyscall(char *arg)
7171 {
7272 kvmclock_vsyscall = 0;
7373 return 0;
@@ -341,10 +341,10 @@ static void kvm_guest_cpu_init(void) · 267 unmodified lines
341341 #endif
342342 pa |= KVM_ASYNC_PF_ENABLED;
343343
344− /* Async page fault support for L1 hypervisor is optional */
345− if (wrmsr_safe(MSR_KVM_ASYNC_PF_EN,
346− (pa | KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT) & 0xffffffff, pa >> 32) < 0)
347− wrmsrl(MSR_KVM_ASYNC_PF_EN, pa);
344+ if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_VMEXIT))
345+ pa |= KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
346+
347+ wrmsrl(MSR_KVM_ASYNC_PF_EN, pa);
348348 __this_cpu_write(apf_reason.enabled, 1);
349349 printk(KERN_INFO"KVM setup async PF for cpu %d\n",
350350 smp_processor_id());
@@ -545,7 +545,8 @@ static void __init kvm_guest_init(void) · 194 unmodified lines
545545 pv_time_ops.steal_clock = kvm_steal_clock;
546546 }
547547
548− if (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH))
548+ if (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) &&
549+ !kvm_para_has_feature(KVM_FEATURE_STEAL_TIME))
549550 pv_mmu_ops.flush_tlb_others = kvm_flush_tlb_others;
550551
551552 if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
@@ -633,7 +634,8 @@ static __init int kvm_setup_pv_tlb_flush(void) · 81 unmodified lines
633634 {
634635 int cpu;
635636
636− if (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH)) {
637+ if (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) &&
638+ !kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
637639 for_each_possible_cpu(cpu) {
638640 zalloc_cpumask_var_node(per_cpu_ptr(&__pv_tlb_mask, cpu),
639641 GFP_KERNEL, cpu_to_node(cpu));
arch/x86/kvm/cpuid.c+2−1
@@ -607,7 +607,8 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, · 606 unmodified lines
607607 (1 << KVM_FEATURE_PV_EOI) |
608608 (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT) |
609609 (1 << KVM_FEATURE_PV_UNHALT) |
610− (1 << KVM_FEATURE_PV_TLB_FLUSH);
610+ (1 << KVM_FEATURE_PV_TLB_FLUSH) |
611+ (1 << KVM_FEATURE_ASYNC_PF_VMEXIT);
611612
612613 if (sched_info_on())
613614 entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);
arch/x86/kvm/lapic.c+0−1
@@ -2165,7 +2165,6 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu) · 2164 unmodified lines
21652165 */
21662166 vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
21672167 static_key_slow_inc(&apic_sw_disabled.key); /* sw disabled at reset */
2168− kvm_lapic_reset(vcpu, false);
21692168 kvm_iodevice_init(&apic->dev, &apic_mmio_ops);
21702169
21712170 return 0;
arch/x86/kvm/mmu.c+1−1
@@ -3029,7 +3029,7 @@ static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn) · 3028 unmodified lines
30293029 return RET_PF_RETRY;
30303030 }
30313031
3032− return -EFAULT;
3032+ return RET_PF_EMULATE;
30333033 }
30343034
30353035 static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu,
arch/x86/kvm/svm.c+24−13
@@ -300,6 +300,8 @@ module_param(vgif, int, 0444); · 299 unmodified lines
300300 static int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT);
301301 module_param(sev, int, 0444);
302302
303+static u8 rsm_ins_bytes[] = "\x0f\xaa";
304+
303305 static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
304306 static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa);
305307 static void svm_complete_interrupts(struct vcpu_svm *svm);
@@ -1383,6 +1385,7 @@ static void init_vmcb(struct vcpu_svm *svm) · 1077 unmodified lines
13831385 set_intercept(svm, INTERCEPT_SKINIT);
13841386 set_intercept(svm, INTERCEPT_WBINVD);
13851387 set_intercept(svm, INTERCEPT_XSETBV);
1388+ set_intercept(svm, INTERCEPT_RSM);
13861389
13871390 if (!kvm_mwait_in_guest()) {
13881391 set_intercept(svm, INTERCEPT_MONITOR);
@@ -3699,6 +3702,12 @@ static int emulate_on_interception(struct vcpu_svm *svm) · 2310 unmodified lines
36993702 return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
37003703 }
37013704
3705+static int rsm_interception(struct vcpu_svm *svm)
3706+{
3707+ return x86_emulate_instruction(&svm->vcpu, 0, 0,
3708+ rsm_ins_bytes, 2) == EMULATE_DONE;
3709+}
3710+
37023711 static int rdpmc_interception(struct vcpu_svm *svm)
37033712 {
37043713 int err;
@@ -4541,7 +4550,7 @@ static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = { · 836 unmodified lines
45414550 [SVM_EXIT_MWAIT] = mwait_interception,
45424551 [SVM_EXIT_XSETBV] = xsetbv_interception,
45434552 [SVM_EXIT_NPF] = npf_interception,
4544− [SVM_EXIT_RSM] = emulate_on_interception,
4553+ [SVM_EXIT_RSM] = rsm_interception,
45454554 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
45464555 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
45474556 };
@@ -6236,16 +6245,18 @@ e_free: · 1688 unmodified lines
62366245
62376246 static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
62386247 {
6248+ void __user *measure = (void __user *)(uintptr_t)argp->data;
62396249 struct kvm_sev_info *sev = &kvm->arch.sev_info;
62406250 struct sev_data_launch_measure *data;
62416251 struct kvm_sev_launch_measure params;
6252+ void __user *p = NULL;
62426253 void *blob = NULL;
62436254 int ret;
62446255
62456256 if (!sev_guest(kvm))
62466257 return -ENOTTY;
62476258
6248− if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
6259+ if (copy_from_user(&params, measure, sizeof(params)))
62496260 return -EFAULT;
62506261
62516262 data = kzalloc(sizeof(*data), GFP_KERNEL);
@@ -6256,17 +6267,13 @@ static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp) · 4 unmodified lines
62566267 if (!params.len)
62576268 goto cmd;
62586269
6259− if (params.uaddr) {
6270+ p = (void __user *)(uintptr_t)params.uaddr;
6271+ if (p) {
62606272 if (params.len > SEV_FW_BLOB_MAX_SIZE) {
62616273 ret = -EINVAL;
62626274 goto e_free;
62636275 }
62646276
6265− if (!access_ok(VERIFY_WRITE, params.uaddr, params.len)) {
6266− ret = -EFAULT;
6267− goto e_free;
6268− }
6269−
62706277 ret = -ENOMEM;
62716278 blob = kmalloc(params.len, GFP_KERNEL);
62726279 if (!blob)
@@ -6290,13 +6297,13 @@ cmd: · 17 unmodified lines
62906297 goto e_free_blob;
62916298
62926299 if (blob) {
6293− if (copy_to_user((void __user *)(uintptr_t)params.uaddr, blob, params.len))
6300+ if (copy_to_user(p, blob, params.len))
62946301 ret = -EFAULT;
62956302 }
62966303
62976304 done:
62986305 params.len = data->len;
6299− if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
6306+ if (copy_to_user(measure, &params, sizeof(params)))
63006307 ret = -EFAULT;
63016308 e_free_blob:
63026309 kfree(blob);
@@ -6597,7 +6604,7 @@ static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) · 294 unmodified lines
65976604 struct page **pages;
65986605 void *blob, *hdr;
65996606 unsigned long n;
6600− int ret;
6607+ int ret, offset;
66016608
66026609 if (!sev_guest(kvm))
66036610 return -ENOTTY;
@@ -6623,6 +6630,10 @@ static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) · 19 unmodified lines
66236630 if (!data)
66246631 goto e_unpin_memory;
66256632
6633+ offset = params.guest_uaddr & (PAGE_SIZE - 1);
6634+ data->guest_address = __sme_page_pa(pages[0]) + offset;
6635+ data->guest_len = params.guest_len;
6636+
66266637 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
66276638 if (IS_ERR(blob)) {
66286639 ret = PTR_ERR(blob);
@@ -6637,8 +6648,8 @@ static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp) · 8 unmodified lines
66376648 ret = PTR_ERR(hdr);
66386649 goto e_free_blob;
66396650 }
6640− data->trans_address = __psp_pa(blob);
6641− data->trans_len = params.trans_len;
6651+ data->hdr_address = __psp_pa(hdr);
6652+ data->hdr_len = params.hdr_len;
66426653
66436654 data->handle = sev->handle;
66446655 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error);
arch/x86/kvm/vmx.c+8−2
@@ -4485,7 +4485,8 @@ static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) · 4484 unmodified lines
44854485 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
44864486 SECONDARY_EXEC_DESC);
44874487 hw_cr4 &= ~X86_CR4_UMIP;
4488− } else
4488+ } else if (!is_guest_mode(vcpu) ||
4489+ !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
44894490 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
44904491 SECONDARY_EXEC_DESC);
44914492
@@ -11199,7 +11200,12 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) · 6707 unmodified lines
1119911200 if (ret)
1120011201 return ret;
1120111202
11202− if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
11203+ /*
11204+ * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11205+ * by event injection, halt vcpu.
11206+ */
11207+ if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
11208+ !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK))
1120311209 return kvm_vcpu_halt(vcpu);
1120411210
1120511211 vmx->nested.nested_run_pending = 1;
arch/x86/kvm/x86.c+3−4
@@ -7975,6 +7975,7 @@ int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) · 7974 unmodified lines
79757975 kvm_vcpu_mtrr_init(vcpu);
79767976 vcpu_load(vcpu);
79777977 kvm_vcpu_reset(vcpu, false);
7978+ kvm_lapic_reset(vcpu, false);
79787979 kvm_mmu_setup(vcpu);
79797980 vcpu_put(vcpu);
79807981 return 0;
@@ -8460,10 +8461,8 @@ int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) · 479 unmodified lines
84608461 return r;
84618462 }
84628463
8463− if (!size) {
8464− r = vm_munmap(old.userspace_addr, old.npages * PAGE_SIZE);
8465− WARN_ON(r < 0);
8466− }
8464+ if (!size)
8465+ vm_munmap(old.userspace_addr, old.npages * PAGE_SIZE);
84678466
84688467 return 0;
84698468 }
drivers/crypto/ccp/psp-dev.c+4−4
@@ -211,7 +211,7 @@ static int __sev_platform_shutdown_locked(int *error) · 210 unmodified lines
211211 {
212212 int ret;
213213
214− ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, 0, error);
214+ ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
215215 if (ret)
216216 return ret;
217217
@@ -271,7 +271,7 @@ static int sev_ioctl_do_reset(struct sev_issue_cmd *argp) · 53 unmodified lines
271271 return rc;
272272 }
273273
274− return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, 0, &argp->error);
274+ return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
275275 }
276276
277277 static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
@@ -299,7 +299,7 @@ static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp) · 21 unmodified lines
299299 return rc;
300300 }
301301
302− return __sev_do_cmd_locked(cmd, 0, &argp->error);
302+ return __sev_do_cmd_locked(cmd, NULL, &argp->error);
303303 }
304304
305305 static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp)
@@ -624,7 +624,7 @@ EXPORT_SYMBOL_GPL(sev_guest_decommission); · 318 unmodified lines
624624
625625 int sev_guest_df_flush(int *error)
626626 {
627− return sev_do_cmd(SEV_CMD_DF_FLUSH, 0, error);
627+ return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
628628 }
629629 EXPORT_SYMBOL_GPL(sev_guest_df_flush);
630630
include/linux/kvm_host.h+5−1
@@ -1105,7 +1105,6 @@ static inline void kvm_irq_routing_update(struct kvm *kvm) · 1104 unmodified lines
11051105 {
11061106 }
11071107 #endif
1108−void kvm_arch_irq_routing_update(struct kvm *kvm);
11091108
11101109 static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
11111110 {
@@ -1114,6 +1113,8 @@ static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) · 2 unmodified lines
11141113
11151114 #endif /* CONFIG_HAVE_KVM_EVENTFD */
11161115
1116+void kvm_arch_irq_routing_update(struct kvm *kvm);
1117+
11171118 static inline void kvm_make_request(int req, struct kvm_vcpu *vcpu)
11181119 {
11191120 /*
@@ -1272,4 +1273,7 @@ static inline long kvm_arch_vcpu_async_ioctl(struct file *filp, · 152 unmodified lines
12721273 }
12731274 #endif /* CONFIG_HAVE_KVM_VCPU_ASYNC_IOCTL */
12741275
1276+void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
1277+ unsigned long start, unsigned long end);
1278+
12751279 #endif
include/uapi/linux/psp-sev.h+1−1
@@ -42,7 +42,7 @@ typedef enum { · 41 unmodified lines
4242 SEV_RET_INVALID_PLATFORM_STATE,
4343 SEV_RET_INVALID_GUEST_STATE,
4444 SEV_RET_INAVLID_CONFIG,
45− SEV_RET_INVALID_len,
45+ SEV_RET_INVALID_LEN,
4646 SEV_RET_ALREADY_OWNED,
4747 SEV_RET_INVALID_CERTIFICATE,
4848 SEV_RET_POLICY_FAILURE,
tools/kvm/kvm_stat/kvm_stat+285−218
@@ -33,7 +33,7 @@ import resource · 32 unmodified lines
3333 import struct
3434 import re
3535 import subprocess
36−from collections import defaultdict
36+from collections import defaultdict, namedtuple
3737
3838 VMX_EXIT_REASONS = {
3939 'EXCEPTION_NMI': 0,
@@ -228,6 +228,7 @@ IOCTL_NUMBERS = { · 188 unmodified lines
228228 }
229229
230230 ENCODING = locale.getpreferredencoding(False)
231+TRACE_FILTER = re.compile(r'^[^\(]*$')
231232
232233
233234 class Arch(object):
@@ -260,6 +261,11 @@ class Arch(object): · 26 unmodified lines
260261 return ArchX86(SVM_EXIT_REASONS)
261262 return
262263
264+ def tracepoint_is_child(self, field):
265+ if (TRACE_FILTER.match(field)):
266+ return None
267+ return field.split('(', 1)[0]
268+
263269
264270 class ArchX86(Arch):
265271 def __init__(self, exit_reasons):
@@ -267,6 +273,10 @@ class ArchX86(Arch): · 1 unmodified line
267273 self.ioctl_numbers = IOCTL_NUMBERS
268274 self.exit_reasons = exit_reasons
269275
276+ def debugfs_is_child(self, field):
277+ """ Returns name of parent if 'field' is a child, None otherwise """
278+ return None
279+
270280
271281 class ArchPPC(Arch):
272282 def __init__(self):
@@ -282,6 +292,10 @@ class ArchPPC(Arch): · 9 unmodified lines
282292 self.ioctl_numbers['SET_FILTER'] = 0x80002406 | char_ptr_size << 16
283293 self.exit_reasons = {}
284294
295+ def debugfs_is_child(self, field):
296+ """ Returns name of parent if 'field' is a child, None otherwise """
297+ return None
298+
285299
286300 class ArchA64(Arch):
287301 def __init__(self):
@@ -289,6 +303,10 @@ class ArchA64(Arch): · 1 unmodified line
289303 self.ioctl_numbers = IOCTL_NUMBERS
290304 self.exit_reasons = AARCH64_EXIT_REASONS
291305
306+ def debugfs_is_child(self, field):
307+ """ Returns name of parent if 'field' is a child, None otherwise """
308+ return None
309+
292310
293311 class ArchS390(Arch):
294312 def __init__(self):
@@ -296,6 +314,12 @@ class ArchS390(Arch): · 1 unmodified line
296314 self.ioctl_numbers = IOCTL_NUMBERS
297315 self.exit_reasons = None
298316
317+ def debugfs_is_child(self, field):
318+ """ Returns name of parent if 'field' is a child, None otherwise """
319+ if field.startswith('instruction_'):
320+ return 'exit_instruction'
321+
322+
299323 ARCH = Arch.get_arch()
300324
301325
@@ -331,9 +355,6 @@ class perf_event_attr(ctypes.Structure): · 29 unmodified lines
331355 PERF_TYPE_TRACEPOINT = 2
332356 PERF_FORMAT_GROUP = 1 << 3
333357
334−PATH_DEBUGFS_TRACING = '/sys/kernel/debug/tracing'
335−PATH_DEBUGFS_KVM = '/sys/kernel/debug/kvm'
336−
337358
338359 class Group(object):
339360 """Represents a perf event group."""
@@ -376,8 +397,8 @@ class Event(object): · 36 unmodified lines
376397 self.syscall = self.libc.syscall
377398 self.name = name
378399 self.fd = None
379− self.setup_event(group, trace_cpu, trace_pid, trace_point,
380− trace_filter, trace_set)
400+ self._setup_event(group, trace_cpu, trace_pid, trace_point,
401+ trace_filter, trace_set)
381402
382403 def __del__(self):
383404 """Closes the event's file descriptor.
@@ -390,7 +411,7 @@ class Event(object): · 6 unmodified lines
390411 if self.fd:
391412 os.close(self.fd)
392413
393− def perf_event_open(self, attr, pid, cpu, group_fd, flags):
414+ def _perf_event_open(self, attr, pid, cpu, group_fd, flags):
394415 """Wrapper for the sys_perf_evt_open() syscall.
395416
396417 Used to set up performance events, returns a file descriptor or -1
@@ -409,7 +430,7 @@ class Event(object): · 12 unmodified lines
409430 ctypes.c_int(pid), ctypes.c_int(cpu),
410431 ctypes.c_int(group_fd), ctypes.c_long(flags))
411432
412− def setup_event_attribute(self, trace_set, trace_point):
433+ def _setup_event_attribute(self, trace_set, trace_point):
413434 """Returns an initialized ctype perf_event_attr struct."""
414435
415436 id_path = os.path.join(PATH_DEBUGFS_TRACING, 'events', trace_set,
@@ -419,8 +440,8 @@ class Event(object): · 3 unmodified lines
419440 event_attr.config = int(open(id_path).read())
420441 return event_attr
421442
422− def setup_event(self, group, trace_cpu, trace_pid, trace_point,
423− trace_filter, trace_set):
443+ def _setup_event(self, group, trace_cpu, trace_pid, trace_point,
444+ trace_filter, trace_set):
424445 """Sets up the perf event in Linux.
425446
426447 Issues the syscall to register the event in the kernel and
@@ -428,7 +449,7 @@ class Event(object): · 1 unmodified line
428449
429450 """
430451
431− event_attr = self.setup_event_attribute(trace_set, trace_point)
452+ event_attr = self._setup_event_attribute(trace_set, trace_point)
432453
433454 # First event will be group leader.
434455 group_leader = -1
@@ -437,8 +458,8 @@ class Event(object): · 2 unmodified lines
437458 if group.events:
438459 group_leader = group.events[0].fd
439460
440− fd = self.perf_event_open(event_attr, trace_pid,
441− trace_cpu, group_leader, 0)
461+ fd = self._perf_event_open(event_attr, trace_pid,
462+ trace_cpu, group_leader, 0)
442463 if fd == -1:
443464 err = ctypes.get_errno()
444465 raise OSError(err, os.strerror(err),
@@ -475,6 +496,10 @@ class Event(object): · 30 unmodified lines
475496
476497 class Provider(object):
477498 """Encapsulates functionalities used by all providers."""
499+ def __init__(self, pid):
500+ self.child_events = False
501+ self.pid = pid
502+
478503 @staticmethod
479504 def is_field_wanted(fields_filter, field):
480505 """Indicate whether field is valid according to fields_filter."""
@@ -500,12 +525,12 @@ class TracepointProvider(Provider): · 19 unmodified lines
500525 """
501526 def __init__(self, pid, fields_filter):
502527 self.group_leaders = []
503− self.filters = self.get_filters()
528+ self.filters = self._get_filters()
504529 self.update_fields(fields_filter)
505− self.pid = pid
530+ super(TracepointProvider, self).__init__(pid)
506531
507532 @staticmethod
508− def get_filters():
533+ def _get_filters():
509534 """Returns a dict of trace events, their filter ids and
510535 the values that can be filtered.
511536
@@ -521,8 +546,8 @@ class TracepointProvider(Provider): · 9 unmodified lines
521546 filters['kvm_exit'] = ('exit_reason', ARCH.exit_reasons)
522547 return filters
523548
524− def get_available_fields(self):
525− """Returns a list of available event's of format 'event name(filter
549+ def _get_available_fields(self):
550+ """Returns a list of available events of format 'event name(filter
526551 name)'.
527552
528553 All available events have directories under
@@ -549,11 +574,12 @@ class TracepointProvider(Provider): · 20 unmodified lines
549574
550575 def update_fields(self, fields_filter):
551576 """Refresh fields, applying fields_filter"""
552− self.fields = [field for field in self.get_available_fields()
553− if self.is_field_wanted(fields_filter, field)]
577+ self.fields = [field for field in self._get_available_fields()
578+ if self.is_field_wanted(fields_filter, field) or
579+ ARCH.tracepoint_is_child(field)]
554580
555581 @staticmethod
556− def get_online_cpus():
582+ def _get_online_cpus():
557583 """Returns a list of cpu id integers."""
558584 def parse_int_list(list_string):
559585 """Returns an int list from a string of comma separated integers and
@@ -575,17 +601,17 @@ class TracepointProvider(Provider): · 15 unmodified lines
575601 cpu_string = cpu_list.readline()
576602 return parse_int_list(cpu_string)
577603
578− def setup_traces(self):
604+ def _setup_traces(self):
579605 """Creates all event and group objects needed to be able to retrieve
580606 data."""
581− fields = self.get_available_fields()
607+ fields = self._get_available_fields()
582608 if self._pid > 0:
583609 # Fetch list of all threads of the monitored pid, as qemu
584610 # starts a thread for each vcpu.
585611 path = os.path.join('/proc', str(self._pid), 'task')
586612 groupids = self.walkdir(path)[1]
587613 else:
588− groupids = self.get_online_cpus()
614+ groupids = self._get_online_cpus()
589615
590616 # The constant is needed as a buffer for python libs, std
591617 # streams and other files that the script opens.
@@ -663,7 +689,7 @@ class TracepointProvider(Provider): · 71 unmodified lines
663689 # The garbage collector will get rid of all Event/Group
664690 # objects and open files after removing the references.
665691 self.group_leaders = []
666− self.setup_traces()
692+ self._setup_traces()
667693 self.fields = self._fields
668694
669695 def read(self, by_guest=0):
@@ -671,8 +697,12 @@ class TracepointProvider(Provider): · 1 unmodified line
671697 ret = defaultdict(int)
672698 for group in self.group_leaders:
673699 for name, val in group.read().items():
674− if name in self._fields:
675− ret[name] += val
700+ if name not in self._fields:
701+ continue
702+ parent = ARCH.tracepoint_is_child(name)
703+ if parent:
704+ name += ' ' + parent
705+ ret[name] += val
676706 return ret
677707
678708 def reset(self):
@@ -690,11 +720,11 @@ class DebugfsProvider(Provider): · 11 unmodified lines
690720 self._baseline = {}
691721 self.do_read = True
692722 self.paths = []
693− self.pid = pid
723+ super(DebugfsProvider, self).__init__(pid)
694724 if include_past:
695− self.restore()
725+ self._restore()
696726
697− def get_available_fields(self):
727+ def _get_available_fields(self):
698728 """"Returns a list of available fields.
699729
700730 The fields are all available KVM debugfs files
@@ -704,8 +734,9 @@ class DebugfsProvider(Provider): · 3 unmodified lines
704734
705735 def update_fields(self, fields_filter):
706736 """Refresh fields, applying fields_filter"""
707− self._fields = [field for field in self.get_available_fields()
708− if self.is_field_wanted(fields_filter, field)]
737+ self._fields = [field for field in self._get_available_fields()
738+ if self.is_field_wanted(fields_filter, field) or
739+ ARCH.debugfs_is_child(field)]
709740
710741 @property
711742 def fields(self):
@@ -758,7 +789,7 @@ class DebugfsProvider(Provider): · 46 unmodified lines
758789 paths.append(dir)
759790 for path in paths:
760791 for field in self._fields:
761− value = self.read_field(field, path)
792+ value = self._read_field(field, path)
762793 key = path + field
763794 if reset == 1:
764795 self._baseline[key] = value
@@ -766,20 +797,21 @@ class DebugfsProvider(Provider): · 1 unmodified line
766797 self._baseline[key] = 0
767798 if self._baseline.get(key, -1) == -1:
768799 self._baseline[key] = value
769− increment = (results.get(field, 0) + value -
770− self._baseline.get(key, 0))
771− if by_guest:
772− pid = key.split('-')[0]
773− if pid in results:
774− results[pid] += increment
775− else:
776− results[pid] = increment
800+ parent = ARCH.debugfs_is_child(field)
801+ if parent:
802+ field = field + ' ' + parent
803+ else:
804+ if by_guest:
805+ field = key.split('-')[0] # set 'field' to 'pid'
806+ increment = value - self._baseline.get(key, 0)
807+ if field in results:
808+ results[field] += increment
777809 else:
778810 results[field] = increment
779811
780812 return results
781813
782− def read_field(self, field, path):
814+ def _read_field(self, field, path):
783815 """Returns the value of a single field from a specific VM."""
784816 try:
785817 return int(open(os.path.join(PATH_DEBUGFS_KVM,
@@ -794,12 +826,15 @@ class DebugfsProvider(Provider): · 8 unmodified lines
794826 self._baseline = {}
795827 self.read(1)
796828
797− def restore(self):
829+ def _restore(self):
798830 """Reset field counters"""
799831 self._baseline = {}
800832 self.read(2)
801833
802834
835+EventStat = namedtuple('EventStat', ['value', 'delta'])
836+
837+
803838 class Stats(object):
804839 """Manages the data providers and the data they provide.
805840
@@ -808,13 +843,13 @@ class Stats(object): · 2 unmodified lines
808843
809844 """
810845 def __init__(self, options):
811− self.providers = self.get_providers(options)
846+ self.providers = self._get_providers(options)
812847 self._pid_filter = options.pid
813848 self._fields_filter = options.fields
814849 self.values = {}
850+ self._child_events = False
815851
816− @staticmethod
817− def get_providers(options):
852+ def _get_providers(self, options):
818853 """Returns a list of data providers depending on the passed options."""
819854 providers = []
820855
@@ -826,7 +861,7 @@ class Stats(object): · 5 unmodified lines
826861
827862 return providers
828863
829− def update_provider_filters(self):
864+ def _update_provider_filters(self):
830865 """Propagates fields filters to providers."""
831866 # As we reset the counters when updating the fields we can
832867 # also clear the cache of old values.
@@ -847,7 +882,7 @@ class Stats(object): · 14 unmodified lines
847882 def fields_filter(self, fields_filter):
848883 if fields_filter != self._fields_filter:
849884 self._fields_filter = fields_filter
850− self.update_provider_filters()
885+ self._update_provider_filters()
851886
852887 @property
853888 def pid_filter(self):
@@ -861,16 +896,33 @@ class Stats(object): · 7 unmodified lines
861896 for provider in self.providers:
862897 provider.pid = self._pid_filter
863898
899+ @property
900+ def child_events(self):
901+ return self._child_events
902+
903+ @child_events.setter
904+ def child_events(self, val):
905+ self._child_events = val
906+ for provider in self.providers:
907+ provider.child_events = val
908+
864909 def get(self, by_guest=0):
865910 """Returns a dict with field -> (value, delta to last value) of all
866− provider data."""
911+ provider data.
912+ Key formats:
913+ * plain: 'key' is event name
914+ * child-parent: 'key' is in format '<child> <parent>'
915+ * pid: 'key' is the pid of the guest, and the record contains the
916+ aggregated event data
917+ These formats are generated by the providers, and handled in class TUI.
918+ """
867919 for provider in self.providers:
868920 new = provider.read(by_guest=by_guest)
869− for key in new if by_guest else provider.fields:
870− oldval = self.values.get(key, (0, 0))[0]
921+ for key in new:
922+ oldval = self.values.get(key, EventStat(0, 0)).value
871923 newval = new.get(key, 0)
872924 newdelta = newval - oldval
873− self.values[key] = (newval, newdelta)
925+ self.values[key] = EventStat(newval, newdelta)
874926 return self.values
875927
876928 def toggle_display_guests(self, to_pid):
@@ -899,10 +951,10 @@ class Stats(object): · 22 unmodified lines
899951 self.get(to_pid)
900952 return 0
901953
954+
902955 DELAY_DEFAULT = 3.0
903956 MAX_GUEST_NAME_LEN = 48
904957 MAX_REGEX_LEN = 44
905−DEFAULT_REGEX = r'^[^\(]*$'
906958 SORT_DEFAULT = 0
907959
908960
@@ -969,7 +1021,7 @@ class Tui(object): · 60 unmodified lines
9691021
9701022 return res
9711023
972− def print_all_gnames(self, row):
1024+ def _print_all_gnames(self, row):
9731025 """Print a list of all running guests along with their pids."""
9741026 self.screen.addstr(row, 2, '%8s %-60s' %
9751027 ('Pid', 'Guest Name (fuzzy list, might be '
@@ -1032,19 +1084,13 @@ class Tui(object): · 56 unmodified lines
10321084
10331085 return name
10341086
1035− def update_drilldown(self):
1036− """Sets or removes a filter that only allows fields without braces."""
1037− if not self.stats.fields_filter:
1038− self.stats.fields_filter = DEFAULT_REGEX
1039−
1040− elif self.stats.fields_filter == DEFAULT_REGEX:
1041− self.stats.fields_filter = None
1042−
1043− def update_pid(self, pid):
1087+ def _update_pid(self, pid):
10441088 """Propagates pid selection to stats object."""
1089+ self.screen.addstr(4, 1, 'Updating pid filter...')
1090+ self.screen.refresh()
10451091 self.stats.pid_filter = pid
10461092
1047− def refresh_header(self, pid=None):
1093+ def _refresh_header(self, pid=None):
10481094 """Refreshes the header."""
10491095 if pid is None:
10501096 pid = self.stats.pid_filter
@@ -1059,8 +1105,7 @@ class Tui(object): · 8 unmodified lines
10591105 .format(pid, gname), curses.A_BOLD)
10601106 else:
10611107 self.screen.addstr(0, 0, 'kvm statistics - summary', curses.A_BOLD)
1062− if self.stats.fields_filter and self.stats.fields_filter \
1063− != DEFAULT_REGEX:
1108+ if self.stats.fields_filter:
10641109 regex = self.stats.fields_filter
10651110 if len(regex) > MAX_REGEX_LEN:
10661111 regex = regex[:MAX_REGEX_LEN] + '...'
@@ -1075,56 +1120,99 @@ class Tui(object): · 8 unmodified lines
10751120 self.screen.addstr(4, 1, 'Collecting data...')
10761121 self.screen.refresh()
10771122
1078− def refresh_body(self, sleeptime):
1123+ def _refresh_body(self, sleeptime):
1124+ def is_child_field(field):
1125+ return field.find('(') != -1
1126+
1127+ def insert_child(sorted_items, child, values, parent):
1128+ num = len(sorted_items)
1129+ for i in range(0, num):
1130+ # only add child if parent is present
1131+ if parent.startswith(sorted_items[i][0]):
1132+ sorted_items.insert(i + 1, (' ' + child, values))
1133+
1134+ def get_sorted_events(self, stats):
1135+ """ separate parent and child events """
1136+ if self._sorting == SORT_DEFAULT:
1137+ def sortkey((_k, v)):
1138+ # sort by (delta value, overall value)
1139+ return (v.delta, v.value)
1140+ else:
1141+ def sortkey((_k, v)):
1142+ # sort by overall value
1143+ return v.value
1144+
1145+ childs = []
1146+ sorted_items = []
1147+ # we can't rule out child events to appear prior to parents even
1148+ # when sorted - separate out all children first, and add in later
1149+ for key, values in sorted(stats.items(), key=sortkey,
1150+ reverse=True):
1151+ if values == (0, 0):
1152+ continue
1153+ if key.find(' ') != -1:
1154+ if not self.stats.child_events:
1155+ continue
1156+ childs.insert(0, (key, values))
1157+ else:
1158+ sorted_items.append((key, values))
1159+ if self.stats.child_events:
1160+ for key, values in childs:
1161+ (child, parent) = key.split(' ')
1162+ insert_child(sorted_items, child, values, parent)
1163+
1164+ return sorted_items
1165+
10791166 row = 3
10801167 self.screen.move(row, 0)
10811168 self.screen.clrtobot()
10821169 stats = self.stats.get(self._display_guests)
1083−
1084− def sortCurAvg(x):
1085− # sort by current events if available
1086− if stats[x][1]:
1087− return (-stats[x][1], -stats[x][0])
1170+ total = 0.
1171+ ctotal = 0.
1172+ for key, values in stats.items():
1173+ if self._display_guests:
1174+ if self.get_gname_from_pid(key):
1175+ total += values.value
1176+ continue
1177+ if not key.find(' ') != -1:
1178+ total += values.value
10881179 else:
1089− return (0, -stats[x][0])
1180+ ctotal += values.value
1181+ if total == 0.:
1182+ # we don't have any fields, or all non-child events are filtered
1183+ total = ctotal
10901184
1091− def sortTotal(x):
1092− # sort by totals
1093− return (0, -stats[x][0])
1094− total = 0.
1095− for key in stats.keys():
1096− if key.find('(') is -1:
1097− total += stats[key][0]
1098− if self._sorting == SORT_DEFAULT:
1099− sortkey = sortCurAvg
1100− else:
1101− sortkey = sortTotal
1185+ # print events
11021186 tavg = 0
1103− for key in sorted(stats.keys(), key=sortkey):
1104− if row >= self.screen.getmaxyx()[0] - 1:
1105− break
1106− values = stats[key]
1107− if not values[0] and not values[1]:
1187+ tcur = 0
1188+ for key, values in get_sorted_events(self, stats):
1189+ if row >= self.screen.getmaxyx()[0] - 1 or values == (0, 0):
11081190 break
1109− if values[0] is not None:
1110− cur = int(round(values[1] / sleeptime)) if values[1] else ''
1111− if self._display_guests:
1112− key = self.get_gname_from_pid(key)
1113− self.screen.addstr(row, 1, '%-40s %10d%7.1f %8s' %
1114− (key, values[0], values[0] * 100 / total,
1115− cur))
1116− if cur is not '' and key.find('(') is -1:
1117− tavg += cur
1191+ if self._display_guests:
1192+ key = self.get_gname_from_pid(key)
1193+ if not key:
1194+ continue
1195+ cur = int(round(values.delta / sleeptime)) if values.delta else ''
1196+ if key[0] != ' ':
1197+ if values.delta:
1198+ tcur += values.delta
1199+ ptotal = values.value
1200+ ltotal = total
1201+ else:
1202+ ltotal = ptotal
1203+ self.screen.addstr(row, 1, '%-40s %10d%7.1f %8s' % (key,
1204+ values.value,
1205+ values.value * 100 / float(ltotal), cur))
11181206 row += 1
11191207 if row == 3:
11201208 self.screen.addstr(4, 1, 'No matching events reported yet')
1121− else:
1209+ if row > 4:
1210+ tavg = int(round(tcur / sleeptime)) if tcur > 0 else ''
11221211 self.screen.addstr(row, 1, '%-40s %10d %8s' %
1123− ('Total', total, tavg if tavg else ''),
1124− curses.A_BOLD)
1212+ ('Total', total, tavg), curses.A_BOLD)
11251213 self.screen.refresh()
11261214
1127− def show_msg(self, text):
1215+ def _show_msg(self, text):
11281216 """Display message centered text and exit on key press"""
11291217 hint = 'Press any key to continue'
11301218 curses.cbreak()
@@ -1139,16 +1227,16 @@ class Tui(object): · 8 unmodified lines
11391227 curses.A_STANDOUT)
11401228 self.screen.getkey()
11411229
1142− def show_help_interactive(self):
1230+ def _show_help_interactive(self):
11431231 """Display help with list of interactive commands"""
11441232 msg = (' b toggle events by guests (debugfs only, honors'
11451233 ' filters)',
11461234 ' c clear filter',
11471235 ' f filter by regular expression',
1148− ' g filter by guest name',
1236+ ' g filter by guest name/PID',
11491237 ' h display interactive commands reference',
11501238 ' o toggle sorting order (Total vs CurAvg/s)',
1151− ' p filter by PID',
1239+ ' p filter by guest name/PID',
11521240 ' q quit',
11531241 ' r reset stats',
11541242 ' s set update interval',
@@ -1165,14 +1253,15 @@ class Tui(object): · 10 unmodified lines
11651253 self.screen.addstr(row, 0, line)
11661254 row += 1
11671255 self.screen.getkey()
1168− self.refresh_header()
1256+ self._refresh_header()
11691257
1170− def show_filter_selection(self):
1258+ def _show_filter_selection(self):
11711259 """Draws filter selection mask.
11721260
11731261 Asks for a valid regex and sets the fields filter accordingly.
11741262
11751263 """
1264+ msg = ''
11761265 while True:
11771266 self.screen.erase()
11781267 self.screen.addstr(0, 0,
@@ -1181,61 +1270,25 @@ class Tui(object): · 2 unmodified lines
11811270 self.screen.addstr(2, 0,
11821271 "Current regex: {0}"
11831272 .format(self.stats.fields_filter))
1273+ self.screen.addstr(5, 0, msg)
11841274 self.screen.addstr(3, 0, "New regex: ")
11851275 curses.echo()
11861276 regex = self.screen.getstr().decode(ENCODING)
11871277 curses.noecho()
11881278 if len(regex) == 0:
1189− self.stats.fields_filter = DEFAULT_REGEX
1190− self.refresh_header()
1279+ self.stats.fields_filter = ''
1280+ self._refresh_header()
11911281 return
11921282 try:
11931283 re.compile(regex)
11941284 self.stats.fields_filter = regex
1195− self.refresh_header()
1285+ self._refresh_header()
11961286 return
11971287 except re.error:
1288+ msg = '"' + regex + '": Not a valid regular expression'
11981289 continue
11991290
1200− def show_vm_selection_by_pid(self):
1201− """Draws PID selection mask.
1202−
1203− Asks for a pid until a valid pid or 0 has been entered.
1204−
1205− """
1206− msg = ''
1207− while True:
1208− self.screen.erase()
1209− self.screen.addstr(0, 0,
1210− 'Show statistics for specific pid.',
1211− curses.A_BOLD)
1212− self.screen.addstr(1, 0,
1213− 'This might limit the shown data to the trace '
1214− 'statistics.')
1215− self.screen.addstr(5, 0, msg)
1216− self.print_all_gnames(7)
1217−
1218− curses.echo()
1219− self.screen.addstr(3, 0, "Pid [0 or pid]: ")
1220− pid = self.screen.getstr().decode(ENCODING)
1221− curses.noecho()
1222−
1223− try:
1224− if len(pid) > 0:
1225− pid = int(pid)
1226− if pid != 0 and not os.path.isdir(os.path.join('/proc/',
1227− str(pid))):
1228− msg = '"' + str(pid) + '": Not a running process'
1229− continue
1230− else:
1231− pid = 0
1232− self.refresh_header(pid)
1233− self.update_pid(pid)
1234− break
1235− except ValueError:
1236− msg = '"' + str(pid) + '": Not a valid pid'
1237−
1238− def show_set_update_interval(self):
1291+ def _show_set_update_interval(self):
12391292 """Draws update interval selection mask."""
12401293 msg = ''
12411294 while True:
@@ -1265,60 +1318,67 @@ class Tui(object): · 23 unmodified lines
12651318
12661319 except ValueError:
12671320 msg = '"' + str(val) + '": Invalid value'
1268− self.refresh_header()
1321+ self._refresh_header()
12691322
1270− def show_vm_selection_by_guest_name(self):
1323+ def _show_vm_selection_by_guest(self):
12711324 """Draws guest selection mask.
12721325
1273− Asks for a guest name until a valid guest name or '' is entered.
1326+ Asks for a guest name or pid until a valid guest name or '' is entered.
12741327
12751328 """
12761329 msg = ''
12771330 while True:
12781331 self.screen.erase()
12791332 self.screen.addstr(0, 0,
1280− 'Show statistics for specific guest.',
1333+ 'Show statistics for specific guest or pid.',
12811334 curses.A_BOLD)
12821335 self.screen.addstr(1, 0,
12831336 'This might limit the shown data to the trace '
12841337 'statistics.')
12851338 self.screen.addstr(5, 0, msg)
1286− self.print_all_gnames(7)
1339+ self._print_all_gnames(7)
12871340 curses.echo()
1288− self.screen.addstr(3, 0, "Guest [ENTER or guest]: ")
1289− gname = self.screen.getstr().decode(ENCODING)
1341+ curses.curs_set(1)
1342+ self.screen.addstr(3, 0, "Guest or pid [ENTER exits]: ")
1343+ guest = self.screen.getstr().decode(ENCODING)
12901344 curses.noecho()
12911345
1292− if not gname:
1293− self.refresh_header(0)
1294− self.update_pid(0)
1346+ pid = 0
1347+ if not guest or guest == '0':
12951348 break
1296− else:
1297− pids = []
1298− try:
1299− pids = self.get_pid_from_gname(gname)
1300− except:
1301− msg = '"' + gname + '": Internal error while searching, ' \
1302− 'use pid filter instead'
1303− continue
1304− if len(pids) == 0:
1305− msg = '"' + gname + '": Not an active guest'
1349+ if guest.isdigit():
1350+ if not os.path.isdir(os.path.join('/proc/', guest)):
1351+ msg = '"' + guest + '": Not a running process'
13061352 continue
1307− if len(pids) > 1:
1308− msg = '"' + gname + '": Multiple matches found, use pid ' \
1309− 'filter instead'
1310− continue
1311− self.refresh_header(pids[0])
1312− self.update_pid(pids[0])
1353+ pid = int(guest)
13131354 break
1355+ pids = []
1356+ try:
1357+ pids = self.get_pid_from_gname(guest)
1358+ except:
1359+ msg = '"' + guest + '": Internal error while searching, ' \
1360+ 'use pid filter instead'
1361+ continue
1362+ if len(pids) == 0:
1363+ msg = '"' + guest + '": Not an active guest'
1364+ continue
1365+ if len(pids) > 1:
1366+ msg = '"' + guest + '": Multiple matches found, use pid ' \
1367+ 'filter instead'
1368+ continue
1369+ pid = pids[0]
1370+ break
1371+ curses.curs_set(0)
1372+ self._refresh_header(pid)
1373+ self._update_pid(pid)
13141374
13151375 def show_stats(self):
13161376 """Refreshes the screen and processes user input."""
13171377 sleeptime = self._delay_initial
1318− self.refresh_header()
1378+ self._refresh_header()
13191379 start = 0.0 # result based on init value never appears on screen
13201380 while True:
1321− self.refresh_body(time.time() - start)
1381+ self._refresh_body(time.time() - start)
13221382 curses.halfdelay(int(sleeptime * 10))
13231383 start = time.time()
13241384 sleeptime = self._delay_regular
@@ -1327,47 +1387,39 @@ class Tui(object): · 2 unmodified lines
13271387 if char == 'b':
13281388 self._display_guests = not self._display_guests
13291389 if self.stats.toggle_display_guests(self._display_guests):
1330− self.show_msg(['Command not available with tracepoints'
1331− ' enabled', 'Restart with debugfs only '
1332− '(see option \'-d\') and try again!'])
1390+ self._show_msg(['Command not available with '
1391+ 'tracepoints enabled', 'Restart with '
1392+ 'debugfs only (see option \'-d\') and '
1393+ 'try again!'])
13331394 self._display_guests = not self._display_guests
1334− self.refresh_header()
1395+ self._refresh_header()
13351396 if char == 'c':
1336− self.stats.fields_filter = DEFAULT_REGEX
1337− self.refresh_header(0)
1338− self.update_pid(0)
1397+ self.stats.fields_filter = ''
1398+ self._refresh_header(0)
1399+ self._update_pid(0)
13391400 if char == 'f':
13401401 curses.curs_set(1)
1341− self.show_filter_selection()
1402+ self._show_filter_selection()
13421403 curses.curs_set(0)
13431404 sleeptime = self._delay_initial
1344− if char == 'g':
1345− curses.curs_set(1)
1346− self.show_vm_selection_by_guest_name()
1347− curses.curs_set(0)
1405+ if char == 'g' or char == 'p':
1406+ self._show_vm_selection_by_guest()
13481407 sleeptime = self._delay_initial
13491408 if char == 'h':
1350− self.show_help_interactive()
1409+ self._show_help_interactive()
13511410 if char == 'o':
13521411 self._sorting = not self._sorting
1353− if char == 'p':
1354− curses.curs_set(1)
1355− self.show_vm_selection_by_pid()
1356− curses.curs_set(0)
1357− sleeptime = self._delay_initial
13581412 if char == 'q':
13591413 break
13601414 if char == 'r':
13611415 self.stats.reset()
13621416 if char == 's':
13631417 curses.curs_set(1)
1364− self.show_set_update_interval()
1418+ self._show_set_update_interval()
13651419 curses.curs_set(0)
13661420 sleeptime = self._delay_initial
13671421 if char == 'x':
1368− self.update_drilldown()
1369− # prevents display of current values on next refresh
1370− self.stats.get(self._display_guests)
1422+ self.stats.child_events = not self.stats.child_events
13711423 except KeyboardInterrupt:
13721424 break
13731425 except curses.error:
@@ -1380,9 +1432,9 @@ def batch(stats): · 6 unmodified lines
13801432 s = stats.get()
13811433 time.sleep(1)
13821434 s = stats.get()
1383− for key in sorted(s.keys()):
1384− values = s[key]
1385− print('%-42s%10d%10d' % (key, values[0], values[1]))
1435+ for key, values in sorted(s.items()):
1436+ print('%-42s%10d%10d' % (key.split(' ')[0], values.value,
1437+ values.delta))
13861438 except KeyboardInterrupt:
13871439 pass
13881440
@@ -1392,14 +1444,14 @@ def log(stats): · 3 unmodified lines
13921444 keys = sorted(stats.get().keys())
13931445
13941446 def banner():
1395− for k in keys:
1396− print(k, end=' ')
1447+ for key in keys:
1448+ print(key.split(' ')[0], end=' ')
13971449 print()
13981450
13991451 def statline():
14001452 s = stats.get()
1401− for k in keys:
1402− print(' %9d' % s[k][1], end=' ')
1453+ for key in keys:
1454+ print(' %9d' % s[key].delta, end=' ')
14031455 print()
14041456 line = 0
14051457 banner_repeat = 20
@@ -1504,7 +1556,7 @@ Press any other key to refresh statistics immediately. · 98 unmodified lines
15041556 )
15051557 optparser.add_option('-f', '--fields',
15061558 action='store',
1507− default=DEFAULT_REGEX,
1559+ default='',
15081560 dest='fields',
15091561 help='''fields to display (regex)
15101562 "-f help" for a list of available events''',
@@ -1539,17 +1591,6 @@ Press any other key to refresh statistics immediately. · 28 unmodified lines
15391591
15401592 def check_access(options):
15411593 """Exits if the current user can't access all needed directories."""
1542− if not os.path.exists('/sys/kernel/debug'):
1543− sys.stderr.write('Please enable CONFIG_DEBUG_FS in your kernel.')
1544− sys.exit(1)
1545−
1546− if not os.path.exists(PATH_DEBUGFS_KVM):
1547− sys.stderr.write("Please make sure, that debugfs is mounted and "
1548− "readable by the current user:\n"
1549− "('mount -t debugfs debugfs /sys/kernel/debug')\n"
1550− "Also ensure, that the kvm modules are loaded.\n")
1551− sys.exit(1)
1552−
15531594 if not os.path.exists(PATH_DEBUGFS_TRACING) and (options.tracepoints or
15541595 not options.debugfs):
15551596 sys.stderr.write("Please enable CONFIG_TRACING in your kernel "
@@ -1567,7 +1608,33 @@ def check_access(options): · 11 unmodified lines
15671608 return options
15681609
15691610
1611+def assign_globals():
1612+ global PATH_DEBUGFS_KVM
1613+ global PATH_DEBUGFS_TRACING
1614+
1615+ debugfs = ''
1616+ for line in file('/proc/mounts'):
1617+ if line.split(' ')[0] == 'debugfs':
1618+ debugfs = line.split(' ')[1]
1619+ break
1620+ if debugfs == '':
1621+ sys.stderr.write("Please make sure that CONFIG_DEBUG_FS is enabled in "
1622+ "your kernel, mounted and\nreadable by the current "
1623+ "user:\n"
1624+ "('mount -t debugfs debugfs /sys/kernel/debug')\n")
1625+ sys.exit(1)
1626+
1627+ PATH_DEBUGFS_KVM = os.path.join(debugfs, 'kvm')
1628+ PATH_DEBUGFS_TRACING = os.path.join(debugfs, 'tracing')
1629+
1630+ if not os.path.exists(PATH_DEBUGFS_KVM):
1631+ sys.stderr.write("Please make sure that CONFIG_KVM is enabled in "
1632+ "your kernel and that the modules are loaded.\n")
1633+ sys.exit(1)
1634+
1635+
15701636 def main():
1637+ assign_globals()
15711638 options = get_options()
15721639 options = check_access(options)
15731640
tools/kvm/kvm_stat/kvm_stat.txt+2−2
@@ -35,13 +35,13 @@ INTERACTIVE COMMANDS · 34 unmodified lines
3535
3636 *f*:: filter by regular expression
3737
38−*g*:: filter by guest name
38+*g*:: filter by guest name/PID
3939
4040 *h*:: display interactive commands reference
4141
4242 *o*:: toggle sorting order (Total vs CurAvg/s)
4343
44−*p*:: filter by PID
44+*p*:: filter by guest name/PID
4545
4646 *q*:: quit
4747
virt/kvm/arm/arch_timer.c+64−52
@@ -36,6 +36,8 @@ static struct timecounter *timecounter; · 35 unmodified lines
3636 static unsigned int host_vtimer_irq;
3737 static u32 host_vtimer_irq_flags;
3838
39+static DEFINE_STATIC_KEY_FALSE(has_gic_active_state);
40+
3941 static const struct kvm_irq_level default_ptimer_irq = {
4042 .irq = 30,
4143 .level = 1,
@@ -56,6 +58,12 @@ u64 kvm_phys_timer_read(void) · 14 unmodified lines
5658 return timecounter->cc->read(timecounter->cc);
5759 }
5860
61+static inline bool userspace_irqchip(struct kvm *kvm)
62+{
63+ return static_branch_unlikely(&userspace_irqchip_in_use) &&
64+ unlikely(!irqchip_in_kernel(kvm));
65+}
66+
5967 static void soft_timer_start(struct hrtimer *hrt, u64 ns)
6068 {
6169 hrtimer_start(hrt, ktime_add_ns(ktime_get(), ns),
@@ -69,25 +77,6 @@ static void soft_timer_cancel(struct hrtimer *hrt, struct work_struct *work) · 7 unmodified lines
6977 cancel_work_sync(work);
7078 }
7179
72−static void kvm_vtimer_update_mask_user(struct kvm_vcpu *vcpu)
73−{
74− struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
75−
76− /*
77− * When using a userspace irqchip with the architected timers, we must
78− * prevent continuously exiting from the guest, and therefore mask the
79− * physical interrupt by disabling it on the host interrupt controller
80− * when the virtual level is high, such that the guest can make
81− * forward progress. Once we detect the output level being
82− * de-asserted, we unmask the interrupt again so that we exit from the
83− * guest when the timer fires.
84− */
85− if (vtimer->irq.level)
86− disable_percpu_irq(host_vtimer_irq);
87− else
88− enable_percpu_irq(host_vtimer_irq, 0);
89−}
90−
9180 static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
9281 {
9382 struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
@@ -106,9 +95,9 @@ static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id) · 12 unmodified lines
10695 if (kvm_timer_should_fire(vtimer))
10796 kvm_timer_update_irq(vcpu, true, vtimer);
10897
109− if (static_branch_unlikely(&userspace_irqchip_in_use) &&
110− unlikely(!irqchip_in_kernel(vcpu->kvm)))
111− kvm_vtimer_update_mask_user(vcpu);
98+ if (userspace_irqchip(vcpu->kvm) &&
99+ !static_branch_unlikely(&has_gic_active_state))
100+ disable_percpu_irq(host_vtimer_irq);
112101
113102 return IRQ_HANDLED;
114103 }
@@ -290,8 +279,7 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level, · 175 unmodified lines
290279 trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_ctx->irq.irq,
291280 timer_ctx->irq.level);
292281
293− if (!static_branch_unlikely(&userspace_irqchip_in_use) ||
294− likely(irqchip_in_kernel(vcpu->kvm))) {
282+ if (!userspace_irqchip(vcpu->kvm)) {
295283 ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
296284 timer_ctx->irq.irq,
297285 timer_ctx->irq.level,
@@ -350,12 +338,6 @@ static void kvm_timer_update_state(struct kvm_vcpu *vcpu) · 52 unmodified lines
350338 phys_timer_emulate(vcpu);
351339 }
352340
353−static void __timer_snapshot_state(struct arch_timer_context *timer)
354−{
355− timer->cnt_ctl = read_sysreg_el0(cntv_ctl);
356− timer->cnt_cval = read_sysreg_el0(cntv_cval);
357−}
358−
359341 static void vtimer_save_state(struct kvm_vcpu *vcpu)
360342 {
361343 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
@@ -367,8 +349,10 @@ static void vtimer_save_state(struct kvm_vcpu *vcpu) · 5 unmodified lines
367349 if (!vtimer->loaded)
368350 goto out;
369351
370− if (timer->enabled)
371− __timer_snapshot_state(vtimer);
352+ if (timer->enabled) {
353+ vtimer->cnt_ctl = read_sysreg_el0(cntv_ctl);
354+ vtimer->cnt_cval = read_sysreg_el0(cntv_cval);
355+ }
372356
373357 /* Disable the virtual timer */
374358 write_sysreg_el0(0, cntv_ctl);
@@ -460,23 +444,43 @@ static void set_cntvoff(u64 cntvoff) · 85 unmodified lines
460444 kvm_call_hyp(__kvm_timer_set_cntvoff, low, high);
461445 }
462446
463−static void kvm_timer_vcpu_load_vgic(struct kvm_vcpu *vcpu)
447+static inline void set_vtimer_irq_phys_active(struct kvm_vcpu *vcpu, bool active)
448+{
449+ int r;
450+ r = irq_set_irqchip_state(host_vtimer_irq, IRQCHIP_STATE_ACTIVE, active);
451+ WARN_ON(r);
452+}
453+
454+static void kvm_timer_vcpu_load_gic(struct kvm_vcpu *vcpu)
464455 {
465456 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
466457 bool phys_active;
467− int ret;
468458
469− phys_active = kvm_vgic_map_is_active(vcpu, vtimer->irq.irq);
470−
471− ret = irq_set_irqchip_state(host_vtimer_irq,
472− IRQCHIP_STATE_ACTIVE,
473− phys_active);
474− WARN_ON(ret);
459+ if (irqchip_in_kernel(vcpu->kvm))
460+ phys_active = kvm_vgic_map_is_active(vcpu, vtimer->irq.irq);
461+ else
462+ phys_active = vtimer->irq.level;
463+ set_vtimer_irq_phys_active(vcpu, phys_active);
475464 }
476465
477−static void kvm_timer_vcpu_load_user(struct kvm_vcpu *vcpu)
466+static void kvm_timer_vcpu_load_nogic(struct kvm_vcpu *vcpu)
478467 {
479− kvm_vtimer_update_mask_user(vcpu);
468+ struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
469+
470+ /*
471+ * When using a userspace irqchip with the architected timers and a
472+ * host interrupt controller that doesn't support an active state, we
473+ * must still prevent continuously exiting from the guest, and
474+ * therefore mask the physical interrupt by disabling it on the host
475+ * interrupt controller when the virtual level is high, such that the
476+ * guest can make forward progress. Once we detect the output level
477+ * being de-asserted, we unmask the interrupt again so that we exit
478+ * from the guest when the timer fires.
479+ */
480+ if (vtimer->irq.level)
481+ disable_percpu_irq(host_vtimer_irq);
482+ else
483+ enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
480484 }
481485
482486 void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)
@@ -487,10 +491,10 @@ void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu) · 4 unmodified lines
487491 if (unlikely(!timer->enabled))
488492 return;
489493
490− if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
491− kvm_timer_vcpu_load_user(vcpu);
494+ if (static_branch_likely(&has_gic_active_state))
495+ kvm_timer_vcpu_load_gic(vcpu);
492496 else
493− kvm_timer_vcpu_load_vgic(vcpu);
497+ kvm_timer_vcpu_load_nogic(vcpu);
494498
495499 set_cntvoff(vtimer->cntvoff);
496500
@@ -555,18 +559,24 @@ static void unmask_vtimer_irq_user(struct kvm_vcpu *vcpu) · 58 unmodified lines
555559 {
556560 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
557561
558− if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
559− __timer_snapshot_state(vtimer);
560− if (!kvm_timer_should_fire(vtimer)) {
561− kvm_timer_update_irq(vcpu, false, vtimer);
562− kvm_vtimer_update_mask_user(vcpu);
563− }
562+ if (!kvm_timer_should_fire(vtimer)) {
563+ kvm_timer_update_irq(vcpu, false, vtimer);
564+ if (static_branch_likely(&has_gic_active_state))
565+ set_vtimer_irq_phys_active(vcpu, false);
566+ else
567+ enable_percpu_irq(host_vtimer_irq, host_vtimer_irq_flags);
564568 }
565569 }
566570
567571 void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
568572 {
569− unmask_vtimer_irq_user(vcpu);
573+ struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
574+
575+ if (unlikely(!timer->enabled))
576+ return;
577+
578+ if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
579+ unmask_vtimer_irq_user(vcpu);
570580 }
571581
572582 int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)
@@ -753,6 +763,8 @@ int kvm_timer_hyp_init(bool has_gic) · 180 unmodified lines
753763 kvm_err("kvm_arch_timer: error setting vcpu affinity\n");
754764 goto out_free_irq;
755765 }
766+
767+ static_branch_enable(&has_gic_active_state);
756768 }
757769
758770 kvm_info("virtual timer IRQ%d\n", host_vtimer_irq);
virt/kvm/kvm_main.c+1−2
@@ -969,8 +969,7 @@ int __kvm_set_memory_region(struct kvm *kvm, · 968 unmodified lines
969969 /* Check for overlaps */
970970 r = -EEXIST;
971971 kvm_for_each_memslot(slot, __kvm_memslots(kvm, as_id)) {
972− if ((slot->id >= KVM_USER_MEM_SLOTS) ||
973− (slot->id == id))
972+ if (slot->id == id)
974973 continue;
975974 if (!((base_gfn + npages <= slot->base_gfn) ||
976975 (base_gfn >= slot->base_gfn + slot->npages)))