Skip to content

Commit 34a255e

Browse files
nicolincawilliam
authored andcommitted
vfio: Replace phys_pfn with pages for vfio_pin_pages()
Most of the callers of vfio_pin_pages() want "struct page *" and the low-level mm code to pin pages returns a list of "struct page *" too. So there's no gain in converting "struct page *" to PFN in between. Replace the output parameter "phys_pfn" list with a "pages" list, to simplify callers. This also allows us to replace the vfio_iommu_type1 implementation with a more efficient one. And drop the pfn_valid check in the gvt code, as there is no need to do such a check at a page-backed struct page pointer. For now, also update vfio_iommu_type1 to fit this new parameter too. Reviewed-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Kirti Wankhede <kwankhede@nvidia.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Acked-by: Eric Farman <farman@linux.ibm.com> Tested-by: Terrence Xu <terrence.xu@intel.com> Tested-by: Eric Farman <farman@linux.ibm.com> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> Link: https://lore.kernel.org/r/20220723020256.30081-11-nicolinc@nvidia.com Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
1 parent c2863fe commit 34a255e

File tree

8 files changed

+36
-41
lines changed

8 files changed

+36
-41
lines changed

Documentation/driver-api/vfio-mediated-device.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ The following APIs are provided for translating user pfn to host pfn in a VFIO
263263
driver::
264264

265265
int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
266-
int npage, int prot, unsigned long *phys_pfn);
266+
int npage, int prot, struct page **pages);
267267

268268
void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova,
269269
int npage);

