drm/i915/kvmgt: Handle kzalloc failure

Submitted by changbin.du@intel.com on March 12, 2018, 7:12 a.m.

Details

Message ID 1520838754-7766-1-git-send-email-changbin.du@intel.com
State New
Headers show
Series "drm/i915/kvmgt: Handle kzalloc failure" ( rev: 1 ) in Intel GVT devel

Not browsing as part of any series.

Commit Message

changbin.du@intel.com March 12, 2018, 7:12 a.m.
From: Changbin Du <changbin.du@intel.com>

Out-of-memory error must be handled correctly.

Signed-off-by: Changbin Du <changbin.du@intel.com>
---
 drivers/gpu/drm/i915/gvt/kvmgt.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 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 725dc78..c16a492 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -184,7 +184,7 @@  static struct gvt_dma *__gvt_cache_find_gfn(struct intel_vgpu *vgpu, gfn_t gfn)
 	return NULL;
 }
 
-static void __gvt_cache_add(struct intel_vgpu *vgpu, gfn_t gfn,
+static int __gvt_cache_add(struct intel_vgpu *vgpu, gfn_t gfn,
 		dma_addr_t dma_addr)
 {
 	struct gvt_dma *new, *itr;
@@ -192,7 +192,7 @@  static void __gvt_cache_add(struct intel_vgpu *vgpu, gfn_t gfn,
 
 	new = kzalloc(sizeof(struct gvt_dma), GFP_KERNEL);
 	if (!new)
-		return;
+		return -ENOMEM;
 
 	new->vgpu = vgpu;
 	new->gfn = gfn;
@@ -229,6 +229,7 @@  static void __gvt_cache_add(struct intel_vgpu *vgpu, gfn_t gfn,
 	rb_insert_color(&new->dma_addr_node, &vgpu->vdev.dma_addr_cache);
 
 	vgpu->vdev.nr_cache_entries++;
+	return 0;
 }
 
 static void __gvt_cache_remove_entry(struct intel_vgpu *vgpu,
@@ -1633,11 +1634,12 @@  int kvmgt_dma_map_guest_page(unsigned long handle, unsigned long gfn,
 	entry = __gvt_cache_find_gfn(info->vgpu, gfn);
 	if (!entry) {
 		ret = gvt_dma_map_page(vgpu, gfn, dma_addr);
-		if (ret) {
-			mutex_unlock(&info->vgpu->vdev.cache_lock);
-			return ret;
-		}
-		__gvt_cache_add(info->vgpu, gfn, *dma_addr);
+		if (ret)
+			goto err_unlock;
+
+		ret = __gvt_cache_add(info->vgpu, gfn, *dma_addr);
+		if (ret)
+			goto err_unmap;
 	} else {
 		kref_get(&entry->ref);
 		*dma_addr = entry->dma_addr;
@@ -1645,6 +1647,12 @@  int kvmgt_dma_map_guest_page(unsigned long handle, unsigned long gfn,
 
 	mutex_unlock(&info->vgpu->vdev.cache_lock);
 	return 0;
+
+err_unmap:
+	gvt_dma_unmap_page(vgpu, gfn, *dma_addr);
+err_unlock:
+	mutex_unlock(&info->vgpu->vdev.cache_lock);
+	return ret;
 }
 
 static void __gvt_dma_release(struct kref *ref)

Comments

On 2018.03.12 15:12:34 +0800, changbin.du@intel.com wrote:
> From: Changbin Du <changbin.du@intel.com>
> 
> Out-of-memory error must be handled correctly.
> 
> Signed-off-by: Changbin Du <changbin.du@intel.com>
> ---

Looks good to me, applied, thanks!

>  drivers/gpu/drm/i915/gvt/kvmgt.c | 22 +++++++++++++++-------
>  1 file changed, 15 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
> index 725dc78..c16a492 100644
> --- a/drivers/gpu/drm/i915/gvt/kvmgt.c
> +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
> @@ -184,7 +184,7 @@ static struct gvt_dma *__gvt_cache_find_gfn(struct intel_vgpu *vgpu, gfn_t gfn)
>  	return NULL;
>  }
>  
> -static void __gvt_cache_add(struct intel_vgpu *vgpu, gfn_t gfn,
> +static int __gvt_cache_add(struct intel_vgpu *vgpu, gfn_t gfn,
>  		dma_addr_t dma_addr)
>  {
>  	struct gvt_dma *new, *itr;
> @@ -192,7 +192,7 @@ static void __gvt_cache_add(struct intel_vgpu *vgpu, gfn_t gfn,
>  
>  	new = kzalloc(sizeof(struct gvt_dma), GFP_KERNEL);
>  	if (!new)
> -		return;
> +		return -ENOMEM;
>  
>  	new->vgpu = vgpu;
>  	new->gfn = gfn;
> @@ -229,6 +229,7 @@ static void __gvt_cache_add(struct intel_vgpu *vgpu, gfn_t gfn,
>  	rb_insert_color(&new->dma_addr_node, &vgpu->vdev.dma_addr_cache);
>  
>  	vgpu->vdev.nr_cache_entries++;
> +	return 0;
>  }
>  
>  static void __gvt_cache_remove_entry(struct intel_vgpu *vgpu,
> @@ -1633,11 +1634,12 @@ int kvmgt_dma_map_guest_page(unsigned long handle, unsigned long gfn,
>  	entry = __gvt_cache_find_gfn(info->vgpu, gfn);
>  	if (!entry) {
>  		ret = gvt_dma_map_page(vgpu, gfn, dma_addr);
> -		if (ret) {
> -			mutex_unlock(&info->vgpu->vdev.cache_lock);
> -			return ret;
> -		}
> -		__gvt_cache_add(info->vgpu, gfn, *dma_addr);
> +		if (ret)
> +			goto err_unlock;
> +
> +		ret = __gvt_cache_add(info->vgpu, gfn, *dma_addr);
> +		if (ret)
> +			goto err_unmap;
>  	} else {
>  		kref_get(&entry->ref);
>  		*dma_addr = entry->dma_addr;
> @@ -1645,6 +1647,12 @@ int kvmgt_dma_map_guest_page(unsigned long handle, unsigned long gfn,
>  
>  	mutex_unlock(&info->vgpu->vdev.cache_lock);
>  	return 0;
> +
> +err_unmap:
> +	gvt_dma_unmap_page(vgpu, gfn, *dma_addr);
> +err_unlock:
> +	mutex_unlock(&info->vgpu->vdev.cache_lock);
> +	return ret;
>  }
>  
>  static void __gvt_dma_release(struct kref *ref)
> -- 
> 2.7.4
> 
> _______________________________________________
> intel-gvt-dev mailing list
> intel-gvt-dev@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/intel-gvt-dev