Re: [PATCH v2 07/15] KVM: s390: Interfaces to configure/deconfigure guest's AP matrix

2018-03-08 Thread Tony Krowiak

On 02/27/2018 09:28 AM, Tony Krowiak wrote:

Provides interfaces to assign AP adapters, usage domains
and control domains to a KVM guest.

A KVM guest is started by executing the Start Interpretive Execution (SIE)
instruction. The SIE state description is a control block that contains the
state information for a KVM guest and is supplied as input to the SIE
instruction. The SIE state description has a satellite structure called the
Crypto Control Block (CRYCB). The CRYCB contains three bitmask fields
identifying the adapters, queues (domains) and control domains assigned to
the KVM guest:

* The AP Adapter Mask (APM) field identifies the AP adapters assigned to
   the KVM guest

* The AP Queue Mask (AQM) field identifies the AP queues assigned to
   the KVM guest. Each AP queue is connected to a usage domain within
   an AP adapter.

* The AP Domain Mask (ADM) field identifies the control domains
   assigned to the KVM guest.

Each adapter, queue (usage domain) and control domain are identified by
a number from 0 to 255. The bits in each mask, from most significant to
least significant bit, correspond to the numbers 0-255. When a bit is
set, the corresponding adapter, queue (usage domain) or control domain
is assigned to the KVM guest.

This patch will set the bits in the APM, AQM and ADM fields of the
CRYCB referenced by the KVM guest's SIE state description. The process
used is:

1. Verify that the bits to be set do not exceed the maximum bit
number for the given mask.

2. Verify that the APQNs that can be derived from the intersection
of the bits set in the APM and AQM fields of the KVM guest's CRYCB
are not assigned to any other KVM guest running on the same linux
host.

3. Set the APM, AQM and ADM in the CRYCB according to the matrix
configured for the mediated matrix device via its sysfs
adapter, domain and control domain attribute files respectively.

Signed-off-by: Tony Krowiak 
---
  arch/s390/include/asm/kvm-ap.h|   36 +
  arch/s390/kvm/kvm-ap.c|  257 +
  drivers/s390/crypto/vfio_ap_ops.c |   19 +++
  drivers/s390/crypto/vfio_ap_private.h |4 +
  4 files changed, 316 insertions(+), 0 deletions(-)

diff --git a/arch/s390/include/asm/kvm-ap.h b/arch/s390/include/asm/kvm-ap.h
index ef749e7..46e7c5b 100644
--- a/arch/s390/include/asm/kvm-ap.h
+++ b/arch/s390/include/asm/kvm-ap.h
@@ -10,9 +10,45 @@
  #define _ASM_KVM_AP
  #include 
  #include 
+#include 
+#include 
+#include 
+
+#define KVM_AP_MASK_BYTES(n)(n / BITS_PER_BYTE)
+
+/**
+ * The AP matrix is comprised of three bit masks identifying the adapters,
+ * queues (domains) and control domains that belong to an AP matrix. The bits 
in
+ * each mask, from least significant to most significant bit, correspond to IDs
+ * 0 to the maximum ID allowed for a given mask. When a bit is set, the
+ * corresponding ID belongs to the matrix.
+ *
+ * @apm_max: max number of bits in @apm
+ * @apm identifies the AP adapters in the matrix
+ * @aqm_max: max number of bits in @aqm
+ * @aqm identifies the AP queues (domains) in the matrix
+ * @adm_max: max number of bits in @adm
+ * @adm identifies the AP control domains in the matrix
+ */
+struct kvm_ap_matrix {
+   int apm_max;
+   unsigned long *apm;
+   int aqm_max;
+   unsigned long *aqm;
+   int adm_max;
+   unsigned long *adm;
+};

  void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd);

  int kvm_ap_get_crycb_format(struct kvm *kvm);

+int kvm_ap_matrix_create(struct kvm_ap_matrix **ap_matrix);
+
+void kvm_ap_matrix_destroy(struct kvm_ap_matrix *ap_matrix);
+
+int kvm_ap_configure_matrix(struct kvm *kvm, struct kvm_ap_matrix *matrix);
+
+void kvm_ap_deconfigure_matrix(struct kvm *kvm);
+
  #endif /* _ASM_KVM_AP */
diff --git a/arch/s390/kvm/kvm-ap.c b/arch/s390/kvm/kvm-ap.c
index bafe63b..bb29045 100644
--- a/arch/s390/kvm/kvm-ap.c
+++ b/arch/s390/kvm/kvm-ap.c
@@ -8,6 +8,7 @@

  #include 
  #include 
+#include 

  #include "kvm-s390.h"

@@ -16,6 +17,125 @@ int kvm_ap_get_crycb_format(struct kvm *kvm)
return kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK;
  }

+static inline void kvm_ap_clear_crycb_masks(struct kvm *kvm)
+{
+   int crycb_fmt = kvm_ap_get_crycb_format(kvm);
+
+   if (crycb_fmt == CRYCB_FORMAT2)
+   memset(>arch.crypto.crycb->apcb1, 0,
+  sizeof(kvm->arch.crypto.crycb->apcb1));
+   else
+   memset(>arch.crypto.crycb->apcb0, 0,
+  sizeof(kvm->arch.crypto.crycb->apcb0));
+}
+
+static inline unsigned long *kvm_ap_get_crycb_apm(struct kvm *kvm)
+{
+   unsigned long *apm;
+   int crycb_fmt = kvm_ap_get_crycb_format(kvm);
+
+   if (crycb_fmt == CRYCB_FORMAT2)
+   apm = (unsigned long *)kvm->arch.crypto.crycb->apcb1.apm;
+   else
+   apm = (unsigned long *)kvm->arch.crypto.crycb->apcb0.apm;
+
+   return apm;
+}
+
+static inline 

