Skip to contentSkip to conversation

drm: modify drm_global_item_ref to avoid two times of writing ref->object

PATCH Superseded Huang Rui 3 participants 3 messages Sep 5, 2016 – Sep 7, 2016

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Huang Rui

author
#0 lore raw

PATCH 1/1 drm: modify drm_global_item_ref to avoid two times of writing ref->object

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.

---
 drivers/gpu/drm/drm_global.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/gpu/drm/drm_global.c +7 -7
index 3d2e91c..3abe738 100644
@@ -70,25 +70,25 @@ int drm_global_item_ref(struct drm_global_reference *ref)
 
 	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;
 		}
-
-		ref->object = item->object;
 		ret = ref->init(ref);
 		if (unlikely(ret != 0))
 			goto out_err;
 
-	}
+		item->object = ref->object;
+	} else
+		ref->object = item->object;
+
 	++item->refcount;
-	ref->object = item->object;
 	mutex_unlock(&item->mutex);
 	return 0;
 out_err:
 	mutex_unlock(&item->mutex);
-	item->object = NULL;
+	ref->object = NULL;
 	return ret;
 }
 EXPORT_SYMBOL(drm_global_item_ref);
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#1 lore raw
Quoted 27 lines · 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.

---
 drivers/gpu/drm/drm_global.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/gpu/drm/drm_global.c +2 -3
index 3d2e91c..3abe738 100644
@@ -70,25 +70,25 @@ int drm_global_item_ref(struct drm_global_reference *ref)
 
        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;
                }
-

drivers/gpu/drm/drm_global.c int drm_global_item_ref(struct drm_global_reference *ref) · line 85

Quoted 10 lines
drivers/gpu/drm/drm_global.c +4 -2
-               ref->object = item->object;
                ret = ref->init(ref);
                if (unlikely(ret != 0))
                        goto out_err;
 
-       }
+               item->object = ref->object;
+       } else
+               ref->object = item->object;
+

You should add the braces to all blocks if they're present in any block.

drivers/gpu/drm/drm_global.c int drm_global_item_ref(struct drm_global_reference *ref) · line 91

Quoted 8 lines
drivers/gpu/drm/drm_global.c +1 -2
        ++item->refcount;
-       ref->object = item->object;
        mutex_unlock(&item->mutex);
        return 0;
 out_err:
        mutex_unlock(&item->mutex);
-       item->object = NULL;
+       ref->object = NULL;

It seems like you're leaking memory here. Do you need a kfree(ref->object) before this?

I think perhaps you can simplify (or maybe streamline is a better word) things better by unifying the unlock and return. Something like:

drivers/gpu/drm/drm_global.c +13 -12
index 3d2e91c..5b255e4 100644
@@ -65,30 +65,31 @@ 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 out;
                }
-
-               ref->object = item->object;
                ret = ref->init(ref);
                if (unlikely(ret != 0))
                        goto out_err;
-
+       } else {
+               ref->object = item->object;
        }
-       ++item->refcount;
-       ref->object = item->object;
-       mutex_unlock(&item->mutex);
-       return 0;
+
+       item->refcount++;
+       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);
Quoted 10 lines
drivers/gpu/drm/drm_global.c +0 -1
        return ret;
 }
 EXPORT_SYMBOL(drm_global_item_ref);
--

2.7.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#2 lore raw
Quoted 50 lines · Sean Paul
Quoted 27 lines · 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.

---
 drivers/gpu/drm/drm_global.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/gpu/drm/drm_global.c +2 -3
index 3d2e91c..3abe738 100644
@@ -70,25 +70,25 @@ int drm_global_item_ref(struct drm_global_reference *ref)
 
        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;
                }
-

drivers/gpu/drm/drm_global.c int drm_global_item_ref(struct drm_global_reference *ref) · line 85

Quoted 10 lines
drivers/gpu/drm/drm_global.c +4 -2
-               ref->object = item->object;
                ret = ref->init(ref);
                if (unlikely(ret != 0))
                        goto out_err;
 
-       }
+               item->object = ref->object;
+       } else
+               ref->object = item->object;
+

You should add the braces to all blocks if they're present in any block.

drivers/gpu/drm/drm_global.c void drm_global_release(void)

Quoted 8 lines
drivers/gpu/drm/drm_global.c +1 -2
        ++item->refcount;
-       ref->object = item->object;
        mutex_unlock(&item->mutex);
        return 0;
 out_err:
        mutex_unlock(&item->mutex);
-       item->object = NULL;
+       ref->object = NULL;
Quoted 2 lines

It seems like you're leaking memory here. Do you need a kfree(ref->object) before this?

Yep, right. The original code seems to missed a kfree exceptional handle.

Quoted 4 lines

I think perhaps you can simplify (or maybe streamline is a better word) things better by unifying the unlock and return. Something like:

Thanks, will update it in V2.

Rui
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel