Skip to content

Commit 7fbdda3

Browse files
committed
KVM: guest_memfd: do not go through struct page
We have a perfectly usable folio, use it to retrieve the pfn and order. All that's needed is a version of folio_file_page that returns a pfn. Reviewed-by: Michael Roth <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent d04c77d commit 7fbdda3

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

virt/kvm/guest_memfd.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ struct kvm_gmem {
1313
struct list_head entry;
1414
};
1515

16+
/**
17+
* folio_file_pfn - like folio_file_page, but return a pfn.
18+
* @folio: The folio which contains this index.
19+
* @index: The index we want to look up.
20+
*
21+
* Return: The pfn for this index.
22+
*/
23+
static inline kvm_pfn_t folio_file_pfn(struct folio *folio, pgoff_t index)
24+
{
25+
return folio_pfn(folio) + (index & (folio_nr_pages(folio) - 1));
26+
}
27+
1628
static int kvm_gmem_prepare_folio(struct inode *inode, pgoff_t index, struct folio *folio)
1729
{
1830
#ifdef CONFIG_HAVE_KVM_GMEM_PREPARE
@@ -22,7 +34,6 @@ static int kvm_gmem_prepare_folio(struct inode *inode, pgoff_t index, struct fol
2234
list_for_each_entry(gmem, gmem_list, entry) {
2335
struct kvm_memory_slot *slot;
2436
struct kvm *kvm = gmem->kvm;
25-
struct page *page;
2637
kvm_pfn_t pfn;
2738
gfn_t gfn;
2839
int rc;
@@ -34,13 +45,12 @@ static int kvm_gmem_prepare_folio(struct inode *inode, pgoff_t index, struct fol
3445
if (!slot)
3546
continue;
3647

37-
page = folio_file_page(folio, index);
38-
pfn = page_to_pfn(page);
48+
pfn = folio_file_pfn(folio, index);
3949
gfn = slot->base_gfn + index - slot->gmem.pgoff;
40-
rc = kvm_arch_gmem_prepare(kvm, gfn, pfn, compound_order(compound_head(page)));
50+
rc = kvm_arch_gmem_prepare(kvm, gfn, pfn, folio_order(folio));
4151
if (rc) {
42-
pr_warn_ratelimited("gmem: Failed to prepare folio for index %lx GFN %llx PFN %llx error %d.\n",
43-
index, gfn, pfn, rc);
52+
pr_warn_ratelimited("gmem: Failed to prepare folio for GFN %llx PFN %llx error %d.\n",
53+
gfn, pfn, rc);
4454
return rc;
4555
}
4656
}
@@ -548,7 +558,6 @@ __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot,
548558
pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff;
549559
struct kvm_gmem *gmem = file->private_data;
550560
struct folio *folio;
551-
struct page *page;
552561

553562
if (file != slot->gmem.file) {
554563
WARN_ON_ONCE(slot->gmem.file);
@@ -571,9 +580,7 @@ __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot,
571580
return ERR_PTR(-EHWPOISON);
572581
}
573582

574-
page = folio_file_page(folio, index);
575-
576-
*pfn = page_to_pfn(page);
583+
*pfn = folio_file_pfn(folio, index);
577584
if (max_order)
578585
*max_order = 0;
579586

0 commit comments

Comments
 (0)