Skip to contentSkip to conversation

kvm/vfio: ensure kvg instance stays around in kvm_vfio_group_add()

PATCH Applied Dmitry Torokhov 3 participants 10 messages Jul 14, 2023 – Aug 3, 2023 KVM

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 kvm/vfio: ensure kvg instance stays around in kvm_vfio_group_add()

kvm_vfio_group_add() creates kvg instance, links it to kv->group_list, and calls kvm_vfio_file_set_kvm() with kvg->file as an argument after dropping kv->lock. If we race group addition and deletion calls, kvg instance may get freed by the time we get around to calling kvm_vfio_file_set_kvm().

Previous iterations of the code did not reference kvg->file outside of the critical section, but used a temporary variable. Still, they had similar problem of the file reference being owned by kvg structure and potential for kvm_vfio_group_del() dropping it before kvm_vfio_group_add() had a chance to complete.

Fix this by moving call to kvm_vfio_file_set_kvm() under the protection of kv->lock. We already call it while holding the same lock when vfio group is being deleted, so it should be safe here as well.

Fixes 2fc1bec15883 ("kvm: set/clear kvm to/from vfio_group when group add/delete")

---

v3: added Alex's reviewed-by

v2: updated commit description with the correct "Fixes" tag (per Alex),
expanded commit description to mention issues with the earlier implementation of kvm_vfio_group_add().

 virt/kvm/vfio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
virt/kvm/vfio.c +1 -1
index 9584eb57e0ed..cd46d7ef98d6 100644
@@ -179,10 +179,10 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
 	list_add_tail(&kvg->node, &kv->group_list);
 
 	kvm_arch_start_assignment(dev->kvm);
+	kvm_vfio_file_set_kvm(kvg->file, dev->kvm);
 
 	mutex_unlock(&kv->lock);
 
-	kvm_vfio_file_set_kvm(kvg->file, dev->kvm);
 	kvm_vfio_update_coherency(dev);
 
 	return 0;
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
#1 lore raw

PATCH v3 2/2 kvm/vfio: avoid bouncing the mutex when adding and deleting groups

Stop taking kv->lock mutex in kvm_vfio_update_coherency() and instead call it with this mutex held: the callers of the function usually already have it taken (and released) before calling kvm_vfio_update_coherency(). This avoid bouncing the lock up and down.

The exception is kvm_vfio_release() where we do not take the lock, but it is being executed when the very last reference to kvm_device is being dropped, so there are no concerns about concurrency.

---

v3: initialize "ret" with 0 (per Alex), added Alex's reviewed-by

v2: new patch.

 virt/kvm/vfio.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)
virt/kvm/vfio.c +9 -17
index cd46d7ef98d6..dbf2b855cf78 100644
@@ -122,8 +122,6 @@ static void kvm_vfio_update_coherency(struct kvm_device *dev)
 	bool noncoherent = false;
 	struct kvm_vfio_group *kvg;
 
-	mutex_lock(&kv->lock);
-
 	list_for_each_entry(kvg, &kv->group_list, node) {
 		if (!kvm_vfio_file_enforced_coherent(kvg->file)) {
 			noncoherent = true;
@@ -139,8 +137,6 @@ static void kvm_vfio_update_coherency(struct kvm_device *dev)
 		else
 			kvm_arch_unregister_noncoherent_dma(dev->kvm);
 	}
-
-	mutex_unlock(&kv->lock);
 }
 
 static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
@@ -148,7 +144,7 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
 	struct kvm_vfio *kv = dev->private;
 	struct kvm_vfio_group *kvg;
 	struct file *filp;
-	int ret;
+	int ret = 0;
 
 	filp = fget(fd);
 	if (!filp)
@@ -157,7 +153,7 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
 	/* Ensure the FD is a vfio group FD.*/
 	if (!kvm_vfio_file_is_group(filp)) {
 		ret = -EINVAL;
-		goto err_fput;
+		goto out_fput;
 	}
 
 	mutex_lock(&kv->lock);
@@ -165,30 +161,26 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
 	list_for_each_entry(kvg, &kv->group_list, node) {
 		if (kvg->file == filp) {
 			ret = -EEXIST;
-			goto err_unlock;
+			goto out_unlock;
 		}
 	}
 
 	kvg = kzalloc(sizeof(*kvg), GFP_KERNEL_ACCOUNT);
 	if (!kvg) {
 		ret = -ENOMEM;
-		goto err_unlock;
+		goto out_unlock;
 	}
 
