Skip to content

Commit

Permalink
KVM: Nullify async #PF worker's "apf" pointer as soon as it might be …
Browse files Browse the repository at this point in the history
…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 <[email protected]>
Reviewed-by: Vitaly Kuznetsov <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Sean Christopherson <[email protected]>
  • Loading branch information
sean-jc committed Feb 6, 2024
1 parent 8284765 commit c2744ed
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions virt/kvm/async_pf.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ static void async_pf_execute(struct work_struct *work)
apf->vcpu = NULL;
spin_unlock(&vcpu->async_pf.lock);

if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC) && first)
kvm_arch_async_page_present_queued(vcpu);

/*
* apf may be freed by kvm_check_async_pf_completion() after
* this point
* The apf struct may be freed by kvm_check_async_pf_completion() as
* soon as the lock is dropped. Nullify it to prevent improper usage.
*/
apf = NULL;

if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC) && first)
kvm_arch_async_page_present_queued(vcpu);

trace_kvm_async_pf_completed(addr, cr2_or_gpa);

Expand Down

0 comments on commit c2744ed

Please sign in to comment.