Skip to content

Commit 90e4447

Browse files
Lai Jiangshansean-jc
authored andcommitted
KVM: x86/mmu: Move the check in FNAME(sync_page) as kvm_sync_page_check()
Prepare to check mmu->sync_page pointer before calling it. Signed-off-by: Lai Jiangshan <jiangshan.ljs@antgroup.com> Link: https://lore.kernel.org/r/20230216154115.710033-3-jiangshanlai@gmail.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 753b43c commit 90e4447

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

arch/x86/kvm/mmu/mmu.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1914,10 +1914,51 @@ static bool sp_has_gptes(struct kvm_mmu_page *sp)
19141914
&(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)]) \
19151915
if ((_sp)->gfn != (_gfn) || !sp_has_gptes(_sp)) {} else
19161916

1917+
static bool kvm_sync_page_check(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1918+
{
1919+
union kvm_mmu_page_role root_role = vcpu->arch.mmu->root_role;
1920+
1921+
/*
1922+
* Ignore various flags when verifying that it's safe to sync a shadow
1923+
* page using the current MMU context.
1924+
*
1925+
* - level: not part of the overall MMU role and will never match as the MMU's
1926+
* level tracks the root level
1927+
* - access: updated based on the new guest PTE
1928+
* - quadrant: not part of the overall MMU role (similar to level)
1929+
*/
1930+
const union kvm_mmu_page_role sync_role_ign = {
1931+
.level = 0xf,
1932+
.access = 0x7,
1933+
.quadrant = 0x3,
1934+
.passthrough = 0x1,
1935+
};
1936+
1937+
/*
1938+
* Direct pages can never be unsync, and KVM should never attempt to
1939+
* sync a shadow page for a different MMU context, e.g. if the role
1940+
* differs then the memslot lookup (SMM vs. non-SMM) will be bogus, the
1941+
* reserved bits checks will be wrong, etc...
1942+
*/
1943+
if (WARN_ON_ONCE(sp->role.direct ||
1944+
(sp->role.word ^ root_role.word) & ~sync_role_ign.word))
1945+
return false;
1946+
1947+
return true;
1948+
}
1949+
1950+
static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1951+
{
1952+
if (!kvm_sync_page_check(vcpu, sp))
1953+
return -1;
1954+
1955+
return vcpu->arch.mmu->sync_page(vcpu, sp);
1956+
}
1957+
19171958
static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
19181959
struct list_head *invalid_list)
19191960
{
1920-
int ret = vcpu->arch.mmu->sync_page(vcpu, sp);
1961+
int ret = __kvm_sync_page(vcpu, sp);
19211962

19221963
if (ret < 0)
19231964
kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);

arch/x86/kvm/mmu/paging_tmpl.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -943,38 +943,11 @@ static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
943943
*/
944944
static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
945945
{
946-
union kvm_mmu_page_role root_role = vcpu->arch.mmu->root_role;
947946
int i;
948947
bool host_writable;
949948
gpa_t first_pte_gpa;
950949
bool flush = false;
951950

952-
/*
953-
* Ignore various flags when verifying that it's safe to sync a shadow
954-
* page using the current MMU context.
955-
*
956-
* - level: not part of the overall MMU role and will never match as the MMU's
957-
* level tracks the root level
958-
* - access: updated based on the new guest PTE
959-
* - quadrant: not part of the overall MMU role (similar to level)
960-
*/
961-
const union kvm_mmu_page_role sync_role_ign = {
962-
.level = 0xf,
963-
.access = 0x7,
964-
.quadrant = 0x3,
965-
.passthrough = 0x1,
966-
};
967-
968-
/*
969-
* Direct pages can never be unsync, and KVM should never attempt to
970-
* sync a shadow page for a different MMU context, e.g. if the role
971-
* differs then the memslot lookup (SMM vs. non-SMM) will be bogus, the
972-
* reserved bits checks will be wrong, etc...
973-
*/
974-
if (WARN_ON_ONCE(sp->role.direct ||
975-
(sp->role.word ^ root_role.word) & ~sync_role_ign.word))
976-
return -1;
977-
978951
first_pte_gpa = FNAME(get_level1_sp_gpa)(sp);
979952

980953
for (i = 0; i < SPTE_ENT_PER_PAGE; i++) {

0 commit comments

Comments
 (0)