Skip to content

Commit 19d031e

Browse files
committed
Merge branch 'kvm-updates/2.6.32' of git://git.kernel.org/pub/scm/virt/kvm/kvm
* 'kvm-updates/2.6.32' of git://git.kernel.org/pub/scm/virt/kvm/kvm: KVM: add support for change_pte mmu notifiers KVM: MMU: add SPTE_HOST_WRITEABLE flag to the shadow ptes KVM: MMU: dont hold pagecount reference for mapped sptes pages KVM: Prevent overflow in KVM_GET_SUPPORTED_CPUID KVM: VMX: flush TLB with INVEPT on cpu migration KVM: fix LAPIC timer period overflow KVM: s390: fix memsize >= 4G KVM: SVM: Handle tsc in svm_get_msr/svm_set_msr correctly KVM: SVM: Fix tsc offset adjustment when running nested
2 parents e8d809c + 3da0dd4 commit 19d031e

9 files changed

Lines changed: 120 additions & 30 deletions

File tree

‎arch/s390/kvm/kvm-s390.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
5858
int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code);
5959
int kvm_s390_inject_sigp_stop(struct kvm_vcpu *vcpu, int action);
6060

61-
static inline int kvm_s390_vcpu_get_memsize(struct kvm_vcpu *vcpu)
61+
static inline long kvm_s390_vcpu_get_memsize(struct kvm_vcpu *vcpu)
6262
{
6363
return vcpu->arch.sie_block->gmslm
6464
- vcpu->arch.sie_block->gmsor

‎arch/x86/include/asm/kvm_host.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ asmlinkage void kvm_handle_fault_on_reboot(void);
796796
#define KVM_ARCH_WANT_MMU_NOTIFIER
797797
int kvm_unmap_hva(struct kvm *kvm, unsigned long hva);
798798
int kvm_age_hva(struct kvm *kvm, unsigned long hva);
799+
void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte);
799800
int cpuid_maxphyaddr(struct kvm_vcpu *vcpu);
800801
int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu);
801802
int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu);

‎arch/x86/kvm/lapic.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ static void start_apic_timer(struct kvm_lapic *apic)
664664
{
665665
ktime_t now = apic->lapic_timer.timer.base->get_time();
666666

667-
apic->lapic_timer.period = apic_get_reg(apic, APIC_TMICT) *
667+
apic->lapic_timer.period = (u64)apic_get_reg(apic, APIC_TMICT) *
668668
APIC_BUS_CYCLE_NS * apic->divide_count;
669669
atomic_set(&apic->lapic_timer.pending, 0);
670670

‎arch/x86/kvm/mmu.c‎

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ module_param(oos_shadow, bool, 0644);
156156
#define CREATE_TRACE_POINTS
157157
#include "mmutrace.h"
158158

159+
#define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
160+
159161
#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
160162

161163
struct kvm_rmap_desc {
@@ -634,9 +636,7 @@ static void rmap_remove(struct kvm *kvm, u64 *spte)
634636
if (*spte & shadow_accessed_mask)
635637
kvm_set_pfn_accessed(pfn);
636638
if (is_writeble_pte(*spte))
637-
kvm_release_pfn_dirty(pfn);
638-
else
639-
kvm_release_pfn_clean(pfn);
639+
kvm_set_pfn_dirty(pfn);
640640
rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], sp->role.level);
641641
if (!*rmapp) {
642642
printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
@@ -748,7 +748,7 @@ static int rmap_write_protect(struct kvm *kvm, u64 gfn)
748748
return write_protected;
749749
}
750750

751-
static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
751+
static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp, u64 data)
752752
{
753753
u64 *spte;
754754
int need_tlb_flush = 0;
@@ -763,8 +763,45 @@ static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
763763
return need_tlb_flush;
764764
}
765765

766-
static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
767-
int (*handler)(struct kvm *kvm, unsigned long *rmapp))
766+
static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp, u64 data)
767+
{
768+
int need_flush = 0;
769+
u64 *spte, new_spte;
770+
pte_t *ptep = (pte_t *)data;
771+
pfn_t new_pfn;
772+
773+
WARN_ON(pte_huge(*ptep));
774+
new_pfn = pte_pfn(*ptep);
775+
spte = rmap_next(kvm, rmapp, NULL);
776+
while (spte) {
777+
BUG_ON(!is_shadow_present_pte(*spte));
778+
rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
779+
need_flush = 1;
780+
if (pte_write(*ptep)) {
781+
rmap_remove(kvm, spte);
782+
__set_spte(spte, shadow_trap_nonpresent_pte);
783+
spte = rmap_next(kvm, rmapp, NULL);
784+
} else {
785+
new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
786+
new_spte |= (u64)new_pfn << PAGE_SHIFT;
787+
788+
new_spte &= ~PT_WRITABLE_MASK;
789+
new_spte &= ~SPTE_HOST_WRITEABLE;
790+
if (is_writeble_pte(*spte))
791+
kvm_set_pfn_dirty(spte_to_pfn(*spte));
792+
__set_spte(spte, new_spte);
793+
spte = rmap_next(kvm, rmapp, spte);
794+
}
795+
}
796+
if (need_flush)
797+
kvm_flush_remote_tlbs(kvm);
798+
799+
return 0;
800+
}
801+
802+
static int kvm_handle_hva(struct kvm *kvm, unsigned long hva, u64 data,
803+
int (*handler)(struct kvm *kvm, unsigned long *rmapp,
804+
u64 data))
768805
{
769806
int i, j;
770807
int retval = 0;
@@ -786,13 +823,15 @@ static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
786823
if (hva >= start && hva < end) {
787824
gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
788825

789-
retval |= handler(kvm, &memslot->rmap[gfn_offset]);
826+
retval |= handler(kvm, &memslot->rmap[gfn_offset],
827+
data);
790828

791829
for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
792830
int idx = gfn_offset;
793831
idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
794832
retval |= handler(kvm,
795-
&memslot->lpage_info[j][idx].rmap_pde);
833+
&memslot->lpage_info[j][idx].rmap_pde,
834+
data);
796835
}
797836
}
798837
}
@@ -802,10 +841,15 @@ static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
802841

