Skip to contentSkip to conversation

vfio: ap: AP Queue Interrupt Control

PATCH Superseded Pierre Morel 3 participants 29 messages Mar 13, 2019 – Mar 21, 2019

Subsystems (5)

v5 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v5 cover vfio: ap: AP Queue Interrupt Control

This patch implement PQAP/AQIC interception in KVM.

To implement this we need to add a new structure, vfio_ap_queue,to be able to retrieve the mediated device associated with a queue and specific values needed to register/unregister the interrupt structures:
- APQN: to be able to issue the commands and search for queue structures
- NIB : to unpin the NIB on clear IRQ
- ISC : to unregister with the GIB interface
- MATRIX: a pointer to the matrix mediated device
- LIST: the list_head to handle the vfio_queue life cycle

Having this structure and the list management greatly ease the handling of the AP queues and diminues the LOCs needed in the vfio_ap driver by more than 150 lines in comparison with the previous version.

0) Queues life cycle

vfio_ap_queues are created on probe

We define one bucket on the matrix device to store the free vfio_ap_queues, the queues not assign to any matrix mediated device.

We define one bucket on each matrix mediated device to hold the vfio_ap_queues belonging to it.

vfio_ap_queues are deleted on remove

This makes the search for a queue easy and the detection of assignent incoherency obvious (the queue is not avilable) and simplifies assignment.

1) Phase 1, probe and remove from vfio_ap_queue

The vfio_ap_queue structures are dynamically allocated and setup when a queue is probed by the ap_vfio_driver.
The vfio_ap_queue is linked to the ap_queue device as the driver data.

The new The vfio_ap_queue is put on a free_list belonging to the matrix device.

The vfio_ap_queue are free during remove.

2) Phase 2, assignment of vfio_ap_queue to a mediated device

When a APID is assigned we look for APQI already assigned to the matrix mediated device and associate all the queue with the APQN = (APID,APQI) to the mediated device by adding them to the mediated device queue list.
We do the same when a APQI is assigned.

If any queue with a matching APQN can not be found on the matrix device free list it means it is already associated to another matrix mediated device and no queue is added to the matrix mediated device.

3) Phase 3, starting the guest

When the VFIO device is opened the PQAP callback and a pointer to the matrix mediated device are set inside KVM during the open callback.

When the device is closed or if a queue is removed, the vfio_ap_queue is dissociated from the mediated device.

4) Phase 3 intercepting the PQAP/AQIC instruction

On interception of the PQAP/AQIC instruction, the interception code makes sure the pqap_hook is initialized and allowed to be called and call it.
Otherwise it reports the usual -EOPNOTSUPP return code to let QEMU handle the fault.

the pqap callback search for the queue asociated with the APQN stored in the register 0, setting the code to "illegal APQN" if the vfio_ap_queue can not be found.

Depending on the "i" bit of the register 1, the pqap callback setup or clear the interruption by calling the host format PQAP/AQIC instruction.
When seting up the interruption it uses the NIB and the guest ISC provided by the guest and the host ISC provided by the registration to the GIB code, pin the NIB and also stores ISC and NIB inside the vfio_ap_queue structure.
When clearing the interrupt it retrieves the host ISC to unregister with the GIB code and unpin the NIB.

We take care when enabling GISA that the guest may have issued a reset and will not need to disable the interuptions before re-enabling interruptions.

To make sure that the module holding the callback does not disapear we use a module reference counting in the structure containing the callback.

5) Phase 4 clean dissociation from the mediated device on remove

On removing of the AP device the remove callback is called. To be sure that the guest will not access the queue anymore we clear the APID CRYCB bit.
Cleaning the APID, over the APQI, is chosen because the architecture specifies that only the APID can be dynamically changed outside IPL.

To be sure that the IRQ is cleared before the GISA is free we use the KVM reference counting, raise it in open, lower it on release.

6) Associated QEMU patch

There is a QEMU patch which is needed to enable the PQAP/AQIC facility in the guest.

Posted in [email protected] as:
Message-Id: <[email protected]>

Pierre Morel (7):
s390: ap: kvm: add PQAP interception for AQIC
s390: ap: new vfio_ap_queue structure
vfio: ap: register IOMMU VFIO notifier
s390: ap: setup relation betwen KVM and mediated device
s390: ap: implement PAPQ AQIC interception in kernel
s390: ap: Cleanup on removing the AP device
s390: ap: kvm: Enable PQAP/AQIC facility for the guest

 arch/s390/include/asm/kvm_host.h      |   8 +
 arch/s390/kvm/priv.c                  |  62 +++
 arch/s390/tools/gen_facilities.c      |   1 +
 drivers/s390/crypto/ap_bus.h          |   1 +
 drivers/s390/crypto/vfio_ap_drv.c     |  69 +++-
 drivers/s390/crypto/vfio_ap_ops.c     | 727 +++++++++++++++++++++++-----------
 drivers/s390/crypto/vfio_ap_private.h |  20 +
 7 files changed, 658 insertions(+), 230 deletions(-)
Signature · 31 lines
2.7.4

Changelog from v4:
- Add forgotten locking for vfio_get_queue() in pqap callback
  (Conny / Halil)
- Add KVM reference counting to make sure GISA is free after IRQ
  (Christian / Halil)
- Take care that ISC = 0 is a valid ISC
  (Halil)
- Integrate the PQAP call back in a structure with module owner
  reference counting to make sure the callback does not disappear.
- Restrict functionality to always open KVM before opening the
  VFIO device.
- Search all devices in the vfio_ap driver list when associating
  a queue to a mediated device
  (Halil / Tony)
- Get vfio_ap_free_irq() out of vfio_ap_mdev_reset_queue() to call
  it always, whatever the result of the reset.
  (Tony)

Changelog from v3:
- Associating the vfio_queues during APID/APQI assign
  (Tony)
- Dissociating the vfio_queues during APID/APQI unassign
  (Tony)
- Taking care that the guest can directly disable the interrupt
  by using a RESET
  (Halil)
- Remove the patch creating the matrix bus to accelerate its
  integration in Linux stable
  (Christian)
#1 lore raw

PATCH v5 1/7 s390: ap: kvm: add PQAP interception for AQIC

We prepare the interception of the PQAP/AQIC instruction for the case the AQIC facility is enabled in the guest.

We add a callback inside the KVM arch structure for s390 for a VFIO driver to handle a specific response to the PQAP instruction with the AQIC command and only this command. The preceding behavior for other commands should not change.

We inject the correct exceptions from inside KVM for the case the callback is not initialized, which happens when the vfio_ap driver is not loaded.

It is the duty of the vfio_driver to setup a pqap callback inside the crypto structure.
If the callback has been setup we call it.
If not we setup an answer considering that no queue is available for the guest when no callback has been setup.

We do consider the responsability of the driver to always initialize the PQAP callback if it defines queues by initializing the CRYCB for a guest.

---
 arch/s390/include/asm/kvm_host.h      |  8 +++++
 arch/s390/kvm/priv.c                  | 62 +++++++++++++++++++++++++++++++++++
 drivers/s390/crypto/vfio_ap_private.h |  2 ++
 3 files changed, 72 insertions(+)
arch/s390/include/asm/kvm_host.h +8 -0
index a496276..624460b 100644
@@ -18,6 +18,7 @@
 #include <linux/kvm_host.h>
 #include <linux/kvm.h>
 #include <linux/seqlock.h>
+#include <linux/module.h>
 #include <asm/debug.h>
 #include <asm/cpu.h>
 #include <asm/fpu/api.h>
@@ -721,8 +722,15 @@ struct kvm_s390_cpu_model {
 	unsigned short ibc;
 };
 
+struct kvm_s390_module_hook {
+	int (*hook)(struct kvm_vcpu *vcpu);
+	void *data;
+	struct module *owner;
+};
+
 struct kvm_s390_crypto {
 	struct kvm_s390_crypto_cb *crycb;
+	struct kvm_s390_module_hook *pqap_hook;
 	__u32 crycbd;
 	__u8 aes_kw;
 	__u8 dea_kw;
arch/s390/kvm/priv.c +62 -0

80 lines not on this page · raw message

drivers/s390/crypto/vfio_ap_private.h +2 -0
index 76b7f98..a910be1 100644
@@ -16,6 +16,7 @@
 #include <linux/mdev.h>
 #include <linux/delay.h>
 #include <linux/mutex.h>
+#include <linux/kvm_host.h>
 
 #include "ap_bus.h"
 
@@ -81,6 +82,7 @@ struct ap_matrix_mdev {
 	struct ap_matrix matrix;
 	struct notifier_block group_notifier;
 	struct kvm *kvm;
+	struct kvm_s390_module_hook pqap_hook;
 };
 
 extern int vfio_ap_mdev_register(void);
Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH v5 1/7] s390: ap: kvm: add PQAP interception for AQIC

Quoted 45 lines · On Wed, 13 Mar 2019 17:04:58 +0100 Pierre Morel <[email protected]> wrote:
arch/s390/kvm/priv.c +45 -0
+/*
+ * handle_pqap: Handling pqap interception
+ * @vcpu: the vcpu having issue the pqap instruction
+ *
+ * We now support PQAP/AQIC instructions and we need to correctly
+ * answer the guest even if no dedicated driver's hook is available.
+ *
+ * The intercepting code calls a dedicated callback for this instruction
+ * if a driver did register one in the CRYPTO satellite of the
+ * SIE block.
+ *
+ * For PQAP/AQIC instructions only, verify privilege and specifications.
+ *
+ * If no callback available, the queues are not available, return this to
+ * the caller.
+ * Else return the value returned by the callback.
+ */
+static int handle_pqap(struct kvm_vcpu *vcpu)
+{
+	uint8_t fc;
+	struct ap_queue_status status = {};
+	int ret;
+	/* Verify that the AP instruction are available */
+	if (!ap_instructions_available())
+		return -EOPNOTSUPP;
+	/* Verify that the guest is allowed to use AP instructions */
+	if (!(vcpu->arch.sie_block->eca & ECA_APIE))
+		return -EOPNOTSUPP;
+	/* Verify that the function code is AQIC */
+	fc = vcpu->run->s.regs.gprs[0] >> 24;
+	/* We do not want to change the behavior we had before this patch*/
+	if (fc != 0x03)
+		return -EOPNOTSUPP;
+
+	/* PQAP instructions are allowed for guest kernel only */
+	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
+		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
+	/* AQIC instruction is allowed only if facility 65 is available */
+	if (!test_kvm_facility(vcpu->kvm, 65))
+		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
+	/* Verify that the hook callback is registered and call it */
+	if (vcpu->kvm->arch.crypto.pqap_hook) {
+		if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
+			return -EOPNOTSUPP;
+		ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);

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

Quoted 10 lines
arch/s390/kvm/priv.c +10 -0
+		module_put(vcpu->kvm->arch.crypto.pqap_hook->owner);
+		return ret;
+	}
+	/*
+	 * It is the duty of the vfio_driver to register a hook
+	 * If it does not and we get an exception on AQIC we must
+	 * guess that there is no vfio_ap_driver at all and no one
+	 * to handle the guests's CRYCB and the CRYCB is empty.
+	 */
+	status.response_code = 0x01;

I'm still confused here, sorry. From previous discussions I recall that this indicates "no crypto device" (please correct me if I'm wrong.)

Before this patch, we had:
- guest issues PQAP/AQIC -> drop to userspace

With a correct implementation, we get:
- guest issues PQAP/AQIC -> callback does what needs to be done

With an incorrect implementation (no callback), we get:
- guest issues PQAP/AQIC -> guest gets response code 0x01

Why not drop to userspace in that case?

Quoted 4 lines
arch/s390/kvm/priv.c +4 -0
+	memcpy(&vcpu->run->s.regs.gprs[1], &status, sizeof(status));
+	return 0;
+}
+
#3 lore raw

Re: [PATCH v5 1/7] s390: ap: kvm: add PQAP interception for AQIC

Quoted 65 lines · Cornelia Huck
Quoted 45 lines · On Wed, 13 Mar 2019 17:04:58 +0100 Pierre Morel <[email protected]> wrote:
arch/s390/kvm/priv.c +45 -0
+/*
+ * handle_pqap: Handling pqap interception
+ * @vcpu: the vcpu having issue the pqap instruction
+ *
+ * We now support PQAP/AQIC instructions and we need to correctly
+ * answer the guest even if no dedicated driver's hook is available.
+ *
+ * The intercepting code calls a dedicated callback for this instruction
+ * if a driver did register one in the CRYPTO satellite of the
+ * SIE block.
+ *
+ * For PQAP/AQIC instructions only, verify privilege and specifications.
+ *
+ * If no callback available, the queues are not available, return this to
+ * the caller.
+ * Else return the value returned by the callback.
+ */
+static int handle_pqap(struct kvm_vcpu *vcpu)
+{
+	uint8_t fc;
+	struct ap_queue_status status = {};
+	int ret;
+	/* Verify that the AP instruction are available */
+	if (!ap_instructions_available())
+		return -EOPNOTSUPP;
+	/* Verify that the guest is allowed to use AP instructions */
+	if (!(vcpu->arch.sie_block->eca & ECA_APIE))
+		return -EOPNOTSUPP;
+	/* Verify that the function code is AQIC */
+	fc = vcpu->run->s.regs.gprs[0] >> 24;
+	/* We do not want to change the behavior we had before this patch*/
+	if (fc != 0x03)
+		return -EOPNOTSUPP;
+
+	/* PQAP instructions are allowed for guest kernel only */
+	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
+		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
+	/* AQIC instruction is allowed only if facility 65 is available */
+	if (!test_kvm_facility(vcpu->kvm, 65))
+		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
+	/* Verify that the hook callback is registered and call it */
+	if (vcpu->kvm->arch.crypto.pqap_hook) {
+		if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
+			return -EOPNOTSUPP;
+		ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);

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

Quoted 10 lines
arch/s390/kvm/priv.c +10 -0
+		module_put(vcpu->kvm->arch.crypto.pqap_hook->owner);
+		return ret;
+	}
+	/*
+	 * It is the duty of the vfio_driver to register a hook
+	 * If it does not and we get an exception on AQIC we must
+	 * guess that there is no vfio_ap_driver at all and no one
+	 * to handle the guests's CRYCB and the CRYCB is empty.
+	 */
+	status.response_code = 0x01;

