From: Michael Roth <michael.roth@amd.com>
To: <kvm@vger.kernel.org>
Cc: <linux-coco@lists.linux.dev>, <linux-mm@kvack.org>,
<linux-kernel@vger.kernel.org>, <jroedel@suse.de>,
<thomas.lendacky@amd.com>, <pbonzini@redhat.com>,
<seanjc@google.com>, <vbabka@suse.cz>, <amit.shah@amd.com>,
<pratikrajesh.sampat@amd.com>, <ashish.kalra@amd.com>,
<liam.merwick@oracle.com>, <david@redhat.com>,
<vannapurve@google.com>, <ackerleytng@google.com>,
<quic_eberman@quicinc.com>
Subject: [PATCH 3/5] KVM: gmem: Hold filemap invalidate lock while allocating/preparing folios
Date: Thu, 12 Dec 2024 00:36:33 -0600 [thread overview]
Message-ID: <20241212063635.712877-4-michael.roth@amd.com> (raw)
In-Reply-To: <20241212063635.712877-1-michael.roth@amd.com>
Currently the preparedness tracking relies on holding a folio's lock
to keep allocations/preparations and corresponding updates to the
prepared bitmap atomic.
However, on the invalidation side, the bitmap entry for the GFN/index
corresponding to a folio might need to be cleared after truncation. In
these cases the folio's are no longer part of the filemap, so nothing
guards against a newly-allocated folio getting prepared for the same
GFN/index, and then subsequently having its bitmap entry cleared by the
concurrently executing invalidation code.
Avoid this by ensuring that the filemap invalidation lock is held to
ensure allocations/preparations and corresponding updates to the
prepared bitmap are atomic even versus invalidations. Use a shared lock
in the kvm_gmem_get_pfn() case so vCPUs can still fault in pages in
parallel.
Signed-off-by: Michael Roth <michael.roth@amd.com>
---
virt/kvm/guest_memfd.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index 6907ae9fe149..9a5172de6a03 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -154,6 +154,8 @@ static void kvm_gmem_mark_prepared(struct file *file, pgoff_t index, int order)
unsigned long npages = (1ul << order);
unsigned long *p;
+ rwsem_assert_held(&file->f_mapping->invalidate_lock);
+
/* The index isn't necessarily aligned to the requested order. */
index &= ~(npages - 1);
p = i_gmem->prepared + BIT_WORD(index);
@@ -174,6 +176,8 @@ static void kvm_gmem_mark_range_unprepared(struct inode *inode, pgoff_t index, p
struct kvm_gmem_inode *i_gmem = (struct kvm_gmem_inode *)inode->i_private;
unsigned long *p = i_gmem->prepared + BIT_WORD(index);
+ rwsem_assert_held(&inode->i_mapping->invalidate_lock);
+
index &= BITS_PER_LONG - 1;
if (index) {
int first_word_count = min(npages, BITS_PER_LONG - index);
@@ -200,6 +204,8 @@ static bool kvm_gmem_is_prepared(struct file *file, pgoff_t index, int order)
unsigned long *p;
bool ret;
+ rwsem_assert_held(&file->f_mapping->invalidate_lock);
+
/* The index isn't necessarily aligned to the requested order. */
index &= ~(npages - 1);
p = i_gmem->prepared + BIT_WORD(index);
@@ -232,6 +238,8 @@ static int kvm_gmem_prepare_folio(struct kvm *kvm, struct file *file,
pgoff_t index, aligned_index;
int r;
+ rwsem_assert_held(&file->f_mapping->invalidate_lock);
+
index = gfn - slot->base_gfn + slot->gmem.pgoff;
nr_pages = (1ull << max_order);
WARN_ON(nr_pages > folio_nr_pages(folio));
@@ -819,12 +827,16 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
pgoff_t index = kvm_gmem_get_index(slot, gfn);
struct file *file = kvm_gmem_get_file(slot);
int max_order_local;
+ struct address_space *mapping;
struct folio *folio;
int r = 0;
if (!file)
return -EFAULT;
+ mapping = file->f_inode->i_mapping;
+ filemap_invalidate_lock_shared(mapping);
+
/*
* The caller might pass a NULL 'max_order', but internally this
* function needs to be aware of any order limitations set by
@@ -838,6 +850,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
folio = __kvm_gmem_get_pfn(file, slot, index, pfn, &max_order_local);
if (IS_ERR(folio)) {
r = PTR_ERR(folio);
+ filemap_invalidate_unlock_shared(mapping);
goto out;
}
@@ -845,6 +858,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,
r = kvm_gmem_prepare_folio(kvm, file, slot, gfn, folio, max_order_local);
folio_unlock(folio);
+ filemap_invalidate_unlock_shared(mapping);
if (!r)
*page = folio_file_page(folio, index);
--
2.25.1
next prev parent reply other threads:[~2024-12-12 6:38 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-12 6:36 [PATCH RFC v1 0/5] KVM: gmem: 2MB THP support and preparedness tracking changes Michael Roth
2024-12-12 6:36 ` [PATCH 1/5] KVM: gmem: Don't rely on __kvm_gmem_get_pfn() for preparedness Michael Roth
2025-01-22 14:39 ` Tom Lendacky
2025-02-20 1:12 ` Michael Roth
2024-12-12 6:36 ` [PATCH 2/5] KVM: gmem: Don't clear pages that have already been prepared Michael Roth
2024-12-12 6:36 ` Michael Roth [this message]
2025-03-14 9:20 ` [PATCH 3/5] KVM: gmem: Hold filemap invalidate lock while allocating/preparing folios Yan Zhao
2025-04-07 8:25 ` Yan Zhao
2025-04-23 20:30 ` Ackerley Tng
2025-05-19 17:04 ` Ackerley Tng
2025-05-21 6:46 ` Yan Zhao
2025-06-03 1:05 ` Vishal Annapurve
2025-06-03 1:31 ` Yan Zhao
2025-06-04 6:28 ` Vishal Annapurve
2025-06-12 12:40 ` Yan Zhao
2025-06-12 14:43 ` Vishal Annapurve
2025-07-03 6:29 ` Yan Zhao
2025-06-13 15:19 ` Michael Roth
2025-06-13 18:04 ` Michael Roth
2025-07-03 6:33 ` Yan Zhao
2024-12-12 6:36 ` [PATCH 4/5] KVM: SEV: Improve handling of large ranges in gmem prepare callback Michael Roth
2024-12-12 6:36 ` [PATCH 5/5] KVM: Add hugepage support for dedicated guest memory Michael Roth
2025-03-14 9:50 ` Yan Zhao
2024-12-20 11:31 ` [PATCH RFC v1 0/5] KVM: gmem: 2MB THP support and preparedness tracking changes David Hildenbrand
2025-01-07 12:11 ` Shah, Amit
2025-01-22 14:25 ` David Hildenbrand
2025-03-14 9:09 ` Yan Zhao
2025-03-14 9:33 ` David Hildenbrand
2025-03-14 11:19 ` Yan Zhao
2025-03-18 2:24 ` Yan Zhao
2025-03-18 19:13 ` David Hildenbrand
2025-03-19 7:39 ` Yan Zhao
2025-02-11 1:16 ` Vishal Annapurve
2025-02-20 1:09 ` Michael Roth
2025-03-14 9:16 ` Yan Zhao
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241212063635.712877-4-michael.roth@amd.com \
--to=michael.roth@amd.com \
--cc=ackerleytng@google.com \
--cc=amit.shah@amd.com \
--cc=ashish.kalra@amd.com \
--cc=david@redhat.com \
--cc=jroedel@suse.de \
--cc=kvm@vger.kernel.org \
--cc=liam.merwick@oracle.com \
--cc=linux-coco@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=pbonzini@redhat.com \
--cc=pratikrajesh.sampat@amd.com \
--cc=quic_eberman@quicinc.com \
--cc=seanjc@google.com \
--cc=thomas.lendacky@amd.com \
--cc=vannapurve@google.com \
--cc=vbabka@suse.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox