Skip to contentSkip to conversation

KVM: s390: enable zPCI for interpretive execution

PATCH Superseded Matthew Rosato 6 participants 39 messages May 13, 2022 – May 18, 2022

Subsystems (14)

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Matthew Rosato

author maintainer
#0 lore raw

v7 cover KVM: s390: enable zPCI for interpretive execution

Enable interpretive execution of zPCI instructions + adapter interruption forwarding for s390x KVM vfio-pci. This is done by triggering a routine when the VFIO group is associated with the KVM guest, transmitting to firmware a special token (GISA designation) to enable that specific guest for interpretive execution on that zPCI device. Load/store interpreation enablement is then controlled by userspace (based upon whether or not a SHM bit is placed in the virtual function handle). Adapter Event Notification interpretation is controlled from userspace via a new KVM ioctl.

By allowing intepretation of zPCI instructions and firmware delivery of interrupts to guests, we can reduce the frequency of guest SIE exits for zPCI.

From the perspective of guest configuration, you passthrough zPCI devices in the same manner as before, with intepretation support being used by default if available in kernel+qemu.

Some minor changes are already suggested for the QEMU series, but for now the v5 one is still compatible (with one exception: in linux-headers/linux/kvm.h KVM_CAP_S390_ZPCI_OP is now changed to 216)

https://lore.kernel.org/kvm/[email protected]/

Changelog v6->v7:
- Add some more R-b tags
- Mark a few routines static (kernel bot)
- Add a few lockdep_assert_held calls (Christian)
- Add a new condition under which locked_vm is defined (Christian)
- After discussions with Jason, let's go back to using a Kconfig option to control vfio-pci-zdev inclusion so we can avoid some symbol_get messiness. The name was chosen so that we can re-introduce zdev for non-KVM environments in the future, should it be needed.
- Since we re-introduce a Kconfig option, we can go back to using this in various places instead of CONFIG_VFIO_PCI.
- Expose separate interfaces for register and unregister of kvm (Jason)
- Cleanup KVM association during zdev device_close; if the device fd is closed before we receive the kvm notifier, we will never see the notification because we unregister it, but cleanup must still be done. Conversely, if the kvm fd closes before the device is closed, we should get the NULL notifier from kvm_destroy_devices since the notifier has not yet been unregistered.
- As a result of the above, we no longer need to clear the pci list during vm exit. But to be safe let's WARN_ON_ONCE if it's found to be non-empty during free_vm.
- Add kzdev_lock to protect the kzdev
- uapi KVM_CAP_S390_ZPCI_OP moved to 216 (previously 214)

Matthew Rosato (22):
s390/sclp: detect the zPCI load/store interpretation facility s390/sclp: detect the AISII facility
s390/sclp: detect the AENI facility
s390/sclp: detect the AISI facility
s390/airq: pass more TPI info to airq handlers
s390/airq: allow for airq structure that uses an input vector s390/pci: externalize the SIC operation controls and routine s390/pci: stash associated GISA designation
s390/pci: stash dtsm and maxstbl
vfio/pci: introduce CONFIG_VFIO_PCI_ZDEV_KVM
KVM: s390: pci: add basic kvm_zdev structure
KVM: s390: pci: do initial setup for AEN interpretation
KVM: s390: pci: enable host forwarding of Adapter Event Notifications
KVM: s390: mechanism to enable guest zPCI Interpretation
KVM: s390: pci: provide routines for enabling/disabling interrupt
forwarding
KVM: s390: pci: add routines to start/stop interpretive execution vfio-pci/zdev: add open/close device hooks
vfio-pci/zdev: add function handle to clp base capability vfio-pci/zdev: different maxstbl for interpreted devices
KVM: s390: add KVM_S390_ZPCI_OP to manage guest zPCI devices
KVM: s390: introduce CPU feature for zPCI Interpretation
MAINTAINERS: additional files related kvm s390 pci passthrough

 Documentation/virt/kvm/api.rst   |  45 ++
 MAINTAINERS                      |   1 +
 arch/s390/include/asm/airq.h     |   7 +-
 arch/s390/include/asm/kvm_host.h |  26 ++
 arch/s390/include/asm/pci.h      |  13 +
 arch/s390/include/asm/pci_clp.h  |   9 +-
 arch/s390/include/asm/pci_insn.h |  29 +-
 arch/s390/include/asm/sclp.h     |   4 +
 arch/s390/include/asm/tpi.h      |  13 +
 arch/s390/include/uapi/asm/kvm.h |   1 +
 arch/s390/kvm/Makefile           |   1 +
 arch/s390/kvm/interrupt.c        |  96 ++++-
 arch/s390/kvm/kvm-s390.c         |  94 ++++-
 arch/s390/kvm/kvm-s390.h         |  10 +
 arch/s390/kvm/pci.c              | 686 +++++++++++++++++++++++++++++++
 arch/s390/kvm/pci.h              |  88 ++++
 arch/s390/pci/pci.c              |  16 +
 arch/s390/pci/pci_clp.c          |   7 +
 arch/s390/pci/pci_insn.c         |   4 +-
 arch/s390/pci/pci_irq.c          |  48 ++-
 drivers/s390/char/sclp_early.c   |   4 +
 drivers/s390/cio/airq.c          |  12 +-
 drivers/s390/cio/qdio_thinint.c  |   6 +-
 drivers/s390/crypto/ap_bus.c     |   9 +-
 drivers/s390/virtio/virtio_ccw.c |   6 +-
 drivers/vfio/pci/Kconfig         |  11 +
 drivers/vfio/pci/Makefile        |   2 +-
 drivers/vfio/pci/vfio_pci_core.c |   2 +
 drivers/vfio/pci/vfio_pci_zdev.c |  65 ++-
 include/linux/sched/user.h       |   3 +-
 include/linux/vfio_pci_core.h    |  12 +-
 include/uapi/linux/kvm.h         |  31 ++
 include/uapi/linux/vfio_zdev.h   |   7 +
 33 files changed, 1313 insertions(+), 55 deletions(-)
 create mode 100644 arch/s390/kvm/pci.c
 create mode 100644 arch/s390/kvm/pci.h
Signature · 1 line
2.27.0
author maintainer
#1 lore raw

PATCH v7 1/22 s390/sclp: detect the zPCI load/store interpretation facility

Detect the zPCI Load/Store Interpretation facility.

---
 arch/s390/include/asm/sclp.h   | 1 +
 drivers/s390/char/sclp_early.c | 1 +
 2 files changed, 2 insertions(+)
arch/s390/include/asm/sclp.h +1 -0
index 04cb1e7582a6..06c0b5eb8ef5 100644
@@ -87,6 +87,7 @@ struct sclp_info {
 	unsigned char has_diag318 : 1;
 	unsigned char has_sipl : 1;
 	unsigned char has_dirq : 1;
+	unsigned char has_zpci_lsi : 1;
 	unsigned int ibc;
 	unsigned int mtid;
 	unsigned int mtid_cp;
drivers/s390/char/sclp_early.c +1 -0
index e9943a86c361..b88dd0da1231 100644
@@ -45,6 +45,7 @@ static void __init sclp_early_facilities_detect(void)
 	sclp.has_gisaf = !!(sccb->fac118 & 0x08);
 	sclp.has_hvs = !!(sccb->fac119 & 0x80);
 	sclp.has_kss = !!(sccb->fac98 & 0x01);
+	sclp.has_zpci_lsi = !!(sccb->fac118 & 0x01);
 	if (sccb->fac85 & 0x02)
 		S390_lowcore.machine_flags |= MACHINE_FLAG_ESOP;
 	if (sccb->fac91 & 0x40)
Signature · 1 line
2.27.0
author maintainer
#2 lore raw

PATCH v7 2/22 s390/sclp: detect the AISII facility

In reply to Matthew Rosato

Detect the Adapter Interruption Source ID Interpretation facility.

---
 arch/s390/include/asm/sclp.h   | 1 +
 drivers/s390/char/sclp_early.c | 1 +
 2 files changed, 2 insertions(+)
arch/s390/include/asm/sclp.h +1 -0
index 06c0b5eb8ef5..be14644fe113 100644
@@ -88,6 +88,7 @@ struct sclp_info {
 	unsigned char has_sipl : 1;
 	unsigned char has_dirq : 1;
 	unsigned char has_zpci_lsi : 1;
+	unsigned char has_aisii : 1;
 	unsigned int ibc;
 	unsigned int mtid;
 	unsigned int mtid_cp;
drivers/s390/char/sclp_early.c +1 -0
index b88dd0da1231..29fee179e197 100644
@@ -45,6 +45,7 @@ static void __init sclp_early_facilities_detect(void)
 	sclp.has_gisaf = !!(sccb->fac118 & 0x08);
 	sclp.has_hvs = !!(sccb->fac119 & 0x80);
 	sclp.has_kss = !!(sccb->fac98 & 0x01);
+	sclp.has_aisii = !!(sccb->fac118 & 0x40);
 	sclp.has_zpci_lsi = !!(sccb->fac118 & 0x01);
 	if (sccb->fac85 & 0x02)
 		S390_lowcore.machine_flags |= MACHINE_FLAG_ESOP;
Signature · 1 line
2.27.0
author maintainer
#3 lore raw

PATCH v7 3/22 s390/sclp: detect the AENI facility

In reply to Matthew Rosato

Detect the Adapter Event Notification Interpretation facility.

---
 arch/s390/include/asm/sclp.h   | 1 +
 drivers/s390/char/sclp_early.c | 1 +
 2 files changed, 2 insertions(+)
arch/s390/include/asm/sclp.h +1 -0
index be14644fe113..ff359df62154 100644
@@ -89,6 +89,7 @@ struct sclp_info {
 	unsigned char has_dirq : 1;
 	unsigned char has_zpci_lsi : 1;
 	unsigned char has_aisii : 1;
+	unsigned char has_aeni : 1;
 	unsigned int ibc;
 	unsigned int mtid;
 	unsigned int mtid_cp;
drivers/s390/char/sclp_early.c +1 -0
index 29fee179e197..e9af01b4c97a 100644
@@ -46,6 +46,7 @@ static void __init sclp_early_facilities_detect(void)
 	sclp.has_hvs = !!(sccb->fac119 & 0x80);
 	sclp.has_kss = !!(sccb->fac98 & 0x01);
 	sclp.has_aisii = !!(sccb->fac118 & 0x40);
+	sclp.has_aeni = !!(sccb->fac118 & 0x20);
 	sclp.has_zpci_lsi = !!(sccb->fac118 & 0x01);
 	if (sccb->fac85 & 0x02)
 		S390_lowcore.machine_flags |= MACHINE_FLAG_ESOP;
Signature · 1 line
2.27.0
author maintainer
#4 lore raw

PATCH v7 4/22 s390/sclp: detect the AISI facility

In reply to Matthew Rosato

Detect the Adapter Interruption Suppression Interpretation facility.

---
 arch/s390/include/asm/sclp.h   | 1 +
 drivers/s390/char/sclp_early.c | 1 +
 2 files changed, 2 insertions(+)
arch/s390/include/asm/sclp.h +1 -0
index ff359df62154..bdd365407176 100644
@@ -90,6 +90,7 @@ struct sclp_info {
 	unsigned char has_zpci_lsi : 1;
 	unsigned char has_aisii : 1;
 	unsigned char has_aeni : 1;
+	unsigned char has_aisi : 1;
 	unsigned int ibc;
 	unsigned int mtid;
 	unsigned int mtid_cp;
drivers/s390/char/sclp_early.c +1 -0
index e9af01b4c97a..c13e55cc4a5d 100644
@@ -47,6 +47,7 @@ static void __init sclp_early_facilities_detect(void)
 	sclp.has_kss = !!(sccb->fac98 & 0x01);
 	sclp.has_aisii = !!(sccb->fac118 & 0x40);
 	sclp.has_aeni = !!(sccb->fac118 & 0x20);
+	sclp.has_aisi = !!(sccb->fac118 & 0x10);
 	sclp.has_zpci_lsi = !!(sccb->fac118 & 0x01);
 	if (sccb->fac85 & 0x02)
 		S390_lowcore.machine_flags |= MACHINE_FLAG_ESOP;
Signature · 1 line
2.27.0
author maintainer
#5 lore raw

PATCH v7 5/22 s390/airq: pass more TPI info to airq handlers

In reply to Matthew Rosato

A subsequent patch will introduce an airq handler that requires additional TPI information beyond directed vs floating, so pass the entire tpi_info structure via the handler. Only pci actually uses this information today, for the other airq handlers this is effectively a no-op.

---
 arch/s390/include/asm/airq.h     | 3 ++-
 arch/s390/kvm/interrupt.c        | 4 +++-
 arch/s390/pci/pci_irq.c          | 9 +++++++--
 drivers/s390/cio/airq.c          | 2 +-
 drivers/s390/cio/qdio_thinint.c  | 6 ++++--
 drivers/s390/crypto/ap_bus.c     | 9 ++++++---
 drivers/s390/virtio/virtio_ccw.c | 4 +++-
 7 files changed, 26 insertions(+), 11 deletions(-)
arch/s390/include/asm/airq.h +2 -1
index 01936fdfaddb..7918a7d09028 100644
@@ -12,10 +12,11 @@
 
 #include <linux/bit_spinlock.h>
 #include <linux/dma-mapping.h>
+#include <asm/tpi.h>
 
 struct airq_struct {
 	struct hlist_node list;		/* Handler queueing. */
-	void (*handler)(struct airq_struct *airq, bool floating);
+	void (*handler)(struct airq_struct *airq, struct tpi_info *tpi_info);
 	u8 *lsi_ptr;			/* Local-Summary-Indicator pointer */
 	u8 lsi_mask;			/* Local-Summary-Indicator mask */
 	u8 isc;				/* Interrupt-subclass */
arch/s390/kvm/interrupt.c +3 -1
index af96dc0549a4..17ff475157d8 100644
@@ -28,6 +28,7 @@
 #include <asm/switch_to.h>
 #include <asm/nmi.h>
 #include <asm/airq.h>
+#include <asm/tpi.h>
 #include "kvm-s390.h"
 #include "gaccess.h"
 #include "trace-s390.h"
@@ -3311,7 +3312,8 @@ int kvm_s390_gisc_unregister(struct kvm *kvm, u32 gisc)
 }
 EXPORT_SYMBOL_GPL(kvm_s390_gisc_unregister);
 
-static void gib_alert_irq_handler(struct airq_struct *airq, bool floating)
+static void gib_alert_irq_handler(struct airq_struct *airq,
+				  struct tpi_info *tpi_info)
 {
 	inc_irq_stat(IRQIO_GAL);
 	process_gib_alert_list();
arch/s390/pci/pci_irq.c +7 -2
index 500cd2dbdf53..b805c75252ed 100644
@@ -11,6 +11,7 @@
 
 #include <asm/isc.h>
 #include <asm/airq.h>
+#include <asm/tpi.h>
 
 static enum {FLOATING, DIRECTED} irq_delivery;
 
@@ -216,8 +217,11 @@ static void zpci_handle_fallback_irq(void)
 	}
 }
 
-static void zpci_directed_irq_handler(struct airq_struct *airq, bool floating)
+static void zpci_directed_irq_handler(struct airq_struct *airq,
+				      struct tpi_info *tpi_info)
 {
+	bool floating = !tpi_info->directed_irq;
+
 	if (floating) {
 		inc_irq_stat(IRQIO_PCF);
 		zpci_handle_fallback_irq();
@@ -227,7 +231,8 @@ static void zpci_directed_irq_handler(struct airq_struct *airq, bool floating)
 	}
 }
 
-static void zpci_floating_irq_handler(struct airq_struct *airq, bool floating)
+static void zpci_floating_irq_handler(struct airq_struct *airq,
+				      struct tpi_info *tpi_info)
 {
 	unsigned long si, ai;
 	struct airq_iv *aibv;
drivers/s390/cio/airq.c +1 -1
index c0ed364bf446..230eb5b5b64e 100644
@@ -99,7 +99,7 @@ static irqreturn_t do_airq_interrupt(int irq, void *dummy)
 	rcu_read_lock();
 	hlist_for_each_entry_rcu(airq, head, list)
 		if ((*airq->lsi_ptr & airq->lsi_mask) != 0)
-			airq->handler(airq, !tpi_info->directed_irq);
+			airq->handler(airq, tpi_info);
 	rcu_read_unlock();
 
 	return IRQ_HANDLED;
drivers/s390/cio/qdio_thinint.c +4 -2
index 8e09bf3a2fcd..9b9335dd06db 100644
@@ -15,6 +15,7 @@
 #include <asm/qdio.h>
 #include <asm/airq.h>
 #include <asm/isc.h>
+#include <asm/tpi.h>
 
 #include "cio.h"
 #include "ioasm.h"
@@ -93,9 +94,10 @@ static inline u32 clear_shared_ind(void)
 /**
  * tiqdio_thinint_handler - thin interrupt handler for qdio
  * @airq: pointer to adapter interrupt descriptor
- * @floating: flag to recognize floating vs. directed interrupts (unused)
+ * @tpi_info: interrupt information (e.g. floating vs directed -- unused)
  */
-static void tiqdio_thinint_handler(struct airq_struct *airq, bool floating)
+static void tiqdio_thinint_handler(struct airq_struct *airq,
+				   struct tpi_info *tpi_info)
 {
 	u64 irq_time = S390_lowcore.int_clock;
 	u32 si_used = clear_shared_ind();
drivers/s390/crypto/ap_bus.c +6 -3
index fdf16cb70881..6cb8a2802ef8 100644
@@ -27,6 +27,7 @@
 #include <linux/kthread.h>
 #include <linux/mutex.h>
 #include <asm/airq.h>
+#include <asm/tpi.h>
 #include <linux/atomic.h>
 #include <asm/isc.h>
 #include <linux/hrtimer.h>
@@ -131,7 +132,8 @@ static int ap_max_adapter_id = 63;
 static struct bus_type ap_bus_type;
 
 /* Adapter interrupt definitions */
-static void ap_interrupt_handler(struct airq_struct *airq, bool floating);
+static void ap_interrupt_handler(struct airq_struct *airq,
+				 struct tpi_info *tpi_info);
 
 static bool ap_irq_flag;
 
@@ -452,9 +454,10 @@ static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
 /**
  * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
  * @airq: pointer to adapter interrupt descriptor
- * @floating: ignored
+ * @tpi_info: ignored
  */
-static void ap_interrupt_handler(struct airq_struct *airq, bool floating)
+static void ap_interrupt_handler(struct airq_struct *airq,
+				 struct tpi_info *tpi_info)
 {
 	inc_irq_stat(IRQIO_APB);
 	tasklet_schedule(&ap_tasklet);
drivers/s390/virtio/virtio_ccw.c +3 -1
index d35e7a3f7067..52c376d15978 100644
@@ -33,6 +33,7 @@
 #include <asm/virtio-ccw.h>
 #include <asm/isc.h>
 #include <asm/airq.h>
+#include <asm/tpi.h>
 
 /*
  * virtio related functions
@@ -203,7 +204,8 @@ static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
 	write_unlock_irqrestore(&info->lock, flags);
 }
 
-static void virtio_airq_handler(struct airq_struct *airq, bool floating)
+static void virtio_airq_handler(struct airq_struct *airq,
+				struct tpi_info *tpi_info)
 {
 	struct airq_info *info = container_of(airq, struct airq_info, airq);
 	unsigned long ai;
Signature · 1 line
2.27.0
author maintainer
#6 lore raw

PATCH v7 6/22 s390/airq: allow for airq structure that uses an input vector

In reply to Matthew Rosato

When doing device passthrough where interrupts are being forwarded from host to guest, we wish to use a pinned section of guest memory as the vector (the same memory used by the guest as the vector). To accomplish this, add a new parameter for airq_iv_create which allows passing an existing vector to be used instead of allocating a new one. The caller is responsible for ensuring the vector is pinned in memory as well as for unpinning the memory when the vector is no longer needed.

A subsequent patch will use this new parameter for zPCI interpretation.

---
 arch/s390/include/asm/airq.h     |  4 +++-
 arch/s390/pci/pci_irq.c          |  8 ++++----
 drivers/s390/cio/airq.c          | 10 +++++++---
 drivers/s390/virtio/virtio_ccw.c |  2 +-
 4 files changed, 15 insertions(+), 9 deletions(-)
arch/s390/include/asm/airq.h +3 -1
index 7918a7d09028..e82e5626e139 100644
@@ -47,8 +47,10 @@ struct airq_iv {
 #define AIRQ_IV_PTR		4	/* Allocate the ptr array */
 #define AIRQ_IV_DATA		8	/* Allocate the data array */
 #define AIRQ_IV_CACHELINE	16	/* Cacheline alignment for the vector */
+#define AIRQ_IV_GUESTVEC	32	/* Vector is a pinned guest page */
 
-struct airq_iv *airq_iv_create(unsigned long bits, unsigned long flags);
+struct airq_iv *airq_iv_create(unsigned long bits, unsigned long flags,
+			       unsigned long *vec);
 void airq_iv_release(struct airq_iv *iv);
 unsigned long airq_iv_alloc(struct airq_iv *iv, unsigned long num);
 void airq_iv_free(struct airq_iv *iv, unsigned long bit, unsigned long num);
arch/s390/pci/pci_irq.c +4 -4
index b805c75252ed..87c7d121c255 100644
@@ -296,7 +296,7 @@ int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
 		zdev->aisb = bit;
 
 		/* Create adapter interrupt vector */
-		zdev->aibv = airq_iv_create(msi_vecs, AIRQ_IV_DATA | AIRQ_IV_BITLOCK);
+		zdev->aibv = airq_iv_create(msi_vecs, AIRQ_IV_DATA | AIRQ_IV_BITLOCK, NULL);
 		if (!zdev->aibv)
 			return -ENOMEM;
 
@@ -419,7 +419,7 @@ static int __init zpci_directed_irq_init(void)
 	union zpci_sic_iib iib = {{0}};
 	unsigned int cpu;
 
-	zpci_sbv = airq_iv_create(num_possible_cpus(), 0);
+	zpci_sbv = airq_iv_create(num_possible_cpus(), 0, NULL);
 	if (!zpci_sbv)
 		return -ENOMEM;
 
@@ -441,7 +441,7 @@ static int __init zpci_directed_irq_init(void)
 		zpci_ibv[cpu] = airq_iv_create(cache_line_size() * BITS_PER_BYTE,
 					       AIRQ_IV_DATA |
 					       AIRQ_IV_CACHELINE |
-					       (!cpu ? AIRQ_IV_ALLOC : 0));
+					       (!cpu ? AIRQ_IV_ALLOC : 0), NULL);
 		if (!zpci_ibv[cpu])
 			return -ENOMEM;
 	}
@@ -458,7 +458,7 @@ static int __init zpci_floating_irq_init(void)
 	if (!zpci_ibv)
 		return -ENOMEM;
 
-	zpci_sbv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC);
+	zpci_sbv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC, NULL);
 	if (!zpci_sbv)
 		goto out_free;
 
drivers/s390/cio/airq.c +7 -3
index 230eb5b5b64e..34967e67249e 100644
@@ -122,10 +122,12 @@ static inline unsigned long iv_size(unsigned long bits)
  * airq_iv_create - create an interrupt vector
  * @bits: number of bits in the interrupt vector
  * @flags: allocation flags
+ * @vec: pointer to pinned guest memory if AIRQ_IV_GUESTVEC
  *
  * Returns a pointer to an interrupt vector structure
  */
-struct airq_iv *airq_iv_create(unsigned long bits, unsigned long flags)
+struct airq_iv *airq_iv_create(unsigned long bits, unsigned long flags,
+			       unsigned long *vec)
 {
 	struct airq_iv *iv;
 	unsigned long size;
@@ -146,6 +148,8 @@ struct airq_iv *airq_iv_create(unsigned long bits, unsigned long flags)
 					     &iv->vector_dma);
 		if (!iv->vector)
 			goto out_free;
+	} else if (flags & AIRQ_IV_GUESTVEC) {
+		iv->vector = vec;
 	} else {
 		iv->vector = cio_dma_zalloc(size);
 		if (!iv->vector)
@@ -185,7 +189,7 @@ struct airq_iv *airq_iv_create(unsigned long bits, unsigned long flags)
 	kfree(iv->avail);
 	if (iv->flags & AIRQ_IV_CACHELINE && iv->vector)
 		dma_pool_free(airq_iv_cache, iv->vector, iv->vector_dma);
-	else
+	else if (!(iv->flags & AIRQ_IV_GUESTVEC))
 		cio_dma_free(iv->vector, size);
 	kfree(iv);
 out:
@@ -204,7 +208,7 @@ void airq_iv_release(struct airq_iv *iv)
 	kfree(iv->bitlock);
 	if (iv->flags & AIRQ_IV_CACHELINE)
 		dma_pool_free(airq_iv_cache, iv->vector, iv->vector_dma);
-	else
+	else if (!(iv->flags & AIRQ_IV_GUESTVEC))
 		cio_dma_free(iv->vector, iv_size(iv->bits));
 	kfree(iv->avail);
 	kfree(iv);
drivers/s390/virtio/virtio_ccw.c +1 -1
index 52c376d15978..410498d693f8 100644
@@ -241,7 +241,7 @@ static struct airq_info *new_airq_info(int index)
 		return NULL;
 	rwlock_init(&info->lock);
 	info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR
-				   | AIRQ_IV_CACHELINE);
+				   | AIRQ_IV_CACHELINE, NULL);
 	if (!info->aiv) {
 		kfree(info);
 		return NULL;
Signature · 1 line
2.27.0
Reviewed-by on v7 6/22
#7 lore raw

Re: [PATCH v7 06/22] s390/airq: allow for airq structure that uses an input vector

Quoted 14 lines · Matthew Rosato

When doing device passthrough where interrupts are being forwarded from host to guest, we wish to use a pinned section of guest memory as the vector (the same memory used by the guest as the vector). To accomplish this, add a new parameter for airq_iv_create which allows passing an existing vector to be used instead of allocating a new one. The caller is responsible for ensuring the vector is pinned in memory as well as for unpinning the memory when the vector is no longer needed.

A subsequent patch will use this new parameter for zPCI interpretation.

Quoted 6 lines
---
  arch/s390/include/asm/airq.h     |  4 +++-
  arch/s390/pci/pci_irq.c          |  8 ++++----
  drivers/s390/cio/airq.c          | 10 +++++++---
  drivers/s390/virtio/virtio_ccw.c |  2 +-
  4 files changed, 15 insertions(+), 9 deletions(-)
author maintainer
#8 lore raw

PATCH v7 7/22 s390/pci: externalize the SIC operation controls and routine

In reply to Matthew Rosato

A subsequent patch will be issuing SIC from KVM -- export the necessary routine and make the operation control definitions available from a header. Because the routine will now be exported, let's rename __zpci_set_irq_ctrl to zpci_set_irq_ctrl and get rid of the zero'd iib wrapper function of the same name.

---
 arch/s390/include/asm/pci_insn.h | 17 +++++++++--------
 arch/s390/pci/pci_insn.c         |  3 ++-
 arch/s390/pci/pci_irq.c          | 26 ++++++++++++--------------
 3 files changed, 23 insertions(+), 23 deletions(-)
arch/s390/include/asm/pci_insn.h +9 -8
index 61cf9531f68f..5331082fa516 100644
@@ -98,6 +98,14 @@ struct zpci_fib {
 	u32 gd;
 } __packed __aligned(8);
 
+/* Set Interruption Controls Operation Controls  */
+#define	SIC_IRQ_MODE_ALL		0
+#define	SIC_IRQ_MODE_SINGLE		1
+#define	SIC_IRQ_MODE_DIRECT		4
+#define	SIC_IRQ_MODE_D_ALL		16
+#define	SIC_IRQ_MODE_D_SINGLE		17
+#define	SIC_IRQ_MODE_SET_CPU		18
+
 /* directed interruption information block */
 struct zpci_diib {
 	u32 : 1;
@@ -134,13 +142,6 @@ int __zpci_store(u64 data, u64 req, u64 offset);
 int zpci_store(const volatile void __iomem *addr, u64 data, unsigned long len);
 int __zpci_store_block(const u64 *data, u64 req, u64 offset);
 void zpci_barrier(void);
-int __zpci_set_irq_ctrl(u16 ctl, u8 isc, union zpci_sic_iib *iib);
-
-static inline int zpci_set_irq_ctrl(u16 ctl, u8 isc)
-{
-	union zpci_sic_iib iib = {{0}};
-
-	return __zpci_set_irq_ctrl(ctl, isc, &iib);
-}
+int zpci_set_irq_ctrl(u16 ctl, u8 isc, union zpci_sic_iib *iib);
 
 #endif
arch/s390/pci/pci_insn.c +2 -1
index 1710d006ee93..4c6967b73932 100644
@@ -98,7 +98,7 @@ int zpci_refresh_trans(u64 fn, u64 addr, u64 range)
 }
 
 /* Set Interruption Controls */
-int __zpci_set_irq_ctrl(u16 ctl, u8 isc, union zpci_sic_iib *iib)
+int zpci_set_irq_ctrl(u16 ctl, u8 isc, union zpci_sic_iib *iib)
 {
 	if (!test_facility(72))
 		return -EIO;
@@ -109,6 +109,7 @@ int __zpci_set_irq_ctrl(u16 ctl, u8 isc, union zpci_sic_iib *iib)
 
 	return 0;
 }
+EXPORT_SYMBOL_GPL(zpci_set_irq_ctrl);
 
 /* PCI Load */
 static inline int ____pcilg(u64 *data, u64 req, u64 offset, u8 *status)
arch/s390/pci/pci_irq.c +12 -14
index 87c7d121c255..f2b3145b6697 100644
@@ -15,13 +15,6 @@
 
 static enum {FLOATING, DIRECTED} irq_delivery;
 
-#define	SIC_IRQ_MODE_ALL		0
-#define	SIC_IRQ_MODE_SINGLE		1
-#define	SIC_IRQ_MODE_DIRECT		4
-#define	SIC_IRQ_MODE_D_ALL		16
-#define	SIC_IRQ_MODE_D_SINGLE		17
-#define	SIC_IRQ_MODE_SET_CPU		18
-
 /*
  * summary bit vector
  * FLOATING - summary bit per function
@@ -154,6 +147,7 @@ static struct irq_chip zpci_irq_chip = {
 static void zpci_handle_cpu_local_irq(bool rescan)
 {
 	struct airq_iv *dibv = zpci_ibv[smp_processor_id()];
+	union zpci_sic_iib iib = {{0}};
 	unsigned long bit;
 	int irqs_on = 0;
 
@@ -165,7 +159,7 @@ static void zpci_handle_cpu_local_irq(bool rescan)
 				/* End of second scan with interrupts on. */
 				break;
 			/* First scan complete, reenable interrupts. */
-			if (zpci_set_irq_ctrl(SIC_IRQ_MODE_D_SINGLE, PCI_ISC))
+			if (zpci_set_irq_ctrl(SIC_IRQ_MODE_D_SINGLE, PCI_ISC, &iib))
 				break;
 			bit = 0;
 			continue;
@@ -193,6 +187,7 @@ static void zpci_handle_remote_irq(void *data)
 static void zpci_handle_fallback_irq(void)
 {
 	struct cpu_irq_data *cpu_data;
+	union zpci_sic_iib iib = {{0}};
 	unsigned long cpu;
 	int irqs_on = 0;
 
@@ -203,7 +198,7 @@ static void zpci_handle_fallback_irq(void)
 				/* End of second scan with interrupts on. */
 				break;
 			/* First scan complete, reenable interrupts. */
-			if (zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, PCI_ISC))
+			if (zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, PCI_ISC, &iib))
 				break;
 			cpu = 0;
 			continue;
@@ -234,6 +229,7 @@ static void zpci_directed_irq_handler(struct airq_struct *airq,
 static void zpci_floating_irq_handler(struct airq_struct *airq,
 				      struct tpi_info *tpi_info)
 {
+	union zpci_sic_iib iib = {{0}};
 	unsigned long si, ai;
 	struct airq_iv *aibv;
 	int irqs_on = 0;
@@ -247,7 +243,7 @@ static void zpci_floating_irq_handler(struct airq_struct *airq,
 				/* End of second scan with interrupts on. */
 				break;
 			/* First scan complete, reenable interrupts. */
-			if (zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, PCI_ISC))
+			if (zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, PCI_ISC, &iib))
 				break;
 			si = 0;
 			continue;
@@ -407,11 +403,12 @@ static struct airq_struct zpci_airq = {
 static void __init cpu_enable_directed_irq(void *unused)
 {
 	union zpci_sic_iib iib = {{0}};
+	union zpci_sic_iib ziib = {{0}};
 
 	iib.cdiib.dibv_addr = (u64) zpci_ibv[smp_processor_id()]->vector;
 
-	__zpci_set_irq_ctrl(SIC_IRQ_MODE_SET_CPU, 0, &iib);
-	zpci_set_irq_ctrl(SIC_IRQ_MODE_D_SINGLE, PCI_ISC);
+	zpci_set_irq_ctrl(SIC_IRQ_MODE_SET_CPU, 0, &iib);
+	zpci_set_irq_ctrl(SIC_IRQ_MODE_D_SINGLE, PCI_ISC, &ziib);
 }
 
 static int __init zpci_directed_irq_init(void)
@@ -426,7 +423,7 @@ static int __init zpci_directed_irq_init(void)
 	iib.diib.isc = PCI_ISC;
 	iib.diib.nr_cpus = num_possible_cpus();
 	iib.diib.disb_addr = virt_to_phys(zpci_sbv->vector);
-	__zpci_set_irq_ctrl(SIC_IRQ_MODE_DIRECT, 0, &iib);
+	zpci_set_irq_ctrl(SIC_IRQ_MODE_DIRECT, 0, &iib);
 
 	zpci_ibv = kcalloc(num_possible_cpus(), sizeof(*zpci_ibv),
 			   GFP_KERNEL);
@@ -471,6 +468,7 @@ static int __init zpci_floating_irq_init(void)
 
 int __init zpci_irq_init(void)
 {
+	union zpci_sic_iib iib = {{0}};
 	int rc;
 
 	irq_delivery = sclp.has_dirq ? DIRECTED : FLOATING;
@@ -502,7 +500,7 @@ int __init zpci_irq_init(void)
 	 * Enable floating IRQs (with suppression after one IRQ). When using
 	 * directed IRQs this enables the fallback path.
 	 */
-	zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, PCI_ISC);
+	zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, PCI_ISC, &iib);
 
 	return 0;
 out_airq:
Signature · 1 line
2.27.0
author maintainer
#9 lore raw

PATCH v7 8/22 s390/pci: stash associated GISA designation

In reply to Matthew Rosato

For passthrough devices, we will need to know the GISA designation of the guest if interpretation facilities are to be used. Setup to stash this in the zdev and set a default of 0 (no GISA designation) for now; a subsequent patch will set a valid GISA designation for passthrough devices. Also, extend mpcific routines to specify this stashed designation as part of the mpcific command.

---
 arch/s390/include/asm/pci.h     | 1 +
 arch/s390/include/asm/pci_clp.h | 3 ++-
 arch/s390/pci/pci.c             | 6 ++++++
 arch/s390/pci/pci_clp.c         | 5 +++++
 arch/s390/pci/pci_irq.c         | 5 +++++
 5 files changed, 19 insertions(+), 1 deletion(-)
arch/s390/include/asm/pci.h +1 -0
index fdb9745ee998..42a4a312a6dd 100644
@@ -123,6 +123,7 @@ struct zpci_dev {
 	enum zpci_state state;
 	u32		fid;		/* function ID, used by sclp */
 	u32		fh;		/* function handle, used by insn's */
+	u32		gisa;		/* GISA designation for passthrough */
 	u16		vfn;		/* virtual function number */
 	u16		pchid;		/* physical channel ID */
 	u8		pfgid;		/* function group ID */
arch/s390/include/asm/pci_clp.h +2 -1
index 1f4b666e85ee..f3286bc5ba6e 100644
@@ -173,7 +173,8 @@ struct clp_req_set_pci {
 	u16 reserved2;
 	u8 oc;				/* operation controls */
 	u8 ndas;			/* number of dma spaces */
-	u64 reserved3;
+	u32 reserved3;
+	u32 gisa;			/* GISA designation */
 } __packed;
 
 /* Set PCI function response */
arch/s390/pci/pci.c +6 -0
index e563cb65c0c4..a86cd1cbb80e 100644
@@ -120,6 +120,7 @@ int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
 	fib.pba = base;
 	fib.pal = limit;
 	fib.iota = iota | ZPCI_IOTA_RTTO_FLAG;
+	fib.gd = zdev->gisa;
 	cc = zpci_mod_fc(req, &fib, &status);
 	if (cc)
 		zpci_dbg(3, "reg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, status);
@@ -133,6 +134,8 @@ int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
 	struct zpci_fib fib = {0};
 	u8 cc, status;
 
+	fib.gd = zdev->gisa;
+
 	cc = zpci_mod_fc(req, &fib, &status);
 	if (cc)
 		zpci_dbg(3, "unreg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, status);
@@ -160,6 +163,7 @@ int zpci_fmb_enable_device(struct zpci_dev *zdev)
 	atomic64_set(&zdev->unmapped_pages, 0);
 
 	fib.fmb_addr = virt_to_phys(zdev->fmb);
+	fib.gd = zdev->gisa;
 	cc = zpci_mod_fc(req, &fib, &status);
 	if (cc) {
 		kmem_cache_free(zdev_fmb_cache, zdev->fmb);
@@ -178,6 +182,8 @@ int zpci_fmb_disable_device(struct zpci_dev *zdev)
 	if (!zdev->fmb)
 		return -EINVAL;
 
+	fib.gd = zdev->gisa;
+
 	/* Function measurement is disabled if fmb address is zero */
 	cc = zpci_mod_fc(req, &fib, &status);
 	if (cc == 3) /* Function already gone. */
arch/s390/pci/pci_clp.c +5 -0
index 1057d7af4a55..deed35edea14 100644
@@ -229,12 +229,16 @@ static int clp_set_pci_fn(struct zpci_dev *zdev, u32 *fh, u8 nr_dma_as, u8 comma
 {
 	struct clp_req_rsp_set_pci *rrb;
 	int rc, retries = 100;
+	u32 gisa = 0;
 
 	*fh = 0;
 	rrb = clp_alloc_block(GFP_KERNEL);
 	if (!rrb)
 		return -ENOMEM;
 
+	if (command != CLP_SET_DISABLE_PCI_FN)
+		gisa = zdev->gisa;
+
 	do {
 		memset(rrb, 0, sizeof(*rrb));
 		rrb->request.hdr.len = sizeof(rrb->request);
@@ -243,6 +247,7 @@ static int clp_set_pci_fn(struct zpci_dev *zdev, u32 *fh, u8 nr_dma_as, u8 comma
 		rrb->request.fh = zdev->fh;
 		rrb->request.oc = command;
 		rrb->request.ndas = nr_dma_as;
+		rrb->request.gisa = gisa;
 
 		rc = clp_req(rrb, CLP_LPS_PCI);
 		if (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY) {
arch/s390/pci/pci_irq.c +5 -0
index f2b3145b6697..a2b42a63a53b 100644
@@ -43,6 +43,7 @@ static int zpci_set_airq(struct zpci_dev *zdev)
 	fib.fmt0.aibvo = 0;	/* each zdev has its own interrupt vector */
 	fib.fmt0.aisb = virt_to_phys(zpci_sbv->vector) + (zdev->aisb / 64) * 8;
 	fib.fmt0.aisbo = zdev->aisb & 63;
+	fib.gd = zdev->gisa;
 
 	return zpci_mod_fc(req, &fib, &status) ? -EIO : 0;
 }
@@ -54,6 +55,8 @@ static int zpci_clear_airq(struct zpci_dev *zdev)
 	struct zpci_fib fib = {0};
 	u8 cc, status;
 
+	fib.gd = zdev->gisa;
+
 	cc = zpci_mod_fc(req, &fib, &status);
 	if (cc == 3 || (cc == 1 && status == 24))
 		/* Function already gone or IRQs already deregistered. */
@@ -72,6 +75,7 @@ static int zpci_set_directed_irq(struct zpci_dev *zdev)
 	fib.fmt = 1;
 	fib.fmt1.noi = zdev->msi_nr_irqs;
 	fib.fmt1.dibvo = zdev->msi_first_bit;
+	fib.gd = zdev->gisa;
 
 	return zpci_mod_fc(req, &fib, &status) ? -EIO : 0;
 }
@@ -84,6 +88,7 @@ static int zpci_clear_directed_irq(struct zpci_dev *zdev)
 	u8 cc, status;
 
 	fib.fmt = 1;
+	fib.gd = zdev->gisa;
 	cc = zpci_mod_fc(req, &fib, &status);
 	if (cc == 3 || (cc == 1 && status == 24))
 		/* Function already gone or IRQs already deregistered. */
Signature · 1 line
2.27.0
author maintainer
#10 lore raw

PATCH v7 9/22 s390/pci: stash dtsm and maxstbl

In reply to Matthew Rosato

Store information about what IOAT designation types are supported by underlying hardware as well as the largest store block size allowed. These values will be needed by passthrough.

---
 arch/s390/include/asm/pci.h     | 2 ++
 arch/s390/include/asm/pci_clp.h | 6 ++++--
 arch/s390/pci/pci_clp.c         | 2 ++
 3 files changed, 8 insertions(+), 2 deletions(-)
arch/s390/include/asm/pci.h +2 -0
index 42a4a312a6dd..4c5b8fbc2079 100644
@@ -126,9 +126,11 @@ struct zpci_dev {
 	u32		gisa;		/* GISA designation for passthrough */
 	u16		vfn;		/* virtual function number */
 	u16		pchid;		/* physical channel ID */
+	u16		maxstbl;	/* Maximum store block size */
 	u8		pfgid;		/* function group ID */
 	u8		pft;		/* pci function type */
 	u8		port;
+	u8		dtsm;		/* Supported DT mask */
 	u8		rid_available	: 1;
 	u8		has_hp_slot	: 1;
 	u8		has_resources	: 1;
arch/s390/include/asm/pci_clp.h +4 -2
index f3286bc5ba6e..d6189ed14f84 100644
@@ -153,9 +153,11 @@ struct clp_rsp_query_pci_grp {
 	u8			:  6;
 	u8 frame		:  1;
 	u8 refresh		:  1;	/* TLB refresh mode */
-	u16 reserved2;
+	u16			:  3;
+	u16 maxstbl		: 13;	/* Maximum store block size */
 	u16 mui;
-	u16			: 16;
+	u8 dtsm;			/* Supported DT mask */
+	u8 reserved3;
 	u16 maxfaal;
 	u16			:  4;
 	u16 dnoi		: 12;
arch/s390/pci/pci_clp.c +2 -0
index deed35edea14..1edb07ceb766 100644
@@ -106,6 +106,8 @@ static void clp_store_query_pci_fngrp(struct zpci_dev *zdev,
 	zdev->max_msi = response->noi;
 	zdev->fmb_update = response->mui;
 	zdev->version = response->version;
+	zdev->maxstbl = response->maxstbl;
+	zdev->dtsm = response->dtsm;
 
 	switch (response->version) {
 	case 1:
Signature · 1 line
2.27.0
author maintainer
#11 lore raw

PATCH v7 10/22 vfio/pci: introduce CONFIG_VFIO_PCI_ZDEV_KVM

In reply to Matthew Rosato

The current contents of vfio-pci-zdev are today only useful in a KVM environment; let's tie everything currently under vfio-pci-zdev to this Kconfig statement and require KVM in this case, reducing complexity (e.g. symbol lookups).

---
 drivers/vfio/pci/Kconfig      | 11 +++++++++++
 drivers/vfio/pci/Makefile     |  2 +-
 include/linux/vfio_pci_core.h |  2 +-
 3 files changed, 13 insertions(+), 2 deletions(-)
drivers/vfio/pci/Kconfig +11 -0
index 4da1914425e1..ac1290d484e1 100644
@@ -44,6 +44,17 @@ config VFIO_PCI_IGD
 	  To enable Intel IGD assignment through vfio-pci, say Y.
 endif
 
+config VFIO_PCI_ZDEV_KVM
+	bool "VFIO PCI extensions for s390x KVM passthrough"
+	depends on S390 && KVM
+	default y
+	help
+	  Support s390x-specific extensions to enable support for enhancements
+	  to KVM passthrough capabilities, such as interpretive execution of
+	  zPCI instructions.
+
+	  To enable s390x KVM vfio-pci extensions, say
+
 source "drivers/vfio/pci/mlx5/Kconfig"
 
 source "drivers/vfio/pci/hisilicon/Kconfig"
drivers/vfio/pci/Makefile +1 -1
index 7052ebd893e0..24c524224da5 100644
@@ -1,7 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
 vfio-pci-core-y := vfio_pci_core.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o
-vfio-pci-core-$(CONFIG_S390) += vfio_pci_zdev.o
+vfio-pci-core-$(CONFIG_VFIO_PCI_ZDEV_KVM) += vfio_pci_zdev.o
 obj-$(CONFIG_VFIO_PCI_CORE) += vfio-pci-core.o
 
 vfio-pci-y := vfio_pci.o
include/linux/vfio_pci_core.h +1 -1
index 48f2dd3c568c..cfc9eb1f0b27 100644
@@ -206,7 +206,7 @@ static inline int vfio_pci_igd_init(struct vfio_pci_core_device *vdev)
 }
 #endif
 
-#ifdef CONFIG_S390
+#ifdef CONFIG_VFIO_PCI_ZDEV_KVM
 extern int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
 				       struct vfio_info_cap *caps);
 #else
Signature · 1 line
2.27.0
#12 lore raw

Re: [PATCH v7 10/22] vfio/pci: introduce CONFIG_VFIO_PCI_ZDEV_KVM

Quoted 7 lines · Matthew Rosato
drivers/vfio/pci/Kconfig +1 -0
index 4da1914425e1..ac1290d484e1 100644
+++ b/drivers/vfio/pci/Kconfig
@@ -44,6 +44,17 @@ config VFIO_PCI_IGD
 	  To enable Intel IGD assignment through vfio-pci, say Y.
 endif
 

drivers/vfio/pci/Kconfig config VFIO_PCI_IGD · line 56

Quoted 10 lines
drivers/vfio/pci/Kconfig +10 -0
+config VFIO_PCI_ZDEV_KVM
+	bool "VFIO PCI extensions for s390x KVM passthrough"
+	depends on S390 && KVM
+	default y
+	help
+	  Support s390x-specific extensions to enable support for enhancements
+	  to KVM passthrough capabilities, such as interpretive execution of
+	  zPCI instructions.
+
+	  To enable s390x KVM vfio-pci extensions, say

"say Y"

author maintainer
#13 lore raw

PATCH v7 11/22 KVM: s390: pci: add basic kvm_zdev structure

In reply to Matthew Rosato

This structure will be used to carry kvm passthrough information related to zPCI devices.

---
 arch/s390/include/asm/pci.h |  3 +++
 arch/s390/kvm/Makefile      |  1 +
 arch/s390/kvm/pci.c         | 36 ++++++++++++++++++++++++++++++++++++
 arch/s390/kvm/pci.h         | 21 +++++++++++++++++++++
 4 files changed, 61 insertions(+)
 create mode 100644 arch/s390/kvm/pci.c
 create mode 100644 arch/s390/kvm/pci.h
arch/s390/include/asm/pci.h +3 -0
index 4c5b8fbc2079..50f1851edfbe 100644
@@ -97,6 +97,7 @@ struct zpci_bar_struct {
 };
 
 struct s390_domain;
+struct kvm_zdev;
 
 #define ZPCI_FUNCTIONS_PER_BUS 256
 struct zpci_bus {
@@ -189,7 +190,9 @@ struct zpci_dev {
 
 	struct dentry	*debugfs_dev;
 
+	/* IOMMU and passthrough */
 	struct s390_domain *s390_domain; /* s390 IOMMU domain data */
+	struct kvm_zdev *kzdev;
 };
 
 static inline bool zdev_enabled(struct zpci_dev *zdev)
arch/s390/kvm/Makefile +1 -0
index 26f4a74e5ce4..02217fb4ae10 100644
@@ -10,4 +10,5 @@ ccflags-y := -Ivirt/kvm -Iarch/s390/kvm
 kvm-y += kvm-s390.o intercept.o interrupt.o priv.o sigp.o
 kvm-y += diag.o gaccess.o guestdbg.o vsie.o pv.o
 
+kvm-$(CONFIG_VFIO_PCI_ZDEV_KVM) += pci.o
 obj-$(CONFIG_KVM) += kvm.o
arch/s390/kvm/pci.c new file +36 -0
new file mode 100644
index 000000000000..21c2be5c2713
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * s390 kvm PCI passthrough support
+ *
+ * Copyright IBM Corp. 2022
+ *
+ *    Author(s): Matthew Rosato <[email protected]>
+ */
+
+#include <linux/kvm_host.h>
+#include <linux/pci.h>
+#include "pci.h"
+
+static int kvm_s390_pci_dev_open(struct zpci_dev *zdev)
+{
+	struct kvm_zdev *kzdev;
+
+	kzdev = kzalloc(sizeof(struct kvm_zdev), GFP_KERNEL);
+	if (!kzdev)
+		return -ENOMEM;
+
+	kzdev->zdev = zdev;
+	zdev->kzdev = kzdev;
+
+	return 0;
+}
+
+static void kvm_s390_pci_dev_release(struct zpci_dev *zdev)
+{
+	struct kvm_zdev *kzdev;
+
+	kzdev = zdev->kzdev;
+	WARN_ON(kzdev->zdev != zdev);
+	zdev->kzdev = 0;
+	kfree(kzdev);
+}
arch/s390/kvm/pci.h new file +21 -0
new file mode 100644
index 000000000000..ce93978e8913
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * s390 kvm PCI passthrough support
+ *
+ * Copyright IBM Corp. 2022
+ *
+ *    Author(s): Matthew Rosato <[email protected]>
+ */
+
+#ifndef __KVM_S390_PCI_H
+#define __KVM_S390_PCI_H
+
+#include <linux/kvm_host.h>
+#include <linux/pci.h>
+
+struct kvm_zdev {
+	struct zpci_dev *zdev;
+	struct kvm *kvm;
+};
+
+#endif /* __KVM_S390_PCI_H */
Signature · 1 line
2.27.0
author maintainer
#14 lore raw

PATCH v7 12/22 KVM: s390: pci: do initial setup for AEN interpretation

In reply to Matthew Rosato

Initial setup for Adapter Event Notification Interpretation for zPCI passthrough devices. Specifically, allocate a structure for forwarding of adapter events and pass the address of this structure to firmware.

---
 arch/s390/include/asm/pci.h      |   4 +
 arch/s390/include/asm/pci_insn.h |  12 +++
 arch/s390/kvm/interrupt.c        |  14 +++
 arch/s390/kvm/kvm-s390.c         |  11 +++
 arch/s390/kvm/pci.c              | 160 +++++++++++++++++++++++++++++++
 arch/s390/kvm/pci.h              |  49 ++++++++++
 arch/s390/pci/pci.c              |   6 ++
 7 files changed, 256 insertions(+)
arch/s390/include/asm/pci.h +4 -0
index 50f1851edfbe..322060a75d9f 100644
@@ -9,6 +9,7 @@
 #include <asm-generic/pci.h>
 #include <asm/pci_clp.h>
 #include <asm/pci_debug.h>
+#include <asm/pci_insn.h>
 #include <asm/sclp.h>
 
 #define PCIBIOS_MIN_IO		0x1000
@@ -204,6 +205,9 @@ extern const struct attribute_group *zpci_attr_groups[];
 extern unsigned int s390_pci_force_floating __initdata;
 extern unsigned int s390_pci_no_rid;
 
+extern union zpci_sic_iib *zpci_aipb;
+extern struct airq_iv *zpci_aif_sbv;
+
 /* -----------------------------------------------------------------------------
   Prototypes
 ----------------------------------------------------------------------------- */
arch/s390/include/asm/pci_insn.h +12 -0
index 5331082fa516..e5f57cfe1d45 100644
@@ -101,6 +101,7 @@ struct zpci_fib {
 /* Set Interruption Controls Operation Controls  */
 #define	SIC_IRQ_MODE_ALL		0
 #define	SIC_IRQ_MODE_SINGLE		1
+#define	SIC_SET_AENI_CONTROLS		2
 #define	SIC_IRQ_MODE_DIRECT		4
 #define	SIC_IRQ_MODE_D_ALL		16
 #define	SIC_IRQ_MODE_D_SINGLE		17
@@ -127,9 +128,20 @@ struct zpci_cdiib {
 	u64 : 64;
 } __packed __aligned(8);
 
+/* adapter interruption parameters block */
+struct zpci_aipb {
+	u64 faisb;
+	u64 gait;
+	u16 : 13;
+	u16 afi : 3;
+	u32 : 32;
+	u16 faal;
+} __packed __aligned(8);
+
 union zpci_sic_iib {
 	struct zpci_diib diib;
 	struct zpci_cdiib cdiib;
+	struct zpci_aipb aipb;
 };
 
 DECLARE_STATIC_KEY_FALSE(have_mio);
arch/s390/kvm/interrupt.c +14 -0
index 17ff475157d8..37ff4358121a 100644
@@ -32,6 +32,7 @@
 #include "kvm-s390.h"
 #include "gaccess.h"
 #include "trace-s390.h"
+#include "pci.h"
 
 #define PFAULT_INIT 0x0600
 #define PFAULT_DONE 0x0680
@@ -3328,6 +3329,11 @@ void kvm_s390_gib_destroy(void)
 {
 	if (!gib)
 		return;
+	if (kvm_s390_pci_interp_allowed() && aift) {
+		mutex_lock(&aift->aift_lock);
+		kvm_s390_pci_aen_exit();
+		mutex_unlock(&aift->aift_lock);
+	}
 	chsc_sgib(0);
 	unregister_adapter_interrupt(&gib_alert_irq);
 	free_page((unsigned long)gib);
@@ -3365,6 +3371,14 @@ int kvm_s390_gib_init(u8 nisc)
 		goto out_unreg_gal;
 	}
 
+	if (kvm_s390_pci_interp_allowed()) {
+		if (kvm_s390_pci_aen_init(nisc)) {
+			pr_err("Initializing AEN for PCI failed\n");
+			rc = -EIO;
+			goto out_unreg_gal;
+		}
+	}
+
 	KVM_EVENT(3, "gib 0x%pK (nisc=%d) initialized", gib, gib->nisc);
 	goto out;
 
arch/s390/kvm/kvm-s390.c +11 -0
index 76ad6408cb2c..efc0d8e22308 100644
@@ -47,6 +47,7 @@
 #include <asm/fpu/api.h>
 #include "kvm-s390.h"
 #include "gaccess.h"
+#include "pci.h"
 
 #define CREATE_TRACE_POINTS
 #include "trace.h"
@@ -502,6 +503,14 @@ int kvm_arch_init(void *opaque)
 		goto out;
 	}
 
+	if (kvm_s390_pci_interp_allowed()) {
+		rc = kvm_s390_pci_init();
+		if (rc) {
+			pr_err("Unable to allocate AIFT for PCI\n");
+			goto out;
+		}
+	}
+
 	rc = kvm_s390_gib_init(GAL_ISC);
 	if (rc)
 		goto out;
@@ -516,6 +525,8 @@ int kvm_arch_init(void *opaque)
 void kvm_arch_exit(void)
 {
 	kvm_s390_gib_destroy();
+	if (kvm_s390_pci_interp_allowed())
+		kvm_s390_pci_exit();
 	debug_unregister(kvm_s390_dbf);
 	debug_unregister(kvm_s390_dbf_uv);
 }
arch/s390/kvm/pci.c +160 -0

171 lines not on this page · raw message

arch/s390/kvm/pci.h +49 -0

59 lines not on this page · raw message

arch/s390/pci/pci.c +6 -0
index a86cd1cbb80e..f0a439c43395 100644
@@ -61,6 +61,12 @@ DEFINE_STATIC_KEY_FALSE(have_mio);
 
 static struct kmem_cache *zdev_fmb_cache;
 
+/* AEN structures that must be preserved over KVM module re-insertion */
+union zpci_sic_iib *zpci_aipb;
+EXPORT_SYMBOL_GPL(zpci_aipb);
+struct airq_iv *zpci_aif_sbv;
+EXPORT_SYMBOL_GPL(zpci_aif_sbv);
+
 struct zpci_dev *get_zdev_by_fid(u32 fid)
 {
 	struct zpci_dev *tmp, *zdev = NULL;
Signature · 1 line
2.27.0
author maintainer
#15 lore raw

PATCH v7 13/22 KVM: s390: pci: enable host forwarding of Adapter Event Notifications

In reply to Matthew Rosato

In cases where interrupts are not forwarded to the guest via firmware, KVM is responsible for ensuring delivery. When an interrupt presents with the forwarding bit, we must process the forwarding tables until all interrupts are delivered.

---
 arch/s390/include/asm/kvm_host.h |  1 +
 arch/s390/include/asm/tpi.h      | 13 ++++++
 arch/s390/kvm/interrupt.c        | 78 +++++++++++++++++++++++++++++++-
 arch/s390/kvm/kvm-s390.c         |  3 +-
 arch/s390/kvm/pci.h              | 10 ++++
 5 files changed, 103 insertions(+), 2 deletions(-)
arch/s390/include/asm/kvm_host.h +1 -0
index 766028d54a3e..c1518a505060 100644
@@ -759,6 +759,7 @@ struct kvm_vm_stat {
 	u64 inject_pfault_done;
 	u64 inject_service_signal;
 	u64 inject_virtio;
+	u64 aen_forward;
 };
 
 struct kvm_arch_memory_slot {
arch/s390/include/asm/tpi.h +13 -0
index 1ac538b8cbf5..f76e5fdff23a 100644
@@ -19,6 +19,19 @@ struct tpi_info {
 	u32 :12;
 } __packed __aligned(4);
 
+/* I/O-Interruption Code as stored by TPI for an Adapter I/O */
+struct tpi_adapter_info {
+	u32 aism:8;
+	u32 :22;
+	u32 error:1;
+	u32 forward:1;
+	u32 reserved;
+	u32 adapter_IO:1;
+	u32 directed_irq:1;
+	u32 isc:3;
+	u32 :27;
+} __packed __aligned(4);
+
 #endif /* __ASSEMBLY__ */
 
 #endif /* _ASM_S390_TPI_H */
arch/s390/kvm/interrupt.c +77 -1

88 lines not on this page · raw message

arch/s390/kvm/kvm-s390.c +2 -1
index efc0d8e22308..5a0fbfd19c4a 100644
@@ -64,7 +64,8 @@ const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
 	STATS_DESC_COUNTER(VM, inject_float_mchk),
 	STATS_DESC_COUNTER(VM, inject_pfault_done),
 	STATS_DESC_COUNTER(VM, inject_service_signal),
-	STATS_DESC_COUNTER(VM, inject_virtio)
+	STATS_DESC_COUNTER(VM, inject_virtio),
+	STATS_DESC_COUNTER(VM, aen_forward)
 };
 
 const struct kvm_stats_header kvm_vm_stats_header = {
arch/s390/kvm/pci.h +10 -0
index c357d900f8b0..9f7828d97605 100644
@@ -13,6 +13,7 @@
 #include <linux/kvm_host.h>
 #include <linux/pci.h>
 #include <linux/mutex.h>
+#include <linux/kvm_host.h>
 #include <asm/airq.h>
 #include <asm/cpu.h>
 
@@ -40,6 +41,15 @@ struct zpci_aift {
 
 extern struct zpci_aift *aift;
 
+static inline struct kvm *kvm_s390_pci_si_to_kvm(struct zpci_aift *aift,
+						 unsigned long si)
+{
+	if (!IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM) || aift->kzdev == 0 ||
+	    aift->kzdev[si] == 0)
+		return 0;
+	return aift->kzdev[si]->kvm;
+};
+
 int kvm_s390_pci_aen_init(u8 nisc);
 void kvm_s390_pci_aen_exit(void);
 
Signature · 1 line
2.27.0
author maintainer
#16 lore raw

PATCH v7 14/22 KVM: s390: mechanism to enable guest zPCI Interpretation

In reply to Matthew Rosato

The guest must have access to certain facilities in order to allow interpretive execution of zPCI instructions and adapter event notifications. However, there are some cases where a guest might disable interpretation -- provide a mechanism via which we can defer enabling the associated zPCI interpretation facilities until the guest indicates it wishes to use them.

---
 arch/s390/include/asm/kvm_host.h |  4 ++++
 arch/s390/kvm/kvm-s390.c         | 38 ++++++++++++++++++++++++++++++++
 arch/s390/kvm/kvm-s390.h         | 10 +++++++++
 3 files changed, 52 insertions(+)
arch/s390/include/asm/kvm_host.h +4 -0
index c1518a505060..8e381603b6a7 100644
@@ -254,7 +254,10 @@ struct kvm_s390_sie_block {
 #define ECB2_IEP	0x20
 #define ECB2_PFMFI	0x08
 #define ECB2_ESCA	0x04
+#define ECB2_ZPCI_LSI	0x02
 	__u8    ecb2;                   /* 0x0062 */
+#define ECB3_AISI	0x20
+#define ECB3_AISII	0x10
 #define ECB3_DEA 0x08
 #define ECB3_AES 0x04
 #define ECB3_RI  0x01
@@ -940,6 +943,7 @@ struct kvm_arch{
 	int use_cmma;
 	int use_pfmfi;
 	int use_skf;
+	int use_zpci_interp;
 	int user_cpu_state_ctrl;
 	int user_sigp;
 	int user_stsi;
arch/s390/kvm/kvm-s390.c +38 -0
index 5a0fbfd19c4a..0797661732cc 100644
@@ -1031,6 +1031,42 @@ static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
 	return 0;
 }
 
+static void kvm_s390_vcpu_pci_setup(struct kvm_vcpu *vcpu)
+{
+	/* Only set the ECB bits after guest requests zPCI interpretation */
+	if (!vcpu->kvm->arch.use_zpci_interp)
+		return;
+
+	vcpu->arch.sie_block->ecb2 |= ECB2_ZPCI_LSI;
+	vcpu->arch.sie_block->ecb3 |= ECB3_AISII + ECB3_AISI;
+}
+
+void kvm_s390_vcpu_pci_enable_interp(struct kvm *kvm)
+{
+	struct kvm_vcpu *vcpu;
+	unsigned long i;
+
+	lockdep_assert_held(&kvm->lock);
+
+	/*
+	 * If host is configured for PCI and the necessary facilities are
+	 * available, turn on interpretation for the life of this guest
+	 */
+	if (!kvm_s390_pci_interp_allowed())
+		return;
+
+	kvm->arch.use_zpci_interp = 1;
+
+	kvm_s390_vcpu_block_all(kvm);
+
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		kvm_s390_vcpu_pci_setup(vcpu);
+		kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
+	}
+
+	kvm_s390_vcpu_unblock_all(kvm);
+}
+
 static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req)
 {
 	unsigned long cx;
@@ -3340,6 +3376,8 @@ static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu)
 
 	kvm_s390_vcpu_crypto_setup(vcpu);
 
+	kvm_s390_vcpu_pci_setup(vcpu);
+
 	mutex_lock(&vcpu->kvm->lock);
 	if (kvm_s390_pv_is_protected(vcpu->kvm)) {
 		rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc);
arch/s390/kvm/kvm-s390.h +10 -0
index 497d52a83c78..975cffaf13de 100644
@@ -507,6 +507,16 @@ void kvm_s390_reinject_machine_check(struct kvm_vcpu *vcpu,
  */
 void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm);
 
+/**
+ * kvm_s390_vcpu_pci_enable_interp
+ *
+ * Set the associated PCI attributes for each vcpu to allow for zPCI Load/Store
+ * interpretation as well as adapter interruption forwarding.
+ *
+ * @kvm: the KVM guest
+ */
+void kvm_s390_vcpu_pci_enable_interp(struct kvm *kvm);
+
 /**
  * diag9c_forwarding_hz
  *
Signature · 1 line
2.27.0
#17 lore raw

Re: [PATCH v7 14/22] KVM: s390: mechanism to enable guest zPCI Interpretation

Quoted 58 lines · Matthew Rosato

The guest must have access to certain facilities in order to allow interpretive execution of zPCI instructions and adapter event notifications. However, there are some cases where a guest might disable interpretation -- provide a mechanism via which we can defer enabling the associated zPCI interpretation facilities until the guest indicates it wishes to use them.

---
  arch/s390/include/asm/kvm_host.h |  4 ++++
  arch/s390/kvm/kvm-s390.c         | 38 ++++++++++++++++++++++++++++++++
  arch/s390/kvm/kvm-s390.h         | 10 +++++++++
  3 files changed, 52 insertions(+)
arch/s390/include/asm/kvm_host.h +4 -0
index c1518a505060..8e381603b6a7 100644
@@ -254,7 +254,10 @@ struct kvm_s390_sie_block {
  #define ECB2_IEP	0x20
  #define ECB2_PFMFI	0x08
  #define ECB2_ESCA	0x04
+#define ECB2_ZPCI_LSI	0x02
  	__u8    ecb2;                   /* 0x0062 */
+#define ECB3_AISI	0x20
+#define ECB3_AISII	0x10
  #define ECB3_DEA 0x08
  #define ECB3_AES 0x04
  #define ECB3_RI  0x01
@@ -940,6 +943,7 @@ struct kvm_arch{
  	int use_cmma;
  	int use_pfmfi;
  	int use_skf;
+	int use_zpci_interp;
  	int user_cpu_state_ctrl;
  	int user_sigp;
  	int user_stsi;
arch/s390/kvm/kvm-s390.c +11 -0
index 5a0fbfd19c4a..0797661732cc 100644
@@ -1031,6 +1031,42 @@ static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
  	return 0;
  }
 
+static void kvm_s390_vcpu_pci_setup(struct kvm_vcpu *vcpu)
+{
+	/* Only set the ECB bits after guest requests zPCI interpretation */
+	if (!vcpu->kvm->arch.use_zpci_interp)
+		return;
+
+	vcpu->arch.sie_block->ecb2 |= ECB2_ZPCI_LSI;
+	vcpu->arch.sie_block->ecb3 |= ECB3_AISII + ECB3_AISI;
+}
+
+void kvm_s390_vcpu_pci_enable_interp(struct kvm *kvm)

arch/s390/kvm/kvm-s390.c static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr) · line 1054

Quoted 10 lines
arch/s390/kvm/kvm-s390.c +10 -0
+{
+	struct kvm_vcpu *vcpu;
+	unsigned long i;
+
+	lockdep_assert_held(&kvm->lock);
+
+	/*
+	 * If host is configured for PCI and the necessary facilities are
+	 * available, turn on interpretation for the life of this guest
+	 */

I'd suggest to move the comment after the if-statement - it seems to fit better there.

Quoted 4 lines
arch/s390/kvm/kvm-s390.c +4 -0
+	if (!kvm_s390_pci_interp_allowed())
+		return;
+
+	kvm->arch.use_zpci_interp = 1;

arch/s390/kvm/kvm-s390.c static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr) · line 1068

Quoted 10 lines
arch/s390/kvm/kvm-s390.c +10 -0
+
+	kvm_s390_vcpu_block_all(kvm);
+
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		kvm_s390_vcpu_pci_setup(vcpu);
+		kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
+	}
+
+	kvm_s390_vcpu_unblock_all(kvm);
+}

Thomas

author maintainer
#18 lore raw

PATCH v7 15/22 KVM: s390: pci: provide routines for enabling/disabling interrupt forwarding

In reply to Matthew Rosato

These routines will be wired into a kvm ioctl in order to respond to requests to enable / disable a device for Adapter Event Notifications / Adapter Interuption Forwarding.

---
 arch/s390/kvm/pci.c        | 247 +++++++++++++++++++++++++++++++++++++
 arch/s390/kvm/pci.h        |   1 +
 arch/s390/pci/pci_insn.c   |   1 +
 include/linux/sched/user.h |   3 +-
 4 files changed, 251 insertions(+), 1 deletion(-)
arch/s390/kvm/pci.c +247 -0
index 268cbe99fbac..5a9bc954e677 100644
@@ -11,6 +11,7 @@
 #include <linux/pci.h>
 #include <asm/pci.h>
 #include <asm/pci_insn.h>
+#include <asm/pci_io.h>
 #include "pci.h"
 
 struct zpci_aift *aift;
@@ -152,6 +153,252 @@ int kvm_s390_pci_aen_init(u8 nisc)
 	return rc;
 }
 
+/* Modify PCI: Register floating adapter interruption forwarding */
+static int kvm_zpci_set_airq(struct zpci_dev *zdev)
+{
+	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT);
+	struct zpci_fib fib = {};
+	u8 status;
+
+	fib.fmt0.isc = zdev->kzdev->fib.fmt0.isc;
+	fib.fmt0.sum = 1;       /* enable summary notifications */
+	fib.fmt0.noi = airq_iv_end(zdev->aibv);
+	fib.fmt0.aibv = virt_to_phys(zdev->aibv->vector);
+	fib.fmt0.aibvo = 0;
+	fib.fmt0.aisb = virt_to_phys(aift->sbv->vector + (zdev->aisb / 64) * 8);
+	fib.fmt0.aisbo = zdev->aisb & 63;
+	fib.gd = zdev->gisa;
+
+	return zpci_mod_fc(req, &fib, &status) ? -EIO : 0;
+}
+
+/* Modify PCI: Unregister floating adapter interruption forwarding */
+static int kvm_zpci_clear_airq(struct zpci_dev *zdev)
+{
+	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_DEREG_INT);
+	struct zpci_fib fib = {};
+	u8 cc, status;
+
+	fib.gd = zdev->gisa;
+
+	cc = zpci_mod_fc(req, &fib, &status);
+	if (cc == 3 || (cc == 1 && status == 24))
+		/* Function already gone or IRQs already deregistered. */
+		cc = 0;
+
+	return cc ? -EIO : 0;
+}
+
+static inline void unaccount_mem(unsigned long nr_pages)
+{
+	struct user_struct *user = get_uid(current_user());
+
+	if (user)
+		atomic_long_sub(nr_pages, &user->locked_vm);
+	if (current->mm)
+		atomic64_sub(nr_pages, &current->mm->pinned_vm);
+}
+
+static inline int account_mem(unsigned long nr_pages)
+{
+	struct user_struct *user = get_uid(current_user());
+	unsigned long page_limit, cur_pages, new_pages;
+
+	page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
+
+	do {
+		cur_pages = atomic_long_read(&user->locked_vm);
+		new_pages = cur_pages + nr_pages;
+		if (new_pages > page_limit)
+			return -ENOMEM;
+	} while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
+					new_pages) != cur_pages);
+
+	atomic64_add(nr_pages, &current->mm->pinned_vm);
+
+	return 0;
+}
+
+static int kvm_s390_pci_aif_enable(struct zpci_dev *zdev, struct zpci_fib *fib,
+				   bool assist)
+{
+	struct page *pages[1], *aibv_page, *aisb_page = NULL;
+	unsigned int msi_vecs, idx;
+	struct zpci_gaite *gaite;
+	unsigned long hva, bit;
+	struct kvm *kvm;
+	phys_addr_t gaddr;
+	int rc = 0, gisc, npages, pcount = 0;
+
+	/*
+	 * Interrupt forwarding is only applicable if the device is already
+	 * enabled for interpretation
+	 */
+	if (zdev->gisa == 0)
+		return -EINVAL;
+
+	kvm = zdev->kzdev->kvm;
+	msi_vecs = min_t(unsigned int, fib->fmt0.noi, zdev->max_msi);
+
+	/* Get the associated forwarding ISC - if invalid, return the error */
+	gisc = kvm_s390_gisc_register(kvm, fib->fmt0.isc);
+	if (gisc < 0)
+		return gisc;
+
+	/* Replace AIBV address */
+	idx = srcu_read_lock(&kvm->srcu);
+	hva = gfn_to_hva(kvm, gpa_to_gfn((gpa_t)fib->fmt0.aibv));
+	npages = pin_user_pages_fast(hva, 1, FOLL_WRITE | FOLL_LONGTERM, pages);
+	srcu_read_unlock(&kvm->srcu, idx);
+	if (npages < 1) {
+		rc = -EIO;
+		goto out;
+	}
+	aibv_page = pages[0];
+	pcount++;
+	gaddr = page_to_phys(aibv_page) + (fib->fmt0.aibv & ~PAGE_MASK);
+	fib->fmt0.aibv = gaddr;
+
+	/* Pin the guest AISB if one was specified */
+	if (fib->fmt0.sum == 1) {
+		idx = srcu_read_lock(&kvm->srcu);
+		hva = gfn_to_hva(kvm, gpa_to_gfn((gpa_t)fib->fmt0.aisb));
+		npages = pin_user_pages_fast(hva, 1, FOLL_WRITE | FOLL_LONGTERM,
+					     pages);
+		srcu_read_unlock(&kvm->srcu, idx);
+		if (npages < 1) {
+			rc = -EIO;
+			goto unpin1;
+		}
+		aisb_page = pages[0];
+		pcount++;
+	}
+
+	/* Account for pinned pages, roll back on failure */
+	if (account_mem(pcount))
+		goto unpin2;
+
+	/* AISB must be allocated before we can fill in GAITE */
+	mutex_lock(&aift->aift_lock);
+	bit = airq_iv_alloc_bit(aift->sbv);
+	if (bit == -1UL)
+		goto unlock;
+	zdev->aisb = bit; /* store the summary bit number */
+	zdev->aibv = airq_iv_create(msi_vecs, AIRQ_IV_DATA |
+				    AIRQ_IV_BITLOCK |
+				    AIRQ_IV_GUESTVEC,
+				    phys_to_virt(fib->fmt0.aibv));
+
+	spin_lock_irq(&aift->gait_lock);
+	gaite = (struct zpci_gaite *)aift->gait + (zdev->aisb *
+						   sizeof(struct zpci_gaite));
+
+	/* If assist not requested, host will get all alerts */
+	if (assist)
+		gaite->gisa = (u32)virt_to_phys(&kvm->arch.sie_page2->gisa);
+	else
+		gaite->gisa = 0;
+
+	gaite->gisc = fib->fmt0.isc;
+	gaite->count++;
+	gaite->aisbo = fib->fmt0.aisbo;
+	gaite->aisb = virt_to_phys(page_address(aisb_page) + (fib->fmt0.aisb &
+							      ~PAGE_MASK));
+	aift->kzdev[zdev->aisb] = zdev->kzdev;
+	spin_unlock_irq(&aift->gait_lock);
+
+	/* Update guest FIB for re-issue */
+	fib->fmt0.aisbo = zdev->aisb & 63;
+	fib->fmt0.aisb = virt_to_phys(aift->sbv->vector + (zdev->aisb / 64) * 8);
+	fib->fmt0.isc = gisc;
+
+	/* Save some guest fib values in the host for later use */
+	zdev->kzdev->fib.fmt0.isc = fib->fmt0.isc;
+	zdev->kzdev->fib.fmt0.aibv = fib->fmt0.aibv;
+	mutex_unlock(&aift->aift_lock);
+
+	/* Issue the clp to setup the irq now */
+	rc = kvm_zpci_set_airq(zdev);
+	return rc;
+
+unlock:
+	mutex_unlock(&aift->aift_lock);
+unpin2:
+	if (fib->fmt0.sum == 1)
+		unpin_user_page(aisb_page);
+unpin1:
+	unpin_user_page(aibv_page);
+out:
+	return rc;
+}
+
+static int kvm_s390_pci_aif_disable(struct zpci_dev *zdev, bool force)
+{
+	struct kvm_zdev *kzdev = zdev->kzdev;
+	struct zpci_gaite *gaite;
+	struct page *vpage = NULL, *spage = NULL;
+	int rc, pcount = 0;
+	u8 isc;
+
+	if (zdev->gisa == 0)
+		return -EINVAL;
+
+	mutex_lock(&aift->aift_lock);
+
+	/*
+	 * If the clear fails due to an error, leave now unless we know this
+	 * device is about to go away (force) -- In that case clear the GAITE
+	 * regardless.
+	 */
+	rc = kvm_zpci_clear_airq(zdev);
+	if (rc && !force)
+		goto out;
+
+	if (zdev->kzdev->fib.fmt0.aibv == 0)
+		goto out;
+	spin_lock_irq(&aift->gait_lock);
+	gaite = (struct zpci_gaite *)aift->gait + (zdev->aisb *
+						   sizeof(struct zpci_gaite));
+	isc = gaite->gisc;
+	gaite->count--;
+	if (gaite->count == 0) {
+		/* Release guest AIBV and AISB */
+		vpage = phys_to_page(kzdev->fib.fmt0.aibv);
+		if (gaite->aisb != 0)
+			spage = phys_to_page(gaite->aisb);
+		/* Clear the GAIT entry */
+		gaite->aisb = 0;
+		gaite->gisc = 0;
+		gaite->aisbo = 0;
+		gaite->gisa = 0;
+		aift->kzdev[zdev->aisb] = 0;
+		/* Clear zdev info */
+		airq_iv_free_bit(aift->sbv, zdev->aisb);
+		airq_iv_release(zdev->aibv);
+		zdev->aisb = 0;
+		zdev->aibv = NULL;
+	}
+	spin_unlock_irq(&aift->gait_lock);
+	kvm_s390_gisc_unregister(kzdev->kvm, isc);
+	kzdev->fib.fmt0.isc = 0;
+	kzdev->fib.fmt0.aibv = 0;
+
+	if (vpage) {
+		unpin_user_page(vpage);
+		pcount++;
+	}
+	if (spage) {
+		unpin_user_page(spage);
+		pcount++;
+	}
+	if (pcount > 0)
+		unaccount_mem(pcount);
+out:
+	mutex_unlock(&aift->aift_lock);
+
+	return rc;
+}
+
 static int kvm_s390_pci_dev_open(struct zpci_dev *zdev)
 {
 	struct kvm_zdev *kzdev;
arch/s390/kvm/pci.h +1 -0
index 9f7828d97605..9d091033fc02 100644
@@ -20,6 +20,7 @@
 struct kvm_zdev {
 	struct zpci_dev *zdev;
 	struct kvm *kvm;
+	struct zpci_fib fib;
 };
 
 struct zpci_gaite {
arch/s390/pci/pci_insn.c +1 -0
index 4c6967b73932..cd9fb186a6be 100644
@@ -60,6 +60,7 @@ u8 zpci_mod_fc(u64 req, struct zpci_fib *fib, u8 *status)
 
 	return cc;
 }
+EXPORT_SYMBOL_GPL(zpci_mod_fc);
 
 /* Refresh PCI Translations */
 static inline u8 __rpcit(u64 fn, u64 addr, u64 range, u8 *status)
include/linux/sched/user.h +2 -1
index 00ed419dd464..f054d0360a75 100644
@@ -24,7 +24,8 @@ struct user_struct {
 	kuid_t uid;
 
 #if defined(CONFIG_PERF_EVENTS) || defined(CONFIG_BPF_SYSCALL) || \
-    defined(CONFIG_NET) || defined(CONFIG_IO_URING)
+	defined(CONFIG_NET) || defined(CONFIG_IO_URING) || \
+	defined(CONFIG_VFIO_PCI_ZDEV_KVM)
 	atomic_long_t locked_vm;
 #endif
 #ifdef CONFIG_WATCH_QUEUE
Signature · 1 line
2.27.0
author maintainer
#19 lore raw

PATCH v7 16/22 KVM: s390: pci: add routines to start/stop interpretive execution

In reply to Matthew Rosato

These routines will be invoked at the time an s390x vfio-pci device is associated with a KVM (or when the association is removed), allowing the zPCI device to enable or disable load/store intepretation mode; this requires the host zPCI device to inform firmware of the unique token (GISA designation) that is associated with the owning KVM.

---
 arch/s390/include/asm/kvm_host.h |  21 ++++
 arch/s390/include/asm/pci.h      |   1 +
 arch/s390/kvm/kvm-s390.c         |  15 +++
 arch/s390/kvm/pci.c              | 162 +++++++++++++++++++++++++++++++
 arch/s390/kvm/pci.h              |   5 +
 arch/s390/pci/pci.c              |   4 +
 6 files changed, 208 insertions(+)
arch/s390/include/asm/kvm_host.h +21 -0
index 8e381603b6a7..2bf931d290b5 100644
@@ -19,6 +19,7 @@
 #include <linux/kvm.h>
 #include <linux/seqlock.h>
 #include <linux/module.h>
+#include <linux/pci.h>
 #include <asm/debug.h>
 #include <asm/cpu.h>
 #include <asm/fpu/api.h>
@@ -967,6 +968,8 @@ struct kvm_arch{
 	DECLARE_BITMAP(idle_mask, KVM_MAX_VCPUS);
 	struct kvm_s390_gisa_interrupt gisa_int;
 	struct kvm_s390_pv pv;
+	struct list_head kzdev_list;
+	spinlock_t kzdev_list_lock;
 };
 
 #define KVM_HVA_ERR_BAD		(-1UL)
@@ -1017,4 +1020,22 @@ static inline void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
 static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) {}
 static inline void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) {}
 
+#define __KVM_HAVE_ARCH_VM_FREE
+void kvm_arch_free_vm(struct kvm *kvm);
+
+#ifdef CONFIG_VFIO_PCI_ZDEV_KVM
+int kvm_s390_pci_register_kvm(struct zpci_dev *zdev, struct kvm *kvm);
+int kvm_s390_pci_unregister_kvm(struct zpci_dev *zdev);
+#else
+static inline int kvm_s390_pci_register_kvm(struct zpci_dev *dev,
+					    struct kvm *kvm)
+{
+	return -EPERM;
+}
+static inline int kvm_s390_pci_unregister_kvm(struct zpci_dev *dev)
+{
+	return -EPERM;
+}
+#endif
+
 #endif
arch/s390/include/asm/pci.h +1 -0
index 322060a75d9f..85eb0ef9d4c3 100644
@@ -194,6 +194,7 @@ struct zpci_dev {
 	/* IOMMU and passthrough */
 	struct s390_domain *s390_domain; /* s390 IOMMU domain data */
 	struct kvm_zdev *kzdev;
+	struct mutex kzdev_lock;
 };
 
 static inline bool zdev_enabled(struct zpci_dev *zdev)
arch/s390/kvm/kvm-s390.c +15 -0
index 0797661732cc..b95b25490018 100644
@@ -2794,6 +2794,14 @@ static void sca_dispose(struct kvm *kvm)
 	kvm->arch.sca = NULL;
 }
 
+void kvm_arch_free_vm(struct kvm *kvm)
+{
+	if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
+		kvm_s390_pci_clear_list(kvm);
+
+	__kvm_arch_free_vm(kvm);
+}
+
 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 {
 	gfp_t alloc_flags = GFP_KERNEL_ACCOUNT;
@@ -2876,6 +2884,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 	kvm_s390_crypto_init(kvm);
 
+	if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) {
+		mutex_lock(&kvm->lock);
+		kvm_s390_pci_init_list(kvm);
+		kvm_s390_vcpu_pci_enable_interp(kvm);
+		mutex_unlock(&kvm->lock);
+	}
+
 	mutex_init(&kvm->arch.float_int.ais_lock);
 	spin_lock_init(&kvm->arch.float_int.lock);
 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
arch/s390/kvm/pci.c +162 -0

175 lines not on this page · raw message

arch/s390/kvm/pci.h +5 -0
index 9d091033fc02..fb2b91b76e0c 100644
@@ -13,6 +13,7 @@
 #include <linux/kvm_host.h>
 #include <linux/pci.h>
 #include <linux/mutex.h>
+#include <linux/kvm.h>
 #include <linux/kvm_host.h>
 #include <asm/airq.h>
 #include <asm/cpu.h>
@@ -21,6 +22,7 @@ struct kvm_zdev {
 	struct zpci_dev *zdev;
 	struct kvm *kvm;
 	struct zpci_fib fib;
+	struct list_head entry;
 };
 
 struct zpci_gaite {
@@ -54,6 +56,9 @@ static inline struct kvm *kvm_s390_pci_si_to_kvm(struct zpci_aift *aift,
 int kvm_s390_pci_aen_init(u8 nisc);
 void kvm_s390_pci_aen_exit(void);
 
+void kvm_s390_pci_init_list(struct kvm *kvm);
+void kvm_s390_pci_clear_list(struct kvm *kvm);
+
 int kvm_s390_pci_init(void);
 void kvm_s390_pci_exit(void);
 
arch/s390/pci/pci.c +4 -0
index f0a439c43395..c0a20c418513 100644
@@ -132,6 +132,7 @@ int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
 		zpci_dbg(3, "reg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, status);
 	return cc;
 }
+EXPORT_SYMBOL_GPL(zpci_register_ioat);
 
 /* Modify PCI: Unregister I/O address translation parameters */
 int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
@@ -712,6 +713,7 @@ int zpci_enable_device(struct zpci_dev *zdev)
 		zpci_update_fh(zdev, fh);
 	return rc;
 }
+EXPORT_SYMBOL_GPL(zpci_enable_device);
 
 int zpci_disable_device(struct zpci_dev *zdev)
 {
@@ -735,6 +737,7 @@ int zpci_disable_device(struct zpci_dev *zdev)
 	}
 	return rc;
 }
+EXPORT_SYMBOL_GPL(zpci_disable_device);
 
 /**
  * zpci_hot_reset_device - perform a reset of the given zPCI function
@@ -828,6 +831,7 @@ struct zpci_dev *zpci_create_device(u32 fid, u32 fh, enum zpci_state state)
 
 	kref_init(&zdev->kref);
 	mutex_init(&zdev->lock);
+	mutex_init(&zdev->kzdev_lock);
 
 	rc = zpci_init_iommu(zdev);
 	if (rc)
Signature · 1 line
2.27.0
author maintainer
#20 lore raw

PATCH v7 17/22 vfio-pci/zdev: add open/close device hooks

In reply to Matthew Rosato

During vfio-pci open_device, register a notifier for the zPCI device to catch KVM registration. This is needed in order to pass a special indicator (GISA) to firmware to allow zPCI interpretation facilities to be used for only the specific KVM associated with the vfio-pci device. During vfio-pci close_device, unregister the notifier.

---
 arch/s390/include/asm/pci.h      |  2 ++
 drivers/vfio/pci/vfio_pci_core.c |  2 ++
 drivers/vfio/pci/vfio_pci_zdev.c | 54 ++++++++++++++++++++++++++++++++
 include/linux/vfio_pci_core.h    | 10 ++++++
 4 files changed, 68 insertions(+)
arch/s390/include/asm/pci.h +2 -0
index 85eb0ef9d4c3..67fbce1ea0c9 100644
@@ -5,6 +5,7 @@
 #include <linux/pci.h>
 #include <linux/mutex.h>
 #include <linux/iommu.h>
+#include <linux/notifier.h>
 #include <linux/pci_hotplug.h>
 #include <asm-generic/pci.h>
 #include <asm/pci_clp.h>
@@ -195,6 +196,7 @@ struct zpci_dev {
 	struct s390_domain *s390_domain; /* s390 IOMMU domain data */
 	struct kvm_zdev *kzdev;
 	struct mutex kzdev_lock;
+	struct notifier_block nb; /* vfio notifications */
 };
 
 static inline bool zdev_enabled(struct zpci_dev *zdev)
drivers/vfio/pci/vfio_pci_core.c +2 -0
index 06b6f3594a13..d53125b308f0 100644
@@ -449,6 +449,7 @@ void vfio_pci_core_close_device(struct vfio_device *core_vdev)
 		vdev->sriov_pf_core_dev->vf_token->users--;
 		mutex_unlock(&vdev->sriov_pf_core_dev->vf_token->lock);
 	}
+	vfio_pci_zdev_release(vdev);
 	vfio_spapr_pci_eeh_release(vdev->pdev);
 	vfio_pci_core_disable(vdev);
 
@@ -469,6 +470,7 @@ void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev)
 {
 	vfio_pci_probe_mmaps(vdev);
 	vfio_spapr_pci_eeh_open(vdev->pdev);
+	vfio_pci_zdev_open(vdev);
 
 	if (vdev->sriov_pf_core_dev) {
 		mutex_lock(&vdev->sriov_pf_core_dev->vf_token->lock);
drivers/vfio/pci/vfio_pci_zdev.c +54 -0

63 lines not on this page · raw message

include/linux/vfio_pci_core.h +10 -0
index cfc9eb1f0b27..68ab01b592d3 100644
@@ -209,12 +209,22 @@ static inline int vfio_pci_igd_init(struct vfio_pci_core_device *vdev)
 #ifdef CONFIG_VFIO_PCI_ZDEV_KVM
 extern int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
 				       struct vfio_info_cap *caps);
+void vfio_pci_zdev_open(struct vfio_pci_core_device *vdev);
+void vfio_pci_zdev_release(struct vfio_pci_core_device *vdev);
 #else
 static inline int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
 					      struct vfio_info_cap *caps)
 {
 	return -ENODEV;
 }
