[RFC v2 PATCH 10/13] KVM: Match inode for invalidation of fd-based slot

From: Chao Peng
Date: Fri Nov 19 2021 - 08:50:00 EST


Different fd/priv_fd can have the same userspace_addr so start/end
is meaningful only when they are used together with fd/priv_fd.

Signed-off-by: Yu Zhang <yu.c.zhang@xxxxxxxxxxxxxxx>
Signed-off-by: Chao Peng <chao.p.peng@xxxxxxxxxxxxxxx>
---
virt/kvm/kvm_main.c | 13 +++++++++++++
1 file changed, 13 insertions(+)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 090afbadb03f..65055ac460eb 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -479,6 +479,7 @@ typedef void (*on_lock_fn_t)(struct kvm *kvm, unsigned long start,
struct kvm_useraddr_range {
unsigned long start;
unsigned long end;
+ struct inode *inode;
pte_t pte;
gfn_handler_t handler;
on_lock_fn_t on_lock;
@@ -520,6 +521,17 @@ static __always_inline int __kvm_handle_useraddr_range(struct kvm *kvm,
kvm_for_each_memslot(slot, slots) {
unsigned long useraddr_start, useraddr_end;

+ /*
+ * Skip the slot if range->inode is not the same as
+ * that in slot->file or slot->priv_file.
+ */
+ if (range->inode &&
+ (!slot->file ||
+ slot->file->f_inode != range->inode) &&
+ (!slot->priv_file ||
+ slot->priv_file->f_inode != range->inode))
+ continue;
+
useraddr_start = max(range->start, slot->userspace_addr);
useraddr_end = min(range->end, slot->userspace_addr +
(slot->npages << PAGE_SHIFT));
@@ -818,6 +830,7 @@ int kvm_memfd_invalidate_range(struct kvm *kvm, struct inode *inode,
const struct kvm_useraddr_range useraddr_range = {
.start = start,
.end = end,
+ .inode = inode,
.pte = __pte(0),
.handler = kvm_unmap_gfn_range,
.on_lock = (void *)kvm_null_fn,
--
2.17.1