Skip to content

Commit a4bff3d

Browse files
Paul Durrantsean-jc
authored andcommitted
KVM: pfncache: remove KVM_GUEST_USES_PFN usage
As noted in [1] the KVM_GUEST_USES_PFN usage flag is never set by any callers of kvm_gpc_init(), and for good reason: the implementation is incomplete/broken. And it's not clear that there will ever be a user of KVM_GUEST_USES_PFN, as coordinating vCPUs with mmu_notifier events is non-trivial. Remove KVM_GUEST_USES_PFN and all related code, e.g. dropping KVM_GUEST_USES_PFN also makes the 'vcpu' argument redundant, to avoid having to reason about broken code as __kvm_gpc_refresh() evolves. Moreover, all existing callers specify KVM_HOST_USES_PFN so the usage check in hva_to_pfn_retry() and hence the 'usage' argument to kvm_gpc_init() are also redundant. [1] https://lore.kernel.org/all/ZQiR8IpqOZrOpzHC@google.com Signed-off-by: Paul Durrant <pdurrant@amazon.com> Reviewed-by: David Woodhouse <dwmw@amazon.co.uk> Link: https://lore.kernel.org/r/20240215152916.1158-6-paul@xen.org [sean: explicitly call out that guest usage is incomplete] Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 78b7463 commit a4bff3d

File tree

5 files changed

+16
-80
lines changed

5 files changed

+16
-80
lines changed

arch/x86/kvm/x86.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12056,7 +12056,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
1205612056
vcpu->arch.regs_avail = ~0;
1205712057
vcpu->arch.regs_dirty = ~0;
1205812058

12059-
kvm_gpc_init(&vcpu->arch.pv_time, vcpu->kvm, vcpu, KVM_HOST_USES_PFN);
12059+
kvm_gpc_init(&vcpu->arch.pv_time, vcpu->kvm);
1206012060

1206112061
if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
1206212062
vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;

arch/x86/kvm/xen.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,14 +2108,10 @@ void kvm_xen_init_vcpu(struct kvm_vcpu *vcpu)
21082108

21092109
timer_setup(&vcpu->arch.xen.poll_timer, cancel_evtchn_poll, 0);
21102110

2111-
kvm_gpc_init(&vcpu->arch.xen.runstate_cache, vcpu->kvm, NULL,
2112-
KVM_HOST_USES_PFN);
2113-
kvm_gpc_init(&vcpu->arch.xen.runstate2_cache, vcpu->kvm, NULL,
2114-
KVM_HOST_USES_PFN);
2115-
kvm_gpc_init(&vcpu->arch.xen.vcpu_info_cache, vcpu->kvm, NULL,
2116-
KVM_HOST_USES_PFN);
2117-
kvm_gpc_init(&vcpu->arch.xen.vcpu_time_info_cache, vcpu->kvm, NULL,
2118-
KVM_HOST_USES_PFN);
2111+
kvm_gpc_init(&vcpu->arch.xen.runstate_cache, vcpu->kvm);
2112+
kvm_gpc_init(&vcpu->arch.xen.runstate2_cache, vcpu->kvm);
2113+
kvm_gpc_init(&vcpu->arch.xen.vcpu_info_cache, vcpu->kvm);
2114+
kvm_gpc_init(&vcpu->arch.xen.vcpu_time_info_cache, vcpu->kvm);
21192115
}
21202116

21212117
void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu)
@@ -2158,7 +2154,7 @@ void kvm_xen_init_vm(struct kvm *kvm)
21582154
{
21592155
mutex_init(&kvm->arch.xen.xen_lock);
21602156
idr_init(&kvm->arch.xen.evtchn_ports);
2161-
kvm_gpc_init(&kvm->arch.xen.shinfo_cache, kvm, NULL, KVM_HOST_USES_PFN);
2157+
kvm_gpc_init(&kvm->arch.xen.shinfo_cache, kvm);
21622158
}
21632159

21642160
void kvm_xen_destroy_vm(struct kvm *kvm)

include/linux/kvm_host.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,21 +1319,12 @@ void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn);
13191319
*
13201320
* @gpc: struct gfn_to_pfn_cache object.
13211321
* @kvm: pointer to kvm instance.
1322-
* @vcpu: vCPU to be used for marking pages dirty and to be woken on
1323-
* invalidation.
1324-
* @usage: indicates if the resulting host physical PFN is used while
1325-
* the @vcpu is IN_GUEST_MODE (in which case invalidation of
1326-
* the cache from MMU notifiers---but not for KVM memslot
1327-
* changes!---will also force @vcpu to exit the guest and
1328-
* refresh the cache); and/or if the PFN used directly
1329-
* by KVM (and thus needs a kernel virtual mapping).
13301322
*
13311323
* This sets up a gfn_to_pfn_cache by initializing locks and assigning the
13321324
* immutable attributes. Note, the cache must be zero-allocated (or zeroed by
13331325
* the caller before init).
13341326
*/
1335-
void kvm_gpc_init(struct gfn_to_pfn_cache *gpc, struct kvm *kvm,
1336-
struct kvm_vcpu *vcpu, enum pfn_cache_usage usage);
1327+
void kvm_gpc_init(struct gfn_to_pfn_cache *gpc, struct kvm *kvm);
13371328

13381329
/**
13391330
* kvm_gpc_activate - prepare a cached kernel mapping and HPA for a given guest

include/linux/kvm_types.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@ typedef u64 hfn_t;
4949

5050
typedef hfn_t kvm_pfn_t;
5151

52-
enum pfn_cache_usage {
53-
KVM_GUEST_USES_PFN = BIT(0),
54-
KVM_HOST_USES_PFN = BIT(1),
55-
KVM_GUEST_AND_HOST_USE_PFN = KVM_GUEST_USES_PFN | KVM_HOST_USES_PFN,
56-
};
57-
5852
struct gfn_to_hva_cache {
5953
u64 generation;
6054
gpa_t gpa;
@@ -69,13 +63,11 @@ struct gfn_to_pfn_cache {
6963
unsigned long uhva;
7064
struct kvm_memory_slot *memslot;
7165
struct kvm *kvm;
72-
struct kvm_vcpu *vcpu;
7366
struct list_head list;
7467
rwlock_t lock;
7568
struct mutex refresh_lock;
7669
void *khva;
7770
kvm_pfn_t pfn;
78-
enum pfn_cache_usage usage;
7971
bool active;
8072
bool valid;
8173
};

virt/kvm/pfncache.c

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, unsigned long start,
2626
unsigned long end, bool may_block)
2727
{
28-
DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS);
2928
struct gfn_to_pfn_cache *gpc;
30-
bool evict_vcpus = false;
3129

3230
spin_lock(&kvm->gpc_lock);
3331
list_for_each_entry(gpc, &kvm->gpc_list, list) {
@@ -37,43 +35,10 @@ void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, unsigned long start,
3735
if (gpc->valid && !is_error_noslot_pfn(gpc->pfn) &&
3836
gpc->uhva >= start && gpc->uhva < end) {
3937
gpc->valid = false;
40-
41-
/*
42-
* If a guest vCPU could be using the physical address,
43-
* it needs to be forced out of guest mode.
44-
*/
45-
if (gpc->usage & KVM_GUEST_USES_PFN) {
46-
if (!evict_vcpus) {
47-
evict_vcpus = true;
48-
bitmap_zero(vcpu_bitmap, KVM_MAX_VCPUS);
49-
}
50-
__set_bit(gpc->vcpu->vcpu_idx, vcpu_bitmap);
51-
}
5238
}
5339
write_unlock_irq(&gpc->lock);
5440
}
5541
spin_unlock(&kvm->gpc_lock);
56-
57-
if (evict_vcpus) {
58-
/*
59-
* KVM needs to ensure the vCPU is fully out of guest context
60-
* before allowing the invalidation to continue.
61-
*/
62-
unsigned int req = KVM_REQ_OUTSIDE_GUEST_MODE;
63-
bool called;
64-
65-
/*
66-
* If the OOM reaper is active, then all vCPUs should have
67-
* been stopped already, so perform the request without
68-
* KVM_REQUEST_WAIT and be sad if any needed to be IPI'd.
69-
*/
70-
if (!may_block)
71-
req &= ~KVM_REQUEST_WAIT;
72-
73-
called = kvm_make_vcpus_request_mask(kvm, req, vcpu_bitmap);
74-
75-
WARN_ON_ONCE(called && !may_block);
76-
}
7742
}
7843

7944
bool kvm_gpc_check(struct gfn_to_pfn_cache *gpc, unsigned long len)
@@ -206,16 +171,14 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc)
206171
* pfn. Note, kmap() and memremap() can both sleep, so this
207172
* too must be done outside of gpc->lock!
208173
*/
209-
if (gpc->usage & KVM_HOST_USES_PFN) {
210-
if (new_pfn == gpc->pfn)
211-
new_khva = old_khva;
212-
else
213-
new_khva = gpc_map(new_pfn);
214-
215-
if (!new_khva) {
216-
kvm_release_pfn_clean(new_pfn);
217-
goto out_error;
218-
}
174+
if (new_pfn == gpc->pfn)
175+
new_khva = old_khva;
176+
else
177+
new_khva = gpc_map(new_pfn);
178+
179+
if (!new_khva) {
180+
kvm_release_pfn_clean(new_pfn);
181+
goto out_error;
219182
}
220183

221184
write_lock_irq(&gpc->lock);
@@ -346,18 +309,12 @@ int kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, unsigned long len)
346309
return __kvm_gpc_refresh(gpc, gpc->gpa, len);
347310
}
348311

349-
void kvm_gpc_init(struct gfn_to_pfn_cache *gpc, struct kvm *kvm,
350-
struct kvm_vcpu *vcpu, enum pfn_cache_usage usage)
312+
void kvm_gpc_init(struct gfn_to_pfn_cache *gpc, struct kvm *kvm)
351313
{
352-
WARN_ON_ONCE(!usage || (usage & KVM_GUEST_AND_HOST_USE_PFN) != usage);
353-
WARN_ON_ONCE((usage & KVM_GUEST_USES_PFN) && !vcpu);
354-
355314
rwlock_init(&gpc->lock);
356315
mutex_init(&gpc->refresh_lock);
357316

358317
gpc->kvm = kvm;
359-
gpc->vcpu = vcpu;
360-
gpc->usage = usage;
361318
gpc->pfn = KVM_PFN_ERR_FAULT;
362319
gpc->uhva = KVM_HVA_ERR_BAD;
363320
}

0 commit comments

Comments
 (0)