Skip to content

Commit c2744ed

Browse files
committed
KVM: Nullify async #PF worker's "apf" pointer as soon as it might be freed
Nullify the async #PF worker's local "apf" pointer immediately after the point where the structure can be freed by the vCPU. The existing comment is helpful, but easy to overlook as there is no associated code. Update the comment to clarify that it can be freed by as soon as the lock is dropped, as "after this point" isn't strictly accurate, nor does it help understand what prevents the structure from being freed earlier. Reviewed-by: Xu Yilun <yilun.xu@intel.com> Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com> Link: https://lore.kernel.org/r/20240110011533.503302-5-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 8284765 commit c2744ed

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

virt/kvm/async_pf.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ static void async_pf_execute(struct work_struct *work)
8383
apf->vcpu = NULL;
8484
spin_unlock(&vcpu->async_pf.lock);
8585

86-
if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC) && first)
87-
kvm_arch_async_page_present_queued(vcpu);
88-
8986
/*
90-
* apf may be freed by kvm_check_async_pf_completion() after
91-
* this point
87+
* The apf struct may be freed by kvm_check_async_pf_completion() as
88+
* soon as the lock is dropped. Nullify it to prevent improper usage.
9289
*/
90+
apf = NULL;
91+
92+
if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC) && first)
93+
kvm_arch_async_page_present_queued(vcpu);
9394

9495
trace_kvm_async_pf_completed(addr, cr2_or_gpa);
9596

0 commit comments

Comments
 (0)