I'm still confused here, sorry. From previous discussions I recall that this indicates "no crypto device" (please correct me if I'm wrong.)

Before this patch, we had:
- guest issues PQAP/AQIC -> drop to userspace

Quoted 7 lines

With a correct implementation, we get:
- guest issues PQAP/AQIC -> callback does what needs to be done

With an incorrect implementation (no callback), we get:
- guest issues PQAP/AQIC -> guest gets response code 0x01

Why not drop to userspace in that case?

This is what I had in the previous patches.
Hum, I do not remember which discussion lead me to modify this.

Anyway, now that you put the finger on this problem, I think the problem is worse.

The behavior with old / new Linux, vfio driver and qemu is:

LINUX VFIO_AP QEMU PGM
OLD x x OPERATION
NEW - OLD SPECIFICATION
NEW - NEW/aqic=off SPECIFICATION
NEW x NEW/aqic=on -

x = whatever
- = absent/none

So yes there is a change in behavior for the userland for the case QEMU do not set the AQIC facility 65, OLD QEMU or NEW QEMU wanting to behave like an older one.

I fear we have the same problem with the privileged operation...

For the last case, when the kvm_facility(65) is set, the explication is the following:

This is related to the handling of PQAP AQIC which is now authorized by this patch series.
If we authorize PQAP AQIC, by setting the bit for facility 65, the guest can use this instruction.
If the instruction follows the specifications we must answer something realistic and since there is nothing in the CRYCB (no driver) we answer that there is no queue.

Conclusion: we must handle this in userland, it will have the benefit to keep old behavior when there is no callback. OLD QEMU will not see change as they will not set aqic facility
NEW QEMU will handle this correctly.

In this case we also do not need to handle all other tests here but can move it to the callback as Tony wanted.

Would you agree with something simple like:

static int handle_pqap(struct kvm_vcpu *vcpu)

{
        int ret = -EOPNOTSUPP;

        /* Verify that the hook callback is registered and call it */
        if (pqap_hook)
                if (try_module_get(pqap_hook->owner)) {
                        ret = pqap_hook->hook(vcpu);
                        module_put(pqap_hook->owner);
                }
        return ret;
}

All other tests in QEMU and in the callback.

Thanks for the comments.

Regards,
Pierre

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#4 lore raw

Re: [PATCH v5 1/7] s390: ap: kvm: add PQAP interception for AQIC

Quoted 23 lines · On Fri, 15 Mar 2019 14:26:34 +0100 Pierre Morel <[email protected]> wrote:

Conclusion: we must handle this in userland, it will have the benefit to keep old behavior when there is no callback.
OLD QEMU will not see change as they will not set aqic facility NEW QEMU will handle this correctly.

In this case we also do not need to handle all other tests here but can move it to the callback as Tony wanted.

Would you agree with something simple like:

static int handle_pqap(struct kvm_vcpu *vcpu)

{
         int ret = -EOPNOTSUPP;

         /* Verify that the hook callback is registered and call it */
         if (pqap_hook)
                 if (try_module_get(pqap_hook->owner)) {
                         ret = pqap_hook->hook(vcpu);
                         module_put(pqap_hook->owner);
                 }
         return ret;
}
Quoted 1 line

All other tests in QEMU and in the callback.

With the hook checking for priv, fc, etc.? Yeah, might work.

But don't count on my feedback too much right now, better wait for others' comments :) I'll resume in April, if needed.

#5 lore raw

Re: [PATCH v5 1/7] s390: ap: kvm: add PQAP interception for AQIC

Quoted 26 lines · Cornelia Huck
Quoted 23 lines · On Fri, 15 Mar 2019 14:26:34 +0100 Pierre Morel <[email protected]> wrote:

Conclusion: we must handle this in userland, it will have the benefit to keep old behavior when there is no callback.
OLD QEMU will not see change as they will not set aqic facility NEW QEMU will handle this correctly.

In this case we also do not need to handle all other tests here but can move it to the callback as Tony wanted.

Would you agree with something simple like:

static int handle_pqap(struct kvm_vcpu *vcpu)

{
          int ret = -EOPNOTSUPP;

          /* Verify that the hook callback is registered and call it */
          if (pqap_hook)
                  if (try_module_get(pqap_hook->owner)) {
                          ret = pqap_hook->hook(vcpu);
                          module_put(pqap_hook->owner);
                  }
          return ret;
}
Quoted 7 lines
Quoted 1 line

All other tests in QEMU and in the callback.

With the hook checking for priv, fc, etc.? Yeah, might work.

But don't count on my feedback too much right now, better wait for others' comments :) I'll resume in April, if needed.

OK, thanks

Pierre

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#6 lore raw

Re: [PATCH v5 1/7] s390: ap: kvm: add PQAP interception for AQIC

In reply to Pierre Morel
Quoted 99 lines · Pierre Morel
Quoted 68 lines · Cornelia Huck
Quoted 48 lines · On Wed, 13 Mar 2019 17:04:58 +0100 Pierre Morel <[email protected]> wrote:

+/*
+ * handle_pqap: Handling pqap interception
+ * @vcpu: the vcpu having issue the pqap instruction
+ *
+ * We now support PQAP/AQIC instructions and we need to correctly
+ * answer the guest even if no dedicated driver's hook is available.
+ *
+ * The intercepting code calls a dedicated callback for this instruction
+ * if a driver did register one in the CRYPTO satellite of the
+ * SIE block.
+ *
+ * For PQAP/AQIC instructions only, verify privilege and specifications.
+ *
+ * If no callback available, the queues are not available, return this to
+ * the caller.
+ * Else return the value returned by the callback.
+ */

arch/s390/kvm/priv.c +28 -0
+static int handle_pqap(struct kvm_vcpu *vcpu)
+{
+    uint8_t fc;
+    struct ap_queue_status status = {};
+    int ret;
+    /* Verify that the AP instruction are available */
+    if (!ap_instructions_available())
+        return -EOPNOTSUPP;
+    /* Verify that the guest is allowed to use AP instructions */
+    if (!(vcpu->arch.sie_block->eca & ECA_APIE))
+        return -EOPNOTSUPP;
+    /* Verify that the function code is AQIC */
+    fc = vcpu->run->s.regs.gprs[0] >> 24;
+    /* We do not want to change the behavior we had before this patch*/
+    if (fc != 0x03)
+        return -EOPNOTSUPP;
+
+    /* PQAP instructions are allowed for guest kernel only */
+    if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
+        return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
+    /* AQIC instruction is allowed only if facility 65 is available */
+    if (!test_kvm_facility(vcpu->kvm, 65))
+        return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
+    /* Verify that the hook callback is registered and call it */
+    if (vcpu->kvm->arch.crypto.pqap_hook) {
+        if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
+            return -EOPNOTSUPP;
+        ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);

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

Quoted 10 lines
arch/s390/kvm/priv.c +10 -0
+        module_put(vcpu->kvm->arch.crypto.pqap_hook->owner);
+        return ret;
+    }
+    /*
+     * It is the duty of the vfio_driver to register a hook
+     * If it does not and we get an exception on AQIC we must
+     * guess that there is no vfio_ap_driver at all and no one
+     * to handle the guests's CRYCB and the CRYCB is empty.
+     */
+    status.response_code = 0x01;

I'm still confused here, sorry. From previous discussions I recall that this indicates "no crypto device" (please correct me if I'm wrong.)

Before this patch, we had:
- guest issues PQAP/AQIC -> drop to userspace

Quoted 7 lines

With a correct implementation, we get:
- guest issues PQAP/AQIC -> callback does what needs to be done

With an incorrect implementation (no callback), we get:
- guest issues PQAP/AQIC -> guest gets response code 0x01

Why not drop to userspace in that case?

This is what I had in the previous patches.
Hum, I do not remember which discussion lead me to modify this.

Anyway, now that you put the finger on this problem, I think the problem is worse.

The behavior with old / new Linux, vfio driver and qemu is:

LINUX    VFIO_AP    QEMU    PGM
OLD    x    x    OPERATION
NEW    -    OLD    SPECIFICATION
NEW    -    NEW/aqic=off    SPECIFICATION
NEW    x    NEW/aqic=on    -

x = whatever
- = absent/none

So yes there is a change in behavior for the userland for the case QEMU do not set the AQIC facility 65, OLD QEMU or NEW QEMU wanting to behave like an older one.

I fear we have the same problem with the privileged operation...

For the last case, when the kvm_facility(65) is set, the explication is the following:

Quoted 8 lines

This is related to the handling of PQAP AQIC which is now authorized by this patch series.
If we authorize PQAP AQIC, by setting the bit for facility 65, the guest can use this instruction.
If the instruction follows the specifications we must answer something realistic and since there is nothing in the CRYCB (no driver) we answer that there is no queue.

Conclusion:  we must handle this in userland, it will have the benefit to keep old behavior when there is no callback. OLD QEMU will not see change as they will not set aqic facility
NEW QEMU will handle this correctly.

Sorry, wrong conclusion, handling this in userland will bring us much too far if we want to answer correctly for the case the hook is not there but QEMU accepted the facility for AQIC.

The alternative is easier, we just continue to respond with the OPERATION exception here and only handle the specification and privileged exception cases in QEMU and in the hook.

So, I think the discussion will go on until you come back :)

Regards,
Pierre

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
maintainer
#7 lore raw

Re: [PATCH v5 1/7] s390: ap: kvm: add PQAP interception for AQIC

Quoted 7 lines · On Fri, 15 Mar 2019 15:10:25 +0100 Pierre Morel <[email protected]> wrote:

Sorry, wrong conclusion, handling this in userland will bring us much too far if we want to answer correctly for the case the hook is not there but QEMU accepted the facility for AQIC.

The alternative is easier, we just continue to respond with the OPERATION exception here and only handle the specification and privileged exception cases in QEMU and in the hook.

I don't quite understand what do you mean by this paragraph. Especially not what do you mean by 'just continue to respond with the OPERATION exception here'.

In any case if the guest is supposed to have ap instructions, and does not have facility 65 the right answer is specification and not operation exception. And this has to work regardless of vfio-ap module loaded or not.

Regards,
Halil

Quoted 5 lines

So, I think the discussion will go on until you come back :)

Regards,
Pierre

#8 lore raw

Re: [PATCH v5 1/7] s390: ap: kvm: add PQAP interception for AQIC

In reply to Pierre Morel
Quoted 105 lines · Pierre Morel
Quoted 104 lines · Pierre Morel
Quoted 69 lines · Cornelia Huck
Quoted 51 lines · On Wed, 13 Mar 2019 17:04:58 +0100 Pierre Morel <[email protected]> wrote:

+/*
+ * handle_pqap: Handling pqap interception
+ * @vcpu: the vcpu having issue the pqap instruction
+ *
+ * We now support PQAP/AQIC instructions and we need to correctly
+ * answer the guest even if no dedicated driver's hook is available.
+ *
+ * The intercepting code calls a dedicated callback for this instruction
+ * if a driver did register one in the CRYPTO satellite of the
+ * SIE block.
+ *
+ * For PQAP/AQIC instructions only, verify privilege and specifications.
+ *
+ * If no callback available, the queues are not available, return this to
+ * the caller.
+ * Else return the value returned by the callback.
+ */

arch/s390/kvm/priv.c +14 -0
+static int handle_pqap(struct kvm_vcpu *vcpu)
+{
+    uint8_t fc;
+    struct ap_queue_status status = {};
+    int ret;
+    /* Verify that the AP instruction are available */
+    if (!ap_instructions_available())
+        return -EOPNOTSUPP;
+    /* Verify that the guest is allowed to use AP instructions */
+    if (!(vcpu->arch.sie_block->eca & ECA_APIE))
+        return -EOPNOTSUPP;
+    /* Verify that the function code is AQIC */
+    fc = vcpu->run->s.regs.gprs[0] >> 24;
+    /* We do not want to change the behavior we had before this 

patch*/
+    if (fc != 0x03)
+        return -EOPNOTSUPP;
+
+    /* PQAP instructions are allowed for guest kernel only */ +    if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) +        return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); +    /* AQIC instruction is allowed only if facility 65 is available */ +    if (!test_kvm_facility(vcpu->kvm, 65))
+        return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); +    /* Verify that the hook callback is registered and call it */ +    if (vcpu->kvm->arch.crypto.pqap_hook) {
+        if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner)) +            return -EOPNOTSUPP;
+        ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu); +        module_put(vcpu->kvm->arch.crypto.pqap_hook->owner); +        return ret;

Quoted 8 lines

