vfio/type1: prevent underflow of locked_vm via exec()
authorSteve Sistare <steven.sistare@oracle.com>
Tue, 31 Jan 2023 16:58:04 +0000 (08:58 -0800)
committerAlex Williamson <alex.williamson@redhat.com>
Thu, 9 Feb 2023 18:39:14 +0000 (11:39 -0700)
commit046eca5018f8a5dd1dc2cedf87fb5843b9ea3026
treebb4f28348e7ac703569d12aa7b247897a9fa8c2a
parentef3a3f6a294ba65fd906a291553935881796f8a5
vfio/type1: prevent underflow of locked_vm via exec()

When a vfio container is preserved across exec, the task does not change,
but it gets a new mm with locked_vm=0, and loses the count from existing
dma mappings.  If the user later unmaps a dma mapping, locked_vm underflows
to a large unsigned value, and a subsequent dma map request fails with
ENOMEM in __account_locked_vm.

To avoid underflow, grab and save the mm at the time a dma is mapped.
Use that mm when adjusting locked_vm, rather than re-acquiring the saved
task's mm, which may have changed.  If the saved mm is dead, do nothing.

locked_vm is incremented for existing mappings in a subsequent patch.

Fixes: 73fa0d10d077 ("vfio: Type1 IOMMU implementation")
Cc: stable@vger.kernel.org
Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/1675184289-267876-3-git-send-email-steven.sistare@oracle.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
drivers/vfio/vfio_iommu_type1.c