Skip to content

Commit 63d28a2

Browse files
committed
KVM: x86/mmu: simplify kvm_tdp_mmu_map flow when guest has to retry
A removed SPTE is never present, hence the "if" in kvm_tdp_mmu_map only fails in the exact same conditions that the earlier loop tested in order to issue a "break". So, instead of checking twice the condition (upper level SPTEs could not be created or was frozen), just exit the loop with a goto---the usual poor-man C replacement for RAII early returns. While at it, do not use the "ret" variable for return values of functions that do not return a RET_PF_* enum. This is clearer and also makes it possible to initialize ret to RET_PF_RETRY. Suggested-by: Robert Hoo <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent c4b33d2 commit 63d28a2

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

arch/x86/kvm/mmu/tdp_mmu.c

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
11591159
struct kvm *kvm = vcpu->kvm;
11601160
struct tdp_iter iter;
11611161
struct kvm_mmu_page *sp;
1162-
int ret;
1162+
int ret = RET_PF_RETRY;
11631163

11641164
kvm_mmu_hugepage_adjust(vcpu, fault);
11651165

@@ -1168,23 +1168,25 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
11681168
rcu_read_lock();
11691169

11701170
tdp_mmu_for_each_pte(iter, mmu, fault->gfn, fault->gfn + 1) {
1171+
int r;
1172+
11711173
if (fault->nx_huge_page_workaround_enabled)
11721174
disallowed_hugepage_adjust(fault, iter.old_spte, iter.level);
11731175

11741176
if (iter.level == fault->goal_level)
11751177
break;
11761178

1177-
/* Step down into the lower level page table if it exists. */
1178-
if (is_shadow_present_pte(iter.old_spte) &&
1179-
!is_large_pte(iter.old_spte))
1180-
continue;
1181-
11821179
/*
11831180
* If SPTE has been frozen by another thread, just give up and
11841181
* retry, avoiding unnecessary page table allocation and free.
11851182
*/
11861183
if (is_removed_spte(iter.old_spte))
1187-
break;
1184+
goto retry;
1185+
1186+
/* Step down into the lower level page table if it exists. */
1187+
if (is_shadow_present_pte(iter.old_spte) &&
1188+
!is_large_pte(iter.old_spte))
1189+
continue;
11881190

11891191
/*
11901192
* The SPTE is either non-present or points to a huge page that
@@ -1196,13 +1198,17 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
11961198
sp->nx_huge_page_disallowed = fault->huge_page_disallowed;
11971199

11981200
if (is_shadow_present_pte(iter.old_spte))
1199-
ret = tdp_mmu_split_huge_page(kvm, &iter, sp, true);
1201+
r = tdp_mmu_split_huge_page(kvm, &iter, sp, true);
12001202
else
1201-
ret = tdp_mmu_link_sp(kvm, &iter, sp, true);
1203+
r = tdp_mmu_link_sp(kvm, &iter, sp, true);
12021204

1203-
if (ret) {
1205+
/*
1206+
* Also force the guest to retry the access if the upper level SPTEs
1207+
* aren't in place.
1208+
*/
1209+
if (r) {
12041210
tdp_mmu_free_sp(sp);
1205-
break;
1211+
goto retry;
12061212
}
12071213

12081214
if (fault->huge_page_disallowed &&
@@ -1213,18 +1219,10 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
12131219
}
12141220
}
12151221

1216-
/*
1217-
* Force the guest to retry the access if the upper level SPTEs aren't
1218-
* in place, or if the target leaf SPTE is frozen by another CPU.
1219-
*/
1220-
if (iter.level != fault->goal_level || is_removed_spte(iter.old_spte)) {
1221-
rcu_read_unlock();
1222-
return RET_PF_RETRY;
1223-
}
1224-
12251222
ret = tdp_mmu_map_handle_target_level(vcpu, fault, &iter);
1226-
rcu_read_unlock();
12271223

1224+
retry:
1225+
rcu_read_unlock();
12281226
return ret;
12291227
}
12301228

0 commit comments

Comments
 (0)