Skip to contentSkip to conversation

KVM: MMU: Add shadow_accessed_shift

PATCH In review [email protected] 3 participants 3 messages Aug 30, 2008 – Sep 1, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: MMU: Add shadow_accessed_shift

From: Sheng Yang <[email protected]>

We use a "fake" A/D bit for EPT, to keep epte behaviour consistent with shadow spte. But it's not that good for MMU notifier. Now we can only expect return young=0 for clean_flush_young() in most condition.

Also fix a unproper judgement based on shadow_accessed_mask=0 for EPT.

---
 arch/x86/kvm/mmu.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)
arch/x86/kvm/mmu.c +6 -4
index 9bc31fc..1e9f9b4 100644
@@ -158,6 +158,7 @@ static u64 __read_mostly shadow_nx_mask;
 static u64 __read_mostly shadow_x_mask;	/* mutual exclusive with nx_mask */
 static u64 __read_mostly shadow_user_mask;
 static u64 __read_mostly shadow_accessed_mask;
+static u16 __read_mostly shadow_accessed_shift;
 static u64 __read_mostly shadow_dirty_mask;
 
 void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
@@ -178,6 +179,8 @@ void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
 {
 	shadow_user_mask = user_mask;
 	shadow_accessed_mask = accessed_mask;
+	shadow_accessed_shift = find_first_bit((unsigned long *)&accessed_mask,
+					       sizeof(accessed_mask));
 	shadow_dirty_mask = dirty_mask;
 	shadow_nx_mask = nx_mask;
 	shadow_x_mask = x_mask;
@@ -716,10 +719,10 @@ static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
 		int _young;
 		u64 _spte = *spte;
 		BUG_ON(!(_spte & PT_PRESENT_MASK));
-		_young = _spte & PT_ACCESSED_MASK;
+		_young = _spte & shadow_accessed_mask;
 		if (_young) {
 			young = 1;
-			clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
+			clear_bit(shadow_accessed_shift, (unsigned long *)spte);
 		}
 		spte = rmap_next(kvm, rmapp, spte);
 	}
@@ -1789,10 +1792,9 @@ static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
 
 	if (spte
 	    && vcpu->arch.last_pte_gfn == gfn
-	    && shadow_accessed_mask
 	    && !(*spte & shadow_accessed_mask)
 	    && is_shadow_present_pte(*spte))
-		set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
+		set_bit(shadow_accessed_shift, (unsigned long *)spte);
 }
 
 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Signature · 1 line
1.5.3
#1 lore raw
Quoted 7 lines · [email protected] wrote:

From: Sheng Yang <[email protected]>

We use a "fake" A/D bit for EPT, to keep epte behaviour consistent with shadow spte. But it's not that good for MMU notifier. Now we can only expect return young=0 for clean_flush_young() in most condition.

Perhaps we are better off setting shadow_accessed_mask to 0 for ept, and adding a test for clear_flush_young()? This is the only place that needs adjusting as far as I can tell.

I don't see what having a fake accessed bit buys us, and I'd like the patch to be as small as possible, since it needs to go into 2.6.26-stable and 2.6.27-rc.

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 7 lines · On Sunday 31 August 2008 23:13:54 Avi Kivity wrote:
Quoted 5 lines · [email protected] wrote:

From: Sheng Yang <[email protected]>

We use a "fake" A/D bit for EPT, to keep epte behaviour consistent with shadow spte. But it's not that good for MMU notifier. Now we can only expect return young=0 for clean_flush_young() in most condition.

Quoted 7 lines

Perhaps we are better off setting shadow_accessed_mask to 0 for ept, and adding a test for clear_flush_young()? This is the only place that needs adjusting as far as I can tell.

I don't see what having a fake accessed bit buys us, and I'd like the patch to be as small as possible, since it needs to go into 2.6.26-stable and 2.6.27-rc.

Though I still think fake accessed bit here makes logic consistent, here is the patch follow your comment. But I think it may not necessary for the 2.6.26-stable?

----
From: Sheng Yang <[email protected]>
Date: Mon, 1 Sep 2008 13:22:09 +0800
Subject: [PATCH] KVM: VMX: Always return 0 for clear_flush_young() when using EPT

As well as discard fake accessed bit and dirty bit of EPT.

---
 arch/x86/kvm/mmu.c |   15 +++++++++++----
 arch/x86/kvm/vmx.c |    3 +--
 arch/x86/kvm/vmx.h |    2 --
 3 files changed, 12 insertions(+), 8 deletions(-)
