All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] drm: modify drm_global_item_ref to avoid two times of writing ref->object
@ 2016-09-08  2:07 Huang Rui
  2016-09-08  6:36 ` Chris Wilson
  2016-09-08  7:26 ` Christian König
  0 siblings, 2 replies; 7+ messages in thread
From: Huang Rui @ 2016-09-08  2:07 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 V2 -> V3:
- Use duplicate mutex release to avoid "goto" in non-error patch.
- Rename error label

---
 drivers/gpu/drm/drm_global.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/drivers/gpu/drm/drm_global.c b/drivers/gpu/drm/drm_global.c
index 3d2e91c..b404287 100644
--- a/drivers/gpu/drm/drm_global.c
+++ b/drivers/gpu/drm/drm_global.c
@@ -65,30 +65,34 @@ void drm_global_release(void)
 
 int drm_global_item_ref(struct drm_global_reference *ref)
 {
-	int ret;
+	int ret = 0;
 	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)) {
+		ref->object = kzalloc(ref->size, GFP_KERNEL);
+		if (unlikely(ref->object == NULL)) {
 			ret = -ENOMEM;
-			goto out_err;
+			goto error_unlock;
 		}
-
-		ref->object = item->object;
 		ret = ref->init(ref);
 		if (unlikely(ret != 0))
-			goto out_err;
+			goto error_free;
 
+		item->object = ref->object;
+	} else {
+		ref->object = item->object;
 	}
+
 	++item->refcount;
-	ref->object = item->object;
 	mutex_unlock(&item->mutex);
 	return 0;
-out_err:
+
+error_free:
+	kfree(ref->object);
+	ref->object = NULL;
+error_unlock:
 	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] 7+ messages in thread

* Re: [PATCH v3] drm: modify drm_global_item_ref to avoid two times of writing ref->object
  2016-09-08  2:07 [PATCH v3] drm: modify drm_global_item_ref to avoid two times of writing ref->object Huang Rui
@ 2016-09-08  6:36 ` Chris Wilson
  2016-09-08  7:22   ` Huang Rui
  2016-09-08  7:26 ` Christian König
  1 sibling, 1 reply; 7+ messages in thread
From: Chris Wilson @ 2016-09-08  6:36 UTC (permalink / raw)
  To: Huang Rui
  Cc: Daniel Vetter, dri-devel, amd-gfx, Alex Deucher, Ken Wang,
	Christian König

On Wed, Sep 07, 2016 at 10:07:57PM -0400, 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 V2 -> V3:
> - Use duplicate mutex release to avoid "goto" in non-error patch.
> - Rename error label
> 
> ---
>  drivers/gpu/drm/drm_global.c | 24 ++++++++++++++----------
>  1 file changed, 14 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/gpu/drm/drm_global.c b/drivers/gpu/drm/drm_global.c
> index 3d2e91c..b404287 100644
> --- a/drivers/gpu/drm/drm_global.c
> +++ b/drivers/gpu/drm/drm_global.c
> @@ -65,30 +65,34 @@ void drm_global_release(void)
>  
>  int drm_global_item_ref(struct drm_global_reference *ref)
>  {
> -	int ret;
> +	int ret = 0;
>  	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)) {
> +		ref->object = kzalloc(ref->size, GFP_KERNEL);

So the item refcount is 0, we operate on ref, whereas previous we
inspected item and operated on item. Not an improvement.
-Chris

-- 
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

* Re: [PATCH v3] drm: modify drm_global_item_ref to avoid two times of writing ref->object
  2016-09-08  6:36 ` Chris Wilson
@ 2016-09-08  7:22   ` Huang Rui
  2016-09-08  7:35     ` Chris Wilson
  0 siblings, 1 reply; 7+ messages in thread
From: Huang Rui @ 2016-09-08  7:22 UTC (permalink / raw)
  To: Chris Wilson
  Cc: Daniel Vetter, amd-gfx, dri-devel, Deucher, Alexander, Wang, Ken,
	Koenig, Christian

