Skip to contentSkip to conversation

KVM: destruct kvm_io_device while unregistering it from kvm_io_bus

PATCH Superseded Wei Wang 3 participants 6 messages Dec 29, 2022 – Jan 25, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wei Wang

author
#0 lore raw

PATCH 1/1 KVM: destruct kvm_io_device while unregistering it from kvm_io_bus

Current usage of kvm_io_device requires users to destruct it with an extra call of kvm_iodevice_destructor after the device gets unregistered from the kvm_io_bus. This is not necessary and can cause errors if a user forgot to make the extra call.

Simplify the usage by combining kvm_iodevice_destructor into kvm_io_bus_unregister_dev. This reduces LOCs a bit for users and can avoid the leakage of destructing the device explicitly.

The fix was originally provided by Sean Christopherson.

---
 include/kvm/iodev.h       |  6 ------
 virt/kvm/coalesced_mmio.c |  1 -
 virt/kvm/eventfd.c        |  1 -
 virt/kvm/kvm_main.c       | 24 +++++++++++++++---------
 4 files changed, 15 insertions(+), 17 deletions(-)
include/kvm/iodev.h +0 -6
index d75fc4365746..56619e33251e 100644
@@ -55,10 +55,4 @@ static inline int kvm_iodevice_write(struct kvm_vcpu *vcpu,
 				 : -EOPNOTSUPP;
 }
 
-static inline void kvm_iodevice_destructor(struct kvm_io_device *dev)
-{
-	if (dev->ops->destructor)
-		dev->ops->destructor(dev);
-}
-
 #endif /* __KVM_IODEV_H__ */
virt/kvm/coalesced_mmio.c +0 -1
index 0be80c213f7f..d7135a5e76f8 100644
@@ -195,7 +195,6 @@ int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
 			 */
 			if (r)
 				break;
-			kvm_iodevice_destructor(&dev->dev);
 		}
 	}
 
virt/kvm/eventfd.c +0 -1
index 2a3ed401ce46..1b277afb545b 100644
@@ -898,7 +898,6 @@ kvm_deassign_ioeventfd_idx(struct kvm *kvm, enum kvm_bus bus_idx,
 		bus = kvm_get_bus(kvm, bus_idx);
 		if (bus)
 			bus->ioeventfd_count--;
-		ioeventfd_release(p);
 		ret = 0;
 		break;
 	}
virt/kvm/kvm_main.c +15 -9
index 13e88297f999..582757ebdce6 100644
@@ -5200,6 +5200,12 @@ static struct notifier_block kvm_reboot_notifier = {
 	.priority = 0,
 };
 
+static void kvm_iodevice_destructor(struct kvm_io_device *dev)
+{
+	if (dev->ops->destructor)
+		dev->ops->destructor(dev);
+}
+
 static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
 {
 	int i;
@@ -5423,7 +5429,7 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
 int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
 			      struct kvm_io_device *dev)
 {
-	int i, j;
+	int i;
 	struct kvm_io_bus *new_bus, *bus;
 
 	lockdep_assert_held(&kvm->slots_lock);
@@ -5453,18 +5459,18 @@ int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
 	rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
 	synchronize_srcu_expedited(&kvm->srcu);
 
-	/* Destroy the old bus _after_ installing the (null) bus. */
+	/*
+	 * If (null) bus is installed, destroy the old bus, including all the
+	 * attached devices. Otherwise, destroy the caller's device only.
+	 */
 	if (!new_bus) {
 		pr_err("kvm: failed to shrink bus, removing it completely\n");
-		for (j = 0; j < bus->dev_count; j++) {
-			if (j == i)
-				continue;
-			kvm_iodevice_destructor(bus->range[j].dev);
-		}
+		kvm_io_bus_destroy(bus);
+		return -ENOMEM;
 	}
 
-	kfree(bus);
-	return new_bus ? 0 : -ENOMEM;
+	kvm_iodevice_destructor(dev);
+	return 0;
 }
 
 struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx,
Signature · 1 line
2.27.0
#1 lore raw
Quoted 42 lines · Wei Wang

Current usage of kvm_io_device requires users to destruct it with an extra call of kvm_iodevice_destructor after the device gets unregistered from the kvm_io_bus. This is not necessary and can cause errors if a user forgot to make the extra call.

Simplify the usage by combining kvm_iodevice_destructor into kvm_io_bus_unregister_dev. This reduces LOCs a bit for users and can avoid the leakage of destructing the device explicitly.

The fix was originally provided by Sean Christopherson.

---
 include/kvm/iodev.h       |  6 ------
 virt/kvm/coalesced_mmio.c |  1 -
 virt/kvm/eventfd.c        |  1 -
 virt/kvm/kvm_main.c       | 24 +++++++++++++++---------
 4 files changed, 15 insertions(+), 17 deletions(-)
include/kvm/iodev.h +0 -6
index d75fc4365746..56619e33251e 100644
@@ -55,10 +55,4 @@ static inline int kvm_iodevice_write(struct kvm_vcpu *vcpu,
 				 : -EOPNOTSUPP;
 }
 
