Skip to content

Commit 6dd761d

Browse files
committed
KVM: guest_memfd: make kvm_gmem_prepare_folio() operate on a single struct kvm
This is now possible because preparation is done by kvm_gmem_get_pfn() instead of fallocate(). In practice this is not a limitation, because even though guest_memfd can be bound to multiple struct kvm, for hardware implementations of confidential computing only one guest (identified by an ASID on SEV-SNP, or an HKID on TDX) will be able to access it. In the case of intra-host migration (not implemented yet for SEV-SNP, but we can use SEV-ES as an idea of how it will work), the new struct kvm inherits the same ASID and preparation need not be repeated. Reviewed-by: Michael Roth <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent b855243 commit 6dd761d

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

virt/kvm/guest_memfd.c

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,27 @@ static inline kvm_pfn_t folio_file_pfn(struct folio *folio, pgoff_t index)
2525
return folio_pfn(folio) + (index & (folio_nr_pages(folio) - 1));
2626
}
2727

28-
static int __kvm_gmem_prepare_folio(struct inode *inode, pgoff_t index, struct folio *folio)
28+
static int __kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot *slot,
29+
pgoff_t index, struct folio *folio)
2930
{
3031
#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_PREPARE
31-
struct list_head *gmem_list = &inode->i_mapping->i_private_list;
32-
struct kvm_gmem *gmem;
32+
kvm_pfn_t pfn;
33+
gfn_t gfn;
34+
int rc;
3335

34-
list_for_each_entry(gmem, gmem_list, entry) {
35-
struct kvm_memory_slot *slot;
36-
struct kvm *kvm = gmem->kvm;
37-
kvm_pfn_t pfn;
38-
gfn_t gfn;
39-
int rc;
40-
41-
if (!kvm_arch_gmem_prepare_needed(kvm))
42-
continue;
43-
44-
slot = xa_load(&gmem->bindings, index);
45-
if (!slot)
46-
continue;
47-
48-
pfn = folio_file_pfn(folio, index);
49-
gfn = slot->base_gfn + index - slot->gmem.pgoff;
50-
rc = kvm_arch_gmem_prepare(kvm, gfn, pfn, folio_order(folio));
51-
if (rc) {
52-
pr_warn_ratelimited("gmem: Failed to prepare folio for GFN %llx PFN %llx error %d.\n",
53-
gfn, pfn, rc);
54-
return rc;
55-
}
56-
}
36+
if (!kvm_arch_gmem_prepare_needed(kvm))
37+
return 0;
5738

39+
pfn = folio_file_pfn(folio, index);
40+
gfn = slot->base_gfn + index - slot->gmem.pgoff;
41+
rc = kvm_arch_gmem_prepare(kvm, gfn, pfn, folio_order(folio));
42+
if (rc) {
43+
pr_warn_ratelimited("gmem: Failed to prepare folio for index %lx GFN %llx PFN %llx error %d.\n",
44+
index, gfn, pfn, rc);
45+
return rc;
46+
}
5847
#endif
48+
5949
return 0;
6050
}
6151

@@ -65,7 +55,7 @@ static int __kvm_gmem_prepare_folio(struct inode *inode, pgoff_t index, struct f
6555
* On successful return the guest sees a zero page so as to avoid
6656
* leaking host data and the up-to-date flag is set.
6757
*/
68-
static int kvm_gmem_prepare_folio(struct file *file, struct kvm_memory_slot *slot,
58+
static int kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot *slot,
6959
gfn_t gfn, struct folio *folio)
7060
{
7161
unsigned long nr_pages, i;
@@ -95,8 +85,7 @@ static int kvm_gmem_prepare_folio(struct file *file, struct kvm_memory_slot *slo
9585
WARN_ON(!IS_ALIGNED(slot->gmem.pgoff, 1 << folio_order(folio)));
9686
index = gfn - slot->base_gfn + slot->gmem.pgoff;
9787
index = ALIGN_DOWN(index, 1 << folio_order(folio));
98-
99-
r = __kvm_gmem_prepare_folio(file_inode(file), index, folio);
88+
r = __kvm_gmem_prepare_folio(kvm, slot, index, folio);
10089
if (!r)
10190
folio_mark_uptodate(folio);
10291

@@ -619,7 +608,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
619608
goto out;
620609
}
621610

622-
r = kvm_gmem_prepare_folio(file, slot, gfn, folio);
611+
r = kvm_gmem_prepare_folio(kvm, slot, gfn, folio);
623612
folio_unlock(folio);
624613
if (r < 0)
625614
folio_put(folio);

0 commit comments

Comments
 (0)