+    }
+    /*
+     * It is the duty of the vfio_driver to register a hook +     * If it does not and we get an exception on AQIC we must +     * guess that there is no vfio_ap_driver at all and no one +     * to handle the guests's CRYCB and the CRYCB is empty. +     */
+    status.response_code = 0x01;

I'm still confused here, sorry. From previous discussions I recall that this indicates "no crypto device" (please correct me if I'm wrong.)

Before this patch, we had:
- guest issues PQAP/AQIC -> drop to userspace

Quoted 7 lines

With a correct implementation, we get:
- guest issues PQAP/AQIC -> callback does what needs to be done

With an incorrect implementation (no callback), we get:
- guest issues PQAP/AQIC -> guest gets response code 0x01

Why not drop to userspace in that case?

This is what I had in the previous patches.
Hum, I do not remember which discussion lead me to modify this.

Anyway, now that you put the finger on this problem, I think the problem is worse.

The behavior with old / new Linux, vfio driver and qemu is:

LINUX    VFIO_AP    QEMU    PGM
OLD    x    x    OPERATION
NEW    -    OLD    SPECIFICATION
NEW    -    NEW/aqic=off    SPECIFICATION
NEW    x    NEW/aqic=on    -

x = whatever
- = absent/none

So yes there is a change in behavior for the userland for the case QEMU do not set the AQIC facility 65, OLD QEMU or NEW QEMU wanting to behave like an older one.

I fear we have the same problem with the privileged operation...

For the last case, when the kvm_facility(65) is set, the explication is the following:

This is related to the handling of PQAP AQIC which is now authorized by this patch series.
If we authorize PQAP AQIC, by setting the bit for facility 65, the guest can use this instruction.
If the instruction follows the specifications we must answer something realistic and since there is nothing in the CRYCB (no driver) we answer that there is no queue.

Quoted 6 lines
Quoted 4 lines

Conclusion:  we must handle this in userland, it will have the benefit to keep old behavior when there is no callback. OLD QEMU will not see change as they will not set aqic facility
NEW QEMU will handle this correctly.

Sorry, wrong conclusion, handling this in userland will bring us much too far if we want to answer correctly for the case the hook is not there but QEMU accepted the facility for AQIC.

Sorry, forget it, I was tired.

Pierre

Quoted 8 lines

The alternative is easier, we just continue to respond with the OPERATION exception here and only handle the specification and privileged exception cases in QEMU and in the hook.

So, I think the discussion will go on until you come back :)

Regards,
Pierre

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
maintainer
#9 lore raw

Re: [PATCH v5 1/7] s390: ap: kvm: add PQAP interception for AQIC

In reply to Pierre Morel
Quoted 77 lines · On Fri, 15 Mar 2019 14:26:34 +0100 Pierre Morel <[email protected]> wrote:
Quoted 65 lines · Cornelia Huck
Quoted 45 lines · On Wed, 13 Mar 2019 17:04:58 +0100 Pierre Morel <[email protected]> wrote:
arch/s390/kvm/priv.c +45 -0
+/*
+ * handle_pqap: Handling pqap interception
+ * @vcpu: the vcpu having issue the pqap instruction
+ *
+ * We now support PQAP/AQIC instructions and we need to correctly
+ * answer the guest even if no dedicated driver's hook is available.
+ *
+ * The intercepting code calls a dedicated callback for this instruction
+ * if a driver did register one in the CRYPTO satellite of the
+ * SIE block.
+ *
+ * For PQAP/AQIC instructions only, verify privilege and specifications.
+ *
+ * If no callback available, the queues are not available, return this to
+ * the caller.
+ * Else return the value returned by the callback.
+ */
+static int handle_pqap(struct kvm_vcpu *vcpu)
+{
+	uint8_t fc;
+	struct ap_queue_status status = {};
+	int ret;
+	/* Verify that the AP instruction are available */
+	if (!ap_instructions_available())
+		return -EOPNOTSUPP;
+	/* Verify that the guest is allowed to use AP instructions */
+	if (!(vcpu->arch.sie_block->eca & ECA_APIE))
+		return -EOPNOTSUPP;
+	/* Verify that the function code is AQIC */
+	fc = vcpu->run->s.regs.gprs[0] >> 24;
+	/* We do not want to change the behavior we had before this patch*/
+	if (fc != 0x03)
+		return -EOPNOTSUPP;
+
+	/* PQAP instructions are allowed for guest kernel only */
+	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
+		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
+	/* AQIC instruction is allowed only if facility 65 is available */
+	if (!test_kvm_facility(vcpu->kvm, 65))
+		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
+	/* Verify that the hook callback is registered and call it */
+	if (vcpu->kvm->arch.crypto.pqap_hook) {
+		if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
+			return -EOPNOTSUPP;
+		ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);

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

Quoted 10 lines
arch/s390/kvm/priv.c +10 -0
+		module_put(vcpu->kvm->arch.crypto.pqap_hook->owner);
+		return ret;
+	}
+	/*
+	 * It is the duty of the vfio_driver to register a hook
+	 * If it does not and we get an exception on AQIC we must
+	 * guess that there is no vfio_ap_driver at all and no one
+	 * to handle the guests's CRYCB and the CRYCB is empty.
+	 */
+	status.response_code = 0x01;

I'm still confused here, sorry. From previous discussions I recall that this indicates "no crypto device" (please correct me if I'm wrong.)

Before this patch, we had:
- guest issues PQAP/AQIC -> drop to userspace

Quoted 7 lines

With a correct implementation, we get:
- guest issues PQAP/AQIC -> callback does what needs to be done

With an incorrect implementation (no callback), we get:
- guest issues PQAP/AQIC -> guest gets response code 0x01

Why not drop to userspace in that case?

This is what I had in the previous patches.
Hum, I do not remember which discussion lead me to modify this.

Quoted 7 lines

Anyway, now that you put the finger on this problem, I think the problem is worse.

The behavior with old / new Linux, vfio driver and qemu is:

LINUX VFIO_AP QEMU PGM
OLD x x OPERATION

Isn't OPERATION a bad answer if ap=on? It should not happen with a well behaved guest because facility 65 is not indicated, but if it does, I guess we give the wrong answer.

Quoted 4 lines

NEW - OLD SPECIFICATION
NEW - NEW/aqic=off SPECIFICATION
NEW x NEW/aqic=on -

AFAICT with LINUX == NEW we get the correct answer. OPERATION exception is only good if ap=off.

Quoted 9 lines

x = whatever
- = absent/none

So yes there is a change in behavior for the userland for the case QEMU do not set the AQIC facility 65, OLD QEMU or NEW QEMU wanting to behave like an older one.

I fear we have the same problem with the privileged operation...

IMHO this boils down to:
* either OLD QEMU or
* OLD LINUX
should have taken care of handling the mandatory intercept for PQAP/AQIC if ap=on (i.e. guest has AP instructions), and does not have facility 65 which was the case for OLD.

Things get complicated when one considers that ECA.28 is an effective control.

Quoted 11 lines

For the last case, when the kvm_facility(65) is set, the explication is the following:

This is related to the handling of PQAP AQIC which is now authorized by this patch series.
If we authorize PQAP AQIC, by setting the bit for facility 65, the guest can use this instruction.
If the instruction follows the specifications we must answer something realistic and since there is nothing in the CRYCB (no driver) we answer that there is no queue.

Quoted 3 lines

Conclusion: we must handle this in userland, it will have the benefit to keep old behavior when there is no callback.
OLD QEMU will not see change as they will not set aqic facility

That would mean we remain quirky.

Quoted 20 lines

NEW QEMU will handle this correctly.

In this case we also do not need to handle all other tests here but can move it to the callback as Tony wanted.

Would you agree with something simple like:

static int handle_pqap(struct kvm_vcpu *vcpu)

{
         int ret = -EOPNOTSUPP;

         /* Verify that the hook callback is registered and call it */
         if (pqap_hook)
                 if (try_module_get(pqap_hook->owner)) {
                         ret = pqap_hook->hook(vcpu);
                         module_put(pqap_hook->owner);
                 }
         return ret;
}
Quoted 2 lines

All other tests in QEMU and in the callback.

You stated in another email that the conclusion is wrong. I'm not sure what is the cleanest solution here. This effective control thing does make my head spin.

Regards,
Halil

#10 lore raw

Re: [PATCH v5 1/7] s390: ap: kvm: add PQAP interception for AQIC

Quoted 83 lines · Halil Pasic
Quoted 80 lines · On Fri, 15 Mar 2019 14:26:34 +0100 Pierre Morel <[email protected]> wrote:
Quoted 65 lines · Cornelia Huck
Quoted 45 lines · On Wed, 13 Mar 2019 17:04:58 +0100 Pierre Morel <[email protected]> wrote:
arch/s390/kvm/priv.c +45 -0
+/*
+ * handle_pqap: Handling pqap interception
+ * @vcpu: the vcpu having issue the pqap instruction
+ *
+ * We now support PQAP/AQIC instructions and we need to correctly
+ * answer the guest even if no dedicated driver's hook is available.
+ *
+ * The intercepting code calls a dedicated callback for this instruction
+ * if a driver did register one in the CRYPTO satellite of the
+ * SIE block.
+ *
+ * For PQAP/AQIC instructions only, verify privilege and specifications.
+ *
+ * If no callback available, the queues are not available, return this to
+ * the caller.
+ * Else return the value returned by the callback.
+ */
+static int handle_pqap(struct kvm_vcpu *vcpu)
+{
+	uint8_t fc;
+	struct ap_queue_status status = {};
+	int ret;
+	/* Verify that the AP instruction are available */
+	if (!ap_instructions_available())
+		return -EOPNOTSUPP;
+	/* Verify that the guest is allowed to use AP instructions */
+	if (!(vcpu->arch.sie_block->eca & ECA_APIE))
+		return -EOPNOTSUPP;
+	/* Verify that the function code is AQIC */
+	fc = vcpu->run->s.regs.gprs[0] >> 24;
+	/* We do not want to change the behavior we had before this patch*/
+	if (fc != 0x03)
+		return -EOPNOTSUPP;
+
+	/* PQAP instructions are allowed for guest kernel only */
+	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
+		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
+	/* AQIC instruction is allowed only if facility 65 is available */
+	if (!test_kvm_facility(vcpu->kvm, 65))
+		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
+	/* Verify that the hook callback is registered and call it */
+	if (vcpu->kvm->arch.crypto.pqap_hook) {
+		if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
+			return -EOPNOTSUPP;
+		ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);

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

Quoted 10 lines
arch/s390/kvm/priv.c +10 -0
+		module_put(vcpu->kvm->arch.crypto.pqap_hook->owner);
+		return ret;
+	}
+	/*
+	 * It is the duty of the vfio_driver to register a hook
+	 * If it does not and we get an exception on AQIC we must
+	 * guess that there is no vfio_ap_driver at all and no one
+	 * to handle the guests's CRYCB and the CRYCB is empty.
+	 */
+	status.response_code = 0x01;

I'm still confused here, sorry. From previous discussions I recall that this indicates "no crypto device" (please correct me if I'm wrong.)

Before this patch, we had:
- guest issues PQAP/AQIC -> drop to userspace

Quoted 7 lines

With a correct implementation, we get:
- guest issues PQAP/AQIC -> callback does what needs to be done

With an incorrect implementation (no callback), we get:
- guest issues PQAP/AQIC -> guest gets response code 0x01

Why not drop to userspace in that case?

This is what I had in the previous patches.
Hum, I do not remember which discussion lead me to modify this.

Anyway, now that you put the finger on this problem, I think the problem is worse.

Quoted 8 lines
Quoted 4 lines

The behavior with old / new Linux, vfio driver and qemu is:

LINUX VFIO_AP QEMU PGM
OLD x x OPERATION

Isn't OPERATION a bad answer if ap=on? It should not happen with a well behaved guest because facility 65 is not indicated, but if it does, I guess we give the wrong answer.

It is clearly wrong but we can not change the past :)

Quoted 8 lines
Quoted 5 lines

NEW - OLD SPECIFICATION
NEW - NEW/aqic=off SPECIFICATION
NEW x NEW/aqic=on -

AFAICT with LINUX == NEW we get the correct answer. OPERATION exception is only good if ap=off.

Exact.

Quoted 11 lines
Quoted 10 lines

x = whatever
- = absent/none

So yes there is a change in behavior for the userland for the case QEMU do not set the AQIC facility 65, OLD QEMU or NEW QEMU wanting to behave like an older one.

I fear we have the same problem with the privileged operation...

Quoted 6 lines

IMHO this boils down to:
* either OLD QEMU or
* OLD LINUX
should have taken care of handling the mandatory intercept for PQAP/AQIC if ap=on (i.e. guest has AP instructions), and does not have facility 65 which was the case for OLD.

yes

Quoted 3 lines

Things get complicated when one considers that ECA.28 is an effective control.

I don't think so, ECA_28 is not really a problem.
We do not propagate ECA_AIV in VSIE and ECA_AIV is tested in the vfio driver to support GISA. So that the guest 3 will not support interrupt.

Quoted 12 lines
Quoted 12 lines

For the last case, when the kvm_facility(65) is set, the explication is the following:

This is related to the handling of PQAP AQIC which is now authorized by this patch series.
If we authorize PQAP AQIC, by setting the bit for facility 65, the guest can use this instruction.
If the instruction follows the specifications we must answer something realistic and since there is nothing in the CRYCB (no driver) we answer that there is no queue.

Quoted 5 lines
Quoted 3 lines

Conclusion: we must handle this in userland, it will have the benefit to keep old behavior when there is no callback.
OLD QEMU will not see change as they will not set aqic facility

