kvm: don't lose the higher 32 bits of tlbs_dirty
Subsystems (2)
PATCH 1/1 kvm: don't lose the higher 32 bits of tlbs_dirty
From: Lai Jiangshan <[email protected]>
In kvm_mmu_notifier_invalidate_range_start(), tlbs_dirty is used as:
need_tlb_flush |= kvm->tlbs_dirty;
with need_tlb_flush's type being int and tlbs_dirty's type being long.
It means that tlbs_dirty is always used as int and the higher 32 bits is useless. We can just change need_tlb_flush's type to long to make full use of tlbs_dirty.
--- virt/kvm/kvm_main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
virt/kvm/kvm_main.c +2 -1
@@ -470,7 +470,8 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, const struct mmu_notifier_range *range) {struct kvm *kvm =mmu_notifier_to_kvm (mn); -int need_tlb_flush =0 , idx; +long need_tlb_flush =0 ; +int idx; idx =srcu_read_lock (&kvm->srcu);spin_lock (&kvm->mmu_lock);
Signature · 1 line
2.19.1.6.gb485710b
Sean Christopherson
reviewerQuoted 8 lines · Lai Jiangshan
From: Lai Jiangshan <[email protected]>
In
kvm_mmu_notifier_invalidate_range_start(),tlbs_dirtyis used as:need_tlb_flush |= kvm->tlbs_dirty;with need_tlb_flush's type being int and tlbs_dirty's type being long.
It means that
tlbs_dirtyis always used as int and the higher 32 bits is useless.
It's probably worth noting in the changelog that it's _extremely_ unlikely this bug can cause problems in practice. It would require encountering tlbs_dirty on a 4 billion count boundary, and KVM would need to be using shadow paging or be running a nested guest.
Quoted 2 lines
We can just change need_tlb_flush's type to long to make full use of
tlbs_dirty.
Hrm, this does solve the problem, but I'm not a fan of continuing to use an integer variable as a boolean. Rather than propagate tlbs_dirty to need_tlb_flush, what if this bug fix patch checks tlbs_dirty directly, and then a follow up patch converts need_tlb_flush to a bool and removes the unnecessary initialization (see below).
E.g. the net result of both patches would be:
virt/kvm/kvm_main.c +5 -5
@@ -473,7 +473,8 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, const struct mmu_notifier_range *range) {struct kvm *kvm =mmu_notifier_to_kvm (mn); -int need_tlb_flush =0 , idx; +bool need_tlb_flush; +int idx; idx =srcu_read_lock (&kvm->srcu);spin_lock (&kvm->mmu_lock);
@@ -483,11 +484,10 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,* count is also read inside the mmu_lock critical section. */ kvm->mmu_notifier_count++; - need_tlb_flush =kvm_unmap_hva_range (kvm, range->start, range->end, - range->flags); - need_tlb_flush |= kvm->tlbs_dirty; + need_tlb_flush = !!kvm_unmap_hva_range (kvm, range->start, range->end, + range->flags);/* we've to flush the tlb before the pages can be freed */ -if (need_tlb_flush) +if (need_tlb_flush || kvm->tlbs_dirty)kvm_flush_remote_tlbs (kvm);spin_unlock (&kvm->mmu_lock);
Quoted 10 lines
Signed-off-by Lai Jiangshan <[email protected]>--- virt/kvm/kvm_main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)virt/kvm/kvm_main.c +0
-0
virt/kvm/kvm_main.c static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, · line 473
Quoted 5 lines
virt/kvm/kvm_main.c +1
-1@@ -470,7 +470,8 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, const struct mmu_notifier_range *range) {struct kvm *kvm =mmu_notifier_to_kvm (mn); -int need_tlb_flush =0 , idx; +long need_tlb_flush =0 ;
need_tlb_flush doesn't need to be initialized here, it's explicitly set via the call to kvm_unmap_hva_range().
Quoted 7 lines
virt/kvm/kvm_main.c +1
-0+int idx; idx =srcu_read_lock (&kvm->srcu);spin_lock (&kvm->mmu_lock);Signature · 1 line
2.19.1.6.gb485710b
Quoted 8 lines · Sean Christopherson
Quoted 6 lines · Lai Jiangshan
From: Lai Jiangshan <[email protected]>
In
kvm_mmu_notifier_invalidate_range_start(),tlbs_dirtyis used as:need_tlb_flush |= kvm->tlbs_dirty;with need_tlb_flush's type being int and tlbs_dirty's type being long.
Quoted 7 lines
Quoted 2 lines
It means that
tlbs_dirtyis always used as int and the higher 32 bits is useless.It's probably worth noting in the changelog that it's _extremely_ unlikely this bug can cause problems in practice. It would require encountering
tlbs_dirtyon a 4 billion count boundary, and KVM would need to be using shadow paging or be running a nested guest.
You are right, I don't consider it would cause problems in practice, and I also tried to make tlbs_dirty as "int" and found that I have to change too many places.
And you are right it is worth noting about it, I'm sorry for neglecting.
Quoted 42 lines
Quoted 3 lines
We can just change need_tlb_flush's type to long to make full use of
tlbs_dirty.Hrm, this does solve the problem, but I'm not a fan of continuing to use an integer variable as a boolean. Rather than propagate
tlbs_dirtytoneed_tlb_flush, what if this bug fix patch checkstlbs_dirtydirectly, and then a follow up patch convertsneed_tlb_flushto a bool and removes the unnecessary initialization (see below).E.g. the net result of both patches would be:
virt/kvm/kvm_main.c +5
-5@@ -473,7 +473,8 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, const struct mmu_notifier_range *range) {struct kvm *kvm =mmu_notifier_to_kvm (mn); -int need_tlb_flush =0 , idx; +bool need_tlb_flush; +int idx; idx =srcu_read_lock (&kvm->srcu);spin_lock (&kvm->mmu_lock);@@ -483,11 +484,10 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,* count is also read inside the mmu_lock critical section. */ kvm->mmu_notifier_count++; - need_tlb_flush =kvm_unmap_hva_range (kvm, range->start, range->end, - range->flags); - need_tlb_flush |= kvm->tlbs_dirty; + need_tlb_flush = !!kvm_unmap_hva_range (kvm, range->start, range->end, + range->flags);/* we've to flush the tlb before the pages can be freed */ -if (need_tlb_flush) +if (need_tlb_flush || kvm->tlbs_dirty)kvm_flush_remote_tlbs (kvm);spin_unlock (&kvm->mmu_lock);
Quoted 2 lines
Fixes a4ee1ca4a36e ("KVM: MMU: delay flush all tlbs on sync_page path")
I searched back, found it and considered adding this fixes tag, but I was afraid that this cleanup would be backported. Is it worth backporting such patch which is extremely hardly a problem in practice?
Quoted 27 lines
Quoted 11 lines
Signed-off-by Lai Jiangshan <[email protected]>--- virt/kvm/kvm_main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)virt/kvm/kvm_main.c +0
-0virt/kvm/kvm_main.c static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, · line 473
Quoted 5 lines
virt/kvm/kvm_main.c +1
-1@@ -470,7 +470,8 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, const struct mmu_notifier_range *range) {struct kvm *kvm =mmu_notifier_to_kvm (mn); -int need_tlb_flush =0 , idx; +long need_tlb_flush =0 ;
need_tlb_flushdoesn't need to be initialized here, it's explicitly set via the call tokvm_unmap_hva_range().Quoted 7 lines
virt/kvm/kvm_main.c +1
-1+int idx; idx =srcu_read_lock (&kvm->srcu);spin_lock (&kvm->mmu_lock); --2.19.1.6.gb485710b
Paolo Bonzini
maintainerQuoted 15 lines · Sean Christopherson
Quoted 8 lines · Lai Jiangshan
From: Lai Jiangshan <[email protected]>
In
kvm_mmu_notifier_invalidate_range_start(),tlbs_dirtyis used as:need_tlb_flush |= kvm->tlbs_dirty;with need_tlb_flush's type being int and tlbs_dirty's type being long.
It means that
tlbs_dirtyis always used as int and the higher 32 bits is useless.It's probably worth noting in the changelog that it's _extremely_ unlikely this bug can cause problems in practice. It would require encountering
tlbs_dirtyon a 4 billion count boundary, and KVM would need to be using shadow paging or be running a nested guest.
Quoted 8 lines
Quoted 2 lines
We can just change need_tlb_flush's type to long to make full use of
tlbs_dirty.Hrm, this does solve the problem, but I'm not a fan of continuing to use an integer variable as a boolean. Rather than propagate
tlbs_dirtytoneed_tlb_flush, what if this bug fix patch checkstlbs_dirtydirectly, and then a follow up patch convertsneed_tlb_flushto a bool and removes the unnecessary initialization (see below).
Indeed, the compiler should be able to convert || to | if useful and valid (it may or may not do it depending on the sizes of types involved, but that's Someone Else's Problem and this is not really a path where every instruction matter).
Paolo
Quoted 62 lines
E.g. the net result of both patches would be:
virt/kvm/kvm_main.c +5
-5@@ -473,7 +473,8 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, const struct mmu_notifier_range *range) {struct kvm *kvm =mmu_notifier_to_kvm (mn); -int need_tlb_flush =0 , idx; +bool need_tlb_flush; +int idx; idx =srcu_read_lock (&kvm->srcu);spin_lock (&kvm->mmu_lock);@@ -483,11 +484,10 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,* count is also read inside the mmu_lock critical section. */ kvm->mmu_notifier_count++; - need_tlb_flush =kvm_unmap_hva_range (kvm, range->start, range->end, - range->flags); - need_tlb_flush |= kvm->tlbs_dirty; + need_tlb_flush = !!kvm_unmap_hva_range (kvm, range->start, range->end, + range->flags);/* we've to flush the tlb before the pages can be freed */ -if (need_tlb_flush) +if (need_tlb_flush || kvm->tlbs_dirty)kvm_flush_remote_tlbs (kvm);spin_unlock (&kvm->mmu_lock);Fixes a4ee1ca4a36e ("KVM: MMU: delay flush all tlbs on sync_page path")Quoted 10 lines
Signed-off-by Lai Jiangshan <[email protected]>--- virt/kvm/kvm_main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)virt/kvm/kvm_main.c +0
-0virt/kvm/kvm_main.c static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, · line 473
Quoted 5 lines
virt/kvm/kvm_main.c +1
-1@@ -470,7 +470,8 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, const struct mmu_notifier_range *range) {struct kvm *kvm =mmu_notifier_to_kvm (mn); -int need_tlb_flush =0 , idx; +long need_tlb_flush =0 ;
need_tlb_flushdoesn't need to be initialized here, it's explicitly set via the call tokvm_unmap_hva_range().Quoted 7 lines
virt/kvm/kvm_main.c +1
-0+int idx; idx =srcu_read_lock (&kvm->srcu);spin_lock (&kvm->mmu_lock);Signature · 1 line
2.19.1.6.gb485710b
PATCH v2 1/1 kvm: check tlbs_dirty directly
From: Lai Jiangshan <[email protected]>
In kvm_mmu_notifier_invalidate_range_start(), tlbs_dirty is used as:
need_tlb_flush |= kvm->tlbs_dirty;
with need_tlb_flush's type being int and tlbs_dirty's type being long.
It means that tlbs_dirty is always used as int and the higher 32 bits is useless. We need to check tlbs_dirty in a correct way and this change checks it directly without propagating it to need_tlb_flush.
And need_tlb_flush is changed to boolean because it is used as a boolean and its name starts with "need".
Note: it's _extremely_ unlikely this neglecting of higher 32 bits can cause problems in practice. It would require encountering tlbs_dirty on a 4 billion count boundary, and KVM would need to be using shadow paging or be running a nested guest.
---
Changed from V1:
Update the patch and the changelog as Sean Christopherson suggested.
virt/kvm/kvm_main.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
virt/kvm/kvm_main.c +5 -5
@@ -470,7 +470,8 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, const struct mmu_notifier_range *range) {struct kvm *kvm =mmu_notifier_to_kvm (mn); -int need_tlb_flush =0 , idx; +int idx; +bool need_tlb_flush; idx =srcu_read_lock (&kvm->srcu);spin_lock (&kvm->mmu_lock);
@@ -480,11 +481,10 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,* count is also read inside the mmu_lock critical section. */ kvm->mmu_notifier_count++; - need_tlb_flush =kvm_unmap_hva_range (kvm, range->start, range->end, - range->flags); - need_tlb_flush |= kvm->tlbs_dirty; + need_tlb_flush = !!kvm_unmap_hva_range (kvm, range->start, range->end, + range->flags);/* we've to flush the tlb before the pages can be freed */ -if (need_tlb_flush) +if (need_tlb_flush || kvm->tlbs_dirty)kvm_flush_remote_tlbs (kvm);spin_unlock (&kvm->mmu_lock);
Signature · 1 line
2.19.1.6.gb485710b
Sean Christopherson
reviewerRe: [PATCH V2] kvm: check tlbs_dirty directly
Note, you don't actually need to Cc [email protected] when sending the patch. If/when the patch is merged to Linus' tree, the stable tree maintainers, or more accurately their scripts, will automatically pick up the patch and apply it to the relevant stable trees.
You'll probably be getting the following letter from Greg KH any time now :-)
<formletter>
This is not the correct way to submit patches for inclusion in the stable kernel tree. Please read:
https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.
</formletter>
Quoted 33 lines · Lai Jiangshan
From: Lai Jiangshan <[email protected]>
In
kvm_mmu_notifier_invalidate_range_start(),tlbs_dirtyis used as:need_tlb_flush |= kvm->tlbs_dirty;with need_tlb_flush's type being int and tlbs_dirty's type being long.
It means that
tlbs_dirtyis always used as int and the higher 32 bits is useless. We need to checktlbs_dirtyin a correct way and this change checks it directly without propagating it toneed_tlb_flush.And
need_tlb_flushis changed to boolean because it is used as a boolean and its name starts with "need".Note: it's _extremely_ unlikely this neglecting of higher 32 bits can cause problems in practice. It would require encountering
tlbs_dirtyon a 4 billion count boundary, and KVM would need to be using shadow paging or be running a nested guest.Fixes a4ee1ca4a36e ("KVM: MMU: delay flush all tlbs on sync_page path")Signed-off-by Lai Jiangshan <[email protected]>---
Changed from V1:
Update the patch and the changelog as Sean Christopherson suggested.virt/kvm/kvm_main.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)virt/kvm/kvm_main.c +0
-0
virt/kvm/kvm_main.c static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, · line 474
Quoted 6 lines
virt/kvm/kvm_main.c +2
-1@@ -470,7 +470,8 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, const struct mmu_notifier_range *range) {struct kvm *kvm =mmu_notifier_to_kvm (mn); -int need_tlb_flush =0 , idx; +int idx; +bool need_tlb_flush;
It's a bit silly given how small this patch already is, but I do think this should be split into two patches so that the kvm->tlbs_dirty bug fix is fully isolated for backporting. I.e. patch 1/2 would simply be:
virt/kvm/kvm_main.c +1 -2
@@ -485,9 +485,8 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, kvm->mmu_notifier_count++; need_tlb_flush =kvm_unmap_hva_range (kvm, range->start, range->end, range->flags); - need_tlb_flush |= kvm->tlbs_dirty;/* we've to flush the tlb before the pages can be freed */ -if (need_tlb_flush) +if (need_tlb_flush || kvm->tlbs_dirty)kvm_flush_remote_tlbs (kvm);spin_unlock (&kvm->mmu_lock);
Quoted 21 lines
virt/kvm/kvm_main.c +3
-4idx =srcu_read_lock (&kvm->srcu);spin_lock (&kvm->mmu_lock);@@ -480,11 +481,10 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,* count is also read inside the mmu_lock critical section. */ kvm->mmu_notifier_count++; - need_tlb_flush =kvm_unmap_hva_range (kvm, range->start, range->end, - range->flags); - need_tlb_flush |= kvm->tlbs_dirty; + need_tlb_flush = !!kvm_unmap_hva_range (kvm, range->start, range->end, + range->flags);/* we've to flush the tlb before the pages can be freed */ -if (need_tlb_flush) +if (need_tlb_flush || kvm->tlbs_dirty)kvm_flush_remote_tlbs (kvm);spin_unlock (&kvm->mmu_lock);Signature · 1 line
2.19.1.6.gb485710b
Greg KH
maintainerRe: [PATCH V2] kvm: check tlbs_dirty directly
Quoted 6 lines · Sean Christopherson
Note, you don't actually need to Cc [email protected] when sending the patch. If/when the patch is merged to Linus' tree, the stable tree maintainers, or more accurately their scripts, will automatically pick up the patch and apply it to the relevant stable trees.
You'll probably be getting the following letter from Greg KH any time now :-)
Nope, this was fine, I don't mind seeing the patch before it hits Linus's tree (or any tree) at all. It lets me know what to look out for, nothing was done incorrectly here at all from what I can tell.
thanks,
greg k-h
Paolo Bonzini
maintainerRe: [PATCH V2] kvm: check tlbs_dirty directly
In reply to Sean ChristophersonQuoted 7 lines · Sean Christopherson
Note, you don't actually need to [email protected] when sending the patch. If/when the patch is merged to Linus' tree, the stable tree maintainers, or more accurately their scripts, will automatically pick up the patch and apply it to the relevant stable trees.
You'll probably be getting the following letter from Greg KH any time now:-)
Quoted 8 lines
<formletter>
This is not the correct way to submit patches for inclusion in the stable kernel tree. Please read:
https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.</formletter>
As far as I know, as long as he sees LKML or something else in Cc he assumes it's an indication _for the maintainer_. Don't underestimate the power of Greg's mailing list filters. :)
Paolo
PATCH v3 1/1 kvm: check tlbs_dirty directly
In reply to Sean ChristophersonFrom: Lai Jiangshan <[email protected]>
In kvm_mmu_notifier_invalidate_range_start(), tlbs_dirty is used as:
need_tlb_flush |= kvm->tlbs_dirty;
with need_tlb_flush's type being int and tlbs_dirty's type being long.
It means that tlbs_dirty is always used as int and the higher 32 bits is useless. We need to check tlbs_dirty in a correct way and this change checks it directly without propagating it to need_tlb_flush.
Note: it's _extremely_ unlikely this neglecting of higher 32 bits can cause problems in practice. It would require encountering tlbs_dirty on a 4 billion count boundary, and KVM would need to be using shadow paging or be running a nested guest.
---
Changed from V1:
Update the patch and the changelog as Sean Christopherson suggested.
Changed from v2:
don't change the type of need_tlb_flush
virt/kvm/kvm_main.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
virt/kvm/kvm_main.c +1 -2
@@ -482,9 +482,8 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, kvm->mmu_notifier_count++; need_tlb_flush =kvm_unmap_hva_range (kvm, range->start, range->end, range->flags); - need_tlb_flush |= kvm->tlbs_dirty;/* we've to flush the tlb before the pages can be freed */ -if (need_tlb_flush) +if (need_tlb_flush || kvm->tlbs_dirty)kvm_flush_remote_tlbs (kvm);spin_unlock (&kvm->mmu_lock);
Signature · 1 line
2.19.1.6.gb485710b
Re: [PATCH V3] kvm: check tlbs_dirty directly
Quoted 34 lines · Lai Jiangshan
From: Lai Jiangshan <[email protected]>
In
kvm_mmu_notifier_invalidate_range_start(),tlbs_dirtyis used as:need_tlb_flush |= kvm->tlbs_dirty;with need_tlb_flush's type being int and tlbs_dirty's type being long.
It means that
tlbs_dirtyis always used as int and the higher 32 bits is useless. We need to checktlbs_dirtyin a correct way and this change checks it directly without propagating it toneed_tlb_flush.Note: it's _extremely_ unlikely this neglecting of higher 32 bits can cause problems in practice. It would require encountering
tlbs_dirtyon a 4 billion count boundary, and KVM would need to be using shadow paging or be running a nested guest.Fixes a4ee1ca4a36e ("KVM: MMU: delay flush all tlbs on sync_page path")Signed-off-by Lai Jiangshan <[email protected]>---
Changed from V1:
Update the patch and the changelog as Sean Christopherson suggested.Changed from v2:
don't change the type ofneed_tlb_flushvirt/kvm/kvm_main.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)virt/kvm/kvm_main.c +0
-0
virt/kvm/kvm_main.c static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, · line 489
Quoted 10 lines
virt/kvm/kvm_main.c +1
-2@@ -482,9 +482,8 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, kvm->mmu_notifier_count++; need_tlb_flush =kvm_unmap_hva_range (kvm, range->start, range->end, range->flags); - need_tlb_flush |= kvm->tlbs_dirty;/* we've to flush the tlb before the pages can be freed */ -if (need_tlb_flush) +if (need_tlb_flush || kvm->tlbs_dirty)kvm_flush_remote_tlbs (kvm);spin_unlock (&kvm->mmu_lock);
Queued, thanks.
Paolo