Re: [PATCH v2 07/15] KVM: s390: Interfaces to configure/deconfigure guest's AP matrix

2018-03-08 Thread Tony Krowiak

On 02/27/2018 09:28 AM, Tony Krowiak wrote:

Provides interfaces to assign AP adapters, usage domains
and control domains to a KVM guest.

A KVM guest is started by executing the Start Interpretive Execution (SIE)
instruction. The SIE state description is a control block that contains the
state information for a KVM guest and is supplied as input to the SIE
instruction. The SIE state description has a satellite structure called the
Crypto Control Block (CRYCB). The CRYCB contains three bitmask fields
identifying the adapters, queues (domains) and control domains assigned to
the KVM guest:

* The AP Adapter Mask (APM) field identifies the AP adapters assigned to
   the KVM guest

* The AP Queue Mask (AQM) field identifies the AP queues assigned to
   the KVM guest. Each AP queue is connected to a usage domain within
   an AP adapter.

* The AP Domain Mask (ADM) field identifies the control domains
   assigned to the KVM guest.

Each adapter, queue (usage domain) and control domain are identified by
a number from 0 to 255. The bits in each mask, from most significant to
least significant bit, correspond to the numbers 0-255. When a bit is
set, the corresponding adapter, queue (usage domain) or control domain
is assigned to the KVM guest.

This patch will set the bits in the APM, AQM and ADM fields of the
CRYCB referenced by the KVM guest's SIE state description. The process
used is:

1. Verify that the bits to be set do not exceed the maximum bit
number for the given mask.

2. Verify that the APQNs that can be derived from the intersection
of the bits set in the APM and AQM fields of the KVM guest's CRYCB
are not assigned to any other KVM guest running on the same linux
host.

3. Set the APM, AQM and ADM in the CRYCB according to the matrix
configured for the mediated matrix device via its sysfs
adapter, domain and control domain attribute files respectively.

Signed-off-by: Tony Krowiak 
---
  arch/s390/include/asm/kvm-ap.h|   36 +
  arch/s390/kvm/kvm-ap.c|  257 +
  drivers/s390/crypto/vfio_ap_ops.c |   19 +++
  drivers/s390/crypto/vfio_ap_private.h |4 +
  4 files changed, 316 insertions(+), 0 deletions(-)

diff --git a/arch/s390/include/asm/kvm-ap.h b/arch/s390/include/asm/kvm-ap.h
index ef749e7..46e7c5b 100644
--- a/arch/s390/include/asm/kvm-ap.h
+++ b/arch/s390/include/asm/kvm-ap.h
@@ -10,9 +10,45 @@
  #define _ASM_KVM_AP
  #include 
  #include 
+#include 
+#include 
+#include 
+
+#define KVM_AP_MASK_BYTES(n)(n / BITS_PER_BYTE)
+
+/**
+ * The AP matrix is comprised of three bit masks identifying the adapters,
+ * queues (domains) and control domains that belong to an AP matrix. The bits 
in
+ * each mask, from least significant to most significant bit, correspond to IDs
+ * 0 to the maximum ID allowed for a given mask. When a bit is set, the
+ * corresponding ID belongs to the matrix.
+ *
+ * @apm_max: max number of bits in @apm
+ * @apm identifies the AP adapters in the matrix
+ * @aqm_max: max number of bits in @aqm
+ * @aqm identifies the AP queues (domains) in the matrix
+ * @adm_max: max number of bits in @adm
+ * @adm identifies the AP control domains in the matrix
+ */
+struct kvm_ap_matrix {
+   int apm_max;
+   unsigned long *apm;
+   int aqm_max;
+   unsigned long *aqm;
+   int adm_max;
+   unsigned long *adm;
+};

  void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd);

  int kvm_ap_get_crycb_format(struct kvm *kvm);

+int kvm_ap_matrix_create(struct kvm_ap_matrix **ap_matrix);
+
+void kvm_ap_matrix_destroy(struct kvm_ap_matrix *ap_matrix);
+
+int kvm_ap_configure_matrix(struct kvm *kvm, struct kvm_ap_matrix *matrix);
+
+void kvm_ap_deconfigure_matrix(struct kvm *kvm);
+
  #endif /* _ASM_KVM_AP */
diff --git a/arch/s390/kvm/kvm-ap.c b/arch/s390/kvm/kvm-ap.c
index bafe63b..bb29045 100644
--- a/arch/s390/kvm/kvm-ap.c
+++ b/arch/s390/kvm/kvm-ap.c
@@ -8,6 +8,7 @@

  #include 
  #include 
+#include 

  #include "kvm-s390.h"

@@ -16,6 +17,125 @@ int kvm_ap_get_crycb_format(struct kvm *kvm)
return kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK;
  }

+static inline void kvm_ap_clear_crycb_masks(struct kvm *kvm)
+{
+   int crycb_fmt = kvm_ap_get_crycb_format(kvm);
+
+   if (crycb_fmt == CRYCB_FORMAT2)
+   memset(>arch.crypto.crycb->apcb1, 0,
+  sizeof(kvm->arch.crypto.crycb->apcb1));
+   else
+   memset(>arch.crypto.crycb->apcb0, 0,
+  sizeof(kvm->arch.crypto.crycb->apcb0));
+}
+
+static inline unsigned long *kvm_ap_get_crycb_apm(struct kvm *kvm)
+{
+   unsigned long *apm;
+   int crycb_fmt = kvm_ap_get_crycb_format(kvm);
+
+   if (crycb_fmt == CRYCB_FORMAT2)
+   apm = (unsigned long *)kvm->arch.crypto.crycb->apcb1.apm;
+   else
+   apm = (unsigned long *)kvm->arch.crypto.crycb->apcb0.apm;
+
+   return apm;
+}
+
+static inline unsigned long 