-	kvg->file = filp;
+	kvg->file = get_file(filp);
 	list_add_tail(&kvg->node, &kv->group_list);
 
 	kvm_arch_start_assignment(dev->kvm);
 	kvm_vfio_file_set_kvm(kvg->file, dev->kvm);
-
-	mutex_unlock(&kv->lock);
-
 	kvm_vfio_update_coherency(dev);
 
-	return 0;
-err_unlock:
+out_unlock:
 	mutex_unlock(&kv->lock);
-err_fput:
+out_fput:
 	fput(filp);
 	return ret;
 }
@@ -224,12 +216,12 @@ static int kvm_vfio_group_del(struct kvm_device *dev, unsigned int fd)
 		break;
 	}
 
+	kvm_vfio_update_coherency(dev);
+
 	mutex_unlock(&kv->lock);
 
 	fdput(f);
 
-	kvm_vfio_update_coherency(dev);
-
 	return ret;
 }
 
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
#2 lore raw

RE: [PATCH v3 2/2] kvm/vfio: avoid bouncing the mutex when adding and deleting groups

Quoted 12 lines

From: Dmitry Torokhov <[email protected]>
Sent: Saturday, July 15, 2023 6:46 AM

virt/kvm/vfio.c +1 -1
@@ -165,30 +161,26 @@ static int kvm_vfio_group_add(struct kvm_device
 *dev, unsigned int fd)
 	list_for_each_entry(kvg, &kv->group_list, node) {
 		if (kvg->file == filp) {
 			ret = -EEXIST;
-			goto err_unlock;
+			goto out_unlock;
 		}
 	}

virt/kvm/vfio.c static int kvm_vfio_group_add(struct kvm_device · line 175

Quoted 10 lines
virt/kvm/vfio.c +2 -2
 
 	kvg = kzalloc(sizeof(*kvg), GFP_KERNEL_ACCOUNT);
 	if (!kvg) {
 		ret = -ENOMEM;
-		goto err_unlock;
+		goto out_unlock;
 	}
 
-	kvg->file = filp;
+	kvg->file = get_file(filp);

Why is another reference required here?

#3 lore raw

Re: [PATCH v3 2/2] kvm/vfio: avoid bouncing the mutex when adding and deleting groups

Quoted 20 lines · Kevin
Quoted 20 lines

From: Dmitry Torokhov <[email protected]>
Sent: Saturday, July 15, 2023 6:46 AM

virt/kvm/vfio.c +2 -2
@@ -165,30 +161,26 @@ static int kvm_vfio_group_add(struct kvm_device
 *dev, unsigned int fd)
 	list_for_each_entry(kvg, &kv->group_list, node) {
 		if (kvg->file == filp) {
 			ret = -EEXIST;
-			goto err_unlock;
+			goto out_unlock;
 		}
 	}
 
 	kvg = kzalloc(sizeof(*kvg), GFP_KERNEL_ACCOUNT);
 	if (!kvg) {
 		ret = -ENOMEM;
-		goto err_unlock;
+		goto out_unlock;
 	}
 
Quoted 4 lines

virt/kvm/vfio.c static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd) · line 174

Quoted 2 lines
virt/kvm/vfio.c +1 -1
-	kvg->file = filp;
+	kvg->file = get_file(filp);

Why is another reference required here?

Because the function now has a single exit point and the original reference is dropped unconditionally on exit. It looks cleaner than checking for non-zero "ret" and deciding whether the reference should be dropped or kept.

Thanks.

Signature · 1 line
Dmitry
Reviewed-by
#4 lore raw

RE: [PATCH v3 2/2] kvm/vfio: avoid bouncing the mutex when adding and deleting groups

Quoted 30 lines

From: Dmitry Torokhov <[email protected]>
Sent: Wednesday, July 19, 2023 2:11 PM

Quoted 4 lines · Kevin

Review static int kvm_vfio_group_add(struct

Quoted 4 lines

From: Dmitry Torokhov <[email protected]>
Sent: Saturday, July 15, 2023 6:46 AM

(quoted diff) +0 -0
@@ -165,30 +161,26 @@ static int kvm_vfio_group_add(struct

kvm_device

Quoted 16 lines
Quoted 16 lines
virt/kvm/vfio.c +2 -2
 *dev, unsigned int fd)
 	list_for_each_entry(kvg, &kv->group_list, node) {
 		if (kvg->file == filp) {
 			ret = -EEXIST;
-			goto err_unlock;
+			goto out_unlock;
 		}
 	}
 
 	kvg = kzalloc(sizeof(*kvg), GFP_KERNEL_ACCOUNT);
 	if (!kvg) {
 		ret = -ENOMEM;
-		goto err_unlock;
+		goto out_unlock;
 	}
 
Quoted 4 lines

virt/kvm/vfio.c static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd) · line 174

