diff mbox series

[RFC,v3,6/9] vfio/pci: export vfio_pci_setup_barmap

Message ID 20200211101419.21067-1-yan.y.zhao@intel.com (mailing list archive)
State New, archived
Headers show
Series Introduce vendor ops in vfio-pci | expand

Commit Message

Yan Zhao Feb. 11, 2020, 10:14 a.m. UTC
This allows vendor driver to read/write to bars directly which is useful
in security checking condition.

Signed-off-by: Yan Zhao <yan.y.zhao@intel.com>
---
 drivers/vfio/pci/vfio_pci_rdwr.c | 26 +++++++++++++-------------
 include/linux/vfio.h             |  2 ++
 2 files changed, 15 insertions(+), 13 deletions(-)

Comments

Alex Williamson Feb. 20, 2020, 9 p.m. UTC | #1
On Tue, 11 Feb 2020 05:14:19 -0500
Yan Zhao <yan.y.zhao@intel.com> wrote:

> This allows vendor driver to read/write to bars directly which is useful
> in security checking condition.
> 
> Signed-off-by: Yan Zhao <yan.y.zhao@intel.com>
> ---
>  drivers/vfio/pci/vfio_pci_rdwr.c | 26 +++++++++++++-------------
>  include/linux/vfio.h             |  2 ++
>  2 files changed, 15 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
> index a0ef1de4f74a..3ba85fb2af5b 100644
> --- a/drivers/vfio/pci/vfio_pci_rdwr.c
> +++ b/drivers/vfio/pci/vfio_pci_rdwr.c
> @@ -129,7 +129,7 @@ static ssize_t do_io_rw(void __iomem *io, char __user *buf,
>  	return done;
>  }
>  
> -static int vfio_pci_setup_barmap(struct vfio_pci_device *vdev, int bar)
> +void __iomem *vfio_pci_setup_barmap(struct vfio_pci_device *vdev, int bar)
>  {
>  	struct pci_dev *pdev = vdev->pdev;
>  	struct vfio_pci_device_private *priv = VDEV_TO_PRIV(vdev);
> @@ -137,22 +137,23 @@ static int vfio_pci_setup_barmap(struct vfio_pci_device *vdev, int bar)
>  	void __iomem *io;
>  
>  	if (priv->barmap[bar])
> -		return 0;
> +		return priv->barmap[bar];
>  
>  	ret = pci_request_selected_regions(pdev, 1 << bar, "vfio");
>  	if (ret)
> -		return ret;
> +		return NULL;
>  
>  	io = pci_iomap(pdev, bar, 0);
>  	if (!io) {
>  		pci_release_selected_regions(pdev, 1 << bar);
> -		return -ENOMEM;
> +		return NULL;
>  	}
>  
>  	priv->barmap[bar] = io;
>  
> -	return 0;
> +	return io;
>  }
> +EXPORT_SYMBOL_GPL(vfio_pci_setup_barmap);

This should instead become a vfio_pci_get_barmap() function that tests
for an optionally calls vfio_pci_setup_barmap before returning the
pointer.  I'm now willing to lose the better error returns in the
original.  Thanks,

Alex
Yan Zhao Feb. 21, 2020, 6:20 a.m. UTC | #2
On Fri, Feb 21, 2020 at 05:00:13AM +0800, Alex Williamson wrote:
> On Tue, 11 Feb 2020 05:14:19 -0500
> Yan Zhao <yan.y.zhao@intel.com> wrote:
> 
> > This allows vendor driver to read/write to bars directly which is useful
> > in security checking condition.
> > 
> > Signed-off-by: Yan Zhao <yan.y.zhao@intel.com>
> > ---
> >  drivers/vfio/pci/vfio_pci_rdwr.c | 26 +++++++++++++-------------
> >  include/linux/vfio.h             |  2 ++
> >  2 files changed, 15 insertions(+), 13 deletions(-)
> > 
> > diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
> > index a0ef1de4f74a..3ba85fb2af5b 100644
> > --- a/drivers/vfio/pci/vfio_pci_rdwr.c
> > +++ b/drivers/vfio/pci/vfio_pci_rdwr.c
> > @@ -129,7 +129,7 @@ static ssize_t do_io_rw(void __iomem *io, char __user *buf,
> >  	return done;
> >  }
> >  
> > -static int vfio_pci_setup_barmap(struct vfio_pci_device *vdev, int bar)
> > +void __iomem *vfio_pci_setup_barmap(struct vfio_pci_device *vdev, int bar)
> >  {
> >  	struct pci_dev *pdev = vdev->pdev;
> >  	struct vfio_pci_device_private *priv = VDEV_TO_PRIV(vdev);
> > @@ -137,22 +137,23 @@ static int vfio_pci_setup_barmap(struct vfio_pci_device *vdev, int bar)
> >  	void __iomem *io;
> >  
> >  	if (priv->barmap[bar])
> > -		return 0;
> > +		return priv->barmap[bar];
> >  
> >  	ret = pci_request_selected_regions(pdev, 1 << bar, "vfio");
> >  	if (ret)
> > -		return ret;
> > +		return NULL;
> >  
> >  	io = pci_iomap(pdev, bar, 0);
> >  	if (!io) {
> >  		pci_release_selected_regions(pdev, 1 << bar);
> > -		return -ENOMEM;
> > +		return NULL;
> >  	}
> >  
> >  	priv->barmap[bar] = io;
> >  
> > -	return 0;
> > +	return io;
> >  }
> > +EXPORT_SYMBOL_GPL(vfio_pci_setup_barmap);
> 
> This should instead become a vfio_pci_get_barmap() function that tests
> for an optionally calls vfio_pci_setup_barmap before returning the
> pointer.  I'm now willing to lose the better error returns in the
> original.  Thanks,
>
Got it. will change it.
Thanks!

Yan
diff mbox series

Patch

diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
index a0ef1de4f74a..3ba85fb2af5b 100644
--- a/drivers/vfio/pci/vfio_pci_rdwr.c
+++ b/drivers/vfio/pci/vfio_pci_rdwr.c
@@ -129,7 +129,7 @@  static ssize_t do_io_rw(void __iomem *io, char __user *buf,
 	return done;
 }
 
-static int vfio_pci_setup_barmap(struct vfio_pci_device *vdev, int bar)
+void __iomem *vfio_pci_setup_barmap(struct vfio_pci_device *vdev, int bar)
 {
 	struct pci_dev *pdev = vdev->pdev;
 	struct vfio_pci_device_private *priv = VDEV_TO_PRIV(vdev);
@@ -137,22 +137,23 @@  static int vfio_pci_setup_barmap(struct vfio_pci_device *vdev, int bar)
 	void __iomem *io;
 
 	if (priv->barmap[bar])
-		return 0;
+		return priv->barmap[bar];
 
 	ret = pci_request_selected_regions(pdev, 1 << bar, "vfio");
 	if (ret)
-		return ret;
+		return NULL;
 
 	io = pci_iomap(pdev, bar, 0);
 	if (!io) {
 		pci_release_selected_regions(pdev, 1 << bar);
-		return -ENOMEM;
+		return NULL;
 	}
 
 	priv->barmap[bar] = io;
 
-	return 0;
+	return io;
 }
+EXPORT_SYMBOL_GPL(vfio_pci_setup_barmap);
 
 ssize_t vfio_pci_bar_rw(struct vfio_pci_device *vdev, char __user *buf,
 			size_t count, loff_t *ppos, bool iswrite)
@@ -190,11 +191,9 @@  ssize_t vfio_pci_bar_rw(struct vfio_pci_device *vdev, char __user *buf,
 			return -ENOMEM;
 		x_end = end;
 	} else {
-		int ret = vfio_pci_setup_barmap(vdev, bar);
-		if (ret)
-			return ret;
-
-		io = priv->barmap[bar];
+		io = vfio_pci_setup_barmap(vdev, bar);
+		if (!io)
+			return -EFAULT;
 	}
 
 	if (bar == priv->msix_bar) {
@@ -309,6 +308,7 @@  long vfio_pci_ioeventfd(struct vfio_pci_device *vdev, loff_t offset,
 	loff_t pos = offset & VFIO_PCI_OFFSET_MASK;
 	int ret, bar = VFIO_PCI_OFFSET_TO_INDEX(offset);
 	struct vfio_pci_ioeventfd *ioeventfd;
+	void __iomem *io;
 
 	/* Only support ioeventfds into BARs */
 	if (bar > VFIO_PCI_BAR5_REGION_INDEX)
@@ -328,9 +328,9 @@  long vfio_pci_ioeventfd(struct vfio_pci_device *vdev, loff_t offset,
 		return -EINVAL;
 #endif
 
-	ret = vfio_pci_setup_barmap(vdev, bar);
-	if (ret)
-		return ret;
+	io = vfio_pci_setup_barmap(vdev, bar);
+	if (!io)
+		return -EFAULT;
 
 	mutex_lock(&priv->ioeventfds_lock);
 
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index 4d7e80b2ed1b..00112ffd9ad0 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -223,6 +223,8 @@  extern int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma);
 extern void vfio_pci_request(void *device_data, unsigned int count);
 extern int vfio_pci_open(void *device_data);
 extern void vfio_pci_release(void *device_data);
+extern void __iomem *vfio_pci_setup_barmap(struct vfio_pci_device *vdev,
+					   int bar);
 
 #define vfio_pci_register_vendor_driver(__name, __probe, __remove,	\
 					__device_ops)			\