+
+static inline void vfio_pci_zdev_open(struct vfio_pci_core_device *vdev)
+{
+}
+
+static inline void vfio_pci_zdev_release(struct vfio_pci_core_device *vdev)
+{
+}
 #endif
 
 /* Will be exported for vfio pci drivers usage */
Signature · 1 line
2.27.0
#21 lore raw

Re: [PATCH v7 17/22] vfio-pci/zdev: add open/close device hooks

Quoted 7 lines · Matthew Rosato
(quoted diff) +3 -0
@@ -136,3 +137,56 @@ int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
 
 	return ret;
 }
+
+static int vfio_pci_zdev_group_notifier(struct notifier_block *nb,
+					unsigned long action, void *data)

Review int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev, · line 152

Quoted 10 lines
(quoted diff) +10 -0
+{
+	struct zpci_dev *zdev = container_of(nb, struct zpci_dev, nb);
+
+	if (action == VFIO_GROUP_NOTIFY_SET_KVM) {
+		if (!zdev)
+			return NOTIFY_DONE;
+
+		if (data) {
+			if (kvm_s390_pci_register_kvm(zdev, (struct kvm *)data))
+				return NOTIFY_BAD;

The error codes are all ignored for this notifier chains, so this seems like a problem.

Review int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev, · line 155

Quoted 3 lines
(quoted diff) +3 -0
+		} else {
+			if (kvm_s390_pci_unregister_kvm(zdev))
+				return NOTIFY_BAD;

unregister really shouldn't fail.

Quoted 9 lines
(quoted diff) +9 -0
+		}
+
+	}
+
+	return NOTIFY_OK;
+}
+
+void vfio_pci_zdev_open(struct vfio_pci_core_device *vdev)
+{

Review int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev, · line 174

Quoted 10 lines
(quoted diff) +10 -0
+	unsigned long events = VFIO_GROUP_NOTIFY_SET_KVM;
+	struct zpci_dev *zdev = to_zpci(vdev->pdev);
+
+	if (!zdev)
+		return;
+
+	zdev->nb.notifier_call = vfio_pci_zdev_group_notifier;
+
+	vfio_register_notifier(vdev->vdev.dev, VFIO_GROUP_NOTIFY,
+			       &events, &zdev->nb);

Normally you'd want to do what is kvm_s390_pci_register_kvm() here, where a failure can be propogated but then you have a race condition with the kvm.

Blech, maybe it is time to just fix this race condition permanently, what do you think? (I didn't even compile it)

drivers/gpu/drm/i915/gvt/gvt.h +0 -2
index 2af4c83e733c6c..633acfcf76bf23 100644
@@ -227,8 +227,6 @@ struct intel_vgpu {
 	struct mutex cache_lock;
 
 	struct notifier_block iommu_notifier;
-	struct notifier_block group_notifier;
-	struct kvm *kvm;
 	struct work_struct release_work;
 	atomic_t released;
 
drivers/gpu/drm/i915/gvt/kvmgt.c +13 -48

139 lines not on this page · raw message

drivers/s390/crypto/vfio_ap_ops.c +5 -30
index e8914024f5b1af..f378f809d8a00d 100644
@@ -1284,25 +1284,6 @@ static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
 	}
 }
 
-static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
-				       unsigned long action, void *data)
-{
-	int notify_rc = NOTIFY_OK;
-	struct ap_matrix_mdev *matrix_mdev;
-
-	if (action != VFIO_GROUP_NOTIFY_SET_KVM)
-		return NOTIFY_OK;
-
-	matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);
-
-	if (!data)
-		vfio_ap_mdev_unset_kvm(matrix_mdev);
-	else if (vfio_ap_mdev_set_kvm(matrix_mdev, data))
-		notify_rc = NOTIFY_DONE;
-
-	return notify_rc;
-}
-
 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
 {
 	struct device *dev;
@@ -1402,11 +1383,7 @@ static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
 	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(vdev, VFIO_GROUP_NOTIFY, &events,
-				     &matrix_mdev->group_notifier);
+	ret = vfio_ap_mdev_set_kvm(matrix_mdev, vdev->vdev.kvm);
 	if (ret)
 		return ret;
 
@@ -1415,12 +1392,11 @@ static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
 	ret = vfio_register_notifier(vdev, VFIO_IOMMU_NOTIFY, &events,
 				     &matrix_mdev->iommu_notifier);
 	if (ret)
-		goto out_unregister_group;
+		goto err_kvm;
 	return 0;
 
-out_unregister_group:
-	vfio_unregister_notifier(vdev, VFIO_GROUP_NOTIFY,
-				 &matrix_mdev->group_notifier);
+err_kvm:
+	vfio_ap_mdev_unset_kvm(matrix_mdev);
 	return ret;
 }
 