803842
int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
804843
{
805-
return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
844+
return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
806845
}
807846

808-
static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
847+
void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
848+
{
849+
kvm_handle_hva(kvm, hva, (u64)&pte, kvm_set_pte_rmapp);
850+
}
851+
852+
static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp, u64 data)
809853
{
810854
u64 *spte;
811855
int young = 0;
@@ -841,13 +885,13 @@ static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
841885
gfn = unalias_gfn(vcpu->kvm, gfn);
842886
rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
843887

844-
kvm_unmap_rmapp(vcpu->kvm, rmapp);
888+
kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
845889
kvm_flush_remote_tlbs(vcpu->kvm);
846890
}
847891

848892
int kvm_age_hva(struct kvm *kvm, unsigned long hva)
849893
{
850-
return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
894+
return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
851895
}
852896

853897
#ifdef MMU_DEBUG
@@ -1756,7 +1800,7 @@ static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
17561800
unsigned pte_access, int user_fault,
17571801
int write_fault, int dirty, int level,
17581802
gfn_t gfn, pfn_t pfn, bool speculative,
1759-
bool can_unsync)
1803+
bool can_unsync, bool reset_host_protection)
17601804
{
17611805
u64 spte;
17621806
int ret = 0;
@@ -1783,6 +1827,9 @@ static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
17831827
spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
17841828
kvm_is_mmio_pfn(pfn));
17851829

1830+
if (reset_host_protection)
1831+
spte |= SPTE_HOST_WRITEABLE;
1832+
17861833
spte |= (u64)pfn << PAGE_SHIFT;
17871834

17881835
if ((pte_access & ACC_WRITE_MASK)
@@ -1828,7 +1875,8 @@ static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
18281875
unsigned pt_access, unsigned pte_access,
18291876
int user_fault, int write_fault, int dirty,
18301877
int *ptwrite, int level, gfn_t gfn,
1831-
pfn_t pfn, bool speculative)
1878+
pfn_t pfn, bool speculative,
1879+
bool reset_host_protection)
18321880
{
18331881
int was_rmapped = 0;
18341882
int was_writeble = is_writeble_pte(*sptep);
@@ -1860,7 +1908,8 @@ static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
18601908
}
18611909

18621910
if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
1863-
dirty, level, gfn, pfn, speculative, true)) {
1911+
dirty, level, gfn, pfn, speculative, true,
1912+
reset_host_protection)) {
18641913
if (write_fault)
18651914
*ptwrite = 1;
18661915
kvm_x86_ops->tlb_flush(vcpu);
@@ -1877,8 +1926,7 @@ static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
18771926
page_header_update_slot(vcpu->kvm, sptep, gfn);
18781927
if (!was_rmapped) {
18791928
rmap_count = rmap_add(vcpu, sptep, gfn);
1880-
if (!is_rmap_spte(*sptep))
1881-
kvm_release_pfn_clean(pfn);
1929+
kvm_release_pfn_clean(pfn);
18821930
if (rmap_count > RMAP_RECYCLE_THRESHOLD)
18831931
rmap_recycle(vcpu, sptep, gfn);
18841932
} else {
@@ -1909,7 +1957,7 @@ static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
19091957
if (iterator.level == level) {
19101958
mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
19111959
0, write, 1, &pt_write,
1912-
level, gfn, pfn, false);
1960+
level, gfn, pfn, false, true);
19131961
++vcpu->stat.pf_fixed;
19141962
break;
19151963
}

‎arch/x86/kvm/paging_tmpl.h‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,13 @@ static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,
273273
if (mmu_notifier_retry(vcpu, vcpu->arch.update_pte.mmu_seq))
274274
return;
275275
kvm_get_pfn(pfn);
276+
/*
277+
* we call mmu_set_spte() with reset_host_protection = true beacuse that
278+
* vcpu->arch.update_pte.pfn was fetched from get_user_pages(write = 1).
279+
*/
276280
mmu_set_spte(vcpu, spte, page->role.access, pte_access, 0, 0,
277281
gpte & PT_DIRTY_MASK, NULL, PT_PAGE_TABLE_LEVEL,
278-
gpte_to_gfn(gpte), pfn, true);
282+
gpte_to_gfn(gpte), pfn, true, true);
279283
}
280284

