Skip to content

Commit 80dbe1f

Browse files
author
Alex Williamson
committed
vfio/type1: Prune vfio_pin_page_external()
With vfio_lock_acct() testing the locked memory limit under mmap_sem, it's redundant to do it here for a single page. We can also reorder our tests such that we can avoid testing for reserved pages if we're not doing accounting and let vfio_lock_acct() test the process CAP_IPC_LOCK. Finally, this function oddly returns 1 on success. Update to return zero on success, -errno on error. Since the function only pins a single page, there's no need to return the number of pages pinned. N.B. vfio_pin_pages_remote() can pin a large contiguous range of pages before calling vfio_lock_acct(). If we were to similarly remove the extra test there, a user could temporarily pin far more pages than they're allowed. Suggested-by: Kirti Wankhede <kwankhede@nvidia.com> Suggested-by: Eric Auger <eric.auger@redhat.com> Reviewed-by: Kirti Wankhede <kwankhede@nvidia.com> Reviewed-by: Peter Xu <peterx@redhat.com> Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
1 parent 0cfef2b commit 80dbe1f

1 file changed

Lines changed: 8 additions & 27 deletions

File tree

‎drivers/vfio/vfio_iommu_type1.c‎

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -482,43 +482,26 @@ static long vfio_unpin_pages_remote(struct vfio_dma *dma, dma_addr_t iova,
482482
static int vfio_pin_page_external(struct vfio_dma *dma, unsigned long vaddr,
483483
unsigned long *pfn_base, bool do_accounting)
484484
{
485-
unsigned long limit;
486-
bool lock_cap = has_capability(dma->task, CAP_IPC_LOCK);
487485
struct mm_struct *mm;
488486
int ret;
489-
bool rsvd;
490487

491488
mm = get_task_mm(dma->task);
492489
if (!mm)
493490
return -ENODEV;
494491

495492
ret = vaddr_get_pfn(mm, vaddr, dma->prot, pfn_base);
496-
if (ret)
497-
goto pin_page_exit;
498-
499-
rsvd = is_invalid_reserved_pfn(*pfn_base);
500-
limit = task_rlimit(dma->task, RLIMIT_MEMLOCK) >> PAGE_SHIFT;
501-
502-
if (!rsvd && !lock_cap && mm->locked_vm + 1 > limit) {
503-
put_pfn(*pfn_base, dma->prot);
504-
pr_warn("%s: Task %s (%d) RLIMIT_MEMLOCK (%ld) exceeded\n",
505-
__func__, dma->task->comm, task_pid_nr(dma->task),
506-
limit << PAGE_SHIFT);
507-
ret = -ENOMEM;
508-
goto pin_page_exit;
509-
}
510-
511-
if (!rsvd && do_accounting) {
512-
ret = vfio_lock_acct(dma->task, 1, &lock_cap);
493+
if (!ret && do_accounting && !is_invalid_reserved_pfn(*pfn_base)) {
494+
ret = vfio_lock_acct(dma->task, 1, NULL);
513495
if (ret) {
514496
put_pfn(*pfn_base, dma->prot);
515-
goto pin_page_exit;
497+
if (ret == -ENOMEM)
498+
pr_warn("%s: Task %s (%d) RLIMIT_MEMLOCK "
499+
"(%ld) exceeded\n", __func__,
500+
dma->task->comm, task_pid_nr(dma->task),
501+
task_rlimit(dma->task, RLIMIT_MEMLOCK));
516502
}
517503
}
518504

519-
ret = 1;
520-
521-
pin_page_exit:
522505
mmput(mm);
523506
return ret;
524507
}
@@ -598,10 +581,8 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data,
598581
remote_vaddr = dma->vaddr + iova - dma->iova;
599582
ret = vfio_pin_page_external(dma, remote_vaddr, &phys_pfn[i],
600583
do_accounting);
601-
if (ret <= 0) {
602-
WARN_ON(!ret);
584+
if (ret)
603585
goto pin_unwind;
604-
}
605586

606587
ret = vfio_add_to_pfn_list(dma, iova, phys_pfn[i]);
607588
if (ret) {

0 commit comments

Comments
 (0)