[PATCH v6 0/4] KVM: allow mapping non-refcounted pages

From: David Stevens
Date: Thu Mar 30 2023 - 04:58:16 EST


From: David Stevens <stevensd@xxxxxxxxxxxx>

This patch series adds support for mapping VM_IO and VM_PFNMAP memory
that is backed by struct pages that aren't currently being refcounted
(e.g. tail pages of non-compound higher order allocations) into the
guest.

Our use case is virtio-gpu blob resources [1], which directly map host
graphics buffers into the guest as "vram" for the virtio-gpu device.
This feature currently does not work on systems using the amdgpu driver,
as that driver allocates non-compound higher order pages via
ttm_pool_alloc_page.

Currently, the gfn_to_pfn functions require being able to pin the target
pfn, so they fail when the pfn returned by follow_pte isn't a
ref-counted page. However, the KVM secondary MMUs do not require that
the pfn be pinned, since they are integrated with the mmu notifier API.
This series adds a new set of gfn_to_pfn_noref functions which parallel
the gfn_to_pfn functions but do not pin the pfn. The new functions
return the page from gup if it was present, so callers can use it and
call put_page when done.

This series updates x86 and arm64 secondary MMUs to the new API. Other
MMUs can likely be updated without too much difficulty, but I am not
familiar with them and have no way to test them. On the other hand,
updating the rest of KVM would require replacing all usages of
kvm_vcpu_map with the gfn_to_pfn_cache, which is not at all easy [2].

[1]
https://patchwork.kernel.org/project/dri-devel/cover/20200814024000.2485-1-gurchetansingh@xxxxxxxxxxxx/
[2] https://lore.kernel.org/all/ZBEEQtmtNPaEqU1i@xxxxxxxxxx/

v5 -> v6:
- rebase on kvm next branch
- rename gfn_to_pfn_page to gfn_to_pfn_noref
- fix uninitialized outparam in error case of __kvm_faultin_pfn
- add kvm_release_pfn_noref_clean for releasing pfn/page pair
v4 -> v5:
- rebase on kvm next branch again
v3 -> v4:
- rebase on kvm next branch again
- Add some more context to a comment in ensure_pfn_ref
v2 -> v3:
- rebase on kvm next branch
v1 -> v2:
- Introduce new gfn_to_pfn_page functions instead of modifying the
behavior of existing gfn_to_pfn functions, to make the change less
invasive.
- Drop changes to mmu_audit.c
- Include Nicholas Piggin's patch to avoid corrupting refcount in the
follow_pte case, and use it in depreciated gfn_to_pfn functions.
- Rebase on kvm/next

David Stevens (4):
KVM: mmu: introduce new gfn_to_pfn_noref functions
KVM: x86/mmu: use gfn_to_pfn_noref
KVM: arm64/mmu: use gfn_to_pfn_noref
KVM: mmu: remove over-aggressive warnings

arch/arm64/kvm/mmu.c | 21 ++--
arch/x86/kvm/mmu/mmu.c | 29 ++---
arch/x86/kvm/mmu/mmu_internal.h | 1 +
arch/x86/kvm/mmu/paging_tmpl.h | 7 +-
arch/x86/kvm/x86.c | 5 +-
include/linux/kvm_host.h | 18 +++
virt/kvm/kvm_main.c | 214 +++++++++++++++++++++++---------
virt/kvm/kvm_mm.h | 6 +-
virt/kvm/pfncache.c | 12 +-
9 files changed, 220 insertions(+), 93 deletions(-)

--
2.40.0.348.gf938b09366-goog