-static inline void kvm_iodevice_destructor(struct kvm_io_device *dev)
-{
-	if (dev->ops->destructor)
-		dev->ops->destructor(dev);
-}
-
 #endif /* __KVM_IODEV_H__ */
virt/kvm/coalesced_mmio.c +0 -0
index 0be80c213f7f..d7135a5e76f8 100644

virt/kvm/coalesced_mmio.c int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm, · line 198

Quoted 4 lines
virt/kvm/coalesced_mmio.c +0 -1
@@ -195,7 +195,6 @@ int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
 			 */
 			if (r)
 				break;
-			kvm_iodevice_destructor(&dev->dev);

The comment lurking above this is now stale.

Quoted 49 lines
virt/kvm/coalesced_mmio.c +0 -0
 		}
 	}
 
virt/kvm/eventfd.c +0 -1
index 2a3ed401ce46..1b277afb545b 100644
@@ -898,7 +898,6 @@ kvm_deassign_ioeventfd_idx(struct kvm *kvm, enum kvm_bus bus_idx,
 		bus = kvm_get_bus(kvm, bus_idx);
 		if (bus)
 			bus->ioeventfd_count--;
-		ioeventfd_release(p);
 		ret = 0;
 		break;
 	}
virt/kvm/kvm_main.c +10 -2
index 13e88297f999..582757ebdce6 100644
@@ -5200,6 +5200,12 @@ static struct notifier_block kvm_reboot_notifier = {
 	.priority = 0,
 };
 
+static void kvm_iodevice_destructor(struct kvm_io_device *dev)
+{
+	if (dev->ops->destructor)
+		dev->ops->destructor(dev);
+}
+
 static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
 {
 	int i;
@@ -5423,7 +5429,7 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
 int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
 			      struct kvm_io_device *dev)
 {
-	int i, j;
+	int i;
 	struct kvm_io_bus *new_bus, *bus;
 
 	lockdep_assert_held(&kvm->slots_lock);
@@ -5453,18 +5459,18 @@ int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
 	rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
 	synchronize_srcu_expedited(&kvm->srcu);
 
-	/* Destroy the old bus _after_ installing the (null) bus. */
+	/*
+	 * If (null) bus is installed, destroy the old bus, including all the
+	 * attached devices. Otherwise, destroy the caller's device only.

virt/kvm/kvm_main.c int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, · line 5469

Quoted 10 lines
virt/kvm/kvm_main.c +3 -5
+	 */
 	if (!new_bus) {
 		pr_err("kvm: failed to shrink bus, removing it completely\n");
-		for (j = 0; j < bus->dev_count; j++) {
-			if (j == i)
-				continue;
-			kvm_iodevice_destructor(bus->range[j].dev);
-		}
+		kvm_io_bus_destroy(bus);
+		return -ENOMEM;

Returning an error code is unnecessary if unregister_dev() destroys the bus. Nothing ultimately consumes the result, e.g. kvm_vm_ioctl_unregister_coalesced_mmio() intentionally ignores the result other than to bail from the loop, and destroying the bus means it will immediately bail from the loop anyways.

virt/kvm/kvm_main.c int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, · line 5473

Quoted 6 lines
virt/kvm/kvm_main.c +2 -2
 	}
 
-	kfree(bus);
-	return new_bus ? 0 : -ENOMEM;
+	kvm_iodevice_destructor(dev);
+	return 0;

Unless I'm misreading things, this path leaks "bus".

Given that that intent is to send the fix for stable, that this is as much a cleanup as it is a bug fix, and that it's not super trivial, I'm inclined to queue my patch and then land this on top as cleanup.

#2 lore raw
Quoted 13 lines · Sean Christopherson

virt/kvm/eventfd.c kvm_deassign_ioeventfd_idx(struct kvm *kvm, enum kvm_bus bus_idx, · line 903

Quoted 12 lines · Wei Wang
virt/kvm/eventfd.c +0 -1
index 2a3ed401ce46..1b277afb545b 100644
@@ -898,7 +898,6 @@ kvm_deassign_ioeventfd_idx(struct kvm *kvm, enum kvm_bus bus_idx,
 		bus = kvm_get_bus(kvm, bus_idx);
 		if (bus)
 			bus->ioeventfd_count--;
-		ioeventfd_release(p);
 		ret = 0;
 		break;
 	}

I was wondering: would it make sense to simplify from list_for_each_entry_safe() to list_for_each_entry() in this loop?

Quoted 15 lines
Quoted 15 lines
virt/kvm/eventfd.c +4 -5
@@ -5453,18 +5459,18 @@ int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
 	rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
 	synchronize_srcu_expedited(&kvm->srcu);
 
-	/* Destroy the old bus _after_ installing the (null) bus. */
+	/*
+	 * If (null) bus is installed, destroy the old bus, including all the
+	 * attached devices. Otherwise, destroy the caller's device only.
+	 */
 	if (!new_bus) {
 		pr_err("kvm: failed to shrink bus, removing it completely\n");
-		for (j = 0; j < bus->dev_count; j++) {
-			if (j == i)
-				continue;
-			kvm_iodevice_destructor(bus->range[j].dev);
Quoted 8 lines

virt/kvm/eventfd.c int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, · line 5469

Quoted 3 lines
virt/kvm/eventfd.c +2 -1
-		}
+		kvm_io_bus_destroy(bus);
+		return -ENOMEM;

Returning an error code is unnecessary if unregister_dev() destroys the bus. Nothing ultimately consumes the result, e.g. kvm_vm_ioctl_unregister_coalesced_mmio() intentionally ignores the result other than to bail from the loop, and destroying the bus means it will immediately bail from the loop anyways.

But it is important to know _if_ the bus was destroyed, right? IOW, doesn't your comment from commit 5d3c4c79384a still hold?

(...) But, it doesn't tell the caller that it obliterated the bus and invoked the destructor for all devices that were on the bus. In the coalesced MMIO case, this can result in a deleted list entry dereference due to attempting to continue iterating on coalesced_zones after future entries (in the walk) have been deleted.

Michal

#3 lore raw
Quoted 17 lines · Michal Luczaj
Quoted 13 lines · Sean Christopherson

virt/kvm/eventfd.c kvm_deassign_ioeventfd_idx(struct kvm *kvm, enum kvm_bus bus_idx, · line 903

Quoted 12 lines · Wei Wang
virt/kvm/eventfd.c +0 -1
index 2a3ed401ce46..1b277afb545b 100644
@@ -898,7 +898,6 @@ kvm_deassign_ioeventfd_idx(struct kvm *kvm, enum kvm_bus bus_idx,
 		bus = kvm_get_bus(kvm, bus_idx);
 		if (bus)
 			bus->ioeventfd_count--;
-		ioeventfd_release(p);
 		ret = 0;
 		break;
 	}

I was wondering: would it make sense to simplify from list_for_each_entry_safe() to list_for_each_entry() in this loop?

Ooh, yeah, that's super confusing, at least to me, because the "safe" part implies that the loop processes entries after kvm_io_bus_unregister_dev(), i.e. needs to guard against failure same as the coalesced MMIO case.

Wei, want to tack on a patch in v2?

Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
virt/kvm/eventfd.c +4 -5
@@ -5453,18 +5459,18 @@ int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
 	rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
 	synchronize_srcu_expedited(&kvm->srcu);
 
-	/* Destroy the old bus _after_ installing the (null) bus. */
+	/*
+	 * If (null) bus is installed, destroy the old bus, including all the
+	 * attached devices. Otherwise, destroy the caller's device only.
+	 */
 	if (!new_bus) {
 		pr_err("kvm: failed to shrink bus, removing it completely\n");
-		for (j = 0; j < bus->dev_count; j++) {
-			if (j == i)
-				continue;
-			kvm_iodevice_destructor(bus->range[j].dev);
Quoted 8 lines

virt/kvm/eventfd.c int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, · line 5469

Quoted 3 lines
virt/kvm/eventfd.c +2 -1
-		}
+		kvm_io_bus_destroy(bus);
+		return -ENOMEM;

Returning an error code is unnecessary if unregister_dev() destroys the bus. Nothing ultimately consumes the result, e.g. kvm_vm_ioctl_unregister_coalesced_mmio() intentionally ignores the result other than to bail from the loop, and destroying the bus means it will immediately bail from the loop anyways.

Quoted 2 lines

But it is important to know _if_ the bus was destroyed, right? IOW, doesn't your comment from commit 5d3c4c79384a still hold?

/facepalm

Yes, it matters. I somehow got on the train of thought that list_for_each_entry_safe() magically bails if the list is purged, but the safe variant only plays nice with the _current_ entry being deleted.

So yeah, the return code needs to stay.

Quoted 8 lines

(...) But, it doesn't tell the caller that it obliterated the bus and invoked the destructor for all devices that were on the bus. In the coalesced MMIO case, this can result in a deleted list entry dereference due to attempting to continue iterating on coalesced_zones after future entries (in the walk) have been deleted.

Michal

#5 lore raw
In reply to Sean Christopherson
Quoted 6 lines · Sean Christopherson

virt/kvm/kvm_main.c int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, · line 5473

Quoted 4 lines
virt/kvm/kvm_main.c +2 -2
-	kfree(bus);
-	return new_bus ? 0 : -ENOMEM;
+	kvm_iodevice_destructor(dev);
+	return 0;

Unless I'm misreading things, this path leaks "bus".

Right, should keep the kfree above.

Quoted 3 lines

Given that that intent is to send the fix for stable, that this is as much a cleanup as it is a bug fix, and that it's not super trivial, I'm inclined to queue my patch and then land this on top as cleanup.

Sounds good to me.

#4 lore raw
In reply to Sean Christopherson
Quoted 9 lines · Sean Christopherson
Quoted 3 lines

I was wondering: would it make sense to simplify from list_for_each_entry_safe() to list_for_each_entry() in this loop?

Ooh, yeah, that's super confusing, at least to me, because the "safe" part implies that the loop processes entries after kvm_io_bus_unregister_dev(), i.e. needs to guard against failure same as the coalesced MMIO case.

Wei, want to tack on a patch in v2?

Yes. I will include it in v2.