Skip to contentSkip to conversation

s390/vfio-ap: fix memory leak in mdev remove callback

PATCH Superseded Tony Krowiak 3 participants 15 messages May 19, 2021 – May 21, 2021

Subsystems (5)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Tony Krowiak

author maintainer
#0 lore raw

v3 cover s390/vfio-ap: fix memory leak in mdev remove callback

Fixes a memory leak in the mdev remove callback when invoked while the mdev is in use by a KVM guest. Instead of returning -EBUSY from the callback, a full cleanup of the resources allocated to the mdev is performed because regardless of the value returned from the function, the mdev is removed from sysfs.

The cleanup of resources allocated to the mdev may coincide with the interception of the PQAP(AQIC) instruction in which case data needed to handle the interception may get removed. A patch is included in this series to synchronize access to resources needed by the interception handler to protect against invalid memory accesses.

Change log:
v2 -> v3:
--------
* Added a patch to control access to the PQAP(AQIC) hook using RCU

v1 -> v2:
--------
* Call vfio_ap_mdev_unset_kvm() function from the remove callback instead of merely clearing the guest's APCB.

Tony Krowiak (2):
s390/vfio-ap: fix memory leak in mdev remove callback s390/vfio-ap: control access to PQAP(AQIC) interception handler

 arch/s390/include/asm/kvm_host.h  |  1 +
 arch/s390/kvm/priv.c              | 47 +++++++++++++---------
 drivers/s390/crypto/vfio_ap_ops.c | 67 +++++++++++++++++++++----------
 3 files changed, 75 insertions(+), 40 deletions(-)
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH v3 1/2 s390/vfio-ap: fix memory leak in mdev remove callback

The mdev remove callback for the vfio_ap device driver bails out with -EBUSY if the mdev is in use by a KVM guest. The intended purpose was to prevent the mdev from being removed while in use; however, returning a non-zero rc does not prevent removal. This could result in a memory leak of the resources allocated when the mdev was created. In addition, the KVM guest will still have access to the AP devices assigned to the mdev even though the mdev no longer exists.

To prevent this scenario, cleanup will be done - including unplugging the AP adapters, domains and control domains - regardless of whether the mdev is in use by a KVM guest or not.

Fixes 258287c994de ("s390: vfio-ap: implement mediated device open callback")
---
 drivers/s390/crypto/vfio_ap_ops.c | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +2 -11
index b2c7e10dfdcd..f90c9103dac2 100644
@@ -26,6 +26,7 @@
 
 static int vfio_ap_mdev_reset_queues(struct mdev_device *mdev);
 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
+static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev);
 
 static int match_apqn(struct device *dev, const void *data)
 {
@@ -366,17 +367,7 @@ static int vfio_ap_mdev_remove(struct mdev_device *mdev)
 	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
 
 	mutex_lock(&matrix_dev->lock);
-
-	/*
-	 * If the KVM pointer is in flux or the guest is running, disallow
-	 * un-assignment of control domain.
-	 */
-	if (matrix_mdev->kvm_busy || matrix_mdev->kvm) {
-		mutex_unlock(&matrix_dev->lock);
-		return -EBUSY;
-	}
-
-	vfio_ap_mdev_reset_queues(mdev);
+	vfio_ap_mdev_unset_kvm(matrix_mdev);
 	list_del(&matrix_mdev->node);
 	kfree(matrix_mdev);
 	mdev_set_drvdata(mdev, NULL);
Signature · 1 line
2.30.2
author maintainer
#2 lore raw

PATCH v3 2/2 s390/vfio-ap: control access to PQAP(AQIC) interception handler

In reply to Tony Krowiak

There is currently nothing that controls access to the structure that contains the function pointer to the handler that processes interception of the PQAP(AQIC) instruction. If the mdev is removed while the PQAP(AQIC) instruction is being intercepted, there is a possibility that the function pointer to the handler can get wiped out prior to the attempt to call it.

This patch utilizes RCU to synchronize access to the kvm_s390_module_hook structure used to process interception of the PQAP(AQIC) instruction.

---
 arch/s390/include/asm/kvm_host.h  |  1 +
 arch/s390/kvm/priv.c              | 47 ++++++++++++++++-----------
 drivers/s390/crypto/vfio_ap_ops.c | 54 ++++++++++++++++++++++++-------
 3 files changed, 73 insertions(+), 29 deletions(-)
arch/s390/include/asm/kvm_host.h +1 -0
index 8925f3969478..4987e82d6116 100644
@@ -806,6 +806,7 @@ struct kvm_s390_cpu_model {
 struct kvm_s390_module_hook {
 	int (*hook)(struct kvm_vcpu *vcpu);
 	struct module *owner;
+	void *data;
 };
 
 struct kvm_s390_crypto {
arch/s390/kvm/priv.c +29 -18

61 lines not on this page · raw message

drivers/s390/crypto/vfio_ap_ops.c +43 -11

127 lines not on this page · raw message

Signature · 1 line
2.30.2
#3 lore raw

Re: [PATCH v3 2/2] s390/vfio-ap: control access to PQAP(AQIC) interception handler

Quoted 10 lines · Tony Krowiak
drivers/s390/crypto/vfio_ap_ops.c +4 -2
@@ -287,13 +289,17 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
 	if (!(vcpu->arch.sie_block->eca & ECA_AIV))
 		return -EOPNOTSUPP;
 
-	apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
-	mutex_lock(&matrix_dev->lock);
+	rcu_read_lock();
+	pqap_module_hook = rcu_dereference(vcpu->kvm->arch.crypto.pqap_hook);
+	if (!pqap_module_hook) {
+		rcu_read_unlock();

drivers/s390/crypto/vfio_ap_ops.c static int handle_pqap(struct kvm_vcpu *vcpu) · line 301

Quoted 10 lines
drivers/s390/crypto/vfio_ap_ops.c +5 -4
+		goto set_status;
+	}
 
-	if (!vcpu->kvm->arch.crypto.pqap_hook)
-		goto out_unlock;
-	matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
-				   struct ap_matrix_mdev, pqap_hook);
+	matrix_mdev = pqap_module_hook->data;
+	rcu_read_unlock();
+	mutex_lock(&matrix_dev->lock);

The matrix_mdev pointer was extracted from the pqap_module_hook, but now there is nothing protecting it since the rcu was dropped and it gets freed in vfio_ap_mdev_remove.

And, again, module locking doesn't prevent vfio_ap_mdev_remove() from being called. None of these patches should be combining module locking with RCU.

Jason

author maintainer
#4 lore raw

Re: [PATCH v3 2/2] s390/vfio-ap: control access to PQAP(AQIC) interception handler

Quoted 15 lines · Jason Gunthorpe
Quoted 13 lines · Tony Krowiak
drivers/s390/crypto/vfio_ap_ops.c +6 -2
@@ -287,13 +289,17 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
  	if (!(vcpu->arch.sie_block->eca & ECA_AIV))
  		return -EOPNOTSUPP;
 
-	apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
-	mutex_lock(&matrix_dev->lock);
+	rcu_read_lock();
+	pqap_module_hook = rcu_dereference(vcpu->kvm->arch.crypto.pqap_hook);
+	if (!pqap_module_hook) {
+		rcu_read_unlock();
+		goto set_status;
+	}
 
Quoted 10 lines

drivers/s390/crypto/vfio_ap_ops.c static int handle_pqap(struct kvm_vcpu *vcpu) · line 301

Quoted 7 lines
drivers/s390/crypto/vfio_ap_ops.c +3 -4
-	if (!vcpu->kvm->arch.crypto.pqap_hook)
-		goto out_unlock;
-	matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
-				   struct ap_matrix_mdev, pqap_hook);
+	matrix_mdev = pqap_module_hook->data;
+	rcu_read_unlock();
+	mutex_lock(&matrix_dev->lock);

The matrix_mdev pointer was extracted from the pqap_module_hook, but now there is nothing protecting it since the rcu was dropped and it gets freed in vfio_ap_mdev_remove.

Therein lies the rub. We can't hold the rcu_read_lock across the entire time that the interception is being processed because of wait conditions in the interception handler. Regardless of whether the pointer to the matrix_mdev is retrieved as the container of or extracted from the pqap_hook, there is nothing protecting it and there appears to be no way to do so using RCU.

Quoted 4 lines

And, again, module locking doesn't prevent vfio_ap_mdev_remove() from being called. None of these patches should be combining module locking with RCU.

Is there any other way besides user interaction with the mdev's sysfs remove interface for the remove callback to get invoked? If I try to remove the mdev using the sysfs interface while the mdev fd is still open by the guest, the remove hangs until the fd is closed. That being the case, the mdev release callback will get invoked prior to the remove callback being invoked which renders this whole debate moot. What am I missing here?

Quoted 2 lines

Jason

#5 lore raw

Re: [PATCH v3 2/2] s390/vfio-ap: control access to PQAP(AQIC) interception handler

Quoted 27 lines · Tony Krowiak
Quoted 15 lines · Jason Gunthorpe
Quoted 13 lines · Tony Krowiak
(quoted diff) +6 -4
@@ -287,13 +289,17 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
  	if (!(vcpu->arch.sie_block->eca & ECA_AIV))
  		return -EOPNOTSUPP;
-	apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
-	mutex_lock(&matrix_dev->lock);
+	rcu_read_lock();
+	pqap_module_hook = rcu_dereference(vcpu->kvm->arch.crypto.pqap_hook);
+	if (!pqap_module_hook) {
+		rcu_read_unlock();
+		goto set_status;
+	}
-	if (!vcpu->kvm->arch.crypto.pqap_hook)
-		goto out_unlock;
Quoted 8 lines

Review static int handle_pqap(struct kvm_vcpu *vcpu) · line 299

Quoted 5 lines
(quoted diff) +3 -2
-	matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
-				   struct ap_matrix_mdev, pqap_hook);
+	matrix_mdev = pqap_module_hook->data;
+	rcu_read_unlock();
+	mutex_lock(&matrix_dev->lock);

