Skip to contentSkip to conversation

KVM: MMU: Don't flush shadow when enabling dirty tracking

PATCH Superseded Avi Kivity 2 participants 4 messages Dec 26, 2010 – Dec 27, 2010

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

PATCH 1/1 KVM: MMU: Don't flush shadow when enabling dirty tracking

Instead, drop large mappings, which were the reason we dropped shadow.

---
 arch/x86/kvm/mmu.c  |   10 ++++++----
 virt/kvm/kvm_main.c |    7 +------
 2 files changed, 7 insertions(+), 10 deletions(-)
arch/x86/kvm/mmu.c +6 -4
index 43bd5e3..1bbe2c0 100644
@@ -3444,14 +3444,16 @@ void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
 		if (!test_bit(slot, sp->slot_bitmap))
 			continue;
 
-		if (sp->role.level != PT_PAGE_TABLE_LEVEL)
-			continue;
-
 		pt = sp->spt;
-		for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
+		for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
+			if (sp->role.level != PT_PAGE_TABLE_LEVEL
+			    && is_large_pte(pt[i]))
+				drop_spte(kvm, &pt[i],
+					  shadow_trap_nonpresent_pte);
 			/* avoid RMW */
 			if (is_writable_pte(pt[i]))
 				update_spte(&pt[i], pt[i] & ~PT_WRITABLE_MASK);
+		}
 	}
 	kvm_flush_remote_tlbs(kvm);
 }
virt/kvm/kvm_main.c +1 -6
index b1b6cbb..b3bfeb8 100644
@@ -586,7 +586,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
 			    struct kvm_userspace_memory_region *mem,
 			    int user_alloc)
 {
-	int r, flush_shadow = 0;
+	int r;
 	gfn_t base_gfn;
 	unsigned long npages;
 	unsigned long i;
@@ -706,8 +706,6 @@ skip_lpage:
 		if (kvm_create_dirty_bitmap(&new) < 0)
 			goto out_free;
 		/* destroy any largepage mappings for dirty tracking */
-		if (old.npages)
-			flush_shadow = 1;
 	}
 #else  /* not defined CONFIG_S390 */
 	new.user_alloc = user_alloc;
@@ -778,9 +776,6 @@ skip_lpage:
 	kvm_free_physmem_slot(&old, &new);
 	kfree(old_memslots);
 
-	if (flush_shadow)
-		kvm_arch_flush_shadow(kvm);
-
 	return 0;
 
 out_free:
Signature · 1 line
1.7.1
#1 lore raw
Quoted 23 lines · Avi Kivity

Instead, drop large mappings, which were the reason we dropped shadow.

---
 arch/x86/kvm/mmu.c  |   10 ++++++----
 virt/kvm/kvm_main.c |    7 +------
 2 files changed, 7 insertions(+), 10 deletions(-)
arch/x86/kvm/mmu.c +2 -4
index 43bd5e3..1bbe2c0 100644
@@ -3444,14 +3444,16 @@ void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
 		if (!test_bit(slot, sp->slot_bitmap))
 			continue;
 
-		if (sp->role.level != PT_PAGE_TABLE_LEVEL)
-			continue;
-
 		pt = sp->spt;
-		for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
+		for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
+			if (sp->role.level != PT_PAGE_TABLE_LEVEL

arch/x86/kvm/mmu.c void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot) · line 3459

Quoted 10 lines
arch/x86/kvm/mmu.c +4 -0
+			    && is_large_pte(pt[i]))
+				drop_spte(kvm, &pt[i],
+					  shadow_trap_nonpresent_pte);
 			/* avoid RMW */
 			if (is_writable_pte(pt[i]))
 				update_spte(&pt[i], pt[i] & ~PT_WRITABLE_MASK);
+		}
 	}
 	kvm_flush_remote_tlbs(kvm);
 }

What is the difference from the similar part in rmap_write_protect()?

=== from rmap_write_protect() ===

	/* check for huge page mappings */
	for (i = PT_DIRECTORY_LEVEL;
	     i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
		rmapp = gfn_to_rmap(kvm, gfn, i);
		spte = rmap_next(kvm, rmapp, NULL);
		while (spte) {
			BUG_ON(!spte);
			BUG_ON(!(*spte & PT_PRESENT_MASK));
			BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
			pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
			if (is_writable_pte(*spte)) {
				drop_spte(kvm, spte,
					  shadow_trap_nonpresent_pte);
				--kvm->stat.lpages;
				spte = NULL;
				write_protected = 1;
			}
			spte = rmap_next(kvm, rmapp, spte);
		}
	}

===

Takuya

author
#2 lore raw
Quoted 60 lines · Takuya Yoshikawa
Quoted 25 lines · Avi Kivity

Instead, drop large mappings, which were the reason we dropped shadow.

---

  arch/x86/kvm/mmu.c  |   10 ++++++----
  virt/kvm/kvm_main.c |    7 +------
  2 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
