KVM: guest_memfd: Elaborate on how release() vs. get_pfn() is safe against UAF
Subsystems (2)
Sean Christopherson
maintainerPATCH 1/1 KVM: guest_memfd: Elaborate on how release() vs. get_pfn() is safe against UAF
Add more context and information to the comment in kvm_gmem_release() that explains why there's no synchronization on RCU _or_ kvm->srcu. Point (b) from commit 67b43038ce14 ("KVM: guest_memfd: Remove RCU-protected attribute from slot->gmem.file")
b) kvm->srcu ensures that kvm_gmem_unbind() and freeing of a memslot occur after the memslot is no longer visible to kvm_gmem_get_pfn().
is especially difficult to fully grok, particularly in light of commit ae431059e75d ("KVM: guest_memfd: Remove bindings on memslot deletion when gmem is dying"), which addressed a race between unbind() and release().
No functional change intended.
--- virt/kvm/guest_memfd.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
virt/kvm/guest_memfd.c +14 -6
@@ -338,17 +338,25 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)* dereferencing the slot for existing bindings needs to be protected * against memslot updates, specifically so that unbind doesn't race * and free the memslot (kvm_gmem_get_file() will return NULL). -* -* Since .release is called only when the reference count is zero, -* after which file_ref_get() and get_file_active() fail, -* kvm_gmem_get_pfn() cannot be using the file concurrently. -* file_ref_put() provides a full barrier, and get_file_active() the -* matching acquire barrier. */ mutex_lock (&kvm->slots_lock);filemap_invalidate_lock (inode->i_mapping); +/* +* Note! synchronize_srcu() is _not_ needed after nullifying memslot +* bindings as slot->gmem.file cannot be set back to a non-null value +* without the memslot first being deleted. I.e. this relies on the +* synchronize_srcu_expedited() in kvm_swap_active_memslots() to ensure +* kvm_gmem_get_pfn() (which runs with kvm->srcu held for read) can't +* grab a reference to slot->gmem.file even if the struct file object +* is reallocated. +* +* file_ref_put() provides a full barrier, and __get_file_rcu() the +* matching acquire barrier, to ensure that kvm_gmem_get_file() (via +* __get_file_rcu()) sees refcount==0 or fails the "file reloaded" +* check (file != NULL due to nullifying the file pointer here). +*/ xa_for_each (&f->bindings, index, slot)WRITE_ONCE (slot->gmem.file,NULL );
base-commit: 16ec4fb4ac95d878b879192d280db2baeec43272
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Quoted 11 lines · Sean Christopherson
Add more context and information to the comment in
kvm_gmem_release()that explains why there's no synchronization on RCU _or_kvm->srcu. Point (b) from commit67b43038ce14("KVM:guest_memfd: Remove RCU-protected attribute fromslot->gmem.file")b)
kvm->srcuensures thatkvm_gmem_unbind()and freeing of a memslot occur after the memslot is no longer visible tokvm_gmem_get_pfn().is especially difficult to fully grok, particularly in light of commit
ae431059e75d("KVM:guest_memfd: Remove bindings on memslot deletion when gmem is dying"), which addressed a race betweenunbind()andrelease().
As mentioned in commit ae431059e75d ("KVM: guest_memfd: Remove bindings on memslot deletion when gmem is dying"), unbind() and release() are mutually exclusive, i.e., both protected by slots_lock, mentioning "race" here is confusing. So, that commit addressed the mishandling in unbind() after kvm_gmem_get_file() returning NULL?
Quoted 27 lines
No functional change intended.
Cc Yan Zhao <[email protected]>Cc Vishal Annapurve <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- virt/kvm/guest_memfd.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)virt/kvm/guest_memfd.c +0
-6@@ -338,17 +338,25 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)* dereferencing the slot for existing bindings needs to be protected * against memslot updates, specifically so that unbind doesn't race * and free the memslot (kvm_gmem_get_file() will return NULL). -* -* Since .release is called only when the reference count is zero, -* after which file_ref_get() and get_file_active() fail, -* kvm_gmem_get_pfn() cannot be using the file concurrently. -* file_ref_put() provides a full barrier, and get_file_active() the -* matching acquire barrier. */ mutex_lock (&kvm->slots_lock);
virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 354
Quoted 10 lines
virt/kvm/guest_memfd.c +8
-0filemap_invalidate_lock (inode->i_mapping); +/* +* Note! synchronize_srcu() is _not_ needed after nullifying memslot +* bindings as slot->gmem.file cannot be set back to a non-null value +* without the memslot first being deleted. I.e. this relies on the +* synchronize_srcu_expedited() in kvm_swap_active_memslots() to ensure +* kvm_gmem_get_pfn() (which runs with kvm->srcu held for read) can't +* grab a reference to slot->gmem.file even if the struct file object +* is reallocated.
Do you mean that
as kvm_gmem_get_pfn() can't find a stale slot, it can't grab reference to stale slot->gmem.file, even if slot->gmem.file has been set to a different value, i.e., after invoking unbind(), bind() ?
But I'm not sure why to put the kvm_gmem_get_pfn() relying on synchronize_srcu_expedited() in kvm_swap_active_memslots() in the comment of release().
Without the guard of kvm_gmem_get_file(), kvm_gmem_get_pfn() may need to provide some RCU read-critial section too for release() to wait by synchronize_srcu()?
So
* Since .release is called only when the reference count is zero,
* after which file_ref_get() and get_file_active() fail,
is still helpful?
Quoted 15 lines
virt/kvm/guest_memfd.c +6
-0+* +* file_ref_put() provides a full barrier, and __get_file_rcu() the +* matching acquire barrier, to ensure that kvm_gmem_get_file() (via +* __get_file_rcu()) sees refcount==0 or fails the "file reloaded" +* check (file != NULL due to nullifying the file pointer here). +*/ xa_for_each (&f->bindings, index, slot)
WRITE_ONCE(slot->gmem.file, NULL);base-commit:
16ec4fb4ac95d878b879192d280db2baeec43272Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
Yan Zhao <[email protected]> writes:
Coming here from https://lore.kernel.org/all/[email protected]/.
Quoted 9 lines · Sean Christopherson
Quoted 8 lines
Add more context and information to the comment in
kvm_gmem_release()that explains why there's no synchronization on RCU _or_kvm->srcu. Point (b) from commit67b43038ce14("KVM:guest_memfd: Remove RCU-protected attribute fromslot->gmem.file")b)
kvm->srcuensures thatkvm_gmem_unbind()and freeing of a memslot occur after the memslot is no longer visible tokvm_gmem_get_pfn().
Quoted 9 lines
Quoted 3 lines
is especially difficult to fully grok, particularly in light of commit
ae431059e75d("KVM:guest_memfd: Remove bindings on memslot deletion when gmem is dying"), which addressed a race betweenunbind()andrelease().As mentioned in commit
ae431059e75d("KVM:guest_memfd: Remove bindings on memslot deletion when gmem is dying"),unbind()andrelease()are mutually exclusive, i.e., both protected byslots_lock, mentioning "race" here is confusing. So, that commit addressed the mishandling inunbind()afterkvm_gmem_get_file()returning NULL?
I agree that the use of the word "race" here is odd since both unbind and release() are mutually exclusive and protected by slots_lock.
Looking at ae431059e75d again, there are really 3 changes:
1. Refactoring out __kvm_gmem_unbind(), which is mostly about setting bindings in the xarray to NULL and setting slot->gmem.file to NULL,
2. Checking that if slot->gmem.file is NULL, skip unbinding
3. Checking that if kvm_gmem_get_file() returns NULL, just unbind without taking filemap_invalidate_lock().
(2.) is explained in the comment
/* * Nothing to do if the underlying file was _already_ closed, as * kvm_gmem_release() invalidates and nullifies all bindings. */
The real fix is in (3.), I think? Could you explain how kvm_gmem_get_file() works? Why would the file be dying with the slots_lock making the unbind and release mutually exclusive?
Quoted 42 lines
Quoted 27 lines
No functional change intended.
Cc Yan Zhao <[email protected]>Cc Vishal Annapurve <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- virt/kvm/guest_memfd.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)virt/kvm/guest_memfd.c +0
-6@@ -338,17 +338,25 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)* dereferencing the slot for existing bindings needs to be protected * against memslot updates, specifically so that unbind doesn't race * and free the memslot (kvm_gmem_get_file() will return NULL). -* -* Since .release is called only when the reference count is zero, -* after which file_ref_get() and get_file_active() fail, -* kvm_gmem_get_pfn() cannot be using the file concurrently. -* file_ref_put() provides a full barrier, and get_file_active() the -* matching acquire barrier. */ mutex_lock (&kvm->slots_lock);virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 354
Quoted 10 lines
virt/kvm/guest_memfd.c +8
-0filemap_invalidate_lock (inode->i_mapping); +/* +* Note! synchronize_srcu() is _not_ needed after nullifying memslot +* bindings as slot->gmem.file cannot be set back to a non-null value +* without the memslot first being deleted. I.e. this relies on the +* synchronize_srcu_expedited() in kvm_swap_active_memslots() to ensure +* kvm_gmem_get_pfn() (which runs with kvm->srcu held for read) can't +* grab a reference to slot->gmem.file even if the struct file object +* is reallocated. Do you mean that
askvm_gmem_get_pfn()can't find a stale slot, it can't grab reference to staleslot->gmem.file, even ifslot->gmem.filehas been set to a different value, i.e., after invokingunbind(),bind()?
Quoted 3 lines
But I'm not sure why to put the
kvm_gmem_get_pfn()relying onsynchronize_srcu_expedited()inkvm_swap_active_memslots()in the comment ofrelease().
This is a good question, I think the comment can be improved to explain this.
Quoted 24 lines
Without the guard of
kvm_gmem_get_file(),kvm_gmem_get_pfn()may need to provide some RCU read-critial section too forrelease()to wait bysynchronize_srcu()?So
* Since .release is called only when the reference count is zero,
* after whichfile_ref_get()andget_file_active()fail,
is still helpful?Quoted 15 lines
virt/kvm/guest_memfd.c +6
-0+* +* file_ref_put() provides a full barrier, and __get_file_rcu() the +* matching acquire barrier, to ensure that kvm_gmem_get_file() (via +* __get_file_rcu()) sees refcount==0 or fails the "file reloaded" +* check (file != NULL due to nullifying the file pointer here). +*/ xa_for_each (&f->bindings, index, slot)
WRITE_ONCE(slot->gmem.file, NULL);base-commit:
16ec4fb4ac95d878b879192d280db2baeec43272--
2.52.0.rc1.455.g30608eb744-goog
Quoted 36 lines · Ackerley Tng
Yan Zhao <[email protected]> writes:
Coming here from https://lore.kernel.org/all/[email protected]/.
Quoted 9 lines · Sean Christopherson
Quoted 8 lines
Add more context and information to the comment in
kvm_gmem_release()that explains why there's no synchronization on RCU _or_kvm->srcu. Point (b) from commit67b43038ce14("KVM:guest_memfd: Remove RCU-protected attribute fromslot->gmem.file")b)
kvm->srcuensures thatkvm_gmem_unbind()and freeing of a memslot occur after the memslot is no longer visible tokvm_gmem_get_pfn().Quoted 9 lines
Quoted 3 lines
is especially difficult to fully grok, particularly in light of commit
ae431059e75d("KVM:guest_memfd: Remove bindings on memslot deletion when gmem is dying"), which addressed a race betweenunbind()andrelease().As mentioned in commit
ae431059e75d("KVM:guest_memfd: Remove bindings on memslot deletion when gmem is dying"),unbind()andrelease()are mutually exclusive, i.e., both protected byslots_lock, mentioning "race" here is confusing. So, that commit addressed the mishandling inunbind()afterkvm_gmem_get_file()returning NULL?I agree that the use of the word "race" here is odd since both unbind and
release()are mutually exclusive and protected byslots_lock.Looking at
ae431059e75dagain, there are really 3 changes:1. Refactoring out
__kvm_gmem_unbind(), which is mostly about setting bindings in the xarray to NULL and settingslot->gmem.fileto NULL,
2. Checking that ifslot->gmem.fileis NULL, skip unbinding
3. Checking that ifkvm_gmem_get_file()returns NULL, just unbind without takingfilemap_invalidate_lock().(2.) is explained in the comment
Quoted 8 lines
/* * Nothing to do if the underlying file was _already_ closed, as * kvm_gmem_release() invalidates and nullifies all bindings. */The real fix is in (3.), I think? Could you explain how
kvm_gmem_get_file()works? Why would the file be dying with theslots_lockmaking the unbind and release mutually exclusive?
Below is my understanding:
kvm_gmem_unbind() is invoked under slots_lock. kvm_gmem_release() holds slots_lock before it accesses any slot and sets slot->gmem.file to NULL.
When kvm_gmem_get_file() returns NULL, it could be
1) kvm_gmem_release() is to be invoked soon.
2) kvm_gmem_release() is being invoked, before holding slots_lock.
3) kvm_gmem_release() is being invoked, holding slots_lock.
4) kvm_gmem_release() is being invoked, after releasing slots_lock.
5) kvm_gmem_release() has been invoked and completed.
So, when kvm_gmem_unbind() finds slot->gmem.file != NULL while kvm_gmem_get_file() returns NULL, it could only be 1) or 2). And at that point, the remaining user of the slot->gmem.file should only be kvm_gmem_release().
For both 1) and 2), kvm_gmem_unbind() and kvm_gmem_release() can't operate on f->bindings simultaneously. kvm_gmem_unbind() needs to remove the slot from slot->gmem.file's binding. Otherwise, after kvm_gmem_unbind() returns, the slot will be deleted and kvm_gmem_release() will later hold slots_lock and cause use-after-free.
Quoted 42 lines · Yan Zhao
Quoted 41 lines · Ackerley Tng
Yan Zhao <[email protected]> writes:
Coming here from https://lore.kernel.org/all/[email protected]/.
Quoted 9 lines · Sean Christopherson
Quoted 8 lines
Add more context and information to the comment in
kvm_gmem_release()that explains why there's no synchronization on RCU _or_kvm->srcu. Point (b) from commit67b43038ce14("KVM:guest_memfd: Remove RCU-protected attribute fromslot->gmem.file")b)
kvm->srcuensures thatkvm_gmem_unbind()and freeing of a memslot occur after the memslot is no longer visible tokvm_gmem_get_pfn().Quoted 9 lines
Quoted 3 lines
is especially difficult to fully grok, particularly in light of commit
ae431059e75d("KVM:guest_memfd: Remove bindings on memslot deletion when gmem is dying"), which addressed a race betweenunbind()andrelease().As mentioned in commit
ae431059e75d("KVM:guest_memfd: Remove bindings on memslot deletion when gmem is dying"),unbind()andrelease()are mutually exclusive, i.e., both protected byslots_lock, mentioning "race" here is confusing. So, that commit addressed the mishandling inunbind()afterkvm_gmem_get_file()returning NULL?I agree that the use of the word "race" here is odd since both unbind and
release()are mutually exclusive and protected byslots_lock.Looking at
ae431059e75dagain, there are really 3 changes:1. Refactoring out
__kvm_gmem_unbind(), which is mostly about setting bindings in the xarray to NULL and settingslot->gmem.fileto NULL,
2. Checking that ifslot->gmem.fileis NULL, skip unbinding
3. Checking that ifkvm_gmem_get_file()returns NULL, just unbind without takingfilemap_invalidate_lock().(2.) is explained in the comment
/* * Nothing to do if the underlying file was _already_ closed, as * kvm_gmem_release() invalidates and nullifies all bindings. */
Quoted 5 lines
Quoted 3 lines
The real fix is in (3.), I think? Could you explain how
kvm_gmem_get_file()works? Why would the file be dying with theslots_lockmaking the unbind and release mutually exclusive?Below is my understanding:
Thanks for explaining!
Quoted 11 lines
kvm_gmem_unbind()is invoked underslots_lock.kvm_gmem_release()holdsslots_lockbefore it accesses any slot and setsslot->gmem.fileto NULL.When
kvm_gmem_get_file()returns NULL, it could be
1)kvm_gmem_release()is to be invoked soon.
2)kvm_gmem_release()is being invoked, before holdingslots_lock.
3)kvm_gmem_release()is being invoked, holdingslots_lock.
4)kvm_gmem_release()is being invoked, after releasingslots_lock.
5)kvm_gmem_release()has been invoked and completed.
Quoted 2 lines
So, when
kvm_gmem_unbind()findsslot->gmem.file!= NULL whilekvm_gmem_get_file()returns NULL, it could only be 1) or 2). And at
To clarify, 3, 4, 5 are not possible because kvm_gmem_unbind() is called with slots_lock held, right?
Quoted 8 lines
that point,
the remaining user of theslot->gmem.fileshould only bekvm_gmem_release().For both 1) and 2),
kvm_gmem_unbind()andkvm_gmem_release()can't operate onf->bindingssimultaneously.kvm_gmem_unbind()needs to remove the slot from slot->gmem.file's binding. Otherwise, afterkvm_gmem_unbind()returns, the slot will be deleted andkvm_gmem_release()will later holdslots_lockand cause use-after-free.
Thanks, this makes sense. The purpose of using kvm_gmem_get_file() is that if the file is _being_ closed, as stated in the comment in kvm_gmem_unbind(), kvm_gmem_get_file() would find the refcount to be 0 and return NULL. (The missing part for me was that _being_ closed leads to kvm_gmem_get_file() finding a 0 refcount.)
Annotating all of these:
Quoted 7 lines
void
kvm_gmem_unbind(struct kvm_memory_slot *slot){ /* * Nothing to do if the underlying file was _already_ closed, as * kvm_gmem_release() invalidates and nullifies all bindings. */
Above comment rephrased: !slot->gmem.file means kvm_gmem_release() had already taken slots_lock, iterated all bindings and set slot->gmem.file to NULL, and dropped slots_lock.
Quoted 4 lines
if (!slot->gmem.file) return;
If we get here, we know for sure that slot->gmem.file is valid (not freed) and can be dereferenced safely. We know this because kvm_gmem_release() is the place that frees slot->gmem.file, and if there was any chance of slot->gmem.file being freed, slot->gmem.file would have been NULL-ed first.
Hence we can also safely proceed to do filemap_invalidate_lock(slot->gmem.file->f_mapping), but as described in ae431059e75d, file->f_mapping is not under KVM's purview. A future change could NULL out file->f_mapping before .release() is called, for example, and break the assumption that gmem.file->f_mapping is still valid. Hence, rely on taking a reference on gmem.file from slot instead, which is within KVM's control.
Quoted 2 lines
CLASS(gmem_get_file, file)(slot);
If we do get the reference, we're good and we can take filemap_invalidate_lock() and then unbind.
Quoted 2 lines
/* * However, if the file is _being_ closed, then the bindings need to be
If we don't get the reference, then the file is _being_ closed, so the file's refcount is already 0, but kvm_gmem_release() hasn't yet been called.
Later, when kvm_gmem_release() is called, it will iterate bindings and then dereference slot. Hence, kvm_gmem_release() must not see the slot - the slot must be removed from bindings. Hence we must unbind.
Unbinding without taking filemap_invalidate_lock() is fine since if !file, we're sure there's only one last user of file - kvm_gmem_release(), and kvm_gmem_release() is locked out by slots_lock.
Quoted 4 lines
* removed as
kvm_gmem_release()might not run until after the memslot
* is freed. Note, modifying the bindings is safe even though the file
* is dying askvm_gmem_release()nullifiesslot->gmem.fileunder
*slots_lock, and only puts its reference to KVM after destroying all
Quoted 9 lines
* bindings. I.e. reaching this point means
kvm_gmem_release()hasn't
* yet destroyed the bindings or freed thegmem_file, and can't do so
* until the caller dropsslots_lock.
*/
if (!file) {
__kvm_gmem_unbind(slot, slot->gmem.file->private_data); return;
}
If we did get the reference, this is the most straightforward case. Take the filemap_invalidate_lock(), unbind.
Quoted 4 lines
filemap_invalidate_lock(file->f_mapping); __kvm_gmem_unbind(slot, file->private_data); filemap_invalidate_unlock(file->f_mapping); }
Quoted 63 lines · Ackerley Tng
Quoted 42 lines · Yan Zhao
Quoted 41 lines · Ackerley Tng
Yan Zhao <[email protected]> writes:
Coming here from https://lore.kernel.org/all/[email protected]/.
Quoted 9 lines · Sean Christopherson
Quoted 8 lines
Add more context and information to the comment in
kvm_gmem_release()that explains why there's no synchronization on RCU _or_kvm->srcu. Point (b) from commit67b43038ce14("KVM:guest_memfd: Remove RCU-protected attribute fromslot->gmem.file")b)
kvm->srcuensures thatkvm_gmem_unbind()and freeing of a memslot occur after the memslot is no longer visible tokvm_gmem_get_pfn().Quoted 9 lines
Quoted 3 lines
is especially difficult to fully grok, particularly in light of commit
ae431059e75d("KVM:guest_memfd: Remove bindings on memslot deletion when gmem is dying"), which addressed a race betweenunbind()andrelease().As mentioned in commit
ae431059e75d("KVM:guest_memfd: Remove bindings on memslot deletion when gmem is dying"),unbind()andrelease()are mutually exclusive, i.e., both protected byslots_lock, mentioning "race" here is confusing. So, that commit addressed the mishandling inunbind()afterkvm_gmem_get_file()returning NULL?I agree that the use of the word "race" here is odd since both unbind and
release()are mutually exclusive and protected byslots_lock.Looking at
ae431059e75dagain, there are really 3 changes:1. Refactoring out
__kvm_gmem_unbind(), which is mostly about setting bindings in the xarray to NULL and settingslot->gmem.fileto NULL,
2. Checking that ifslot->gmem.fileis NULL, skip unbinding
3. Checking that ifkvm_gmem_get_file()returns NULL, just unbind without takingfilemap_invalidate_lock().(2.) is explained in the comment
/* * Nothing to do if the underlying file was _already_ closed, as * kvm_gmem_release() invalidates and nullifies all bindings. */Quoted 5 lines
Quoted 3 lines
The real fix is in (3.), I think? Could you explain how
kvm_gmem_get_file()works? Why would the file be dying with theslots_lockmaking the unbind and release mutually exclusive?Below is my understanding:
Thanks for explaining!
Quoted 11 lines
kvm_gmem_unbind()is invoked underslots_lock.kvm_gmem_release()holdsslots_lockbefore it accesses any slot and setsslot->gmem.fileto NULL.When
kvm_gmem_get_file()returns NULL, it could be
1)kvm_gmem_release()is to be invoked soon.
2)kvm_gmem_release()is being invoked, before holdingslots_lock.
3)kvm_gmem_release()is being invoked, holdingslots_lock.
4)kvm_gmem_release()is being invoked, after releasingslots_lock.
5)kvm_gmem_release()has been invoked and completed.
Quoted 5 lines
Quoted 2 lines
So, when
kvm_gmem_unbind()findsslot->gmem.file!= NULL whilekvm_gmem_get_file()returns NULL, it could only be 1) or 2). And atTo clarify, 3, 4, 5 are not possible because
kvm_gmem_unbind()is called withslots_lockheld, right?
Yes.
Quoted 9 lines
Quoted 8 lines
that point,
the remaining user of theslot->gmem.fileshould only bekvm_gmem_release().For both 1) and 2),
kvm_gmem_unbind()andkvm_gmem_release()can't operate onf->bindingssimultaneously.kvm_gmem_unbind()needs to remove the slot from slot->gmem.file's binding. Otherwise, afterkvm_gmem_unbind()returns, the slot will be deleted andkvm_gmem_release()will later holdslots_lockand cause use-after-free.
Quoted 8 lines
Thanks, this makes sense. The purpose of using
kvm_gmem_get_file()is that if the file is _being_ closed, as stated in the comment inkvm_gmem_unbind(),kvm_gmem_get_file()would find the refcount to be 0 and return NULL. (The missing part for me was that _being_ closed leads tokvm_gmem_get_file()finding a 0 refcount.)Annotating all of these:
LGTM.
Sean Christopherson
maintainerQuoted 9 lines · Yan Zhao
Quoted 8 lines · Sean Christopherson
Add more context and information to the comment in
kvm_gmem_release()that explains why there's no synchronization on RCU _or_kvm->srcu. Point (b) from commit67b43038ce14("KVM:guest_memfd: Remove RCU-protected attribute fromslot->gmem.file")b)
kvm->srcuensures thatkvm_gmem_unbind()and freeing of a memslot occur after the memslot is no longer visible tokvm_gmem_get_pfn().
Quoted 8 lines
Quoted 3 lines
is especially difficult to fully grok, particularly in light of commit
ae431059e75d("KVM:guest_memfd: Remove bindings on memslot deletion when gmem is dying"), which addressed a race betweenunbind()andrelease().As mentioned in commit
ae431059e75d("KVM:guest_memfd: Remove bindings on memslot deletion when gmem is dying"),unbind()andrelease()are mutually exclusive, i.e., both protected byslots_lock, mentioning "race" here is confusing. So, that commit addressed the mishandling inunbind()afterkvm_gmem_get_file()returning NULL?
Yes, that's the race. Just because two chunks of code are mutually exclusive doesn't mean there's no race, it just changes the granularity of the race, and/or how it manifests.
I consider it a race because in the failing case, there's simply no way for release() and unbind() to run sequentially. I.e. the only way to encounter the bug was to run two operations concurrently, *and* for a specific ordering to occur.
Quoted 29 lines
Quoted 29 lines
No functional change intended.
Cc Yan Zhao <[email protected]>Cc Vishal Annapurve <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- virt/kvm/guest_memfd.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)virt/kvm/guest_memfd.c +0
-6@@ -338,17 +338,25 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)* dereferencing the slot for existing bindings needs to be protected * against memslot updates, specifically so that unbind doesn't race * and free the memslot (kvm_gmem_get_file() will return NULL). -* -* Since .release is called only when the reference count is zero, -* after which file_ref_get() and get_file_active() fail, -* kvm_gmem_get_pfn() cannot be using the file concurrently. -* file_ref_put() provides a full barrier, and get_file_active() the -* matching acquire barrier. */ mutex_lock (&kvm->slots_lock);filemap_invalidate_lock (inode->i_mapping);
Quoted 11 lines
virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 354
Quoted 8 lines
virt/kvm/guest_memfd.c +8
-0+/* +* Note! synchronize_srcu() is _not_ needed after nullifying memslot +* bindings as slot->gmem.file cannot be set back to a non-null value +* without the memslot first being deleted. I.e. this relies on the +* synchronize_srcu_expedited() in kvm_swap_active_memslots() to ensure +* kvm_gmem_get_pfn() (which runs with kvm->srcu held for read) can't +* grab a reference to slot->gmem.file even if the struct file object +* is reallocated. Do you mean that as
kvm_gmem_get_pfn()can't find a stale slot, it can't grab reference to staleslot->gmem.file, even ifslot->gmem.filehas been set to a different value, i.e., after invokingunbind(),bind()?
I was specifically trying to say this:
1. slot->gmem.file == old ==
synchronize_srcu() |===> window #1 for readers
2. slot->gmem.file == NULL ==
synchronize_srcu() |===> window #2 for readers 3. slot->gmem.file == new ==
kvm_gmem_get_pfn() can run in two "windows". In the window #1, it can see @old or NULL. In window #2, it can see NULL or @new. The synchronization ensures it can't grab a reference to @old once window #2 has been opened.
Quoted 5 lines
But I'm not sure why to put the
kvm_gmem_get_pfn()relying onsynchronize_srcu_expedited()inkvm_swap_active_memslots()in the comment ofrelease(). Without the guard ofkvm_gmem_get_file(),kvm_gmem_get_pfn()may need to provide some RCU read-critial section too forrelease()to wait bysynchronize_srcu()?
No, what I'm saying is that without synchronize_srcu() to create distinct window #1 and window #2 above, KVM would need this:
kvm/guest_memfd.c +2 -0
@@ -347,6 +347,8 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)xa_for_each (&f->bindings, index, slot)WRITE_ONCE (slot->gmem.file,NULL ); +synchronize_srcu (); +/* * All in-flight operations are gone and new bindings can be created. * Zap all SPTEs pointed at by this file. Do not free the backing
Otherwise there is no guarantee in-flight readers can't reach the old slot->gmem.file.
Quoted 19 lines · Sean Christopherson
Quoted 9 lines · Yan Zhao
Quoted 8 lines · Sean Christopherson
Add more context and information to the comment in
kvm_gmem_release()that explains why there's no synchronization on RCU _or_kvm->srcu. Point (b) from commit67b43038ce14("KVM:guest_memfd: Remove RCU-protected attribute fromslot->gmem.file")b)
kvm->srcuensures thatkvm_gmem_unbind()and freeing of a memslot occur after the memslot is no longer visible tokvm_gmem_get_pfn().Quoted 8 lines
Quoted 3 lines
is especially difficult to fully grok, particularly in light of commit
ae431059e75d("KVM:guest_memfd: Remove bindings on memslot deletion when gmem is dying"), which addressed a race betweenunbind()andrelease().As mentioned in commit
ae431059e75d("KVM:guest_memfd: Remove bindings on memslot deletion when gmem is dying"),unbind()andrelease()are mutually exclusive, i.e., both protected byslots_lock, mentioning "race" here is confusing. So, that commit addressed the mishandling inunbind()afterkvm_gmem_get_file()returning NULL?
Quoted 8 lines
Yes, that's the race. Just because two chunks of code are mutually exclusive doesn't mean there's no race, it just changes the granularity of the race, and/or how it manifests.
I consider it a race because in the failing case, there's simply no way for
release()andunbind()to run sequentially. I.e. the only way to encounter the bug was to run two operations concurrently, *and* for a specific ordering to occur.
Thanks for the explanation!
Quoted 44 lines
Quoted 29 lines
Quoted 29 lines
No functional change intended.
Cc Yan Zhao <[email protected]>Cc Vishal Annapurve <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- virt/kvm/guest_memfd.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)virt/kvm/guest_memfd.c +0
-6@@ -338,17 +338,25 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)* dereferencing the slot for existing bindings needs to be protected * against memslot updates, specifically so that unbind doesn't race * and free the memslot (kvm_gmem_get_file() will return NULL). -* -* Since .release is called only when the reference count is zero, -* after which file_ref_get() and get_file_active() fail, -* kvm_gmem_get_pfn() cannot be using the file concurrently. -* file_ref_put() provides a full barrier, and get_file_active() the -* matching acquire barrier. */ mutex_lock (&kvm->slots_lock);filemap_invalidate_lock (inode->i_mapping);Quoted 11 lines
virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 354
Quoted 8 lines
virt/kvm/guest_memfd.c +8
-0+/* +* Note! synchronize_srcu() is _not_ needed after nullifying memslot +* bindings as slot->gmem.file cannot be set back to a non-null value +* without the memslot first being deleted. I.e. this relies on the +* synchronize_srcu_expedited() in kvm_swap_active_memslots() to ensure +* kvm_gmem_get_pfn() (which runs with kvm->srcu held for read) can't +* grab a reference to slot->gmem.file even if the struct file object +* is reallocated. Do you mean that as
kvm_gmem_get_pfn()can't find a stale slot, it can't grab reference to staleslot->gmem.file, even ifslot->gmem.filehas been set to a different value, i.e., after invokingunbind(),bind()?I was specifically trying to say this:
1.
slot->gmem.file== old ==
Quoted 8 lines
synchronize_srcu() |===> window #1 for readers 2. slot->gmem.file == NULL == synchronize_srcu() |===> window #2 for readers3.
slot->gmem.file== new ==
kvm_gmem_get_pfn()can run in two "windows". In the window #1, it can see @old or NULL. In window #2, it can see NULL or @new. The synchronization ensures it can't grab a reference to @old once window #2 has been opened.
Thanks for the clarification.
Quoted 23 lines
Quoted 5 lines
But I'm not sure why to put the
kvm_gmem_get_pfn()relying onsynchronize_srcu_expedited()inkvm_swap_active_memslots()in the comment ofrelease(). Without the guard ofkvm_gmem_get_file(),kvm_gmem_get_pfn()may need to provide some RCU read-critial section too forrelease()to wait bysynchronize_srcu()?No, what I'm saying is that without
synchronize_srcu()to create distinct window #1 and window #2 above, KVM would need this:kvm/guest_memfd.c +2
-0@@ -347,6 +347,8 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)xa_for_each (&f->bindings, index, slot)WRITE_ONCE (slot->gmem.file,NULL ); +synchronize_srcu (); +/* * All in-flight operations are gone and new bindings can be created. * Zap all SPTEs pointed at by this file. Do not free the backing
Quoted 2 lines
Otherwise there is no guarantee in-flight readers can't reach the old
slot->gmem.file.
Hmm, however, if the reader (kvm_gmem_get_pfn()) has grabbed a reference to @old, kvm_gmem_release() can't come; if kvm_gmem_release() comes, kvm_gmem_get_pfn() should not have been able to successfully invoke get_file_active() on @old. After kvm_gmem_release() returns, any subsequent calls to get_file_active() on @old should fail and return NULL.
So, even if the reader can see @old, we're still fine without synchronize_srcu(), as get_file_active() and SLAB_TYPESAFE_BY_RCU should have provide the safety.
Am I missing something?
Sean Christopherson
maintainerQuoted 57 lines · Yan Zhao
Quoted 15 lines · Sean Christopherson
Quoted 11 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+ /* + * Note! synchronize_srcu() is _not_ needed after nullifying memslot + * bindings as slot->gmem.file cannot be set back to a non-null value + * without the memslot first being deleted. I.e. this relies on the + * synchronize_srcu_expedited() in kvm_swap_active_memslots() to ensure + * kvm_gmem_get_pfn() (which runs with kvm->srcu held for read) can't + * grab a reference to slot->gmem.file even if the struct file object + * is reallocated.Do you mean that as
kvm_gmem_get_pfn()can't find a stale slot, it can't grab reference to staleslot->gmem.file, even ifslot->gmem.filehas been set to a different value, i.e., after invokingunbind(),bind()?I was specifically trying to say this:
1.
slot->gmem.file== old ==Quoted 8 lines
synchronize_srcu() |===> window #1 for readers 2. slot->gmem.file == NULL == synchronize_srcu() |===> window #2 for readers3.
slot->gmem.file== new ==
kvm_gmem_get_pfn()can run in two "windows". In the window #1, it can see @old or NULL. In window #2, it can see NULL or @new. The synchronization ensures it can't grab a reference to @old once window #2 has been opened.Thanks for the clarification.
Quoted 23 lines
Quoted 5 lines
But I'm not sure why to put the
kvm_gmem_get_pfn()relying onsynchronize_srcu_expedited()inkvm_swap_active_memslots()in the comment ofrelease(). Without the guard ofkvm_gmem_get_file(),kvm_gmem_get_pfn()may need to provide some RCU read-critial section too forrelease()to wait bysynchronize_srcu()?No, what I'm saying is that without
synchronize_srcu()to create distinct window #1 and window #2 above, KVM would need this:kvm/guest_memfd.c +2
-0@@ -347,6 +347,8 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)xa_for_each (&f->bindings, index, slot)WRITE_ONCE (slot->gmem.file,NULL ); +synchronize_srcu (); +/* * All in-flight operations are gone and new bindings can be created. * Zap all SPTEs pointed at by this file. Do not free the backing Quoted 2 lines
Otherwise there is no guarantee in-flight readers can't reach the old
slot->gmem.file.Hmm, however, if the reader (
kvm_gmem_get_pfn()) has grabbed a reference to @old,kvm_gmem_release()can't come; ifkvm_gmem_release()comes,kvm_gmem_get_pfn()should not have been able to successfully invokeget_file_active()on @old. Afterkvm_gmem_release()returns, any subsequent calls toget_file_active()on @old should fail and return NULL.
Quoted 4 lines
So, even if the reader can see @old, we're still fine without
synchronize_srcu(), asget_file_active()andSLAB_TYPESAFE_BY_RCUshould have provide the safety.Am I missing something?
In kvm_gmem_get_pfn(), because slots_lock isn't held, this could happen if kvm_set_memory_region() didn't synchronize_srcu().
CPU0 CPU1
kvm_gmem_get_pfn()
f = X (from slot->gmem.file)
kvm_gmem_release())
slot->gmem.file = NULL
kvm_set_memory_region()
slot deleted
kvm_set_memory_region()
slot created
slot->gmem.file = f (alloc the same object) get_file_active()
file = f
file_reloaded = f
<KVM does weird things with an old memslot+file>
Obviously KVM would be wildly broken for other reasons, but just saying get_file_active() takes care of everything is misleading because it only handles reallocation of the object, it doesn't guarantee a reference to the correct file was obtained.
E.g. AFAICT, users of get_mm_exe_file() don't care if they race with replace_mm_exe_file(), they only care that they have a reference to a live file.
KVM on the other hand cares that kvm_gmem_get_pfn() gets the exact file that is associated with its memslot point.
Quoted 86 lines · Sean Christopherson
Quoted 57 lines · Yan Zhao
Quoted 15 lines · Sean Christopherson
Quoted 11 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+ /* + * Note! synchronize_srcu() is _not_ needed after nullifying memslot + * bindings as slot->gmem.file cannot be set back to a non-null value + * without the memslot first being deleted. I.e. this relies on the + * synchronize_srcu_expedited() in kvm_swap_active_memslots() to ensure + * kvm_gmem_get_pfn() (which runs with kvm->srcu held for read) can't + * grab a reference to slot->gmem.file even if the struct file object + * is reallocated.Do you mean that as
kvm_gmem_get_pfn()can't find a stale slot, it can't grab reference to staleslot->gmem.file, even ifslot->gmem.filehas been set to a different value, i.e., after invokingunbind(),bind()?I was specifically trying to say this:
1.
slot->gmem.file== old ==Quoted 8 lines
synchronize_srcu() |===> window #1 for readers 2. slot->gmem.file == NULL == synchronize_srcu() |===> window #2 for readers3.
slot->gmem.file== new ==
kvm_gmem_get_pfn()can run in two "windows". In the window #1, it can see @old or NULL. In window #2, it can see NULL or @new. The synchronization ensures it can't grab a reference to @old once window #2 has been opened.Thanks for the clarification.
Quoted 23 lines
Quoted 5 lines
But I'm not sure why to put the
kvm_gmem_get_pfn()relying onsynchronize_srcu_expedited()inkvm_swap_active_memslots()in the comment ofrelease(). Without the guard ofkvm_gmem_get_file(),kvm_gmem_get_pfn()may need to provide some RCU read-critial section too forrelease()to wait bysynchronize_srcu()?No, what I'm saying is that without
synchronize_srcu()to create distinct window #1 and window #2 above, KVM would need this:kvm/guest_memfd.c +2
-0@@ -347,6 +347,8 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)xa_for_each (&f->bindings, index, slot)WRITE_ONCE (slot->gmem.file,NULL ); +synchronize_srcu (); +/* * All in-flight operations are gone and new bindings can be created. * Zap all SPTEs pointed at by this file. Do not free the backing Quoted 2 lines
Otherwise there is no guarantee in-flight readers can't reach the old
slot->gmem.file.Hmm, however, if the reader (
kvm_gmem_get_pfn()) has grabbed a reference to @old,kvm_gmem_release()can't come; ifkvm_gmem_release()comes,kvm_gmem_get_pfn()should not have been able to successfully invokeget_file_active()on @old. Afterkvm_gmem_release()returns, any subsequent calls toget_file_active()on @old should fail and return NULL.Quoted 4 lines
So, even if the reader can see @old, we're still fine without
synchronize_srcu(), asget_file_active()andSLAB_TYPESAFE_BY_RCUshould have provide the safety.Am I missing something?
In
kvm_gmem_get_pfn(), becauseslots_lockisn't held, this could happen ifkvm_set_memory_region()didn'tsynchronize_srcu().CPU0 CPU1
kvm_gmem_get_pfn()f = X (from slot->gmem.file) kvm_gmem_release()) slot->gmem.file = NULL kvm_set_memory_region() slot deleted kvm_set_memory_region() slot created slot->gmem.file = f (alloc the same object)
get_file_active()
file = f
file_reloaded= f
<KVM does weird things with an old memslot+file>
Quoted 4 lines
Obviously KVM would be wildly broken for other reasons, but just saying
get_file_active()takes care of everything is misleading because it only handles reallocation of the object, it doesn't guarantee a reference to the correct file was obtained.
Ok. Thanks for the explanation. I get your point now.
My previous confusion came from the fact that if kvm_set_memory_region() does not wait for all users of the slot being deleted to exit the SRCU read critical section before committing the slot deletion, KVM would be wildly broken regardless of whether gmem is released.
Quoted 5 lines
E.g. AFAICT, users of
get_mm_exe_file()don't care if they race withreplace_mm_exe_file(), they only care that they have a reference to a live file.KVM on the other hand cares that
kvm_gmem_get_pfn()gets the exact file that is associated with its memslot point.
If slot->gmem.file can be set back to a non-null value without the memslot first being deleted, synchronize_srcu() in release() isn't required, right?
CPU0 CPU1
kvm_gmem_get_pfn()
f = X (from slot->gmem.file)
kvm_gmem_release())
slot->gmem.file = NULL
kvm_set_memory_region()
slot->gmem.file = f (alloc the same object) get_file_active()
file = f
file_reloaded = f
<KVM proceeds here should be fine with slot + new file>
Sean Christopherson
maintainerQuoted 54 lines · Yan Zhao
Quoted 24 lines · Sean Christopherson
On Fri, Jun 05, 2026, Yan Zhao wrote:
Inkvm_gmem_get_pfn(), becauseslots_lockisn't held, this could happen ifkvm_set_memory_region()didn'tsynchronize_srcu().CPU0 CPU1
kvm_gmem_get_pfn()f = X (from slot->gmem.file) kvm_gmem_release()) slot->gmem.file = NULL kvm_set_memory_region() slot deleted kvm_set_memory_region() slot created slot->gmem.file = f (alloc the same object)
get_file_active()
file = f
file_reloaded= f
<KVM does weird things with an old memslot+file>Quoted 4 lines
Obviously KVM would be wildly broken for other reasons, but just saying
get_file_active()takes care of everything is misleading because it only handles reallocation of the object, it doesn't guarantee a reference to the correct file was obtained.Ok. Thanks for the explanation. I get your point now.
My previous confusion came from the fact that if
kvm_set_memory_region()does not wait for all users of the slot being deleted to exit the SRCU read critical section before committing the slot deletion, KVM would be wildly broken regardless of whether gmem is released.Quoted 5 lines
E.g. AFAICT, users of
get_mm_exe_file()don't care if they race withreplace_mm_exe_file(), they only care that they have a reference to a live file.KVM on the other hand cares that
kvm_gmem_get_pfn()gets the exact file that is associated with its memslot point.If
slot->gmem.filecan be set back to a non-null value without the memslot first being deleted,synchronize_srcu()inrelease()isn't required, right?CPU0 CPU1
kvm_gmem_get_pfn()f = X (from slot->gmem.file) kvm_gmem_release()) slot->gmem.file = NULL kvm_set_memory_region() slot->gmem.file = f (alloc the same object)
Quoted 5 lines
get_file_active()
file = f
file_reloaded= f<KVM proceeds here should be fine with slot + new file>
Not really? There are no guarantees that the old slot and new slot have the same GPA range and the same gfn=>gmem bindings. E.g. __kvm_gmem_get_pfn() could get invoked with an out-of-range index, or worse could provide the wrong pfn due to computing .
It's kind of an impossible question to answer though, because it's sooo far down a hypothetical path where a KVM bug basically means all bets are off.
Quoted 59 lines · Sean Christopherson
Quoted 58 lines · Yan Zhao
Quoted 24 lines · Sean Christopherson
On Fri, Jun 05, 2026, Yan Zhao wrote:
Inkvm_gmem_get_pfn(), becauseslots_lockisn't held, this could happen ifkvm_set_memory_region()didn'tsynchronize_srcu().CPU0 CPU1
kvm_gmem_get_pfn()f = X (from slot->gmem.file) kvm_gmem_release()) slot->gmem.file = NULL kvm_set_memory_region() slot deleted kvm_set_memory_region() slot created slot->gmem.file = f (alloc the same object)
get_file_active()
file = f
file_reloaded= f
<KVM does weird things with an old memslot+file>Quoted 4 lines
Obviously KVM would be wildly broken for other reasons, but just saying
get_file_active()takes care of everything is misleading because it only handles reallocation of the object, it doesn't guarantee a reference to the correct file was obtained.Ok. Thanks for the explanation. I get your point now.
My previous confusion came from the fact that if
kvm_set_memory_region()does not wait for all users of the slot being deleted to exit the SRCU read critical section before committing the slot deletion, KVM would be wildly broken regardless of whether gmem is released.Quoted 5 lines
E.g. AFAICT, users of
get_mm_exe_file()don't care if they race withreplace_mm_exe_file(), they only care that they have a reference to a live file.KVM on the other hand cares that
kvm_gmem_get_pfn()gets the exact file that is associated with its memslot point.If
slot->gmem.filecan be set back to a non-null value without the memslot first being deleted,synchronize_srcu()inrelease()isn't required, right?CPU0 CPU1
kvm_gmem_get_pfn()f = X (from slot->gmem.file) kvm_gmem_release()) slot->gmem.file = NULL kvm_set_memory_region() slot->gmem.file = f (alloc the same object)
get_file_active()
file = f
file_reloaded= f
Quoted 6 lines
Quoted 1 line
<KVM proceeds here should be fine with slot + new file>
Not really? There are no guarantees that the old slot and new slot have the same GPA range and the same gfn=>gmem bindings. E.g.
__kvm_gmem_get_pfn()could get invoked with an out-of-range index, or worse could provide the wrong pfn due to computing .
Ah, Ok.
Quoted 2 lines
It's kind of an impossible question to answer though, because it's sooo far down a hypothetical path where a KVM bug basically means all bets are off.
Ok. Thanks :)
Quoted 35 lines · Sean Christopherson
Add more context and information to the comment in
kvm_gmem_release()that explains why there's no synchronization on RCU _or_kvm->srcu. Point (b) from commit67b43038ce14("KVM:guest_memfd: Remove RCU-protected attribute fromslot->gmem.file")b)
kvm->srcuensures thatkvm_gmem_unbind()and freeing of a memslot occur after the memslot is no longer visible tokvm_gmem_get_pfn().is especially difficult to fully grok, particularly in light of commit
ae431059e75d("KVM:guest_memfd: Remove bindings on memslot deletion when gmem is dying"), which addressed a race betweenunbind()andrelease().No functional change intended.
Cc Yan Zhao <[email protected]>Cc Vishal Annapurve <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- virt/kvm/guest_memfd.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)virt/kvm/guest_memfd.c +0
-6@@ -338,17 +338,25 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)* dereferencing the slot for existing bindings needs to be protected * against memslot updates, specifically so that unbind doesn't race * and free the memslot (kvm_gmem_get_file() will return NULL). -* -* Since .release is called only when the reference count is zero, -* after which file_ref_get() and get_file_active() fail, -* kvm_gmem_get_pfn() cannot be using the file concurrently. -* file_ref_put() provides a full barrier, and get_file_active() the -* matching acquire barrier.
virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 350
Quoted 10 lines
virt/kvm/guest_memfd.c +5
-0*/ mutex_lock (&kvm->slots_lock);filemap_invalidate_lock (inode->i_mapping); +/* +* Note! synchronize_srcu() is _not_ needed after nullifying memslot +* bindings as slot->gmem.file cannot be set back to a non-null value +* without the memslot first being deleted. I.e. this relies on the +* synchronize_srcu_expedited() in kvm_swap_active_memslots() to ensure
Is this kvm_swap_active_memslots() referring not to swapping out of the deleted memslot, but swapping in of a new memslot?
virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 354
Quoted 4 lines
virt/kvm/guest_memfd.c +4
-0+* kvm_gmem_get_pfn() (which runs with kvm->srcu held for read) can't +* grab a reference to slot->gmem.file even if the struct file object +* is reallocated. +*
I think the part that's missing for me is: why is synchronize_srcu() required after NULLifying memslot bindings? Normally, synchronize_srcu() will be needed after memslot updates to make sure the next user of kvm->srcu gets the new state of the memslots?
The "new state" here could be
1. slot->gmem.file == NULL
2. slot->gmem.file == some new file, set after the old memslot was replaced
virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 358
Quoted 4 lines
virt/kvm/guest_memfd.c +4
-0+* file_ref_put() provides a full barrier, and __get_file_rcu() the +* matching acquire barrier, to ensure that kvm_gmem_get_file() (via +* __get_file_rcu()) sees refcount==0 or fails the "file reloaded" +* check (file != NULL due to nullifying the file pointer here).
In this case, kvm_gmem_get_pfn() uses kvm_gmem_get_file(), and because of the above, kvm_gmem_get_pfn() will not try to get a folio from a NULL file (new state 1).
To handle new state 2, kvm_swap_active_memslots() to swap in a new memslot must have called synchronize_srcu(), so synchronize_srcu() can be skipped here.
Quoted 8 lines
virt/kvm/guest_memfd.c +1
-0+*/ xa_for_each (&f->bindings, index, slot)WRITE_ONCE (slot->gmem.file,NULL );base-commit:
16ec4fb4ac95d878b879192d280db2baeec43272--
2.52.0.rc1.455.g30608eb744-goog
Sean Christopherson
maintainerQuoted 42 lines · Ackerley Tng
Quoted 40 lines · Sean Christopherson
Add more context and information to the comment in
kvm_gmem_release()that explains why there's no synchronization on RCU _or_kvm->srcu. Point (b) from commit67b43038ce14("KVM:guest_memfd: Remove RCU-protected attribute fromslot->gmem.file")b)
kvm->srcuensures thatkvm_gmem_unbind()and freeing of a memslot occur after the memslot is no longer visible tokvm_gmem_get_pfn().is especially difficult to fully grok, particularly in light of commit
ae431059e75d("KVM:guest_memfd: Remove bindings on memslot deletion when gmem is dying"), which addressed a race betweenunbind()andrelease().No functional change intended.
Cc Yan Zhao <[email protected]>Cc Vishal Annapurve <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- virt/kvm/guest_memfd.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)virt/kvm/guest_memfd.c +0
-6@@ -338,17 +338,25 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)* dereferencing the slot for existing bindings needs to be protected * against memslot updates, specifically so that unbind doesn't race * and free the memslot (kvm_gmem_get_file() will return NULL). -* -* Since .release is called only when the reference count is zero, -* after which file_ref_get() and get_file_active() fail, -* kvm_gmem_get_pfn() cannot be using the file concurrently. -* file_ref_put() provides a full barrier, and get_file_active() the -* matching acquire barrier. */ mutex_lock (&kvm->slots_lock);filemap_invalidate_lock (inode->i_mapping);
Quoted 8 lines
virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 350
Quoted 5 lines
virt/kvm/guest_memfd.c +5
-0+/* +* Note! synchronize_srcu() is _not_ needed after nullifying memslot +* bindings as slot->gmem.file cannot be set back to a non-null value +* without the memslot first being deleted. I.e. this relies on the +* synchronize_srcu_expedited() in kvm_swap_active_memslots() to ensure Is this
kvm_swap_active_memslots()referring not to swapping out of the deleted memslot, but swapping in of a new memslot?
No, it's the deletion that matters. When doing an (S)RCU-protect pointer swap, readers can see _either_ pointer (the old or the new). I.e. when creating a new memslot, synchronize_srcu_expedited() would only guarantee readers would see the new slot *after* synchronization completes.
But that's not what we care about. What we care about is that an in-flight kvm_gmem_get_pfn() _can't_ reach the file for new, re-allocated memslot. That's guaranteed because deleting the old memslot waiting for readers to away, i.e. guarantees that any readers that saw a non-NULL slot->gmem.file saw the one associated with the old memslot.
Quoted 10 lines
virt/kvm/guest_memfd.c static int kvm_gmem_release(struct inode *inode, struct file *file) · line 354
Quoted 4 lines
virt/kvm/guest_memfd.c +4
-0+* kvm_gmem_get_pfn() (which runs with kvm->srcu held for read) can't +* grab a reference to slot->gmem.file even if the struct file object +* is reallocated. +* I think the part that's missing for me is: why is
synchronize_srcu()required after NULLifying memslot bindings? Normally,synchronize_srcu()will be needed after memslot updates to make sure the next user ofkvm->srcugets the new state of the memslots?
Quoted 5 lines
The "new state" here could be
1.
slot->gmem.file== NULL
2.slot->gmem.file== some new file, set after the old memslot was replaced
Only because there's a synchronize_srcu(). Without that, it could be:
1. slot->gmem.file == old ==
2. slot->gmem.file == NULL |==> window for readers
3. slot->gmem.file == new ==
versus
1. slot->gmem.file == old ==
synchronize_srcu() |===> window for readers
2. slot->gmem.file == NULL ==
synchronize_srcu() |===> window #2 for readers 3. slot->gmem.file == new ==