Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp868857ybb; Fri, 20 Mar 2020 09:22:46 -0700 (PDT) X-Google-Smtp-Source: ADFU+vv2sP7jC9istpMxsmBQT3vwc7vhJzG/+N0q484HeNqJKW3lf2z9OTDCPG183vt7kvQ9avAR X-Received: by 2002:aca:d987:: with SMTP id q129mr6830855oig.64.1584721366821; Fri, 20 Mar 2020 09:22:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584721366; cv=none; d=google.com; s=arc-20160816; b=SfLYlEah+hIYsdpKezNl2LTo7Pr4a7AWe4tg2oI9bVa6YC7iZLuihxBNq0PIo8/XiQ gZLxW1kOZyuw37wzwLcEubKhJl6VaD5D8KwyJr0YyJArjILEBZCq0kB/2zTbdrspsHwP iNwR2t2fQovPwkwVtsor1mZKJg34/SwzSVm/1s1GJMKIyEkGWAHGY1TGbpq6NNCq4hHI x8B9OSBVj/UMEWUQUjK1SZuMCOEwy48NFLD/FzFupg3J8B8hVd6domXuiZ6HQtC6k9vI AoLmfatcPya7hCOKfzOKyGPMJh4/yc0Eu1UkPt1ZEfVMPHYbu6Lv63fXrQGAJz07cR3D IC5A== 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 :dkim-signature; bh=mhzJgSkCJsNgfAjdjebamfXi6WM/A0Mk2yWVsVfPFeA=; b=MfoWn34Spye8XSpZPYkRdoFqF9O8ahbIhZBFiQmHI8pNQQpQB4Unm9p4Lwyk9244f0 kix3hMhS2pV6W3xzZ6Y8k7jgFD5b5VE0yBmv3cxno+kTP6cPJXQHM3p4Ujzfct1moejB hRWRyjZTS9FpbIpFeIt1NVvXBHrDjLSBbcOeFiJgiNjqr5Yo1FIGG3+P1hMnB/+LvJ7b 2GeJTtjVSGlB4MSrcPqHaKiK+5LkEZp2fEjJk1cMWd9zPNLvcvZv7z5sSwmQV4f/vOz+ jm52ecHrO31DdkUuHr3z64qQu729oA0pbs2AN6HpAxl0hZPH4FzLImRUSYCBDQU4C4Dv AGeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DAPqlV0z; 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=pass (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 r71si2899799oie.265.2020.03.20.09.22.27; Fri, 20 Mar 2020 09:22:46 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DAPqlV0z; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727724AbgCTQUX (ORCPT + 99 others); Fri, 20 Mar 2020 12:20:23 -0400 Received: from us-smtp-delivery-74.mimecast.com ([63.128.21.74]:47394 "EHLO us-smtp-delivery-74.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727730AbgCTQUW (ORCPT ); Fri, 20 Mar 2020 12:20:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584721220; 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=mhzJgSkCJsNgfAjdjebamfXi6WM/A0Mk2yWVsVfPFeA=; b=DAPqlV0zLojrsphrmYJ7UkRSfBPWWm98tzk/OXksc03NhL8LWznEZKiMpKKdM1FnZXgSw8 A12VuVnguVESfPBYaxi3g2HbzkJUOW9OMu/ewgAMbULrCGj9L7pvwS66EfQGMZ2oQ/ZFCZ WCQbB8BF9KyBcNczjkhL1VOR/WtWF8A= 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-44-LMNIg5YeMteTqmLbGVuhnw-1; Fri, 20 Mar 2020 12:20:19 -0400 X-MC-Unique: LMNIg5YeMteTqmLbGVuhnw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1B1CC100550D; Fri, 20 Mar 2020 16:20:17 +0000 (UTC) Received: from laptop.redhat.com (ovpn-113-142.ams2.redhat.com [10.36.113.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id 61B9A60BF1; Fri, 20 Mar 2020 16:20:12 +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@intel.com, jean-philippe.brucker@arm.com, will.deacon@arm.com, robin.murphy@arm.com Cc: marc.zyngier@arm.com, peter.maydell@linaro.org, zhangfei.gao@gmail.com Subject: [PATCH v10 06/11] vfio/pci: Allow to mmap the fault queue Date: Fri, 20 Mar 2020 17:19:06 +0100 Message-Id: <20200320161911.27494-7-eric.auger@redhat.com> In-Reply-To: <20200320161911.27494-1-eric.auger@redhat.com> References: <20200320161911.27494-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The DMA FAULT region contains the fault ring buffer. There is benefit to let the userspace mmap this area. Expose this mmappable area through a sparse mmap entry and implement the mmap operation. Signed-off-by: Eric Auger --- v8 -> v9: - remove unused index local variable in vfio_pci_fault_mmap --- drivers/vfio/pci/vfio_pci.c | 61 +++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index 69595c240baf..3c99f6f3825b 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -266,21 +266,75 @@ static void vfio_pci_dma_fault_release(struct vfio_= pci_device *vdev, { } =20 +static int vfio_pci_dma_fault_mmap(struct vfio_pci_device *vdev, + struct vfio_pci_region *region, + struct vm_area_struct *vma) +{ + u64 phys_len, req_len, pgoff, req_start; + unsigned long long addr; + unsigned int ret; + + phys_len =3D region->size; + + req_len =3D vma->vm_end - vma->vm_start; + pgoff =3D vma->vm_pgoff & + ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1); + req_start =3D pgoff << PAGE_SHIFT; + + /* only the second page of the producer fault region is mmappable */ + if (req_start < PAGE_SIZE) + return -EINVAL; + + if (req_start + req_len > phys_len) + return -EINVAL; + + addr =3D virt_to_phys(vdev->fault_pages); + vma->vm_private_data =3D vdev; + vma->vm_pgoff =3D (addr >> PAGE_SHIFT) + pgoff; + + ret =3D remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, + req_len, vma->vm_page_prot); + return ret; +} + static int vfio_pci_dma_fault_add_capability(struct vfio_pci_device *vde= v, struct vfio_pci_region *region, struct vfio_info_cap *caps) { + struct vfio_region_info_cap_sparse_mmap *sparse =3D NULL; struct vfio_region_info_cap_fault cap =3D { .header.id =3D VFIO_REGION_INFO_CAP_DMA_FAULT, .header.version =3D 1, .version =3D 1, }; - return vfio_info_add_capability(caps, &cap.header, sizeof(cap)); + size_t size =3D sizeof(*sparse) + sizeof(*sparse->areas); + int ret; + + ret =3D vfio_info_add_capability(caps, &cap.header, sizeof(cap)); + if (ret) + return ret; + + sparse =3D kzalloc(size, GFP_KERNEL); + if (!sparse) + return -ENOMEM; + + sparse->header.id =3D VFIO_REGION_INFO_CAP_SPARSE_MMAP; + sparse->header.version =3D 1; + sparse->nr_areas =3D 1; + sparse->areas[0].offset =3D PAGE_SIZE; + sparse->areas[0].size =3D region->size - PAGE_SIZE; + + ret =3D vfio_info_add_capability(caps, &sparse->header, size); + if (ret) + kfree(sparse); + + return ret; } =20 static const struct vfio_pci_regops vfio_pci_dma_fault_regops =3D { .rw =3D vfio_pci_dma_fault_rw, .release =3D vfio_pci_dma_fault_release, + .mmap =3D vfio_pci_dma_fault_mmap, .add_capability =3D vfio_pci_dma_fault_add_capability, }; =20 @@ -341,7 +395,8 @@ static int vfio_pci_init_dma_fault_region(struct vfio= _pci_device *vdev) VFIO_REGION_TYPE_NESTED, VFIO_REGION_SUBTYPE_NESTED_DMA_FAULT, &vfio_pci_dma_fault_regops, size, - VFIO_REGION_INFO_FLAG_READ | VFIO_REGION_INFO_FLAG_WRITE, + VFIO_REGION_INFO_FLAG_READ | VFIO_REGION_INFO_FLAG_WRITE | + VFIO_REGION_INFO_FLAG_MMAP, vdev->fault_pages); if (ret) goto out; @@ -349,7 +404,7 @@ static int vfio_pci_init_dma_fault_region(struct vfio= _pci_device *vdev) header =3D (struct vfio_region_dma_fault *)vdev->fault_pages; header->entry_size =3D sizeof(struct iommu_fault); header->nb_entries =3D DMA_FAULT_RING_LENGTH; - header->offset =3D sizeof(struct vfio_region_dma_fault); + header->offset =3D PAGE_SIZE; =20 ret =3D iommu_register_device_fault_handler(&vdev->pdev->dev, vfio_pci_iommu_dev_fault_handler, --=20 2.20.1