Skip to content

Commit 67b4303

Browse files
yanzhao56bonzini
authored andcommitted
KVM: guest_memfd: Remove RCU-protected attribute from slot->gmem.file
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 <[email protected]> Message-ID: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent c50be1c commit 67b4303

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
@@ -596,7 +596,12 @@ struct kvm_memory_slot {
596596

597597
#ifdef CONFIG_KVM_PRIVATE_MEM
598598
struct {
599-
struct file __rcu *file;
599+
/*
600+
* Writes protected by kvm->slots_lock. Acquiring a
601+
* reference via kvm_gmem_get_file() is protected by
602+
* either kvm->slots_lock or kvm->srcu.
603+
*/
604+
struct file *file;
600605
pgoff_t pgoff;
601606
} gmem;
602607
#endif

virt/kvm/guest_memfd.c

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

265271
filemap_invalidate_lock(inode->i_mapping);
266272

267273
xa_for_each(&gmem->bindings, index, slot)
268-
rcu_assign_pointer(slot->gmem.file, NULL);
269-
270-
synchronize_rcu();
274+
WRITE_ONCE(slot->gmem.file, NULL);
271275

272276
/*
273277
* All in-flight operations are gone and new bindings can be created.
@@ -296,8 +300,7 @@ static inline struct file *kvm_gmem_get_file(struct kvm_memory_slot *slot)
296300
/*
297301
* Do not return slot->gmem.file if it has already been closed;
298302
* there might be some time between the last fput() and when
299-
* kvm_gmem_release() clears slot->gmem.file, and you do not
300-
* want to spin in the meanwhile.
303+
* kvm_gmem_release() clears slot->gmem.file.
301304
*/
302305
return get_file_active(&slot->gmem.file);
303306
}
@@ -508,11 +511,11 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,
508511
}
509512

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

518521
xa_store_range(&gmem->bindings, start, end - 1, slot, GFP_KERNEL);
@@ -548,8 +551,12 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot)
548551

549552
filemap_invalidate_lock(file->f_mapping);
550553
xa_store_range(&gmem->bindings, start, end - 1, NULL, GFP_KERNEL);
551-
rcu_assign_pointer(slot->gmem.file, NULL);
552-
synchronize_rcu();
554+
555+
/*
556+
* synchronize_srcu(&kvm->srcu) ensured that kvm_gmem_get_pfn()
557+
* cannot see this memslot.
558+
*/
559+
WRITE_ONCE(slot->gmem.file, NULL);
553560
filemap_invalidate_unlock(file->f_mapping);
554561

555562
fput(file);
@@ -561,11 +568,12 @@ static struct folio *__kvm_gmem_get_pfn(struct file *file,
561568
pgoff_t index, kvm_pfn_t *pfn,
562569
bool *is_prepared, int *max_order)
563570
{
571+
struct file *gmem_file = READ_ONCE(slot->gmem.file);
564572
struct kvm_gmem *gmem = file->private_data;
565573
struct folio *folio;
566574

567-
if (file != slot->gmem.file) {
568-
WARN_ON_ONCE(slot->gmem.file);
575+
if (file != gmem_file) {
576+
WARN_ON_ONCE(gmem_file);
569577
return ERR_PTR(-EFAULT);
570578
}
571579

0 commit comments

Comments
 (0)