Re: [PATCH v2 07/15] KVM: s390: Interfaces to configure/deconfigure guest's AP matrix

2018-02-28 Thread Tony Krowiak

On 02/27/2018 09:28 AM, Tony Krowiak wrote:

Provides interfaces to assign AP adapters, usage domains
and control domains to a KVM guest.

A KVM guest is started by executing the Start Interpretive Execution (SIE)
instruction. The SIE state description is a control block that contains the
state information for a KVM guest and is supplied as input to the SIE
instruction. The SIE state description has a satellite structure called the
Crypto Control Block (CRYCB). The CRYCB contains three bitmask fields
identifying the adapters, queues (domains) and control domains assigned to
the KVM guest:

* The AP Adapter Mask (APM) field identifies the AP adapters assigned to
   the KVM guest

* The AP Queue Mask (AQM) field identifies the AP queues assigned to
   the KVM guest. Each AP queue is connected to a usage domain within
   an AP adapter.

* The AP Domain Mask (ADM) field identifies the control domains
   assigned to the KVM guest.

Each adapter, queue (usage domain) and control domain are identified by
a number from 0 to 255. The bits in each mask, from most significant to
least significant bit, correspond to the numbers 0-255. When a bit is
set, the corresponding adapter, queue (usage domain) or control domain
is assigned to the KVM guest.

This patch will set the bits in the APM, AQM and ADM fields of the
CRYCB referenced by the KVM guest's SIE state description. The process
used is:

1. Verify that the bits to be set do not exceed the maximum bit
number for the given mask.

2. Verify that the APQNs that can be derived from the intersection
of the bits set in the APM and AQM fields of the KVM guest's CRYCB
are not assigned to any other KVM guest running on the same linux
host.

3. Set the APM, AQM and ADM in the CRYCB according to the matrix
configured for the mediated matrix device via its sysfs
adapter, domain and control domain attribute files respectively.

Signed-off-by: Tony Krowiak 
---
  arch/s390/include/asm/kvm-ap.h|   36 +
  arch/s390/kvm/kvm-ap.c|  257 +
  drivers/s390/crypto/vfio_ap_ops.c |   19 +++
  drivers/s390/crypto/vfio_ap_private.h |4 +
  4 files changed, 316 insertions(+), 0 deletions(-)

diff --git a/arch/s390/include/asm/kvm-ap.h b/arch/s390/include/asm/kvm-ap.h
index ef749e7..46e7c5b 100644
--- a/arch/s390/include/asm/kvm-ap.h
+++ b/arch/s390/include/asm/kvm-ap.h
@@ -10,9 +10,45 @@
  #define _ASM_KVM_AP
  #include 
  #include 
+#include 
+#include 
+#include 
+
+#define KVM_AP_MASK_BYTES(n)(n / BITS_PER_BYTE)

This macro is accurate only if (n % BITS_PER_BYTE) == 0.
There is a BITS_TO_BYTES macro in tools/include/linux/bitops.h that does 
the

job, but that header file is not available to kvm-ap.h. I'm going to steal
the concept and make the following change:

#define KVM_AP_MASK_BYTES(n)DIV_ROUND_UP(n, BITS_PER_BYTE)

+
+/**
+ * The AP matrix is comprised of three bit masks identifying the adapters,
+ * queues (domains) and control domains that belong to an AP matrix. The bits 
in
+ * each mask, from least significant to most significant bit, correspond to IDs
+ * 0 to the maximum ID allowed for a given mask. When a bit is set, the
+ * corresponding ID belongs to the matrix.
+ *
+ * @apm_max: max number of bits in @apm
+ * @apm identifies the AP adapters in the matrix
+ * @aqm_max: max number of bits in @aqm
+ * @aqm identifies the AP queues (domains) in the matrix
+ * @adm_max: max number of bits in @adm
+ * @adm identifies the AP control domains in the matrix
+ */
+struct kvm_ap_matrix {
+   int apm_max;
+   unsigned long *apm;
+   int aqm_max;
+   unsigned long *aqm;
+   int adm_max;
+   unsigned long *adm;
+};

  void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd);

  int kvm_ap_get_crycb_format(struct kvm *kvm);

+int kvm_ap_matrix_create(struct kvm_ap_matrix **ap_matrix);
+
+void kvm_ap_matrix_destroy(struct kvm_ap_matrix *ap_matrix);
+
+int kvm_ap_configure_matrix(struct kvm *kvm, struct kvm_ap_matrix *matrix);
+
+void kvm_ap_deconfigure_matrix(struct kvm *kvm);
+
  #endif /* _ASM_KVM_AP */
diff --git a/arch/s390/kvm/kvm-ap.c b/arch/s390/kvm/kvm-ap.c
index bafe63b..bb29045 100644
--- a/arch/s390/kvm/kvm-ap.c
+++ b/arch/s390/kvm/kvm-ap.c
@@ -8,6 +8,7 @@

  #include 
  #include 
+#include 

  #include "kvm-s390.h"

@@ -16,6 +17,125 @@ int kvm_ap_get_crycb_format(struct kvm *kvm)
return kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK;
  }

+static inline void kvm_ap_clear_crycb_masks(struct kvm *kvm)
+{
+   int crycb_fmt = kvm_ap_get_crycb_format(kvm);
+
+   if (crycb_fmt == CRYCB_FORMAT2)
+   memset(>arch.crypto.crycb->apcb1, 0,
+  sizeof(kvm->arch.crypto.crycb->apcb1));
+   else
+   memset(>arch.crypto.crycb->apcb0, 0,
+  sizeof(kvm->arch.crypto.crycb->apcb0));
+}
+
+static inline unsigned long *kvm_ap_get_crycb_apm(struct kvm *kvm)
+{
+   

Re: [PATCH v2 07/15] KVM: s390: Interfaces to configure/deconfigure guest's AP matrix

2018-02-28 Thread Tony Krowiak

On 02/27/2018 09:28 AM, Tony Krowiak wrote:

Provides interfaces to assign AP adapters, usage domains
and control domains to a KVM guest.

A KVM guest is started by executing the Start Interpretive Execution (SIE)
instruction. The SIE state description is a control block that contains the
state information for a KVM guest and is supplied as input to the SIE
instruction. The SIE state description has a satellite structure called the
Crypto Control Block (CRYCB). The CRYCB contains three bitmask fields
identifying the adapters, queues (domains) and control domains assigned to
the KVM guest:

* The AP Adapter Mask (APM) field identifies the AP adapters assigned to
   the KVM guest

* The AP Queue Mask (AQM) field identifies the AP queues assigned to
   the KVM guest. Each AP queue is connected to a usage domain within
   an AP adapter.

* The AP Domain Mask (ADM) field identifies the control domains
   assigned to the KVM guest.

Each adapter, queue (usage domain) and control domain are identified by
a number from 0 to 255. The bits in each mask, from most significant to
least significant bit, correspond to the numbers 0-255. When a bit is
set, the corresponding adapter, queue (usage domain) or control domain
is assigned to the KVM guest.

This patch will set the bits in the APM, AQM and ADM fields of the
CRYCB referenced by the KVM guest's SIE state description. The process
used is:

1. Verify that the bits to be set do not exceed the maximum bit
number for the given mask.

2. Verify that the APQNs that can be derived from the intersection
of the bits set in the APM and AQM fields of the KVM guest's CRYCB
are not assigned to any other KVM guest running on the same linux
host.

3. Set the APM, AQM and ADM in the CRYCB according to the matrix
configured for the mediated matrix device via its sysfs
adapter, domain and control domain attribute files respectively.

Signed-off-by: Tony Krowiak 
---
  arch/s390/include/asm/kvm-ap.h|   36 +
  arch/s390/kvm/kvm-ap.c|  257 +
  drivers/s390/crypto/vfio_ap_ops.c |   19 +++
  drivers/s390/crypto/vfio_ap_private.h |4 +
  4 files changed, 316 insertions(+), 0 deletions(-)

diff --git a/arch/s390/include/asm/kvm-ap.h b/arch/s390/include/asm/kvm-ap.h
index ef749e7..46e7c5b 100644
--- a/arch/s390/include/asm/kvm-ap.h
+++ b/arch/s390/include/asm/kvm-ap.h
@@ -10,9 +10,45 @@
  #define _ASM_KVM_AP
  #include 
  #include 
+#include 
+#include 
+#include 
+
+#define KVM_AP_MASK_BYTES(n)(n / BITS_PER_BYTE)

This macro is accurate only if (n % BITS_PER_BYTE) == 0.
There is a BITS_TO_BYTES macro in tools/include/linux/bitops.h that does 
the

job, but that header file is not available to kvm-ap.h. I'm going to steal
the concept and make the following change:

#define KVM_AP_MASK_BYTES(n)DIV_ROUND_UP(n, BITS_PER_BYTE)

+
+/**
+ * The AP matrix is comprised of three bit masks identifying the adapters,
+ * queues (domains) and control domains that belong to an AP matrix. The bits 
in
+ * each mask, from least significant to most significant bit, correspond to IDs
+ * 0 to the maximum ID allowed for a given mask. When a bit is set, the
+ * corresponding ID belongs to the matrix.
+ *
+ * @apm_max: max number of bits in @apm
+ * @apm identifies the AP adapters in the matrix
+ * @aqm_max: max number of bits in @aqm
+ * @aqm identifies the AP queues (domains) in the matrix
+ * @adm_max: max number of bits in @adm
+ * @adm identifies the AP control domains in the matrix
+ */
+struct kvm_ap_matrix {
+   int apm_max;
+   unsigned long *apm;
+   int aqm_max;
+   unsigned long *aqm;
+   int adm_max;
+   unsigned long *adm;
+};

  void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd);

  int kvm_ap_get_crycb_format(struct kvm *kvm);

+int kvm_ap_matrix_create(struct kvm_ap_matrix **ap_matrix);
+
+void kvm_ap_matrix_destroy(struct kvm_ap_matrix *ap_matrix);
+
+int kvm_ap_configure_matrix(struct kvm *kvm, struct kvm_ap_matrix *matrix);
+
+void kvm_ap_deconfigure_matrix(struct kvm *kvm);
+
  #endif /* _ASM_KVM_AP */
diff --git a/arch/s390/kvm/kvm-ap.c b/arch/s390/kvm/kvm-ap.c
index bafe63b..bb29045 100644
--- a/arch/s390/kvm/kvm-ap.c
+++ b/arch/s390/kvm/kvm-ap.c
@@ -8,6 +8,7 @@

  #include 
  #include 
+#include 

  #include "kvm-s390.h"

@@ -16,6 +17,125 @@ int kvm_ap_get_crycb_format(struct kvm *kvm)
return kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK;
  }