drivers/gpu/drm/i915/gvt/kvmgt.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ static int gvt_pin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
240240
unsigned long size, struct page **page)
241241
{
242242
int total_pages = DIV_ROUND_UP(size, PAGE_SIZE);
243-
unsigned long base_pfn = 0;
243+
struct page *base_page = NULL;
244244
int npage;
245245
int ret;
246246

@@ -250,34 +250,27 @@ static int gvt_pin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
250250
*/
251251
for (npage = 0; npage < total_pages; npage++) {
252252
dma_addr_t cur_iova = (gfn + npage) << PAGE_SHIFT;
253-
unsigned long pfn;
253+
struct page *cur_page;
254254

255255
ret = vfio_pin_pages(&vgpu->vfio_device, cur_iova, 1,
256-
IOMMU_READ | IOMMU_WRITE, &pfn);
256+
IOMMU_READ | IOMMU_WRITE, &cur_page);
257257
if (ret != 1) {
258258
gvt_vgpu_err("vfio_pin_pages failed for iova %pad, ret %d\n",
259259
&cur_iova, ret);
260260
goto err;
261261
}
262262

263-
if (!pfn_valid(pfn)) {
264-
gvt_vgpu_err("pfn 0x%lx is not mem backed\n", pfn);
265-
npage++;
266-
ret = -EFAULT;
267-
goto err;
268-
}
269-
270263
if (npage == 0)
271-
base_pfn = pfn;
272-
else if (base_pfn + npage != pfn) {
264+
base_page = cur_page;
265+
else if (base_page + npage != cur_page) {
273266
gvt_vgpu_err("The pages are not continuous\n");
274267
ret = -EINVAL;
275268
npage++;
276269
goto err;
277270
}
278271
}
279272

280-
*page = pfn_to_page(base_pfn);
273+
*page = base_page;
281274
return 0;
282275
err:
283276
gvt_unpin_guest_page(vgpu, gfn, npage * PAGE_SIZE);

drivers/s390/cio/vfio_ccw_cp.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
struct page_array {
2323
/* Array that stores pages need to pin. */
2424
dma_addr_t *pa_iova;
25-
/* Array that receives PFNs of the pages pinned. */
26-
unsigned long *pa_pfn;
25+
/* Array that receives the pinned pages. */
26+
struct page **pa_page;
2727
/* Number of pages pinned from @pa_iova. */
2828
int pa_nr;
2929
};
@@ -68,19 +68,19 @@ static int page_array_alloc(struct page_array *pa, u64 iova, unsigned int len)
6868
return -EINVAL;
6969

7070
pa->pa_iova = kcalloc(pa->pa_nr,
71-
sizeof(*pa->pa_iova) + sizeof(*pa->pa_pfn),
71+
sizeof(*pa->pa_iova) + sizeof(*pa->pa_page),
7272
GFP_KERNEL);
7373
if (unlikely(!pa->pa_iova)) {
7474
pa->pa_nr = 0;
7575
return -ENOMEM;
7676
}
77-
pa->pa_pfn = (unsigned long *)&pa->pa_iova[pa->pa_nr];
77+
pa->pa_page = (struct page **)&pa->pa_iova[pa->pa_nr];
7878

7979
pa->pa_iova[0] = iova;
80-
pa->pa_pfn[0] = -1ULL;
80+
pa->pa_page[0] = NULL;
8181
for (i = 1; i < pa->pa_nr; i++) {
8282
pa->pa_iova[i] = pa->pa_iova[i - 1] + PAGE_SIZE;
83-
pa->pa_pfn[i] = -1ULL;
83+
pa->pa_page[i] = NULL;
8484
}
8585

8686
return 0;
@@ -144,7 +144,7 @@ static int page_array_pin(struct page_array *pa, struct vfio_device *vdev)
144144

145145
ret = vfio_pin_pages(vdev, *first, npage,
146146
IOMMU_READ | IOMMU_WRITE,
147-
&pa->pa_pfn[pinned]);
147+
&pa->pa_page[pinned]);
148148
if (ret < 0) {
149149
goto err_out;
150150
} else if (ret > 0 && ret != npage) {
@@ -195,7 +195,7 @@ static inline void page_array_idal_create_words(struct page_array *pa,
195195
*/
196196

197197
for (i = 0; i < pa->pa_nr; i++)
198-
idaws[i] = pa->pa_pfn[i] << PAGE_SHIFT;
198+
idaws[i] = page_to_phys(pa->pa_page[i]);
199199

200200
/* Adjust the first IDAW, since it may not start on a page boundary */
201201
idaws[0] += pa->pa_iova[0] & (PAGE_SIZE - 1);
@@ -246,8 +246,7 @@ static long copy_from_iova(struct vfio_device *vdev, void *to, u64 iova,
246246

247247
l = n;
248248
for (i = 0; i < pa.pa_nr; i++) {
249-
struct page *page = pfn_to_page(pa.pa_pfn[i]);
250-
void *from = kmap_local_page(page);
249+
void *from = kmap_local_page(pa.pa_page[i]);
251250

252251
m = PAGE_SIZE;
253252
if (i == 0) {

drivers/s390/crypto/vfio_ap_ops.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
234234
struct ap_qirq_ctrl aqic_gisa = {};
235235
struct ap_queue_status status = {};
236236
struct kvm_s390_gisa *gisa;
237+
struct page *h_page;
237238
int nisc;
238239
struct kvm *kvm;
239-
unsigned long h_pfn;
240240
phys_addr_t h_nib;
241241
dma_addr_t nib;
242242
int ret;
@@ -251,7 +251,7 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
251251
}
252252

253253
ret = vfio_pin_pages(&q->matrix_mdev->vdev, nib, 1,
254-
IOMMU_READ | IOMMU_WRITE, &h_pfn);
254+
IOMMU_READ | IOMMU_WRITE, &h_page);
255255
switch (ret) {
256256
case 1:
257257
break;
@@ -267,7 +267,7 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
267267
kvm = q->matrix_mdev->kvm;
268268
gisa = kvm->arch.gisa_int.origin;
269269

270-
h_nib = (h_pfn << PAGE_SHIFT) | (nib & ~PAGE_MASK);
270+
h_nib = page_to_phys(h_page) | (nib & ~PAGE_MASK);
271271
aqic_gisa.gisc = isc;
272272

273273
nisc = kvm_s390_gisc_register(kvm, isc);

drivers/vfio/vfio.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,18 +1941,18 @@ EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare);
19411941
* @npage [in] : count of pages to be pinned. This count should not
19421942
* be greater than VFIO_PIN_PAGES_MAX_ENTRIES.
19431943
* @prot [in] : protection flags
1944-
* @phys_pfn[out]: array of host PFNs
1944+
* @pages[out] : array of host pages
19451945
* Return error or number of pages pinned.
19461946
*/
19471947
int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
1948-
int npage, int prot, unsigned long *phys_pfn)
1948+
int npage, int prot, struct page **pages)
19491949
{
19501950
struct vfio_container *container;
19511951
struct vfio_group *group = device->group;
19521952
struct vfio_iommu_driver *driver;
19531953
int ret;
19541954

1955-
if (!phys_pfn || !npage || !vfio_assert_device_open(device))
1955+
if (!pages || !npage || !vfio_assert_device_open(device))
19561956
return -EINVAL;
19571957

19581958
if (npage > VFIO_PIN_PAGES_MAX_ENTRIES)
@@ -1967,7 +1967,7 @@ int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
19671967
if (likely(driver && driver->ops->pin_pages))
19681968
ret = driver->ops->pin_pages(container->iommu_data,
19691969
group->iommu_group, iova,
1970-
npage, prot, phys_pfn);
1970+
npage, prot, pages);
19711971
else
19721972
ret = -ENOTTY;
19731973

drivers/vfio/vfio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct vfio_iommu_driver_ops {
5252
struct iommu_group *group,
5353
dma_addr_t user_iova,
5454
int npage, int prot,
55-
unsigned long *phys_pfn);
55+
struct page **pages);
5656
void (*unpin_pages)(void *iommu_data,
5757
dma_addr_t user_iova, int npage);
5858
void (*register_device)(void *iommu_data,

drivers/vfio/vfio_iommu_type1.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data,
831831
struct iommu_group *iommu_group,
832832
dma_addr_t user_iova,
833833
int npage, int prot,
834-
unsigned long *phys_pfn)
834+
struct page **pages)
835835
{
836836
struct vfio_iommu *iommu = iommu_data;
837837
struct vfio_iommu_group *group;
@@ -841,7 +841,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data,
841841
bool do_accounting;
842842
dma_addr_t iova;
843843

844-
if (!iommu || !phys_pfn)
844+
if (!iommu || !pages)
845845
return -EINVAL;
846846

847847
/* Supported for v2 version only */
@@ -880,6 +880,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data,
880880
do_accounting = list_empty(&iommu->domain_list);
881881

882882
for (i = 0; i < npage; i++) {
883+
unsigned long phys_pfn;
883884
struct vfio_pfn *vpfn;
884885

885886
iova = user_iova + PAGE_SIZE * i;
@@ -896,23 +897,25 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data,
896897

897898
vpfn = vfio_iova_get_vfio_pfn(dma, iova);
898899
if (vpfn) {
899-
phys_pfn[i] = vpfn->pfn;
900+
pages[i] = pfn_to_page(vpfn->pfn);
900901
continue;
901902
}
902903

903904
remote_vaddr = dma->vaddr + (iova - dma->iova);
904-
ret = vfio_pin_page_external(dma, remote_vaddr, &phys_pfn[i],
905+
ret = vfio_pin_page_external(dma, remote_vaddr, &phys_pfn,
905906
do_accounting);
906907
if (ret)
907908
goto pin_unwind;
908909

909-
ret = vfio_add_to_pfn_list(dma, iova, phys_pfn[i]);
910+
ret = vfio_add_to_pfn_list(dma, iova, phys_pfn);
910911
if (ret) {
911-
if (put_pfn(phys_pfn[i], dma->prot) && do_accounting)
912+
if (put_pfn(phys_pfn, dma->prot) && do_accounting)
912913
vfio_lock_acct(dma, -1, true);
913914
goto pin_unwind;
914915
}
915916

917+
pages[i] = pfn_to_page(phys_pfn);
918+
916919
if (iommu->dirty_page_tracking) {
917920
unsigned long pgshift = __ffs(iommu->pgsize_bitmap);
918921

@@ -935,14 +938,14 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data,
935938
goto pin_done;
936939

937940
pin_unwind:
938-
phys_pfn[i] = 0;
941+
pages[i] = NULL;
939942
for (j = 0; j < i; j++) {
940943
dma_addr_t iova;
941944

942945
iova = user_iova + PAGE_SIZE * j;
943946
dma = vfio_find_dma(iommu, iova, PAGE_SIZE);
944947
vfio_unpin_page_external(dma, iova, do_accounting);
945-
phys_pfn[j] = 0;
948+
pages[j] = NULL;
946949
}
947950
pin_done:
948951
mutex_unlock(&iommu->lock);

include/linux/vfio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
162162
#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
163163

164164
int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
165-
int npage, int prot, unsigned long *phys_pfn);
165+
int npage, int prot, struct page **pages);
166166
void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage);
167167
int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova,
168168
void *data, size_t len, bool write);

0 commit comments

Comments
 (0)