@@ -257,7 +257,8 @@ static int vfio_iova_put_vfio_pfn(struct vfio_dma *dma, struct vfio_pfn *vpfn)
static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async)
{
struct mm_struct *mm;
- int ret;
+ s64 locked_vm;
+ int ret = 0;
if (!npage)
return 0;
@@ -266,25 +267,15 @@ static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async)
if (!mm)
return -ESRCH; /* process exited */
- ret = down_write_killable(&mm->mmap_sem);
- if (!ret) {
- if (npage > 0) {
- if (!dma->lock_cap) {
- s64 locked_vm = atomic64_read(&mm->locked_vm);
- unsigned long limit;
-
- limit = task_rlimit(dma->task,
- RLIMIT_MEMLOCK) >> PAGE_SHIFT;
+ locked_vm = atomic64_add_return(npage, &mm->locked_vm);
- if (locked_vm + npage > limit)
- ret = -ENOMEM;
- }
+ if (npage > 0 && !dma->lock_cap) {
+ unsigned long limit = task_rlimit(dma->task, RLIMIT_MEMLOCK) >>
+ PAGE_SHIFT;
+ if (locked_vm > limit) {
+ atomic64_sub(npage, &mm->locked_vm);
+ ret = -ENOMEM;
}
-
- if (!ret)
- atomic64_add(npage, &mm->locked_vm);
-
- up_write(&mm->mmap_sem);
}
if (async)
With locked_vm now an atomic, there is no need to take mmap_sem as writer. Delete and refactor accordingly. Signed-off-by: Daniel Jordan <daniel.m.jordan@oracle.com> Cc: Alex Williamson <alex.williamson@redhat.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Christoph Lameter <cl@linux.com> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: <linux-mm@kvack.org> Cc: <kvm@vger.kernel.org> Cc: <linux-kernel@vger.kernel.org> --- drivers/vfio/vfio_iommu_type1.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-)