Skip to content

Commit 7788255

Browse files
Keir FraserMarc Zyngier
authored andcommitted
KVM: Implement barriers before accessing kvm->buses[] on SRCU read paths
This ensures that, if a VCPU has "observed" that an IO registration has occurred, the instruction currently being trapped or emulated will also observe the IO registration. At the same time, enforce that kvm_get_bus() is used only on the update side, ensuring that a long-term reference cannot be obtained by an SRCU reader. Signed-off-by: Keir Fraser <keirf@google.com> Signed-off-by: Marc Zyngier <maz@kernel.org>
1 parent 11490b5 commit 7788255

3 files changed

Lines changed: 40 additions & 9 deletions

File tree

arch/x86/kvm/vmx/vmx.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5785,6 +5785,13 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
57855785
if (kvm_test_request(KVM_REQ_EVENT, vcpu))
57865786
return 1;
57875787

5788+
/*
5789+
* Ensure that any updates to kvm->buses[] observed by the
5790+
* previous instruction (emulated or otherwise) are also
5791+
* visible to the instruction KVM is about to emulate.
5792+
*/
5793+
smp_rmb();
5794+
57885795
if (!kvm_emulate_instruction(vcpu, 0))
57895796
return 0;
57905797

include/linux/kvm_host.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -966,11 +966,15 @@ static inline bool kvm_dirty_log_manual_protect_and_init_set(struct kvm *kvm)
966966
return !!(kvm->manual_dirty_log_protect & KVM_DIRTY_LOG_INITIALLY_SET);
967967
}
968968

969+
/*
970+
* Get a bus reference under the update-side lock. No long-term SRCU reader
971+
* references are permitted, to avoid stale reads vs concurrent IO
972+
* registrations.
973+
*/
969974
static inline struct kvm_io_bus *kvm_get_bus(struct kvm *kvm, enum kvm_bus idx)
970975
{
971-
return srcu_dereference_check(kvm->buses[idx], &kvm->srcu,
972-
lockdep_is_held(&kvm->slots_lock) ||
973-
!refcount_read(&kvm->users_count));
976+
return rcu_dereference_protected(kvm->buses[idx],
977+
lockdep_is_held(&kvm->slots_lock));
974978
}
975979

976980
static inline struct kvm_vcpu *kvm_get_vcpu(struct kvm *kvm, int i)

virt/kvm/kvm_main.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,14 @@ void __weak kvm_arch_create_vm_debugfs(struct kvm *kvm)
11031103
{
11041104
}
11051105

1106+
/* Called only on cleanup and destruction paths when there are no users. */
1107+
static inline struct kvm_io_bus *kvm_get_bus_for_destruction(struct kvm *kvm,
1108+
enum kvm_bus idx)
1109+
{
1110+
return rcu_dereference_protected(kvm->buses[idx],
1111+
!refcount_read(&kvm->users_count));
1112+
}
1113+
11061114
static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
11071115
{
11081116
struct kvm *kvm = kvm_arch_alloc_vm();
@@ -1228,7 +1236,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
12281236
out_err_no_arch_destroy_vm:
12291237
WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count));
12301238
for (i = 0; i < KVM_NR_BUSES; i++)
1231-
kfree(kvm_get_bus(kvm, i));
1239+
kfree(kvm_get_bus_for_destruction(kvm, i));
12321240
kvm_free_irq_routing(kvm);
12331241
out_err_no_irq_routing:
12341242
cleanup_srcu_struct(&kvm->irq_srcu);
@@ -1276,7 +1284,7 @@ static void kvm_destroy_vm(struct kvm *kvm)
12761284

12771285
kvm_free_irq_routing(kvm);
12781286
for (i = 0; i < KVM_NR_BUSES; i++) {
1279-
struct kvm_io_bus *bus = kvm_get_bus(kvm, i);
1287+
struct kvm_io_bus *bus = kvm_get_bus_for_destruction(kvm, i);
12801288

12811289
if (bus)
12821290
kvm_io_bus_destroy(bus);
@@ -5843,6 +5851,18 @@ static int __kvm_io_bus_write(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus,
58435851
return -EOPNOTSUPP;
58445852
}
58455853

5854+
static struct kvm_io_bus *kvm_get_bus_srcu(struct kvm *kvm, enum kvm_bus idx)
5855+
{
5856+
/*
5857+
* Ensure that any updates to kvm_buses[] observed by the previous vCPU
5858+
* machine instruction are also visible to the vCPU machine instruction
5859+
* that triggered this call.
5860+
*/
5861+
smp_mb__after_srcu_read_lock();
5862+
5863+
return srcu_dereference(kvm->buses[idx], &kvm->srcu);
5864+
}
5865+
58465866
int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
58475867
int len, const void *val)
58485868
{
@@ -5855,7 +5875,7 @@ int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
58555875
.len = len,
58565876
};
58575877

5858-
bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
5878+
bus = kvm_get_bus_srcu(vcpu->kvm, bus_idx);
58595879
if (!bus)
58605880
return -ENOMEM;
58615881
r = __kvm_io_bus_write(vcpu, bus, &range, val);
@@ -5874,7 +5894,7 @@ int kvm_io_bus_write_cookie(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx,
58745894
.len = len,
58755895
};
58765896

5877-
bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
5897+
bus = kvm_get_bus_srcu(vcpu->kvm, bus_idx);
58785898
if (!bus)
58795899
return -ENOMEM;
58805900

@@ -5924,7 +5944,7 @@ int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
59245944
.len = len,
59255945
};
59265946

5927-
bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
5947+
bus = kvm_get_bus_srcu(vcpu->kvm, bus_idx);
59285948
if (!bus)
59295949
return -ENOMEM;
59305950
r = __kvm_io_bus_read(vcpu, bus, &range, val);
@@ -6033,7 +6053,7 @@ struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx,
60336053

60346054
srcu_idx = srcu_read_lock(&kvm->srcu);
60356055

6036-
bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
6056+
bus = kvm_get_bus_srcu(kvm, bus_idx);
60376057
if (!bus)
60386058
goto out_unlock;
60396059

0 commit comments

Comments
 (0)