Skip to contentSkip to conversation

KVM: Move kvm_io_bus_get_dev() locking responsibilities to callers

PATCH Applied Marc Zyngier 2 participants 3 messages Jun 27 – Jul 6

Subsystems (4)
lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

PATCH v2 1/1 KVM: Move kvm_io_bus_get_dev() locking responsibilities to callers

kvm_io_bus_get_dev() returns a device that is only matched by the address, and nothing else. This can cause a lifetime issue if the matched device is not the expected type, as by the time the caller can introspect the object, it might be gone (the srcu lock having been dropped).

Given that there is only a single user of this helper, the simplest option is to move the locking responsibility to the caller, which can keep the srcu lock held for as long as it wants.

Note that this aligns with other kvm_io_bus*() helpers, which already require the srcu lock to be held by the callers.

Fixes 8a39d00670f07 ("KVM: kvm_io_bus: Add kvm_io_bus_get_dev() call")

---

Notes:
v2: Drop the previous filtering approach, and move the locking into
the only caller, similar to kvm_io_bus_{read,write}().

 arch/arm64/kvm/vgic/vgic-its.c |  2 ++
 virt/kvm/kvm_main.c            | 16 +++++-----------
 2 files changed, 7 insertions(+), 11 deletions(-)
arch/arm64/kvm/vgic/vgic-its.c +2 -0
index 4477f870c7b36..740b39875728d 100644
@@ -508,6 +508,8 @@ static struct vgic_its *__vgic_doorbell_to_its(struct kvm *kvm, gpa_t db)
 	struct kvm_io_device *kvm_io_dev;
 	struct vgic_io_device *iodev;
 
+	guard(srcu)(&kvm->srcu);
+
 	kvm_io_dev = kvm_io_bus_get_dev(kvm, KVM_MMIO_BUS, db);
 	if (!kvm_io_dev)
 		return ERR_PTR(-EINVAL);
virt/kvm/kvm_main.c +5 -11
index 881f92d7a469e..1a529098eec98 100644
@@ -6069,25 +6069,19 @@ struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx,
 					 gpa_t addr)
 {
 	struct kvm_io_bus *bus;
-	int dev_idx, srcu_idx;
-	struct kvm_io_device *iodev = NULL;
+	int dev_idx;
 
-	srcu_idx = srcu_read_lock(&kvm->srcu);
+	lockdep_assert_held(&kvm->srcu);
 
 	bus = kvm_get_bus_srcu(kvm, bus_idx);
 	if (!bus)
-		goto out_unlock;
+		return NULL;
 
 	dev_idx = kvm_io_bus_get_first_dev(bus, addr, 1);
 	if (dev_idx < 0)
-		goto out_unlock;
-
-	iodev = bus->range[dev_idx].dev;
-
-out_unlock:
-	srcu_read_unlock(&kvm->srcu, srcu_idx);
+		return NULL;
 
-	return iodev;
+	return bus->range[dev_idx].dev;
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_io_bus_get_dev);
 
Signature · 1 line
2.47.3
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · Marc Zyngier

kvm_io_bus_get_dev() returns a device that is only matched by the address, and nothing else. This can cause a lifetime issue if the matched device is not the expected type, as by the time the caller can introspect the object, it might be gone (the srcu lock having been dropped).

Given that there is only a single user of this helper, the simplest option is to move the locking responsibility to the caller, which can keep the srcu lock held for as long as it wants.

Quoted 8 lines

Note that this aligns with other kvm_io_bus*() helpers, which already require the srcu lock to be held by the callers.

Fixes 8a39d00670f07 ("KVM: kvm_io_bus: Add kvm_io_bus_get_dev() call")

Thanks for respinning.

author maintainer Applied
#2 lore raw
In reply to Marc Zyngier
Quoted 11 lines · Marc Zyngier

kvm_io_bus_get_dev() returns a device that is only matched by the address, and nothing else. This can cause a lifetime issue if the matched device is not the expected type, as by the time the caller can introspect the object, it might be gone (the srcu lock having been dropped).

Given that there is only a single user of this helper, the simplest option is to move the locking responsibility to the caller, which can keep the srcu lock held for as long as it wants.

[...]

Applied to fixes, thanks!

[1/1] KVM: Move kvm_io_bus_get_dev() locking responsibilities to callers

      commit: 3a07249981629ace483ebbef81ef6b34c2d2afec

Cheers,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.