Re: [RFC PATCH 6/8] kvm: gmem: Temporarily restore direct map entries when needed

From: Paolo Bonzini
Date: Thu Jul 11 2024 - 02:26:55 EST


On 7/9/24 15:20, Patrick Roy wrote:
If KVM_GMEM_NO_DIRECT_MAP is set, and KVM tries to internally access
guest-private memory inside kvm_{read,write}_guest, or via a
gfn_to_pfn_cache, temporarily restore the direct map entry.

To avoid race conditions between two threads restoring or zapping direct
map entries for the same page and potentially interfering with each
other (e.g. unfortune interweavings of map->read->unmap in the form of
map(A)->map(B)->read(A)->unmap(A)->read(B) [BOOM]), the following
invariant is upheld in this patch:

- Only a single gfn_to_pfn_cache can exist for any given pfn, and

I think this is not ensured. You can however use set_page_private()/page_private() to count the number of references.

Paolo

- All non-gfn_to_pfn_cache code paths that temporarily restore direct
map entries complete the entire map->access->unmap critical section
while holding the folio lock.