diff mbox series

[v11,05/13] vfio/pci: Register an iommu fault handler

Message ID 20201116110030.32335-6-eric.auger@redhat.com (mailing list archive)
State New, archived
Headers show
Series SMMUv3 Nested Stage Setup (VFIO part) | expand

Commit Message

Eric Auger Nov. 16, 2020, 11 a.m. UTC
Register an IOMMU fault handler which records faults in
the DMA FAULT region ring buffer. In a subsequent patch, we
will add the signaling of a specific eventfd to allow the
userspace to be notified whenever a new fault as shown up.

Signed-off-by: Eric Auger <eric.auger@redhat.com>

---
v11 -> v12:
- take the fault_queue_lock before reading header (Zenghui)
- also record recoverable errors

v10 -> v11:
- move iommu_unregister_device_fault_handler into
  vfio_pci_disable
- check fault_pages != 0

v8 -> v9:
- handler now takes an iommu_fault handle
- eventfd signaling moved to a subsequent patch
- check the fault type and return an error if != UNRECOV
- still the fault handler registration can fail. We need to
  reach an agreement about how to deal with the situation

v3 -> v4:
- move iommu_unregister_device_fault_handler to vfio_pci_release
---
 drivers/vfio/pci/vfio_pci.c | 45 +++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

Comments

kernel test robot Nov. 16, 2020, 1:51 p.m. UTC | #1
Hi Eric,

I love your patch! Perhaps something to improve:

[auto build test WARNING on v5.10-rc4]
[also build test WARNING on next-20201116]
[cannot apply to vfio/next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Eric-Auger/SMMUv3-Nested-Stage-Setup-VFIO-part/20201116-190742
base:    09162bc32c880a791c6c0668ce0745cf7958f576
config: powerpc64-randconfig-r026-20201116 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project c044709b8fbea2a9a375e4173a6bd735f6866c0c)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install powerpc64 cross compiling tool for clang build
        # apt-get install binutils-powerpc64-linux-gnu
        # https://github.com/0day-ci/linux/commit/747ef402696e1192684908ca99f06f3d68466c04
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Eric-Auger/SMMUv3-Nested-Stage-Setup-VFIO-part/20201116-190742
        git checkout 747ef402696e1192684908ca99f06f3d68466c04
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=powerpc64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

   In file included from drivers/vfio/pci/vfio_pci.c:26:
   In file included from include/linux/vfio.h:16:
   include/uapi/linux/vfio.h:1231:34: error: field has incomplete type 'struct iommu_pasid_table_config'
           struct iommu_pasid_table_config config; /* used on SET */
                                           ^
   include/uapi/linux/vfio.h:1231:9: note: forward declaration of 'struct iommu_pasid_table_config'
           struct iommu_pasid_table_config config; /* used on SET */
                  ^
>> drivers/vfio/pci/vfio_pci.c:339:5: warning: no previous prototype for function 'vfio_pci_iommu_dev_fault_handler' [-Wmissing-prototypes]
   int vfio_pci_iommu_dev_fault_handler(struct iommu_fault *fault, void *data)
       ^
   drivers/vfio/pci/vfio_pci.c:339:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   int vfio_pci_iommu_dev_fault_handler(struct iommu_fault *fault, void *data)
   ^
   static 
   1 warning and 1 error generated.

vim +/vfio_pci_iommu_dev_fault_handler +339 drivers/vfio/pci/vfio_pci.c

   338	
 > 339	int vfio_pci_iommu_dev_fault_handler(struct iommu_fault *fault, void *data)
   340	{
   341		struct vfio_pci_device *vdev = (struct vfio_pci_device *)data;
   342		struct vfio_region_dma_fault *reg =
   343			(struct vfio_region_dma_fault *)vdev->fault_pages;
   344		struct iommu_fault *new;
   345		u32 head, tail, size;
   346		int ret = -EINVAL;
   347	
   348	
   349		if (WARN_ON(!reg))
   350			return ret;
   351	
   352		mutex_lock(&vdev->fault_queue_lock);
   353	
   354		head = reg->head;
   355		tail = reg->tail;
   356		size = reg->nb_entries;
   357	
   358		new = (struct iommu_fault *)(vdev->fault_pages + reg->offset +
   359					     head * reg->entry_size);
   360	
   361		if (CIRC_SPACE(head, tail, size) < 1) {
   362			ret = -ENOSPC;
   363			goto unlock;
   364		}
   365	
   366		*new = *fault;
   367		reg->head = (head + 1) % size;
   368		ret = 0;
   369	unlock:
   370		mutex_unlock(&vdev->fault_queue_lock);
   371		return ret;
   372	}
   373	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
diff mbox series

Patch

diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index 7546a81e7fb6..b39d6ed66c71 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -27,6 +27,7 @@ 
 #include <linux/vgaarb.h>
 #include <linux/nospec.h>
 #include <linux/sched/mm.h>
+#include <linux/circ_buf.h>
 
 #include "vfio_pci_private.h"
 
@@ -335,6 +336,41 @@  static const struct vfio_pci_regops vfio_pci_dma_fault_regops = {
 	.add_capability = vfio_pci_dma_fault_add_capability,
 };
 
+int vfio_pci_iommu_dev_fault_handler(struct iommu_fault *fault, void *data)
+{
+	struct vfio_pci_device *vdev = (struct vfio_pci_device *)data;
+	struct vfio_region_dma_fault *reg =
+		(struct vfio_region_dma_fault *)vdev->fault_pages;
+	struct iommu_fault *new;
+	u32 head, tail, size;
+	int ret = -EINVAL;
+
+
+	if (WARN_ON(!reg))
+		return ret;
+
+	mutex_lock(&vdev->fault_queue_lock);
+
+	head = reg->head;
+	tail = reg->tail;
+	size = reg->nb_entries;
+
+	new = (struct iommu_fault *)(vdev->fault_pages + reg->offset +
+				     head * reg->entry_size);
+
+	if (CIRC_SPACE(head, tail, size) < 1) {
+		ret = -ENOSPC;
+		goto unlock;
+	}
+
+	*new = *fault;
+	reg->head = (head + 1) % size;
+	ret = 0;
+unlock:
+	mutex_unlock(&vdev->fault_queue_lock);
+	return ret;
+}
+
 #define DMA_FAULT_RING_LENGTH 512
 
 static int vfio_pci_dma_fault_init(struct vfio_pci_device *vdev)
@@ -376,6 +412,13 @@  static int vfio_pci_dma_fault_init(struct vfio_pci_device *vdev)
 	header->entry_size = sizeof(struct iommu_fault);
 	header->nb_entries = DMA_FAULT_RING_LENGTH;
 	header->offset = sizeof(struct vfio_region_dma_fault);
+
+	ret = iommu_register_device_fault_handler(&vdev->pdev->dev,
+					vfio_pci_iommu_dev_fault_handler,
+					vdev);
+	if (ret) /* the dma fault region is freed in vfio_pci_disable() */
+		goto out;
+
 	return 0;
 out:
 	kfree(vdev->fault_pages);
@@ -508,6 +551,8 @@  static void vfio_pci_disable(struct vfio_pci_device *vdev)
 				VFIO_IRQ_SET_ACTION_TRIGGER,
 				vdev->irq_type, 0, 0, NULL);
 
+	WARN_ON(iommu_unregister_device_fault_handler(&vdev->pdev->dev));
+
 	/* Device closed, don't need mutex here */
 	list_for_each_entry_safe(ioeventfd, ioeventfd_tmp,
 				 &vdev->ioeventfds_list, next) {