The matrix_mdev pointer was extracted from the pqap_module_hook, but now there is nothing protecting it since the rcu was dropped and it gets freed in vfio_ap_mdev_remove.

Quoted 6 lines

Therein lies the rub. We can't hold the rcu_read_lock across the entire time that the interception is being processed because of wait conditions in the interception handler. Regardless of whether the pointer to the matrix_mdev is retrieved as the container of or extracted from the pqap_hook, there is nothing protecting it and there appears to be no way to do so using RCU.

RCU is a lock that should only be used for highly performance sensitive read work loads. It eliminates one atomic from a lock, but if you go on to immediately do something like try_module_get, which has an atomic inside, the whole thing is pointless (assuming try_module_get was even the right thing to do)

Use a simple sleepable rwsem around the whole thing and forget about the module_get. Hold the write side when NULL'ing the pointer.

Quoted 6 lines
Quoted 3 lines

And, again, module locking doesn't prevent vfio_ap_mdev_remove() from being called. None of these patches should be combining module locking with RCU.

Is there any other way besides user interaction with the mdev's sysfs remove interface for the remove callback to get invoked?

There are more options after my re-organizing series.

But I'm not sure why you ask in response to my point about module locking? Module locking is not really related to sysfs.

Quoted 3 lines

If I try to remove the mdev using the sysfs interface while the mdev fd is still open by the guest, the remove hangs until the fd is closed.

Yes, it will wait when the vfio_device is being destroyed.

Quoted 3 lines

That being the case, the mdev release callback will get invoked prior to the remove callback being invoked which renders this whole debate moot. What am I missing here?

AFAICT the control flow is such that release can immediately move on to remove on the same CPU without an additional synchronization. So the kfree can still race with the above.

But the more I look at this the wonkier it is.. The real issue is not the matrix_mdev, it is the whole vcpu->kvm->arch.crypto.pqap_hook

This is nonesense too:

	if (vcpu->kvm->arch.crypto.pqap_hook) {
		if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
			return -EOPNOTSUPP;
		ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);

It should have a lock around it of some kind, not a try_module_get. module_get is not la lock.

And that lock should interact with loading the hook in the first place:

		kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;

And of course NULLin'g the hooke should be synchronous with the lock.

There should be no owner for something like this. unregistering the function pointer should fully fence all access.

The simple solution in sketch is just this:

arch/s390/kvm/priv.c +3 -4
index 9928f785c6773a..f70386452367dd 100644
@@ -657,13 +657,12 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
 	 * Verify that the hook callback is registered, lock the owner
 	 * and call the hook.
 	 */
