Skip to content

Commit 8284765

Browse files
committed
KVM: Get reference to VM's address space in the async #PF worker
Get a reference to the target VM's address space in async_pf_execute() instead of gifting a reference from kvm_setup_async_pf(). Keeping the address space alive just to service an async #PF is counter-productive, i.e. if the process is exiting and all vCPUs are dead, then NOT doing get_user_pages_remote() and freeing the address space asap is desirable. Handling the mm reference entirely within async_pf_execute() also simplifies the async #PF flows as a whole, e.g. it's not immediately obvious when the worker task vs. the vCPU task is responsible for putting the gifted mm reference. Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com> Reviewed-by: Xu Yilun <yilun.xu@intel.com> Link: https://lore.kernel.org/r/20240110011533.503302-4-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 422eeb5 commit 8284765

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

include/linux/kvm_host.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ struct kvm_async_pf {
238238
struct list_head link;
239239
struct list_head queue;
240240
struct kvm_vcpu *vcpu;
241-
struct mm_struct *mm;
242241
gpa_t cr2_or_gpa;
243242
unsigned long addr;
244243
struct kvm_arch_async_pf arch;

virt/kvm/async_pf.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ static void async_pf_execute(struct work_struct *work)
4646
{
4747
struct kvm_async_pf *apf =
4848
container_of(work, struct kvm_async_pf, work);
49-
struct mm_struct *mm = apf->mm;
5049
struct kvm_vcpu *vcpu = apf->vcpu;
50+
struct mm_struct *mm = vcpu->kvm->mm;
5151
unsigned long addr = apf->addr;
5252
gpa_t cr2_or_gpa = apf->cr2_or_gpa;
5353
int locked = 1;
@@ -56,16 +56,24 @@ static void async_pf_execute(struct work_struct *work)
5656
might_sleep();
5757

5858
/*
59-
* This work is run asynchronously to the task which owns
60-
* mm and might be done in another context, so we must
61-
* access remotely.
59+
* Attempt to pin the VM's host address space, and simply skip gup() if
60+
* acquiring a pin fail, i.e. if the process is exiting. Note, KVM
61+
* holds a reference to its associated mm_struct until the very end of
62+
* kvm_destroy_vm(), i.e. the struct itself won't be freed before this
63+
* work item is fully processed.
6264
*/
63-
mmap_read_lock(mm);
64-
get_user_pages_remote(mm, addr, 1, FOLL_WRITE, NULL, &locked);
65-
if (locked)
66-
mmap_read_unlock(mm);
67-
mmput(mm);
65+
if (mmget_not_zero(mm)) {
66+
mmap_read_lock(mm);
67+
get_user_pages_remote(mm, addr, 1, FOLL_WRITE, NULL, &locked);
68+
if (locked)
69+
mmap_read_unlock(mm);
70+
mmput(mm);
71+
}
6872

73+
/*
74+
* Notify and kick the vCPU even if faulting in the page failed, e.g.
75+
* so that the vCPU can retry the fault synchronously.
76+
*/
6977
if (IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC))
7078
kvm_arch_async_page_present(vcpu, apf);
7179

@@ -131,10 +139,8 @@ void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu)
131139
#ifdef CONFIG_KVM_ASYNC_PF_SYNC
132140
flush_work(&work->work);
133141
#else
134-
if (cancel_work_sync(&work->work)) {
135-
mmput(work->mm);
142+
if (cancel_work_sync(&work->work))
136143
kmem_cache_free(async_pf_cache, work);
137-
}
138144
#endif
139145
spin_lock(&vcpu->async_pf.lock);
140146
}
@@ -205,8 +211,6 @@ bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
205211
work->cr2_or_gpa = cr2_or_gpa;
206212
work->addr = hva;
207213
work->arch = *arch;
208-
work->mm = current->mm;
209-
mmget(work->mm);
210214

211215
INIT_WORK(&work->work, async_pf_execute);
212216

0 commit comments

Comments
 (0)