linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] s390/vfio-ap: fix memory leak in mdev remove callback
@ 2021-05-05 17:28 Tony Krowiak
  2021-05-05 17:44 ` Christian Borntraeger
  2021-05-06 10:22 ` Cornelia Huck
  0 siblings, 2 replies; 8+ messages in thread
From: Tony Krowiak @ 2021-05-05 17:28 UTC (permalink / raw)
  To: linux-s390, linux-kernel
  Cc: borntraeger, cohuck, pasic, jjherne, jgg, alex.williamson,
	kwankhede, Tony Krowiak, stable, 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. 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")
Cc: stable@vger.kernel.org
Signed-off-by: Tony Krowiak <akrowiak@stny.rr.com>
Signed-off-by: Tony Krowiak <akrowiak@linux.ibm.com>
---
 drivers/s390/crypto/vfio_ap_ops.c | 39 +++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 10 deletions(-)

diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
index b2c7e10dfdcd..757166da947e 100644
--- a/drivers/s390/crypto/vfio_ap_ops.c
+++ b/drivers/s390/crypto/vfio_ap_ops.c
@@ -335,6 +335,32 @@ static void vfio_ap_matrix_init(struct ap_config_info *info,
 	matrix->adm_max = info->apxa ? info->Nd : 15;
 }
 
+static bool vfio_ap_mdev_has_crycb(struct ap_matrix_mdev *matrix_mdev)
+{
+	return (matrix_mdev->kvm && matrix_mdev->kvm->arch.crypto.crycbd);
+}
+
+static void vfio_ap_mdev_clear_apcb(struct ap_matrix_mdev *matrix_mdev)
+{
+	/*
+	 * If the KVM pointer is in the process of being set, wait until the
+	 * process has completed.
+	 */
+	wait_event_cmd(matrix_mdev->wait_for_kvm,
+		       !matrix_mdev->kvm_busy,
+		       mutex_unlock(&matrix_dev->lock),
+		       mutex_lock(&matrix_dev->lock));
+
+	if (vfio_ap_mdev_has_crycb(matrix_mdev)) {
+		matrix_mdev->kvm_busy = true;
+		mutex_unlock(&matrix_dev->lock);
+		kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
+		mutex_lock(&matrix_dev->lock);
+		matrix_mdev->kvm_busy = false;
+		wake_up_all(&matrix_mdev->wait_for_kvm);
+	}
+}
+
 static int vfio_ap_mdev_create(struct mdev_device *mdev)
 {
 	struct ap_matrix_mdev *matrix_mdev;
@@ -366,16 +392,9 @@ 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;
-	}
-
+	WARN(vfio_ap_mdev_has_crycb(matrix_mdev),
+	     "Removing mdev leaves KVM guest without any crypto devices");
+	vfio_ap_mdev_clear_apcb(matrix_mdev);
 	vfio_ap_mdev_reset_queues(mdev);
 	list_del(&matrix_mdev->node);
 	kfree(matrix_mdev);
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] s390/vfio-ap: fix memory leak in mdev remove callback
  2021-05-05 17:28 [PATCH] s390/vfio-ap: fix memory leak in mdev remove callback Tony Krowiak
@ 2021-05-05 17:44 ` Christian Borntraeger
  2021-05-05 18:00   ` Jason Gunthorpe
  2021-05-10 17:22   ` Tony Krowiak
  2021-05-06 10:22 ` Cornelia Huck
  1 sibling, 2 replies; 8+ messages in thread
From: Christian Borntraeger @ 2021-05-05 17:44 UTC (permalink / raw)
  To: Tony Krowiak, linux-s390, linux-kernel
  Cc: cohuck, pasic, jjherne, jgg, alex.williamson, kwankhede, stable,
	Tony Krowiak



