All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] drm: modify drm_global_item_ref to avoid two times of writing ref->object
@ 2016-09-07  3:26 Huang Rui
       [not found] ` <1473218799-6890-1-git-send-email-ray.huang-5C7GfCeVMHo@public.gmane.org>
  0 siblings, 1 reply; 2+ messages in thread
From: Huang Rui @ 2016-09-07  3:26 UTC (permalink / raw)
  To: dri-devel-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW, Alex Deucher,
	Dave Airlie, Christian König, Daniel Vetter, Sean Paul
  Cc: Ken Wang, Huang Rui, amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW

In previous drm_global_item_ref, there are two times of writing
ref->object if item->refcount is 0. So this patch does a minor update
to put alloc and init ref firstly, and then to modify the item of glob
array. Use "else" to avoid two times of writing ref->object. It can
make the code logic more clearly.

Signed-off-by: Huang Rui <ray.huang@amd.com>
---

Changes from V1 -> V2:
- Add kfree exceptional handle to avoid memory leak.
- Improve code style.

---
 drivers/gpu/drm/drm_global.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/drivers/gpu/drm/drm_global.c b/drivers/gpu/drm/drm_global.c
index 3d2e91c..6a63808 100644
--- a/drivers/gpu/drm/drm_global.c
+++ b/drivers/gpu/drm/drm_global.c
@@ -65,30 +65,31 @@ void drm_global_release(void)
 
 int drm_global_item_ref(struct drm_global_reference *ref)
 {
-	int ret;
+	int ret = -ENOMEM;
 	struct drm_global_item *item = &glob[ref->global_type];
 
 	mutex_lock(&item->mutex);
 	if (item->refcount == 0) {
-		item->object = kzalloc(ref->size, GFP_KERNEL);
-		if (unlikely(item->object == NULL)) {
-			ret = -ENOMEM;
+		ref->object = kzalloc(ref->size, GFP_KERNEL);
+		if (unlikely(ref->object == NULL))
 			goto out_err;
-		}
-
-		ref->object = item->object;
 		ret = ref->init(ref);
 		if (unlikely(ret != 0))
-			goto out_err;
+			goto out;
 
+		item->object = ref->object;
+	} else {
+		ref->object = item->object;
 	}
+
 	++item->refcount;
-	ref->object = item->object;
-	mutex_unlock(&item->mutex);
-	return 0;
+	goto out;
+
 out_err:
+	kfree(ref->object);
+	ref->object = NULL;
+out:
 	mutex_unlock(&item->mutex);
-	item->object = NULL;
 	return ret;
 }
 EXPORT_SYMBOL(drm_global_item_ref);
-- 
1.9.1

_______________________________________________
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] drm: modify drm_global_item_ref to avoid two times of writing ref->object
       [not found] ` <1473218799-6890-1-git-send-email-ray.huang-5C7GfCeVMHo@public.gmane.org>
@ 2016-09-07  4:51   ` Huang Rui
  0 siblings, 0 replies; 2+ messages in thread
From: Huang Rui @ 2016-09-07  4:51 UTC (permalink / raw)
  To: dri-devel-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW, Deucher, Alexander,
	Dave Airlie, Koenig, Christian, Daniel Vetter, Sean Paul
  Cc: Wang, Ken, amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW

Sorry, please ignore this V2, there is a typo in "goto out" at this patch.
I will send a "update V2" later.

Thanks,
Rui

On Wed, Sep 07, 2016 at 11:26:39AM +0800, Huang Rui wrote:
> In previous drm_global_item_ref, there are two times of writing
> ref->object if item->refcount is 0. So this patch does a minor update
> to put alloc and init ref firstly, and then to modify the item of glob
> array. Use "else" to avoid two times of writing ref->object. It can
> make the code logic more clearly.
> 
> Signed-off-by: Huang Rui <ray.huang@amd.com>
> ---
> 
> Changes from V1 -> V2:
> - Add kfree exceptional handle to avoid memory leak.
> - Improve code style.
> 
> ---
>  drivers/gpu/drm/drm_global.c | 25 +++++++++++++------------
>  1 file changed, 13 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/gpu/drm/drm_global.c b/drivers/gpu/drm/drm_global.c
> index 3d2e91c..6a63808 100644
> --- a/drivers/gpu/drm/drm_global.c
> +++ b/drivers/gpu/drm/drm_global.c
> @@ -65,30 +65,31 @@ void drm_global_release(void)
>  
>  int drm_global_item_ref(struct drm_global_reference *ref)
>  {
> -	int ret;
> +	int ret = -ENOMEM;
>  	struct drm_global_item *item = &glob[ref->global_type];
>  
>  	mutex_lock(&item->mutex);
>  	if (item->refcount == 0) {
> -		item->object = kzalloc(ref->size, GFP_KERNEL);
> -		if (unlikely(item->object == NULL)) {
> -			ret = -ENOMEM;
> +		ref->object = kzalloc(ref->size, GFP_KERNEL);
> +		if (unlikely(ref->object == NULL))
>  			goto out_err;
> -		}
> -
> -		ref->object = item->object;
>  		ret = ref->init(ref);
>  		if (unlikely(ret != 0))
> -			goto out_err;
> +			goto out;
>  
> +		item->object = ref->object;
> +	} else {
> +		ref->object = item->object;
>  	}
> +
>  	++item->refcount;
> -	ref->object = item->object;
> -	mutex_unlock(&item->mutex);
> -	return 0;
> +	goto out;
> +
>  out_err:
> +	kfree(ref->object);
> +	ref->object = NULL;
> +out:
>  	mutex_unlock(&item->mutex);
> -	item->object = NULL;
>  	return ret;
>  }
>  EXPORT_SYMBOL(drm_global_item_ref);
> -- 
> 1.9.1
> 
_______________________________________________
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-09-07  4:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-07  3:26 [PATCH v2] drm: modify drm_global_item_ref to avoid two times of writing ref->object Huang Rui
     [not found] ` <1473218799-6890-1-git-send-email-ray.huang-5C7GfCeVMHo@public.gmane.org>
2016-09-07  4:51   ` Huang Rui

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.