[PATCH v2 11/20] KVM: Add CLASS() constructs to automagically handle lock+check of gpc

From: Sean Christopherson

Date: Fri May 29 2026 - 13:16:25 EST


Add CLASS() definitions for locally mapping a PFN given a gpc (gfn_to_pfn
cache), to eventually deduplicate a number of users that do:

lock();
while (!check()) {
unlock();

if (refresh())
return err;

lock()
}

...

mark_dirty();
unlock();

Implement read-only (for cases where KVM is only reading) and "try" (for
use in atomic code where rwlock might sleep due to PREEMPT_RT) variations.

Use "map local" as the primary terminology as the basic concept is more or
less the same as kmap_local(): ensure the current CPU has a kernel mapping
to the underlying memory.

Convert the pvclock code as the first user, as it is straightforward and
thus easier to audit for correctness.

For all intents and purposes, no functional change intended.

Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
---
arch/x86/kvm/x86.c | 17 ++++----------
include/linux/kvm_host.h | 48 +++++++++++++++++++++++++++++++---------
virt/kvm/pfncache.c | 36 ++++++++++++++++++++++++++++++
3 files changed, 78 insertions(+), 23 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 87e99756de0a..ea10ed4ab06f 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -3268,17 +3268,11 @@ static void kvm_setup_guest_pvclock(struct pvclock_vcpu_time_info *ref_hv_clock,

memcpy(&hv_clock, ref_hv_clock, sizeof(hv_clock));

- read_lock(&gpc->lock);
- while (!kvm_gpc_check(gpc, offset + sizeof(*guest_hv_clock))) {
- read_unlock(&gpc->lock);
+ CLASS(gpc_map_local, clock_map)(gpc, offset + sizeof(*guest_hv_clock));
+ if (IS_ERR(clock_map))
+ return;

- if (kvm_gpc_refresh(gpc, offset + sizeof(*guest_hv_clock)))
- return;
-
- read_lock(&gpc->lock);
- }
-
- guest_hv_clock = (void *)(gpc->khva + offset);
+ guest_hv_clock = *clock_map + offset;

/*
* This VCPU is paused, but it's legal for a guest to read another
@@ -3299,9 +3293,6 @@ static void kvm_setup_guest_pvclock(struct pvclock_vcpu_time_info *ref_hv_clock,

guest_hv_clock->version = ++hv_clock.version;

- kvm_gpc_mark_dirty_in_slot(gpc);
- read_unlock(&gpc->lock);
-
trace_kvm_pvclock_update(vcpu->vcpu_id, &hv_clock);
}

diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index ffbae1e6e84e..d70fa91cda0c 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -1535,6 +1535,44 @@ static inline bool kvm_gpc_is_hva_active(struct gfn_to_pfn_cache *gpc)
return gpc->active && kvm_is_error_gpa(gpc->gpa);
}

+static inline void kvm_gpc_mark_dirty_in_slot(struct gfn_to_pfn_cache *gpc)
+{
+ lockdep_assert_held(&gpc->lock);
+
+ if (!gpc->memslot || gpc->never_dirty)
+ return;
+
+ mark_page_dirty_in_slot(gpc->kvm, gpc->memslot, gpa_to_gfn(gpc->gpa));
+}
+
+void **gpc_map_local_lock(struct gfn_to_pfn_cache *gpc, unsigned long len);
+void **gpc_try_map_local_lock(struct gfn_to_pfn_cache *gpc, unsigned long len);
+
+static inline void gpc_map_local_unlock(void **khva)
+{
+ struct gfn_to_pfn_cache *gpc = container_of(khva, struct gfn_to_pfn_cache, khva);
+
+ kvm_gpc_mark_dirty_in_slot(gpc);
+
+ read_unlock(&gpc->lock);
+}
+
+static inline void gpc_map_local_unlock_ro(void **khva)
+{
+ read_unlock(&container_of(khva, struct gfn_to_pfn_cache, khva)->lock);
+}
+
+#define DEFINE_GPC_CLASS(try, ro) \
+DEFINE_CLASS(gpc##try##_map_local##ro, void **, \
+ if (!IS_ERR(_T)) gpc_map_local_unlock##ro(_T), \
+ gpc##try##_map_local_lock(gpc, len), \
+ struct gfn_to_pfn_cache *gpc, unsigned long len) \
+
+DEFINE_GPC_CLASS(,);
+DEFINE_GPC_CLASS(_try,);
+DEFINE_GPC_CLASS(, _ro);
+DEFINE_GPC_CLASS(_try, _ro);
+
void kvm_sigset_activate(struct kvm_vcpu *vcpu);
void kvm_sigset_deactivate(struct kvm_vcpu *vcpu);

@@ -1930,16 +1968,6 @@ static inline bool kvm_is_gpa_in_memslot(struct kvm *kvm, gpa_t gpa)
return !kvm_is_error_hva(hva);
}

-static inline void kvm_gpc_mark_dirty_in_slot(struct gfn_to_pfn_cache *gpc)
-{
- lockdep_assert_held(&gpc->lock);
-
- if (!gpc->memslot || gpc->never_dirty)
- return;
-
- mark_page_dirty_in_slot(gpc->kvm, gpc->memslot, gpa_to_gfn(gpc->gpa));
-}
-
enum kvm_stat_kind {
KVM_STAT_VM,
KVM_STAT_VCPU,
diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c
index 9209f06c46b4..d3e02a2bac38 100644
--- a/virt/kvm/pfncache.c
+++ b/virt/kvm/pfncache.c
@@ -484,3 +484,39 @@ void kvm_gpc_deactivate(struct gfn_to_pfn_cache *gpc)
gpc_unmap(old_pfn, old_khva);
}
}
+
+void **gpc_try_map_local_lock(struct gfn_to_pfn_cache *gpc, unsigned long len)
+{
+ if (!read_trylock(&gpc->lock))
+ return ERR_PTR(-EWOULDBLOCK);
+
+ if (!kvm_gpc_check(gpc, len)) {
+ read_unlock(&gpc->lock);
+ return ERR_PTR(-EWOULDBLOCK);
+ }
+
+ return &gpc->khva;
+}
+
+void **gpc_map_local_lock(struct gfn_to_pfn_cache *gpc, unsigned long len)
+{
+ /*
+ * Yes, this is an open-coded loop. But that's just what put_user()
+ * does anyway. Page it in and retry the instruction. We're just a
+ * little more honest about it.
+ */
+ for (;;) {
+ int r;
+
+ read_lock(&gpc->lock);
+
+ if (kvm_gpc_check(gpc, len))
+ return &gpc->khva;
+
+ read_unlock(&gpc->lock);
+
+ r = kvm_gpc_refresh(gpc, len);
+ if (r)
+ return ERR_PTR(r);
+ }
+}
--
2.54.0.823.g6e5bcc1fc9-goog