On 05.05.21 19:28, Tony Krowiak wrote:
> 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.
[...]
>   static int vfio_ap_mdev_create(struct mdev_device *mdev)
>   {
>   	struct ap_matrix_mdev *matrix_mdev;
> @@ -366,16 +392,9 @@ 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;
> -	}
> -
> +	WARN(vfio_ap_mdev_has_crycb(matrix_mdev),
> +	     "Removing mdev leaves KVM guest without any crypto devices");
> +	vfio_ap_mdev_clear_apcb(matrix_mdev);

Triggering a kernel warning due to an administrative task is not good.
Can't you simply clear the crycb? Maybe do a printk, but not a WARN.

>   	vfio_ap_mdev_reset_queues(mdev);
>   	list_del(&matrix_mdev->node);
>   	kfree(matrix_mdev);
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] s390/vfio-ap: fix memory leak in mdev remove callback
  2021-05-05 17:44 ` Christian Borntraeger
@ 2021-05-05 18:00   ` Jason Gunthorpe
  2021-05-10 17:22   ` Tony Krowiak
  1 sibling, 0 replies; 8+ messages in thread
From: Jason Gunthorpe @ 2021-05-05 18:00 UTC (permalink / raw)
  To: Christian Borntraeger
  Cc: Tony Krowiak, linux-s390, linux-kernel, cohuck, pasic, jjherne,
	alex.williamson, kwankhede, stable, Tony Krowiak

On Wed, May 05, 2021 at 07:44:55PM +0200, Christian Borntraeger wrote:
> 
> 
> On 05.05.21 19:28, Tony Krowiak wrote:
> > 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.
> [...]
> >   static int vfio_ap_mdev_create(struct mdev_device *mdev)
> >   {
> >   	struct ap_matrix_mdev *matrix_mdev;
> > @@ -366,16 +392,9 @@ 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;
> > -	}
> > -
> > +	WARN(vfio_ap_mdev_has_crycb(matrix_mdev),
> > +	     "Removing mdev leaves KVM guest without any crypto devices");
> > +	vfio_ap_mdev_clear_apcb(matrix_mdev);
> 
> Triggering a kernel warning due to an administrative task is not good.
> Can't you simply clear the crycb? Maybe do a printk, but not a WARN.

+1

Jason

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] s390/vfio-ap: fix memory leak in mdev remove callback
  2021-05-05 17:28 [PATCH] s390/vfio-ap: fix memory leak in mdev remove callback Tony Krowiak
  2021-05-05 17:44 ` Christian Borntraeger
@ 2021-05-06 10:22 ` Cornelia Huck
  2021-05-06 10:45   ` Cornelia Huck
  2021-05-10 17:44   ` Tony Krowiak
  1 sibling, 2 replies; 8+ messages in thread
From: Cornelia Huck @ 2021-05-06 10:22 UTC (permalink / raw)
  To: Tony Krowiak
  Cc: linux-s390, linux-kernel, borntraeger, pasic, jjherne, jgg,
	alex.williamson, kwankhede, stable, Tony Krowiak

On Wed,  5 May 2021 13:28:26 -0400
Tony Krowiak <akrowiak@linux.ibm.com> wrote:

> 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")
> Cc: stable@vger.kernel.org
> Signed-off-by: Tony Krowiak <akrowiak@stny.rr.com>
> Signed-off-by: Tony Krowiak <akrowiak@linux.ibm.com>
> ---
>  drivers/s390/crypto/vfio_ap_ops.c | 39 +++++++++++++++++++++++--------
>  1 file changed, 29 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
> index b2c7e10dfdcd..757166da947e 100644
> --- a/drivers/s390/crypto/vfio_ap_ops.c
> +++ b/drivers/s390/crypto/vfio_ap_ops.c
> @@ -335,6 +335,32 @@ static void vfio_ap_matrix_init(struct ap_config_info *info,
>  	matrix->adm_max = info->apxa ? info->Nd : 15;
>  }
>  
> +static bool vfio_ap_mdev_has_crycb(struct ap_matrix_mdev *matrix_mdev)
> +{
> +	return (matrix_mdev->kvm && matrix_mdev->kvm->arch.crypto.crycbd);
> +}
> +
> +static void vfio_ap_mdev_clear_apcb(struct ap_matrix_mdev *matrix_mdev)
> +{
> +	/*
> +	 * If the KVM pointer is in the process of being set, wait until the
> +	 * process has completed.
> +	 */
> +	wait_event_cmd(matrix_mdev->wait_for_kvm,
> +		       !matrix_mdev->kvm_busy,
> +		       mutex_unlock(&matrix_dev->lock),
> +		       mutex_lock(&matrix_dev->lock));
> +
> +	if (vfio_ap_mdev_has_crycb(matrix_mdev)) {
> +		matrix_mdev->kvm_busy = true;
> +		mutex_unlock(&matrix_dev->lock);
> +		kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
> +		mutex_lock(&matrix_dev->lock);
> +		matrix_mdev->kvm_busy = false;
> +		wake_up_all(&matrix_mdev->wait_for_kvm);
> +	}
> +}

Looking at vfio_ap_mdev_unset_kvm(), do you need to unhook the kvm here
as well?

(Or can you maybe even combine the two functions into one?)

> +
>  static int vfio_ap_mdev_create(struct mdev_device *mdev)
>  {
>  	struct ap_matrix_mdev *matrix_mdev;
> @@ -366,16 +392,9 @@ 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;
> -	}
> -
> +	WARN(vfio_ap_mdev_has_crycb(matrix_mdev),
> +	     "Removing mdev leaves KVM guest without any crypto devices");
> +	vfio_ap_mdev_clear_apcb(matrix_mdev);
>  	vfio_ap_mdev_reset_queues(mdev);
>  	list_del(&matrix_mdev->node);
>  	kfree(matrix_mdev);


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] s390/vfio-ap: fix memory leak in mdev remove callback
  2021-05-06 10:22 ` Cornelia Huck
