Skip to contentSkip to conversation

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

PATCH Superseded Tony Krowiak 2 participants 6 messages Jun 16, 2021 – Jun 17, 2021

Subsystems (4)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Tony Krowiak

author maintainer
#0 lore raw

v5 cover 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 of the mdev. Consequently, the memory for the resources allocated when the mdev was created are leaked.

To fix this issue:

* The remove callback will not terminate with -EBUSY when the mdev is in use by a KVM guest.

* The memory for the resources allocated when the mdev was created will be freed.

* Since the struct ap_matrix_mdev now gets freed while the guest is is still running, we need to ensure that the pointer to the function that handles interception of the PQAP instruction executed on the guest is not accessed while it is being set to NULL. To prevent this, a r/w lock is introduced that protects the function pointer.

Tony Krowiak (2):
s390/vfio-ap: clean up mdev resources when remove callback invoked s390/vfio-ap: r/w lock for PQAP interception handler function pointer

 arch/s390/include/asm/kvm_host.h      |  6 +++---
 arch/s390/kvm/kvm-s390.c              |  1 +
 arch/s390/kvm/priv.c                  |  6 +++---
 drivers/s390/crypto/vfio_ap_ops.c     | 31 ++++++++++++++-------------
 drivers/s390/crypto/vfio_ap_private.h |  2 +-
 5 files changed, 24 insertions(+), 22 deletions(-)
Signature · 1 line
2.30.2
author maintainer
#3 lore raw

PATCH v5 2/2 s390/vfio-ap: r/w lock for PQAP interception handler function pointer

The function pointer to the interception handler for the PQAP instruction can get changed during the interception process. Let's add a semaphore to struct kvm_s390_crypto to control read/write access to the function pointer contained therein.

The semaphore must be locked for write access by the vfio_ap device driver when notified that the KVM pointer has been set or cleared. It must be locked for read access by the interception framework when the PQAP instruction is intercepted.

---
 arch/s390/include/asm/kvm_host.h      |  6 +++---
 arch/s390/kvm/kvm-s390.c              |  1 +
 arch/s390/kvm/priv.c                  |  6 +++---
 drivers/s390/crypto/vfio_ap_ops.c     | 21 ++++++++++++++++-----
 drivers/s390/crypto/vfio_ap_private.h |  2 +-
 5 files changed, 24 insertions(+), 12 deletions(-)
arch/s390/include/asm/kvm_host.h +3 -3
index 8925f3969478..58edaa3f9602 100644
@@ -803,14 +803,14 @@ struct kvm_s390_cpu_model {
 	unsigned short ibc;
 };
 