+static inline void kvm_ap_clear_crycb_masks(struct kvm *kvm)
+{
+   int crycb_fmt = kvm_ap_get_crycb_format(kvm);
+
+   if (crycb_fmt == CRYCB_FORMAT2)
+   memset(>arch.crypto.crycb->apcb1, 0,
+  sizeof(kvm->arch.crypto.crycb->apcb1));
+   else
+   memset(>arch.crypto.crycb->apcb0, 0,
+  sizeof(kvm->arch.crypto.crycb->apcb0));
+}
+
+static inline unsigned long *kvm_ap_get_crycb_apm(struct kvm *kvm)
+{
+   unsigned long *apm;
+   

Re: [PATCH v2 07/15] KVM: s390: Interfaces to configure/deconfigure guest's AP matrix

2018-02-28 Thread Tony Krowiak

On 02/28/2018 11:15 AM, Pierre Morel wrote:

On 27/02/2018 15:28, Tony Krowiak wrote:

Provides interfaces to assign AP adapters, usage domains
and control domains to a KVM guest.

A KVM guest is started by executing the Start Interpretive Execution 
(SIE)
instruction. The SIE state description is a control block that 
contains the

state information for a KVM guest and is supplied as input to the SIE
instruction. The SIE state description has a satellite structure 
called the

Crypto Control Block (CRYCB). The CRYCB contains three bitmask fields
identifying the adapters, queues (domains) and control domains 
assigned to

the KVM guest:

* The AP Adapter Mask (APM) field identifies the AP adapters assigned to
   the KVM guest

* The AP Queue Mask (AQM) field identifies the AP queues assigned to
   the KVM guest. Each AP queue is connected to a usage domain within
   an AP adapter.

* The AP Domain Mask (ADM) field identifies the control domains
   assigned to the KVM guest.

Each adapter, queue (usage domain) and control domain are identified by
a number from 0 to 255. The bits in each mask, from most significant to
least significant bit, correspond to the numbers 0-255. When a bit is
set, the corresponding adapter, queue (usage domain) or control domain
is assigned to the KVM guest.


...snip...


  static int kvm_ap_apxa_installed(void)
  {
  int ret;
@@ -50,3 +170,140 @@ void kvm_ap_set_crycb_format(struct kvm *kvm, 
__u32 *crycbd)

  *crycbd |= CRYCB_FORMAT1;
  }
  }
+
+static int kvm_ap_matrix_apm_create(struct kvm_ap_matrix *ap_matrix, 
int apxa)

+{
+if (apxa)
+ap_matrix->apm_max = 256;


AFAIK the number of possible bits in the masks for a system is not a 
generic value but is

returned by the QCI instruction.
Is there a reason to use a fix value?
Right you are! I'll initialize the value based on what is returned from 
the QCI call.




+else
+ap_matrix->apm_max = 64;
+
+ap_matrix->apm = kzalloc(KVM_AP_MASK_BYTES(ap_matrix->apm_max),
+ GFP_KERNEL);
+if (!ap_matrix->apm)
+return -ENOMEM;
+
+return 0;
+}
+
+static int kvm_ap_matrix_aqm_create(struct kvm_ap_matrix *ap_matrix, 
int apxa)

+{
+if (apxa)
+ap_matrix->aqm_max = 256;


same here

ditto



+else
+ap_matrix->aqm_max = 16;
+
+ap_matrix->aqm = kzalloc(KVM_AP_MASK_BYTES(ap_matrix->aqm_max),
+ GFP_KERNEL);
+if (!ap_matrix->aqm)
+return -ENOMEM;
+
+return 0;
+}
+
+static int kvm_ap_matrix_adm_create(struct kvm_ap_matrix *ap_matrix, 
int apxa)

+{
+if (apxa)
+ap_matrix->adm_max = 256;


and here

ditto



Pierre





Re: [PATCH v2 07/15] KVM: s390: Interfaces to configure/deconfigure guest's AP matrix

2018-02-28 Thread Tony Krowiak

On 02/28/2018 11:15 AM, Pierre Morel wrote:

On 27/02/2018 15:28, Tony Krowiak wrote:

Provides interfaces to assign AP adapters, usage domains
and control domains to a KVM guest.

A KVM guest is started by executing the Start Interpretive Execution 
(SIE)
instruction. The SIE state description is a control block that 
contains the

state information for a KVM guest and is supplied as input to the SIE
instruction. The SIE state description has a satellite structure 
called the

Crypto Control Block (CRYCB). The CRYCB contains three bitmask fields
identifying the adapters, queues (domains) and control domains 
assigned to

the KVM guest:

* The AP Adapter Mask (APM) field identifies the AP adapters assigned to
   the KVM guest

* The AP Queue Mask (AQM) field identifies the AP queues assigned to
   the KVM guest. Each AP queue is connected to a usage domain within
   an AP adapter.

* The AP Domain Mask (ADM) field identifies the control domains
   assigned to the KVM guest.

Each adapter, queue (usage domain) and control domain are identified by
a number from 0 to 255. The bits in each mask, from most significant to
least significant bit, correspond to the numbers 0-255. When a bit is
set, the corresponding adapter, queue (usage domain) or control domain
is assigned to the KVM guest.


...snip...


  static int kvm_ap_apxa_installed(void)
  {
  int ret;
@@ -50,3 +170,140 @@ void kvm_ap_set_crycb_format(struct kvm *kvm, 
__u32 *crycbd)

  *crycbd |= CRYCB_FORMAT1;
  }
  }
+
+static int kvm_ap_matrix_apm_create(struct kvm_ap_matrix *ap_matrix, 
int apxa)

+{
+if (apxa)
+ap_matrix->apm_max = 256;


AFAIK the number of possible bits in the masks for a system is not a 
generic value but is

returned by the QCI instruction.
Is there a reason to use a fix value?
Right you are! I'll initialize the value based on what is returned from 
the QCI call.




+else
+ap_matrix->apm_max = 64;
+
+ap_matrix->apm = kzalloc(KVM_AP_MASK_BYTES(ap_matrix->apm_max),
+ GFP_KERNEL);
+if (!ap_matrix->apm)
+return -ENOMEM;
+
+return 0;
+}
+
+static int kvm_ap_matrix_aqm_create(struct kvm_ap_matrix *ap_matrix, 
int apxa)

+{
+if (apxa)
+ap_matrix->aqm_max = 256;


same here

ditto



+else
+ap_matrix->aqm_max = 16;
+
+ap_matrix->aqm = kzalloc(KVM_AP_MASK_BYTES(ap_matrix->aqm_max),
+ GFP_KERNEL);
+if (!ap_matrix->aqm)
+return -ENOMEM;
+
+return 0;
+}
+
+static int kvm_ap_matrix_adm_create(struct kvm_ap_matrix *ap_matrix, 
int apxa)

