Skip to contentSkip to conversation

x86/hyper-v: Validate entire GVA range for non-canonical addresses during PV TLB flush

PATCH Applied Manuel Andreas 3 participants 5 messages Feb 19 – Mar 5

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

PATCH 1/1 x86/hyper-v: Validate entire GVA range for non-canonical addresses during PV TLB flush

In KVM guests with Hyper-V hypercalls enabled, the hypercalls HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST and HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX allow a guest to request invalidation of portions of a virtual TLB. For this, the hypercall parameter includes a list of GVAs that are supposed to be invalidated.

Currently, only the base GVA is checked to be canonical. In reality, this check needs to be performed for the entire range of GVAs. This still enables guests running on Intel hardware to trigger a WARN_ONCE in the host (see prior commit below).

This patch simply moves the check for non-canonical addresses to be performed for every single GVA of the supplied range. This should also be more in line with the Hyper-V specification, since, although unlikely, a range starting with an invalid GVA may still contain GVAs that are valid.

Fixes fa787ac07b3c ("KVM: x86/hyper-v: Skip non-canonical addresses during PV TLB flush")
---
 arch/x86/kvm/hyperv.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/hyperv.c +5 -4
index de92292eb1f5..f4f6accf1a33 100644
@@ -1981,16 +1981,17 @@ int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu)
 		if (entries[i] == KVM_HV_TLB_FLUSHALL_ENTRY)
 			goto out_flush_all;
 
-		if (is_noncanonical_invlpg_address(entries[i], vcpu))
-			continue;
-
 		/*
 		 * Lower 12 bits of 'address' encode the number of additional
 		 * pages to flush.
 		 */
 		gva = entries[i] & PAGE_MASK;
-		for (j = 0; j < (entries[i] & ~PAGE_MASK) + 1; j++)
+		for (j = 0; j < (entries[i] & ~PAGE_MASK) + 1; j++) {
+			if (is_noncanonical_invlpg_address(gva + j * PAGE_SIZE, vcpu))
+				continue;
+
 			kvm_x86_call(flush_tlb_gva)(vcpu, gva + j * PAGE_SIZE);
+		}
 
 		++vcpu->stat.tlb_flush;
 	}
Signature · 1 line
2.50.1
#1 lore raw

+Vitaly and Paolo

Please use scripts/get_maintainer.pl, otherwise your emails might not reach the right eyeballs.

Quoted 36 lines · Manuel Andreas

In KVM guests with Hyper-V hypercalls enabled, the hypercalls HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST and HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX allow a guest to request invalidation of portions of a virtual TLB. For this, the hypercall parameter includes a list of GVAs that are supposed to be invalidated.

Currently, only the base GVA is checked to be canonical. In reality, this check needs to be performed for the entire range of GVAs. This still enables guests running on Intel hardware to trigger a WARN_ONCE in the host (see prior commit below).

This patch simply moves the check for non-canonical addresses to be performed for every single GVA of the supplied range. This should also be more in line with the Hyper-V specification, since, although unlikely, a range starting with an invalid GVA may still contain GVAs that are valid.

Fixes fa787ac07b3c ("KVM: x86/hyper-v: Skip non-canonical addresses during PV TLB flush")
---
 arch/x86/kvm/hyperv.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/hyperv.c +0 -3
index de92292eb1f5..f4f6accf1a33 100644
@@ -1981,16 +1981,17 @@ int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu)
 		if (entries[i] == KVM_HV_TLB_FLUSHALL_ENTRY)
 			goto out_flush_all;
 
-		if (is_noncanonical_invlpg_address(entries[i], vcpu))
-			continue;
-
 		/*
 		 * Lower 12 bits of 'address' encode the number of additional

arch/x86/kvm/hyperv.c int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu) · line 1994

Quoted 10 lines
arch/x86/kvm/hyperv.c +5 -1
 		 * pages to flush.
 		 */
 		gva = entries[i] & PAGE_MASK;
-		for (j = 0; j < (entries[i] & ~PAGE_MASK) + 1; j++)
+		for (j = 0; j < (entries[i] & ~PAGE_MASK) + 1; j++) {
+			if (is_noncanonical_invlpg_address(gva + j * PAGE_SIZE, vcpu))
+				continue;
+
 			kvm_x86_call(flush_tlb_gva)(vcpu, gva + j * PAGE_SIZE);
+		}

Vitaly, can we treat the entire request as garbage and throw it away if any part isn't valid? Or do you think we should go with the more conservative approach as above?