281285
/*
@@ -308,7 +312,7 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
308312
user_fault, write_fault,
309313
gw->ptes[gw->level-1] & PT_DIRTY_MASK,
310314
ptwrite, level,
311-
gw->gfn, pfn, false);
315+
gw->gfn, pfn, false, true);
312316
break;
313317
}
314318

@@ -558,6 +562,7 @@ static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
558562
static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
559563
{
560564
int i, offset, nr_present;
565+
bool reset_host_protection;
561566

562567
offset = nr_present = 0;
563568

@@ -595,9 +600,16 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
595600

596601
nr_present++;
597602
pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
603+
if (!(sp->spt[i] & SPTE_HOST_WRITEABLE)) {
604+
pte_access &= ~ACC_WRITE_MASK;
605+
reset_host_protection = 0;
606+
} else {
607+
reset_host_protection = 1;
608+
}
598609
set_spte(vcpu, &sp->spt[i], pte_access, 0, 0,
599610
is_dirty_gpte(gpte), PT_PAGE_TABLE_LEVEL, gfn,
600-
spte_to_pfn(sp->spt[i]), true, false);
611+
spte_to_pfn(sp->spt[i]), true, false,
612+
reset_host_protection);
601613
}
602614

603615
return !nr_present;

‎arch/x86/kvm/svm.c‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,8 @@ static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
767767
rdtscll(tsc_this);
768768
delta = vcpu->arch.host_tsc - tsc_this;
769769
svm->vmcb->control.tsc_offset += delta;
770+
if (is_nested(svm))
771+
svm->nested.hsave->control.tsc_offset += delta;
770772
vcpu->cpu = cpu;
771773
kvm_migrate_timers(vcpu);
772774
svm->asid_generation = 0;
@@ -2057,10 +2059,14 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
20572059

20582060
switch (ecx) {
20592061
case MSR_IA32_TSC: {
2060-
u64 tsc;
2062+
u64 tsc_offset;
2063+
2064+
if (is_nested(svm))
2065+
tsc_offset = svm->nested.hsave->control.tsc_offset;
2066+
else
2067+
tsc_offset = svm->vmcb->control.tsc_offset;
20612068

2062-
rdtscll(tsc);
2063-
*data = svm->vmcb->control.tsc_offset + tsc;
2069+
*data = tsc_offset + native_read_tsc();
20642070
break;
20652071
}
20662072
case MSR_K6_STAR:
@@ -2146,10 +2152,17 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
21462152

21472153
switch (ecx) {
21482154
case MSR_IA32_TSC: {
2149-
u64 tsc;
2155+
u64 tsc_offset = data - native_read_tsc();
2156+
u64 g_tsc_offset = 0;
2157+
2158+
if (is_nested(svm)) {
2159+
g_tsc_offset = svm->vmcb->control.tsc_offset -
2160+
svm->nested.hsave->control.tsc_offset;
2161+
svm->nested.hsave->control.tsc_offset = tsc_offset;
2162+
}
2163+
2164+
svm->vmcb->control.tsc_offset = tsc_offset + g_tsc_offset;
21502165

2151-
rdtscll(tsc);
2152-
svm->vmcb->control.tsc_offset = data - tsc;
21532166
break;
21542167
}
21552168
case MSR_K6_STAR:

‎arch/x86/kvm/vmx.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
709709
if (vcpu->cpu != cpu) {
710710
vcpu_clear(vmx);
711711
kvm_migrate_timers(vcpu);
712-
vpid_sync_vcpu_all(vmx);
712+
set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
713713
local_irq_disable();
714714
list_add(&vmx->local_vcpus_link,
715715
&per_cpu(vcpus_on_cpu, cpu));

‎arch/x86/kvm/x86.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,8 @@ static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
15911591

15921592
if (cpuid->nent < 1)
15931593
goto out;
1594+
if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1595+
cpuid->nent = KVM_MAX_CPUID_ENTRIES;
15941596
r = -ENOMEM;
15951597
cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
15961598
if (!cpuid_entries)

‎virt/kvm/kvm_main.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,19 @@ static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
850850

851851
}
852852

853+
static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
854+
struct mm_struct *mm,
855+
unsigned long address,
856+
pte_t pte)
857+
{
858+
struct kvm *kvm = mmu_notifier_to_kvm(mn);
859+
860+
spin_lock(&kvm->mmu_lock);
861+
kvm->mmu_notifier_seq++;
862+
kvm_set_spte_hva(kvm, address, pte);
863+
spin_unlock(&kvm->mmu_lock);
864+
}
865+
853866
static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
854867
struct mm_struct *mm,
855868
unsigned long start,
@@ -929,6 +942,7 @@ static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
929942
.invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
930943
.invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
931944
.clear_flush_young = kvm_mmu_notifier_clear_flush_young,
945+
.change_pte = kvm_mmu_notifier_change_pte,
932946
.release = kvm_mmu_notifier_release,
933947
};
934948
#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */

0 commit comments

Comments
 (0)