@@ -1431,8 +1407,6 @@ static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
 
 	vfio_unregister_notifier(vdev, VFIO_IOMMU_NOTIFY,
 				 &matrix_mdev->iommu_notifier);
-	vfio_unregister_notifier(vdev, VFIO_GROUP_NOTIFY,
-				 &matrix_mdev->group_notifier);
 	vfio_ap_mdev_unset_kvm(matrix_mdev);
 }
 
@@ -1481,6 +1455,7 @@ static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
 }
 
 static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
+	.flags = VFIO_DEVICE_NEEDS_KVM,
 	.open_device = vfio_ap_mdev_open_device,
 	.close_device = vfio_ap_mdev_close_device,
 	.ioctl = vfio_ap_mdev_ioctl,
drivers/s390/crypto/vfio_ap_private.h +0 -3
index 648fcaf8104abb..a26efd804d0df3 100644
@@ -81,8 +81,6 @@ struct ap_matrix {
  * @node:	allows the ap_matrix_mdev struct to be added to a list
  * @matrix:	the adapters, usage domains and control domains assigned to the
  *		mediated matrix device.
- * @group_notifier: notifier block used for specifying callback function for
- *		    handling the VFIO_GROUP_NOTIFY_SET_KVM event
  * @iommu_notifier: notifier block used for specifying callback function for
  *		    handling the VFIO_IOMMU_NOTIFY_DMA_UNMAP even
  * @kvm:	the struct holding guest's state
@@ -94,7 +92,6 @@ struct ap_matrix_mdev {
 	struct vfio_device vdev;
 	struct list_head node;
 	struct ap_matrix matrix;
-	struct notifier_block group_notifier;
 	struct notifier_block iommu_notifier;
 	struct kvm *kvm;
 	crypto_hook pqap_hook;
drivers/vfio/vfio.c +27 -46

125 lines not on this page · raw message

include/linux/vfio.h +6 -4
index 45b287826ce686..aaf120b9c080b7 100644
@@ -36,6 +36,7 @@ struct vfio_device {
 	struct vfio_device_set *dev_set;
 	struct list_head dev_set_list;
 	unsigned int migration_flags;
+	struct kvm *kvm;
 
 	/* Members below here are private, not for driver use */
 	refcount_t refcount;
@@ -44,6 +45,10 @@ struct vfio_device {
 	struct list_head group_next;
 };
 
+enum {
+	VFIO_DEVICE_NEEDS_KVM = 1,
+};
+
 /**
  * struct vfio_device_ops - VFIO bus driver device callbacks
  *
@@ -72,6 +77,7 @@ struct vfio_device {
  */
 struct vfio_device_ops {
 	char	*name;
+	unsigned int flags;
 	int	(*open_device)(struct vfio_device *vdev);
 	void	(*close_device)(struct vfio_device *vdev);
 	ssize_t	(*read)(struct vfio_device *vdev, char __user *buf,
@@ -155,15 +161,11 @@ extern int vfio_dma_rw(struct vfio_device *device, dma_addr_t user_iova,
 /* each type has independent events */
 enum vfio_notify_type {
 	VFIO_IOMMU_NOTIFY = 0,
-	VFIO_GROUP_NOTIFY = 1,
 };
 
 /* events for VFIO_IOMMU_NOTIFY */
 #define VFIO_IOMMU_NOTIFY_DMA_UNMAP	BIT(0)
 
-/* events for VFIO_GROUP_NOTIFY */
-#define VFIO_GROUP_NOTIFY_SET_KVM	BIT(0)
-
 extern int vfio_register_notifier(struct vfio_device *device,
 				  enum vfio_notify_type type,
 				  unsigned long *required_events,
author maintainer
#22 lore raw

Re: [PATCH v7 17/22] vfio-pci/zdev: add open/close device hooks

Quoted 53 lines · Jason Gunthorpe
Quoted 7 lines · Matthew Rosato
(quoted diff) +3 -0
@@ -136,3 +137,56 @@ int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
 
  	return ret;
  }
+
+static int vfio_pci_zdev_group_notifier(struct notifier_block *nb,
+					unsigned long action, void *data)

Review int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev, · line 152

Quoted 10 lines
(quoted diff) +10 -0
+{
+	struct zpci_dev *zdev = container_of(nb, struct zpci_dev, nb);
+
+	if (action == VFIO_GROUP_NOTIFY_SET_KVM) {
+		if (!zdev)
+			return NOTIFY_DONE;
+
+		if (data) {
+			if (kvm_s390_pci_register_kvm(zdev, (struct kvm *)data))
+				return NOTIFY_BAD;

The error codes are all ignored for this notifier chains, so this seems like a problem.

Review int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev, · line 155

Quoted 3 lines
(quoted diff) +3 -0
+		} else {
+			if (kvm_s390_pci_unregister_kvm(zdev))
+				return NOTIFY_BAD;

unregister really shouldn't fail.

Quoted 9 lines
(quoted diff) +9 -0
+		}
+
+	}
+
+	return NOTIFY_OK;
+}
+
+void vfio_pci_zdev_open(struct vfio_pci_core_device *vdev)
+{

Review int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev, · line 174

Quoted 10 lines
(quoted diff) +10 -0
+	unsigned long events = VFIO_GROUP_NOTIFY_SET_KVM;
+	struct zpci_dev *zdev = to_zpci(vdev->pdev);
+
+	if (!zdev)
+		return;
+
+	zdev->nb.notifier_call = vfio_pci_zdev_group_notifier;
+
+	vfio_register_notifier(vdev->vdev.dev, VFIO_GROUP_NOTIFY,
+			       &events, &zdev->nb);

Normally you'd want to do what is kvm_s390_pci_register_kvm() here, where a failure can be propogated but then you have a race condition with the kvm.

Quoted 2 lines

Blech, maybe it is time to just fix this race condition permanently, what do you think? (I didn't even compile it)

Conceptually I think this would work for QEMU anyway (it always sets the kvm before we open the device). I tried to test the idea quickly but couldn't get the following to apply on vfio-next or your vfio_group_locking -- but I understand what you're trying to do so I'll re-work and try it out.

@Alex can you think of any usecase/reason why we would need to be able to set the KVM sometime after the device was opened? Doing something like below would break that, as this introduces the assumption that the group is associated with the KVM before the device is opened (and if it's not, the device open fails).

Quoted 447 lines
drivers/gpu/drm/i915/gvt/gvt.h +0 -2
index 2af4c83e733c6c..633acfcf76bf23 100644
@@ -227,8 +227,6 @@ struct intel_vgpu {
  	struct mutex cache_lock;
 
  	struct notifier_block iommu_notifier;
-	struct notifier_block group_notifier;
-	struct kvm *kvm;
  	struct work_struct release_work;
  	atomic_t released;
 
drivers/gpu/drm/i915/gvt/kvmgt.c +13 -48
index 7655ffa97d5116..655d47c65470d5 100644
@@ -761,23 +761,6 @@ static int intel_vgpu_iommu_notifier(struct notifier_block *nb,
  	return NOTIFY_OK;
  }
 
-static int intel_vgpu_group_notifier(struct notifier_block *nb,
-				     unsigned long action, void *data)
-{
-	struct intel_vgpu *vgpu =
-		container_of(nb, struct intel_vgpu, group_notifier);
-
-	/* the only action we care about */
-	if (action == VFIO_GROUP_NOTIFY_SET_KVM) {
-		vgpu->kvm = data;
-
-		if (!data)
-			schedule_work(&vgpu->release_work);
-	}
-
-	return NOTIFY_OK;
-}
-
  static bool __kvmgt_vgpu_exist(struct intel_vgpu *vgpu)
  {
  	struct intel_vgpu *itr;
@@ -789,7 +772,7 @@ static bool __kvmgt_vgpu_exist(struct intel_vgpu *vgpu)
  		if (!itr->attached)
  			continue;
 
-		if (vgpu->kvm == itr->kvm) {
+		if (vgpu->vfio_device.kvm == itr->vfio_device.kvm) {
  			ret = true;
  			goto out;
  		}
@@ -806,7 +789,6 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev)
  	int ret;
 
  	vgpu->iommu_notifier.notifier_call = intel_vgpu_iommu_notifier;
-	vgpu->group_notifier.notifier_call = intel_vgpu_group_notifier;
 
  	events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
  	ret = vfio_register_notifier(vfio_dev, VFIO_IOMMU_NOTIFY, &events,
@@ -817,38 +799,30 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev)
  		goto out;
  	}
 
-	events = VFIO_GROUP_NOTIFY_SET_KVM;
-	ret = vfio_register_notifier(vfio_dev, VFIO_GROUP_NOTIFY, &events,
-				     &vgpu->group_notifier);
-	if (ret != 0) {
-		gvt_vgpu_err("vfio_register_notifier for group failed: %d\n",
-			ret);
-		goto undo_iommu;
-	}
-
  	ret = -EEXIST;
  	if (vgpu->attached)
-		goto undo_register;
+		goto undo_iommu;
 
  	ret = -ESRCH;
-	if (!vgpu->kvm || vgpu->kvm->mm != current->mm) {
+	if (!vgpu->vfio_device.kvm ||
+	    vgpu->vfio_device.kvm->mm != current->mm) {
  		gvt_vgpu_err("KVM is required to use Intel vGPU\n");
-		goto undo_register;
+		goto undo_iommu;
  	}
 
  	ret = -EEXIST;
  	if (__kvmgt_vgpu_exist(vgpu))
-		goto undo_register;
+		goto undo_iommu;
 
  	vgpu->attached = true;
-	kvm_get_kvm(vgpu->kvm);
 
  	kvmgt_protect_table_init(vgpu);
  	gvt_cache_init(vgpu);
 
  	vgpu->track_node.track_write = kvmgt_page_track_write;
  	vgpu->track_node.track_flush_slot = kvmgt_page_track_flush_slot;
-	kvm_page_track_register_notifier(vgpu->kvm, &vgpu->track_node);
+	kvm_page_track_register_notifier(vgpu->vfio_device.kvm,
+					 &vgpu->track_node);
 
  	debugfs_create_ulong(KVMGT_DEBUGFS_FILENAME, 0444, vgpu->debugfs,
  			     &vgpu->nr_cache_entries);
@@ -858,10 +832,6 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev)
  	atomic_set(&vgpu->released, 0);
  	return 0;
 
-undo_register:
-	vfio_unregister_notifier(vfio_dev, VFIO_GROUP_NOTIFY,
-				 &vgpu->group_notifier);
-
  undo_iommu:
  	vfio_unregister_notifier(vfio_dev, VFIO_IOMMU_NOTIFY,
  				 &vgpu->iommu_notifier);
@@ -898,21 +868,15 @@ static void __intel_vgpu_release(struct intel_vgpu *vgpu)
  	drm_WARN(&i915->drm, ret,
  		 "vfio_unregister_notifier for iommu failed: %d\n", ret);
 
-	ret = vfio_unregister_notifier(&vgpu->vfio_device, VFIO_GROUP_NOTIFY,
-				       &vgpu->group_notifier);
-	drm_WARN(&i915->drm, ret,
-		 "vfio_unregister_notifier for group failed: %d\n", ret);
-
  	debugfs_remove(debugfs_lookup(KVMGT_DEBUGFS_FILENAME, vgpu->debugfs));
 
-	kvm_page_track_unregister_notifier(vgpu->kvm, &vgpu->track_node);
-	kvm_put_kvm(vgpu->kvm);
+	kvm_page_track_unregister_notifier(vgpu->vfio_device.kvm,
+					   &vgpu->track_node);
  	kvmgt_protect_table_destroy(vgpu);
  	gvt_cache_destroy(vgpu);
 
  	intel_vgpu_release_msi_eventfd_ctx(vgpu);
 
-	vgpu->kvm = NULL;
  	vgpu->attached = false;
  }
 
@@ -1649,6 +1613,7 @@ static const struct attribute_group *intel_vgpu_groups[] = {
  };
 
  static const struct vfio_device_ops intel_vgpu_dev_ops = {
+	.flags		= VFIO_DEVICE_NEEDS_KVM,
  	.open_device	= intel_vgpu_open_device,
  	.close_device	= intel_vgpu_close_device,
  	.read		= intel_vgpu_read,
@@ -1713,7 +1678,7 @@ static struct mdev_driver intel_vgpu_mdev_driver = {
 
  int intel_gvt_page_track_add(struct intel_vgpu *info, u64 gfn)
  {
-	struct kvm *kvm = info->kvm;
+	struct kvm *kvm = info->vfio_device.kvm;
  	struct kvm_memory_slot *slot;
  	int idx;
 
@@ -1743,7 +1708,7 @@ int intel_gvt_page_track_add(struct intel_vgpu *info, u64 gfn)
 
  int intel_gvt_page_track_remove(struct intel_vgpu *info, u64 gfn)
  {
-	struct kvm *kvm = info->kvm;
+	struct kvm *kvm = info->vfio_device.kvm;
  	struct kvm_memory_slot *slot;
  	int idx;
 
drivers/s390/crypto/vfio_ap_ops.c +5 -30
index e8914024f5b1af..f378f809d8a00d 100644
@@ -1284,25 +1284,6 @@ static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
  	}
  }
 
-static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
-				       unsigned long action, void *data)
-{
-	int notify_rc = NOTIFY_OK;
-	struct ap_matrix_mdev *matrix_mdev;
-
-	if (action != VFIO_GROUP_NOTIFY_SET_KVM)
-		return NOTIFY_OK;
-
-	matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);
-
-	if (!data)
-		vfio_ap_mdev_unset_kvm(matrix_mdev);
-	else if (vfio_ap_mdev_set_kvm(matrix_mdev, data))
-		notify_rc = NOTIFY_DONE;
-
-	return notify_rc;
-}
-
  static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
  {
  	struct device *dev;
@@ -1402,11 +1383,7 @@ static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
  	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(vdev, VFIO_GROUP_NOTIFY, &events,
-				     &matrix_mdev->group_notifier);
+	ret = vfio_ap_mdev_set_kvm(matrix_mdev, vdev->vdev.kvm);
  	if (ret)
  		return ret;
 
@@ -1415,12 +1392,11 @@ static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
  	ret = vfio_register_notifier(vdev, VFIO_IOMMU_NOTIFY, &events,
  				     &matrix_mdev->iommu_notifier);
  	if (ret)
-		goto out_unregister_group;
+		goto err_kvm;
  	return 0;
 
-out_unregister_group:
-	vfio_unregister_notifier(vdev, VFIO_GROUP_NOTIFY,
-				 &matrix_mdev->group_notifier);
+err_kvm:
+	vfio_ap_mdev_unset_kvm(matrix_mdev);
  	return ret;
  }
 
@@ -1431,8 +1407,6 @@ static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
 
  	vfio_unregister_notifier(vdev, VFIO_IOMMU_NOTIFY,
  				 &matrix_mdev->iommu_notifier);
-	vfio_unregister_notifier(vdev, VFIO_GROUP_NOTIFY,
-				 &matrix_mdev->group_notifier);
  	vfio_ap_mdev_unset_kvm(matrix_mdev);
  }
 
@@ -1481,6 +1455,7 @@ static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
  }
 
  static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
+	.flags = VFIO_DEVICE_NEEDS_KVM,
  	.open_device = vfio_ap_mdev_open_device,
  	.close_device = vfio_ap_mdev_close_device,
  	.ioctl = vfio_ap_mdev_ioctl,
drivers/s390/crypto/vfio_ap_private.h +0 -3
index 648fcaf8104abb..a26efd804d0df3 100644
@@ -81,8 +81,6 @@ struct ap_matrix {
   * @node:	allows the ap_matrix_mdev struct to be added to a list
   * @matrix:	the adapters, usage domains and control domains assigned to the
   *		mediated matrix device.
- * @group_notifier: notifier block used for specifying callback function for
- *		    handling the VFIO_GROUP_NOTIFY_SET_KVM event
   * @iommu_notifier: notifier block used for specifying callback function for
   *		    handling the VFIO_IOMMU_NOTIFY_DMA_UNMAP even
   * @kvm:	the struct holding guest's state
@@ -94,7 +92,6 @@ struct ap_matrix_mdev {
  	struct vfio_device vdev;
  	struct list_head node;
  	struct ap_matrix matrix;
-	struct notifier_block group_notifier;
  	struct notifier_block iommu_notifier;
  	struct kvm *kvm;
  	crypto_hook pqap_hook;
drivers/vfio/vfio.c +27 -46
index cfd797629a21ab..1c20bb5484afde 100644
@@ -10,6 +10,7 @@
   * Author: Tom Lyon, [email protected]
   */
 
+#include "linux/kvm_host.h"
  #include <linux/cdev.h>
  #include <linux/compat.h>
  #include <linux/device.h>
@@ -1097,13 +1098,25 @@ static struct file *vfio_device_open(struct vfio_device *device)
 
  	down_write(&device->group->group_rwsem);
  	ret = vfio_device_assign_container(device);
-	up_write(&device->group->group_rwsem);
-	if (ret)
+	if (ret) {
+		up_write(&device->group->group_rwsem);
  		return ERR_PTR(ret);
+	}
+
+	if (device->ops->flags & VFIO_DEVICE_NEEDS_KVM)
+	{
+		if (!device->group->kvm) {
+			up_write(&device->group->group_rwsem);
+			goto err_unassign_container;
+		}
+		device->kvm = device->group->kvm;
+		kvm_get_kvm(device->kvm);
+	}
+	up_write(&device->group->group_rwsem);
 
  	if (!try_module_get(device->dev->driver->owner)) {
  		ret = -ENODEV;
-		goto err_unassign_container;
+		goto err_put_kvm;
  	}
 
  	mutex_lock(&device->dev_set->lock);
@@ -1147,6 +1160,11 @@ static struct file *vfio_device_open(struct vfio_device *device)
  	device->open_count--;
  	mutex_unlock(&device->dev_set->lock);
  	module_put(device->dev->driver->owner);
+err_put_kvm:
+	if (device->kvm) {
+		kvm_put_kvm(device->kvm);
+		device->kvm = NULL;
+	}
  err_unassign_container:
  	vfio_device_unassign_container(device);
  	return ERR_PTR(ret);
@@ -1344,6 +1362,10 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 
  	module_put(device->dev->driver->owner);
 
+	if (device->kvm) {
+		kvm_put_kvm(device->kvm);
+		device->kvm = NULL;
+	}
  	vfio_device_unassign_container(device);
 
  	vfio_device_put(device);
@@ -1748,8 +1770,8 @@ EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent);
   * @file: VFIO group file
   * @kvm: KVM to link
   *
- * The kvm pointer will be forwarded to all the vfio_device's attached to the
- * VFIO file via the VFIO_GROUP_NOTIFY_SET_KVM notifier.
+ * When a VFIO device is first opened the KVM will be available in
+ * device->kvm if VFIO_DEVICE_NEEDS_KVM is set.
   */
  void vfio_file_set_kvm(struct file *file, struct kvm *kvm)
  {
@@ -1760,8 +1782,6 @@ void vfio_file_set_kvm(struct file *file, struct kvm *kvm)
 
  	down_write(&group->group_rwsem);
  	group->kvm = kvm;
-	blocking_notifier_call_chain(&group->notifier,
-				     VFIO_GROUP_NOTIFY_SET_KVM, kvm);
  	up_write(&group->group_rwsem);
  }
  EXPORT_SYMBOL_GPL(vfio_file_set_kvm);
@@ -2061,42 +2081,6 @@ static int vfio_unregister_iommu_notifier(struct vfio_group *group,
  	return ret;
  }
 
-static int vfio_register_group_notifier(struct vfio_group *group,
-					unsigned long *events,
-					struct notifier_block *nb)
-{
-	int ret;
-	bool set_kvm = false;
-
-	if (*events & VFIO_GROUP_NOTIFY_SET_KVM)
-		set_kvm = true;
-
-	/* clear known events */
-	*events &= ~VFIO_GROUP_NOTIFY_SET_KVM;
-
-	/* refuse to continue if still events remaining */
-	if (*events)
-		return -EINVAL;
-
-	ret = blocking_notifier_chain_register(&group->notifier, nb);
-	if (ret)
-		return ret;
-
-	/*
-	 * The attaching of kvm and vfio_group might already happen, so
-	 * here we replay once upon registration.
-	 */
-	if (set_kvm) {
-		down_read(&group->group_rwsem);
-		if (group->kvm)
-			blocking_notifier_call_chain(&group->notifier,
-						     VFIO_GROUP_NOTIFY_SET_KVM,
-						     group->kvm);
-		up_read(&group->group_rwsem);
-	}
-	return 0;
-}
-
  int vfio_register_notifier(struct vfio_device *device,
  			   enum vfio_notify_type type, unsigned long *events,
  			   struct notifier_block *nb)
@@ -2112,9 +2096,6 @@ int vfio_register_notifier(struct vfio_device *device,
  	case VFIO_IOMMU_NOTIFY:
  		ret = vfio_register_iommu_notifier(group, events, nb);
  		break;
-	case VFIO_GROUP_NOTIFY:
-		ret = vfio_register_group_notifier(group, events, nb);
-		break;
  	default:
  		ret = -EINVAL;
  	}
include/linux/vfio.h +6 -4
index 45b287826ce686..aaf120b9c080b7 100644
@@ -36,6 +36,7 @@ struct vfio_device {
  	struct vfio_device_set *dev_set;
  	struct list_head dev_set_list;
  	unsigned int migration_flags;
+	struct kvm *kvm;
 
  	/* Members below here are private, not for driver use */
  	refcount_t refcount;
@@ -44,6 +45,10 @@ struct vfio_device {
  	struct list_head group_next;
  };
 
+enum {
+	VFIO_DEVICE_NEEDS_KVM = 1,
+};
+
  /**
   * struct vfio_device_ops - VFIO bus driver device callbacks
   *
@@ -72,6 +77,7 @@ struct vfio_device {
   */
  struct vfio_device_ops {
  	char	*name;
+	unsigned int flags;
  	int	(*open_device)(struct vfio_device *vdev);
  	void	(*close_device)(struct vfio_device *vdev);
  	ssize_t	(*read)(struct vfio_device *vdev, char __user *buf,
@@ -155,15 +161,11 @@ extern int vfio_dma_rw(struct vfio_device *device, dma_addr_t user_iova,
  /* each type has independent events */
  enum vfio_notify_type {
  	VFIO_IOMMU_NOTIFY = 0,
-	VFIO_GROUP_NOTIFY = 1,
  };
 
  /* events for VFIO_IOMMU_NOTIFY */
  #define VFIO_IOMMU_NOTIFY_DMA_UNMAP	BIT(0)
 
-/* events for VFIO_GROUP_NOTIFY */
-#define VFIO_GROUP_NOTIFY_SET_KVM	BIT(0)
-
  extern int vfio_register_notifier(struct vfio_device *device,
  				  enum vfio_notify_type type,
  				  unsigned long *required_events,
#23 lore raw

Re: [PATCH v7 17/22] vfio-pci/zdev: add open/close device hooks

Quoted 4 lines · Matthew Rosato

Conceptually I think this would work for QEMU anyway (it always sets the kvm before we open the device). I tried to test the idea quickly but couldn't get the following to apply on vfio-next or your vfio_group_locking -- but I understand what you're trying to do so I'll re-work and try it out.

I created it on 8c9350e9bf43de1ebab3cc8a80703671e6495ab4 which is the vfio_group_locking.. I can send you a github if it helps
https://github.com/jgunthorpe/linux/commits/vfio_group_lockin

Quoted 5 lines

@Alex can you think of any usecase/reason why we would need to be able to set the KVM sometime after the device was opened? Doing something like below would break that, as this introduces the assumption that the group is associated with the KVM before the device is opened (and if it's not, the device open fails).

Keep in mind that GVT already hard requires this ordering to even allow open_device to work - so it already sets a floor for what userspace can do..

Jason

#24 lore raw

Re: [PATCH v7 17/22] vfio-pci/zdev: add open/close device hooks

Quoted 17 lines · On Mon, 16 May 2022 15:35:58 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 4 lines · Matthew Rosato

Conceptually I think this would work for QEMU anyway (it always sets the kvm before we open the device). I tried to test the idea quickly but couldn't get the following to apply on vfio-next or your vfio_group_locking -- but I understand what you're trying to do so I'll re-work and try it out.

I created it on 8c9350e9bf43de1ebab3cc8a80703671e6495ab4 which is the vfio_group_locking.. I can send you a github if it helps
https://github.com/jgunthorpe/linux/commits/vfio_group_lockin

Quoted 5 lines

@Alex can you think of any usecase/reason why we would need to be able to set the KVM sometime after the device was opened? Doing something like below would break that, as this introduces the assumption that the group is associated with the KVM before the device is opened (and if it's not, the device open fails).

Quoted 3 lines

Keep in mind that GVT already hard requires this ordering to even allow open_device to work - so it already sets a floor for what userspace can do..

How is this going to work when vfio devices are exposed directly? We currently have a strict ordering through the group to get to the device, and it's therefore a reasonable requirement for userspace to register the group with kvm before opening the device. Is the notifier and async KVM registration necessary to support this dependency with direct device access? I don't have as clear a picture of the ordering with with direct access. Thanks,

Alex

#25 lore raw

Re: [PATCH v7 17/22] vfio-pci/zdev: add open/close device hooks

Quoted 24 lines · Alex Williamson
Quoted 17 lines · On Mon, 16 May 2022 15:35:58 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 4 lines · Matthew Rosato

Conceptually I think this would work for QEMU anyway (it always sets the kvm before we open the device). I tried to test the idea quickly but couldn't get the following to apply on vfio-next or your vfio_group_locking -- but I understand what you're trying to do so I'll re-work and try it out.

I created it on 8c9350e9bf43de1ebab3cc8a80703671e6495ab4 which is the vfio_group_locking.. I can send you a github if it helps
https://github.com/jgunthorpe/linux/commits/vfio_group_lockin

Quoted 5 lines

@Alex can you think of any usecase/reason why we would need to be able to set the KVM sometime after the device was opened? Doing something like below would break that, as this introduces the assumption that the group is associated with the KVM before the device is opened (and if it's not, the device open fails).

Quoted 3 lines

Keep in mind that GVT already hard requires this ordering to even allow open_device to work - so it already sets a floor for what userspace can do..

Quoted 7 lines

How is this going to work when vfio devices are exposed directly? We currently have a strict ordering through the group to get to the device, and it's therefore a reasonable requirement for userspace to register the group with kvm before opening the device. Is the notifier and async KVM registration necessary to support this dependency with direct device access? I don't have as clear a picture of the ordering with with direct access. Thanks,

With the device FD there is already a zombie state after open("/dev/...") but before the internal open_device op is called. This state ends after the iommufd is assigned, then we can go to open_device.

It is reasonable that the KVM would have to be setup before assigning the iommfd to the device fd.

Jason

author maintainer
#26 lore raw

Re: [PATCH v7 17/22] vfio-pci/zdev: add open/close device hooks

In reply to Jason Gunthorpe
Quoted 11 lines · Jason Gunthorpe
Quoted 4 lines · Matthew Rosato

Conceptually I think this would work for QEMU anyway (it always sets the kvm before we open the device). I tried to test the idea quickly but couldn't get the following to apply on vfio-next or your vfio_group_locking -- but I understand what you're trying to do so I'll re-work and try it out.

I created it on 8c9350e9bf43de1ebab3cc8a80703671e6495ab4 which is the vfio_group_locking.. I can send you a github if it helps
https://github.com/jgunthorpe/linux/commits/vfio_group_lockin

Thanks -- I was able to successfully test your proposed idea (+ some changes to make it compile :)) on top of vfio_group_locking along with a modified version of my zdev series. I also tried it out with vfio-ap successfully, but have nothing to test GVT with.

That said, this has caused me to realize that 'iommu: iommu_group_claim_dma_owner() must always assign a domain' breaks s390x vfio-pci :( I wonder if it is due to the way s390x PCI currently switches between dma ops and iommu ops. It looks like it breaks vfio-ap mdevs too, but I know less about that -- I will have to investigate both more tomorrow.

#27 lore raw

Re: [PATCH v7 17/22] vfio-pci/zdev: add open/close device hooks

Quoted 17 lines · Matthew Rosato
Quoted 11 lines · Jason Gunthorpe
Quoted 4 lines · Matthew Rosato

Conceptually I think this would work for QEMU anyway (it always sets the kvm before we open the device). I tried to test the idea quickly but couldn't get the following to apply on vfio-next or your vfio_group_locking -- but I understand what you're trying to do so I'll re-work and try it out.

I created it on 8c9350e9bf43de1ebab3cc8a80703671e6495ab4 which is the vfio_group_locking.. I can send you a github if it helps
https://github.com/jgunthorpe/linux/commits/vfio_group_lockin

Thanks -- I was able to successfully test your proposed idea (+ some changes to make it compile :)) on top of vfio_group_locking along with a modified version of my zdev series. I also tried it out with vfio-ap successfully, but have nothing to test GVT with.

Quoted 6 lines

That said, this has caused me to realize that 'iommu:
iommu_group_claim_dma_owner() must always assign a domain' breaks s390x vfio-pci :( I wonder if it is due to the way s390x PCI currently switches between dma ops and iommu ops. It looks like it breaks vfio-ap mdevs too, but I know less about that -- I will have to investigate both more tomorrow.

At the very least it appears that s390 thinks that attach/detach_dev are strictly paired and that isn't how the iommu ops are defined.

attach_dev can be called multiple times without any detach_dev.

Possibly the unbalance of zpci_register_ioat / zpci_unregister_ioat causes the trouble?

Jason

#28 lore raw

Re: [PATCH v7 17/22] vfio-pci/zdev: add open/close device hooks

In reply to Jason Gunthorpe
Quoted 6 lines · Jason Gunthorpe

Normally you'd want to do what is kvm_s390_pci_register_kvm() here, where a failure can be propogated but then you have a race condition with the kvm.

Blech, maybe it is time to just fix this race condition permanently, what do you think? (I didn't even compile it)

This is roughly were I was planning to get to, with one difference:
I don't think we need or even want the VFIO_DEVICE_NEEDS_KVM flag. Instead just propagation ->kvm to the device whenever it is set and let drivers that have a hard requirements on it like gvt fail if it isn't there. This could still allow using vfio for userspace PCI drivers on s390 for example or in general allow expressing a soft requirement, just without the whole notifier mess.

The other question is if we even need an extra reference per device, can't we hold the group reference until all devices are gone anyway? That would remove the need to include kvm_host.h in the vfio code.

#29 lore raw

Re: [PATCH v7 17/22] vfio-pci/zdev: add open/close device hooks

Quoted 5 lines · Christoph Hellwig
Quoted 4 lines · Jason Gunthorpe

Normally you'd want to do what is kvm_s390_pci_register_kvm() here, where a failure can be propogated but then you have a race condition with the kvm.

Quoted 8 lines
Quoted 2 lines

Blech, maybe it is time to just fix this race condition permanently, what do you think? (I didn't even compile it)

This is roughly were I was planning to get to, with one difference:
I don't think we need or even want the VFIO_DEVICE_NEEDS_KVM flag. Instead just propagation ->kvm to the device whenever it is set and let drivers that have a hard requirements on it like gvt fail if it isn't there.

I did it so we didn't uselessly hold a ref on the kvm object, but maybe that is not relevant.

Quoted 4 lines

The other question is if we even need an extra reference per device, can't we hold the group reference until all devices are gone anyway? That would remove the need to include kvm_host.h in the vfio code.

The device does now hold a reference on the group fd after this patch series:

https://lore.kernel.org/r/[email protected]

However the group does not hold a reference on the KVM, it has a set/remove interface toward KVM and can have its group->kvm pointer NULL'd via an ioctl at any time.

So, the semantic here is that the KVM is captured when the device FD opens and then is immutable for the lifetime of that device FD even if the group FD's KVM is reassigned or removed.

And I realize that it is all botched, this needs to check and respect the open_count which requires nesting the locks..

Jason

author maintainer
#30 lore raw

PATCH v7 18/22 vfio-pci/zdev: add function handle to clp base capability

In reply to Matthew Rosato

The function handle is a system-wide unique identifier for a zPCI device. With zPCI instruction interpretation, the host will no longer be executing the zPCI instructions on behalf of the guest. As a result, the guest needs to use the real function handle in order for firmware to associate the instruction with the proper PCI function. Let's provide that handle to the guest.

---
 drivers/vfio/pci/vfio_pci_zdev.c | 5 +++--
 include/uapi/linux/vfio_zdev.h   | 3 +++
 2 files changed, 6 insertions(+), 2 deletions(-)
drivers/vfio/pci/vfio_pci_zdev.c +3 -2
index 130ab298ab98..c7557f80ec64 100644
@@ -24,14 +24,15 @@ static int zpci_base_cap(struct zpci_dev *zdev, struct vfio_info_cap *caps)
 {
 	struct vfio_device_info_cap_zpci_base cap = {
 		.header.id = VFIO_DEVICE_INFO_CAP_ZPCI_BASE,
-		.header.version = 1,
+		.header.version = 2,
 		.start_dma = zdev->start_dma,
 		.end_dma = zdev->end_dma,
 		.pchid = zdev->pchid,
 		.vfn = zdev->vfn,
 		.fmb_length = zdev->fmb_length,
 		.pft = zdev->pft,
-		.gid = zdev->pfgid
+		.gid = zdev->pfgid,
+		.fh = zdev->fh
 	};
 
 	return vfio_info_add_capability(caps, &cap.header, sizeof(cap));
include/uapi/linux/vfio_zdev.h +3 -0
index b4309397b6b2..78c022af3d29 100644
@@ -29,6 +29,9 @@ struct vfio_device_info_cap_zpci_base {
 	__u16 fmb_length;	/* Measurement Block Length (in bytes) */
 	__u8 pft;		/* PCI Function Type */
 	__u8 gid;		/* PCI function group ID */
+	/* End of version 1 */
+	__u32 fh;		/* PCI function handle */
+	/* End of version 2 */
 };
 
 /**
Signature · 1 line
2.27.0
author maintainer
#31 lore raw

PATCH v7 19/22 vfio-pci/zdev: different maxstbl for interpreted devices

In reply to Matthew Rosato

When doing load/store interpretation, the maximum store block length is determined by the underlying firmware, not the host kernel API. Reflect that in the associated Query PCI Function Group clp capability and let userspace decide which is appropriate to present to the guest.

---
 drivers/vfio/pci/vfio_pci_zdev.c | 6 ++++--
 include/uapi/linux/vfio_zdev.h   | 4 ++++
 2 files changed, 8 insertions(+), 2 deletions(-)
drivers/vfio/pci/vfio_pci_zdev.c +4 -2
index c7557f80ec64..067383f84cd8 100644
@@ -45,14 +45,16 @@ static int zpci_group_cap(struct zpci_dev *zdev, struct vfio_info_cap *caps)
 {
 	struct vfio_device_info_cap_zpci_group cap = {
 		.header.id = VFIO_DEVICE_INFO_CAP_ZPCI_GROUP,
-		.header.version = 1,
+		.header.version = 2,
 		.dasm = zdev->dma_mask,
 		.msi_addr = zdev->msi_addr,
 		.flags = VFIO_DEVICE_INFO_ZPCI_FLAG_REFRESH,
 		.mui = zdev->fmb_update,
 		.noi = zdev->max_msi,
 		.maxstbl = ZPCI_MAX_WRITE_SIZE,
-		.version = zdev->version
+		.version = zdev->version,
+		.reserved = 0,
+		.imaxstbl = zdev->maxstbl
 	};
 
 	return vfio_info_add_capability(caps, &cap.header, sizeof(cap));
include/uapi/linux/vfio_zdev.h +4 -0
index 78c022af3d29..77f2aff1f27e 100644
@@ -50,6 +50,10 @@ struct vfio_device_info_cap_zpci_group {
 	__u16 noi;		/* Maximum number of MSIs */
 	__u16 maxstbl;		/* Maximum Store Block Length */
 	__u8 version;		/* Supported PCI Version */
+	/* End of version 1 */
+	__u8 reserved;
+	__u16 imaxstbl;		/* Maximum Interpreted Store Block Length */
+	/* End of version 2 */
 };
 
 /**
Signature · 1 line
2.27.0
author maintainer
#32 lore raw

PATCH v7 20/22 KVM: s390: add KVM_S390_ZPCI_OP to manage guest zPCI devices

In reply to Matthew Rosato

The KVM_S390_ZPCI_OP ioctl provides a mechanism for managing hardware-assisted virtualization features for s390X zPCI passthrough. Add the first 2 operations, which can be used to enable/disable the specified device for Adapter Event Notification interpretation.

---
 Documentation/virt/kvm/api.rst | 45 +++++++++++++++++++
 arch/s390/kvm/kvm-s390.c       | 23 ++++++++++
 arch/s390/kvm/pci.c            | 81 ++++++++++++++++++++++++++++++++++
 arch/s390/kvm/pci.h            |  2 +
 include/uapi/linux/kvm.h       | 31 +++++++++++++
 5 files changed, 182 insertions(+)
Documentation/virt/kvm/api.rst +45 -0
index 4a900cdbc62e..a7cd5ebce031 100644
@@ -5645,6 +5645,51 @@ enabled with ``arch_prctl()``, but this may change in the future.
 The offsets of the state save areas in struct kvm_xsave follow the contents
 of CPUID leaf 0xD on the host.
 
+4.135 KVM_S390_ZPCI_OP
+--------------------
+
+:Capability: KVM_CAP_S390_ZPCI_OP
+:Architectures: s390
+:Type: vcpu ioctl
+:Parameters: struct kvm_s390_zpci_op (in)
+:Returns: 0 on success, <0 on error
+
+Used to manage hardware-assisted virtualization features for zPCI devices.
+
+Parameters are specified via the following structure::
+
+  struct kvm_s390_zpci_op {
+	/* in */
+	__u32 fh;		/* target device */
+	__u8  op;		/* operation to perform */
+	__u8  pad[3];
+	union {
+		/* for KVM_S390_ZPCIOP_REG_AEN */
+		struct {
+			__u64 ibv;	/* Guest addr of interrupt bit vector */
+			__u64 sb;	/* Guest addr of summary bit */
+			__u32 flags;
+			__u32 noi;	/* Number of interrupts */
+			__u8 isc;	/* Guest interrupt subclass */
+			__u8 sbo;	/* Offset of guest summary bit vector */
+			__u16 pad;
+		} reg_aen;
+		__u64 reserved[8];
+	} u;
+  };
+
+The type of operation is specified in the "op" field.
+KVM_S390_ZPCIOP_REG_AEN is used to register the VM for adapter event
+notification interpretation, which will allow firmware delivery of adapter
+events directly to the vm, with KVM providing a backup delivery mechanism;
+KVM_S390_ZPCIOP_DEREG_AEN is used to subsequently disable interpretation of
+adapter event notifications.
+
+The target zPCI function must also be specified via the "fh" field.  For the
+KVM_S390_ZPCIOP_REG_AEN operation, additional information to establish firmware
+delivery must be provided via the "reg_aen" struct.
+
+The "reserved" field is meant for future extensions.
 
 5. The kvm_run structure
 ========================
arch/s390/kvm/kvm-s390.c +23 -0
index b95b25490018..1af7cea9d579 100644
@@ -618,6 +618,12 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_S390_PROTECTED:
 		r = is_prot_virt_host();
 		break;
+	case KVM_CAP_S390_ZPCI_OP:
+		if (kvm_s390_pci_interp_allowed())
+			r = 1;
+		else
+			r = 0;
+		break;
 	default:
 		r = 0;
 	}
@@ -2633,6 +2639,23 @@ long kvm_arch_vm_ioctl(struct file *filp,
 			r = -EFAULT;
 		break;
 	}
+	case KVM_S390_ZPCI_OP: {
+		struct kvm_s390_zpci_op args;
+
+		r = -EINVAL;
+		if (!IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
+			break;
+		if (copy_from_user(&args, argp, sizeof(args))) {
+			r = -EFAULT;
+			break;
+		}
+		r = kvm_s390_pci_zpci_op(kvm, &args);
+		if (r)
+			break;
+		if (copy_to_user(argp, &args, sizeof(args)))
+			r = -EFAULT;
+		break;
+	}
 	default:
 		r = -ENOTTY;
 	}
arch/s390/kvm/pci.c +81 -0

87 lines not on this page · raw message

arch/s390/kvm/pci.h +2 -0
index fb2b91b76e0c..0351382e990f 100644
@@ -59,6 +59,8 @@ void kvm_s390_pci_aen_exit(void);
 void kvm_s390_pci_init_list(struct kvm *kvm);
 void kvm_s390_pci_clear_list(struct kvm *kvm);
 
+int kvm_s390_pci_zpci_op(struct kvm *kvm, struct kvm_s390_zpci_op *args);
+
 int kvm_s390_pci_init(void);
 void kvm_s390_pci_exit(void);
 
include/uapi/linux/kvm.h +31 -0
index 6a184d260c7f..1d3d41523d10 100644
@@ -1152,6 +1152,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_DISABLE_QUIRKS2 213
 /* #define KVM_CAP_VM_TSC_CONTROL 214 */
 #define KVM_CAP_SYSTEM_EVENT_DATA 215
+#define KVM_CAP_S390_ZPCI_OP 216
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -2068,4 +2069,34 @@ struct kvm_stats_desc {
 /* Available with KVM_CAP_XSAVE2 */
 #define KVM_GET_XSAVE2		  _IOR(KVMIO,  0xcf, struct kvm_xsave)
 
+/* Available with KVM_CAP_S390_ZPCI_OP */
+#define KVM_S390_ZPCI_OP	  _IOW(KVMIO,  0xd0, struct kvm_s390_zpci_op)
+
+struct kvm_s390_zpci_op {
+	/* in */
+	__u32 fh;		/* target device */
+	__u8  op;		/* operation to perform */
+	__u8  pad[3];
+	union {
+		/* for KVM_S390_ZPCIOP_REG_AEN */
+		struct {
+			__u64 ibv;	/* Guest addr of interrupt bit vector */
+			__u64 sb;	/* Guest addr of summary bit */
+			__u32 flags;
+			__u32 noi;	/* Number of interrupts */
+			__u8 isc;	/* Guest interrupt subclass */
+			__u8 sbo;	/* Offset of guest summary bit vector */
+			__u16 pad;
+		} reg_aen;
+		__u64 reserved[8];
+	} u;
+};
+
+/* types for kvm_s390_zpci_op->op */
+#define KVM_S390_ZPCIOP_REG_AEN		0
+#define KVM_S390_ZPCIOP_DEREG_AEN	1
+
+/* flags for kvm_s390_zpci_op->u.reg_aen.flags */
+#define KVM_S390_ZPCIOP_REGAEN_HOST	(1 << 0)
+
 #endif /* __LINUX_KVM_H */
Signature · 1 line
2.27.0
#33 lore raw

Re: [PATCH v7 20/22] KVM: s390: add KVM_S390_ZPCI_OP to manage guest zPCI devices

Quoted 2 lines · Matthew Rosato

The KVM_S390_ZPCI_OP ioctl provides a mechanism for managing hardware-assisted virtualization features for s390X zPCI passthrough.

s/s390X/s390x/

Quoted 17 lines

Add the first 2 operations, which can be used to enable/disable the specified device for Adapter Event Notification interpretation.

---
  Documentation/virt/kvm/api.rst | 45 +++++++++++++++++++
  arch/s390/kvm/kvm-s390.c       | 23 ++++++++++
  arch/s390/kvm/pci.c            | 81 ++++++++++++++++++++++++++++++++++
  arch/s390/kvm/pci.h            |  2 +
  include/uapi/linux/kvm.h       | 31 +++++++++++++
  5 files changed, 182 insertions(+)
Documentation/virt/kvm/api.rst +0 -0
index 4a900cdbc62e..a7cd5ebce031 100644

Documentation/virt/kvm/api.rst enabled with ``arch_prctl()``, but this may change in the future. · line 5653

Quoted 9 lines
Documentation/virt/kvm/api.rst +6 -0
@@ -5645,6 +5645,51 @@ enabled with ``arch_prctl()``, but this may change in the future.
  The offsets of the state save areas in struct kvm_xsave follow the contents
  of CPUID leaf 0xD on the host.
 
+4.135 KVM_S390_ZPCI_OP
+--------------------
+
+:Capability: KVM_CAP_S390_ZPCI_OP
+:Architectures: s390
+:Type: vcpu ioctl

vcpu? ... you're wiring it up in kvm_arch_vm_ioctl() later, so I assume it's rather a VM ioctl?

Documentation/virt/kvm/api.rst enabled with ``arch_prctl()``, but this may change in the future. · line 5662

Quoted 9 lines
Documentation/virt/kvm/api.rst +9 -0
+:Parameters: struct kvm_s390_zpci_op (in)
+:Returns: 0 on success, <0 on error
+
+Used to manage hardware-assisted virtualization features for zPCI devices.
+
+Parameters are specified via the following structure::
+
+  struct kvm_s390_zpci_op {
+	/* in */

If all is "in", why is there a copy_to_user() in the code later?

Documentation/virt/kvm/api.rst enabled with ``arch_prctl()``, but this may change in the future. · line 5670

Quoted 8 lines
Documentation/virt/kvm/api.rst +8 -0
+	__u32 fh;		/* target device */
+	__u8  op;		/* operation to perform */
+	__u8  pad[3];
+	union {
+		/* for KVM_S390_ZPCIOP_REG_AEN */
+		struct {
+			__u64 ibv;	/* Guest addr of interrupt bit vector */
+			__u64 sb;	/* Guest addr of summary bit */

If this is really a vcpu ioctl, what kind of addresses are you talking about here? virtual addresses? real addresses? absolute addresses?

Quoted 12 lines
Documentation/virt/kvm/api.rst +12 -0
+			__u32 flags;
+			__u32 noi;	/* Number of interrupts */
+			__u8 isc;	/* Guest interrupt subclass */
+			__u8 sbo;	/* Offset of guest summary bit vector */
+			__u16 pad;
+		} reg_aen;
+		__u64 reserved[8];
+	} u;
+  };
+
+The type of operation is specified in the "op" field.
+KVM_S390_ZPCIOP_REG_AEN is used to register the VM for adapter event

Documentation/virt/kvm/api.rst enabled with ``arch_prctl()``, but this may change in the future. · line 5692

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+notification interpretation, which will allow firmware delivery of adapter
+events directly to the vm, with KVM providing a backup delivery mechanism;
+KVM_S390_ZPCIOP_DEREG_AEN is used to subsequently disable interpretation of
+adapter event notifications.
+
+The target zPCI function must also be specified via the "fh" field.  For the
+KVM_S390_ZPCIOP_REG_AEN operation, additional information to establish firmware
+delivery must be provided via the "reg_aen" struct.
+
+The "reserved" field is meant for future extensions.

Maybe also mention the "pad" fields? And add should these also be initialized to 0 by the calling userspace program?

Quoted 7 lines
Documentation/virt/kvm/api.rst +0 -0
  5. The kvm_run structure
  ========================
arch/s390/kvm/kvm-s390.c +0 -0
index b95b25490018..1af7cea9d579 100644

arch/s390/kvm/kvm-s390.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 625

Quoted 8 lines
arch/s390/kvm/kvm-s390.c +5 -0
@@ -618,6 +618,12 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  	case KVM_CAP_S390_PROTECTED:
  		r = is_prot_virt_host();
  		break;
+	case KVM_CAP_S390_ZPCI_OP:
+		if (kvm_s390_pci_interp_allowed())
+			r = 1;
+		else
+			r = 0;

Could be shortened to:

		r = kvm_s390_pci_interp_allowed();
Quoted 13 lines
arch/s390/kvm/kvm-s390.c +6 -0
+		break;
  	default:
  		r = 0;
  	}
@@ -2633,6 +2639,23 @@ long kvm_arch_vm_ioctl(struct file *filp,
  			r = -EFAULT;
  		break;
  	}
+	case KVM_S390_ZPCI_OP: {
+		struct kvm_s390_zpci_op args;
+
+		r = -EINVAL;
+		if (!IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))

arch/s390/kvm/kvm-s390.c long kvm_arch_vm_ioctl(struct file *filp, · line 2656

Quoted 10 lines
arch/s390/kvm/kvm-s390.c +10 -0
+			break;
+		if (copy_from_user(&args, argp, sizeof(args))) {
+			r = -EFAULT;
+			break;
+		}
+		r = kvm_s390_pci_zpci_op(kvm, &args);
+		if (r)
+			break;
+		if (copy_to_user(argp, &args, sizeof(args)))
+			r = -EFAULT;

So this copy_to_user() indicates that information is returned to userspace ... but below, the ioctl is declared with _IOW only ... this does not match. Should it be declared with _IOWR or should the copy_to_user() be dropped?

Quoted 42 lines
arch/s390/kvm/kvm-s390.c +2 -0
+		break;
+	}
  	default:
  		r = -ENOTTY;
  	}
arch/s390/kvm/pci.c +29 -0
index 1393a1604494..6e6254016be4 100644
@@ -585,6 +585,87 @@ void kvm_s390_pci_clear_list(struct kvm *kvm)
  	spin_unlock(&kvm->arch.kzdev_list_lock);
  }
 
+static struct zpci_dev *get_zdev_from_kvm_by_fh(struct kvm *kvm, u32 fh)
+{
+	struct zpci_dev *zdev = NULL;
+	struct kvm_zdev *kzdev;
+
+	spin_lock(&kvm->arch.kzdev_list_lock);
+	list_for_each_entry(kzdev, &kvm->arch.kzdev_list, entry) {
+		if (kzdev->zdev->fh == fh) {
+			zdev = kzdev->zdev;
+			break;
+		}
+	}
+	spin_unlock(&kvm->arch.kzdev_list_lock);
+
+	return zdev;
+}
+
+static int kvm_s390_pci_zpci_reg_aen(struct zpci_dev *zdev,
+				     struct kvm_s390_zpci_op *args)
+{
+	struct zpci_fib fib = {};
+
+	fib.fmt0.aibv = args->u.reg_aen.ibv;
+	fib.fmt0.isc = args->u.reg_aen.isc;
+	fib.fmt0.noi = args->u.reg_aen.noi;
+	if (args->u.reg_aen.sb != 0) {
+		fib.fmt0.aisb = args->u.reg_aen.sb;
+		fib.fmt0.aisbo = args->u.reg_aen.sbo;
+		fib.fmt0.sum = 1;

arch/s390/kvm/pci.c void kvm_s390_pci_clear_list(struct kvm *kvm) · line 626

Quoted 10 lines
arch/s390/kvm/pci.c +10 -0
+	} else {
+		fib.fmt0.aisb = 0;
+		fib.fmt0.aisbo = 0;
+		fib.fmt0.sum = 0;
+	}
+
+	if (args->u.reg_aen.flags & KVM_S390_ZPCIOP_REGAEN_HOST)
+		return kvm_s390_pci_aif_enable(zdev, &fib, true);
+	else
+		return kvm_s390_pci_aif_enable(zdev, &fib, false);

Alternatively (just a matter of taste):

	bool hostflag;
	...
	hostflag = (args->u.reg_aen.flags & KVM_S390_ZPCIOP_REGAEN_HOST);
	return kvm_s390_pci_aif_enable(zdev, &fib, hostflag);
Quoted 17 lines
arch/s390/kvm/pci.c +17 -0
+}
+
+int kvm_s390_pci_zpci_op(struct kvm *kvm, struct kvm_s390_zpci_op *args)
+{
+	struct kvm_zdev *kzdev;
+	struct zpci_dev *zdev;
+	int r;
+
+	zdev = get_zdev_from_kvm_by_fh(kvm, args->fh);
+	if (!zdev)
+		return -ENODEV;
+
+	mutex_lock(&zdev->kzdev_lock);
+	mutex_lock(&kvm->lock);
+
+	kzdev = zdev->kzdev;
+	if (!kzdev) {

arch/s390/kvm/pci.c void kvm_s390_pci_clear_list(struct kvm *kvm) · line 653

Quoted 10 lines
arch/s390/kvm/pci.c +10 -0
+		r = -ENODEV;
+		goto out;
+	}
+	if (kzdev->kvm != kvm) {
+		r = -EPERM;
+		goto out;
+	}
+
+	switch (args->op) {
+	case KVM_S390_ZPCIOP_REG_AEN:

Please also check here that args->u.reg_aen.flags does not have any bits set that we don't support here (otherwise, this could cause some trouble when introducing additional flags later).

Quoted 44 lines
arch/s390/kvm/pci.c +15 -0
+		r = kvm_s390_pci_zpci_reg_aen(zdev, args);
+		break;
+	case KVM_S390_ZPCIOP_DEREG_AEN:
+		r = kvm_s390_pci_aif_disable(zdev, false);
+		break;
+	default:
+		r = -EINVAL;
+	}
+
+out:
+	mutex_unlock(&kvm->lock);
+	mutex_unlock(&zdev->kzdev_lock);
+	return r;
+}
+
  int kvm_s390_pci_init(void)
  {
  	aift = kzalloc(sizeof(struct zpci_aift), GFP_KERNEL);
arch/s390/kvm/pci.h +2 -0
index fb2b91b76e0c..0351382e990f 100644
@@ -59,6 +59,8 @@ void kvm_s390_pci_aen_exit(void);
  void kvm_s390_pci_init_list(struct kvm *kvm);
  void kvm_s390_pci_clear_list(struct kvm *kvm);
 
+int kvm_s390_pci_zpci_op(struct kvm *kvm, struct kvm_s390_zpci_op *args);
+
  int kvm_s390_pci_init(void);
  void kvm_s390_pci_exit(void);
 
include/uapi/linux/kvm.h +1 -0
index 6a184d260c7f..1d3d41523d10 100644
@@ -1152,6 +1152,7 @@ struct kvm_ppc_resize_hpt {
  #define KVM_CAP_DISABLE_QUIRKS2 213
  /* #define KVM_CAP_VM_TSC_CONTROL 214 */
  #define KVM_CAP_SYSTEM_EVENT_DATA 215
+#define KVM_CAP_S390_ZPCI_OP 216
 
  #ifdef KVM_CAP_IRQ_ROUTING
 

include/uapi/linux/kvm.h struct kvm_stats_desc { · line 2073

Quoted 5 lines
include/uapi/linux/kvm.h +2 -0
@@ -2068,4 +2069,34 @@ struct kvm_stats_desc {
  /* Available with KVM_CAP_XSAVE2 */
  #define KVM_GET_XSAVE2		  _IOR(KVMIO,  0xcf, struct kvm_xsave)
 
+/* Available with KVM_CAP_S390_ZPCI_OP */
+#define KVM_S390_ZPCI_OP	  _IOW(KVMIO,  0xd0, struct kvm_s390_zpci_op)

Please double-check whether this should be _IOWR instead (see above).

Quoted 18 lines
include/uapi/linux/kvm.h +18 -0
+struct kvm_s390_zpci_op {
+	/* in */
+	__u32 fh;		/* target device */
+	__u8  op;		/* operation to perform */
+	__u8  pad[3];
+	union {
+		/* for KVM_S390_ZPCIOP_REG_AEN */
+		struct {
+			__u64 ibv;	/* Guest addr of interrupt bit vector */
+			__u64 sb;	/* Guest addr of summary bit */
+			__u32 flags;
+			__u32 noi;	/* Number of interrupts */
+			__u8 isc;	/* Guest interrupt subclass */
+			__u8 sbo;	/* Offset of guest summary bit vector */
+			__u16 pad;
+		} reg_aen;
+		__u64 reserved[8];
+	} u;

include/uapi/linux/kvm.h struct kvm_stats_desc { · line 2102

Quoted 10 lines
include/uapi/linux/kvm.h +9 -0
+};
+
+/* types for kvm_s390_zpci_op->op */
+#define KVM_S390_ZPCIOP_REG_AEN		0
+#define KVM_S390_ZPCIOP_DEREG_AEN	1
+
+/* flags for kvm_s390_zpci_op->u.reg_aen.flags */
+#define KVM_S390_ZPCIOP_REGAEN_HOST	(1 << 0)
+
  #endif /* __LINUX_KVM_H */

Thomas

author maintainer
#34 lore raw

Re: [PATCH v7 20/22] KVM: s390: add KVM_S390_ZPCI_OP to manage guest zPCI devices

Quoted 18 lines · Thomas Huth
Quoted 2 lines · Matthew Rosato

The KVM_S390_ZPCI_OP ioctl provides a mechanism for managing hardware-assisted virtualization features for s390X zPCI passthrough.

s/s390X/s390x/

Quoted 12 lines

Add the first 2 operations, which can be used to enable/disable the specified device for Adapter Event Notification interpretation.

---
  Documentation/virt/kvm/api.rst | 45 +++++++++++++++++++ arch/s390/kvm/kvm-s390.c       | 23 ++++++++++ arch/s390/kvm/pci.c            | 81 ++++++++++++++++++++++++++++++++++ arch/s390/kvm/pci.h            |  2 + include/uapi/linux/kvm.h       | 31 +++++++++++++

  5 files changed, 182 insertions(+)
Quoted 16 lines
Quoted 6 lines
Documentation/virt/kvm/api.rst +0 -0
index 4a900cdbc62e..a7cd5ebce031 100644
@@ -5645,6 +5645,51 @@ enabled with ``arch_prctl()``, but this may 

change in the future.

Quoted 8 lines

  The offsets of the state save areas in struct kvm_xsave follow the contents of CPUID leaf 0xD on the host.
+4.135 KVM_S390_ZPCI_OP
+--------------------
+
+:Capability: KVM_CAP_S390_ZPCI_OP
+:Architectures: s390
+:Type: vcpu ioctl

vcpu? ... you're wiring it up in  kvm_arch_vm_ioctl() later, so I assume it's rather a VM ioctl?

Yup, VM ioctl, bad copy/paste job...

Quoted 14 lines

Documentation/virt/kvm/api.rst enabled with ``arch_prctl()``, but this may change in the future. · line 5662

Quoted 11 lines

+:Parameters: struct kvm_s390_zpci_op (in)
+:Returns: 0 on success, <0 on error

Documentation/virt/kvm/api.rst +2 -0
+
+Used to manage hardware-assisted virtualization features for zPCI 

devices.

Documentation/virt/kvm/api.rst +5 -0
+
+Parameters are specified via the following structure::
+
+  struct kvm_s390_zpci_op {
+    /* in */

If all is "in", why is there a copy_to_user() in the code later?

Oh no, this is a leftover from a prior version... Good catch. There should no longer be a copy_to_user.

Quoted 10 lines
Quoted 8 lines

+    __u32 fh;        /* target device */
+    __u8  op;        /* operation to perform */
+    __u8  pad[3];
+    union {
+        /* for KVM_S390_ZPCIOP_REG_AEN */
+        struct {
+            __u64 ibv;    /* Guest addr of interrupt bit vector */ +            __u64 sb;    /* Guest addr of summary bit */

If this is really a vcpu ioctl, what kind of addresses are you talking about here? virtual addresses? real addresses? absolute addresses?

It's a VM ioctl. These are guest kernel physical addresses that are later pinned in arch/s390/kvm/pci.c:kvm_s390_pci_aif_enable() as part of handling the ioctl.

Quoted 22 lines
Quoted 22 lines

+            __u32 flags;
+            __u32 noi;    /* Number of interrupts */
+            __u8 isc;    /* Guest interrupt subclass */ +            __u8 sbo;    /* Offset of guest summary bit vector */ +            __u16 pad;
+        } reg_aen;
+        __u64 reserved[8];
+    } u;
+  };

Documentation/virt/kvm/api.rst +4 -0
+
+The type of operation is specified in the "op" field.
+KVM_S390_ZPCIOP_REG_AEN is used to register the VM for adapter event
+notification interpretation, which will allow firmware delivery of 

adapter
+events directly to the vm, with KVM providing a backup delivery mechanism;
+KVM_S390_ZPCIOP_DEREG_AEN is used to subsequently disable interpretation of

Documentation/virt/kvm/api.rst +3 -0
+adapter event notifications.
+
+The target zPCI function must also be specified via the "fh" field.  
Quoted 8 lines

Documentation/virt/kvm/api.rst enabled with ``arch_prctl()``, but this may change in the future. · line 5692

Quoted 6 lines

For the
+KVM_S390_ZPCIOP_REG_AEN operation, additional information to establish firmware

Documentation/virt/kvm/api.rst +3 -0
+delivery must be provided via the "reg_aen" struct.
+
+The "reserved" field is meant for future extensions.

Maybe also mention the "pad" fields? And add should these also be initialized to 0 by the calling userspace program?

Sure, I can mention them. And yes, I agree that userspace should initialize them to 0, I'll update the QEMU series accordingly.

Quoted 39 lines
Quoted 9 lines

  5. The kvm_run structure
  ========================

arch/s390/kvm/kvm-s390.c +0 -0
index b95b25490018..1af7cea9d579 100644
@@ -618,6 +618,12 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, 
 long ext)
Quoted 8 lines

      case KVM_CAP_S390_PROTECTED:
          r = is_prot_virt_host();
          break;
+    case KVM_CAP_S390_ZPCI_OP:
+        if (kvm_s390_pci_interp_allowed())
+            r = 1;
+        else
+            r = 0;

Could be shortened to:

        r = kvm_s390_pci_interp_allowed();

Quoted 17 lines

+        break;
      default:
          r = 0;
      }

arch/s390/kvm/kvm-s390.c +9 -0
@@ -2633,6 +2639,23 @@ long kvm_arch_vm_ioctl(struct file *filp,
               r = -EFAULT;
           break;
       }
+    case KVM_S390_ZPCI_OP: {
+        struct kvm_s390_zpci_op args;
+
+        r = -EINVAL;
+        if (!IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
+            break;
+        if (copy_from_user(&args, argp, sizeof(args))) {
+            r = -EFAULT;
+            break;
Quoted 8 lines

arch/s390/kvm/kvm-s390.c long kvm_arch_vm_ioctl(struct file *filp, · line 2656

Quoted 6 lines
arch/s390/kvm/kvm-s390.c +6 -0
+        }
+        r = kvm_s390_pci_zpci_op(kvm, &args);
+        if (r)
+            break;
+        if (copy_to_user(argp, &args, sizeof(args)))
+            r = -EFAULT;

So this copy_to_user() indicates that information is returned to userspace ... but below, the ioctl is declared with _IOW only ... this does not match. Should it be declared with _IOWR or should the copy_to_user() be dropped?

copy_to_user should be dropped. Thanks!

Quoted 81 lines
Quoted 42 lines
arch/s390/kvm/kvm-s390.c +2 -0
+        break;
+    }

      default:
          r = -ENOTTY;
      }

arch/s390/kvm/pci.c +29 -0
index 1393a1604494..6e6254016be4 100644
@@ -585,6 +585,87 @@ void kvm_s390_pci_clear_list(struct kvm *kvm)
       spin_unlock(&kvm->arch.kzdev_list_lock);
   }
+static struct zpci_dev *get_zdev_from_kvm_by_fh(struct kvm *kvm, u32 fh)
+{
+    struct zpci_dev *zdev = NULL;
+    struct kvm_zdev *kzdev;
+
+    spin_lock(&kvm->arch.kzdev_list_lock);
+    list_for_each_entry(kzdev, &kvm->arch.kzdev_list, entry) {
+        if (kzdev->zdev->fh == fh) {
+            zdev = kzdev->zdev;
+            break;
+        }
+    }
+    spin_unlock(&kvm->arch.kzdev_list_lock);
+
+    return zdev;
+}
+
+static int kvm_s390_pci_zpci_reg_aen(struct zpci_dev *zdev,
+                     struct kvm_s390_zpci_op *args)
+{
+    struct zpci_fib fib = {};
+
+    fib.fmt0.aibv = args->u.reg_aen.ibv;
+    fib.fmt0.isc = args->u.reg_aen.isc;
+    fib.fmt0.noi = args->u.reg_aen.noi;
+    if (args->u.reg_aen.sb != 0) {
+        fib.fmt0.aisb = args->u.reg_aen.sb;
+        fib.fmt0.aisbo = args->u.reg_aen.sbo;
+        fib.fmt0.sum = 1;

arch/s390/kvm/pci.c void kvm_s390_pci_clear_list(struct kvm *kvm) · line 625

Quoted 10 lines
arch/s390/kvm/pci.c +10 -0
+    } else {
+        fib.fmt0.aisb = 0;
+        fib.fmt0.aisbo = 0;
+        fib.fmt0.sum = 0;
+    }
+
+    if (args->u.reg_aen.flags & KVM_S390_ZPCIOP_REGAEN_HOST)
+        return kvm_s390_pci_aif_enable(zdev, &fib, true);
+    else
+        return kvm_s390_pci_aif_enable(zdev, &fib, false);

Alternatively (just a matter of taste):

    bool hostflag;
    ...
    hostflag = (args->u.reg_aen.flags & KVM_S390_ZPCIOP_REGAEN_HOST); return kvm_s390_pci_aif_enable(zdev, &fib, hostflag);

Quoted 21 lines
arch/s390/kvm/pci.c +21 -0
+}
+
+int kvm_s390_pci_zpci_op(struct kvm *kvm, struct kvm_s390_zpci_op *args)
+{
+    struct kvm_zdev *kzdev;
+    struct zpci_dev *zdev;
+    int r;
+
+    zdev = get_zdev_from_kvm_by_fh(kvm, args->fh);
+    if (!zdev)
+        return -ENODEV;
+
+    mutex_lock(&zdev->kzdev_lock);
+    mutex_lock(&kvm->lock);
+
+    kzdev = zdev->kzdev;
+    if (!kzdev) {
+        r = -ENODEV;
+        goto out;
+    }
+    if (kzdev->kvm != kvm) {
Quoted 8 lines

arch/s390/kvm/pci.c void kvm_s390_pci_clear_list(struct kvm *kvm) · line 653

Quoted 6 lines
arch/s390/kvm/pci.c +6 -0
+        r = -EPERM;
+        goto out;
+    }
+
+    switch (args->op) {
+    case KVM_S390_ZPCIOP_REG_AEN:

Please also check here that args->u.reg_aen.flags does not have any bits set that we don't support here (otherwise, this could cause some trouble when introducing additional flags later).

Good idea, will do

Quoted 41 lines
Quoted 41 lines
arch/s390/kvm/pci.c +15 -0
+        r = kvm_s390_pci_zpci_reg_aen(zdev, args);
+        break;
+    case KVM_S390_ZPCIOP_DEREG_AEN:
+        r = kvm_s390_pci_aif_disable(zdev, false);
+        break;
+    default:
+        r = -EINVAL;
+    }
+
+out:
+    mutex_unlock(&kvm->lock);
+    mutex_unlock(&zdev->kzdev_lock);
+    return r;
+}
+
   int kvm_s390_pci_init(void)
   {
       aift = kzalloc(sizeof(struct zpci_aift), GFP_KERNEL);
arch/s390/kvm/pci.h +2 -0
index fb2b91b76e0c..0351382e990f 100644
@@ -59,6 +59,8 @@ void kvm_s390_pci_aen_exit(void);
   void kvm_s390_pci_init_list(struct kvm *kvm);
   void kvm_s390_pci_clear_list(struct kvm *kvm);
+int kvm_s390_pci_zpci_op(struct kvm *kvm, struct kvm_s390_zpci_op 
 *args);
+
   int kvm_s390_pci_init(void);
   void kvm_s390_pci_exit(void);
include/uapi/linux/kvm.h +1 -0
index 6a184d260c7f..1d3d41523d10 100644
@@ -1152,6 +1152,7 @@ struct kvm_ppc_resize_hpt {
   #define KVM_CAP_DISABLE_QUIRKS2 213
   /* #define KVM_CAP_VM_TSC_CONTROL 214 */
   #define KVM_CAP_SYSTEM_EVENT_DATA 215
+#define KVM_CAP_S390_ZPCI_OP 216
   #ifdef KVM_CAP_IRQ_ROUTING
Quoted 8 lines

include/uapi/linux/kvm.h struct kvm_stats_desc { · line 2073

Quoted 6 lines
include/uapi/linux/kvm.h +2 -0
@@ -2068,4 +2069,34 @@ struct kvm_stats_desc {
   /* Available with KVM_CAP_XSAVE2 */
   #define KVM_GET_XSAVE2          _IOR(KVMIO,  0xcf, struct kvm_xsave)
+/* Available with KVM_CAP_S390_ZPCI_OP */
+#define KVM_S390_ZPCI_OP      _IOW(KVMIO,  0xd0, struct 
 kvm_s390_zpci_op)

Please double-check whether this should be _IOWR instead (see above). >

As mentioned above, the copy_to_user() should be removed.

Quoted 31 lines
Quoted 18 lines
include/uapi/linux/kvm.h +18 -0
+struct kvm_s390_zpci_op {
+    /* in */
+    __u32 fh;        /* target device */
+    __u8  op;        /* operation to perform */
+    __u8  pad[3];
+    union {
+        /* for KVM_S390_ZPCIOP_REG_AEN */
+        struct {
+            __u64 ibv;    /* Guest addr of interrupt bit vector */
+            __u64 sb;    /* Guest addr of summary bit */
+            __u32 flags;
+            __u32 noi;    /* Number of interrupts */
+            __u8 isc;    /* Guest interrupt subclass */
+            __u8 sbo;    /* Offset of guest summary bit vector */
+            __u16 pad;
+        } reg_aen;
+        __u64 reserved[8];
+    } u;

include/uapi/linux/kvm.h struct kvm_stats_desc { · line 2102

Quoted 10 lines
include/uapi/linux/kvm.h +9 -0
+};
+
+/* types for kvm_s390_zpci_op->op */
+#define KVM_S390_ZPCIOP_REG_AEN        0
+#define KVM_S390_ZPCIOP_DEREG_AEN    1
+
+/* flags for kvm_s390_zpci_op->u.reg_aen.flags */
+#define KVM_S390_ZPCIOP_REGAEN_HOST    (1 << 0)
+
   #endif /* __LINUX_KVM_H */

 Thomas

#35 lore raw

Re: [PATCH v7 20/22] KVM: s390: add KVM_S390_ZPCI_OP to manage guest zPCI devices

Am 16.05.22 um 17:35 schrieb Matthew Rosato:

Quoted 35 lines · Thomas Huth
Quoted 18 lines · Matthew Rosato
Quoted 2 lines

The KVM_S390_ZPCI_OP ioctl provides a mechanism for managing hardware-assisted virtualization features for s390X zPCI passthrough.

s/s390X/s390x/

Quoted 12 lines

Add the first 2 operations, which can be used to enable/disable the specified device for Adapter Event Notification interpretation.

---
  Documentation/virt/kvm/api.rst | 45 +++++++++++++++++++ arch/s390/kvm/kvm-s390.c       | 23 ++++++++++ arch/s390/kvm/pci.c            | 81 ++++++++++++++++++++++++++++++++++ arch/s390/kvm/pci.h            |  2 + include/uapi/linux/kvm.h       | 31 +++++++++++++

  5 files changed, 182 insertions(+)
Quoted 15 lines
Quoted 5 lines
Documentation/virt/kvm/api.rst +0 -0
index 4a900cdbc62e..a7cd5ebce031 100644
@@ -5645,6 +5645,51 @@ enabled with ``arch_prctl()``, but this may change in the future.
Quoted 8 lines

  The offsets of the state save areas in struct kvm_xsave follow the contents of CPUID leaf 0xD on the host.
+4.135 KVM_S390_ZPCI_OP
+--------------------
+
+:Capability: KVM_CAP_S390_ZPCI_OP
+:Architectures: s390
+:Type: vcpu ioctl

vcpu? ... you're wiring it up in  kvm_arch_vm_ioctl() later, so I assume it's rather a VM ioctl?

Quoted 1 line

Yup, VM ioctl, bad copy/paste job...

Can you maybe resend just the 1 or 2 patches with feedback? In the end this series might be "old" and good enough to still be queued for the next merge window.
Would be good if Jason (Gunthorpe) would double check that his concerns are addressed and I will have a look at the patches without RB/ACK.

#36 lore raw

Re: [PATCH v7 20/22] KVM: s390: add KVM_S390_ZPCI_OP to manage guest zPCI devices

In reply to Matthew Rosato
Quoted 94 lines · Matthew Rosato
Quoted 18 lines · Thomas Huth
Quoted 2 lines · Matthew Rosato

The KVM_S390_ZPCI_OP ioctl provides a mechanism for managing hardware-assisted virtualization features for s390X zPCI passthrough.

s/s390X/s390x/

Quoted 12 lines

Add the first 2 operations, which can be used to enable/disable the specified device for Adapter Event Notification interpretation.

---
  Documentation/virt/kvm/api.rst | 45 +++++++++++++++++++ arch/s390/kvm/kvm-s390.c       | 23 ++++++++++ arch/s390/kvm/pci.c            | 81 ++++++++++++++++++++++++++++++++++ arch/s390/kvm/pci.h            |  2 + include/uapi/linux/kvm.h       | 31 +++++++++++++

  5 files changed, 182 insertions(+)
Quoted 16 lines
Quoted 6 lines
Documentation/virt/kvm/api.rst +0 -0
index 4a900cdbc62e..a7cd5ebce031 100644
@@ -5645,6 +5645,51 @@ enabled with ``arch_prctl()``, but this may change 

in the future.

Quoted 8 lines

  The offsets of the state save areas in struct kvm_xsave follow the contents of CPUID leaf 0xD on the host.
+4.135 KVM_S390_ZPCI_OP
+--------------------
+
+:Capability: KVM_CAP_S390_ZPCI_OP
+:Architectures: s390
+:Type: vcpu ioctl

vcpu? ... you're wiring it up in  kvm_arch_vm_ioctl() later, so I assume it's rather a VM ioctl?

Yup, VM ioctl, bad copy/paste job...

Quoted 13 lines

Documentation/virt/kvm/api.rst enabled with ``arch_prctl()``, but this may change in the future. · line 5662

Quoted 10 lines

+:Parameters: struct kvm_s390_zpci_op (in)
+:Returns: 0 on success, <0 on error

Documentation/virt/kvm/api.rst +7 -0
+
+Used to manage hardware-assisted virtualization features for zPCI devices.
+
+Parameters are specified via the following structure::
+
+  struct kvm_s390_zpci_op {
+    /* in */

If all is "in", why is there a copy_to_user() in the code later?

Oh no, this is a leftover from a prior version...  Good catch.  There should no longer be a copy_to_user.

Quoted 10 lines
Quoted 8 lines

+    __u32 fh;        /* target device */
+    __u8  op;        /* operation to perform */
+    __u8  pad[3];
+    union {
+        /* for KVM_S390_ZPCIOP_REG_AEN */
+        struct {
+            __u64 ibv;    /* Guest addr of interrupt bit vector */ +            __u64 sb;    /* Guest addr of summary bit */

If this is really a vcpu ioctl, what kind of addresses are you talking about here? virtual addresses? real addresses? absolute addresses?

It's a VM ioctl.  These are guest kernel physical addresses that are later pinned in arch/s390/kvm/pci.c:kvm_s390_pci_aif_enable() as part of handling the ioctl.

Quoted 18 lines
Quoted 18 lines

+            __u32 flags;
+            __u32 noi;    /* Number of interrupts */
+            __u8 isc;    /* Guest interrupt subclass */ +            __u8 sbo;    /* Offset of guest summary bit vector */ +            __u16 pad;
+        } reg_aen;
+        __u64 reserved[8];
+    } u;
+  };

Documentation/virt/kvm/api.rst +8 -0
+
+The type of operation is specified in the "op" field.
+KVM_S390_ZPCIOP_REG_AEN is used to register the VM for adapter event
+notification interpretation, which will allow firmware delivery of adapter
+events directly to the vm, with KVM providing a backup delivery mechanism;
+KVM_S390_ZPCIOP_DEREG_AEN is used to subsequently disable interpretation of
+adapter event notifications.
+
Quoted 8 lines

Documentation/virt/kvm/api.rst enabled with ``arch_prctl()``, but this may change in the future. · line 5692

Quoted 6 lines
Documentation/virt/kvm/api.rst +2 -0
+The target zPCI function must also be specified via the "fh" field. For the
+KVM_S390_ZPCIOP_REG_AEN operation, additional information to establish 

firmware

Documentation/virt/kvm/api.rst +3 -0
+delivery must be provided via the "reg_aen" struct.
+
+The "reserved" field is meant for future extensions.

Maybe also mention the "pad" fields? And add should these also be initialized to 0 by the calling userspace program?

Quoted 1 line

Sure, I can mention them.  And yes, I agree that userspace should initialize them to 0, I'll update the QEMU series accordingly.

I just spotted the corresponding patch in the QEMU series, and I think it should already be fine there, since you're using "= { ... }" while declaring the variables:

+int s390_pci_kvm_aif_disable(S390PCIBusDevice *pbdev) +{
+ struct kvm_s390_zpci_op args = {
+ .fh = pbdev->fh,
+ .op = KVM_S390_ZPCIOP_DEREG_AEN
+ };

That means unspecified fields will be set to 0 by the compiler already, as far as I know.

Thomas

author maintainer
#37 lore raw

PATCH v7 21/22 KVM: s390: introduce CPU feature for zPCI Interpretation

In reply to Matthew Rosato

KVM_S390_VM_CPU_FEAT_ZPCI_INTERP relays whether zPCI interpretive execution is possible based on the available hardware facilities.

---
 arch/s390/include/uapi/asm/kvm.h | 1 +
 arch/s390/kvm/kvm-s390.c         | 4 ++++
 2 files changed, 5 insertions(+)
arch/s390/include/uapi/asm/kvm.h +1 -0
index 7a6b14874d65..ed06458a871f 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_ZPCI_INTERP 14
 struct kvm_s390_vm_cpu_feat {
 	__u64 feat[16];
 };
arch/s390/kvm/kvm-s390.c +4 -0
index 1af7cea9d579..0fc2d8c5ccb1 100644
@@ -433,6 +433,10 @@ static void kvm_s390_cpu_feat_init(void)
 	if (test_facility(151)) /* DFLTCC */
 		__insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc);
 
+	/* zPCI Interpretation */
+	if (kvm_s390_pci_interp_allowed())
+		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ZPCI_INTERP);
+
 	if (MACHINE_HAS_ESOP)
 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
 	/*
Signature · 1 line
2.27.0
author maintainer
#38 lore raw

PATCH v7 22/22 MAINTAINERS: additional files related kvm s390 pci passthrough

In reply to Matthew Rosato

Add entries from the s390 kvm subdirectory related to pci passthrough.

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index 28f809560ac9..767f93172b77 100644
@@ -17266,6 +17266,7 @@ M:	Eric Farman <[email protected]>
 L:	[email protected]
 L:	[email protected]
 S:	Supported
+F:	arch/s390/kvm/pci*
 F:	drivers/vfio/pci/vfio_pci_zdev.c
 F:	include/uapi/linux/vfio_zdev.h
 
Signature · 1 line
2.27.0