arch/x86/kvm/hyperv.c +9 -5
index de92292eb1f5..f568f3d4f6e5 100644
@@ -1967,8 +1967,8 @@ int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu)
        struct kvm_vcpu_hv_tlb_flush_fifo *tlb_flush_fifo;
        struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
        u64 entries[KVM_HV_TLB_FLUSH_FIFO_SIZE];
+       gva_t gva, extra_pages;
        int i, j, count;
-       gva_t gva;
 
        if (!tdp_enabled || !hv_vcpu)
                return -EINVAL;
@@ -1978,18 +1978,22 @@ int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu)
        count = kfifo_out(&tlb_flush_fifo->entries, entries, KVM_HV_TLB_FLUSH_FIFO_SIZE);
 
        for (i = 0; i < count; i++) {
+
                if (entries[i] == KVM_HV_TLB_FLUSHALL_ENTRY)
                        goto out_flush_all;
 
-               if (is_noncanonical_invlpg_address(entries[i], vcpu))
-                       continue;
-
                /*
                 * Lower 12 bits of 'address' encode the number of additional
                 * pages to flush.
                 */
                gva = entries[i] & PAGE_MASK;
-               for (j = 0; j < (entries[i] & ~PAGE_MASK) + 1; j++)
+               extra_pages = (entries[i] & ~PAGE_MASK);
+
+               if (is_noncanonical_invlpg_address(gva, vcpu) ||
+                   is_noncanonical_invlpg_address(gva + extra_pages * PAGE_SIZE))
+                       continue;
+
+               for (j = 0; j < extra_pages + 1; j++)
                        kvm_x86_call(flush_tlb_gva)(vcpu, gva + j * PAGE_SIZE);
 
                ++vcpu->stat.tlb_flush;
maintainer
#2 lore raw
Quoted 48 lines · Sean Christopherson

+Vitaly and Paolo

Please use scripts/get_maintainer.pl, otherwise your emails might not reach the right eyeballs.

Quoted 42 lines · Manuel Andreas

In KVM guests with Hyper-V hypercalls enabled, the hypercalls HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST and HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX allow a guest to request invalidation of portions of a virtual TLB. For this, the hypercall parameter includes a list of GVAs that are supposed to be invalidated.

Currently, only the base GVA is checked to be canonical. In reality, this check needs to be performed for the entire range of GVAs. This still enables guests running on Intel hardware to trigger a WARN_ONCE in the host (see prior commit below).

This patch simply moves the check for non-canonical addresses to be performed for every single GVA of the supplied range. This should also be more in line with the Hyper-V specification, since, although unlikely, a range starting with an invalid GVA may still contain GVAs that are valid.

Fixes fa787ac07b3c ("KVM: x86/hyper-v: Skip non-canonical addresses during PV TLB flush")
---
 arch/x86/kvm/hyperv.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/hyperv.c +2 -4
index de92292eb1f5..f4f6accf1a33 100644
@@ -1981,16 +1981,17 @@ int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu)
 		if (entries[i] == KVM_HV_TLB_FLUSHALL_ENTRY)
 			goto out_flush_all;
 
-		if (is_noncanonical_invlpg_address(entries[i], vcpu))
-			continue;
-
 		/*
 		 * Lower 12 bits of 'address' encode the number of additional
 		 * pages to flush.
 		 */
 		gva = entries[i] & PAGE_MASK;
-		for (j = 0; j < (entries[i] & ~PAGE_MASK) + 1; j++)
+		for (j = 0; j < (entries[i] & ~PAGE_MASK) + 1; j++) {
+			if (is_noncanonical_invlpg_address(gva + j * PAGE_SIZE, vcpu))
Quoted 8 lines

arch/x86/kvm/hyperv.c int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu)

Quoted 4 lines
arch/x86/kvm/hyperv.c +3 -0
+				continue;
+
 			kvm_x86_call(flush_tlb_gva)(vcpu, gva + j * PAGE_SIZE);
+		}

Vitaly, can we treat the entire request as garbage and throw it away if any part isn't valid? Or do you think we should go with the more conservative approach as above?

I don't remember if I have ever seen real Windows trying to flush anything non-canonical at all but my gut feeling tells me we should rather play safe and use Manuel's 'conservative' approach. Also, this should be consistent with TLFS which says:

"Invalid GVAs (those that specify addresses beyond the end of the partition’s GVA space) are ignored."

i.e. it doesn't say 'Invalid GVA RANGES are ignored'.

Quoted 43 lines
arch/x86/kvm/hyperv.c +9 -5
index de92292eb1f5..f568f3d4f6e5 100644
@@ -1967,8 +1967,8 @@ int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu)
        struct kvm_vcpu_hv_tlb_flush_fifo *tlb_flush_fifo;
        struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
        u64 entries[KVM_HV_TLB_FLUSH_FIFO_SIZE];
