Skip to content

Commit 7cb671e

Browse files
author
Alex Williamson
committed
vfio/type1: Reduce repetitive calls in vfio_pin_pages_remote()
vfio_pin_pages_remote() is typically called to iterate over a range of memory. Testing CAP_IPC_LOCK is relatively expensive, so it makes sense to push it up to the caller, which can then repeatedly call vfio_pin_pages_remote() using that value. This can show nearly a 20% improvement on the worst case path through VFIO_IOMMU_MAP_DMA with contiguous page mapping disabled. Testing RLIMIT_MEMLOCK is much more lightweight, but we bring it along on the same principle and it does seem to show a marginal improvement. Reviewed-by: Peter Xu <peterx@redhat.com> Reviewed-by: Kirti Wankhede <kwankhede@nvidia.com> Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
1 parent 80dbe1f commit 7cb671e

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

‎drivers/vfio/vfio_iommu_type1.c‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,10 @@ static int vaddr_get_pfn(struct mm_struct *mm, unsigned long vaddr,
380380
* first page and all consecutive pages with the same locking.
381381
*/
382382
static long vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr,
383-
long npage, unsigned long *pfn_base)
383+
long npage, unsigned long *pfn_base,
384+
bool lock_cap, unsigned long limit)
384385
{
385-
unsigned long pfn = 0, limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
386-
bool lock_cap = capable(CAP_IPC_LOCK);
386+
unsigned long pfn = 0;
387387
long ret, pinned = 0, lock_acct = 0;
388388
bool rsvd;
389389
dma_addr_t iova = vaddr - dma->vaddr + dma->iova;
@@ -924,13 +924,15 @@ static int vfio_pin_map_dma(struct vfio_iommu *iommu, struct vfio_dma *dma,
924924
unsigned long vaddr = dma->vaddr;
925925
size_t size = map_size;
926926
long npage;
927-
unsigned long pfn;
927+
unsigned long pfn, limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
928+
bool lock_cap = capable(CAP_IPC_LOCK);
928929
int ret = 0;
929930

930931
while (size) {
931932
/* Pin a contiguous chunk of memory */
932933
npage = vfio_pin_pages_remote(dma, vaddr + dma->size,
933-
size >> PAGE_SHIFT, &pfn);
934+
size >> PAGE_SHIFT, &pfn,
935+
lock_cap, limit);
934936
if (npage <= 0) {
935937
WARN_ON(!npage);
936938
ret = (int)npage;
@@ -1040,6 +1042,8 @@ static int vfio_iommu_replay(struct vfio_iommu *iommu,
10401042
{
10411043
struct vfio_domain *d;
10421044
struct rb_node *n;
1045+
unsigned long limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1046+
bool lock_cap = capable(CAP_IPC_LOCK);
10431047
int ret;
10441048

10451049
/* Arbitrarily pick the first domain in the list for lookups */
@@ -1086,7 +1090,8 @@ static int vfio_iommu_replay(struct vfio_iommu *iommu,
10861090

10871091
npage = vfio_pin_pages_remote(dma, vaddr,
10881092
n >> PAGE_SHIFT,
1089-
&pfn);
1093+
&pfn, lock_cap,
1094+
limit);
10901095
if (npage <= 0) {
10911096
WARN_ON(!npage);
10921097
ret = (int)npage;

0 commit comments

Comments
 (0)