Re: [PATCH v13 05/15] s390/vfio-ap: manage link between queue struct and matrix mdev

2021-01-14 Thread Tony Krowiak




On 1/13/21 9:50 PM, Halil Pasic wrote:

On Wed, 13 Jan 2021 16:41:27 -0500
Tony Krowiak  wrote:


On 1/11/21 2:17 PM, Halil Pasic wrote:

On Tue, 22 Dec 2020 20:15:56 -0500
Tony Krowiak  wrote:
  

Let's create links between each queue device bound to the vfio_ap device
driver and the matrix mdev to which the queue's APQN is assigned. The idea
is to facilitate efficient retrieval of the objects representing the queue
devices and matrix mdevs as well as to verify that a queue assigned to
a matrix mdev is bound to the driver.

The links will be created as follows:

 * When the queue device is probed, if its APQN is assigned to a matrix
   mdev, the structures representing the queue device and the matrix mdev
   will be linked.

 * When an adapter or domain is assigned to a matrix mdev, for each new
   APQN assigned that references a queue device bound to the vfio_ap
   device driver, the structures representing the queue device and the
   matrix mdev will be linked.

The links will be removed as follows:

 * When the queue device is removed, if its APQN is assigned to a matrix
   mdev, the structures representing the queue device and the matrix mdev
   will be unlinked.

 * When an adapter or domain is unassigned from a matrix mdev, for each
   APQN unassigned that references a queue device bound to the vfio_ap
   device driver, the structures representing the queue device and the
   matrix mdev will be unlinked.

Signed-off-by: Tony Krowiak 

Reviewed-by: Halil Pasic 
  

[..]


