Received: by 2002:ac0:950e:0:0:0:0:0 with SMTP id f14csp1321527imc; Sun, 17 Mar 2019 10:26:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqzemPulf2uCRht2QRUKJvKuNvCTSq+1tY1Ljnszqg/U7EDsdJ/0fYXD0qi2Bk53HY5ziuSZ X-Received: by 2002:a62:bd09:: with SMTP id a9mr14762719pff.61.1552843593394; Sun, 17 Mar 2019 10:26:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552843593; cv=none; d=google.com; s=arc-20160816; b=XcsIiYkEepRSmnuNqvQ10qw0DxMopYF/gslpCCV1kJ4Fbn/sPre8D/AOJJ7qoaCRZa f+zDmHUzAw3qDJ90G6vsjGBCnlceU+5QlhAwd0wIYLLmectOHg/FKG3hHyVXGbZhZAcD YgfH0X7++z0Pqk+1RIpbpheS1KAY3LSC+oSoeSyPVxJ0vHlLAqAF5M0nS8g9FOKYG+zW U2itKDCIkP3MQDZj5Jfz7sV0lGe0WNxJyllOvXlnWGZYuOhpRo6/Rf3wiZiSsBb1zR3B QDT8ZDQFkGF+TH//utZLrl+oPUU6kxxuOebMI6rh1vpdvUzmrJH2P2I2oBaPYtZBDpB+ iRuw== 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=6VcxBDRwff1EG6rXverXlI/8ocCarYad4roBHYgbutY=; b=FI2rQnr4E8ZPLzKlzeHzNKNJdy+E8hRTpP4ClDWqLn2nJLfR+6zzs3E41WFc09093y Uo2/vBTUlEFnXOYAi0LdTqPO31Ycsc6mowhwOZUVUPMVSP9wBplS6IECwI10OZw8acAT 9GoSmuGtUyvqFNKWcFDzox3t7y3tIE876q03xFCLZoVZ64DoiEKMPDq3pi3ASPjmrUvX Pjp2Y8UH15kzGtUiGSoN47lPgoaCKf6dKk6p+UYPyP5PWsMolR1kWyNltKdZjzvDqXV3 zkyTHN5/sS3XnXxtrc4Uq1nUGD0QGUUvYiyCxfDpwpZfGV9ZpYXkWrONmC76s6Q+v+PW LLgQ== 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 a4si7343562plp.431.2019.03.17.10.26.18; Sun, 17 Mar 2019 10:26:33 -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 S1727693AbfCQRYw (ORCPT + 99 others); Sun, 17 Mar 2019 13:24:52 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51692 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727383AbfCQRYu (ORCPT ); Sun, 17 Mar 2019 13:24:50 -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 71BF33084214; Sun, 17 Mar 2019 17:24:49 +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 621BB19C71; Sun, 17 Mar 2019 17:24:45 +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 20/22] vfio_pci: Allow to mmap the fault queue Date: Sun, 17 Mar 2019 18:22:30 +0100 Message-Id: <20190317172232.1068-21-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.40]); Sun, 17 Mar 2019 17:24:49 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The Producer Fault region contains the fault queue in the second page. There is benefit to let the userspace mmap this area. So let's expose this mmappable area through a sparse mmap entry and implement the mmap operation. Signed-off-by: Eric Auger --- drivers/vfio/pci/vfio_pci.c | 61 +++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index cf12204486c3..8c895ece4750 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -274,15 +274,70 @@ static const struct vfio_pci_fault_abi fault_abi_versions[] = { #define NR_FAULT_ABIS ARRAY_SIZE(fault_abi_versions) +static int vfio_pci_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 index, ret; + + index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT); + + phys_len = region->size; + + req_len = vma->vm_end - vma->vm_start; + pgoff = vma->vm_pgoff & + ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1); + req_start = 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 = virt_to_phys(vdev->fault_pages); + vma->vm_private_data = vdev; + vma->vm_pgoff = (addr >> PAGE_SHIFT) + pgoff; + + ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, + req_len, vma->vm_page_prot); + return ret; +} + static int vfio_pci_fault_prod_add_capability(struct vfio_pci_device *vdev, struct vfio_pci_region *region, struct vfio_info_cap *caps) { + struct vfio_region_info_cap_sparse_mmap *sparse = NULL; struct vfio_region_info_cap_fault cap = { .header.id = VFIO_REGION_INFO_CAP_PRODUCER_FAULT, .header.version = 1, .version = NR_FAULT_ABIS, }; - return vfio_info_add_capability(caps, &cap.header, sizeof(cap)); + size_t size = sizeof(*sparse) + sizeof(*sparse->areas); + int ret; + + ret = vfio_info_add_capability(caps, &cap.header, sizeof(cap)); + if (ret) + return ret; + + sparse = kzalloc(size, GFP_KERNEL); + if (!sparse) + return -ENOMEM; + + sparse->header.id = VFIO_REGION_INFO_CAP_SPARSE_MMAP; + sparse->header.version = 1; + sparse->nr_areas = 1; + sparse->areas[0].offset = PAGE_SIZE; + sparse->areas[0].size = PAGE_SIZE; + + ret = vfio_info_add_capability(caps, &sparse->header, size); + if (ret) + kfree(sparse); + + return ret; } static const struct vfio_pci_regops vfio_pci_fault_cons_regops = { @@ -293,6 +348,7 @@ static const struct vfio_pci_regops vfio_pci_fault_cons_regops = { static const struct vfio_pci_regops vfio_pci_fault_prod_regops = { .rw = vfio_pci_fault_prod_rw, .release = vfio_pci_fault_release, + .mmap = vfio_pci_fault_mmap, .add_capability = vfio_pci_fault_prod_add_capability, }; @@ -351,7 +407,8 @@ static int vfio_pci_init_fault_region(struct vfio_pci_device *vdev) VFIO_REGION_TYPE_NESTED, VFIO_REGION_SUBTYPE_NESTED_FAULT_PROD, &vfio_pci_fault_prod_regops, 2 * PAGE_SIZE, - VFIO_REGION_INFO_FLAG_READ, vdev->fault_pages); + VFIO_REGION_INFO_FLAG_READ | VFIO_REGION_INFO_FLAG_MMAP, + vdev->fault_pages); if (ret) goto out; -- 2.20.1