drm/i915/gvt: Fix mmap range check

Submitted by Zhenyu Wang on Jan. 11, 2019, 5:58 a.m.

Details

Message ID 20190111055853.1943-1-zhenyuw@linux.intel.com
State New
Headers show
Series "drm/i915/gvt: Fix mmap range check" ( rev: 1 ) in Intel GVT devel

Not browsing as part of any series.

Commit Message

Zhenyu Wang Jan. 11, 2019, 5:58 a.m.
This is to fix missed mmap range check on vGPU bar2 region
and only allow to map vGPU allocated GMADDR range, which means
user space should support sparse mmap to get proper offset for
mmap vGPU aperture. And this takes care of actual pgoff in mmap
request as original code always does from beginning of vGPU
aperture.

Fixes: 659643f7d814 ("drm/i915/gvt/kvmgt: add vfio/mdev support to KVMGT")
Cc: "Monroy, Rodrigo Axel" <rodrigo.axel.monroy@intel.com>
Cc: "Orrala Contreras, Alfredo" <alfredo.orrala.contreras@intel.com>
Cc: stable@vger.kernel.org # v4.10+
Signed-off-by: Zhenyu Wang <zhenyuw@linux.intel.com>
---
 drivers/gpu/drm/i915/gvt/kvmgt.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

Patch hide | download patch | download mbox

diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index a19e684e621a..5488c2e2a3ff 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -1005,7 +1005,7 @@  static int intel_vgpu_mmap(struct mdev_device *mdev, struct vm_area_struct *vma)
 {
 	unsigned int index;
 	u64 virtaddr;
-	unsigned long req_size, pgoff = 0;
+	unsigned long req_size, pgoff, req_start;
 	pgprot_t pg_prot;
 	struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
 
@@ -1023,7 +1023,17 @@  static int intel_vgpu_mmap(struct mdev_device *mdev, struct vm_area_struct *vma)
 	pg_prot = vma->vm_page_prot;
 	virtaddr = vma->vm_start;
 	req_size = vma->vm_end - vma->vm_start;
-	pgoff = vgpu_aperture_pa_base(vgpu) >> PAGE_SHIFT;
+	pgoff = vma->vm_pgoff &
+		((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
+	req_start = pgoff << PAGE_SHIFT;
+
+	if (!intel_vgpu_in_aperture(vgpu, req_start))
+		return -EINVAL;
+	if (req_start + req_size >
+	    vgpu_aperture_offset(vgpu) + vgpu_aperture_sz(vgpu))
+		return -EINVAL;
+
+	pgoff = (gvt_aperture_pa_base(vgpu->gvt) >> PAGE_SHIFT) + pgoff;
 
 	return remap_pfn_range(vma, virtaddr, pgoff, req_size, pg_prot);
 }

Comments


Looks fine to me. Thanks.

Reviewed-by: Hang Yuan <hang.yuan@intel.com>


> -----Original Message-----

> From: intel-gvt-dev [mailto:intel-gvt-dev-bounces@lists.freedesktop.org] On

> Behalf Of Zhenyu Wang

> Sent: Friday, January 11, 2019 1:59 PM

> To: intel-gvt-dev@lists.freedesktop.org

> Cc: Monroy, Rodrigo Axel <rodrigo.axel.monroy@intel.com>; Orrala

> Contreras, Alfredo <alfredo.orrala.contreras@intel.com>;

> stable@vger.kernel.org

> Subject: [PATCH] drm/i915/gvt: Fix mmap range check

> 

> This is to fix missed mmap range check on vGPU bar2 region and only allow

> to map vGPU allocated GMADDR range, which means user space should

> support sparse mmap to get proper offset for mmap vGPU aperture. And

> this takes care of actual pgoff in mmap request as original code always does

> from beginning of vGPU aperture.

> 

> Fixes: 659643f7d814 ("drm/i915/gvt/kvmgt: add vfio/mdev support to

> KVMGT")

> Cc: "Monroy, Rodrigo Axel" <rodrigo.axel.monroy@intel.com>

> Cc: "Orrala Contreras, Alfredo" <alfredo.orrala.contreras@intel.com>

> Cc: stable@vger.kernel.org # v4.10+

> Signed-off-by: Zhenyu Wang <zhenyuw@linux.intel.com>

> ---

>  drivers/gpu/drm/i915/gvt/kvmgt.c | 14 ++++++++++++--

>  1 file changed, 12 insertions(+), 2 deletions(-)

> 

> diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c

> b/drivers/gpu/drm/i915/gvt/kvmgt.c

> index a19e684e621a..5488c2e2a3ff 100644

> --- a/drivers/gpu/drm/i915/gvt/kvmgt.c

> +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c

> @@ -1005,7 +1005,7 @@ static int intel_vgpu_mmap(struct mdev_device

> *mdev, struct vm_area_struct *vma)  {

>  	unsigned int index;

>  	u64 virtaddr;

> -	unsigned long req_size, pgoff = 0;

> +	unsigned long req_size, pgoff, req_start;

>  	pgprot_t pg_prot;

>  	struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);

> 

> @@ -1023,7 +1023,17 @@ static int intel_vgpu_mmap(struct mdev_device

> *mdev, struct vm_area_struct *vma)

>  	pg_prot = vma->vm_page_prot;

>  	virtaddr = vma->vm_start;

>  	req_size = vma->vm_end - vma->vm_start;

> -	pgoff = vgpu_aperture_pa_base(vgpu) >> PAGE_SHIFT;

> +	pgoff = vma->vm_pgoff &

> +		((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);

> +	req_start = pgoff << PAGE_SHIFT;

> +

> +	if (!intel_vgpu_in_aperture(vgpu, req_start))

> +		return -EINVAL;

> +	if (req_start + req_size >

> +	    vgpu_aperture_offset(vgpu) + vgpu_aperture_sz(vgpu))

> +		return -EINVAL;

> +

> +	pgoff = (gvt_aperture_pa_base(vgpu->gvt) >> PAGE_SHIFT) + pgoff;

> 

>  	return remap_pfn_range(vma, virtaddr, pgoff, req_size, pg_prot);  }

> --

> 2.19.1

> 

> _______________________________________________

> intel-gvt-dev mailing list

> intel-gvt-dev@lists.freedesktop.org

> https://lists.freedesktop.org/mailman/listinfo/intel-gvt-dev
Hi,

[This is an automated email]

This commit has been processed because it contains a "Fixes:" tag,
fixing commit: 659643f7d814 drm/i915/gvt/kvmgt: add vfio/mdev support to KVMGT.

The bot has tested the following trees: v4.20.2, v4.19.15, v4.14.93.

v4.20.2: Build OK!
v4.19.15: Build OK!
v4.14.93: Build failed! Errors:
    drivers/gpu/drm/i915/gvt/kvmgt.c:836:7: error: implicit declaration of function ‘intel_vgpu_in_aperture’; did you mean ‘intel_vgpu_create’? [-Werror=implicit-function-declaration]


How should we proceed with this patch?

--
Thanks,
Sasha
On 2019.01.16 13:35:56 +0000, Sasha Levin wrote:
> Hi,
> 
> [This is an automated email]
> 
> This commit has been processed because it contains a "Fixes:" tag,
> fixing commit: 659643f7d814 drm/i915/gvt/kvmgt: add vfio/mdev support to KVMGT.
> 
> The bot has tested the following trees: v4.20.2, v4.19.15, v4.14.93.
> 
> v4.20.2: Build OK!
> v4.19.15: Build OK!
> v4.14.93: Build failed! Errors:
>     drivers/gpu/drm/i915/gvt/kvmgt.c:836:7: error: implicit declaration of function ???intel_vgpu_in_aperture???; did you mean ???intel_vgpu_create???? [-Werror=implicit-function-declaration]
> 
> 
> How should we proceed with this patch?
>

I'll prepare a backport version once this one hits linux master.

Thanks.