-	if (vcpu->kvm->arch.crypto.pqap_hook) {
-		if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
-			return -EOPNOTSUPP;
+	if (down_read_trylock(&vcpu->kvm->arch.crypto.rwsem) &&
+	    vcpu->kvm->arch.crypto.pqap_hook) {
 		ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);
-		module_put(vcpu->kvm->arch.crypto.pqap_hook->owner);
 		if (!ret && vcpu->run->s.regs.gprs[1] & 0x00ff0000)
 			kvm_s390_set_psw_cc(vcpu, 3);
+		up_read(&vcpu->kv->arch.crypto.rwsem);
 		return ret;
 	}
 	/*
drivers/s390/crypto/vfio_ap_ops.c +5 -2
index b2c7e10dfdcdcf..64c89f6a711e94 100644
@@ -352,8 +352,7 @@ static int vfio_ap_mdev_create(struct mdev_device *mdev)
 	vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
 	init_waitqueue_head(&matrix_mdev->wait_for_kvm);
 	mdev_set_drvdata(mdev, matrix_mdev);
-	matrix_mdev->pqap_hook.hook = handle_pqap;
-	matrix_mdev->pqap_hook.owner = THIS_MODULE;
+	down_write(&&vcpu->kvm->arch.crypto.rwsem);
 	mutex_lock(&matrix_dev->lock);
 	list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
 	mutex_unlock(&matrix_dev->lock);
@@ -1132,7 +1131,9 @@ static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
 					  matrix_mdev->matrix.aqm,
 					  matrix_mdev->matrix.adm);
 		mutex_lock(&matrix_dev->lock);
+		down_write(&kvm->arch.crypto.rwsem);
 		kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
+		up_write(&kvm->arch.crypto.rwsem);
 		matrix_mdev->kvm = kvm;
 		matrix_mdev->kvm_busy = false;
 		wake_up_all(&matrix_mdev->wait_for_kvm);
@@ -1202,7 +1203,9 @@ static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
 		kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
 		mutex_lock(&matrix_dev->lock);
 		vfio_ap_mdev_reset_queues(matrix_mdev->mdev);
+		down_write(&matrix_mdev->kvm->arch.crypto.rwsem);
 		matrix_mdev->kvm->arch.crypto.pqap_hook = NULL;
+		up_write(&matrix_mdev->kvm->arch.crypto.rwsem);
 		kvm_put_kvm(matrix_mdev->kvm);
 		matrix_mdev->kvm = NULL;
 		matrix_mdev->kvm_busy = false;

Jason

author maintainer
#6 lore raw

Re: [PATCH v3 2/2] s390/vfio-ap: control access to PQAP(AQIC) interception handler

Quoted 44 lines · Jason Gunthorpe
Quoted 25 lines · Tony Krowiak
Quoted 15 lines · Jason Gunthorpe
Quoted 13 lines · Tony Krowiak
(quoted diff) +6 -4
@@ -287,13 +289,17 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
   	if (!(vcpu->arch.sie_block->eca & ECA_AIV))
   		return -EOPNOTSUPP;
-	apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
-	mutex_lock(&matrix_dev->lock);
+	rcu_read_lock();
+	pqap_module_hook = rcu_dereference(vcpu->kvm->arch.crypto.pqap_hook);
+	if (!pqap_module_hook) {
+		rcu_read_unlock();
+		goto set_status;
+	}
-	if (!vcpu->kvm->arch.crypto.pqap_hook)
-		goto out_unlock;
Quoted 8 lines

Review static int handle_pqap(struct kvm_vcpu *vcpu) · line 299

Quoted 5 lines
(quoted diff) +3 -2
-	matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
-				   struct ap_matrix_mdev, pqap_hook);
+	matrix_mdev = pqap_module_hook->data;
+	rcu_read_unlock();
+	mutex_lock(&matrix_dev->lock);

The matrix_mdev pointer was extracted from the pqap_module_hook, but now there is nothing protecting it since the rcu was dropped and it gets freed in vfio_ap_mdev_remove.

Quoted 6 lines

Therein lies the rub. We can't hold the rcu_read_lock across the entire time that the interception is being processed because of wait conditions in the interception handler. Regardless of whether the pointer to the matrix_mdev is retrieved as the container of or extracted from the pqap_hook, there is nothing protecting it and there appears to be no way to do so using RCU.

RCU is a lock that should only be used for highly performance sensitive read work loads. It eliminates one atomic from a lock, but if you go on to immediately do something like try_module_get, which has an atomic inside, the whole thing is pointless (assuming try_module_get was even the right thing to do)

Use a simple sleepable rwsem around the whole thing and forget about the module_get. Hold the write side when NULL'ing the pointer.

Quoted 3 lines
Quoted 3 lines

And, again, module locking doesn't prevent vfio_ap_mdev_remove() from being called. None of these patches should be combining module locking with RCU.

Quoted 6 lines
Quoted 2 lines

Is there any other way besides user interaction with the mdev's sysfs remove interface for the remove callback to get invoked?

There are more options after my re-organizing series.

But I'm not sure why you ask in response to my point about module locking? Module locking is not really related to sysfs.

I don't know why you keep harping on module locking. I didn't write the original code, so I really have no idea why that was incorporated. The question wasn't in response to module locking, it's just something that popped into my head at the time because based on my observations, the remove callback did not get invoked in response to a user removing the mdev via the sysfs interface until the mdev fd was closed by the guest.

Quoted 4 lines
Quoted 4 lines

If I try to remove the mdev using the sysfs interface while the mdev fd is still open by the guest, the remove hangs until the fd is closed.

Quoted 8 lines

Yes, it will wait when the vfio_device is being destroyed.

Quoted 3 lines

That being the case, the mdev release callback will get invoked prior to the remove callback being invoked which renders this whole debate moot. What am I missing here?

AFAICT the control flow is such that release can immediately move on to remove on the same CPU without an additional synchronization. So the kfree can still race with the above.

I'd have to look into it more, but my initial thought is that this is not true. If what I observed is true - i.e., the mdev remove callback won't get invoked until the mdev fd is closed by the guest - then the matrix_mdev will never get freed while the pqap_hook is not NULL because the mdev release callback - invoked when the mdev fd is closed - nullifies it. That is why I asked whether there is any other way that the mdev remove callback gets invoked other than when a user removes it via the sysfs remove attribute. Keep in mind, the matrix_mdev is removed under the matrix_dev->lock. If retrieve the matix_mdev in the interception handler from the matrix_dev->mdev_list under the matrix_dev->lock - instead of from the pqap_hook or the container_of macro - and exit the handler if it has been removed, then I think the race issue can be avoided.

Quoted 3 lines

But the more I look at this the wonkier it is.. The real issue is not the matrix_mdev, it is the whole vcpu->kvm->arch.crypto.pqap_hook

Actually, the matrix_mdev is the issue. If the handle_pqap callback is invoked after the matrix_mdev is freed, then because we retrieve it from the pqap_hook structure or even if we retrieve it from the enclosing container, the interception handler is screwed.

Quoted 10 lines

This is nonesense too:

	if (vcpu->kvm->arch.crypto.pqap_hook) {
		if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
			return -EOPNOTSUPP;
		ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);

It should have a lock around it of some kind, not a try_module_get. module_get is not la lock.

As I said earlier, I don't know why the author did this. My best guess is that he wanted to ensure that the module was still loaded; otherwise, the data structures contained therein - for example, the pqap_hook and matrix_mdev that contains it - would be gonzo.

Quoted 22 lines

And that lock should interact with loading the hook in the first place:

		kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;

And of course NULLin'g the hooke should be synchronous with the lock.

There should be no owner for something like this. unregistering the function pointer should fully fence all access.

The simple solution in sketch is just this:

arch/s390/kvm/priv.c +0 -2
index 9928f785c6773a..f70386452367dd 100644
@@ -657,13 +657,12 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
  	 * Verify that the hook callback is registered, lock the owner
  	 * and call the hook.
  	 */
-	if (vcpu->kvm->arch.crypto.pqap_hook) {
-		if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))

arch/s390/kvm/priv.c static int handle_pqap(struct kvm_vcpu *vcpu) · line 667

Quoted 10 lines
arch/s390/kvm/priv.c +3 -2
-			return -EOPNOTSUPP;
+	if (down_read_trylock(&vcpu->kvm->arch.crypto.rwsem) &&
+	    vcpu->kvm->arch.crypto.pqap_hook) {
  		ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);
-		module_put(vcpu->kvm->arch.crypto.pqap_hook->owner);
  		if (!ret && vcpu->run->s.regs.gprs[1] & 0x00ff0000)
  			kvm_s390_set_psw_cc(vcpu, 3);
+		up_read(&vcpu->kv->arch.crypto.rwsem);
  		return ret;
  	}

Since reading the various review comments related to this patch, I started working on something along the lines of that which you propose. I will post a new set of patches, probably tomorrow.