@ 2021-05-06 10:45   ` Cornelia Huck
  2021-05-10 17:50     ` Tony Krowiak
  2021-05-10 17:44   ` Tony Krowiak
  1 sibling, 1 reply; 8+ messages in thread
From: Cornelia Huck @ 2021-05-06 10:45 UTC (permalink / raw)
  To: Tony Krowiak
  Cc: linux-s390, linux-kernel, borntraeger, pasic, jjherne, jgg,
	alex.williamson, kwankhede, stable, Tony Krowiak

On Thu, 6 May 2021 12:22:45 +0200
Cornelia Huck <cohuck@redhat.com> wrote:

> On Wed,  5 May 2021 13:28:26 -0400
> Tony Krowiak <akrowiak@linux.ibm.com> wrote:
> 
> > 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")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Tony Krowiak <akrowiak@stny.rr.com>
> > Signed-off-by: Tony Krowiak <akrowiak@linux.ibm.com>
> > ---
> >  drivers/s390/crypto/vfio_ap_ops.c | 39 +++++++++++++++++++++++--------
> >  1 file changed, 29 insertions(+), 10 deletions(-)
> > 
> > diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
> > index b2c7e10dfdcd..757166da947e 100644
> > --- a/drivers/s390/crypto/vfio_ap_ops.c
> > +++ b/drivers/s390/crypto/vfio_ap_ops.c
> > @@ -335,6 +335,32 @@ static void vfio_ap_matrix_init(struct ap_config_info *info,
> >  	matrix->adm_max = info->apxa ? info->Nd : 15;
> >  }
> >  
> > +static bool vfio_ap_mdev_has_crycb(struct ap_matrix_mdev *matrix_mdev)
> > +{
> > +	return (matrix_mdev->kvm && matrix_mdev->kvm->arch.crypto.crycbd);
> > +}
> > +
> > +static void vfio_ap_mdev_clear_apcb(struct ap_matrix_mdev *matrix_mdev)
> > +{
> > +	/*
> > +	 * If the KVM pointer is in the process of being set, wait until the
> > +	 * process has completed.
> > +	 */
> > +	wait_event_cmd(matrix_mdev->wait_for_kvm,
> > +		       !matrix_mdev->kvm_busy,
> > +		       mutex_unlock(&matrix_dev->lock),
> > +		       mutex_lock(&matrix_dev->lock));
> > +
> > +	if (vfio_ap_mdev_has_crycb(matrix_mdev)) {
> > +		matrix_mdev->kvm_busy = true;
> > +		mutex_unlock(&matrix_dev->lock);
> > +		kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
> > +		mutex_lock(&matrix_dev->lock);
> > +		matrix_mdev->kvm_busy = false;
> > +		wake_up_all(&matrix_mdev->wait_for_kvm);
> > +	}
> > +}  
> 
> Looking at vfio_ap_mdev_unset_kvm(), do you need to unhook the kvm here
> as well?
> 
> (Or can you maybe even combine the two functions into one?)

Staring at the code some more, the rules where you unset the kvm stuff
seem pretty confusing (at least to me). Does this partial unhooking in
the remove callback make sense?

> 
> > +
> >  static int vfio_ap_mdev_create(struct mdev_device *mdev)
> >  {
> >  	struct ap_matrix_mdev *matrix_mdev;
> > @@ -366,16 +392,9 @@ 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;
> > -	}
> > -
> > +	WARN(vfio_ap_mdev_has_crycb(matrix_mdev),
> > +	     "Removing mdev leaves KVM guest without any crypto devices");
> > +	vfio_ap_mdev_clear_apcb(matrix_mdev);
> >  	vfio_ap_mdev_reset_queues(mdev);
> >  	list_del(&matrix_mdev->node);
> >  	kfree(matrix_mdev);  
> 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] s390/vfio-ap: fix memory leak in mdev remove callback
  2021-05-05 17:44 ` Christian Borntraeger
  2021-05-05 18:00   ` Jason Gunthorpe
@ 2021-05-10 17:22   ` Tony Krowiak
  1 sibling, 0 replies; 8+ messages in thread