+       gva_t gva, extra_pages;
        int i, j, count;
-       gva_t gva;
 
        if (!tdp_enabled || !hv_vcpu)
                return -EINVAL;
@@ -1978,18 +1978,22 @@ int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu)
        count = kfifo_out(&tlb_flush_fifo->entries, entries, KVM_HV_TLB_FLUSH_FIFO_SIZE);
 
        for (i = 0; i < count; i++) {
+
                if (entries[i] == KVM_HV_TLB_FLUSHALL_ENTRY)
                        goto out_flush_all;
 
-               if (is_noncanonical_invlpg_address(entries[i], vcpu))
-                       continue;
-
                /*
                 * Lower 12 bits of 'address' encode the number of additional
                 * pages to flush.
                 */
                gva = entries[i] & PAGE_MASK;
-               for (j = 0; j < (entries[i] & ~PAGE_MASK) + 1; j++)
+               extra_pages = (entries[i] & ~PAGE_MASK);
+
+               if (is_noncanonical_invlpg_address(gva, vcpu) ||
+                   is_noncanonical_invlpg_address(gva + extra_pages * PAGE_SIZE))
+                       continue;
+
+               for (j = 0; j < extra_pages + 1; j++)
                        kvm_x86_call(flush_tlb_gva)(vcpu, gva + j * PAGE_SIZE);
 
                ++vcpu->stat.tlb_flush;
Signature · 1 line
Vitaly
maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Manuel Andreas
Quoted 39 lines · Manuel Andreas

In KVM guests with Hyper-V hypercalls enabled, the hypercalls HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST and HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX allow a guest to request invalidation of portions of a virtual TLB. For this, the hypercall parameter includes a list of GVAs that are supposed to be invalidated.

Currently, only the base GVA is checked to be canonical. In reality, this check needs to be performed for the entire range of GVAs. This still enables guests running on Intel hardware to trigger a WARN_ONCE in the host (see prior commit below).

This patch simply moves the check for non-canonical addresses to be performed for every single GVA of the supplied range. This should also be more in line with the Hyper-V specification, since, although unlikely, a range starting with an invalid GVA may still contain GVAs that are valid.

Fixes fa787ac07b3c ("KVM: x86/hyper-v: Skip non-canonical addresses during PV TLB flush")
---
 arch/x86/kvm/hyperv.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/hyperv.c +0 -3
index de92292eb1f5..f4f6accf1a33 100644
@@ -1981,16 +1981,17 @@ int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu)
 		if (entries[i] == KVM_HV_TLB_FLUSHALL_ENTRY)
 			goto out_flush_all;
 
-		if (is_noncanonical_invlpg_address(entries[i], vcpu))
-			continue;
-
 		/*
 		 * Lower 12 bits of 'address' encode the number of additional
 		 * pages to flush.
 		 */
 		gva = entries[i] & PAGE_MASK;

arch/x86/kvm/hyperv.c int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu) · line 1997

Quoted 10 lines
arch/x86/kvm/hyperv.c +5 -1
-		for (j = 0; j < (entries[i] & ~PAGE_MASK) + 1; j++)
+		for (j = 0; j < (entries[i] & ~PAGE_MASK) + 1; j++) {
+			if (is_noncanonical_invlpg_address(gva + j * PAGE_SIZE, vcpu))
+				continue;
+
 			kvm_x86_call(flush_tlb_gva)(vcpu, gva + j * PAGE_SIZE);
+		}
 
 		++vcpu->stat.tlb_flush;
 	}
Signature · 1 line
Vitaly
maintainer Applied
#4 lore raw
In reply to Manuel Andreas
Quoted 6 lines · Manuel Andreas

In KVM guests with Hyper-V hypercalls enabled, the hypercalls HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST and HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX allow a guest to request invalidation of portions of a virtual TLB. For this, the hypercall parameter includes a list of GVAs that are supposed to be invalidated.

Quoted 6 lines

Currently, only the base GVA is checked to be canonical. In reality, this check needs to be performed for the entire range of GVAs. This still enables guests running on Intel hardware to trigger a WARN_ONCE in the host (see prior commit below).

[...]

Applied to kvm-x86 fixes, thanks!

[1/1] x86/hyper-v: Validate entire GVA range for non-canonical addresses during PV TLB flush

      https://github.com/kvm-x86/linux/commit/45692aa4a7ce

--
https://github.com/kvm-x86/linux/tree/next