Skip to contentSkip to conversation

s390: vfio-ap: guest dedicated crypto adapters

PATCH In review Tony Krowiak 6 participants 56 messages Feb 27, 2018 – Mar 14, 2018

Subsystems (7)
Documentation KVM s390 VFIO driver +3 more subsystems
lore
Share & tools
lore
#0 lore raw

v2 cover s390: vfio-ap: guest dedicated crypto adapters

On s390, we have cryptographic coprocessor cards, which are modeled on Linux as devices on the AP bus. Each card can be partitioned into domains which can be thought of as a set of hardware registers for processing crypto commands. Crypto commands are sent to a specific domain within a card is via a queue which is identified as a (card,domain) tuple. We model this something like the following (assuming we have access to cards 3 and 4 and domains 1 and 2):

AP -> card3 -> queue (3,1)

            -> queue (3,2)
   -> card4 -> queue (4,1)
            -> queue (4,2)

If we want to virtualize this, we can use a feature provided by the hardware. We basically attach a satellite control block to our main hardware virtualization control block and the hardware takes care of most of the rest.

For this control block, we don't specify explicit tuples, but a list of cards and a list of domains. The guest will get access to the cross product.

Because of this, we need to take care that the lists provided to different guests don't overlap; i.e., we need to enforce sane configurations. Otherwise, one guest may get access to things like secret keys for another guest.

The idea of this patch set is to introduce a new device, the matrix device. This matrix device hangs off a different root and acts as the parent node for mdev devices.

If you now want to give the tuples (4,1) and (4,2), you need to do the following:

- Unbind the (4,1) and (4,2) tuples from their ap bus driver.
- Bind the (4,1) and (4,2) tuples to the vfio_ap driver.
- Create the mediated device.
- Assign card 4 and domains 1 and 2 to the mediated device

QEMU will now simply consume the mediated device and things should work.

For a complete description of the architecture and concepts underlying the design, see the Documentation/s390/vfio-ap.txt file included with this patch set.

v1 => v2 Change log:
===================
* Added documentation vfio-ap.txt
* Renamed vfio_ap_matrix module and device driver to vfio_ap
* Use device core device list instead of maintaining list of matrix devices in driver
* Added VSIE support for AP
* Create matrix device before registering VFIO AP device driver with the AP bus
* Renamed the following files in drivers/s390/crypto:
* vfio_ap_matrix.drv -> vfio_ap_drv
* vfio_ap_matrix_private.h -> vfio_ap_private.h
* vfio_ap_matrix_ops.c -> vfio_ap_ops.c
* arch/s390/include/asm/kvm/ap-matrix-config.h
* Renamed to kvm-ap.h
* Changed the data type of the bit mask fields for the matrix structure to unsigned long and create them with DECLARE_BITMAP
* Changed #define prefixes from AP_MATRIX to KVM_AP
* Changed function and structure prefixes from ap_matrix to kvm_ap
* Added function interface to check if AP Extended Addressing (APXA) facility is installedCRYCB_FORMAT_MASK
* Added function interface to get the maximum ID for AP mask type
* Added function interface to set the AP execution mode
* arch/s390/kvm/ap-matrix-config.c
* Renamed to kvm-ap.c
* Changed function prefixes from ap_matrix to kvm_ap
* Added function to check if AP Extended Addressing (APXA) facility is installed
* Added function to get the maximum ID for AP mask type
* Added function to set the AP execution mode
* Added a boolean parameter to the functions that retrieve the APM, AQM and ADM bit mask fields from the CRYCB. If true, then the function will clear the bits in the mask before returning a reference to it
* Added validation to verify that APM, AQM and ADM bits that are set do not exceed the maximum ID value allowed
*
* arch/s390/include/asm/kvm_host.h
* Changed defined for ECA_AP to ECA_APIE - interpretive execution mode
* Added a flag to struct kvm_s390_crypto to indicate whether the KVM_S390_VM_CPU_FEAT_AP CPU model feature for AP facilities is set
* Added two CPU facilities features to set STFLE.12 and STFLE.15
* arch/s390/kvm/kvm-s390.c
* Added initialization for new KVM_S390_VM_CPU_FEAT_AP CPU model feature
* Removed kvm_s390_apxa_installed() function
* Changed call to kvm_s390_apxa_installed() which has been removed to a call to new kvm_ap_apxa_installed() function.
* Added code to kvm_s390_vcpu_crypto_setup() to set the new CPU model feature flag in the kvm_s390_crypto structure
* Added CRYCB_FORMAT_MASK to mask CRYCBD
* arch/s390/tools/gen_facilities.c
* Added STFLE.12 and STFLE.15 to struct facility _def
* drivers/s390/crypto/vfio_ap_matrix_private.h
* Changed name of file to vfio_ap.private.h
* Changed #define prefixes from VFIO_AP_MATRIX to VFIO_AP
* struct ap_matrix: removed list fields and locks
* struct vfio_ap_queue: removed list field
* Renamed functions ap_matrix_mdev_register and ap_matrix_mdev_unregister to vfio_ap_mdev_register and vfio_ap_mdev_unregister respectively
* drivers/s390/crypto/vfio_ap_matrix_drv.c
* Renamed file to drivers/s390/crypto/vfio_ap_drv.c
* Changed all #define, structure and function prefixes to vfio_ap
* probe function
* Changed root device name for the matrix device to vfio_ap:
i.e., /sys/devices/vfio_ap/matrix
* No longer storing the AP queue device in a list, it is retrievable via the device core
* Removed unnecessary check whether matrix device exists
* Store the vfio_ap_queue structure in the private field of the ap_queue structure rather than using list interface
* remove function
* Retrieve vfio_ap_queue structure from the struct ap_queue private data rather than from a list
* Removed unnecessary check
* drivers/s390/crypto/vfio_ap_matrix_ops.c
* Renamed file to vfio_ap_ops.c
* Changed #define prefixes from AP_MATRIX to VFIO_AP
* Changed function name prefixes from ap_matrix to vfio_ap
* Removed ioctl to configure the CRYCB
* create function
* Removed ap_matrix_mdev_find_by_uuid() function - function is provided by mdev core
* Removed available_instances verification, provided by mdev core
* Removed check to see if mediated device exists, handled by mdev core
* notifier function
* Configuring matrix here instead of via ioctl
* Set interpretive execution mode for all VCPUs
* Removed R/O attributes to display adapters and domains
* Added an R/O attribute to display the matrix
* assign_control_domain mdev attribute:
* Removed check to see if the domain is installed on the linux host
* Added check to verify the control domain ID does not exceed the max value
* assign_adapter mdev attribute:
* Added check to verify the adapter ID does not exceed the max value
* If any APQNs configured for the mediated matrix device that have an APID matching the adapter ID being assigned are not bound to the vfio_ap device driver then it is assumed that the APQN is bound to another driver and assignment will fail
* assign_domain mdev attribute:
* Added check to verify the domain ID does not exceed the max value
* If any APQNs configured for the mediated matrix device that have an APQI matching the domain ID being assigned are not bound to the vfio_ap device driver then it is assumed that the APQN is bound to another driver and assignment will fail
* tools/arch/s390/include/uapi/asm/kvm.h
* removed KVM_S390_VM_CPU_FEAT_AP feature definition

Tony Krowiak (15):
KVM: s390: refactor crypto initialization
s390: vsie: implement AP support for second level guest
s390: zcrypt: externalize AP instructions available function
KVM: s390: CPU model support for AP virtualization
s390: vfio-ap: base implementation of VFIO AP device driver
s390: vfio-ap: register matrix device with VFIO mdev framework
KVM: s390: Interfaces to configure/deconfigure guest's AP matrix
KVM: s390: interface to enable AP execution mode
s390: vfio-ap: sysfs interfaces to configure adapters
s390: vfio-ap: sysfs interfaces to configure domains
s390: vfio-ap: sysfs interfaces to configure control domains
s390: vfio-ap: sysfs interface to view matrix mdev matrix
KVM: s390: Configure the guest's CRYCB
s390: vfio-ap: implement VFIO_DEVICE_GET_INFO ioctl
s390: doc: detailed specifications for AP virtualization

 Documentation/s390/vfio-ap.txt          |  514 ++++++++++++++++++
 MAINTAINERS                             |   14 +
 arch/s390/Kconfig                       |    8 +
 arch/s390/configs/default_defconfig     |    3 +
 arch/s390/configs/gcov_defconfig        |    3 +
 arch/s390/configs/performance_defconfig |    3 +
 arch/s390/defconfig                     |    3 +
 arch/s390/include/asm/ap.h              |    7 +
 arch/s390/include/asm/kvm-ap.h          |   59 +++
 arch/s390/include/asm/kvm_host.h        |    2 +
 arch/s390/include/uapi/asm/kvm.h        |    1 +
 arch/s390/kvm/Makefile                  |    2 +-
 arch/s390/kvm/kvm-ap.c                  |  336 ++++++++++++
 arch/s390/kvm/kvm-s390.c                |   66 +--
 arch/s390/kvm/kvm-s390.h                |    1 +
 arch/s390/kvm/vsie.c                    |   71 +++-
 arch/s390/tools/gen_facilities.c        |    2 +
 drivers/s390/crypto/Makefile            |    4 +
 drivers/s390/crypto/ap_bus.c            |    6 +
 drivers/s390/crypto/vfio_ap_drv.c       |  143 +++++
 drivers/s390/crypto/vfio_ap_ops.c       |  868 +++++++++++++++++++++++++++++++
 drivers/s390/crypto/vfio_ap_private.h   |   45 ++
 include/uapi/linux/vfio.h               |    2 +
 23 files changed, 2096 insertions(+), 67 deletions(-)
 create mode 100644 Documentation/s390/vfio-ap.txt
 create mode 100644 arch/s390/include/asm/kvm-ap.h
 create mode 100644 arch/s390/kvm/kvm-ap.c
 create mode 100644 drivers/s390/crypto/vfio_ap_drv.c
 create mode 100644 drivers/s390/crypto/vfio_ap_ops.c
 create mode 100644 drivers/s390/crypto/vfio_ap_private.h
#1 lore raw

PATCH v2 1/15 KVM: s390: refactor crypto initialization

The crypto control block designation (CRYCBD) is a 32-bit field in the KVM guest's SIE state description. The contents of bits 1-28 of this field, with three zero bits appended on the right, designate the host real 31-bit address of a crypto control block (CRYCB). Bits 30-31 specify the format of the CRYCB. In the current implementation, the address of the CRYCB is stored in the CRYCBD only if the Message-Security-Assist extension 3 (MSA3) facility is installed. Virtualization of AP facilities, however, requires that a CRYCB of the appropriate format be made available to SIE regardless of whether MSA3 is installed or not.

This patch introduces a new compilation unit to provide all interfaces related to configuration of AP facilities. Let's start by moving the function for setting the CRYCB format from arch/s390/kvm/kvm-s390 to this new AP configuration interface.

---
 MAINTAINERS                      |   10 ++++++
 arch/s390/include/asm/kvm-ap.h   |   16 ++++++++++
 arch/s390/include/asm/kvm_host.h |    1 +
 arch/s390/kvm/Makefile           |    2 +-
 arch/s390/kvm/kvm-ap.c           |   47 ++++++++++++++++++++++++++++
 arch/s390/kvm/kvm-s390.c         |   62 +++++---------------------------------
 6 files changed, 83 insertions(+), 55 deletions(-)
 create mode 100644 arch/s390/include/asm/kvm-ap.h
 create mode 100644 arch/s390/kvm/kvm-ap.c
MAINTAINERS +10 -0
index 0ec5881..4acf7c2 100644
@@ -11875,6 +11875,16 @@ W:	http://www.ibm.com/developerworks/linux/linux390/
 S:	Supported
 F:	drivers/s390/crypto/
 
+S390 VFIO AP DRIVER
+M:	Tony Krowiak <[email protected]>
+M:	Christian BornTraeger <[email protected]>
+M:	Martin Schwidefsky <[email protected]>
+L:	[email protected]
+W:	http://www.ibm.com/developerworks/linux/linux390/
+S:	Supported
+F:	arch/s390/include/asm/kvm/kvm-ap.h
+F:	arch/s390/kvm/kvm-ap.c
+
 S390 ZFCP DRIVER
 M:	Steffen Maier <[email protected]>
 M:	Benjamin Block <[email protected]>
arch/s390/include/asm/kvm-ap.h new file +16 -0
new file mode 100644
index 0000000..4e43117
@@ -0,0 +1,16 @@
+/*
+ * Adjunct Processor (AP) configuration management for KVM guests
+ *
+ * Copyright IBM Corp. 2017
+ *
+ * Author(s): Tony Krowiak <[email protected]>
+ */
+
+#ifndef _ASM_KVM_AP
+#define _ASM_KVM_AP
+#include <linux/types.h>
+#include <linux/kvm_host.h>
+
+void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd);
+
+#endif /* _ASM_KVM_AP */
arch/s390/include/asm/kvm_host.h +1 -0
index 27918b1..a4c77d3 100644
@@ -257,6 +257,7 @@ struct kvm_s390_sie_block {
 	__u8	reservedf0[12];		/* 0x00f0 */
 #define CRYCB_FORMAT1 0x00000001
 #define CRYCB_FORMAT2 0x00000003
+#define CRYCB_FORMAT_MASK 0x00000003
 	__u32	crycbd;			/* 0x00fc */
 	__u64	gcr[16];		/* 0x0100 */
 	__u64	gbea;			/* 0x0180 */
arch/s390/kvm/Makefile +1 -1
index 05ee90a..1876bfe 100644
@@ -9,6 +9,6 @@ common-objs = $(KVM)/kvm_main.o $(KVM)/eventfd.o  $(KVM)/async_pf.o $(KVM)/irqch
 ccflags-y := -Ivirt/kvm -Iarch/s390/kvm
 
 kvm-objs := $(common-objs) kvm-s390.o intercept.o interrupt.o priv.o sigp.o
-kvm-objs += diag.o gaccess.o guestdbg.o vsie.o
+kvm-objs += diag.o gaccess.o guestdbg.o vsie.o kvm-ap.o
 
 obj-$(CONFIG_KVM) += kvm.o
arch/s390/kvm/kvm-ap.c new file +47 -0

47 lines not on this page · raw message

arch/s390/kvm/kvm-s390.c +8 -54

95 lines not on this page · raw message

Signature · 1 line
1.7.1
#2 lore raw

Re: [PATCH v2 01/15] KVM: s390: refactor crypto initialization

Quoted 13 lines · On Tue, 27 Feb 2018 09:27:59 -0500 Tony Krowiak <[email protected]>…

The crypto control block designation (CRYCBD) is a 32-bit field in the KVM guest's SIE state description. The contents of bits 1-28 of this field, with three zero bits appended on the right, designate the host real 31-bit address of a crypto control block (CRYCB). Bits 30-31 specify the format of the CRYCB. In the current implementation, the address of the CRYCB is stored in the CRYCBD only if the Message-Security-Assist extension 3 (MSA3) facility is installed. Virtualization of AP facilities, however, requires that a CRYCB of the appropriate format be made available to SIE regardless of whether MSA3 is installed or not.

Quoted 5 lines

This patch introduces a new compilation unit to provide all interfaces related to configuration of AP facilities. Let's start by moving the function for setting the CRYCB format from arch/s390/kvm/kvm-s390 to this new AP configuration interface.

Hm, I would tweak this patch description a bit. First, you talk about what the crycbd is; then, what needs to be done for vfio-ap support; then you simply state that you move some interfaces to a new file. I'd like to see a connection between those parts :)

[It sounds a bit like you'd just introduce a new file and move some functions, while you do have more changes in there.]

Quoted 18 lines
---
 MAINTAINERS                      |   10 ++++++
 arch/s390/include/asm/kvm-ap.h   |   16 ++++++++++
 arch/s390/include/asm/kvm_host.h |    1 +
 arch/s390/kvm/Makefile           |    2 +-
 arch/s390/kvm/kvm-ap.c           |   47 ++++++++++++++++++++++++++++
 arch/s390/kvm/kvm-s390.c         |   62 +++++---------------------------------
 6 files changed, 83 insertions(+), 55 deletions(-)
 create mode 100644 arch/s390/include/asm/kvm-ap.h
 create mode 100644 arch/s390/kvm/kvm-ap.c
MAINTAINERS +0 -0
index 0ec5881..4acf7c2 100644

MAINTAINERS W: http://www.ibm.com/developerworks/linux/linux390/ · line 11880

Quoted 6 lines
MAINTAINERS +3 -0
@@ -11875,6 +11875,16 @@ W:	http://www.ibm.com/developerworks/linux/linux390/
 S:	Supported
 F:	drivers/s390/crypto/
 
+S390 VFIO AP DRIVER
+M:	Tony Krowiak <[email protected]>
+M:	Christian BornTraeger <[email protected]>

Typo.

Quoted 10 lines
MAINTAINERS +7 -0
+M:	Martin Schwidefsky <[email protected]>
+L:	[email protected]
+W:	http://www.ibm.com/developerworks/linux/linux390/
+S:	Supported
+F:	arch/s390/include/asm/kvm/kvm-ap.h
+F:	arch/s390/kvm/kvm-ap.c
+
 S390 ZFCP DRIVER
 M:	Steffen Maier <[email protected]>
 M:	Benjamin Block <[email protected]>

(...)

Quoted 15 lines
arch/s390/kvm/kvm-ap.c new file +9 -0
new file mode 100644
index 0000000..5305f4c
@@ -0,0 +1,47 @@
+/*
+ * Adjunct Processor (AP) configuration management for KVM guests
+ *
+ * Copyright IBM Corp. 2017
+ *
+ * Author(s): Tony Krowiak <[email protected]>
+ */
+
+#include <asm/kvm-ap.h>

arch/s390/kvm/kvm-ap.c line 19

Quoted 10 lines
arch/s390/kvm/kvm-ap.c new file +10 -0
+#include <asm/ap.h>
+
+#include "kvm-s390.h"
+
+static int kvm_ap_apxa_installed(void)
+{
+	int ret;
+	struct ap_config_info config;
+
+	ret = ap_query_configuration(&config);

Doesn't that introduce a dependency on CONFIG_ZCRYPT?

arch/s390/kvm/kvm-ap.c line 29

Quoted 10 lines
arch/s390/kvm/kvm-ap.c new file +10 -0
+	if (ret)
+		return 0;
+
+	return (config.apxa == 1);
+}
+
+/**
+ * kvm_ap_set_crycb_format
+ *
+ * Set the CRYCB format in the CRYCBD for the KVM guest.

Spell out "crypto control block" somewhere?

arch/s390/kvm/kvm-ap.c line 40

Quoted 11 lines
arch/s390/kvm/kvm-ap.c new file +11 -0
+ *
+ * @kvm:	the KVM guest
+ * @crycbd:	the CRYCB descriptor
+ */
+void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd)
+{
+	*crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
+
+	*crycbd &= ~(CRYCB_FORMAT_MASK);
+
+	/* If the MSAX3 is installed */

/* check whether MSAX3 is installed */ ?

Quoted 13 lines
arch/s390/kvm/kvm-ap.c new file +7 -0
+	if (test_kvm_facility(kvm, 76)) {
+		if (kvm_ap_apxa_installed())
+			*crycbd |= CRYCB_FORMAT2;
+		else
+			*crycbd |= CRYCB_FORMAT1;
+	}
+}
arch/s390/kvm/kvm-s390.c +0 -0
index 5f5a4cb..de1e299 100644

arch/s390/kvm/kvm-s390.c static u64 kvm_s390_get_initial_cpuid(void) · line 1871

Quoted 6 lines
arch/s390/kvm/kvm-s390.c +3 -0
@@ -1913,12 +1866,13 @@ static u64 kvm_s390_get_initial_cpuid(void)
 
 static void kvm_s390_crypto_init(struct kvm *kvm)
 {
+	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
+	kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
+	kvm_ap_set_crycb_format(kvm, &kvm->arch.crypto.crycbd);

Doesn't kvm_ap_set_crycb_format() already initialize its second parameter?

Would it make sense to do

kvm->arch.crypto.crycbd = kvm_ap_build_crycbd(kvm);

or so instead?

Quoted 10 lines
arch/s390/kvm/kvm-s390.c +1 -3
+
 	if (!test_kvm_facility(kvm, 76))
 		return;
 
-	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
-	kvm_s390_set_crycb_format(kvm);
-
 	/* Enable AES/DEA protected key functions by default */
 	kvm->arch.crypto.aes_kw = 1;
 	kvm->arch.crypto.dea_kw = 1;
#3 lore raw

Re: [PATCH v2 01/15] KVM: s390: refactor crypto initialization

Quoted 21 lines · Cornelia Huck
Quoted 13 lines · On Tue, 27 Feb 2018 09:27:59 -0500 Tony Krowiak <[email protected]>…

The crypto control block designation (CRYCBD) is a 32-bit field in the KVM guest's SIE state description. The contents of bits 1-28 of this field, with three zero bits appended on the right, designate the host real 31-bit address of a crypto control block (CRYCB). Bits 30-31 specify the format of the CRYCB. In the current implementation, the address of the CRYCB is stored in the CRYCBD only if the Message-Security-Assist extension 3 (MSA3) facility is installed. Virtualization of AP facilities, however, requires that a CRYCB of the appropriate format be made available to SIE regardless of whether MSA3 is installed or not.

Quoted 5 lines

This patch introduces a new compilation unit to provide all interfaces related to configuration of AP facilities. Let's start by moving the function for setting the CRYCB format from arch/s390/kvm/kvm-s390 to this new AP configuration interface.

Quoted 7 lines

Hm, I would tweak this patch description a bit. First, you talk about what the crycbd is; then, what needs to be done for vfio-ap support; then you simply state that you move some interfaces to a new file. I'd like to see a connection between those parts :)

[It sounds a bit like you'd just introduce a new file and move some functions, while you do have more changes in there.]

I'll try to wordsmith the patch description.

Quoted 21 lines
Quoted 21 lines
---
  MAINTAINERS                      |   10 ++++++
  arch/s390/include/asm/kvm-ap.h   |   16 ++++++++++
  arch/s390/include/asm/kvm_host.h |    1 +
  arch/s390/kvm/Makefile           |    2 +-
  arch/s390/kvm/kvm-ap.c           |   47 ++++++++++++++++++++++++++++
  arch/s390/kvm/kvm-s390.c         |   62 +++++---------------------------------
  6 files changed, 83 insertions(+), 55 deletions(-)

create mode 100644 arch/s390/include/asm/kvm-ap.h create mode 100644 arch/s390/kvm/kvm-ap.c

MAINTAINERS +0 -0
index 0ec5881..4acf7c2 100644
@@ -11875,6 +11875,16 @@ W:	http://www.ibm.com/developerworks/linux/linux390/
  S:	Supported
  F:	drivers/s390/crypto/
 
Quoted 4 lines

MAINTAINERS W: http://www.ibm.com/developerworks/linux/linux390/ · line 11880

Quoted 3 lines
MAINTAINERS +3 -0
+S390 VFIO AP DRIVER
+M:	Tony Krowiak <[email protected]>
+M:	Christian BornTraeger <[email protected]>

Typo.

Will fix

Quoted 31 lines
Quoted 11 lines
MAINTAINERS +7 -0
+M:	Martin Schwidefsky <[email protected]>
+L:	[email protected]
+W:	http://www.ibm.com/developerworks/linux/linux390/
+S:	Supported
+F:	arch/s390/include/asm/kvm/kvm-ap.h
+F:	arch/s390/kvm/kvm-ap.c
+
  S390 ZFCP DRIVER
  M:	Steffen Maier <[email protected]>
  M:	Benjamin Block <[email protected]>

(...)

Quoted 18 lines
arch/s390/kvm/kvm-ap.c new file +12 -0
new file mode 100644
index 0000000..5305f4c
@@ -0,0 +1,47 @@
+/*
+ * Adjunct Processor (AP) configuration management for KVM guests
+ *
+ * Copyright IBM Corp. 2017
+ *
+ * Author(s): Tony Krowiak <[email protected]>
+ */
+
+#include <asm/kvm-ap.h>
+#include <asm/ap.h>
+
+#include "kvm-s390.h"
Quoted 8 lines

arch/s390/kvm/kvm-ap.c line 19

Quoted 7 lines
arch/s390/kvm/kvm-ap.c new file +7 -0
+
+static int kvm_ap_apxa_installed(void)
+{
+	int ret;
+	struct ap_config_info config;
+
+	ret = ap_query_configuration(&config);

Doesn't that introduce a dependency on CONFIG_ZCRYPT?

It does, but AFAIK zcrypt is built into the kernel. Or is that not what you are asking?

Quoted 12 lines

arch/s390/kvm/kvm-ap.c line 29

Quoted 11 lines
arch/s390/kvm/kvm-ap.c new file +10 -0
+	if (ret)
+		return 0;
+
+	return (config.apxa == 1);
+}
+KVM guest's use.
+/**
+ * kvm_ap_set_crycb_format
+ *
+ * Set the CRYCB format in the CRYCBD for the KVM guest.

Spell out "crypto control block" somewhere?

Done

Quoted 13 lines

arch/s390/kvm/kvm-ap.c line 40

Quoted 12 lines
arch/s390/kvm/kvm-ap.c new file +11 -0
+ *
+ * @kvm:	the KVM guest
+ * @crycbd:	the CRYCB descriptor
+ */
+void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd)
+{
+	*crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
+
+	*crycbd &= ~(CRYCB_FORMAT_MASK);
+
+	/* If the MSAX3 is installed */

/* check whether MSAX3 is installed */ ?

Sure, why not

Quoted 14 lines
Quoted 14 lines
arch/s390/kvm/kvm-ap.c new file +7 -0
+	if (test_kvm_facility(kvm, 76)) {
+		if (kvm_ap_apxa_installed())
+			*crycbd |= CRYCB_FORMAT2;
+		else
+			*crycbd |= CRYCB_FORMAT1;
+	}
+}
arch/s390/kvm/kvm-s390.c +0 -0
index 5f5a4cb..de1e299 100644
@@ -1913,12 +1866,13 @@ static u64 kvm_s390_get_initial_cpuid(void)
 
Quoted 7 lines

arch/s390/kvm/kvm-s390.c static u64 kvm_s390_get_initial_cpuid(void) · line 1871

Quoted 5 lines
arch/s390/kvm/kvm-s390.c +3 -0
  static void kvm_s390_crypto_init(struct kvm *kvm)
  {
+	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
+	kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
+	kvm_ap_set_crycb_format(kvm, &kvm->arch.crypto.crycbd);

Doesn't kvm_ap_set_crycb_format() already initialize its second parameter?

Yes it does. I'm going to have to rework this (see comment below)

Quoted 6 lines

Would it make sense to do

kvm->arch.crypto.crycbd = kvm_ap_build_crycbd(kvm);

or so instead?

It would if this was the only place the function gets called. In patch 2, this is called from VSIE and it wouldn't make sense in that context. I like your idea, let me work on this and figure out how best to make it happen.

Quoted 11 lines
Quoted 11 lines
arch/s390/kvm/kvm-s390.c +1 -3
+
  	if (!test_kvm_facility(kvm, 76))
  		return;
 
-	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
-	kvm_s390_set_crycb_format(kvm);
-
  	/* Enable AES/DEA protected key functions by default */
  	kvm->arch.crypto.aes_kw = 1;
  	kvm->arch.crypto.dea_kw = 1;
#4 lore raw

Re: [PATCH v2 01/15] KVM: s390: refactor crypto initialization

Quoted 30 lines · On Wed, 28 Feb 2018 16:23:29 -0500 Tony Krowiak <[email protected]>…
Quoted 21 lines · Cornelia Huck
Quoted 18 lines · On Tue, 27 Feb 2018 09:27:59 -0500 Tony Krowiak <[email protected]>…
arch/s390/kvm/kvm-ap.c new file +12 -0
new file mode 100644
index 0000000..5305f4c
@@ -0,0 +1,47 @@
+/*
+ * Adjunct Processor (AP) configuration management for KVM guests
+ *
+ * Copyright IBM Corp. 2017
+ *
+ * Author(s): Tony Krowiak <[email protected]>
+ */
+
+#include <asm/kvm-ap.h>
+#include <asm/ap.h>
+
+#include "kvm-s390.h"
Quoted 8 lines

arch/s390/kvm/kvm-ap.c line 19

Quoted 7 lines
arch/s390/kvm/kvm-ap.c new file +7 -0
+
+static int kvm_ap_apxa_installed(void)
+{
+	int ret;
+	struct ap_config_info config;
+
+	ret = ap_query_configuration(&config);  

Doesn't that introduce a dependency on CONFIG_ZCRYPT?

Quoted 2 lines

It does, but AFAIK zcrypt is built into the kernel. Or is that not what you are asking?

It is built into the kernel (and not into a module) if CONFIG_ZCRYPT is set. When I compile a kernel with CONFIG_KVM set and CONFIG_ZCRYPT unset, with this patch applied I get

arch/s390/kvm/kvm-ap.o: In function `kvm_ap_apxa_installed':
/home/cohuck/git/linux/arch/s390/kvm/kvm-ap.c:19: undefined reference to `ap_query_configuration'

So I'm afraid you cannot use ap_query_configuration() in base s390 kvm code unless you move that function to a place where it is always built (or at least always built if either CONFIG_KVM or CONFIG_ZCRYPT are set).

#5 lore raw

Re: [PATCH v2 01/15] KVM: s390: refactor crypto initialization

Quoted 38 lines · Cornelia Huck
Quoted 29 lines · On Wed, 28 Feb 2018 16:23:29 -0500 Tony Krowiak <[email protected]>…
Quoted 20 lines · Cornelia Huck
Quoted 18 lines · On Tue, 27 Feb 2018 09:27:59 -0500 Tony Krowiak <[email protected]>…
arch/s390/kvm/kvm-ap.c new file +12 -0
new file mode 100644
index 0000000..5305f4c
@@ -0,0 +1,47 @@
+/*
+ * Adjunct Processor (AP) configuration management for KVM guests
+ *
+ * Copyright IBM Corp. 2017
+ *
+ * Author(s): Tony Krowiak <[email protected]>
+ */
+
+#include <asm/kvm-ap.h>
+#include <asm/ap.h>
+
+#include "kvm-s390.h"
Quoted 8 lines

arch/s390/kvm/kvm-ap.c line 19

Quoted 7 lines
arch/s390/kvm/kvm-ap.c new file +7 -0
+
+static int kvm_ap_apxa_installed(void)
+{
+	int ret;
+	struct ap_config_info config;
+
+	ret = ap_query_configuration(&config);

Doesn't that introduce a dependency on CONFIG_ZCRYPT?

Quoted 2 lines

It does, but AFAIK zcrypt is built into the kernel. Or is that not what you are asking?

It is built into the kernel (and not into a module) if CONFIG_ZCRYPT is set. When I compile a kernel with CONFIG_KVM set and CONFIG_ZCRYPT unset, with this patch applied I get

Quoted 7 lines

arch/s390/kvm/kvm-ap.o: In function `kvm_ap_apxa_installed':
/home/cohuck/git/linux/arch/s390/kvm/kvm-ap.c:19: undefined reference to `ap_query_configuration'

So I'm afraid you cannot use ap_query_configuration() in base s390 kvm code unless you move that function to a place where it is always built (or at least always built if either CONFIG_KVM or CONFIG_ZCRYPT are set).

I was able to get around this by inserting the following into arch/s390/kvm/Kconfig:

config KVM
def_tristate y
prompt "Kernel-based Virtual Machine (KVM) support" depends on HAVE_KVM
select PREEMPT_NOTIFIERS
...
select ZCRYPT

If this is a problem, we can discuss it in the v3 which is forthcoming very shortly

Quoted 1 line
#6 lore raw

PATCH v2 2/15 s390: vsie: implement AP support for second level guest

In reply to Tony Krowiak

Set effective masks and CRYCB format in the shadow copy of the guest level 2 CRYCB.

---
 arch/s390/include/asm/kvm-ap.h |    2 +
 arch/s390/kvm/kvm-ap.c         |    5 +++
 arch/s390/kvm/vsie.c           |   71 +++++++++++++++++++++++++++++++++-------
 3 files changed, 66 insertions(+), 12 deletions(-)
arch/s390/include/asm/kvm-ap.h +2 -0
index 4e43117..ef749e7 100644
@@ -13,4 +13,6 @@
 
 void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd);
 
+int kvm_ap_get_crycb_format(struct kvm *kvm);
+
 #endif /* _ASM_KVM_AP */
arch/s390/kvm/kvm-ap.c +5 -0
index 5305f4c..bafe63b 100644
@@ -11,6 +11,11 @@
 
 #include "kvm-s390.h"
 
+int kvm_ap_get_crycb_format(struct kvm *kvm)
+{
+	return kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK;
+}
+
 static int kvm_ap_apxa_installed(void)
 {
 	int ret;
arch/s390/kvm/vsie.c +59 -12

99 lines not on this page · raw message

Signature · 1 line
1.7.1
#7 lore raw

Re: [PATCH v2 02/15] s390: vsie: implement AP support for second level guest

Quoted 26 lines · Tony Krowiak

Set effective masks and CRYCB format in the shadow copy of the guest level 2 CRYCB.

---
 arch/s390/include/asm/kvm-ap.h |    2 +
 arch/s390/kvm/kvm-ap.c         |    5 +++
 arch/s390/kvm/vsie.c           |   71 +++++++++++++++++++++++++++++++++-------
 3 files changed, 66 insertions(+), 12 deletions(-)
arch/s390/include/asm/kvm-ap.h +2 -0
index 4e43117..ef749e7 100644
@@ -13,4 +13,6 @@
 
 void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd);
 
+int kvm_ap_get_crycb_format(struct kvm *kvm);
+
 #endif /* _ASM_KVM_AP */
arch/s390/kvm/kvm-ap.c +0 -0
index 5305f4c..bafe63b 100644

arch/s390/kvm/kvm-ap.c line 17

Quoted 7 lines
arch/s390/kvm/kvm-ap.c +4 -0
@@ -11,6 +11,11 @@
 
 #include "kvm-s390.h"
 
+int kvm_ap_get_crycb_format(struct kvm *kvm)
+{
+	return kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK;
+}

Why exactly do we need this function? If there are no other users, just do it directly in the code below.

Quoted 22 lines
arch/s390/kvm/kvm-ap.c +1 -0
+
 static int kvm_ap_apxa_installed(void)
 {
 	int ret;
arch/s390/kvm/vsie.c +3 -0
index 8961e39..93076ba 100644
@@ -18,6 +18,7 @@
 #include <asm/sclp.h>
 #include <asm/nmi.h>
 #include <asm/dis.h>
+#include <asm/kvm-ap.h>
 #include "kvm-s390.h"
 #include "gaccess.h"
 
@@ -137,12 +138,56 @@ static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 }
 
 /*
+ * Set up the effective masks for the shadow copy of the crycb. The effective
+ * masks for guest 3 are set by performing a logical bitwise AND of the guest 3

arch/s390/kvm/vsie.c static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 152

Quoted 10 lines
arch/s390/kvm/vsie.c +10 -0
+ * masks with the guest 2 masks.
+ */
+static void set_crycb_emasks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
+{
+	int fmt = kvm_ap_get_crycb_format(vcpu->kvm);
+	unsigned long *mask1, *mask2;
+
+	switch (fmt) {
+	case CRYCB_FORMAT1:
+	case CRYCB_FORMAT2:

Assume L2 gave us FORMAT0 and we are using FORMAT2. You would copy wrong bits. See below, maybe we need conversion.

Quoted 48 lines
arch/s390/kvm/vsie.c +35 -3
+		mask1 = (unsigned long *)vsie_page->crycb.apcb1.apm;
+		mask2 = (unsigned long *)
+			vcpu->kvm->arch.crypto.crycb->apcb1.apm;
+		bitmap_and(mask1, mask1, mask2, APCB1_MASK_SIZE);
+
+		mask1 = (unsigned long *)vsie_page->crycb.apcb1.aqm;
+		mask2 = (unsigned long *)
+			vcpu->kvm->arch.crypto.crycb->apcb1.aqm;
+		bitmap_and(mask1, mask1, mask2, APCB1_MASK_SIZE);
+
+		mask1 = (unsigned long *)vsie_page->crycb.apcb1.adm;
+		mask2 = (unsigned long *)
+			vcpu->kvm->arch.crypto.crycb->apcb1.adm;
+		bitmap_and(mask1, mask1, mask2, APCB1_MASK_SIZE);
+		break;
+	default:
+		mask1 = (unsigned long *)vsie_page->crycb.apcb0.apm;
+		mask2 = (unsigned long *)
+			vcpu->kvm->arch.crypto.crycb->apcb1.apm;
+		bitmap_and(mask1, mask1, mask2, APCB0_MASK_SIZE);
+
+		mask1 = (unsigned long *)vsie_page->crycb.apcb0.aqm;
+		mask2 = (unsigned long *)
+			vcpu->kvm->arch.crypto.crycb->apcb1.aqm;
+		bitmap_and(mask1, mask1, mask2, APCB0_MASK_SIZE);
+
+		mask1 = (unsigned long *)vsie_page->crycb.apcb0.adm;
+		mask2 = (unsigned long *)
+			vcpu->kvm->arch.crypto.crycb->apcb1.adm;
+		bitmap_and(mask1, mask1, mask2, APCB0_MASK_SIZE);
+		break;
+	}
+}
+
+/*
  * Create a shadow copy of the crycb block and setup key wrapping, if
  * requested for guest 3 and enabled for guest 2.
  *
- * We only accept format-1 (no AP in g2), but convert it into format-2
- * There is nothing to do for format-0.
- *
  * Returns: - 0 if shadowed or nothing to do
  *          - > 0 if control has to be given to guest 2
  */
@@ -155,9 +200,17 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 	unsigned long *b1, *b2;
 	u8 ecb3_flags;
 

arch/s390/kvm/vsie.c static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 209

Quoted 10 lines
arch/s390/kvm/vsie.c +7 -3
-	scb_s->crycbd = 0;
-	if (!(crycbd_o & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))
-		return 0;
+	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb);
+	kvm_ap_set_crycb_format(vcpu->kvm, &scb_s->crycbd);
+
+	/* copy the crycb */
+	if (read_guest_real(vcpu, crycb_addr, &vsie_page->crycb,
+			    sizeof(vsie_page->crycb)))
+		return set_validity_icpt(scb_s, 0x0035U);

This looks wrong. You are always copying from L2, although L2 might not even set up a crycb for L3. You removed the important crycbd_o check.

Don't we need the following?

a) Determine the _allowed_ crycb format for L2 -> L3. (How can we tell which format L2 is allowed to use for L3)
b) Determine the target crycb format. This is basically vm_ap_set_crycb_format afaics.
c) Convert the given crycb to the target crycb format.

So importantly, can you clarify (as I don't have access to documentation):

- When is L2 allowed to use format-0? So when will the format not be ignored but is actually used?

- When is L2 allowed to use fortma-2? So when will the format not be ignored but is actually used?

- When does the SIE start interpreting AP instructions? So how can we hinder it from doing so? Empty masks? crycb format?

arch/s390/kvm/vsie.c static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 216

Quoted 7 lines
arch/s390/kvm/vsie.c +4 -0
+
+	/* set up the effective masks */
+	set_crycb_emasks(vcpu, vsie_page);
+
 	/* format-1 is supported with message-security-assist extension 3 */
 	if (!test_kvm_facility(vcpu->kvm, 76))
 		return 0;

You now already copied the wrapping keys. So they could be !0. Please add a comment why we don't care.

/* wrapping keys are ignored without ECB3_AES / ECB3_DEA */

Quoted 15 lines
arch/s390/kvm/vsie.c +0 -6
@@ -172,13 +225,7 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 	else if (!crycb_addr)
 		return set_validity_icpt(scb_s, 0x0039U);
 
-	/* copy only the wrapping keys */
-	if (read_guest_real(vcpu, crycb_addr + 72, &vsie_page->crycb, 56))
-		return set_validity_icpt(scb_s, 0x0035U);
-
 	scb_s->ecb3 |= ecb3_flags;
-	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
-			CRYCB_FORMAT2;
 
 	/* xor both blocks in one run */
 	b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
Signature · 3 lines
Thanks,

David / dhildenb
#8 lore raw

Re: [PATCH v2 02/15] s390: vsie: implement AP support for second level guest

Quoted 56 lines · David Hildenbrand
Quoted 26 lines · Tony Krowiak

Set effective masks and CRYCB format in the shadow copy of the guest level 2 CRYCB.

---
  arch/s390/include/asm/kvm-ap.h |    2 +
  arch/s390/kvm/kvm-ap.c         |    5 +++
  arch/s390/kvm/vsie.c           |   71 +++++++++++++++++++++++++++++++++-------
  3 files changed, 66 insertions(+), 12 deletions(-)
arch/s390/include/asm/kvm-ap.h +2 -0
index 4e43117..ef749e7 100644
@@ -13,4 +13,6 @@
 
  void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd);
 
+int kvm_ap_get_crycb_format(struct kvm *kvm);
+
  #endif /* _ASM_KVM_AP */
arch/s390/kvm/kvm-ap.c +0 -0
index 5305f4c..bafe63b 100644

arch/s390/kvm/kvm-ap.c line 17

Quoted 7 lines
arch/s390/kvm/kvm-ap.c +4 -0
@@ -11,6 +11,11 @@
 
  #include "kvm-s390.h"
 
+int kvm_ap_get_crycb_format(struct kvm *kvm)
+{
+	return kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK;
+}

Why exactly do we need this function? If there are no other users, just do it directly in the code below.

Quoted 19 lines
arch/s390/kvm/kvm-ap.c +1 -0
+
  static int kvm_ap_apxa_installed(void)
  {
  	int ret;
arch/s390/kvm/vsie.c +1 -0
index 8961e39..93076ba 100644
@@ -18,6 +18,7 @@
  #include <asm/sclp.h>
  #include <asm/nmi.h>
  #include <asm/dis.h>
+#include <asm/kvm-ap.h>
  #include "kvm-s390.h"
  #include "gaccess.h"
 
@@ -137,12 +138,56 @@ static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  }
 
Quoted 15 lines

arch/s390/kvm/vsie.c static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 152

Quoted 13 lines
arch/s390/kvm/vsie.c +12 -0
  /*
+ * Set up the effective masks for the shadow copy of the crycb. The effective
+ * masks for guest 3 are set by performing a logical bitwise AND of the guest 3
+ * masks with the guest 2 masks.
+ */
+static void set_crycb_emasks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
+{
+	int fmt = kvm_ap_get_crycb_format(vcpu->kvm);
+	unsigned long *mask1, *mask2;
+
+	switch (fmt) {
+	case CRYCB_FORMAT1:
+	case CRYCB_FORMAT2:

Assume L2 gave us FORMAT0 and we are using FORMAT2. You would copy wrong bits. See below, maybe we need conversion.

+1
and in the case we have FORTMAT1 in L2 and/or L3, you also copy them from and/or to the wrong place.

Quoted 109 lines
Quoted 49 lines
arch/s390/kvm/vsie.c +35 -3
+		mask1 = (unsigned long *)vsie_page->crycb.apcb1.apm;
+		mask2 = (unsigned long *)
+			vcpu->kvm->arch.crypto.crycb->apcb1.apm;
+		bitmap_and(mask1, mask1, mask2, APCB1_MASK_SIZE);
+
+		mask1 = (unsigned long *)vsie_page->crycb.apcb1.aqm;
+		mask2 = (unsigned long *)
+			vcpu->kvm->arch.crypto.crycb->apcb1.aqm;
+		bitmap_and(mask1, mask1, mask2, APCB1_MASK_SIZE);
+
+		mask1 = (unsigned long *)vsie_page->crycb.apcb1.adm;
+		mask2 = (unsigned long *)
+			vcpu->kvm->arch.crypto.crycb->apcb1.adm;
+		bitmap_and(mask1, mask1, mask2, APCB1_MASK_SIZE);
+		break;
+	default:
+		mask1 = (unsigned long *)vsie_page->crycb.apcb0.apm;
+		mask2 = (unsigned long *)
+			vcpu->kvm->arch.crypto.crycb->apcb1.apm;
+		bitmap_and(mask1, mask1, mask2, APCB0_MASK_SIZE);
+
+		mask1 = (unsigned long *)vsie_page->crycb.apcb0.aqm;
+		mask2 = (unsigned long *)
+			vcpu->kvm->arch.crypto.crycb->apcb1.aqm;
+		bitmap_and(mask1, mask1, mask2, APCB0_MASK_SIZE);
+
+		mask1 = (unsigned long *)vsie_page->crycb.apcb0.adm;
+		mask2 = (unsigned long *)
+			vcpu->kvm->arch.crypto.crycb->apcb1.adm;
+		bitmap_and(mask1, mask1, mask2, APCB0_MASK_SIZE);
+		break;
+	}
+}
+
+/*
   * Create a shadow copy of the crycb block and setup key wrapping, if
   * requested for guest 3 and enabled for guest 2.
   *
- * We only accept format-1 (no AP in g2), but convert it into format-2
- * There is nothing to do for format-0.
- *
   * Returns: - 0 if shadowed or nothing to do
   *          - > 0 if control has to be given to guest 2
   */
@@ -155,9 +200,17 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  	unsigned long *b1, *b2;
  	u8 ecb3_flags;
 

arch/s390/kvm/vsie.c static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 209

Quoted 10 lines
arch/s390/kvm/vsie.c +7 -3
-	scb_s->crycbd = 0;
-	if (!(crycbd_o & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))
-		return 0;
+	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb);
+	kvm_ap_set_crycb_format(vcpu->kvm, &scb_s->crycbd);
+
+	/* copy the crycb */
+	if (read_guest_real(vcpu, crycb_addr, &vsie_page->crycb,
+			    sizeof(vsie_page->crycb)))
+		return set_validity_icpt(scb_s, 0x0035U);

This looks wrong. You are always copying from L2, although L2 might not even set up a crycb for L3. You removed the important crycbd_o check.

Don't we need the following?

a) Determine the _allowed_ crycb format for L2 -> L3. (How can we tell which format L2 is allowed to use for L3)
b) Determine the target crycb format. This is basically vm_ap_set_crycb_format afaics.
c) Convert the given crycb to the target crycb format.

So importantly, can you clarify (as I don't have access to documentation):

- When is L2 allowed to use format-0? So when will the format not be ignored but is actually used?

- When is L2 allowed to use fortma-2? So when will the format not be ignored but is actually used?

- When does the SIE start interpreting AP instructions? So how can we hinder it from doing so? Empty masks? crycb format?

arch/s390/kvm/vsie.c static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 216

Quoted 7 lines
arch/s390/kvm/vsie.c +4 -0
+
+	/* set up the effective masks */
+	set_crycb_emasks(vcpu, vsie_page);
+
  	/* format-1 is supported with message-security-assist extension 3 */
  	if (!test_kvm_facility(vcpu->kvm, 76))
  		return 0;

You now already copied the wrapping keys. So they could be !0. Please add a comment why we don't care.

/* wrapping keys are ignored without ECB3_AES / ECB3_DEA */

Quoted 15 lines
arch/s390/kvm/vsie.c +0 -6
@@ -172,13 +225,7 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  	else if (!crycb_addr)
  		return set_validity_icpt(scb_s, 0x0039U);
 
-	/* copy only the wrapping keys */
-	if (read_guest_real(vcpu, crycb_addr + 72, &vsie_page->crycb, 56))
-		return set_validity_icpt(scb_s, 0x0035U);
-
  	scb_s->ecb3 |= ecb3_flags;
-	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
-			CRYCB_FORMAT2;
 
  	/* xor both blocks in one run */
  	b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#9 lore raw

PATCH v2 3/15 s390: zcrypt: externalize AP instructions available function

In reply to Tony Krowiak

If the AP instructions are not available on the linux host, then AP devices can not be interpreted by the SIE. The AP bus has a function it uses to determine if the AP instructions are available. This patch provides a new function that wraps the AP bus's function to externalize it for use by KVM.

---
 arch/s390/include/asm/ap.h   |    7 +++++++
 drivers/s390/crypto/ap_bus.c |    6 ++++++
 2 files changed, 13 insertions(+), 0 deletions(-)
arch/s390/include/asm/ap.h +7 -0
index cfce683..1df6b59 100644
@@ -120,4 +120,11 @@ struct ap_queue_status ap_queue_irq_ctrl(ap_qid_t qid,
 					 struct ap_qirq_ctrl qirqctrl,
 					 void *ind);
 
+/**
+ * ap_instructions_installed() - Tests whether AP instructions are installed
+ *
+ * Returns 1 if the AP instructions are installed, otherwise; returns 0
+ */
+int ap_instructions_installed(void);
+
 #endif /* _ASM_S390_AP_H_ */
drivers/s390/crypto/ap_bus.c +6 -0
index 48d55dc..089b1cf 100644
@@ -211,6 +211,12 @@ int ap_query_configuration(struct ap_config_info *info)
 }
 EXPORT_SYMBOL(ap_query_configuration);
 
+int ap_instructions_installed(void)
+{
+	return (ap_instructions_available() == 0);
+}
+EXPORT_SYMBOL(ap_instructions_installed);
+
 /**
  * ap_init_configuration(): Allocate and query configuration array.
  */
Signature · 1 line
1.7.1
Reviewed-by on v2 3/15
#10 lore raw

Re: [PATCH v2 03/15] s390: zcrypt: externalize AP instructions available function

Quoted 36 lines · Tony Krowiak

If the AP instructions are not available on the linux host, then AP devices can not be interpreted by the SIE. The AP bus has a function it uses to determine if the AP instructions are available. This patch provides a new function that wraps the AP bus's function to externalize it for use by KVM.

---
 arch/s390/include/asm/ap.h   |    7 +++++++
 drivers/s390/crypto/ap_bus.c |    6 ++++++
 2 files changed, 13 insertions(+), 0 deletions(-)
arch/s390/include/asm/ap.h +7 -0
index cfce683..1df6b59 100644
@@ -120,4 +120,11 @@ struct ap_queue_status ap_queue_irq_ctrl(ap_qid_t qid,
 					 struct ap_qirq_ctrl qirqctrl,
 					 void *ind);
 
+/**
+ * ap_instructions_installed() - Tests whether AP instructions are installed
+ *
+ * Returns 1 if the AP instructions are installed, otherwise; returns 0
+ */
+int ap_instructions_installed(void);
+
 #endif /* _ASM_S390_AP_H_ */
drivers/s390/crypto/ap_bus.c +0 -0
index 48d55dc..089b1cf 100644
@@ -211,6 +211,12 @@ int ap_query_configuration(struct ap_config_info *info)
 }
 EXPORT_SYMBOL(ap_query_configuration);

drivers/s390/crypto/ap_bus.c int ap_query_configuration(struct ap_config_info *info) · line 222

Quoted 10 lines
drivers/s390/crypto/ap_bus.c +6 -0
 
+int ap_instructions_installed(void)
+{
+	return (ap_instructions_available() == 0);
+}
+EXPORT_SYMBOL(ap_instructions_installed);
+
 /**
  * ap_init_configuration(): Allocate and query configuration array.
  */
#11 lore raw

Re: [PATCH v2 03/15] s390: zcrypt: externalize AP instructions available function

In reply to Tony Krowiak
Quoted 6 lines · On Tue, 27 Feb 2018 09:28:01 -0500 Tony Krowiak <[email protected]>…

If the AP instructions are not available on the linux host, then AP devices can not be interpreted by the SIE. The AP bus has a function it uses to determine if the AP instructions are available. This patch provides a new function that wraps the AP bus's function to externalize it for use by KVM.

Quoted 6 lines
---
 arch/s390/include/asm/ap.h   |    7 +++++++
 drivers/s390/crypto/ap_bus.c |    6 ++++++
 2 files changed, 13 insertions(+), 0 deletions(-)

While I don't see anything wrong with this patch as it stands, you need to be careful to call this only from vfio-ap code (which depends on CONFIG_ZCRYPT).

Speaking of which: the ap_bus part cannot be a module, but only built-in, right? So we don't get additional module complications?

#12 lore raw

Re: [PATCH v2 03/15] s390: zcrypt: externalize AP instructions available function

Quoted 15 lines · Cornelia Huck
Quoted 6 lines · On Tue, 27 Feb 2018 09:28:01 -0500 Tony Krowiak <[email protected]>…

If the AP instructions are not available on the linux host, then AP devices can not be interpreted by the SIE. The AP bus has a function it uses to determine if the AP instructions are available. This patch provides a new function that wraps the AP bus's function to externalize it for use by KVM.

Quoted 6 lines
---
 arch/s390/include/asm/ap.h   |    7 +++++++
 drivers/s390/crypto/ap_bus.c |    6 ++++++
 2 files changed, 13 insertions(+), 0 deletions(-)
Quoted 6 lines

While I don't see anything wrong with this patch as it stands, you need to be careful to call this only from vfio-ap code (which depends on CONFIG_ZCRYPT).

Speaking of which: the ap_bus part cannot be a module, but only built-in, right? So we don't get additional module complications?

Right, the AP bus is not available as a module but statically build.

Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-s390" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#13 lore raw

PATCH v2 4/15 KVM: s390: CPU model support for AP virtualization

In reply to Tony Krowiak

Introduces a new CPU model feature and two CPU model facilities to support AP virtualization for KVM guests.

CPU model feature:

The KVM_S390_VM_CPU_FEAT_AP feature indicates that the AP facilities are installed on the KVM guest. This feature will be enabled by the kernel only if the AP facilities are installed on the linux host. This feature must be specifically turned on for the KVM guest from userspace to allow guest access to AP devices installed on the linux host.

CPU model facilities:

1. AP Query Configuration Information (QCI) facility is installed.

This is indicated by setting facilities bit 12 for the guest. The kernel will not enable this facility for the guest if it is not set on the host. This facility must not be set by userspace if the KVM_S390_VM_CPU_FEAT_AP feature is not installed.

2. AP Facilities Test facility (APFT) is installed.

This is indicated by setting facilities bit 15 for the guest. The kernel will not enable this facility for the guest if it is not set on the host. This facility must not be set by userspace if the KVM_S390_VM_CPU_FEAT_AP feature is not installed.

---
 arch/s390/include/uapi/asm/kvm.h |    1 +
 arch/s390/kvm/kvm-s390.c         |    4 ++++
 arch/s390/tools/gen_facilities.c |    2 ++
 3 files changed, 7 insertions(+), 0 deletions(-)
arch/s390/include/uapi/asm/kvm.h +1 -0
index 4cdaa55..a580dec 100644
@@ -130,6 +130,7 @@ struct kvm_s390_vm_cpu_machine {
 #define KVM_S390_VM_CPU_FEAT_PFMFI	11
 #define KVM_S390_VM_CPU_FEAT_SIGPIF	12
 #define KVM_S390_VM_CPU_FEAT_KSS	13
+#define KVM_S390_VM_CPU_FEAT_AP		14
 struct kvm_s390_vm_cpu_feat {
 	__u64 feat[16];
 };
arch/s390/kvm/kvm-s390.c +4 -0
index de1e299..c68ca86 100644
@@ -347,6 +347,10 @@ static void kvm_s390_cpu_feat_init(void)
 
 	if (MACHINE_HAS_ESOP)
 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
+
+	if (ap_instructions_installed()) /* AP instructions installed on host */
+		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_AP);
+
 	/*
 	 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
 	 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
arch/s390/tools/gen_facilities.c +2 -0
index 90a8c9e..a52290b 100644
@@ -106,6 +106,8 @@ struct facility_def {
 
 		.name = "FACILITIES_KVM_CPUMODEL",
 		.bits = (int[]){
+			12, /* AP Query Configuration Information */
+			15, /* AP Facilities Test */
 			-1  /* END */
 		}
 	},
Signature · 1 line
1.7.1
#14 lore raw

Re: [PATCH v2 04/15] KVM: s390: CPU model support for AP virtualization

Quoted 57 lines · Tony Krowiak

Introduces a new CPU model feature and two CPU model facilities to support AP virtualization for KVM guests.

CPU model feature:

The KVM_S390_VM_CPU_FEAT_AP feature indicates that the AP facilities are installed on the KVM guest. This feature will be enabled by the kernel only if the AP facilities are installed on the linux host. This feature must be specifically turned on for the KVM guest from userspace to allow guest access to AP devices installed on the linux host.

CPU model facilities:

1. AP Query Configuration Information (QCI) facility is installed.

This is indicated by setting facilities bit 12 for the guest. The kernel will not enable this facility for the guest if it is not set on the host. This facility must not be set by userspace if the KVM_S390_VM_CPU_FEAT_AP feature is not installed.

2. AP Facilities Test facility (APFT) is installed.

This is indicated by setting facilities bit 15 for the guest. The kernel will not enable this facility for the guest if it is not set on the host. This facility must not be set by userspace if the KVM_S390_VM_CPU_FEAT_AP feature is not installed.

---
 arch/s390/include/uapi/asm/kvm.h |    1 +
 arch/s390/kvm/kvm-s390.c         |    4 ++++
 arch/s390/tools/gen_facilities.c |    2 ++
 3 files changed, 7 insertions(+), 0 deletions(-)
arch/s390/include/uapi/asm/kvm.h +1 -0
index 4cdaa55..a580dec 100644
@@ -130,6 +130,7 @@ struct kvm_s390_vm_cpu_machine {
 #define KVM_S390_VM_CPU_FEAT_PFMFI	11
 #define KVM_S390_VM_CPU_FEAT_SIGPIF	12
 #define KVM_S390_VM_CPU_FEAT_KSS	13
+#define KVM_S390_VM_CPU_FEAT_AP		14
 struct kvm_s390_vm_cpu_feat {
 	__u64 feat[16];
 };
arch/s390/kvm/kvm-s390.c +0 -0
index de1e299..c68ca86 100644

arch/s390/kvm/kvm-s390.c static void kvm_s390_cpu_feat_init(void) · line 352

Quoted 6 lines
arch/s390/kvm/kvm-s390.c +3 -0
@@ -347,6 +347,10 @@ static void kvm_s390_cpu_feat_init(void)
 
 	if (MACHINE_HAS_ESOP)
 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
+
+	if (ap_instructions_installed()) /* AP instructions installed on host */
+		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_AP);

Don't we have a SIE specific AP feature? So is it true, that once we have AP instructions, we are allowed to use them for SIE? Isn't there a "AP interpretation facility" or anything like that? (that unlocks ECA_APIE)

Quoted 10 lines
arch/s390/kvm/kvm-s390.c +1 -0
+
 	/*
 	 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
 	 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
arch/s390/tools/gen_facilities.c +0 -0
index 90a8c9e..a52290b 100644

arch/s390/tools/gen_facilities.c struct facility_def { · line 113

Quoted 8 lines
arch/s390/tools/gen_facilities.c +2 -0
@@ -106,6 +106,8 @@ struct facility_def {
 
 		.name = "FACILITIES_KVM_CPUMODEL",
 		.bits = (int[]){
+			12, /* AP Query Configuration Information */
+			15, /* AP Facilities Test */
 			-1  /* END */
 		}
 	},

So only if this feature is enabled, we later on allow to kvm_ap_enable_ie_mode, right?

So basically, without this feature:

1. We will never set the execution control ECA_APIE.
2. The masks will always be 0.

Which also results in VSIE never having masks set.

Signature · 3 lines
Thanks,

David / dhildenb
#15 lore raw

Re: [PATCH v2 04/15] KVM: s390: CPU model support for AP virtualization

Quoted 59 lines · David Hildenbrand
Quoted 58 lines · Tony Krowiak

Introduces a new CPU model feature and two CPU model facilities to support AP virtualization for KVM guests.

CPU model feature:

The KVM_S390_VM_CPU_FEAT_AP feature indicates that the AP facilities are installed on the KVM guest. This feature will be enabled by the kernel only if the AP facilities are installed on the linux host. This feature must be specifically turned on for the KVM guest from userspace to allow guest access to AP devices installed on the linux host.

CPU model facilities:

1. AP Query Configuration Information (QCI) facility is installed.

This is indicated by setting facilities bit 12 for the guest. The kernel will not enable this facility for the guest if it is not set on the host. This facility must not be set by userspace if the KVM_S390_VM_CPU_FEAT_AP feature is not installed.

2. AP Facilities Test facility (APFT) is installed.

This is indicated by setting facilities bit 15 for the guest. The kernel will not enable this facility for the guest if it is not set on the host. This facility must not be set by userspace if the KVM_S390_VM_CPU_FEAT_AP feature is not installed.

---
 arch/s390/include/uapi/asm/kvm.h |    1 +
 arch/s390/kvm/kvm-s390.c         |    4 ++++
 arch/s390/tools/gen_facilities.c |    2 ++
 3 files changed, 7 insertions(+), 0 deletions(-)
arch/s390/include/uapi/asm/kvm.h +1 -0
index 4cdaa55..a580dec 100644
@@ -130,6 +130,7 @@ struct kvm_s390_vm_cpu_machine {
 #define KVM_S390_VM_CPU_FEAT_PFMFI	11
 #define KVM_S390_VM_CPU_FEAT_SIGPIF	12
 #define KVM_S390_VM_CPU_FEAT_KSS	13
+#define KVM_S390_VM_CPU_FEAT_AP		14
 struct kvm_s390_vm_cpu_feat {
 	__u64 feat[16];
 };
arch/s390/kvm/kvm-s390.c +0 -0
index de1e299..c68ca86 100644
@@ -347,6 +347,10 @@ static void kvm_s390_cpu_feat_init(void)
 
Quoted 9 lines

arch/s390/kvm/kvm-s390.c static void kvm_s390_cpu_feat_init(void) · line 352

Quoted 5 lines
arch/s390/kvm/kvm-s390.c +3 -0
 	if (MACHINE_HAS_ESOP)
 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
+
+	if (ap_instructions_installed()) /* AP instructions installed on host */
+		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_AP);

Don't we have a SIE specific AP feature? So is it true, that once we have AP instructions, we are allowed to use them for SIE? Isn't there a "AP interpretation facility" or anything like that? (that unlocks ECA_APIE)

This seems to be coupled to the AP facility and there is no facility or scp bit. AP is too old to have that (predates STFLE)

#16 lore raw

Re: [PATCH v2 04/15] KVM: s390: CPU model support for AP virtualization

Quoted 72 lines · Christian Borntraeger
Quoted 59 lines · David Hildenbrand
Quoted 58 lines · Tony Krowiak

Introduces a new CPU model feature and two CPU model facilities to support AP virtualization for KVM guests.

CPU model feature:

The KVM_S390_VM_CPU_FEAT_AP feature indicates that the AP facilities are installed on the KVM guest. This feature will be enabled by the kernel only if the AP facilities are installed on the linux host. This feature must be specifically turned on for the KVM guest from userspace to allow guest access to AP devices installed on the linux host.

CPU model facilities:

1. AP Query Configuration Information (QCI) facility is installed.

This is indicated by setting facilities bit 12 for the guest. The kernel will not enable this facility for the guest if it is not set on the host. This facility must not be set by userspace if the KVM_S390_VM_CPU_FEAT_AP feature is not installed.

2. AP Facilities Test facility (APFT) is installed.

This is indicated by setting facilities bit 15 for the guest. The kernel will not enable this facility for the guest if it is not set on the host. This facility must not be set by userspace if the KVM_S390_VM_CPU_FEAT_AP feature is not installed.

---
 arch/s390/include/uapi/asm/kvm.h |    1 +
 arch/s390/kvm/kvm-s390.c         |    4 ++++
 arch/s390/tools/gen_facilities.c |    2 ++
 3 files changed, 7 insertions(+), 0 deletions(-)
arch/s390/include/uapi/asm/kvm.h +1 -0
index 4cdaa55..a580dec 100644
@@ -130,6 +130,7 @@ struct kvm_s390_vm_cpu_machine {
 #define KVM_S390_VM_CPU_FEAT_PFMFI	11
 #define KVM_S390_VM_CPU_FEAT_SIGPIF	12
 #define KVM_S390_VM_CPU_FEAT_KSS	13
+#define KVM_S390_VM_CPU_FEAT_AP		14
 struct kvm_s390_vm_cpu_feat {
 	__u64 feat[16];
 };
arch/s390/kvm/kvm-s390.c +0 -0
index de1e299..c68ca86 100644
@@ -347,6 +347,10 @@ static void kvm_s390_cpu_feat_init(void)
 
Quoted 9 lines

arch/s390/kvm/kvm-s390.c static void kvm_s390_cpu_feat_init(void) · line 352

Quoted 5 lines
arch/s390/kvm/kvm-s390.c +3 -0
 	if (MACHINE_HAS_ESOP)
 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
+
+	if (ap_instructions_installed()) /* AP instructions installed on host */
+		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_AP);

Don't we have a SIE specific AP feature? So is it true, that once we have AP instructions, we are allowed to use them for SIE? Isn't there a "AP interpretation facility" or anything like that? (that unlocks ECA_APIE)

Quoted 3 lines

This seems to be coupled to the AP facility and there is no facility or scp bit. AP is too old to have that (predates STFLE)

Interesting, so we (I :) ) didn't implement vSIE support back then because we never indicated the AP facility (sensed by executing the instruction) to the guest.

So ECA_APIE can be used when we sense the AP facility. Thanks!

Signature · 3 lines
Thanks,

David / dhildenb
#17 lore raw

Re: [PATCH v2 04/15] KVM: s390: CPU model support for AP virtualization

Quoted 70 lines · David Hildenbrand
Quoted 69 lines · Christian Borntraeger
Quoted 67 lines · David Hildenbrand
Quoted 57 lines · Tony Krowiak

Introduces a new CPU model feature and two CPU model facilities to support AP virtualization for KVM guests.

CPU model feature:

The KVM_S390_VM_CPU_FEAT_AP feature indicates that the AP facilities are installed on the KVM guest. This feature will be enabled by the kernel only if the AP facilities are installed on the linux host. This feature must be specifically turned on for the KVM guest from userspace to allow guest access to AP devices installed on the linux host.

CPU model facilities:

1. AP Query Configuration Information (QCI) facility is installed.

This is indicated by setting facilities bit 12 for the guest. The kernel will not enable this facility for the guest if it is not set on the host. This facility must not be set by userspace if the KVM_S390_VM_CPU_FEAT_AP feature is not installed.

2. AP Facilities Test facility (APFT) is installed.

This is indicated by setting facilities bit 15 for the guest. The kernel will not enable this facility for the guest if it is not set on the host. This facility must not be set by userspace if the KVM_S390_VM_CPU_FEAT_AP feature is not installed.

---
  arch/s390/include/uapi/asm/kvm.h |    1 +
  arch/s390/kvm/kvm-s390.c         |    4 ++++
  arch/s390/tools/gen_facilities.c |    2 ++
  3 files changed, 7 insertions(+), 0 deletions(-)
arch/s390/include/uapi/asm/kvm.h +1 -0
index 4cdaa55..a580dec 100644
@@ -130,6 +130,7 @@ struct kvm_s390_vm_cpu_machine {
  #define KVM_S390_VM_CPU_FEAT_PFMFI	11
  #define KVM_S390_VM_CPU_FEAT_SIGPIF	12
  #define KVM_S390_VM_CPU_FEAT_KSS	13
+#define KVM_S390_VM_CPU_FEAT_AP		14
  struct kvm_s390_vm_cpu_feat {
  	__u64 feat[16];
  };
arch/s390/kvm/kvm-s390.c +0 -0
index de1e299..c68ca86 100644

arch/s390/kvm/kvm-s390.c static void kvm_s390_cpu_feat_init(void) · line 352

Quoted 6 lines
arch/s390/kvm/kvm-s390.c +3 -0
@@ -347,6 +347,10 @@ static void kvm_s390_cpu_feat_init(void)
 
  	if (MACHINE_HAS_ESOP)
  		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
+
+	if (ap_instructions_installed()) /* AP instructions installed on host */
+		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_AP);

Don't we have a SIE specific AP feature? So is it true, that once we have AP instructions, we are allowed to use them for SIE? Isn't there a "AP interpretation facility" or anything like that? (that unlocks ECA_APIE)

