From patchwork Tue Mar 20 21:33:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 10297995 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7102460386 for ; Tue, 20 Mar 2018 21:33:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 62B40296A5 for ; Tue, 20 Mar 2018 21:33:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5747329677; Tue, 20 Mar 2018 21:33:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D16432914E for ; Tue, 20 Mar 2018 21:33:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751985AbeCTVd1 (ORCPT ); Tue, 20 Mar 2018 17:33:27 -0400 Received: from mx1.redhat.com ([209.132.183.28]:50860 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751961AbeCTVdY (ORCPT ); Tue, 20 Mar 2018 17:33:24 -0400 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 mx1.redhat.com (Postfix) with ESMTPS id 326AC8553E; Tue, 20 Mar 2018 21:33:24 +0000 (UTC) Received: from t450s.home (ovpn-116-103.phx2.redhat.com [10.3.116.103]) by smtp.corp.redhat.com (Postfix) with ESMTP id B0A8B5D9CC; Tue, 20 Mar 2018 21:33:23 +0000 (UTC) Date: Tue, 20 Mar 2018 15:33:23 -0600 From: Alex Williamson To: "Jason Cai (Xiang Feng)" Cc: pbonzini@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, gnehzuil@linux.alibaba.com Subject: Re: [PATCH] vfio iommu type1: improve memory pinning process for raw PFN mapping Message-ID: <20180320153323.41c58c19@t450s.home> In-Reply-To: <7F93BB33-4ABF-468F-8814-78DE9D23FA08@linux.alibaba.com> References: <7F93BB33-4ABF-468F-8814-78DE9D23FA08@linux.alibaba.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 20 Mar 2018 21:33:24 +0000 (UTC) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP On Mon, 19 Mar 2018 10:30:24 +0800 "Jason Cai (Xiang Feng)" wrote: > When using vfio to pass through a PCIe device (e.g. a GPU card) that > has a huge BAR (e.g. 16GB), a lot of cycles are wasted on memory > pinning because PFNs of PCI BAR are not backed by struct page, and > the corresponding VMA has flag VM_PFNMAP. > > With this change, when pinning a region which is a raw PFN mapping, > it can skip unnecessary user memory pinning process, and thus, can > significantly improve VM's boot up time when passing through devices > via VFIO. In my test on a Xeon E5 2.6GHz, the time mapping a 16GB > BAR was reduced from about 0.4s to 1.5us. > > Signed-off-by: Jason Cai (Xiang Feng) > --- > drivers/vfio/vfio_iommu_type1.c | 24 ++++++++++++++---------- > 1 file changed, 14 insertions(+), 10 deletions(-) > > diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c > index 45657e2b1ff7..0658f35318b8 100644 > --- a/drivers/vfio/vfio_iommu_type1.c > +++ b/drivers/vfio/vfio_iommu_type1.c > @@ -397,7 +397,6 @@ static long vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr, > { > unsigned long pfn = 0; > long ret, pinned = 0, lock_acct = 0; > - bool rsvd; > dma_addr_t iova = vaddr - dma->vaddr + dma->iova; > > /* This code path is only user initiated */ > @@ -408,14 +407,22 @@ static long vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr, > if (ret) > return ret; > > + if (is_invalid_reserved_pfn(*pfn_base)) { > + struct vm_area_struct *vma; > + down_read(¤t->mm->mmap_sem); > + vma = find_vma_intersection(current->mm, vaddr, vaddr + 1); > + pinned = min(npage, (long)vma_pages(vma)); > + up_read(¤t->mm->mmap_sem); > + return pinned; > + } > + > pinned++; > - rsvd = is_invalid_reserved_pfn(*pfn_base); > > /* > * Reserved pages aren't counted against the user, externally pinned > * pages are already counted against the user. > */ > - if (!rsvd && !vfio_find_vpfn(dma, iova)) { > + if (!vfio_find_vpfn(dma, iova)) { > if (!lock_cap && current->mm->locked_vm + 1 > limit) { > put_pfn(*pfn_base, dma->prot); > pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n", __func__, > @@ -435,13 +442,12 @@ static long vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr, > if (ret) > break; > > - if (pfn != *pfn_base + pinned || > - rsvd != is_invalid_reserved_pfn(pfn)) { > + if (pfn != *pfn_base + pinned) { > put_pfn(pfn, dma->prot); > break; > } > > - if (!rsvd && !vfio_find_vpfn(dma, iova)) { > + if (!vfio_find_vpfn(dma, iova)) { > if (!lock_cap && > current->mm->locked_vm + lock_acct + 1 > limit) { > put_pfn(pfn, dma->prot); > @@ -459,10 +465,8 @@ static long vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr, > > unpin_out: > if (ret) { > - if (!rsvd) { > - for (pfn = *pfn_base ; pinned ; pfn++, pinned--) > - put_pfn(pfn, dma->prot); > - } > + for (pfn = *pfn_base ; pinned ; pfn++, pinned--) > + put_pfn(pfn, dma->prot); > > return ret; > } Hi Jason, Something is wrong with your mail setup, the patch looks normal above, but when I view the source or save it to try to apply it, the diff is corrupt, as below. It looks like maybe you're pasting the patch into your mailer and it's wrapping lines (ending with '=') and actual '=' are replaced with '=3D' and tabs are converted to spaces. Please fix your mailer and resend. Thanks, Alex /* This code path is only user initiated */ @@ -408,14 +407,22 @@ static long vfio_pin_pages_remote(struct vfio_dma = *dma, unsigned long vaddr, if (ret) return ret; + if (is_invalid_reserved_pfn(*pfn_base)) { + struct vm_area_struct *vma; + down_read(¤t->mm->mmap_sem); + vma =3D find_vma_intersection(current->mm, vaddr, vaddr = + 1); + pinned =3D min(npage, (long)vma_pages(vma)); + up_read(¤t->mm->mmap_sem); + return pinned; + } + pinned++; - rsvd =3D is_invalid_reserved_pfn(*pfn_base); /* * Reserved pages aren't counted against the user, externally = pinned * pages are already counted against the user. */ - if (!rsvd && !vfio_find_vpfn(dma, iova)) { + if (!vfio_find_vpfn(dma, iova)) { if (!lock_cap && current->mm->locked_vm + 1 > limit) { put_pfn(*pfn_base, dma->prot); pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n", = __func__, @@ -435,13 +442,12 @@ static long vfio_pin_pages_remote(struct vfio_dma = *dma, unsigned long vaddr, if (ret) break; - if (pfn !=3D *pfn_base + pinned || - rsvd !=3D is_invalid_reserved_pfn(pfn)) { + if (pfn !=3D *pfn_base + pinned) { put_pfn(pfn, dma->prot); break; } - if (!rsvd && !vfio_find_vpfn(dma, iova)) { + if (!vfio_find_vpfn(dma, iova)) { if (!lock_cap && current->mm->locked_vm + lock_acct + 1 > = limit) { put_pfn(pfn, dma->prot); @@ -459,10 +465,8 @@ static long vfio_pin_pages_remote(struct vfio_dma = *dma, unsigned long vaddr, unpin_out: if (ret) { - if (!rsvd) { - for (pfn =3D *pfn_base ; pinned ; pfn++, = pinned--) - put_pfn(pfn, dma->prot); - } + for (pfn =3D *pfn_base ; pinned ; pfn++, pinned--) + put_pfn(pfn, dma->prot); return ret; } --- 2.13.6 diff --git a/drivers/vfio/vfio_iommu_type1.c = b/drivers/vfio/vfio_iommu_type1.c index 45657e2b1ff7..0658f35318b8 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -397,7 +397,6 @@ static long vfio_pin_pages_remote(struct vfio_dma = *dma, unsigned long vaddr, { unsigned long pfn =3D 0; long ret, pinned =3D 0, lock_acct =3D 0; - bool rsvd; dma_addr_t iova =3D vaddr - dma->vaddr + dma->iova;