On Thu, Sep 08, 2016 at 02:36:06PM +0800, Chris Wilson wrote:
> On Wed, Sep 07, 2016 at 10:07:57PM -0400, 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 V2 -> V3:
> > - Use duplicate mutex release to avoid "goto" in non-error patch.
> > - Rename error label
> > 
> > ---
> >  drivers/gpu/drm/drm_global.c | 24 ++++++++++++++----------
> >  1 file changed, 14 insertions(+), 10 deletions(-)
> > 
> > diff --git a/drivers/gpu/drm/drm_global.c b/drivers/gpu/drm/drm_global.c
> > index 3d2e91c..b404287 100644
> > --- a/drivers/gpu/drm/drm_global.c
> > +++ b/drivers/gpu/drm/drm_global.c
> > @@ -65,30 +65,34 @@ void drm_global_release(void)
> >  
> >  int drm_global_item_ref(struct drm_global_reference *ref)
> >  {
> > -	int ret;
> > +	int ret = 0;
> >  	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)) {
> > +		ref->object = kzalloc(ref->size, GFP_KERNEL);
> 
> So the item refcount is 0, we operate on ref, whereas previous we
> inspected item and operated on item. Not an improvement.

Hmm, when item refcount is 0, we operate on ref to create object and init
it for item, so although item->object and ref->object here should point the
same thing, but we should alloc and init ref firstly and pass the
ref->object address to item (item actually points a static area). This make
the code logic more clearly and readable. So I updated it to "ref" here.
:-)

Thanks,
Rui
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

* Re: [PATCH v3] drm: modify drm_global_item_ref to avoid two times of writing ref->object
  2016-09-08  2:07 [PATCH v3] drm: modify drm_global_item_ref to avoid two times of writing ref->object Huang Rui
  2016-09-08  6:36 ` Chris Wilson
@ 2016-09-08  7:26 ` Christian König
  1 sibling, 0 replies; 7+ messages in thread
From: Christian König @ 2016-09-08  7:26 UTC (permalink / raw)
  To: Huang Rui, dri-devel, Alex Deucher, Dave Airlie, Daniel Vetter,
	Sean Paul
  Cc: Ken Wang, amd-gfx

Am 08.09.2016 um 04:07 schrieb Huang Rui:
> 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>

Reviewed-by: Christian König <christian.koenig@amd.com>.

> ---
>
> Changes from V2 -> V3:
> - Use duplicate mutex release to avoid "goto" in non-error patch.
> - Rename error label
>
> ---
>   drivers/gpu/drm/drm_global.c | 24 ++++++++++++++----------
>   1 file changed, 14 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/gpu/drm/drm_global.c b/drivers/gpu/drm/drm_global.c
> index 3d2e91c..b404287 100644
> --- a/drivers/gpu/drm/drm_global.c
> +++ b/drivers/gpu/drm/drm_global.c
> @@ -65,30 +65,34 @@ void drm_global_release(void)
>   
>   int drm_global_item_ref(struct drm_global_reference *ref)
>   {
> -	int ret;
> +	int ret = 0;
>   	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)) {
> +		ref->object = kzalloc(ref->size, GFP_KERNEL);
> +		if (unlikely(ref->object == NULL)) {
>   			ret = -ENOMEM;
> -			goto out_err;
> +			goto error_unlock;
>   		}
> -
> -		ref->object = item->object;
>   		ret = ref->init(ref);
>   		if (unlikely(ret != 0))
> -			goto out_err;
> +			goto error_free;
>   
> +		item->object = ref->object;
> +	} else {
> +		ref->object = item->object;
>   	}
> +
>   	++item->refcount;
> -	ref->object = item->object;
>   	mutex_unlock(&item->mutex);
>   	return 0;
> -out_err:
> +
> +error_free:
> +	kfree(ref->object);
> +	ref->object = NULL;
> +error_unlock:
>   	mutex_unlock(&item->mutex);
> -	item->object = NULL;
>   	return ret;
>   }
>   EXPORT_SYMBOL(drm_global_item_ref);


_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

* Re: [PATCH v3] drm: modify drm_global_item_ref to avoid two times of writing ref->object
  2016-09-08  7:22   ` Huang Rui
@ 2016-09-08  7:35     ` Chris Wilson
  2016-09-08  7:43       ` Christian König
  0 siblings, 1 reply; 7+ messages in thread
From: Chris Wilson @ 2016-09-08  7:35 UTC (permalink / raw)
  To: Huang Rui
  Cc: Daniel Vetter, amd-gfx, dri-devel, Deucher, Alexander, Wang, Ken,
	Koenig, Christian

On Thu, Sep 08, 2016 at 03:22:48PM +0800, Huang Rui wrote:
> On Thu, Sep 08, 2016 at 02:36:06PM +0800, Chris Wilson wrote:
> > On Wed, Sep 07, 2016 at 10:07:57PM -0400, 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 V2 -> V3:
> > > - Use duplicate mutex release to avoid "goto" in non-error patch.
> > > - Rename error label
> > > 
> > > ---
> > >  drivers/gpu/drm/drm_global.c | 24 ++++++++++++++----------
> > >  1 file changed, 14 insertions(+), 10 deletions(-)
> > > 
> > > diff --git a/drivers/gpu/drm/drm_global.c b/drivers/gpu/drm/drm_global.c
> > > index 3d2e91c..b404287 100644
> > > --- a/drivers/gpu/drm/drm_global.c
> > > +++ b/drivers/gpu/drm/drm_global.c
> > > @@ -65,30 +65,34 @@ void drm_global_release(void)
> > >  
> > >  int drm_global_item_ref(struct drm_global_reference *ref)
> > >  {
> > > -	int ret;
> > > +	int ret = 0;
> > >  	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)) {
> > > +		ref->object = kzalloc(ref->size, GFP_KERNEL);
> > 
> > So the item refcount is 0, we operate on ref, whereas previous we
> > inspected item and operated on item. Not an improvement.
> 
> Hmm, when item refcount is 0, we operate on ref to create object and init
> it for item, so although item->object and ref->object here should point the
> same thing, but we should alloc and init ref firstly and pass the
> ref->object address to item (item actually points a static area). This make
> the code logic more clearly and readable. So I updated it to "ref" here.
> :-)

The object is owned by item. ref is just a pointer to it.
-Chris

-- 
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

* Re: [PATCH v3] drm: modify drm_global_item_ref to avoid two times of writing ref->object
  2016-09-08  7:35     ` Chris Wilson
@ 2016-09-08  7:43       ` Christian König
  2016-09-08  8:08         ` Chris Wilson
  0 siblings, 1 reply; 7+ messages in thread
From: Christian König @ 2016-09-08  7:43 UTC (permalink / raw)
  To: Chris Wilson, Huang Rui, Daniel Vetter, dri-devel, amd-gfx,
	Deucher, Alexander, Wang, Ken

Am 08.09.2016 um 09:35 schrieb Chris Wilson:
> On Thu, Sep 08, 2016 at 03:22:48PM +0800, Huang Rui wrote:
>> On Thu, Sep 08, 2016 at 02:36:06PM +0800, Chris Wilson wrote:
>>> On Wed, Sep 07, 2016 at 10:07:57PM -0400, 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 V2 -> V3:
>>>> - Use duplicate mutex release to avoid "goto" in non-error patch.
>>>> - Rename error label
>>>>
>>>> ---
>>>>   drivers/gpu/drm/drm_global.c | 24 ++++++++++++++----------
>>>>   1 file changed, 14 insertions(+), 10 deletions(-)
>>>>
>>>> diff --git a/drivers/gpu/drm/drm_global.c b/drivers/gpu/drm/drm_global.c
>>>> index 3d2e91c..b404287 100644
>>>> --- a/drivers/gpu/drm/drm_global.c
>>>> +++ b/drivers/gpu/drm/drm_global.c
>>>> @@ -65,30 +65,34 @@ void drm_global_release(void)
>>>>   
>>>>   int drm_global_item_ref(struct drm_global_reference *ref)
>>>>   {
>>>> -	int ret;
>>>> +	int ret = 0;
>>>>   	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)) {
>>>> +		ref->object = kzalloc(ref->size, GFP_KERNEL);
>>> So the item refcount is 0, we operate on ref, whereas previous we
>>> inspected item and operated on item. Not an improvement.
>> Hmm, when item refcount is 0, we operate on ref to create object and init
>> it for item, so although item->object and ref->object here should point the
>> same thing, but we should alloc and init ref firstly and pass the
>> ref->object address to item (item actually points a static area). This make
>> the code logic more clearly and readable. So I updated it to "ref" here.
>> :-)
> The object is owned by item. ref is just a pointer to it.

