Received: by 2002:ac0:950e:0:0:0:0:0 with SMTP id f14csp1321456imc; Sun, 17 Mar 2019 10:26:24 -0700 (PDT) X-Google-Smtp-Source: APXvYqyzOQNL6jeM4e+RPP+bAqDNtRL7n+qPRgX+jEK6F921kHqyuAqaHK/XYGAuq6Ni6llodaBq X-Received: by 2002:a62:8c:: with SMTP id 134mr14831292pfa.27.1552843584117; Sun, 17 Mar 2019 10:26:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552843584; cv=none; d=google.com; s=arc-20160816; b=nv36IUDbHoDsZeEiCH0cm9MPcJlzgAhfR8JLG4+eBtK1J3CALzORUlwlt9b5KqXb5l Mnyi7t63wwn+Y1zRSqoVVqgwm2HR1PRuKF+TPzz+PfkjuSliIGRYwqTJql4PG6j5MfBO kO267RKlBCL51hOOLl3veAsxHQSYhsTRp44Aj4k5bDC9cj5l5iKEtvtYMhKYKb7S80KU 9qHUooETFgstQ+aEq5/OlkNX3r/qDWLHMi7DOZusgnWUWSSkQhx6bDZhOaeKF1ioAyE+ l7l4zsKwD31vIGs1EWwOFZQ8QMZ+7fFV+E1OlCUGR/XkaguguSDDMJLDGrNBce0eVUge f02Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Fvf4c3vz9+Ntb1o8Pd1/Djr5/lxp92X8PhgG9dPmAOw=; b=Zm9Y7PZajcim03BUC2bVmOWuhQ3TxfgjSqau8Ba+cG/IaXdDee83gs6V/djRqBEnCc /Yvfo2CCS81MWCOSz+28E7SU9IqjlkfTwFPn/d7t7PtkM7ODy+C7PP/HEWJDjL/3e8Mu 6afcW4MdCz7iWNQj2qVujpz18gKFziBUfM9+jfnHb9BqG+3+CSmfcjDmbRZ0e2/X7lCz x46vuIBuKGKmFgh5pKd3NEw+y0tVidyTW8jDzczmZUGIErnxVE7mCypAHn51TeShT2Nh /r5c0NjcZaThiQ4bcHUKvejBV8QPanZyBs8M5gfbeF9T5elDW1txuemzjRcSY0blIqY7 83Dw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 20si6914685pgr.200.2019.03.17.10.26.09; Sun, 17 Mar 2019 10:26:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727671AbfCQRYr (ORCPT + 99 others); Sun, 17 Mar 2019 13:24:47 -0400 Received: from mx1.redhat.com ([209.132.183.28]:46066 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727241AbfCQRYp (ORCPT ); Sun, 17 Mar 2019 13:24:45 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0B65883F3C; Sun, 17 Mar 2019 17:24:45 +0000 (UTC) Received: from laptop.redhat.com (ovpn-116-102.ams2.redhat.com [10.36.116.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9277119C71; Sun, 17 Mar 2019 17:24:35 +0000 (UTC) From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, joro@8bytes.org, alex.williamson@redhat.com, jacob.jun.pan@linux.intel.com, yi.l.liu@linux.intel.com, jean-philippe.brucker@arm.com, will.deacon@arm.com, robin.murphy@arm.com Cc: kevin.tian@intel.com, ashok.raj@intel.com, marc.zyngier@arm.com, christoffer.dall@arm.com, peter.maydell@linaro.org, vincent.stehle@arm.com Subject: [PATCH v6 19/22] vfio-pci: Register an iommu fault handler Date: Sun, 17 Mar 2019 18:22:29 +0100 Message-Id: <20190317172232.1068-20-eric.auger@redhat.com> In-Reply-To: <20190317172232.1068-1-eric.auger@redhat.com> References: <20190317172232.1068-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Sun, 17 Mar 2019 17:24:45 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch registers a fault handler which records faults in a circular buffer and then signals an eventfd. This buffer is exposed within the fault region. Signed-off-by: Eric Auger --- v3 -> v4: - move iommu_unregister_device_fault_handler to vfio_pci_release --- drivers/vfio/pci/vfio_pci.c | 49 +++++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_private.h | 1 + 2 files changed, 50 insertions(+) diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index 01b1b4cb8349..cf12204486c3 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "vfio_pci_private.h" @@ -295,6 +296,46 @@ static const struct vfio_pci_regops vfio_pci_fault_prod_regops = { .add_capability = vfio_pci_fault_prod_add_capability, }; +int vfio_pci_iommu_dev_fault_handler(struct iommu_fault_event *evt, void *data) +{ + struct vfio_pci_device *vdev = (struct vfio_pci_device *) data; + struct vfio_region_fault_prod *prod_region = + (struct vfio_region_fault_prod *)vdev->fault_pages; + struct vfio_region_fault_cons *cons_region = + (struct vfio_region_fault_cons *)(vdev->fault_pages + 2 * PAGE_SIZE); + struct iommu_fault *new = + (struct iommu_fault *)(vdev->fault_pages + prod_region->offset + + prod_region->prod * prod_region->entry_size); + int prod, cons, size; + + mutex_lock(&vdev->fault_queue_lock); + + if (!vdev->fault_abi) + goto unlock; + + prod = prod_region->prod; + cons = cons_region->cons; + size = prod_region->nb_entries; + + if (CIRC_SPACE(prod, cons, size) < 1) + goto unlock; + + *new = evt->fault; + prod = (prod + 1) % size; + prod_region->prod = prod; + mutex_unlock(&vdev->fault_queue_lock); + + mutex_lock(&vdev->igate); + if (vdev->dma_fault_trigger) + eventfd_signal(vdev->dma_fault_trigger, 1); + mutex_unlock(&vdev->igate); + return 0; + +unlock: + mutex_unlock(&vdev->fault_queue_lock); + return -EINVAL; +} + static int vfio_pci_init_fault_region(struct vfio_pci_device *vdev) { struct vfio_region_fault_prod *header; @@ -327,6 +368,13 @@ static int vfio_pci_init_fault_region(struct vfio_pci_device *vdev) header = (struct vfio_region_fault_prod *)vdev->fault_pages; header->version = -1; header->offset = PAGE_SIZE; + + ret = iommu_register_device_fault_handler(&vdev->pdev->dev, + vfio_pci_iommu_dev_fault_handler, + vdev); + if (ret) + goto out; + return 0; out: kfree(vdev->fault_pages); @@ -574,6 +622,7 @@ static void vfio_pci_release(void *device_data) if (!(--vdev->refcnt)) { vfio_spapr_pci_eeh_release(vdev->pdev); vfio_pci_disable(vdev); + iommu_unregister_device_fault_handler(&vdev->pdev->dev); } mutex_unlock(&vdev->reflck->lock); diff --git a/drivers/vfio/pci/vfio_pci_private.h b/drivers/vfio/pci/vfio_pci_private.h index 8e0a55682d3f..a9276926f008 100644 --- a/drivers/vfio/pci/vfio_pci_private.h +++ b/drivers/vfio/pci/vfio_pci_private.h @@ -122,6 +122,7 @@ struct vfio_pci_device { int ioeventfds_nr; struct eventfd_ctx *err_trigger; struct eventfd_ctx *req_trigger; + struct eventfd_ctx *dma_fault_trigger; struct mutex fault_queue_lock; int fault_abi; struct list_head dummy_resources_list; -- 2.20.1