Skip to contentSkip to conversation

kvm x86/mmu: use KVM_REQ_MMU_SYNC to sync when needed

PATCH Superseded Lai Jiangshan 4 participants 12 messages Aug 24, 2020 – Sep 11, 2020

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

PATCH 1/1 kvm x86/mmu: use KVM_REQ_MMU_SYNC to sync when needed

From: Lai Jiangshan <[email protected]>

8c8560b83390("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes) changed it without giving any reason in the changelog.

In theory, the syncing is needed, and need to be fixed by reverting this part of change.

---
 arch/x86/kvm/mmu/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +1 -1
index 4e03841f053d..9a93de921f2b 100644
@@ -2468,7 +2468,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
 		}
 
 		if (sp->unsync_children)
-			kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
+			kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
 
 		__clear_sp_write_flooding_count(sp);
 
Signature · 1 line
2.19.1.6.gb485710b
#1 lore raw

Ping @Sean Christopherson

Quoted 30 lines · Lai Jiangshan

From: Lai Jiangshan <[email protected]>

8c8560b83390("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes) changed it without giving any reason in the changelog.

In theory, the syncing is needed, and need to be fixed by reverting this part of change.

---
 arch/x86/kvm/mmu/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +1 -2
index 4e03841f053d..9a93de921f2b 100644
@@ -2468,7 +2468,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
                }
 
                if (sp->unsync_children)
-                       kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
+                       kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
 
                __clear_sp_write_flooding_count(sp);
 
--

2.19.1.6.gb485710b

#2 lore raw
In reply to Lai Jiangshan

Ping @Sean Christopherson

Quoted 30 lines · Lai Jiangshan

From: Lai Jiangshan <[email protected]>

8c8560b83390("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes) changed it without giving any reason in the changelog.

In theory, the syncing is needed, and need to be fixed by reverting this part of change.

---
 arch/x86/kvm/mmu/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +1 -2
index 4e03841f053d..9a93de921f2b 100644
@@ -2468,7 +2468,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
                }
 
                if (sp->unsync_children)
-                       kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
+                       kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
 
                __clear_sp_write_flooding_count(sp);
 
--

2.19.1.6.gb485710b

#3 lore raw
Quoted 2 lines · Lai Jiangshan

Ping @Sean Christopherson

Let's try 'Beetlejuice' instead :-)

Quoted 9 lines · Lai Jiangshan
Quoted 8 lines

From: Lai Jiangshan <[email protected]>

8c8560b83390("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes) changed it without giving any reason in the changelog.

In theory, the syncing is needed, and need to be fixed by reverting this part of change.

Even if the original commit is not wordy enough this is hardly better. Are you seeing a particular scenario when a change in current vCPU's MMU requires flushing TLB entries for *other* contexts, ... (see below)

Quoted 13 lines
Quoted 13 lines
---
 arch/x86/kvm/mmu/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +0 -0
index 4e03841f053d..9a93de921f2b 100644
@@ -2468,7 +2468,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
                }
 
Quoted 3 lines

arch/x86/kvm/mmu/mmu.c static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, · line 2471

Quoted 3 lines
arch/x86/kvm/mmu/mmu.c +1 -1
                if (sp->unsync_children)
-                       kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
+                       kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);

... in particular, why are you reverting only this hunk? Please elaborate.

Quoted 7 lines
Quoted 6 lines
arch/x86/kvm/mmu/mmu.c +0 -1
                __clear_sp_write_flooding_count(sp);
 
--

2.19.1.6.gb485710b

Signature · 1 line
Vitaly
#4 lore raw
Quoted 12 lines · Vitaly Kuznetsov
Quoted 2 lines · Lai Jiangshan

Ping @Sean Christopherson

Let's try 'Beetlejuice' instead :-)

Quoted 4 lines · Lai Jiangshan
Quoted 3 lines

From: Lai Jiangshan <[email protected]>

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

8c8560b83390("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes) changed it without giving any reason in the changelog.

In theory, the syncing is needed, and need to be fixed by reverting this part of change.

Even if the original commit is not wordy enough this is hardly better.

Hello,
Thank you for reviewing it.

I'm sorry that when I said "reverting this part of change", I meant "reverting this line of code". This line of code itself is quite clear that it is not related to the original commit according to its changelog.

Quoted 3 lines

Are you seeing a particular scenario when a change in current vCPU's MMU requires flushing TLB entries for *other* contexts, ... (see below)

So I don't think the patch needs to explain this because the patch does not change/revert anything about it.

Anyway, using a "revert" in the changelog is misleading, when it is not really reverting the whole targeted commit. I would remove this wording.

For the change in my patch, when kvm_mmu_get_page() gets a page with unsync children, the host side pagetable is unsynchronized with the guest side pagedtable, and the guest might not issue a "flush" operation on it. It is all about the host's emulation of the pagetable. So the host has the responsibility to synchronize the pagetables.

Thanks
Lai

Quoted 30 lines
Quoted 13 lines
Quoted 13 lines
---
 arch/x86/kvm/mmu/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +0 -0
index 4e03841f053d..9a93de921f2b 100644
@@ -2468,7 +2468,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
                }
 
Quoted 3 lines

arch/x86/kvm/mmu/mmu.c static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, · line 2471

Quoted 3 lines
arch/x86/kvm/mmu/mmu.c +1 -1
                if (sp->unsync_children)
-                       kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
+                       kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);

... in particular, why are you reverting only this hunk? Please elaborate.

Quoted 7 lines
Quoted 6 lines
arch/x86/kvm/mmu/mmu.c +0 -1
                __clear_sp_write_flooding_count(sp);
 
--

2.19.1.6.gb485710b

--
Vitaly

#5 lore raw
Quoted 41 lines · Lai Jiangshan
Quoted 12 lines · Vitaly Kuznetsov
Quoted 2 lines · Lai Jiangshan

Ping @Sean Christopherson

Let's try 'Beetlejuice' instead :-)

Quoted 4 lines · Lai Jiangshan
Quoted 3 lines

From: Lai Jiangshan <[email protected]>

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

