Skip to contentSkip to conversation

Flush TLB when D bit is manually changed.

PATCH In review Kai Huang 3 participants 4 messages Jan 9, 2015

Subsystems (3)
lore
Share & tools
lore

Kai Huang

author
#0 lore raw

PATCH 1/1 Flush TLB when D bit is manually changed.

When software changes D bit (either from 1 to 0, or 0 to 1), the corresponding TLB entity in the hardware won't be updated immediately. We should flush it to guarantee the consistence of D bit between TLB and MMU page table in memory. This is required if some specific hardware feature uses D-bit status to do specific things.

Sanity test was done on my machine with Intel processor.

---
 arch/x86/kvm/mmu.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/x86/kvm/mmu.c +12 -0
index 978f402..1feac0c 100644
@@ -547,6 +547,11 @@ static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
 	return (old_spte & bit_mask) && !(new_spte & bit_mask);
 }
 
+static bool spte_is_bit_changed(u64 old_spte, u64 new_spte, u64 bit_mask)
+{
+	return (old_spte & bit_mask) != (new_spte & bit_mask);
+}
+
 /* Rules for using mmu_spte_set:
  * Set the sptep from nonpresent to present.
  * Note: the sptep being assigned *must* be either not present
@@ -597,6 +602,13 @@ static bool mmu_spte_update(u64 *sptep, u64 new_spte)
 	if (!shadow_accessed_mask)
 		return ret;
 
+	/*
+	 * We also need to flush TLB when D-bit is changed by software to
+	 * guarantee the D-bit consistence between TLB and MMU page table.
+	 */
+	if (spte_is_bit_changed(old_spte, new_spte, shadow_dirty_mask))
+		ret = true;
+
 	if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
 		kvm_set_pfn_accessed(spte_to_pfn(old_spte));
 	if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
Signature · 1 line
2.1.0
maintainer
#1 lore raw
Quoted 30 lines · Kai Huang

When software changes D bit (either from 1 to 0, or 0 to 1), the corresponding TLB entity in the hardware won't be updated immediately. We should flush it to guarantee the consistence of D bit between TLB and MMU page table in memory. This is required if some specific hardware feature uses D-bit status to do specific things.

Sanity test was done on my machine with Intel processor.

---
 arch/x86/kvm/mmu.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/x86/kvm/mmu.c +5 -0
index 978f402..1feac0c 100644
@@ -547,6 +547,11 @@ static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
 	return (old_spte & bit_mask) && !(new_spte & bit_mask);
 }
 
+static bool spte_is_bit_changed(u64 old_spte, u64 new_spte, u64 bit_mask)
+{
+	return (old_spte & bit_mask) != (new_spte & bit_mask);
+}
+
 /* Rules for using mmu_spte_set:
  * Set the sptep from nonpresent to present.
  * Note: the sptep being assigned *must* be either not present

arch/x86/kvm/mmu.c static bool mmu_spte_update(u64 *sptep, u64 new_spte) · line 609

Quoted 8 lines
arch/x86/kvm/mmu.c +5 -0
@@ -597,6 +602,13 @@ static bool mmu_spte_update(u64 *sptep, u64 new_spte)
 	if (!shadow_accessed_mask)
 		return ret;
 
+	/*
+	 * We also need to flush TLB when D-bit is changed by software to
+	 * guarantee the D-bit consistence between TLB and MMU page table.
+	 */
+	if (spte_is_bit_changed(old_spte, new_spte, shadow_dirty_mask))

I think shadow_accessed_mask needs to be checked too. I made the change and applied the patch.

Paolo

Quoted 6 lines
arch/x86/kvm/mmu.c +2 -0
+		ret = true;
+
 	if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
 		kvm_set_pfn_accessed(spte_to_pfn(old_spte));
 	if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
maintainer
#2 lore raw
In reply to Kai Huang
Quoted 30 lines · Kai Huang

When software changes D bit (either from 1 to 0, or 0 to 1), the corresponding TLB entity in the hardware won't be updated immediately. We should flush it to guarantee the consistence of D bit between TLB and MMU page table in memory. This is required if some specific hardware feature uses D-bit status to do specific things.

Sanity test was done on my machine with Intel processor.

---
 arch/x86/kvm/mmu.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/x86/kvm/mmu.c +5 -0
index 978f402..1feac0c 100644
@@ -547,6 +547,11 @@ static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
 	return (old_spte & bit_mask) && !(new_spte & bit_mask);
 }
 
+static bool spte_is_bit_changed(u64 old_spte, u64 new_spte, u64 bit_mask)
+{
+	return (old_spte & bit_mask) != (new_spte & bit_mask);
+}
+
 /* Rules for using mmu_spte_set:
  * Set the sptep from nonpresent to present.
  * Note: the sptep being assigned *must* be either not present

arch/x86/kvm/mmu.c static bool mmu_spte_update(u64 *sptep, u64 new_spte) · line 609

Quoted 8 lines
arch/x86/kvm/mmu.c +5 -0
@@ -597,6 +602,13 @@ static bool mmu_spte_update(u64 *sptep, u64 new_spte)
 	if (!shadow_accessed_mask)
 		return ret;
 
+	/*
+	 * We also need to flush TLB when D-bit is changed by software to
+	 * guarantee the D-bit consistence between TLB and MMU page table.
+	 */
+	if (spte_is_bit_changed(old_spte, new_spte, shadow_dirty_mask))

I think shadow_accessed_mask needs to be checked too. I made the change and applied the patch.

Paolo

Quoted 6 lines
arch/x86/kvm/mmu.c +2 -0
+		ret = true;
+
 	if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
 		kvm_set_pfn_accessed(spte_to_pfn(old_spte));
 	if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
#3 lore raw
In reply to Kai Huang
Quoted 6 lines · Kai Huang

When software changes D bit (either from 1 to 0, or 0 to 1), the corresponding TLB entity in the hardware won't be updated immediately. We should flush it to guarantee the consistence of D bit between TLB and MMU page table in memory. This is required if some specific hardware feature uses D-bit status to do specific things.

Currently, A/D is lazied synced and it does not hurt anything since we have marked the page dirty after clearing the bit and mmu-notifier can keep the coherence on host (It is the guest's responsibility to sync TLBs when changing the bit).

So, i am just curious what "some specific hardware" is. :)