Quoted 2 lines
virt/kvm/vfio.c +1 -1
-	kvg->file = filp;
+	kvg->file = get_file(filp);

Why is another reference required here?

Quoted 5 lines

Because the function now has a single exit point and the original reference is dropped unconditionally on exit. It looks cleaner than checking for non-zero "ret" and deciding whether the reference should be dropped or kept.

A comment is appreciated. otherwise,

#5 lore raw

Re: [PATCH v3 2/2] kvm/vfio: avoid bouncing the mutex when adding and deleting groups

Quoted 36 lines · Kevin
Quoted 30 lines

From: Dmitry Torokhov <[email protected]>
Sent: Wednesday, July 19, 2023 2:11 PM

Quoted 4 lines · Kevin

Review static int kvm_vfio_group_add(struct

Quoted 4 lines

From: Dmitry Torokhov <[email protected]>
Sent: Saturday, July 15, 2023 6:46 AM

(quoted diff) +0 -0
@@ -165,30 +161,26 @@ static int kvm_vfio_group_add(struct

kvm_device

Quoted 16 lines
Quoted 16 lines
virt/kvm/vfio.c +2 -2
 *dev, unsigned int fd)
 	list_for_each_entry(kvg, &kv->group_list, node) {
 		if (kvg->file == filp) {
 			ret = -EEXIST;
-			goto err_unlock;
+			goto out_unlock;
 		}
 	}
 
 	kvg = kzalloc(sizeof(*kvg), GFP_KERNEL_ACCOUNT);
 	if (!kvg) {
 		ret = -ENOMEM;
-		goto err_unlock;
+		goto out_unlock;
 	}
 
Quoted 4 lines

virt/kvm/vfio.c static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd) · line 174

Quoted 2 lines
virt/kvm/vfio.c +1 -1
-	kvg->file = filp;
+	kvg->file = get_file(filp);

Why is another reference required here?

Quoted 5 lines

Because the function now has a single exit point and the original reference is dropped unconditionally on exit. It looks cleaner than checking for non-zero "ret" and deciding whether the reference should be dropped or kept.

Quoted 3 lines

A comment is appreciated. otherwise,

Thank you for the review! However I do not think any comment is needed, if one is looking at the final source and not the patch form, the reason for taking another reference is plain to see.

Thanks!

Signature · 1 line
Dmitry
Reviewed-by on v3 1/2
#6 lore raw
In reply to Dmitry Torokhov
Quoted 15 lines

From: Dmitry Torokhov <[email protected]>
Sent: Saturday, July 15, 2023 6:46 AM

kvm_vfio_group_add() creates kvg instance, links it to kv->group_list, and calls kvm_vfio_file_set_kvm() with kvg->file as an argument after dropping kv->lock. If we race group addition and deletion calls, kvg instance may get freed by the time we get around to calling kvm_vfio_file_set_kvm().

Previous iterations of the code did not reference kvg->file outside of the critical section, but used a temporary variable. Still, they had similar problem of the file reference being owned by kvg structure and potential for kvm_vfio_group_del() dropping it before kvm_vfio_group_add() had a chance to complete.

Quoted 8 lines

Fix this by moving call to kvm_vfio_file_set_kvm() under the protection of kv->lock. We already call it while holding the same lock when vfio group is being deleted, so it should be safe here as well.

