Skip to contentSkip to conversation

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

PATCH Superseded Huang Rui 1 participant 2 messages Sep 7, 2016 1 message not in the archive

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Huang Rui

author
#0 lore raw

PATCH v2 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.

---

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(-)
drivers/gpu/drm/drm_global.c +13 -12
index 3d2e91c..6a63808 100644
@@ -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);
Signature · 6 lines
1.9.1

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

A message that is not in the archive (sent off-list or not yet received): 1473218799-6890-1-git-send-email-ray.huang-5C7GfCeVMHo@public.gmane.org

author
#2 lore raw

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

Thanks,
Rui

Quoted 68 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.

---

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(-)
drivers/gpu/drm/drm_global.c +13 -12
index 3d2e91c..6a63808 100644
@@ -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);
Signature · 1 line
1.9.1

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