Skip to content

Commit bf30824

Browse files
Andre-ARMbonzini
authored andcommitted
KVM: arm/arm64: VGIC/ITS: protect kvm_read_guest() calls with SRCU lock
kvm_read_guest() will eventually look up in kvm_memslots(), which requires either to hold the kvm->slots_lock or to be inside a kvm->srcu critical section. In contrast to x86 and s390 we don't take the SRCU lock on every guest exit, so we have to do it individually for each kvm_read_guest() call. Provide a wrapper which does that and use that everywhere. Note that ending the SRCU critical section before returning from the kvm_read_guest() wrapper is safe, because the data has been *copied*, so we don't need to rely on valid references to the memslot anymore. Cc: Stable <stable@vger.kernel.org> # 4.8+ Reported-by: Jan Glauber <jan.glauber@caviumnetworks.com> Signed-off-by: Andre Przywara <andre.przywara@arm.com> Acked-by: Christoffer Dall <christoffer.dall@arm.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 9c41887 commit bf30824

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

arch/arm/include/asm/kvm_mmu.h

+16
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,22 @@ static inline unsigned int kvm_get_vmid_bits(void)
309309
return 8;
310310
}
311311

312+
/*
313+
* We are not in the kvm->srcu critical section most of the time, so we take
314+
* the SRCU read lock here. Since we copy the data from the user page, we
315+
* can immediately drop the lock again.
316+
*/
317+
static inline int kvm_read_guest_lock(struct kvm *kvm,
318+
gpa_t gpa, void *data, unsigned long len)
319+
{
320+
int srcu_idx = srcu_read_lock(&kvm->srcu);
321+
int ret = kvm_read_guest(kvm, gpa, data, len);
322+
323+
srcu_read_unlock(&kvm->srcu, srcu_idx);
324+
325+
return ret;
326+
}
327+
312328
static inline void *kvm_get_hyp_vector(void)
313329
{
314330
return kvm_ksym_ref(__kvm_hyp_vector);

arch/arm64/include/asm/kvm_mmu.h

+16
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,22 @@ static inline unsigned int kvm_get_vmid_bits(void)
360360
return (cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR1_VMIDBITS_SHIFT) == 2) ? 16 : 8;
361361
}
362362

363+
/*
364+
* We are not in the kvm->srcu critical section most of the time, so we take
365+
* the SRCU read lock here. Since we copy the data from the user page, we
366+
* can immediately drop the lock again.
367+
*/
368+
static inline int kvm_read_guest_lock(struct kvm *kvm,
369+
gpa_t gpa, void *data, unsigned long len)
370+
{
371+
int srcu_idx = srcu_read_lock(&kvm->srcu);
372+
int ret = kvm_read_guest(kvm, gpa, data, len);
373+
374+
srcu_read_unlock(&kvm->srcu, srcu_idx);
375+
376+
return ret;
377+
}
378+
363379
#ifdef CONFIG_KVM_INDIRECT_VECTORS
364380
/*
365381
* EL2 vectors can be mapped and rerouted in a number of ways,

virt/kvm/arm/vgic/vgic-its.c

+8-7
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
281281
int ret;
282282
unsigned long flags;
283283

284-
ret = kvm_read_guest(kvm, propbase + irq->intid - GIC_LPI_OFFSET,
285-
&prop, 1);
284+
ret = kvm_read_guest_lock(kvm, propbase + irq->intid - GIC_LPI_OFFSET,
285+
&prop, 1);
286286

287287
if (ret)
288288
return ret;
@@ -444,8 +444,9 @@ static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)
444444
* this very same byte in the last iteration. Reuse that.
445445
*/
446446
if (byte_offset != last_byte_offset) {
447-
ret = kvm_read_guest(vcpu->kvm, pendbase + byte_offset,
448-
&pendmask, 1);
447+
ret = kvm_read_guest_lock(vcpu->kvm,
448+
pendbase + byte_offset,
449+
&pendmask, 1);
449450
if (ret) {
450451
kfree(intids);
451452
return ret;
@@ -789,7 +790,7 @@ static bool vgic_its_check_id(struct vgic_its *its, u64 baser, u32 id,
789790
return false;
790791

791792
/* Each 1st level entry is represented by a 64-bit value. */
792-
if (kvm_read_guest(its->dev->kvm,
793+
if (kvm_read_guest_lock(its->dev->kvm,
793794
BASER_ADDRESS(baser) + index * sizeof(indirect_ptr),
794795
&indirect_ptr, sizeof(indirect_ptr)))
795796
return false;
@@ -1370,8 +1371,8 @@ static void vgic_its_process_commands(struct kvm *kvm, struct vgic_its *its)
13701371
cbaser = CBASER_ADDRESS(its->cbaser);
13711372

13721373
while (its->cwriter != its->creadr) {
1373-
int ret = kvm_read_guest(kvm, cbaser + its->creadr,
1374-
cmd_buf, ITS_CMD_SIZE);
1374+
int ret = kvm_read_guest_lock(kvm, cbaser + its->creadr,
1375+
cmd_buf, ITS_CMD_SIZE);
13751376
/*
13761377
* If kvm_read_guest() fails, this could be due to the guest
13771378
* programming a bogus value in CBASER or something else going

0 commit comments

Comments
 (0)