Skip to contentSkip to conversation

EEH Support for VFIO PCI device

RFC PATCH Superseded Gavin Shan 5 participants 29 messages May 14, 2014 – May 20, 2014

Subsystems (5)

v3 of 19 Superseded Latest is v19 →

lore
Share & tools
lore

Gavin Shan

author
#0 lore raw

v3 cover EEH Support for VFIO PCI device

The series of patches intends to support EEH for PCI devices, which are passed through to PowerKVM based guest via VFIO. The implementation is straightforward based on the issues or problems we have to resolve to support EEH for PowerKVM based guest.

- Emulation for EEH RTAS requests. All EEH RTAS requests goes to QEMU firstly. If QEMU can't handle it, the request will be sent to host via newly introduced VFIO container IOCTL command (VFIO_EEH_INFO) and gets handled in host kernel.

- The error injection infrastructure need support request from the userland utility "errinjct" and PowerKVM based guest. The userland utility "errinjct" works on pSeries platform well with dedicated syscall, which helps invoking RTAS service to fulfil error injection in kernel. From the perspective, it's reasonable to extend the syscall to support PowerNV platform so that OPAL call can be invoked in host kernel for injecting errors. The data transported between userland and kerenl is still following "struct rtas_args" for both cases of PowerNV (OPAL) and pSeries (RTAS).

The series of patches requires corresponding firmware changes from Mike Qiu to support error injection and QEMU changes to support EEH for guest. QEMU patchset will be sent separately.

Change log
=====
v1 -> v2:
* EEH RTAS requests are routed to QEMU, and then possiblly to host kerenl. The mechanism KVM in-kernel handling is dropped.
* Error injection is reimplemented based syscall, instead of KVM in-kerenl handling. The logic for error injection token management is moved to QEMU. The error injection request is routed to QEMU and then possiblly to host kernel.
v2 -> v3:
* Make the fields in struct eeh_vfio_pci_addr, struct vfio_eeh_info based on the comments from Alexey.
* Define macros for EEH VFIO operations (Alexey).
* Clear frozen state after successful PE reset.
* Merge original [PATCH 1/2/3] to one.

Testing on P7
======
- Emulex adapter

Testing on P8
======
- Need more testing after design is finalized.

-----

Gavin Shan (8):
drivers/vfio: Introduce CONFIG_VFIO_EEH powerpc/eeh: Info to trace passed devices drivers/vfio: New IOCTL command VFIO_EEH_INFO powerpc/eeh: Avoid event on passed PE powerpc/powernv: Sync OPAL header file with firmware
powerpc: Extend syscall ppc_rtas() powerpc/powernv: Implement ppc_call_opal() powerpc/powernv: Error injection infrastructure

 arch/powerpc/include/asm/eeh.h                 |  52 +++
 arch/powerpc/include/asm/opal.h                |  74 ++-
 arch/powerpc/include/asm/rtas.h                |  10 +-
 arch/powerpc/include/asm/syscalls.h            |   2 +-
 arch/powerpc/include/asm/systbl.h              |   2 +-
 arch/powerpc/include/uapi/asm/unistd.h         |   2 +-
 arch/powerpc/kernel/eeh.c                      |   8 +
 arch/powerpc/kernel/eeh_pe.c                   |  80 ++++
 arch/powerpc/kernel/rtas.c                     |  57 +--
 arch/powerpc/kernel/syscalls.c                 |  50 +++
 arch/powerpc/platforms/powernv/Makefile        |   3 +-
 arch/powerpc/platforms/powernv/eeh-ioda.c      |   3 +-
 arch/powerpc/platforms/powernv/eeh-vfio.c      | 593 +++++++++++++++++++++++++
 arch/powerpc/platforms/powernv/errinject.c     | 224 ++++++++++
 arch/powerpc/platforms/powernv/opal-wrappers.S |   1 +
 arch/powerpc/platforms/powernv/opal.c          |  93 ++++
 drivers/vfio/Kconfig                           |   6 +
 drivers/vfio/vfio_iommu_spapr_tce.c            |  12 +
 include/uapi/linux/vfio.h                      |  57 +++
 kernel/sys_ni.c                                |   2 +-
 20 files changed, 1278 insertions(+), 53 deletions(-)
 create mode 100644 arch/powerpc/platforms/powernv/eeh-vfio.c
 create mode 100644 arch/powerpc/platforms/powernv/errinject.c

Thanks,
Gavin

author
#1 lore raw

PATCH v3 1/8 drivers/vfio: Introduce CONFIG_VFIO_EEH

The patch introduces CONFIG_VFIO_EEH for more IOCTL commands on tce_iommu_driver_ops to support EEH funtionality for PCI devices that are passed through from host to guest.

---
 drivers/vfio/Kconfig | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/vfio/Kconfig +6 -0
index af7b204..4f3293b 100644
@@ -8,11 +8,17 @@ config VFIO_IOMMU_SPAPR_TCE
 	depends on VFIO && SPAPR_TCE_IOMMU
 	default n
 
+config VFIO_EEH
+	tristate
+	depends on EEH && VFIO_IOMMU_SPAPR_TCE
+	default n
+
 menuconfig VFIO
 	tristate "VFIO Non-Privileged userspace driver framework"
 	depends on IOMMU_API
 	select VFIO_IOMMU_TYPE1 if X86
 	select VFIO_IOMMU_SPAPR_TCE if (PPC_POWERNV || PPC_PSERIES)
+	select VFIO_EEH if PPC_POWERNV
 	select ANON_INODES
 	help
 	  VFIO provides a framework for secure userspace device drivers.
Signature · 1 line
1.8.3.2
author
#2 lore raw

PATCH v3 2/8 powerpc/eeh: Info to trace passed devices

In reply to Gavin Shan

The address of passed PCI devices (domain:bus:slot:func) might be quite different from the perspective of host and guest. We have to trace the address mapping so that we can emulate EEH RTAS requests from guest. The patch introduces additional fields to eeh_pe and eeh_dev for the purpose.

Also, the patch adds function eeh_vfio_pe_get() and eeh_vfio_dev_get() to search EEH PE or device according to the given guest address. Both of them will be used by subsequent patches.

---
 arch/powerpc/include/asm/eeh.h | 52 +++++++++++++++++++++++++++
 arch/powerpc/kernel/eeh_pe.c   | 80 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 132 insertions(+)
arch/powerpc/include/asm/eeh.h +52 -0
index 7782056..96dabfc 100644
@@ -48,6 +48,14 @@ struct device_node;
 #define EEH_PE_RST_HOLD_TIME		250
 #define EEH_PE_RST_SETTLE_TIME		1800
 
+#ifdef CONFIG_VFIO_EEH
+struct eeh_vfio_pci_addr {
+	uint64_t	buid;		/* PHB BUID			*/
+	uint16_t	config_addr;	/* Bus/Device/Function number	*/
+	uint32_t	pe_addr;	/* PE configuration address	*/
+};
+#endif /* CONFIG_VFIO_EEH */
+
 /*
  * The struct is used to trace PE related EEH functionality.
  * In theory, there will have one instance of the struct to
@@ -72,6 +80,7 @@ struct device_node;
 #define EEH_PE_RESET		(1 << 2)	/* PE reset in progress	*/
 
 #define EEH_PE_KEEP		(1 << 8)	/* Keep PE on hotplug	*/
+#define EEH_PE_PASSTHROUGH	(1 << 9)	/* PE owned by guest	*/
 
 struct eeh_pe {
 	int type;			/* PE type: PHB/Bus/Device	*/
@@ -85,6 +94,9 @@ struct eeh_pe {
 	struct timeval tstamp;		/* Time on first-time freeze	*/
 	int false_positives;		/* Times of reported #ff's	*/
 	struct eeh_pe *parent;		/* Parent PE			*/
+#ifdef CONFIG_VFIO_EEH
+	struct eeh_vfio_pci_addr guest_addr;
+#endif
 	struct list_head child_list;	/* Link PE to the child list	*/
 	struct list_head edevs;		/* Link list of EEH devices	*/
 	struct list_head child;		/* Child PEs			*/
@@ -93,6 +105,21 @@ struct eeh_pe {
 #define eeh_pe_for_each_dev(pe, edev, tmp) \
 		list_for_each_entry_safe(edev, tmp, &pe->edevs, list)
 
+static inline bool eeh_pe_passed(struct eeh_pe *pe)
+{
+	return pe ? !!(pe->state & EEH_PE_PASSTHROUGH) : false;
+}
+
+static inline void eeh_pe_set_passed(struct eeh_pe *pe, bool passed)
+{
+	if (pe) {
+		if (passed)
+			pe->state |= EEH_PE_PASSTHROUGH;
+		else
+			pe->state &= ~EEH_PE_PASSTHROUGH;
+	}
+}
+
 /*
  * The struct is used to trace EEH state for the associated
  * PCI device node or PCI device. In future, it might
@@ -110,6 +137,7 @@ struct eeh_pe {
 #define EEH_DEV_SYSFS		(1 << 9)	/* Sysfs created	*/
 #define EEH_DEV_REMOVED		(1 << 10)	/* Removed permanently	*/
 #define EEH_DEV_FRESET		(1 << 11)	/* Fundamental reset	*/
+#define EEH_DEV_PASSTHROUGH	(1 << 12)	/* Owned by guest	*/
 
 struct eeh_dev {
 	int mode;			/* EEH mode			*/
@@ -126,6 +154,9 @@ struct eeh_dev {
 	struct device_node *dn;		/* Associated device node	*/
 	struct pci_dev *pdev;		/* Associated PCI device	*/
 	struct pci_bus *bus;		/* PCI bus for partial hotplug	*/
+#ifdef CONFIG_VFIO_EEH
+	struct eeh_vfio_pci_addr guest_addr;
+#endif
 };
 
 static inline struct device_node *eeh_dev_to_of_node(struct eeh_dev *edev)
@@ -138,6 +169,21 @@ static inline struct pci_dev *eeh_dev_to_pci_dev(struct eeh_dev *edev)
 	return edev ? edev->pdev : NULL;
 }
 
+static inline bool eeh_dev_passed(struct eeh_dev *dev)
+{
+	return dev ? !!(dev->mode & EEH_DEV_PASSTHROUGH) : false;
+}
+
+static inline void eeh_dev_set_passed(struct eeh_dev *dev, bool passed)
+{
+	if (dev) {
+		if (passed)
+			dev->mode |= EEH_DEV_PASSTHROUGH;
+		else
+			dev->mode &= ~EEH_DEV_PASSTHROUGH;
+	}
+}
+
 /* Return values from eeh_ops::next_error */
 enum {
 	EEH_NEXT_ERR_NONE = 0,
@@ -335,6 +381,12 @@ static inline void eeh_remove_device(struct pci_dev *dev) { }
 #define EEH_IO_ERROR_VALUE(size) (-1UL)
 #endif /* CONFIG_EEH */
 
+
+#ifdef CONFIG_VFIO_EEH
+struct eeh_dev *eeh_vfio_dev_get(struct eeh_vfio_pci_addr *addr);
+struct eeh_pe *eeh_vfio_pe_get(struct eeh_vfio_pci_addr *addr);
+#endif /* CONFIG_VFIO_EEH */
+
 #ifdef CONFIG_PPC64
 /*
  * MMIO read/write operations with EEH support.
arch/powerpc/kernel/eeh_pe.c +80 -0

86 lines not on this page · raw message

Signature · 1 line
1.8.3.2
#3 lore raw

Re: [PATCH 2/8] powerpc/eeh: Info to trace passed devices

Quoted 11 lines · Gavin Shan

The address of passed PCI devices (domain:bus:slot:func) might be quite different from the perspective of host and guest. We have to trace the address mapping so that we can emulate EEH RTAS requests from guest. The patch introduces additional fields to eeh_pe and eeh_dev for the purpose.

Also, the patch adds function eeh_vfio_pe_get() and eeh_vfio_dev_get() to search EEH PE or device according to the given guest address. Both of them will be used by subsequent patches.

I don't see the point of VFIO knowing about guest addresses. They are not unique across a system and the whole idea that a VFIO device has to be owned by a guest is also pretty dubious.

I suppose what you really care about here is just a token for a specific device? But why do you need one where we don't have tokens yet?

Alex

#4 lore raw

Re: [PATCH 2/8] powerpc/eeh: Info to trace passed devices

Quoted 6 lines · Alexander Graf

I don't see the point of VFIO knowing about guest addresses. They are not unique across a system and the whole idea that a VFIO device has to be owned by a guest is also pretty dubious.

I suppose what you really care about here is just a token for a specific device? But why do you need one where we don't have tokens yet?

I think this is going to be needed when doing in-kernel acceleration of some of the RTAS calls, but yes, Gavin, why do we need that now ?

Cheers,
Ben.

author
#5 lore raw

Re: [PATCH 2/8] powerpc/eeh: Info to trace passed devices

Quoted 11 lines · Benjamin Herrenschmidt
Quoted 6 lines · Alexander Graf

I don't see the point of VFIO knowing about guest addresses. They are not unique across a system and the whole idea that a VFIO device has to be owned by a guest is also pretty dubious.

I suppose what you really care about here is just a token for a specific device? But why do you need one where we don't have tokens yet?

I think this is going to be needed when doing in-kernel acceleration of some of the RTAS calls, but yes, Gavin, why do we need that now ?

Thanks for your comments :-)

For now, the newly introduced IOCTL command (VFIO_EEH_INFO) is handled by VFIO-Container fd, which can't identify IOMMU group (PE) or VFIO-PCI-dev naturally because one VFIO-container can have multiple IOMMU groups docked there. I'm going to let VFIO-PCI-dev fd handle the IOCTL command because VFIO-PCI-dev fd can identify the device and the corresponding IOMMU group (PE). With that, the address mapping stuff isn't necessary.

Thanks,
Gavin

author
#6 lore raw

PATCH v3 3/8 drivers/vfio: New IOCTL command VFIO_EEH_INFO

In reply to Gavin Shan

The patch adds new IOCTL command VFIO_EEH_INFO to VFIO container to support EEH functionality for PCI devices, which have been passed from host to guest via VFIO.

---
 arch/powerpc/platforms/powernv/Makefile   |   1 +
 arch/powerpc/platforms/powernv/eeh-vfio.c | 593 ++++++++++++++++++++++++++++++
 drivers/vfio/vfio_iommu_spapr_tce.c       |  12 +
 include/uapi/linux/vfio.h                 |  57 +++
 4 files changed, 663 insertions(+)
 create mode 100644 arch/powerpc/platforms/powernv/eeh-vfio.c
arch/powerpc/platforms/powernv/Makefile +1 -0
index 63cebb9..2b15a03 100644
@@ -6,5 +6,6 @@ obj-y			+= opal-msglog.o
 obj-$(CONFIG_SMP)	+= smp.o
 obj-$(CONFIG_PCI)	+= pci.o pci-p5ioc2.o pci-ioda.o
 obj-$(CONFIG_EEH)	+= eeh-ioda.o eeh-powernv.o
+obj-$(CONFIG_VFIO_EEH)	+= eeh-vfio.o
 obj-$(CONFIG_PPC_SCOM)	+= opal-xscom.o
 obj-$(CONFIG_MEMORY_FAILURE)	+= opal-memory-errors.o
arch/powerpc/platforms/powernv/eeh-vfio.c new file +593 -0

593 lines not on this page · raw message

drivers/vfio/vfio_iommu_spapr_tce.c +12 -0
index a84788b..c45dece 100644
@@ -26,6 +26,11 @@
 #define DRIVER_AUTHOR   "[email protected]"
 #define DRIVER_DESC     "VFIO IOMMU SPAPR TCE"
 
+#ifdef CONFIG_VFIO_EEH
+extern void eeh_vfio_release(struct iommu_table *tbl);
+extern int eeh_vfio_ioctl(unsigned long arg);
+#endif
+
 static void tce_iommu_detach_group(void *iommu_data,
 		struct iommu_group *iommu_group);
 
@@ -283,6 +288,10 @@ static long tce_iommu_ioctl(void *iommu_data,
 		tce_iommu_disable(container);
 		mutex_unlock(&container->lock);
 		return 0;
+#ifdef CONFIG_VFIO_EEH
+	case VFIO_EEH_INFO:
+		return eeh_vfio_ioctl(arg);
+#endif
 	}
 
 	return -ENOTTY;
@@ -342,6 +351,9 @@ static void tce_iommu_detach_group(void *iommu_data,
 		/* pr_debug("tce_vfio: detaching group #%u from iommu %p\n",
 				iommu_group_id(iommu_group), iommu_group); */
 		container->tbl = NULL;
+#ifdef CONFIG_VFIO_EEH
+		eeh_vfio_release(tbl);
+#endif
 		iommu_release_ownership(tbl);
 	}
 	mutex_unlock(&container->lock);
include/uapi/linux/vfio.h +57 -0

63 lines not on this page · raw message

Signature · 1 line
1.8.3.2
#7 lore raw

Re: [PATCH 3/8] drivers/vfio: New IOCTL command VFIO_EEH_INFO

Quoted 10 lines · Gavin Shan

The patch adds new IOCTL command VFIO_EEH_INFO to VFIO container to support EEH functionality for PCI devices, which have been passed from host to guest via VFIO.

---
  arch/powerpc/platforms/powernv/Makefile   |   1 +
  arch/powerpc/platforms/powernv/eeh-vfio.c | 593 ++++++++++++++++++++++++++++++
  drivers/vfio/vfio_iommu_spapr_tce.c       |  12 +
  include/uapi/linux/vfio.h                 |  57 +++

Documentation/...?

Alex

author
#8 lore raw

Re: [PATCH 3/8] drivers/vfio: New IOCTL command VFIO_EEH_INFO

Quoted 6 lines · Alexander Graf
Quoted 4 lines · Gavin Shan

The patch adds new IOCTL command VFIO_EEH_INFO to VFIO container to support EEH functionality for PCI devices, which have been passed from host to guest via VFIO.

Quoted 9 lines
Quoted 6 lines
---
 arch/powerpc/platforms/powernv/Makefile   |   1 +
 arch/powerpc/platforms/powernv/eeh-vfio.c | 593 ++++++++++++++++++++++++++++++
 drivers/vfio/vfio_iommu_spapr_tce.c       |  12 +
 include/uapi/linux/vfio.h                 |  57 +++

Documentation/...?

Will amend it in next revision. Thanks for reminder.

Thanks,
Gavin

#9 lore raw

Re: [PATCH 3/8] drivers/vfio: New IOCTL command VFIO_EEH_INFO

In reply to Gavin Shan
Quoted 3 lines · Gavin Shan

The patch adds new IOCTL command VFIO_EEH_INFO to VFIO container to support EEH functionality for PCI devices, which have been passed from host to guest via VFIO.

Some comments throughout, but overall this seems to forgo every bit of the device ownership and protection model used by VFIO and lets the user pick arbitrary host devices and do various operations, mostly unchecked. That's not acceptable.

Quoted 65 lines
---
 arch/powerpc/platforms/powernv/Makefile   |   1 +
 arch/powerpc/platforms/powernv/eeh-vfio.c | 593 ++++++++++++++++++++++++++++++
 drivers/vfio/vfio_iommu_spapr_tce.c       |  12 +
 include/uapi/linux/vfio.h                 |  57 +++
 4 files changed, 663 insertions(+)
 create mode 100644 arch/powerpc/platforms/powernv/eeh-vfio.c
arch/powerpc/platforms/powernv/Makefile +1 -0
index 63cebb9..2b15a03 100644
@@ -6,5 +6,6 @@ obj-y			+= opal-msglog.o
 obj-$(CONFIG_SMP)	+= smp.o
 obj-$(CONFIG_PCI)	+= pci.o pci-p5ioc2.o pci-ioda.o
 obj-$(CONFIG_EEH)	+= eeh-ioda.o eeh-powernv.o
+obj-$(CONFIG_VFIO_EEH)	+= eeh-vfio.o
 obj-$(CONFIG_PPC_SCOM)	+= opal-xscom.o
 obj-$(CONFIG_MEMORY_FAILURE)	+= opal-memory-errors.o
arch/powerpc/platforms/powernv/eeh-vfio.c new file +39 -0
new file mode 100644
index 0000000..69d5f2d
@@ -0,0 +1,593 @@
+/*
+  * The file intends to support EEH funtionality for those PCI devices,
+  * which have been passed through from host to guest via VFIO. So this
+  * file is naturally part of VFIO implementation on PowerNV platform.
+  *
+  * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2014.
+  *
+  * This program is free software; you can redistribute it and/or modify
+  * it under the terms of the GNU General Public License as published by
+  * the Free Software Foundation; either version 2 of the License, or
+  * (at your option) any later version.
+  */
+
+#include <linux/init.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/kernel.h>
+#include <linux/kvm_host.h>
+#include <linux/msi.h>
+#include <linux/pci.h>
+#include <linux/string.h>
+#include <linux/vfio.h>
+
+#include <asm/eeh.h>
+#include <asm/eeh_event.h>
+#include <asm/io.h>
+#include <asm/iommu.h>
+#include <asm/opal.h>
+#include <asm/msi_bitmap.h>
+#include <asm/pci-bridge.h>
+#include <asm/ppc-pci.h>
+#include <asm/tce.h>
+#include <asm/uaccess.h>
+
+#include "powernv.h"
+#include "pci.h"
+
+static int powernv_eeh_vfio_map(struct vfio_eeh_info *info)
+{

arch/powerpc/platforms/powernv/eeh-vfio.c line 49

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +10 -0
+	struct pci_bus *bus, *pe_bus;
+	struct pci_dev *pdev;
+	struct eeh_dev *edev;
+	struct eeh_pe *pe;
+	int domain, bus_no, devfn;
+
+	/* Host address */
+	domain = info->map.host_domain;
+	bus_no = (info->map.host_cfg_addr >> 8) & 0xff;
+	devfn = info->map.host_cfg_addr & 0xff;

Where are we validating that the user has any legitimate claim to be touching this device?

Quoted 21 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +21 -0
+	/* Find PCI bus */
+	bus = pci_find_bus(domain, bus_no);
+	if (!bus) {
+		pr_warn("%s: PCI bus %04x:%02x not found\n",
+			__func__, domain, bus_no);
+		return -ENODEV;
+	}
+
+	/* Find PCI device */
+	pdev = pci_get_slot(bus, devfn);
+	if (!pdev) {
+		pr_warn("%s: PCI device %04x:%02x:%02x.%01x not found\n",
+			__func__, domain, bus_no,
+			PCI_SLOT(devfn), PCI_FUNC(devfn));
+		return -ENODEV;
+	}
+
+	/* No EEH device - almost impossible */
+	edev = pci_dev_to_eeh_dev(pdev);
+	if (unlikely(!edev)) {
+		pci_dev_put(pdev);

arch/powerpc/platforms/powernv/eeh-vfio.c line 81

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +10 -0
+		pr_warn("%s: No EEH dev for PCI device %s\n",
+			__func__, pci_name(pdev));
+		return -ENODEV;
+	}
+
+	/* Doesn't support PE migration between different PHBs */
+	pe = edev->pe;
+	if (!eeh_pe_passed(pe)) {
+		pe_bus = eeh_pe_bus_get(pe);
+		BUG_ON(!pe_bus);

Can a user trigger this maliciously?

Quoted 15 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +15 -0
+
+		/* PE# has format 00BBSS00 */
+		pe->guest_addr.buid    = info->map.guest_buid;
+		pe->guest_addr.pe_addr = pe_bus->number << 16;
+		eeh_pe_set_passed(pe, true);
+	} else if (pe->guest_addr.buid != info->map.guest_buid) {
+		pci_dev_put(pdev);
+		pr_warn("%s: Mismatched PHB BUID (0x%llx, 0x%llx)\n",
+			__func__, pe->guest_addr.buid, info->map.guest_buid);
+		return -EINVAL;
+	}
+
+	edev->guest_addr.buid = info->map.guest_buid;
+	edev->guest_addr.config_addr = info->map.guest_cfg_addr;
+	eeh_dev_set_passed(edev, true);

arch/powerpc/platforms/powernv/eeh-vfio.c line 106

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +10 -0
+
+	pr_debug("EEH: Host PCI dev %s to %llx-%02x:%02x.%01x\n",
+		 pci_name(pdev), info->map.guest_buid,
+		 (info->map.guest_cfg_addr >> 8) & 0xFF,
+		 PCI_SLOT(info->map.guest_cfg_addr & 0xFF),
+		 PCI_FUNC(info->map.guest_cfg_addr & 0xFF));
+
+	pci_dev_put(pdev);
+	return 0;
+}

So the effect of this function is that a user gets to setup an arbitrary guest mapping for an arbitrary host device and associated pe. Is that right? It seems bad.

arch/powerpc/platforms/powernv/eeh-vfio.c line 119

Quoted 13 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +13 -0
+
+static int powernv_eeh_vfio_unmap(struct vfio_eeh_info *info)
+{
+	struct eeh_vfio_pci_addr addr;
+	struct pci_dev *pdev;
+	struct eeh_dev *edev, *tmp;
+	struct eeh_pe *pe;
+	bool passed;
+
+	/* Get EEH device */
+	addr.buid = info->unmap.buid;
+	addr.config_addr = info->unmap.cfg_addr;
+	edev = eeh_vfio_dev_get(&addr);

eeh_vfio_dev_get() just looks for a "passed" dev and a match for a well known address space. Seems very exploitable.

Quoted 44 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +44 -0
+	if (!edev) {
+		pr_warn("%s: Cannot find %llx:%02x:%02x.%01x\n",
+			__func__, info->unmap.buid,
+			(info->unmap.cfg_addr >> 8) & 0xFF,
+			PCI_SLOT(info->unmap.cfg_addr & 0xFF),
+			PCI_FUNC(info->unmap.cfg_addr & 0xFF));
+		return -ENODEV;
+	}
+
+	/* Return EEH device */
+	memset(&edev->guest_addr, 0, sizeof(edev->guest_addr));
+	eeh_dev_set_passed(edev, false);
+	pdev = eeh_dev_to_pci_dev(edev);
+	pr_debug("EEH: Host PCI dev %s returned\n",
+		 pdev ? pci_name(pdev) : "NULL");
+
+	/* Return PE if no EEH device is owned by guest */
+	pe = edev->pe;
+	passed = false;
+	eeh_pe_for_each_dev(pe, edev, tmp) {
+		pdev = eeh_dev_to_pci_dev(edev);
+		if (pdev && pdev->subordinate)
+			continue;
+
+		if (eeh_dev_passed(edev)) {
+			passed = true;
+			break;
+		}
+	}
+
+	if (!passed) {
+		memset(&pe->guest_addr, 0, sizeof(pe->guest_addr));
+		eeh_pe_set_passed(pe, false);
+		pr_debug("EEH: PHB#%x-PE#%x returned to host\n",
+			 pe->phb->global_number, pe->addr);
+	}
+
+	return 0;
+}
+
+static int powernv_eeh_vfio_set_option(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_dev *edev;

arch/powerpc/platforms/powernv/eeh-vfio.c line 173

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +10 -0
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int opcode = info->option.option;
+	int ret = 0;
+
+	/* Check opcode */
+	if (opcode < EEH_OPT_DISABLE || opcode > EEH_OPT_THAW_DMA) {
+		pr_warn("%s: opcode %d out of range (%d, %d)\n",
+			__func__, opcode, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);
+		ret = 3;

Please don't make up arbitrary return values.

Quoted 196 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +196 -0
+		goto out;
+	}
+
+	/* Option "enable" uses PCI config address */
+	if (opcode = EEH_OPT_ENABLE) {
+		addr.buid = info->option.buid;
+		addr.config_addr = (info->option.addr >> 8) & 0xFFFF;
+		edev = eeh_vfio_dev_get(&addr);
+		if (!edev) {
+			pr_warn("%s: Cannot find %llx:%02x:%02x.%01x\n",
+				__func__, addr.buid,
+				(addr.config_addr >> 8) & 0xFF,
+				PCI_SLOT(addr.config_addr & 0xFF),
+				PCI_FUNC(addr.config_addr & 0xFF));
+			ret = 7;
+			goto out;
+		}
+		phb = edev->phb->private_data;
+	} else {
+		addr.buid    = info->option.buid;
+		addr.pe_addr = info->option.addr;
+		pe = eeh_vfio_pe_get(&addr);
+		if (!pe) {
+			pr_warn("%s: Cannot find PE %llx:%x\n",
+				__func__, addr.buid, addr.pe_addr);
+			ret = 7;
+			goto out;
+		}
+		phb = pe->phb->private_data;
+	}
+
+	/* Insure that the EEH stuff has been initialized */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 7;
+		goto out;
+	}
+
+	/*
+	 * The EEH functionality has been enabled on all PEs
+	 * by default. So just return success. The same situation
+	 * would be applied while we disable EEH functionality.
+	 * However, the guest isn't expected to disable that
+	 * at all.
+	 */
+	if (opcode = EEH_OPT_DISABLE ||
+	    opcode = EEH_OPT_ENABLE) {
+		ret = 0;
+		goto out;
+	}
+
+	/*
+	 * Call into the IODA dependent backend in order
+	 * to enable DMA or MMIO for the indicated PE.
+	 */
+	if (phb->eeh_ops && phb->eeh_ops->set_option) {
+		if (phb->eeh_ops->set_option(pe, opcode)) {
+			pr_warn("%s: Failure from backend\n",
+				__func__);
+			ret = 1;
+		}
+	} else {
+		pr_warn("%s: Unsupported request\n",
+			__func__);
+		ret = 7;
+	}
+
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_get_addr(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_dev *edev;
+	struct eeh_vfio_pci_addr addr;
+	int opcode = info->addr.option;
+	int ret = 0;
+
+	/* Check opcode */
+	if (opcode != 0 && opcode != 1) {
+		pr_warn("%s: opcode %d out of range (0, 1)\n",
+			__func__, opcode);
+		ret = 3;
+		goto out;
+	}
+
+	/* Find EEH device */
+	addr.buid = info->addr.buid;
+	addr.config_addr = (info->addr.cfg_addr >> 8 ) & 0xFFFF;
+	edev = eeh_vfio_dev_get(&addr);
+	if (!edev) {
+		pr_warn("%s: Cannot find %llx:%02x:%02x.%01x\n",
+			__func__, addr.buid,
+			(addr.config_addr >> 8) & 0xFF,
+			PCI_SLOT(addr.config_addr & 0xFF),
+			PCI_FUNC(addr.config_addr & 0xFF));
+		ret = 7;
+		goto out;
+	}
+	phb = edev->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+	}
+
+	/* EEH device passed ? */
+	if (!eeh_dev_passed(edev)) {
+		pr_warn("%s: EEH dev %llx:%02x:%02x.%01x owned by host\n",
+			__func__, addr.buid,
+			(addr.config_addr >> 8) & 0xFF,
+			PCI_SLOT(addr.config_addr & 0xFF),
+			PCI_FUNC(addr.config_addr & 0xFF));
+		ret = 3;
+		goto out;
+	}
+
+	/*
+	 * Fill result according to opcode. We don't differentiate
+	 * PCI bus and device sensitive PE here.
+	 */
+	if (opcode = 0)
+		info->addr.ret = edev->pe->guest_addr.pe_addr;
+	else
+		info->addr.ret = 1;
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_get_state(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int result, ret = 0;
+
+	/* Locate the PE */
+	addr.buid    = info->state.buid;
+	addr.pe_addr = info->state.pe_addr;
+	pe = eeh_vfio_pe_get(&addr);
+	if (!pe) {
+		pr_warn("%s: Cannot locate %llx:%x\n",
+			__func__, addr.buid, addr.pe_addr);
+		ret = 3;
+		goto out;
+	}
+	phb = pe->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+	}
+
+	/* Call to the IOC dependent function */
+	if (phb->eeh_ops && phb->eeh_ops->get_state) {
+		result = phb->eeh_ops->get_state(pe);
+
+		if (!(result & EEH_STATE_RESET_ACTIVE) &&
+		     (result & EEH_STATE_DMA_ENABLED) &&
+		     (result & EEH_STATE_MMIO_ENABLED))
+			info->state.state = 0;
+		else if (result & EEH_STATE_RESET_ACTIVE)
+			info->state.state = 1;
+		else if (!(result & EEH_STATE_RESET_ACTIVE) &&
+			 !(result & EEH_STATE_DMA_ENABLED) &&
+			 !(result & EEH_STATE_MMIO_ENABLED))
+			info->state.state = 2;
+		else if (!(result & EEH_STATE_RESET_ACTIVE) &&
+			 (result & EEH_STATE_DMA_ENABLED) &&
+			 !(result & EEH_STATE_MMIO_ENABLED))
+			info->state.state = 4;
+		else
+			info->state.state = 5;
+
+		ret = 0;
+	} else {
+		pr_warn("%s: Unsupported request\n", __func__);
+		ret = 3;
+	}
+
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_pe_reset(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_pe *pe;

arch/powerpc/platforms/powernv/eeh-vfio.c line 379

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +10 -0
+	struct eeh_vfio_pci_addr addr;
+	int opcode = info->reset.option;
+	int ret = 0;
+
+	/* Check opcode */
+	if (opcode != EEH_RESET_DEACTIVATE &&
+	    opcode != EEH_RESET_HOT &&
+	    opcode != EEH_RESET_FUNDAMENTAL) {
+		pr_warn("%s: Unsupported opcode %d\n",
+			__func__, opcode);

Console warnings are exploitable DoS attacks.

Quoted 139 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +139 -0
+		ret = 3;
+		goto out;
+	}
+
+	/* Locate the PE */
+	addr.buid    = info->reset.buid;
+	addr.pe_addr = info->reset.pe_addr;
+	pe = eeh_vfio_pe_get(&addr);
+	if (!pe) {
+		pr_warn("%s: Cannot locate %llx:%x\n",
+			__func__, addr.buid, addr.pe_addr);
+		ret = 3;
+		goto out;
+	}
+	phb = pe->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+	}
+
+	/* Call into the IODA dependent backend to do the reset */
+	if (!phb->eeh_ops ||
+	    !phb->eeh_ops->set_option ||
+	    !phb->eeh_ops->reset) {
+		pr_warn("%s: Unsupported request\n",
+			__func__);
+		ret = 7;
+	} else {
+		/*
+		 * The frozen PE might be caused by the mechanism called
+		 * PAPR error injection, which is supposed to be one-shot
+		 * without "sticky" bit as being stated by the spec. But
+		 * the reality isn't that, at least on P7IOC. So we have
+		 * to clear that to avoid recrusive error, which fails the
+		 * recovery eventually.
+		 */
+		if (opcode = EEH_RESET_DEACTIVATE)
+			opal_pci_reset(phb->opal_id,
+				       OPAL_PHB_ERROR,
+				       OPAL_ASSERT_RESET);
+
+		if (phb->eeh_ops->reset(pe, opcode)) {
+			pr_warn("%s: Failure from backend\n", __func__);
+			ret = 1;
+			goto out;
+		}
+
+		/*
+		 * The PE is still in frozen state and we need clear that.
+		 * It's good to clear frozen state after deassert to avoid
+		 * messy IO access during reset, which might cause recrusive
+		 * frozen PE.
+		 */
+		if (opcode = EEH_RESET_DEACTIVATE) {
+			if (phb->eeh_ops->set_option(pe, EEH_OPT_THAW_MMIO) ||
+			    phb->eeh_ops->set_option(pe, EEH_OPT_THAW_DMA)) {
+				pr_warn("%s: Cannot clear frozen state\n",
+					__func__);
+				ret = 1;
+			}
+
+			eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
+		}
+	}
+
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_pe_config(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int ret = 0;
+
+	/* Locate the PE */
+	addr.buid    = info->config.buid;
+	addr.pe_addr = info->config.pe_addr;
+	pe = eeh_vfio_pe_get(&addr);
+	if (!pe) {
+		pr_warn("%s: Cannot locate %llx:%x\n",
+			__func__, addr.buid, addr.pe_addr);
+		ret = 3;
+		goto out;
+	}
+	phb = pe->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+        }
+
+	/*
+	 * The access to PCI config space on VFIO device has some
+	 * limitations. Part of PCI config space, including BAR
+	 * registers are not readable and writable. So the guest
+	 * should have stale values for those registers and we have
+	 * to restore them in host side.
+	 */
+	eeh_pe_restore_bars(pe);
+out:
+	return ret;
+}
+
+void eeh_vfio_release(struct iommu_table *tbl)
+{
+	struct pnv_ioda_pe *pnv_pe = container_of(tbl, struct pnv_ioda_pe,
+						  tce32_table);
+	struct pnv_phb *phb = pnv_pe->phb;
+	struct eeh_pe *phb_pe, *pe;
+	struct eeh_dev dev, *edev, *tmp;
+
+	/* Find PHB PE */
+	phb_pe = eeh_phb_pe_get(phb->hose);
+	if (unlikely(!phb_pe)) {
+		pr_warn("%s: Cannot find PHB#%d PE\n",
+			__func__, phb->hose->global_number);
+		return;
+	}
+
+	/* Find PE */
+	memset(&dev, 0, sizeof(struct eeh_dev));
+	dev.phb = phb->hose;
+	dev.pe_config_addr = pnv_pe->pe_number;
+	pe = eeh_pe_get(&dev);
+	if (unlikely(!pe)) {
+		pr_warn("%s: Cannot find PE instance for PHB#%d-PE#%d\n",
+			__func__, phb->hose->global_number,
+			pnv_pe->pe_number);
+		return;
+	}

arch/powerpc/platforms/powernv/eeh-vfio.c line 528

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +10 -0
+
+	/* Release it to host */
+	if (!eeh_pe_passed(pe))
+		return;
+
+	eeh_pe_for_each_dev(pe, edev, tmp) {
+		if (!eeh_dev_passed(edev))
+			continue;
+
+		memset(&edev->guest_addr, 0, sizeof(edev->guest_addr));

Is guest_addr = { 0 } not valid? As agraf already mentioned, there are a number of issues with using a guest_address for a token.

Quoted 12 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +12 -0
+		eeh_dev_set_passed(edev, false);
+	}
+
+	memset(&pe->guest_addr, 0, sizeof(pe->guest_addr));
+	eeh_pe_set_passed(pe, false);
+}
+EXPORT_SYMBOL(eeh_vfio_release);
+
+int eeh_vfio_ioctl(unsigned long arg)
+{
+	struct vfio_eeh_info info;
+	int ret = -EINVAL;

arch/powerpc/platforms/powernv/eeh-vfio.c line 550

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +10 -0
+
+	/* Copy over user argument */
+	if (copy_from_user(&info, (void __user *)arg, sizeof(info))) {
+		pr_warn("%s: Cannot copy user argument 0x%lx\n",
+			__func__, arg);
+		return -EFAULT;
+	}
+
+	/* Sanity check */
+	if (info.argsz != sizeof(info)) {

This breaks compatibility if you need to later add a new ops with a larger footprint.

Quoted 60 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +43 -0
+		pr_warn("%s: Invalid argument size (%d, %ld)\n",
+			__func__, info.argsz, sizeof(info));
+		return -EINVAL;
+	}
+
+	/* Route according to operation */
+	switch (info.op) {
+	case VFIO_EEH_OP_MAP:
+		ret = powernv_eeh_vfio_map(&info);
+		break;
+	case VFIO_EEH_OP_UNMAP:
+		ret = powernv_eeh_vfio_unmap(&info);
+		break;
+	case VFIO_EEH_OP_SET_OPTION:
+		ret = powernv_eeh_vfio_set_option(&info);
+		break;
+	case VFIO_EEH_OP_GET_ADDR:
+		ret = powernv_eeh_vfio_get_addr(&info);
+		break;
+	case VFIO_EEH_OP_GET_STATE:
+		ret = powernv_eeh_vfio_get_state(&info);
+		break;
+	case VFIO_EEH_OP_PE_RESET:
+		ret = powernv_eeh_vfio_pe_reset(&info);
+		break;
+	case VFIO_EEH_OP_PE_CONFIG:
+		ret = powernv_eeh_vfio_pe_config(&info);
+		break;
+	default:
+		pr_info("%s: Cannot handle op#%d\n",
+			__func__, info.op);
+	}
+
+	/* Copy data back */
+	if (!ret && copy_to_user((void __user *)arg, &info, sizeof(info))) {
+		pr_warn("%s: Cannot copy to user 0x%lx\n",
+			__func__, arg);
+		return -EFAULT;
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(eeh_vfio_ioctl);
drivers/vfio/vfio_iommu_spapr_tce.c +5 -0
index a84788b..c45dece 100644
@@ -26,6 +26,11 @@
 #define DRIVER_AUTHOR   "[email protected]"
 #define DRIVER_DESC     "VFIO IOMMU SPAPR TCE"
 
+#ifdef CONFIG_VFIO_EEH
+extern void eeh_vfio_release(struct iommu_table *tbl);
+extern int eeh_vfio_ioctl(unsigned long arg);
+#endif
+
 static void tce_iommu_detach_group(void *iommu_data,
 		struct iommu_group *iommu_group);
 

drivers/vfio/vfio_iommu_spapr_tce.c static long tce_iommu_ioctl(void *iommu_data, · line 291

Quoted 4 lines
drivers/vfio/vfio_iommu_spapr_tce.c +1 -0
@@ -283,6 +288,10 @@ static long tce_iommu_ioctl(void *iommu_data,
 		tce_iommu_disable(container);
 		mutex_unlock(&container->lock);
 		return 0;
+#ifdef CONFIG_VFIO_EEH

I'm not a fan of all these #ifdefs, hide it in eeh_vfio_ioctl() and eeh_vfio_release() if needed.

Quoted 25 lines
drivers/vfio/vfio_iommu_spapr_tce.c +6 -0
+	case VFIO_EEH_INFO:
+		return eeh_vfio_ioctl(arg);
+#endif
 	}
 
 	return -ENOTTY;
@@ -342,6 +351,9 @@ static void tce_iommu_detach_group(void *iommu_data,
 		/* pr_debug("tce_vfio: detaching group #%u from iommu %p\n",
 				iommu_group_id(iommu_group), iommu_group); */
 		container->tbl = NULL;
+#ifdef CONFIG_VFIO_EEH
+		eeh_vfio_release(tbl);
+#endif
 		iommu_release_ownership(tbl);
 	}
 	mutex_unlock(&container->lock);
include/uapi/linux/vfio.h +1 -0
index cb9023d..1fd1bfb 100644
@@ -455,6 +455,63 @@ struct vfio_iommu_spapr_tce_info {
 
 #define VFIO_IOMMU_SPAPR_TCE_GET_INFO	_IO(VFIO_TYPE, VFIO_BASE + 12)
 
+/*

include/uapi/linux/vfio.h struct vfio_iommu_spapr_tce_info { · line 468

Quoted 10 lines
include/uapi/linux/vfio.h +10 -0
+ * The VFIO EEH info struct provides way to support EEH functionality
+ * for PCI device that is passed from host to guest via VFIO.
+ */
+#define VFIO_EEH_OP_MAP		0
+#define VFIO_EEH_OP_UNMAP	1
+#define VFIO_EEH_OP_SET_OPTION	2
+#define VFIO_EEH_OP_GET_ADDR	3
+#define VFIO_EEH_OP_GET_STATE	4
+#define VFIO_EEH_OP_PE_RESET	5
+#define VFIO_EEH_OP_PE_CONFIG	6

Is this really an "info" ioctl?

Quoted 49 lines
include/uapi/linux/vfio.h +46 -0
+
+struct vfio_eeh_info {
+	__u32 argsz;
+	__u32 op;
+
+	union {
+		struct vfio_eeh_map {
+			__u32 host_domain;
+			__u16 host_cfg_addr;
+			__u64 guest_buid;
+			__u16 guest_cfg_addr;
+		} map;
+		struct vfio_eeh_unmap {
+			__u64 buid;
+			__u16 cfg_addr;
+		} unmap;
+		struct vfio_eeh_set_option {
+			__u64 buid;
+			__u32 addr;
+			__u32 option;
+		} option;
+		struct vfio_eeh_pe_addr {
+			__u64 buid;
+			__u32 cfg_addr;
+			__u32 option;
+			__u32 ret;
+		} addr;
+		struct vfio_eeh_state {
+			__u64 buid;
+			__u32 pe_addr;
+			__u32 state;
+                } state;
+		struct vfio_eeh_reset {
+			__u64 buid;
+			__u32 pe_addr;
+			__u32 option;
+		} reset;
+		struct vfio_eeh_config {
+			__u64 buid;
+			__u32 pe_addr;
+		} config;
+	};
+};
+
+#define VFIO_EEH_INFO	_IO(VFIO_TYPE, VFIO_BASE + 21)
+
 /* ***************************************************************** */
 
 #endif /* _UAPIVFIO_H */
#10 lore raw

Re: [PATCH 3/8] drivers/vfio: New IOCTL command VFIO_EEH_INFO

Quoted 9 lines · Alex Williamson
Quoted 3 lines · Gavin Shan

The patch adds new IOCTL command VFIO_EEH_INFO to VFIO container to support EEH functionality for PCI devices, which have been passed from host to guest via VFIO.

Some comments throughout, but overall this seems to forgo every bit of the device ownership and protection model used by VFIO and lets the user pick arbitrary host devices and do various operations, mostly unchecked. That's not acceptable.

Right, I don't understand why we need that mapping for base EEH. The VFIO fd owns the PE, it can do operations on that PE, it doesn't need to know the guest token/address.

Only when we start doing in-kernel acceleration of RTAS do we need that sort of mapping established, which we'll be able to do via something akin to what we did with TCEs to ensure we validate that we have ownership of the physical device.

But let's proceed step by step. First, have something that is strictly EEH from qemu RTAS to VFIO via ioctl, and that requires no mapping of any sort.

Cheers,
Ben.

Quoted 762 lines
Quoted 65 lines
---
 arch/powerpc/platforms/powernv/Makefile   |   1 +
 arch/powerpc/platforms/powernv/eeh-vfio.c | 593 ++++++++++++++++++++++++++++++
 drivers/vfio/vfio_iommu_spapr_tce.c       |  12 +
 include/uapi/linux/vfio.h                 |  57 +++
 4 files changed, 663 insertions(+)
 create mode 100644 arch/powerpc/platforms/powernv/eeh-vfio.c
arch/powerpc/platforms/powernv/Makefile +1 -0
index 63cebb9..2b15a03 100644
@@ -6,5 +6,6 @@ obj-y			+= opal-msglog.o
 obj-$(CONFIG_SMP)	+= smp.o
 obj-$(CONFIG_PCI)	+= pci.o pci-p5ioc2.o pci-ioda.o
 obj-$(CONFIG_EEH)	+= eeh-ioda.o eeh-powernv.o
+obj-$(CONFIG_VFIO_EEH)	+= eeh-vfio.o
 obj-$(CONFIG_PPC_SCOM)	+= opal-xscom.o
 obj-$(CONFIG_MEMORY_FAILURE)	+= opal-memory-errors.o
arch/powerpc/platforms/powernv/eeh-vfio.c new file +39 -0
new file mode 100644
index 0000000..69d5f2d
@@ -0,0 +1,593 @@
+/*
+  * The file intends to support EEH funtionality for those PCI devices,
+  * which have been passed through from host to guest via VFIO. So this
+  * file is naturally part of VFIO implementation on PowerNV platform.
+  *
+  * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2014.
+  *
+  * This program is free software; you can redistribute it and/or modify
+  * it under the terms of the GNU General Public License as published by
+  * the Free Software Foundation; either version 2 of the License, or
+  * (at your option) any later version.
+  */
+
+#include <linux/init.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/kernel.h>
+#include <linux/kvm_host.h>
+#include <linux/msi.h>
+#include <linux/pci.h>
+#include <linux/string.h>
+#include <linux/vfio.h>
+
+#include <asm/eeh.h>
+#include <asm/eeh_event.h>
+#include <asm/io.h>
+#include <asm/iommu.h>
+#include <asm/opal.h>
+#include <asm/msi_bitmap.h>
+#include <asm/pci-bridge.h>
+#include <asm/ppc-pci.h>
+#include <asm/tce.h>
+#include <asm/uaccess.h>
+
+#include "powernv.h"
+#include "pci.h"
+
+static int powernv_eeh_vfio_map(struct vfio_eeh_info *info)
+{

arch/powerpc/platforms/powernv/eeh-vfio.c line 49

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +10 -0
+	struct pci_bus *bus, *pe_bus;
+	struct pci_dev *pdev;
+	struct eeh_dev *edev;
+	struct eeh_pe *pe;
+	int domain, bus_no, devfn;
+
+	/* Host address */
+	domain = info->map.host_domain;
+	bus_no = (info->map.host_cfg_addr >> 8) & 0xff;
+	devfn = info->map.host_cfg_addr & 0xff;

Where are we validating that the user has any legitimate claim to be touching this device?

Quoted 21 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +21 -0
+	/* Find PCI bus */
+	bus = pci_find_bus(domain, bus_no);
+	if (!bus) {
+		pr_warn("%s: PCI bus %04x:%02x not found\n",
+			__func__, domain, bus_no);
+		return -ENODEV;
+	}
+
+	/* Find PCI device */
+	pdev = pci_get_slot(bus, devfn);
+	if (!pdev) {
+		pr_warn("%s: PCI device %04x:%02x:%02x.%01x not found\n",
+			__func__, domain, bus_no,
+			PCI_SLOT(devfn), PCI_FUNC(devfn));
+		return -ENODEV;
+	}
+
+	/* No EEH device - almost impossible */
+	edev = pci_dev_to_eeh_dev(pdev);
+	if (unlikely(!edev)) {
+		pci_dev_put(pdev);

arch/powerpc/platforms/powernv/eeh-vfio.c line 81

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +10 -0
+		pr_warn("%s: No EEH dev for PCI device %s\n",
+			__func__, pci_name(pdev));
+		return -ENODEV;
+	}
+
+	/* Doesn't support PE migration between different PHBs */
+	pe = edev->pe;
+	if (!eeh_pe_passed(pe)) {
+		pe_bus = eeh_pe_bus_get(pe);
+		BUG_ON(!pe_bus);

Can a user trigger this maliciously?

Quoted 15 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +15 -0
+
+		/* PE# has format 00BBSS00 */
+		pe->guest_addr.buid    = info->map.guest_buid;
+		pe->guest_addr.pe_addr = pe_bus->number << 16;
+		eeh_pe_set_passed(pe, true);
+	} else if (pe->guest_addr.buid != info->map.guest_buid) {
+		pci_dev_put(pdev);
+		pr_warn("%s: Mismatched PHB BUID (0x%llx, 0x%llx)\n",
+			__func__, pe->guest_addr.buid, info->map.guest_buid);
+		return -EINVAL;
+	}
+
+	edev->guest_addr.buid = info->map.guest_buid;
+	edev->guest_addr.config_addr = info->map.guest_cfg_addr;
+	eeh_dev_set_passed(edev, true);

arch/powerpc/platforms/powernv/eeh-vfio.c line 106

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +10 -0
+
+	pr_debug("EEH: Host PCI dev %s to %llx-%02x:%02x.%01x\n",
+		 pci_name(pdev), info->map.guest_buid,
+		 (info->map.guest_cfg_addr >> 8) & 0xFF,
+		 PCI_SLOT(info->map.guest_cfg_addr & 0xFF),
+		 PCI_FUNC(info->map.guest_cfg_addr & 0xFF));
+
+	pci_dev_put(pdev);
+	return 0;
+}

So the effect of this function is that a user gets to setup an arbitrary guest mapping for an arbitrary host device and associated pe. Is that right? It seems bad.

arch/powerpc/platforms/powernv/eeh-vfio.c line 119

Quoted 13 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +13 -0
+
+static int powernv_eeh_vfio_unmap(struct vfio_eeh_info *info)
+{
+	struct eeh_vfio_pci_addr addr;
+	struct pci_dev *pdev;
+	struct eeh_dev *edev, *tmp;
+	struct eeh_pe *pe;
+	bool passed;
+
+	/* Get EEH device */
+	addr.buid = info->unmap.buid;
+	addr.config_addr = info->unmap.cfg_addr;
+	edev = eeh_vfio_dev_get(&addr);

eeh_vfio_dev_get() just looks for a "passed" dev and a match for a well known address space. Seems very exploitable.

Quoted 44 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +44 -0
+	if (!edev) {
+		pr_warn("%s: Cannot find %llx:%02x:%02x.%01x\n",
+			__func__, info->unmap.buid,
+			(info->unmap.cfg_addr >> 8) & 0xFF,
+			PCI_SLOT(info->unmap.cfg_addr & 0xFF),
+			PCI_FUNC(info->unmap.cfg_addr & 0xFF));
+		return -ENODEV;
+	}
+
+	/* Return EEH device */
+	memset(&edev->guest_addr, 0, sizeof(edev->guest_addr));
+	eeh_dev_set_passed(edev, false);
+	pdev = eeh_dev_to_pci_dev(edev);
+	pr_debug("EEH: Host PCI dev %s returned\n",
+		 pdev ? pci_name(pdev) : "NULL");
+
+	/* Return PE if no EEH device is owned by guest */
+	pe = edev->pe;
+	passed = false;
+	eeh_pe_for_each_dev(pe, edev, tmp) {
+		pdev = eeh_dev_to_pci_dev(edev);
+		if (pdev && pdev->subordinate)
+			continue;
+
+		if (eeh_dev_passed(edev)) {
+			passed = true;
+			break;
+		}
+	}
+
+	if (!passed) {
+		memset(&pe->guest_addr, 0, sizeof(pe->guest_addr));
+		eeh_pe_set_passed(pe, false);
+		pr_debug("EEH: PHB#%x-PE#%x returned to host\n",
+			 pe->phb->global_number, pe->addr);
+	}
+
+	return 0;
+}
+
+static int powernv_eeh_vfio_set_option(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_dev *edev;

arch/powerpc/platforms/powernv/eeh-vfio.c line 173

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +10 -0
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int opcode = info->option.option;
+	int ret = 0;
+
+	/* Check opcode */
+	if (opcode < EEH_OPT_DISABLE || opcode > EEH_OPT_THAW_DMA) {
+		pr_warn("%s: opcode %d out of range (%d, %d)\n",
+			__func__, opcode, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);
+		ret = 3;

Please don't make up arbitrary return values.

Quoted 196 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +196 -0
+		goto out;
+	}
+
+	/* Option "enable" uses PCI config address */
+	if (opcode = EEH_OPT_ENABLE) {
+		addr.buid = info->option.buid;
+		addr.config_addr = (info->option.addr >> 8) & 0xFFFF;
+		edev = eeh_vfio_dev_get(&addr);
+		if (!edev) {
+			pr_warn("%s: Cannot find %llx:%02x:%02x.%01x\n",
+				__func__, addr.buid,
+				(addr.config_addr >> 8) & 0xFF,
+				PCI_SLOT(addr.config_addr & 0xFF),
+				PCI_FUNC(addr.config_addr & 0xFF));
+			ret = 7;
+			goto out;
+		}
+		phb = edev->phb->private_data;
+	} else {
+		addr.buid    = info->option.buid;
+		addr.pe_addr = info->option.addr;
+		pe = eeh_vfio_pe_get(&addr);
+		if (!pe) {
+			pr_warn("%s: Cannot find PE %llx:%x\n",
+				__func__, addr.buid, addr.pe_addr);
+			ret = 7;
+			goto out;
+		}
+		phb = pe->phb->private_data;
+	}
+
+	/* Insure that the EEH stuff has been initialized */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 7;
+		goto out;
+	}
+
+	/*
+	 * The EEH functionality has been enabled on all PEs
+	 * by default. So just return success. The same situation
+	 * would be applied while we disable EEH functionality.
+	 * However, the guest isn't expected to disable that
+	 * at all.
+	 */
+	if (opcode = EEH_OPT_DISABLE ||
+	    opcode = EEH_OPT_ENABLE) {
+		ret = 0;
+		goto out;
+	}
+
+	/*
+	 * Call into the IODA dependent backend in order
+	 * to enable DMA or MMIO for the indicated PE.
+	 */
+	if (phb->eeh_ops && phb->eeh_ops->set_option) {
+		if (phb->eeh_ops->set_option(pe, opcode)) {
+			pr_warn("%s: Failure from backend\n",
+				__func__);
+			ret = 1;
+		}
+	} else {
+		pr_warn("%s: Unsupported request\n",
+			__func__);
+		ret = 7;
+	}
+
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_get_addr(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_dev *edev;
+	struct eeh_vfio_pci_addr addr;
+	int opcode = info->addr.option;
+	int ret = 0;
+
+	/* Check opcode */
+	if (opcode != 0 && opcode != 1) {
+		pr_warn("%s: opcode %d out of range (0, 1)\n",
+			__func__, opcode);
+		ret = 3;
+		goto out;
+	}
+
+	/* Find EEH device */
+	addr.buid = info->addr.buid;
+	addr.config_addr = (info->addr.cfg_addr >> 8 ) & 0xFFFF;
+	edev = eeh_vfio_dev_get(&addr);
+	if (!edev) {
+		pr_warn("%s: Cannot find %llx:%02x:%02x.%01x\n",
+			__func__, addr.buid,
+			(addr.config_addr >> 8) & 0xFF,
+			PCI_SLOT(addr.config_addr & 0xFF),
+			PCI_FUNC(addr.config_addr & 0xFF));
+		ret = 7;
+		goto out;
+	}
+	phb = edev->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+	}
+
+	/* EEH device passed ? */
+	if (!eeh_dev_passed(edev)) {
+		pr_warn("%s: EEH dev %llx:%02x:%02x.%01x owned by host\n",
+			__func__, addr.buid,
+			(addr.config_addr >> 8) & 0xFF,
+			PCI_SLOT(addr.config_addr & 0xFF),
+			PCI_FUNC(addr.config_addr & 0xFF));
+		ret = 3;
+		goto out;
+	}
+
+	/*
+	 * Fill result according to opcode. We don't differentiate
+	 * PCI bus and device sensitive PE here.
+	 */
+	if (opcode = 0)
+		info->addr.ret = edev->pe->guest_addr.pe_addr;
+	else
+		info->addr.ret = 1;
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_get_state(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int result, ret = 0;
+
+	/* Locate the PE */
+	addr.buid    = info->state.buid;
+	addr.pe_addr = info->state.pe_addr;
+	pe = eeh_vfio_pe_get(&addr);
+	if (!pe) {
+		pr_warn("%s: Cannot locate %llx:%x\n",
+			__func__, addr.buid, addr.pe_addr);
+		ret = 3;
+		goto out;
+	}
+	phb = pe->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+	}
+
+	/* Call to the IOC dependent function */
+	if (phb->eeh_ops && phb->eeh_ops->get_state) {
+		result = phb->eeh_ops->get_state(pe);
+
+		if (!(result & EEH_STATE_RESET_ACTIVE) &&
+		     (result & EEH_STATE_DMA_ENABLED) &&
+		     (result & EEH_STATE_MMIO_ENABLED))
+			info->state.state = 0;
+		else if (result & EEH_STATE_RESET_ACTIVE)
+			info->state.state = 1;
+		else if (!(result & EEH_STATE_RESET_ACTIVE) &&
+			 !(result & EEH_STATE_DMA_ENABLED) &&
+			 !(result & EEH_STATE_MMIO_ENABLED))
+			info->state.state = 2;
+		else if (!(result & EEH_STATE_RESET_ACTIVE) &&
+			 (result & EEH_STATE_DMA_ENABLED) &&
+			 !(result & EEH_STATE_MMIO_ENABLED))
+			info->state.state = 4;
+		else
+			info->state.state = 5;
+
+		ret = 0;
+	} else {
+		pr_warn("%s: Unsupported request\n", __func__);
+		ret = 3;
+	}
+
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_pe_reset(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_pe *pe;

arch/powerpc/platforms/powernv/eeh-vfio.c line 379

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +10 -0
+	struct eeh_vfio_pci_addr addr;
+	int opcode = info->reset.option;
+	int ret = 0;
+
+	/* Check opcode */
+	if (opcode != EEH_RESET_DEACTIVATE &&
+	    opcode != EEH_RESET_HOT &&
+	    opcode != EEH_RESET_FUNDAMENTAL) {
+		pr_warn("%s: Unsupported opcode %d\n",
+			__func__, opcode);

Console warnings are exploitable DoS attacks.

Quoted 139 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +139 -0
+		ret = 3;
+		goto out;
+	}
+
+	/* Locate the PE */
+	addr.buid    = info->reset.buid;
+	addr.pe_addr = info->reset.pe_addr;
+	pe = eeh_vfio_pe_get(&addr);
+	if (!pe) {
+		pr_warn("%s: Cannot locate %llx:%x\n",
+			__func__, addr.buid, addr.pe_addr);
+		ret = 3;
+		goto out;
+	}
+	phb = pe->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+	}
+
+	/* Call into the IODA dependent backend to do the reset */
+	if (!phb->eeh_ops ||
+	    !phb->eeh_ops->set_option ||
+	    !phb->eeh_ops->reset) {
+		pr_warn("%s: Unsupported request\n",
+			__func__);
+		ret = 7;
+	} else {
+		/*
+		 * The frozen PE might be caused by the mechanism called
+		 * PAPR error injection, which is supposed to be one-shot
+		 * without "sticky" bit as being stated by the spec. But
+		 * the reality isn't that, at least on P7IOC. So we have
+		 * to clear that to avoid recrusive error, which fails the
+		 * recovery eventually.
+		 */
+		if (opcode = EEH_RESET_DEACTIVATE)
+			opal_pci_reset(phb->opal_id,
+				       OPAL_PHB_ERROR,
+				       OPAL_ASSERT_RESET);
+
+		if (phb->eeh_ops->reset(pe, opcode)) {
+			pr_warn("%s: Failure from backend\n", __func__);
+			ret = 1;
+			goto out;
+		}
+
+		/*
+		 * The PE is still in frozen state and we need clear that.
+		 * It's good to clear frozen state after deassert to avoid
+		 * messy IO access during reset, which might cause recrusive
+		 * frozen PE.
+		 */
+		if (opcode = EEH_RESET_DEACTIVATE) {
+			if (phb->eeh_ops->set_option(pe, EEH_OPT_THAW_MMIO) ||
+			    phb->eeh_ops->set_option(pe, EEH_OPT_THAW_DMA)) {
+				pr_warn("%s: Cannot clear frozen state\n",
+					__func__);
+				ret = 1;
+			}
+
+			eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
+		}
+	}
+
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_pe_config(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int ret = 0;
+
+	/* Locate the PE */
+	addr.buid    = info->config.buid;
+	addr.pe_addr = info->config.pe_addr;
+	pe = eeh_vfio_pe_get(&addr);
+	if (!pe) {
+		pr_warn("%s: Cannot locate %llx:%x\n",
+			__func__, addr.buid, addr.pe_addr);
+		ret = 3;
+		goto out;
+	}
+	phb = pe->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+        }
+
+	/*
+	 * The access to PCI config space on VFIO device has some
+	 * limitations. Part of PCI config space, including BAR
+	 * registers are not readable and writable. So the guest
+	 * should have stale values for those registers and we have
+	 * to restore them in host side.
+	 */
+	eeh_pe_restore_bars(pe);
+out:
+	return ret;
+}
+
+void eeh_vfio_release(struct iommu_table *tbl)
+{
+	struct pnv_ioda_pe *pnv_pe = container_of(tbl, struct pnv_ioda_pe,
+						  tce32_table);
+	struct pnv_phb *phb = pnv_pe->phb;
+	struct eeh_pe *phb_pe, *pe;
+	struct eeh_dev dev, *edev, *tmp;
+
+	/* Find PHB PE */
+	phb_pe = eeh_phb_pe_get(phb->hose);
+	if (unlikely(!phb_pe)) {
+		pr_warn("%s: Cannot find PHB#%d PE\n",
+			__func__, phb->hose->global_number);
+		return;
+	}
+
+	/* Find PE */
+	memset(&dev, 0, sizeof(struct eeh_dev));
+	dev.phb = phb->hose;
+	dev.pe_config_addr = pnv_pe->pe_number;
+	pe = eeh_pe_get(&dev);
+	if (unlikely(!pe)) {
+		pr_warn("%s: Cannot find PE instance for PHB#%d-PE#%d\n",
+			__func__, phb->hose->global_number,
+			pnv_pe->pe_number);
+		return;
+	}

arch/powerpc/platforms/powernv/eeh-vfio.c line 528

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +10 -0
+
+	/* Release it to host */
+	if (!eeh_pe_passed(pe))
+		return;
+
+	eeh_pe_for_each_dev(pe, edev, tmp) {
+		if (!eeh_dev_passed(edev))
+			continue;
+
+		memset(&edev->guest_addr, 0, sizeof(edev->guest_addr));

Is guest_addr = { 0 } not valid? As agraf already mentioned, there are a number of issues with using a guest_address for a token.

Quoted 12 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +12 -0
+		eeh_dev_set_passed(edev, false);
+	}
+
+	memset(&pe->guest_addr, 0, sizeof(pe->guest_addr));
+	eeh_pe_set_passed(pe, false);
+}
+EXPORT_SYMBOL(eeh_vfio_release);
+
+int eeh_vfio_ioctl(unsigned long arg)
+{
+	struct vfio_eeh_info info;
+	int ret = -EINVAL;

arch/powerpc/platforms/powernv/eeh-vfio.c line 550

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +10 -0
+
+	/* Copy over user argument */
+	if (copy_from_user(&info, (void __user *)arg, sizeof(info))) {
+		pr_warn("%s: Cannot copy user argument 0x%lx\n",
+			__func__, arg);
+		return -EFAULT;
+	}
+
+	/* Sanity check */
+	if (info.argsz != sizeof(info)) {

This breaks compatibility if you need to later add a new ops with a larger footprint.

Quoted 60 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +43 -0
+		pr_warn("%s: Invalid argument size (%d, %ld)\n",
+			__func__, info.argsz, sizeof(info));
+		return -EINVAL;
+	}
+
+	/* Route according to operation */
+	switch (info.op) {
+	case VFIO_EEH_OP_MAP:
+		ret = powernv_eeh_vfio_map(&info);
+		break;
+	case VFIO_EEH_OP_UNMAP:
+		ret = powernv_eeh_vfio_unmap(&info);
+		break;
+	case VFIO_EEH_OP_SET_OPTION:
+		ret = powernv_eeh_vfio_set_option(&info);
+		break;
+	case VFIO_EEH_OP_GET_ADDR:
+		ret = powernv_eeh_vfio_get_addr(&info);
+		break;
+	case VFIO_EEH_OP_GET_STATE:
+		ret = powernv_eeh_vfio_get_state(&info);
+		break;
+	case VFIO_EEH_OP_PE_RESET:
+		ret = powernv_eeh_vfio_pe_reset(&info);
+		break;
+	case VFIO_EEH_OP_PE_CONFIG:
+		ret = powernv_eeh_vfio_pe_config(&info);
+		break;
+	default:
+		pr_info("%s: Cannot handle op#%d\n",
+			__func__, info.op);
+	}
+
+	/* Copy data back */
+	if (!ret && copy_to_user((void __user *)arg, &info, sizeof(info))) {
+		pr_warn("%s: Cannot copy to user 0x%lx\n",
+			__func__, arg);
+		return -EFAULT;
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(eeh_vfio_ioctl);
drivers/vfio/vfio_iommu_spapr_tce.c +5 -0
index a84788b..c45dece 100644
@@ -26,6 +26,11 @@
 #define DRIVER_AUTHOR   "[email protected]"
 #define DRIVER_DESC     "VFIO IOMMU SPAPR TCE"
 
+#ifdef CONFIG_VFIO_EEH
+extern void eeh_vfio_release(struct iommu_table *tbl);
+extern int eeh_vfio_ioctl(unsigned long arg);
+#endif
+
 static void tce_iommu_detach_group(void *iommu_data,
 		struct iommu_group *iommu_group);
 

drivers/vfio/vfio_iommu_spapr_tce.c static long tce_iommu_ioctl(void *iommu_data, · line 291

Quoted 4 lines
drivers/vfio/vfio_iommu_spapr_tce.c +1 -0
@@ -283,6 +288,10 @@ static long tce_iommu_ioctl(void *iommu_data,
 		tce_iommu_disable(container);
 		mutex_unlock(&container->lock);
 		return 0;
+#ifdef CONFIG_VFIO_EEH

I'm not a fan of all these #ifdefs, hide it in eeh_vfio_ioctl() and eeh_vfio_release() if needed.

Quoted 25 lines
drivers/vfio/vfio_iommu_spapr_tce.c +6 -0
+	case VFIO_EEH_INFO:
+		return eeh_vfio_ioctl(arg);
+#endif
 	}
 
 	return -ENOTTY;
@@ -342,6 +351,9 @@ static void tce_iommu_detach_group(void *iommu_data,
 		/* pr_debug("tce_vfio: detaching group #%u from iommu %p\n",
 				iommu_group_id(iommu_group), iommu_group); */
 		container->tbl = NULL;
+#ifdef CONFIG_VFIO_EEH
+		eeh_vfio_release(tbl);
+#endif
 		iommu_release_ownership(tbl);
 	}
 	mutex_unlock(&container->lock);
include/uapi/linux/vfio.h +1 -0
index cb9023d..1fd1bfb 100644
@@ -455,6 +455,63 @@ struct vfio_iommu_spapr_tce_info {
 
 #define VFIO_IOMMU_SPAPR_TCE_GET_INFO	_IO(VFIO_TYPE, VFIO_BASE + 12)
 
+/*

include/uapi/linux/vfio.h struct vfio_iommu_spapr_tce_info { · line 468

Quoted 10 lines
include/uapi/linux/vfio.h +10 -0
+ * The VFIO EEH info struct provides way to support EEH functionality
+ * for PCI device that is passed from host to guest via VFIO.
+ */
+#define VFIO_EEH_OP_MAP		0
+#define VFIO_EEH_OP_UNMAP	1
+#define VFIO_EEH_OP_SET_OPTION	2
+#define VFIO_EEH_OP_GET_ADDR	3
+#define VFIO_EEH_OP_GET_STATE	4
+#define VFIO_EEH_OP_PE_RESET	5
+#define VFIO_EEH_OP_PE_CONFIG	6

Is this really an "info" ioctl?

Quoted 49 lines
include/uapi/linux/vfio.h +46 -0
+
+struct vfio_eeh_info {
+	__u32 argsz;
+	__u32 op;
+
+	union {
+		struct vfio_eeh_map {
+			__u32 host_domain;
+			__u16 host_cfg_addr;
+			__u64 guest_buid;
+			__u16 guest_cfg_addr;
+		} map;
+		struct vfio_eeh_unmap {
+			__u64 buid;
+			__u16 cfg_addr;
+		} unmap;
+		struct vfio_eeh_set_option {
+			__u64 buid;
+			__u32 addr;
+			__u32 option;
+		} option;
+		struct vfio_eeh_pe_addr {
+			__u64 buid;
+			__u32 cfg_addr;
+			__u32 option;
+			__u32 ret;
+		} addr;
+		struct vfio_eeh_state {
+			__u64 buid;
+			__u32 pe_addr;
+			__u32 state;
+                } state;
+		struct vfio_eeh_reset {
+			__u64 buid;
+			__u32 pe_addr;
+			__u32 option;
+		} reset;
+		struct vfio_eeh_config {
+			__u64 buid;
+			__u32 pe_addr;
+		} config;
+	};
+};
+
+#define VFIO_EEH_INFO	_IO(VFIO_TYPE, VFIO_BASE + 21)
+
 /* ***************************************************************** */
 
 #endif /* _UAPIVFIO_H */
author
#11 lore raw

Re: [PATCH 3/8] drivers/vfio: New IOCTL command VFIO_EEH_INFO

In reply to Alex Williamson
Quoted 4 lines · Alex Williamson
Quoted 3 lines · Gavin Shan

The patch adds new IOCTL command VFIO_EEH_INFO to VFIO container to support EEH functionality for PCI devices, which have been passed from host to guest via VFIO.

Thanks for your comments, Alex.W :-)

Quoted 6 lines

Some comments throughout, but overall this seems to forgo every bit of the device ownership and protection model used by VFIO and lets the user pick arbitrary host devices and do various operations, mostly unchecked. That's not acceptable.

As what I replied to patch[2], I'm going to let VFIO-PCI-dev fd handle the newly introduced IOCTL command. That way, we should follow the VFIO design principles (ownership and protection) because VFIO-PCI-dev fd is owned by QEMU process usually.

Also, the address mapping maintained in EEH will be removed.

Quoted 70 lines
Quoted 70 lines
---
 arch/powerpc/platforms/powernv/Makefile   |   1 +
 arch/powerpc/platforms/powernv/eeh-vfio.c | 593 ++++++++++++++++++++++++++++++
 drivers/vfio/vfio_iommu_spapr_tce.c       |  12 +
 include/uapi/linux/vfio.h                 |  57 +++
 4 files changed, 663 insertions(+)
 create mode 100644 arch/powerpc/platforms/powernv/eeh-vfio.c
arch/powerpc/platforms/powernv/Makefile +1 -0
index 63cebb9..2b15a03 100644
@@ -6,5 +6,6 @@ obj-y			+= opal-msglog.o
 obj-$(CONFIG_SMP)	+= smp.o
 obj-$(CONFIG_PCI)	+= pci.o pci-p5ioc2.o pci-ioda.o
 obj-$(CONFIG_EEH)	+= eeh-ioda.o eeh-powernv.o
+obj-$(CONFIG_VFIO_EEH)	+= eeh-vfio.o
 obj-$(CONFIG_PPC_SCOM)	+= opal-xscom.o
 obj-$(CONFIG_MEMORY_FAILURE)	+= opal-memory-errors.o
arch/powerpc/platforms/powernv/eeh-vfio.c new file +44 -0
new file mode 100644
index 0000000..69d5f2d
@@ -0,0 +1,593 @@
+/*
+  * The file intends to support EEH funtionality for those PCI devices,
+  * which have been passed through from host to guest via VFIO. So this
+  * file is naturally part of VFIO implementation on PowerNV platform.
+  *
+  * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2014.
+  *
+  * This program is free software; you can redistribute it and/or modify
+  * it under the terms of the GNU General Public License as published by
+  * the Free Software Foundation; either version 2 of the License, or
+  * (at your option) any later version.
+  */
+
+#include <linux/init.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/kernel.h>
+#include <linux/kvm_host.h>
+#include <linux/msi.h>
+#include <linux/pci.h>
+#include <linux/string.h>
+#include <linux/vfio.h>
+
+#include <asm/eeh.h>
+#include <asm/eeh_event.h>
+#include <asm/io.h>
+#include <asm/iommu.h>
+#include <asm/opal.h>
+#include <asm/msi_bitmap.h>
+#include <asm/pci-bridge.h>
+#include <asm/ppc-pci.h>
+#include <asm/tce.h>
+#include <asm/uaccess.h>
+
+#include "powernv.h"
+#include "pci.h"
+
+static int powernv_eeh_vfio_map(struct vfio_eeh_info *info)
+{
+	struct pci_bus *bus, *pe_bus;
+	struct pci_dev *pdev;
+	struct eeh_dev *edev;
+	struct eeh_pe *pe;
+	int domain, bus_no, devfn;
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 49

Quoted 5 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +5 -0
+
+	/* Host address */
+	domain = info->map.host_domain;
+	bus_no = (info->map.host_cfg_addr >> 8) & 0xff;
+	devfn = info->map.host_cfg_addr & 0xff;

Where are we validating that the user has any legitimate claim to be touching this device?

I'll let VFIO-PCI-dev fd handle the IOCTL command. With that, we shouldn't have the problem.

Quoted 55 lines
Quoted 21 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +21 -0
+	/* Find PCI bus */
+	bus = pci_find_bus(domain, bus_no);
+	if (!bus) {
+		pr_warn("%s: PCI bus %04x:%02x not found\n",
+			__func__, domain, bus_no);
+		return -ENODEV;
+	}
+
+	/* Find PCI device */
+	pdev = pci_get_slot(bus, devfn);
+	if (!pdev) {
+		pr_warn("%s: PCI device %04x:%02x:%02x.%01x not found\n",
+			__func__, domain, bus_no,
+			PCI_SLOT(devfn), PCI_FUNC(devfn));
+		return -ENODEV;
+	}
+
+	/* No EEH device - almost impossible */
+	edev = pci_dev_to_eeh_dev(pdev);
+	if (unlikely(!edev)) {
+		pci_dev_put(pdev);

arch/powerpc/platforms/powernv/eeh-vfio.c line 81

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +10 -0
+		pr_warn("%s: No EEH dev for PCI device %s\n",
+			__func__, pci_name(pdev));
+		return -ENODEV;
+	}
+
+	/* Doesn't support PE migration between different PHBs */
+	pe = edev->pe;
+	if (!eeh_pe_passed(pe)) {
+		pe_bus = eeh_pe_bus_get(pe);
+		BUG_ON(!pe_bus);

Can a user trigger this maliciously?

Quoted 21 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +21 -0
+
+		/* PE# has format 00BBSS00 */
+		pe->guest_addr.buid    = info->map.guest_buid;
+		pe->guest_addr.pe_addr = pe_bus->number << 16;
+		eeh_pe_set_passed(pe, true);
+	} else if (pe->guest_addr.buid != info->map.guest_buid) {
+		pci_dev_put(pdev);
+		pr_warn("%s: Mismatched PHB BUID (0x%llx, 0x%llx)\n",
+			__func__, pe->guest_addr.buid, info->map.guest_buid);
+		return -EINVAL;
+	}
+
+	edev->guest_addr.buid = info->map.guest_buid;
+	edev->guest_addr.config_addr = info->map.guest_cfg_addr;
+	eeh_dev_set_passed(edev, true);
+
+	pr_debug("EEH: Host PCI dev %s to %llx-%02x:%02x.%01x\n",
+		 pci_name(pdev), info->map.guest_buid,
+		 (info->map.guest_cfg_addr >> 8) & 0xFF,
+		 PCI_SLOT(info->map.guest_cfg_addr & 0xFF),
+		 PCI_FUNC(info->map.guest_cfg_addr & 0xFF));
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 106

Quoted 4 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +4 -0
+
+	pci_dev_put(pdev);
+	return 0;
+}

So the effect of this function is that a user gets to setup an arbitrary guest mapping for an arbitrary host device and associated pe. Is that right? It seems bad.

I'm going to remove this mapping in next revision.

Quoted 65 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 119

Quoted 13 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +13 -0
+
+static int powernv_eeh_vfio_unmap(struct vfio_eeh_info *info)
+{
+	struct eeh_vfio_pci_addr addr;
+	struct pci_dev *pdev;
+	struct eeh_dev *edev, *tmp;
+	struct eeh_pe *pe;
+	bool passed;
+
+	/* Get EEH device */
+	addr.buid = info->unmap.buid;
+	addr.config_addr = info->unmap.cfg_addr;
+	edev = eeh_vfio_dev_get(&addr);

eeh_vfio_dev_get() just looks for a "passed" dev and a match for a well known address space. Seems very exploitable.

Quoted 48 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +48 -0
+	if (!edev) {
+		pr_warn("%s: Cannot find %llx:%02x:%02x.%01x\n",
+			__func__, info->unmap.buid,
+			(info->unmap.cfg_addr >> 8) & 0xFF,
+			PCI_SLOT(info->unmap.cfg_addr & 0xFF),
+			PCI_FUNC(info->unmap.cfg_addr & 0xFF));
+		return -ENODEV;
+	}
+
+	/* Return EEH device */
+	memset(&edev->guest_addr, 0, sizeof(edev->guest_addr));
+	eeh_dev_set_passed(edev, false);
+	pdev = eeh_dev_to_pci_dev(edev);
+	pr_debug("EEH: Host PCI dev %s returned\n",
+		 pdev ? pci_name(pdev) : "NULL");
+
+	/* Return PE if no EEH device is owned by guest */
+	pe = edev->pe;
+	passed = false;
+	eeh_pe_for_each_dev(pe, edev, tmp) {
+		pdev = eeh_dev_to_pci_dev(edev);
+		if (pdev && pdev->subordinate)
+			continue;
+
+		if (eeh_dev_passed(edev)) {
+			passed = true;
+			break;
+		}
+	}
+
+	if (!passed) {
+		memset(&pe->guest_addr, 0, sizeof(pe->guest_addr));
+		eeh_pe_set_passed(pe, false);
+		pr_debug("EEH: PHB#%x-PE#%x returned to host\n",
+			 pe->phb->global_number, pe->addr);
+	}
+
+	return 0;
+}
+
+static int powernv_eeh_vfio_set_option(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_dev *edev;
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int opcode = info->option.option;
+	int ret = 0;
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 173

Quoted 6 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +6 -0
+
+	/* Check opcode */
+	if (opcode < EEH_OPT_DISABLE || opcode > EEH_OPT_THAW_DMA) {
+		pr_warn("%s: opcode %d out of range (%d, %d)\n",
+			__func__, opcode, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);
+		ret = 3;

Please don't make up arbitrary return values.

Nope, it will be turned to "-3" eventually by QEMU. That means "Invalid Parameter" defined in PAPR spec.

The IOCTL command handler return 3 values:

< 0: Linux kernel error. For example, error from copy_from_user().

Quoted 1 line

0: Error code to the EEH RTAS request, which will be returned to guest.

= 0: Success

Quoted 200 lines
Quoted 200 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +200 -0
+		goto out;
+	}
+
+	/* Option "enable" uses PCI config address */
+	if (opcode = EEH_OPT_ENABLE) {
+		addr.buid = info->option.buid;
+		addr.config_addr = (info->option.addr >> 8) & 0xFFFF;
+		edev = eeh_vfio_dev_get(&addr);
+		if (!edev) {
+			pr_warn("%s: Cannot find %llx:%02x:%02x.%01x\n",
+				__func__, addr.buid,
+				(addr.config_addr >> 8) & 0xFF,
+				PCI_SLOT(addr.config_addr & 0xFF),
+				PCI_FUNC(addr.config_addr & 0xFF));
+			ret = 7;
+			goto out;
+		}
+		phb = edev->phb->private_data;
+	} else {
+		addr.buid    = info->option.buid;
+		addr.pe_addr = info->option.addr;
+		pe = eeh_vfio_pe_get(&addr);
+		if (!pe) {
+			pr_warn("%s: Cannot find PE %llx:%x\n",
+				__func__, addr.buid, addr.pe_addr);
+			ret = 7;
+			goto out;
+		}
+		phb = pe->phb->private_data;
+	}
+
+	/* Insure that the EEH stuff has been initialized */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 7;
+		goto out;
+	}
+
+	/*
+	 * The EEH functionality has been enabled on all PEs
+	 * by default. So just return success. The same situation
+	 * would be applied while we disable EEH functionality.
+	 * However, the guest isn't expected to disable that
+	 * at all.
+	 */
+	if (opcode = EEH_OPT_DISABLE ||
+	    opcode = EEH_OPT_ENABLE) {
+		ret = 0;
+		goto out;
+	}
+
+	/*
+	 * Call into the IODA dependent backend in order
+	 * to enable DMA or MMIO for the indicated PE.
+	 */
+	if (phb->eeh_ops && phb->eeh_ops->set_option) {
+		if (phb->eeh_ops->set_option(pe, opcode)) {
+			pr_warn("%s: Failure from backend\n",
+				__func__);
+			ret = 1;
+		}
+	} else {
+		pr_warn("%s: Unsupported request\n",
+			__func__);
+		ret = 7;
+	}
+
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_get_addr(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_dev *edev;
+	struct eeh_vfio_pci_addr addr;
+	int opcode = info->addr.option;
+	int ret = 0;
+
+	/* Check opcode */
+	if (opcode != 0 && opcode != 1) {
+		pr_warn("%s: opcode %d out of range (0, 1)\n",
+			__func__, opcode);
+		ret = 3;
+		goto out;
+	}
+
+	/* Find EEH device */
+	addr.buid = info->addr.buid;
+	addr.config_addr = (info->addr.cfg_addr >> 8 ) & 0xFFFF;
+	edev = eeh_vfio_dev_get(&addr);
+	if (!edev) {
+		pr_warn("%s: Cannot find %llx:%02x:%02x.%01x\n",
+			__func__, addr.buid,
+			(addr.config_addr >> 8) & 0xFF,
+			PCI_SLOT(addr.config_addr & 0xFF),
+			PCI_FUNC(addr.config_addr & 0xFF));
+		ret = 7;
+		goto out;
+	}
+	phb = edev->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+	}
+
+	/* EEH device passed ? */
+	if (!eeh_dev_passed(edev)) {
+		pr_warn("%s: EEH dev %llx:%02x:%02x.%01x owned by host\n",
+			__func__, addr.buid,
+			(addr.config_addr >> 8) & 0xFF,
+			PCI_SLOT(addr.config_addr & 0xFF),
+			PCI_FUNC(addr.config_addr & 0xFF));
+		ret = 3;
+		goto out;
+	}
+
+	/*
+	 * Fill result according to opcode. We don't differentiate
+	 * PCI bus and device sensitive PE here.
+	 */
+	if (opcode = 0)
+		info->addr.ret = edev->pe->guest_addr.pe_addr;
+	else
+		info->addr.ret = 1;
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_get_state(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int result, ret = 0;
+
+	/* Locate the PE */
+	addr.buid    = info->state.buid;
+	addr.pe_addr = info->state.pe_addr;
+	pe = eeh_vfio_pe_get(&addr);
+	if (!pe) {
+		pr_warn("%s: Cannot locate %llx:%x\n",
+			__func__, addr.buid, addr.pe_addr);
+		ret = 3;
+		goto out;
+	}
+	phb = pe->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+	}
+
+	/* Call to the IOC dependent function */
+	if (phb->eeh_ops && phb->eeh_ops->get_state) {
+		result = phb->eeh_ops->get_state(pe);
+
+		if (!(result & EEH_STATE_RESET_ACTIVE) &&
+		     (result & EEH_STATE_DMA_ENABLED) &&
+		     (result & EEH_STATE_MMIO_ENABLED))
+			info->state.state = 0;
+		else if (result & EEH_STATE_RESET_ACTIVE)
+			info->state.state = 1;
+		else if (!(result & EEH_STATE_RESET_ACTIVE) &&
+			 !(result & EEH_STATE_DMA_ENABLED) &&
+			 !(result & EEH_STATE_MMIO_ENABLED))
+			info->state.state = 2;
+		else if (!(result & EEH_STATE_RESET_ACTIVE) &&
+			 (result & EEH_STATE_DMA_ENABLED) &&
+			 !(result & EEH_STATE_MMIO_ENABLED))
+			info->state.state = 4;
+		else
+			info->state.state = 5;
+
+		ret = 0;
+	} else {
+		pr_warn("%s: Unsupported request\n", __func__);
+		ret = 3;
+	}
+
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_pe_reset(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int opcode = info->reset.option;
+	int ret = 0;
+
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 379

Quoted 6 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +6 -0
+	/* Check opcode */
+	if (opcode != EEH_RESET_DEACTIVATE &&
+	    opcode != EEH_RESET_HOT &&
+	    opcode != EEH_RESET_FUNDAMENTAL) {
+		pr_warn("%s: Unsupported opcode %d\n",
+			__func__, opcode);

Console warnings are exploitable DoS attacks.

Yep. I'll change all pr_warn() to pr_debug() in next revision.

Quoted 144 lines
Quoted 144 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +144 -0
+		ret = 3;
+		goto out;
+	}
+
+	/* Locate the PE */
+	addr.buid    = info->reset.buid;
+	addr.pe_addr = info->reset.pe_addr;
+	pe = eeh_vfio_pe_get(&addr);
+	if (!pe) {
+		pr_warn("%s: Cannot locate %llx:%x\n",
+			__func__, addr.buid, addr.pe_addr);
+		ret = 3;
+		goto out;
+	}
+	phb = pe->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+	}
+
+	/* Call into the IODA dependent backend to do the reset */
+	if (!phb->eeh_ops ||
+	    !phb->eeh_ops->set_option ||
+	    !phb->eeh_ops->reset) {
+		pr_warn("%s: Unsupported request\n",
+			__func__);
+		ret = 7;
+	} else {
+		/*
+		 * The frozen PE might be caused by the mechanism called
+		 * PAPR error injection, which is supposed to be one-shot
+		 * without "sticky" bit as being stated by the spec. But
+		 * the reality isn't that, at least on P7IOC. So we have
+		 * to clear that to avoid recrusive error, which fails the
+		 * recovery eventually.
+		 */
+		if (opcode = EEH_RESET_DEACTIVATE)
+			opal_pci_reset(phb->opal_id,
+				       OPAL_PHB_ERROR,
+				       OPAL_ASSERT_RESET);
+
+		if (phb->eeh_ops->reset(pe, opcode)) {
+			pr_warn("%s: Failure from backend\n", __func__);
+			ret = 1;
+			goto out;
+		}
+
+		/*
+		 * The PE is still in frozen state and we need clear that.
+		 * It's good to clear frozen state after deassert to avoid
+		 * messy IO access during reset, which might cause recrusive
+		 * frozen PE.
+		 */
+		if (opcode = EEH_RESET_DEACTIVATE) {
+			if (phb->eeh_ops->set_option(pe, EEH_OPT_THAW_MMIO) ||
+			    phb->eeh_ops->set_option(pe, EEH_OPT_THAW_DMA)) {
+				pr_warn("%s: Cannot clear frozen state\n",
+					__func__);
+				ret = 1;
+			}
+
+			eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
+		}
+	}
+
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_pe_config(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int ret = 0;
+
+	/* Locate the PE */
+	addr.buid    = info->config.buid;
+	addr.pe_addr = info->config.pe_addr;
+	pe = eeh_vfio_pe_get(&addr);
+	if (!pe) {
+		pr_warn("%s: Cannot locate %llx:%x\n",
+			__func__, addr.buid, addr.pe_addr);
+		ret = 3;
+		goto out;
+	}
+	phb = pe->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+        }
+
+	/*
+	 * The access to PCI config space on VFIO device has some
+	 * limitations. Part of PCI config space, including BAR
+	 * registers are not readable and writable. So the guest
+	 * should have stale values for those registers and we have
+	 * to restore them in host side.
+	 */
+	eeh_pe_restore_bars(pe);
+out:
+	return ret;
+}
+
+void eeh_vfio_release(struct iommu_table *tbl)
+{
+	struct pnv_ioda_pe *pnv_pe = container_of(tbl, struct pnv_ioda_pe,
+						  tce32_table);
+	struct pnv_phb *phb = pnv_pe->phb;
+	struct eeh_pe *phb_pe, *pe;
+	struct eeh_dev dev, *edev, *tmp;
+
+	/* Find PHB PE */
+	phb_pe = eeh_phb_pe_get(phb->hose);
+	if (unlikely(!phb_pe)) {
+		pr_warn("%s: Cannot find PHB#%d PE\n",
+			__func__, phb->hose->global_number);
+		return;
+	}
+
+	/* Find PE */
+	memset(&dev, 0, sizeof(struct eeh_dev));
+	dev.phb = phb->hose;
+	dev.pe_config_addr = pnv_pe->pe_number;
+	pe = eeh_pe_get(&dev);
+	if (unlikely(!pe)) {
+		pr_warn("%s: Cannot find PE instance for PHB#%d-PE#%d\n",
+			__func__, phb->hose->global_number,
+			pnv_pe->pe_number);
+		return;
+	}
+
+	/* Release it to host */
+	if (!eeh_pe_passed(pe))
+		return;
+
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 528

Quoted 5 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +5 -0
+	eeh_pe_for_each_dev(pe, edev, tmp) {
+		if (!eeh_dev_passed(edev))
+			continue;
+
+		memset(&edev->guest_addr, 0, sizeof(edev->guest_addr));

Is guest_addr = { 0 } not valid? As agraf already mentioned, there are a number of issues with using a guest_address for a token.

For now, PHB BUID can't be "0". Originally, I was planing to have some code in QEMU to have unique PHB BUID across the system so that guest_address could be the unique token. But I'm going to remove the address mapping in next revision as Alex.G suggested.

Quoted 17 lines
Quoted 17 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +17 -0
+		eeh_dev_set_passed(edev, false);
+	}
+
+	memset(&pe->guest_addr, 0, sizeof(pe->guest_addr));
+	eeh_pe_set_passed(pe, false);
+}
+EXPORT_SYMBOL(eeh_vfio_release);
+
+int eeh_vfio_ioctl(unsigned long arg)
+{
+	struct vfio_eeh_info info;
+	int ret = -EINVAL;
+
+	/* Copy over user argument */
+	if (copy_from_user(&info, (void __user *)arg, sizeof(info))) {
+		pr_warn("%s: Cannot copy user argument 0x%lx\n",
+			__func__, arg);
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 550

Quoted 5 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +5 -0
+		return -EFAULT;
+	}
+
+	/* Sanity check */
+	if (info.argsz != sizeof(info)) {

This breaks compatibility if you need to later add a new ops with a larger footprint.

Ok. I'll fix it in next revision. Thanks for pointing it out.

Quoted 59 lines
Quoted 59 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +43 -0
+		pr_warn("%s: Invalid argument size (%d, %ld)\n",
+			__func__, info.argsz, sizeof(info));
+		return -EINVAL;
+	}
+
+	/* Route according to operation */
+	switch (info.op) {
+	case VFIO_EEH_OP_MAP:
+		ret = powernv_eeh_vfio_map(&info);
+		break;
+	case VFIO_EEH_OP_UNMAP:
+		ret = powernv_eeh_vfio_unmap(&info);
+		break;
+	case VFIO_EEH_OP_SET_OPTION:
+		ret = powernv_eeh_vfio_set_option(&info);
+		break;
+	case VFIO_EEH_OP_GET_ADDR:
+		ret = powernv_eeh_vfio_get_addr(&info);
+		break;
+	case VFIO_EEH_OP_GET_STATE:
+		ret = powernv_eeh_vfio_get_state(&info);
+		break;
+	case VFIO_EEH_OP_PE_RESET:
+		ret = powernv_eeh_vfio_pe_reset(&info);
+		break;
+	case VFIO_EEH_OP_PE_CONFIG:
+		ret = powernv_eeh_vfio_pe_config(&info);
+		break;
+	default:
+		pr_info("%s: Cannot handle op#%d\n",
+			__func__, info.op);
+	}
+
+	/* Copy data back */
+	if (!ret && copy_to_user((void __user *)arg, &info, sizeof(info))) {
+		pr_warn("%s: Cannot copy to user 0x%lx\n",
+			__func__, arg);
+		return -EFAULT;
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(eeh_vfio_ioctl);
drivers/vfio/vfio_iommu_spapr_tce.c +5 -0
index a84788b..c45dece 100644
@@ -26,6 +26,11 @@
 #define DRIVER_AUTHOR   "[email protected]"
 #define DRIVER_DESC     "VFIO IOMMU SPAPR TCE"
 
+#ifdef CONFIG_VFIO_EEH
+extern void eeh_vfio_release(struct iommu_table *tbl);
+extern int eeh_vfio_ioctl(unsigned long arg);
+#endif
+
 static void tce_iommu_detach_group(void *iommu_data,
 		struct iommu_group *iommu_group);
 
Quoted 9 lines

drivers/vfio/vfio_iommu_spapr_tce.c static long tce_iommu_ioctl(void *iommu_data, · line 291

Quoted 5 lines
drivers/vfio/vfio_iommu_spapr_tce.c +1 -0
@@ -283,6 +288,10 @@ static long tce_iommu_ioctl(void *iommu_data,
 		tce_iommu_disable(container);
 		mutex_unlock(&container->lock);
 		return 0;
+#ifdef CONFIG_VFIO_EEH

I'm not a fan of all these #ifdefs, hide it in eeh_vfio_ioctl() and eeh_vfio_release() if needed.

Ok. Will do it in next revision.

Quoted 24 lines
Quoted 24 lines
drivers/vfio/vfio_iommu_spapr_tce.c +6 -0
+	case VFIO_EEH_INFO:
+		return eeh_vfio_ioctl(arg);
+#endif
 	}
 
 	return -ENOTTY;
@@ -342,6 +351,9 @@ static void tce_iommu_detach_group(void *iommu_data,
 		/* pr_debug("tce_vfio: detaching group #%u from iommu %p\n",
 				iommu_group_id(iommu_group), iommu_group); */
 		container->tbl = NULL;
+#ifdef CONFIG_VFIO_EEH
+		eeh_vfio_release(tbl);
+#endif
 		iommu_release_ownership(tbl);
 	}
 	mutex_unlock(&container->lock);
include/uapi/linux/vfio.h +0 -0
index cb9023d..1fd1bfb 100644
@@ -455,6 +455,63 @@ struct vfio_iommu_spapr_tce_info {
 
 #define VFIO_IOMMU_SPAPR_TCE_GET_INFO	_IO(VFIO_TYPE, VFIO_BASE + 12)
 
Quoted 14 lines

include/uapi/linux/vfio.h struct vfio_iommu_spapr_tce_info { · line 468

Quoted 11 lines
include/uapi/linux/vfio.h +11 -0
+/*
+ * The VFIO EEH info struct provides way to support EEH functionality
+ * for PCI device that is passed from host to guest via VFIO.
+ */
+#define VFIO_EEH_OP_MAP		0
+#define VFIO_EEH_OP_UNMAP	1
+#define VFIO_EEH_OP_SET_OPTION	2
+#define VFIO_EEH_OP_GET_ADDR	3
+#define VFIO_EEH_OP_GET_STATE	4
+#define VFIO_EEH_OP_PE_RESET	5
+#define VFIO_EEH_OP_PE_CONFIG	6

Is this really an "info" ioctl?

Yeah, "VFIO_EEH_INFO" isn't a good name. How about to have "VFIO_EEH_HANDLER" ?

Quoted 48 lines
Quoted 48 lines
include/uapi/linux/vfio.h +46 -0
+
+struct vfio_eeh_info {
+	__u32 argsz;
+	__u32 op;
+
+	union {
+		struct vfio_eeh_map {
+			__u32 host_domain;
+			__u16 host_cfg_addr;
+			__u64 guest_buid;
+			__u16 guest_cfg_addr;
+		} map;
+		struct vfio_eeh_unmap {
+			__u64 buid;
+			__u16 cfg_addr;
+		} unmap;
+		struct vfio_eeh_set_option {
+			__u64 buid;
+			__u32 addr;
+			__u32 option;
+		} option;
+		struct vfio_eeh_pe_addr {
+			__u64 buid;
+			__u32 cfg_addr;
+			__u32 option;
+			__u32 ret;
+		} addr;
+		struct vfio_eeh_state {
+			__u64 buid;
+			__u32 pe_addr;
+			__u32 state;
+                } state;
+		struct vfio_eeh_reset {
+			__u64 buid;
+			__u32 pe_addr;
+			__u32 option;
+		} reset;
+		struct vfio_eeh_config {
+			__u64 buid;
+			__u32 pe_addr;
+		} config;
+	};
+};
+
+#define VFIO_EEH_INFO	_IO(VFIO_TYPE, VFIO_BASE + 21)
+
 /* ***************************************************************** */
 
Quoted 1 line

include/uapi/linux/vfio.h struct vfio_iommu_spapr_tce_info { · line 517

Quoted 1 line
include/uapi/linux/vfio.h +0 -0
 #endif /* _UAPIVFIO_H */

Thanks,
Gavin

#12 lore raw

Re: [PATCH 3/8] drivers/vfio: New IOCTL command VFIO_EEH_INFO

Quoted 247 lines · Gavin Shan
Quoted 4 lines · Alex Williamson
Quoted 3 lines · Gavin Shan

The patch adds new IOCTL command VFIO_EEH_INFO to VFIO container to support EEH functionality for PCI devices, which have been passed from host to guest via VFIO.

Thanks for your comments, Alex.W :-)

Quoted 6 lines

Some comments throughout, but overall this seems to forgo every bit of the device ownership and protection model used by VFIO and lets the user pick arbitrary host devices and do various operations, mostly unchecked. That's not acceptable.

As what I replied to patch[2], I'm going to let VFIO-PCI-dev fd handle the newly introduced IOCTL command. That way, we should follow the VFIO design principles (ownership and protection) because VFIO-PCI-dev fd is owned by QEMU process usually.

Also, the address mapping maintained in EEH will be removed.

Quoted 70 lines
Quoted 70 lines
---
 arch/powerpc/platforms/powernv/Makefile   |   1 +
 arch/powerpc/platforms/powernv/eeh-vfio.c | 593 ++++++++++++++++++++++++++++++
 drivers/vfio/vfio_iommu_spapr_tce.c       |  12 +
 include/uapi/linux/vfio.h                 |  57 +++
 4 files changed, 663 insertions(+)
 create mode 100644 arch/powerpc/platforms/powernv/eeh-vfio.c
arch/powerpc/platforms/powernv/Makefile +1 -0
index 63cebb9..2b15a03 100644
@@ -6,5 +6,6 @@ obj-y			+= opal-msglog.o
 obj-$(CONFIG_SMP)	+= smp.o
 obj-$(CONFIG_PCI)	+= pci.o pci-p5ioc2.o pci-ioda.o
 obj-$(CONFIG_EEH)	+= eeh-ioda.o eeh-powernv.o
+obj-$(CONFIG_VFIO_EEH)	+= eeh-vfio.o
 obj-$(CONFIG_PPC_SCOM)	+= opal-xscom.o
 obj-$(CONFIG_MEMORY_FAILURE)	+= opal-memory-errors.o
arch/powerpc/platforms/powernv/eeh-vfio.c new file +44 -0
new file mode 100644
index 0000000..69d5f2d
@@ -0,0 +1,593 @@
+/*
+  * The file intends to support EEH funtionality for those PCI devices,
+  * which have been passed through from host to guest via VFIO. So this
+  * file is naturally part of VFIO implementation on PowerNV platform.
+  *
+  * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2014.
+  *
+  * This program is free software; you can redistribute it and/or modify
+  * it under the terms of the GNU General Public License as published by
+  * the Free Software Foundation; either version 2 of the License, or
+  * (at your option) any later version.
+  */
+
+#include <linux/init.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/kernel.h>
+#include <linux/kvm_host.h>
+#include <linux/msi.h>
+#include <linux/pci.h>
+#include <linux/string.h>
+#include <linux/vfio.h>
+
+#include <asm/eeh.h>
+#include <asm/eeh_event.h>
+#include <asm/io.h>
+#include <asm/iommu.h>
+#include <asm/opal.h>
+#include <asm/msi_bitmap.h>
+#include <asm/pci-bridge.h>
+#include <asm/ppc-pci.h>
+#include <asm/tce.h>
+#include <asm/uaccess.h>
+
+#include "powernv.h"
+#include "pci.h"
+
+static int powernv_eeh_vfio_map(struct vfio_eeh_info *info)
+{
+	struct pci_bus *bus, *pe_bus;
+	struct pci_dev *pdev;
+	struct eeh_dev *edev;
+	struct eeh_pe *pe;
+	int domain, bus_no, devfn;
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 49

Quoted 5 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +5 -0
+
+	/* Host address */
+	domain = info->map.host_domain;
+	bus_no = (info->map.host_cfg_addr >> 8) & 0xff;
+	devfn = info->map.host_cfg_addr & 0xff;

Where are we validating that the user has any legitimate claim to be touching this device?

I'll let VFIO-PCI-dev fd handle the IOCTL command. With that, we shouldn't have the problem.

Quoted 55 lines
Quoted 21 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +21 -0
+	/* Find PCI bus */
+	bus = pci_find_bus(domain, bus_no);
+	if (!bus) {
+		pr_warn("%s: PCI bus %04x:%02x not found\n",
+			__func__, domain, bus_no);
+		return -ENODEV;
+	}
+
+	/* Find PCI device */
+	pdev = pci_get_slot(bus, devfn);
+	if (!pdev) {
+		pr_warn("%s: PCI device %04x:%02x:%02x.%01x not found\n",
+			__func__, domain, bus_no,
+			PCI_SLOT(devfn), PCI_FUNC(devfn));
+		return -ENODEV;
+	}
+
+	/* No EEH device - almost impossible */
+	edev = pci_dev_to_eeh_dev(pdev);
+	if (unlikely(!edev)) {
+		pci_dev_put(pdev);

arch/powerpc/platforms/powernv/eeh-vfio.c line 81

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +10 -0
+		pr_warn("%s: No EEH dev for PCI device %s\n",
+			__func__, pci_name(pdev));
+		return -ENODEV;
+	}
+
+	/* Doesn't support PE migration between different PHBs */
+	pe = edev->pe;
+	if (!eeh_pe_passed(pe)) {
+		pe_bus = eeh_pe_bus_get(pe);
+		BUG_ON(!pe_bus);

Can a user trigger this maliciously?

Quoted 21 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +21 -0
+
+		/* PE# has format 00BBSS00 */
+		pe->guest_addr.buid    = info->map.guest_buid;
+		pe->guest_addr.pe_addr = pe_bus->number << 16;
+		eeh_pe_set_passed(pe, true);
+	} else if (pe->guest_addr.buid != info->map.guest_buid) {
+		pci_dev_put(pdev);
+		pr_warn("%s: Mismatched PHB BUID (0x%llx, 0x%llx)\n",
+			__func__, pe->guest_addr.buid, info->map.guest_buid);
+		return -EINVAL;
+	}
+
+	edev->guest_addr.buid = info->map.guest_buid;
+	edev->guest_addr.config_addr = info->map.guest_cfg_addr;
+	eeh_dev_set_passed(edev, true);
+
+	pr_debug("EEH: Host PCI dev %s to %llx-%02x:%02x.%01x\n",
+		 pci_name(pdev), info->map.guest_buid,
+		 (info->map.guest_cfg_addr >> 8) & 0xFF,
+		 PCI_SLOT(info->map.guest_cfg_addr & 0xFF),
+		 PCI_FUNC(info->map.guest_cfg_addr & 0xFF));
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 106

Quoted 4 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +4 -0
+
+	pci_dev_put(pdev);
+	return 0;
+}

So the effect of this function is that a user gets to setup an arbitrary guest mapping for an arbitrary host device and associated pe. Is that right? It seems bad.

I'm going to remove this mapping in next revision.

Quoted 65 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 119

Quoted 13 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +13 -0
+
+static int powernv_eeh_vfio_unmap(struct vfio_eeh_info *info)
+{
+	struct eeh_vfio_pci_addr addr;
+	struct pci_dev *pdev;
+	struct eeh_dev *edev, *tmp;
+	struct eeh_pe *pe;
+	bool passed;
+
+	/* Get EEH device */
+	addr.buid = info->unmap.buid;
+	addr.config_addr = info->unmap.cfg_addr;
+	edev = eeh_vfio_dev_get(&addr);

eeh_vfio_dev_get() just looks for a "passed" dev and a match for a well known address space. Seems very exploitable.

Quoted 48 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +48 -0
+	if (!edev) {
+		pr_warn("%s: Cannot find %llx:%02x:%02x.%01x\n",
+			__func__, info->unmap.buid,
+			(info->unmap.cfg_addr >> 8) & 0xFF,
+			PCI_SLOT(info->unmap.cfg_addr & 0xFF),
+			PCI_FUNC(info->unmap.cfg_addr & 0xFF));
+		return -ENODEV;
+	}
+
+	/* Return EEH device */
+	memset(&edev->guest_addr, 0, sizeof(edev->guest_addr));
+	eeh_dev_set_passed(edev, false);
+	pdev = eeh_dev_to_pci_dev(edev);
+	pr_debug("EEH: Host PCI dev %s returned\n",
+		 pdev ? pci_name(pdev) : "NULL");
+
+	/* Return PE if no EEH device is owned by guest */
+	pe = edev->pe;
+	passed = false;
+	eeh_pe_for_each_dev(pe, edev, tmp) {
+		pdev = eeh_dev_to_pci_dev(edev);
+		if (pdev && pdev->subordinate)
+			continue;
+
+		if (eeh_dev_passed(edev)) {
+			passed = true;
+			break;
+		}
+	}
+
+	if (!passed) {
+		memset(&pe->guest_addr, 0, sizeof(pe->guest_addr));
+		eeh_pe_set_passed(pe, false);
+		pr_debug("EEH: PHB#%x-PE#%x returned to host\n",
+			 pe->phb->global_number, pe->addr);
+	}
+
+	return 0;
+}
+
+static int powernv_eeh_vfio_set_option(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_dev *edev;
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int opcode = info->option.option;
+	int ret = 0;
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 173

Quoted 6 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +6 -0
+
+	/* Check opcode */
+	if (opcode < EEH_OPT_DISABLE || opcode > EEH_OPT_THAW_DMA) {
+		pr_warn("%s: opcode %d out of range (%d, %d)\n",
+			__func__, opcode, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);
+		ret = 3;

Please don't make up arbitrary return values.

Quoted 1 line

Nope, it will be turned to "-3" eventually by QEMU.

Don't assume QEMU is your userspace.

Quoted 2 lines

That means "Invalid Parameter"
defined in PAPR spec.

Is there value in matching the PAPR spec (which most people can't read)? If there is...

Quoted 5 lines

The IOCTL command handler return 3 values:

< 0: Linux kernel error. For example, error from copy_from_user().

Quoted 1 line

0: Error code to the EEH RTAS request, which will be returned to guest.

= 0: Success

Maybe the ioctl return should match normal ioctl return values and the EEH error code can be stored somewhere in the structure.

Quoted 510 lines
Quoted 200 lines
Quoted 200 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +200 -0
+		goto out;
+	}
+
+	/* Option "enable" uses PCI config address */
+	if (opcode = EEH_OPT_ENABLE) {
+		addr.buid = info->option.buid;
+		addr.config_addr = (info->option.addr >> 8) & 0xFFFF;
+		edev = eeh_vfio_dev_get(&addr);
+		if (!edev) {
+			pr_warn("%s: Cannot find %llx:%02x:%02x.%01x\n",
+				__func__, addr.buid,
+				(addr.config_addr >> 8) & 0xFF,
+				PCI_SLOT(addr.config_addr & 0xFF),
+				PCI_FUNC(addr.config_addr & 0xFF));
+			ret = 7;
+			goto out;
+		}
+		phb = edev->phb->private_data;
+	} else {
+		addr.buid    = info->option.buid;
+		addr.pe_addr = info->option.addr;
+		pe = eeh_vfio_pe_get(&addr);
+		if (!pe) {
+			pr_warn("%s: Cannot find PE %llx:%x\n",
+				__func__, addr.buid, addr.pe_addr);
+			ret = 7;
+			goto out;
+		}
+		phb = pe->phb->private_data;
+	}
+
+	/* Insure that the EEH stuff has been initialized */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 7;
+		goto out;
+	}
+
+	/*
+	 * The EEH functionality has been enabled on all PEs
+	 * by default. So just return success. The same situation
+	 * would be applied while we disable EEH functionality.
+	 * However, the guest isn't expected to disable that
+	 * at all.
+	 */
+	if (opcode = EEH_OPT_DISABLE ||
+	    opcode = EEH_OPT_ENABLE) {
+		ret = 0;
+		goto out;
+	}
+
+	/*
+	 * Call into the IODA dependent backend in order
+	 * to enable DMA or MMIO for the indicated PE.
+	 */
+	if (phb->eeh_ops && phb->eeh_ops->set_option) {
+		if (phb->eeh_ops->set_option(pe, opcode)) {
+			pr_warn("%s: Failure from backend\n",
+				__func__);
+			ret = 1;
+		}
+	} else {
+		pr_warn("%s: Unsupported request\n",
+			__func__);
+		ret = 7;
+	}
+
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_get_addr(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_dev *edev;
+	struct eeh_vfio_pci_addr addr;
+	int opcode = info->addr.option;
+	int ret = 0;
+
+	/* Check opcode */
+	if (opcode != 0 && opcode != 1) {
+		pr_warn("%s: opcode %d out of range (0, 1)\n",
+			__func__, opcode);
+		ret = 3;
+		goto out;
+	}
+
+	/* Find EEH device */
+	addr.buid = info->addr.buid;
+	addr.config_addr = (info->addr.cfg_addr >> 8 ) & 0xFFFF;
+	edev = eeh_vfio_dev_get(&addr);
+	if (!edev) {
+		pr_warn("%s: Cannot find %llx:%02x:%02x.%01x\n",
+			__func__, addr.buid,
+			(addr.config_addr >> 8) & 0xFF,
+			PCI_SLOT(addr.config_addr & 0xFF),
+			PCI_FUNC(addr.config_addr & 0xFF));
+		ret = 7;
+		goto out;
+	}
+	phb = edev->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+	}
+
+	/* EEH device passed ? */
+	if (!eeh_dev_passed(edev)) {
+		pr_warn("%s: EEH dev %llx:%02x:%02x.%01x owned by host\n",
+			__func__, addr.buid,
+			(addr.config_addr >> 8) & 0xFF,
+			PCI_SLOT(addr.config_addr & 0xFF),
+			PCI_FUNC(addr.config_addr & 0xFF));
+		ret = 3;
+		goto out;
+	}
+
+	/*
+	 * Fill result according to opcode. We don't differentiate
+	 * PCI bus and device sensitive PE here.
+	 */
+	if (opcode = 0)
+		info->addr.ret = edev->pe->guest_addr.pe_addr;
+	else
+		info->addr.ret = 1;
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_get_state(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int result, ret = 0;
+
+	/* Locate the PE */
+	addr.buid    = info->state.buid;
+	addr.pe_addr = info->state.pe_addr;
+	pe = eeh_vfio_pe_get(&addr);
+	if (!pe) {
+		pr_warn("%s: Cannot locate %llx:%x\n",
+			__func__, addr.buid, addr.pe_addr);
+		ret = 3;
+		goto out;
+	}
+	phb = pe->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+	}
+
+	/* Call to the IOC dependent function */
+	if (phb->eeh_ops && phb->eeh_ops->get_state) {
+		result = phb->eeh_ops->get_state(pe);
+
+		if (!(result & EEH_STATE_RESET_ACTIVE) &&
+		     (result & EEH_STATE_DMA_ENABLED) &&
+		     (result & EEH_STATE_MMIO_ENABLED))
+			info->state.state = 0;
+		else if (result & EEH_STATE_RESET_ACTIVE)
+			info->state.state = 1;
+		else if (!(result & EEH_STATE_RESET_ACTIVE) &&
+			 !(result & EEH_STATE_DMA_ENABLED) &&
+			 !(result & EEH_STATE_MMIO_ENABLED))
+			info->state.state = 2;
+		else if (!(result & EEH_STATE_RESET_ACTIVE) &&
+			 (result & EEH_STATE_DMA_ENABLED) &&
+			 !(result & EEH_STATE_MMIO_ENABLED))
+			info->state.state = 4;
+		else
+			info->state.state = 5;
+
+		ret = 0;
+	} else {
+		pr_warn("%s: Unsupported request\n", __func__);
+		ret = 3;
+	}
+
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_pe_reset(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int opcode = info->reset.option;
+	int ret = 0;
+
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 379

Quoted 6 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +6 -0
+	/* Check opcode */
+	if (opcode != EEH_RESET_DEACTIVATE &&
+	    opcode != EEH_RESET_HOT &&
+	    opcode != EEH_RESET_FUNDAMENTAL) {
+		pr_warn("%s: Unsupported opcode %d\n",
+			__func__, opcode);

Console warnings are exploitable DoS attacks.

Yep. I'll change all pr_warn() to pr_debug() in next revision.

Quoted 144 lines
Quoted 144 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +144 -0
+		ret = 3;
+		goto out;
+	}
+
+	/* Locate the PE */
+	addr.buid    = info->reset.buid;
+	addr.pe_addr = info->reset.pe_addr;
+	pe = eeh_vfio_pe_get(&addr);
+	if (!pe) {
+		pr_warn("%s: Cannot locate %llx:%x\n",
+			__func__, addr.buid, addr.pe_addr);
+		ret = 3;
+		goto out;
+	}
+	phb = pe->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+	}
+
+	/* Call into the IODA dependent backend to do the reset */
+	if (!phb->eeh_ops ||
+	    !phb->eeh_ops->set_option ||
+	    !phb->eeh_ops->reset) {
+		pr_warn("%s: Unsupported request\n",
+			__func__);
+		ret = 7;
+	} else {
+		/*
+		 * The frozen PE might be caused by the mechanism called
+		 * PAPR error injection, which is supposed to be one-shot
+		 * without "sticky" bit as being stated by the spec. But
+		 * the reality isn't that, at least on P7IOC. So we have
+		 * to clear that to avoid recrusive error, which fails the
+		 * recovery eventually.
+		 */
+		if (opcode = EEH_RESET_DEACTIVATE)
+			opal_pci_reset(phb->opal_id,
+				       OPAL_PHB_ERROR,
+				       OPAL_ASSERT_RESET);
+
+		if (phb->eeh_ops->reset(pe, opcode)) {
+			pr_warn("%s: Failure from backend\n", __func__);
+			ret = 1;
+			goto out;
+		}
+
+		/*
+		 * The PE is still in frozen state and we need clear that.
+		 * It's good to clear frozen state after deassert to avoid
+		 * messy IO access during reset, which might cause recrusive
+		 * frozen PE.
+		 */
+		if (opcode = EEH_RESET_DEACTIVATE) {
+			if (phb->eeh_ops->set_option(pe, EEH_OPT_THAW_MMIO) ||
+			    phb->eeh_ops->set_option(pe, EEH_OPT_THAW_DMA)) {
+				pr_warn("%s: Cannot clear frozen state\n",
+					__func__);
+				ret = 1;
+			}
+
+			eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
+		}
+	}
+
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_pe_config(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int ret = 0;
+
+	/* Locate the PE */
+	addr.buid    = info->config.buid;
+	addr.pe_addr = info->config.pe_addr;
+	pe = eeh_vfio_pe_get(&addr);
+	if (!pe) {
+		pr_warn("%s: Cannot locate %llx:%x\n",
+			__func__, addr.buid, addr.pe_addr);
+		ret = 3;
+		goto out;
+	}
+	phb = pe->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+        }
+
+	/*
+	 * The access to PCI config space on VFIO device has some
+	 * limitations. Part of PCI config space, including BAR
+	 * registers are not readable and writable. So the guest
+	 * should have stale values for those registers and we have
+	 * to restore them in host side.
+	 */
+	eeh_pe_restore_bars(pe);
+out:
+	return ret;
+}
+
+void eeh_vfio_release(struct iommu_table *tbl)
+{
+	struct pnv_ioda_pe *pnv_pe = container_of(tbl, struct pnv_ioda_pe,
+						  tce32_table);
+	struct pnv_phb *phb = pnv_pe->phb;
+	struct eeh_pe *phb_pe, *pe;
+	struct eeh_dev dev, *edev, *tmp;
+
+	/* Find PHB PE */
+	phb_pe = eeh_phb_pe_get(phb->hose);
+	if (unlikely(!phb_pe)) {
+		pr_warn("%s: Cannot find PHB#%d PE\n",
+			__func__, phb->hose->global_number);
+		return;
+	}
+
+	/* Find PE */
+	memset(&dev, 0, sizeof(struct eeh_dev));
+	dev.phb = phb->hose;
+	dev.pe_config_addr = pnv_pe->pe_number;
+	pe = eeh_pe_get(&dev);
+	if (unlikely(!pe)) {
+		pr_warn("%s: Cannot find PE instance for PHB#%d-PE#%d\n",
+			__func__, phb->hose->global_number,
+			pnv_pe->pe_number);
+		return;
+	}
+
+	/* Release it to host */
+	if (!eeh_pe_passed(pe))
+		return;
+
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 528

Quoted 5 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +5 -0
+	eeh_pe_for_each_dev(pe, edev, tmp) {
+		if (!eeh_dev_passed(edev))
+			continue;
+
+		memset(&edev->guest_addr, 0, sizeof(edev->guest_addr));

Is guest_addr = { 0 } not valid? As agraf already mentioned, there are a number of issues with using a guest_address for a token.

For now, PHB BUID can't be "0". Originally, I was planing to have some code in QEMU to have unique PHB BUID across the system so that guest_address could be the unique token. But I'm going to remove the address mapping in next revision as Alex.G suggested.

Quoted 17 lines
Quoted 17 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +17 -0
+		eeh_dev_set_passed(edev, false);
+	}
+
+	memset(&pe->guest_addr, 0, sizeof(pe->guest_addr));
+	eeh_pe_set_passed(pe, false);
+}
+EXPORT_SYMBOL(eeh_vfio_release);
+
+int eeh_vfio_ioctl(unsigned long arg)
+{
+	struct vfio_eeh_info info;
+	int ret = -EINVAL;
+
+	/* Copy over user argument */
+	if (copy_from_user(&info, (void __user *)arg, sizeof(info))) {
+		pr_warn("%s: Cannot copy user argument 0x%lx\n",
+			__func__, arg);
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 550

Quoted 5 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +5 -0
+		return -EFAULT;
+	}
+
+	/* Sanity check */
+	if (info.argsz != sizeof(info)) {

This breaks compatibility if you need to later add a new ops with a larger footprint.

Ok. I'll fix it in next revision. Thanks for pointing it out.

Quoted 59 lines
Quoted 59 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +43 -0
+		pr_warn("%s: Invalid argument size (%d, %ld)\n",
+			__func__, info.argsz, sizeof(info));
+		return -EINVAL;
+	}
+
+	/* Route according to operation */
+	switch (info.op) {
+	case VFIO_EEH_OP_MAP:
+		ret = powernv_eeh_vfio_map(&info);
+		break;
+	case VFIO_EEH_OP_UNMAP:
+		ret = powernv_eeh_vfio_unmap(&info);
+		break;
+	case VFIO_EEH_OP_SET_OPTION:
+		ret = powernv_eeh_vfio_set_option(&info);
+		break;
+	case VFIO_EEH_OP_GET_ADDR:
+		ret = powernv_eeh_vfio_get_addr(&info);
+		break;
+	case VFIO_EEH_OP_GET_STATE:
+		ret = powernv_eeh_vfio_get_state(&info);
+		break;
+	case VFIO_EEH_OP_PE_RESET:
+		ret = powernv_eeh_vfio_pe_reset(&info);
+		break;
+	case VFIO_EEH_OP_PE_CONFIG:
+		ret = powernv_eeh_vfio_pe_config(&info);
+		break;
+	default:
+		pr_info("%s: Cannot handle op#%d\n",
+			__func__, info.op);
+	}
+
+	/* Copy data back */
+	if (!ret && copy_to_user((void __user *)arg, &info, sizeof(info))) {
+		pr_warn("%s: Cannot copy to user 0x%lx\n",
+			__func__, arg);
+		return -EFAULT;
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(eeh_vfio_ioctl);
drivers/vfio/vfio_iommu_spapr_tce.c +5 -0
index a84788b..c45dece 100644
@@ -26,6 +26,11 @@
 #define DRIVER_AUTHOR   "[email protected]"
 #define DRIVER_DESC     "VFIO IOMMU SPAPR TCE"
 
+#ifdef CONFIG_VFIO_EEH
+extern void eeh_vfio_release(struct iommu_table *tbl);
+extern int eeh_vfio_ioctl(unsigned long arg);
+#endif
+
 static void tce_iommu_detach_group(void *iommu_data,
 		struct iommu_group *iommu_group);
 
Quoted 9 lines

drivers/vfio/vfio_iommu_spapr_tce.c static long tce_iommu_ioctl(void *iommu_data, · line 291

Quoted 5 lines
drivers/vfio/vfio_iommu_spapr_tce.c +1 -0
@@ -283,6 +288,10 @@ static long tce_iommu_ioctl(void *iommu_data,
 		tce_iommu_disable(container);
 		mutex_unlock(&container->lock);
 		return 0;
+#ifdef CONFIG_VFIO_EEH

I'm not a fan of all these #ifdefs, hide it in eeh_vfio_ioctl() and eeh_vfio_release() if needed.

Ok. Will do it in next revision.

Quoted 24 lines
Quoted 24 lines
drivers/vfio/vfio_iommu_spapr_tce.c +6 -0
+	case VFIO_EEH_INFO:
+		return eeh_vfio_ioctl(arg);
+#endif
 	}
 
 	return -ENOTTY;
@@ -342,6 +351,9 @@ static void tce_iommu_detach_group(void *iommu_data,
 		/* pr_debug("tce_vfio: detaching group #%u from iommu %p\n",
 				iommu_group_id(iommu_group), iommu_group); */
 		container->tbl = NULL;
+#ifdef CONFIG_VFIO_EEH
+		eeh_vfio_release(tbl);
+#endif
 		iommu_release_ownership(tbl);
 	}
 	mutex_unlock(&container->lock);
include/uapi/linux/vfio.h +0 -0
index cb9023d..1fd1bfb 100644
@@ -455,6 +455,63 @@ struct vfio_iommu_spapr_tce_info {
 
 #define VFIO_IOMMU_SPAPR_TCE_GET_INFO	_IO(VFIO_TYPE, VFIO_BASE + 12)
 
Quoted 14 lines

include/uapi/linux/vfio.h struct vfio_iommu_spapr_tce_info { · line 468

Quoted 11 lines
include/uapi/linux/vfio.h +11 -0
+/*
+ * The VFIO EEH info struct provides way to support EEH functionality
+ * for PCI device that is passed from host to guest via VFIO.
+ */
+#define VFIO_EEH_OP_MAP		0
+#define VFIO_EEH_OP_UNMAP	1
+#define VFIO_EEH_OP_SET_OPTION	2
+#define VFIO_EEH_OP_GET_ADDR	3
+#define VFIO_EEH_OP_GET_STATE	4
+#define VFIO_EEH_OP_PE_RESET	5
+#define VFIO_EEH_OP_PE_CONFIG	6

Is this really an "info" ioctl?

Quoted 1 line

Yeah, "VFIO_EEH_INFO" isn't a good name. How about to have "VFIO_EEH_HANDLER" ?

VFIO_EEH_OP perhaps. Thanks,

Alex

Quoted 53 lines
Quoted 48 lines
Quoted 48 lines
include/uapi/linux/vfio.h +46 -0
+
+struct vfio_eeh_info {
+	__u32 argsz;
+	__u32 op;
+
+	union {
+		struct vfio_eeh_map {
+			__u32 host_domain;
+			__u16 host_cfg_addr;
+			__u64 guest_buid;
+			__u16 guest_cfg_addr;
+		} map;
+		struct vfio_eeh_unmap {
+			__u64 buid;
+			__u16 cfg_addr;
+		} unmap;
+		struct vfio_eeh_set_option {
+			__u64 buid;
+			__u32 addr;
+			__u32 option;
+		} option;
+		struct vfio_eeh_pe_addr {
+			__u64 buid;
+			__u32 cfg_addr;
+			__u32 option;
+			__u32 ret;
+		} addr;
+		struct vfio_eeh_state {
+			__u64 buid;
+			__u32 pe_addr;
+			__u32 state;
+                } state;
+		struct vfio_eeh_reset {
+			__u64 buid;
+			__u32 pe_addr;
+			__u32 option;
+		} reset;
+		struct vfio_eeh_config {
+			__u64 buid;
+			__u32 pe_addr;
+		} config;
+	};
+};
+
+#define VFIO_EEH_INFO	_IO(VFIO_TYPE, VFIO_BASE + 21)
+
 /* ***************************************************************** */
 
Quoted 1 line

include/uapi/linux/vfio.h struct vfio_iommu_spapr_tce_info { · line 517

Quoted 1 line
include/uapi/linux/vfio.h +0 -0
 #endif /* _UAPIVFIO_H */

Thanks,
Gavin

author
#13 lore raw

Re: [PATCH 3/8] drivers/vfio: New IOCTL command VFIO_EEH_INFO

Quoted 258 lines · Alex Williamson
Quoted 247 lines · Gavin Shan
Quoted 4 lines · Alex Williamson
Quoted 3 lines · Gavin Shan

The patch adds new IOCTL command VFIO_EEH_INFO to VFIO container to support EEH functionality for PCI devices, which have been passed from host to guest via VFIO.

Thanks for your comments, Alex.W :-)

Quoted 6 lines

Some comments throughout, but overall this seems to forgo every bit of the device ownership and protection model used by VFIO and lets the user pick arbitrary host devices and do various operations, mostly unchecked. That's not acceptable.

As what I replied to patch[2], I'm going to let VFIO-PCI-dev fd handle the newly introduced IOCTL command. That way, we should follow the VFIO design principles (ownership and protection) because VFIO-PCI-dev fd is owned by QEMU process usually.

Also, the address mapping maintained in EEH will be removed.

Quoted 70 lines
Quoted 70 lines
---
 arch/powerpc/platforms/powernv/Makefile   |   1 +
 arch/powerpc/platforms/powernv/eeh-vfio.c | 593 ++++++++++++++++++++++++++++++
 drivers/vfio/vfio_iommu_spapr_tce.c       |  12 +
 include/uapi/linux/vfio.h                 |  57 +++
 4 files changed, 663 insertions(+)
 create mode 100644 arch/powerpc/platforms/powernv/eeh-vfio.c
arch/powerpc/platforms/powernv/Makefile +1 -0
index 63cebb9..2b15a03 100644
@@ -6,5 +6,6 @@ obj-y			+= opal-msglog.o
 obj-$(CONFIG_SMP)	+= smp.o
 obj-$(CONFIG_PCI)	+= pci.o pci-p5ioc2.o pci-ioda.o
 obj-$(CONFIG_EEH)	+= eeh-ioda.o eeh-powernv.o
+obj-$(CONFIG_VFIO_EEH)	+= eeh-vfio.o
 obj-$(CONFIG_PPC_SCOM)	+= opal-xscom.o
 obj-$(CONFIG_MEMORY_FAILURE)	+= opal-memory-errors.o
arch/powerpc/platforms/powernv/eeh-vfio.c new file +44 -0
new file mode 100644
index 0000000..69d5f2d
@@ -0,0 +1,593 @@
+/*
+  * The file intends to support EEH funtionality for those PCI devices,
+  * which have been passed through from host to guest via VFIO. So this
+  * file is naturally part of VFIO implementation on PowerNV platform.
+  *
+  * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2014.
+  *
+  * This program is free software; you can redistribute it and/or modify
+  * it under the terms of the GNU General Public License as published by
+  * the Free Software Foundation; either version 2 of the License, or
+  * (at your option) any later version.
+  */
+
+#include <linux/init.h>
+#include <linux/io.h>
+#include <linux/irq.h>
+#include <linux/kernel.h>
+#include <linux/kvm_host.h>
+#include <linux/msi.h>
+#include <linux/pci.h>
+#include <linux/string.h>
+#include <linux/vfio.h>
+
+#include <asm/eeh.h>
+#include <asm/eeh_event.h>
+#include <asm/io.h>
+#include <asm/iommu.h>
+#include <asm/opal.h>
+#include <asm/msi_bitmap.h>
+#include <asm/pci-bridge.h>
+#include <asm/ppc-pci.h>
+#include <asm/tce.h>
+#include <asm/uaccess.h>
+
+#include "powernv.h"
+#include "pci.h"
+
+static int powernv_eeh_vfio_map(struct vfio_eeh_info *info)
+{
+	struct pci_bus *bus, *pe_bus;
+	struct pci_dev *pdev;
+	struct eeh_dev *edev;
+	struct eeh_pe *pe;
+	int domain, bus_no, devfn;
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 49

Quoted 5 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +5 -0
+
+	/* Host address */
+	domain = info->map.host_domain;
+	bus_no = (info->map.host_cfg_addr >> 8) & 0xff;
+	devfn = info->map.host_cfg_addr & 0xff;

Where are we validating that the user has any legitimate claim to be touching this device?

I'll let VFIO-PCI-dev fd handle the IOCTL command. With that, we shouldn't have the problem.

Quoted 55 lines
Quoted 21 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +21 -0
+	/* Find PCI bus */
+	bus = pci_find_bus(domain, bus_no);
+	if (!bus) {
+		pr_warn("%s: PCI bus %04x:%02x not found\n",
+			__func__, domain, bus_no);
+		return -ENODEV;
+	}
+
+	/* Find PCI device */
+	pdev = pci_get_slot(bus, devfn);
+	if (!pdev) {
+		pr_warn("%s: PCI device %04x:%02x:%02x.%01x not found\n",
+			__func__, domain, bus_no,
+			PCI_SLOT(devfn), PCI_FUNC(devfn));
+		return -ENODEV;
+	}
+
+	/* No EEH device - almost impossible */
+	edev = pci_dev_to_eeh_dev(pdev);
+	if (unlikely(!edev)) {
+		pci_dev_put(pdev);

arch/powerpc/platforms/powernv/eeh-vfio.c line 81

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +10 -0
+		pr_warn("%s: No EEH dev for PCI device %s\n",
+			__func__, pci_name(pdev));
+		return -ENODEV;
+	}
+
+	/* Doesn't support PE migration between different PHBs */
+	pe = edev->pe;
+	if (!eeh_pe_passed(pe)) {
+		pe_bus = eeh_pe_bus_get(pe);
+		BUG_ON(!pe_bus);

Can a user trigger this maliciously?

Quoted 21 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +21 -0
+
+		/* PE# has format 00BBSS00 */
+		pe->guest_addr.buid    = info->map.guest_buid;
+		pe->guest_addr.pe_addr = pe_bus->number << 16;
+		eeh_pe_set_passed(pe, true);
+	} else if (pe->guest_addr.buid != info->map.guest_buid) {
+		pci_dev_put(pdev);
+		pr_warn("%s: Mismatched PHB BUID (0x%llx, 0x%llx)\n",
+			__func__, pe->guest_addr.buid, info->map.guest_buid);
+		return -EINVAL;
+	}
+
+	edev->guest_addr.buid = info->map.guest_buid;
+	edev->guest_addr.config_addr = info->map.guest_cfg_addr;
+	eeh_dev_set_passed(edev, true);
+
+	pr_debug("EEH: Host PCI dev %s to %llx-%02x:%02x.%01x\n",
+		 pci_name(pdev), info->map.guest_buid,
+		 (info->map.guest_cfg_addr >> 8) & 0xFF,
+		 PCI_SLOT(info->map.guest_cfg_addr & 0xFF),
+		 PCI_FUNC(info->map.guest_cfg_addr & 0xFF));
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 106

Quoted 4 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +4 -0
+
+	pci_dev_put(pdev);
+	return 0;
+}

So the effect of this function is that a user gets to setup an arbitrary guest mapping for an arbitrary host device and associated pe. Is that right? It seems bad.

I'm going to remove this mapping in next revision.

Quoted 65 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 119

Quoted 13 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +13 -0
+
+static int powernv_eeh_vfio_unmap(struct vfio_eeh_info *info)
+{
+	struct eeh_vfio_pci_addr addr;
+	struct pci_dev *pdev;
+	struct eeh_dev *edev, *tmp;
+	struct eeh_pe *pe;
+	bool passed;
+
+	/* Get EEH device */
+	addr.buid = info->unmap.buid;
+	addr.config_addr = info->unmap.cfg_addr;
+	edev = eeh_vfio_dev_get(&addr);

eeh_vfio_dev_get() just looks for a "passed" dev and a match for a well known address space. Seems very exploitable.

Quoted 48 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +48 -0
+	if (!edev) {
+		pr_warn("%s: Cannot find %llx:%02x:%02x.%01x\n",
+			__func__, info->unmap.buid,
+			(info->unmap.cfg_addr >> 8) & 0xFF,
+			PCI_SLOT(info->unmap.cfg_addr & 0xFF),
+			PCI_FUNC(info->unmap.cfg_addr & 0xFF));
+		return -ENODEV;
+	}
+
+	/* Return EEH device */
+	memset(&edev->guest_addr, 0, sizeof(edev->guest_addr));
+	eeh_dev_set_passed(edev, false);
+	pdev = eeh_dev_to_pci_dev(edev);
+	pr_debug("EEH: Host PCI dev %s returned\n",
+		 pdev ? pci_name(pdev) : "NULL");
+
+	/* Return PE if no EEH device is owned by guest */
+	pe = edev->pe;
+	passed = false;
+	eeh_pe_for_each_dev(pe, edev, tmp) {
+		pdev = eeh_dev_to_pci_dev(edev);
+		if (pdev && pdev->subordinate)
+			continue;
+
+		if (eeh_dev_passed(edev)) {
+			passed = true;
+			break;
+		}
+	}
+
+	if (!passed) {
+		memset(&pe->guest_addr, 0, sizeof(pe->guest_addr));
+		eeh_pe_set_passed(pe, false);
+		pr_debug("EEH: PHB#%x-PE#%x returned to host\n",
+			 pe->phb->global_number, pe->addr);
+	}
+
+	return 0;
+}
+
+static int powernv_eeh_vfio_set_option(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_dev *edev;
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int opcode = info->option.option;
+	int ret = 0;
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 173

Quoted 6 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +6 -0
+
+	/* Check opcode */
+	if (opcode < EEH_OPT_DISABLE || opcode > EEH_OPT_THAW_DMA) {
+		pr_warn("%s: opcode %d out of range (%d, %d)\n",
+			__func__, opcode, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);
+		ret = 3;

Please don't make up arbitrary return values.

Quoted 1 line

Nope, it will be turned to "-3" eventually by QEMU.

Don't assume QEMU is your userspace.

Quoted 2 lines

That means "Invalid Parameter"
defined in PAPR spec.

Is there value in matching the PAPR spec (which most people can't read)? If there is...

Quoted 9 lines
Quoted 5 lines

The IOCTL command handler return 3 values:

< 0: Linux kernel error. For example, error from copy_from_user().

Quoted 1 line

0: Error code to the EEH RTAS request, which will be returned to guest.

= 0: Success

Maybe the ioctl return should match normal ioctl return values and the EEH error code can be stored somewhere in the structure.

Good idea and will apply it to next revision. Thanks.

Quoted 510 lines
Quoted 510 lines
Quoted 200 lines
Quoted 200 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +200 -0
+		goto out;
+	}
+
+	/* Option "enable" uses PCI config address */
+	if (opcode = EEH_OPT_ENABLE) {
+		addr.buid = info->option.buid;
+		addr.config_addr = (info->option.addr >> 8) & 0xFFFF;
+		edev = eeh_vfio_dev_get(&addr);
+		if (!edev) {
+			pr_warn("%s: Cannot find %llx:%02x:%02x.%01x\n",
+				__func__, addr.buid,
+				(addr.config_addr >> 8) & 0xFF,
+				PCI_SLOT(addr.config_addr & 0xFF),
+				PCI_FUNC(addr.config_addr & 0xFF));
+			ret = 7;
+			goto out;
+		}
+		phb = edev->phb->private_data;
+	} else {
+		addr.buid    = info->option.buid;
+		addr.pe_addr = info->option.addr;
+		pe = eeh_vfio_pe_get(&addr);
+		if (!pe) {
+			pr_warn("%s: Cannot find PE %llx:%x\n",
+				__func__, addr.buid, addr.pe_addr);
+			ret = 7;
+			goto out;
+		}
+		phb = pe->phb->private_data;
+	}
+
+	/* Insure that the EEH stuff has been initialized */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 7;
+		goto out;
+	}
+
+	/*
+	 * The EEH functionality has been enabled on all PEs
+	 * by default. So just return success. The same situation
+	 * would be applied while we disable EEH functionality.
+	 * However, the guest isn't expected to disable that
+	 * at all.
+	 */
+	if (opcode = EEH_OPT_DISABLE ||
+	    opcode = EEH_OPT_ENABLE) {
+		ret = 0;
+		goto out;
+	}
+
+	/*
+	 * Call into the IODA dependent backend in order
+	 * to enable DMA or MMIO for the indicated PE.
+	 */
+	if (phb->eeh_ops && phb->eeh_ops->set_option) {
+		if (phb->eeh_ops->set_option(pe, opcode)) {
+			pr_warn("%s: Failure from backend\n",
+				__func__);
+			ret = 1;
+		}
+	} else {
+		pr_warn("%s: Unsupported request\n",
+			__func__);
+		ret = 7;
+	}
+
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_get_addr(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_dev *edev;
+	struct eeh_vfio_pci_addr addr;
+	int opcode = info->addr.option;
+	int ret = 0;
+
+	/* Check opcode */
+	if (opcode != 0 && opcode != 1) {
+		pr_warn("%s: opcode %d out of range (0, 1)\n",
+			__func__, opcode);
+		ret = 3;
+		goto out;
+	}
+
+	/* Find EEH device */
+	addr.buid = info->addr.buid;
+	addr.config_addr = (info->addr.cfg_addr >> 8 ) & 0xFFFF;
+	edev = eeh_vfio_dev_get(&addr);
+	if (!edev) {
+		pr_warn("%s: Cannot find %llx:%02x:%02x.%01x\n",
+			__func__, addr.buid,
+			(addr.config_addr >> 8) & 0xFF,
+			PCI_SLOT(addr.config_addr & 0xFF),
+			PCI_FUNC(addr.config_addr & 0xFF));
+		ret = 7;
+		goto out;
+	}
+	phb = edev->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+	}
+
+	/* EEH device passed ? */
+	if (!eeh_dev_passed(edev)) {
+		pr_warn("%s: EEH dev %llx:%02x:%02x.%01x owned by host\n",
+			__func__, addr.buid,
+			(addr.config_addr >> 8) & 0xFF,
+			PCI_SLOT(addr.config_addr & 0xFF),
+			PCI_FUNC(addr.config_addr & 0xFF));
+		ret = 3;
+		goto out;
+	}
+
+	/*
+	 * Fill result according to opcode. We don't differentiate
+	 * PCI bus and device sensitive PE here.
+	 */
+	if (opcode = 0)
+		info->addr.ret = edev->pe->guest_addr.pe_addr;
+	else
+		info->addr.ret = 1;
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_get_state(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int result, ret = 0;
+
+	/* Locate the PE */
+	addr.buid    = info->state.buid;
+	addr.pe_addr = info->state.pe_addr;
+	pe = eeh_vfio_pe_get(&addr);
+	if (!pe) {
+		pr_warn("%s: Cannot locate %llx:%x\n",
+			__func__, addr.buid, addr.pe_addr);
+		ret = 3;
+		goto out;
+	}
+	phb = pe->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+	}
+
+	/* Call to the IOC dependent function */
+	if (phb->eeh_ops && phb->eeh_ops->get_state) {
+		result = phb->eeh_ops->get_state(pe);
+
+		if (!(result & EEH_STATE_RESET_ACTIVE) &&
+		     (result & EEH_STATE_DMA_ENABLED) &&
+		     (result & EEH_STATE_MMIO_ENABLED))
+			info->state.state = 0;
+		else if (result & EEH_STATE_RESET_ACTIVE)
+			info->state.state = 1;
+		else if (!(result & EEH_STATE_RESET_ACTIVE) &&
+			 !(result & EEH_STATE_DMA_ENABLED) &&
+			 !(result & EEH_STATE_MMIO_ENABLED))
+			info->state.state = 2;
+		else if (!(result & EEH_STATE_RESET_ACTIVE) &&
+			 (result & EEH_STATE_DMA_ENABLED) &&
+			 !(result & EEH_STATE_MMIO_ENABLED))
+			info->state.state = 4;
+		else
+			info->state.state = 5;
+
+		ret = 0;
+	} else {
+		pr_warn("%s: Unsupported request\n", __func__);
+		ret = 3;
+	}
+
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_pe_reset(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int opcode = info->reset.option;
+	int ret = 0;
+
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 379

Quoted 6 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +6 -0
+	/* Check opcode */
+	if (opcode != EEH_RESET_DEACTIVATE &&
+	    opcode != EEH_RESET_HOT &&
+	    opcode != EEH_RESET_FUNDAMENTAL) {
+		pr_warn("%s: Unsupported opcode %d\n",
+			__func__, opcode);

Console warnings are exploitable DoS attacks.

Yep. I'll change all pr_warn() to pr_debug() in next revision.

Quoted 144 lines
Quoted 144 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +144 -0
+		ret = 3;
+		goto out;
+	}
+
+	/* Locate the PE */
+	addr.buid    = info->reset.buid;
+	addr.pe_addr = info->reset.pe_addr;
+	pe = eeh_vfio_pe_get(&addr);
+	if (!pe) {
+		pr_warn("%s: Cannot locate %llx:%x\n",
+			__func__, addr.buid, addr.pe_addr);
+		ret = 3;
+		goto out;
+	}
+	phb = pe->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+	}
+
+	/* Call into the IODA dependent backend to do the reset */
+	if (!phb->eeh_ops ||
+	    !phb->eeh_ops->set_option ||
+	    !phb->eeh_ops->reset) {
+		pr_warn("%s: Unsupported request\n",
+			__func__);
+		ret = 7;
+	} else {
+		/*
+		 * The frozen PE might be caused by the mechanism called
+		 * PAPR error injection, which is supposed to be one-shot
+		 * without "sticky" bit as being stated by the spec. But
+		 * the reality isn't that, at least on P7IOC. So we have
+		 * to clear that to avoid recrusive error, which fails the
+		 * recovery eventually.
+		 */
+		if (opcode = EEH_RESET_DEACTIVATE)
+			opal_pci_reset(phb->opal_id,
+				       OPAL_PHB_ERROR,
+				       OPAL_ASSERT_RESET);
+
+		if (phb->eeh_ops->reset(pe, opcode)) {
+			pr_warn("%s: Failure from backend\n", __func__);
+			ret = 1;
+			goto out;
+		}
+
+		/*
+		 * The PE is still in frozen state and we need clear that.
+		 * It's good to clear frozen state after deassert to avoid
+		 * messy IO access during reset, which might cause recrusive
+		 * frozen PE.
+		 */
+		if (opcode = EEH_RESET_DEACTIVATE) {
+			if (phb->eeh_ops->set_option(pe, EEH_OPT_THAW_MMIO) ||
+			    phb->eeh_ops->set_option(pe, EEH_OPT_THAW_DMA)) {
+				pr_warn("%s: Cannot clear frozen state\n",
+					__func__);
+				ret = 1;
+			}
+
+			eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
+		}
+	}
+
+out:
+	return ret;
+}
+
+static int powernv_eeh_vfio_pe_config(struct vfio_eeh_info *info)
+{
+	struct pnv_phb *phb;
+	struct eeh_pe *pe;
+	struct eeh_vfio_pci_addr addr;
+	int ret = 0;
+
+	/* Locate the PE */
+	addr.buid    = info->config.buid;
+	addr.pe_addr = info->config.pe_addr;
+	pe = eeh_vfio_pe_get(&addr);
+	if (!pe) {
+		pr_warn("%s: Cannot locate %llx:%x\n",
+			__func__, addr.buid, addr.pe_addr);
+		ret = 3;
+		goto out;
+	}
+	phb = pe->phb->private_data;
+
+	/* EEH enabled ? */
+	if (!(phb->flags & PNV_PHB_FLAG_EEH)) {
+		pr_warn("%s: EEH disabled on PHB#%d\n",
+			__func__, phb->hose->global_number);
+		ret = 3;
+		goto out;
+        }
+
+	/*
+	 * The access to PCI config space on VFIO device has some
+	 * limitations. Part of PCI config space, including BAR
+	 * registers are not readable and writable. So the guest
+	 * should have stale values for those registers and we have
+	 * to restore them in host side.
+	 */
+	eeh_pe_restore_bars(pe);
+out:
+	return ret;
+}
+
+void eeh_vfio_release(struct iommu_table *tbl)
+{
+	struct pnv_ioda_pe *pnv_pe = container_of(tbl, struct pnv_ioda_pe,
+						  tce32_table);
+	struct pnv_phb *phb = pnv_pe->phb;
+	struct eeh_pe *phb_pe, *pe;
+	struct eeh_dev dev, *edev, *tmp;
+
+	/* Find PHB PE */
+	phb_pe = eeh_phb_pe_get(phb->hose);
+	if (unlikely(!phb_pe)) {
+		pr_warn("%s: Cannot find PHB#%d PE\n",
+			__func__, phb->hose->global_number);
+		return;
+	}
+
+	/* Find PE */
+	memset(&dev, 0, sizeof(struct eeh_dev));
+	dev.phb = phb->hose;
+	dev.pe_config_addr = pnv_pe->pe_number;
+	pe = eeh_pe_get(&dev);
+	if (unlikely(!pe)) {
+		pr_warn("%s: Cannot find PE instance for PHB#%d-PE#%d\n",
+			__func__, phb->hose->global_number,
+			pnv_pe->pe_number);
+		return;
+	}
+
+	/* Release it to host */
+	if (!eeh_pe_passed(pe))
+		return;
+
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 528

Quoted 5 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +5 -0
+	eeh_pe_for_each_dev(pe, edev, tmp) {
+		if (!eeh_dev_passed(edev))
+			continue;
+
+		memset(&edev->guest_addr, 0, sizeof(edev->guest_addr));

Is guest_addr = { 0 } not valid? As agraf already mentioned, there are a number of issues with using a guest_address for a token.

For now, PHB BUID can't be "0". Originally, I was planing to have some code in QEMU to have unique PHB BUID across the system so that guest_address could be the unique token. But I'm going to remove the address mapping in next revision as Alex.G suggested.

Quoted 17 lines
Quoted 17 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +17 -0
+		eeh_dev_set_passed(edev, false);
+	}
+
+	memset(&pe->guest_addr, 0, sizeof(pe->guest_addr));
+	eeh_pe_set_passed(pe, false);
+}
+EXPORT_SYMBOL(eeh_vfio_release);
+
+int eeh_vfio_ioctl(unsigned long arg)
+{
+	struct vfio_eeh_info info;
+	int ret = -EINVAL;
+
+	/* Copy over user argument */
+	if (copy_from_user(&info, (void __user *)arg, sizeof(info))) {
+		pr_warn("%s: Cannot copy user argument 0x%lx\n",
+			__func__, arg);
Quoted 9 lines

arch/powerpc/platforms/powernv/eeh-vfio.c line 550

Quoted 5 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +5 -0
+		return -EFAULT;
+	}
+
+	/* Sanity check */
+	if (info.argsz != sizeof(info)) {

This breaks compatibility if you need to later add a new ops with a larger footprint.

Ok. I'll fix it in next revision. Thanks for pointing it out.

Quoted 59 lines
Quoted 59 lines
arch/powerpc/platforms/powernv/eeh-vfio.c new file +43 -0
+		pr_warn("%s: Invalid argument size (%d, %ld)\n",
+			__func__, info.argsz, sizeof(info));
+		return -EINVAL;
+	}
+
+	/* Route according to operation */
+	switch (info.op) {
+	case VFIO_EEH_OP_MAP:
+		ret = powernv_eeh_vfio_map(&info);
+		break;
+	case VFIO_EEH_OP_UNMAP:
+		ret = powernv_eeh_vfio_unmap(&info);
+		break;
+	case VFIO_EEH_OP_SET_OPTION:
+		ret = powernv_eeh_vfio_set_option(&info);
+		break;
+	case VFIO_EEH_OP_GET_ADDR:
+		ret = powernv_eeh_vfio_get_addr(&info);
+		break;
+	case VFIO_EEH_OP_GET_STATE:
+		ret = powernv_eeh_vfio_get_state(&info);
+		break;
+	case VFIO_EEH_OP_PE_RESET:
+		ret = powernv_eeh_vfio_pe_reset(&info);
+		break;
+	case VFIO_EEH_OP_PE_CONFIG:
+		ret = powernv_eeh_vfio_pe_config(&info);
+		break;
+	default:
+		pr_info("%s: Cannot handle op#%d\n",
+			__func__, info.op);
+	}
+
+	/* Copy data back */
+	if (!ret && copy_to_user((void __user *)arg, &info, sizeof(info))) {
+		pr_warn("%s: Cannot copy to user 0x%lx\n",
+			__func__, arg);
+		return -EFAULT;
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(eeh_vfio_ioctl);
drivers/vfio/vfio_iommu_spapr_tce.c +5 -0
index a84788b..c45dece 100644
@@ -26,6 +26,11 @@
 #define DRIVER_AUTHOR   "[email protected]"
 #define DRIVER_DESC     "VFIO IOMMU SPAPR TCE"
 
+#ifdef CONFIG_VFIO_EEH
+extern void eeh_vfio_release(struct iommu_table *tbl);
+extern int eeh_vfio_ioctl(unsigned long arg);
+#endif
+
 static void tce_iommu_detach_group(void *iommu_data,
 		struct iommu_group *iommu_group);
 
Quoted 9 lines

drivers/vfio/vfio_iommu_spapr_tce.c static long tce_iommu_ioctl(void *iommu_data, · line 291

Quoted 5 lines
drivers/vfio/vfio_iommu_spapr_tce.c +1 -0
@@ -283,6 +288,10 @@ static long tce_iommu_ioctl(void *iommu_data,
 		tce_iommu_disable(container);
 		mutex_unlock(&container->lock);
 		return 0;
+#ifdef CONFIG_VFIO_EEH

I'm not a fan of all these #ifdefs, hide it in eeh_vfio_ioctl() and eeh_vfio_release() if needed.

Ok. Will do it in next revision.

Quoted 38 lines
Quoted 25 lines
drivers/vfio/vfio_iommu_spapr_tce.c +6 -0
+	case VFIO_EEH_INFO:
+		return eeh_vfio_ioctl(arg);
+#endif
 	}
 
 	return -ENOTTY;
@@ -342,6 +351,9 @@ static void tce_iommu_detach_group(void *iommu_data,
 		/* pr_debug("tce_vfio: detaching group #%u from iommu %p\n",
 				iommu_group_id(iommu_group), iommu_group); */
 		container->tbl = NULL;
+#ifdef CONFIG_VFIO_EEH
+		eeh_vfio_release(tbl);
+#endif
 		iommu_release_ownership(tbl);
 	}
 	mutex_unlock(&container->lock);
include/uapi/linux/vfio.h +1 -0
index cb9023d..1fd1bfb 100644
@@ -455,6 +455,63 @@ struct vfio_iommu_spapr_tce_info {
 
 #define VFIO_IOMMU_SPAPR_TCE_GET_INFO	_IO(VFIO_TYPE, VFIO_BASE + 12)
 
+/*

include/uapi/linux/vfio.h struct vfio_iommu_spapr_tce_info { · line 468

Quoted 10 lines
include/uapi/linux/vfio.h +10 -0
+ * The VFIO EEH info struct provides way to support EEH functionality
+ * for PCI device that is passed from host to guest via VFIO.
+ */
+#define VFIO_EEH_OP_MAP		0
+#define VFIO_EEH_OP_UNMAP	1
+#define VFIO_EEH_OP_SET_OPTION	2
+#define VFIO_EEH_OP_GET_ADDR	3
+#define VFIO_EEH_OP_GET_STATE	4
+#define VFIO_EEH_OP_PE_RESET	5
+#define VFIO_EEH_OP_PE_CONFIG	6

Is this really an "info" ioctl?

Quoted 4 lines
Quoted 1 line

Yeah, "VFIO_EEH_INFO" isn't a good name. How about to have "VFIO_EEH_HANDLER" ?

VFIO_EEH_OP perhaps. Thanks,

Ok. Will rename it to VFIO_EEH_OP in next revision.

Quoted 52 lines

Alex

Quoted 50 lines
Quoted 49 lines
Quoted 49 lines
include/uapi/linux/vfio.h +46 -0
+
+struct vfio_eeh_info {
+	__u32 argsz;
+	__u32 op;
+
+	union {
+		struct vfio_eeh_map {
+			__u32 host_domain;
+			__u16 host_cfg_addr;
+			__u64 guest_buid;
+			__u16 guest_cfg_addr;
+		} map;
+		struct vfio_eeh_unmap {
+			__u64 buid;
+			__u16 cfg_addr;
+		} unmap;
+		struct vfio_eeh_set_option {
+			__u64 buid;
+			__u32 addr;
+			__u32 option;
+		} option;
+		struct vfio_eeh_pe_addr {
+			__u64 buid;
+			__u32 cfg_addr;
+			__u32 option;
+			__u32 ret;
+		} addr;
+		struct vfio_eeh_state {
+			__u64 buid;
+			__u32 pe_addr;
+			__u32 state;
+                } state;
+		struct vfio_eeh_reset {
+			__u64 buid;
+			__u32 pe_addr;
+			__u32 option;
+		} reset;
+		struct vfio_eeh_config {
+			__u64 buid;
+			__u32 pe_addr;
+		} config;
+	};
+};
+
+#define VFIO_EEH_INFO	_IO(VFIO_TYPE, VFIO_BASE + 21)
+
 /* ***************************************************************** */
 
 #endif /* _UAPIVFIO_H */

Thanks,
Gavin

Quoted 1 line
#14 lore raw

Re: [PATCH 3/8] drivers/vfio: New IOCTL command VFIO_EEH_INFO

Quoted 7 lines · Gavin Shan
Quoted 6 lines · Alex Williamson
Quoted 5 lines · Gavin Shan
Quoted 4 lines · Alex Williamson
Quoted 3 lines · Gavin Shan

The patch adds new IOCTL command VFIO_EEH_INFO to VFIO container to support EEH functionality for PCI devices, which have been passed from host to guest via VFIO.

[...]

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
include/uapi/linux/vfio.h +11 -0
index cb9023d..1fd1bfb 100644
@@ -455,6 +455,63 @@ struct vfio_iommu_spapr_tce_info {
 
  #define VFIO_IOMMU_SPAPR_TCE_GET_INFO	_IO(VFIO_TYPE, VFIO_BASE + 12)
 
+/*
+ * The VFIO EEH info struct provides way to support EEH functionality
+ * for PCI device that is passed from host to guest via VFIO.
+ */
+#define VFIO_EEH_OP_MAP		0
+#define VFIO_EEH_OP_UNMAP	1
+#define VFIO_EEH_OP_SET_OPTION	2
+#define VFIO_EEH_OP_GET_ADDR	3
+#define VFIO_EEH_OP_GET_STATE	4
+#define VFIO_EEH_OP_PE_RESET	5
+#define VFIO_EEH_OP_PE_CONFIG	6
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

Is this really an "info" ioctl?

Yeah, "VFIO_EEH_INFO" isn't a good name. How about to have "VFIO_EEH_HANDLER" ?

VFIO_EEH_OP perhaps. Thanks,

Ok. Will rename it to VFIO_EEH_OP in next revision.

Is there any benefit of a multiplexing EEH ioctl over just 7 individual ioctls?

Alex

author
#15 lore raw

Re: [PATCH 3/8] drivers/vfio: New IOCTL command VFIO_EEH_INFO

Quoted 31 lines · Alexander Graf
Quoted 7 lines · Gavin Shan
Quoted 6 lines · Alex Williamson
Quoted 5 lines · Gavin Shan
Quoted 4 lines · Alex Williamson
Quoted 3 lines · Gavin Shan

The patch adds new IOCTL command VFIO_EEH_INFO to VFIO container to support EEH functionality for PCI devices, which have been passed from host to guest via VFIO.

[...]

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
include/uapi/linux/vfio.h +11 -0
index cb9023d..1fd1bfb 100644
@@ -455,6 +455,63 @@ struct vfio_iommu_spapr_tce_info {
 #define VFIO_IOMMU_SPAPR_TCE_GET_INFO	_IO(VFIO_TYPE, VFIO_BASE + 12)
+/*
+ * The VFIO EEH info struct provides way to support EEH functionality
+ * for PCI device that is passed from host to guest via VFIO.
+ */
+#define VFIO_EEH_OP_MAP		0
+#define VFIO_EEH_OP_UNMAP	1
+#define VFIO_EEH_OP_SET_OPTION	2
+#define VFIO_EEH_OP_GET_ADDR	3
+#define VFIO_EEH_OP_GET_STATE	4
+#define VFIO_EEH_OP_PE_RESET	5
+#define VFIO_EEH_OP_PE_CONFIG	6
Quoted 2 lines

Is this really an "info" ioctl?

Quoted 8 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

Yeah, "VFIO_EEH_INFO" isn't a good name. How about to have "VFIO_EEH_HANDLER" ?

VFIO_EEH_OP perhaps. Thanks,

Ok. Will rename it to VFIO_EEH_OP in next revision.

Is there any benefit of a multiplexing EEH ioctl over just 7 individual ioctls?

One benefit is pass one data struct to the real handler implemented in arch/powerpc/platforms/powernv/eeh-vfio.c. With 7 commands, we either passing "void *arg" + "command" to the handler, or export 7 functions with EXPORT_SYMBOL_GPL().

I just send RFCv4 out. VFIO_EEH_OP_{MAP, UNMAP} is removed there.

Thanks,
Gavin

author
#16 lore raw

PATCH v3 4/8 powerpc/eeh: Avoid event on passed PE

In reply to Gavin Shan

If we detects frozen state on PE that has been passed to guest, we needn't handle it. Instead, we rely on the guest to detect and recover it. The patch avoid EEH event on the frozen passed PE so that the guest can have chance to handle that.

---
 arch/powerpc/kernel/eeh.c                 | 8 ++++++++
 arch/powerpc/platforms/powernv/eeh-ioda.c | 3 ++-
 2 files changed, 10 insertions(+), 1 deletion(-)
arch/powerpc/kernel/eeh.c +8 -0
index 9c6b899..6543f05 100644
@@ -400,6 +400,14 @@ int eeh_dev_check_failure(struct eeh_dev *edev)
 	if (ret > 0)
 		return ret;
 
+	/*
+	 * If the PE has been passed to guest, we won't check the
+	 * state. Instead, let the guest handle it if the PE has
+	 * been frozen.
+	 */
+	if (eeh_pe_passed(pe))
+		return 0;
+
 	/* If we already have a pending isolation event for this
 	 * slot, we know it's bad already, we don't need to check.
 	 * Do this checking under a lock; as multiple PCI devices
arch/powerpc/platforms/powernv/eeh-ioda.c +2 -1
index 1b5982f..03a3ed2 100644
@@ -890,7 +890,8 @@ static int ioda_eeh_next_error(struct eeh_pe **pe)
 				opal_pci_eeh_freeze_clear(phb->opal_id, frozen_pe_no,
 					OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
 				ret = EEH_NEXT_ERR_NONE;
-			} else if ((*pe)->state & EEH_PE_ISOLATED) {
+			} else if ((*pe)->state & EEH_PE_ISOLATED ||
+				   eeh_pe_passed(*pe)) {
 				ret = EEH_NEXT_ERR_NONE;
 			} else {
 				pr_err("EEH: Frozen PHB#%x-PE#%x (%s) detected\n",
Signature · 1 line
1.8.3.2
author
#17 lore raw

PATCH v3 5/8 powerpc/powernv: Sync OPAL header file with firmware

In reply to Gavin Shan

The patch synchronizes OPAL header file with firmware so that the host kernel can make OPAL call to do error injection.

---
 arch/powerpc/include/asm/opal.h                | 65 ++++++++++++++++++++++++++
 arch/powerpc/platforms/powernv/opal-wrappers.S |  1 +
 2 files changed, 66 insertions(+)
arch/powerpc/include/asm/opal.h +65 -0
index 66ad7a7..ca55d9c 100644
@@ -175,6 +175,7 @@ extern int opal_enter_rtas(struct rtas_args *args,
 #define OPAL_SET_PARAM				90
 #define OPAL_DUMP_RESEND			91
 #define OPAL_DUMP_INFO2				94
+#define OPAL_ERR_INJECT				96
 
 #ifndef __ASSEMBLY__
 
@@ -219,6 +220,69 @@ enum OpalPciErrorSeverity {
 	OPAL_EEH_SEV_INF	= 5
 };
 
+enum OpalErrinjctType {
+	OpalErrinjctTypeFirst			= 0,
+	OpalErrinjctTypeFatal			= 1,
+	OpalErrinjctTypeRecoverRandomEvent	= 2,
+	OpalErrinjctTypeRecoverSpecialEvent	= 3,
+	OpalErrinjctTypeCorruptedPage		= 4,
+	OpalErrinjctTypeCorruptedSlb		= 5,
+	OpalErrinjctTypeTranslatorFailure	= 6,
+	OpalErrinjctTypeIoaBusError		= 7,
+	OpalErrinjctTypeIoaBusError64		= 8,
+	OpalErrinjctTypePlatformSpecific	= 9,
+	OpalErrinjctTypeDcacheStart		= 10,
+	OpalErrinjctTypeDcacheEnd		= 11,
+	OpalErrinjctTypeIcacheStart		= 12,
+	OpalErrinjctTypeIcacheEnd		= 13,
+	OpalErrinjctTypeTlbStart		= 14,
+	OpalErrinjctTypeTlbEnd			= 15,
+	OpalErrinjctTypeUpstreamIoError		= 16,
+	OpalErrinjctTypeLast			= 17,
+
+	/* IoaBusError & IoaBusError64 */
+	OpalEjtIoaLoadMemAddr			= 0,
+	OpalEjtIoaLoadMemData			= 1,
+	OpalEjtIoaLoadIoAddr			= 2,
+	OpalEjtIoaLoadIoData			= 3,
+	OpalEjtIoaLoadConfigAddr		= 4,
+	OpalEjtIoaLoadConfigData		= 5,
+	OpalEjtIoaStoreMemAddr			= 6,
+	OpalEjtIoaStoreMemData			= 7,
+	OpalEjtIoaStoreIoAddr			= 8,
+	OpalEjtIoaStoreIoData			= 9,
+	OpalEjtIoaStoreConfigAddr		= 10,
+	OpalEjtIoaStoreConfigData		= 11,
+	OpalEjtIoaDmaReadMemAddr		= 12,
+	OpalEjtIoaDmaReadMemData		= 13,
+	OpalEjtIoaDmaReadMemMaster		= 14,
+	OpalEjtIoaDmaReadMemTarget		= 15,
+	OpalEjtIoaDmaWriteMemAddr		= 16,
+	OpalEjtIoaDmaWriteMemData		= 17,
+	OpalEjtIoaDmaWriteMemMaster		= 18,
+	OpalEjtIoaDmaWriteMemTarget		= 19,
+};
+
+struct OpalErrinjct {
+	int32_t type;
+	union {
+		struct {
+			uint32_t addr;
+			uint32_t mask;
+			uint64_t phb_id;
+			uint32_t pe;
+			uint32_t function;
+		}ioa;
+		struct {
+			uint64_t addr;
+			uint64_t mask;
+			uint64_t phb_id;
+			uint32_t pe;
+			uint32_t function;
+		}ioa64;
+	};
+};
+
 enum OpalShpcAction {
 	OPAL_SHPC_GET_LINK_STATE = 0,
 	OPAL_SHPC_GET_SLOT_STATE = 1
@@ -839,6 +903,7 @@ int64_t opal_pci_get_phb_diag_data(uint64_t phb_id, void *diag_buffer,
 				   uint64_t diag_buffer_len);
 int64_t opal_pci_get_phb_diag_data2(uint64_t phb_id, void *diag_buffer,
 				    uint64_t diag_buffer_len);
+int64_t opal_err_injct(void *data);
 int64_t opal_pci_fence_phb(uint64_t phb_id);
 int64_t opal_pci_reinit(uint64_t phb_id, uint64_t reinit_scope, uint64_t data);
 int64_t opal_pci_mask_pe_error(uint64_t phb_id, uint16_t pe_number, uint8_t error_type, uint8_t mask_action);
arch/powerpc/platforms/powernv/opal-wrappers.S +1 -0
index f531ffe..46265de 100644
@@ -119,6 +119,7 @@ OPAL_CALL(opal_pci_next_error,			OPAL_PCI_NEXT_ERROR);
 OPAL_CALL(opal_pci_poll,			OPAL_PCI_POLL);
 OPAL_CALL(opal_pci_msi_eoi,			OPAL_PCI_MSI_EOI);
 OPAL_CALL(opal_pci_get_phb_diag_data2,		OPAL_PCI_GET_PHB_DIAG_DATA2);
+OPAL_CALL(opal_err_injct,			OPAL_ERR_INJECT);
 OPAL_CALL(opal_xscom_read,			OPAL_XSCOM_READ);
 OPAL_CALL(opal_xscom_write,			OPAL_XSCOM_WRITE);
 OPAL_CALL(opal_lpc_read,			OPAL_LPC_READ);
Signature · 1 line
1.8.3.2
author
#18 lore raw

PATCH v3 6/8 powerpc: Extend syscall ppc_rtas()

In reply to Gavin Shan

Originally, syscall ppc_rtas() can be used to invoke RTAS call from user space. Utility "errinjct" is using it to inject various errors to the system for testing purpose. The patch intends to extend the syscall to support both pSeries and PowerNV platform. With that, RTAS and OPAL call can be invoked from user space. In turn, utility "errinjct" can be supported on pSeries and PowerNV platform at same time.

The original syscall handler ppc_rtas() is renamed to ppc_firmware(), which calls ppc_call_rtas() or ppc_call_opal() depending on the running platform. The data transported between userland and kerenl is by "struct rtas_args". It's platform specific on how to use the data.

---
 arch/powerpc/include/asm/rtas.h        | 10 +++++-
 arch/powerpc/include/asm/syscalls.h    |  2 +-
 arch/powerpc/include/asm/systbl.h      |  2 +-
 arch/powerpc/include/uapi/asm/unistd.h |  2 +-
 arch/powerpc/kernel/rtas.c             | 57 +++++++---------------------------
 arch/powerpc/kernel/syscalls.c         | 50 +++++++++++++++++++++++++++++
 arch/powerpc/platforms/powernv/opal.c  |  7 +++++
 kernel/sys_ni.c                        |  2 +-
 8 files changed, 82 insertions(+), 50 deletions(-)
arch/powerpc/include/asm/rtas.h +9 -1
index b390f55..3428524 100644
@@ -20,7 +20,7 @@
 #define RTAS_UNKNOWN_SERVICE (-1)
 #define RTAS_INSTANTIATE_MAX (1ULL<<30) /* Don't instantiate rtas at/above this value */
 
-/* Buffer size for ppc_rtas system call. */
+/* Buffer size for ppc_firmware system call. */
 #define RTAS_RMOBUF_MAX (64 * 1024)
 
 /* RTAS return status codes */
@@ -427,9 +427,17 @@ static inline int page_is_rtas_user_buf(unsigned long pfn)
 /* Not the best place to put pSeries_coalesce_init, will be fixed when we
  * move some of the rtas suspend-me stuff to pseries */
 extern void pSeries_coalesce_init(void);
+extern int ppc_call_rtas(struct rtas_args *args);
 #else
 static inline int page_is_rtas_user_buf(unsigned long pfn) { return 0;}
 static inline void pSeries_coalesce_init(void) { }
+static inline int ppc_call_rtas(struct rtas_args *args) { return -ENXIO; }
+#endif
+
+#ifdef CONFIG_PPC_POWERNV
+extern int ppc_call_opal(struct rtas_args *args);
+#else
+static inline int ppc_call_opal(struct rtas_arts *args) { return -ENXIO; }
 #endif
 
 extern int call_rtas(const char *, int, int, unsigned long *, ...);
arch/powerpc/include/asm/syscalls.h +1 -1
index 23be8f1..3383e50 100644
@@ -15,7 +15,7 @@ asmlinkage unsigned long sys_mmap2(unsigned long addr, size_t len,
 		unsigned long prot, unsigned long flags,
 		unsigned long fd, unsigned long pgoff);
 asmlinkage long ppc64_personality(unsigned long personality);
-asmlinkage int ppc_rtas(struct rtas_args __user *uargs);
+asmlinkage int ppc_firmware(struct rtas_args __user *uargs);
 
 #endif /* __KERNEL__ */
 #endif /* __ASM_POWERPC_SYSCALLS_H */
arch/powerpc/include/asm/systbl.h +1 -1
index 3ddf702..00f8bb2 100644
@@ -259,7 +259,7 @@ COMPAT_SYS_SPU(utimes)
 COMPAT_SYS_SPU(statfs64)
 COMPAT_SYS_SPU(fstatfs64)
 SYSX(sys_ni_syscall, ppc_fadvise64_64, ppc_fadvise64_64)
-PPC_SYS_SPU(rtas)
+PPC_SYS_SPU(firmware)
 OLDSYS(debug_setcontext)
 SYSCALL(ni_syscall)
 COMPAT_SYS(migrate_pages)
arch/powerpc/include/uapi/asm/unistd.h +1 -1
index 881bf2e..3aee765 100644
@@ -273,7 +273,7 @@
 #ifndef __powerpc64__
 #define __NR_fadvise64_64	254
 #endif
-#define __NR_rtas		255
+#define __NR_firmware		255
 #define __NR_sys_debug_setcontext 256
 /* Number 257 is reserved for vserver */
 #define __NR_migrate_pages	258
arch/powerpc/kernel/rtas.c +12 -45

84 lines not on this page · raw message

arch/powerpc/kernel/syscalls.c +50 -0

56 lines not on this page · raw message

arch/powerpc/platforms/powernv/opal.c +7 -0
index 360ad80c..ad33c2b 100644
@@ -25,6 +25,7 @@
 #include <asm/opal.h>
 #include <asm/firmware.h>
 #include <asm/mce.h>
+#include <asm/rtas.h>
 
 #include "powernv.h"
 
@@ -701,3 +702,9 @@ void opal_free_sg_list(struct opal_sg_list *sg)
 			sg = NULL;
 	}
 }
+
+/* Extend it later */
+int ppc_call_opal(struct rtas_args *args)
+{
+	return 0;
+}
kernel/sys_ni.c +1 -1
index bc8d1b7..2c5b3fa 100644
@@ -159,7 +159,7 @@ cond_syscall(sys_pciconfig_read);
 cond_syscall(sys_pciconfig_write);
 cond_syscall(sys_pciconfig_iobase);
 cond_syscall(compat_sys_s390_ipc);
-cond_syscall(ppc_rtas);
+cond_syscall(ppc_firmware);
 cond_syscall(sys_spu_run);
 cond_syscall(sys_spu_create);
 cond_syscall(sys_subpage_prot);
Signature · 1 line
1.8.3.2
#19 lore raw

Re: [PATCH 6/8] powerpc: Extend syscall ppc_rtas()

Quoted 11 lines · Gavin Shan

Originally, syscall ppc_rtas() can be used to invoke RTAS call from user space. Utility "errinjct" is using it to inject various errors to the system for testing purpose. The patch intends to extend the syscall to support both pSeries and PowerNV platform. With that, RTAS and OPAL call can be invoked from user space. In turn, utility "errinjct" can be supported on pSeries and PowerNV platform at same time.

The original syscall handler ppc_rtas() is renamed to ppc_firmware(), which calls ppc_call_rtas() or ppc_call_opal() depending on the running platform. The data transported between userland and kerenl is

Please fix your spelling of kernel.

Quoted 4 lines

by "struct rtas_args". It's platform specific on how to use the data.

I think the basic idea to maintain the same interface between PAPR and OPAL to user space is sound, but this is really Ben's call.

Alex

#20 lore raw

Re: [PATCH 6/8] powerpc: Extend syscall ppc_rtas()

Quoted 15 lines · Alexander Graf
Quoted 11 lines · Gavin Shan

Originally, syscall ppc_rtas() can be used to invoke RTAS call from user space. Utility "errinjct" is using it to inject various errors to the system for testing purpose. The patch intends to extend the syscall to support both pSeries and PowerNV platform. With that, RTAS and OPAL call can be invoked from user space. In turn, utility "errinjct" can be supported on pSeries and PowerNV platform at same time.

The original syscall handler ppc_rtas() is renamed to ppc_firmware(), which calls ppc_call_rtas() or ppc_call_opal() depending on the running platform. The data transported between userland and kerenl is

Please fix your spelling of kernel.

Quoted 7 lines
Quoted 4 lines

by "struct rtas_args". It's platform specific on how to use the data.

I think the basic idea to maintain the same interface between PAPR and OPAL to user space is sound, but this is really Ben's call.

Yeah that worries me a bit, RTAS and OPAL are completely different beasts.

We can keep that error injection separate from the rest of the EEH enablement for now. I'll look at it when I get a chance.

Cheers,
Ben.

author
#21 lore raw

PATCH v3 7/8 powerpc/powernv: Implement ppc_call_opal()

In reply to Gavin Shan

If we're running PowerNV platform, ppc_firmware() will be directed to ppc_call_opal() where we can call to OPAL API accordingly. In ppc_call_opal(), the input argument are parsed out and call to appropriate OPAL API to handle that. Each request passed to the function is identified with token. As we get to the function either from host owned application (e.g. errinjct) or VM, we always have the first parameter (so-called "virtual") to differentiate the cases.

The patch implements above logic and OPAL call handler dynamica registeration mechanism so that the handlers could be distributed.

---
 arch/powerpc/include/asm/opal.h       |  3 +-
 arch/powerpc/platforms/powernv/opal.c | 90 ++++++++++++++++++++++++++++++++++-
 2 files changed, 90 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/opal.h +2 -1
index ca55d9c..7c4ffd0 100644
@@ -997,7 +997,8 @@ extern void opal_lpc_init(void);
 struct opal_sg_list *opal_vmalloc_to_sg_list(void *vmalloc_addr,
 					     unsigned long vmalloc_size);
 void opal_free_sg_list(struct opal_sg_list *sg);
-
+int opal_call_handler_register(bool virt, int token,
+			       int (*fn)(struct rtas_args *));
 #endif /* __ASSEMBLY__ */
 
 #endif /* __OPAL_H */
arch/powerpc/platforms/powernv/opal.c +88 -2

108 lines not on this page · raw message

Signature · 1 line
1.8.3.2
#22 lore raw

Re: [PATCH 7/8] powerpc/powernv: Implement ppc_call_opal()

Quoted 8 lines · Gavin Shan

If we're running PowerNV platform, ppc_firmware() will be directed to ppc_call_opal() where we can call to OPAL API accordingly. In ppc_call_opal(), the input argument are parsed out and call to appropriate OPAL API to handle that. Each request passed to the function is identified with token. As we get to the function either from host owned application (e.g. errinjct) or VM, we always have the first parameter (so-called "virtual") to differentiate the cases.

This sounds like nonsense. A virtual parameter? What is that supposed to be?

I don't think you can use this interface for VM device management, as the syscall interface is (hopefully!) limited to CAP_ADMIN which you (hopefully!) don't have as VFIO user.

Alex

author
#23 lore raw

PATCH v3 8/8 powerpc/powernv: Error injection infrastructure

In reply to Gavin Shan

The patch intends to implement the error injection infrastructure for PowerNV platform. The predetermined handlers will be called according to the type of injected error (e.g. OpalErrinjctTypeIoaBusError). For now, we just support PCI error injection. We need support injecting other types of errors in future.

---
 arch/powerpc/include/asm/opal.h            |   6 +
 arch/powerpc/platforms/powernv/Makefile    |   2 +-
 arch/powerpc/platforms/powernv/errinject.c | 224 +++++++++++++++++++++++++++++
 3 files changed, 231 insertions(+), 1 deletion(-)
 create mode 100644 arch/powerpc/platforms/powernv/errinject.c
arch/powerpc/include/asm/opal.h +6 -0
index 7c4ffd0..7bf86ba 100644
@@ -794,6 +794,12 @@ typedef struct oppanel_line {
 	uint64_t 	line_len;
 } oppanel_line_t;
 
+enum OpalCallToken{
+	OPAL_CALL_TOKEN_MIN = 0,
+	OPAL_CALL_TOKEN_ERRINJCT,
+	OPAL_CALL_TOKEN_MAX
+};
+
 /* /sys/firmware/opal */
 extern struct kobject *opal_kobj;
 
arch/powerpc/platforms/powernv/Makefile +1 -1
index 2b15a03..5ae8257 100644
@@ -1,7 +1,7 @@
 obj-y			+= setup.o opal-takeover.o opal-wrappers.o opal.o opal-async.o
 obj-y			+= opal-rtc.o opal-nvram.o opal-lpc.o opal-flash.o
 obj-y			+= rng.o opal-elog.o opal-dump.o opal-sysparam.o opal-sensor.o
-obj-y			+= opal-msglog.o
+obj-y			+= opal-msglog.o errinject.o
 
 obj-$(CONFIG_SMP)	+= smp.o
 obj-$(CONFIG_PCI)	+= pci.o pci-p5ioc2.o pci-ioda.o
arch/powerpc/platforms/powernv/errinject.c new file +224 -0

224 lines not on this page · raw message

Signature · 1 line
1.8.3.2
#24 lore raw

Re: [PATCH 8/8] powerpc/powernv: Error injection infrastructure

Quoted 5 lines · Gavin Shan

The patch intends to implement the error injection infrastructure for PowerNV platform. The predetermined handlers will be called according to the type of injected error (e.g. OpalErrinjctTypeIoaBusError). For now, we just support PCI error injection. We need support injecting other types of errors in future.

Your token to a VFIO device is the VFIO fd. If you want to inject an error into that device, you should do it via that token. That gets you all permission problems solved for free.

But I still didn't quite grasp why you need to do this. Why do we need to inject an error into a device via OPAL when we want to do EEH inside of a guest? Are you trying to emulate guest side error injection?

Alex

#25 lore raw

Re: [PATCH 8/8] powerpc/powernv: Error injection infrastructure

Quoted 7 lines · Alexander Graf
Quoted 5 lines · Gavin Shan

The patch intends to implement the error injection infrastructure for PowerNV platform. The predetermined handlers will be called according to the type of injected error (e.g. OpalErrinjctTypeIoaBusError). For now, we just support PCI error injection. We need support injecting other types of errors in future.

Quoted 7 lines

Your token to a VFIO device is the VFIO fd. If you want to inject an error into that device, you should do it via that token. That gets you all permission problems solved for free.

But I still didn't quite grasp why you need to do this. Why do we need to inject an error into a device via OPAL when we want to do EEH inside of a guest? Are you trying to emulate guest side error injection?

Yes, that's what he's trying to do but let's keep that separate from the core EEH. I'd like the latter to be reviewed /fixed and upstream first, then we can look at guest side injection.

Cheers,
Ben.

#26 lore raw
In reply to Gavin Shan

Hi Gavin,

Can error injection be done if EEH is not enbaled?

Thanks
Mike

Quoted 85 lines · Gavin Shan

The series of patches intends to support EEH for PCI devices, which are passed through to PowerKVM based guest via VFIO. The implementation is straightforward based on the issues or problems we have to resolve to support EEH for PowerKVM based guest.

- Emulation for EEH RTAS requests. All EEH RTAS requests goes to QEMU firstly. If QEMU can't handle it, the request will be sent to host via newly introduced VFIO container IOCTL command (VFIO_EEH_INFO) and gets handled in host kernel.

- The error injection infrastructure need support request from the userland utility "errinjct" and PowerKVM based guest. The userland utility "errinjct" works on pSeries platform well with dedicated syscall, which helps invoking RTAS service to fulfil error injection in kernel. From the perspective, it's reasonable to extend the syscall to support PowerNV platform so that OPAL call can be invoked in host kernel for injecting errors. The data transported between userland and kerenl is still following "struct rtas_args" for both cases of PowerNV (OPAL) and pSeries (RTAS).

The series of patches requires corresponding firmware changes from Mike Qiu to support error injection and QEMU changes to support EEH for guest. QEMU patchset will be sent separately.

Change log
=====
v1 -> v2:
* EEH RTAS requests are routed to QEMU, and then possiblly to host kerenl. The mechanism KVM in-kernel handling is dropped.
* Error injection is reimplemented based syscall, instead of KVM in-kerenl handling. The logic for error injection token management is moved to QEMU. The error injection request is routed to QEMU and then possiblly to host kernel.
v2 -> v3:
* Make the fields in struct eeh_vfio_pci_addr, struct vfio_eeh_info based on the comments from Alexey.
* Define macros for EEH VFIO operations (Alexey).
* Clear frozen state after successful PE reset.
* Merge original [PATCH 1/2/3] to one.

Testing on P7
======>
- Emulex adapter

Testing on P8
======>
- Need more testing after design is finalized.

-----

Gavin Shan (8):
drivers/vfio: Introduce CONFIG_VFIO_EEH powerpc/eeh: Info to trace passed devices drivers/vfio: New IOCTL command VFIO_EEH_INFO powerpc/eeh: Avoid event on passed PE powerpc/powernv: Sync OPAL header file with firmware
powerpc: Extend syscall ppc_rtas() powerpc/powernv: Implement ppc_call_opal() powerpc/powernv: Error injection infrastructure

  arch/powerpc/include/asm/eeh.h                 |  52 +++
  arch/powerpc/include/asm/opal.h                |  74 ++-
  arch/powerpc/include/asm/rtas.h                |  10 +-
  arch/powerpc/include/asm/syscalls.h            |   2 +-
  arch/powerpc/include/asm/systbl.h              |   2 +-
  arch/powerpc/include/uapi/asm/unistd.h         |   2 +-
  arch/powerpc/kernel/eeh.c                      |   8 +
  arch/powerpc/kernel/eeh_pe.c                   |  80 ++++
  arch/powerpc/kernel/rtas.c                     |  57 +--
  arch/powerpc/kernel/syscalls.c                 |  50 +++
  arch/powerpc/platforms/powernv/Makefile        |   3 +-
  arch/powerpc/platforms/powernv/eeh-ioda.c      |   3 +-
  arch/powerpc/platforms/powernv/eeh-vfio.c      | 593 +++++++++++++++++++++++++
  arch/powerpc/platforms/powernv/errinject.c     | 224 ++++++++++
  arch/powerpc/platforms/powernv/opal-wrappers.S |   1 +
  arch/powerpc/platforms/powernv/opal.c          |  93 ++++
  drivers/vfio/Kconfig                           |   6 +
  drivers/vfio/vfio_iommu_spapr_tce.c            |  12 +
  include/uapi/linux/vfio.h                      |  57 +++
  kernel/sys_ni.c                                |   2 +-
  20 files changed, 1278 insertions(+), 53 deletions(-)

create mode 100644 arch/powerpc/platforms/powernv/eeh-vfio.c create mode 100644 arch/powerpc/platforms/powernv/errinject.c

Thanks,
Gavin

author
#27 lore raw
Quoted 4 lines · Mike Qiu

Hi Gavin,

Can error injection be done if EEH is not enbaled?

Yes, utility "errinject" still can inject PCI errors to host for testing purpose. Since the PCI errors injected by "errinjct" is mainly for EEH testing. it would be meaningless to do PCI error injection without EEH support. So for the case, the hooks doing real work is being protected by CONFIG_EEH.

The hooks for another case (error injection from "errinject" utility running on guest) are protected by CONFIG_EEH_VFIO

Thanks,
Gavin

Quoted 87 lines · Gavin Shan
Quoted 85 lines

The series of patches intends to support EEH for PCI devices, which are passed through to PowerKVM based guest via VFIO. The implementation is straightforward based on the issues or problems we have to resolve to support EEH for PowerKVM based guest.

- Emulation for EEH RTAS requests. All EEH RTAS requests goes to QEMU firstly. If QEMU can't handle it, the request will be sent to host via newly introduced VFIO container IOCTL command (VFIO_EEH_INFO) and gets handled in host kernel.

- The error injection infrastructure need support request from the userland utility "errinjct" and PowerKVM based guest. The userland utility "errinjct" works on pSeries platform well with dedicated syscall, which helps invoking RTAS service to fulfil error injection in kernel. From the perspective, it's reasonable to extend the syscall to support PowerNV platform so that OPAL call can be invoked in host kernel for injecting errors. The data transported between userland and kerenl is still following "struct rtas_args" for both cases of PowerNV (OPAL) and pSeries (RTAS).

The series of patches requires corresponding firmware changes from Mike Qiu to support error injection and QEMU changes to support EEH for guest. QEMU patchset will be sent separately.

Change log
=====
v1 -> v2:
* EEH RTAS requests are routed to QEMU, and then possiblly to host kerenl. The mechanism KVM in-kernel handling is dropped.
* Error injection is reimplemented based syscall, instead of KVM in-kerenl handling. The logic for error injection token management is moved to QEMU. The error injection request is routed to QEMU and then possiblly to host kernel.
v2 -> v3:
* Make the fields in struct eeh_vfio_pci_addr, struct vfio_eeh_info based on the comments from Alexey.
* Define macros for EEH VFIO operations (Alexey).
* Clear frozen state after successful PE reset.
* Merge original [PATCH 1/2/3] to one.

Testing on P7
======>>
- Emulex adapter

Testing on P8
======>>
- Need more testing after design is finalized.

-----

Gavin Shan (8):
drivers/vfio: Introduce CONFIG_VFIO_EEH powerpc/eeh: Info to trace passed devices drivers/vfio: New IOCTL command VFIO_EEH_INFO powerpc/eeh: Avoid event on passed PE powerpc/powernv: Sync OPAL header file with firmware
powerpc: Extend syscall ppc_rtas() powerpc/powernv: Implement ppc_call_opal() powerpc/powernv: Error injection infrastructure

 arch/powerpc/include/asm/eeh.h                 |  52 +++
 arch/powerpc/include/asm/opal.h                |  74 ++-
 arch/powerpc/include/asm/rtas.h                |  10 +-
 arch/powerpc/include/asm/syscalls.h            |   2 +-
 arch/powerpc/include/asm/systbl.h              |   2 +-
 arch/powerpc/include/uapi/asm/unistd.h         |   2 +-
 arch/powerpc/kernel/eeh.c                      |   8 +
 arch/powerpc/kernel/eeh_pe.c                   |  80 ++++
 arch/powerpc/kernel/rtas.c                     |  57 +--
 arch/powerpc/kernel/syscalls.c                 |  50 +++
 arch/powerpc/platforms/powernv/Makefile        |   3 +-
 arch/powerpc/platforms/powernv/eeh-ioda.c      |   3 +-
 arch/powerpc/platforms/powernv/eeh-vfio.c      | 593 +++++++++++++++++++++++++
 arch/powerpc/platforms/powernv/errinject.c     | 224 ++++++++++
 arch/powerpc/platforms/powernv/opal-wrappers.S |   1 +
 arch/powerpc/platforms/powernv/opal.c          |  93 ++++
 drivers/vfio/Kconfig                           |   6 +
 drivers/vfio/vfio_iommu_spapr_tce.c            |  12 +
 include/uapi/linux/vfio.h                      |  57 +++
 kernel/sys_ni.c                                |   2 +-
 20 files changed, 1278 insertions(+), 53 deletions(-)
 create mode 100644 arch/powerpc/platforms/powernv/eeh-vfio.c
 create mode 100644 arch/powerpc/platforms/powernv/errinject.c

Thanks,
Gavin

author
#28 lore raw
In reply to Gavin Shan

On Wed, May 14, 2014 at 02:11:54PM +1000, Gavin Shan wrote:

Ping, Alex.G and Alex.W. Could you please take a look on this when you have available bandwidth? Thanks in advance for your time :-)

Quoted 79 lines

The series of patches intends to support EEH for PCI devices, which are passed through to PowerKVM based guest via VFIO. The implementation is straightforward based on the issues or problems we have to resolve to support EEH for PowerKVM based guest.

- Emulation for EEH RTAS requests. All EEH RTAS requests goes to QEMU firstly. If QEMU can't handle it, the request will be sent to host via newly introduced VFIO container IOCTL command (VFIO_EEH_INFO) and gets handled in host kernel.

- The error injection infrastructure need support request from the userland utility "errinjct" and PowerKVM based guest. The userland utility "errinjct" works on pSeries platform well with dedicated syscall, which helps invoking RTAS service to fulfil error injection in kernel. From the perspective, it's reasonable to extend the syscall to support PowerNV platform so that OPAL call can be invoked in host kernel for injecting errors. The data transported between userland and kerenl is still following "struct rtas_args" for both cases of PowerNV (OPAL) and pSeries (RTAS).

The series of patches requires corresponding firmware changes from Mike Qiu to support error injection and QEMU changes to support EEH for guest. QEMU patchset will be sent separately.

Change log
=====
v1 -> v2:
* EEH RTAS requests are routed to QEMU, and then possiblly to host kerenl. The mechanism KVM in-kernel handling is dropped.
* Error injection is reimplemented based syscall, instead of KVM in-kerenl handling. The logic for error injection token management is moved to QEMU. The error injection request is routed to QEMU and then possiblly to host kernel.
v2 -> v3:
* Make the fields in struct eeh_vfio_pci_addr, struct vfio_eeh_info based on the comments from Alexey.
* Define macros for EEH VFIO operations (Alexey).
* Clear frozen state after successful PE reset.
* Merge original [PATCH 1/2/3] to one.

Testing on P7
======>
- Emulex adapter

Testing on P8
======>
- Need more testing after design is finalized.

-----

Gavin Shan (8):
drivers/vfio: Introduce CONFIG_VFIO_EEH powerpc/eeh: Info to trace passed devices drivers/vfio: New IOCTL command VFIO_EEH_INFO powerpc/eeh: Avoid event on passed PE powerpc/powernv: Sync OPAL header file with firmware
powerpc: Extend syscall ppc_rtas() powerpc/powernv: Implement ppc_call_opal() powerpc/powernv: Error injection infrastructure

arch/powerpc/include/asm/eeh.h                 |  52 +++
arch/powerpc/include/asm/opal.h                |  74 ++-
arch/powerpc/include/asm/rtas.h                |  10 +-
arch/powerpc/include/asm/syscalls.h            |   2 +-
arch/powerpc/include/asm/systbl.h              |   2 +-
arch/powerpc/include/uapi/asm/unistd.h         |   2 +-
arch/powerpc/kernel/eeh.c                      |   8 +
arch/powerpc/kernel/eeh_pe.c                   |  80 ++++
arch/powerpc/kernel/rtas.c                     |  57 +--
arch/powerpc/kernel/syscalls.c                 |  50 +++
arch/powerpc/platforms/powernv/Makefile        |   3 +-
arch/powerpc/platforms/powernv/eeh-ioda.c      |   3 +-
arch/powerpc/platforms/powernv/eeh-vfio.c      | 593 +++++++++++++++++++++++++
arch/powerpc/platforms/powernv/errinject.c     | 224 ++++++++++
arch/powerpc/platforms/powernv/opal-wrappers.S |   1 +
arch/powerpc/platforms/powernv/opal.c          |  93 ++++
drivers/vfio/Kconfig                           |   6 +
drivers/vfio/vfio_iommu_spapr_tce.c            |  12 +
include/uapi/linux/vfio.h                      |  57 +++
kernel/sys_ni.c                                |   2 +-
20 files changed, 1278 insertions(+), 53 deletions(-)
Quoted 3 lines

create mode 100644 arch/powerpc/platforms/powernv/eeh-vfio.c create mode 100644 arch/powerpc/platforms/powernv/errinject.c

Thanks,
Gavin