That would mean we remain quirky.

Yes, the alternative is:

1) We do things right but this mean we change the ABI (SPECIFICATION instead of OPERATION)

I thing this is the best thing to do, it is the implementation proposed by this patch where all is done in Kernel, so that we are right what ever the userland user is (QEMU or other).

2) We want to preserve the old ABI for old QEMU
Then I proposed the implementation here under.

My personal opinion, is that we should change the ABI and do things right now.
We should also do it right for TAPQ with t bit set. I remember Christian already warned about this but we did not implement it.

Quoted 21 lines
Quoted 21 lines

NEW QEMU will handle this correctly.

In this case we also do not need to handle all other tests here but can move it to the callback as Tony wanted.

Would you agree with something simple like:

static int handle_pqap(struct kvm_vcpu *vcpu)

{
          int ret = -EOPNOTSUPP;

          /* Verify that the hook callback is registered and call it */
          if (pqap_hook)
                  if (try_module_get(pqap_hook->owner)) {
                          ret = pqap_hook->hook(vcpu);
                          module_put(pqap_hook->owner);
                  }
          return ret;
}
Quoted 4 lines
Quoted 2 lines

All other tests in QEMU and in the callback.

You stated in another email that the conclusion is wrong. I'm not sure

Forget it, I do not understand what I wanted to say there /o\ .

Quoted 2 lines

what is the cleanest solution here. This effective control thing does make my head spin.

As I said I do not thing any effective control do interfere here. IMHO Alternative 1 is the cleanest solution

Regards,
Pierre

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
maintainer
#11 lore raw

Re: [PATCH v5 1/7] s390: ap: kvm: add PQAP interception for AQIC

Quoted 1 line · On Tue, 19 Mar 2019 11:01:44 +0100 Pierre Morel <[email protected]> wrote:

On 15/03/2019 18:28, Halil Pasic wrote:

[..]

Quoted 9 lines
Quoted 3 lines

Things get complicated when one considers that ECA.28 is an effective control.

I don't think so, ECA_28 is not really a problem.
We do not propagate ECA_AIV in VSIE and ECA_AIV is tested in the vfio driver to support GISA.
So that the guest 3 will not support interrupt.

That was not my concern, but while we are at it... I guess you refer to the check in handle_pqap(). That seems to do -EOPNOTSUPP, i.e. got to userspace, i.e. with today's QEMU operation exception. Which does not seem right.

My concern was the following. Let assume
ECA.28 == 1 and EECA.28 == 0 != 1
and guest issues a PQAP (for simplicity AQIC).

Currently I guess we take a 0x04 interception and go to userspace, which may or may not be the best thing to do.

With this patch we would take a 0x04, but (opposed to before) if guest does not have facility 65 we go with a specification exception. Operation exception should however take priority over this kind of specification exception. So basically everything except PQAP/AQIC would give you and operation exception (with current QEMU), but PQAP/AQIC would give you a specification exception. Which is wrong!

AFAICT there is no way to tell if we got a 04 interception because EECA.28 != 1 (and ECA.28 == 1) and FW won't interpret the AP instructions for us, or because it PQAP/AQIC is a mandatory intercept. In other words I don't see a way to tell if EECA.28 is 1 when interpreting PQAP/AQIC.

Do you agree?

[..]

Quoted 10 lines

Yes, the alternative is:

1) We do things right but this mean we change the ABI (SPECIFICATION instead of OPERATION)

I thing this is the best thing to do, it is the implementation proposed by this patch where all is done in Kernel, so that we are right what ever the userland user is (QEMU or other).

Quoted 6 lines

2) We want to preserve the old ABI for old QEMU
Then I proposed the implementation here under.

My personal opinion, is that we should change the ABI and do things right now.

I tend to agree. Giving an operation exception instead of a specification exception is a bug. If it is a kernel or qemu bug it ain't clear to me at the moment.

Quoted 3 lines

We should also do it right for TAPQ with t bit set. I remember Christian already warned about this but we did not implement it.

Yes, I have some blurry memories of something similar myself. I wonder if there was a reason, or did we just forget to address this issue.

Regards,
Halil

#12 lore raw

Re: [PATCH v5 1/7] s390: ap: kvm: add PQAP interception for AQIC

Quoted 17 lines · Halil Pasic
Quoted 1 line · On Tue, 19 Mar 2019 11:01:44 +0100 Pierre Morel <[email protected]> wrote:

On 15/03/2019 18:28, Halil Pasic wrote:

[..]

Quoted 9 lines
Quoted 3 lines

Things get complicated when one considers that ECA.28 is an effective control.

I don't think so, ECA_28 is not really a problem.
We do not propagate ECA_AIV in VSIE and ECA_AIV is tested in the vfio driver to support GISA.
So that the guest 3 will not support interrupt.

Quoted 4 lines

That was not my concern, but while we are at it... I guess you refer to the check in handle_pqap(). That seems to do -EOPNOTSUPP, i.e. got to userspace, i.e. with today's QEMU operation exception. Which does not seem right.

We already discussed this. no?

Quoted 10 lines

My concern was the following. Let assume
ECA.28 == 1 and EECA.28 == 0 != 1
and guest issues a PQAP (for simplicity AQIC).

Currently I guess we take a 0x04 interception and go to userspace, which may or may not be the best thing to do.

With this patch we would take a 0x04, but (opposed to before) if guest does not have facility 65 we go with a specification exception.

This is not right.
We return -EOPNOTSUPP which will be intercepted by QEMU which will report an OPERATION exception as before.

Quoted 5 lines

Operation exception should however take priority over this kind of specification exception. So basically everything except PQAP/AQIC would give you and operation exception (with current QEMU), but PQAP/AQIC would give you a specification exception. Which is wrong!

Quoted 7 lines

AFAICT there is no way to tell if we got a 04 interception because EECA.28 != 1 (and ECA.28 == 1) and FW won't interpret the AP instructions for us, or because it PQAP/AQIC is a mandatory intercept. In other words I don't see a way to tell if EECA.28 is 1 when interpreting PQAP/AQIC.

Do you agree?

No.
EECA = HOST_ECA & GUEST_ECA
after we made sure that AP instructions are available, HOST_ECA=1

(vcpu->arch.sie_block->eca & ECA_APIE) gives us the answer.

In the case HOST_ECA=0 we always go to userland as before.

Quoted 24 lines

[..]

Quoted 10 lines

Yes, the alternative is:

1) We do things right but this mean we change the ABI (SPECIFICATION instead of OPERATION)

I thing this is the best thing to do, it is the implementation proposed by this patch where all is done in Kernel, so that we are right what ever the userland user is (QEMU or other).

Quoted 6 lines

2) We want to preserve the old ABI for old QEMU
Then I proposed the implementation here under.

My personal opinion, is that we should change the ABI and do things right now.

I tend to agree. Giving an operation exception instead of a specification exception is a bug. If it is a kernel or qemu bug it ain't clear to me at the moment.

Quoted 6 lines
Quoted 3 lines

We should also do it right for TAPQ with t bit set. I remember Christian already warned about this but we did not implement it.

Yes, I have some blurry memories of something similar myself. I wonder if there was a reason, or did we just forget to address this issue.

I will integrate it in the next iteration too, even it is not IRQ, the PQAP hook patch can be more general.

Regards,
Pierre

Quoted 4 lines

Regards,
Halil

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#13 lore raw

Re: [PATCH v5 1/7] s390: ap: kvm: add PQAP interception for AQIC

Quoted 6 lines · Pierre Morel
Quoted 5 lines · Halil Pasic
Quoted 1 line · On Tue, 19 Mar 2019 11:01:44 +0100 Pierre Morel <[email protected]> wrote:

On 15/03/2019 18:28, Halil Pasic wrote:

...snip...

Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

We should also do it right for TAPQ with t bit set. I remember Christian already warned about this but we did not implement it.

Yes, I have some blurry memories of something similar myself. I wonder if there was a reason, or did we just forget to address this issue.

I will integrate it in the next iteration too, even it is not IRQ, the PQAP hook patch can be more general.

After all, I will not do this, I remember the reason why we did not do it once: simply it is not intercepted until we enable it.

So we will handle it when we enable the TAPQ-t interception

Regards,
Pierre

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#14 lore raw

PATCH v5 2/7 s390: ap: new vfio_ap_queue structure

In reply to Pierre Morel

The AP interruptions are assigned on a queue basis and the GISA structure is handled on a VM basis, so that we need to add a structure we can retrieve from both side holding the information we need to handle PQAP/AQIC interception and setup the GISA.

Since we can not add more information to the ap_device we add a new structure vfio_ap_queue, to hold per queue information useful to handle interruptions and set it as driver's data of the standard ap_queue device.

Usually, the device and the mediated device are linked together but in the vfio_ap driver design we have a bunch of "sub" devices (the ap_queue devices) belonging to the mediated device.

Linking these structure to the mediated device it is assigned to, with the help of the vfio_ap_queue structure will help us to retrieve the AP devices associated with the mediated devices during the mediated device operations.

------------    -------------
| AP queue |--> | AP_vfio_q |<----
------------    ------^------    |    ---------------
                      |          <--->| matrix_mdev |
------------    ------v------    |    ---------------
| AP queue |--> | AP_vfio_q |-----
------------    -------------

The vfio_ap_queue device will hold the following entries:
- apqn: AP queue number (defined here)
- isc : Interrupt subclass (defined later)
- nib : notification information byte (defined later)
- list: a list_head entry allowing to link this structure to a matrix mediated device it is assigned to.

The vfio_ap_queue structure is allocated when the vfio_ap_driver is probed and added as driver data to the ap_queue device. It is free on remove.

The structure is linked to the matrix_dev host device at the probe of the device building some kind of free list for the matrix mediated devices.

When the vfio_queue is associated to a matrix mediated device, during assign_adapter or assign_domain,
the vfio_ap_queue device is linked to this matrix mediated device and unlinked when dissociated.

Queuing the devices on a list of free devices and testing the matrix_mdev pointer to the associated matrix allow us to know if the queue is associated to the matrix device and associated or not to a mediated device.

---
 drivers/s390/crypto/vfio_ap_drv.c     |  31 ++-
 drivers/s390/crypto/vfio_ap_ops.c     | 390 +++++++++++++++++-----------------
 drivers/s390/crypto/vfio_ap_private.h |   7 +
 3 files changed, 234 insertions(+), 194 deletions(-)
