Skip to content

Commit 22af48c

Browse files
YongjiXiemstsirkin
authored andcommitted
vdpa: factor out vhost_vdpa_pa_map() and vhost_vdpa_pa_unmap()
The upcoming patch is going to support VA mapping/unmapping. So let's factor out the logic of PA mapping/unmapping firstly to make the code more readable. Suggested-by: Jason Wang <[email protected]> Signed-off-by: Xie Yongji <[email protected]> Acked-by: Jason Wang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent c10fb94 commit 22af48c

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

drivers/vhost/vdpa.c

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,
504504
return r;
505505
}
506506

507-
static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v, u64 start, u64 last)
507+
static void vhost_vdpa_pa_unmap(struct vhost_vdpa *v, u64 start, u64 last)
508508
{
509509
struct vhost_dev *dev = &v->vdev;
510510
struct vhost_iotlb *iotlb = dev->iotlb;
@@ -526,6 +526,11 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v, u64 start, u64 last)
526526
}
527527
}
528528

529+
static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v, u64 start, u64 last)
530+
{
531+
return vhost_vdpa_pa_unmap(v, start, last);
532+
}
533+
529534
static void vhost_vdpa_iotlb_free(struct vhost_vdpa *v)
530535
{
531536
struct vhost_dev *dev = &v->vdev;
@@ -606,38 +611,28 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v, u64 iova, u64 size)
606611
}
607612
}
608613

609-
static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
610-
struct vhost_iotlb_msg *msg)
614+
static int vhost_vdpa_pa_map(struct vhost_vdpa *v,
615+
u64 iova, u64 size, u64 uaddr, u32 perm)
611616
{
612617
struct vhost_dev *dev = &v->vdev;
613-
struct vhost_iotlb *iotlb = dev->iotlb;
614618
struct page **page_list;
615619
unsigned long list_size = PAGE_SIZE / sizeof(struct page *);
616620
unsigned int gup_flags = FOLL_LONGTERM;
617621
unsigned long npages, cur_base, map_pfn, last_pfn = 0;
618622
unsigned long lock_limit, sz2pin, nchunks, i;
619-
u64 iova = msg->iova;
623+
u64 start = iova;
620624
long pinned;
621625
int ret = 0;
622626

623-
if (msg->iova < v->range.first || !msg->size ||
624-
msg->iova > U64_MAX - msg->size + 1 ||
625-
msg->iova + msg->size - 1 > v->range.last)
626-
return -EINVAL;
627-
628-
if (vhost_iotlb_itree_first(iotlb, msg->iova,
629-
msg->iova + msg->size - 1))
630-
return -EEXIST;
631-
632627
/* Limit the use of memory for bookkeeping */
633628
page_list = (struct page **) __get_free_page(GFP_KERNEL);
634629
if (!page_list)
635630
return -ENOMEM;
636631

637-
if (msg->perm & VHOST_ACCESS_WO)
632+
if (perm & VHOST_ACCESS_WO)
638633
gup_flags |= FOLL_WRITE;
639634

640-
npages = PFN_UP(msg->size + (iova & ~PAGE_MASK));
635+
npages = PFN_UP(size + (iova & ~PAGE_MASK));
641636
if (!npages) {
642637
ret = -EINVAL;
643638
goto free;
@@ -651,7 +646,7 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
651646
goto unlock;
652647
}
653648

654-
cur_base = msg->uaddr & PAGE_MASK;
649+
cur_base = uaddr & PAGE_MASK;
655650
iova &= PAGE_MASK;
656651
nchunks = 0;
657652

@@ -682,7 +677,7 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
682677
csize = PFN_PHYS(last_pfn - map_pfn + 1);
683678
ret = vhost_vdpa_map(v, iova, csize,
684679
PFN_PHYS(map_pfn),
685-
msg->perm);
680+
perm);
686681
if (ret) {
687682
/*
688683
* Unpin the pages that are left unmapped
@@ -711,7 +706,7 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
711706

712707
/* Pin the rest chunk */
713708
ret = vhost_vdpa_map(v, iova, PFN_PHYS(last_pfn - map_pfn + 1),
714-
PFN_PHYS(map_pfn), msg->perm);
709+
PFN_PHYS(map_pfn), perm);
715710
out:
716711
if (ret) {
717712
if (nchunks) {
@@ -730,13 +725,33 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
730725
for (pfn = map_pfn; pfn <= last_pfn; pfn++)
731726
unpin_user_page(pfn_to_page(pfn));
732727
}
733-
vhost_vdpa_unmap(v, msg->iova, msg->size);
728+
vhost_vdpa_unmap(v, start, size);
734729
}
735730
unlock:
736731
mmap_read_unlock(dev->mm);
737732
free:
738733
free_page((unsigned long)page_list);
739734
return ret;
735+
736+
}
737+
738+
static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
739+
struct vhost_iotlb_msg *msg)
740+
{
741+
struct vhost_dev *dev = &v->vdev;
742+
struct vhost_iotlb *iotlb = dev->iotlb;
743+
744+
if (msg->iova < v->range.first || !msg->size ||
745+
msg->iova > U64_MAX - msg->size + 1 ||
746+
msg->iova + msg->size - 1 > v->range.last)
747+
return -EINVAL;
748+
749+
if (vhost_iotlb_itree_first(iotlb, msg->iova,
750+
msg->iova + msg->size - 1))
751+
return -EEXIST;
752+
753+
return vhost_vdpa_pa_map(v, msg->iova, msg->size, msg->uaddr,
754+
msg->perm);
740755
}
741756

742757
static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,

0 commit comments

Comments
 (0)