Quoted 38 lines
arch/s390/kvm/priv.c +0 -0
  	/*
drivers/s390/crypto/vfio_ap_ops.c +5 -2
index b2c7e10dfdcdcf..64c89f6a711e94 100644
@@ -352,8 +352,7 @@ static int vfio_ap_mdev_create(struct mdev_device *mdev)
  	vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
  	init_waitqueue_head(&matrix_mdev->wait_for_kvm);
  	mdev_set_drvdata(mdev, matrix_mdev);
-	matrix_mdev->pqap_hook.hook = handle_pqap;
-	matrix_mdev->pqap_hook.owner = THIS_MODULE;
+	down_write(&&vcpu->kvm->arch.crypto.rwsem);
  	mutex_lock(&matrix_dev->lock);
  	list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
  	mutex_unlock(&matrix_dev->lock);
@@ -1132,7 +1131,9 @@ static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
  					  matrix_mdev->matrix.aqm,
  					  matrix_mdev->matrix.adm);
  		mutex_lock(&matrix_dev->lock);
+		down_write(&kvm->arch.crypto.rwsem);
  		kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
+		up_write(&kvm->arch.crypto.rwsem);
  		matrix_mdev->kvm = kvm;
  		matrix_mdev->kvm_busy = false;
  		wake_up_all(&matrix_mdev->wait_for_kvm);
@@ -1202,7 +1203,9 @@ static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
  		kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
  		mutex_lock(&matrix_dev->lock);
  		vfio_ap_mdev_reset_queues(matrix_mdev->mdev);
+		down_write(&matrix_mdev->kvm->arch.crypto.rwsem);
  		matrix_mdev->kvm->arch.crypto.pqap_hook = NULL;
+		up_write(&matrix_mdev->kvm->arch.crypto.rwsem);
  		kvm_put_kvm(matrix_mdev->kvm);
  		matrix_mdev->kvm = NULL;
  		matrix_mdev->kvm_busy = false;

Jason

maintainer
#7 lore raw

Re: [PATCH v3 2/2] s390/vfio-ap: control access to PQAP(AQIC) interception handler

Quoted 8 lines · On Wed, 19 May 2021 21:08:15 -0400 Tony Krowiak <[email protected]> wrote:
Quoted 8 lines

This is nonesense too:

	if (vcpu->kvm->arch.crypto.pqap_hook) {
		if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
			return -EOPNOTSUPP;
		ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);
Quoted 4 lines
Quoted 2 lines

It should have a lock around it of some kind, not a try_module_get. module_get is not la lock.

As I said earlier, I don't know why the author did this.

Please have a look at these links from the archive to get some perspective:
https://lkml.org/lkml/2020/12/4/994
https://lkml.org/lkml/2020/12/3/987
https://www.lkml.org/lkml/2019/3/1/260

We can ask the original author, but I don't think we have to. BTW the patch that introduced it has your r-b.

Quoted 4 lines

My best guess
is that he wanted to ensure that the module was still loaded; otherwise, the data structures contained therein - for example, the pqap_hook and matrix_mdev that contains it - would be gonzo.

More precisely prevent the module from unloading while we execute code from it. As I've pointed out in a previous email the module may be gone by the time we call try_module_get().

Regards,
Halil

#8 lore raw

Re: [PATCH v3 2/2] s390/vfio-ap: control access to PQAP(AQIC) interception handler

Quoted 25 lines · Halil Pasic
Quoted 8 lines · On Wed, 19 May 2021 21:08:15 -0400 Tony Krowiak <[email protected]> wrote:
Quoted 8 lines

This is nonesense too:

	if (vcpu->kvm->arch.crypto.pqap_hook) {
		if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
			return -EOPNOTSUPP;
		ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);
Quoted 4 lines
Quoted 2 lines

It should have a lock around it of some kind, not a try_module_get. module_get is not la lock.

As I said earlier, I don't know why the author did this.

Please have a look at these links from the archive to get some perspective:
https://lkml.org/lkml/2020/12/4/994
https://lkml.org/lkml/2020/12/3/987
https://www.lkml.org/lkml/2019/3/1/260

We can ask the original author, but I don't think we have to. BTW the patch that introduced it has your r-b.

Quoted 8 lines
Quoted 4 lines

My best guess
is that he wanted to ensure that the module was still loaded; otherwise, the data structures contained therein - for example, the pqap_hook and matrix_mdev that contains it - would be gonzo.

More precisely prevent the module from unloading while we execute code from it. As I've pointed out in a previous email the module may be gone by the time we call try_module_get().

No, this is a common misconception.

The module_get prevents the module from even being attempted to be unloaded. Code should acquire this if it has done something that would cause a module remove function hang indefinitely, such as a design that waits for a user FD to close.

This provides a good user experience but should generally not be required for correctness.

All code passing function pointers across subsystems should always fully fence those function pointers during removal. This means it interacts with some kind of locking that guarentees nothing is currently calling, or ever will call again, those function pointers.

This is not just to protect the function pointer code itself, but the lock should also protect the data access that function pointer almost always invokes. This is the bug here, ap is accessing the matrix_dev data from a function pointer without any locking or serialization against kfree(matrix_dev). Fencing to guarentee the hook isn't and won't run also serves as a strong enough serialization to allow the kfree().

The basic logic is that a module removal cannot complete until all its function pointers have been removed from everywhere and all the locking that protect those removals are satisified.

Jason

maintainer
#9 lore raw

Re: [PATCH v3 2/2] s390/vfio-ap: control access to PQAP(AQIC) interception handler

In reply to Jason Gunthorpe
Quoted 35 lines · On Wed, 19 May 2021 20:22:02 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 27 lines · Tony Krowiak
Quoted 15 lines · Jason Gunthorpe
Quoted 13 lines · Tony Krowiak
(quoted diff) +6 -4
@@ -287,13 +289,17 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
  	if (!(vcpu->arch.sie_block->eca & ECA_AIV))
  		return -EOPNOTSUPP;
-	apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
-	mutex_lock(&matrix_dev->lock);
+	rcu_read_lock();
+	pqap_module_hook = rcu_dereference(vcpu->kvm->arch.crypto.pqap_hook);
+	if (!pqap_module_hook) {
+		rcu_read_unlock();
+		goto set_status;
+	}
-	if (!vcpu->kvm->arch.crypto.pqap_hook)
-		goto out_unlock;
Quoted 8 lines

Review static int handle_pqap(struct kvm_vcpu *vcpu) · line 299

Quoted 5 lines
(quoted diff) +3 -2
-	matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
-				   struct ap_matrix_mdev, pqap_hook);
+	matrix_mdev = pqap_module_hook->data;
+	rcu_read_unlock();
+	mutex_lock(&matrix_dev->lock);  

The matrix_mdev pointer was extracted from the pqap_module_hook, but now there is nothing protecting it since the rcu was dropped and it gets freed in vfio_ap_mdev_remove.

Quoted 6 lines

Therein lies the rub. We can't hold the rcu_read_lock across the entire time that the interception is being processed because of wait conditions in the interception handler. Regardless of whether the pointer to the matrix_mdev is retrieved as the container of or extracted from the pqap_hook, there is nothing protecting it and there appears to be no way to do so using RCU.

Quoted 2 lines

RCU is a lock that should only be used for highly performance sensitive read work loads.

This is not a highly performance sensitive read workload.

Quoted 4 lines

It eliminates one atomic from a lock, but
if you go on to immediately do something like try_module_get, which has an atomic inside, the whole thing is pointless (assuming try_module_get was even the right thing to do)

I'm not sure about this argument, or that RCU should be used only for highly performance sensitive read workloads. Can you please elaborate on the argument above and also put your statement in perspective with
https://lwn.net/Articles/263130/?

@Christian: Since you proposed RCU for this, I guess your opinion does not align with Jason's.

Quoted 4 lines

Use a simple sleepable rwsem around the whole thing and forget about the module_get. Hold the write side when NULL'ing the pointer.

Yes a simple sleepable lock would work, and we wouldn't need get_module(). Because before the vfio_ap module unloads, it sets all vcpu->kvm->arch.crypto.pqap_hook instances to NULL. So if we know that vcpu->kvm->arch.crypto.pqap_hook then we know that the still have valid references to the module.

Quoted 38 lines
Quoted 6 lines
Quoted 3 lines

And, again, module locking doesn't prevent vfio_ap_mdev_remove() from being called. None of these patches should be combining module locking with RCU.

Is there any other way besides user interaction with the mdev's sysfs remove interface for the remove callback to get invoked?

There are more options after my re-organizing series.

But I'm not sure why you ask in response to my point about module locking? Module locking is not really related to sysfs.

Quoted 3 lines

If I try to remove the mdev using the sysfs interface while the mdev fd is still open by the guest, the remove hangs until the fd is closed.

Yes, it will wait when the vfio_device is being destroyed.

Quoted 3 lines

That being the case, the mdev release callback will get invoked prior to the remove callback being invoked which renders this whole debate moot. What am I missing here?

AFAICT the control flow is such that release can immediately move on to remove on the same CPU without an additional synchronization. So the kfree can still race with the above.

But the more I look at this the wonkier it is.. The real issue is not the matrix_mdev, it is the whole vcpu->kvm->arch.crypto.pqap_hook

This is nonesense too:

	if (vcpu->kvm->arch.crypto.pqap_hook) {
		if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
			return -EOPNOTSUPP;
		ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);
Quoted 2 lines

It should have a lock around it of some kind, not a try_module_get. module_get is not la lock.

I tend to agree. In fact I asked for a lock around it several times:
https://www.lkml.org/lkml/2019/3/1/260
https://lkml.org/lkml/2020/12/3/987
https://lkml.org/lkml/2020/12/4/994

But in my opinion RCU is also viable (not this patch). I think, I prefer a lock for simplicity, unless it is not (deadlocks) ;).

Many thanks for bringing your perspective to this. I'm optimistic about getting this finally addressed properly.

Regards,
Halil

#10 lore raw

Re: [PATCH v3 2/2] s390/vfio-ap: control access to PQAP(AQIC) interception handler

Quoted 7 lines · Halil Pasic
Quoted 4 lines

It eliminates one atomic from a lock, but
if you go on to immediately do something like try_module_get, which has an atomic inside, the whole thing is pointless (assuming try_module_get was even the right thing to do)

I'm not sure about this argument, or that RCU should be used only for highly performance sensitive read workloads.

Fundamentally RCU is a technique for building a read/write lock that avoids an atomic incr on the read side. This comes at the cost of significantly slowing down the write side.

Everything about RCU is very complicated, people typically use it wrong.

People use it wrong so often than Paul wrote a very long list of guidelines to help understand if it is being used properly:

Documentation/RCU/checklist.rst

If you haven't memorized this document then you probably shouldn't be using RCU at all :)

Quoted 2 lines

Can you please elaborate on the argument above and also put your statement in perspective with https://lwn.net/Articles/263130/?

This article doesn't talk much about the downsides - and focuses on performance win. If you need the performance then sure use RCU, but RCU is not a general purpose replacement for a rwsem. People should *always* reach for a rwsem first. Design that properly and then ask themselves if the rwsem can or should be optimized to a RCU.

Quoted 5 lines

Yes a simple sleepable lock would work, and we wouldn't need get_module(). Because before the vfio_ap module unloads, it sets all vcpu->kvm->arch.crypto.pqap_hook instances to NULL. So if we know that vcpu->kvm->arch.crypto.pqap_hook then we know that the still have valid references to the module.

Yes

Quoted 2 lines

But in my opinion RCU is also viable (not this patch). I think, I prefer a lock for simplicity, unless it is not (deadlocks) ;).

As soon as you said you needed to sleep RCU stopped being viable. To make a sleepable region you need to do an atomic write and at that instant all the value of RCU was destroyed - use a normal rwsem.

There is SRCU that solves the sleepable problem, but it has an incredible cost in both write side performance and memory usage that should only be reached for if high read side performance is really required.

Jason

author maintainer
#11 lore raw

Re: [PATCH v3 2/2] s390/vfio-ap: control access to PQAP(AQIC) interception handler

In reply to Jason Gunthorpe
Quoted 2 lines

The simple solution in sketch is just this:

The code below results in a lockdep WARN_ON for the reasons documented in my comments interspersed in the code.

After trying several different permutations using RCU and
an rw_semaphore, I came to the conclusion that setting and clearing the hook pointer while the mdev fd is being open and closed or when the mdev is being removed unnecessarily complicates things. There is no good reason to set/clear the function pointer at this time, nor is there any compelling reason to store the function pointer in a satellite structure of the kvm struct. Since the hook function's lifespan coincides with the lifespan of the vfio_ap module, why not store it when the module is loaded and clear it when the module is unloaded? Access to the function pointer can be controlled by a lock that is independent of the matrix_dev->lock, thus avoiding potential lock dependencies. Access to the mdev is controlled by the matrix_dev lock, so if the mdev is retrieved from the matrix_dev->mdev_list in the hook function, then we are assured that the mdev will never be accessed after it is freed; problem solved.

Quoted 6 lines
arch/s390/kvm/priv.c +0 -0
index 9928f785c6773a..f70386452367dd 100644

arch/s390/kvm/priv.c static int handle_pqap(struct kvm_vcpu *vcpu) · line 662

Quoted 9 lines
arch/s390/kvm/priv.c +2 -3
@@ -657,13 +657,12 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
  	 * Verify that the hook callback is registered, lock the owner
  	 * and call the hook.
  	 */