8c8560b83390("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes) changed it without giving any reason in the changelog.

In theory, the syncing is needed, and need to be fixed by reverting this part of change.

Even if the original commit is not wordy enough this is hardly better.

Hello,
Thank you for reviewing it.

I'm sorry that when I said "reverting this part of change", I meant "reverting this line of code". This line of code itself is quite clear that it is not related to the original commit according to its changelog.

Quoted 3 lines

Are you seeing a particular scenario when a change in current vCPU's MMU requires flushing TLB entries for *other* contexts, ... (see below)

So I don't think the patch needs to explain this because the patch does not change/revert anything about it.

Anyway, using a "revert" in the changelog is misleading, when it is not really reverting the whole targeted commit. I would remove this wording.

Quoted 7 lines

For the change in my patch, when kvm_mmu_get_page() gets a page with unsync children, the host side pagetable is unsynchronized with the guest side pagedtable, and the guest might not issue a "flush" operation on it. It is all about the host's emulation of the pagetable. So the host has the responsibility to synchronize the pagetables.

Ah, I see now, so it seems Sean's commit has a stray change in it: the intention was to change KVM_REQ_TLB_FLUSH -> KVM_REQ_TLB_FLUSH_CURRENT so we don't unneedlesly flush other contexts but one of the hunks changed KVM_REQ_MMU_SYNC instead. Syncronizing MMU roots can't be replaced with a TLB flush, we need to revert back the change. This sounds reasonable to me, please send out v2 with the updated description. Thanks!

Signature · 1 line
Vitaly
#6 lore raw

PATCH v2 1/1 kvm x86/mmu: use KVM_REQ_MMU_SYNC to sync when needed

From: Lai Jiangshan <[email protected]>

When kvm_mmu_get_page() gets a page with unsynced children, the spt pagetable is unsynchronized with the guest pagetable. But the guest might not issue a "flush" operation on it when the pagetable entry is changed from zero or other cases. The hypervisor has the responsibility to synchronize the pagetables.

The linux kernel behaves as above for many years, But 8c8560b83390("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes) inadvertently included a line of code to change it without giving any reason in the changelog. It is clear that the commit's intention was to change KVM_REQ_TLB_FLUSH -> KVM_REQ_TLB_FLUSH_CURRENT, so we don't unneedlesly flush other contexts but one of the hunks changed
nearby KVM_REQ_MMU_SYNC instead.

The this patch changes it back.

---
Changed from v1:
update patch description

 arch/x86/kvm/mmu/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +1 -1
index 4e03841f053d..9a93de921f2b 100644
@@ -2468,7 +2468,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
 		}
 
 		if (sp->unsync_children)
-			kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
+			kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
 
 		__clear_sp_write_flooding_count(sp);
 
Signature · 1 line
2.19.1.6.gb485710b
Reviewed-by on v2 1/1
#7 lore raw
Quoted 10 lines · Lai Jiangshan

From: Lai Jiangshan <[email protected]>

When kvm_mmu_get_page() gets a page with unsynced children, the spt pagetable is unsynchronized with the guest pagetable. But the guest might not issue a "flush" operation on it when the pagetable entry is changed from zero or other cases. The hypervisor has the responsibility to synchronize the pagetables.

The linux kernel behaves as above for many years, But 8c8560b83390("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes)

Nit: checkpatch.pl complains here with

ERROR: Please use git commit description style 'commit <12+ chars of sha1> ("<title line>")' - ie: 'commit 8c8560b83390 ("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes")'
#118:
8c8560b83390("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes)

Quoted 24 lines

inadvertently included a line of code to change it without giving any reason in the changelog. It is clear that the commit's intention was to change KVM_REQ_TLB_FLUSH -> KVM_REQ_TLB_FLUSH_CURRENT, so we don't unneedlesly flush other contexts but one of the hunks changed nearby KVM_REQ_MMU_SYNC instead.

The this patch changes it back.

---
Changed from v1:
update patch description

 arch/x86/kvm/mmu/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +0 -0
index 4e03841f053d..9a93de921f2b 100644

arch/x86/kvm/mmu/mmu.c static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, · line 2473

Quoted 7 lines
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -2468,7 +2468,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
 		}
 
 		if (sp->unsync_children)
-			kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
+			kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
 
 		__clear_sp_write_flooding_count(sp);

FWIW,

but it'd be great to hear from Sean).

Signature · 1 line
Vitaly
#8 lore raw
Quoted 13 lines · Vitaly Kuznetsov
Quoted 10 lines · Lai Jiangshan

From: Lai Jiangshan <[email protected]>

When kvm_mmu_get_page() gets a page with unsynced children, the spt pagetable is unsynchronized with the guest pagetable. But the guest might not issue a "flush" operation on it when the pagetable entry is changed from zero or other cases. The hypervisor has the responsibility to synchronize the pagetables.

The linux kernel behaves as above for many years, But 8c8560b83390("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes)

Quoted 7 lines

Nit: checkpatch.pl complains here with

ERROR: Please use git commit description style 'commit <12+ chars of sha1> ("<title line>")' - ie: 'commit 8c8560b83390 ("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes")'
#118:
8c8560b83390("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes)

Definitely needs a

Fixes 8c8560b83390("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes)

At that point I'd just have the changelog say "a recent commit".

Quoted 30 lines
Quoted 30 lines

inadvertently included a line of code to change it without giving any reason in the changelog. It is clear that the commit's intention was to change KVM_REQ_TLB_FLUSH -> KVM_REQ_TLB_FLUSH_CURRENT, so we don't unneedlesly flush other contexts but one of the hunks changed nearby KVM_REQ_MMU_SYNC instead.

The this patch changes it back.

---
Changed from v1:
update patch description

 arch/x86/kvm/mmu/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +1 -1
index 4e03841f053d..9a93de921f2b 100644
@@ -2468,7 +2468,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
 		}
 
 		if (sp->unsync_children)
-			kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
+			kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
 
Quoted 7 lines

arch/x86/kvm/mmu/mmu.c static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, · line 2473

Quoted 1 line
arch/x86/kvm/mmu/mmu.c +0 -0
 		__clear_sp_write_flooding_count(sp);

FWIW,

but it'd be great to hear from Sean).

I got nothing, AFAICT I was simply overzealous.

#9 lore raw

PATCH v3 1/1 kvm x86/mmu: use KVM_REQ_MMU_SYNC to sync when needed

From: Lai Jiangshan <[email protected]>

When kvm_mmu_get_page() gets a page with unsynced children, the spt pagetable is unsynchronized with the guest pagetable. But the guest might not issue a "flush" operation on it when the pagetable entry is changed from zero or other cases. The hypervisor has the responsibility to synchronize the pagetables.

The linux kernel behaves correctly as above for many years, but a recent commit 8c8560b83390 ("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes") inadvertently included a line of code to change it without giving any reason in the changelog. It is clear that the commit's intention was to change KVM_REQ_TLB_FLUSH -> KVM_REQ_TLB_FLUSH_CURRENT, so we don't unneedlesly flush other contexts but one of the hunks changed nearby KVM_REQ_MMU_SYNC instead.

This patch changes it back.

Fixes 8c8560b83390("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes)

---
Changed from v1:
update patch description

Changed form v2:
update patch description

 arch/x86/kvm/mmu/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +1 -1
index 4e03841f053d..9a93de921f2b 100644
@@ -2468,7 +2468,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
 		}
 
 		if (sp->unsync_children)
-			kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
+			kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
 
 		__clear_sp_write_flooding_count(sp);
 
Signature · 1 line
2.19.1.6.gb485710b
#10 lore raw

Hi, Paolo

Could you pick the patch please?

I think it'd be better to be merged before v5.9 is released since it fixes a flaw.

Thanks
Lai

Quoted 50 lines · Lai Jiangshan

From: Lai Jiangshan <[email protected]>

When kvm_mmu_get_page() gets a page with unsynced children, the spt pagetable is unsynchronized with the guest pagetable. But the guest might not issue a "flush" operation on it when the pagetable entry is changed from zero or other cases. The hypervisor has the responsibility to synchronize the pagetables.

The linux kernel behaves correctly as above for many years, but a recent commit 8c8560b83390 ("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes") inadvertently included a line of code to change it without giving any reason in the changelog. It is clear that the commit's intention was to change KVM_REQ_TLB_FLUSH -> KVM_REQ_TLB_FLUSH_CURRENT, so we don't unneedlesly flush other contexts but one of the hunks changed nearby KVM_REQ_MMU_SYNC instead.

This patch changes it back.

Fixes 8c8560b83390("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes)

---
Changed from v1:
update patch description

Changed form v2:
update patch description

 arch/x86/kvm/mmu/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +1 -2
index 4e03841f053d..9a93de921f2b 100644
@@ -2468,7 +2468,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
                }
 
                if (sp->unsync_children)
-                       kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
+                       kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
 
                __clear_sp_write_flooding_count(sp);
 
--

2.19.1.6.gb485710b

maintainer Applied
#11 lore raw
In reply to Lai Jiangshan
Quoted 35 lines · Lai Jiangshan

From: Lai Jiangshan <[email protected]>

When kvm_mmu_get_page() gets a page with unsynced children, the spt pagetable is unsynchronized with the guest pagetable. But the guest might not issue a "flush" operation on it when the pagetable entry is changed from zero or other cases. The hypervisor has the responsibility to synchronize the pagetables.

The linux kernel behaves as above for many years, But 8c8560b83390("KVM: x86/mmu: Use KVM_REQ_TLB_FLUSH_CURRENT for MMU specific flushes) inadvertently included a line of code to change it without giving any reason in the changelog. It is clear that the commit's intention was to change KVM_REQ_TLB_FLUSH -> KVM_REQ_TLB_FLUSH_CURRENT, so we don't unneedlesly flush other contexts but one of the hunks changed
nearby KVM_REQ_MMU_SYNC instead.

The this patch changes it back.

---
Changed from v1:
update patch description

 arch/x86/kvm/mmu/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +0 -0
index 4e03841f053d..9a93de921f2b 100644

arch/x86/kvm/mmu/mmu.c static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, · line 2474

Quoted 8 lines
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -2468,7 +2468,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
 		}
 
 		if (sp->unsync_children)
-			kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
+			kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
 
 		__clear_sp_write_flooding_count(sp);
 

Queued, thanks.

Paolo