+{
+if (apxa)
+ap_matrix->adm_max = 256;


and here

ditto



Pierre





Re: [PATCH v2 07/15] KVM: s390: Interfaces to configure/deconfigure guest's AP matrix

2018-02-28 Thread Tony Krowiak

On 02/27/2018 09:28 AM, Tony Krowiak wrote:

Provides interfaces to assign AP adapters, usage domains
and control domains to a KVM guest.

A KVM guest is started by executing the Start Interpretive Execution (SIE)
instruction. The SIE state description is a control block that contains the
state information for a KVM guest and is supplied as input to the SIE
instruction. The SIE state description has a satellite structure called the
Crypto Control Block (CRYCB). The CRYCB contains three bitmask fields
identifying the adapters, queues (domains) and control domains assigned to
the KVM guest:

* The AP Adapter Mask (APM) field identifies the AP adapters assigned to
   the KVM guest

* The AP Queue Mask (AQM) field identifies the AP queues assigned to
   the KVM guest. Each AP queue is connected to a usage domain within
   an AP adapter.

* The AP Domain Mask (ADM) field identifies the control domains
   assigned to the KVM guest.

Each adapter, queue (usage domain) and control domain are identified by
a number from 0 to 255. The bits in each mask, from most significant to
least significant bit, correspond to the numbers 0-255. When a bit is
set, the corresponding adapter, queue (usage domain) or control domain
is assigned to the KVM guest.

This patch will set the bits in the APM, AQM and ADM fields of the
CRYCB referenced by the KVM guest's SIE state description. The process
used is:

1. Verify that the bits to be set do not exceed the maximum bit
number for the given mask.

2. Verify that the APQNs that can be derived from the intersection
of the bits set in the APM and AQM fields of the KVM guest's CRYCB
are not assigned to any other KVM guest running on the same linux
host.

3. Set the APM, AQM and ADM in the CRYCB according to the matrix
configured for the mediated matrix device via its sysfs
adapter, domain and control domain attribute files respectively.

Signed-off-by: Tony Krowiak 
---
  arch/s390/include/asm/kvm-ap.h|   36 +
  arch/s390/kvm/kvm-ap.c|  257 +
  drivers/s390/crypto/vfio_ap_ops.c |   19 +++
  drivers/s390/crypto/vfio_ap_private.h |4 +
  4 files changed, 316 insertions(+), 0 deletions(-)

diff --git a/arch/s390/include/asm/kvm-ap.h b/arch/s390/include/asm/kvm-ap.h
index ef749e7..46e7c5b 100644
--- a/arch/s390/include/asm/kvm-ap.h
+++ b/arch/s390/include/asm/kvm-ap.h
@@ -10,9 +10,45 @@
  #define _ASM_KVM_AP
  #include 
  #include 
+#include 
+#include 
+#include 
+
+#define KVM_AP_MASK_BYTES(n)(n / BITS_PER_BYTE)
I don't know how I missed it, but there is a BITS_TO_BYTES macro in 
linux/bitops.h.

It makes no sense to reinvent the wheel. Also, the above will only produce
a valid value if (n % BITS_PER_BYTE) == 0. That would most likely hold 
true for
the cases in which the macro is used, but the BITS_TO_BYTES macro 
compensates for

the case where n is not evenly divisible by 8.

+
+/**
+ * The AP matrix is comprised of three bit masks identifying the adapters,
+ * queues (domains) and control domains that belong to an AP matrix. The bits 
in
+ * each mask, from least significant to most significant bit, correspond to IDs
+ * 0 to the maximum ID allowed for a given mask. When a bit is set, the
+ * corresponding ID belongs to the matrix.
+ *
+ * @apm_max: max number of bits in @apm
+ * @apm identifies the AP adapters in the matrix
+ * @aqm_max: max number of bits in @aqm
+ * @aqm identifies the AP queues (domains) in the matrix
+ * @adm_max: max number of bits in @adm
+ * @adm identifies the AP control domains in the matrix
+ */
+struct kvm_ap_matrix {
+   int apm_max;
+   unsigned long *apm;
+   int aqm_max;
+   unsigned long *aqm;
+   int adm_max;
+   unsigned long *adm;
+};

  void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd);

  int kvm_ap_get_crycb_format(struct kvm *kvm);

+int kvm_ap_matrix_create(struct kvm_ap_matrix **ap_matrix);
+
+void kvm_ap_matrix_destroy(struct kvm_ap_matrix *ap_matrix);
+
+int kvm_ap_configure_matrix(struct kvm *kvm, struct kvm_ap_matrix *matrix);
+
+void kvm_ap_deconfigure_matrix(struct kvm *kvm);
+
  #endif /* _ASM_KVM_AP */
diff --git a/arch/s390/kvm/kvm-ap.c b/arch/s390/kvm/kvm-ap.c
index bafe63b..bb29045 100644
--- a/arch/s390/kvm/kvm-ap.c
+++ b/arch/s390/kvm/kvm-ap.c
@@ -8,6 +8,7 @@

  #include 
  #include 
+#include 

  #include "kvm-s390.h"

@@ -16,6 +17,125 @@ int kvm_ap_get_crycb_format(struct kvm *kvm)
return kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK;
  }

+static inline void kvm_ap_clear_crycb_masks(struct kvm *kvm)
+{
+   int crycb_fmt = kvm_ap_get_crycb_format(kvm);
+
+   if (crycb_fmt == CRYCB_FORMAT2)
+   memset(>arch.crypto.crycb->apcb1, 0,
+  sizeof(kvm->arch.crypto.crycb->apcb1));
+   else
+   memset(>arch.crypto.crycb->apcb0, 0,
+  sizeof(kvm->arch.crypto.crycb->apcb0));
+}
+
+static inline unsigned 