-	if (vcpu->kvm->arch.crypto.pqap_hook) {
-		if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
-			return -EOPNOTSUPP;
+	if (down_read_trylock(&vcpu->kvm->arch.crypto.rwsem) &&
+	    vcpu->kvm->arch.crypto.pqap_hook) {
  		ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);

So, the hook function (handle_pqap in vfio_ap_ops.c) is executed while holding the rwsem lock. The hook function tries to lock the matrix_dev->lock mutex.

Quoted 22 lines
arch/s390/kvm/priv.c +1 -1
-		module_put(vcpu->kvm->arch.crypto.pqap_hook->owner);
  		if (!ret && vcpu->run->s.regs.gprs[1] & 0x00ff0000)
  			kvm_s390_set_psw_cc(vcpu, 3);
+		up_read(&vcpu->kv->arch.crypto.rwsem);
  		return ret;
  	}
  	/*
drivers/s390/crypto/vfio_ap_ops.c +1 -2
index b2c7e10dfdcdcf..64c89f6a711e94 100644
@@ -352,8 +352,7 @@ static int vfio_ap_mdev_create(struct mdev_device *mdev)
  	vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
  	init_waitqueue_head(&matrix_mdev->wait_for_kvm);
  	mdev_set_drvdata(mdev, matrix_mdev);
-	matrix_mdev->pqap_hook.hook = handle_pqap;
-	matrix_mdev->pqap_hook.owner = THIS_MODULE;
+	down_write(&&vcpu->kvm->arch.crypto.rwsem);
  	mutex_lock(&matrix_dev->lock);
  	list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
  	mutex_unlock(&matrix_dev->lock);

drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev, · line 1133

Quoted 3 lines
drivers/s390/crypto/vfio_ap_ops.c +0 -0
@@ -1132,7 +1131,9 @@ static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
  					  matrix_mdev->matrix.aqm,
  					  matrix_mdev->matrix.adm);
  		mutex_lock(&matrix_dev->lock);

Locks the matrix_dev->lock mutex, then tries to lock rwsem to store the pqap_hook under the rwsem lock. During testing, this occurred while the interception of the PQAP instruction was taking place, so the read lock was already taken and the hook function was waiting on the matrix_dev->lock taken above. All of the test cases ran successfully to completion, so there didn't appear to be a deadlock of any sort, but lockdep apparently detected a problem.

I was able to get around this by doing the down_write and setting the hook vfio_ap_mdev_group_notifier function before calling this function (above) and before taking the matrix_dev->lock, but that circumvents the protection against accessing a matrix_dev that's already been freed.

drivers/s390/crypto/vfio_ap_ops.c static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev) · line 1206

Quoted 11 lines
drivers/s390/crypto/vfio_ap_ops.c +3 -0
+		down_write(&kvm->arch.crypto.rwsem);
  		kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
+		up_write(&kvm->arch.crypto.rwsem);
  		matrix_mdev->kvm = kvm;
  		matrix_mdev->kvm_busy = false;
  		wake_up_all(&matrix_mdev->wait_for_kvm);
@@ -1202,7 +1203,9 @@ static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
  		kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
  		mutex_lock(&matrix_dev->lock);
  		vfio_ap_mdev_reset_queues(matrix_mdev->mdev);
+		down_write(&matrix_mdev->kvm->arch.crypto.rwsem);

Same scenario here.

Quoted 8 lines
drivers/s390/crypto/vfio_ap_ops.c +1 -0
  		matrix_mdev->kvm->arch.crypto.pqap_hook = NULL;
+		up_write(&matrix_mdev->kvm->arch.crypto.rwsem);
  		kvm_put_kvm(matrix_mdev->kvm);
  		matrix_mdev->kvm = NULL;
  		matrix_mdev->kvm_busy = false;

Jason

#12 lore raw

Re: [PATCH v3 2/2] s390/vfio-ap: control access to PQAP(AQIC) interception handler

Quoted 6 lines · Tony Krowiak
Quoted 2 lines

The simple solution in sketch is just this:

The code below results in a lockdep WARN_ON for the reasons documented in my comments interspersed in the code.

Sure, to be expected for a 2 min effort, but you seem to have solved it by trivially putting things in the right locking order?

Quoted 11 lines

After trying several different permutations using RCU and an rw_semaphore, I came to the conclusion that setting and clearing the hook pointer while the mdev fd is being open and closed or when the mdev is being removed unnecessarily complicates things. There is no good reason to set/clear the function pointer at this time, nor is there any compelling reason to store the function pointer in a satellite structure of the kvm struct. Since the hook function's lifespan coincides with the lifespan of the vfio_ap module, why not store it when the module is loaded and clear it when the module is unloaded?

Well, the hook function isn't really the problem..

Quoted 6 lines

Access to the function pointer can be controlled by a lock that is independent of the matrix_dev->lock, thus avoiding potential lock dependencies. Access to the mdev is controlled by the matrix_dev lock, so if the mdev is retrieved from the matrix_dev->mdev_list in the hook function, then we are assured that the mdev will never be accessed after it is freed; problem solved.

This just transforms the problem into needing to hold a lock around mdev_list while accessing a member of the mdev_list

Is it simpler?

Jason

maintainer
#13 lore raw

Re: [PATCH v3 2/2] s390/vfio-ap: control access to PQAP(AQIC) interception handler

In reply to Tony Krowiak
Quoted 21 lines · On Wed, 19 May 2021 11:39:21 -0400 Tony Krowiak <[email protected]> wrote:

There is currently nothing that controls access to the structure that contains the function pointer to the handler that processes interception of the PQAP(AQIC) instruction. If the mdev is removed while the PQAP(AQIC) instruction is being intercepted, there is a possibility that the function pointer to the handler can get wiped out prior to the attempt to call it.

This patch utilizes RCU to synchronize access to the kvm_s390_module_hook structure used to process interception of the PQAP(AQIC) instruction.

---
 arch/s390/include/asm/kvm_host.h  |  1 +
 arch/s390/kvm/priv.c              | 47 ++++++++++++++++-----------
 drivers/s390/crypto/vfio_ap_ops.c | 54 ++++++++++++++++++++++++-------
 3 files changed, 73 insertions(+), 29 deletions(-)
arch/s390/include/asm/kvm_host.h +0 -0
index 8925f3969478..4987e82d6116 100644

arch/s390/include/asm/kvm_host.h struct kvm_s390_cpu_model { · line 809

Quoted 4 lines
arch/s390/include/asm/kvm_host.h +1 -0
@@ -806,6 +806,7 @@ struct kvm_s390_cpu_model {
 struct kvm_s390_module_hook {
 	int (*hook)(struct kvm_vcpu *vcpu);
 	struct module *owner;
+	void *data;

I guess you need this, because you stopped using the member of struct ap_mdev_matrix and instead you kzalloc() a new object. Yet I don't understand why do you do so?

Quoted 29 lines
arch/s390/include/asm/kvm_host.h +0 -0
 };
 
 struct kvm_s390_crypto {
arch/s390/kvm/priv.c +4 -6
index 9928f785c677..2d330dfbdb61 100644
@@ -610,8 +610,11 @@ static int handle_io_inst(struct kvm_vcpu *vcpu)
 static int handle_pqap(struct kvm_vcpu *vcpu)
 {
 	struct ap_queue_status status = {};
+	struct kvm_s390_module_hook *pqap_module_hook;
+	int (*pqap_hook)(struct kvm_vcpu *vcpu);
+	struct module *owner;
 	unsigned long reg0;
-	int ret;
+	int ret = 0;
 	uint8_t fc;
 
 	/* Verify that the AP instruction are available */