-struct kvm_s390_module_hook {
+struct kvm_s390_crypto_hook {
 	int (*hook)(struct kvm_vcpu *vcpu);
-	struct module *owner;
 };
 
 struct kvm_s390_crypto {
 	struct kvm_s390_crypto_cb *crycb;
-	struct kvm_s390_module_hook *pqap_hook;
+	struct rw_semaphore pqap_hook_rwsem;
+	struct kvm_s390_crypto_hook *pqap_hook;
 	__u32 crycbd;
 	__u8 aes_kw;
 	__u8 dea_kw;
arch/s390/kvm/kvm-s390.c +1 -0
index 1296fc10f80c..418d910df569 100644
@@ -2606,6 +2606,7 @@ static void kvm_s390_crypto_init(struct kvm *kvm)
 {
 	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
 	kvm_s390_set_crycb_format(kvm);
+	init_rwsem(&kvm->arch.crypto.pqap_hook_rwsem);
 
 	if (!test_kvm_facility(kvm, 76))
 		return;
arch/s390/kvm/priv.c +3 -3
index 9928f785c677..bbbd84ffe239 100644
@@ -657,15 +657,15 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
 	 * Verify that the hook callback is registered, lock the owner
 	 * and call the hook.
 	 */
+	down_read(&vcpu->kvm->arch.crypto.pqap_hook_rwsem);
 	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);
+		up_read(&vcpu->kvm->arch.crypto.pqap_hook_rwsem);
 		return ret;
 	}
+	up_read(&vcpu->kvm->arch.crypto.pqap_hook_rwsem);
 	/*
 	 * A vfio_driver must register a hook.
 	 * No hook means no driver to enable the SIE CRYCB and no queues.
drivers/s390/crypto/vfio_ap_ops.c +16 -5
index 122c85c22469..d8abe5a11e49 100644
@@ -353,7 +353,6 @@ static int vfio_ap_mdev_create(struct mdev_device *mdev)
 	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;
 	mutex_lock(&matrix_dev->lock);
 	list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
 	mutex_unlock(&matrix_dev->lock);
@@ -1115,15 +1114,20 @@ static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
 		}
 
 		kvm_get_kvm(kvm);
+		matrix_mdev->kvm = kvm;
 		matrix_mdev->kvm_busy = true;
 		mutex_unlock(&matrix_dev->lock);
+
+		down_write(&matrix_mdev->kvm->arch.crypto.pqap_hook_rwsem);
+		kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
+		up_write(&matrix_mdev->kvm->arch.crypto.pqap_hook_rwsem);
+
 		kvm_arch_crypto_set_masks(kvm,
 					  matrix_mdev->matrix.apm,
 					  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);
 	}
@@ -1189,10 +1193,17 @@ static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
 	if (matrix_mdev->kvm) {
 		matrix_mdev->kvm_busy = true;
 		mutex_unlock(&matrix_dev->lock);
-		kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
+
+		if (matrix_mdev->kvm->arch.crypto.crycbd) {
+			down_write(&matrix_mdev->kvm->arch.crypto.pqap_hook_rwsem);
+			matrix_mdev->kvm->arch.crypto.pqap_hook = NULL;
+			up_write(&matrix_mdev->kvm->arch.crypto.pqap_hook_rwsem);
+
+			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;
drivers/s390/crypto/vfio_ap_private.h +1 -1
index f82a6396acae..5d4fe6efbc73 100644
@@ -86,7 +86,7 @@ struct ap_matrix_mdev {
 	bool kvm_busy;
 	wait_queue_head_t wait_for_kvm;
 	struct kvm *kvm;
-	struct kvm_s390_module_hook pqap_hook;
+	struct kvm_s390_crypto_hook pqap_hook;
 	struct mdev_device *mdev;
 };
 
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH v5 1/2 s390/vfio-ap: clean up mdev resources when remove callback invoked

In reply to Tony Krowiak

The mdev remove callback for the vfio_ap device driver bails out with -EBUSY if the mdev is in use by a KVM guest (i.e., the KVM pointer in the struct ap_matrix_mdev is not NULL). The intended purpose was to prevent the mdev from being removed while in use. There are two problems with this scenario:

1. Returning a non-zero return code from the remove callback does not prevent the removal of the mdev.

2. The KVM pointer in the struct ap_matrix_mdev will always be NULL because the remove callback will not get invoked until the mdev fd is closed. When the mdev fd is closed, the mdev release callback is invoked and clears the KVM pointer from the struct ap_matrix_mdev.

Let's go ahead and remove the check for KVM in the remove callback and allow the cleanup of mdev resources to proceed.

---
 drivers/s390/crypto/vfio_ap_ops.c | 10 ----------
 1 file changed, 10 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +0 -10
index b2c7e10dfdcd..122c85c22469 100644
@@ -366,16 +366,6 @@ 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);
 	list_del(&matrix_mdev->node);
 	kfree(matrix_mdev);
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH v5 1/2] s390/vfio-ap: clean up mdev resources when remove callback invoked

Quoted 14 lines · Tony Krowiak

The mdev remove callback for the vfio_ap device driver bails out with -EBUSY if the mdev is in use by a KVM guest (i.e., the KVM pointer in the struct ap_matrix_mdev is not NULL). The intended purpose was to prevent the mdev from being removed while in use. There are two problems with this scenario:

1. Returning a non-zero return code from the remove callback does not prevent the removal of the mdev.

2. The KVM pointer in the struct ap_matrix_mdev will always be NULL because the remove callback will not get invoked until the mdev fd is closed. When the mdev fd is closed, the mdev release callback is invoked and clears the KVM pointer from the struct ap_matrix_mdev.

Quoted 5 lines

Let's go ahead and remove the check for KVM in the remove callback and allow the cleanup of mdev resources to proceed.

queued. Do we need cc stable?

Quoted 26 lines
---
  drivers/s390/crypto/vfio_ap_ops.c | 10 ----------
  1 file changed, 10 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +0 -10
index b2c7e10dfdcd..122c85c22469 100644
@@ -366,16 +366,6 @@ 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);
  	list_del(&matrix_mdev->node);
  	kfree(matrix_mdev);
#4 lore raw

Re: [PATCH v5 2/2] s390/vfio-ap: r/w lock for PQAP interception handler function pointer

In reply to Tony Krowiak
Quoted 10 lines · Tony Krowiak

The function pointer to the interception handler for the PQAP instruction can get changed during the interception process. Let's add a semaphore to struct kvm_s390_crypto to control read/write access to the function pointer contained therein.

The semaphore must be locked for write access by the vfio_ap device driver when notified that the KVM pointer has been set or cleared. It must be locked for read access by the interception framework when the PQAP instruction is intercepted.

The locking looks so much nicer now. One (optional) thing.

Quoted 15 lines
---
  arch/s390/include/asm/kvm_host.h      |  6 +++---
  arch/s390/kvm/kvm-s390.c              |  1 +
  arch/s390/kvm/priv.c                  |  6 +++---
  drivers/s390/crypto/vfio_ap_ops.c     | 21 ++++++++++++++++-----
  drivers/s390/crypto/vfio_ap_private.h |  2 +-
  5 files changed, 24 insertions(+), 12 deletions(-)
arch/s390/include/asm/kvm_host.h +0 -0
index 8925f3969478..58edaa3f9602 100644

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

Quoted 8 lines
arch/s390/include/asm/kvm_host.h +1 -2
@@ -803,14 +803,14 @@ struct kvm_s390_cpu_model {
  	unsigned short ibc;
  };
 
-struct kvm_s390_module_hook {
+struct kvm_s390_crypto_hook {
  	int (*hook)(struct kvm_vcpu *vcpu);
-	struct module *owner;
  };

Can we actually get rid of the structure and use
int (*pqap_hook)(struct kvm_vcpu *vcpu);

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

Quoted 6 lines
arch/s390/include/asm/kvm_host.h +2 -1
  struct kvm_s390_crypto {
  	struct kvm_s390_crypto_cb *crycb;
-	struct kvm_s390_module_hook *pqap_hook;
+	struct rw_semaphore pqap_hook_rwsem;
+	struct kvm_s390_crypto_hook *pqap_hook;

here and below

Quoted 20 lines
arch/s390/include/asm/kvm_host.h +0 -0
  	__u32 crycbd;
  	__u8 aes_kw;
  	__u8 dea_kw;
arch/s390/kvm/kvm-s390.c +1 -0
index 1296fc10f80c..418d910df569 100644
@@ -2606,6 +2606,7 @@ static void kvm_s390_crypto_init(struct kvm *kvm)
  {
  	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
  	kvm_s390_set_crycb_format(kvm);
+	init_rwsem(&kvm->arch.crypto.pqap_hook_rwsem);
 
  	if (!test_kvm_facility(kvm, 76))
  		return;
arch/s390/kvm/priv.c +0 -0
index 9928f785c677..bbbd84ffe239 100644

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

Quoted 8 lines
arch/s390/kvm/priv.c +1 -2
@@ -657,15 +657,15 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
  	 * Verify that the hook callback is registered, lock the owner
  	 * and call the hook.
  	 */
+	down_read(&vcpu->kvm->arch.crypto.pqap_hook_rwsem);
  	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);