index 43bd5e3..1bbe2c0 100644
--- a/arch/x86/kvm/mmu.c
+++ b/arch/x86/kvm/mmu.c
@@ -3444,14 +3444,16 @@ void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
if (!test_bit(slot, sp->slot_bitmap))
continue;

- if (sp->role.level != PT_PAGE_TABLE_LEVEL)
- continue;
-
pt = sp->spt;
- for (i = 0; i< PT64_ENT_PER_PAGE; ++i)
+ for (i = 0; i< PT64_ENT_PER_PAGE; ++i) {
+ if (sp->role.level != PT_PAGE_TABLE_LEVEL
+ && is_large_pte(pt[i]))
+ drop_spte(kvm,&pt[i],

Quoted 8 lines

+ shadow_trap_nonpresent_pte);
/* avoid RMW */
if (is_writable_pte(pt[i]))
update_spte(&pt[i], pt[i]& ~PT_WRITABLE_MASK);
+ }
}
kvm_flush_remote_tlbs(kvm);
}

What is the difference from the similar part in rmap_write_protect()?

=== from rmap_write_protect() ===

	/* check for huge page mappings */
	for (i = PT_DIRECTORY_LEVEL;
	     i<  PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
		rmapp = gfn_to_rmap(kvm, gfn, i);
		spte = rmap_next(kvm, rmapp, NULL);
		while (spte) {
			BUG_ON(!spte);
			BUG_ON(!(*spte&  PT_PRESENT_MASK));
			BUG_ON((*spte&  (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
			pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
			if (is_writable_pte(*spte)) {
				drop_spte(kvm, spte,
					  shadow_trap_nonpresent_pte);
				--kvm->stat.lpages;
				spte = NULL;
				write_protected = 1;
			}
			spte = rmap_next(kvm, rmapp, spte);
		}
	}
Quoted 1 line

===

Shouldn't be any. I forgot to rmap_remove() and to update kvm_stat, thanks for pointing it out.

I'll post an updated patch. We'll want to share code between the two later on.

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#3 lore raw
Quoted 67 lines · Avi Kivity
Quoted 64 lines · Takuya Yoshikawa
Quoted 1 line · Avi Kivity

Instead, drop large mappings, which were the reason we dropped

shadow.

Quoted 7 lines

---

  arch/x86/kvm/mmu.c  |   10 ++++++----
  virt/kvm/kvm_main.c |    7 +------
  2 files changed, 7 insertions(+), 10 deletions(-)
Quoted 5 lines

diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c index 43bd5e3..1bbe2c0 100644
--- a/arch/x86/kvm/mmu.c
+++ b/arch/x86/kvm/mmu.c
@@ -3444,14 +3444,16 @@ void

kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)

Quoted 19 lines
          if (!test_bit(slot, sp->slot_bitmap))
              continue;

- if (sp->role.level != PT_PAGE_TABLE_LEVEL)
- continue;
-

          pt = sp->spt;
 -        for (i = 0; i<  PT64_ENT_PER_PAGE; ++i)
 +        for (i = 0; i<  PT64_ENT_PER_PAGE; ++i) {
 +            if (sp->role.level != PT_PAGE_TABLE_LEVEL
 + &&  is_large_pte(pt[i]))
 +                drop_spte(kvm,&pt[i],
 +                      shadow_trap_nonpresent_pte);
              /* avoid RMW */
              if (is_writable_pte(pt[i]))
                  update_spte(&pt[i], pt[i]&  ~PT_WRITABLE_MASK);
 +        }
      }
      kvm_flush_remote_tlbs(kvm);
Quoted 1 line

}

What is the difference from the similar part in rmap_write_protect()?

=== from rmap_write_protect() ===

    /* check for huge page mappings */
    for (i = PT_DIRECTORY_LEVEL;
         i<  PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
        rmapp = gfn_to_rmap(kvm, gfn, i);
        spte = rmap_next(kvm, rmapp, NULL);
        while (spte) {
            BUG_ON(!spte);
            BUG_ON(!(*spte&  PT_PRESENT_MASK));
            BUG_ON((*spte&  (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != 
(PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
            pgprintk("rmap_write_protect(large): spte %p %llx 
%lld\n", spte, *spte, gfn);
            if (is_writable_pte(*spte)) {
                drop_spte(kvm, spte,
                      shadow_trap_nonpresent_pte);
                --kvm->stat.lpages;
                spte = NULL;
                write_protected = 1;
            }
            spte = rmap_next(kvm, rmapp, spte);
        }
    }
Quoted 1 line

===

Quoted 4 lines

Shouldn't be any. I forgot to rmap_remove() and to update kvm_stat, thanks for pointing it out.

I'll post an updated patch. We'll want to share code between the two later on.

Actually, drop_spte() calls rmap_remove(), so all that's broken is kvm->stat.lpages accounting.

Signature · 1 line
error compiling committee.c: too many arguments to function