@@ -657,24 +660,32 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
 	 * Verify that the hook callback is registered, lock the owner
 	 * and call the hook.
 	 */
-	if (vcpu->kvm->arch.crypto.pqap_hook) {
-		if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
-			return -EOPNOTSUPP;
-		ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);
-		module_put(vcpu->kvm->arch.crypto.pqap_hook->owner);

arch/s390/kvm/priv.c static int handle_pqap(struct kvm_vcpu *vcpu) · line 669

Quoted 10 lines
arch/s390/kvm/priv.c +7 -3
-		if (!ret && vcpu->run->s.regs.gprs[1] & 0x00ff0000)
-			kvm_s390_set_psw_cc(vcpu, 3);
-		return ret;
+	rcu_read_lock();
+	pqap_module_hook = rcu_dereference(vcpu->kvm->arch.crypto.pqap_hook);
+	if (pqap_module_hook) {
+		pqap_hook = pqap_module_hook->hook;
+		owner = pqap_module_hook->owner;
+		rcu_read_unlock();
+		if (!try_module_get(owner)) {

Why do this outside the rcu_read lock?

What guarantees that the module ain't gone by this time? I don't think try_module_get() is guaranteed to give you false if passed in a pointer that points to some memory that ain't a struct module any more (use-after-free).

Quoted 61 lines
arch/s390/kvm/priv.c +18 -9
+			ret = -EOPNOTSUPP;
+		} else {
+			ret = pqap_hook(vcpu);
+			module_put(owner);
+			if (!ret && vcpu->run->s.regs.gprs[1] & 0x00ff0000)
+				kvm_s390_set_psw_cc(vcpu, 3);
+		}
+	} else {
+		rcu_read_unlock();
+		/*
+		 * A vfio_driver must register a hook.
+		 * No hook means no driver to enable the SIE CRYCB and no
+		 * queues. We send this response to the guest.
+		 */
+		status.response_code = 0x01;
+		memcpy(&vcpu->run->s.regs.gprs[1], &status, sizeof(status));
+		kvm_s390_set_psw_cc(vcpu, 3);
 	}
-	/*
-	 * A vfio_driver must register a hook.
-	 * No hook means no driver to enable the SIE CRYCB and no queues.
-	 * We send this response to the guest.
-	 */
-	status.response_code = 0x01;
-	memcpy(&vcpu->run->s.regs.gprs[1], &status, sizeof(status));
-	kvm_s390_set_psw_cc(vcpu, 3);
-	return 0;
+	return ret;
 }
 
 static int handle_stfl(struct kvm_vcpu *vcpu)
drivers/s390/crypto/vfio_ap_ops.c +6 -2
index f90c9103dac2..a6aa3f753ac4 100644
@@ -16,6 +16,7 @@
 #include <linux/bitops.h>
 #include <linux/kvm_host.h>
 #include <linux/module.h>
+#include <linux/rcupdate.h>
 #include <asm/kvm.h>
 #include <asm/zcrypt.h>
 
@@ -279,6 +280,7 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
 	uint64_t status;
 	uint16_t apqn;
 	struct vfio_ap_queue *q;
+	struct kvm_s390_module_hook *pqap_module_hook;
 	struct ap_queue_status qstatus = {
 			       .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
 	struct ap_matrix_mdev *matrix_mdev;
@@ -287,13 +289,17 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
 	if (!(vcpu->arch.sie_block->eca & ECA_AIV))
 		return -EOPNOTSUPP;
 
-	apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
-	mutex_lock(&matrix_dev->lock);
+	rcu_read_lock();
+	pqap_module_hook = rcu_dereference(vcpu->kvm->arch.crypto.pqap_hook);
+	if (!pqap_module_hook) {
+		rcu_read_unlock();

drivers/s390/crypto/vfio_ap_ops.c static int handle_pqap(struct kvm_vcpu *vcpu) · line 301

Quoted 10 lines
drivers/s390/crypto/vfio_ap_ops.c +5 -4
+		goto set_status;
+	}
 
-	if (!vcpu->kvm->arch.crypto.pqap_hook)
-		goto out_unlock;
-	matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
-				   struct ap_matrix_mdev, pqap_hook);
+	matrix_mdev = pqap_module_hook->data;
+	rcu_read_unlock();
+	mutex_lock(&matrix_dev->lock);

I agree with Jason's assessment. At this point the matrix_dev pointer may point to garbage.

Above, I think we can use the pqap_hook function pointer local to handle_pqap, because we know that as long as the module is there the callback will sit at the same address and won't go away. And we do the try_module_get() to ensure that the module stays loaded.