arch/x86/kvm/mmu.c +0 -0
index f33c594..e437985 100644
@@ -716,10 +716,17 @@ static int kvm_age_rmapp(struct kvm *kvm, unsigned long 

*rmapp)
int _young;
u64 _spte = *spte; BUG_ON(!(_spte & PT_PRESENT_MASK));
- _young = _spte & PT_ACCESSED_MASK;
- if (_young) {
- young = 1;
- clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
+
+ /* always return old for EPT */
+ if (!shadow_accessed_mask)
+ _young = 0;
+ else {
+ _young = _spte & PT_ACCESSED_MASK;
+ if (_young) {
+ young = 1;
+ clear_bit(PT_ACCESSED_SHIFT,
+ (unsigned long *)spte);
+ }
}
spte = rmap_next(kvm, rmapp, spte);
}

arch/x86/kvm/vmx.c +1 -2
index 81c121c..d637897 100644
@@ -3557,8 +3557,7 @@ static int __init vmx_init(void)
 		kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
 			VMX_EPT_WRITABLE_MASK |
 			VMX_EPT_DEFAULT_MT << VMX_EPT_MT_EPTE_SHIFT);
-		kvm_mmu_set_mask_ptes(0ull, VMX_EPT_FAKE_ACCESSED_MASK,
-				VMX_EPT_FAKE_DIRTY_MASK, 0ull,
+		kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
 				VMX_EPT_EXECUTABLE_MASK);
 		kvm_enable_tdp();
 	} else
arch/x86/kvm/vmx.h +0 -2
index 0c22e5f..41e8c10 100644
@@ -370,8 +370,6 @@ enum vmcs_field {
 #define VMX_EPT_READABLE_MASK			0x1ull
 #define VMX_EPT_WRITABLE_MASK			0x2ull
 #define VMX_EPT_EXECUTABLE_MASK			0x4ull
-#define VMX_EPT_FAKE_ACCESSED_MASK		(1ull << 62)
-#define VMX_EPT_FAKE_DIRTY_MASK			(1ull << 63)
 
 #define VMX_EPT_IDENTITY_PAGETABLE_ADDR		0xfffbc000ul
 

--
1.5.4.5

From 23229946e717294091bf54cee704fb3b1cd4167d Mon Sep 17 00:00:00 2001
From: Sheng Yang <[email protected]>
Date: Mon, 1 Sep 2008 13:22:09 +0800
Subject: [PATCH] KVM: VMX: Always return 0 for clear_flush_young() when using EPT

As well as discard fake accessed bit and dirty bit of EPT.

---
 arch/x86/kvm/mmu.c |   15 +++++++++++----
 arch/x86/kvm/vmx.c |    3 +--
 arch/x86/kvm/vmx.h |    2 --
 3 files changed, 12 insertions(+), 8 deletions(-)
arch/x86/kvm/mmu.c +11 -4
index f33c594..e437985 100644
@@ -716,10 +716,17 @@ static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
 		int _young;
 		u64 _spte = *spte;
 		BUG_ON(!(_spte & PT_PRESENT_MASK));
-		_young = _spte & PT_ACCESSED_MASK;
-		if (_young) {
-			young = 1;
-			clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
+
+		/* always return old for EPT */
+		if (!shadow_accessed_mask)
+			_young = 0;
+		else {
+			_young = _spte & PT_ACCESSED_MASK;
+			if (_young) {
+				young = 1;
+				clear_bit(PT_ACCESSED_SHIFT,
+					  (unsigned long *)spte);
+			}
 		}
 		spte = rmap_next(kvm, rmapp, spte);
 	}
arch/x86/kvm/vmx.c +1 -2
index 81c121c..d637897 100644
@@ -3557,8 +3557,7 @@ static int __init vmx_init(void)
 		kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
 			VMX_EPT_WRITABLE_MASK |
 			VMX_EPT_DEFAULT_MT << VMX_EPT_MT_EPTE_SHIFT);
-		kvm_mmu_set_mask_ptes(0ull, VMX_EPT_FAKE_ACCESSED_MASK,
-				VMX_EPT_FAKE_DIRTY_MASK, 0ull,
+		kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
 				VMX_EPT_EXECUTABLE_MASK);
 		kvm_enable_tdp();
 	} else
arch/x86/kvm/vmx.h +0 -2
index 0c22e5f..41e8c10 100644
@@ -370,8 +370,6 @@ enum vmcs_field {
 #define VMX_EPT_READABLE_MASK			0x1ull
 #define VMX_EPT_WRITABLE_MASK			0x2ull
 #define VMX_EPT_EXECUTABLE_MASK			0x4ull
-#define VMX_EPT_FAKE_ACCESSED_MASK		(1ull << 62)
-#define VMX_EPT_FAKE_DIRTY_MASK			(1ull << 63)
 
 #define VMX_EPT_IDENTITY_PAGETABLE_ADDR		0xfffbc000ul
 
Signature · 1 line
1.5.4.5