Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2620796pxk; Sun, 6 Sep 2020 07:16:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyMAzMD1hD6vNVEzW/57jC7xjg+/xs3QrFoY2aoM3oWtwjfZRTTxa8lajTnjTwndJsaxoeP X-Received: by 2002:aa7:d959:: with SMTP id l25mr17003285eds.383.1599401772505; Sun, 06 Sep 2020 07:16:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599401772; cv=none; d=google.com; s=arc-20160816; b=g7sPkfR0nZ33lNqI+QanyyIUf++u4SOOGmck90OzHRA1Pu8Em7zMsE1aJZl0keZk81 GytnmTnShdIPjKFPq/6Sg5DlhJEXVRjd0iSgdi6Iu1YiWbJHh7CuDxB2FQunYAh766ro c8ZQ6ot2ZpAnKIDeBaCQigDd1za+DIkdiDH+bRxPbj2Usp6V1B5srLg7Gwa71Dux+9qy yWuI8Wk+25tnBZeGb3lHkIDFK2/u/i5rZMdCz9vmpuy0GVN9rI5rQF8hoJQrEIsPbYQG 3EzRWyaWTOhtRyYcTjvqeitzgtI1hX/UR7EeWoVQQ+rH2qsUaGqvDR3/43Eg5gtSF+fy ObfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=kYsgw732/Pw7J5iGJaQDGJt2fMz1OOGuN7fWx4hSQug=; b=SkXQQeyaITK3vcJ27M8ubYaS3gXYSeKvvPfIgrmX7rmF5AWL9jy5l9iuN3kMbwxBiw 8YbhCQ0IxuddbSU457ID4QHCriItlMCejRN/ktXiSbGFXGwZTS5TCZGjK3tw86eA7Jkc BUiDhDJgREBiHJLgOSuCZhkvHj3rjKzbVSGVL+0xvbAyQqT3w2XziMN+HLrQGq+V9pHI 2mII8LLh+Xtj1MYtAqS3wXzPkXCXP9uBz84njpDtfc8713tmQzsy+F+esgmqOADG7XCT TczjSD6bDQmo//lgYtotipxsN+zSNyzEMAQKMuWOer5OdY04GQWHwK6LVqO3jsaJ6Ktv sunA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=vmware.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c25si7794912edr.545.2020.09.06.07.15.47; Sun, 06 Sep 2020 07:16:12 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=vmware.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728865AbgIFOMU (ORCPT + 99 others); Sun, 6 Sep 2020 10:12:20 -0400 Received: from ex13-edg-ou-002.vmware.com ([208.91.0.190]:38470 "EHLO EX13-EDG-OU-002.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726931AbgIFOMK (ORCPT ); Sun, 6 Sep 2020 10:12:10 -0400 Received: from sc9-mailhost3.vmware.com (10.113.161.73) by EX13-EDG-OU-002.vmware.com (10.113.208.156) with Microsoft SMTP Server id 15.0.1156.6; Sun, 6 Sep 2020 07:12:04 -0700 Received: from akaher-virtual-machine.eng.vmware.com (unknown [10.197.103.239]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id B392B408D4; Sun, 6 Sep 2020 07:12:03 -0700 (PDT) From: Ajay Kaher To: CC: , , , , , , , , , Subject: [PATCH v5.4.y 1/3] vfio/type1: Support faulting PFNMAP vmas Date: Sun, 6 Sep 2020 19:37:54 +0530 Message-ID: <1599401277-32172-1-git-send-email-akaher@vmware.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain Received-SPF: None (EX13-EDG-OU-002.vmware.com: akaher@vmware.com does not designate permitted sender hosts) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org commit 41311242221e3482b20bfed10fa4d9db98d87016 upstream. With conversion to follow_pfn(), DMA mapping a PFNMAP range depends on the range being faulted into the vma. Add support to manually provide that, in the same way as done on KVM with hva_to_pfn_remapped(). Reviewed-by: Peter Xu Signed-off-by: Alex Williamson Signed-off-by: Ajay Kaher --- drivers/vfio/vfio_iommu_type1.c | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 6cc47af..d679996 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -335,6 +335,32 @@ static int put_pfn(unsigned long pfn, int prot) return 0; } +static int follow_fault_pfn(struct vm_area_struct *vma, struct mm_struct *mm, + unsigned long vaddr, unsigned long *pfn, + bool write_fault) +{ + int ret; + + ret = follow_pfn(vma, vaddr, pfn); + if (ret) { + bool unlocked = false; + + ret = fixup_user_fault(NULL, mm, vaddr, + FAULT_FLAG_REMOTE | + (write_fault ? FAULT_FLAG_WRITE : 0), + &unlocked); + if (unlocked) + return -EAGAIN; + + if (ret) + return ret; + + ret = follow_pfn(vma, vaddr, pfn); + } + + return ret; +} + static int vaddr_get_pfn(struct mm_struct *mm, unsigned long vaddr, int prot, unsigned long *pfn) { @@ -377,12 +403,16 @@ static int vaddr_get_pfn(struct mm_struct *mm, unsigned long vaddr, vaddr = untagged_addr(vaddr); +retry: vma = find_vma_intersection(mm, vaddr, vaddr + 1); if (vma && vma->vm_flags & VM_PFNMAP) { - if (!follow_pfn(vma, vaddr, pfn) && - is_invalid_reserved_pfn(*pfn)) - ret = 0; + ret = follow_fault_pfn(vma, mm, vaddr, pfn, prot & IOMMU_WRITE); + if (ret == -EAGAIN) + goto retry; + + if (!ret && !is_invalid_reserved_pfn(*pfn)) + ret = -EFAULT; } up_read(&mm->mmap_sem); -- 2.7.4