drivers/s390/crypto/vfio_ap_drv.c +30 -1
index e9824c3..df6f21a 100644
@@ -40,14 +40,42 @@ static struct ap_device_id ap_queue_ids[] = {
 
 MODULE_DEVICE_TABLE(vfio_ap, ap_queue_ids);
 
+/**
+ * vfio_ap_queue_dev_probe:
+ *
+ * Allocate a vfio_ap_queue structure and associate it
+ * with the device as driver_data.
+ */
 static int vfio_ap_queue_dev_probe(struct ap_device *apdev)
 {
+	struct vfio_ap_queue *q;
+
+	q = kzalloc(sizeof(*q), GFP_KERNEL);
+	if (!q)
+		return -ENOMEM;
+	dev_set_drvdata(&apdev->device, q);
+	q->apqn = to_ap_queue(&apdev->device)->qid;
+	INIT_LIST_HEAD(&q->list);
+	mutex_lock(&matrix_dev->lock);
+	list_add(&q->list, &matrix_dev->free_list);
+	mutex_unlock(&matrix_dev->lock);
 	return 0;
 }
 
+/**
+ * vfio_ap_queue_dev_remove:
+ *
+ * Free the associated vfio_ap_queue structure
+ */
 static void vfio_ap_queue_dev_remove(struct ap_device *apdev)
 {
-	/* Nothing to do yet */
+	struct vfio_ap_queue *q;
+
+	q = dev_get_drvdata(&apdev->device);
+	mutex_lock(&matrix_dev->lock);
+	list_del(&q->list);
+	mutex_unlock(&matrix_dev->lock);
+	kfree(q);
 }
 
 static void vfio_ap_matrix_dev_release(struct device *dev)
@@ -107,6 +135,7 @@ static int vfio_ap_matrix_dev_create(void)
 	matrix_dev->device.bus = &matrix_bus;
 	matrix_dev->device.release = vfio_ap_matrix_dev_release;
 	matrix_dev->vfio_ap_drv = &vfio_ap_drv;
+	INIT_LIST_HEAD(&matrix_dev->free_list);
 
 	ret = device_register(&matrix_dev->device);
 	if (ret)
drivers/s390/crypto/vfio_ap_ops.c +197 -193

517 lines not on this page · raw message

drivers/s390/crypto/vfio_ap_private.h +7 -0
index a910be1..3e6940c 100644
@@ -40,6 +40,7 @@ struct ap_matrix_dev {
 	atomic_t available_instances;
 	struct ap_config_info info;
 	struct list_head mdev_list;
+	struct list_head free_list;
 	struct mutex lock;
 	struct ap_driver  *vfio_ap_drv;
 };
@@ -83,9 +84,15 @@ struct ap_matrix_mdev {
 	struct notifier_block group_notifier;
 	struct kvm *kvm;
 	struct kvm_s390_module_hook pqap_hook;
+	struct list_head qlist;
 };
 
 extern int vfio_ap_mdev_register(void);
 extern void vfio_ap_mdev_unregister(void);
 
+struct vfio_ap_queue {
+	struct list_head list;
+	struct ap_matrix_mdev *matrix_mdev;
+	int	apqn;
+};
 #endif /* _VFIO_AP_PRIVATE_H_ */
Signature · 1 line
2.7.4
#15 lore raw

Re: [PATCH v5 2/7] s390: ap: new vfio_ap_queue structure

Quoted 28 lines · On Wed, 13 Mar 2019 17:04:59 +0100 Pierre Morel <[email protected]> wrote:
drivers/s390/crypto/vfio_ap_drv.c +17 -0
index e9824c3..df6f21a 100644
@@ -40,14 +40,42 @@ static struct ap_device_id ap_queue_ids[] = {
 
 MODULE_DEVICE_TABLE(vfio_ap, ap_queue_ids);
 
+/**
+ * vfio_ap_queue_dev_probe:
+ *
+ * Allocate a vfio_ap_queue structure and associate it
+ * with the device as driver_data.
+ */
 static int vfio_ap_queue_dev_probe(struct ap_device *apdev)
 {
+	struct vfio_ap_queue *q;
+
+	q = kzalloc(sizeof(*q), GFP_KERNEL);
+	if (!q)
+		return -ENOMEM;
+	dev_set_drvdata(&apdev->device, q);
+	q->apqn = to_ap_queue(&apdev->device)->qid;
+	INIT_LIST_HEAD(&q->list);
+	mutex_lock(&matrix_dev->lock);
+	list_add(&q->list, &matrix_dev->free_list);
+	mutex_unlock(&matrix_dev->lock);
 
Quoted 1 line

From what I can see, dealing with the free_list is supposed to be

protected by the matrix_dev mutex, and at a glance, it indeed seems to be held every time you interact with the list. I think it would be good to document that with a comment.

(I have not reviewed this deeply, and I won't be able to look at this more until April, sorry.)

Quoted 2 lines

return 0;
}

#16 lore raw

Re: [PATCH v5 2/7] s390: ap: new vfio_ap_queue structure

Quoted 27 lines · Cornelia Huck
Quoted 24 lines · On Wed, 13 Mar 2019 17:04:59 +0100 Pierre Morel <[email protected]> wrote:
drivers/s390/crypto/vfio_ap_drv.c +14 -0
index e9824c3..df6f21a 100644
@@ -40,14 +40,42 @@ static struct ap_device_id ap_queue_ids[] = {
 
  MODULE_DEVICE_TABLE(vfio_ap, ap_queue_ids);
 
+/**
+ * vfio_ap_queue_dev_probe:
+ *
+ * Allocate a vfio_ap_queue structure and associate it
+ * with the device as driver_data.
+ */
  static int vfio_ap_queue_dev_probe(struct ap_device *apdev)
  {
+	struct vfio_ap_queue *q;
+
+	q = kzalloc(sizeof(*q), GFP_KERNEL);
+	if (!q)
+		return -ENOMEM;
+	dev_set_drvdata(&apdev->device, q);
+	q->apqn = to_ap_queue(&apdev->device)->qid;
+	INIT_LIST_HEAD(&q->list);
Quoted 8 lines

drivers/s390/crypto/vfio_ap_drv.c static struct ap_device_id ap_queue_ids[] = { · line 61

Quoted 3 lines
drivers/s390/crypto/vfio_ap_drv.c +3 -0
+	mutex_lock(&matrix_dev->lock);
+	list_add(&q->list, &matrix_dev->free_list);
+	mutex_unlock(&matrix_dev->lock);

From what I can see, dealing with the free_list is supposed to be
protected by the matrix_dev mutex, and at a glance, it indeed seems to be held every time you interact with the list. I think it would be good to document that with a comment.

Yes.
It is a big lock but only on administrative tasks so I think it is harmless.

Quoted 3 lines

(I have not reviewed this deeply, and I won't be able to look at this more until April, sorry.)

OK.
Thanks for the comments.

Regards,
Pierre

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#17 lore raw

PATCH v5 3/7 vfio: ap: register IOMMU VFIO notifier

In reply to Pierre Morel

To be able to use the VFIO interface to facilitate the mediated device memory pinning/unpinning we need to register a notifier for IOMMU.

While we will start to pin one guest page for the interrupt indicator byte, this is still ok with ballooning as this page will never be used by the guest virtio-balloon driver.
So the pinned page will never be freed. And even a broken guest does so, that would not impact the host as the original page is still in control by vfio.

---
 drivers/s390/crypto/vfio_ap_ops.c     | 53 ++++++++++++++++++++++++++++++++---
 drivers/s390/crypto/vfio_ap_private.h |  2 ++
 2 files changed, 51 insertions(+), 4 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +49 -4
index 485595c..0f8952c23 100644
@@ -757,6 +757,36 @@ static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
 };
 
 /**
+ * vfio_ap_mdev_iommu_notifier: IOMMU notifier callback
+ *
+ * @nb: The notifier block
+ * @action: Action to be taken
+ * @data: data associated with the request
+ *
+ * For an UNMAP request, unpin the guest IOVA (the NIB guest address we
+ * pinned before). Other requests are ignored.
+ *
+ */
+static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,
+				       unsigned long action, void *data)
+{
+	struct ap_matrix_mdev *matrix_mdev;
+
+	matrix_mdev = container_of(nb, struct ap_matrix_mdev, iommu_notifier);
+
+	if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
+		struct vfio_iommu_type1_dma_unmap *unmap = data;
+		unsigned long g_pfn = unmap->iova >> PAGE_SHIFT;
+
+		vfio_unpin_pages(mdev_dev(matrix_mdev->mdev), &g_pfn, 1);
+		return NOTIFY_OK;
+	}
+
+	return NOTIFY_DONE;
+}
+
+
+/**
  * vfio_ap_mdev_set_kvm
  *
  * @matrix_mdev: a mediated matrix device
@@ -855,12 +885,25 @@ static int vfio_ap_mdev_open(struct mdev_device *mdev)
 
 	ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
 				     &events, &matrix_mdev->group_notifier);
-	if (ret) {
-		module_put(THIS_MODULE);
-		return ret;
-	}
+	if (ret)
+		goto err_group;
+
+	matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
+	events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
+
+	ret = vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
+				     &events, &matrix_mdev->iommu_notifier);
+	if (ret)
+		goto err_iommu;
 
 	return 0;
+
+err_iommu:
+	vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
+				 &matrix_mdev->group_notifier);
+err_group:
+	module_put(THIS_MODULE);
+	return ret;
 }
 
 static void vfio_ap_mdev_release(struct mdev_device *mdev)
@@ -872,6 +915,8 @@ static void vfio_ap_mdev_release(struct mdev_device *mdev)
 
 	matrix_mdev->kvm = NULL;
 	vfio_ap_mdev_reset_queues(mdev);
+	vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
+				 &matrix_mdev->iommu_notifier);
 	vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
 				 &matrix_mdev->group_notifier);
 	module_put(THIS_MODULE);
drivers/s390/crypto/vfio_ap_private.h +2 -0
index 3e6940c..4a287c8 100644
@@ -82,9 +82,11 @@ struct ap_matrix_mdev {
 	struct list_head node;
 	struct ap_matrix matrix;
 	struct notifier_block group_notifier;
+	struct notifier_block iommu_notifier;
 	struct kvm *kvm;
 	struct kvm_s390_module_hook pqap_hook;
 	struct list_head qlist;
+	struct mdev_device *mdev;
 };
 
 extern int vfio_ap_mdev_register(void);
Signature · 1 line
2.7.4
#18 lore raw

PATCH v5 4/7 s390: ap: setup relation betwen KVM and mediated device

In reply to Pierre Morel

When the mediated device is open we setup the relation with KVM unset it when the mediated device is released.

We ensure KVM is present on opening of the mediated device.

We ensure that KVM survives the mediated device, and establish a direct link from KVM to the mediated device to simplify the relationship.

---
 drivers/s390/crypto/vfio_ap_ops.c | 80 ++++++++++++++++++++++-----------------
 1 file changed, 46 insertions(+), 34 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +46 -34
index 0f8952c23..6b559ca 100644
@@ -790,7 +790,6 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,
  * vfio_ap_mdev_set_kvm
  *
  * @matrix_mdev: a mediated matrix device
- * @kvm: reference to KVM instance
  *
  * Verifies no other mediated matrix device has @kvm and sets a reference to
  * it in @matrix_mdev->kvm.
@@ -798,53 +797,39 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,
  * Return 0 if no other mediated matrix device has a reference to @kvm;
  * otherwise, returns an -EPERM.
  */
-static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
-				struct kvm *kvm)
+static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev)
 {
-	struct ap_matrix_mdev *m;
-
 	mutex_lock(&matrix_dev->lock);
+	if (matrix_mdev->kvm->arch.crypto.pqap_hook)
+		goto err_unlock;
 
-	list_for_each_entry(m, &matrix_dev->mdev_list, node) {
-		if ((m != matrix_mdev) && (m->kvm == kvm)) {
-			mutex_unlock(&matrix_dev->lock);
-			return -EPERM;
-		}
-	}
+	if (!matrix_mdev->kvm->arch.crypto.crycbd)
+		goto err_unlock;
 
-	matrix_mdev->kvm = kvm;
-	mutex_unlock(&matrix_dev->lock);
+	matrix_mdev->kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
 
+	kvm_arch_crypto_set_masks(matrix_mdev->kvm, matrix_mdev->matrix.apm,
+				  matrix_mdev->matrix.aqm,
+				  matrix_mdev->matrix.adm);
+	kvm_get_kvm(matrix_mdev->kvm);
+	mutex_unlock(&matrix_dev->lock);
 	return 0;
+
+err_unlock:
+	mutex_unlock(&matrix_dev->lock);
+	return -EPERM;
 }
 
 static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
 				       unsigned long action, void *data)
 {
-	int ret;
 	struct ap_matrix_mdev *matrix_mdev;
 
 	if (action != VFIO_GROUP_NOTIFY_SET_KVM)
 		return NOTIFY_OK;
 
 	matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);
-
-	if (!data) {
-		matrix_mdev->kvm = NULL;
-		return NOTIFY_OK;
-	}
-
-	ret = vfio_ap_mdev_set_kvm(matrix_mdev, data);
-	if (ret)
-		return NOTIFY_DONE;
-
-	/* If there is no CRYCB pointer, then we can't copy the masks */
-	if (!matrix_mdev->kvm->arch.crypto.crycbd)
-		return NOTIFY_DONE;
-
-	kvm_arch_crypto_set_masks(matrix_mdev->kvm, matrix_mdev->matrix.apm,
-				  matrix_mdev->matrix.aqm,
-				  matrix_mdev->matrix.adm);
+	matrix_mdev->kvm = data;
 
 	return NOTIFY_OK;
 }
@@ -888,6 +873,12 @@ static int vfio_ap_mdev_open(struct mdev_device *mdev)
 	if (ret)
 		goto err_group;
 
+	/* We do not support opening the mediated device without KVM */
+	if (!matrix_mdev->kvm) {
+		ret = -ENODEV;
+		goto err_group;
+	}
+
 	matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
 	events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
 
@@ -896,8 +887,15 @@ static int vfio_ap_mdev_open(struct mdev_device *mdev)
 	if (ret)
 		goto err_iommu;
 
+	ret = vfio_ap_mdev_set_kvm(matrix_mdev);
+	if (ret)
+		goto err_kvm;
+
 	return 0;
 
+err_kvm:
+	vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
+				 &matrix_mdev->iommu_notifier);
 err_iommu:
 	vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
 				 &matrix_mdev->group_notifier);
@@ -906,19 +904,33 @@ static int vfio_ap_mdev_open(struct mdev_device *mdev)
 	return ret;
 }
 
-static void vfio_ap_mdev_release(struct mdev_device *mdev)
+static int vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
 {
-	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
+	struct kvm *kvm = matrix_mdev->kvm;
 
 	if (matrix_mdev->kvm)
 		kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
-
+	vfio_ap_mdev_reset_queues(matrix_mdev->mdev);
+	matrix_mdev->kvm->arch.crypto.pqap_hook = NULL;
 	matrix_mdev->kvm = NULL;
+
+	kvm_put_kvm(kvm);
+	return 0;
+}
+
+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);
+
 	vfio_ap_mdev_reset_queues(mdev);
+	vfio_ap_mdev_unset_kvm(matrix_mdev);
 	vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
 				 &matrix_mdev->iommu_notifier);
 	vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
 				 &matrix_mdev->group_notifier);
+	mutex_unlock(&matrix_dev->lock);
 	module_put(THIS_MODULE);
 }
 
Signature · 1 line
2.7.4
maintainer
#19 lore raw

Re: [PATCH v5 4/7] s390: ap: setup relation betwen KVM and mediated device

Quoted 6 lines · On Wed, 13 Mar 2019 17:05:01 +0100 Pierre Morel <[email protected]> wrote:

When the mediated device is open we setup the relation with KVM unset it when the mediated device is released.

We ensure KVM is present on opening of the mediated device.

We ensure that KVM survives the mediated device, and establish a direct

survives?

Quoted 103 lines

link from KVM to the mediated device to simplify the relationship.

---
 drivers/s390/crypto/vfio_ap_ops.c | 80 ++++++++++++++++++++++-----------------
 1 file changed, 46 insertions(+), 34 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +22 -31
index 0f8952c23..6b559ca 100644
@@ -790,7 +790,6 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,
  * vfio_ap_mdev_set_kvm
  *
  * @matrix_mdev: a mediated matrix device
- * @kvm: reference to KVM instance
  *
  * Verifies no other mediated matrix device has @kvm and sets a reference to
  * it in @matrix_mdev->kvm.
@@ -798,53 +797,39 @@ static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,
  * Return 0 if no other mediated matrix device has a reference to @kvm;
  * otherwise, returns an -EPERM.
  */
-static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
-				struct kvm *kvm)
+static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev)
 {
-	struct ap_matrix_mdev *m;
-
 	mutex_lock(&matrix_dev->lock);
+	if (matrix_mdev->kvm->arch.crypto.pqap_hook)
+		goto err_unlock;
 
-	list_for_each_entry(m, &matrix_dev->mdev_list, node) {
-		if ((m != matrix_mdev) && (m->kvm == kvm)) {
-			mutex_unlock(&matrix_dev->lock);
-			return -EPERM;
-		}
-	}
+	if (!matrix_mdev->kvm->arch.crypto.crycbd)
+		goto err_unlock;
 
-	matrix_mdev->kvm = kvm;
-	mutex_unlock(&matrix_dev->lock);
+	matrix_mdev->kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
 
+	kvm_arch_crypto_set_masks(matrix_mdev->kvm, matrix_mdev->matrix.apm,
+				  matrix_mdev->matrix.aqm,
+				  matrix_mdev->matrix.adm);
+	kvm_get_kvm(matrix_mdev->kvm);
+	mutex_unlock(&matrix_dev->lock);
 	return 0;
+
+err_unlock:
+	mutex_unlock(&matrix_dev->lock);
+	return -EPERM;
 }
 
 static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
 				       unsigned long action, void *data)
 {
-	int ret;
 	struct ap_matrix_mdev *matrix_mdev;
 
 	if (action != VFIO_GROUP_NOTIFY_SET_KVM)
 		return NOTIFY_OK;
 
 	matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);
-
-	if (!data) {
-		matrix_mdev->kvm = NULL;
-		return NOTIFY_OK;
-	}
-
-	ret = vfio_ap_mdev_set_kvm(matrix_mdev, data);
-	if (ret)
-		return NOTIFY_DONE;
-
-	/* If there is no CRYCB pointer, then we can't copy the masks */
-	if (!matrix_mdev->kvm->arch.crypto.crycbd)
-		return NOTIFY_DONE;
-
-	kvm_arch_crypto_set_masks(matrix_mdev->kvm, matrix_mdev->matrix.apm,
-				  matrix_mdev->matrix.aqm,
-				  matrix_mdev->matrix.adm);
+	matrix_mdev->kvm = data;
 
 	return NOTIFY_OK;
 }
@@ -888,6 +873,12 @@ static int vfio_ap_mdev_open(struct mdev_device *mdev)
 	if (ret)
 		goto err_group;
 
+	/* We do not support opening the mediated device without KVM */
+	if (!matrix_mdev->kvm) {
+		ret = -ENODEV;
+		goto err_group;
+	}
+
 	matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
 	events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
 

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

Quoted 4 lines
drivers/s390/crypto/vfio_ap_ops.c +1 -0
@@ -896,8 +887,15 @@ static int vfio_ap_mdev_open(struct mdev_device *mdev)
 	if (ret)
 		goto err_iommu;
 
+	ret = vfio_ap_mdev_set_kvm(matrix_mdev);

At this point the matrix_mdev->kvm ain't guaranteed to be valid IMHO. Or am I wrong? If I'm right kvm_get_kvm(matrix_mdev->kvm) could be too late.

Quoted 13 lines
drivers/s390/crypto/vfio_ap_ops.c +6 -0
+	if (ret)
+		goto err_kvm;
+
 	return 0;
 
+err_kvm:
+	vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
+				 &matrix_mdev->iommu_notifier);
 err_iommu:
 	vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
 				 &matrix_mdev->group_notifier);
@@ -906,19 +904,33 @@ static int vfio_ap_mdev_open(struct mdev_device *mdev)
 	return ret;

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

Quoted 10 lines
drivers/s390/crypto/vfio_ap_ops.c +2 -2
 }
 
-static void vfio_ap_mdev_release(struct mdev_device *mdev)
+static int vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
 {
-	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
+	struct kvm *kvm = matrix_mdev->kvm;
 
 	if (matrix_mdev->kvm)
 		kvm_arch_crypto_clear_masks(matrix_mdev->kvm);

This still conditional?

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

Quoted 2 lines
drivers/s390/crypto/vfio_ap_ops.c +1 -1
-
+	vfio_ap_mdev_reset_queues(matrix_mdev->mdev);

I guess your intention was to move vfio_ap_mdev_reset_queues() here from vfio_ap_mdev_release(), but you still have a vfio_ap_mdev_reset_queues() call in vfio_ap_mdev_release().

Quoted 4 lines
drivers/s390/crypto/vfio_ap_ops.c +3 -0
+	matrix_mdev->kvm->arch.crypto.pqap_hook = NULL;
 	matrix_mdev->kvm = NULL;
+
+	kvm_put_kvm(kvm);

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

Quoted 10 lines
drivers/s390/crypto/vfio_ap_ops.c +9 -0
+	return 0;
+}
+
+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);
+
 	vfio_ap_mdev_reset_queues(mdev);

Here.

Regards,
Halil

Quoted 9 lines
drivers/s390/crypto/vfio_ap_ops.c +2 -0
+	vfio_ap_mdev_unset_kvm(matrix_mdev);
 	vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
 				 &matrix_mdev->iommu_notifier);
 	vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
 				 &matrix_mdev->group_notifier);
+	mutex_unlock(&matrix_dev->lock);
 	module_put(THIS_MODULE);
 }
 
#20 lore raw

Re: [PATCH v5 4/7] s390: ap: setup relation betwen KVM and mediated device

Quoted 11 lines · Halil Pasic
Quoted 6 lines · On Wed, 13 Mar 2019 17:05:01 +0100 Pierre Morel <[email protected]> wrote:

When the mediated device is open we setup the relation with KVM unset it when the mediated device is released.

We ensure KVM is present on opening of the mediated device.

We ensure that KVM survives the mediated device, and establish a direct

survives?

what alternative do you prefer?

Quoted 5 lines
Quoted 5 lines

link from KVM to the mediated device to simplify the relationship.

---

...snip...

Quoted 44 lines
Quoted 44 lines

static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
unsigned long action, void *data)
{

drivers/s390/crypto/vfio_ap_ops.c +7 -18
-	int ret;
  	struct ap_matrix_mdev *matrix_mdev;
 
  	if (action != VFIO_GROUP_NOTIFY_SET_KVM)
  		return NOTIFY_OK;
 
  	matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);
-
-	if (!data) {
-		matrix_mdev->kvm = NULL;
-		return NOTIFY_OK;
-	}
-
-	ret = vfio_ap_mdev_set_kvm(matrix_mdev, data);
-	if (ret)
-		return NOTIFY_DONE;
-
-	/* If there is no CRYCB pointer, then we can't copy the masks */
-	if (!matrix_mdev->kvm->arch.crypto.crycbd)
-		return NOTIFY_DONE;
-
-	kvm_arch_crypto_set_masks(matrix_mdev->kvm, matrix_mdev->matrix.apm,
-				  matrix_mdev->matrix.aqm,
-				  matrix_mdev->matrix.adm);
+	matrix_mdev->kvm = data;
 
  	return NOTIFY_OK;
  }
@@ -888,6 +873,12 @@ static int vfio_ap_mdev_open(struct mdev_device *mdev)
  	if (ret)
  		goto err_group;
 
+	/* We do not support opening the mediated device without KVM */
+	if (!matrix_mdev->kvm) {
+		ret = -ENODEV;
+		goto err_group;
+	}
+
  	matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
  	events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
 
Quoted 8 lines

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

Quoted 5 lines
drivers/s390/crypto/vfio_ap_ops.c +1 -0
@@ -896,8 +887,15 @@ static int vfio_ap_mdev_open(struct mdev_device *mdev)
  	if (ret)
  		goto err_iommu;
 
+	ret = vfio_ap_mdev_set_kvm(matrix_mdev);

At this point the matrix_mdev->kvm ain't guaranteed to be valid IMHO. Or am I wrong? If I'm right kvm_get_kvm(matrix_mdev->kvm) could be too late.

What about the if (!matrix_mdev->kvm) 10 lines above ?

Quoted 22 lines
Quoted 22 lines
drivers/s390/crypto/vfio_ap_ops.c +8 -2
+	if (ret)
+		goto err_kvm;
+
  	return 0;
 
+err_kvm:
+	vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
+				 &matrix_mdev->iommu_notifier);
  err_iommu:
  	vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
  				 &matrix_mdev->group_notifier);
@@ -906,19 +904,33 @@ static int vfio_ap_mdev_open(struct mdev_device *mdev)
  	return ret;
  }
 
-static void vfio_ap_mdev_release(struct mdev_device *mdev)
+static int vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
  {
-	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
+	struct kvm *kvm = matrix_mdev->kvm;
 
Quoted 4 lines

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

Quoted 2 lines
drivers/s390/crypto/vfio_ap_ops.c +0 -0
  	if (matrix_mdev->kvm)
  		kvm_arch_crypto_clear_masks(matrix_mdev->kvm);

This still conditional?

Yes, nothing to clear if there is no KVM.

Quoted 8 lines

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

Quoted 3 lines
drivers/s390/crypto/vfio_ap_ops.c +1 -1
-
+	vfio_ap_mdev_reset_queues(matrix_mdev->mdev);

I guess your intention was to move vfio_ap_mdev_reset_queues() here from vfio_ap_mdev_release(), but you still have a vfio_ap_mdev_reset_queues() call in vfio_ap_mdev_release().

Quoted 14 lines
Quoted 4 lines
drivers/s390/crypto/vfio_ap_ops.c +3 -0
+	matrix_mdev->kvm->arch.crypto.pqap_hook = NULL;
  	matrix_mdev->kvm = NULL;
+
+	kvm_put_kvm(kvm);

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

Quoted 10 lines
drivers/s390/crypto/vfio_ap_ops.c +9 -0
+	return 0;
+}
+
+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);
+
  	vfio_ap_mdev_reset_queues(mdev);

right, this one will go away.
Thanks for reviewing.

Regards,
Pierre

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
maintainer
#21 lore raw

Re: [PATCH v5 4/7] s390: ap: setup relation betwen KVM and mediated device

Quoted 7 lines · On Tue, 19 Mar 2019 10:38:42 +0100 Pierre Morel <[email protected]> wrote:
Quoted 6 lines · Halil Pasic
Quoted 3 lines · On Wed, 13 Mar 2019 17:05:01 +0100 Pierre Morel <[email protected]> wrote:

When the mediated device is open we setup the relation with KVM unset it when the mediated device is released.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

We ensure KVM is present on opening of the mediated device.

We ensure that KVM survives the mediated device, and establish a direct

survives?

what alternative do you prefer?

Increase kvm's refcount to ensure the guest is alive when the ap_matrix_mdev is active. An ap mp_matrix becomes active with a successful open() and ceases to be active with a release().

Your sentence was materially wrong as the mdev is allowed to outlive the KVM. BTW survive tends to have an 'in spite of' note to it, which outlive does not. vfio-ap is, I hope, not a calamity that threatens the life of KVM ;). https://en.oxforddictionaries.com/definition/survive

Quoted 56 lines
Quoted 5 lines
Quoted 5 lines

link from KVM to the mediated device to simplify the relationship.

---

...snip...

Quoted 48 lines
Quoted 48 lines

static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
unsigned long action, void *data)
{

drivers/s390/crypto/vfio_ap_ops.c +7 -18
-	int ret;
  	struct ap_matrix_mdev *matrix_mdev;
 
  	if (action != VFIO_GROUP_NOTIFY_SET_KVM)
  		return NOTIFY_OK;
 
  	matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);
-
-	if (!data) {
-		matrix_mdev->kvm = NULL;
-		return NOTIFY_OK;
-	}
-
-	ret = vfio_ap_mdev_set_kvm(matrix_mdev, data);
-	if (ret)
-		return NOTIFY_DONE;
-
-	/* If there is no CRYCB pointer, then we can't copy the masks */
-	if (!matrix_mdev->kvm->arch.crypto.crycbd)
-		return NOTIFY_DONE;
-
-	kvm_arch_crypto_set_masks(matrix_mdev->kvm, matrix_mdev->matrix.apm,
-				  matrix_mdev->matrix.aqm,
-				  matrix_mdev->matrix.adm);
+	matrix_mdev->kvm = data;
 
  	return NOTIFY_OK;
  }
@@ -888,6 +873,12 @@ static int vfio_ap_mdev_open(struct mdev_device *mdev)
  	if (ret)
  		goto err_group;
 
+	/* We do not support opening the mediated device without KVM */
+	if (!matrix_mdev->kvm) {
+		ret = -ENODEV;
+		goto err_group;
+	}
+
  	matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
  	events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
 
@@ -896,8 +887,15 @@ static int vfio_ap_mdev_open(struct mdev_device *mdev)
  	if (ret)
  		goto err_iommu;
 
Quoted 7 lines
Quoted 4 lines

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

Quoted 1 line
drivers/s390/crypto/vfio_ap_ops.c +1 -0
+	ret = vfio_ap_mdev_set_kvm(matrix_mdev);

At this point the matrix_mdev->kvm ain't guaranteed to be valid IMHO. Or am I wrong? If I'm right kvm_get_kvm(matrix_mdev->kvm) could be too late.

What about the if (!matrix_mdev->kvm) 10 lines above ?

That check is not sufficient.

You should do the kvm_get_kvm() in vfio_ap_mdev_group_notifier(). VFIO must ensure that the kvm pointer you get is valid, in a sense that it points to a valid struct kvm and the kvm object is alive, while you are in the callback. But not beyond.

If another thread were to decrement the refcount of the kvm object you would end up with matrix_mdev->kvm pointing to an object that has already died.

Does my analysis make sense to you?

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
drivers/s390/crypto/vfio_ap_ops.c +8 -2
+	if (ret)
+		goto err_kvm;
+
  	return 0;
 
+err_kvm:
+	vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
+				 &matrix_mdev->iommu_notifier);
  err_iommu:
  	vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
  				 &matrix_mdev->group_notifier);
@@ -906,19 +904,33 @@ static int vfio_ap_mdev_open(struct mdev_device *mdev)
  	return ret;
  }
 
-static void vfio_ap_mdev_release(struct mdev_device *mdev)
+static int vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
  {
-	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
+	struct kvm *kvm = matrix_mdev->kvm;
 
Quoted 7 lines
Quoted 4 lines

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

Quoted 2 lines
drivers/s390/crypto/vfio_ap_ops.c +0 -0
  	if (matrix_mdev->kvm)
  		kvm_arch_crypto_clear_masks(matrix_mdev->kvm);

This still conditional?

Yes, nothing to clear if there is no KVM.

Since we have ensured the open only works if there is a KVM at that point in time, and we have taken a reference to KVM, I would expect KVM can not go away before we give up our reference.

Quoted 17 lines
Quoted 17 lines

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

Quoted 3 lines
drivers/s390/crypto/vfio_ap_ops.c +1 -1
-
+	vfio_ap_mdev_reset_queues(matrix_mdev->mdev);

I guess your intention was to move vfio_ap_mdev_reset_queues() here from vfio_ap_mdev_release(), but you still have a vfio_ap_mdev_reset_queues() call in vfio_ap_mdev_release().

Quoted 9 lines
drivers/s390/crypto/vfio_ap_ops.c +8 -0
+	matrix_mdev->kvm->arch.crypto.pqap_hook = NULL;
  	matrix_mdev->kvm = NULL;
+
+	kvm_put_kvm(kvm);
+	return 0;
+}
+
+static void vfio_ap_mdev_release(struct mdev_device *mdev)
+{
Quoted 9 lines
Quoted 5 lines

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

Quoted 5 lines
drivers/s390/crypto/vfio_ap_ops.c +4 -0
+	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
+
+	mutex_lock(&matrix_dev->lock);
+
  	vfio_ap_mdev_reset_queues(mdev);

right, this one will go away.
Thanks for reviewing.

yw

Regards,
Halil

#22 lore raw

Re: [PATCH v5 4/7] s390: ap: setup relation betwen KVM and mediated device

Quoted 14 lines · Halil Pasic
Quoted 11 lines · On Tue, 19 Mar 2019 10:38:42 +0100 Pierre Morel <[email protected]> wrote:
Quoted 10 lines · Halil Pasic
Quoted 6 lines · On Wed, 13 Mar 2019 17:05:01 +0100 Pierre Morel <[email protected]> wrote:

When the mediated device is open we setup the relation with KVM unset it when the mediated device is released.

We ensure KVM is present on opening of the mediated device.

We ensure that KVM survives the mediated device, and establish a direct

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

survives?

what alternative do you prefer?

Increase kvm's refcount to ensure the guest is alive when the ap_matrix_mdev is active. An ap mp_matrix becomes active with a successful open() and ceases to be active with a release().

Right, it is mdev usage not mdev.

Quoted 5 lines

Your sentence was materially wrong as the mdev is allowed to outlive the KVM. BTW survive tends to have an 'in spite of' note to it, which outlive does not. vfio-ap is, I hope, not a calamity that threatens the life of KVM ;). https://en.oxforddictionaries.com/definition/survive

Thanks, your description is much better.

Quoted 72 lines
Quoted 57 lines
Quoted 6 lines
Quoted 6 lines

link from KVM to the mediated device to simplify the relationship.

---

...snip...

Quoted 48 lines
Quoted 48 lines

static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
unsigned long action, void *data)
{

drivers/s390/crypto/vfio_ap_ops.c +7 -18
-	int ret;
   	struct ap_matrix_mdev *matrix_mdev;
 
   	if (action != VFIO_GROUP_NOTIFY_SET_KVM)
   		return NOTIFY_OK;
 
   	matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);
-
-	if (!data) {
-		matrix_mdev->kvm = NULL;
-		return NOTIFY_OK;
-	}
-
-	ret = vfio_ap_mdev_set_kvm(matrix_mdev, data);
-	if (ret)
-		return NOTIFY_DONE;
-
-	/* If there is no CRYCB pointer, then we can't copy the masks */
-	if (!matrix_mdev->kvm->arch.crypto.crycbd)
-		return NOTIFY_DONE;
-
-	kvm_arch_crypto_set_masks(matrix_mdev->kvm, matrix_mdev->matrix.apm,
-				  matrix_mdev->matrix.aqm,
-				  matrix_mdev->matrix.adm);
+	matrix_mdev->kvm = data;
 
   	return NOTIFY_OK;
   }
@@ -888,6 +873,12 @@ static int vfio_ap_mdev_open(struct mdev_device *mdev)
   	if (ret)
   		goto err_group;
 
+	/* We do not support opening the mediated device without KVM */
+	if (!matrix_mdev->kvm) {
+		ret = -ENODEV;
+		goto err_group;
+	}
+
   	matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
   	events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
 
@@ -896,8 +887,15 @@ static int vfio_ap_mdev_open(struct mdev_device *mdev)
   	if (ret)
   		goto err_iommu;
 
Quoted 7 lines
Quoted 4 lines

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

Quoted 1 line
drivers/s390/crypto/vfio_ap_ops.c +1 -0
+	ret = vfio_ap_mdev_set_kvm(matrix_mdev);

At this point the matrix_mdev->kvm ain't guaranteed to be valid IMHO. Or am I wrong? If I'm right kvm_get_kvm(matrix_mdev->kvm) could be too late.

What about the if (!matrix_mdev->kvm) 10 lines above ?

That check is not sufficient.

You should do the kvm_get_kvm() in vfio_ap_mdev_group_notifier(). VFIO must ensure that the kvm pointer you get is valid, in a sense that it points to a valid struct kvm and the kvm object is alive, while you are in the callback. But not beyond.

Quoted 5 lines

If another thread were to decrement the refcount of the kvm object you would end up with matrix_mdev->kvm pointing to an object that has already died.

Does my analysis make sense to you?

Yes thanks the explication is good, it would have been worth to get it the first time.

Quoted 28 lines
Quoted 28 lines
Quoted 27 lines
Quoted 15 lines
drivers/s390/crypto/vfio_ap_ops.c +6 -0
+	if (ret)
+		goto err_kvm;
+
   	return 0;
 
+err_kvm:
+	vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
+				 &matrix_mdev->iommu_notifier);
   err_iommu:
   	vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
   				 &matrix_mdev->group_notifier);
@@ -906,19 +904,33 @@ static int vfio_ap_mdev_open(struct mdev_device *mdev)
   	return ret;

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

Quoted 10 lines
drivers/s390/crypto/vfio_ap_ops.c +2 -2
   }
 
-static void vfio_ap_mdev_release(struct mdev_device *mdev)
+static int vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
   {
-	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
+	struct kvm *kvm = matrix_mdev->kvm;
 
   	if (matrix_mdev->kvm)
   		kvm_arch_crypto_clear_masks(matrix_mdev->kvm);

This still conditional?

Quoted 6 lines
Quoted 2 lines

Yes, nothing to clear if there is no KVM.

Since we have ensured the open only works if there is a KVM at that point in time, and we have taken a reference to KVM, I would expect KVM can not go away before we give up our reference.

Right.

Thanks,
Pierre

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#23 lore raw

Re: [PATCH v5 4/7] s390: ap: setup relation betwen KVM and mediated device

Quoted 137 lines · Pierre Morel
Quoted 14 lines · Halil Pasic
Quoted 11 lines · On Tue, 19 Mar 2019 10:38:42 +0100 Pierre Morel <[email protected]> wrote:
Quoted 10 lines · Halil Pasic
Quoted 6 lines · On Wed, 13 Mar 2019 17:05:01 +0100 Pierre Morel <[email protected]> wrote:

When the mediated device is open we setup the relation with KVM unset it when the mediated device is released.

We ensure KVM is present on opening of the mediated device.

We ensure that KVM survives the mediated device, and establish a direct

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

survives?

what alternative do you prefer?

Increase kvm's refcount to ensure the guest is alive when the ap_matrix_mdev is active. An ap mp_matrix becomes active with a successful open() and ceases to be active with a release().

Right, it is mdev usage not mdev.

Quoted 5 lines

Your sentence was materially wrong as the mdev is allowed to outlive the KVM. BTW survive tends to have an 'in spite of' note to it, which outlive does not. vfio-ap is, I hope, not a calamity that threatens the life of KVM ;). https://en.oxforddictionaries.com/definition/survive

Thanks, your description is much better.

Quoted 69 lines
Quoted 59 lines
Quoted 6 lines
Quoted 6 lines

link from KVM to the mediated device to simplify the relationship.

---

...snip...

Quoted 41 lines
Quoted 41 lines

   static int vfio_ap_mdev_group_notifier(struct notifier_block *nb, unsigned long action, void *data)
   {
-    int ret;
       struct ap_matrix_mdev *matrix_mdev;
       if (action != VFIO_GROUP_NOTIFY_SET_KVM)
           return NOTIFY_OK;
       matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);

drivers/s390/crypto/vfio_ap_ops.c +0 -15
-
-    if (!data) {
-        matrix_mdev->kvm = NULL;
-        return NOTIFY_OK;
-    }
-
-    ret = vfio_ap_mdev_set_kvm(matrix_mdev, data);
-    if (ret)
-        return NOTIFY_DONE;
-
-    /* If there is no CRYCB pointer, then we can't copy the masks */
-    if (!matrix_mdev->kvm->arch.crypto.crycbd)
-        return NOTIFY_DONE;
-
-    kvm_arch_crypto_set_masks(matrix_mdev->kvm, 

matrix_mdev->matrix.apm,
-                  matrix_mdev->matrix.aqm,
-                  matrix_mdev->matrix.adm);
+    matrix_mdev->kvm = data;
       return NOTIFY_OK;
   }

drivers/s390/crypto/vfio_ap_ops.c +6 -0
@@ -888,6 +873,12 @@ static int vfio_ap_mdev_open(struct 
 mdev_device *mdev)
        if (ret)
            goto err_group;
+    /* We do not support opening the mediated device without KVM */
+    if (!matrix_mdev->kvm) {
+        ret = -ENODEV;
+        goto err_group;
+    }
+
        matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
        events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
Quoted 8 lines

drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_open(struct · line 890

Quoted 5 lines
drivers/s390/crypto/vfio_ap_ops.c +1 -0
@@ -896,8 +887,15 @@ static int vfio_ap_mdev_open(struct 
 mdev_device *mdev)
        if (ret)
            goto err_iommu;
+    ret = vfio_ap_mdev_set_kvm(matrix_mdev);

At this point the matrix_mdev->kvm ain't guaranteed to be valid IMHO. Or am I wrong? If I'm right kvm_get_kvm(matrix_mdev->kvm) could be too late.

Quoted 2 lines

What about the if (!matrix_mdev->kvm) 10 lines above ?

That check is not sufficient.

You should do the kvm_get_kvm() in vfio_ap_mdev_group_notifier(). VFIO must ensure that the kvm pointer you get is valid, in a sense that it points to a valid struct kvm and the kvm object is alive, while you are in the callback. But not beyond.

Quoted 5 lines

If another thread were to decrement the refcount of the kvm object you would end up with matrix_mdev->kvm pointing to an object that has already died.

Does my analysis make sense to you?

Yes thanks the explication is good, it would have been worth to get it the first time.

Quoted 26 lines
Quoted 26 lines
Quoted 25 lines
Quoted 13 lines
drivers/s390/crypto/vfio_ap_ops.c +6 -0
+    if (ret)
+        goto err_kvm;
+
        return 0;
+err_kvm:
+    vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
+                 &matrix_mdev->iommu_notifier);

   err_iommu:
       vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY, &matrix_mdev->group_notifier);

drivers/s390/crypto/vfio_ap_ops.c static int vfio_ap_mdev_open(struct · line 911

Quoted 10 lines
drivers/s390/crypto/vfio_ap_ops.c +2 -2
@@ -906,19 +904,33 @@ static int vfio_ap_mdev_open(struct 
 mdev_device *mdev)
        return ret;
    }
-static void vfio_ap_mdev_release(struct mdev_device *mdev)
+static int vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
    {
-    struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
+    struct kvm *kvm = matrix_mdev->kvm;
        if (matrix_mdev->kvm)
            kvm_arch_crypto_clear_masks(matrix_mdev->kvm);

This still conditional?

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Yes, nothing to clear if there is no KVM.

Since we have ensured the open only works if there is a KVM at that point in time, and we have taken a reference to KVM, I would expect KVM can not go away before we give up our reference.

Right.

Right but based on the assumption we do a kvm_get_kvm() during open.

But now we will do it inside the notifier, so the logic is to do a kvm_put_kvm in the notifier too. This is important because userland will ask us to release the KVM/VFIO link through this notifier. So I will have to rework this part where KVM==NULL in the notifier too.

Regards,
Pierre

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
maintainer
#24 lore raw

Re: [PATCH v5 4/7] s390: ap: setup relation betwen KVM and mediated device

Quoted 16 lines · On Tue, 19 Mar 2019 15:47:05 +0100 Pierre Morel <[email protected]> wrote:
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

       if (matrix_mdev->kvm) kvm_arch_crypto_clear_masks(matrix_mdev->kvm);

This still conditional?

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Yes, nothing to clear if there is no KVM.

Since we have ensured the open only works if there is a KVM at that point in time, and we have taken a reference to KVM, I would expect KVM can not go away before we give up our reference.

Right.

Right but based on the assumption we do a kvm_get_kvm() during open.

Quoted 8 lines

But now we will do it inside the notifier, so the logic is to do a kvm_put_kvm in the notifier too.
This is important because userland will ask us to release the KVM/VFIO link through this notifier.
So I will have to rework this part where KVM==NULL in the notifier too.

Regards,
Pierre

I think it can be done both ways. If you ensure KVM != NULL if the open succeeds and take the reference in the notifier. I suppose if open() fails release() won't be called. But the logic/code in open() would get quite ugly because the callback could be called assync so that it overlaps with the rest of open().

Not failing open() in case of no KVM is there yet is in my opinion cleaner anyway.

Regards,
Halil

#25 lore raw

Re: [PATCH v5 4/7] s390: ap: setup relation betwen KVM and mediated device

Quoted 25 lines · Halil Pasic
Quoted 22 lines · On Tue, 19 Mar 2019 15:47:05 +0100 Pierre Morel <[email protected]> wrote:
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

       if (matrix_mdev->kvm) kvm_arch_crypto_clear_masks(matrix_mdev->kvm);

This still conditional?

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Yes, nothing to clear if there is no KVM.

Since we have ensured the open only works if there is a KVM at that point in time, and we have taken a reference to KVM, I would expect KVM can not go away before we give up our reference.

Right.

Right but based on the assumption we do a kvm_get_kvm() during open.

But now we will do it inside the notifier, so the logic is to do a kvm_put_kvm in the notifier too.
This is important because userland will ask us to release the KVM/VFIO link through this notifier.
So I will have to rework this part where KVM==NULL in the notifier too.

Quoted 8 lines
Quoted 2 lines

Regards,
Pierre

I think it can be done both ways. If you ensure KVM != NULL if the open succeeds and take the reference in the notifier. I suppose if open() fails release() won't be called. But the logic/code in open() would get quite ugly because the callback could be called assync so that it overlaps with the rest of open().

Not necessary, but there is more than just the kvm_get_kvm().

When the user calls KVM_DEV_VFIO_GROUP_DEL he asks to break the link between VFIO and KVM.

Currently we just ignore this instead of stopping all activity associated with KVM.

But we have more bugs there:
We should not support multiple open of the mdev which will overwrite matrix->kvm for the same mdev with a different KVM. I send a bugfix for this.

Quoted 3 lines

Not failing open() in case of no KVM is there yet is in my opinion cleaner anyway.

If we handle correctly the notifiers and the exclusivity, we can do this. I will make this correctly for the next iteration.

Regards,
Pierre

Quoted 4 lines

Regards,
Halil

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#26 lore raw

PATCH v5 5/7 s390: ap: implement PAPQ AQIC interception in kernel

In reply to Pierre Morel

We register the AP PQAP instruction hook during the open of the mediated device. And unregister it on release.

In the AP PQAP instruction hook, if we receive a demand to enable IRQs,
- we retrieve the vfio_ap_queue based on the APQN we receive in REG1,
- we retrieve the page of the guest address, (NIB), from register REG2
- we the mediated device to use the VFIO pinning infratrsucture to pin the page of the guest address,
- we retrieve the pointer to KVM to register the guest ISC and retrieve the host ISC
- finaly we activate GISA

If we receive a demand to disable IRQs,
- we deactivate GISA
- unregister from the GIB
- unping the NIB

---
 drivers/s390/crypto/ap_bus.h          |   1 +
 drivers/s390/crypto/vfio_ap_drv.c     |   2 +
 drivers/s390/crypto/vfio_ap_ops.c     | 204 +++++++++++++++++++++++++++++++++-
 drivers/s390/crypto/vfio_ap_private.h |   6 +
 4 files changed, 210 insertions(+), 3 deletions(-)
drivers/s390/crypto/ap_bus.h +1 -0
index d0059ea..9a4fd96 100644
@@ -43,6 +43,7 @@ static inline int ap_test_bit(unsigned int *ptr, unsigned int nr)
 #define AP_RESPONSE_BUSY		0x05
 #define AP_RESPONSE_INVALID_ADDRESS	0x06
 #define AP_RESPONSE_OTHERWISE_CHANGED	0x07
+#define AP_RESPONSE_INVALID_GISA	0x08
 #define AP_RESPONSE_Q_FULL		0x10
 #define AP_RESPONSE_NO_PENDING_REPLY	0x10
 #define AP_RESPONSE_INDEX_TOO_BIG	0x11
drivers/s390/crypto/vfio_ap_drv.c +2 -0
index df6f21a..796e73d4 100644
@@ -55,6 +55,8 @@ static int vfio_ap_queue_dev_probe(struct ap_device *apdev)
 		return -ENOMEM;
 	dev_set_drvdata(&apdev->device, q);
 	q->apqn = to_ap_queue(&apdev->device)->qid;
+	q->a_isc = VFIO_AP_ISC_INVALID;
+	q->p_isc = VFIO_AP_ISC_INVALID;
 	INIT_LIST_HEAD(&q->list);
 	mutex_lock(&matrix_dev->lock);
 	list_add(&q->list, &matrix_dev->free_list);
drivers/s390/crypto/vfio_ap_ops.c +201 -3

242 lines not on this page · raw message

drivers/s390/crypto/vfio_ap_private.h +6 -0
index 4a287c8..968d8aa 100644
@@ -95,6 +95,12 @@ extern void vfio_ap_mdev_unregister(void);
 struct vfio_ap_queue {
 	struct list_head list;
 	struct ap_matrix_mdev *matrix_mdev;
+	unsigned long a_nib;
+	unsigned long a_pfn;
+	unsigned long p_pfn;
 	int	apqn;
+#define VFIO_AP_ISC_INVALID 0xff
+	unsigned char a_isc;
+	unsigned char p_isc;
 };
 #endif /* _VFIO_AP_PRIVATE_H_ */
Signature · 1 line
2.7.4
#27 lore raw

PATCH v5 6/7 s390: ap: Cleanup on removing the AP device

In reply to Pierre Morel

When a AP device is remove, clear the queue's APID bit in the guest CRYCB. to be sure that the guest will not access the AP queue anymore.

Then we clear the interruptions and reset the AP device properly.

---
 drivers/s390/crypto/vfio_ap_drv.c     | 36 +++++++++++++++++++++++++++++++++++
 drivers/s390/crypto/vfio_ap_ops.c     | 16 +++++++++++++---
 drivers/s390/crypto/vfio_ap_private.h |  3 +++
 3 files changed, 52 insertions(+), 3 deletions(-)
drivers/s390/crypto/vfio_ap_drv.c +36 -0
index 796e73d4..850ba6e 100644
@@ -5,6 +5,7 @@
  * Copyright IBM Corp. 2018
  *
  * Author(s): Tony Krowiak <[email protected]>
+ *	      Pierre Morel <[email protected]>
  */
 
 #include <linux/module.h>
@@ -12,6 +13,8 @@
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <asm/facility.h>
+#include <linux/bitops.h>
+#include <linux/kvm_host.h>
 #include "vfio_ap_private.h"
 
 #define VFIO_AP_ROOT_NAME "vfio_ap"
@@ -65,6 +68,33 @@ static int vfio_ap_queue_dev_probe(struct ap_device *apdev)
 }
 
 /**
+ * vfio_ap_update_crycb
+ * @q: A pointer to the queue being removed
+ *
+ * We clear the APID of the queue, making this queue unusable for the guest.
+ * After this function we can reset the queue without to fear a race with
+ * the guest to access the queue again.
+ * We do not fear race with the host as we still get the device.
+ */
+static void vfio_ap_update_crycb(struct vfio_ap_queue *q)
+{
+	struct ap_matrix_mdev *matrix_mdev = q->matrix_mdev;
+
+	if (!matrix_mdev)
+		return;
+
+	clear_bit_inv(AP_QID_CARD(q->apqn), matrix_mdev->matrix.apm);
+
+	if (!matrix_mdev->kvm)
+		return;
+
+	kvm_arch_crypto_set_masks(matrix_mdev->kvm,
+				  matrix_mdev->matrix.apm,
+				  matrix_mdev->matrix.aqm,
+				  matrix_mdev->matrix.adm);
+}
+
+/**
  * vfio_ap_queue_dev_remove:
  *
  * Free the associated vfio_ap_queue structure
@@ -74,7 +104,13 @@ static void vfio_ap_queue_dev_remove(struct ap_device *apdev)
 	struct vfio_ap_queue *q;
 
 	q = dev_get_drvdata(&apdev->device);
+	if (!q)
+		return;
+
 	mutex_lock(&matrix_dev->lock);
+	vfio_ap_update_crycb(q);
+	vfio_ap_mdev_reset_queue(q);
+	vfio_ap_free_irq(q);
 	list_del(&q->list);
 	mutex_unlock(&matrix_dev->lock);
 	kfree(q);
drivers/s390/crypto/vfio_ap_ops.c +13 -3
index dc3454f..dc3caa7 100644
@@ -43,15 +43,22 @@ struct vfio_ap_queue *vfio_ap_get_queue(int apqn, struct list_head *l)
 	return NULL;
 }
 
-static int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q)
+int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q)
 {
 	struct ap_queue_status status;
-	int retry = 1;
+	int retry = 20;
 
 	do {
 		status = ap_zapq(q->apqn);
 		switch (status.response_code) {
 		case AP_RESPONSE_NORMAL:
+			while (!status.queue_empty && retry--) {
+				msleep(20);
+				status = ap_tapq(q->apqn, NULL);
+			}
+			if (retry <= 0)
+				pr_warn("%s: queue 0x%04x not empty\n",
+					__func__, q->apqn);
 			return 0;
 		case AP_RESPONSE_RESET_IN_PROGRESS:
 		case AP_RESPONSE_BUSY:
@@ -74,7 +81,7 @@ static int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q)
  * Unregister the ISC from the GIB alert
  * Clear the vfio_ap_queue intern fields
  */
-static void vfio_ap_free_irq(struct vfio_ap_queue *q)
+void vfio_ap_free_irq(struct vfio_ap_queue *q)
 {
 	if (!q)
 		return;
@@ -300,6 +307,7 @@ static int vfio_ap_mdev_remove(struct mdev_device *mdev)
 	list_for_each_entry_safe(q, qtmp, &matrix_mdev->qlist, list) {
 		q->matrix_mdev = NULL;
 		vfio_ap_mdev_reset_queue(q);
+		vfio_ap_free_irq(q);
 		list_move(&q->list, &matrix_dev->free_list);
 	}
 	list_del(&matrix_mdev->node);
@@ -362,6 +370,7 @@ static void vfio_ap_free_queue(int apqn, struct ap_matrix_mdev *matrix_mdev)
 		return;
 	q->matrix_mdev = NULL;
 	vfio_ap_mdev_reset_queue(q);
+	vfio_ap_free_irq(q);
 	list_move(&q->list, &matrix_dev->free_list);
 }
 
@@ -1041,6 +1050,7 @@ static int vfio_ap_mdev_reset_queues(struct mdev_device *mdev)
 
 	list_for_each_entry(q, &matrix_mdev->qlist, list) {
 		ret = vfio_ap_mdev_reset_queue(q);
+		vfio_ap_free_irq(q);
 		/*
 		 * Regardless whether a queue turns out to be busy, or
 		 * is not operational, we need to continue resetting
drivers/s390/crypto/vfio_ap_private.h +3 -0
index 968d8aa..9fe580b 100644
@@ -4,6 +4,7 @@
  *
  * Author(s): Tony Krowiak <[email protected]>
  *	      Halil Pasic <[email protected]>
+ *	      Pierre Morel <[email protected]>
  *
  * Copyright IBM Corp. 2018
  */
@@ -103,4 +104,6 @@ struct vfio_ap_queue {
 	unsigned char a_isc;
 	unsigned char p_isc;
 };
+void vfio_ap_free_irq(struct vfio_ap_queue *q);
+int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q);
 #endif /* _VFIO_AP_PRIVATE_H_ */
Signature · 1 line
2.7.4
#28 lore raw

PATCH v5 7/7 s390: ap: kvm: Enable PQAP/AQIC facility for the guest

In reply to Pierre Morel

AP Queue Interruption Control (AQIC) facility gives the guest the possibility to control interruption for the Cryptographic Adjunct Processor queues.

---
 arch/s390/tools/gen_facilities.c | 1 +
 1 file changed, 1 insertion(+)
arch/s390/tools/gen_facilities.c +1 -0
index 04c5f07..be4b826 100644
@@ -111,6 +111,7 @@ static struct facility_def facility_defs[] = {
 		.bits = (int[]){
 			12, /* AP Query Configuration Information */
 			15, /* AP Facilities Test */
+			65, /* AP Queue Interruption Control */
 			156, /* etoken facility */
 			-1  /* END */
 		}
Signature · 1 line
2.7.4