Quoted 17 lines
drivers/s390/crypto/vfio_ap_ops.c +3 -1
+	apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
 
 	/*
 	 * If the KVM pointer is in the process of being set, wait until the
@@ -322,9 +328,10 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
 		qstatus = vfio_ap_irq_disable(q);
 
 out_unlock:
+	mutex_unlock(&matrix_dev->lock);
+set_status:
 	memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
 	vcpu->run->s.regs.gprs[1] >>= 32;
-	mutex_unlock(&matrix_dev->lock);
 	return 0;
 }
 

drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_create(struct mdev_device *mdev) · line 357

Quoted 5 lines
drivers/s390/crypto/vfio_ap_ops.c +0 -2
@@ -353,8 +360,6 @@ static int vfio_ap_mdev_create(struct mdev_device *mdev)
 	vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
 	init_waitqueue_head(&matrix_mdev->wait_for_kvm);
 	mdev_set_drvdata(mdev, matrix_mdev);
-	matrix_mdev->pqap_hook.hook = handle_pqap;
-	matrix_mdev->pqap_hook.owner = THIS_MODULE;

I guess the member of struct ap_matrix_mdev is still around, it will remain all zero. Is this somehow intentional?

drivers/s390/crypto/vfio_ap_ops.c static const struct attribute_group *vfio_ap_mdev_attr_groups[] = { · line 1098

Quoted 13 lines
drivers/s390/crypto/vfio_ap_ops.c +6 -0
 	mutex_lock(&matrix_dev->lock);
 	list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
 	mutex_unlock(&matrix_dev->lock);
@@ -1085,6 +1090,22 @@ static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
 	NULL
 };
 
+static int vfio_ap_mdev_set_pqap_hook(struct ap_matrix_mdev *matrix_mdev,
+				       struct kvm *kvm)
+{
+	struct kvm_s390_module_hook *pqap_hook;
+
+	pqap_hook = kmalloc(sizeof(*kvm->arch.crypto.pqap_hook), GFP_KERNEL);

What is the extra allocation supposed to buy us?

Quoted 67 lines
drivers/s390/crypto/vfio_ap_ops.c +23 -2
+	if (!pqap_hook)
+		return -ENOMEM;
+	pqap_hook->data = matrix_mdev;
+	pqap_hook->hook = handle_pqap;
+	pqap_hook->owner = THIS_MODULE;
+	rcu_assign_pointer(kvm->arch.crypto.pqap_hook, pqap_hook);
+
+	return 0;
+}
+
 /**
  * vfio_ap_mdev_set_kvm
  *
@@ -1107,6 +1128,7 @@ static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
 				struct kvm *kvm)
 {
+	int ret;
 	struct ap_matrix_mdev *m;
 
 	if (kvm->arch.crypto.crycbd) {
@@ -1115,6 +1137,10 @@ static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
 				return -EPERM;
 		}
 
+		ret = vfio_ap_mdev_set_pqap_hook(matrix_mdev, kvm);
+		if (ret)
+			return ret;
+
 		kvm_get_kvm(kvm);
 		matrix_mdev->kvm_busy = true;
 		mutex_unlock(&matrix_dev->lock);
@@ -1123,7 +1149,6 @@ static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
 					  matrix_mdev->matrix.aqm,
 					  matrix_mdev->matrix.adm);
 		mutex_lock(&matrix_dev->lock);
-		kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
 		matrix_mdev->kvm = kvm;
 		matrix_mdev->kvm_busy = false;
 		wake_up_all(&matrix_mdev->wait_for_kvm);
@@ -1161,6 +1186,13 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,
 	return NOTIFY_DONE;
 }
 
+static void vfio_ap_mdev_unset_pqap_hook(struct kvm *kvm)
+{
+	rcu_assign_pointer(kvm->arch.crypto.pqap_hook, NULL);
+	synchronize_rcu();
+	kfree(kvm->arch.crypto.pqap_hook);
+}
+
 /**
  * vfio_ap_mdev_unset_kvm
  *
@@ -1189,11 +1221,11 @@ static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
 
 	if (matrix_mdev->kvm) {
 		matrix_mdev->kvm_busy = true;
+		vfio_ap_mdev_unset_pqap_hook(matrix_mdev->kvm);
 		mutex_unlock(&matrix_dev->lock);
 		kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
 		mutex_lock(&matrix_dev->lock);
 		vfio_ap_mdev_reset_queues(matrix_mdev->mdev);
-		matrix_mdev->kvm->arch.crypto.pqap_hook = NULL;
 		kvm_put_kvm(matrix_mdev->kvm);
 		matrix_mdev->kvm = NULL;
 		matrix_mdev->kvm_busy = false;
author maintainer
#14 lore raw

Re: [PATCH v3 2/2] s390/vfio-ap: control access to PQAP(AQIC) interception handler

Quoted 19 lines · Halil Pasic
Quoted 16 lines · On Wed, 19 May 2021 11:39:21 -0400 Tony Krowiak <[email protected]> wrote:

There is currently nothing that controls access to the structure that contains the function pointer to the handler that processes interception of the PQAP(AQIC) instruction. If the mdev is removed while the PQAP(AQIC) instruction is being intercepted, there is a possibility that the function pointer to the handler can get wiped out prior to the attempt to call it.

This patch utilizes RCU to synchronize access to the kvm_s390_module_hook structure used to process interception of the PQAP(AQIC) instruction.

---
  arch/s390/include/asm/kvm_host.h  |  1 +
  arch/s390/kvm/priv.c              | 47 ++++++++++++++++-----------
  drivers/s390/crypto/vfio_ap_ops.c | 54 ++++++++++++++++++++++++-------
  3 files changed, 73 insertions(+), 29 deletions(-)
Quoted 12 lines

arch/s390/include/asm/kvm_host.h struct kvm_s390_cpu_model { · line 809

Quoted 9 lines
arch/s390/include/asm/kvm_host.h +1 -0
index 8925f3969478..4987e82d6116 100644
@@ -806,6 +806,7 @@ struct kvm_s390_cpu_model {
  struct kvm_s390_module_hook {
  	int (*hook)(struct kvm_vcpu *vcpu);
  	struct module *owner;
+	void *data;

I guess you need this, because you stopped using the member of struct ap_mdev_matrix and instead you kzalloc() a new object. Yet I don't understand why do you do so?

I did so because the mdev remove callback frees the matrix_mdev and I thought I could protect against accessing freed storage by storing the pointer in the pqap_hook structure; however, since we can't hold the rcu_read_lock while the handle_pqap is executing - due to wait conditions - this turns out to be a bad idea. I'm not sure at this point that we can use RCU for this because the freeing of the matrix_mdev is independent of pqap processing.

Quoted 33 lines
Quoted 33 lines
arch/s390/include/asm/kvm_host.h +0 -0
  };
 
  struct kvm_s390_crypto {
arch/s390/kvm/priv.c +4 -9
index 9928f785c677..2d330dfbdb61 100644
@@ -610,8 +610,11 @@ static int handle_io_inst(struct kvm_vcpu *vcpu)
  static int handle_pqap(struct kvm_vcpu *vcpu)
  {
  	struct ap_queue_status status = {};
+	struct kvm_s390_module_hook *pqap_module_hook;
+	int (*pqap_hook)(struct kvm_vcpu *vcpu);
+	struct module *owner;
  	unsigned long reg0;
-	int ret;
+	int ret = 0;
  	uint8_t fc;
 
  	/* Verify that the AP instruction are available */
@@ -657,24 +660,32 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
  	 * Verify that the hook callback is registered, lock the owner
  	 * and call the hook.
  	 */
-	if (vcpu->kvm->arch.crypto.pqap_hook) {
-		if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
-			return -EOPNOTSUPP;
-		ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);
-		module_put(vcpu->kvm->arch.crypto.pqap_hook->owner);
-		if (!ret && vcpu->run->s.regs.gprs[1] & 0x00ff0000)
-			kvm_s390_set_psw_cc(vcpu, 3);
-		return ret;
Quoted 8 lines

arch/s390/kvm/priv.c static int handle_pqap(struct kvm_vcpu *vcpu) · line 669

Quoted 7 lines
arch/s390/kvm/priv.c +7 -0
+	rcu_read_lock();
+	pqap_module_hook = rcu_dereference(vcpu->kvm->arch.crypto.pqap_hook);
+	if (pqap_module_hook) {
+		pqap_hook = pqap_module_hook->hook;
+		owner = pqap_module_hook->owner;
+		rcu_read_unlock();
+		if (!try_module_get(owner)) {

Why do this outside the rcu_read lock?

It should be done inside the lock.

Quoted 5 lines

What guarantees that the module ain't gone by this time? I don't think try_module_get() is guaranteed to give you false if passed in a pointer that points to some memory that ain't a struct module any more (use-after-free).

That needs to be inside the lock.

Quoted 75 lines
Quoted 62 lines
arch/s390/kvm/priv.c +18 -9
+			ret = -EOPNOTSUPP;
+		} else {
+			ret = pqap_hook(vcpu);
+			module_put(owner);
+			if (!ret && vcpu->run->s.regs.gprs[1] & 0x00ff0000)
+				kvm_s390_set_psw_cc(vcpu, 3);
+		}
+	} else {
+		rcu_read_unlock();
+		/*
+		 * A vfio_driver must register a hook.
+		 * No hook means no driver to enable the SIE CRYCB and no
+		 * queues. We send this response to the guest.
+		 */
+		status.response_code = 0x01;
+		memcpy(&vcpu->run->s.regs.gprs[1], &status, sizeof(status));
+		kvm_s390_set_psw_cc(vcpu, 3);
  	}
