Skip to content

Commit 7c54803

Browse files
yanzhao56bonzini
authored andcommitted
KVM: x86/mmu: Return RET_PF* instead of 1 in kvm_mmu_page_fault()
Return RET_PF* (excluding RET_PF_EMULATE/RET_PF_CONTINUE/RET_PF_INVALID) instead of 1 in kvm_mmu_page_fault(). The callers of kvm_mmu_page_fault() are KVM page fault handlers (i.e., npf_interception(), handle_ept_misconfig(), __vmx_handle_ept_violation(), kvm_handle_page_fault()). They either check if the return value is > 0 (as in npf_interception()) or pass it further to vcpu_run() to decide whether to break out of the kernel loop and return to the user when r <= 0. Therefore, returning any positive value is equivalent to returning 1. Warn if r == RET_PF_CONTINUE (which should not be a valid value) to ensure a positive return value. This is a preparation to allow TDX's EPT violation handler to check the RET_PF* value and retry internally for RET_PF_RETRY. No functional changes are intended. Signed-off-by: Yan Zhao <yan.y.zhao@intel.com> Message-ID: <20250113021138.18875-1-yan.y.zhao@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 2c3412e commit 7c54803

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

arch/x86/kvm/mmu/mmu.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6111,8 +6111,16 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err
61116111
else if (r == RET_PF_SPURIOUS)
61126112
vcpu->stat.pf_spurious++;
61136113

6114+
/*
6115+
* None of handle_mmio_page_fault(), kvm_mmu_do_page_fault(), or
6116+
* kvm_mmu_write_protect_fault() return RET_PF_CONTINUE.
6117+
* kvm_mmu_do_page_fault() only uses RET_PF_CONTINUE internally to
6118+
* indicate continuing the page fault handling until to the final
6119+
* page table mapping phase.
6120+
*/
6121+
WARN_ON_ONCE(r == RET_PF_CONTINUE);
61146122
if (r != RET_PF_EMULATE)
6115-
return 1;
6123+
return r;
61166124

61176125
emulate:
61186126
return x86_emulate_instruction(vcpu, cr2_or_gpa, emulation_type, insn,

arch/x86/kvm/mmu/mmu_internal.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault);
315315
* tracepoints via TRACE_DEFINE_ENUM() in mmutrace.h
316316
*
317317
* Note, all values must be greater than or equal to zero so as not to encroach
318-
* on -errno return values. Somewhat arbitrarily use '0' for CONTINUE, which
319-
* will allow for efficient machine code when checking for CONTINUE, e.g.
320-
* "TEST %rax, %rax, JNZ", as all "stop!" values are non-zero.
318+
* on -errno return values.
321319
*/
322320
enum {
323321
RET_PF_CONTINUE = 0,
@@ -329,6 +327,14 @@ enum {
329327
RET_PF_SPURIOUS,
330328
};
331329

330+
/*
331+
* Define RET_PF_CONTINUE as 0 to allow for
332+
* - efficient machine code when checking for CONTINUE, e.g.
333+
* "TEST %rax, %rax, JNZ", as all "stop!" values are non-zero,
334+
* - kvm_mmu_do_page_fault() to return other RET_PF_* as a positive value.
335+
*/
336+
static_assert(RET_PF_CONTINUE == 0);
337+
332338
static inline void kvm_mmu_prepare_memory_fault_exit(struct kvm_vcpu *vcpu,
333339
struct kvm_page_fault *fault)
334340
{

0 commit comments

Comments
 (0)