From: Tony Krowiak @ 2021-05-10 17:22 UTC (permalink / raw)
  To: Christian Borntraeger, linux-s390, linux-kernel
  Cc: cohuck, pasic, jjherne, jgg, alex.williamson, kwankhede, stable,
	Tony Krowiak



On 5/5/21 1:44 PM, Christian Borntraeger wrote:
>
>
> On 05.05.21 19:28, Tony Krowiak wrote:
>> 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.
> [...]
>>   static int vfio_ap_mdev_create(struct mdev_device *mdev)
>>   {
>>       struct ap_matrix_mdev *matrix_mdev;
>> @@ -366,16 +392,9 @@ 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;
>> -    }
>> -
>> +    WARN(vfio_ap_mdev_has_crycb(matrix_mdev),
>> +         "Removing mdev leaves KVM guest without any crypto devices");
>> +    vfio_ap_mdev_clear_apcb(matrix_mdev);
>
> Triggering a kernel warning due to an administrative task is not good.
> Can't you simply clear the crycb? Maybe do a printk, but not a WARN.

I'll take the warning out.

>
>>       vfio_ap_mdev_reset_queues(mdev);
>>       list_del(&matrix_mdev->node);
>>       kfree(matrix_mdev);
>>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] s390/vfio-ap: fix memory leak in mdev remove callback
  2021-05-06 10:22 ` Cornelia Huck
  2021-05-06 10:45   ` Cornelia Huck