-	/*
-	 * A vfio_driver must register a hook.
-	 * No hook means no driver to enable the SIE CRYCB and no queues.
-	 * We send this response to the guest.
-	 */
-	status.response_code = 0x01;
-	memcpy(&vcpu->run->s.regs.gprs[1], &status, sizeof(status));
-	kvm_s390_set_psw_cc(vcpu, 3);
-	return 0;
+	return ret;
  }
 
  static int handle_stfl(struct kvm_vcpu *vcpu)
drivers/s390/crypto/vfio_ap_ops.c +6 -2
index f90c9103dac2..a6aa3f753ac4 100644
@@ -16,6 +16,7 @@
  #include <linux/bitops.h>
  #include <linux/kvm_host.h>
  #include <linux/module.h>
+#include <linux/rcupdate.h>
  #include <asm/kvm.h>
  #include <asm/zcrypt.h>
 
@@ -279,6 +280,7 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
  	uint64_t status;
  	uint16_t apqn;
  	struct vfio_ap_queue *q;
+	struct kvm_s390_module_hook *pqap_module_hook;
  	struct ap_queue_status qstatus = {
  			       .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
  	struct ap_matrix_mdev *matrix_mdev;
@@ -287,13 +289,17 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
  	if (!(vcpu->arch.sie_block->eca & ECA_AIV))
  		return -EOPNOTSUPP;
 
-	apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
-	mutex_lock(&matrix_dev->lock);
+	rcu_read_lock();
+	pqap_module_hook = rcu_dereference(vcpu->kvm->arch.crypto.pqap_hook);
+	if (!pqap_module_hook) {
+		rcu_read_unlock();

drivers/s390/crypto/vfio_ap_ops.c static int handle_pqap(struct kvm_vcpu *vcpu) · line 301

Quoted 10 lines
drivers/s390/crypto/vfio_ap_ops.c +5 -4
+		goto set_status;
+	}
 
-	if (!vcpu->kvm->arch.crypto.pqap_hook)
-		goto out_unlock;
-	matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
-				   struct ap_matrix_mdev, pqap_hook);
+	matrix_mdev = pqap_module_hook->data;
+	rcu_read_unlock();
+	mutex_lock(&matrix_dev->lock);

I agree with Jason's assessment. At this point the matrix_dev pointer may point to garbage.

Quoted 4 lines

Above, I think we can use the pqap_hook function pointer local to handle_pqap, because we know that as long as the module is there the callback will sit at the same address and won't go away. And we do the try_module_get() to ensure that the module stays loaded.

I'll take your word that is true. If so, then I think I can replace the way we get the matrix_mdev in the pqap handler which can be done under the matrix_dev->lock. I can write a function to get the matrix from the list of mdevs in matrix_dev. If the matrix_mdev has been removed, then we can bail out of the handle_pqap function. Maybe the RCU can be used after all.

Quoted 110 lines
Quoted 19 lines
drivers/s390/crypto/vfio_ap_ops.c +3 -1
+	apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
 
  	/*
  	 * If the KVM pointer is in the process of being set, wait until the
@@ -322,9 +328,10 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
  		qstatus = vfio_ap_irq_disable(q);
 
  out_unlock:
+	mutex_unlock(&matrix_dev->lock);
+set_status:
  	memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
  	vcpu->run->s.regs.gprs[1] >>= 32;
-	mutex_unlock(&matrix_dev->lock);
  	return 0;
  }
 

drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_create(struct mdev_device *mdev) · line 357

Quoted 5 lines
drivers/s390/crypto/vfio_ap_ops.c +0 -2
@@ -353,8 +360,6 @@ static int vfio_ap_mdev_create(struct mdev_device *mdev)
  	vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
  	init_waitqueue_head(&matrix_mdev->wait_for_kvm);
  	mdev_set_drvdata(mdev, matrix_mdev);
-	matrix_mdev->pqap_hook.hook = handle_pqap;
-	matrix_mdev->pqap_hook.owner = THIS_MODULE;

I guess the member of struct ap_matrix_mdev is still around, it will remain all zero. Is this somehow intentional?

drivers/s390/crypto/vfio_ap_ops.c static const struct attribute_group *vfio_ap_mdev_attr_groups[] = { · line 1098

Quoted 13 lines
drivers/s390/crypto/vfio_ap_ops.c +6 -0
  	mutex_lock(&matrix_dev->lock);
  	list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
  	mutex_unlock(&matrix_dev->lock);
@@ -1085,6 +1090,22 @@ static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
  	NULL
  };
 
+static int vfio_ap_mdev_set_pqap_hook(struct ap_matrix_mdev *matrix_mdev,
+				       struct kvm *kvm)
+{
+	struct kvm_s390_module_hook *pqap_hook;
+
+	pqap_hook = kmalloc(sizeof(*kvm->arch.crypto.pqap_hook), GFP_KERNEL);

What is the extra allocation supposed to buy us?

Quoted 67 lines
drivers/s390/crypto/vfio_ap_ops.c +23 -2
+	if (!pqap_hook)
+		return -ENOMEM;
+	pqap_hook->data = matrix_mdev;
+	pqap_hook->hook = handle_pqap;
+	pqap_hook->owner = THIS_MODULE;
+	rcu_assign_pointer(kvm->arch.crypto.pqap_hook, pqap_hook);
+
+	return 0;
+}
+
  /**
   * vfio_ap_mdev_set_kvm
   *
@@ -1107,6 +1128,7 @@ static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
  static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
  				struct kvm *kvm)
  {
+	int ret;
  	struct ap_matrix_mdev *m;
 
  	if (kvm->arch.crypto.crycbd) {
@@ -1115,6 +1137,10 @@ static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
  				return -EPERM;
  		}
 
+		ret = vfio_ap_mdev_set_pqap_hook(matrix_mdev, kvm);
+		if (ret)
+			return ret;
+
  		kvm_get_kvm(kvm);
  		matrix_mdev->kvm_busy = true;
  		mutex_unlock(&matrix_dev->lock);
@@ -1123,7 +1149,6 @@ static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
  					  matrix_mdev->matrix.aqm,
  					  matrix_mdev->matrix.adm);
  		mutex_lock(&matrix_dev->lock);
-		kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
  		matrix_mdev->kvm = kvm;
  		matrix_mdev->kvm_busy = false;
  		wake_up_all(&matrix_mdev->wait_for_kvm);
@@ -1161,6 +1186,13 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,
  	return NOTIFY_DONE;
  }
 
+static void vfio_ap_mdev_unset_pqap_hook(struct kvm *kvm)
+{
+	rcu_assign_pointer(kvm->arch.crypto.pqap_hook, NULL);
+	synchronize_rcu();
+	kfree(kvm->arch.crypto.pqap_hook);
+}
+
  /**
   * vfio_ap_mdev_unset_kvm
   *
@@ -1189,11 +1221,11 @@ static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
 
  	if (matrix_mdev->kvm) {
  		matrix_mdev->kvm_busy = true;
+		vfio_ap_mdev_unset_pqap_hook(matrix_mdev->kvm);
  		mutex_unlock(&matrix_dev->lock);
  		kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
  		mutex_lock(&matrix_dev->lock);
  		vfio_ap_mdev_reset_queues(matrix_mdev->mdev);
-		matrix_mdev->kvm->arch.crypto.pqap_hook = NULL;
  		kvm_put_kvm(matrix_mdev->kvm);
  		matrix_mdev->kvm = NULL;
  		matrix_mdev->kvm_busy = false;