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>,
<hch@infradead.org>
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>, <terrence.xu@intel.com>
Subject: [PATCH v3 04/10] vfio/ccw: Only pass in contiguous pages
Date: Fri, 8 Jul 2022 15:44:22 -0700 [thread overview]
Message-ID: <20220708224427.1245-5-nicolinc@nvidia.com> (raw)
In-Reply-To: <20220708224427.1245-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.
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Eric Farman <farman@linux.ibm.com>
Tested-by: Eric Farman <farman@linux.ibm.com>
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
drivers/s390/cio/vfio_ccw_cp.c | 70 +++++++++++++++++++++++++++-------
1 file changed, 56 insertions(+), 14 deletions(-)
diff --git a/drivers/s390/cio/vfio_ccw_cp.c b/drivers/s390/cio/vfio_ccw_cp.c
index 0c2be9421ab7..3b94863ad24e 100644
--- a/drivers/s390/cio/vfio_ccw_cp.c
+++ b/drivers/s390/cio/vfio_ccw_cp.c
@@ -90,6 +90,38 @@ 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 +133,44 @@ 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-07-08 22:44 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-08 22:44 [PATCH v3 00/10] Update vfio_pin/unpin_pages API Nicolin Chen
2022-07-08 22:44 ` [PATCH v3 01/10] vfio: Make vfio_unpin_pages() return void Nicolin Chen
2022-07-12 14:21 ` Anthony Krowiak
2022-07-12 18:18 ` Nicolin Chen
2022-07-08 22:44 ` [PATCH v3 02/10] drm/i915/gvt: Replace roundup with DIV_ROUND_UP Nicolin Chen
2022-07-08 22:44 ` [PATCH v3 03/10] vfio/ap: Pass in physical address of ind to ap_aqic() Nicolin Chen
2022-07-08 22:44 ` Nicolin Chen [this message]
2022-07-08 22:44 ` [PATCH v3 05/10] vfio: Pass in starting IOVA to vfio_pin/unpin_pages API Nicolin Chen
2022-07-12 14:25 ` Anthony Krowiak
2022-07-08 22:44 ` [PATCH v3 06/10] vfio/ap: Change saved_pfn to saved_iova Nicolin Chen
2022-07-12 14:28 ` Anthony Krowiak
2022-07-08 22:44 ` [PATCH v3 07/10] vfio/ccw: Change pa_pfn list to pa_iova list Nicolin Chen
2022-07-08 22:44 ` [PATCH v3 08/10] vfio: Rename user_iova of vfio_dma_rw() Nicolin Chen
2022-07-08 22:44 ` [PATCH v3 09/10] vfio/ccw: Add kmap_local_page() for memcpy Nicolin Chen
2022-07-22 22:11 ` [PATCH v3 00/10] Update vfio_pin/unpin_pages API Alex Williamson
2022-07-22 23:12 ` Nicolin Chen
2022-07-23 0:18 ` Alex Williamson
2022-07-23 0:38 ` Nicolin Chen
2022-07-23 1:09 ` Alex Williamson
2022-07-23 2:10 ` 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=20220708224427.1245-5-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=hch@infradead.org \
--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=terrence.xu@intel.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: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox