diff mbox series

[v2,10/10] vfio: Do not allow !ops->dma_unmap in vfio_pin/unpin_pages()

Message ID 59e5eeac675172ab1cb07236a3eb3e166553fe71.1675802050.git.nicolinc@nvidia.com (mailing list archive)
State New
Headers show
Series Add IO page table replacement support | expand

Commit Message

Nicolin Chen Feb. 7, 2023, 9:18 p.m. UTC
A driver that doesn't implement ops->dma_unmap shouldn't be allowed to do
vfio_pin/unpin_pages(), though it can use vfio_dma_rw() to access an iova
range. Deny !ops->dma_unmap cases in vfio_pin/unpin_pages().

Suggested-by: Kevin Tian <kevin.tian@intel.com>
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
 drivers/vfio/vfio_main.c | 4 ++++
 1 file changed, 4 insertions(+)

Comments

Tian, Kevin Feb. 9, 2023, 4:10 a.m. UTC | #1
> From: Nicolin Chen <nicolinc@nvidia.com>
> Sent: Wednesday, February 8, 2023 5:18 AM
> 
> A driver that doesn't implement ops->dma_unmap shouldn't be allowed to
> do
> vfio_pin/unpin_pages(), though it can use vfio_dma_rw() to access an iova
> range. Deny !ops->dma_unmap cases in vfio_pin/unpin_pages().
> 
> Suggested-by: Kevin Tian <kevin.tian@intel.com>
> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
> ---
>  drivers/vfio/vfio_main.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
> index 8559c3dfb335..c7f3251ad6e5 100644
> --- a/drivers/vfio/vfio_main.c
> +++ b/drivers/vfio/vfio_main.c
> @@ -1543,6 +1543,8 @@ int vfio_pin_pages(struct vfio_device *device,
> dma_addr_t iova,
> 
>  		if (iova > ULONG_MAX)
>  			return -EINVAL;
> +		if (!device->ops->dma_unmap)
> +			return -EINVAL;
>  		/*
>  		 * VFIO ignores the sub page offset, npages is from the start
> of
>  		 * a PAGE_SIZE chunk of IOVA. The caller is expected to
> recover
> @@ -1580,6 +1582,8 @@ void vfio_unpin_pages(struct vfio_device *device,
> dma_addr_t iova, int npage)
>  	if (device->iommufd_access) {
>  		if (WARN_ON(iova > ULONG_MAX))
>  			return;
> +		if (!device->ops->dma_unmap)
> +			return;

IMHO this restriction applies to both iommufd and legacy container.
Jason Gunthorpe Feb. 9, 2023, 1:26 p.m. UTC | #2
On Thu, Feb 09, 2023 at 04:10:04AM +0000, Tian, Kevin wrote:
> > From: Nicolin Chen <nicolinc@nvidia.com>
> > Sent: Wednesday, February 8, 2023 5:18 AM
> > 
> > A driver that doesn't implement ops->dma_unmap shouldn't be allowed to
> > do
> > vfio_pin/unpin_pages(), though it can use vfio_dma_rw() to access an iova
> > range. Deny !ops->dma_unmap cases in vfio_pin/unpin_pages().
> > 
> > Suggested-by: Kevin Tian <kevin.tian@intel.com>
> > Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
> > ---
> >  drivers/vfio/vfio_main.c | 4 ++++
> >  1 file changed, 4 insertions(+)
> > 
> > diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
> > index 8559c3dfb335..c7f3251ad6e5 100644
> > --- a/drivers/vfio/vfio_main.c
> > +++ b/drivers/vfio/vfio_main.c
> > @@ -1543,6 +1543,8 @@ int vfio_pin_pages(struct vfio_device *device,
> > dma_addr_t iova,
> > 
> >  		if (iova > ULONG_MAX)
> >  			return -EINVAL;
> > +		if (!device->ops->dma_unmap)
> > +			return -EINVAL;
> >  		/*
> >  		 * VFIO ignores the sub page offset, npages is from the start
> > of
> >  		 * a PAGE_SIZE chunk of IOVA. The caller is expected to
> > recover
> > @@ -1580,6 +1582,8 @@ void vfio_unpin_pages(struct vfio_device *device,
> > dma_addr_t iova, int npage)
> >  	if (device->iommufd_access) {
> >  		if (WARN_ON(iova > ULONG_MAX))
> >  			return;
> > +		if (!device->ops->dma_unmap)
> > +			return;
> 
> IMHO this restriction applies to both iommufd and legacy container.

Yeah that makes sense

Jason
Nicolin Chen Feb. 9, 2023, 4:19 p.m. UTC | #3
On Thu, Feb 09, 2023 at 09:26:15AM -0400, Jason Gunthorpe wrote:
> On Thu, Feb 09, 2023 at 04:10:04AM +0000, Tian, Kevin wrote:
> > > From: Nicolin Chen <nicolinc@nvidia.com>
> > > Sent: Wednesday, February 8, 2023 5:18 AM
> > > 
> > > A driver that doesn't implement ops->dma_unmap shouldn't be allowed to
> > > do
> > > vfio_pin/unpin_pages(), though it can use vfio_dma_rw() to access an iova
> > > range. Deny !ops->dma_unmap cases in vfio_pin/unpin_pages().
> > > 
> > > Suggested-by: Kevin Tian <kevin.tian@intel.com>
> > > Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
> > > ---
> > >  drivers/vfio/vfio_main.c | 4 ++++
> > >  1 file changed, 4 insertions(+)
> > > 
> > > diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
> > > index 8559c3dfb335..c7f3251ad6e5 100644
> > > --- a/drivers/vfio/vfio_main.c
> > > +++ b/drivers/vfio/vfio_main.c
> > > @@ -1543,6 +1543,8 @@ int vfio_pin_pages(struct vfio_device *device,
> > > dma_addr_t iova,
> > > 
> > >  		if (iova > ULONG_MAX)
> > >  			return -EINVAL;
> > > +		if (!device->ops->dma_unmap)
> > > +			return -EINVAL;
> > >  		/*
> > >  		 * VFIO ignores the sub page offset, npages is from the start
> > > of
> > >  		 * a PAGE_SIZE chunk of IOVA. The caller is expected to
> > > recover
> > > @@ -1580,6 +1582,8 @@ void vfio_unpin_pages(struct vfio_device *device,
> > > dma_addr_t iova, int npage)
> > >  	if (device->iommufd_access) {
> > >  		if (WARN_ON(iova > ULONG_MAX))
> > >  			return;
> > > +		if (!device->ops->dma_unmap)
> > > +			return;
> > 
> > IMHO this restriction applies to both iommufd and legacy container.
> 
> Yeah that makes sense

Will move them to the beginning of vfio_pin_pages/vfio_unpin_pages.

Thanks
Nic
diff mbox series

Patch

diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 8559c3dfb335..c7f3251ad6e5 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -1543,6 +1543,8 @@  int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
 
 		if (iova > ULONG_MAX)
 			return -EINVAL;
+		if (!device->ops->dma_unmap)
+			return -EINVAL;
 		/*
 		 * VFIO ignores the sub page offset, npages is from the start of
 		 * a PAGE_SIZE chunk of IOVA. The caller is expected to recover
@@ -1580,6 +1582,8 @@  void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage)
 	if (device->iommufd_access) {
 		if (WARN_ON(iova > ULONG_MAX))
 			return;
+		if (!device->ops->dma_unmap)
+			return;
 		iommufd_access_unpin_pages(device->iommufd_access,
 					   ALIGN_DOWN(iova, PAGE_SIZE),
 					   npage * PAGE_SIZE);