Yeah, so what? We initialized ref->item when the refcount was 0 before 
as well.

Why not create the reference first and initialize the item later on?

Regards,
Christian.

> -Chris
>

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

* Re: [PATCH v3] drm: modify drm_global_item_ref to avoid two times of writing ref->object
  2016-09-08  7:43       ` Christian König
@ 2016-09-08  8:08         ` Chris Wilson
  0 siblings, 0 replies; 7+ messages in thread
From: Chris Wilson @ 2016-09-08  8:08 UTC (permalink / raw)
  To: Christian König
  Cc: Daniel Vetter, amd-gfx, Huang Rui, dri-devel, Deucher, Alexander,
	Wang, Ken

On Thu, Sep 08, 2016 at 09:43:52AM +0200, Christian König wrote:
> Am 08.09.2016 um 09:35 schrieb Chris Wilson:
> >On Thu, Sep 08, 2016 at 03:22:48PM +0800, Huang Rui wrote:
> >>On Thu, Sep 08, 2016 at 02:36:06PM +0800, Chris Wilson wrote:
> >>>On Wed, Sep 07, 2016 at 10:07:57PM -0400, 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 V2 -> V3:
> >>>>- Use duplicate mutex release to avoid "goto" in non-error patch.
> >>>>- Rename error label
> >>>>
> >>>>---
> >>>>  drivers/gpu/drm/drm_global.c | 24 ++++++++++++++----------
> >>>>  1 file changed, 14 insertions(+), 10 deletions(-)
> >>>>
> >>>>diff --git a/drivers/gpu/drm/drm_global.c b/drivers/gpu/drm/drm_global.c
> >>>>index 3d2e91c..b404287 100644
> >>>>--- a/drivers/gpu/drm/drm_global.c
> >>>>+++ b/drivers/gpu/drm/drm_global.c
> >>>>@@ -65,30 +65,34 @@ void drm_global_release(void)
> >>>>  int drm_global_item_ref(struct drm_global_reference *ref)
> >>>>  {
> >>>>-	int ret;
> >>>>+	int ret = 0;
> >>>>  	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)) {
> >>>>+		ref->object = kzalloc(ref->size, GFP_KERNEL);
> >>>So the item refcount is 0, we operate on ref, whereas previous we
> >>>inspected item and operated on item. Not an improvement.
> >>Hmm, when item refcount is 0, we operate on ref to create object and init
> >>it for item, so although item->object and ref->object here should point the
> >>same thing, but we should alloc and init ref firstly and pass the
> >>ref->object address to item (item actually points a static area). This make
> >>the code logic more clearly and readable. So I updated it to "ref" here.
> >>:-)
> >The object is owned by item. ref is just a pointer to it.
> 
> Yeah, so what? We initialized ref->item when the refcount was 0
> before as well.
> 
> Why not create the reference first and initialize the item later on?

More to the point, there is a genuine bug in the code. It happens to be
fixed by this patch, but not purported to be. There is also a leak of
item->object, and the globals conflict between drivers. If radeon
instantiates the DRM_GLOBAL_TTM_MEM, nouveau bumps the reference and
radeon subsequently unloaded, when nouveau unloads it will crash due to
executing a random address. As a singleton class for ttm, it could do
with a revamp.
-Chris

-- 
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

end of thread, other threads:[~2016-09-08  8:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-08  2:07 [PATCH v3] drm: modify drm_global_item_ref to avoid two times of writing ref->object Huang Rui
2016-09-08  6:36 ` Chris Wilson
2016-09-08  7:22   ` Huang Rui
2016-09-08  7:35     ` Chris Wilson
2016-09-08  7:43       ` Christian König
2016-09-08  8:08         ` Chris Wilson
2016-09-08  7:26 ` Christian König

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.