Fixes 2fc1bec15883 ("kvm: set/clear kvm to/from vfio_group when group

add/delete")

#7 lore raw
In reply to Dmitry Torokhov

Hey Paolo,

I'll pull this through the vfio tree unless you have a particular interest. Thanks,

Alex

Quoted 46 lines · On Fri, 14 Jul 2023 15:45:32 -0700 Dmitry Torokhov <[email protected]>…

kvm_vfio_group_add() creates kvg instance, links it to kv->group_list, and calls kvm_vfio_file_set_kvm() with kvg->file as an argument after dropping kv->lock. If we race group addition and deletion calls, kvg instance may get freed by the time we get around to calling kvm_vfio_file_set_kvm().

Previous iterations of the code did not reference kvg->file outside of the critical section, but used a temporary variable. Still, they had similar problem of the file reference being owned by kvg structure and potential for kvm_vfio_group_del() dropping it before kvm_vfio_group_add() had a chance to complete.

Fix this by moving call to kvm_vfio_file_set_kvm() under the protection of kv->lock. We already call it while holding the same lock when vfio group is being deleted, so it should be safe here as well.

Fixes 2fc1bec15883 ("kvm: set/clear kvm to/from vfio_group when group add/delete")

---

v3: added Alex's reviewed-by

v2: updated commit description with the correct "Fixes" tag (per Alex),
expanded commit description to mention issues with the earlier implementation of kvm_vfio_group_add().

 virt/kvm/vfio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
virt/kvm/vfio.c +1 -1
index 9584eb57e0ed..cd46d7ef98d6 100644
@@ -179,10 +179,10 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
 	list_add_tail(&kvg->node, &kv->group_list);
 
 	kvm_arch_start_assignment(dev->kvm);
+	kvm_vfio_file_set_kvm(kvg->file, dev->kvm);
 
 	mutex_unlock(&kv->lock);
 
-	kvm_vfio_file_set_kvm(kvg->file, dev->kvm);
 	kvm_vfio_update_coherency(dev);
 
 	return 0;
#8 lore raw
In reply to Dmitry Torokhov
Quoted 12 lines · On Fri, 14 Jul 2023 15:45:32 -0700 Dmitry Torokhov <[email protected]>…

kvm_vfio_group_add() creates kvg instance, links it to kv->group_list, and calls kvm_vfio_file_set_kvm() with kvg->file as an argument after dropping kv->lock. If we race group addition and deletion calls, kvg instance may get freed by the time we get around to calling kvm_vfio_file_set_kvm().

Previous iterations of the code did not reference kvg->file outside of the critical section, but used a temporary variable. Still, they had similar problem of the file reference being owned by kvg structure and potential for kvm_vfio_group_del() dropping it before kvm_vfio_group_add() had a chance to complete.

Quoted 8 lines

Fix this by moving call to kvm_vfio_file_set_kvm() under the protection of kv->lock. We already call it while holding the same lock when vfio group is being deleted, so it should be safe here as well.

Fixes 2fc1bec15883 ("kvm: set/clear kvm to/from vfio_group when group add/delete")

---

Applied series to vfio next branch for v6.6. There's a minor rebase involved, so please double check the results:

https://github.com/awilliam/linux-vfio/commits/next

Thanks,
Alex

Quoted 26 lines

v3: added Alex's reviewed-by

v2: updated commit description with the correct "Fixes" tag (per Alex),
expanded commit description to mention issues with the earlier implementation of kvm_vfio_group_add().

 virt/kvm/vfio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
virt/kvm/vfio.c +1 -1
index 9584eb57e0ed..cd46d7ef98d6 100644
@@ -179,10 +179,10 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd)
 	list_add_tail(&kvg->node, &kv->group_list);
 
 	kvm_arch_start_assignment(dev->kvm);
+	kvm_vfio_file_set_kvm(kvg->file, dev->kvm);
 
 	mutex_unlock(&kv->lock);
 
-	kvm_vfio_file_set_kvm(kvg->file, dev->kvm);
 	kvm_vfio_update_coherency(dev);
 
 	return 0;
#9 lore raw
Quoted 24 lines · Alex Williamson
Quoted 12 lines · On Fri, 14 Jul 2023 15:45:32 -0700 Dmitry Torokhov <[email protected]>…

kvm_vfio_group_add() creates kvg instance, links it to kv->group_list, and calls kvm_vfio_file_set_kvm() with kvg->file as an argument after dropping kv->lock. If we race group addition and deletion calls, kvg instance may get freed by the time we get around to calling kvm_vfio_file_set_kvm().

Previous iterations of the code did not reference kvg->file outside of the critical section, but used a temporary variable. Still, they had similar problem of the file reference being owned by kvg structure and potential for kvm_vfio_group_del() dropping it before kvm_vfio_group_add() had a chance to complete.

Quoted 8 lines

Fix this by moving call to kvm_vfio_file_set_kvm() under the protection of kv->lock. We already call it while holding the same lock when vfio group is being deleted, so it should be safe here as well.

Fixes 2fc1bec15883 ("kvm: set/clear kvm to/from vfio_group when group add/delete")

---

Quoted 4 lines

Applied series to vfio next branch for v6.6. There's a minor rebase involved, so please double check the results:

https://github.com/awilliam/linux-vfio/commits/next

Looks good to me, thanks!

Signature · 1 line
Dmitry