ret = vcpu->kvm->arch.crypto.pqap_hook(vcpu);

Quoted 70 lines
arch/s390/kvm/priv.c +2 -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->kvm->arch.crypto.pqap_hook_rwsem);
  		return ret;
  	}
+	up_read(&vcpu->kvm->arch.crypto.pqap_hook_rwsem);
  	/*
  	 * A vfio_driver must register a hook.
  	 * No hook means no driver to enable the SIE CRYCB and no queues.
drivers/s390/crypto/vfio_ap_ops.c +16 -5
index 122c85c22469..d8abe5a11e49 100644
@@ -353,7 +353,6 @@ static int vfio_ap_mdev_create(struct mdev_device *mdev)
  	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;
  	mutex_lock(&matrix_dev->lock);
  	list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
  	mutex_unlock(&matrix_dev->lock);
@@ -1115,15 +1114,20 @@ static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
  		}
 
  		kvm_get_kvm(kvm);
+		matrix_mdev->kvm = kvm;
  		matrix_mdev->kvm_busy = true;
  		mutex_unlock(&matrix_dev->lock);
+
+		down_write(&matrix_mdev->kvm->arch.crypto.pqap_hook_rwsem);
+		kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
+		up_write(&matrix_mdev->kvm->arch.crypto.pqap_hook_rwsem);
+
  		kvm_arch_crypto_set_masks(kvm,
  					  matrix_mdev->matrix.apm,
  					  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);
  	}
@@ -1189,10 +1193,17 @@ static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
  	if (matrix_mdev->kvm) {
  		matrix_mdev->kvm_busy = true;
  		mutex_unlock(&matrix_dev->lock);
-		kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
+
+		if (matrix_mdev->kvm->arch.crypto.crycbd) {
+			down_write(&matrix_mdev->kvm->arch.crypto.pqap_hook_rwsem);
+			matrix_mdev->kvm->arch.crypto.pqap_hook = NULL;
+			up_write(&matrix_mdev->kvm->arch.crypto.pqap_hook_rwsem);
+
+			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;
drivers/s390/crypto/vfio_ap_private.h +0 -0
index f82a6396acae..5d4fe6efbc73 100644

drivers/s390/crypto/vfio_ap_private.h struct ap_matrix_mdev { · line 89

Quoted 4 lines
drivers/s390/crypto/vfio_ap_private.h +0 -1
@@ -86,7 +86,7 @@ struct ap_matrix_mdev {
  	bool kvm_busy;
  	wait_queue_head_t wait_for_kvm;
  	struct kvm *kvm;
-	struct kvm_s390_module_hook pqap_hook;

and here?

Quoted 5 lines
drivers/s390/crypto/vfio_ap_private.h +1 -0
+	struct kvm_s390_crypto_hook pqap_hook;
  	struct mdev_device *mdev;
  };
 
author maintainer
#5 lore raw

Re: [PATCH v5 2/2] s390/vfio-ap: r/w lock for PQAP interception handler function pointer

Quoted 8 lines · Christian Borntraeger
Quoted 5 lines · Tony Krowiak

The function pointer to the interception handler for the PQAP instruction can get changed during the interception process. Let's add a semaphore to struct kvm_s390_crypto to control read/write access to the function pointer contained therein.

Quoted 7 lines
Quoted 5 lines

The semaphore must be locked for write access by the vfio_ap device driver when notified that the KVM pointer has been set or cleared. It must be locked for read access by the interception framework when the PQAP instruction is intercepted.

The locking looks so much nicer now. One (optional) thing.

Unfortunately, we aren't done yet.

Quoted 11 lines
Quoted 11 lines

---
  arch/s390/include/asm/kvm_host.h      |  6 +++--- arch/s390/kvm/kvm-s390.c              |  1 + arch/s390/kvm/priv.c                  |  6 +++--- drivers/s390/crypto/vfio_ap_ops.c     | 21 ++++++++++++++++----- drivers/s390/crypto/vfio_ap_private.h |  2 +-

  5 files changed, 24 insertions(+), 12 deletions(-)
Quoted 15 lines

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

Quoted 12 lines
arch/s390/include/asm/kvm_host.h +1 -1
index 8925f3969478..58edaa3f9602 100644
@@ -803,14 +803,14 @@ struct kvm_s390_cpu_model {
       unsigned short ibc;
   };
   -struct kvm_s390_module_hook {
+struct kvm_s390_crypto_hook {
       int (*hook)(struct kvm_vcpu *vcpu);
-    struct module *owner;
   };

Can we actually get rid of the structure and use
       int (*pqap_hook)(struct kvm_vcpu *vcpu);

Yes, we can.

Quoted 7 lines

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

Quoted 5 lines
arch/s390/include/asm/kvm_host.h +2 -1
     struct kvm_s390_crypto {
       struct kvm_s390_crypto_cb *crycb;
-    struct kvm_s390_module_hook *pqap_hook;
+    struct rw_semaphore pqap_hook_rwsem;
+    struct kvm_s390_crypto_hook *pqap_hook;

here and below

Yes

Quoted 100 lines
Quoted 20 lines
arch/s390/include/asm/kvm_host.h +0 -0
       __u32 crycbd;
       __u8 aes_kw;
       __u8 dea_kw;
arch/s390/kvm/kvm-s390.c +1 -0
index 1296fc10f80c..418d910df569 100644
@@ -2606,6 +2606,7 @@ static void kvm_s390_crypto_init(struct kvm *kvm)
   {
       kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
       kvm_s390_set_crycb_format(kvm);
+    init_rwsem(&kvm->arch.crypto.pqap_hook_rwsem);
         if (!test_kvm_facility(kvm, 76))
           return;
arch/s390/kvm/priv.c +0 -0
index 9928f785c677..bbbd84ffe239 100644

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

Quoted 8 lines
arch/s390/kvm/priv.c +1 -2
@@ -657,15 +657,15 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
        * Verify that the hook callback is registered, lock the owner
        * and call the hook.
        */
+ down_read(&vcpu->kvm->arch.crypto.pqap_hook_rwsem);
       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);

           ret = vcpu->kvm->arch.crypto.pqap_hook(vcpu);

Quoted 70 lines
arch/s390/kvm/priv.c +2 -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->kvm->arch.crypto.pqap_hook_rwsem);
           return ret;
       }
+    up_read(&vcpu->kvm->arch.crypto.pqap_hook_rwsem);
       /*
        * A vfio_driver must register a hook.
        * No hook means no driver to enable the SIE CRYCB and no queues.
drivers/s390/crypto/vfio_ap_ops.c +0 -1
index 122c85c22469..d8abe5a11e49 100644
@@ -353,7 +353,6 @@ static int vfio_ap_mdev_create(struct mdev_device 
 *mdev)
       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;
       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 +16 -4
@@ -1115,15 +1114,20 @@ static int vfio_ap_mdev_set_kvm(struct 
 ap_matrix_mdev *matrix_mdev,
           }
             kvm_get_kvm(kvm);
+        matrix_mdev->kvm = kvm;
           matrix_mdev->kvm_busy = true;
           mutex_unlock(&matrix_dev->lock);
+
+ down_write(&matrix_mdev->kvm->arch.crypto.pqap_hook_rwsem);
+        kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
+ up_write(&matrix_mdev->kvm->arch.crypto.pqap_hook_rwsem);
+
           kvm_arch_crypto_set_masks(kvm,
                         matrix_mdev->matrix.apm,
                         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);
       }
@@ -1189,10 +1193,17 @@ static void vfio_ap_mdev_unset_kvm(struct 
 ap_matrix_mdev *matrix_mdev)
       if (matrix_mdev->kvm) {
           matrix_mdev->kvm_busy = true;
           mutex_unlock(&matrix_dev->lock);
-        kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
+
+        if (matrix_mdev->kvm->arch.crypto.crycbd) {
+ down_write(&matrix_mdev->kvm->arch.crypto.pqap_hook_rwsem);
+            matrix_mdev->kvm->arch.crypto.pqap_hook = NULL;
+ up_write(&matrix_mdev->kvm->arch.crypto.pqap_hook_rwsem);
+
+            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;

drivers/s390/crypto/vfio_ap_private.h +0 -1
index f82a6396acae..5d4fe6efbc73 100644
--- a/drivers/s390/crypto/vfio_ap_private.h
Quoted 8 lines

drivers/s390/crypto/vfio_ap_private.h struct ap_matrix_mdev { · line 89

Quoted 6 lines

+++ b/drivers/s390/crypto/vfio_ap_private.h

drivers/s390/crypto/vfio_ap_private.h +0 -1
@@ -86,7 +86,7 @@ struct ap_matrix_mdev {
       bool kvm_busy;
       wait_queue_head_t wait_for_kvm;
       struct kvm *kvm;
-    struct kvm_s390_module_hook pqap_hook;

and here?

Yep

Quoted 5 lines
Quoted 5 lines
drivers/s390/crypto/vfio_ap_private.h +1 -0
+    struct kvm_s390_crypto_hook pqap_hook;
       struct mdev_device *mdev;
   };