Received: by 2002:a25:1104:0:0:0:0:0 with SMTP id 4csp526894ybr; Fri, 22 May 2020 12:19:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxP41Pp7qxjkdvViK2bE72e01FE/Hg7iqyWVhX9opzSfaio3rEd2JpqusN6TeCb3NF5BJg0 X-Received: by 2002:a17:906:bc4c:: with SMTP id s12mr9324897ejv.285.1590175166993; Fri, 22 May 2020 12:19:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590175166; cv=none; d=google.com; s=arc-20160816; b=VMAoB9s9VPf/4mhY1hIW2TOtWJdBB3GNk38EKyIEotSWtwRH/oF8wCYRlteA3f9S9b mRXS4StayFKhBzuGGHUUb6UKHur15LSqEVT9ASunhSAihbcAn5lJ0bMfbJF+tmyw5XAm trWAjZyt2oIl/QE32BzWldAD2n/4YsD6FpS54mQIWQNXjg6z6NuFFhA4gVdYXmu+Tfaw 34HzAd1WcQtkAaKdj6a9ONgtnYdU6kIEao23WNXhzHXWs7MdSt2fPn3sGdguiEU7fwY4 442rJqvibtPJH/YjOsiTALhwU29leD2YNSQqAIbybE/m53ViTj//IgXifdTlNnYbAx1q 4mTQ== 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 :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject:dkim-signature; bh=ZgNktiHsIZ924aqvYT3EY50JCjQImiswMfvHQgVPbvU=; b=QDw7t19JwqyEmSCdQVrkjwl+3n3qcrSJGzKybVx7TELDAtUEIqK04oBjXZQ4/i8Hgy g62JswGcH4bEDZfIKvwKZjozHzbUzKR9zBrL+EfLIYVlvaG0wkQY5YtiKziuny0mqE5D ErRyCOfhUNBJWgKyTNtvUc5XNBSlccnlWpDqazKN7RCLkcqJo6acSo7VWJKc1dYSmxBI kBP3T4jZ6EGyp+38UhzV+OGXN64B33IfeEx+Bx1U/ruePGifpLv+jcwlZUDaIx7SsCJH fTJlRyo2SrblVgX/weRmd7/fBiVsrbFV+hym93SZpDBcciHBAQwj1m0dNgEY6ru44oAd L/tQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SkMnWOkF; 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 l10si5454186ejb.576.2020.05.22.12.19.04; Fri, 22 May 2020 12:19:26 -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=SkMnWOkF; 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 S1730981AbgEVTRe (ORCPT + 99 others); Fri, 22 May 2020 15:17:34 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:44899 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730967AbgEVTRd (ORCPT ); Fri, 22 May 2020 15:17:33 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1590175052; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZgNktiHsIZ924aqvYT3EY50JCjQImiswMfvHQgVPbvU=; b=SkMnWOkFTo4XtBLRe2/qFd3CxiJ0GzI0eINB1EvxkjMpfQ3krlz80QfUrhEFiGoJM8Uhwc K1OnCQXxUUDOZ5TEAs4wRnzpWWOKiP31uiiuIju367kKcDUDqoDDhVx/7f3D3T2n40ya4n uPVadkeS1eDX+9dDRgfuRJ4UCRYtJZU= 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-294-pN0Huj8GNWqLoz2Ri4j5gw-1; Fri, 22 May 2020 15:17:28 -0400 X-MC-Unique: pN0Huj8GNWqLoz2Ri4j5gw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AF646474; Fri, 22 May 2020 19:17:27 +0000 (UTC) Received: from gimli.home (ovpn-114-203.phx2.redhat.com [10.3.114.203]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0C9405D9CC; Fri, 22 May 2020 19:17:21 +0000 (UTC) Subject: [PATCH v3 1/3] vfio/type1: Support faulting PFNMAP vmas From: Alex Williamson To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, cohuck@redhat.com, jgg@ziepe.ca, peterx@redhat.com, cai@lca.pw Date: Fri, 22 May 2020 13:17:20 -0600 Message-ID: <159017504067.18853.16324343896241710000.stgit@gimli.home> In-Reply-To: <159017449210.18853.15037950701494323009.stgit@gimli.home> References: <159017449210.18853.15037950701494323009.stgit@gimli.home> User-Agent: StGit/0.19-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 cc1d64765ce7..4a4cb7cd86b2 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -317,6 +317,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) { @@ -339,12 +365,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; } done: up_read(&mm->mmap_sem);