s390/vfio-ap: Clean up vfio_ap resources when KVM pointer invalidated
Subsystems (4)
Tony Krowiak
maintainerPATCH 1/1 s390/vfio-ap: Clean up vfio_ap resources when KVM pointer invalidated
The vfio_ap device driver registers a group notifier with VFIO when the file descriptor for a VFIO mediated device for a KVM guest is opened to receive notification that the KVM pointer is set (VFIO_GROUP_NOTIFY_SET_KVM event). When the KVM pointer is set, the vfio_ap driver stashes the pointer and calls the kvm_get_kvm() function to increment its reference counter. When the notifier is called to make notification that the KVM pointer has been set to NULL, the driver should clean up any resources associated with the KVM pointer and decrement its reference counter. The current implementation does not take care of this clean up.
--- drivers/s390/crypto/vfio_ap_ops.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +13 -8
@@ -1083,6 +1083,17 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,return NOTIFY_DONE ; } +static void vfio_ap_mdev_put_kvm (struct ap_matrix_mdev *matrix_mdev) +{ +if (matrix_mdev->kvm) { +kvm_arch_crypto_clear_masks (matrix_mdev->kvm); + matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ; +vfio_ap_mdev_reset_queues (matrix_mdev->mdev); +kvm_put_kvm (matrix_mdev->kvm); + matrix_mdev->kvm =NULL ; + } +} +static int vfio_ap_mdev_group_notifier (struct notifier_block *nb,unsigned long action,void *data) {
@@ -1095,7 +1106,7 @@ static int vfio_ap_mdev_group_notifier(struct notifier_block *nb, matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);if (!data) { - matrix_mdev->kvm =NULL ; +vfio_ap_mdev_put_kvm (matrix_mdev);return NOTIFY_OK ; }
@@ -1222,13 +1233,7 @@ static void vfio_ap_mdev_release(struct mdev_device *mdev)struct ap_matrix_mdev *matrix_mdev =mdev_get_drvdata (mdev);mutex_lock (&matrix_dev->lock); -if (matrix_mdev->kvm) { -kvm_arch_crypto_clear_masks (matrix_mdev->kvm); - matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ; -vfio_ap_mdev_reset_queues (mdev); -kvm_put_kvm (matrix_mdev->kvm); - matrix_mdev->kvm =NULL ; - } +vfio_ap_mdev_put_kvm (matrix_mdev);mutex_unlock (&matrix_dev->lock); vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
Signature · 1 line
2.21.1
Quoted 38 lines · On Wed, 2 Dec 2020 18:41:01 -0500 Tony Krowiak <[email protected]> wrote:
The
vfio_apdevice driver registers a group notifier with VFIO when the file descriptor for a VFIO mediated device for a KVM guest is opened to receive notification that the KVM pointer is set (VFIO_GROUP_NOTIFY_SET_KVMevent). When the KVM pointer is set, thevfio_apdriver stashes the pointer and calls thekvm_get_kvm()function to increment its reference counter. When the notifier is called to make notification that the KVM pointer has been set to NULL, the driver should clean up any resources associated with the KVM pointer and decrement its reference counter. The current implementation does not take care of this clean up.Signed-off-by Tony Krowiak <[email protected]>--- drivers/s390/crypto/vfio_ap_ops.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)drivers/s390/crypto/vfio_ap_ops.c +11
-0@@ -1083,6 +1083,17 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,return NOTIFY_DONE ; } +static void vfio_ap_mdev_put_kvm (struct ap_matrix_mdev *matrix_mdev) +{ +if (matrix_mdev->kvm) { +kvm_arch_crypto_clear_masks (matrix_mdev->kvm); + matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ; +vfio_ap_mdev_reset_queues (matrix_mdev->mdev); +kvm_put_kvm (matrix_mdev->kvm); + matrix_mdev->kvm =NULL ; + } +} +static int vfio_ap_mdev_group_notifier (struct notifier_block *nb,unsigned long action,void *data) {
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_group_notifier(struct notifier_block *nb, · line 1109
Quoted 5 lines
drivers/s390/crypto/vfio_ap_ops.c +1
-1@@ -1095,7 +1106,7 @@ static int vfio_ap_mdev_group_notifier(struct notifier_block *nb, matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);if (!data) { - matrix_mdev->kvm =NULL ; +vfio_ap_mdev_put_kvm (matrix_mdev);
Hm. I'm wondering whether you need to hold the maxtrix_dev lock here as well?
Quoted 18 lines
drivers/s390/crypto/vfio_ap_ops.c +1
-7return NOTIFY_OK ; }@@ -1222,13 +1233,7 @@ static void vfio_ap_mdev_release(struct mdev_device *mdev)struct ap_matrix_mdev *matrix_mdev =mdev_get_drvdata (mdev);mutex_lock (&matrix_dev->lock); -if (matrix_mdev->kvm) { -kvm_arch_crypto_clear_masks (matrix_mdev->kvm); - matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ; -vfio_ap_mdev_reset_queues (mdev); -kvm_put_kvm (matrix_mdev->kvm); - matrix_mdev->kvm =NULL ; - } +vfio_ap_mdev_put_kvm (matrix_mdev);mutex_unlock (&matrix_dev->lock); vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
Halil Pasic
maintainerQuoted 9 lines · On Thu, 3 Dec 2020 11:19:07 +0100 Cornelia Huck <[email protected]> wrote:
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_group_notifier(struct notifier_block *nb, · line 1109
Quoted 6 lines
drivers/s390/crypto/vfio_ap_ops.c +1
-1@@ -1095,7 +1106,7 @@ static int vfio_ap_mdev_group_notifier(struct notifier_block *nb, matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);if (!data) { - matrix_mdev->kvm =NULL ; +vfio_ap_mdev_put_kvm (matrix_mdev);Hm. I'm wondering whether you need to hold the
maxtrix_devlock here as well?
In v12 we eventually did come along and patch "s390/vfio-ap: allow hot plug/unplug of AP resources using mdev device" made this a part of a critical section protected by the matrix_dev->lock.
IMHO the cleanup should definitely happen with the matrix_dev->lock held.
Regards,
Halil
Tony Krowiak
maintainerQuoted 11 lines · Halil Pasic
Quoted 8 lines · On Thu, 3 Dec 2020 11:19:07 +0100 Cornelia Huck <[email protected]> wrote:
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_group_notifier(struct notifier_block *nb, · line 1109
Quoted 6 lines
drivers/s390/crypto/vfio_ap_ops.c +1
-1@@ -1095,7 +1106,7 @@ static int vfio_ap_mdev_group_notifier(struct notifier_block *nb, matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);if (!data) { - matrix_mdev->kvm =NULL ; +vfio_ap_mdev_put_kvm (matrix_mdev);Hm. I'm wondering whether you need to hold the
maxtrix_devlock here as well?
Quoted 5 lines
In v12 we eventually did come along and patch "s390/vfio-ap: allow hot plug/unplug of AP resources using mdev device" made this a part of a critical section protected by the
matrix_dev->lock.IMHO the cleanup should definitely happen with the
matrix_dev->lockheld.
Agreed!
Quoted 3 lines
Regards,
Halil
Halil Pasic
maintainerQuoted 11 lines · On Wed, 2 Dec 2020 18:41:01 -0500 Tony Krowiak <[email protected]> wrote:
The
vfio_apdevice driver registers a group notifier with VFIO when the file descriptor for a VFIO mediated device for a KVM guest is opened to receive notification that the KVM pointer is set (VFIO_GROUP_NOTIFY_SET_KVMevent). When the KVM pointer is set, thevfio_apdriver stashes the pointer and calls thekvm_get_kvm()function to increment its reference counter. When the notifier is called to make notification that the KVM pointer has been set to NULL, the driver should clean up any resources associated with the KVM pointer and decrement its reference counter. The current implementation does not take care of this clean up.Signed-off-by Tony Krowiak <[email protected]>
Do we need a Fixes tag? Do we need this backported? In my opinion this is necessary since the interrupt patches.
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, · line 1086
Quoted 13 lines
--- drivers/s390/crypto/vfio_ap_ops.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)drivers/s390/crypto/vfio_ap_ops.c +1
-0@@ -1083,6 +1083,17 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,return NOTIFY_DONE ; } +static void vfio_ap_mdev_put_kvm (struct ap_matrix_mdev *matrix_mdev)
I don't like the name. The function does more that put_kvm. Maybe something like _disconnect_kvm()?
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, · line 1090
Quoted 4 lines
drivers/s390/crypto/vfio_ap_ops.c +4
-0+{ +if (matrix_mdev->kvm) { + (matrix_mdev->kvm); + matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ;
Is a plain assignment to arch.crypto.pqap_hook apropriate, or do we need to take more care?
For instance kvm_arch_crypto_set_masks() takes kvm->lock before poking kvm->arch.crypto.crycb.
Quoted 10 lines
drivers/s390/crypto/vfio_ap_ops.c +6
-0+vfio_ap_mdev_reset_queues (matrix_mdev->mdev); +kvm_put_kvm (matrix_mdev->kvm); + matrix_mdev->kvm =NULL ; + } +} +static int vfio_ap_mdev_group_notifier (struct notifier_block *nb,unsigned long action,void *data) {
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_group_notifier(struct notifier_block *nb, · line 1109
Quoted 5 lines
drivers/s390/crypto/vfio_ap_ops.c +1
-1@@ -1095,7 +1106,7 @@ static int vfio_ap_mdev_group_notifier(struct notifier_block *nb, matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);if (!data) { - matrix_mdev->kvm =NULL ; +vfio_ap_mdev_put_kvm (matrix_mdev);
The lock question was already raised.
What are the exact circumstances under which this branch can be taken?
Quoted 18 lines
drivers/s390/crypto/vfio_ap_ops.c +1
-7return NOTIFY_OK ; }@@ -1222,13 +1233,7 @@ static void vfio_ap_mdev_release(struct mdev_device *mdev)struct ap_matrix_mdev *matrix_mdev =mdev_get_drvdata (mdev);mutex_lock (&matrix_dev->lock); -if (matrix_mdev->kvm) { -kvm_arch_crypto_clear_masks (matrix_mdev->kvm); - matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ; -vfio_ap_mdev_reset_queues (mdev); -kvm_put_kvm (matrix_mdev->kvm); - matrix_mdev->kvm =NULL ; - } +vfio_ap_mdev_put_kvm (matrix_mdev);mutex_unlock (&matrix_dev->lock); vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
Tony Krowiak
maintainerQuoted 40 lines · Halil Pasic
Quoted 11 lines · On Wed, 2 Dec 2020 18:41:01 -0500 Tony Krowiak <[email protected]> wrote:
The
vfio_apdevice driver registers a group notifier with VFIO when the file descriptor for a VFIO mediated device for a KVM guest is opened to receive notification that the KVM pointer is set (VFIO_GROUP_NOTIFY_SET_KVMevent). When the KVM pointer is set, thevfio_apdriver stashes the pointer and calls thekvm_get_kvm()function to increment its reference counter. When the notifier is called to make notification that the KVM pointer has been set to NULL, the driver should clean up any resources associated with the KVM pointer and decrement its reference counter. The current implementation does not take care of this clean up.Signed-off-by Tony Krowiak <[email protected]>Do we need a Fixes tag? Do we need this backported? In my opinion this is necessary since the interrupt patches.
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, · line 1086
Quoted 13 lines
--- drivers/s390/crypto/vfio_ap_ops.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)drivers/s390/crypto/vfio_ap_ops.c +1
-0@@ -1083,6 +1083,17 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,return NOTIFY_DONE ; } +static void vfio_ap_mdev_put_kvm (struct ap_matrix_mdev *matrix_mdev)I don't like the name. The function does more that
put_kvm. Maybe something like_disconnect_kvm()?drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, · line 1090
Quoted 4 lines
drivers/s390/crypto/vfio_ap_ops.c +4
-0+{ +if (matrix_mdev->kvm) { + (matrix_mdev->kvm); + matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ;Is a plain assignment to arch.crypto.pqap_hook apropriate, or do we need to take more care?
Quoted 2 lines
For instance
kvm_arch_crypto_set_masks()takeskvm->lockbefore pokingkvm->arch.crypto.crycb.
I do not think so. The CRYCB is used by KVM to provide crypto resources to the guest so it makes sense to protect it from changes to it while passing the AP devices through to the guest. The hook is used only when an AQIC executed on the guest is intercepted by KVM. If the notifier
is being invoked to notify vfio_ap that KVM has been set to NULL, this means the guest is gone in which case there will be no AP instructions to intercept.
Quoted 13 lines
Quoted 13 lines
drivers/s390/crypto/vfio_ap_ops.c +6
-0+vfio_ap_mdev_reset_queues (matrix_mdev->mdev); +kvm_put_kvm (matrix_mdev->kvm); + matrix_mdev->kvm =NULL ; + } +} +static int vfio_ap_mdev_group_notifier (struct notifier_block *nb,unsigned long action,void *data) {@@ -1095,7 +1106,7 @@ static int vfio_ap_mdev_group_notifier(struct notifier_block *nb, matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);
Quoted 6 lines
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_group_notifier(struct notifier_block *nb, · line 1109
Quoted 3 lines
drivers/s390/crypto/vfio_ap_ops.c +1
-1if (!data) { - matrix_mdev->kvm =NULL ; +vfio_ap_mdev_put_kvm (matrix_mdev);The lock question was already raised.
What are the exact circumstances under which this branch can be taken?
Under normal circumstances (i.e., the mdev fd is closed before the guest terminates), this notifier is not be called because the release callback (invoked when the mdev fd is closed) unregisters the notifier. This fix is primarily to ensure that proper cleanup is done should the notifier get called; for example, if userspace does not close the mdev fd before shutting the guest down.
Quoted 19 lines
Quoted 19 lines
drivers/s390/crypto/vfio_ap_ops.c +1
-7return NOTIFY_OK ; }@@ -1222,13 +1233,7 @@ static void vfio_ap_mdev_release(struct mdev_device *mdev)struct ap_matrix_mdev *matrix_mdev =mdev_get_drvdata (mdev);mutex_lock (&matrix_dev->lock); -if (matrix_mdev->kvm) { -kvm_arch_crypto_clear_masks (matrix_mdev->kvm); - matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ; -vfio_ap_mdev_reset_queues (mdev); -kvm_put_kvm (matrix_mdev->kvm); - matrix_mdev->kvm =NULL ; - } +vfio_ap_mdev_put_kvm (matrix_mdev);mutex_unlock (&matrix_dev->lock); vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
Halil Pasic
maintainerQuoted 10 lines · On Fri, 4 Dec 2020 09:43:59 -0500 Tony Krowiak <[email protected]> wrote:
Quoted 9 lines
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, · line 1090
Quoted 4 lines
drivers/s390/crypto/vfio_ap_ops.c +4
-0+{ +if (matrix_mdev->kvm) { + (matrix_mdev->kvm); + matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ;Is a plain assignment to arch.crypto.pqap_hook apropriate, or do we need to take more care?
For instance
kvm_arch_crypto_set_masks()takeskvm->lockbefore pokingkvm->arch.crypto.crycb.
Quoted 8 lines
I do not think so. The CRYCB is used by KVM to provide crypto resources to the guest so it makes sense to protect it from changes to it while passing
the AP devices through to the guest. The hook is used only when an AQIC executed on the guest is intercepted by KVM. If the notifier
is being invoked to notifyvfio_apthat KVM has been set to NULL, this means the guest is gone in which case there will be no AP instructions to intercept.
If the update to pqap_hook isn't observed as atomic we still have a problem. With torn writes or reads we would try to use a corrupt function pointer. While the compiler probably ain't likely to generate silly code for the above assignment (multiple write instructions less then quadword wide), I know of nothing that would prohibit the compiler to do so.
I'm not certain about the scope of the kvm->lock (if it's supposed to protect the whole sub-tree of objects). Maybe Janosch can help us out. @Janosch: what do you think?
Regards,
Halil
Tony Krowiak
maintainerQuoted 20 lines · Halil Pasic
Quoted 9 lines · On Fri, 4 Dec 2020 09:43:59 -0500 Tony Krowiak <[email protected]> wrote:
Quoted 9 lines
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, · line 1090
Quoted 4 lines
drivers/s390/crypto/vfio_ap_ops.c +4
-0+{ +if (matrix_mdev->kvm) { + (matrix_mdev->kvm); + matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ;Is a plain assignment to arch.crypto.pqap_hook apropriate, or do we need to take more care?
For instance
kvm_arch_crypto_set_masks()takeskvm->lockbefore pokingkvm->arch.crypto.crycb.Quoted 8 lines
I do not think so. The CRYCB is used by KVM to provide crypto resources to the guest so it makes sense to protect it from changes to it while passing
the AP devices through to the guest. The hook is used only when an AQIC executed on the guest is intercepted by KVM. If the notifier
is being invoked to notifyvfio_apthat KVM has been set to NULL, this means the guest is gone in which case there will be no AP instructions to intercept.
Quoted 6 lines
If the update to
pqap_hookisn't observed as atomic we still have a problem. With torn writes or reads we would try to use a corrupt function pointer. While the compiler probably ain't likely to generate silly code for the above assignment (multiple write instructions less then quadword wide), I know of nothing that would prohibit the compiler to do so.
I see that in the handle_pqap() function in arch/s390/kvm/priv.c that gets called when the AQIC instruction is intercepted, the pqap_hook is protected by locking the owner of the 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);
Maybe that is what we should do when the kvm->arch.crypto.pqap_hook is set to NULL?
Quoted 7 lines
I'm not certain about the scope of the
kvm->lock(if it's supposed to protect the whole sub-tree of objects). Maybe Janosch can help us out. @Janosch: what do you think?Regards,
Halil
Halil Pasic
maintainerQuoted 32 lines · On Fri, 4 Dec 2020 14:46:30 -0500 Tony Krowiak <[email protected]> wrote:
Quoted 20 lines · Halil Pasic
Quoted 9 lines · On Fri, 4 Dec 2020 09:43:59 -0500 Tony Krowiak <[email protected]> wrote:
Quoted 9 lines
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, · line 1090
Quoted 4 lines
drivers/s390/crypto/vfio_ap_ops.c +4
-0+{ +if (matrix_mdev->kvm) { + (matrix_mdev->kvm); + matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ;Is a plain assignment to arch.crypto.pqap_hook apropriate, or do we need to take more care?
For instance
kvm_arch_crypto_set_masks()takeskvm->lockbefore pokingkvm->arch.crypto.crycb.Quoted 8 lines
I do not think so. The CRYCB is used by KVM to provide crypto resources to the guest so it makes sense to protect it from changes to it while passing
the AP devices through to the guest. The hook is used only when an AQIC executed on the guest is intercepted by KVM. If the notifier
is being invoked to notifyvfio_apthat KVM has been set to NULL, this means the guest is gone in which case there will be no AP instructions to intercept.Quoted 6 lines
If the update to
pqap_hookisn't observed as atomic we still have a problem. With torn writes or reads we would try to use a corrupt function pointer. While the compiler probably ain't likely to generate silly code for the above assignment (multiple write instructions less then quadword wide), I know of nothing that would prohibit the compiler to do so.I see that in the
handle_pqap()function inarch/s390/kvm/priv.cthat gets called when the AQIC instruction is intercepted, thepqap_hookis protected by locking the owner of the hook:
Quoted 7 lines
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);Maybe that is what we should do when the
kvm->arch.crypto.pqap_hookis set to NULL?
To my best knowledge that ain't no locking but mere refcounting. The purpose of that is probably to prevent the owner module, and the code pointed to by the 'hook' function pointer from being unloaded while we are executing that very same code.
Why is that necessary, frankly I have no idea. We do tend to invalidate the callback before doing our module_put in vfio_ap_mdev_release(). Maybe the case you are handling right now is the reason (because the callback is invalidated in vfio_ap_mdev_release() only if !!kvm.
Regards,
Halil
Tony Krowiak
maintainerQuoted 20 lines · Halil Pasic
Quoted 9 lines · On Fri, 4 Dec 2020 09:43:59 -0500 Tony Krowiak <[email protected]> wrote:
Quoted 9 lines
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, · line 1090
Quoted 4 lines
drivers/s390/crypto/vfio_ap_ops.c +4
-0+{ +if (matrix_mdev->kvm) { + (matrix_mdev->kvm); + matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ;Is a plain assignment to arch.crypto.pqap_hook apropriate, or do we need to take more care?
For instance
kvm_arch_crypto_set_masks()takeskvm->lockbefore pokingkvm->arch.crypto.crycb.Quoted 8 lines
I do not think so. The CRYCB is used by KVM to provide crypto resources to the guest so it makes sense to protect it from changes to it while passing
the AP devices through to the guest. The hook is used only when an AQIC executed on the guest is intercepted by KVM. If the notifier
is being invoked to notifyvfio_apthat KVM has been set to NULL, this means the guest is gone in which case there will be no AP instructions to intercept.
Quoted 6 lines
If the update to
pqap_hookisn't observed as atomic we still have a problem. With torn writes or reads we would try to use a corrupt function pointer. While the compiler probably ain't likely to generate silly code for the above assignment (multiple write instructions less then quadword wide), I know of nothing that would prohibit the compiler to do so.
I'm sorry, but I still don't understand why you think this is a problem given what I stated above.
Quoted 7 lines
I'm not certain about the scope of the
kvm->lock(if it's supposed to protect the whole sub-tree of objects). Maybe Janosch can help us out. @Janosch: what do you think?Regards,
Halil
Halil Pasic
maintainerQuoted 28 lines · On Mon, 7 Dec 2020 13:50:36 -0500 Tony Krowiak <[email protected]> wrote:
Quoted 20 lines · Halil Pasic
Quoted 9 lines · On Fri, 4 Dec 2020 09:43:59 -0500 Tony Krowiak <[email protected]> wrote:
Quoted 9 lines
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, · line 1090
Quoted 4 lines
drivers/s390/crypto/vfio_ap_ops.c +4
-0+{ +if (matrix_mdev->kvm) { + (matrix_mdev->kvm); + matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ;Is a plain assignment to arch.crypto.pqap_hook apropriate, or do we need to take more care?
For instance
kvm_arch_crypto_set_masks()takeskvm->lockbefore pokingkvm->arch.crypto.crycb.Quoted 8 lines
I do not think so. The CRYCB is used by KVM to provide crypto resources to the guest so it makes sense to protect it from changes to it while passing
the AP devices through to the guest. The hook is used only when an AQIC executed on the guest is intercepted by KVM. If the notifier
is being invoked to notifyvfio_apthat KVM has been set to NULL, this means the guest is gone in which case there will be no AP instructions to intercept.Quoted 6 lines
If the update to
pqap_hookisn't observed as atomic we still have a problem. With torn writes or reads we would try to use a corrupt function pointer. While the compiler probably ain't likely to generate silly code for the above assignment (multiple write instructions less then quadword wide), I know of nothing that would prohibit the compiler to do so.
Quoted 2 lines
I'm sorry, but I still don't understand why you
tkvm_vfio_group_set_kvmhinkthis is a problem given what I stated above.
I assume you are specifically referring to 'the guest is gone in which case there will be no AP instructions to intercept'. I assume by 'guest is gone' you mean that the VM is being destroyed, and the vcpus are out of SIE. You are probably right for the invocation of kvm_vfio_group_set_kvm() in kvm_vfio_destroy(), but is that true for the invocation in the KVM_DEV_VFIO_GROUP_DEL case in kvm_vfio_set_group()? I.e. can't we get the notifier called when the qemu device is hot unplugged (modulo remove which unregisters the notifier and usually precludes the notifier being with NULL called at all)?
Regards,
Halil
A message that is not in the archive (sent off-list or not yet received): [email protected]
Halil Pasic
maintainerQuoted 46 lines · On Fri, 11 Dec 2020 15:52:55 -0500 Tony Krowiak <[email protected]> wrote:
Quoted 32 lines · Halil Pasic
Quoted 21 lines · On Mon, 7 Dec 2020 13:50:36 -0500 Tony Krowiak <[email protected]> wrote:
Quoted 20 lines · Halil Pasic
Quoted 17 lines · On Fri, 4 Dec 2020 09:43:59 -0500 Tony Krowiak <[email protected]> wrote:
Quoted 9 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+{ + if (matrix_mdev->kvm) { + (matrix_mdev->kvm); + matrix_mdev->kvm->arch.crypto.pqap_hook = NULL;Is a plain assignment to arch.crypto.pqap_hook apropriate, or do we need to take more care?
For instance
kvm_arch_crypto_set_masks()takeskvm->lockbefore pokingkvm->arch.crypto.crycb.I do not think so. The CRYCB is used by KVM to provide crypto resources to the guest so it makes sense to protect it from changes to it while passing
the AP devices through to the guest. The hook is used only when an AQIC executed on the guest is intercepted by KVM. If the notifier
is being invoked to notifyvfio_apthat KVM has been set to NULL, this means the guest is gone in which case there will be no AP instructions to intercept.Quoted 8 lines
Quoted 6 lines
If the update to
pqap_hookisn't observed as atomic we still have a problem. With torn writes or reads we would try to use a corrupt function pointer. While the compiler probably ain't likely to generate silly code for the above assignment (multiple write instructions less then quadword wide), I know of nothing that would prohibit the compiler to do so.I'm sorry, but I still don't understand why you
tkvm_vfio_group_set_kvmhinkthis is a problem given what I stated above.Quoted 10 lines
I assume you are specifically referring to 'the guest is gone in which case there will be no AP instructions to intercept'. I assume by 'guest is gone' you mean that the VM is being destroyed, and the vcpus are out of SIE. You are probably right for the invocation of
kvm_vfio_group_set_kvm()inkvm_vfio_destroy(), but is that true for the invocation in theKVM_DEV_VFIO_GROUP_DELcase inkvm_vfio_set_group()? I.e. can't we get the notifier called when the qemu device is hot unplugged (modulo remove which unregisters the notifier and usually precludes the notifier being with NULL called at all)?
Quoted 3 lines
I am assuming by your question that the qemu device you are talking about the '-device vfio-ap' specified on the qemu command line or attached vi||||||a qemu
device_add.
Yes.
Quoted 7 lines
When an mdev is hot
unplugged, thevfio_apdriver's release callback gets invoked when the mdev fd is closed. The
release callback unregisters the notifier, so it does not get called when the guest subsequently shuts down.
That is what I meant by 'modulo remove which unregisters the notifier and usually precludes the notifier being with NULL called at all', but unfortunately I mixed up remove and release.
AFAIU release should be called before the notifier gets invoked regardless of whether we have a hot-unplug of '-device vfio-ap' or a shutdown. The whole effort is about what happens if userspace does not adhered to this. If I apply the logic of your last response to the whole situation, then there is nothing to do (AFAIU).
The point I'm trying to make is, that in a case of the hot-unplug, the guest may survive the call to the notifier and also the vfio_mdev device it was associated to at some point. So your argument that 'the guest is gone in which case there will be no AP instructions to interpret' does not hold.
Regards,
Halil
Tony Krowiak
maintainerQuoted 13 lines · Halil Pasic
Quoted 10 lines · On Wed, 2 Dec 2020 18:41:01 -0500 Tony Krowiak <[email protected]> wrote:
The
vfio_apdevice driver registers a group notifier with VFIO when the file descriptor for a VFIO mediated device for a KVM guest is opened to receive notification that the KVM pointer is set (VFIO_GROUP_NOTIFY_SET_KVMevent). When the KVM pointer is set, thevfio_apdriver stashes the pointer and calls thekvm_get_kvm()function to increment its reference counter. When the notifier is called to make notification that the KVM pointer has been set to NULL, the driver should clean up any resources associated with the KVM pointer and decrement its reference counter. The current implementation does not take care of this clean up.
Quoted 3 lines
Quoted 1 line
Signed-off-by Tony Krowiak <[email protected]>Do we need a Fixes tag? Do we need this backported? In my opinion this is necessary since the interrupt patches.
I'll put in a fixes tag:
Yes, this should probably be backported.
Quoted 13 lines
Quoted 13 lines
--- drivers/s390/crypto/vfio_ap_ops.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)drivers/s390/crypto/vfio_ap_ops.c +0
-0@@ -1083,6 +1083,17 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,return NOTIFY_DONE ; }
Quoted 3 lines
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, · line 1086
Quoted 1 line
drivers/s390/crypto/vfio_ap_ops.c +1
-0+static void vfio_ap_mdev_put_kvm (struct ap_matrix_mdev *matrix_mdev)I don't like the name. The function does more that
put_kvm. Maybe something like_disconnect_kvm()?
Since the vfio_ap_mdev_set_kvm() function is called by the notifier when the KVM pointer is set, how about:
vfio_ap_mdev_unset_kvm()
for when the KVM pointer is nullified?
Quoted 48 lines
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, · line 1090
Quoted 5 lines
drivers/s390/crypto/vfio_ap_ops.c +4
-0+{ +if (matrix_mdev->kvm) { + (matrix_mdev->kvm); + matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ;Is a plain assignment to arch.crypto.pqap_hook apropriate, or do we need to take more care?
For instance
kvm_arch_crypto_set_masks()takeskvm->lockbefore pokingkvm->arch.crypto.crycb.Quoted 10 lines
drivers/s390/crypto/vfio_ap_ops.c +6
-0+vfio_ap_mdev_reset_queues (matrix_mdev->mdev); +kvm_put_kvm (matrix_mdev->kvm); + matrix_mdev->kvm =NULL ; + } +} +static int vfio_ap_mdev_group_notifier (struct notifier_block *nb,unsigned long action,void *data) {drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_group_notifier(struct notifier_block *nb, · line 1109
Quoted 5 lines
drivers/s390/crypto/vfio_ap_ops.c +1
-1@@ -1095,7 +1106,7 @@ static int vfio_ap_mdev_group_notifier(struct notifier_block *nb, matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);if (!data) { - matrix_mdev->kvm =NULL ; +vfio_ap_mdev_put_kvm (matrix_mdev);The lock question was already raised.
What are the exact circumstances under which this branch can be taken?
Quoted 18 lines
drivers/s390/crypto/vfio_ap_ops.c +1
-7return NOTIFY_OK ; }@@ -1222,13 +1233,7 @@ static void vfio_ap_mdev_release(struct mdev_device *mdev)struct ap_matrix_mdev *matrix_mdev =mdev_get_drvdata (mdev);mutex_lock (&matrix_dev->lock); -if (matrix_mdev->kvm) { -kvm_arch_crypto_clear_masks (matrix_mdev->kvm); - matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ; -vfio_ap_mdev_reset_queues (mdev); -kvm_put_kvm (matrix_mdev->kvm); - matrix_mdev->kvm =NULL ; - } +vfio_ap_mdev_put_kvm (matrix_mdev);mutex_unlock (&matrix_dev->lock); vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
Quoted 14 lines · On Fri, 4 Dec 2020 11:48:24 -0500 Tony Krowiak <[email protected]> wrote:
Quoted 13 lines · Halil Pasic
Quoted 10 lines · On Wed, 2 Dec 2020 18:41:01 -0500 Tony Krowiak <[email protected]> wrote:
The
vfio_apdevice driver registers a group notifier with VFIO when the file descriptor for a VFIO mediated device for a KVM guest is opened to receive notification that the KVM pointer is set (VFIO_GROUP_NOTIFY_SET_KVMevent). When the KVM pointer is set, thevfio_apdriver stashes the pointer and calls thekvm_get_kvm()function to increment its reference counter. When the notifier is called to make notification that the KVM pointer has been set to NULL, the driver should clean up any resources associated with the KVM pointer and decrement its reference counter. The current implementation does not take care of this clean up.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Tony Krowiak <[email protected]>Do we need a Fixes tag? Do we need this backported? In my opinion this is necessary since the interrupt patches.
I'll put in a fixes tag:
Fixes 258287c994de (s390: vfio-ap: implement mediated device open callback)
The canonical format would be
Quoted 19 lines
Yes, this should probably be backported.
Quoted 13 lines
Quoted 13 lines
--- drivers/s390/crypto/vfio_ap_ops.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)drivers/s390/crypto/vfio_ap_ops.c +0
-0@@ -1083,6 +1083,17 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,return NOTIFY_DONE ; }Quoted 3 lines
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, · line 1086
Quoted 1 line
drivers/s390/crypto/vfio_ap_ops.c +1
-0+static void vfio_ap_mdev_put_kvm (struct ap_matrix_mdev *matrix_mdev)I don't like the name. The function does more that
put_kvm. Maybe something like_disconnect_kvm()?
Quoted 6 lines
Since the
vfio_ap_mdev_set_kvm()function is called by the notifier when the KVM pointer is set, how about:
vfio_ap_mdev_unset_kvm()for when the KVM pointer is nullified?
Sounds good to me.
Tony Krowiak
maintainerQuoted 17 lines · Cornelia Huck
Quoted 14 lines · On Fri, 4 Dec 2020 11:48:24 -0500 Tony Krowiak <[email protected]> wrote:
Quoted 13 lines · Halil Pasic
Quoted 10 lines · On Wed, 2 Dec 2020 18:41:01 -0500 Tony Krowiak <[email protected]> wrote:
The
vfio_apdevice driver registers a group notifier with VFIO when the file descriptor for a VFIO mediated device for a KVM guest is opened to receive notification that the KVM pointer is set (VFIO_GROUP_NOTIFY_SET_KVMevent). When the KVM pointer is set, thevfio_apdriver stashes the pointer and calls thekvm_get_kvm()function to increment its reference counter. When the notifier is called to make notification that the KVM pointer has been set to NULL, the driver should clean up any resources associated with the KVM pointer and decrement its reference counter. The current implementation does not take care of this clean up.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Tony Krowiak <[email protected]>Do we need a Fixes tag? Do we need this backported? In my opinion this is necessary since the interrupt patches.
I'll put in a fixes tag:
Fixes 258287c994de (s390: vfio-ap: implement mediated device open callback)The canonical format would be
Fixes 258287c994de ("s390: vfio-ap: implement mediated device open callback")
Okay.
Quoted 27 lines
Quoted 19 lines
Yes, this should probably be backported.
Quoted 13 lines
Quoted 13 lines
--- drivers/s390/crypto/vfio_ap_ops.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)drivers/s390/crypto/vfio_ap_ops.c +0
-0@@ -1083,6 +1083,17 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,return NOTIFY_DONE ; }Quoted 3 lines
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, · line 1086
Quoted 1 line
drivers/s390/crypto/vfio_ap_ops.c +1
-0+static void vfio_ap_mdev_put_kvm (struct ap_matrix_mdev *matrix_mdev)I don't like the name. The function does more that
put_kvm. Maybe something like_disconnect_kvm()?Quoted 6 lines
Since the
vfio_ap_mdev_set_kvm()function is called by the notifier when the KVM pointer is set, how about:
vfio_ap_mdev_unset_kvm()for when the KVM pointer is nullified?
Sounds good to me.
Halil Pasic
maintainerQuoted 14 lines · On Fri, 4 Dec 2020 11:48:24 -0500 Tony Krowiak <[email protected]> wrote:
Quoted 13 lines · Halil Pasic
Quoted 10 lines · On Wed, 2 Dec 2020 18:41:01 -0500 Tony Krowiak <[email protected]> wrote:
The
vfio_apdevice driver registers a group notifier with VFIO when the file descriptor for a VFIO mediated device for a KVM guest is opened to receive notification that the KVM pointer is set (VFIO_GROUP_NOTIFY_SET_KVMevent). When the KVM pointer is set, thevfio_apdriver stashes the pointer and calls thekvm_get_kvm()function to increment its reference counter. When the notifier is called to make notification that the KVM pointer has been set to NULL, the driver should clean up any resources associated with the KVM pointer and decrement its reference counter. The current implementation does not take care of this clean up.
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Tony Krowiak <[email protected]>Do we need a Fixes tag? Do we need this backported? In my opinion this is necessary since the interrupt patches.
I'll put in a fixes tag:
Fixes 258287c994de (s390: vfio-ap: implement mediated device open callback)Yes, this should probably be backported.
I changed my mind regarding the severity of this issue. I was paranoid about post-mortem interrupts, and resulting notifier byte updates by the machine. What I overlooked is that the pin is going to prevent the memory form getting repurposed. I.e. if we have something like vmalloc(), vfio_pin(notifier_page), vfree(), I believe the notifier_page is not free (available for allocation). So the worst case scenario is IMHO a resource leak and not corruption. So I'm not sure this must be backported. Opinions?
Regards,
Halil
Quoted 26 lines · Halil Pasic
Quoted 14 lines · On Fri, 4 Dec 2020 11:48:24 -0500 Tony Krowiak <[email protected]> wrote:
Quoted 13 lines · Halil Pasic
Quoted 10 lines · On Wed, 2 Dec 2020 18:41:01 -0500 Tony Krowiak <[email protected]> wrote:
The
vfio_apdevice driver registers a group notifier with VFIO when the file descriptor for a VFIO mediated device for a KVM guest is opened to receive notification that the KVM pointer is set (VFIO_GROUP_NOTIFY_SET_KVMevent). When the KVM pointer is set, thevfio_apdriver stashes the pointer and calls thekvm_get_kvm()function to increment its reference counter. When the notifier is called to make notification that the KVM pointer has been set to NULL, the driver should clean up any resources associated with the KVM pointer and decrement its reference counter. The current implementation does not take care of this clean up.Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Tony Krowiak <[email protected]>Do we need a Fixes tag? Do we need this backported? In my opinion this is necessary since the interrupt patches.
I'll put in a fixes tag:
Fixes 258287c994de (s390: vfio-ap: implement mediated device open callback)Yes, this should probably be backported.
Quoted 8 lines
I changed my mind regarding the severity of this issue. I was paranoid about post-mortem interrupts, and resulting notifier byte updates by the machine. What I overlooked is that the pin is going to prevent the memory form getting repurposed. I.e. if we have something like
vmalloc(),vfio_pin(notifier_page),vfree(), I believe thenotifier_pageis not free (available for allocation). So the worst case scenario is IMHO a resource leak and not corruption. So I'm not sure this must be backported. Opinions?
Resource leaks qualify for backport and cc stable, but it is not a security issue so this has no urgency and CVE and these kind of things.
So lets finish this without hurry, add cc stable and then look for necessary distro backports.
Tony Krowiak
maintainerQuoted 20 lines · Tony Krowiak
The
vfio_apdevice driver registers a group notifier with VFIO when the file descriptor for a VFIO mediated device for a KVM guest is opened to receive notification that the KVM pointer is set (VFIO_GROUP_NOTIFY_SET_KVMevent). When the KVM pointer is set, thevfio_apdriver stashes the pointer and calls thekvm_get_kvm()function to increment its reference counter. When the notifier is called to make notification that the KVM pointer has been set to NULL, the driver should clean up any resources associated with the KVM pointer and decrement its reference counter. The current implementation does not take care of this clean up.Signed-off-by Tony Krowiak <[email protected]>--- drivers/s390/crypto/vfio_ap_ops.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)drivers/s390/crypto/vfio_ap_ops.c +0
-0
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, · line 1091
Quoted 9 lines
drivers/s390/crypto/vfio_ap_ops.c +6
-0@@ -1083,6 +1083,17 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,return NOTIFY_DONE ; } +static void vfio_ap_mdev_put_kvm (struct ap_matrix_mdev *matrix_mdev) +{ +if (matrix_mdev->kvm) { +kvm_arch_crypto_clear_masks (matrix_mdev->kvm); + matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ; +vfio_ap_mdev_reset_queues (matrix_mdev->mdev);
This reset probably does not belong here since there is no reason to reset the queues in the group notifier (see below). The reset should be done in the release callback only regardless of whether the KVM pointer exists or not.
Quoted 18 lines
drivers/s390/crypto/vfio_ap_ops.c +6
-1+kvm_put_kvm (matrix_mdev->kvm); + matrix_mdev->kvm =NULL ; + } +} +static int vfio_ap_mdev_group_notifier (struct notifier_block *nb,unsigned long action,void *data) {@@ -1095,7 +1106,7 @@ static int vfio_ap_mdev_group_notifier(struct notifier_block *nb, matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);if (!data) { - matrix_mdev->kvm =NULL ; +vfio_ap_mdev_put_kvm (matrix_mdev);return NOTIFY_OK ; }
drivers/s390/crypto/vfio_ap_ops.c static void vfio_ap_mdev_release(struct mdev_device *mdev) · line 1228
Quoted 7 lines
drivers/s390/crypto/vfio_ap_ops.c +0
-4@@ -1222,13 +1233,7 @@ static void vfio_ap_mdev_release(struct mdev_device *mdev)struct ap_matrix_mdev *matrix_mdev =mdev_get_drvdata (mdev);mutex_lock (&matrix_dev->lock); -if (matrix_mdev->kvm) { -kvm_arch_crypto_clear_masks (matrix_mdev->kvm); - matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ; -vfio_ap_mdev_reset_queues (mdev);
This release should be moved outside of the block and performed regardless of whether the KVM pointer exists or not.
Quoted 7 lines
drivers/s390/crypto/vfio_ap_ops.c +1
-3-kvm_put_kvm (matrix_mdev->kvm); - matrix_mdev->kvm =NULL ; - } +vfio_ap_mdev_put_kvm (matrix_mdev);mutex_unlock (&matrix_dev->lock); vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
Halil Pasic
maintainerQuoted 26 lines · On Mon, 7 Dec 2020 14:05:55 -0500 Tony Krowiak <[email protected]> wrote:
Quoted 23 lines · Tony Krowiak
The
vfio_apdevice driver registers a group notifier with VFIO when the file descriptor for a VFIO mediated device for a KVM guest is opened to receive notification that the KVM pointer is set (VFIO_GROUP_NOTIFY_SET_KVMevent). When the KVM pointer is set, thevfio_apdriver stashes the pointer and calls thekvm_get_kvm()function to increment its reference counter. When the notifier is called to make notification that the KVM pointer has been set to NULL, the driver should clean up any resources associated with the KVM pointer and decrement its reference counter. The current implementation does not take care of this clean up.Signed-off-by Tony Krowiak <[email protected]>--- drivers/s390/crypto/vfio_ap_ops.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)drivers/s390/crypto/vfio_ap_ops.c +0
-0@@ -1083,6 +1083,17 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,return NOTIFY_DONE ; }
Quoted 9 lines
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, · line 1091
Quoted 6 lines
drivers/s390/crypto/vfio_ap_ops.c +6
-0+static void vfio_ap_mdev_put_kvm (struct ap_matrix_mdev *matrix_mdev) +{ +if (matrix_mdev->kvm) { +kvm_arch_crypto_clear_masks (matrix_mdev->kvm); + matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ; +vfio_ap_mdev_reset_queues (matrix_mdev->mdev);This reset probably does not belong here since there is no reason to reset the queues in the group notifier (see below).
What about kvm_s390_gisc_unregister()? That needs a valid kvm pointer, or? Or is it OK to not pair a kvm_s390_gisc_register() with an kvm_s390_gisc_unregister()?
Regards,
Halil
Quoted 41 lines
The reset should be done in the release callback only regardless of whether the KVM pointer exists or not.
Quoted 18 lines
drivers/s390/crypto/vfio_ap_ops.c +6
-1+kvm_put_kvm (matrix_mdev->kvm); + matrix_mdev->kvm =NULL ; + } +} +static int vfio_ap_mdev_group_notifier (struct notifier_block *nb,unsigned long action,void *data) {@@ -1095,7 +1106,7 @@ static int vfio_ap_mdev_group_notifier(struct notifier_block *nb, matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);if (!data) { - matrix_mdev->kvm =NULL ; +vfio_ap_mdev_put_kvm (matrix_mdev);return NOTIFY_OK ; }drivers/s390/crypto/vfio_ap_ops.c static void vfio_ap_mdev_release(struct mdev_device *mdev) · line 1228
Quoted 7 lines
drivers/s390/crypto/vfio_ap_ops.c +0
-4@@ -1222,13 +1233,7 @@ static void vfio_ap_mdev_release(struct mdev_device *mdev)struct ap_matrix_mdev *matrix_mdev =mdev_get_drvdata (mdev);mutex_lock (&matrix_dev->lock); -if (matrix_mdev->kvm) { -kvm_arch_crypto_clear_masks (matrix_mdev->kvm); - matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ; -vfio_ap_mdev_reset_queues (mdev);This release should be moved outside of the block and performed regardless of whether the KVM pointer exists or not.
Quoted 7 lines
drivers/s390/crypto/vfio_ap_ops.c +1
-3-kvm_put_kvm (matrix_mdev->kvm); - matrix_mdev->kvm =NULL ; - } +vfio_ap_mdev_put_kvm (matrix_mdev);mutex_unlock (&matrix_dev->lock); vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
Tony Krowiak
maintainerQuoted 36 lines · Halil Pasic
Quoted 25 lines · On Mon, 7 Dec 2020 14:05:55 -0500 Tony Krowiak <[email protected]> wrote:
Quoted 23 lines · Tony Krowiak
The
vfio_apdevice driver registers a group notifier with VFIO when the file descriptor for a VFIO mediated device for a KVM guest is opened to receive notification that the KVM pointer is set (VFIO_GROUP_NOTIFY_SET_KVMevent). When the KVM pointer is set, thevfio_apdriver stashes the pointer and calls thekvm_get_kvm()function to increment its reference counter. When the notifier is called to make notification that the KVM pointer has been set to NULL, the driver should clean up any resources associated with the KVM pointer and decrement its reference counter. The current implementation does not take care of this clean up.Signed-off-by Tony Krowiak <[email protected]>--- drivers/s390/crypto/vfio_ap_ops.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)drivers/s390/crypto/vfio_ap_ops.c +0
-0@@ -1083,6 +1083,17 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,return NOTIFY_DONE ; }Quoted 8 lines
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, · line 1091
Quoted 6 lines
drivers/s390/crypto/vfio_ap_ops.c +6
-0+static void vfio_ap_mdev_put_kvm (struct ap_matrix_mdev *matrix_mdev) +{ +if (matrix_mdev->kvm) { +kvm_arch_crypto_clear_masks (matrix_mdev->kvm); + matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ; +vfio_ap_mdev_reset_queues (matrix_mdev->mdev);This reset probably does not belong here since there is no reason to reset the queues in the group notifier (see below).
Quoted 3 lines
What about
kvm_s390_gisc_unregister()? That needs a valid kvm pointer, or? Or is it OK to not pair akvm_s390_gisc_register()with ankvm_s390_gisc_unregister()?
I probably should have been more specific about what I meant. I was thinking that the reset should not be dependent upon whether there is a KVM pointer or not since this function is also called from the release callback. On the other hand, the vfio_ap_mdev_reset_queues function calls the vfio_ap_irq_disable (AQIC) function after each queue is reset. The vfio_ap_irq_disable function also cleans up the AQIC resources which requires that the KVM point is valid, so if the vfio_ap_reset_queues function is not called with a valid KVM pointer, that could result in an exception.
The thing is, it is unnecessary to disable interrupts after resetting a queue because the reset disables interrupts, so I think I should include a patch for this fix that does the following:
1. Removes the disabling of interrupts subsequent to resetting a queue.
2. Includes the cleanup of AQIC resources when a queue is reset if a KVM pointer is present.
This will allow us to keep the reset in the function above as well as the other places from which reset is executed.
Quoted 43 lines
Regards,
HalilQuoted 39 lines
The reset should be done in the release callback only regardless of whether the KVM pointer exists or not.
Quoted 18 lines
drivers/s390/crypto/vfio_ap_ops.c +6
-1+kvm_put_kvm (matrix_mdev->kvm); + matrix_mdev->kvm =NULL ; + } +} +static int vfio_ap_mdev_group_notifier (struct notifier_block *nb,unsigned long action,void *data) {@@ -1095,7 +1106,7 @@ static int vfio_ap_mdev_group_notifier(struct notifier_block *nb, matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);if (!data) { - matrix_mdev->kvm =NULL ; +vfio_ap_mdev_put_kvm (matrix_mdev);return NOTIFY_OK ; }drivers/s390/crypto/vfio_ap_ops.c static void vfio_ap_mdev_release(struct mdev_device *mdev) · line 1228
Quoted 7 lines
drivers/s390/crypto/vfio_ap_ops.c +0
-4@@ -1222,13 +1233,7 @@ static void vfio_ap_mdev_release(struct mdev_device *mdev)struct ap_matrix_mdev *matrix_mdev =mdev_get_drvdata (mdev);mutex_lock (&matrix_dev->lock); -if (matrix_mdev->kvm) { -kvm_arch_crypto_clear_masks (matrix_mdev->kvm); - matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ; -vfio_ap_mdev_reset_queues (mdev);This release should be moved outside of the block and performed regardless of whether the KVM pointer exists or not.
Quoted 7 lines
drivers/s390/crypto/vfio_ap_ops.c +1
-3-kvm_put_kvm (matrix_mdev->kvm); - matrix_mdev->kvm =NULL ; - } +vfio_ap_mdev_put_kvm (matrix_mdev);mutex_unlock (&matrix_dev->lock); vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
Halil Pasic
maintainerQuoted 28 lines · On Fri, 11 Dec 2020 16:08:53 -0500 Tony Krowiak <[email protected]> wrote:
Quoted 11 lines
Quoted 8 lines
drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb, · line 1091
Quoted 6 lines
drivers/s390/crypto/vfio_ap_ops.c +6
-0+static void vfio_ap_mdev_put_kvm (struct ap_matrix_mdev *matrix_mdev) +{ +if (matrix_mdev->kvm) { +kvm_arch_crypto_clear_masks (matrix_mdev->kvm); + matrix_mdev->kvm->arch.crypto.pqap_hook =NULL ; +vfio_ap_mdev_reset_queues (matrix_mdev->mdev);This reset probably does not belong here since there is no reason to reset the queues in the group notifier (see below).
What about
kvm_s390_gisc_unregister()? That needs a valid kvm pointer, or? Or is it OK to not pair akvm_s390_gisc_register()with ankvm_s390_gisc_unregister()?I probably should have been more specific about what I meant. I was thinking that the reset should not be dependent upon whether there is a KVM pointer or not since this function is also called from the release callback. On the other hand, the
vfio_ap_mdev_reset_queuesfunction calls thevfio_ap_irq_disable(AQIC) function after each queue is reset. Thevfio_ap_irq_disablefunction also cleans up the AQIC resources which requires that the KVM point is valid, so if thevfio_ap_reset_queuesfunction is not called with a valid KVM pointer, that could result in an exception.The thing is, it is unnecessary to disable interrupts after resetting a queue because the reset disables interrupts, so I think I should include a patch for this fix that does the following:
Quoted 4 lines
1. Removes the disabling of interrupts subsequent to resetting a queue.
2. Includes the cleanup of AQIC resources when a queue is reset if a KVM pointer is present.
Sounds like a plan. I see, in your v2 vfio_ap_mdev_unset_kvm() does call vfio_ap_mdev_reset_queues() even when called from the group notifier. I also like that the cleanup of AQIC resources is a part of queue_reset. In fact I asked a while ago (Message-ID:
<[email protected]> in October) to make vfio_ap_mdev_reset_queue() call vfio_ap_free_aqic_resources(q).
Regards,
Halil