@@ -1800,7 +1800,14 @@ static u64 kvm_get_sparse_vp_set(struct kvm *kvm, struct kvm_hv_hcall *hc,
1800
1800
sparse_banks , consumed_xmm_halves , offset );
1801
1801
}
1802
1802
1803
- static void hv_tlb_flush_enqueue (struct kvm_vcpu * vcpu )
1803
+ static int kvm_hv_get_tlb_flush_entries (struct kvm * kvm , struct kvm_hv_hcall * hc , u64 entries [],
1804
+ int consumed_xmm_halves , gpa_t offset )
1805
+ {
1806
+ return kvm_hv_get_hc_data (kvm , hc , hc -> rep_cnt , hc -> rep_cnt ,
1807
+ entries , consumed_xmm_halves , offset );
1808
+ }
1809
+
1810
+ static void hv_tlb_flush_enqueue (struct kvm_vcpu * vcpu , u64 * entries , int count )
1804
1811
{
1805
1812
struct kvm_vcpu_hv_tlb_flush_fifo * tlb_flush_fifo ;
1806
1813
struct kvm_vcpu_hv * hv_vcpu = to_hv_vcpu (vcpu );
@@ -1811,24 +1818,64 @@ static void hv_tlb_flush_enqueue(struct kvm_vcpu *vcpu)
1811
1818
1812
1819
tlb_flush_fifo = & hv_vcpu -> tlb_flush_fifo ;
1813
1820
1814
- kfifo_in_spinlocked_noirqsave (& tlb_flush_fifo -> entries , & flush_all_entry ,
1815
- 1 , & tlb_flush_fifo -> write_lock );
1821
+ spin_lock (& tlb_flush_fifo -> write_lock );
1822
+
1823
+ /*
1824
+ * All entries should fit on the fifo leaving one free for 'flush all'
1825
+ * entry in case another request comes in. In case there's not enough
1826
+ * space, just put 'flush all' entry there.
1827
+ */
1828
+ if (count && entries && count < kfifo_avail (& tlb_flush_fifo -> entries )) {
1829
+ WARN_ON (kfifo_in (& tlb_flush_fifo -> entries , entries , count ) != count );
1830
+ goto out_unlock ;
1831
+ }
1832
+
1833
+ /*
1834
+ * Note: full fifo always contains 'flush all' entry, no need to check the
1835
+ * return value.
1836
+ */
1837
+ kfifo_in (& tlb_flush_fifo -> entries , & flush_all_entry , 1 );
1838
+
1839
+ out_unlock :
1840
+ spin_unlock (& tlb_flush_fifo -> write_lock );
1816
1841
}
1817
1842
1818
1843
int kvm_hv_vcpu_flush_tlb (struct kvm_vcpu * vcpu )
1819
1844
{
1820
1845
struct kvm_vcpu_hv_tlb_flush_fifo * tlb_flush_fifo ;
1821
1846
struct kvm_vcpu_hv * hv_vcpu = to_hv_vcpu (vcpu );
1847
+ u64 entries [KVM_HV_TLB_FLUSH_FIFO_SIZE ];
1848
+ int i , j , count ;
1849
+ gva_t gva ;
1822
1850
1823
- if (!hv_vcpu )
1851
+ if (!tdp_enabled || ! hv_vcpu )
1824
1852
return - EINVAL ;
1825
1853
1826
1854
tlb_flush_fifo = & hv_vcpu -> tlb_flush_fifo ;
1827
1855
1856
+ count = kfifo_out (& tlb_flush_fifo -> entries , entries , KVM_HV_TLB_FLUSH_FIFO_SIZE );
1857
+
1858
+ for (i = 0 ; i < count ; i ++ ) {
1859
+ if (entries [i ] == KVM_HV_TLB_FLUSHALL_ENTRY )
1860
+ goto out_flush_all ;
1861
+
1862
+ /*
1863
+ * Lower 12 bits of 'address' encode the number of additional
1864
+ * pages to flush.
1865
+ */
1866
+ gva = entries [i ] & PAGE_MASK ;
1867
+ for (j = 0 ; j < (entries [i ] & ~PAGE_MASK ) + 1 ; j ++ )
1868
+ static_call (kvm_x86_flush_tlb_gva )(vcpu , gva + j * PAGE_SIZE );
1869
+
1870
+ ++ vcpu -> stat .tlb_flush ;
1871
+ }
1872
+ return 0 ;
1873
+
1874
+ out_flush_all :
1828
1875
kfifo_reset_out (& tlb_flush_fifo -> entries );
1829
1876
1830
- /* Precise flushing isn't implemented yet . */
1831
- return - EOPNOTSUPP ;
1877
+ /* Fall back to full flush . */
1878
+ return - ENOSPC ;
1832
1879
}
1833
1880
1834
1881
static u64 kvm_hv_flush_tlb (struct kvm_vcpu * vcpu , struct kvm_hv_hcall * hc )
@@ -1837,11 +1884,21 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
1837
1884
struct hv_tlb_flush_ex flush_ex ;
1838
1885
struct hv_tlb_flush flush ;
1839
1886
DECLARE_BITMAP (vcpu_mask , KVM_MAX_VCPUS );
1887
+ /*
1888
+ * Normally, there can be no more than 'KVM_HV_TLB_FLUSH_FIFO_SIZE'
1889
+ * entries on the TLB flush fifo. The last entry, however, needs to be
1890
+ * always left free for 'flush all' entry which gets placed when
1891
+ * there is not enough space to put all the requested entries.
1892
+ */
1893
+ u64 __tlb_flush_entries [KVM_HV_TLB_FLUSH_FIFO_SIZE - 1 ];
1894
+ u64 * tlb_flush_entries ;
1840
1895
u64 valid_bank_mask ;
1841
1896
u64 sparse_banks [KVM_HV_MAX_SPARSE_VCPU_SET_BITS ];
1842
1897
struct kvm_vcpu * v ;
1843
1898
unsigned long i ;
1844
1899
bool all_cpus ;
1900
+ int consumed_xmm_halves = 0 ;
1901
+ gpa_t data_offset ;
1845
1902
1846
1903
/*
1847
1904
* The Hyper-V TLFS doesn't allow more than 64 sparse banks, e.g. the
@@ -1857,10 +1914,12 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
1857
1914
flush .address_space = hc -> ingpa ;
1858
1915
flush .flags = hc -> outgpa ;
1859
1916
flush .processor_mask = sse128_lo (hc -> xmm [0 ]);
1917
+ consumed_xmm_halves = 1 ;
1860
1918
} else {
1861
1919
if (unlikely (kvm_read_guest (kvm , hc -> ingpa ,
1862
1920
& flush , sizeof (flush ))))
1863
1921
return HV_STATUS_INVALID_HYPERCALL_INPUT ;
1922
+ data_offset = sizeof (flush );
1864
1923
}
1865
1924
1866
1925
trace_kvm_hv_flush_tlb (flush .processor_mask ,
@@ -1884,10 +1943,12 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
1884
1943
flush_ex .flags = hc -> outgpa ;
1885
1944
memcpy (& flush_ex .hv_vp_set ,
1886
1945
& hc -> xmm [0 ], sizeof (hc -> xmm [0 ]));
1946
+ consumed_xmm_halves = 2 ;
1887
1947
} else {
1888
1948
if (unlikely (kvm_read_guest (kvm , hc -> ingpa , & flush_ex ,
1889
1949
sizeof (flush_ex ))))
1890
1950
return HV_STATUS_INVALID_HYPERCALL_INPUT ;
1951
+ data_offset = sizeof (flush_ex );
1891
1952
}
1892
1953
1893
1954
trace_kvm_hv_flush_tlb_ex (flush_ex .hv_vp_set .valid_bank_mask ,
@@ -1902,26 +1963,44 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
1902
1963
if (hc -> var_cnt != hweight64 (valid_bank_mask ))
1903
1964
return HV_STATUS_INVALID_HYPERCALL_INPUT ;
1904
1965
1905
- if (all_cpus )
1906
- goto do_flush ;
1966
+ if (!all_cpus ) {
1967
+ if (!hc -> var_cnt )
1968
+ goto ret_success ;
1907
1969
1908
- if (!hc -> var_cnt )
1909
- goto ret_success ;
1970
+ if (kvm_get_sparse_vp_set (kvm , hc , sparse_banks ,
1971
+ consumed_xmm_halves , data_offset ))
1972
+ return HV_STATUS_INVALID_HYPERCALL_INPUT ;
1973
+ }
1974
+
1975
+ /*
1976
+ * Hyper-V TLFS doesn't explicitly forbid non-empty sparse vCPU
1977
+ * banks (and, thus, non-zero 'var_cnt') for the 'all vCPUs'
1978
+ * case (HV_GENERIC_SET_ALL). Always adjust data_offset and
1979
+ * consumed_xmm_halves to make sure TLB flush entries are read
1980
+ * from the correct offset.
1981
+ */
1982
+ data_offset += hc -> var_cnt * sizeof (sparse_banks [0 ]);
1983
+ consumed_xmm_halves += hc -> var_cnt ;
1984
+ }
1910
1985
1911
- if (kvm_get_sparse_vp_set (kvm , hc , sparse_banks , 2 ,
1912
- offsetof(struct hv_tlb_flush_ex ,
1913
- hv_vp_set .bank_contents )))
1986
+ if (hc -> code == HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE ||
1987
+ hc -> code == HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX ||
1988
+ hc -> rep_cnt > ARRAY_SIZE (__tlb_flush_entries )) {
1989
+ tlb_flush_entries = NULL ;
1990
+ } else {
1991
+ if (kvm_hv_get_tlb_flush_entries (kvm , hc , __tlb_flush_entries ,
1992
+ consumed_xmm_halves , data_offset ))
1914
1993
return HV_STATUS_INVALID_HYPERCALL_INPUT ;
1994
+ tlb_flush_entries = __tlb_flush_entries ;
1915
1995
}
1916
1996
1917
- do_flush :
1918
1997
/*
1919
1998
* vcpu->arch.cr3 may not be up-to-date for running vCPUs so we can't
1920
1999
* analyze it here, flush TLB regardless of the specified address space.
1921
2000
*/
1922
2001
if (all_cpus ) {
1923
2002
kvm_for_each_vcpu (i , v , kvm )
1924
- hv_tlb_flush_enqueue (v );
2003
+ hv_tlb_flush_enqueue (v , tlb_flush_entries , hc -> rep_cnt );
1925
2004
1926
2005
kvm_make_all_cpus_request (kvm , KVM_REQ_HV_TLB_FLUSH );
1927
2006
} else {
@@ -1931,7 +2010,7 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
1931
2010
v = kvm_get_vcpu (kvm , i );
1932
2011
if (!v )
1933
2012
continue ;
1934
- hv_tlb_flush_enqueue (v );
2013
+ hv_tlb_flush_enqueue (v , tlb_flush_entries , hc -> rep_cnt );
1935
2014
}
1936
2015
1937
2016
kvm_make_vcpus_request_mask (kvm , KVM_REQ_HV_TLB_FLUSH , vcpu_mask );
0 commit comments