@ 2021-05-10 17:44   ` Tony Krowiak
  1 sibling, 0 replies; 8+ messages in thread
From: Tony Krowiak @ 2021-05-10 17:44 UTC (permalink / raw)
  To: Cornelia Huck
  Cc: linux-s390, linux-kernel, borntraeger, pasic, jjherne, jgg,
	alex.williamson, kwankhede, stable, Tony Krowiak



On 5/6/21 6:22 AM, Cornelia Huck wrote:
> On Wed,  5 May 2021 13:28:26 -0400
> Tony Krowiak <akrowiak@linux.ibm.com> wrote:
>
>> 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")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Tony Krowiak <akrowiak@stny.rr.com>
>> Signed-off-by: Tony Krowiak <akrowiak@linux.ibm.com>
>> ---
>>   drivers/s390/crypto/vfio_ap_ops.c | 39 +++++++++++++++++++++++--------
>>   1 file changed, 29 insertions(+), 10 deletions(-)
>>
>> diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
>> index b2c7e10dfdcd..757166da947e 100644
>> --- a/drivers/s390/crypto/vfio_ap_ops.c
>> +++ b/drivers/s390/crypto/vfio_ap_ops.c
>> @@ -335,6 +335,32 @@ static void vfio_ap_matrix_init(struct ap_config_info *info,
>>   	matrix->adm_max = info->apxa ? info->Nd : 15;
>>   }
>>   
>> +static bool vfio_ap_mdev_has_crycb(struct ap_matrix_mdev *matrix_mdev)
>> +{
>> +	return (matrix_mdev->kvm && matrix_mdev->kvm->arch.crypto.crycbd);
>> +}
>> +
>> +static void vfio_ap_mdev_clear_apcb(struct ap_matrix_mdev *matrix_mdev)
>> +{
>> +	/*
>> +	 * If the KVM pointer is in the process of being set, wait until the
>> +	 * process has completed.
>> +	 */
>> +	wait_event_cmd(matrix_mdev->wait_for_kvm,
>> +		       !matrix_mdev->kvm_busy,
>> +		       mutex_unlock(&matrix_dev->lock),
>> +		       mutex_lock(&matrix_dev->lock));
>> +
>> +	if (vfio_ap_mdev_has_crycb(matrix_mdev)) {
>> +		matrix_mdev->kvm_busy = true;
>> +		mutex_unlock(&matrix_dev->lock);
>> +		kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
>> +		mutex_lock(&matrix_dev->lock);
>> +		matrix_mdev->kvm_busy = false;
>> +		wake_up_all(&matrix_mdev->wait_for_kvm);
>> +	}
>> +}
> Looking at vfio_ap_mdev_unset_kvm(), do you need to unhook the kvm here
> as well?
>
> (Or can you maybe even combine the two functions into one?)

I contemplated just calling the vfio_ap_unset_kvm() function from
the vfio_ap_mdev_remove() function, but my thinking at the time
was that some of the other things done in the unset function, such
as kvm_put_kvm() etc., might cause problems.
After thinking about it some more, the vfio_ap_mdev_remove()
function will not get called until the vfio_ap_mdev_release()
callback is invoked unless something weird happens. Since the
remove callback gets rid of the mdev and the release callback
calls the vfio_ap_unset_kvm() function anyway, I now see no harm
in just calling the unset function from the remove callback and
eliminating the function above.

>
>> +
>>   static int vfio_ap_mdev_create(struct mdev_device *mdev)
>>   {
>>   	struct ap_matrix_mdev *matrix_mdev;
>> @@ -366,16 +392,9 @@ 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;
>> -	}
>> -
>> +	WARN(vfio_ap_mdev_has_crycb(matrix_mdev),
>> +	     "Removing mdev leaves KVM guest without any crypto devices");
>> +	vfio_ap_mdev_clear_apcb(matrix_mdev);
>>   	vfio_ap_mdev_reset_queues(mdev);
>>   	list_del(&matrix_mdev->node);
>>   	kfree(matrix_mdev);


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] s390/vfio-ap: fix memory leak in mdev remove callback
  2021-05-06 10:45   ` Cornelia Huck