Quoted 8 lines
Quoted 3 lines

This seems to be coupled to the AP facility and there is no facility or scp bit. AP is too old to have that (predates STFLE)

Interesting, so we (I :) ) didn't implement vSIE support back then because we never indicated the AP facility (sensed by executing the instruction) to the guest.

So ECA_APIE can be used when we sense the AP facility. Thanks!

All true.

Quoted 1 line
#18 lore raw

PATCH v2 5/15 s390: vfio-ap: base implementation of VFIO AP device driver

In reply to Tony Krowiak

Introduces a new AP device driver. This device driver is built on the VFIO mediated device framework. The framework provides sysfs interfaces that facilitate passthrough access by guests to devices installed on the linux host.

The VFIO AP device driver will serve two purposes:

1. Provide the interfaces to reserve AP devices for exclusive use by KVM guests. This is accomplished by unbinding the devices to be reserved for guest usage from the default AP device driver and binding them to the VFIO AP device driver.

2. Implements the functions, callbacks and sysfs attribute interfaces required to create one or more VFIO mediated devices each of which will be used to configure the AP matrix for a guest and serve as a file descriptor for facilitating communication between QEMU and the VFIO AP device driver.

When the VFIO AP device driver is initialized:

* It registers with the AP bus for control of type 10 (CEX4 and newer) AP queue devices. The probe and remove callbacks will be provided to support the binding/unbinding of AP queue devices to/from the VFIO AP device driver.

* Creates a /sys/devices/vfio-ap/matrix device to hold the APQNs of the AP devices bound to the VFIO AP device driver and serves as the parent of the mediated devices created for each guest.

---
 MAINTAINERS                             |    2 +
 arch/s390/Kconfig                       |    8 ++
 arch/s390/configs/default_defconfig     |    3 +
 arch/s390/configs/gcov_defconfig        |    3 +
 arch/s390/configs/performance_defconfig |    3 +
 arch/s390/defconfig                     |    3 +
 drivers/s390/crypto/Makefile            |    4 +
 drivers/s390/crypto/vfio_ap_drv.c       |  134 +++++++++++++++++++++++++++++++
 drivers/s390/crypto/vfio_ap_private.h   |   20 +++++
 include/uapi/linux/vfio.h               |    2 +
 10 files changed, 182 insertions(+), 0 deletions(-)
 create mode 100644 drivers/s390/crypto/vfio_ap_drv.c
 create mode 100644 drivers/s390/crypto/vfio_ap_private.h
MAINTAINERS +2 -0
index 4acf7c2..a2f232d 100644
@@ -11884,6 +11884,8 @@ W:	http://www.ibm.com/developerworks/linux/linux390/
 S:	Supported
 F:	arch/s390/include/asm/kvm/kvm-ap.h
 F:	arch/s390/kvm/kvm-ap.c
+F:	drivers/s390/crypto/vfio_ap_drv.c
+F:	drivers/s390/crypto/vfio_ap_private.h
 
 S390 ZFCP DRIVER
 M:	Steffen Maier <[email protected]>
arch/s390/Kconfig +8 -0
index cbe1d97..9f23caf 100644
@@ -771,6 +771,14 @@ config VFIO_CCW
 	  To compile this driver as a module, choose M here: the
 	  module will be called vfio_ccw.
 
+config VFIO_AP
+	def_tristate m
+	prompt "Support for virtual Adjunct Processor device interface"
+	depends on ZCRYPT && VFIO_MDEV_DEVICE
+	help
+		driver grants access to Adjunct Processor (AP) devices
+		via the VFIO mediated device interface.
+
 endmenu
 
 menu "Dump support"
arch/s390/configs/default_defconfig +3 -0
index 5af8458..40fa3f6 100644
@@ -719,3 +719,6 @@ CONFIG_APPLDATA_BASE=y
 CONFIG_KVM=m
 CONFIG_KVM_S390_UCONTROL=y
 CONFIG_VHOST_NET=m
+VFIO_MDEV=m
+VFIO_MDEV_DEVICE=m
+CONFIG_VFIO_AP=m
arch/s390/configs/gcov_defconfig +3 -0
index d52eafe..377d40f 100644
@@ -659,3 +659,6 @@ CONFIG_APPLDATA_BASE=y
 CONFIG_KVM=m
 CONFIG_KVM_S390_UCONTROL=y
 CONFIG_VHOST_NET=m
+VFIO_MDEV=m
+VFIO_MDEV_DEVICE=m
+CONFIG_VFIO_AP=m
arch/s390/configs/performance_defconfig +3 -0
index 20ed149..f0c52df 100644
@@ -657,3 +657,6 @@ CONFIG_APPLDATA_BASE=y
 CONFIG_KVM=m
 CONFIG_KVM_S390_UCONTROL=y
 CONFIG_VHOST_NET=m
+VFIO_MDEV=m
+VFIO_MDEV_DEVICE=m
+CONFIG_VFIO_AP=m
arch/s390/defconfig +3 -0
index 46a3178..0996eb7 100644
@@ -239,3 +239,6 @@ CONFIG_CRC7=m
 # CONFIG_XZ_DEC_ARMTHUMB is not set
 # CONFIG_XZ_DEC_SPARC is not set
 CONFIG_CMM=m
+VFIO_MDEV=m
+VFIO_MDEV_DEVICE=m
+CONFIG_VFIO_AP=m
drivers/s390/crypto/Makefile +4 -0
index b59af54..48e466e 100644
@@ -15,3 +15,7 @@ obj-$(CONFIG_ZCRYPT) += zcrypt_pcixcc.o zcrypt_cex2a.o zcrypt_cex4.o
 # pkey kernel module
 pkey-objs := pkey_api.o
 obj-$(CONFIG_PKEY) += pkey.o
+
+# adjunct processor matrix
+vfio_ap-objs := vfio_ap_drv.o
+obj-$(CONFIG_VFIO_AP) += vfio_ap.o
drivers/s390/crypto/vfio_ap_drv.c new file +134 -0

134 lines not on this page · raw message

drivers/s390/crypto/vfio_ap_private.h new file +20 -0
new file mode 100644
index 0000000..3505947
@@ -0,0 +1,20 @@
+/*
+ * Private data and functions for adjunct processor VFIO matrix driver.
+ *
+ * Copyright IBM Corp. 2017
+ * Author(s): Tony Krowiak <[email protected]>
+ */
+
+#ifndef _VFIO_AP_PRIVATE_H_
+#define _VFIO_AP_PRIVATE_H_
+
+#include <linux/types.h>
+
+#define VFIO_AP_MODULE_NAME "vfio_ap"
+#define VFIO_AP_DRV_NAME "vfio_ap"
+
+struct ap_matrix {
+	struct device device;
+};
+
+#endif /* _VFIO_AP_PRIVATE_H_ */
include/uapi/linux/vfio.h +2 -0
index e3301db..cf2a5e9 100644
@@ -200,6 +200,7 @@ struct vfio_device_info {
 #define VFIO_DEVICE_FLAGS_PLATFORM (1 << 2)	/* vfio-platform device */
 #define VFIO_DEVICE_FLAGS_AMBA  (1 << 3)	/* vfio-amba device */
 #define VFIO_DEVICE_FLAGS_CCW	(1 << 4)	/* vfio-ccw device */
+#define VFIO_DEVICE_FLAGS_AP	(1 << 5)	/* vfio-ap device */
 	__u32	num_regions;	/* Max region index + 1 */
 	__u32	num_irqs;	/* Max IRQ index + 1 */
 };
@@ -215,6 +216,7 @@ struct vfio_device_info {
 #define VFIO_DEVICE_API_PLATFORM_STRING		"vfio-platform"
 #define VFIO_DEVICE_API_AMBA_STRING		"vfio-amba"
 #define VFIO_DEVICE_API_CCW_STRING		"vfio-ccw"
+#define VFIO_DEVICE_API_AP_STRING		"vfio-ap"
 
 /**
  * VFIO_DEVICE_GET_REGION_INFO - _IOWR(VFIO_TYPE, VFIO_BASE + 8,
Signature · 1 line
1.7.1
#19 lore raw

Re: [PATCH v2 05/15] s390: vfio-ap: base implementation of VFIO AP device driver

Quoted 65 lines · Tony Krowiak

Introduces a new AP device driver. This device driver is built on the VFIO mediated device framework. The framework provides sysfs interfaces that facilitate passthrough access by guests to devices installed on the linux host.

The VFIO AP device driver will serve two purposes:

1. Provide the interfaces to reserve AP devices for exclusive use by KVM guests. This is accomplished by unbinding the devices to be reserved for guest usage from the default AP device driver and binding them to the VFIO AP device driver.

2. Implements the functions, callbacks and sysfs attribute interfaces required to create one or more VFIO mediated devices each of which will be used to configure the AP matrix for a guest and serve as a file descriptor for facilitating communication between QEMU and the VFIO AP device driver.

When the VFIO AP device driver is initialized:

* It registers with the AP bus for control of type 10 (CEX4 and newer) AP queue devices. The probe and remove callbacks will be provided to support the binding/unbinding of AP queue devices to/from the VFIO AP device driver.

* Creates a /sys/devices/vfio-ap/matrix device to hold the APQNs of the AP devices bound to the VFIO AP device driver and serves as the parent of the mediated devices created for each guest.

---
  MAINTAINERS                             |    2 +
  arch/s390/Kconfig                       |    8 ++
  arch/s390/configs/default_defconfig     |    3 +
  arch/s390/configs/gcov_defconfig        |    3 +
  arch/s390/configs/performance_defconfig |    3 +
  arch/s390/defconfig                     |    3 +
  drivers/s390/crypto/Makefile            |    4 +
  drivers/s390/crypto/vfio_ap_drv.c       |  134 +++++++++++++++++++++++++++++++
  drivers/s390/crypto/vfio_ap_private.h   |   20 +++++
  include/uapi/linux/vfio.h               |    2 +
  10 files changed, 182 insertions(+), 0 deletions(-)

create mode 100644 drivers/s390/crypto/vfio_ap_drv.c create mode 100644 drivers/s390/crypto/vfio_ap_private.h

MAINTAINERS +2 -0
index 4acf7c2..a2f232d 100644
@@ -11884,6 +11884,8 @@ W:	http://www.ibm.com/developerworks/linux/linux390/
  S:	Supported
  F:	arch/s390/include/asm/kvm/kvm-ap.h
  F:	arch/s390/kvm/kvm-ap.c
+F:	drivers/s390/crypto/vfio_ap_drv.c
+F:	drivers/s390/crypto/vfio_ap_private.h
 
  S390 ZFCP DRIVER
  M:	Steffen Maier <[email protected]>
arch/s390/Kconfig +0 -0
index cbe1d97..9f23caf 100644

arch/s390/Kconfig config VFIO_CCW · line 776

Quoted 6 lines
arch/s390/Kconfig +3 -0
@@ -771,6 +771,14 @@ config VFIO_CCW
  	  To compile this driver as a module, choose M here: the
  	  module will be called vfio_ccw.
 
+config VFIO_AP
+	def_tristate m
+	prompt "Support for virtual Adjunct Processor device interface"

The VFIO AP devices are not virtual.
What about
    "VFIO support for AP devices"

arch/s390/configs/default_defconfig

Quoted 12 lines
arch/s390/Kconfig +5 -0
+	depends on ZCRYPT && VFIO_MDEV_DEVICE
+	help
+		driver grants access to Adjunct Processor (AP) devices
+		via the VFIO mediated device interface.
+
  endmenu
 
  menu "Dump support"
arch/s390/configs/default_defconfig +0 -0
index 5af8458..40fa3f6 100644

Not sure that this file belongs to this patch

arch/s390/configs/default_defconfig CONFIG_APPLDATA_BASE=y · line 724

Quoted 7 lines
arch/s390/configs/default_defconfig +3 -0
@@ -719,3 +719,6 @@ CONFIG_APPLDATA_BASE=y
  CONFIG_KVM=m
  CONFIG_KVM_S390_UCONTROL=y
  CONFIG_VHOST_NET=m
+VFIO_MDEV=m
+VFIO_MDEV_DEVICE=m
+CONFIG_VFIO_AP=m

What is your goal when modifying this three files? Could you add a comment in the commit message?

Quoted 54 lines
arch/s390/configs/gcov_defconfig +3 -0
index d52eafe..377d40f 100644
@@ -659,3 +659,6 @@ CONFIG_APPLDATA_BASE=y
  CONFIG_KVM=m
  CONFIG_KVM_S390_UCONTROL=y
  CONFIG_VHOST_NET=m
+VFIO_MDEV=m
+VFIO_MDEV_DEVICE=m
+CONFIG_VFIO_AP=m
arch/s390/configs/performance_defconfig +3 -0
index 20ed149..f0c52df 100644
@@ -657,3 +657,6 @@ CONFIG_APPLDATA_BASE=y
  CONFIG_KVM=m
  CONFIG_KVM_S390_UCONTROL=y
  CONFIG_VHOST_NET=m
+VFIO_MDEV=m
+VFIO_MDEV_DEVICE=m
+CONFIG_VFIO_AP=m
arch/s390/defconfig +3 -0
index 46a3178..0996eb7 100644
@@ -239,3 +239,6 @@ CONFIG_CRC7=m
  # CONFIG_XZ_DEC_ARMTHUMB is not set
  # CONFIG_XZ_DEC_SPARC is not set
  CONFIG_CMM=m
+VFIO_MDEV=m
+VFIO_MDEV_DEVICE=m
+CONFIG_VFIO_AP=m
drivers/s390/crypto/Makefile +4 -0
index b59af54..48e466e 100644
@@ -15,3 +15,7 @@ obj-$(CONFIG_ZCRYPT) += zcrypt_pcixcc.o zcrypt_cex2a.o zcrypt_cex4.o
  # pkey kernel module
  pkey-objs := pkey_api.o
  obj-$(CONFIG_PKEY) += pkey.o
+
+# adjunct processor matrix
+vfio_ap-objs := vfio_ap_drv.o
+obj-$(CONFIG_VFIO_AP) += vfio_ap.o
drivers/s390/crypto/vfio_ap_drv.c new file +3 -0
new file mode 100644
index 0000000..8bb72af
@@ -0,0 +1,134 @@
+/*
+ * VFIO based AP device driver
+ *

drivers/s390/crypto/vfio_ap_drv.c line 13

Quoted 10 lines
drivers/s390/crypto/vfio_ap_drv.c new file +10 -0
+ * Copyright IBM Corp. 2017
+ *
+ * Author(s): Tony Krowiak <[email protected]>
+ */
+
+#include <linux/module.h>
+#include <linux/mod_devicetable.h>
+#include <linux/slab.h>
+
+#include "ap_bus.h"

Since you do it in a later patch, may be put this include of ap_bus.h in ap_private.h now.

Quoted 44 lines
drivers/s390/crypto/vfio_ap_drv.c new file +44 -0
+#include "vfio_ap_private.h"
+
+#define VFIO_AP_ROOT_NAME "vfio_ap"
+#define VFIO_AP_DEV_TYPE_NAME "ap_matrix"
+#define VFIO_AP_DEV_NAME "matrix"
+
+MODULE_AUTHOR("IBM Corporation");
+MODULE_DESCRIPTION("VFIO AP device driver, Copyright IBM Corp. 2017");
+MODULE_LICENSE("GPL v2");
+
+static struct device *vfio_ap_root_device;
+
+static struct ap_driver vfio_ap_drv;
+
+static struct ap_matrix *ap_matrix;
+
+static struct device_type vfio_ap_dev_type = {
+	.name = VFIO_AP_DEV_TYPE_NAME,
+};
+
+/* Only type 10 adapters (CEX4 and later) are supported
+ * by the AP matrix device driver
+ */
+static struct ap_device_id ap_queue_ids[] = {
+	{ .dev_type = AP_DEVICE_TYPE_CEX4,
+	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
+	{ .dev_type = AP_DEVICE_TYPE_CEX5,
+	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
+	{ .dev_type = AP_DEVICE_TYPE_CEX6,
+	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
+	{ /* end of sibling */ },
+};
+
+MODULE_DEVICE_TABLE(vfio_ap, ap_queue_ids);
+
+static int vfio_ap_queue_dev_probe(struct ap_device *apdev)
+{
+	return 0;
+}
+
+static void vfio_ap_matrix_dev_release(struct device *dev)
+{
+	struct ap_matrix *ap_matrix = dev_get_drvdata(dev);
+

drivers/s390/crypto/vfio_ap_drv.c line 67

Quoted 10 lines
drivers/s390/crypto/vfio_ap_drv.c new file +10 -0
+	kfree(ap_matrix);
+}
+
+static int vfio_ap_matrix_dev_create(void)
+{
+	int ret;
+
+	vfio_ap_root_device = root_device_register(VFIO_AP_ROOT_NAME);
+
+	ret = PTR_ERR_OR_ZERO(vfio_ap_root_device);

IS_ERR() is enough, root_device_register() never return NULL.

Quoted 113 lines
drivers/s390/crypto/vfio_ap_drv.c new file +67 -0
+	if (ret)
+		goto done;
+
+	ap_matrix = kzalloc(sizeof(*ap_matrix), GFP_KERNEL);
+	if (!ap_matrix) {
+		ret = -ENOMEM;
+		goto matrix_alloc_err;
+	}
+
+	ap_matrix->device.type = &vfio_ap_dev_type;
+	dev_set_name(&ap_matrix->device, "%s", VFIO_AP_DEV_NAME);
+	ap_matrix->device.parent = vfio_ap_root_device;
+	ap_matrix->device.release = vfio_ap_matrix_dev_release;
+	ap_matrix->device.driver = &vfio_ap_drv.driver;
+
+	ret = device_register(&ap_matrix->device);
+	if (ret)
+		goto matrix_reg_err;
+
+	goto done;
+
+matrix_reg_err:
+	put_device(&ap_matrix->device);
+	kfree(ap_matrix);
+
+matrix_alloc_err:
+	root_device_unregister(vfio_ap_root_device);
+
+done:
+	return ret;
+}
+
+static void vfio_ap_matrix_dev_destroy(struct ap_matrix *ap_matrix)
+{
+	device_unregister(&ap_matrix->device);
+	root_device_unregister(vfio_ap_root_device);
+}
+
+int __init vfio_ap_init(void)
+{
+	int ret;
+
+	ret = vfio_ap_matrix_dev_create();
+	if (ret)
+		return ret;
+
+	memset(&vfio_ap_drv, 0, sizeof(vfio_ap_drv));
+	vfio_ap_drv.probe = vfio_ap_queue_dev_probe;
+	vfio_ap_drv.ids = ap_queue_ids;
+
+	ret = ap_driver_register(&vfio_ap_drv, THIS_MODULE, VFIO_AP_DRV_NAME);
+	if (ret) {
+		vfio_ap_matrix_dev_destroy(ap_matrix);
+		return ret;
+	}
+
+	return 0;
+}
+
+void __exit vfio_ap_exit(void)
+{
+	ap_driver_unregister(&vfio_ap_drv);
+	vfio_ap_matrix_dev_destroy(ap_matrix);
+}
+
+module_init(vfio_ap_init);
+module_exit(vfio_ap_exit);
drivers/s390/crypto/vfio_ap_private.h new file +20 -0
new file mode 100644
index 0000000..3505947
@@ -0,0 +1,20 @@
+/*
+ * Private data and functions for adjunct processor VFIO matrix driver.
+ *
+ * Copyright IBM Corp. 2017
+ * Author(s): Tony Krowiak <[email protected]>
+ */
+
+#ifndef _VFIO_AP_PRIVATE_H_
+#define _VFIO_AP_PRIVATE_H_
+
+#include <linux/types.h>
+
+#define VFIO_AP_MODULE_NAME "vfio_ap"
+#define VFIO_AP_DRV_NAME "vfio_ap"
+
+struct ap_matrix {
+	struct device device;
+};
+
+#endif /* _VFIO_AP_PRIVATE_H_ */
include/uapi/linux/vfio.h +2 -0
index e3301db..cf2a5e9 100644
@@ -200,6 +200,7 @@ struct vfio_device_info {
  #define VFIO_DEVICE_FLAGS_PLATFORM (1 << 2)	/* vfio-platform device */
  #define VFIO_DEVICE_FLAGS_AMBA  (1 << 3)	/* vfio-amba device */
  #define VFIO_DEVICE_FLAGS_CCW	(1 << 4)	/* vfio-ccw device */
+#define VFIO_DEVICE_FLAGS_AP	(1 << 5)	/* vfio-ap device */
  	__u32	num_regions;	/* Max region index + 1 */
  	__u32	num_irqs;	/* Max IRQ index + 1 */
  };
@@ -215,6 +216,7 @@ struct vfio_device_info {
  #define VFIO_DEVICE_API_PLATFORM_STRING		"vfio-platform"
  #define VFIO_DEVICE_API_AMBA_STRING		"vfio-amba"
  #define VFIO_DEVICE_API_CCW_STRING		"vfio-ccw"
+#define VFIO_DEVICE_API_AP_STRING		"vfio-ap"
 
  /**
   * VFIO_DEVICE_GET_REGION_INFO - _IOWR(VFIO_TYPE, VFIO_BASE + 8,
Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#20 lore raw

Re: [PATCH v2 05/15] s390: vfio-ap: base implementation of VFIO AP device driver

Quoted 71 lines · Pierre Morel
Quoted 70 lines · Tony Krowiak

Introduces a new AP device driver. This device driver is built on the VFIO mediated device framework. The framework provides sysfs interfaces that facilitate passthrough access by guests to devices installed on the linux host.

The VFIO AP device driver will serve two purposes:

1. Provide the interfaces to reserve AP devices for exclusive use by KVM guests. This is accomplished by unbinding the devices to be reserved for guest usage from the default AP device driver and binding them to the VFIO AP device driver.

2. Implements the functions, callbacks and sysfs attribute interfaces required to create one or more VFIO mediated devices each of which will be used to configure the AP matrix for a guest and serve as a file descriptor for facilitating communication between QEMU and the VFIO AP device driver.

When the VFIO AP device driver is initialized:

* It registers with the AP bus for control of type 10 (CEX4 and newer) AP queue devices. The probe and remove callbacks will be provided to support the binding/unbinding of AP queue devices to/from the VFIO AP device driver.

* Creates a /sys/devices/vfio-ap/matrix device to hold the APQNs of the AP devices bound to the VFIO AP device driver and serves as the parent of the mediated devices created for each guest.

---
  MAINTAINERS                             |    2 +
  arch/s390/Kconfig                       |    8 ++
  arch/s390/configs/default_defconfig     |    3 +
  arch/s390/configs/gcov_defconfig        |    3 +
  arch/s390/configs/performance_defconfig |    3 +
  arch/s390/defconfig                     |    3 +
  drivers/s390/crypto/Makefile            |    4 +
  drivers/s390/crypto/vfio_ap_drv.c       |  134 
+++++++++++++++++++++++++++++++
  drivers/s390/crypto/vfio_ap_private.h   |   20 +++++
  include/uapi/linux/vfio.h               |    2 +
  10 files changed, 182 insertions(+), 0 deletions(-)

create mode 100644 drivers/s390/crypto/vfio_ap_drv.c create mode 100644 drivers/s390/crypto/vfio_ap_private.h

MAINTAINERS +0 -0
index 4acf7c2..a2f232d 100644
@@ -11884,6 +11884,8 @@ W: 

http://www.ibm.com/developerworks/linux/linux390/

  S:    Supported
  F:    arch/s390/include/asm/kvm/kvm-ap.h
  F:    arch/s390/kvm/kvm-ap.c
MAINTAINERS +2 -0
+F:    drivers/s390/crypto/vfio_ap_drv.c
+F:    drivers/s390/crypto/vfio_ap_private.h
 
  S390 ZFCP DRIVER
  M:    Steffen Maier <[email protected]>
arch/s390/Kconfig +0 -0
index cbe1d97..9f23caf 100644
@@ -771,6 +771,14 @@ config VFIO_CCW
        To compile this driver as a module, choose M here: the
        module will be called vfio_ccw.
 
Quoted 7 lines

arch/s390/Kconfig config VFIO_CCW · line 776

Quoted 3 lines
arch/s390/Kconfig +3 -0
+config VFIO_AP
+    def_tristate m
+    prompt "Support for virtual Adjunct Processor device interface"

The VFIO AP devices are not virtual.
What about
"VFIO support for AP devices"

Sounds good.

Quoted 8 lines
Quoted 8 lines
arch/s390/Kconfig +5 -0
+    depends on ZCRYPT && VFIO_MDEV_DEVICE
+    help
+        driver grants access to Adjunct Processor (AP) devices
+        via the VFIO mediated device interface.
+
  endmenu
 
Quoted 7 lines

arch/s390/configs/default_defconfig

Quoted 5 lines
arch/s390/Kconfig +0 -0
  menu "Dump support"
arch/s390/configs/default_defconfig +0 -0
index 5af8458..40fa3f6 100644

Not sure that this file belongs to this patch

Neither am I, but at the time I inserted this here - well before August of last year - I was using vfio-ccw as a model. If someone can verify this does not belong here, I'd be more than happy to remove it.

Quoted 12 lines

arch/s390/configs/default_defconfig CONFIG_APPLDATA_BASE=y · line 724

Quoted 9 lines
arch/s390/configs/default_defconfig +3 -0
@@ -719,3 +719,6 @@ CONFIG_APPLDATA_BASE=y
  CONFIG_KVM=m
  CONFIG_KVM_S390_UCONTROL=y
  CONFIG_VHOST_NET=m
+VFIO_MDEV=m
+VFIO_MDEV_DEVICE=m
+CONFIG_VFIO_AP=m

What is your goal when modifying this three files? Could you add a comment in the commit message?

As stated above, this was originally based on the vfio-ccw model and has been in the patch series since its inception. I'd be happy to remove it if it is not necessary.

Quoted 62 lines
Quoted 62 lines
arch/s390/configs/gcov_defconfig +3 -0
index d52eafe..377d40f 100644
@@ -659,3 +659,6 @@ CONFIG_APPLDATA_BASE=y
  CONFIG_KVM=m
  CONFIG_KVM_S390_UCONTROL=y
  CONFIG_VHOST_NET=m
+VFIO_MDEV=m
+VFIO_MDEV_DEVICE=m
+CONFIG_VFIO_AP=m
arch/s390/configs/performance_defconfig +3 -0
index 20ed149..f0c52df 100644
@@ -657,3 +657,6 @@ CONFIG_APPLDATA_BASE=y
  CONFIG_KVM=m
  CONFIG_KVM_S390_UCONTROL=y
  CONFIG_VHOST_NET=m
+VFIO_MDEV=m
+VFIO_MDEV_DEVICE=m
+CONFIG_VFIO_AP=m
arch/s390/defconfig +3 -0
index 46a3178..0996eb7 100644
@@ -239,3 +239,6 @@ CONFIG_CRC7=m
  # CONFIG_XZ_DEC_ARMTHUMB is not set
  # CONFIG_XZ_DEC_SPARC is not set
  CONFIG_CMM=m
+VFIO_MDEV=m
+VFIO_MDEV_DEVICE=m
+CONFIG_VFIO_AP=m
drivers/s390/crypto/Makefile +0 -0
index b59af54..48e466e 100644
@@ -15,3 +15,7 @@ obj-$(CONFIG_ZCRYPT) += zcrypt_pcixcc.o 

zcrypt_cex2a.o zcrypt_cex4.o

  # pkey kernel module
  pkey-objs := pkey_api.o
  obj-$(CONFIG_PKEY) += pkey.o
drivers/s390/crypto/Makefile +4 -0
+
+# adjunct processor matrix
+vfio_ap-objs := vfio_ap_drv.o
+obj-$(CONFIG_VFIO_AP) += vfio_ap.o
drivers/s390/crypto/vfio_ap_drv.c new file +7 -0
new file mode 100644
index 0000000..8bb72af
@@ -0,0 +1,134 @@
+/*
+ * VFIO based AP device driver
+ *
+ * Copyright IBM Corp. 2017
+ *
+ * Author(s): Tony Krowiak <[email protected]>
+ */
Quoted 8 lines

drivers/s390/crypto/vfio_ap_drv.c line 13

Quoted 6 lines
drivers/s390/crypto/vfio_ap_drv.c new file +6 -0
+
+#include <linux/module.h>
+#include <linux/mod_devicetable.h>
+#include <linux/slab.h>
+
+#include "ap_bus.h"

Since you do it in a later patch, may be put this include of ap_bus.h in ap_private.h now.

Okay

Quoted 50 lines
Quoted 50 lines
drivers/s390/crypto/vfio_ap_drv.c new file +48 -0
+#include "vfio_ap_private.h"
+
+#define VFIO_AP_ROOT_NAME "vfio_ap"
+#define VFIO_AP_DEV_TYPE_NAME "ap_matrix"
+#define VFIO_AP_DEV_NAME "matrix"
+
+MODULE_AUTHOR("IBM Corporation");
+MODULE_DESCRIPTION("VFIO AP device driver, Copyright IBM Corp. 2017");
+MODULE_LICENSE("GPL v2");
+
+static struct device *vfio_ap_root_device;
+
+static struct ap_driver vfio_ap_drv;
+
+static struct ap_matrix *ap_matrix;
+
+static struct device_type vfio_ap_dev_type = {
+    .name = VFIO_AP_DEV_TYPE_NAME,
+};
+
+/* Only type 10 adapters (CEX4 and later) are supported
+ * by the AP matrix device driver
+ */
+static struct ap_device_id ap_queue_ids[] = {
+    { .dev_type = AP_DEVICE_TYPE_CEX4,
+      .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
+    { .dev_type = AP_DEVICE_TYPE_CEX5,
+      .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
+    { .dev_type = AP_DEVICE_TYPE_CEX6,
+      .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
+    { /* end of sibling */ },
+};
+
+MODULE_DEVICE_TABLE(vfio_ap, ap_queue_ids);
+
+static int vfio_ap_queue_dev_probe(struct ap_device *apdev)
+{
+    return 0;
+}
+
+static void vfio_ap_matrix_dev_release(struct device *dev)
+{
+    struct ap_matrix *ap_matrix = dev_get_drvdata(dev);
+
+    kfree(ap_matrix);
+}
+
+static int vfio_ap_matrix_dev_create(void)
Quoted 8 lines

drivers/s390/crypto/vfio_ap_drv.c line 67

Quoted 6 lines
drivers/s390/crypto/vfio_ap_drv.c new file +6 -0
+{
+    int ret;
+
+    vfio_ap_root_device = root_device_register(VFIO_AP_ROOT_NAME);
+
+    ret = PTR_ERR_OR_ZERO(vfio_ap_root_device);

IS_ERR() is enough, root_device_register() never return NULL.

I searched the kernel code to look at other places the root_device_register() function is called to see how the return value is handled. I've seen all of the following used:
if (IS_ERR())

    ret = PTR_ERR()

PTR_ERR()
PTR_ERR_OR_ZERO()

I'm not sure why this is a concern, but I'll use the first option above since PTR_ERR_OR_ZERO() also embeds the first option.

Quoted 117 lines
Quoted 115 lines
drivers/s390/crypto/vfio_ap_drv.c new file +51 -0
+    if (ret)
+        goto done;
+
+    ap_matrix = kzalloc(sizeof(*ap_matrix), GFP_KERNEL);
+    if (!ap_matrix) {
+        ret = -ENOMEM;
+        goto matrix_alloc_err;
+    }
+
+    ap_matrix->device.type = &vfio_ap_dev_type;
+    dev_set_name(&ap_matrix->device, "%s", VFIO_AP_DEV_NAME);
+    ap_matrix->device.parent = vfio_ap_root_device;
+    ap_matrix->device.release = vfio_ap_matrix_dev_release;
+    ap_matrix->device.driver = &vfio_ap_drv.driver;
+
+    ret = device_register(&ap_matrix->device);
+    if (ret)
+        goto matrix_reg_err;
+
+    goto done;
+
+matrix_reg_err:
+    put_device(&ap_matrix->device);
+    kfree(ap_matrix);
+
+matrix_alloc_err:
+    root_device_unregister(vfio_ap_root_device);
+
+done:
+    return ret;
+}
+
+static void vfio_ap_matrix_dev_destroy(struct ap_matrix *ap_matrix)
+{
+    device_unregister(&ap_matrix->device);
+    root_device_unregister(vfio_ap_root_device);
+}
+
+int __init vfio_ap_init(void)
+{
+    int ret;
+
+    ret = vfio_ap_matrix_dev_create();
+    if (ret)
+        return ret;
+
+    memset(&vfio_ap_drv, 0, sizeof(vfio_ap_drv));
+    vfio_ap_drv.probe = vfio_ap_queue_dev_probe;
+    vfio_ap_drv.ids = ap_queue_ids;
+
+    ret = ap_driver_register(&vfio_ap_drv, THIS_MODULE, 

VFIO_AP_DRV_NAME);

drivers/s390/crypto/vfio_ap_drv.c new file +16 -0
+    if (ret) {
+        vfio_ap_matrix_dev_destroy(ap_matrix);
+        return ret;
+    }
+
+    return 0;
+}
+
+void __exit vfio_ap_exit(void)
+{
+    ap_driver_unregister(&vfio_ap_drv);
+    vfio_ap_matrix_dev_destroy(ap_matrix);
+}
+
+module_init(vfio_ap_init);
+module_exit(vfio_ap_exit);
drivers/s390/crypto/vfio_ap_private.h new file +20 -0
new file mode 100644
index 0000000..3505947
@@ -0,0 +1,20 @@
+/*
+ * Private data and functions for adjunct processor VFIO matrix driver.
+ *
+ * Copyright IBM Corp. 2017
+ * Author(s): Tony Krowiak <[email protected]>
+ */
+
+#ifndef _VFIO_AP_PRIVATE_H_
+#define _VFIO_AP_PRIVATE_H_
+
+#include <linux/types.h>
+
+#define VFIO_AP_MODULE_NAME "vfio_ap"
+#define VFIO_AP_DRV_NAME "vfio_ap"
+
+struct ap_matrix {
+    struct device device;
+};
+
+#endif /* _VFIO_AP_PRIVATE_H_ */
include/uapi/linux/vfio.h +0 -0
index e3301db..cf2a5e9 100644
@@ -200,6 +200,7 @@ struct vfio_device_info {
  #define VFIO_DEVICE_FLAGS_PLATFORM (1 << 2)    /* vfio-platform 

device */
#define VFIO_DEVICE_FLAGS_AMBA (1 << 3) /* vfio-amba device */
#define VFIO_DEVICE_FLAGS_CCW (1 << 4) /* vfio-ccw device */

include/uapi/linux/vfio.h +2 -0
+#define VFIO_DEVICE_FLAGS_AP    (1 << 5)    /* vfio-ap device */
      __u32    num_regions;    /* Max region index + 1 */
      __u32    num_irqs;    /* Max IRQ index + 1 */
  };
@@ -215,6 +216,7 @@ struct vfio_device_info {
  #define VFIO_DEVICE_API_PLATFORM_STRING        "vfio-platform"
  #define VFIO_DEVICE_API_AMBA_STRING        "vfio-amba"
  #define VFIO_DEVICE_API_CCW_STRING        "vfio-ccw"
+#define VFIO_DEVICE_API_AP_STRING        "vfio-ap"
 
  /**
   * VFIO_DEVICE_GET_REGION_INFO - _IOWR(VFIO_TYPE, VFIO_BASE + 8,
#21 lore raw

Re: [PATCH v2 05/15] s390: vfio-ap: base implementation of VFIO AP device driver

Quoted 2 lines · On Wed, 28 Feb 2018 11:43:37 -0500 Tony Krowiak <[email protected]>…
Quoted 1 line · Pierre Morel

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

(...)

Quoted 10 lines
Quoted 10 lines

arch/s390/Kconfig config VFIO_CCW · line 775

Quoted 10 lines
arch/s390/Kconfig +2 -0
index cbe1d97..9f23caf 100644
@@ -771,6 +771,14 @@ config VFIO_CCW
        To compile this driver as a module, choose M here: the
        module will be called vfio_ccw.
 
+config VFIO_AP
+    def_tristate m

Any reason you default to m instead of n here?

Quoted 6 lines
Quoted 5 lines

arch/s390/Kconfig config VFIO_CCW · line 776

Quoted 1 line
arch/s390/Kconfig +1 -0
+    prompt "Support for virtual Adjunct Processor device interface"  

The VFIO AP devices are not virtual.
What about
"VFIO support for AP devices"

Sounds good.

+1

Quoted 4 lines
Quoted 4 lines

arch/s390/Kconfig config VFIO_CCW · line 779

Quoted 4 lines
arch/s390/Kconfig +3 -0
+    depends on ZCRYPT && VFIO_MDEV_DEVICE
+    help
+        driver grants access to Adjunct Processor (AP) devices

s/driver/This driver/

Quoted 1 line
Quoted 1 line

arch/s390/Kconfig config VFIO_CCW · line 780

Quoted 1 line
arch/s390/Kconfig +1 -0
+        via the VFIO mediated device interface.

You also might want to add

"To compile this driver as a module, choose M here: the module will be called..."

Quoted 2 lines
Quoted 2 lines

arch/s390/Kconfig config VFIO_CCW · line 782

Quoted 2 lines
arch/s390/Kconfig +1 -0
+
  endmenu

It's a tad confusing to find this in the I/O submenu, but I don't really have a better idea.

Quoted 9 lines
Quoted 9 lines

arch/s390/configs/default_defconfig

Quoted 7 lines
arch/s390/Kconfig +0 -0
  menu "Dump support"
arch/s390/configs/default_defconfig +0 -0
index 5af8458..40fa3f6 100644

Not sure that this file belongs to this patch

Quoted 4 lines

Neither am I, but at the time I inserted this here - well before August of last year - I was using vfio-ccw as a model.
If someone can verify this does not belong here, I'd be more than happy to remove it.

I don't see any entry for VFIO_CCW in there?

Quoted 12 lines
Quoted 12 lines

arch/s390/configs/default_defconfig CONFIG_APPLDATA_BASE=y · line 724

Quoted 9 lines
arch/s390/configs/default_defconfig +3 -0
@@ -719,3 +719,6 @@ CONFIG_APPLDATA_BASE=y
  CONFIG_KVM=m
  CONFIG_KVM_S390_UCONTROL=y
  CONFIG_VHOST_NET=m
+VFIO_MDEV=m
+VFIO_MDEV_DEVICE=m
+CONFIG_VFIO_AP=m  

What is your goal when modifying this three files? Could you add a comment in the commit message?

Quoted 4 lines

As stated above, this was originally based on the vfio-ccw model and has been in the
patch series since its inception. I'd be happy to remove it if it is not necessary.

I'd vote for removing it.

(...)

Quoted 15 lines
Quoted 9 lines

arch/s390/configs/default_defconfig

Quoted 7 lines
arch/s390/configs/default_defconfig +7 -0
+static int vfio_ap_matrix_dev_create(void)
+{
+    int ret;
+
+    vfio_ap_root_device = root_device_register(VFIO_AP_ROOT_NAME);
+
+    ret = PTR_ERR_OR_ZERO(vfio_ap_root_device);  

IS_ERR() is enough, root_device_register() never return NULL.

I searched the kernel code to look at other places the root_device_register()
function is called to see how the return value is handled. I've seen all of the
following used:
if (IS_ERR())

Quoted 6 lines
     ret = PTR_ERR()

PTR_ERR()
PTR_ERR_OR_ZERO()

I'm not sure why this is a concern, but I'll use the first option above since PTR_ERR_OR_ZERO() also embeds the first option.

PTR_ERR_OR_ZERO() seems like the best choice for the way the return code is processed here. (It's just unfortunate that its name conjures up connotations of NULL-pointer handling.)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
arch/s390/configs/default_defconfig +2 -0
+    if (ret)
+        goto done;
#22 lore raw

Re: [PATCH v2 05/15] s390: vfio-ap: base implementation of VFIO AP device driver

Quoted 15 lines · Cornelia Huck
Quoted 2 lines · On Wed, 28 Feb 2018 11:43:37 -0500 Tony Krowiak <[email protected]>…
Quoted 1 line · Pierre Morel

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

(...)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
arch/s390/Kconfig +0 -0
index cbe1d97..9f23caf 100644
@@ -771,6 +771,14 @@ config VFIO_CCW
         To compile this driver as a module, choose M here: the
         module will be called vfio_ccw.
 
Quoted 3 lines
Quoted 2 lines
Quoted 2 lines

arch/s390/Kconfig config VFIO_CCW · line 775

Quoted 2 lines
arch/s390/Kconfig +2 -0
+config VFIO_AP
+    def_tristate m

Any reason you default to m instead of n here?

None in particular, is there a good reason to change this to n?

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

arch/s390/Kconfig config VFIO_CCW · line 776

Quoted 2 lines
arch/s390/Kconfig +1 -0
+    prompt "Support for virtual Adjunct Processor device interface"

The VFIO AP devices are not virtual.
What about
"VFIO support for AP devices"

Quoted 8 lines
Quoted 1 line

Sounds good.

+1

Quoted 4 lines
Quoted 4 lines

arch/s390/Kconfig config VFIO_CCW · line 779

Quoted 4 lines
arch/s390/Kconfig +3 -0
+    depends on ZCRYPT && VFIO_MDEV_DEVICE
+    help
+        driver grants access to Adjunct Processor (AP) devices

s/driver/This driver/

Okay, I'll make the change

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

arch/s390/Kconfig config VFIO_CCW · line 780

Quoted 2 lines
arch/s390/Kconfig +1 -0
+        via the VFIO mediated device interface.

You also might want to add

"To compile this driver as a module, choose M here: the module will be called..."

Will do

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

arch/s390/Kconfig config VFIO_CCW · line 782

Quoted 3 lines
arch/s390/Kconfig +1 -0
+
   endmenu

It's a tad confusing to find this in the I/O submenu, but I don't really have a better idea.

I wasn't sure either, but couldn't think of a better location. Anybody else have any ideas?

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

arch/s390/configs/default_defconfig

Quoted 7 lines
arch/s390/Kconfig +0 -0
   menu "Dump support"
arch/s390/configs/default_defconfig +0 -0
index 5af8458..40fa3f6 100644

Not sure that this file belongs to this patch

Quoted 5 lines
Quoted 4 lines

Neither am I, but at the time I inserted this here - well before August of last year - I was using vfio-ccw as a model.
If someone can verify this does not belong here, I'd be more than happy to remove it.

I don't see any entry for VFIO_CCW in there?

There isn't now, but there was at the time I first started working on this. My first pass was a lot of cut-and-paste followed by modification of the vfio_ccw implementation, but that was long ago. This is a remnant of that time. Like I said, I'd be happy to remove this.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

arch/s390/configs/default_defconfig CONFIG_APPLDATA_BASE=y · line 724

Quoted 9 lines
arch/s390/configs/default_defconfig +3 -0
@@ -719,3 +719,6 @@ CONFIG_APPLDATA_BASE=y
   CONFIG_KVM=m
   CONFIG_KVM_S390_UCONTROL=y
   CONFIG_VHOST_NET=m
+VFIO_MDEV=m
+VFIO_MDEV_DEVICE=m
+CONFIG_VFIO_AP=m

What is your goal when modifying this three files? Could you add a comment in the commit message?

Quoted 5 lines
Quoted 4 lines

As stated above, this was originally based on the vfio-ccw model and has been in the
patch series since its inception. I'd be happy to remove it if it is not necessary.

I'd vote for removing it.

Consider them gone.

Quoted 21 lines

(...)

Quoted 18 lines
Quoted 8 lines

arch/s390/configs/default_defconfig

Quoted 7 lines
arch/s390/configs/default_defconfig +7 -0
+static int vfio_ap_matrix_dev_create(void)
+{
+    int ret;
+
+    vfio_ap_root_device = root_device_register(VFIO_AP_ROOT_NAME);
+
+    ret = PTR_ERR_OR_ZERO(vfio_ap_root_device);

IS_ERR() is enough, root_device_register() never return NULL.

I searched the kernel code to look at other places the root_device_register()
function is called to see how the return value is handled. I've seen all of the
following used:
if (IS_ERR())

      ret = PTR_ERR()

PTR_ERR()
PTR_ERR_OR_ZERO()

Quoted 5 lines
Quoted 2 lines

I'm not sure why this is a concern, but I'll use the first option above since PTR_ERR_OR_ZERO() also embeds the first option.

PTR_ERR_OR_ZERO() seems like the best choice for the way the return code is processed here. (It's just unfortunate that its name conjures up connotations of NULL-pointer handling.)

I changed it to:

    ret = IS_ERR(vfio_ap_root_device);
    if (ret) {
        ret = PTR_ERR(vfio_ap_root_device);
        goto done;
    }

Hopefully everybody is happy with this.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
arch/s390/configs/default_defconfig +2 -0
+    if (ret)
+        goto done;
#23 lore raw

PATCH v2 6/15 s390: vfio-ap: register matrix device with VFIO mdev framework

In reply to Tony Krowiak

Registers the matrix device created by the VFIO AP device driver with the VFIO mediated device framework. Registering the matrix device will create the sysfs structures needed to create mediated matrix devices each of which will be used to configure the AP matrix for a guest and connect it to the VFIO AP device driver.

Registering the matrix device with the VFIO mediated device framework will create the following sysfs structures:

/sys/devices/vfio_ap
... [matrix]
...... [mdev_supported_types]
......... [vfio_ap-passthrough]
............ create

To create a mediated device for the AP matrix device, write a UUID to the create file:

uuidgen > create

A symbolic link to the mediated device's directory will be created in the devices subdirectory named after the generated $uuid:

/sys/devices/vfio_ap
... [matrix]
...... [mdev_supported_types]
......... [vfio_ap-passthrough]
............ [devices]
............... [$uuid]

---
 MAINTAINERS                           |    1 +
 drivers/s390/crypto/Makefile          |    2 +-
 drivers/s390/crypto/vfio_ap_drv.c     |    9 +++
 drivers/s390/crypto/vfio_ap_ops.c     |  105 +++++++++++++++++++++++++++++++++
 drivers/s390/crypto/vfio_ap_private.h |   18 ++++++
 5 files changed, 134 insertions(+), 1 deletions(-)
 create mode 100644 drivers/s390/crypto/vfio_ap_ops.c
MAINTAINERS +1 -0
index a2f232d..fb9e0d4 100644
@@ -11886,6 +11886,7 @@ F:	arch/s390/include/asm/kvm/kvm-ap.h
 F:	arch/s390/kvm/kvm-ap.c
 F:	drivers/s390/crypto/vfio_ap_drv.c
 F:	drivers/s390/crypto/vfio_ap_private.h
+F:	drivers/s390/crypto/vfio_ap_ops.c
 
 S390 ZFCP DRIVER
 M:	Steffen Maier <[email protected]>
drivers/s390/crypto/Makefile +1 -1
index 48e466e..8d36b05 100644
@@ -17,5 +17,5 @@ pkey-objs := pkey_api.o
 obj-$(CONFIG_PKEY) += pkey.o
 
 # adjunct processor matrix
-vfio_ap-objs := vfio_ap_drv.o
+vfio_ap-objs := vfio_ap_drv.o vfio_ap_ops.o
 obj-$(CONFIG_VFIO_AP) += vfio_ap.o
drivers/s390/crypto/vfio_ap_drv.c +9 -0
index 8bb72af..f45f83b 100644
@@ -121,11 +121,20 @@ int __init vfio_ap_init(void)
 		return ret;
 	}
 
+	ret = vfio_ap_mdev_register(ap_matrix);
+	if (ret) {
+		ap_driver_unregister(&vfio_ap_drv);
+		vfio_ap_matrix_dev_destroy(ap_matrix);
+
+		return ret;
+	}
+
 	return 0;
 }
 
 void __exit vfio_ap_exit(void)
 {
+	vfio_ap_mdev_unregister(ap_matrix);
 	ap_driver_unregister(&vfio_ap_drv);
 	vfio_ap_matrix_dev_destroy(ap_matrix);
 }
drivers/s390/crypto/vfio_ap_ops.c new file +105 -0

105 lines not on this page · raw message

drivers/s390/crypto/vfio_ap_private.h +18 -0
index 3505947..c264415 100644
@@ -9,12 +9,30 @@
 #define _VFIO_AP_PRIVATE_H_
 
 #include <linux/types.h>
+#include <linux/mdev.h>
+#include "ap_bus.h"
 
 #define VFIO_AP_MODULE_NAME "vfio_ap"
 #define VFIO_AP_DRV_NAME "vfio_ap"
+#define VFIO_AP_MATRIX_MODULE_NAME "vfio_ap_matrix"
+/**
+ * There must be one mediated matrix device per guest. If every APQN is assigned
+ * to a guest, then the maximum number of guests with a unique APQN assigned
+ * would be 255 adapters x 255 domains = 72351 guests.
+ */
+#define AP_MATRIX_MAX_AVAILABLE_INSTANCES 72351
 
 struct ap_matrix {
 	struct device device;
+	int available_instances;
 };
 
+static inline struct ap_matrix *to_ap_matrix(struct device *dev)
+{
+	return container_of(dev, struct ap_matrix, device);
+}
+
+extern int vfio_ap_mdev_register(struct ap_matrix *ap_matrix);
+extern void vfio_ap_mdev_unregister(struct ap_matrix *ap_matrix);
+
 #endif /* _VFIO_AP_PRIVATE_H_ */
Signature · 1 line
1.7.1
#24 lore raw

PATCH v2 7/15 KVM: s390: Interfaces to configure/deconfigure guest's AP matrix

In reply to Tony Krowiak

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.

---
 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(-)
arch/s390/include/asm/kvm-ap.h +36 -0
index ef749e7..46e7c5b 100644
@@ -10,9 +10,45 @@
 #define _ASM_KVM_AP
 #include <linux/types.h>
 #include <linux/kvm_host.h>
+#include <linux/types.h>
+#include <linux/kvm_host.h>
+#include <linux/bitops.h>
+
+#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 */
arch/s390/kvm/kvm-ap.c +257 -0

272 lines not on this page · raw message

drivers/s390/crypto/vfio_ap_ops.c +19 -0
index 4292a5e..4fda44e 100644
@@ -10,6 +10,7 @@
 #include <linux/device.h>
 #include <linux/list.h>
 #include <linux/ctype.h>
+#include <asm/kvm-ap.h>
 
 #include "vfio_ap_private.h"
 
@@ -18,8 +19,23 @@
 
 static int vfio_ap_mdev_create(struct kobject *kobj, struct mdev_device *mdev)
 {
+	int ret;
+	struct ap_matrix_mdev *matrix_mdev;
 	struct ap_matrix *ap_matrix = to_ap_matrix(mdev_parent_dev(mdev));
+	struct kvm_ap_matrix *matrix;
+
+	ret = kvm_ap_matrix_create(&matrix);
+	if (ret)
+		return ret;
+
+	matrix_mdev = kzalloc(sizeof(*matrix_mdev), GFP_KERNEL);
+	if (!matrix_mdev) {
+		kvm_ap_matrix_destroy(matrix);
+		return -ENOMEM;
+	}
 
+	matrix_mdev->matrix = matrix;
+	mdev_set_drvdata(mdev, matrix_mdev);
 	ap_matrix->available_instances--;
 
 	return 0;
@@ -28,7 +44,10 @@ static int vfio_ap_mdev_create(struct kobject *kobj, struct mdev_device *mdev)
 static int vfio_ap_mdev_remove(struct mdev_device *mdev)
 {
 	struct ap_matrix *ap_matrix = to_ap_matrix(mdev_parent_dev(mdev));
+	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
 
+	kvm_ap_matrix_destroy(matrix_mdev->matrix);
+	kfree(matrix_mdev);
 	ap_matrix->available_instances++;
 
 	return 0;
drivers/s390/crypto/vfio_ap_private.h +4 -0
index c264415..522564e 100644
@@ -27,6 +27,10 @@ struct ap_matrix {
 	int available_instances;
 };
 
+struct ap_matrix_mdev {
+	struct kvm_ap_matrix *matrix;
+};
+
 static inline struct ap_matrix *to_ap_matrix(struct device *dev)
 {
 	return container_of(dev, struct ap_matrix, device);
Signature · 1 line
1.7.1
#25 lore raw

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

Quoted 26 lines · Tony Krowiak

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...

arch/s390/kvm/kvm-ap.c void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd) · line 177

Quoted 12 lines
  static int kvm_ap_apxa_installed(void)
  {
  	int ret;
arch/s390/kvm/kvm-ap.c +5 -0
@@ -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?

Quoted 5 lines
arch/s390/kvm/kvm-ap.c +5 -0
+	else
+		ap_matrix->apm_max = 64;
+
+	ap_matrix->apm = kzalloc(KVM_AP_MASK_BYTES(ap_matrix->apm_max),
+				 GFP_KERNEL);

arch/s390/kvm/kvm-ap.c void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd) · line 192

Quoted 10 lines
arch/s390/kvm/kvm-ap.c +10 -0
+	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

Quoted 5 lines
arch/s390/kvm/kvm-ap.c +5 -0
+	else
+		ap_matrix->aqm_max = 16;
+
+	ap_matrix->aqm = kzalloc(KVM_AP_MASK_BYTES(ap_matrix->aqm_max),
+				 GFP_KERNEL);

arch/s390/kvm/kvm-ap.c void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd) · line 207

Quoted 10 lines
arch/s390/kvm/kvm-ap.c +10 -0
+	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

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

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

Quoted 40 lines · Pierre Morel
Quoted 26 lines · Tony Krowiak

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...

Quoted 10 lines
  static int kvm_ap_apxa_installed(void)
  {
      int ret;
arch/s390/kvm/kvm-ap.c +2 -0
@@ -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, 
Quoted 8 lines

arch/s390/kvm/kvm-ap.c void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd) · line 177

Quoted 4 lines

int apxa)

arch/s390/kvm/kvm-ap.c +3 -0
+{
+    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.

Quoted 12 lines
Quoted 12 lines
arch/s390/kvm/kvm-ap.c +10 -0
+    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;
+}
Quoted 8 lines

arch/s390/kvm/kvm-ap.c void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd) · line 192

Quoted 6 lines
(quoted diff) +2 -0
+
+static int kvm_ap_matrix_aqm_create(struct kvm_ap_matrix *ap_matrix, 

int apxa)

arch/s390/kvm/kvm-ap.c +3 -0
+{
+    if (apxa)
+        ap_matrix->aqm_max = 256;

same here

ditto

Quoted 11 lines
Quoted 11 lines
arch/s390/kvm/kvm-ap.c +10 -0
+    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;
+}
Quoted 8 lines

arch/s390/kvm/kvm-ap.c void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd) · line 207

Quoted 6 lines
(quoted diff) +2 -0
+
+static int kvm_ap_matrix_adm_create(struct kvm_ap_matrix *ap_matrix, 

int apxa)

arch/s390/kvm/kvm-ap.c +3 -0
+{
+    if (apxa)
+        ap_matrix->adm_max = 256;

and here

ditto

Quoted 4 lines

Pierre

#27 lore raw

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

In reply to Tony Krowiak
Quoted 56 lines · Tony Krowiak

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.

---
  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(-)
arch/s390/include/asm/kvm-ap.h +0 -0
index ef749e7..46e7c5b 100644

arch/s390/include/asm/kvm-ap.h line 17

Quoted 8 lines
arch/s390/include/asm/kvm-ap.h +5 -0
@@ -10,9 +10,45 @@
  #define _ASM_KVM_AP
  #include <linux/types.h>
  #include <linux/kvm_host.h>
+#include <linux/types.h>
+#include <linux/kvm_host.h>
+#include <linux/bitops.h>
+
+#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.

Quoted 104 lines
arch/s390/include/asm/kvm-ap.h +31 -0
+
+/**
+ * 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 */
arch/s390/kvm/kvm-ap.c +52 -0
index bafe63b..bb29045 100644
@@ -8,6 +8,7 @@
 
  #include <asm/kvm-ap.h>
  #include <asm/ap.h>
+#include <linux/bitops.h>
 
  #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(&kvm->arch.crypto.crycb->apcb1, 0,
+		       sizeof(kvm->arch.crypto.crycb->apcb1));
+	else
+		memset(&kvm->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 *kvm_ap_get_crycb_aqm(struct kvm *kvm)
+{
+	unsigned long *aqm;
+	int crycb_fmt = kvm_ap_get_crycb_format(kvm);
+
+	if (crycb_fmt == CRYCB_FORMAT2)
+		aqm = (unsigned long *)kvm->arch.crypto.crycb->apcb1.aqm;
+	else
+		aqm = (unsigned long *)kvm->arch.crypto.crycb->apcb0.aqm;
+
+	return aqm;
+}
+
+static inline unsigned long *kvm_ap_get_crycb_adm(struct kvm *kvm)
+{
+	unsigned long *adm;
+	int crycb_fmt = kvm_ap_get_crycb_format(kvm);
+
+	if (crycb_fmt == CRYCB_FORMAT2)
+		adm = (unsigned long *)kvm->arch.crypto.crycb->apcb1.adm;
+	else
+		adm = (unsigned long *)kvm->arch.crypto.crycb->apcb0.adm;
+
+	return adm;
+}
+

arch/s390/kvm/kvm-ap.c int kvm_ap_get_crycb_format(struct kvm *kvm) · line 80

Quoted 10 lines
arch/s390/kvm/kvm-ap.c +10 -0
+static void kvm_ap_set_crycb_masks(struct kvm *kvm,
+				   struct kvm_ap_matrix *matrix)
+{
+	unsigned long *apm = kvm_ap_get_crycb_apm(kvm);
+	unsigned long *aqm = kvm_ap_get_crycb_aqm(kvm);
+	unsigned long *adm = kvm_ap_get_crycb_adm(kvm);
+
+	kvm_ap_clear_crycb_masks(kvm);
+	memcpy(apm, matrix->apm, KVM_AP_MASK_BYTES(matrix->apm_max));
+	memcpy(aqm, matrix->aqm, KVM_AP_MASK_BYTES(matrix->aqm_max));

Replace KVM_AP_MASK_BYTES macro with BITS_TO_BYTES macro

Quoted 65 lines
arch/s390/kvm/kvm-ap.c +58 -0
+
+	/*
+	 * Merge the AQM and ADM since the ADM is a superset of the
+	 * AQM by architectural convention.
+	 */
+	bitmap_or(adm, adm, aqm, matrix->adm_max);
+}
+
+static void kvm_ap_log_sharing_err(struct kvm *kvm, unsigned long apid,
+				   unsigned long apqi)
+{
+	pr_err("%s: AP queue %02lx.%04lx is registered to guest %s", __func__,
+	       apid, apqi, kvm->arch.dbf->name);
+}
+
+/**
+ * kvm_ap_validate_queue_sharing
+ *
+ * Verifies that the APQNs derived from the intersection of the AP adapter IDs
+ * and AP queue indexes comprising the AP matrix are not configured for
+ * another guest. AP queue sharing is not allowed.
+ *
+ * @kvm: the KVM guest
+ * @matrix: the AP matrix
+ *
+ * Returns 0 if the APQNs are valid, otherwise; returns -EBUSY.
+ */
+static int kvm_ap_validate_queue_sharing(struct kvm *kvm,
+					 struct kvm_ap_matrix *matrix)
+{
+	struct kvm *vm;
+	unsigned long *apm, *aqm;
+	unsigned long apid, apqi;
+
+
+	/* No other VM may share an AP Queue with the input VM */
+	list_for_each_entry(vm, &vm_list, vm_list) {
+		if (kvm == vm)
+			continue;
+
+		apm = kvm_ap_get_crycb_apm(vm);
+		if (!bitmap_and(apm, apm, matrix->apm, matrix->apm_max))
+			continue;
+
+		aqm = kvm_ap_get_crycb_aqm(vm);
+		if (!bitmap_and(aqm, aqm, matrix->aqm, matrix->aqm_max))
+			continue;
+
+		for_each_set_bit_inv(apid, apm, matrix->apm_max)
+			for_each_set_bit_inv(apqi, aqm, matrix->aqm_max)
+				kvm_ap_log_sharing_err(kvm, apid, apqi);
+
+		return -EBUSY;
+	}
+
+	return 0;
+}
+
  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;
  	}
  }

arch/s390/kvm/kvm-ap.c void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd) · line 182

Quoted 10 lines
arch/s390/kvm/kvm-ap.c +10 -0
+
+static int kvm_ap_matrix_apm_create(struct kvm_ap_matrix *ap_matrix, int apxa)
+{
+	if (apxa)
+		ap_matrix->apm_max = 256;
+	else
+		ap_matrix->apm_max = 64;
+
+	ap_matrix->apm = kzalloc(KVM_AP_MASK_BYTES(ap_matrix->apm_max),
+				 GFP_KERNEL);

Replace KVM_AP_MASK_BYTES macro with BITS_TO_BYTES macro

Quoted 5 lines
arch/s390/kvm/kvm-ap.c +5 -0
+	if (!ap_matrix->apm)
+		return -ENOMEM;
+
+	return 0;
+}

arch/s390/kvm/kvm-ap.c void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd) · line 197

Quoted 10 lines
arch/s390/kvm/kvm-ap.c +10 -0
+
+static int kvm_ap_matrix_aqm_create(struct kvm_ap_matrix *ap_matrix, int apxa)
+{
+	if (apxa)
+		ap_matrix->aqm_max = 256;
+	else
+		ap_matrix->aqm_max = 16;
+
+	ap_matrix->aqm = kzalloc(KVM_AP_MASK_BYTES(ap_matrix->aqm_max),
+				 GFP_KERNEL);

Replace KVM_AP_MASK_BYTES macro with BITS_TO_BYTES macro

Quoted 5 lines
arch/s390/kvm/kvm-ap.c +5 -0
+	if (!ap_matrix->aqm)
+		return -ENOMEM;
+
+	return 0;
+}

arch/s390/kvm/kvm-ap.c void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd) · line 212

Quoted 10 lines
arch/s390/kvm/kvm-ap.c +10 -0
+
+static int kvm_ap_matrix_adm_create(struct kvm_ap_matrix *ap_matrix, int apxa)
+{
+	if (apxa)
+		ap_matrix->adm_max = 256;
+	else
+		ap_matrix->adm_max = 16;
+
+	ap_matrix->adm = kzalloc(KVM_AP_MASK_BYTES(ap_matrix->adm_max),
+				 GFP_KERNEL);

Replace KVM_AP_MASK_BYTES macro with BITS_TO_BYTES macro

Quoted 159 lines
arch/s390/kvm/kvm-ap.c +97 -0
+	if (!ap_matrix->adm)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static void kvm_ap_matrix_masks_destroy(struct kvm_ap_matrix *ap_matrix)
+{
+	kfree(ap_matrix->apm);
+	kfree(ap_matrix->aqm);
+	kfree(ap_matrix->adm);
+}
+
+int kvm_ap_matrix_create(struct kvm_ap_matrix **ap_matrix)
+{
+	int ret;
+	int apxa = kvm_ap_apxa_installed();
+	struct kvm_ap_matrix *matrix;
+
+	matrix = kzalloc(sizeof(*matrix), GFP_KERNEL);
+	if (!matrix)
+		return -ENOMEM;
+
+	ret = kvm_ap_matrix_apm_create(matrix, apxa);
+	if (ret)
+		goto mask_create_err;
+
+	ret = kvm_ap_matrix_aqm_create(matrix, apxa);
+	if (ret)
+		goto mask_create_err;
+
+	ret = kvm_ap_matrix_adm_create(matrix, apxa);
+	if (ret)
+		goto mask_create_err;
+
+	*ap_matrix = matrix;
+
+	return 0;
+
+mask_create_err:
+	kvm_ap_matrix_masks_destroy(matrix);
+	kfree(matrix);
+	return ret;
+}
+EXPORT_SYMBOL(kvm_ap_matrix_create);
+
+void kvm_ap_matrix_destroy(struct kvm_ap_matrix *ap_matrix)
+{
+	kvm_ap_matrix_masks_destroy(ap_matrix);
+	kfree(ap_matrix);
+}
+EXPORT_SYMBOL(kvm_ap_matrix_destroy);
+
+/**
+ * kvm_ap_configure_matrix
+ *
+ * Configure the AP matrix for a KVM guest.
+ *
+ * @kvm:		 the KVM guest
+ * @matrix:		 the matrix configuration information
+ *
+ * Returns 0 if the APQNs derived from the intersection of the set of adapter
+ * IDs (APM) and queue indexes (AQM) in @matrix are not configured for any
+ * other KVM guest running on the same linux host. Otherwise returns an error
+ * code.
+ */
+int kvm_ap_configure_matrix(struct kvm *kvm, struct kvm_ap_matrix *matrix)
+{
+	int ret = 0;
+
+	mutex_lock(&kvm->lock);
+
+	ret = kvm_ap_validate_queue_sharing(kvm, matrix);
+	if (ret)
+		return ret;
+
+	kvm_ap_set_crycb_masks(kvm, matrix);
+
+	mutex_unlock(&kvm->lock);
+
+	return 0;
+}
+EXPORT_SYMBOL(kvm_ap_configure_matrix);
+
+/**
+ * kvm_ap_deconfigure_matrix
+ *
+ * Deconfigure the AP matrix for a KVM guest. Clears all of the bits in the
+ * APM, AQM and ADM in the guest's CRYCB.
+ *
+ * @kvm: the KVM guest
+ */
+void kvm_ap_deconfigure_matrix(struct kvm *kvm)
+{
+	kvm_ap_clear_crycb_masks(kvm);
+}
+EXPORT_SYMBOL(kvm_ap_deconfigure_matrix);
drivers/s390/crypto/vfio_ap_ops.c +19 -0
index 4292a5e..4fda44e 100644
@@ -10,6 +10,7 @@
  #include <linux/device.h>
  #include <linux/list.h>
  #include <linux/ctype.h>
+#include <asm/kvm-ap.h>
 
  #include "vfio_ap_private.h"
 
@@ -18,8 +19,23 @@
 
  static int vfio_ap_mdev_create(struct kobject *kobj, struct mdev_device *mdev)
  {
+	int ret;
+	struct ap_matrix_mdev *matrix_mdev;
  	struct ap_matrix *ap_matrix = to_ap_matrix(mdev_parent_dev(mdev));
+	struct kvm_ap_matrix *matrix;
+
+	ret = kvm_ap_matrix_create(&matrix);
+	if (ret)
+		return ret;
+
+	matrix_mdev = kzalloc(sizeof(*matrix_mdev), GFP_KERNEL);
+	if (!matrix_mdev) {
+		kvm_ap_matrix_destroy(matrix);
+		return -ENOMEM;
+	}
 
+	matrix_mdev->matrix = matrix;
+	mdev_set_drvdata(mdev, matrix_mdev);
  	ap_matrix->available_instances--;
 
  	return 0;
@@ -28,7 +44,10 @@ static int vfio_ap_mdev_create(struct kobject *kobj, struct mdev_device *mdev)
  static int vfio_ap_mdev_remove(struct mdev_device *mdev)
  {
  	struct ap_matrix *ap_matrix = to_ap_matrix(mdev_parent_dev(mdev));
+	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
 
+	kvm_ap_matrix_destroy(matrix_mdev->matrix);
+	kfree(matrix_mdev);
  	ap_matrix->available_instances++;
 
  	return 0;
drivers/s390/crypto/vfio_ap_private.h +4 -0
index c264415..522564e 100644
@@ -27,6 +27,10 @@ struct ap_matrix {
  	int available_instances;
  };
 
+struct ap_matrix_mdev {
+	struct kvm_ap_matrix *matrix;
+};
+
  static inline struct ap_matrix *to_ap_matrix(struct device *dev)
  {
  	return container_of(dev, struct ap_matrix, device);
#28 lore raw

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

In reply to Tony Krowiak
Quoted 56 lines · Tony Krowiak

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.

---
  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(-)
arch/s390/include/asm/kvm-ap.h +0 -0
index ef749e7..46e7c5b 100644

arch/s390/include/asm/kvm-ap.h line 17

Quoted 8 lines
arch/s390/include/asm/kvm-ap.h +5 -0
@@ -10,9 +10,45 @@
  #define _ASM_KVM_AP
  #include <linux/types.h>
  #include <linux/kvm_host.h>
+#include <linux/types.h>
+#include <linux/kvm_host.h>
+#include <linux/bitops.h>
+
+#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)

Quoted 378 lines
arch/s390/include/asm/kvm-ap.h +31 -0
+
+/**
+ * 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 */
arch/s390/kvm/kvm-ap.c +257 -0
index bafe63b..bb29045 100644
@@ -8,6 +8,7 @@
 
  #include <asm/kvm-ap.h>
  #include <asm/ap.h>
+#include <linux/bitops.h>
 
  #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(&kvm->arch.crypto.crycb->apcb1, 0,
+		       sizeof(kvm->arch.crypto.crycb->apcb1));
+	else
+		memset(&kvm->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 *kvm_ap_get_crycb_aqm(struct kvm *kvm)
+{
+	unsigned long *aqm;
+	int crycb_fmt = kvm_ap_get_crycb_format(kvm);
+
+	if (crycb_fmt == CRYCB_FORMAT2)
+		aqm = (unsigned long *)kvm->arch.crypto.crycb->apcb1.aqm;
+	else
+		aqm = (unsigned long *)kvm->arch.crypto.crycb->apcb0.aqm;
+
+	return aqm;
+}
+
+static inline unsigned long *kvm_ap_get_crycb_adm(struct kvm *kvm)
+{
+	unsigned long *adm;
+	int crycb_fmt = kvm_ap_get_crycb_format(kvm);
+
+	if (crycb_fmt == CRYCB_FORMAT2)
+		adm = (unsigned long *)kvm->arch.crypto.crycb->apcb1.adm;
+	else
+		adm = (unsigned long *)kvm->arch.crypto.crycb->apcb0.adm;
+
+	return adm;
+}
+
+static void kvm_ap_set_crycb_masks(struct kvm *kvm,
+				   struct kvm_ap_matrix *matrix)
+{
+	unsigned long *apm = kvm_ap_get_crycb_apm(kvm);
+	unsigned long *aqm = kvm_ap_get_crycb_aqm(kvm);
+	unsigned long *adm = kvm_ap_get_crycb_adm(kvm);
+
+	kvm_ap_clear_crycb_masks(kvm);
+	memcpy(apm, matrix->apm, KVM_AP_MASK_BYTES(matrix->apm_max));
+	memcpy(aqm, matrix->aqm, KVM_AP_MASK_BYTES(matrix->aqm_max));
+
+	/*
+	 * Merge the AQM and ADM since the ADM is a superset of the
+	 * AQM by architectural convention.
+	 */
+	bitmap_or(adm, adm, aqm, matrix->adm_max);
+}
+
+static void kvm_ap_log_sharing_err(struct kvm *kvm, unsigned long apid,
+				   unsigned long apqi)
+{
+	pr_err("%s: AP queue %02lx.%04lx is registered to guest %s", __func__,
+	       apid, apqi, kvm->arch.dbf->name);
+}
+
+/**
+ * kvm_ap_validate_queue_sharing
+ *
+ * Verifies that the APQNs derived from the intersection of the AP adapter IDs
+ * and AP queue indexes comprising the AP matrix are not configured for
+ * another guest. AP queue sharing is not allowed.
+ *
+ * @kvm: the KVM guest
+ * @matrix: the AP matrix
+ *
+ * Returns 0 if the APQNs are valid, otherwise; returns -EBUSY.
+ */
+static int kvm_ap_validate_queue_sharing(struct kvm *kvm,
+					 struct kvm_ap_matrix *matrix)
+{
+	struct kvm *vm;
+	unsigned long *apm, *aqm;
+	unsigned long apid, apqi;
+
+
+	/* No other VM may share an AP Queue with the input VM */
+	list_for_each_entry(vm, &vm_list, vm_list) {
+		if (kvm == vm)
+			continue;
+
+		apm = kvm_ap_get_crycb_apm(vm);
+		if (!bitmap_and(apm, apm, matrix->apm, matrix->apm_max))
+			continue;
+
+		aqm = kvm_ap_get_crycb_aqm(vm);
+		if (!bitmap_and(aqm, aqm, matrix->aqm, matrix->aqm_max))
+			continue;
+
+		for_each_set_bit_inv(apid, apm, matrix->apm_max)
+			for_each_set_bit_inv(apqi, aqm, matrix->aqm_max)
+				kvm_ap_log_sharing_err(kvm, apid, apqi);
+
+		return -EBUSY;
+	}
+
+	return 0;
+}
+
  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;
+	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;
+	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;
+	else
+		ap_matrix->adm_max = 16;
+
+	ap_matrix->adm = kzalloc(KVM_AP_MASK_BYTES(ap_matrix->adm_max),
+				 GFP_KERNEL);
+	if (!ap_matrix->adm)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static void kvm_ap_matrix_masks_destroy(struct kvm_ap_matrix *ap_matrix)
+{
+	kfree(ap_matrix->apm);
+	kfree(ap_matrix->aqm);
+	kfree(ap_matrix->adm);
+}
+
+int kvm_ap_matrix_create(struct kvm_ap_matrix **ap_matrix)
+{
+	int ret;
+	int apxa = kvm_ap_apxa_installed();
+	struct kvm_ap_matrix *matrix;
+
+	matrix = kzalloc(sizeof(*matrix), GFP_KERNEL);
+	if (!matrix)
+		return -ENOMEM;
+
+	ret = kvm_ap_matrix_apm_create(matrix, apxa);
+	if (ret)
+		goto mask_create_err;
+
+	ret = kvm_ap_matrix_aqm_create(matrix, apxa);
+	if (ret)
+		goto mask_create_err;
+
+	ret = kvm_ap_matrix_adm_create(matrix, apxa);
+	if (ret)
+		goto mask_create_err;
+
+	*ap_matrix = matrix;
+
+	return 0;
+
+mask_create_err:
+	kvm_ap_matrix_masks_destroy(matrix);
+	kfree(matrix);
+	return ret;
+}
+EXPORT_SYMBOL(kvm_ap_matrix_create);
+
+void kvm_ap_matrix_destroy(struct kvm_ap_matrix *ap_matrix)
+{
+	kvm_ap_matrix_masks_destroy(ap_matrix);
+	kfree(ap_matrix);
+}
+EXPORT_SYMBOL(kvm_ap_matrix_destroy);
+
+/**
+ * kvm_ap_configure_matrix
+ *
+ * Configure the AP matrix for a KVM guest.
+ *
+ * @kvm:		 the KVM guest
+ * @matrix:		 the matrix configuration information
+ *
+ * Returns 0 if the APQNs derived from the intersection of the set of adapter
+ * IDs (APM) and queue indexes (AQM) in @matrix are not configured for any
+ * other KVM guest running on the same linux host. Otherwise returns an error
+ * code.
+ */
+int kvm_ap_configure_matrix(struct kvm *kvm, struct kvm_ap_matrix *matrix)
+{
+	int ret = 0;
+
+	mutex_lock(&kvm->lock);
+
+	ret = kvm_ap_validate_queue_sharing(kvm, matrix);
+	if (ret)
+		return ret;
+
+	kvm_ap_set_crycb_masks(kvm, matrix);
+
+	mutex_unlock(&kvm->lock);
+
+	return 0;
+}
+EXPORT_SYMBOL(kvm_ap_configure_matrix);
+
+/**
+ * kvm_ap_deconfigure_matrix
+ *
+ * Deconfigure the AP matrix for a KVM guest. Clears all of the bits in the
+ * APM, AQM and ADM in the guest's CRYCB.
+ *
+ * @kvm: the KVM guest
+ */
+void kvm_ap_deconfigure_matrix(struct kvm *kvm)
+{
+	kvm_ap_clear_crycb_masks(kvm);
+}
+EXPORT_SYMBOL(kvm_ap_deconfigure_matrix);
drivers/s390/crypto/vfio_ap_ops.c +19 -0
index 4292a5e..4fda44e 100644
@@ -10,6 +10,7 @@
  #include <linux/device.h>
  #include <linux/list.h>
  #include <linux/ctype.h>
+#include <asm/kvm-ap.h>
 
  #include "vfio_ap_private.h"
 
@@ -18,8 +19,23 @@
 
  static int vfio_ap_mdev_create(struct kobject *kobj, struct mdev_device *mdev)
  {
+	int ret;
+	struct ap_matrix_mdev *matrix_mdev;
  	struct ap_matrix *ap_matrix = to_ap_matrix(mdev_parent_dev(mdev));
+	struct kvm_ap_matrix *matrix;
+
+	ret = kvm_ap_matrix_create(&matrix);
+	if (ret)
+		return ret;
+
+	matrix_mdev = kzalloc(sizeof(*matrix_mdev), GFP_KERNEL);
+	if (!matrix_mdev) {
+		kvm_ap_matrix_destroy(matrix);
+		return -ENOMEM;
+	}
 
+	matrix_mdev->matrix = matrix;
+	mdev_set_drvdata(mdev, matrix_mdev);
  	ap_matrix->available_instances--;
 
  	return 0;
@@ -28,7 +44,10 @@ static int vfio_ap_mdev_create(struct kobject *kobj, struct mdev_device *mdev)
  static int vfio_ap_mdev_remove(struct mdev_device *mdev)
  {
  	struct ap_matrix *ap_matrix = to_ap_matrix(mdev_parent_dev(mdev));
+	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
 
+	kvm_ap_matrix_destroy(matrix_mdev->matrix);
+	kfree(matrix_mdev);
  	ap_matrix->available_instances++;
 
  	return 0;
drivers/s390/crypto/vfio_ap_private.h +4 -0
index c264415..522564e 100644
@@ -27,6 +27,10 @@ struct ap_matrix {
  	int available_instances;
  };
 
+struct ap_matrix_mdev {
+	struct kvm_ap_matrix *matrix;
+};
+
  static inline struct ap_matrix *to_ap_matrix(struct device *dev)
  {
  	return container_of(dev, struct ap_matrix, device);
#29 lore raw

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

In reply to Tony Krowiak
Quoted 348 lines · Tony Krowiak

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.

---
  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(-)
arch/s390/include/asm/kvm-ap.h +36 -0
index ef749e7..46e7c5b 100644
@@ -10,9 +10,45 @@
  #define _ASM_KVM_AP
  #include <linux/types.h>
  #include <linux/kvm_host.h>
+#include <linux/types.h>
+#include <linux/kvm_host.h>
+#include <linux/bitops.h>
+
+#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 */
arch/s390/kvm/kvm-ap.c +225 -0
index bafe63b..bb29045 100644
@@ -8,6 +8,7 @@
 
  #include <asm/kvm-ap.h>
  #include <asm/ap.h>
+#include <linux/bitops.h>
 
  #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(&kvm->arch.crypto.crycb->apcb1, 0,
+		       sizeof(kvm->arch.crypto.crycb->apcb1));
+	else
+		memset(&kvm->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 *kvm_ap_get_crycb_aqm(struct kvm *kvm)
+{
+	unsigned long *aqm;
+	int crycb_fmt = kvm_ap_get_crycb_format(kvm);
+
+	if (crycb_fmt == CRYCB_FORMAT2)
+		aqm = (unsigned long *)kvm->arch.crypto.crycb->apcb1.aqm;
+	else
+		aqm = (unsigned long *)kvm->arch.crypto.crycb->apcb0.aqm;
+
+	return aqm;
+}
+
+static inline unsigned long *kvm_ap_get_crycb_adm(struct kvm *kvm)
+{
+	unsigned long *adm;
+	int crycb_fmt = kvm_ap_get_crycb_format(kvm);
+
+	if (crycb_fmt == CRYCB_FORMAT2)
+		adm = (unsigned long *)kvm->arch.crypto.crycb->apcb1.adm;
+	else
+		adm = (unsigned long *)kvm->arch.crypto.crycb->apcb0.adm;
+
+	return adm;
+}
+
+static void kvm_ap_set_crycb_masks(struct kvm *kvm,
+				   struct kvm_ap_matrix *matrix)
+{
+	unsigned long *apm = kvm_ap_get_crycb_apm(kvm);
+	unsigned long *aqm = kvm_ap_get_crycb_aqm(kvm);
+	unsigned long *adm = kvm_ap_get_crycb_adm(kvm);
+
+	kvm_ap_clear_crycb_masks(kvm);
+	memcpy(apm, matrix->apm, KVM_AP_MASK_BYTES(matrix->apm_max));
+	memcpy(aqm, matrix->aqm, KVM_AP_MASK_BYTES(matrix->aqm_max));
+
+	/*
+	 * Merge the AQM and ADM since the ADM is a superset of the
+	 * AQM by architectural convention.
+	 */
+	bitmap_or(adm, adm, aqm, matrix->adm_max);
+}
+
+static void kvm_ap_log_sharing_err(struct kvm *kvm, unsigned long apid,
+				   unsigned long apqi)
+{
+	pr_err("%s: AP queue %02lx.%04lx is registered to guest %s", __func__,
+	       apid, apqi, kvm->arch.dbf->name);
+}
+
+/**
+ * kvm_ap_validate_queue_sharing
+ *
+ * Verifies that the APQNs derived from the intersection of the AP adapter IDs
+ * and AP queue indexes comprising the AP matrix are not configured for
+ * another guest. AP queue sharing is not allowed.
+ *
+ * @kvm: the KVM guest
+ * @matrix: the AP matrix
+ *
+ * Returns 0 if the APQNs are valid, otherwise; returns -EBUSY.
+ */
+static int kvm_ap_validate_queue_sharing(struct kvm *kvm,
+					 struct kvm_ap_matrix *matrix)
+{
+	struct kvm *vm;
+	unsigned long *apm, *aqm;
+	unsigned long apid, apqi;
+
+
+	/* No other VM may share an AP Queue with the input VM */
+	list_for_each_entry(vm, &vm_list, vm_list) {
+		if (kvm == vm)
+			continue;
+
+		apm = kvm_ap_get_crycb_apm(vm);
+		if (!bitmap_and(apm, apm, matrix->apm, matrix->apm_max))
+			continue;
+
+		aqm = kvm_ap_get_crycb_aqm(vm);
+		if (!bitmap_and(aqm, aqm, matrix->aqm, matrix->aqm_max))
+			continue;
+
+		for_each_set_bit_inv(apid, apm, matrix->apm_max)
+			for_each_set_bit_inv(apqi, aqm, matrix->aqm_max)
+				kvm_ap_log_sharing_err(kvm, apid, apqi);
+
+		return -EBUSY;
+	}
+
+	return 0;
+}
+
  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;
+	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;
+	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;
+	else
+		ap_matrix->adm_max = 16;
+
+	ap_matrix->adm = kzalloc(KVM_AP_MASK_BYTES(ap_matrix->adm_max),
+				 GFP_KERNEL);
+	if (!ap_matrix->adm)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static void kvm_ap_matrix_masks_destroy(struct kvm_ap_matrix *ap_matrix)
+{
+	kfree(ap_matrix->apm);
+	kfree(ap_matrix->aqm);
+	kfree(ap_matrix->adm);
+}
+
+int kvm_ap_matrix_create(struct kvm_ap_matrix **ap_matrix)
+{
+	int ret;
+	int apxa = kvm_ap_apxa_installed();
+	struct kvm_ap_matrix *matrix;
+
+	matrix = kzalloc(sizeof(*matrix), GFP_KERNEL);
+	if (!matrix)
+		return -ENOMEM;
+
+	ret = kvm_ap_matrix_apm_create(matrix, apxa);
+	if (ret)
+		goto mask_create_err;
+
+	ret = kvm_ap_matrix_aqm_create(matrix, apxa);
+	if (ret)
+		goto mask_create_err;
+
+	ret = kvm_ap_matrix_adm_create(matrix, apxa);
+	if (ret)
+		goto mask_create_err;
+
+	*ap_matrix = matrix;
+
+	return 0;
+
+mask_create_err:
+	kvm_ap_matrix_masks_destroy(matrix);
+	kfree(matrix);
+	return ret;
+}
+EXPORT_SYMBOL(kvm_ap_matrix_create);
+
+void kvm_ap_matrix_destroy(struct kvm_ap_matrix *ap_matrix)
+{
+	kvm_ap_matrix_masks_destroy(ap_matrix);
+	kfree(ap_matrix);
+}
+EXPORT_SYMBOL(kvm_ap_matrix_destroy);
+
+/**
+ * kvm_ap_configure_matrix
+ *
+ * Configure the AP matrix for a KVM guest.
+ *
+ * @kvm:		 the KVM guest
+ * @matrix:		 the matrix configuration information
+ *
+ * Returns 0 if the APQNs derived from the intersection of the set of adapter
+ * IDs (APM) and queue indexes (AQM) in @matrix are not configured for any
+ * other KVM guest running on the same linux host. Otherwise returns an error
+ * code.

arch/s390/kvm/kvm-ap.c void kvm_ap_set_crycb_format(struct kvm *kvm, __u32 *crycbd) · line 287

Quoted 10 lines
arch/s390/kvm/kvm-ap.c +10 -0
+ */
+int kvm_ap_configure_matrix(struct kvm *kvm, struct kvm_ap_matrix *matrix)
+{
+	int ret = 0;
+
+	mutex_lock(&kvm->lock);
+
+	ret = kvm_ap_validate_queue_sharing(kvm, matrix);
+	if (ret)
+		return ret;

The kvm->lock needs to be unlocked.

Quoted 84 lines
arch/s390/kvm/kvm-ap.c +22 -0
+
+	kvm_ap_set_crycb_masks(kvm, matrix);
+
+	mutex_unlock(&kvm->lock);
+
+	return 0;
+}
+EXPORT_SYMBOL(kvm_ap_configure_matrix);
+
+/**
+ * kvm_ap_deconfigure_matrix
+ *
+ * Deconfigure the AP matrix for a KVM guest. Clears all of the bits in the
+ * APM, AQM and ADM in the guest's CRYCB.
+ *
+ * @kvm: the KVM guest
+ */
+void kvm_ap_deconfigure_matrix(struct kvm *kvm)
+{
+	kvm_ap_clear_crycb_masks(kvm);
+}
+EXPORT_SYMBOL(kvm_ap_deconfigure_matrix);
drivers/s390/crypto/vfio_ap_ops.c +19 -0
index 4292a5e..4fda44e 100644
@@ -10,6 +10,7 @@
  #include <linux/device.h>
  #include <linux/list.h>
  #include <linux/ctype.h>
+#include <asm/kvm-ap.h>
 
  #include "vfio_ap_private.h"
 
@@ -18,8 +19,23 @@
 
  static int vfio_ap_mdev_create(struct kobject *kobj, struct mdev_device *mdev)
  {
+	int ret;
+	struct ap_matrix_mdev *matrix_mdev;
  	struct ap_matrix *ap_matrix = to_ap_matrix(mdev_parent_dev(mdev));
+	struct kvm_ap_matrix *matrix;
+
+	ret = kvm_ap_matrix_create(&matrix);
+	if (ret)
+		return ret;
+
+	matrix_mdev = kzalloc(sizeof(*matrix_mdev), GFP_KERNEL);
+	if (!matrix_mdev) {
+		kvm_ap_matrix_destroy(matrix);
+		return -ENOMEM;
+	}
 
+	matrix_mdev->matrix = matrix;
+	mdev_set_drvdata(mdev, matrix_mdev);
  	ap_matrix->available_instances--;
 
  	return 0;
@@ -28,7 +44,10 @@ static int vfio_ap_mdev_create(struct kobject *kobj, struct mdev_device *mdev)
  static int vfio_ap_mdev_remove(struct mdev_device *mdev)
  {
  	struct ap_matrix *ap_matrix = to_ap_matrix(mdev_parent_dev(mdev));
+	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
 
+	kvm_ap_matrix_destroy(matrix_mdev->matrix);
+	kfree(matrix_mdev);
  	ap_matrix->available_instances++;
 
  	return 0;
drivers/s390/crypto/vfio_ap_private.h +4 -0
index c264415..522564e 100644
@@ -27,6 +27,10 @@ struct ap_matrix {
  	int available_instances;
  };
 
+struct ap_matrix_mdev {
+	struct kvm_ap_matrix *matrix;
+};
+
  static inline struct ap_matrix *to_ap_matrix(struct device *dev)
  {
  	return container_of(dev, struct ap_matrix, device);
#30 lore raw

PATCH v2 8/15 KVM: s390: interface to enable AP execution mode

In reply to Tony Krowiak

Introduces a new interface to enable AP interpretive execution (IE) mode for the KVM guest. When running with IE mode enabled, AP instructions executed on the KVM guest will be interpreted by the firmware and passed directly through to an AP device installed on the system. The CPU model feature for AP must be enabled for the KVM guest in order to enable interpretive execution mode.

This interface will be used in a subsequent patch by the VFIO AP device driver.

---
 arch/s390/include/asm/kvm-ap.h   |    2 ++
 arch/s390/include/asm/kvm_host.h |    1 +
 arch/s390/kvm/kvm-ap.c           |   27 +++++++++++++++++++++++++++
 arch/s390/kvm/kvm-s390.h         |    1 +
 4 files changed, 31 insertions(+), 0 deletions(-)
arch/s390/include/asm/kvm-ap.h +2 -0
index 46e7c5b..6bd6bfb 100644
@@ -51,4 +51,6 @@ struct kvm_ap_matrix {
 
 void kvm_ap_deconfigure_matrix(struct kvm *kvm);
 
+int kvm_ap_enable_ie_mode(struct kvm *kvm);
+
 #endif /* _ASM_KVM_AP */
arch/s390/include/asm/kvm_host.h +1 -0
index a4c77d3..1eebdd6 100644
@@ -186,6 +186,7 @@ struct kvm_s390_sie_block {
 #define ECA_AIV		0x00200000
 #define ECA_VX		0x00020000
 #define ECA_PROTEXCI	0x00002000
+#define ECA_APIE	0x00000008
 #define ECA_SII		0x00000001
 	__u32	eca;			/* 0x004c */
 #define ICPT_INST	0x04
arch/s390/kvm/kvm-ap.c +27 -0
index bb29045..862e54b 100644
@@ -307,3 +307,30 @@ void kvm_ap_deconfigure_matrix(struct kvm *kvm)
 	kvm_ap_clear_crycb_masks(kvm);
 }
 EXPORT_SYMBOL(kvm_ap_deconfigure_matrix);
+
+/**
+ * kvm_ap_enable_ie_mode
+ *
+ * Enable interpretrive execution of AP instructions for the guest. When
+ * enabled, AP instructions executed on the guest will be interpreted and
+ * passed through to an AP installed on the host system.
+ *
+ * Returns 0 if interpretrive execution is enabled. Returns -EOPNOTSUPP
+ * if AP facilities are not installed for the guest.
+ *
+ * @kvm: the guest's kvm structure
+ */
+int kvm_ap_enable_ie_mode(struct kvm *kvm)
+{
+	int i;
+	struct kvm_vcpu *vcpu;
+
+	if (!test_kvm_cpu_feat(kvm, KVM_S390_VM_CPU_FEAT_AP))
+		return -EOPNOTSUPP;
+
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		vcpu->arch.sie_block->eca |= ECA_APIE;
+
+	return 0;
+}
+EXPORT_SYMBOL(kvm_ap_enable_ie_mode);
arch/s390/kvm/kvm-s390.h +1 -0
index 1b5621f..3142541 100644
@@ -18,6 +18,7 @@
 #include <asm/facility.h>
 #include <asm/processor.h>
 #include <asm/sclp.h>
+#include <asm/ap.h>
 
 /* Transactional Memory Execution related macros */
 #define IS_TE_ENABLED(vcpu)	((vcpu->arch.sie_block->ecb & ECB_TE))
Signature · 1 line
1.7.1
#31 lore raw

Re: [PATCH v2 08/15] KVM: s390: interface to enable AP execution mode

Quoted 84 lines · Tony Krowiak

Introduces a new interface to enable AP interpretive execution (IE) mode for the KVM guest. When running with IE mode enabled, AP instructions executed on the KVM guest will be interpreted by the firmware and passed directly through to an AP device installed on the system. The CPU model feature for AP must be enabled for the KVM guest in order to enable interpretive execution mode.

This interface will be used in a subsequent patch by the VFIO AP device driver.

---
 arch/s390/include/asm/kvm-ap.h   |    2 ++
 arch/s390/include/asm/kvm_host.h |    1 +
 arch/s390/kvm/kvm-ap.c           |   27 +++++++++++++++++++++++++++
 arch/s390/kvm/kvm-s390.h         |    1 +
 4 files changed, 31 insertions(+), 0 deletions(-)
arch/s390/include/asm/kvm-ap.h +2 -0
index 46e7c5b..6bd6bfb 100644
@@ -51,4 +51,6 @@ struct kvm_ap_matrix {
 
 void kvm_ap_deconfigure_matrix(struct kvm *kvm);
 
+int kvm_ap_enable_ie_mode(struct kvm *kvm);
+
 #endif /* _ASM_KVM_AP */
arch/s390/include/asm/kvm_host.h +1 -0
index a4c77d3..1eebdd6 100644
@@ -186,6 +186,7 @@ struct kvm_s390_sie_block {
 #define ECA_AIV		0x00200000
 #define ECA_VX		0x00020000
 #define ECA_PROTEXCI	0x00002000
+#define ECA_APIE	0x00000008
 #define ECA_SII		0x00000001
 	__u32	eca;			/* 0x004c */
 #define ICPT_INST	0x04
arch/s390/kvm/kvm-ap.c +27 -0
index bb29045..862e54b 100644
@@ -307,3 +307,30 @@ void kvm_ap_deconfigure_matrix(struct kvm *kvm)
 	kvm_ap_clear_crycb_masks(kvm);
 }
 EXPORT_SYMBOL(kvm_ap_deconfigure_matrix);
+
+/**
+ * kvm_ap_enable_ie_mode
+ *
+ * Enable interpretrive execution of AP instructions for the guest. When
+ * enabled, AP instructions executed on the guest will be interpreted and
+ * passed through to an AP installed on the host system.
+ *
+ * Returns 0 if interpretrive execution is enabled. Returns -EOPNOTSUPP
+ * if AP facilities are not installed for the guest.
+ *
+ * @kvm: the guest's kvm structure
+ */
+int kvm_ap_enable_ie_mode(struct kvm *kvm)
+{
+	int i;
+	struct kvm_vcpu *vcpu;
+
+	if (!test_kvm_cpu_feat(kvm, KVM_S390_VM_CPU_FEAT_AP))
+		return -EOPNOTSUPP;
+
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		vcpu->arch.sie_block->eca |= ECA_APIE;
+
+	return 0;
+}
+EXPORT_SYMBOL(kvm_ap_enable_ie_mode);
arch/s390/kvm/kvm-s390.h +0 -0
index 1b5621f..3142541 100644

arch/s390/kvm/kvm-s390.h line 24

Quoted 7 lines
arch/s390/kvm/kvm-s390.h +1 -0
@@ -18,6 +18,7 @@
 #include <asm/facility.h>
 #include <asm/processor.h>
 #include <asm/sclp.h>
+#include <asm/ap.h>
 
 /* Transactional Memory Execution related macros */
 #define IS_TE_ENABLED(vcpu)	((vcpu->arch.sie_block->ecb & ECB_TE))

What about VSIE?

Signature · 3 lines
Thanks,

David / dhildenb
#32 lore raw

Re: [PATCH v2 08/15] KVM: s390: interface to enable AP execution mode

Quoted 89 lines · David Hildenbrand
Quoted 88 lines · Tony Krowiak

Introduces a new interface to enable AP interpretive execution (IE) mode for the KVM guest. When running with IE mode enabled, AP instructions executed on the KVM guest will be interpreted by the firmware and passed directly through to an AP device installed on the system. The CPU model feature for AP must be enabled for the KVM guest in order to enable interpretive execution mode.

This interface will be used in a subsequent patch by the VFIO AP device driver.

---
  arch/s390/include/asm/kvm-ap.h   |    2 ++
  arch/s390/include/asm/kvm_host.h |    1 +
  arch/s390/kvm/kvm-ap.c           |   27 +++++++++++++++++++++++++++
  arch/s390/kvm/kvm-s390.h         |    1 +
  4 files changed, 31 insertions(+), 0 deletions(-)
arch/s390/include/asm/kvm-ap.h +2 -0
index 46e7c5b..6bd6bfb 100644
@@ -51,4 +51,6 @@ struct kvm_ap_matrix {
 
  void kvm_ap_deconfigure_matrix(struct kvm *kvm);
 
+int kvm_ap_enable_ie_mode(struct kvm *kvm);
+
  #endif /* _ASM_KVM_AP */
arch/s390/include/asm/kvm_host.h +1 -0
index a4c77d3..1eebdd6 100644
@@ -186,6 +186,7 @@ struct kvm_s390_sie_block {
  #define ECA_AIV		0x00200000
  #define ECA_VX		0x00020000
  #define ECA_PROTEXCI	0x00002000
+#define ECA_APIE	0x00000008
  #define ECA_SII		0x00000001
  	__u32	eca;			/* 0x004c */
  #define ICPT_INST	0x04
arch/s390/kvm/kvm-ap.c +27 -0
index bb29045..862e54b 100644
@@ -307,3 +307,30 @@ void kvm_ap_deconfigure_matrix(struct kvm *kvm)
  	kvm_ap_clear_crycb_masks(kvm);
  }
  EXPORT_SYMBOL(kvm_ap_deconfigure_matrix);
+
+/**
+ * kvm_ap_enable_ie_mode
+ *
+ * Enable interpretrive execution of AP instructions for the guest. When
+ * enabled, AP instructions executed on the guest will be interpreted and
+ * passed through to an AP installed on the host system.
+ *
+ * Returns 0 if interpretrive execution is enabled. Returns -EOPNOTSUPP
+ * if AP facilities are not installed for the guest.
+ *
+ * @kvm: the guest's kvm structure
+ */
+int kvm_ap_enable_ie_mode(struct kvm *kvm)
+{
+	int i;
+	struct kvm_vcpu *vcpu;
+
+	if (!test_kvm_cpu_feat(kvm, KVM_S390_VM_CPU_FEAT_AP))
+		return -EOPNOTSUPP;
+
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		vcpu->arch.sie_block->eca |= ECA_APIE;
+
+	return 0;
+}
+EXPORT_SYMBOL(kvm_ap_enable_ie_mode);
arch/s390/kvm/kvm-s390.h +1 -0
index 1b5621f..3142541 100644
@@ -18,6 +18,7 @@
  #include <asm/facility.h>
  #include <asm/processor.h>
  #include <asm/sclp.h>
+#include <asm/ap.h>
 
Quoted 4 lines

arch/s390/kvm/kvm-s390.h line 24

Quoted 3 lines
arch/s390/kvm/kvm-s390.h +0 -0
  /* Transactional Memory Execution related macros */
  #define IS_TE_ENABLED(vcpu)	((vcpu->arch.sie_block->ecb & ECB_TE))

What about VSIE?

That is a good question. I'm beginning to think introducing AP on second level guests ought to be postponed for now. I'm going to look into this further.

Quoted 1 line
#33 lore raw

Re: [PATCH v2 08/15] KVM: s390: interface to enable AP execution mode

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
arch/s390/kvm/kvm-ap.c +1 -0
+EXPORT_SYMBOL(kvm_ap_enable_ie_mode);
arch/s390/kvm/kvm-s390.h +1 -0
index 1b5621f..3142541 100644
@@ -18,6 +18,7 @@
  #include <asm/facility.h>
  #include <asm/processor.h>
  #include <asm/sclp.h>
+#include <asm/ap.h>
 
Quoted 8 lines
Quoted 4 lines

arch/s390/kvm/kvm-s390.h line 24

Quoted 3 lines
arch/s390/kvm/kvm-s390.h +0 -0
  /* Transactional Memory Execution related macros */
  #define IS_TE_ENABLED(vcpu)	((vcpu->arch.sie_block->ecb & ECB_TE))

What about VSIE?

That is a good question. I'm beginning to think introducing AP on second level guests
ought to be postponed for now. I'm going to look into this further.

Quoted 1 line

Once you provide AP support to the guest, vSIE also has to be in place.

But don't worry about vSIE, I think I know what we have to do.

Signature · 3 lines
Thanks,

David / dhildenb
#34 lore raw

Re: [PATCH v2 08/15] KVM: s390: interface to enable AP execution mode

In reply to Tony Krowiak
Quoted 84 lines · Tony Krowiak

Introduces a new interface to enable AP interpretive execution (IE) mode for the KVM guest. When running with IE mode enabled, AP instructions executed on the KVM guest will be interpreted by the firmware and passed directly through to an AP device installed on the system. The CPU model feature for AP must be enabled for the KVM guest in order to enable interpretive execution mode.

This interface will be used in a subsequent patch by the VFIO AP device driver.

---
 arch/s390/include/asm/kvm-ap.h   |    2 ++
 arch/s390/include/asm/kvm_host.h |    1 +
 arch/s390/kvm/kvm-ap.c           |   27 +++++++++++++++++++++++++++
 arch/s390/kvm/kvm-s390.h         |    1 +
 4 files changed, 31 insertions(+), 0 deletions(-)
arch/s390/include/asm/kvm-ap.h +2 -0
index 46e7c5b..6bd6bfb 100644
@@ -51,4 +51,6 @@ struct kvm_ap_matrix {
 
 void kvm_ap_deconfigure_matrix(struct kvm *kvm);
 
+int kvm_ap_enable_ie_mode(struct kvm *kvm);
+
 #endif /* _ASM_KVM_AP */
arch/s390/include/asm/kvm_host.h +1 -0
index a4c77d3..1eebdd6 100644
@@ -186,6 +186,7 @@ struct kvm_s390_sie_block {
 #define ECA_AIV		0x00200000
 #define ECA_VX		0x00020000
 #define ECA_PROTEXCI	0x00002000
+#define ECA_APIE	0x00000008
 #define ECA_SII		0x00000001
 	__u32	eca;			/* 0x004c */
 #define ICPT_INST	0x04
arch/s390/kvm/kvm-ap.c +27 -0
index bb29045..862e54b 100644
@@ -307,3 +307,30 @@ void kvm_ap_deconfigure_matrix(struct kvm *kvm)
 	kvm_ap_clear_crycb_masks(kvm);
 }
 EXPORT_SYMBOL(kvm_ap_deconfigure_matrix);
+
+/**
+ * kvm_ap_enable_ie_mode
+ *
+ * Enable interpretrive execution of AP instructions for the guest. When
+ * enabled, AP instructions executed on the guest will be interpreted and
+ * passed through to an AP installed on the host system.
+ *
+ * Returns 0 if interpretrive execution is enabled. Returns -EOPNOTSUPP
+ * if AP facilities are not installed for the guest.
+ *
+ * @kvm: the guest's kvm structure
+ */
+int kvm_ap_enable_ie_mode(struct kvm *kvm)
+{
+	int i;
+	struct kvm_vcpu *vcpu;
+
+	if (!test_kvm_cpu_feat(kvm, KVM_S390_VM_CPU_FEAT_AP))
+		return -EOPNOTSUPP;
+
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		vcpu->arch.sie_block->eca |= ECA_APIE;
+
+	return 0;
+}
+EXPORT_SYMBOL(kvm_ap_enable_ie_mode);
arch/s390/kvm/kvm-s390.h +0 -0
index 1b5621f..3142541 100644

arch/s390/kvm/kvm-s390.h line 24

Quoted 7 lines
arch/s390/kvm/kvm-s390.h +1 -0
@@ -18,6 +18,7 @@
 #include <asm/facility.h>
 #include <asm/processor.h>
 #include <asm/sclp.h>
+#include <asm/ap.h>
 
 /* Transactional Memory Execution related macros */
 #define IS_TE_ENABLED(vcpu)	((vcpu->arch.sie_block->ecb & ECB_TE))

And also, what about hot-plugged CPUs?

Signature · 3 lines
Thanks,

David / dhildenb
#35 lore raw

Re: [PATCH v2 08/15] KVM: s390: interface to enable AP execution mode

Quoted 89 lines · David Hildenbrand
Quoted 88 lines · Tony Krowiak

Introduces a new interface to enable AP interpretive execution (IE) mode for the KVM guest. When running with IE mode enabled, AP instructions executed on the KVM guest will be interpreted by the firmware and passed directly through to an AP device installed on the system. The CPU model feature for AP must be enabled for the KVM guest in order to enable interpretive execution mode.

This interface will be used in a subsequent patch by the VFIO AP device driver.

---
  arch/s390/include/asm/kvm-ap.h   |    2 ++
  arch/s390/include/asm/kvm_host.h |    1 +
  arch/s390/kvm/kvm-ap.c           |   27 +++++++++++++++++++++++++++
  arch/s390/kvm/kvm-s390.h         |    1 +
  4 files changed, 31 insertions(+), 0 deletions(-)
arch/s390/include/asm/kvm-ap.h +2 -0
index 46e7c5b..6bd6bfb 100644
@@ -51,4 +51,6 @@ struct kvm_ap_matrix {
 
  void kvm_ap_deconfigure_matrix(struct kvm *kvm);
 
+int kvm_ap_enable_ie_mode(struct kvm *kvm);
+
  #endif /* _ASM_KVM_AP */
arch/s390/include/asm/kvm_host.h +1 -0
index a4c77d3..1eebdd6 100644
@@ -186,6 +186,7 @@ struct kvm_s390_sie_block {
  #define ECA_AIV		0x00200000
  #define ECA_VX		0x00020000
  #define ECA_PROTEXCI	0x00002000
+#define ECA_APIE	0x00000008
  #define ECA_SII		0x00000001
  	__u32	eca;			/* 0x004c */
  #define ICPT_INST	0x04
arch/s390/kvm/kvm-ap.c +27 -0
index bb29045..862e54b 100644
@@ -307,3 +307,30 @@ void kvm_ap_deconfigure_matrix(struct kvm *kvm)
  	kvm_ap_clear_crycb_masks(kvm);
  }
  EXPORT_SYMBOL(kvm_ap_deconfigure_matrix);
+
+/**
+ * kvm_ap_enable_ie_mode
+ *
+ * Enable interpretrive execution of AP instructions for the guest. When
+ * enabled, AP instructions executed on the guest will be interpreted and
+ * passed through to an AP installed on the host system.
+ *
+ * Returns 0 if interpretrive execution is enabled. Returns -EOPNOTSUPP
+ * if AP facilities are not installed for the guest.
+ *
+ * @kvm: the guest's kvm structure
+ */
+int kvm_ap_enable_ie_mode(struct kvm *kvm)
+{
+	int i;
+	struct kvm_vcpu *vcpu;
+
+	if (!test_kvm_cpu_feat(kvm, KVM_S390_VM_CPU_FEAT_AP))
+		return -EOPNOTSUPP;
+
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		vcpu->arch.sie_block->eca |= ECA_APIE;
+
+	return 0;
+}
+EXPORT_SYMBOL(kvm_ap_enable_ie_mode);
arch/s390/kvm/kvm-s390.h +1 -0
index 1b5621f..3142541 100644
@@ -18,6 +18,7 @@
  #include <asm/facility.h>
  #include <asm/processor.h>
  #include <asm/sclp.h>
+#include <asm/ap.h>
 
Quoted 4 lines

arch/s390/kvm/kvm-s390.h line 24

Quoted 3 lines
arch/s390/kvm/kvm-s390.h +0 -0
  /* Transactional Memory Execution related macros */
  #define IS_TE_ENABLED(vcpu)	((vcpu->arch.sie_block->ecb & ECB_TE))

And also, what about hot-plugged CPUs?

I haven't considered that, do you have any suggestions?

Quoted 1 line
#36 lore raw

Re: [PATCH v2 08/15] KVM: s390: interface to enable AP execution mode

Quoted 90 lines · Tony Krowiak
Quoted 89 lines · David Hildenbrand
Quoted 88 lines · Tony Krowiak

Introduces a new interface to enable AP interpretive execution (IE) mode for the KVM guest. When running with IE mode enabled, AP instructions executed on the KVM guest will be interpreted by the firmware and passed directly through to an AP device installed on the system. The CPU model feature for AP must be enabled for the KVM guest in order to enable interpretive execution mode.

This interface will be used in a subsequent patch by the VFIO AP device driver.

---
  arch/s390/include/asm/kvm-ap.h   |    2 ++
  arch/s390/include/asm/kvm_host.h |    1 +
  arch/s390/kvm/kvm-ap.c           |   27 +++++++++++++++++++++++++++
  arch/s390/kvm/kvm-s390.h         |    1 +
  4 files changed, 31 insertions(+), 0 deletions(-)
arch/s390/include/asm/kvm-ap.h +2 -0
index 46e7c5b..6bd6bfb 100644
@@ -51,4 +51,6 @@ struct kvm_ap_matrix {
 
  void kvm_ap_deconfigure_matrix(struct kvm *kvm);
 
+int kvm_ap_enable_ie_mode(struct kvm *kvm);
+
  #endif /* _ASM_KVM_AP */
arch/s390/include/asm/kvm_host.h +1 -0
index a4c77d3..1eebdd6 100644
@@ -186,6 +186,7 @@ struct kvm_s390_sie_block {
  #define ECA_AIV		0x00200000
  #define ECA_VX		0x00020000
  #define ECA_PROTEXCI	0x00002000
+#define ECA_APIE	0x00000008
  #define ECA_SII		0x00000001
  	__u32	eca;			/* 0x004c */
  #define ICPT_INST	0x04
arch/s390/kvm/kvm-ap.c +27 -0
index bb29045..862e54b 100644
@@ -307,3 +307,30 @@ void kvm_ap_deconfigure_matrix(struct kvm *kvm)
  	kvm_ap_clear_crycb_masks(kvm);
  }
  EXPORT_SYMBOL(kvm_ap_deconfigure_matrix);
+
+/**
+ * kvm_ap_enable_ie_mode
+ *
+ * Enable interpretrive execution of AP instructions for the guest. When
+ * enabled, AP instructions executed on the guest will be interpreted and
+ * passed through to an AP installed on the host system.
+ *
+ * Returns 0 if interpretrive execution is enabled. Returns -EOPNOTSUPP
+ * if AP facilities are not installed for the guest.
+ *
+ * @kvm: the guest's kvm structure
+ */
+int kvm_ap_enable_ie_mode(struct kvm *kvm)
+{
+	int i;
+	struct kvm_vcpu *vcpu;
+
+	if (!test_kvm_cpu_feat(kvm, KVM_S390_VM_CPU_FEAT_AP))
+		return -EOPNOTSUPP;
+
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		vcpu->arch.sie_block->eca |= ECA_APIE;
+
+	return 0;
+}
+EXPORT_SYMBOL(kvm_ap_enable_ie_mode);
arch/s390/kvm/kvm-s390.h +1 -0
index 1b5621f..3142541 100644
@@ -18,6 +18,7 @@
  #include <asm/facility.h>
  #include <asm/processor.h>
  #include <asm/sclp.h>
+#include <asm/ap.h>
 
Quoted 5 lines
Quoted 4 lines

arch/s390/kvm/kvm-s390.h line 24

Quoted 3 lines
arch/s390/kvm/kvm-s390.h +0 -0
  /* Transactional Memory Execution related macros */
  #define IS_TE_ENABLED(vcpu)	((vcpu->arch.sie_block->ecb & ECB_TE))

And also, what about hot-plugged CPUs?

I haven't considered that, do you have any suggestions?

You should handle the KVM_S390_VM_CPU_FEAT_AP feature instead during kvm_arch_vcpu_setup(), independent of any configured AP devices.

This avoids the races I mentioned in regards to this series and also will handle hotplugged CPUs properly.

If KVM_S390_VM_CPU_FEAT_AP is configured for a guest -> each CPU sets ECA_APIE during kvm_arch_vcpu_setup().

(In the vSIE code, simply allow to set ECA_APIE in the shadow SCB in case KVM_S390_VM_CPU_FEAT_AP is enabled)

Signature · 3 lines
Thanks,

David / dhildenb
#37 lore raw

Re: [PATCH v2 08/15] KVM: s390: interface to enable AP execution mode

Quoted 96 lines · David Hildenbrand
Quoted 90 lines · Tony Krowiak
Quoted 89 lines · David Hildenbrand
Quoted 88 lines · Tony Krowiak

Introduces a new interface to enable AP interpretive execution (IE) mode for the KVM guest. When running with IE mode enabled, AP instructions executed on the KVM guest will be interpreted by the firmware and passed directly through to an AP device installed on the system. The CPU model feature for AP must be enabled for the KVM guest in order to enable interpretive execution mode.

This interface will be used in a subsequent patch by the VFIO AP device driver.

---
   arch/s390/include/asm/kvm-ap.h   |    2 ++
   arch/s390/include/asm/kvm_host.h |    1 +
   arch/s390/kvm/kvm-ap.c           |   27 +++++++++++++++++++++++++++
   arch/s390/kvm/kvm-s390.h         |    1 +
   4 files changed, 31 insertions(+), 0 deletions(-)
arch/s390/include/asm/kvm-ap.h +2 -0
index 46e7c5b..6bd6bfb 100644
@@ -51,4 +51,6 @@ struct kvm_ap_matrix {
 
   void kvm_ap_deconfigure_matrix(struct kvm *kvm);
 
+int kvm_ap_enable_ie_mode(struct kvm *kvm);
+
   #endif /* _ASM_KVM_AP */
arch/s390/include/asm/kvm_host.h +1 -0
index a4c77d3..1eebdd6 100644
@@ -186,6 +186,7 @@ struct kvm_s390_sie_block {
   #define ECA_AIV		0x00200000
   #define ECA_VX		0x00020000
   #define ECA_PROTEXCI	0x00002000
+#define ECA_APIE	0x00000008
   #define ECA_SII		0x00000001
   	__u32	eca;			/* 0x004c */
   #define ICPT_INST	0x04
arch/s390/kvm/kvm-ap.c +27 -0
index bb29045..862e54b 100644
@@ -307,3 +307,30 @@ void kvm_ap_deconfigure_matrix(struct kvm *kvm)
   	kvm_ap_clear_crycb_masks(kvm);
   }
   EXPORT_SYMBOL(kvm_ap_deconfigure_matrix);
+
+/**
+ * kvm_ap_enable_ie_mode
+ *
+ * Enable interpretrive execution of AP instructions for the guest. When
+ * enabled, AP instructions executed on the guest will be interpreted and
+ * passed through to an AP installed on the host system.
+ *
+ * Returns 0 if interpretrive execution is enabled. Returns -EOPNOTSUPP
+ * if AP facilities are not installed for the guest.
+ *
+ * @kvm: the guest's kvm structure
+ */
+int kvm_ap_enable_ie_mode(struct kvm *kvm)
+{
+	int i;
+	struct kvm_vcpu *vcpu;
+
+	if (!test_kvm_cpu_feat(kvm, KVM_S390_VM_CPU_FEAT_AP))
+		return -EOPNOTSUPP;
+
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		vcpu->arch.sie_block->eca |= ECA_APIE;
+
+	return 0;
+}
+EXPORT_SYMBOL(kvm_ap_enable_ie_mode);
arch/s390/kvm/kvm-s390.h +1 -0
index 1b5621f..3142541 100644
@@ -18,6 +18,7 @@
   #include <asm/facility.h>
   #include <asm/processor.h>
   #include <asm/sclp.h>
+#include <asm/ap.h>
 
Quoted 5 lines
Quoted 4 lines

arch/s390/kvm/kvm-s390.h line 24

Quoted 3 lines
arch/s390/kvm/kvm-s390.h +0 -0
   /* Transactional Memory Execution related macros */
   #define IS_TE_ENABLED(vcpu)	((vcpu->arch.sie_block->ecb & ECB_TE))

And also, what about hot-plugged CPUs?

I haven't considered that, do you have any suggestions?

Quoted 8 lines

You should handle the KVM_S390_VM_CPU_FEAT_AP feature instead during kvm_arch_vcpu_setup(), independent of any configured AP devices.

This avoids the races I mentioned in regards to this series and also will handle hotplugged CPUs properly.

If KVM_S390_VM_CPU_FEAT_AP is configured for a guest -> each CPU sets ECA_APIE during kvm_arch_vcpu_setup().

We talked about this in a couple of other patches, but I am on board with this. To make things clearer, Should we rename the feature to KVM_S390_VM_CPU_FEAT_APIE so its purpose is more clear?

Quoted 5 lines

(In the vSIE code, simply allow to set ECA_APIE in the shadow SCB in case KVM_S390_VM_CPU_FEAT_AP is enabled)

#38 lore raw

Re: [PATCH v2 08/15] KVM: s390: interface to enable AP execution mode

In reply to David Hildenbrand
Quoted 102 lines · David Hildenbrand
Quoted 90 lines · Tony Krowiak
Quoted 89 lines · David Hildenbrand
Quoted 88 lines · Tony Krowiak

Introduces a new interface to enable AP interpretive execution (IE) mode for the KVM guest. When running with IE mode enabled, AP instructions executed on the KVM guest will be interpreted by the firmware and passed directly through to an AP device installed on the system. The CPU model feature for AP must be enabled for the KVM guest in order to enable interpretive execution mode.

This interface will be used in a subsequent patch by the VFIO AP device driver.

---
   arch/s390/include/asm/kvm-ap.h   |    2 ++
   arch/s390/include/asm/kvm_host.h |    1 +
   arch/s390/kvm/kvm-ap.c           |   27 +++++++++++++++++++++++++++
   arch/s390/kvm/kvm-s390.h         |    1 +
   4 files changed, 31 insertions(+), 0 deletions(-)
arch/s390/include/asm/kvm-ap.h +2 -0
index 46e7c5b..6bd6bfb 100644
@@ -51,4 +51,6 @@ struct kvm_ap_matrix {
 
   void kvm_ap_deconfigure_matrix(struct kvm *kvm);
 
+int kvm_ap_enable_ie_mode(struct kvm *kvm);
+
   #endif /* _ASM_KVM_AP */
arch/s390/include/asm/kvm_host.h +1 -0
index a4c77d3..1eebdd6 100644
@@ -186,6 +186,7 @@ struct kvm_s390_sie_block {
   #define ECA_AIV		0x00200000
   #define ECA_VX		0x00020000
   #define ECA_PROTEXCI	0x00002000
+#define ECA_APIE	0x00000008
   #define ECA_SII		0x00000001
   	__u32	eca;			/* 0x004c */
   #define ICPT_INST	0x04
arch/s390/kvm/kvm-ap.c +27 -0
index bb29045..862e54b 100644
@@ -307,3 +307,30 @@ void kvm_ap_deconfigure_matrix(struct kvm *kvm)
   	kvm_ap_clear_crycb_masks(kvm);
   }
   EXPORT_SYMBOL(kvm_ap_deconfigure_matrix);
+
+/**
+ * kvm_ap_enable_ie_mode
+ *
+ * Enable interpretrive execution of AP instructions for the guest. When
+ * enabled, AP instructions executed on the guest will be interpreted and
+ * passed through to an AP installed on the host system.
+ *
+ * Returns 0 if interpretrive execution is enabled. Returns -EOPNOTSUPP
+ * if AP facilities are not installed for the guest.
+ *
+ * @kvm: the guest's kvm structure
+ */
+int kvm_ap_enable_ie_mode(struct kvm *kvm)
+{
+	int i;
+	struct kvm_vcpu *vcpu;
+
+	if (!test_kvm_cpu_feat(kvm, KVM_S390_VM_CPU_FEAT_AP))
+		return -EOPNOTSUPP;
+
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		vcpu->arch.sie_block->eca |= ECA_APIE;
+
+	return 0;
+}
+EXPORT_SYMBOL(kvm_ap_enable_ie_mode);
arch/s390/kvm/kvm-s390.h +1 -0
index 1b5621f..3142541 100644
@@ -18,6 +18,7 @@
   #include <asm/facility.h>
   #include <asm/processor.h>
   #include <asm/sclp.h>
+#include <asm/ap.h>
 
Quoted 5 lines
Quoted 4 lines

arch/s390/kvm/kvm-s390.h line 24

Quoted 3 lines
arch/s390/kvm/kvm-s390.h +0 -0
   /* Transactional Memory Execution related macros */
   #define IS_TE_ENABLED(vcpu)	((vcpu->arch.sie_block->ecb & ECB_TE))

And also, what about hot-plugged CPUs?

I haven't considered that, do you have any suggestions?

You should handle the KVM_S390_VM_CPU_FEAT_AP feature instead during kvm_arch_vcpu_setup(), independent of any configured AP devices.

This avoids the races I mentioned in regards to this series and also will handle hotplugged CPUs properly.

Quoted 6 lines

If KVM_S390_VM_CPU_FEAT_AP is configured for a guest -> each CPU sets ECA_APIE during kvm_arch_vcpu_setup().

(In the vSIE code, simply allow to set ECA_APIE in the shadow SCB in case KVM_S390_VM_CPU_FEAT_AP is enabled)

Patch series v3 will be posted very shortly, but I thought I'd give you a heads up concerning what is forthcoming with regard to ECA_APIE. I'm adding a device attribute to the KVM_S390_VM_CRYPTO group for setting a flag via the KVM_SET_DEVICE_ATTR ioctl. The flag indicates whether ECA_APIE should be set or not. The flag will be checked in the test_kvm_cpu_feat() function and set or clear ECA_APIE accordingly.

You can comment on this in the v3 patch.

Quoted 1 line
#39 lore raw

PATCH v2 9/15 s390: vfio-ap: sysfs interfaces to configure adapters

In reply to Tony Krowiak

Provides the sysfs interfaces for assigning AP adapters to and unassigning AP adapters from a mediated matrix device.

The IDs of the AP adapters assigned to the mediated matrix device are stored in an AP mask (APM). The bits in the APM, from most significant to least significant bit, correspond to AP adapter numbers 0 to 255. When an adapter is assigned, the bit corresponding adapter ID will be set in the APM. Likewise, when an adapter is unassigned, the bit corresponding to the adapter ID will be cleared from the APM.

The relevant sysfs structures are:

/sys/devices/vfio_ap
... [matrix]
...... [mdev_supported_types]
......... [vfio_ap-passthrough]
............ [devices]
...............[$uuid]
.................. assign_adapter
.................. unassign_adapter

To assign an adapter to the $uuid mediated matrix device's APM, write the adapter ID (APID) to the assign_adapter file. To unassign an adapter, write the APID to the unassign_adapter file. The APID is specified using conventional semantics: If it begins with 0x the number will be parsed as a hexadecimal (case insensitive) number; otherwise, it will be parsed as a decimal number.

For example, to assign adapter 173 (0xad) to the mediated matrix device $uuid:

	echo 173 > assign_adapter

	or

	echo 0xad > assign_adapter

To unassign adapter 173 (0xad):

	echo 173 > unassign_adapter

	or

	echo 0xad > unassign_adapter

The assignment will be rejected:

* If the APID exceeds the maximum value for an AP adapter:
* If the AP Extended Addressing (APXA) facility is installed, the max value is 255
* Else the max value is 64

* If no AP domains have yet been assigned and there are no AP queues bound to the VFIO AP driver that have an APQN with an APID matching that of the AP adapter being assigned.

* If any of the APQNs that can be derived from the intersection of the APID being assigned and the AP queue index (APQI) of each of the AP domains previously assigned can not be matched with an APQN of an AP queue device reserved by the VFIO AP driver.

---
 arch/s390/include/asm/kvm-ap.h        |    1 +
 drivers/s390/crypto/vfio_ap_ops.c     |  296 +++++++++++++++++++++++++++++++++
 drivers/s390/crypto/vfio_ap_private.h |    1 +
 3 files changed, 298 insertions(+), 0 deletions(-)
arch/s390/include/asm/kvm-ap.h +1 -0
index 6bd6bfb..f660aaf 100644
@@ -15,6 +15,7 @@
 #include <linux/bitops.h>
 
 #define KVM_AP_MASK_BYTES(n)(n / BITS_PER_BYTE)
+#define KVM_AP_MAX_APM_INDEX(matrix)(matrix->apm_max - 1)
 
 /**
  * The AP matrix is comprised of three bit masks identifying the adapters,
drivers/s390/crypto/vfio_ap_ops.c +296 -0

305 lines not on this page · raw message

drivers/s390/crypto/vfio_ap_private.h +1 -0
index 522564e..a92d5ad 100644
@@ -10,6 +10,7 @@
 
 #include <linux/types.h>
 #include <linux/mdev.h>
+#include <asm/kvm-ap.h>
 #include "ap_bus.h"
 
 #define VFIO_AP_MODULE_NAME "vfio_ap"
Signature · 1 line
1.7.1
#40 lore raw

PATCH v2 10/15 s390: vfio-ap: sysfs interfaces to configure domains

In reply to Tony Krowiak

Provides the sysfs interfaces for assigning AP domains to and unassigning AP domains from a mediated matrix device.

An AP domain ID corresponds to an AP queue index (APQI). For each domain assigned to the mediated matrix device, its corresponging APQI is stored in an AP queue mask (AQM). The bits in the AQM, from most significant to least significant bit, correspond to AP domain numbers 0 to 255. When a domain is assigned, the bit corresponding to its APQI will be set in the AQM. Likewise, when a domain is unassigned, the bit corresponding to its APQI will be cleared from the AQM.

The relevant sysfs structures are:

/sys/devices/vfio_ap
... [matrix]
...... [mdev_supported_types]
......... [vfio_ap-passthrough]
............ [devices]
...............[$uuid]
.................. assign_domain
.................. unassign_domain

To assign a domain to the $uuid mediated matrix device, write the domain's ID to the assign_domain file. To unassign a domain, write the domain's ID to the unassign_domain file. The ID is specified using conventional semantics: If it begins with 0x, the number will be parsed as a hexadecimal (case insensitive) number; otherwise, it will be parsed as a decimal number.

For example, to assign domain 173 (0xad) to the mediated matrix device $uuid:

	echo 173 > assign_domain

	or

	echo 0xad > assign_domain

To unassign domain 173 (0xad):

	echo 173 > unassign_domain

	or

	echo 0xad > unassign_domain

The assignment will be rejected:

* If the domain ID exceeds the maximum value for an AP domain:
* If the AP Extended Addressing (APXA) facility is installed, the max value is 255
* Else the max value is 15

* If no AP adapters have yet been assigned and there are no AP queues reserved by the VFIO AP driver that have an APQN with an APQI matching that of the AP domain number being assigned.

* If any of the APQNs that can be derived from the intersection of the APQI being assigned and the AP adapter ID (APID) of each of the AP adapters previously assigned can not be matched with an APQN of an AP queue device reserved by the VFIO AP driver.

---
 arch/s390/include/asm/kvm-ap.h    |    1 +
 drivers/s390/crypto/vfio_ap_ops.c |  215 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 215 insertions(+), 1 deletions(-)
arch/s390/include/asm/kvm-ap.h +1 -0
index f660aaf..191071f 100644
@@ -16,6 +16,7 @@
 
 #define KVM_AP_MASK_BYTES(n)(n / BITS_PER_BYTE)
 #define KVM_AP_MAX_APM_INDEX(matrix)(matrix->apm_max - 1)
+#define KVM_AP_MAX_AQM_INDEX(matrix)(matrix->aqm_max - 1)
 
 /**
  * The AP matrix is comprised of three bit masks identifying the adapters,
drivers/s390/crypto/vfio_ap_ops.c +214 -1

224 lines not on this page · raw message

Signature · 1 line
1.7.1
#41 lore raw

PATCH v2 11/15 s390: vfio-ap: sysfs interfaces to configure control domains

In reply to Tony Krowiak

Provides the sysfs interfaces for assigning AP control domains to and unassigning AP control domains from a mediated matrix device.

The IDs of the AP control domains assigned to the mediated matrix device are stored in an AP domain mask (ADM). The bits in the ADM, from most significant to least significant bit, correspond to AP domain numbers 0 to 255. When a control domain is assigned, the bit corresponding its domain ID will be set in the ADM. Likewise, when a domain is unassigned, the bit corresponding to its domain ID will be cleared in the ADM.

The relevant sysfs structures are:

/sys/devices/vfio_ap
... [matrix]
...... [mdev_supported_types]
......... [vfio_ap-passthrough]
............ [devices]
...............[$uuid]
.................. assign_control_domain
.................. unassign_control_domain

To assign a control domain to the $uuid mediated matrix device's ADM, write its domain number to the assign_control_domain file. To unassign a domain, write its domain number to the unassign_control_domain file. The domain number is specified using conventional semantics: If it begins with 0x the number will be parsed as a hexadecimal (case insensitive) number; otherwise, it will be parsed as a decimal number.

For example, to assign control domain 173 (0xad) to the mediated matrix device $uuid:

	echo 173 > assign_control_domain

	or

	echo 0xad > assign_control_domain

To unassign control domain 173 (0xad):

	echo 173 > unassign_control_domain

	or

	echo 0xad > unassign_control_domain

The assignment will be rejected if the APQI exceeds the maximum value for an AP domain:
* If the AP Extended Addressing (APXA) facility is installed, the max value is 255
* Else the max value is 15

---
 arch/s390/include/asm/kvm-ap.h    |    1 +
 drivers/s390/crypto/vfio_ap_ops.c |  107 +++++++++++++++++++++++++++++++++++++
 2 files changed, 108 insertions(+), 0 deletions(-)
arch/s390/include/asm/kvm-ap.h +1 -0
index 191071f..4171f4b 100644
@@ -17,6 +17,7 @@
 #define KVM_AP_MASK_BYTES(n)(n / BITS_PER_BYTE)
 #define KVM_AP_MAX_APM_INDEX(matrix)(matrix->apm_max - 1)
 #define KVM_AP_MAX_AQM_INDEX(matrix)(matrix->aqm_max - 1)
+#define KVM_AP_MAX_ADM_INDEX(matrix)(matrix->adm_max - 1)
 
 /**
  * The AP matrix is comprised of three bit masks identifying the adapters,
drivers/s390/crypto/vfio_ap_ops.c +107 -0

118 lines not on this page · raw message

Signature · 1 line
1.7.1
#42 lore raw

PATCH v2 12/15 s390: vfio-ap: sysfs interface to view matrix mdev matrix

In reply to Tony Krowiak

Provides a sysfs interface to view the AP matrix configured for the mediated matrix device.

The relevant sysfs structures are:

/sys/devices/vfio_ap
... [matrix]
...... [mdev_supported_types]
......... [vfio_ap-passthrough]
............ [devices]
...............[$uuid]
.................. matrix

To view the matrix configured for the mediated matrix device, print the matrix file:

cat matrix

---
 drivers/s390/crypto/vfio_ap_ops.c |   39 +++++++++++++++++++++++++++++++++++++
 1 files changed, 39 insertions(+), 0 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +39 -0
index 461d450..04f7a92 100644
@@ -692,6 +692,44 @@ static ssize_t control_domains_show(struct device *dev,
 }
 DEVICE_ATTR_RO(control_domains);
 
+static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
+			   char *buf)
+{
+	struct mdev_device *mdev = mdev_from_dev(dev);
+	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
+	char *bufpos = buf;
+	unsigned long apid;
+	unsigned long apqi;
+	int nchars = 0;
+	int n;
+
+	n = sprintf(bufpos, "ADAPTER.DOMAIN\n");
+	bufpos += n;
+	nchars += n;
+
+	n = sprintf(bufpos, "--------------\n");
+	bufpos += n;
+	nchars += n;
+
+	for_each_set_bit_inv(apid, matrix_mdev->matrix->apm,
+			     matrix_mdev->matrix->apm_max) {
+		n = sprintf(bufpos, "%02lx\n", apid);
+		bufpos += n;
+		nchars += n;
+
+		for_each_set_bit_inv(apqi, matrix_mdev->matrix->aqm,
+				     matrix_mdev->matrix->aqm_max) {
+			n = sprintf(bufpos, "%02lx.%04lx\n", apid, apqi);
+			bufpos += n;
+			nchars += n;
+		}
+	}
+
+	return nchars;
+}
+DEVICE_ATTR_RO(matrix);
+
+
 static struct attribute *vfio_ap_mdev_attrs[] = {
 	&dev_attr_assign_adapter.attr,
 	&dev_attr_unassign_adapter.attr,
@@ -700,6 +738,7 @@ static ssize_t control_domains_show(struct device *dev,
 	&dev_attr_assign_control_domain.attr,
 	&dev_attr_unassign_control_domain.attr,
 	&dev_attr_control_domains.attr,
+	&dev_attr_matrix.attr,
 	NULL,
 };
 
Signature · 1 line
1.7.1
#43 lore raw

PATCH v2 13/15 KVM: s390: Configure the guest's CRYCB

In reply to Tony Krowiak

Registers a group notifier during the open of the mediated matrix device to get information on KVM presence through the VFIO_GROUP_NOTIFY_SET_KVM event. When notified, the pointer to the kvm structure is saved inside the mediated matrix device. Once the VFIO AP device driver has access to KVM, the AP matrix for the guest can be configured.

Guest access to AP adapters, usage domains and control domains is controlled by three bit masks referenced from the Crypto Control Block (CRYCB) referenced from the guest's SIE state description:

* The AP Mask (APM) controls access to the AP adapters. Each bit in the APM represents an adapter number - from most significant to least significant bit - from 0 to 255. The bits in the APM are set according to the adapter numbers assigned to the mediated matrix device via its 'assign_adapter' sysfs attribute file.

* The AP Queue (AQM) controls access to the AP queues. Each bit in the AQM represents an AP queue index - from most significant to least significant bit - from 0 to 255. A queue index references a specific domain and is synonymous with the domian number. The bits in the AQM are set according to the domain numbers assigned to the mediated matrix device via its 'assign_domain' sysfs attribute file.

* The AP Domain Mask (ADM) controls access to the AP control domains. Each bit in the ADM represents a control domain - from most significant to least significant bit - from 0-255. The bits in the ADM are set according to the domain numbers assigned to the mediated matrix device via its 'assign_control_domain' sysfs attribute file.

---
 drivers/s390/crypto/vfio_ap_ops.c     |   46 +++++++++++++++++++++++++++++++++
 drivers/s390/crypto/vfio_ap_private.h |    2 +
 2 files changed, 48 insertions(+), 0 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +46 -0
index 04f7a92..752d171 100644
@@ -53,6 +53,50 @@ static int vfio_ap_mdev_remove(struct mdev_device *mdev)
 	return 0;
 }
 
+static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
+				       unsigned long action, void *data)
+{
+	struct ap_matrix_mdev *matrix_mdev;
+
+	if (action == VFIO_GROUP_NOTIFY_SET_KVM) {
+		matrix_mdev = container_of(nb, struct ap_matrix_mdev,
+					   group_notifier);
+		matrix_mdev->kvm = data;
+	}
+
+	return NOTIFY_OK;
+}
+
+static int vfio_ap_mdev_open(struct mdev_device *mdev)
+{
+	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
+	unsigned long events;
+	int ret;
+
+	matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
+	events = VFIO_GROUP_NOTIFY_SET_KVM;
+	ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
+				     &events, &matrix_mdev->group_notifier);
+
+	ret = kvm_ap_configure_matrix(matrix_mdev->kvm,
+				      matrix_mdev->matrix);
+	if (ret)
+		return ret;
+
+	ret = kvm_ap_enable_ie_mode(matrix_mdev->kvm);
+
+	return ret;
+}
+
+static void vfio_ap_mdev_release(struct mdev_device *mdev)
+{
+	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
+
+	kvm_ap_deconfigure_matrix(matrix_mdev->kvm);
+	vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
+				 &matrix_mdev->group_notifier);
+}
+
 static ssize_t name_show(struct kobject *kobj, struct device *dev, char *buf)
 {
 	return sprintf(buf, "%s\n", VFIO_AP_MDEV_NAME_HWVIRT);
@@ -757,6 +801,8 @@ static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
 	.mdev_attr_groups	= vfio_ap_mdev_attr_groups,
 	.create			= vfio_ap_mdev_create,
 	.remove			= vfio_ap_mdev_remove,
+	.open			= vfio_ap_mdev_open,
+	.release		= vfio_ap_mdev_release,
 };
 
 int vfio_ap_mdev_register(struct ap_matrix *ap_matrix)
drivers/s390/crypto/vfio_ap_private.h +2 -0
index a92d5ad..1bd3d42 100644
@@ -30,6 +30,8 @@ struct ap_matrix {
 
 struct ap_matrix_mdev {
 	struct kvm_ap_matrix *matrix;
+	struct notifier_block group_notifier;
+	struct kvm *kvm;
 };
 
 static inline struct ap_matrix *to_ap_matrix(struct device *dev)
Signature · 1 line
1.7.1
#44 lore raw

Re: [PATCH v2 13/15] KVM: s390: Configure the guest's CRYCB

Quoted 8 lines
drivers/s390/crypto/vfio_ap_ops.c +7 -0
+static int vfio_ap_mdev_open(struct mdev_device *mdev)
+{
+	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
+	unsigned long events;
+	int ret;
+
+	matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;

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

Quoted 10 lines
drivers/s390/crypto/vfio_ap_ops.c +10 -0
+	events = VFIO_GROUP_NOTIFY_SET_KVM;
+	ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
+				     &events, &matrix_mdev->group_notifier);
+
+	ret = kvm_ap_configure_matrix(matrix_mdev->kvm,
+				      matrix_mdev->matrix);
+	if (ret)
+		return ret;
+
+	ret = kvm_ap_enable_ie_mode(matrix_mdev->kvm);

Can't this happen while the guest is already running? Or what hinders us from doing that?

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

Quoted 5 lines
drivers/s390/crypto/vfio_ap_ops.c +5 -0
+
+	return ret;
+}
+
+static void vfio_ap_mdev_release(struct mdev_device *mdev)

Thanks,

David / dhildenb

#45 lore raw

Re: [PATCH v2 13/15] KVM: s390: Configure the guest's CRYCB

Quoted 11 lines · David Hildenbrand
Quoted 11 lines
drivers/s390/crypto/vfio_ap_ops.c +11 -0
+static int vfio_ap_mdev_open(struct mdev_device *mdev)
+{
+	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
+	unsigned long events;
+	int ret;
+
+	matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
+	events = VFIO_GROUP_NOTIFY_SET_KVM;
+	ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
+				     &events, &matrix_mdev->group_notifier);
+
Quoted 8 lines

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

Quoted 6 lines
drivers/s390/crypto/vfio_ap_ops.c +6 -0
+	ret = kvm_ap_configure_matrix(matrix_mdev->kvm,
+				      matrix_mdev->matrix);
+	if (ret)
+		return ret;
+
+	ret = kvm_ap_enable_ie_mode(matrix_mdev->kvm);

Can't this happen while the guest is already running? Or what hinders us from doing that?

I'm not sure exactly what you're asking here. Are you asking if the vfio_ap_mdev_open() function can be called multiple times while the guest is running? AFAIK this will be called only once when the mediated device's file descriptor is opened. This happens in QEMU when the -device vfio-ap device is realized.

Quoted 10 lines

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

Quoted 6 lines
drivers/s390/crypto/vfio_ap_ops.c +5 -0
+
+	return ret;
+}
+
+static void vfio_ap_mdev_release(struct mdev_device *mdev)

Thanks,

David / dhildenb

#46 lore raw

Re: [PATCH v2 13/15] KVM: s390: Configure the guest's CRYCB

Quoted 20 lines · Tony Krowiak
Quoted 11 lines · David Hildenbrand
Quoted 11 lines
drivers/s390/crypto/vfio_ap_ops.c +11 -0
+static int vfio_ap_mdev_open(struct mdev_device *mdev)
+{
+	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
+	unsigned long events;
+	int ret;
+
+	matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
+	events = VFIO_GROUP_NOTIFY_SET_KVM;
+	ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
+				     &events, &matrix_mdev->group_notifier);
+
Quoted 8 lines

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

Quoted 6 lines
drivers/s390/crypto/vfio_ap_ops.c +6 -0
+	ret = kvm_ap_configure_matrix(matrix_mdev->kvm,
+				      matrix_mdev->matrix);
+	if (ret)
+		return ret;
+
+	ret = kvm_ap_enable_ie_mode(matrix_mdev->kvm);

Can't this happen while the guest is already running? Or what hinders us from doing that?

Quoted 7 lines

I'm not sure exactly what you're asking here. Are you asking if the vfio_ap_mdev_open()
function can be called multiple times while the guest is running? AFAIK this will be
called only once when the mediated device's file descriptor is opened. This happens in
QEMU when the -device vfio-ap device is realized.

Okay, but from a pure interface point of view, this could happen any time, even while the guest is already running. Patching in the SCB of a running VCPU is evil.

But I guess we don't have to worry about that when changing they way we set ECA_APIE, as described in the other mail.

Signature · 3 lines
Thanks,

David / dhildenb
#47 lore raw

Re: [PATCH v2 13/15] KVM: s390: Configure the guest's CRYCB

Quoted 28 lines · David Hildenbrand
Quoted 20 lines · Tony Krowiak
Quoted 11 lines · David Hildenbrand
Quoted 11 lines
drivers/s390/crypto/vfio_ap_ops.c +11 -0
+static int vfio_ap_mdev_open(struct mdev_device *mdev)
+{
+	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
+	unsigned long events;
+	int ret;
+
+	matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
+	events = VFIO_GROUP_NOTIFY_SET_KVM;
+	ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
+				     &events, &matrix_mdev->group_notifier);
+
Quoted 8 lines

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

Quoted 6 lines
drivers/s390/crypto/vfio_ap_ops.c +6 -0
+	ret = kvm_ap_configure_matrix(matrix_mdev->kvm,
+				      matrix_mdev->matrix);
+	if (ret)
+		return ret;
+
+	ret = kvm_ap_enable_ie_mode(matrix_mdev->kvm);

Can't this happen while the guest is already running? Or what hinders us from doing that?

Quoted 7 lines

I'm not sure exactly what you're asking here. Are you asking if the vfio_ap_mdev_open()
function can be called multiple times while the guest is running? AFAIK this will be
called only once when the mediated device's file descriptor is opened. This happens in
QEMU when the -device vfio-ap device is realized.

Quoted 3 lines

Okay, but from a pure interface point of view, this could happen any time, even while the guest is already running. Patching in the SCB of a running VCPU is evil.

How can this happen while the guest is running? QEMU opens the fd when the device is realized and AFAIK vfio mdev will not allow any other process to open it until the guest is terminated. What am I missing?

Quoted 4 lines

But I guess we don't have to worry about that when changing they way we set ECA_APIE, as described in the other mail.

#48 lore raw

Re: [PATCH v2 13/15] KVM: s390: Configure the guest's CRYCB

Quoted 29 lines · Tony Krowiak
Quoted 28 lines · David Hildenbrand
Quoted 27 lines · Tony Krowiak
Quoted 11 lines · David Hildenbrand
Quoted 11 lines
drivers/s390/crypto/vfio_ap_ops.c +11 -0
+static int vfio_ap_mdev_open(struct mdev_device *mdev)
+{
+	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
+	unsigned long events;
+	int ret;
+
+	matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
+	events = VFIO_GROUP_NOTIFY_SET_KVM;
+	ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
+				     &events, &matrix_mdev->group_notifier);
+
Quoted 8 lines

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

Quoted 6 lines
drivers/s390/crypto/vfio_ap_ops.c +6 -0
+	ret = kvm_ap_configure_matrix(matrix_mdev->kvm,
+				      matrix_mdev->matrix);
+	if (ret)
+		return ret;
+
+	ret = kvm_ap_enable_ie_mode(matrix_mdev->kvm);

Can't this happen while the guest is already running? Or what hinders us from doing that?

I'm not sure exactly what you're asking here. Are you asking if the vfio_ap_mdev_open()
function can be called multiple times while the guest is running? AFAIK this will be
called only once when the mediated device's file descriptor is opened. This happens in
QEMU when the -device vfio-ap device is realized.

Quoted 6 lines
Quoted 3 lines

Okay, but from a pure interface point of view, this could happen any time, even while the guest is already running. Patching in the SCB of a running VCPU is evil.

How can this happen while the guest is running? QEMU opens the fd when the device is realized and AFAIK vfio mdev will not allow any other process to open it until the guest is terminated. What am I missing?

It can't happen right now (the way QEMU uses it), but the kernel interface allows it, no?

Anyhow, as discussed this should be handled directly while creating a VCPU. Then also CPU hotplug is properly covered.

Signature · 3 lines
Thanks,

David / dhildenb
#49 lore raw

Re: [PATCH v2 13/15] KVM: s390: Configure the guest's CRYCB

Quoted 33 lines · David Hildenbrand
Quoted 32 lines · Tony Krowiak
Quoted 31 lines · David Hildenbrand
Quoted 20 lines · Tony Krowiak
Quoted 11 lines · David Hildenbrand
Quoted 11 lines
drivers/s390/crypto/vfio_ap_ops.c +11 -0
+static int vfio_ap_mdev_open(struct mdev_device *mdev)
+{
+	struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
+	unsigned long events;
+	int ret;
+
+	matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
+	events = VFIO_GROUP_NOTIFY_SET_KVM;
+	ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
+				     &events, &matrix_mdev->group_notifier);
+
Quoted 8 lines

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

Quoted 6 lines
drivers/s390/crypto/vfio_ap_ops.c +6 -0
+	ret = kvm_ap_configure_matrix(matrix_mdev->kvm,
+				      matrix_mdev->matrix);
+	if (ret)
+		return ret;
+
+	ret = kvm_ap_enable_ie_mode(matrix_mdev->kvm);

Can't this happen while the guest is already running? Or what hinders us from doing that?

Quoted 7 lines

I'm not sure exactly what you're asking here. Are you asking if the vfio_ap_mdev_open()
function can be called multiple times while the guest is running? AFAIK this will be
called only once when the mediated device's file descriptor is opened. This happens in
QEMU when the -device vfio-ap device is realized.

Okay, but from a pure interface point of view, this could happen any time, even while the guest is already running. Patching in the SCB of a running VCPU is evil.

Quoted 8 lines
Quoted 3 lines

How can this happen while the guest is running? QEMU opens the fd when the device is realized and AFAIK vfio mdev will not allow any other process to open it until the guest is terminated. What am I missing?

It can't happen right now (the way QEMU uses it), but the kernel interface allows it, no?

Anyhow, as discussed this should be handled directly while creating a VCPU. Then also CPU hotplug is properly covered.

Here is what I think we should do:

* Set ECA.28 in the VCPU setup function based on whether the CPU model feature has been turned on by user space as you suggest.

* Replace the kvm_ap_enable_ie_mode() call in the open() above with a query of the CPU model feature and return an error if it is not turned on.

Does this sound reasonable?

Would it be more appropriate in this case to rename the feature to KVM_S390_VM_CPU_FEAT_APIE?

Quoted 2 lines
#50 lore raw

PATCH v2 14/15 s390: vfio-ap: implement VFIO_DEVICE_GET_INFO ioctl

In reply to Tony Krowiak

Introduces ioctl access to the VFIO AP Matrix device driver by implementing the VFIO_DEVICE_GET_INFO ioctl. This ioctl provides the VFIO AP Matrix device driver information to the guest machine.

---
 drivers/s390/crypto/vfio_ap_ops.c |   43 +++++++++++++++++++++++++++++++++++++
 1 files changed, 43 insertions(+), 0 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +43 -0
index 752d171..49c119d 100644
@@ -97,6 +97,48 @@ static void vfio_ap_mdev_release(struct mdev_device *mdev)
 				 &matrix_mdev->group_notifier);
 }
 
+static int vfio_ap_mdev_get_device_info(unsigned long arg)
+{
+	unsigned long minsz;
+	struct vfio_device_info info;
+
+	minsz = offsetofend(struct vfio_device_info, num_irqs);
+
+	if (copy_from_user(&info, (void __user *)arg, minsz))
+		return -EFAULT;
+
+	if (info.argsz < minsz) {
+		pr_err("%s: Argument size %u less than min size %li",
+		       VFIO_AP_MATRIX_MODULE_NAME, info.argsz, minsz);
+		return -EINVAL;
+	}
+
+	info.flags = VFIO_DEVICE_FLAGS_AP;
+	info.num_regions = 0;
+	info.num_irqs = 0;
+
+	return copy_to_user((void __user *)arg, &info, minsz);
+}
+
+static ssize_t vfio_ap_mdev_ioctl(struct mdev_device *mdev,
+				    unsigned int cmd, unsigned long arg)
+{
+	int ret;
+
+	switch (cmd) {
+	case VFIO_DEVICE_GET_INFO:
+		ret = vfio_ap_mdev_get_device_info(arg);
+		break;
+	default:
+		pr_err("%s: ioctl command %d is not a supported command",
+		       VFIO_AP_MATRIX_MODULE_NAME, cmd);
+		ret = -EOPNOTSUPP;
+		break;
+	}
+
+	return ret;
+}
+
 static ssize_t name_show(struct kobject *kobj, struct device *dev, char *buf)
 {
 	return sprintf(buf, "%s\n", VFIO_AP_MDEV_NAME_HWVIRT);
@@ -803,6 +845,7 @@ static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
 	.remove			= vfio_ap_mdev_remove,
 	.open			= vfio_ap_mdev_open,
 	.release		= vfio_ap_mdev_release,
+	.ioctl			= vfio_ap_mdev_ioctl,
 };
 
 int vfio_ap_mdev_register(struct ap_matrix *ap_matrix)
Signature · 1 line
1.7.1
#51 lore raw

PATCH v2 15/15 s390: doc: detailed specifications for AP virtualization

In reply to Tony Krowiak

This patch provides documentation describing the AP architecture and design concepts behind the virtualization of AP devices. It also includes an example of how to configure AP devices for exclusive use of KVM guests.

---
 Documentation/s390/vfio-ap.txt |  514 ++++++++++++++++++++++++++++++++++++++++
 MAINTAINERS                    |    1 +
 2 files changed, 515 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/s390/vfio-ap.txt
Documentation/s390/vfio-ap.txt new file +514 -0
new file mode 100644
index 0000000..c599f30
@@ -0,0 +1,514 @@
+Introduction:
+============
+The Adjunct Processor (AP) facility is an IBM Z cryptographic facility comprised
+of three AP instructions and from 1 up to 256 PCIe cryptographic adapter cards.
+The AP devices provide cryptographic functions to all CPUs assigned to a
+linux system running in an IBM Z system LPAR.
+
+The AP adapter cards are exposed via the AP bus. The motivation for vfio-ap
+is to make AP cards available to KVM guests using the VFIO mediated device
+framework.
+
+AP Architectural Overview:
+=========================
+To facilitate the comprehension of the design, let's start with some
+definitions:
+
+* AP adapter
+
+  An AP adapter is an IBM Z adapter card that can perform cryptographic
+  functions. There can be from 0 to 256 adapters assigned to an LPAR. Adapters
+  assigned to the LPAR in which a linux host is running will be available to
+  the linux host. Each adapter is identified by a number from 0 to 255. When
+  installed, an AP adapter is accessed by AP instructions executed by any CPU.
+
+* AP domain
+
+  An adapter is partitioned into domains. Each domain can be thought of as
+  a set of hardware registers for processing AP instructions. An adapter can
+  hold up to 256 domains. Each domain is identified by a number from 0 to 255.
+  Domains can be further classified into two types:
+
+    * Usage domains are domains that can be accessed directly to process AP
+      commands
+
+    * Control domains are domains that are accessed indirectly by AP
+      commands sent to a usage domain to control or change the domain, for
+      example; to set a secure private key for the domain.
+
+* AP Queue
+
+  An AP queue is the means by which an AP command-request message is sent to an
+  AP usage domain inside a specific AP. An AP queue is identified by a tuple
+  comprised of an AP adapter ID (APID) and an AP queue index (APQI). The
+  APQI corresponds to a given usage domain number within the adapter. This tuple
+  forms an AP Queue Number (APQN) uniquely identifying an AP queue. AP
+  instructions include a field containing the APQN to identify the AP queue to
+  which the AP command-request message is to be sent for processing.
+
+* AP Instructions:
+
+  There are three AP instructions:
+
+  * NQAP: to enqueue an AP command-request message to a queue
+  * DQAP: to dequeue an AP command-reply message from a queue
+  * PQAP: to administer the queues
+
+Start Interpretive Execution (SIE) Instruction:
+==============================================
+Let's now see how AP instructions are interpreted by the hardware.
+
+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 contains a field that references
+a Crypto Control Block (CRYCB). The CRYCB contains three fields to identify the
+adapters, usage domains and control domains assigned to the KVM guest:
+
+* The AP Mask (APM) field is a bit mask that identifies the AP adapters assigned
+  to the KVM guest. Each bit in the mask, from most significant to least
+  significant bit, corresponds to an APID from 0-255. If a bit is set, the
+  corresponding adapter is valid for use by the KVM guest.
+
+* The AP Queue Mask (AQM) field is a bit mask identifying the AP queues assigned
+  to the KVM guest. Each bit in the mask, from most significant to least
+  significant bit, corresponds to an AP queue index (APQI) from 0-255. If a bit
+  is set, the corresponding queue is valid for use by the KVM guest.
+
+* The AP Domain Mask field is a bit mask that identifies the AP control domains
+  assigned to the KVM guest. The ADM bit mask controls which domains can be
+  changed by an AP command-request message sent to a usage domain from the
+  guest. Each bit in the mask, from least significant to most significant bit,
+  corresponds to a domain from 0-255. If a bit is set, the corresponding domain
+  can be modified by an AP command-request message sent to a usage domain
+  configured for the KVM guest.
+
+If you recall from the description of an AP Queue, AP instructions include
+an APQN to identify the AP adapter and AP queue to which an AP command-request
+message is to be sent (NQAP and PQAP instructions), or from which a
+command-reply message is to be received (DQAP instruction). The validity of an
+APQN is defined by the matrix calculated from the APM and AQM; it is the
+intersection of all assigned adapter numbers (APM) with all assigned queue
+indexes (AQM). For example, if adapters 1 and 2 and usage domains 5 and 6 are
+assigned to a guest, the APQNs (1,5), (1,6), (2,5) and (2,6) will be valid for
+the guest.
+
+The APQNs provide secure key functionality - i.e., a private key is stored on
+the adapter card for each of its domains - so each APQN must be assigned to at
+most one guest or the linux host.
+
+   Example 1: Valid configuration:
+   ------------------------------
+   Guest1: adapters 1,2  domains 5,6
+   Guest2: adapter  1,2  domain 7
+
+   This is valid because both guests have a unique set of APQNs: Guest1 has
+   APQNs (1,5), (1,6), (2,5) and (2,6); Guest2 has APQNs (1,7) and (2,7).
+
+   Example 2: Invalid configuration:
+   --------------------------------is assigned by writing the adapter's number into the
+   Guest1: adapters 1,2  domains 5,6
+   Guest2: adapter  1    domains 6,7
+
+   This is an invalid configuration because both guests have access to
+   APQN (1,6).
+
+The Design:
+===========
+The design introduces three new objects:
+
+1. AP matrix device
+2. VFIO AP device driver (vfio_ap.ko)
+3. AP mediated matrix passthrough device
+
+The VFIO AP device driver
+-------------------------
+The VFIO AP (vfio_ap) device driver serves the following purposes:
+
+1. Provides the interfaces to reserve APQNs for exclusive use of KVM guests.
+
+2. Sets up the VFIO mediated device interfaces to manage the mediated matrix
+   device and create the sysfs interfaces for assigning adapters, usage domains,
+   and control domains comprising the matrix for a KVM guest.
+
+3. Configure the APM, AQM and ADM in the CRYCB referenced by a KVM guest's
+   SIE state description to grant the guest access to AP devices
+
+4. Initialize the CPU model feature indicating that a KVM guest may use
+   AP facilities installed on the linux host.
+
+5. Enable interpretive execution mode for the KVM guest.
+
+Reserve APQNs for exclusive use of KVM guests
+---------------------------------------------
+The following block diagram illustrates the mechanism by which APQNs are
+reserved:
+
+                              +------------------+
+                 remove       |                  |   unbind
+         +------------------->+ cex4queue driver +<-----------+
+         |                    |                  |            |
+         |                    +------------------+            |
+         |                                                    |
+         |                                                    |
+         |                                                    |
++--------+---------+ register +------------------+      +-----+------+
+|                  +<---------+                  | bind |            |
+|      ap_bus      |          |  vfio_ap driver  +<-----+    admin   |
+|                  +--------->+                  |      |            |
++------------------+  probe   +---+--------+-----+      +------------+
+                                  |        |
+                           create |        | store APQN
+                                  |        |
+                                  v        v
+                              +---+--------+-----+
+                              |                  |
+                              |  matrix device   |
+                              |                  |
+                              +------------------+
+
+The process for reserving an AP queue for use by a KVM guest is:
+
+* The vfio-ap driver during its initialization will perform the following:
+  * Create the 'vfio_ap' root device - /sys/devices/vfio_ap
+  * Create the 'matrix' device in the 'vfio_ap' root
+  * Register the matrix device with the device core
+* Register with the ap_bus for AP queue devices of type CEX4, CEX5 and
+  CEX6 and to provide the vfio_ap driver's probe and remove callback interfaces.
+* The admin unbinds queue cc.qqqq from the cex4queue device driver. This results
+  in the ap_bus calling the the device driver's remove interface which
+  unbinds the cc.qqqq queue device from the driver.
+* The admin binds the cc.qqqq queue to the vfio_ap device driver. This results
+  in the ap_bus calling the device vfio_ap driver's probe interface to bind
+  queue cc.qqqq to the driver. The vfio_ap device driver will store the APQN for
+  the queue in the matrix device
+
+Set up the VFIO mediated device interfaces
+------------------------------------------
+The VFIO AP device driver utilizes the common interface of the VFIO mediated
+device core driver to:
+* Register an AP mediated bus driver to add a mediated matrix device to and
+  remove it from a VFIO group.
+* Create and destroy a mediated matrix device
+* Add a mediated matrix device to and remove it from the AP mediated bus driver
+* Add a mediated matrix device to and remove it from an IOMMU group
+
+The following high-level block diagram shows the main components and interfaces
+of the VFIO AP mediated matrix device driver:
+
+ +-------------+
+ |             |
+ | +---------+ | mdev_register_driver() +--------------+
+ | |  Mdev   | +<-----------------------+              |
+ | |  bus    | |                        | vfio_mdev.ko |
+ | | driver  | +----------------------->+              |<-> VFIO user
+ | +---------+ |    probe()/remove()    +--------------+    APIs
+ |             |
+ |  MDEV CORE  |
+ |   MODULE    |
+ |   mdev.ko   |
+ | +---------+ | mdev_register_device() +--------------+
+ | |Physical | +<-----------------------+              |
+ | | device  | |                        |  vfio_ap.ko  |<-> matrix
+ | |interface| +----------------------->+              |    device
+ | +---------+ |       callback         +--------------+
+ +-------------+
+
+During initialization of the vfio_ap module, the matrix device is registered
+with an 'mdev_parent_ops' structure that provides the sysfs attribute
+structures, mdev functions and callback interfaces for managing the mediated
+matrix device.
+
+* sysfs attribute structures:
+  * supported_type_groups
+    The VFIO mediated device framework supports creation of user-defined
+    mediated device types. These mediated device types are specified
+    via the 'supported_type_groups' structure when a device is registered
+    with the mediated device framework. The registration process creates the
+    sysfs structures for each mediated device type specified in the
+    'mdev_supported_types' sub-directory of the device being registered. Along
+    with the device type, the sysfs attributes of the mediated device type are
+    provided.
+
+    The VFIO AP device driver will register one mediated device type for
+    passthrough devices:
+        /sys/devices/vfio_ap/mdev_supported_types/vfio_ap-passthrough
+    Only the three read-only attributes required by the VFIO mdev framework will
+    be provided:
+        /sys/devices/vfio_ap/mdev_supported_types
+        ... name
+        ... device_api
+        ... available_instances
+        Where:
+        * name: specifies the name of the mediated device type
+        * device_api: the mediated device type's API
+        * available_instances: the number of mediated matrix passthrough devices
+                               that can be created
+  * mdev_attr_groups
+    This attribute group identifies the user-defined sysfs attributes of the
+    mediated device. When a device is registered with the VFIO mediated device
+    framework, the sysfs attributes files identified in the 'mdev_attr_groups'
+    structure will be created in the mediated matrix device's directory. The
+    sysfs attributes for a mediated matrix device are:
+    * assign_adapter:
+      A write-only file for assigning an AP adapter to the mediated matrix
+      device. To assign an adapter, the APID of the adapter is written to the
+      file.
+    * assign_domain:
+      A write-only file for assigning an AP usage domain to the mediated matrix
+      device. To assign a domain, the APQI of the AP queue corresponding to a
+      usage domain is written to the file.
+    * assign_control_domain:
+      A write-only file for assigning an AP control domain to the mediated
+      matrix device. To assign a control domain, the ID of a domain to be
+      controlled is written to the file. By architectural convention, the set of
+      control domains will always include the set of usage domains, so it is
+      only necessary to assign control domains that are not also assigned as
+      usage domains.
+
+* functions:
+  * create:
+    allocates the ap_matrix_mdev structure used by the vfio_ap driver to:
+    * Keep track of the available instances
+    * Store the reference to the struct kvm for the KVM guest
+    * Provide the notifier callback that will get invoked to handle the
+      VFIO_GROUP_NOTIFY_SET_KVM event. When received, the vfio_ap driver will
+      store the reference in the mediated matrix device's ap_matrix_mdev
+      structure and enable the interpretive execution mode for the KVM guest.
+  * remove:
+    deallocates the mediated matrix device's ap_matrix_mdev structure.
+
+* callback interfaces
+  * open:
+    The vfio_ap driver uses this callback to register a
+    VFIO_GROUP_NOTIFY_SET_KVM notifier callback function for the mdev matrix
+    device. The notifier is invoked when QEMU connects the VFIO iommu group
+    for the mdev matrix device to the MDEV bus. Access to the KVM structure used
+    to set up the KVM guest is provided via this callback.
+  * release:
+    unregisters the VFIO_GROUP_NOTIFY_SET_KVM notifier callback function for the
+    mdev matrix device.
+
+Configure the APM, AQM and ADM in the CRYCB:
+-------------------------------------------
+Configuring the AP matrix for a KVM guest will be performed when the
+VFIO_GROUP_NOTIFY_SET_KVM notifier callback is invoked. The notifier is callback
+function is called when QEMU connects the VFIO iommu group for the mdev matrix
+device to the MDEV bus. The CRYCB is configured by:
+* Setting the bits in the APM corresponding to the APIDs assigned to the
+  mediated matrix device via its 'assign_adapter' interface.
+* Setting the bits in the AQM corresponding to the APQIs assigned to the
+  mediated matrix device via its 'assign_domain' interface.
+* Setting the bits in the ADM corresponding to the domain dIDs assigned to the
+  mediated matrix device via its 'assign_control_domains' interface.
+
+Initialize the CPU model feature for AP
+---------------------------------------
+This design exploits a feature of the SIE architecture called interpretive
+execution (IE). When IE is enabled for a KVM guest, the AP instructions
+executed in the guest will be interpreted by the firmware and the commands
+contained therein will be passed directly through to an AP device assigned to
+the linux host. In order to enable interpretive execution for a KVM guest, SIE
+must have access to the AP facilities installed on the linux host. A new CPU
+model feature is introduced by this design to indicate that the guest will
+directly access the host AP facilities. This feature will be enabled by the
+kernel only if the AP facilities are installed on the linux host. This feature
+is turned on for the guest via the qemu command line:
+
+    /usr/bin/qemu-system-s390x ... -cpu xxx,ap=on
+
+    Where xxx is the CPU model being used.
+
+If the CPU model feature is not enabled by the kernel, QEMU
+will fail and report that the feature is not supported.
+
+Example:
+=======
+Let's now provide an example to illustrate how KVM guests may be given
+access to AP facilities. For this example, we will show how to configure
+two guests such that executing the lszcrypt command on the guests would
+look like this:
+
+Guest1
+------
+CARD.DOMAIN TYPE  MODE
+------------------------------
+05          CEX5C CCA-Coproc
+05.0004     CEX5C CCA-Coproc
+05.00ab     CEX5C CCA-Coproc
+06          CEX5A Accelerator
+06.0004     CEX5A Accelerator
+06.00ab     CEX5C CCA-Coproc
+
+Guest2
+------
+CARD.DOMAIN TYPE  MODE
+------------------------------
+05          CEX5A Accelerator
+05.0047     CEX5A Accelerator
+05.00ff     CEX5A Accelerator
+
+These are the steps:
+
+1. Install the vfio_ap module on the linux host. The dependency chain for the
+   vfio_ap module is:
+   * vfio
+   * mdev
+   * vfio_mdev
+   * vfio_ap
+
+2. Secure the AP queues to be used by the two guests so that the host can not
+   access them. This is done by unbinding each AP Queue device from its
+   respective AP driver. In our example, these queues are bound to the cex4queue
+   driver. The sysfs location of these devices is:
+
+   /sys/bus/ap
+   --- [drivers]
+   ------ [cex4queue]
+   --------- [05.0004]
+   --------- [05.0047]
+   --------- [05.00ab]
+   --------- [05.00ff]
+   --------- [06.0004]
+   --------- [06.00ab]
+   --------- unbind
+
+   To unbind AP queue 05.0004 from the cex4queue device driver:
+
+	echo 05.0004 > unbind
+
+   This must also be done for AP queues 05.00ab, 05.0047, 05.00ff, 06.0004,
+   and 06.00ab.
+
+3. Reserve the queues for use by the two KVM guests. This is accomplished by
+   binding them to the vfio_ap device driver. The sysfs location of the
+   device driver is:
+
+   /sys/bus/ap
+   ---[drivers]
+   ------ [vfio_ap]
+   ---------- bind
+
+   To bind queue 05.0004 to the vfio_ap driver:
+
+	echo 05.0004 > bind
+
+   This must also be done for AP queues 05.00ab, 05.0047, 05.00ff, 06.0004,
+   and 06.00ab.
+
+   Take note that the AP queues bound to the vfio_ap driver will be available
+   for guest usage until they are unbound from the driver, the vfio_ap module
+   is unloaded, or the host system is shut down.
+
+4. Create the mediated devices needed to configure the AP matrixes for the
+   two guests and to provide an interface to the vfio_ap driver for
+   use by the guests:
+
+   /sys/devices/
+   --- [vfio_ap]
+   ------ [matrix] (this is the matrix device)
+   --------- [mdev_supported_types]
+   ------------ [vfio_ap-passthrough] (passthrough mediated matrix device type)
+   --------------- create
+   --------------- [devices]
+
+   To create the mediated devices for the two guests:
+
+	uuidgen > create
+	uuidgen > create
+
+   This will create two mediated devices in the [devices] subdirectory named
+   with the UUID written to the create attribute file. We call them $uuid1
+   and $uuid2:
+
+   /sys/devices/
+   --- [vfio_ap]
+   ------ [matrix]
+   --------- [mdev_supported_types]
+   ------------ [vfio_ap-passthrough]
+   --------------- [devices]
+   ------------------ [$uuid1]
+   --------------------- assign_adapter
+   --------------------- assign_control_domain
+   --------------------- assign_domain
+   --------------------- matrix
+   --------------------- unassign_adapter
+   --------------------- unassign_control_domain
+   --------------------- unassign_domain
+
+   ------------------ [$uuid2]
+   --------------------- assign_adapter
+   --------------------- assign_cTo assign an adapter, the APID of the adapter is written to the
+      file. ontrol_domain
+   --------------------- assign_domain
+   --------------------- matrix
+   --------------------- unassign_adapter
+   --------------------- unassign_control_domain
+   --------------------- unassign_domain
+
+5. The administrator now needs to configure the matrixes for mediated
+   devices $uuid1 (for Guest1) and $uuid2 (for Guest2).
+
+   This is how the matrix is configured for Guest1:
+
+   echo 5 > assign_adapter
+   echo 6 > assign_adapter
+   echo 4 > assign_domain
+   echo 0xab > assign_domain
+
+   By architectural convention, all usage domains - i.e., domains assigned
+   via the assign_domain attribute file - will also be configured in the ADM
+   field of the KVM guest's CRYCB, so there is no need to assign control
+   domains here unless you want to assign control domains that are not
+   assigned as usage domains.
+
+   If a mistake is made configuring an adapter, domain or control domain,
+   you can use the unassign_xxx files to unassign the adapter, domain or
+   control domain.
+
+   To display the matrix configuration for Guest1:
+
+   cat matrix
+
+   This is how the matrix is configured for Guest2:
+
+   echo 5 > assign_adapter
+   echo 0x47 > assign_domain
+   echo 0xff > assign_domain
+
+6. Start Guest1:
+
+   /usr/bin/qemu-system-s390x ... -cpu xxx,ap=on \
+      -device vfio-ap,sysfsdev=/sys/devices/vfio_ap/matrix/$uuid1 ...
+
+7. Start Guest2:
+
+   /usr/bin/qemu-system-s390x ... -cpu xxx,ap=on \
+      -device vfio-ap,sysfsdev=/sys/devices/vfio_ap/matrix/$uuid2 ...
+
+When the guest is shut down, the mediated matrix device may be removed.
+
+Using our example again, to remove the mediated matrix device $uuid1:
+
+   /sys/devices/
+      --- [vfio_ap]
+      ------ [matrix]
+      --------- [mdev_supported_types]
+      ------------ [vfio_ap-passthrough]
+      --------------- [devices]
+      ------------------ [$uuid1]
+      --------------------- remove
+
+   echo 1 > remove
+
+   This will remove all of the mdev matrix device's sysfs structures. To
+   recreate and reconfigure the mdev matrix device, all of the steps starting
+   with step 4 will have to be performed again.
+
+   It is not necessary to remove an mdev matrix device, but one may want to
+   remove it if no guest will use it during the lifetime of the linux host. If
+   the mdev matrix device is removed, one may want to unbind the AP queues the
+   guest was using from the vfio_ap device driver and bind them back to the
+   default driver. Alternatively, the AP queues can be configured for another
+   mdev matrix (i.e., guest). In either case, one must take care to change the
+   secure key configured for the domain to which the queue is connected.
\ No newline at end of file
MAINTAINERS +1 -0
index fb9e0d4..bfa94c8 100644
@@ -11887,6 +11887,7 @@ F:	arch/s390/kvm/kvm-ap.c
 F:	drivers/s390/crypto/vfio_ap_drv.c
 F:	drivers/s390/crypto/vfio_ap_private.h
 F:	drivers/s390/crypto/vfio_ap_ops.c
+F:	Documentation/s390/vfio-ap.txt
 
 S390 ZFCP DRIVER
 M:	Steffen Maier <[email protected]>
Signature · 1 line
1.7.1
#52 lore raw

Re: [PATCH v2 15/15] s390: doc: detailed specifications for AP virtualization

Quoted 18 lines · On Tue, 27 Feb 2018 09:28:13 -0500 Tony Krowiak <[email protected]>…

This patch provides documentation describing the AP architecture and design concepts behind the virtualization of AP devices. It also includes an example of how to configure AP devices for exclusive use of KVM guests.

---
 Documentation/s390/vfio-ap.txt |  514 ++++++++++++++++++++++++++++++++++++++++
 MAINTAINERS                    |    1 +
 2 files changed, 515 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/s390/vfio-ap.txt
Documentation/s390/vfio-ap.txt new file +0 -0
new file mode 100644
index 0000000..c599f30

Documentation/s390/vfio-ap.txt line 10

Quoted 10 lines
Documentation/s390/vfio-ap.txt new file +10 -0
@@ -0,0 +1,514 @@
+Introduction:
+============
+The Adjunct Processor (AP) facility is an IBM Z cryptographic facility comprised
+of three AP instructions and from 1 up to 256 PCIe cryptographic adapter cards.
+The AP devices provide cryptographic functions to all CPUs assigned to a
+linux system running in an IBM Z system LPAR.
+
+The AP adapter cards are exposed via the AP bus. The motivation for vfio-ap
+is to make AP cards available to KVM guests using the VFIO mediated device
+framework.

Maybe drop a sentence in here that this makes heavy usage of the s390 virtualization facilities, which do the heavy lifting?

Quoted 35 lines
Documentation/s390/vfio-ap.txt new file +35 -0
+
+AP Architectural Overview:
+=========================
+To facilitate the comprehension of the design, let's start with some
+definitions:
+
+* AP adapter
+
+  An AP adapter is an IBM Z adapter card that can perform cryptographic
+  functions. There can be from 0 to 256 adapters assigned to an LPAR. Adapters
+  assigned to the LPAR in which a linux host is running will be available to
+  the linux host. Each adapter is identified by a number from 0 to 255. When
+  installed, an AP adapter is accessed by AP instructions executed by any CPU.
+
+* AP domain
+
+  An adapter is partitioned into domains. Each domain can be thought of as
+  a set of hardware registers for processing AP instructions. An adapter can
+  hold up to 256 domains. Each domain is identified by a number from 0 to 255.
+  Domains can be further classified into two types:
+
+    * Usage domains are domains that can be accessed directly to process AP
+      commands
+
+    * Control domains are domains that are accessed indirectly by AP
+      commands sent to a usage domain to control or change the domain, for
+      example; to set a secure private key for the domain.
+
+* AP Queue
+
+  An AP queue is the means by which an AP command-request message is sent to an
+  AP usage domain inside a specific AP. An AP queue is identified by a tuple
+  comprised of an AP adapter ID (APID) and an AP queue index (APQI). The
+  APQI corresponds to a given usage domain number within the adapter. This tuple
+  forms an AP Queue Number (APQN) uniquely identifying an AP queue. AP

Documentation/s390/vfio-ap.txt line 55

Quoted 10 lines
Documentation/s390/vfio-ap.txt new file +10 -0
+  instructions include a field containing the APQN to identify the AP queue to
+  which the AP command-request message is to be sent for processing.
+
+* AP Instructions:
+
+  There are three AP instructions:
+
+  * NQAP: to enqueue an AP command-request message to a queue
+  * DQAP: to dequeue an AP command-reply message from a queue
+  * PQAP: to administer the queues

Do you also want to explain how these entities show up on the ap bus in Linux? It might make the explanations further down easier to understand. (Is there any document for the ap bus you could point to?)

Documentation/s390/vfio-ap.txt line 58

Quoted 3 lines
Documentation/s390/vfio-ap.txt new file +3 -0
+
+Start Interpretive Execution (SIE) Instruction:
+==============================================

Call this "AP and SIE" or so? You're not trying to explain the whole SIE architecture :)

Quoted 1 line
Documentation/s390/vfio-ap.txt new file +1 -0
+Let's now see how AP instructions are interpreted by the hardware.

(...)

Quoted 18 lines
Documentation/s390/vfio-ap.txt new file +18 -0
+Reserve APQNs for exclusive use of KVM guests
+---------------------------------------------
+The following block diagram illustrates the mechanism by which APQNs are
+reserved:
+
+                              +------------------+
+                 remove       |                  |   unbind
+         +------------------->+ cex4queue driver +<-----------+
+         |                    |                  |            |
+         |                    +------------------+            |
+         |                                                    |
+         |                                                    |
+         |                                                    |
++--------+---------+ register +------------------+      +-----+------+
+|                  +<---------+                  | bind |            |
+|      ap_bus      |          |  vfio_ap driver  +<-----+    admin   |
+|                  +--------->+                  |      |            |
++------------------+  probe   +---+--------+-----+      +------------+

Documentation/s390/vfio-ap.txt line 169

Quoted 10 lines
Documentation/s390/vfio-ap.txt new file +10 -0
+                                  |        |
+                           create |        | store APQN
+                                  |        |
+                                  v        v
+                              +---+--------+-----+
+                              |                  |
+                              |  matrix device   |
+                              |                  |
+                              +------------------+
+

Thank you for including diagrams, these are really helpful.

(...)

Quoted 9 lines
Documentation/s390/vfio-ap.txt new file +9 -0
+Initialize the CPU model feature for AP
+---------------------------------------
+This design exploits a feature of the SIE architecture called interpretive
+execution (IE). When IE is enabled for a KVM guest, the AP instructions
+executed in the guest will be interpreted by the firmware and the commands
+contained therein will be passed directly through to an AP device assigned to
+the linux host. In order to enable interpretive execution for a KVM guest, SIE
+must have access to the AP facilities installed on the linux host. A new CPU
+model feature is introduced by this design to indicate that the guest will

Documentation/s390/vfio-ap.txt line 323

Quoted 10 lines
Documentation/s390/vfio-ap.txt new file +10 -0
+directly access the host AP facilities. This feature will be enabled by the
+kernel only if the AP facilities are installed on the linux host. This feature
+is turned on for the guest via the qemu command line:
+
+    /usr/bin/qemu-system-s390x ... -cpu xxx,ap=on
+
+    Where xxx is the CPU model being used.
+
+If the CPU model feature is not enabled by the kernel, QEMU
+will fail and report that the feature is not supported.

The cpu model interface is supposed to be user space agnostic, although it is only used by QEMU in practice. Mark this as an example more clearly?

(...)

I have not looked at this in detail (will probably come back to this later), but this looks like a useful document.

#53 lore raw

Re: [PATCH v2 15/15] s390: doc: detailed specifications for AP virtualization

Quoted 25 lines · Cornelia Huck
Quoted 22 lines · On Tue, 27 Feb 2018 09:28:13 -0500 Tony Krowiak <[email protected]>…

This patch provides documentation describing the AP architecture and design concepts behind the virtualization of AP devices. It also includes an example of how to configure AP devices for exclusive use of KVM guests.

---
  Documentation/s390/vfio-ap.txt |  514 ++++++++++++++++++++++++++++++++++++++++
  MAINTAINERS                    |    1 +
  2 files changed, 515 insertions(+), 0 deletions(-)

create mode 100644 Documentation/s390/vfio-ap.txt

Documentation/s390/vfio-ap.txt new file +4 -0
new file mode 100644
index 0000000..c599f30
@@ -0,0 +1,514 @@
+Introduction:
+============
+The Adjunct Processor (AP) facility is an IBM Z cryptographic facility comprised
+of three AP instructions and from 1 up to 256 PCIe cryptographic adapter cards.
Quoted 8 lines

Documentation/s390/vfio-ap.txt line 10

Quoted 6 lines
Documentation/s390/vfio-ap.txt new file +6 -0
+The AP devices provide cryptographic functions to all CPUs assigned to a
+linux system running in an IBM Z system LPAR.
+
+The AP adapter cards are exposed via the AP bus. The motivation for vfio-ap
+is to make AP cards available to KVM guests using the VFIO mediated device
+framework.

Maybe drop a sentence in here that this makes heavy usage of the s390 virtualization facilities, which do the heavy lifting?

Will do.

Quoted 41 lines
Quoted 41 lines
Documentation/s390/vfio-ap.txt new file +40 -0
+
+AP Architectural Overview:
+=========================
+To facilitate the comprehension of the design, let's start with some
+definitions:
+
+* AP adapter
+
+  An AP adapter is an IBM Z adapter card that can perform cryptographic
+  functions. There can be from 0 to 256 adapters assigned to an LPAR. Adapters
+  assigned to the LPAR in which a linux host is running will be available to
+  the linux host. Each adapter is identified by a number from 0 to 255. When
+  installed, an AP adapter is accessed by AP instructions executed by any CPU.
+
+* AP domain
+
+  An adapter is partitioned into domains. Each domain can be thought of as
+  a set of hardware registers for processing AP instructions. An adapter can
+  hold up to 256 domains. Each domain is identified by a number from 0 to 255.
+  Domains can be further classified into two types:
+
+    * Usage domains are domains that can be accessed directly to process AP
+      commands
+
+    * Control domains are domains that are accessed indirectly by AP
+      commands sent to a usage domain to control or change the domain, for
+      example; to set a secure private key for the domain.
+
+* AP Queue
+
+  An AP queue is the means by which an AP command-request message is sent to an
+  AP usage domain inside a specific AP. An AP queue is identified by a tuple
+  comprised of an AP adapter ID (APID) and an AP queue index (APQI). The
+  APQI corresponds to a given usage domain number within the adapter. This tuple
+  forms an AP Queue Number (APQN) uniquely identifying an AP queue. AP
+  instructions include a field containing the APQN to identify the AP queue to
+  which the AP command-request message is to be sent for processing.
+
+* AP Instructions:
+
Quoted 8 lines

Documentation/s390/vfio-ap.txt line 55

Quoted 5 lines
Documentation/s390/vfio-ap.txt new file +5 -0
+  There are three AP instructions:
+
+  * NQAP: to enqueue an AP command-request message to a queue
+  * DQAP: to dequeue an AP command-reply message from a queue
+  * PQAP: to administer the queues

Do you also want to explain how these entities show up on the ap bus in Linux? It might make the explanations further down easier to understand. (Is there any document for the ap bus you could point to?)

I am not aware of any documentation for the AP bus, but I can provide a few sentences to explain how AP devices are managed by the AP bus.

Quoted 6 lines

Documentation/s390/vfio-ap.txt line 58

Quoted 4 lines
Documentation/s390/vfio-ap.txt new file +3 -0
+
+Start Interpretive Execution (SIE) Instruction:
+==============================================

Call this "AP and SIE" or so? You're not trying to explain the whole SIE architecture :)

Okay

Quoted 26 lines
Quoted 3 lines
Documentation/s390/vfio-ap.txt new file +1 -0
+Let's now see how AP instructions are interpreted by the hardware.

(...)

Quoted 21 lines
Documentation/s390/vfio-ap.txt new file +21 -0
+Reserve APQNs for exclusive use of KVM guests
+---------------------------------------------
+The following block diagram illustrates the mechanism by which APQNs are
+reserved:
+
+                              +------------------+
+                 remove       |                  |   unbind
+         +------------------->+ cex4queue driver +<-----------+
+         |                    |                  |            |
+         |                    +------------------+            |
+         |                                                    |
+         |                                                    |
+         |                                                    |
++--------+---------+ register +------------------+      +-----+------+
+|                  +<---------+                  | bind |            |
+|      ap_bus      |          |  vfio_ap driver  +<-----+    admin   |
+|                  +--------->+                  |      |            |
++------------------+  probe   +---+--------+-----+      +------------+
+                                  |        |
+                           create |        | store APQN
+                                  |        |
Quoted 8 lines

Documentation/s390/vfio-ap.txt line 169

Quoted 7 lines
Documentation/s390/vfio-ap.txt new file +7 -0
+                                  v        v
+                              +---+--------+-----+
+                              |                  |
+                              |  matrix device   |
+                              |                  |
+                              +------------------+
+

Thank you for including diagrams, these are really helpful.

You are welcomed

Quoted 17 lines

(...)

Quoted 14 lines
Documentation/s390/vfio-ap.txt new file +14 -0
+Initialize the CPU model feature for AP
+---------------------------------------
+This design exploits a feature of the SIE architecture called interpretive
+execution (IE). When IE is enabled for a KVM guest, the AP instructions
+executed in the guest will be interpreted by the firmware and the commands
+contained therein will be passed directly through to an AP device assigned to
+the linux host. In order to enable interpretive execution for a KVM guest, SIE
+must have access to the AP facilities installed on the linux host. A new CPU
+model feature is introduced by this design to indicate that the guest will
+directly access the host AP facilities. This feature will be enabled by the
+kernel only if the AP facilities are installed on the linux host. This feature
+must be turned turned on for the guest in order to ac:
+
+    /usr/bin/qemu-system-s390x ... -cpu xxx,ap=on
Quoted 8 lines

Documentation/s390/vfio-ap.txt line 323

Quoted 5 lines
Documentation/s390/vfio-ap.txt new file +5 -0
+
+    Where xxx is the CPU model being used.
+
+If the CPU model feature is not enabled by the kernel, QEMU
+will fail and report that the feature is not supported.

The cpu model interface is supposed to be user space agnostic, although it is only used by QEMU in practice. Mark this as an example more clearly?

Will do

Quoted 5 lines

(...)

I have not looked at this in detail (will probably come back to this later), but this looks like a useful document.

Excellent, I'm glad to hear that. It took some time to put together.

Quoted 1 line
#54 lore raw
In reply to Tony Krowiak
Quoted 39 lines · On Tue, 27 Feb 2018 09:27:58 -0500 Tony Krowiak <[email protected]>…

On s390, we have cryptographic coprocessor cards, which are modeled on Linux as devices on the AP bus. Each card can be partitioned into domains which can be thought of as a set of hardware registers for processing crypto commands. Crypto commands are sent to a specific domain within a card is via a queue which is identified as a (card,domain) tuple. We model this something like the following (assuming we have access to cards 3 and 4 and domains 1 and 2):

AP -> card3 -> queue (3,1)

            -> queue (3,2)
   -> card4 -> queue (4,1)
            -> queue (4,2)  

If we want to virtualize this, we can use a feature provided by the hardware. We basically attach a satellite control block to our main hardware virtualization control block and the hardware takes care of most of the rest.

For this control block, we don't specify explicit tuples, but a list of cards and a list of domains. The guest will get access to the cross product.

Because of this, we need to take care that the lists provided to different guests don't overlap; i.e., we need to enforce sane configurations. Otherwise, one guest may get access to things like secret keys for another guest.

The idea of this patch set is to introduce a new device, the matrix device. This matrix device hangs off a different root and acts as the parent node for mdev devices.

If you now want to give the tuples (4,1) and (4,2), you need to do the following:

- Unbind the (4,1) and (4,2) tuples from their ap bus driver.
- Bind the (4,1) and (4,2) tuples to the vfio_ap driver.
- Create the mediated device.
- Assign card 4 and domains 1 and 2 to the mediated device

Quoted 8 lines

QEMU will now simply consume the mediated device and things should work.

For a complete description of the architecture and concepts underlying the design, see the Documentation/s390/vfio-ap.txt file included with this patch set.

v1 => v2 Change log:
===================

Uh, this is very long... It seems most of the changes are not related to the basic approach, but concerned with details? The general structure was fine last time IIRC.

Quoted 155 lines

* Added documentation vfio-ap.txt
* Renamed vfio_ap_matrix module and device driver to vfio_ap
* Use device core device list instead of maintaining list of matrix devices in driver
* Added VSIE support for AP
* Create matrix device before registering VFIO AP device driver with the AP bus
* Renamed the following files in drivers/s390/crypto:
* vfio_ap_matrix.drv -> vfio_ap_drv
* vfio_ap_matrix_private.h -> vfio_ap_private.h
* vfio_ap_matrix_ops.c -> vfio_ap_ops.c
* arch/s390/include/asm/kvm/ap-matrix-config.h
* Renamed to kvm-ap.h
* Changed the data type of the bit mask fields for the matrix structure to unsigned long and create them with DECLARE_BITMAP
* Changed #define prefixes from AP_MATRIX to KVM_AP
* Changed function and structure prefixes from ap_matrix to kvm_ap
* Added function interface to check if AP Extended Addressing (APXA) facility is installedCRYCB_FORMAT_MASK
* Added function interface to get the maximum ID for AP mask type
* Added function interface to set the AP execution mode
* arch/s390/kvm/ap-matrix-config.c
* Renamed to kvm-ap.c
* Changed function prefixes from ap_matrix to kvm_ap
* Added function to check if AP Extended Addressing (APXA) facility is installed
* Added function to get the maximum ID for AP mask type
* Added function to set the AP execution mode
* Added a boolean parameter to the functions that retrieve the APM, AQM and ADM bit mask fields from the CRYCB. If true, then the function will clear the bits in the mask before returning a reference to it
* Added validation to verify that APM, AQM and ADM bits that are set do not exceed the maximum ID value allowed
*
* arch/s390/include/asm/kvm_host.h
* Changed defined for ECA_AP to ECA_APIE - interpretive execution mode
* Added a flag to struct kvm_s390_crypto to indicate whether the KVM_S390_VM_CPU_FEAT_AP CPU model feature for AP facilities is set
* Added two CPU facilities features to set STFLE.12 and STFLE.15
* arch/s390/kvm/kvm-s390.c
* Added initialization for new KVM_S390_VM_CPU_FEAT_AP CPU model feature
* Removed kvm_s390_apxa_installed() function
* Changed call to kvm_s390_apxa_installed() which has been removed to a call to new kvm_ap_apxa_installed() function.
* Added code to kvm_s390_vcpu_crypto_setup() to set the new CPU model feature flag in the kvm_s390_crypto structure
* Added CRYCB_FORMAT_MASK to mask CRYCBD
* arch/s390/tools/gen_facilities.c
* Added STFLE.12 and STFLE.15 to struct facility _def
* drivers/s390/crypto/vfio_ap_matrix_private.h
* Changed name of file to vfio_ap.private.h
* Changed #define prefixes from VFIO_AP_MATRIX to VFIO_AP
* struct ap_matrix: removed list fields and locks
* struct vfio_ap_queue: removed list field
* Renamed functions ap_matrix_mdev_register and ap_matrix_mdev_unregister to vfio_ap_mdev_register and vfio_ap_mdev_unregister respectively
* drivers/s390/crypto/vfio_ap_matrix_drv.c
* Renamed file to drivers/s390/crypto/vfio_ap_drv.c
* Changed all #define, structure and function prefixes to vfio_ap
* probe function
* Changed root device name for the matrix device to vfio_ap:
i.e., /sys/devices/vfio_ap/matrix
* No longer storing the AP queue device in a list, it is retrievable via the device core
* Removed unnecessary check whether matrix device exists
* Store the vfio_ap_queue structure in the private field of the ap_queue structure rather than using list interface
* remove function
* Retrieve vfio_ap_queue structure from the struct ap_queue private data rather than from a list
* Removed unnecessary check
* drivers/s390/crypto/vfio_ap_matrix_ops.c
* Renamed file to vfio_ap_ops.c
* Changed #define prefixes from AP_MATRIX to VFIO_AP
* Changed function name prefixes from ap_matrix to vfio_ap
* Removed ioctl to configure the CRYCB
* create function
* Removed ap_matrix_mdev_find_by_uuid() function - function is provided by mdev core
* Removed available_instances verification, provided by mdev core
* Removed check to see if mediated device exists, handled by mdev core
* notifier function
* Configuring matrix here instead of via ioctl
* Set interpretive execution mode for all VCPUs
* Removed R/O attributes to display adapters and domains
* Added an R/O attribute to display the matrix
* assign_control_domain mdev attribute:
* Removed check to see if the domain is installed on the linux host
* Added check to verify the control domain ID does not exceed the max value
* assign_adapter mdev attribute:
* Added check to verify the adapter ID does not exceed the max value
* If any APQNs configured for the mediated matrix device that have an APID matching the adapter ID being assigned are not bound to the vfio_ap device driver then it is assumed that the APQN is bound to another driver and assignment will fail
* assign_domain mdev attribute:
* Added check to verify the domain ID does not exceed the max value
* If any APQNs configured for the mediated matrix device that have an APQI matching the domain ID being assigned are not bound to the vfio_ap device driver then it is assumed that the APQN is bound to another driver and assignment will fail
* tools/arch/s390/include/uapi/asm/kvm.h
* removed KVM_S390_VM_CPU_FEAT_AP feature definition

Tony Krowiak (15):
KVM: s390: refactor crypto initialization
s390: vsie: implement AP support for second level guest
s390: zcrypt: externalize AP instructions available function
KVM: s390: CPU model support for AP virtualization
s390: vfio-ap: base implementation of VFIO AP device driver
s390: vfio-ap: register matrix device with VFIO mdev framework
KVM: s390: Interfaces to configure/deconfigure guest's AP matrix
KVM: s390: interface to enable AP execution mode
s390: vfio-ap: sysfs interfaces to configure adapters
s390: vfio-ap: sysfs interfaces to configure domains
s390: vfio-ap: sysfs interfaces to configure control domains
s390: vfio-ap: sysfs interface to view matrix mdev matrix
KVM: s390: Configure the guest's CRYCB
s390: vfio-ap: implement VFIO_DEVICE_GET_INFO ioctl
s390: doc: detailed specifications for AP virtualization

 Documentation/s390/vfio-ap.txt          |  514 ++++++++++++++++++
 MAINTAINERS                             |   14 +
 arch/s390/Kconfig                       |    8 +
 arch/s390/configs/default_defconfig     |    3 +
 arch/s390/configs/gcov_defconfig        |    3 +
 arch/s390/configs/performance_defconfig |    3 +
 arch/s390/defconfig                     |    3 +
 arch/s390/include/asm/ap.h              |    7 +
 arch/s390/include/asm/kvm-ap.h          |   59 +++
 arch/s390/include/asm/kvm_host.h        |    2 +
 arch/s390/include/uapi/asm/kvm.h        |    1 +
 arch/s390/kvm/Makefile                  |    2 +-
 arch/s390/kvm/kvm-ap.c                  |  336 ++++++++++++
 arch/s390/kvm/kvm-s390.c                |   66 +--
 arch/s390/kvm/kvm-s390.h                |    1 +
 arch/s390/kvm/vsie.c                    |   71 +++-
 arch/s390/tools/gen_facilities.c        |    2 +
 drivers/s390/crypto/Makefile            |    4 +
 drivers/s390/crypto/ap_bus.c            |    6 +
 drivers/s390/crypto/vfio_ap_drv.c       |  143 +++++
 drivers/s390/crypto/vfio_ap_ops.c       |  868 +++++++++++++++++++++++++++++++
 drivers/s390/crypto/vfio_ap_private.h   |   45 ++
 include/uapi/linux/vfio.h               |    2 +
 23 files changed, 2096 insertions(+), 67 deletions(-)
 create mode 100644 Documentation/s390/vfio-ap.txt
 create mode 100644 arch/s390/include/asm/kvm-ap.h
 create mode 100644 arch/s390/kvm/kvm-ap.c
 create mode 100644 drivers/s390/crypto/vfio_ap_drv.c
 create mode 100644 drivers/s390/crypto/vfio_ap_ops.c
 create mode 100644 drivers/s390/crypto/vfio_ap_private.h
#55 lore raw
Quoted 48 lines · Cornelia Huck
Quoted 45 lines · On Tue, 27 Feb 2018 09:27:58 -0500 Tony Krowiak <[email protected]>…

On s390, we have cryptographic coprocessor cards, which are modeled on Linux as devices on the AP bus. Each card can be partitioned into domains which can be thought of as a set of hardware registers for processing crypto commands. Crypto commands are sent to a specific domain within a card is via a queue which is identified as a (card,domain) tuple. We model this something like the following (assuming we have access to cards 3 and 4 and domains 1 and 2):

AP -> card3 -> queue (3,1)

             -> queue (3,2)
    -> card4 -> queue (4,1)
             -> queue (4,2)

If we want to virtualize this, we can use a feature provided by the hardware. We basically attach a satellite control block to our main hardware virtualization control block and the hardware takes care of most of the rest.

For this control block, we don't specify explicit tuples, but a list of cards and a list of domains. The guest will get access to the cross product.

Because of this, we need to take care that the lists provided to different guests don't overlap; i.e., we need to enforce sane configurations. Otherwise, one guest may get access to things like secret keys for another guest.

The idea of this patch set is to introduce a new device, the matrix device. This matrix device hangs off a different root and acts as the parent node for mdev devices.

If you now want to give the tuples (4,1) and (4,2), you need to do the following:

- Unbind the (4,1) and (4,2) tuples from their ap bus driver.
- Bind the (4,1) and (4,2) tuples to the vfio_ap driver.
- Create the mediated device.
- Assign card 4 and domains 1 and 2 to the mediated device

QEMU will now simply consume the mediated device and things should work.

For a complete description of the architecture and concepts underlying the design, see the Documentation/s390/vfio-ap.txt file included with this patch set.

Quoted 5 lines
Quoted 2 lines

v1 => v2 Change log:
===================

Uh, this is very long... It seems most of the changes are not related to the basic approach, but concerned with details? The general structure was fine last time IIRC.

I'm apologize, I am not well-versed in how much detail is appropriate. This also went through several internal reviews before re-posting to the kernel lists.

Quoted 156 lines
Quoted 156 lines

* Added documentation vfio-ap.txt
* Renamed vfio_ap_matrix module and device driver to vfio_ap
* Use device core device list instead of maintaining list of matrix devices in driver
* Added VSIE support for AP
* Create matrix device before registering VFIO AP device driver with the AP bus
* Renamed the following files in drivers/s390/crypto:
* vfio_ap_matrix.drv -> vfio_ap_drv
* vfio_ap_matrix_private.h -> vfio_ap_private.h
* vfio_ap_matrix_ops.c -> vfio_ap_ops.c
* arch/s390/include/asm/kvm/ap-matrix-config.h
* Renamed to kvm-ap.h
* Changed the data type of the bit mask fields for the matrix structure to unsigned long and create them with DECLARE_BITMAP
* Changed #define prefixes from AP_MATRIX to KVM_AP
* Changed function and structure prefixes from ap_matrix to kvm_ap
* Added function interface to check if AP Extended Addressing (APXA) facility is installedCRYCB_FORMAT_MASK
* Added function interface to get the maximum ID for AP mask type
* Added function interface to set the AP execution mode
* arch/s390/kvm/ap-matrix-config.c
* Renamed to kvm-ap.c
* Changed function prefixes from ap_matrix to kvm_ap
* Added function to check if AP Extended Addressing (APXA) facility is installed
* Added function to get the maximum ID for AP mask type
* Added function to set the AP execution mode
* Added a boolean parameter to the functions that retrieve the APM, AQM and ADM bit mask fields from the CRYCB. If true, then the function will clear the bits in the mask before returning a reference to it
* Added validation to verify that APM, AQM and ADM bits that are set do not exceed the maximum ID value allowed
*
* arch/s390/include/asm/kvm_host.h
* Changed defined for ECA_AP to ECA_APIE - interpretive execution mode
* Added a flag to struct kvm_s390_crypto to indicate whether the KVM_S390_VM_CPU_FEAT_AP CPU model feature for AP facilities is set
* Added two CPU facilities features to set STFLE.12 and STFLE.15
* arch/s390/kvm/kvm-s390.c
* Added initialization for new KVM_S390_VM_CPU_FEAT_AP CPU model feature
* Removed kvm_s390_apxa_installed() function
* Changed call to kvm_s390_apxa_installed() which has been removed to a call to new kvm_ap_apxa_installed() function.
* Added code to kvm_s390_vcpu_crypto_setup() to set the new CPU model feature flag in the kvm_s390_crypto structure
* Added CRYCB_FORMAT_MASK to mask CRYCBD
* arch/s390/tools/gen_facilities.c
* Added STFLE.12 and STFLE.15 to struct facility _def
* drivers/s390/crypto/vfio_ap_matrix_private.h
* Changed name of file to vfio_ap.private.h
* Changed #define prefixes from VFIO_AP_MATRIX to VFIO_AP
* struct ap_matrix: removed list fields and locks
* struct vfio_ap_queue: removed list field
* Renamed functions ap_matrix_mdev_register and ap_matrix_mdev_unregister to vfio_ap_mdev_register and vfio_ap_mdev_unregister respectively
* drivers/s390/crypto/vfio_ap_matrix_drv.c
* Renamed file to drivers/s390/crypto/vfio_ap_drv.c
* Changed all #define, structure and function prefixes to vfio_ap
* probe function
* Changed root device name for the matrix device to vfio_ap:
i.e., /sys/devices/vfio_ap/matrix
* No longer storing the AP queue device in a list, it is retrievable via the device core
* Removed unnecessary check whether matrix device exists
* Store the vfio_ap_queue structure in the private field of the ap_queue structure rather than using list interface
* remove function
* Retrieve vfio_ap_queue structure from the struct ap_queue private data rather than from a list
* Removed unnecessary check
* drivers/s390/crypto/vfio_ap_matrix_ops.c
* Renamed file to vfio_ap_ops.c
* Changed #define prefixes from AP_MATRIX to VFIO_AP
* Changed function name prefixes from ap_matrix to vfio_ap
* Removed ioctl to configure the CRYCB
* create function
* Removed ap_matrix_mdev_find_by_uuid() function - function is provided by mdev core
* Removed available_instances verification, provided by mdev core
* Removed check to see if mediated device exists, handled by mdev core
* notifier function
* Configuring matrix here instead of via ioctl
* Set interpretive execution mode for all VCPUs
* Removed R/O attributes to display adapters and domains
* Added an R/O attribute to display the matrix
* assign_control_domain mdev attribute:
* Removed check to see if the domain is installed on the linux host
* Added check to verify the control domain ID does not exceed the max value
* assign_adapter mdev attribute:
* Added check to verify the adapter ID does not exceed the max value
* If any APQNs configured for the mediated matrix device that have an APID matching the adapter ID being assigned are not bound to the vfio_ap device driver then it is assumed that the APQN is bound to another driver and assignment will fail
* assign_domain mdev attribute:
* Added check to verify the domain ID does not exceed the max value
* If any APQNs configured for the mediated matrix device that have an APQI matching the domain ID being assigned are not bound to the vfio_ap device driver then it is assumed that the APQN is bound to another driver and assignment will fail
* tools/arch/s390/include/uapi/asm/kvm.h
* removed KVM_S390_VM_CPU_FEAT_AP feature definition

Tony Krowiak (15):
KVM: s390: refactor crypto initialization
s390: vsie: implement AP support for second level guest
s390: zcrypt: externalize AP instructions available function
KVM: s390: CPU model support for AP virtualization
s390: vfio-ap: base implementation of VFIO AP device driver
s390: vfio-ap: register matrix device with VFIO mdev framework
KVM: s390: Interfaces to configure/deconfigure guest's AP matrix
KVM: s390: interface to enable AP execution mode
s390: vfio-ap: sysfs interfaces to configure adapters
s390: vfio-ap: sysfs interfaces to configure domains
s390: vfio-ap: sysfs interfaces to configure control domains
s390: vfio-ap: sysfs interface to view matrix mdev matrix
KVM: s390: Configure the guest's CRYCB
s390: vfio-ap: implement VFIO_DEVICE_GET_INFO ioctl
s390: doc: detailed specifications for AP virtualization

  Documentation/s390/vfio-ap.txt          |  514 ++++++++++++++++++
  MAINTAINERS                             |   14 +
  arch/s390/Kconfig                       |    8 +
  arch/s390/configs/default_defconfig     |    3 +
  arch/s390/configs/gcov_defconfig        |    3 +
  arch/s390/configs/performance_defconfig |    3 +
  arch/s390/defconfig                     |    3 +
  arch/s390/include/asm/ap.h              |    7 +
  arch/s390/include/asm/kvm-ap.h          |   59 +++
  arch/s390/include/asm/kvm_host.h        |    2 +
  arch/s390/include/uapi/asm/kvm.h        |    1 +
  arch/s390/kvm/Makefile                  |    2 +-
  arch/s390/kvm/kvm-ap.c                  |  336 ++++++++++++
  arch/s390/kvm/kvm-s390.c                |   66 +--
  arch/s390/kvm/kvm-s390.h                |    1 +
  arch/s390/kvm/vsie.c                    |   71 +++-
  arch/s390/tools/gen_facilities.c        |    2 +
  drivers/s390/crypto/Makefile            |    4 +
  drivers/s390/crypto/ap_bus.c            |    6 +
  drivers/s390/crypto/vfio_ap_drv.c       |  143 +++++
  drivers/s390/crypto/vfio_ap_ops.c       |  868 +++++++++++++++++++++++++++++++
  drivers/s390/crypto/vfio_ap_private.h   |   45 ++
  include/uapi/linux/vfio.h               |    2 +
  23 files changed, 2096 insertions(+), 67 deletions(-)

create mode 100644 Documentation/s390/vfio-ap.txt create mode 100644 arch/s390/include/asm/kvm-ap.h create mode 100644 arch/s390/kvm/kvm-ap.c create mode 100644 drivers/s390/crypto/vfio_ap_drv.c create mode 100644 drivers/s390/crypto/vfio_ap_ops.c create mode 100644 drivers/s390/crypto/vfio_ap_private.h