Re: [PATCH v2 07/15] KVM: s390: Interfaces to configure/deconfigure guest's AP matrix

2018-02-28 Thread Tony Krowiak

On 02/27/2018 09:28 AM, Tony Krowiak wrote:

Provides interfaces to assign AP adapters, usage domains
and control domains to a KVM guest.

A KVM guest is started by executing the Start Interpretive Execution (SIE)
instruction. The SIE state description is a control block that contains the
state information for a KVM guest and is supplied as input to the SIE
instruction. The SIE state description has a satellite structure called the
Crypto Control Block (CRYCB). The CRYCB contains three bitmask fields
identifying the adapters, queues (domains) and control domains assigned to
the KVM guest:

* The AP Adapter Mask (APM) field identifies the AP adapters assigned to
   the KVM guest

* The AP Queue Mask (AQM) field identifies the AP queues assigned to
   the KVM guest. Each AP queue is connected to a usage domain within
   an AP adapter.

* The AP Domain Mask (ADM) field identifies the control domains
   assigned to the KVM guest.

Each adapter, queue (usage domain) and control domain are identified by
a number from 0 to 255. The bits in each mask, from most significant to
least significant bit, correspond to the numbers 0-255. When a bit is
set, the corresponding adapter, queue (usage domain) or control domain
is assigned to the KVM guest.

This patch will set the bits in the APM, AQM and ADM fields of the
CRYCB referenced by the KVM guest's SIE state description. The process
used is:

1. Verify that the bits to be set do not exceed the maximum bit
number for the given mask.

2. Verify that the APQNs that can be derived from the intersection
of the bits set in the APM and AQM fields of the KVM guest's CRYCB
are not assigned to any other KVM guest running on the same linux
host.

3. Set the APM, AQM and ADM in the CRYCB according to the matrix
configured for the mediated matrix device via its sysfs
adapter, domain and control domain attribute files respectively.

Signed-off-by: Tony Krowiak 
---
  arch/s390/include/asm/kvm-ap.h|   36 +
  arch/s390/kvm/kvm-ap.c|  257 +
  drivers/s390/crypto/vfio_ap_ops.c |   19 +++
  drivers/s390/crypto/vfio_ap_private.h |4 +
  4 files changed, 316 insertions(+), 0 deletions(-)

diff --git a/arch/s390/include/asm/kvm-ap.h b/arch/s390/include/asm/kvm-ap.h
index ef749e7..46e7c5b 100644
--- a/arch/s390/include/asm/kvm-ap.h
+++ b/arch/s390/include/asm/kvm-ap.h
@@ -10,9 +10,45 @@
  #define _ASM_KVM_AP
  #include 
  #include 
+#include 
+#include 
+#include 
+
+#define KVM_AP_MASK_BYTES(n)(n / BITS_PER_BYTE)
I don't know how I missed it, but there is a BITS_TO_BYTES macro in 
linux/bitops.h.

It makes no sense to reinvent the wheel. Also, the above will only produce
a valid value if (n % BITS_PER_BYTE) == 0. That would most likely hold 
true for
the cases in which the macro is used, but the BITS_TO_BYTES macro 
compensates for

the case where n is not evenly divisible by 8.

+
+/**
+ * The AP matrix is comprised of three bit masks identifying the adapters,
+ * queues (domains) and control domains that belong to an AP matrix. The bits 
in
+ * each mask, from least significant to most significant bit, correspond to IDs
+ * 0 to the maximum ID allowed for a given mask. When a bit is set, the
+ * corresponding ID belongs to the matrix.
+ *
+ * @apm_max: max number of bits in @apm
+ * @apm identifies the AP adapters in the matrix
+ * @aqm_max: max number of bits in @aqm
+ * @aqm identifies the AP queues (domains) in the matrix
+ * @adm_max: max number of bits in @adm
+ * @adm identifies the AP control domains in the matrix
+ */
+struct kvm_ap_matrix {
+   int apm_max;
+   unsigned long *apm;
+   int aqm_max;
+   unsigned long *aqm;
+   int adm_max;
+   unsigned long *adm;
+};

  void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd);

  int kvm_ap_get_crycb_format(struct kvm *kvm);

+int kvm_ap_matrix_create(struct kvm_ap_matrix **ap_matrix);
+
+void kvm_ap_matrix_destroy(struct kvm_ap_matrix *ap_matrix);
+
+int kvm_ap_configure_matrix(struct kvm *kvm, struct kvm_ap_matrix *matrix);
+
+void kvm_ap_deconfigure_matrix(struct kvm *kvm);
+
  #endif /* _ASM_KVM_AP */
diff --git a/arch/s390/kvm/kvm-ap.c b/arch/s390/kvm/kvm-ap.c
index bafe63b..bb29045 100644
--- a/arch/s390/kvm/kvm-ap.c
+++ b/arch/s390/kvm/kvm-ap.c
@@ -8,6 +8,7 @@

  #include 
  #include 
+#include 

  #include "kvm-s390.h"

@@ -16,6 +17,125 @@ int kvm_ap_get_crycb_format(struct kvm *kvm)
return kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK;
  }

+static inline void kvm_ap_clear_crycb_masks(struct kvm *kvm)
+{
+   int crycb_fmt = kvm_ap_get_crycb_format(kvm);
+
+   if (crycb_fmt == CRYCB_FORMAT2)
+   memset(>arch.crypto.crycb->apcb1, 0,
+  sizeof(kvm->arch.crypto.crycb->apcb1));
+   else
+   memset(>arch.crypto.crycb->apcb0, 0,
+  sizeof(kvm->arch.crypto.crycb->apcb0));
+}
+
+static inline unsigned long 

