Skip to content

Commit dc85c77

Browse files
yanzhao56jfvogel
authored andcommitted
KVM: guest_memfd: Remove RCU-protected attribute from slot->gmem.file
[ Upstream commit 67b4303 ] Remove the RCU-protected attribute from slot->gmem.file. No need to use RCU primitives rcu_assign_pointer()/synchronize_rcu() to update this pointer. - slot->gmem.file is updated in 3 places: kvm_gmem_bind(), kvm_gmem_unbind(), kvm_gmem_release(). All of them are protected by kvm->slots_lock. - slot->gmem.file is read in 2 paths: (1) kvm_gmem_populate kvm_gmem_get_file __kvm_gmem_get_pfn (2) kvm_gmem_get_pfn kvm_gmem_get_file __kvm_gmem_get_pfn Path (1) kvm_gmem_populate() requires holding kvm->slots_lock, so slot->gmem.file is protected by the kvm->slots_lock in this path. Path (2) kvm_gmem_get_pfn() does not require holding kvm->slots_lock. However, it's also not guarded by rcu_read_lock() and rcu_read_unlock(). So synchronize_rcu() in kvm_gmem_unbind()/kvm_gmem_release() actually will not wait for the readers in kvm_gmem_get_pfn() due to lack of RCU read-side critical section. The path (2) kvm_gmem_get_pfn() is safe without RCU protection because: a) kvm_gmem_bind() is called on a new memslot, before the memslot is visible to kvm_gmem_get_pfn(). 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(). c) get_file_active() ensures that kvm_gmem_get_pfn() will not access the stale file if kvm_gmem_release() sets it to NULL. This is because if kvm_gmem_release() occurs before kvm_gmem_get_pfn(), get_file_active() will return NULL; if get_file_active() does not return NULL, kvm_gmem_release() should not occur until after kvm_gmem_get_pfn() releases the file reference. Signed-off-by: Yan Zhao <yan.y.zhao@intel.com> Message-ID: <20241104084303.29909-1-yan.y.zhao@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Stable-dep-of: ae431059e75d ("KVM: guest_memfd: Remove bindings on memslot deletion when gmem is dying") Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> (cherry picked from commit 08adc31ec7ad75638deff691d603343abec7a88c) Signed-off-by: Jack Vogel <jack.vogel@oracle.com>
1 parent 15b8f44 commit dc85c77

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

include/linux/kvm_host.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,12 @@ struct kvm_memory_slot {
608608

609609
#ifdef CONFIG_KVM_PRIVATE_MEM
610610
struct {
611-
struct file __rcu *file;
611+
/*
612+
* Writes protected by kvm->slots_lock. Acquiring a
613+
* reference via kvm_gmem_get_file() is protected by
614+
* either kvm->slots_lock or kvm->srcu.
615+
*/
616+
struct file *file;
612617
pgoff_t pgoff;
613618
} gmem;
614619
#endif

virt/kvm/guest_memfd.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -261,15 +261,19 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)
261261
* dereferencing the slot for existing bindings needs to be protected
262262
* against memslot updates, specifically so that unbind doesn't race
263263
* and free the memslot (kvm_gmem_get_file() will return NULL).
264+
*
265+
* Since .release is called only when the reference count is zero,
266+
* after which file_ref_get() and get_file_active() fail,
267+
* kvm_gmem_get_pfn() cannot be using the file concurrently.
268+
* file_ref_put() provides a full barrier, and get_file_active() the
269+
* matching acquire barrier.
264270
*/
265271
mutex_lock(&kvm->slots_lock);
266272

267273
filemap_invalidate_lock(inode->i_mapping);
268274

269275
xa_for_each(&gmem->bindings, index, slot)
270-
rcu_assign_pointer(slot->gmem.file, NULL);
271-
272-
synchronize_rcu();
276+
WRITE_ONCE(slot->gmem.file, NULL);
273277

274278
/*
275279
* All in-flight operations are gone and new bindings can be created.
@@ -298,8 +302,7 @@ static inline struct file *kvm_gmem_get_file(struct kvm_memory_slot *slot)
298302
/*
299303
* Do not return slot->gmem.file if it has already been closed;
300304
* there might be some time between the last fput() and when
301-
* kvm_gmem_release() clears slot->gmem.file, and you do not
302-
* want to spin in the meanwhile.
305+
* kvm_gmem_release() clears slot->gmem.file.
303306
*/
304307
return get_file_active(&slot->gmem.file);
305308
}
@@ -510,11 +513,11 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
510513
}
511514

512515
/*
513-
* No synchronize_rcu() needed, any in-flight readers are guaranteed to
514-
* be see either a NULL file or this new file, no need for them to go
515-
* away.
516+
* memslots of flag KVM_MEM_GUEST_MEMFD are immutable to change, so
517+
* kvm_gmem_bind() must occur on a new memslot. Because the memslot
518+
* is not visible yet, kvm_gmem_get_pfn() is guaranteed to see the file.
516519
*/
517-
rcu_assign_pointer(slot->gmem.file, file);
520+
WRITE_ONCE(slot->gmem.file, file);
518521
slot->gmem.pgoff = start;
519522

520523
xa_store_range(&gmem->bindings, start, end - 1, slot, GFP_KERNEL);
@@ -550,8 +553,12 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot)
550553

551554
filemap_invalidate_lock(file->f_mapping);
552555
xa_store_range(&gmem->bindings, start, end - 1, NULL, GFP_KERNEL);
553-
rcu_assign_pointer(slot->gmem.file, NULL);
554-
synchronize_rcu();
556+
557+
/*
558+
* synchronize_srcu(&kvm->srcu) ensured that kvm_gmem_get_pfn()
559+
* cannot see this memslot.
560+
*/
561+
WRITE_ONCE(slot->gmem.file, NULL);
555562
filemap_invalidate_unlock(file->f_mapping);
556563

557564
fput(file);
@@ -563,11 +570,12 @@ static struct folio *__kvm_gmem_get_pfn(struct file *file,
563570
pgoff_t index, kvm_pfn_t *pfn,
564571
bool *is_prepared, int *max_order)
565572
{
573+
struct file *gmem_file = READ_ONCE(slot->gmem.file);
566574
struct kvm_gmem *gmem = file->private_data;
567575
struct folio *folio;
568576

569-
if (file != slot->gmem.file) {
570-
WARN_ON_ONCE(slot->gmem.file);
577+
if (file != gmem_file) {
578+
WARN_ON_ONCE(gmem_file);
571579
return ERR_PTR(-EFAULT);
572580
}
573581

0 commit comments

Comments
 (0)