@ 2021-05-10 17:50     ` Tony Krowiak
  0 siblings, 0 replies; 8+ messages in thread
From: Tony Krowiak @ 2021-05-10 17:50 UTC (permalink / raw)
  To: Cornelia Huck
  Cc: linux-s390, linux-kernel, borntraeger, pasic, jjherne, jgg,
	alex.williamson, kwankhede, stable, Tony Krowiak



On 5/6/21 6:45 AM, Cornelia Huck wrote:
> On Thu, 6 May 2021 12:22:45 +0200
> Cornelia Huck <cohuck@redhat.com> wrote:
>
>> On Wed,  5 May 2021 13:28:26 -0400
>> Tony Krowiak <akrowiak@linux.ibm.com> wrote:
>>
>>> 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")
>>> Cc: stable@vger.kernel.org
>>> Signed-off-by: Tony Krowiak <akrowiak@stny.rr.com>
>>> Signed-off-by: Tony Krowiak <akrowiak@linux.ibm.com>
>>> ---
>>>   drivers/s390/crypto/vfio_ap_ops.c | 39 +++++++++++++++++++++++--------
>>>   1 file changed, 29 insertions(+), 10 deletions(-)
>>>
>>> diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
>>> index b2c7e10dfdcd..757166da947e 100644
>>> --- a/drivers/s390/crypto/vfio_ap_ops.c
>>> +++ b/drivers/s390/crypto/vfio_ap_ops.c
>>> @@ -335,6 +335,32 @@ static void vfio_ap_matrix_init(struct ap_config_info *info,
>>>   	matrix->adm_max = info->apxa ? info->Nd : 15;
>>>   }
>>>   
>>> +static bool vfio_ap_mdev_has_crycb(struct ap_matrix_mdev *matrix_mdev)
>>> +{
>>> +	return (matrix_mdev->kvm && matrix_mdev->kvm->arch.crypto.crycbd);
>>> +}
>>> +
>>> +static void vfio_ap_mdev_clear_apcb(struct ap_matrix_mdev *matrix_mdev)
>>> +{
>>> +	/*
>>> +	 * If the KVM pointer is in the process of being set, wait until the
>>> +	 * process has completed.
>>> +	 */
>>> +	wait_event_cmd(matrix_mdev->wait_for_kvm,
>>> +		       !matrix_mdev->kvm_busy,
>>> +		       mutex_unlock(&matrix_dev->lock),
>>> +		       mutex_lock(&matrix_dev->lock));
>>> +
>>> +	if (vfio_ap_mdev_has_crycb(matrix_mdev)) {
>>> +		matrix_mdev->kvm_busy = true;
>>> +		mutex_unlock(&matrix_dev->lock);
>>> +		kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
>>> +		mutex_lock(&matrix_dev->lock);
>>> +		matrix_mdev->kvm_busy = false;
>>> +		wake_up_all(&matrix_mdev->wait_for_kvm);
>>> +	}
>>> +}
>> Looking at vfio_ap_mdev_unset_kvm(), do you need to unhook the kvm here
>> as well?
>>
>> (Or can you maybe even combine the two functions into one?)
> Staring at the code some more, the rules where you unset the kvm stuff
> seem pretty confusing (at least to me). Does this partial unhooking in
> the remove callback make sense?

If you stare at it too long, you'll go blind:) As I stated in my response
to your previous review comment, I'm going to remove the function
above and call the vfio_ap_mdev_unset_kvm() function from the
remove callback.

>
>>> +
>>>   static int vfio_ap_mdev_create(struct mdev_device *mdev)
>>>   {
>>>   	struct ap_matrix_mdev *matrix_mdev;
>>> @@ -366,16 +392,9 @@ 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;
>>> -	}
>>> -
>>> +	WARN(vfio_ap_mdev_has_crycb(matrix_mdev),
>>> +	     "Removing mdev leaves KVM guest without any crypto devices");
>>> +	vfio_ap_mdev_clear_apcb(matrix_mdev);
>>>   	vfio_ap_mdev_reset_queues(mdev);
>>>   	list_del(&matrix_mdev->node);
>>>   	kfree(matrix_mdev);


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-05-10 17:51 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-05 17:28 [PATCH] s390/vfio-ap: fix memory leak in mdev remove callback Tony Krowiak
2021-05-05 17:44 ` Christian Borntraeger
2021-05-05 18:00   ` Jason Gunthorpe
2021-05-10 17:22   ` Tony Krowiak
2021-05-06 10:22 ` Cornelia Huck
2021-05-06 10:45   ` Cornelia Huck
2021-05-10 17:50     ` Tony Krowiak
2021-05-10 17:44   ` Tony Krowiak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).