+
   int vfio_ap_mdev_probe_queue(struct ap_device *apdev)
   {
struct vfio_ap_queue *q;
@@ -1324,9 +1404,13 @@ int vfio_ap_mdev_probe_queue(struct ap_device *apdev)
q = kzalloc(sizeof(*q), GFP_KERNEL);
if (!q)
return -ENOMEM;
+   mutex_lock(_dev->lock);
dev_set_drvdata(>device, q);
q->apqn = to_ap_queue(>device)->qid;
q->saved_isc = VFIO_AP_ISC_INVALID;
+   vfio_ap_queue_link_mdev(q);
+   mutex_unlock(_dev->lock);
+

Does the critical section have to include more than just
vfio_ap_queue_link_mdev()? Did we need the critical section
before this patch?

We did not need the critical section before this patch because
the only function that retrieved the vfio_ap_queue via the queue
device's drvdata was the remove callback. I included the initialization
of the vfio_ap_queue object under lock because the
vfio_ap_find_queue() function retrieves the vfio_ap_queue object from
the queue device's drvdata so it might be advantageous to initialize
it under the mdev lock. On the other hand, I can't come up with a good
argument to change this.



I was asking out of curiosity, not because I want it changed. I was
also wondering if somebody could see a partially initialized device:
we even first call dev_set_drvdata() and only then finish the
initialization. Before 's390/vfio-ap: use new AP bus interface to search
for queue devices', which is the previous patch, we had the klist code
in between, which uses spinlocks, which I think ensure, that all
effects of probe are seen when we get the queue from
vfio_ap_find_queue(). But with patch 4 in place that is not the case any
more. Or am I wrong?


You are correct insofar as patch 4 replaces the driver_find_device()
function call with a call to AP bus's ap_get_qdev() function which
does not use spinlocks. Without digging deeply into the probe call
chain I do not know whether or not  the use of spinlocks by the klist
code ensures all effects of the probe are seen when we get the
queue from vfio_ap_find_queue(). What I'm sure about is that since
both vfio_ap_find_queue() and the setting of the drvdata in the
probe function are always done under the mdev lock, consistency
should be maintained. What I did decide when thinking about your
previous review comment is that we should probably initialize the
vfio_ap_queue object before setting the drvdata, so I made that change.



Regards,
Halil




Re: [PATCH v13 05/15] s390/vfio-ap: manage link between queue struct and matrix mdev

2021-01-13 Thread Halil Pasic
On Wed, 13 Jan 2021 16:41:27 -0500
Tony Krowiak  wrote:

> On 1/11/21 2:17 PM, Halil Pasic wrote:
> > On Tue, 22 Dec 2020 20:15:56 -0500
> > Tony Krowiak  wrote:
> >  
> >> Let's create links between each queue device bound to the vfio_ap device
> >> driver and the matrix mdev to which the queue's APQN is assigned. The idea
> >> is to facilitate efficient retrieval of the objects representing the queue
> >> devices and matrix mdevs as well as to verify that a queue assigned to
> >> a matrix mdev is bound to the driver.
> >>
> >> The links will be created as follows:
> >>
> >> * When the queue device is probed, if its APQN is assigned to a matrix
> >>   mdev, the structures representing the queue device and the matrix 
> >> mdev
> >>   will be linked.
> >>
> >> * When an adapter or domain is assigned to a matrix mdev, for each new
> >>   APQN assigned that references a queue device bound to the vfio_ap
> >>   device driver, the structures representing the queue device and the
> >>   matrix mdev will be linked.
> >>
> >> The links will be removed as follows:
> >>
> >> * When the queue device is removed, if its APQN is assigned to a matrix
> >>   mdev, the structures representing the queue device and the matrix 
> >> mdev
> >>   will be unlinked.
> >>
> >> * When an adapter or domain is unassigned from a matrix mdev, for each
> >>   APQN unassigned that references a queue device bound to the vfio_ap
> >>   device driver, the structures representing the queue device and the
> >>   matrix mdev will be unlinked.
> >>
> >> Signed-off-by: Tony Krowiak   
> > Reviewed-by: Halil Pasic 
> >  

[..]

> >> +
> >>   int vfio_ap_mdev_probe_queue(struct ap_device *apdev)
> >>   {
> >>struct vfio_ap_queue *q;
> >> @@ -1324,9 +1404,13 @@ int vfio_ap_mdev_probe_queue(struct ap_device 
> >> *apdev)
> >>q = kzalloc(sizeof(*q), GFP_KERNEL);
> >>if (!q)
> >>return -ENOMEM;
> >> +  mutex_lock(_dev->lock);
> >>dev_set_drvdata(>device, q);
> >>q->apqn = to_ap_queue(>device)->qid;
> >>q->saved_isc = VFIO_AP_ISC_INVALID;
> >> +  vfio_ap_queue_link_mdev(q);
> >> +  mutex_unlock(_dev->lock);
> >> +  
> > Does the critical section have to include more than just
> > vfio_ap_queue_link_mdev()? Did we need the critical section
> > before this patch?  
> 
> We did not need the critical section before this patch because
> the only function that retrieved the vfio_ap_queue via the queue
> device's drvdata was the remove callback. I included the initialization
> of the vfio_ap_queue object under lock because the
> vfio_ap_find_queue() function retrieves the vfio_ap_queue object from
> the queue device's drvdata so it might be advantageous to initialize
> it under the mdev lock. On the other hand, I can't come up with a good
> argument to change this.
> 
> 

I was asking out of curiosity, not because I want it changed. I was
also wondering if somebody could see a partially initialized device:
we even first call dev_set_drvdata() and only then finish the
initialization. Before 's390/vfio-ap: use new AP bus interface to search
for queue devices', which is the previous patch, we had the klist code
in between, which uses spinlocks, which I think ensure, that all
effects of probe are seen when we get the queue from
vfio_ap_find_queue(). But with patch 4 in place that is not the case any
more. Or am I wrong?

Regards,
Halil


Re: [PATCH v13 05/15] s390/vfio-ap: manage link between queue struct and matrix mdev

2021-01-13 Thread Tony Krowiak




On 1/11/21 2:17 PM, Halil Pasic wrote:

On Tue, 22 Dec 2020 20:15:56 -0500
Tony Krowiak  wrote:


Let's create links between each queue device bound to the vfio_ap device
driver and the matrix mdev to which the queue's APQN is assigned. The idea
is to facilitate efficient retrieval of the objects representing the queue
devices and matrix mdevs as well as to verify that a queue assigned to
a matrix mdev is bound to the driver.

The links will be created as follows:

* When the queue device is probed, if its APQN is assigned to a matrix
  mdev, the structures representing the queue device and the matrix mdev
  will be linked.

* When an adapter or domain is assigned to a matrix mdev, for each new
  APQN assigned that references a queue device bound to the vfio_ap
  device driver, the structures representing the queue device and the
  matrix mdev will be linked.

The links will be removed as follows:

* When the queue device is removed, if its APQN is assigned to a matrix
  mdev, the structures representing the queue device and the matrix mdev
  will be unlinked.

* When an adapter or domain is unassigned from a matrix mdev, for each
  APQN unassigned that references a queue device bound to the vfio_ap
  device driver, the structures representing the queue device and the
  matrix mdev will be unlinked.

Signed-off-by: Tony Krowiak 

Reviewed-by: Halil Pasic 


---
  drivers/s390/crypto/vfio_ap_ops.c | 140 +-
  drivers/s390/crypto/vfio_ap_private.h |   3 +
  2 files changed, 117 insertions(+), 26 deletions(-)

diff --git a/drivers/s390/crypto/vfio_ap_ops.c 
b/drivers/s390/crypto/vfio_ap_ops.c
index 835c963ae16d..cdcc6378b4a5 100644
--- a/drivers/s390/crypto/vfio_ap_ops.c
+++ b/drivers/s390/crypto/vfio_ap_ops.c
@@ -27,33 +27,17 @@
  static int vfio_ap_mdev_reset_queues(struct mdev_device *mdev);
  static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
  
-/**

- * vfio_ap_get_queue: Retrieve a queue with a specific APQN from a list
- * @matrix_mdev: the associated mediated matrix
- * @apqn: The queue APQN
- *
- * Retrieve a queue with a specific APQN from the list of the
- * devices of the vfio_ap_drv.
- * Verify that the APID and the APQI are set in the matrix.
- *
- * Returns the pointer to the associated vfio_ap_queue
- */
-static struct vfio_ap_queue *vfio_ap_get_queue(
-   struct ap_matrix_mdev *matrix_mdev,
-   int apqn)
+static struct vfio_ap_queue *
+vfio_ap_mdev_get_queue(struct ap_matrix_mdev *matrix_mdev, unsigned long apqn)
  {
-   struct vfio_ap_queue *q = NULL;
-
-   if (!test_bit_inv(AP_QID_CARD(apqn), matrix_mdev->matrix.apm))
-   return NULL;
-   if (!test_bit_inv(AP_QID_QUEUE(apqn), matrix_mdev->matrix.aqm))
-   return NULL;
+   struct vfio_ap_queue *q;
  
-	q = vfio_ap_find_queue(apqn);

-   if (q)
-   q->matrix_mdev = matrix_mdev;
+   hash_for_each_possible(matrix_mdev->qtable, q, mdev_qnode, apqn) {
+   if (q && (q->apqn == apqn))
+   return q;
+   }
  
-	return q;

+   return NULL;
  }
  
  /**

@@ -166,7 +150,6 @@ static struct ap_queue_status vfio_ap_irq_disable(struct 
vfio_ap_queue *q)
  status.response_code);
  end_free:
vfio_ap_free_aqic_resources(q);
-   q->matrix_mdev = NULL;
return status;
  }
  
@@ -282,7 +265,7 @@ static int handle_pqap(struct kvm_vcpu *vcpu)

matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
   struct ap_matrix_mdev, pqap_hook);
  
-	q = vfio_ap_get_queue(matrix_mdev, apqn);

+   q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
if (!q)
goto out_unlock;
  
@@ -325,6 +308,7 @@ static int vfio_ap_mdev_create(struct kobject *kobj, struct mdev_device *mdev)
  
  	matrix_mdev->mdev = mdev;

vfio_ap_matrix_init(_dev->info, _mdev->matrix);
+   hash_init(matrix_mdev->qtable);
mdev_set_drvdata(mdev, matrix_mdev);
matrix_mdev->pqap_hook.hook = handle_pqap;
matrix_mdev->pqap_hook.owner = THIS_MODULE;
@@ -553,6 +537,50 @@ static int vfio_ap_mdev_verify_no_sharing(struct 
ap_matrix_mdev *matrix_mdev)
return 0;
  }
  
+static void vfio_ap_mdev_link_queue(struct ap_matrix_mdev *matrix_mdev,

+   struct vfio_ap_queue *q)
+{
+   if (q) {
+   q->matrix_mdev = matrix_mdev;
+   hash_add(matrix_mdev->qtable,
+>mdev_qnode, q->apqn);
+   }
+}
+
+static void vfio_ap_mdev_link_apqn(struct ap_matrix_mdev *matrix_mdev, int 
apqn)
+{
+   struct vfio_ap_queue *q;
+
+   q = vfio_ap_find_queue(apqn);
+   vfio_ap_mdev_link_queue(matrix_mdev, q);
+}
+
+static void vfio_ap_mdev_unlink_queue(struct vfio_ap_queue *q)
+{
+   if (q) {
+   q->matrix_mdev = NULL;
+  

Re: [PATCH v13 05/15] s390/vfio-ap: manage link between queue struct and matrix mdev

2021-01-11 Thread Halil Pasic
On Tue, 22 Dec 2020 20:15:56 -0500
Tony Krowiak  wrote:

> Let's create links between each queue device bound to the vfio_ap device
> driver and the matrix mdev to which the queue's APQN is assigned. The idea
> is to facilitate efficient retrieval of the objects representing the queue
> devices and matrix mdevs as well as to verify that a queue assigned to
> a matrix mdev is bound to the driver.
> 
> The links will be created as follows:
> 
>* When the queue device is probed, if its APQN is assigned to a matrix
>  mdev, the structures representing the queue device and the matrix mdev
>  will be linked.
> 
>* When an adapter or domain is assigned to a matrix mdev, for each new
>  APQN assigned that references a queue device bound to the vfio_ap
>  device driver, the structures representing the queue device and the
>  matrix mdev will be linked.
> 
> The links will be removed as follows:
> 
>* When the queue device is removed, if its APQN is assigned to a matrix
>  mdev, the structures representing the queue device and the matrix mdev
>  will be unlinked.
> 
>* When an adapter or domain is unassigned from a matrix mdev, for each
>  APQN unassigned that references a queue device bound to the vfio_ap
>  device driver, the structures representing the queue device and the
>  matrix mdev will be unlinked.
> 
> Signed-off-by: Tony Krowiak 

Reviewed-by: Halil Pasic 

> ---
>  drivers/s390/crypto/vfio_ap_ops.c | 140 +-
>  drivers/s390/crypto/vfio_ap_private.h |   3 +
>  2 files changed, 117 insertions(+), 26 deletions(-)
> 
> diff --git a/drivers/s390/crypto/vfio_ap_ops.c 
> b/drivers/s390/crypto/vfio_ap_ops.c
> index 835c963ae16d..cdcc6378b4a5 100644
> --- a/drivers/s390/crypto/vfio_ap_ops.c
> +++ b/drivers/s390/crypto/vfio_ap_ops.c
> @@ -27,33 +27,17 @@
>  static int vfio_ap_mdev_reset_queues(struct mdev_device *mdev);
>  static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
>  
> -/**
> - * vfio_ap_get_queue: Retrieve a queue with a specific APQN from a list
> - * @matrix_mdev: the associated mediated matrix
> - * @apqn: The queue APQN
> - *
> - * Retrieve a queue with a specific APQN from the list of the
> - * devices of the vfio_ap_drv.
> - * Verify that the APID and the APQI are set in the matrix.
> - *
> - * Returns the pointer to the associated vfio_ap_queue
> - */
> -static struct vfio_ap_queue *vfio_ap_get_queue(
> - struct ap_matrix_mdev *matrix_mdev,
> - int apqn)
> +static struct vfio_ap_queue *
> +vfio_ap_mdev_get_queue(struct ap_matrix_mdev *matrix_mdev, unsigned long 
> apqn)
>  {
> - struct vfio_ap_queue *q = NULL;
> -
> - if (!test_bit_inv(AP_QID_CARD(apqn), matrix_mdev->matrix.apm))
> - return NULL;
> - if (!test_bit_inv(AP_QID_QUEUE(apqn), matrix_mdev->matrix.aqm))
> - return NULL;
> + struct vfio_ap_queue *q;
>  
> - q = vfio_ap_find_queue(apqn);
> - if (q)
> - q->matrix_mdev = matrix_mdev;
> + hash_for_each_possible(matrix_mdev->qtable, q, mdev_qnode, apqn) {
> + if (q && (q->apqn == apqn))
> + return q;
> + }
>  
> - return q;
> + return NULL;
>  }
>  
>  /**
> @@ -166,7 +150,6 @@ static struct ap_queue_status vfio_ap_irq_disable(struct 
> vfio_ap_queue *q)
> status.response_code);
>  end_free:
>   vfio_ap_free_aqic_resources(q);
> - q->matrix_mdev = NULL;
>   return status;
>  }
>  
> @@ -282,7 +265,7 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
>   matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
>  struct ap_matrix_mdev, pqap_hook);
>  
> - q = vfio_ap_get_queue(matrix_mdev, apqn);
> + q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
>   if (!q)
>   goto out_unlock;
>  
> @@ -325,6 +308,7 @@ static int vfio_ap_mdev_create(struct kobject *kobj, 
> struct mdev_device *mdev)
>  
>   matrix_mdev->mdev = mdev;
>   vfio_ap_matrix_init(_dev->info, _mdev->matrix);
> + hash_init(matrix_mdev->qtable);
>   mdev_set_drvdata(mdev, matrix_mdev);
>   matrix_mdev->pqap_hook.hook = handle_pqap;
>   matrix_mdev->pqap_hook.owner = THIS_MODULE;
> @@ -553,6 +537,50 @@ static int vfio_ap_mdev_verify_no_sharing(struct 
> ap_matrix_mdev *matrix_mdev)
>   return 0;
>  }
>  
> +static void vfio_ap_mdev_link_queue(struct ap_matrix_mdev *matrix_mdev,
> + struct vfio_ap_queue *q)
> +{
> + if (q) {
> + q->matrix_mdev = matrix_mdev;
> + hash_add(matrix_mdev->qtable,
> +  >mdev_qnode, q->apqn);
> + }
> +}
> +
> +static void vfio_ap_mdev_link_apqn(struct ap_matrix_mdev *matrix_mdev, int 
> apqn)
> +{
> + struct vfio_ap_queue *q;
> +
> + q = vfio_ap_find_queue(apqn);
> + vfio_ap_mdev_link_queue(matrix_mdev, q);
> +}
> +
> +static void 

[PATCH v13 05/15] s390/vfio-ap: manage link between queue struct and matrix mdev

2020-12-22 Thread Tony Krowiak
Let's create links between each queue device bound to the vfio_ap device
driver and the matrix mdev to which the queue's APQN is assigned. The idea
is to facilitate efficient retrieval of the objects representing the queue
devices and matrix mdevs as well as to verify that a queue assigned to
a matrix mdev is bound to the driver.

The links will be created as follows:

   * When the queue device is probed, if its APQN is assigned to a matrix
 mdev, the structures representing the queue device and the matrix mdev
 will be linked.

   * When an adapter or domain is assigned to a matrix mdev, for each new
 APQN assigned that references a queue device bound to the vfio_ap
 device driver, the structures representing the queue device and the
 matrix mdev will be linked.

The links will be removed as follows:

   * When the queue device is removed, if its APQN is assigned to a matrix
 mdev, the structures representing the queue device and the matrix mdev
 will be unlinked.

   * When an adapter or domain is unassigned from a matrix mdev, for each
 APQN unassigned that references a queue device bound to the vfio_ap
 device driver, the structures representing the queue device and the
 matrix mdev will be unlinked.

Signed-off-by: Tony Krowiak 
---
 drivers/s390/crypto/vfio_ap_ops.c | 140 +-
 drivers/s390/crypto/vfio_ap_private.h |   3 +
 2 files changed, 117 insertions(+), 26 deletions(-)

diff --git a/drivers/s390/crypto/vfio_ap_ops.c 
b/drivers/s390/crypto/vfio_ap_ops.c
index 835c963ae16d..cdcc6378b4a5 100644
--- a/drivers/s390/crypto/vfio_ap_ops.c
+++ b/drivers/s390/crypto/vfio_ap_ops.c
@@ -27,33 +27,17 @@
 static int vfio_ap_mdev_reset_queues(struct mdev_device *mdev);
 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
 
-/**
- * vfio_ap_get_queue: Retrieve a queue with a specific APQN from a list
- * @matrix_mdev: the associated mediated matrix
- * @apqn: The queue APQN
- *
- * Retrieve a queue with a specific APQN from the list of the
- * devices of the vfio_ap_drv.
- * Verify that the APID and the APQI are set in the matrix.
- *
- * Returns the pointer to the associated vfio_ap_queue
- */
-static struct vfio_ap_queue *vfio_ap_get_queue(
-   struct ap_matrix_mdev *matrix_mdev,
-   int apqn)
+static struct vfio_ap_queue *
+vfio_ap_mdev_get_queue(struct ap_matrix_mdev *matrix_mdev, unsigned long apqn)
 {
-   struct vfio_ap_queue *q = NULL;
-
-   if (!test_bit_inv(AP_QID_CARD(apqn), matrix_mdev->matrix.apm))
-   return NULL;
-   if (!test_bit_inv(AP_QID_QUEUE(apqn), matrix_mdev->matrix.aqm))
-   return NULL;
+   struct vfio_ap_queue *q;
 
-   q = vfio_ap_find_queue(apqn);
-   if (q)
-   q->matrix_mdev = matrix_mdev;
+   hash_for_each_possible(matrix_mdev->qtable, q, mdev_qnode, apqn) {
+   if (q && (q->apqn == apqn))
+   return q;
+   }
 
-   return q;
+   return NULL;
 }
 
 /**
@@ -166,7 +150,6 @@ static struct ap_queue_status vfio_ap_irq_disable(struct 
vfio_ap_queue *q)
  status.response_code);
 end_free:
vfio_ap_free_aqic_resources(q);
-   q->matrix_mdev = NULL;
return status;
 }
 
@@ -282,7 +265,7 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
   struct ap_matrix_mdev, pqap_hook);
 
-   q = vfio_ap_get_queue(matrix_mdev, apqn);
+   q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
if (!q)
goto out_unlock;
 
@@ -325,6 +308,7 @@ static int vfio_ap_mdev_create(struct kobject *kobj, struct 
mdev_device *mdev)
 
matrix_mdev->mdev = mdev;
vfio_ap_matrix_init(_dev->info, _mdev->matrix);
+   hash_init(matrix_mdev->qtable);
mdev_set_drvdata(mdev, matrix_mdev);
matrix_mdev->pqap_hook.hook = handle_pqap;
matrix_mdev->pqap_hook.owner = THIS_MODULE;
@@ -553,6 +537,50 @@ static int vfio_ap_mdev_verify_no_sharing(struct 
ap_matrix_mdev *matrix_mdev)
return 0;
 }
 
+static void vfio_ap_mdev_link_queue(struct ap_matrix_mdev *matrix_mdev,
+   struct vfio_ap_queue *q)
+{
+   if (q) {
+   q->matrix_mdev = matrix_mdev;
+   hash_add(matrix_mdev->qtable,
+>mdev_qnode, q->apqn);
+   }
+}
+
+static void vfio_ap_mdev_link_apqn(struct ap_matrix_mdev *matrix_mdev, int 
apqn)
+{
+   struct vfio_ap_queue *q;
+
+   q = vfio_ap_find_queue(apqn);
+   vfio_ap_mdev_link_queue(matrix_mdev, q);
+}
+
+static void vfio_ap_mdev_unlink_queue(struct vfio_ap_queue *q)
+{
+   if (q) {
+   q->matrix_mdev = NULL;
+   hash_del(>mdev_qnode);
+   }
+}
+
+static void vfio_ap_mdev_unlink_apqn(int apqn)
+{
+   struct vfio_ap_queue *q;
+
+   q =