Re: [PATCH v2 07/15] KVM: s390: Interfaces to configure/deconfigure guest's AP matrix

2018-02-28 Thread Pierre Morel

On 27/02/2018 15:28, Tony Krowiak wrote:

Provides interfaces to assign AP adapters, usage domains
and control domains to a KVM guest.

A KVM guest is started by executing the Start Interpretive Execution (SIE)
instruction. The SIE state description is a control block that contains the
state information for a KVM guest and is supplied as input to the SIE
instruction. The SIE state description has a satellite structure called the
Crypto Control Block (CRYCB). The CRYCB contains three bitmask fields
identifying the adapters, queues (domains) and control domains assigned to
the KVM guest:

* The AP Adapter Mask (APM) field identifies the AP adapters assigned to
   the KVM guest

* The AP Queue Mask (AQM) field identifies the AP queues assigned to
   the KVM guest. Each AP queue is connected to a usage domain within
   an AP adapter.

* The AP Domain Mask (ADM) field identifies the control domains
   assigned to the KVM guest.

Each adapter, queue (usage domain) and control domain are identified by
a number from 0 to 255. The bits in each mask, from most significant to
least significant bit, correspond to the numbers 0-255. When a bit is
set, the corresponding adapter, queue (usage domain) or control domain
is assigned to the KVM guest.


...snip...


  static int kvm_ap_apxa_installed(void)
  {
int ret;
@@ -50,3 +170,140 @@ void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 
*crycbd)
*crycbd |= CRYCB_FORMAT1;
}
  }
+
+static int kvm_ap_matrix_apm_create(struct kvm_ap_matrix *ap_matrix, int apxa)
+{
+   if (apxa)
+   ap_matrix->apm_max = 256;


AFAIK the number of possible bits in the masks for a system is not a 
generic value but is

returned by the QCI instruction.
Is there a reason to use a fix value?


+   else
+   ap_matrix->apm_max = 64;
+
+   ap_matrix->apm = kzalloc(KVM_AP_MASK_BYTES(ap_matrix->apm_max),
+GFP_KERNEL);
+   if (!ap_matrix->apm)
+   return -ENOMEM;
+
+   return 0;
+}
+
+static int kvm_ap_matrix_aqm_create(struct kvm_ap_matrix *ap_matrix, int apxa)
+{
+   if (apxa)
+   ap_matrix->aqm_max = 256;


same here


+   else
+   ap_matrix->aqm_max = 16;
+
+   ap_matrix->aqm = kzalloc(KVM_AP_MASK_BYTES(ap_matrix->aqm_max),
+GFP_KERNEL);
+   if (!ap_matrix->aqm)
+   return -ENOMEM;
+
+   return 0;
+}
+
+static int kvm_ap_matrix_adm_create(struct kvm_ap_matrix *ap_matrix, int apxa)
+{
+   if (apxa)
+   ap_matrix->adm_max = 256;


and here


Pierre

--
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany



Re: [PATCH v2 07/15] KVM: s390: Interfaces to configure/deconfigure guest's AP matrix

2018-02-28 Thread Pierre Morel

On 27/02/2018 15:28, Tony Krowiak wrote:

Provides interfaces to assign AP adapters, usage domains
and control domains to a KVM guest.

A KVM guest is started by executing the Start Interpretive Execution (SIE)
instruction. The SIE state description is a control block that contains the
state information for a KVM guest and is supplied as input to the SIE
instruction. The SIE state description has a satellite structure called the
Crypto Control Block (CRYCB). The CRYCB contains three bitmask fields
identifying the adapters, queues (domains) and control domains assigned to
the KVM guest:

* The AP Adapter Mask (APM) field identifies the AP adapters assigned to
   the KVM guest

* The AP Queue Mask (AQM) field identifies the AP queues assigned to
   the KVM guest. Each AP queue is connected to a usage domain within
   an AP adapter.

* The AP Domain Mask (ADM) field identifies the control domains
   assigned to the KVM guest.

Each adapter, queue (usage domain) and control domain are identified by
a number from 0 to 255. The bits in each mask, from most significant to
least significant bit, correspond to the numbers 0-255. When a bit is
set, the corresponding adapter, queue (usage domain) or control domain
is assigned to the KVM guest.


...snip...


  static int kvm_ap_apxa_installed(void)
  {
int ret;
@@ -50,3 +170,140 @@ void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 
*crycbd)
*crycbd |= CRYCB_FORMAT1;
}
  }
+
+static int kvm_ap_matrix_apm_create(struct kvm_ap_matrix *ap_matrix, int apxa)
+{
+   if (apxa)
+   ap_matrix->apm_max = 256;


AFAIK the number of possible bits in the masks for a system is not a 
generic value but is

returned by the QCI instruction.
Is there a reason to use a fix value?


+   else
+   ap_matrix->apm_max = 64;
+
+   ap_matrix->apm = kzalloc(KVM_AP_MASK_BYTES(ap_matrix->apm_max),
+GFP_KERNEL);
+   if (!ap_matrix->apm)
+   return -ENOMEM;
+
+   return 0;
+}
+
+static int kvm_ap_matrix_aqm_create(struct kvm_ap_matrix *ap_matrix, int apxa)
+{
+   if (apxa)
+   ap_matrix->aqm_max = 256;


same here


+   else
+   ap_matrix->aqm_max = 16;
+
+   ap_matrix->aqm = kzalloc(KVM_AP_MASK_BYTES(ap_matrix->aqm_max),
+GFP_KERNEL);
+   if (!ap_matrix->aqm)
+   return -ENOMEM;
+
+   return 0;
+}
+
+static int kvm_ap_matrix_adm_create(struct kvm_ap_matrix *ap_matrix, int apxa)
+{
+   if (apxa)
+   ap_matrix->adm_max = 256;


and here


Pierre

--
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany