From: Nicolin Chen <nicolinc@nvidia.com> To: <kwankhede@nvidia.com>, <corbet@lwn.net>, <hca@linux.ibm.com>, <gor@linux.ibm.com>, <agordeev@linux.ibm.com>, <borntraeger@linux.ibm.com>, <svens@linux.ibm.com>, <zhenyuw@linux.intel.com>, <zhi.a.wang@intel.com>, <jani.nikula@linux.intel.com>, <joonas.lahtinen@linux.intel.com>, <rodrigo.vivi@intel.com>, <tvrtko.ursulin@linux.intel.com>, <airlied@linux.ie>, <daniel@ffwll.ch>, <farman@linux.ibm.com>, <mjrosato@linux.ibm.com>, <pasic@linux.ibm.com>, <vneethv@linux.ibm.com>, <oberpar@linux.ibm.com>, <freude@linux.ibm.com>, <akrowiak@linux.ibm.com>, <jjherne@linux.ibm.com>, <alex.williamson@redhat.com>, <cohuck@redhat.com>, <jgg@nvidia.com>, <kevin.tian@intel.com> Cc: <jchrist@linux.ibm.com>, <kvm@vger.kernel.org>, <linux-doc@vger.kernel.org>, <linux-kernel@vger.kernel.org>, <linux-s390@vger.kernel.org>, <intel-gvt-dev@lists.freedesktop.org>, <intel-gfx@lists.freedesktop.org>, <dri-devel@lists.freedesktop.org> Subject: [RFT][PATCH v1 2/6] vfio/ccw: Only pass in contiguous pages Date: Thu, 16 Jun 2022 16:52:08 -0700 [thread overview] Message-ID: <20220616235212.15185-3-nicolinc@nvidia.com> (raw) In-Reply-To: <20220616235212.15185-1-nicolinc@nvidia.com> This driver is the only caller of vfio_pin/unpin_pages that might pass in a non-contiguous PFN list, but in many cases it has a contiguous PFN list to process. So letting VFIO API handle a non-contiguous PFN list is actually counterproductive. Add a pair of simple loops to pass in contiguous PFNs only, to have an efficient implementation in VFIO. Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> --- drivers/s390/cio/vfio_ccw_cp.c | 68 +++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/drivers/s390/cio/vfio_ccw_cp.c b/drivers/s390/cio/vfio_ccw_cp.c index 0c2be9421ab7..4659c2e35877 100644 --- a/drivers/s390/cio/vfio_ccw_cp.c +++ b/drivers/s390/cio/vfio_ccw_cp.c @@ -90,6 +90,37 @@ static int pfn_array_alloc(struct pfn_array *pa, u64 iova, unsigned int len) return 0; } +/* + * pfn_array_unpin() - Unpin user pages in memory + * @pa: pfn_array on which to perform the operation + * @vdev: the vfio device to perform the operation + * @pa_nr: number of user pages to unpin + * + * Only unpin if any pages were pinned to begin with, i.e. pa_nr > 0, + * otherwise only clear pa->pa_nr + */ +static void pfn_array_unpin(struct pfn_array *pa, + struct vfio_device *vdev, int pa_nr) +{ + int unpinned = 0, npage = 1; + + while (unpinned < pa_nr) { + unsigned long *first = &pa->pa_iova_pfn[unpinned]; + unsigned long *last = &first[npage]; + + if (unpinned + npage < pa_nr && *first + npage == *last) { + npage++; + continue; + } + + vfio_unpin_pages(vdev, first, npage); + unpinned += npage; + npage = 1; + } + + pa->pa_nr = 0; +} + /* * pfn_array_pin() - Pin user pages in memory * @pa: pfn_array on which to perform the operation @@ -101,34 +132,43 @@ static int pfn_array_alloc(struct pfn_array *pa, u64 iova, unsigned int len) */ static int pfn_array_pin(struct pfn_array *pa, struct vfio_device *vdev) { + int pinned = 0, npage = 1; int ret = 0; - ret = vfio_pin_pages(vdev, pa->pa_iova_pfn, pa->pa_nr, - IOMMU_READ | IOMMU_WRITE, pa->pa_pfn); + while (pinned < pa->pa_nr) { + unsigned long *first = &pa->pa_iova_pfn[pinned]; + unsigned long *last = &first[npage]; - if (ret < 0) { - goto err_out; - } else if (ret > 0 && ret != pa->pa_nr) { - vfio_unpin_pages(vdev, pa->pa_iova_pfn, ret); - ret = -EINVAL; - goto err_out; + if (pinned + npage < pa->pa_nr && *first + npage == *last) { + npage++; + continue; + } + + ret = vfio_pin_pages(vdev, first, npage, + IOMMU_READ | IOMMU_WRITE, + &pa->pa_pfn[pinned]); + if (ret < 0) { + goto err_out; + } else if (ret > 0 && ret != npage) { + pinned += ret; + ret = -EINVAL; + goto err_out; + } + pinned += npage; + npage = 1; } return ret; err_out: - pa->pa_nr = 0; - + pfn_array_unpin(pa, vdev, pinned); return ret; } /* Unpin the pages before releasing the memory. */ static void pfn_array_unpin_free(struct pfn_array *pa, struct vfio_device *vdev) { - /* Only unpin if any pages were pinned to begin with */ - if (pa->pa_nr) - vfio_unpin_pages(vdev, pa->pa_iova_pfn, pa->pa_nr); - pa->pa_nr = 0; + pfn_array_unpin(pa, vdev, pa->pa_nr); kfree(pa->pa_iova_pfn); } -- 2.17.1
WARNING: multiple messages have this Message-ID (diff)
From: Nicolin Chen <nicolinc@nvidia.com> To: <kwankhede@nvidia.com>, <corbet@lwn.net>, <hca@linux.ibm.com>, <gor@linux.ibm.com>, <agordeev@linux.ibm.com>, <borntraeger@linux.ibm.com>, <svens@linux.ibm.com>, <zhenyuw@linux.intel.com>, <zhi.a.wang@intel.com>, <jani.nikula@linux.intel.com>, <joonas.lahtinen@linux.intel.com>, <rodrigo.vivi@intel.com>, <tvrtko.ursulin@linux.intel.com>, <airlied@linux.ie>, <daniel@ffwll.ch>, <farman@linux.ibm.com>, <mjrosato@linux.ibm.com>, <pasic@linux.ibm.com>, <vneethv@linux.ibm.com>, <oberpar@linux.ibm.com>, <freude@linux.ibm.com>, <akrowiak@linux.ibm.com>, <jjherne@linux.ibm.com>, <alex.williamson@redhat.com>, <cohuck@redhat.com>, <jgg@nvidia.com>, <kevin.tian@intel.com> Cc: linux-s390@vger.kernel.org, kvm@vger.kernel.org, linux-doc@vger.kernel.org, intel-gfx@lists.freedesktop.org, jchrist@linux.ibm.com, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, intel-gvt-dev@lists.freedesktop.org Subject: [RFT][PATCH v1 2/6] vfio/ccw: Only pass in contiguous pages Date: Thu, 16 Jun 2022 16:52:08 -0700 [thread overview] Message-ID: <20220616235212.15185-3-nicolinc@nvidia.com> (raw) In-Reply-To: <20220616235212.15185-1-nicolinc@nvidia.com> This driver is the only caller of vfio_pin/unpin_pages that might pass in a non-contiguous PFN list, but in many cases it has a contiguous PFN list to process. So letting VFIO API handle a non-contiguous PFN list is actually counterproductive. Add a pair of simple loops to pass in contiguous PFNs only, to have an efficient implementation in VFIO. Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> --- drivers/s390/cio/vfio_ccw_cp.c | 68 +++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/drivers/s390/cio/vfio_ccw_cp.c b/drivers/s390/cio/vfio_ccw_cp.c index 0c2be9421ab7..4659c2e35877 100644 --- a/drivers/s390/cio/vfio_ccw_cp.c +++ b/drivers/s390/cio/vfio_ccw_cp.c @@ -90,6 +90,37 @@ static int pfn_array_alloc(struct pfn_array *pa, u64 iova, unsigned int len) return 0; } +/* + * pfn_array_unpin() - Unpin user pages in memory + * @pa: pfn_array on which to perform the operation + * @vdev: the vfio device to perform the operation + * @pa_nr: number of user pages to unpin + * + * Only unpin if any pages were pinned to begin with, i.e. pa_nr > 0, + * otherwise only clear pa->pa_nr + */ +static void pfn_array_unpin(struct pfn_array *pa, + struct vfio_device *vdev, int pa_nr) +{ + int unpinned = 0, npage = 1; + + while (unpinned < pa_nr) { + unsigned long *first = &pa->pa_iova_pfn[unpinned]; + unsigned long *last = &first[npage]; + + if (unpinned + npage < pa_nr && *first + npage == *last) { + npage++; + continue; + } + + vfio_unpin_pages(vdev, first, npage); + unpinned += npage; + npage = 1; + } + + pa->pa_nr = 0; +} + /* * pfn_array_pin() - Pin user pages in memory * @pa: pfn_array on which to perform the operation @@ -101,34 +132,43 @@ static int pfn_array_alloc(struct pfn_array *pa, u64 iova, unsigned int len) */ static int pfn_array_pin(struct pfn_array *pa, struct vfio_device *vdev) { + int pinned = 0, npage = 1; int ret = 0; - ret = vfio_pin_pages(vdev, pa->pa_iova_pfn, pa->pa_nr, - IOMMU_READ | IOMMU_WRITE, pa->pa_pfn); + while (pinned < pa->pa_nr) { + unsigned long *first = &pa->pa_iova_pfn[pinned]; + unsigned long *last = &first[npage]; - if (ret < 0) { - goto err_out; - } else if (ret > 0 && ret != pa->pa_nr) { - vfio_unpin_pages(vdev, pa->pa_iova_pfn, ret); - ret = -EINVAL; - goto err_out; + if (pinned + npage < pa->pa_nr && *first + npage == *last) { + npage++; + continue; + } + + ret = vfio_pin_pages(vdev, first, npage, + IOMMU_READ | IOMMU_WRITE, + &pa->pa_pfn[pinned]); + if (ret < 0) { + goto err_out; + } else if (ret > 0 && ret != npage) { + pinned += ret; + ret = -EINVAL; + goto err_out; + } + pinned += npage; + npage = 1; } return ret; err_out: - pa->pa_nr = 0; - + pfn_array_unpin(pa, vdev, pinned); return ret; } /* Unpin the pages before releasing the memory. */ static void pfn_array_unpin_free(struct pfn_array *pa, struct vfio_device *vdev) { - /* Only unpin if any pages were pinned to begin with */ - if (pa->pa_nr) - vfio_unpin_pages(vdev, pa->pa_iova_pfn, pa->pa_nr); - pa->pa_nr = 0; + pfn_array_unpin(pa, vdev, pa->pa_nr); kfree(pa->pa_iova_pfn); } -- 2.17.1
next prev parent reply other threads:[~2022-06-16 23:53 UTC|newest] Thread overview: 75+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-06-16 23:52 [RFT][PATCH v1 0/6] Update vfio_pin/unpin_pages API Nicolin Chen 2022-06-16 23:52 ` Nicolin Chen 2022-06-16 23:52 ` [RFT][PATCH v1 1/6] vfio/ap: Pass in physical address of ind to ap_aqic() Nicolin Chen 2022-06-16 23:52 ` Nicolin Chen 2022-06-20 10:00 ` Harald Freudenberger 2022-06-20 10:00 ` Harald Freudenberger 2022-06-21 21:01 ` Nicolin Chen 2022-06-21 21:01 ` [Intel-gfx] " Nicolin Chen 2022-06-21 21:01 ` Nicolin Chen 2022-06-16 23:52 ` Nicolin Chen [this message] 2022-06-16 23:52 ` [RFT][PATCH v1 2/6] vfio/ccw: Only pass in contiguous pages Nicolin Chen 2022-06-16 23:52 ` [RFT][PATCH v1 3/6] vfio: Pass in starting IOVA to vfio_pin/unpin_pages API Nicolin Chen 2022-06-16 23:52 ` Nicolin Chen 2022-06-17 8:42 ` Christoph Hellwig 2022-06-17 8:42 ` [Intel-gfx] " Christoph Hellwig 2022-06-17 21:57 ` Nicolin Chen 2022-06-17 21:57 ` Nicolin Chen 2022-06-22 1:18 ` Nicolin Chen 2022-06-22 1:18 ` [Intel-gfx] " Nicolin Chen 2022-06-22 1:18 ` Nicolin Chen 2022-06-16 23:52 ` [RFT][PATCH v1 4/6] vfio: Rename user_iova of vfio_dma_rw() Nicolin Chen 2022-06-16 23:52 ` Nicolin Chen 2022-06-16 23:52 ` [RFT][PATCH v1 5/6] vfio/ccw: Add kmap_local_page() for memcpy Nicolin Chen 2022-06-16 23:52 ` Nicolin Chen 2022-06-17 8:44 ` Christoph Hellwig 2022-06-17 8:44 ` [Intel-gfx] " Christoph Hellwig 2022-06-17 21:58 ` Nicolin Chen 2022-06-17 21:58 ` Nicolin Chen 2022-06-20 2:57 ` Jason Gunthorpe 2022-06-20 2:57 ` [Intel-gfx] " Jason Gunthorpe 2022-06-20 2:57 ` Jason Gunthorpe 2022-06-20 6:32 ` Christoph Hellwig 2022-06-20 6:32 ` [Intel-gfx] " Christoph Hellwig 2022-06-20 15:39 ` Jason Gunthorpe 2022-06-20 15:39 ` [Intel-gfx] " Jason Gunthorpe 2022-06-20 15:39 ` Jason Gunthorpe 2022-06-21 21:21 ` Nicolin Chen 2022-06-21 21:21 ` [Intel-gfx] " Nicolin Chen 2022-06-21 21:21 ` Nicolin Chen 2022-06-24 13:56 ` Jason Gunthorpe 2022-06-24 13:56 ` [Intel-gfx] " Jason Gunthorpe 2022-06-24 13:56 ` Jason Gunthorpe 2022-06-24 19:22 ` Nicolin Chen 2022-06-24 19:22 ` Nicolin Chen 2022-06-24 19:30 ` Jason Gunthorpe 2022-06-24 19:30 ` [Intel-gfx] " Jason Gunthorpe 2022-06-24 19:30 ` Jason Gunthorpe 2022-06-24 20:12 ` Nicolin Chen 2022-06-24 20:12 ` Nicolin Chen 2022-06-24 22:42 ` Jason Gunthorpe 2022-06-24 22:42 ` [Intel-gfx] " Jason Gunthorpe 2022-06-24 22:42 ` Jason Gunthorpe 2022-06-16 23:52 ` [RFT][PATCH v1 6/6] vfio: Replace phys_pfn with phys_page for vfio_pin_pages() Nicolin Chen 2022-06-16 23:52 ` Nicolin Chen 2022-06-17 8:54 ` Christoph Hellwig 2022-06-17 8:54 ` [Intel-gfx] " Christoph Hellwig 2022-06-17 22:06 ` Nicolin Chen 2022-06-17 22:06 ` Nicolin Chen 2022-06-19 6:18 ` Christoph Hellwig 2022-06-19 6:18 ` [Intel-gfx] " Christoph Hellwig 2022-06-19 6:41 ` Nicolin Chen 2022-06-19 6:41 ` Nicolin Chen 2022-06-20 3:00 ` Jason Gunthorpe 2022-06-20 3:00 ` [Intel-gfx] " Jason Gunthorpe 2022-06-20 3:00 ` Jason Gunthorpe 2022-06-20 5:51 ` Christoph Hellwig 2022-06-20 5:51 ` [Intel-gfx] " Christoph Hellwig 2022-06-20 6:37 ` Christoph Hellwig 2022-06-20 6:37 ` [Intel-gfx] " Christoph Hellwig 2022-06-20 15:36 ` Jason Gunthorpe 2022-06-20 15:36 ` [Intel-gfx] " Jason Gunthorpe 2022-06-20 15:36 ` Jason Gunthorpe 2022-06-21 21:47 ` Nicolin Chen 2022-06-21 21:47 ` [Intel-gfx] " Nicolin Chen 2022-06-21 21:47 ` Nicolin Chen
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220616235212.15185-3-nicolinc@nvidia.com \ --to=nicolinc@nvidia.com \ --cc=agordeev@linux.ibm.com \ --cc=airlied@linux.ie \ --cc=akrowiak@linux.ibm.com \ --cc=alex.williamson@redhat.com \ --cc=borntraeger@linux.ibm.com \ --cc=cohuck@redhat.com \ --cc=corbet@lwn.net \ --cc=daniel@ffwll.ch \ --cc=dri-devel@lists.freedesktop.org \ --cc=farman@linux.ibm.com \ --cc=freude@linux.ibm.com \ --cc=gor@linux.ibm.com \ --cc=hca@linux.ibm.com \ --cc=intel-gfx@lists.freedesktop.org \ --cc=intel-gvt-dev@lists.freedesktop.org \ --cc=jani.nikula@linux.intel.com \ --cc=jchrist@linux.ibm.com \ --cc=jgg@nvidia.com \ --cc=jjherne@linux.ibm.com \ --cc=joonas.lahtinen@linux.intel.com \ --cc=kevin.tian@intel.com \ --cc=kvm@vger.kernel.org \ --cc=kwankhede@nvidia.com \ --cc=linux-doc@vger.kernel.org \ --cc=linux-kernel@vger.kernel.org \ --cc=linux-s390@vger.kernel.org \ --cc=mjrosato@linux.ibm.com \ --cc=oberpar@linux.ibm.com \ --cc=pasic@linux.ibm.com \ --cc=rodrigo.vivi@intel.com \ --cc=svens@linux.ibm.com \ --cc=tvrtko.ursulin@linux.intel.com \ --cc=vneethv@linux.ibm.com \ --cc=zhenyuw@linux.intel.com \ --cc=zhi.a.wang@intel.com \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.