Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp1011676pxb; Sun, 11 Apr 2021 04:50:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx/qulocNcnAsaJs2memuh//JEGoqb1MFJnatduwWJO2/cdRCFF4COHDOLD4qNTQ+WUt1M9 X-Received: by 2002:a17:90b:1114:: with SMTP id gi20mr23809255pjb.23.1618141802077; Sun, 11 Apr 2021 04:50:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618141802; cv=none; d=google.com; s=arc-20160816; b=p2zW98aHjMqc2DP0JT5DAeDJzwTzVIL+iLyhJXBXzFlEFdIwi3HKel6jbbkrleyWFj FltGl0F7JtvMK9SB6XT2vFdi/5kxdvndtOMcj5AW7ORxslRYwS8cvkagInyIxB7zxHaV g+E3fZELPik9IGL8oSl5bwgrHUFNDWBJ2lDu1/xrGW6XqGUPJD/xyjW/HyaTvzqOXC9H oLzgss6AnaBZYZYSQ1+RG/8tydgvup6MDgfl5lqkHQylk0Gmq5ImHvqvv6VxsChiTf9J pAwcLSb8if0+zkpQhli9XMio/7HMbm5Ayr7lWWnd+vSfxxBguN78QcKlVYnTZi6eXZCO GJPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uCo3xDZ5c+S4RyquMkLA7C9gsGABiuJwUVzp5ZRg8rc=; b=SXpBrni5zdtaPYLZHS2/1scQ7c1DBxTJ+eEb+i8Zkn+c5oc8STHu1+8nbebX46juQF 4XfzP56X1rQyJupIgxlWOIXJ8qb8gFlKNIycd0U4QTHqYqOufhBh/HHMgmzgqgQxsmNJ +N/0nCusOZY3fkVX11/w5YlHflrYbiDDJ5K8d8VV6KFg8hhKmH9drzdN/yfXCfTZ53sO xG/DUfYsZwoXnTGq/VtSgawWevGAwfCVPeSks9OMlhN8Rszy8NV0MKUw1F6Fx5qp6AhL SWzmA3IKSzoRD4QR3iKnmBtpRqJPk/cMfVl2GJWLPgnggpWNlIpululQ1tGhi0WVXn2U L9xA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=J+ZvO0gj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y6si10042701plh.60.2021.04.11.04.49.50; Sun, 11 Apr 2021 04:50:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=J+ZvO0gj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235602AbhDKLtI (ORCPT + 99 others); Sun, 11 Apr 2021 07:49:08 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:45064 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235557AbhDKLsg (ORCPT ); Sun, 11 Apr 2021 07:48:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618141699; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uCo3xDZ5c+S4RyquMkLA7C9gsGABiuJwUVzp5ZRg8rc=; b=J+ZvO0gjI309VDWBzwIgj63DUEO99PtOvfCDk3niY+oVdH2Gl5llco6PKiNBy2tZUxxm/a PK6ccGMlxEHyrqOtEEpKzkiQlPjSoVzEjRZ31WI64mlyjvqBU2494L4h4QzVMkVvvvhj7r T09up4mTbVbVzYmcLz5Im4NAtk8p/eY= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-172-m5a4zVrMO3q3jZCzFDXXjw-1; Sun, 11 Apr 2021 07:48:16 -0400 X-MC-Unique: m5a4zVrMO3q3jZCzFDXXjw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7DBA581744F; Sun, 11 Apr 2021 11:48:13 +0000 (UTC) Received: from laptop.redhat.com (ovpn-112-22.ams2.redhat.com [10.36.112.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id 92F8F5C266; Sun, 11 Apr 2021 11:48:05 +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, will@kernel.org, maz@kernel.org, robin.murphy@arm.com, joro@8bytes.org, alex.williamson@redhat.com, tn@semihalf.com, zhukeqian1@huawei.com Cc: jacob.jun.pan@linux.intel.com, yi.l.liu@intel.com, wangxingang5@huawei.com, jean-philippe@linaro.org, zhangfei.gao@linaro.org, zhangfei.gao@gmail.com, vivek.gautam@arm.com, shameerali.kolothum.thodi@huawei.com, yuzenghui@huawei.com, nicoleotsuka@gmail.com, lushenming@huawei.com, vsethi@nvidia.com, chenxiang66@hisilicon.com, vdumpa@nvidia.com, jiangkunkun@huawei.com Subject: [PATCH v13 05/13] vfio/pci: Register an iommu fault handler Date: Sun, 11 Apr 2021 13:46:51 +0200 Message-Id: <20210411114659.15051-6-eric.auger@redhat.com> In-Reply-To: <20210411114659.15051-1-eric.auger@redhat.com> References: <20210411114659.15051-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 has shown up. Signed-off-by: Eric Auger --- v11 -> v12: - take the fault_queue_lock before reading header (Zenghui) - also record recoverable errors - only WARN_ON if the unregistration returns -EBUSY - make vfio_pci_iommu_dev_fault_handler static 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 | 48 ++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index f37cabddf1d6..92840e0f46bf 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "vfio_pci_private.h" @@ -333,6 +334,41 @@ static const struct vfio_pci_regops vfio_pci_dma_fault_regops = { .add_capability = vfio_pci_dma_fault_add_capability, }; +static 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) @@ -377,6 +413,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); @@ -500,7 +543,7 @@ static void vfio_pci_disable(struct vfio_pci_device *vdev) struct pci_dev *pdev = vdev->pdev; struct vfio_pci_dummy_resource *dummy_res, *tmp; struct vfio_pci_ioeventfd *ioeventfd, *ioeventfd_tmp; - int i, bar; + int i, bar, ret; /* Stop the device from further DMA */ pci_clear_master(pdev); @@ -509,6 +552,9 @@ static void vfio_pci_disable(struct vfio_pci_device *vdev) VFIO_IRQ_SET_ACTION_TRIGGER, vdev->irq_type, 0, 0, NULL); + ret = iommu_unregister_device_fault_handler(&vdev->pdev->dev); + WARN_ON(ret == -EBUSY); + /* Device closed, don't need mutex here */ list_for_each_entry_safe(ioeventfd, ioeventfd_tmp, &vdev->ioeventfds_list, next) { -- 2.26.3